forked from hugowetterberg/services_deprecated
-
Notifications
You must be signed in to change notification settings - Fork 13
/
services.module
468 lines (424 loc) · 13.1 KB
/
services.module
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
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
<?php
// $Id: services.module,v 1.8.2.88.2.6 2009/09/05 13:57:58 marcingy Exp $
/**
* @file
* Provides a generic but powerful API for web services.
*/
/**
* Implementation of hook_help().
*/
function services_help($path, $arg) {
$output = NULL;
switch ($path) {
case 'admin/help#services':
$output = '<p>'. t('Visit the <a href="@handbook_url">Services Handbook</a> for help and information.', array('@handbook_url' => 'http://drupal.org/node/109782')) .'</p>';
break;
case 'admin/build/services':
$output = '<p>'. t('Services are collections of methods available to remote applications. They are defined in modules, and may be accessed in a number of ways through server modules. Visit the <a href="@handbook_url">Services Handbook</a> for help and information.', array('@handbook_url' => 'http://drupal.org/node/109782')) .'</p>';
$output .= '<p>'. t('All enabled services and methods are shown. Click on any method to view information or test.') .'</p>';
break;
}
return $output;
}
/**
* Implementation of hook_perm().
*/
function services_perm() {
return array(
'administer services',
// File resource permissions
'get any binary files',
'get own binary files',
'save file information',
// System resource permissions
'get a system variable',
'set a system varaible',
);
}
/**
* Implementation of hook_hook_info().
*/
function services_hook_info() {
$hooks['services_resources'] = array(
'group' => 'services',
);
return $hooks;
}
/**
* Implementation of hook_menu().
*/
function services_menu() {
$base = array(
'access arguments' => array('administer services'),
'file' => 'services.admin.inc',
);
$items['admin/build/services'] = array(
'title' => 'Services',
'description' => 'Manage how external applications communicates with Drupal.',
'page callback' => 'services_list_endpoint',
) + $base;
$items['admin/build/services/list'] = array(
'title' => 'List',
'page callback' => 'services_list_endpoint',
'type' => MENU_DEFAULT_LOCAL_TASK,
'weight' => -10,
) + $base;
$items['admin/build/services/add'] = array(
'title' => 'Add endpoint',
'page callback' => 'services_add_endpoint',
'type' => MENU_LOCAL_TASK,
) + $base;
$items['admin/build/services/%services_endpoint/edit'] = array(
'title' => 'Edit endpoint',
'page callback' => 'services_edit_endpoint',
'page arguments' => array(3),
'type' => MENU_LOCAL_TASK,
) + $base;
$items['admin/build/services/%services_endpoint/authentication'] = array(
'title' => 'Authentication',
'page callback' => 'services_edit_endpoint_authentication',
'page arguments' => array(3),
'type' => MENU_LOCAL_TASK,
'weight' => 5,
) + $base;
$items['admin/build/services/%services_endpoint/resources'] = array(
'title' => 'Resources',
'page callback' => 'services_edit_endpoint_resources',
'page arguments' => array(3),
'type' => MENU_LOCAL_TASK,
'weight' => 10,
) + $base;
$items['admin/build/services/%services_endpoint/export'] = array(
'title' => 'Export endpoint',
'page callback' => 'drupal_get_form',
'page arguments' => array('services_export_endpoint', 3),
'type' => MENU_LOCAL_TASK,
'weight' => 20,
) + $base;
$items['admin/build/services/%services_endpoint/delete'] = array(
'title' => 'Delete endpoint',
'page callback' => 'drupal_get_form',
'page arguments' => array('services_delete_confirm_endpoint', 3),
'type' => MENU_CALLBACK,
) + $base;
$items['admin/build/services/%services_endpoint/disable'] = array(
'page callback' => 'services_disable_endpoint',
'page arguments' => array(3),
'type' => MENU_CALLBACK,
) + $base;
$items['admin/build/services/%services_endpoint/enable'] = array(
'page callback' => 'services_enable_endpoint',
'page arguments' => array(3),
'type' => MENU_CALLBACK,
) + $base;
$items['admin/build/services/ahah/security-options'] = array(
'page callback' => '_services_ahah_security_options',
'type' => MENU_CALLBACK,
) + $base;
$items['crossdomain.xml'] = array(
'access callback' => 'services_access_menu',
'page callback' => 'services_crossdomain_xml',
'type' => MENU_CALLBACK,
);
// Add menu items for the different endpoints
$endpoints = services_endpoint_load_all();
foreach ($endpoints as $endpoint) {
if (!$endpoint->disabled) {
$items[$endpoint->path] = array(
'title' => 'Services endpoint',
'access callback' => 'services_access_menu',
'page callback' => 'services_endpoint_callback',
'page arguments' => array($endpoint->name),
'type' => MENU_CALLBACK,
);
}
}
return $items;
}
/**
* Access callback that always returns TRUE.
*
* This callback is necessary for services like login and logout that should
* always be wide open and accessible.
*
* *** USE THIS WITH GREAT CAUTION ***
*
* If you think you need it you are almost certainly wrong.
*/
function services_access_menu() {
return TRUE;
}
/**
* Implementation of hook_theme().
*/
function services_theme() {
return array(
'services_endpoint_index' => array(
'template' => 'services_endpoint_index',
'arguments' => array('endpoints' => NULL),
),
);
}
/**
* Returns information about the installed server modules on the system.
*
* @return array
* An associative array keyed after module name containing information about
* the installed server implementations.
*/
function services_get_servers() {
static $servers;
if (!$servers) {
$servers = array();
foreach (module_implements('server_info') as $module) {
$servers[$module] = call_user_func($module . '_server_info');
}
}
return $servers;
}
/**
* Menu system page callback for server endpoints.
*
* @param string $endpoint
* The endpoint name.
* @return void
*/
function services_endpoint_callback($endpoint_name) {
module_load_include('runtime.inc', 'services');
$endpoint = services_endpoint_load($endpoint_name);
$server = $endpoint->server;
if (function_exists($server . '_server')) {
// call the server
services_set_server_info_from_array(array(
'module' => $server,
'endpoint' => $endpoint_name,
'endpoint_path' => $endpoint->path,
'drupal_path' => getcwd(),
));
print call_user_func($server . '_server');
// Do not let this output
module_invoke_all('exit');
exit;
}
// return 404 if the server doesn't exist
drupal_not_found();
}
/**
* Callback for crossdomain.xml
*/
function services_crossdomain_xml() {
$output = '<!DOCTYPE cross-domain-policy SYSTEM "http://www.macromedia.com/xml/dtds/cross-domain-policy.dtd">' . "\n";
$output .= '<cross-domain-policy>' . "\n";
$domains = module_invoke_all('services_crossdomain_domain_policy');
drupal_alter('services_crossdomain_domain_policy', $domains);
foreach ($domains as $domain => $info) {
$output .= ' <allow-access-from domain="' . check_plain($domain) . '" />' . "\n";
if ($info['subdomain_wildcard']) {
$output .= ' <allow-access-from domain="*.' . check_plain($domain) . '" />' . "\n";
}
}
$output .= '</cross-domain-policy>';
services_xml_output($output);
}
/**
* Implementation of hook_services_crossdomain_domain_policy().
*/
function services_services_crossdomain_domain_policy() {
// Allow our own domain and it's subdomains
return array(
$_SERVER['HTTP_HOST'] => array(
'subdomain_wildcard' => TRUE,
),
);
}
/**
* Helper function for services_crossdomain_xml().
* Outputs the necessary http headers and xml processing instruction then exits.
*
* @param string $xml
* The xml document to print.
* @return void
* This function never returns, it always exits.
*/
function services_xml_output($xml) {
$xml = '<?xml version="1.0" encoding="UTF-8"?>' . "\n" . $xml;
header('Connection: close');
header('Content-Length: ' . strlen($xml));
header('Content-Type: text/xml');
header('Date: ' . date('r'));
echo $xml;
exit;
}
/**
* Create a new endpoint with defaults appropriately set from schema.
*
* @return stdClass
* An endpoint initialized with the default values.
*/
function services_endpoint_new() {
ctools_include('export');
return ctools_export_new_object('services_endpoint');
}
/**
* Load a single endpoint.
*
* @param string $name
* The name of the endpoint.
* @return stdClass
* The endpoint configuration.
*/
function services_endpoint_load($name) {
ctools_include('export');
$result = ctools_export_load_object('services_endpoint', 'names', array($name));
if (isset($result[$name])) {
return $result[$name];
}
else {
return FALSE;
}
}
/**
* Load all endpoints.
*
* @return array
* Array of endpoint objects keyed by endpoint names.
*/
function services_endpoint_load_all() {
ctools_include('export');
return ctools_export_load_object('services_endpoint');
}
/**
* Saves an endpoint in the database.
*
* @return void
*/
function services_endpoint_save($endpoint) {
$update = (isset($endpoint->eid)) ? array('eid') : array();
drupal_write_record('services_endpoint', $endpoint, $update);
menu_rebuild();
cache_clear_all('services:' . $endpoint->name . ':', 'cache', TRUE);
}
/**
* Remove an endpoint.
*
* @return void
*/
function services_endpoint_delete($endpoint) {
db_query("DELETE FROM {services_endpoint} WHERE name = '%s' AND eid = %d", $endpoint->name, $endpoint->eid);
menu_rebuild();
cache_clear_all('services:' . $endpoint->name . ':', 'cache', TRUE);
}
/**
* Export an endpoint.
*
* @return string
*/
function services_endpoint_export($endpoint, $indent = '') {
ctools_include('export');
$output = ctools_export_object('services_endpoint', $endpoint, $indent);
return $output;
}
/**
* Lists all available endpoints.
*
* @return array
*/
function services_endpoint_list() {
$return = array();
$endpoints = services_endpoint_load_all();
foreach ($endpoints as $endpoint) {
$return[$endpoint->name] = $endpoint->title;
}
return $return;
}
/**
* Gets all resource definitions.
*
* @param string $endpoint_name
* Optional. The endpoint endpoint that's being used.
* @return array
* An array containing all resources.
*/
function services_get_resources($endpoint_name = '') {
$cache_key = 'services:' . $endpoint_name . ':resources';
$resources = array();
if (($cache = cache_get($cache_key)) && isset($cache->data)) {
$resources = $cache->data;
}
else {
module_load_include('resource_build.inc', 'services');
$resources = _services_build_resources($endpoint_name);
cache_set($cache_key, $resources);
}
return $resources;
}
/**
* Implementation of hook_services_resources().
*/
function services_services_resources() {
module_load_include('resource_build.inc', 'services');
// Return resources representing legacy services
return array_merge(_services_core_resources(), _services_legacy_services_as_resources());
}
/**
* Returns all the controller names for a endpoint.
*
* @param string $endpoint
* The endpoint that should be used.
* @return array
* Either a non associative array containing all controller names. Or, if
* $key_by_resource was set to TRUE, a associative array where the resource
* name is the key and the value is a non-associative array containing the
* resource's controller names.
*/
function services_controllers_list($endpoint) {
$controllers = array();
$ops = array('actions', 'relationships', 'targeted actions');
$resources = services_get_resources($endpoint);
foreach ($resources as $resource_name => $res) {
// Get all basic operations
foreach (array('create', 'retrieve', 'update', 'delete', 'index') as $op) {
if (isset($res[$op])) {
$controllers[] = $resource_name . '.' . $op;
}
}
// Handle extended operatios
foreach ($ops as $op) {
if (isset($res[$op])) {
foreach ($res[$op] as $name => $def) {
// Append prefix if it isn't empty
$controllers[] = $resource_name . '.' . $name;
}
}
}
}
return $controllers;
}
/**
* Returns the requested controller.
*
* @param string $name
* The name of the controller in the format: {resource}.{name} or
* {resource}.{operation}. Examples: "node.retrieve", "system.getVariable".
* @param string $endpoint
* The endpoint that should be used.
*/
function services_controller_get($name, $endpoint) {
list($resource_name, $method) = explode('.', $name);
$ops = array('actions', 'relationships', 'targeted actions');
$resources = services_get_resources($endpoint);
if (isset($resources[$resource_name])) {
$res = $resources[$resource_name];
if (isset($res[$method])) {
return $res[$method];
}
else {
// Handle extended operatios
foreach ($ops as $op) {
if (isset($res[$op]) && isset($res[$op][$method])) {
return $res[$op][$method];
}
}
}
}
}