source: trunk/ws.php @ 12695

Last change on this file since 12695 was 12695, checked in by plg, 12 years ago

merge r12694 from branch 2.3 to trunk

bug 2529 fixed: Piwigo API can only handle the "rest" requestFormat.
If we try to search for another request format the only result is an
"Unknown request format" error for the client. This error happens when
always_populate_raw_post_data is On (which is not the default Apache
setting).

  • Property svn:eol-style set to LF
File size: 19.3 KB
Line 
1<?php
2// +-----------------------------------------------------------------------+
3// | Piwigo - a PHP based photo gallery                                    |
4// +-----------------------------------------------------------------------+
5// | Copyright(C) 2008-2011 Piwigo Team                  http://piwigo.org |
6// | Copyright(C) 2003-2008 PhpWebGallery Team    http://phpwebgallery.net |
7// | Copyright(C) 2002-2003 Pierrick LE GALL   http://le-gall.net/pierrick |
8// +-----------------------------------------------------------------------+
9// | This program is free software; you can redistribute it and/or modify  |
10// | it under the terms of the GNU General Public License as published by  |
11// | the Free Software Foundation                                          |
12// |                                                                       |
13// | This program is distributed in the hope that it will be useful, but   |
14// | WITHOUT ANY WARRANTY; without even the implied warranty of            |
15// | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU      |
16// | General Public License for more details.                              |
17// |                                                                       |
18// | You should have received a copy of the GNU General Public License     |
19// | along with this program; if not, write to the Free Software           |
20// | Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, |
21// | USA.                                                                  |
22// +-----------------------------------------------------------------------+
23
24define ('PHPWG_ROOT_PATH', './');
25
26include_once(PHPWG_ROOT_PATH.'include/common.inc.php');
27check_status(ACCESS_FREE);
28include_once(PHPWG_ROOT_PATH.'include/ws_core.inc.php');
29
30if ( !$conf['allow_web_services'] )
31{
32  page_forbidden('Web services are disabled');
33}
34
35/**
36 * event handler that registers standard methods with the web service
37 */
38function ws_addDefaultMethods( $arr )
39{
40  include_once(PHPWG_ROOT_PATH.'include/ws_functions.inc.php');
41  global $conf, $user;
42  $service = &$arr[0];
43  $service->addMethod('pwg.getVersion', 'ws_getVersion', null,
44      'retrieves the PWG version');
45         
46  $service->addMethod('pwg.getInfos', 'ws_getInfos', null,
47      'retrieves general informations');
48
49  $service->addMethod('pwg.caddie.add', 'ws_caddie_add',
50      array(
51        'image_id'=> array( 'flags'=>WS_PARAM_FORCE_ARRAY ),
52      ),
53      'adds the elements to the caddie');
54
55  $service->addMethod('pwg.categories.getImages', 'ws_categories_getImages',
56      array(
57        'cat_id'=>array('default'=>0, 'flags'=>WS_PARAM_FORCE_ARRAY),
58        'recursive'=>array('default'=>false),
59        'per_page' => array('default'=>100, 'maxValue'=>$conf['ws_max_images_per_page']),
60        'page' => array('default'=>0),
61        'order' => array('default'=>null),
62        'f_min_rate' => array( 'default'=> null ),
63        'f_max_rate' => array( 'default'=> null ),
64        'f_min_hit' => array( 'default'=> null ),
65        'f_max_hit' => array( 'default'=> null ),
66        'f_min_date_available' => array( 'default'=> null ),
67        'f_max_date_available' => array( 'default'=> null ),
68        'f_min_date_created' => array( 'default'=> null ),
69        'f_max_date_created' => array( 'default'=> null ),
70        'f_min_ratio' => array( 'default'=> null ),
71        'f_max_ratio' => array( 'default'=> null ),
72        'f_with_thumbnail' => array( 'default'=> false ),
73      ),
74      'Returns elements for the corresponding categories.
75<br><b>cat_id</b> can be empty if <b>recursive</b> is true. Can be sent as an array.
76<br><b>order</b> comma separated fields for sorting (file,id, rating_score,...)'
77    );
78
79  $service->addMethod('pwg.categories.getList', 'ws_categories_getList',
80      array(
81        'cat_id' => array('default'=>0),
82        'recursive' => array('default'=>false),
83        'public' => array('default'=>false),
84        'tree_output' => array('default'=>false),
85        'fullname' => array('default'=>false),
86      ),
87      'retrieves a list of categories (tree_output option only compatible with json/php output format' );
88
89  $service->addMethod('pwg.images.addComment', 'ws_images_addComment',
90      array(
91        'image_id' => array(),
92        'author' => array( 'default' => is_a_guest()? 'guest':$user['username']),
93        'content' => array(),
94        'key' => array(),
95      ),
96      'add a comment to an image' );
97
98  $service->addMethod('pwg.images.getInfo', 'ws_images_getInfo',
99      array(
100        'image_id' => array(),
101        'comments_page' => array('default'=>0 ),
102        'comments_per_page' => array(
103              'default' => $conf['nb_comment_page'],
104              'maxValue' => 2*$conf['nb_comment_page'],
105            ),
106      ),
107      'retrieves information about the given photo' );
108
109  $service->addMethod('pwg.images.rate', 'ws_images_rate',
110      array(
111        'image_id' => array(),
112        'rate' =>     array(),
113      ),
114      'rate the image' );
115
116  $service->addMethod('pwg.images.search', 'ws_images_search',
117      array(
118        'query'=>array(),
119        'per_page' => array('default'=>100, 'maxValue'=>$conf['ws_max_images_per_page']),
120        'page' => array('default'=>0),
121        'order' => array('default'=>null),
122        'f_min_rate' => array( 'default'=> null ),
123        'f_max_rate' => array( 'default'=> null ),
124        'f_min_hit' => array( 'default'=> null ),
125        'f_max_hit' => array( 'default'=> null ),
126        'f_min_date_available' => array( 'default'=> null ),
127        'f_max_date_available' => array( 'default'=> null ),
128        'f_min_date_created' => array( 'default'=> null ),
129        'f_max_date_created' => array( 'default'=> null ),
130        'f_min_ratio' => array( 'default'=> null ),
131        'f_max_ratio' => array( 'default'=> null ),
132        'f_with_thumbnail' => array( 'default'=> false ),
133      ),
134      'Returns elements for the corresponding query search.'
135    );
136
137  $service->addMethod(
138    'pwg.images.setPrivacyLevel',
139    'ws_images_setPrivacyLevel',
140    array(
141      'image_id' => array('flags'=>WS_PARAM_FORCE_ARRAY),
142      'level' => array('maxValue'=>$conf['available_permission_levels']),
143      ),
144    'sets the privacy levels for the images (POST method only)'
145    );
146
147  $service->addMethod(
148    'pwg.images.setRank',
149    'ws_images_setRank',
150    array(
151      'image_id' => array(),
152      'category_id' => array(),
153      'rank' => array(),
154      ),
155    'sets the rank of a photo for a given album (POST method only, for admins)'
156    );
157
158
159  $service->addMethod('pwg.rates.delete', 'ws_rates_delete',
160    array(
161      'user_id' => array(),
162      'anonymous_id' => array( 'default'=>'' ),
163      ),
164    'deletes all rates for a user (POST method only, admins only)'
165    );
166   
167  $service->addMethod('pwg.session.getStatus', 'ws_session_getStatus', null, '' );
168  $service->addMethod('pwg.session.login', 'ws_session_login',
169    array('username', 'password'),
170    'POST method only' );
171  $service->addMethod('pwg.session.logout', 'ws_session_logout', null, '');
172
173  $service->addMethod('pwg.tags.getList', 'ws_tags_getList',
174    array('sort_by_counter' => array('default' =>false) ),
175    'retrieves a list of available tags');
176  $service->addMethod('pwg.tags.getImages', 'ws_tags_getImages',
177      array(
178        'tag_id'=>array('default'=>null, 'flags'=>WS_PARAM_FORCE_ARRAY ),
179        'tag_url_name'=>array('default'=>null, 'flags'=>WS_PARAM_FORCE_ARRAY ),
180        'tag_name'=>array('default'=>null, 'flags'=>WS_PARAM_FORCE_ARRAY ),
181        'tag_mode_and'=>array('default'=>false),
182        'per_page' => array('default'=>100, 'maxValue'=>$conf['ws_max_images_per_page']),
183        'page' => array('default'=>0),
184        'order' => array('default'=>null),
185        'f_min_rate' => array( 'default'=> null ),
186        'f_max_rate' => array( 'default'=> null ),
187        'f_min_hit' => array( 'default'=> null ),
188        'f_max_hit' => array( 'default'=> null ),
189        'f_min_date_available' => array( 'default'=> null ),
190        'f_max_date_available' => array( 'default'=> null ),
191        'f_min_date_created' => array( 'default'=> null ),
192        'f_max_date_created' => array( 'default'=> null ),
193        'f_min_ratio' => array( 'default'=> null ),
194        'f_max_ratio' => array( 'default'=> null ),
195        'f_with_thumbnail' => array( 'default'=> false ),
196      ),
197      'Returns elements for the corresponding tags. Note that tag_id, tag_url_name, tag_name an be arrays. Fill at least one of them. '
198    );
199
200  $service->addMethod(
201    'pwg.images.addChunk',
202    'ws_images_add_chunk',
203    array(
204      'data' => array(),
205      'original_sum' => array(),
206      'type' => array(),
207      'position' => array(),
208      ),
209    'POST method only. For admin only.'
210    );
211
212  $service->addMethod(
213    'pwg.images.addFile',
214    'ws_images_addFile',
215    array(
216      'image_id' => array(),
217      'type' => array(),
218      'sum' => array(),
219      ),
220    'Add or update a file for an existing photo. pwg.images.addChunk must have been called  before (maybe several times)'
221    );
222
223
224  $service->addMethod(
225    'pwg.images.add',
226    'ws_images_add',
227    array(
228      'file_sum' => array(),
229      'thumbnail_sum' => array(),
230      'high_sum' => array('default' => null),
231      'original_sum' => array(),
232      'original_filename' => array('default' => null),
233      'name' => array('default' => null),
234      'author' => array('default' => null),
235      'date_creation' => array('default' => null),
236      'comment' => array('default' => null),
237      'categories' => array('default' => null),
238      'tag_ids' => array('default' => null),
239      'level' => array(
240        'default' => 0,
241        'maxValue' => $conf['available_permission_levels']
242        ),
243      ),
244    'POST method only.
245<br><b>categories</b> is a string list "category_id[,rank];category_id[,rank]" The rank is optional and is equivalent to "auto" if not given.'
246    );
247
248  $service->addMethod(
249    'pwg.images.addSimple',
250    'ws_images_addSimple',
251    array(
252      'category' => array('default' => null),
253      'name' => array('default' => null),
254      'author' => array('default' => null),
255      'comment' => array('default' => null),
256      'level' => array(
257        'default' => 0,
258        'maxValue' => $conf['available_permission_levels']
259        ),
260      'tags' => array('default' => null),
261      'image_id' => array('default' => null),
262      ),
263    'POST method only.<br>Use the <b>image</b> field for uploading file.<br>Set the form encoding to "form-data"<br><b>category</b> is the numeric identifier of the destination category.<br>You can update an existing photo if you define an existing image_id.'
264    );
265
266  $service->addMethod(
267    'pwg.images.delete',
268    'ws_images_delete',
269    array(
270      'image_id'=>array('default'=>0),
271      'pwg_token' => array(),
272      ),
273    'Delete photos. You can give several image_ids, comma separated'
274    );
275
276  $service->addMethod(
277    'pwg.categories.getAdminList',
278    'ws_categories_getAdminList',
279    array(),
280    'administration method only'
281    );
282
283  $service->addMethod(
284    'pwg.categories.add',
285    'ws_categories_add',
286    array(
287      'name' => array(),
288      'parent' => array('default' => null),
289      ),
290    'administration method only'
291    );
292
293  $service->addMethod(
294    'pwg.categories.delete',
295    'ws_categories_delete',
296    array(
297      'category_id'=>array('default'=>0),
298      'pwg_token' => array(),
299      'photo_deletion_mode' => array('default' => 'delete_orphans'),
300      ),
301    'Delete categories. You can give several category_ids, comma separated.
302<br><b>photo_deletion_mode</b> can be "no_delete" (may create orphan photos), "delete_orphans" (default mode, only deletes photos linked to no other album) or "force_delete" (delete all photos, even those linked to other albums)'
303    );
304
305  $service->addMethod(
306    'pwg.categories.move',
307    'ws_categories_move',
308    array(
309      'category_id'=>array('default'=>0),
310      'parent'=>array('default'=>0),
311      'pwg_token' => array(),
312      ),
313    'Move categories. You can give several category_ids, comma separated. Set parent as 0 to move to gallery root. Only virtual categories can be moved.'
314    );
315
316  $service->addMethod(
317    'pwg.categories.setRepresentative',
318    'ws_categories_setRepresentative',
319    array(
320      'category_id'=>array('default'=>0),
321      'image_id'=>array('default'=>0),
322      ),
323    'Set the representative photo for an album. The photo doesn\'t have to belong to the album. POST method only. Administration method only.'
324    );
325
326  $service->addMethod(
327    'pwg.tags.getAdminList',
328    'ws_tags_getAdminList',
329    array(),
330    'administration method only'
331    );
332
333  $service->addMethod(
334    'pwg.tags.add',
335    'ws_tags_add',
336    array(
337      'name' => array(),
338      ),
339    'administration method only'
340    );
341
342  $service->addMethod(
343    'pwg.images.exist',
344    'ws_images_exist',
345    array(
346      'md5sum_list'=> array('default' => null),
347      'filename_list' => array('default' => null),
348      ),
349    'check existence of a photo list'
350    );
351
352  $service->addMethod(
353    'pwg.images.checkFiles',
354    'ws_images_checkFiles',
355    array(
356      'image_id' => array(),
357      'thumbnail_sum' => array('default' => null),
358      'file_sum' => array('default' => null),
359      'high_sum' => array('default' => null),
360      ),
361    'check if you have updated version of your files for a given photo, for each requested file type, the answer can be "missing", "equals" or "differs"'
362    );
363
364  $service->addMethod(
365    'pwg.images.checkUpload',
366    'ws_images_checkUpload',
367    null,
368    'check if Piwigo is ready for upload'
369    );
370
371  $service->addMethod(
372    'pwg.images.setInfo',
373    'ws_images_setInfo',
374    array(
375      'image_id' => array(),
376
377      'name' => array('default' => null),
378      'author' => array('default' => null),
379      'date_creation' => array('default' => null),
380      'comment' => array('default' => null),
381      'categories' => array('default' => null),
382      'tag_ids' => array('default' => null),
383      'level' => array(
384        'default' => null,
385        'maxValue' => $conf['available_permission_levels']
386        ),
387      'single_value_mode' => array('default' => 'fill_if_empty'),
388      'multiple_value_mode' => array('default' => 'append'),
389      ),
390    'POST method only. Admin only
391<br><b>categories</b> is a string list "category_id[,rank];category_id[,rank]" The rank is optional and is equivalent to "auto" if not given.
392<br><b>single_value_mode</b> can be "fill_if_empty" (only use the input value if the corresponding values is currently empty) or "replace" (overwrite any existing value) and applies to single values properties like name/author/date_creation/comment
393<br><b>multiple_value_mode</b> can be "append" (no change on existing values, add the new values) or "replace" and applies to multiple values properties like tag_ids/categories'
394    );
395
396  $service->addMethod(
397    'pwg.categories.setInfo',
398    'ws_categories_setInfo',
399    array(
400      'category_id' => array(),
401
402      'name' => array('default' => null),
403      'comment' => array('default' => null),
404      ),
405    'POST method only.'
406    );
407 
408  $service->addMethod(
409    'pwg.plugins.getList',
410    'ws_plugins_getList',
411    array(),
412    'get the list of plugin with id, name, version, state and description
413<br>administration status required'
414    );
415
416  $service->addMethod(
417    'pwg.plugins.performAction',
418    'ws_plugins_performAction',
419    array(
420      'action' => array(),
421      'plugin' => array(),
422      'pwg_token' => array(),
423      ),
424    'install/activate/deactivate/uninstall/delete a plugin
425<br>administration status required'
426    );
427
428  $service->addMethod(
429    'pwg.themes.performAction',
430    'ws_themes_performAction',
431    array(
432      'action' => array(),
433      'theme' => array(),
434      'pwg_token' => array(),
435      ),
436    'activate/deactivate/delete/set_default a theme<br>administration status required'
437    );
438
439  $service->addMethod(
440    'pwg.images.resizeThumbnail',
441    'ws_images_resizethumbnail',
442    array(
443      'image_id' => array('default' => null),
444      'image_path' => array('default' => null),
445      'maxwidth' => array('default' => $conf['upload_form_thumb_maxwidth']),
446      'maxheight' => array('default' => $conf['upload_form_thumb_maxheight']),
447      'quality' => array('default' => $conf['upload_form_thumb_quality']),
448      'crop' => array('default' => $conf['upload_form_thumb_crop']),
449      'follow_orientation' => array('default' => $conf['upload_form_thumb_follow_orientation']),
450      'library' => array('default' => $conf['graphics_library']),
451    ),
452    'Create/Regenerate thumbnails photo with given arguments.
453<br>One of arguments "image_id" or "image_path" must be sent.'
454  );
455
456  $service->addMethod(
457    'pwg.images.resizeWebsize',
458    'ws_images_resizewebsize',
459    array(
460      'image_id' => array(),
461      'maxwidth' => array('default' => $conf['upload_form_websize_maxwidth']),
462      'maxheight' => array('default' => $conf['upload_form_websize_maxheight']),
463      'quality' => array('default' => $conf['upload_form_websize_quality']),
464      'automatic_rotation' => array('default' => $conf['upload_form_automatic_rotation']),
465      'library' => array('default' => $conf['graphics_library']),
466    ),
467    'Regenerate websize photo with given arguments.'
468  );
469
470  $service->addMethod(
471    'pwg.extensions.update',
472    'ws_extensions_update',
473    array(
474      'type' => array(),
475      'id' => array(),
476      'revision'=> array(),
477      'pwg_token' => array(),
478    ),
479    'Update an extension. Webmaster only.
480<br>Parameter type must be "plugins", "languages" or "themes".'
481  );
482
483  $service->addMethod(
484    'pwg.extensions.ignoreUpdate',
485    'ws_extensions_ignoreupdate',
486    array(
487      'type' => array('default'=>null),
488      'id' => array('default'=>null),
489      'reset' => array('default'=>null),
490      'pwg_token' => array(),
491    ),
492    'Ignore an extension if it need update.
493<br>Parameter type must be "plugins", "languages" or "themes".
494<br>If reset parameter is true, all ignored extensions will be reinitilized.'
495  );
496
497  $service->addMethod(
498    'pwg.extensions.checkUpdates',
499    'ws_extensions_checkupdates',
500    array(),
501    'Check if piwigo or extensions are up to date.'
502  );
503}
504
505add_event_handler('ws_add_methods', 'ws_addDefaultMethods');
506
507
508add_event_handler('ws_invoke_allowed', 'ws_isInvokeAllowed', EVENT_HANDLER_PRIORITY_NEUTRAL, 3);
509
510$requestFormat = 'rest';
511$responseFormat = null;
512
513if ( isset($_GET['format']) )
514{
515  $responseFormat = $_GET['format'];
516}
517
518if ( !isset($responseFormat) and isset($requestFormat) )
519{
520  $responseFormat = $requestFormat;
521}
522
523$service = new PwgServer();
524
525if (!is_null($requestFormat))
526{
527  $handler = null;
528  switch ($requestFormat)
529  {
530    case 'rest':
531      include_once(PHPWG_ROOT_PATH.'include/ws_protocols/rest_handler.php');
532      $handler = new PwgRestRequestHandler();
533      break;
534  }
535  $service->setHandler($requestFormat, $handler);
536}
537
538if (!is_null($responseFormat))
539{
540  $encoder = null;
541  switch ($responseFormat)
542  {
543    case 'rest':
544      include_once(PHPWG_ROOT_PATH.'include/ws_protocols/rest_encoder.php');
545      $encoder = new PwgRestEncoder();
546      break;
547    case 'php':
548      include_once(PHPWG_ROOT_PATH.'include/ws_protocols/php_encoder.php');
549      $encoder = new PwgSerialPhpEncoder();
550      break;
551    case 'json':
552      include_once(PHPWG_ROOT_PATH.'include/ws_protocols/json_encoder.php');
553      $encoder = new PwgJsonEncoder();
554      break;
555    case 'xmlrpc':
556      include_once(PHPWG_ROOT_PATH.'include/ws_protocols/xmlrpc_encoder.php');
557      $encoder = new PwgXmlRpcEncoder();
558      break;
559  }
560  $service->setEncoder($responseFormat, $encoder);
561}
562
563set_make_full_url();
564$service->run();
565
566?>
Note: See TracBrowser for help on using the repository browser.