source: branches/2.3/ws.php @ 12722

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

feature 2531 added: pwg.images.add is able to generate web size + thumbnail
(remote client needs to set "resize" option to something else than 0). When
the "resize" is On, only the "file" must be send with pwg.images.addChunk.

  • 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('default' => null),
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      'resize' => array('default' => false),
244      ),
245    'POST method only.
246<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.'
247    );
248
249  $service->addMethod(
250    'pwg.images.addSimple',
251    'ws_images_addSimple',
252    array(
253      'category' => array('default' => null),
254      'name' => array('default' => null),
255      'author' => array('default' => null),
256      'comment' => array('default' => null),
257      'level' => array(
258        'default' => 0,
259        'maxValue' => $conf['available_permission_levels']
260        ),
261      'tags' => array('default' => null),
262      'image_id' => array('default' => null),
263      ),
264    '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.'
265    );
266
267  $service->addMethod(
268    'pwg.images.delete',
269    'ws_images_delete',
270    array(
271      'image_id'=>array('default'=>0),
272      'pwg_token' => array(),
273      ),
274    'Delete photos. You can give several image_ids, comma separated'
275    );
276
277  $service->addMethod(
278    'pwg.categories.getAdminList',
279    'ws_categories_getAdminList',
280    array(),
281    'administration method only'
282    );
283
284  $service->addMethod(
285    'pwg.categories.add',
286    'ws_categories_add',
287    array(
288      'name' => array(),
289      'parent' => array('default' => null),
290      ),
291    'administration method only'
292    );
293
294  $service->addMethod(
295    'pwg.categories.delete',
296    'ws_categories_delete',
297    array(
298      'category_id'=>array('default'=>0),
299      'pwg_token' => array(),
300      'photo_deletion_mode' => array('default' => 'delete_orphans'),
301      ),
302    'Delete categories. You can give several category_ids, comma separated.
303<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)'
304    );
305
306  $service->addMethod(
307    'pwg.categories.move',
308    'ws_categories_move',
309    array(
310      'category_id'=>array('default'=>0),
311      'parent'=>array('default'=>0),
312      'pwg_token' => array(),
313      ),
314    '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.'
315    );
316
317  $service->addMethod(
318    'pwg.categories.setRepresentative',
319    'ws_categories_setRepresentative',
320    array(
321      'category_id'=>array('default'=>0),
322      'image_id'=>array('default'=>0),
323      ),
324    'Set the representative photo for an album. The photo doesn\'t have to belong to the album. POST method only. Administration method only.'
325    );
326
327  $service->addMethod(
328    'pwg.tags.getAdminList',
329    'ws_tags_getAdminList',
330    array(),
331    'administration method only'
332    );
333
334  $service->addMethod(
335    'pwg.tags.add',
336    'ws_tags_add',
337    array(
338      'name' => array(),
339      ),
340    'administration method only'
341    );
342
343  $service->addMethod(
344    'pwg.images.exist',
345    'ws_images_exist',
346    array(
347      'md5sum_list'=> array('default' => null),
348      'filename_list' => array('default' => null),
349      ),
350    'check existence of a photo list'
351    );
352
353  $service->addMethod(
354    'pwg.images.checkFiles',
355    'ws_images_checkFiles',
356    array(
357      'image_id' => array(),
358      'thumbnail_sum' => array('default' => null),
359      'file_sum' => array('default' => null),
360      'high_sum' => array('default' => null),
361      ),
362    '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"'
363    );
364
365  $service->addMethod(
366    'pwg.images.checkUpload',
367    'ws_images_checkUpload',
368    null,
369    'check if Piwigo is ready for upload'
370    );
371
372  $service->addMethod(
373    'pwg.images.setInfo',
374    'ws_images_setInfo',
375    array(
376      'image_id' => array(),
377
378      'name' => array('default' => null),
379      'author' => array('default' => null),
380      'date_creation' => array('default' => null),
381      'comment' => array('default' => null),
382      'categories' => array('default' => null),
383      'tag_ids' => array('default' => null),
384      'level' => array(
385        'default' => null,
386        'maxValue' => $conf['available_permission_levels']
387        ),
388      'single_value_mode' => array('default' => 'fill_if_empty'),
389      'multiple_value_mode' => array('default' => 'append'),
390      ),
391    'POST method only. Admin only
392<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.
393<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
394<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'
395    );
396
397  $service->addMethod(
398    'pwg.categories.setInfo',
399    'ws_categories_setInfo',
400    array(
401      'category_id' => array(),
402
403      'name' => array('default' => null),
404      'comment' => array('default' => null),
405      ),
406    'POST method only.'
407    );
408 
409  $service->addMethod(
410    'pwg.plugins.getList',
411    'ws_plugins_getList',
412    array(),
413    'get the list of plugin with id, name, version, state and description
414<br>administration status required'
415    );
416
417  $service->addMethod(
418    'pwg.plugins.performAction',
419    'ws_plugins_performAction',
420    array(
421      'action' => array(),
422      'plugin' => array(),
423      'pwg_token' => array(),
424      ),
425    'install/activate/deactivate/uninstall/delete a plugin
426<br>administration status required'
427    );
428
429  $service->addMethod(
430    'pwg.themes.performAction',
431    'ws_themes_performAction',
432    array(
433      'action' => array(),
434      'theme' => array(),
435      'pwg_token' => array(),
436      ),
437    'activate/deactivate/delete/set_default a theme<br>administration status required'
438    );
439
440  $service->addMethod(
441    'pwg.images.resizeThumbnail',
442    'ws_images_resizethumbnail',
443    array(
444      'image_id' => array('default' => null),
445      'image_path' => array('default' => null),
446      'maxwidth' => array('default' => $conf['upload_form_thumb_maxwidth']),
447      'maxheight' => array('default' => $conf['upload_form_thumb_maxheight']),
448      'quality' => array('default' => $conf['upload_form_thumb_quality']),
449      'crop' => array('default' => $conf['upload_form_thumb_crop']),
450      'follow_orientation' => array('default' => $conf['upload_form_thumb_follow_orientation']),
451      'library' => array('default' => $conf['graphics_library']),
452    ),
453    'Create/Regenerate thumbnails photo with given arguments.
454<br>One of arguments "image_id" or "image_path" must be sent.'
455  );
456
457  $service->addMethod(
458    'pwg.images.resizeWebsize',
459    'ws_images_resizewebsize',
460    array(
461      'image_id' => array(),
462      'maxwidth' => array('default' => $conf['upload_form_websize_maxwidth']),
463      'maxheight' => array('default' => $conf['upload_form_websize_maxheight']),
464      'quality' => array('default' => $conf['upload_form_websize_quality']),
465      'automatic_rotation' => array('default' => $conf['upload_form_automatic_rotation']),
466      'library' => array('default' => $conf['graphics_library']),
467    ),
468    'Regenerate websize photo with given arguments.'
469  );
470
471  $service->addMethod(
472    'pwg.extensions.update',
473    'ws_extensions_update',
474    array(
475      'type' => array(),
476      'id' => array(),
477      'revision'=> array(),
478      'pwg_token' => array(),
479    ),
480    'Update an extension. Webmaster only.
481<br>Parameter type must be "plugins", "languages" or "themes".'
482  );
483
484  $service->addMethod(
485    'pwg.extensions.ignoreUpdate',
486    'ws_extensions_ignoreupdate',
487    array(
488      'type' => array('default'=>null),
489      'id' => array('default'=>null),
490      'reset' => array('default'=>null),
491      'pwg_token' => array(),
492    ),
493    'Ignore an extension if it need update.
494<br>Parameter type must be "plugins", "languages" or "themes".
495<br>If reset parameter is true, all ignored extensions will be reinitilized.'
496  );
497
498  $service->addMethod(
499    'pwg.extensions.checkUpdates',
500    'ws_extensions_checkupdates',
501    array(),
502    'Check if piwigo or extensions are up to date.'
503  );
504}
505
506add_event_handler('ws_add_methods', 'ws_addDefaultMethods');
507
508
509add_event_handler('ws_invoke_allowed', 'ws_isInvokeAllowed', EVENT_HANDLER_PRIORITY_NEUTRAL, 3);
510
511$requestFormat = 'rest';
512$responseFormat = null;
513
514if ( isset($_GET['format']) )
515{
516  $responseFormat = $_GET['format'];
517}
518
519if ( !isset($responseFormat) and isset($requestFormat) )
520{
521  $responseFormat = $requestFormat;
522}
523
524$service = new PwgServer();
525
526if (!is_null($requestFormat))
527{
528  $handler = null;
529  switch ($requestFormat)
530  {
531    case 'rest':
532      include_once(PHPWG_ROOT_PATH.'include/ws_protocols/rest_handler.php');
533      $handler = new PwgRestRequestHandler();
534      break;
535  }
536  $service->setHandler($requestFormat, $handler);
537}
538
539if (!is_null($responseFormat))
540{
541  $encoder = null;
542  switch ($responseFormat)
543  {
544    case 'rest':
545      include_once(PHPWG_ROOT_PATH.'include/ws_protocols/rest_encoder.php');
546      $encoder = new PwgRestEncoder();
547      break;
548    case 'php':
549      include_once(PHPWG_ROOT_PATH.'include/ws_protocols/php_encoder.php');
550      $encoder = new PwgSerialPhpEncoder();
551      break;
552    case 'json':
553      include_once(PHPWG_ROOT_PATH.'include/ws_protocols/json_encoder.php');
554      $encoder = new PwgJsonEncoder();
555      break;
556    case 'xmlrpc':
557      include_once(PHPWG_ROOT_PATH.'include/ws_protocols/xmlrpc_encoder.php');
558      $encoder = new PwgXmlRpcEncoder();
559      break;
560  }
561  $service->setEncoder($responseFormat, $encoder);
562}
563
564set_make_full_url();
565$service->run();
566
567?>
Note: See TracBrowser for help on using the repository browser.