Repository navigation
Expand file tree
/
Copy pathdrupal.js
More file actions
441 lines (356 loc) · 10.7 KB
/
Copy pathdrupal.js
File metadata and controls
441 lines (356 loc) · 10.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
/**
* @module drupal
*
* Yet another Drupal adapter for Titanium.
* This dependency-free module is adapted to the new CSRF token requirement in Services 3.4.
*
* Run the included Jasmine spec with TiShadow.
*
* by Joe Beuckman 2013
* Github project link: https://github.com/jbeuckm/TitaniumDrupalAdapter
*/
var REST_PATH;
try {
Ti.include("drupal/config.js");
REST_PATH = SITE_ROOT + SERVICES_ENDPOINT + '/';
}
catch (e) {
Ti.API.error("************************ DRUPAL CONFIG NOT FOUND *******************************");
Ti.API.error("Please edit 'lib/drupal/config.js.example' and rename to 'lib/drupal/config.js'.");
Ti.API.error("************************ DRUPAL CONFIG NOT FOUND *******************************");
}
/**
* Prepare to connect to a (different) Drupal server and Services 3.4 module.
*/
function setRestPath(root, endpoint) {
SITE_ROOT = root;
SERVICES_ENDPOINT = endpoint;
REST_PATH = root + endpoint + '/';
}
/**
* Establish a session (or return the stored session).
*/
function systemConnect(success, failure) {
var xhr = Ti.Network.createHTTPClient();
var url = REST_PATH + 'system/connect';
Ti.API.debug("POSTing to url "+url);
xhr.open("POST", url);
xhr.onload = function(e) {
if (xhr.status == 200) {
var response = xhr.responseText;
var responseData = JSON.parse(response);
Ti.API.debug("system.connect session "+responseData.sessid);
Ti.API.debug('system.connect user '+responseData.user.uid);
var cookie = responseData.session_name+'='+responseData.sessid;
Ti.App.Properties.setString("Drupal-Cookie", cookie);
getCsrfToken(function(token){
success(responseData);
},
function(err){
failure(err);
});
}
else {
failure(xhr.responseText);
}
};
xhr.onerror = function(e) {
Ti.API.error("There was an error calling systemConnect: ");
Ti.API.error(e);
// since systemConnect failed, will need a new csrf and session
Ti.App.Properties.setString("X-CSRF-Token", null);
Ti.App.Properties.setString("Drupal-Cookie", null);
failure(e);
};
xhr.clearCookies(SITE_ROOT);
xhr.send();
}
/**
* Retrieve the new Services security token identifying this session with this device.
*/
function getCsrfToken(success, failure) {
var existingToken = Ti.App.Properties.getString("X-CSRF-Token");
if (existingToken) {
success(existingToken);
return;
}
var xhr = Ti.Network.createHTTPClient();
xhr.onload = function() {
Ti.App.Properties.setString("X-CSRF-Token", xhr.responseText);
Ti.API.info('got new CSRF token ' + xhr.responseText);
success(xhr.responseText);
};
xhr.onerror = function(err) {
Ti.API.error("error getting CSRF token:");
Ti.API.error(err);
failure(err);
};
var tokenPath = SITE_ROOT + 'services/session/token';
xhr.open("GET", tokenPath);
var cookie = Ti.App.Properties.getString("Drupal-Cookie");
xhr.setRequestHeader("Cookie", cookie);
xhr.send();
}
/**
* Construct and fire arbitrary requests to the connected Drupal server.
* @TODO: encode and append url params here for GET requests.
* Config properties:
*
* httpMethod: GET, POST, PUT, DELETE, etc.
*
* params: An object to be sent to the server
*
* servicePath: Path to the resource including any url parameters like resource id
*
* contentType: String to send as "Content-Type" HTTP header
*
* trace (boolean): If true, echo the request summary with Ti.API.trace()
*/
function makeAuthenticatedRequest(config, success, failure, headers) {
var trace = "makeAuthenticatedRequest()\n";
var url = REST_PATH + config.servicePath;
var xhr = Titanium.Network.createHTTPClient();
trace += config.httpMethod+' '+url+"\n";
// optionally override timeout
if (config.timeout) {
xhr.timeout = config.timeout;
}
xhr.open(config.httpMethod, url);
xhr.onerror = function(e) {
Ti.API.error(JSON.stringify(e));
Ti.API.error('FAILED REQUEST:');
Ti.API.error(trace);
Ti.API.error(config.params);
failure(e);
};
xhr.onload = function() {
if (xhr.status == 200) {
var responseData = JSON.parse(xhr.responseText);
success(responseData);
}
else {
Ti.API.trace('makeAuthReq returned with status '+xhr.status);
failure(xhr.responseText);
}
};
if (!config.skipCookie) {
var cookie = Ti.App.Properties.getString("Drupal-Cookie");
xhr.setRequestHeader("Cookie", cookie);
trace += "Cookie: " + cookie + "\n";
}
if (!config.skipCsrfToken) {
var token = Ti.App.Properties.getString("X-CSRF-Token");
xhr.setRequestHeader("X-CSRF-Token", token);
trace += "X-CSRF-Token: " + token + "\n";
}
xhr.setRequestHeader("Accept", "application/json");
if (config.contentType) {
xhr.setRequestHeader("Content-Type", config.contentType);
trace += "Content-Type: " + config.contentType+"\n";
}
// add optional headers
if (headers) {
for (var key in headers) {
xhr.setRequestHeader(key, headers[key]);
}
}
// optionally output a summary of the request
if (config.trace) {
Ti.API.trace(trace);
Ti.API.trace(config.params);
}
xhr.send(config.params);
}
/**
* Attempt to register a new account. user object must include name, pass, mail properties.
*/
function createAccount(user, success, failure, headers) {
Ti.API.info('Registering new user: '+JSON.stringify(user)+" with cookie "+Ti.App.Properties.getString("Drupal-Cookie"));
makeAuthenticatedRequest({
httpMethod : 'POST',
servicePath : 'user/register.json',
contentType: 'application/json',
params: JSON.stringify(user)
},
//success
function(responseData){
Ti.API.info('registerNewUser SUCCESS');
success(responseData);
},
//fail
function(err){
Ti.API.error('registerNewUser FAIL');
failure(err);
},
headers
);
}
/**
* Construct and send the proper login request.
*/
function login(username, password, success, failure, headers) {
var user = {
username : username,
password : password
};
makeAuthenticatedRequest({
httpMethod : 'POST',
servicePath : 'user/login.json',
contentType: "application/json",
params: JSON.stringify(user)
},
function(responseData) {
var cookie = responseData.session_name+'='+responseData.sessid;
Ti.App.Properties.setString("Drupal-Cookie", cookie);
Ti.API.debug('login saving new cookie '+cookie);
// clear old token and get a new one for this session
Ti.App.Properties.setString("X-CSRF-Token", null);
getCsrfToken(function(token){
success(responseData.user);
},
function(err){
failure(err);
});
},
failure, headers);
};
/**
* Become user:uid=0
*/
function logout(success, failure, headers) {
makeAuthenticatedRequest({
httpMethod : 'POST',
servicePath : 'user/logout.json'
}, function(response) {
// session over - delete the token
Ti.App.Properties.setString("X-CSRF-Token", null);
success(response);
}, failure, headers);
}
/**
* Requires Services Views module
*/
function getView(viewName, args, success, failure, headers) {
makeAuthenticatedRequest({
servicePath : "views/" + viewName + ".json?" + encodeUrlString(args),
httpMethod : 'GET',
contentType : "application/json",
}, success, failure, headers);
}
/**
* Convenience function for GET requests
*/
function getResource(resourceName, args, success, failure, headers) {
makeAuthenticatedRequest({
servicePath : resourceName + ".json?" + encodeUrlString(args),
httpMethod : 'GET'
}, success, failure, headers);
}
/**
* Convenience function for POST requests
*/
function postResource(resourceName, object, success, failure, headers) {
makeAuthenticatedRequest({
servicePath : resourceName + ".json",
httpMethod : 'POST',
params : JSON.stringify(object)
}, success, failure, headers);
}
/**
* Convenience function for PUT requests
*/
function putResource(resourceName, object, success, failure, headers) {
makeAuthenticatedRequest({
servicePath : resourceName + ".json",
httpMethod : 'PUT',
contentType: 'application/json',
params : JSON.stringify(object)
}, success, failure, headers);
}
/**
* The fundamental act of Drupal
*/
function createNode(node, success, failure, headers) {
makeAuthenticatedRequest({
servicePath : "node",
httpMethod : "POST",
params : JSON.stringify({
node : node
})
}, function(response) {
Ti.API.trace(JSON.stringify(response));
success(response);
}, function(response) {
failure(response);
},
headers
);
}
/**
* Haven't tested this in a while but it was working in Services 3.2...
*/
function uploadFile(base64data, filename, filesize, success, failure, headers) {
var fileDescription = {
file : base64data,
filename : filename,
filesize : "" + filesize,
};
makeAuthenticatedRequest({
servicePath : "file.json",
httpMethod : "POST",
contentType : "application/x-www-form-urlencoded; charset=utf-8",
params : fileDescription
}, success, failure, headers);
}
/**
* Do the custom serialization for sending drupal views contextual filter settings
*
* @param {Object} obj
*/
function serializeDrupalViewsFilter(obj) {
var str = [];
for(var p in obj) {
if (obj[p] instanceof Array) {
for (var i=0, l=obj[p].length; i<l; i++) {
str.push(encodeURIComponent(p) + "=" + encodeURIComponent(obj[p][i]));
}
}
else {
str.push(encodeURIComponent(p) + "=" + encodeURIComponent(obj[p]));
}
}
return str.join("&");
}
/**
* Create a request string from an object of request parameters.
*/
function encodeUrlString(args) {
var parts = [];
for (var i in args) {
var arg = args[i];
parts.push(i + '=' + encodeURIComponent(arg));
}
var url = parts.join('&');
return url;
}
/**
* Create the basic field structure for uploading a field.
* This function is mainly here to document the "unique" way that field data must be constructed to work with Services.
*/
function basicField(obj) {
return {
und : [obj]
};
}
exports.setRestPath = setRestPath;
exports.systemConnect = systemConnect;
exports.makeAuthenticatedRequest = makeAuthenticatedRequest;
exports.createAccount = createAccount;
exports.login = login;
exports.logout = logout;
exports.basicField = basicField;
exports.serializeDrupalViewsFilter = serializeDrupalViewsFilter;
exports.getResource = getResource;
exports.postResource = postResource;
exports.putResource = putResource;
exports.getView = getView;