source: branches/2.3/ws.php @ 12725

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

feature 2533 added: ability to deactivate uniqueness check on pwg.images.add

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