source: trunk/ws.php @ 15850

Last change on this file since 15850 was 15850, checked in by mistic100, 12 years ago

feature 2657: More options for pwg.categories.add

  • 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-2012 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_max_level' => array( 'default'=> null ),
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.getMissingDerivatives', 'ws_getMissingDerivatives',
90      array(
91        'types' => array( 'default'=>array(), 'flags'=>WS_PARAM_FORCE_ARRAY),
92        'ids' => array( 'default'=>array(), 'flags'=>WS_PARAM_FORCE_ARRAY),
93        'max_urls' => array( 'default' => 200 ),
94        'prev_page' => array( 'default'=> null),
95        'f_min_rate' => array( 'default'=> null ),
96        'f_max_rate' => array( 'default'=> null ),
97        'f_min_hit' => array( 'default'=> null ),
98        'f_max_hit' => array( 'default'=> null ),
99        'f_min_date_available' => array( 'default'=> null ),
100        'f_max_date_available' => array( 'default'=> null ),
101        'f_min_date_created' => array( 'default'=> null ),
102        'f_max_date_created' => array( 'default'=> null ),
103        'f_min_ratio' => array( 'default'=> null ),
104        'f_max_ratio' => array( 'default'=> null ),
105        'f_max_level' => array( 'default'=> null ),
106      ),
107      'retrieves a list of derivatives to build' );
108
109  $service->addMethod('pwg.images.addComment', 'ws_images_addComment',
110      array(
111        'image_id' => array(),
112        'author' => array( 'default' => is_a_guest()? 'guest':$user['username']),
113        'content' => array(),
114        'key' => array(),
115      ),
116      'add a comment to an image' );
117
118  $service->addMethod('pwg.images.getInfo', 'ws_images_getInfo',
119      array(
120        'image_id' => array(),
121        'comments_page' => array('default'=>0 ),
122        'comments_per_page' => array(
123              'default' => $conf['nb_comment_page'],
124              'maxValue' => 2*$conf['nb_comment_page'],
125            ),
126      ),
127      'retrieves information about the given photo' );
128
129  $service->addMethod('pwg.images.rate', 'ws_images_rate',
130      array(
131        'image_id' => array(),
132        'rate' =>     array(),
133      ),
134      'rate the image' );
135
136  $service->addMethod('pwg.images.search', 'ws_images_search',
137      array(
138        'query'=>array(),
139        'per_page' => array('default'=>100, 'maxValue'=>$conf['ws_max_images_per_page']),
140        'page' => array('default'=>0),
141        'order' => array('default'=>null),
142        'f_min_rate' => array( 'default'=> null ),
143        'f_max_rate' => array( 'default'=> null ),
144        'f_min_hit' => array( 'default'=> null ),
145        'f_max_hit' => array( 'default'=> null ),
146        'f_min_date_available' => array( 'default'=> null ),
147        'f_max_date_available' => array( 'default'=> null ),
148        'f_min_date_created' => array( 'default'=> null ),
149        'f_max_date_created' => array( 'default'=> null ),
150        'f_min_ratio' => array( 'default'=> null ),
151        'f_max_ratio' => array( 'default'=> null ),
152        'f_max_level' => array( 'default'=> null ),
153      ),
154      'Returns elements for the corresponding query search.'
155    );
156
157  $service->addMethod(
158    'pwg.images.setPrivacyLevel',
159    'ws_images_setPrivacyLevel',
160    array(
161      'image_id' => array('flags'=>WS_PARAM_FORCE_ARRAY),
162      'level' => array('maxValue'=>$conf['available_permission_levels']),
163      ),
164    'sets the privacy levels for the images (POST method only)'
165    );
166
167  $service->addMethod(
168    'pwg.images.setRank',
169    'ws_images_setRank',
170    array(
171      'image_id' => array(),
172      'category_id' => array(),
173      'rank' => array(),
174      ),
175    'sets the rank of a photo for a given album (POST method only, for admins)'
176    );
177
178
179  $service->addMethod('pwg.rates.delete', 'ws_rates_delete',
180    array(
181      'user_id' => array(),
182      'anonymous_id' => array( 'default'=>'' ),
183      ),
184    'deletes all rates for a user (POST method only, admins only)'
185    );
186   
187  $service->addMethod('pwg.session.getStatus', 'ws_session_getStatus', null, '' );
188  $service->addMethod('pwg.session.login', 'ws_session_login',
189    array('username', 'password'),
190    'POST method only' );
191  $service->addMethod('pwg.session.logout', 'ws_session_logout', null, '');
192
193  $service->addMethod('pwg.tags.getList', 'ws_tags_getList',
194    array('sort_by_counter' => array('default' =>false) ),
195    'retrieves a list of available tags');
196  $service->addMethod('pwg.tags.getImages', 'ws_tags_getImages',
197      array(
198        'tag_id'=>array('default'=>null, 'flags'=>WS_PARAM_FORCE_ARRAY ),
199        'tag_url_name'=>array('default'=>null, 'flags'=>WS_PARAM_FORCE_ARRAY ),
200        'tag_name'=>array('default'=>null, 'flags'=>WS_PARAM_FORCE_ARRAY ),
201        'tag_mode_and'=>array('default'=>false),
202        'per_page' => array('default'=>100, 'maxValue'=>$conf['ws_max_images_per_page']),
203        'page' => array('default'=>0),
204        'order' => array('default'=>null),
205        'f_min_rate' => array( 'default'=> null ),
206        'f_max_rate' => array( 'default'=> null ),
207        'f_min_hit' => array( 'default'=> null ),
208        'f_max_hit' => array( 'default'=> null ),
209        'f_min_date_available' => array( 'default'=> null ),
210        'f_max_date_available' => array( 'default'=> null ),
211        'f_min_date_created' => array( 'default'=> null ),
212        'f_max_date_created' => array( 'default'=> null ),
213        'f_min_ratio' => array( 'default'=> null ),
214        'f_max_ratio' => array( 'default'=> null ),
215        'f_max_level' => array( 'default'=> null ),
216      ),
217      'Returns elements for the corresponding tags. Note that tag_id, tag_url_name, tag_name an be arrays. Fill at least one of them. '
218    );
219
220  $service->addMethod(
221    'pwg.images.addChunk',
222    'ws_images_add_chunk',
223    array(
224      'data' => array(),
225      'original_sum' => array(),
226      'type' => array(),
227      'position' => array(),
228      ),
229    'POST method only. For admin only.'
230    );
231
232  $service->addMethod(
233    'pwg.images.addFile',
234    'ws_images_addFile',
235    array(
236      'image_id' => array(),
237      'type' => array(),
238      'sum' => array(),
239      ),
240    'Add or update a file for an existing photo. pwg.images.addChunk must have been called  before (maybe several times)'
241    );
242
243
244  $service->addMethod(
245    'pwg.images.add',
246    'ws_images_add',
247    array(
248      'file_sum' => array(),
249      'thumbnail_sum' => array('default' => null),
250      'high_sum' => array('default' => null),
251      'original_sum' => array(),
252      'original_filename' => array('default' => null),
253      'name' => array('default' => null),
254      'author' => array('default' => null),
255      'date_creation' => array('default' => null),
256      'comment' => array('default' => null),
257      'categories' => array('default' => null),
258      'tag_ids' => array('default' => null),
259      'level' => array(
260        'default' => 0,
261        'maxValue' => $conf['available_permission_levels']
262        ),
263      'check_uniqueness' => array('default' => true),
264      'image_id' => array('default' => null),
265      ),
266    'POST method only.
267<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.'
268    );
269
270  $service->addMethod(
271    'pwg.images.addSimple',
272    'ws_images_addSimple',
273    array(
274      'category' => array('default' => null),
275      'name' => array('default' => null),
276      'author' => array('default' => null),
277      'comment' => array('default' => null),
278      'level' => array(
279        'default' => 0,
280        'maxValue' => $conf['available_permission_levels']
281        ),
282      'tags' => array('default' => null),
283      'image_id' => array('default' => null),
284      ),
285    '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.'
286    );
287
288  $service->addMethod(
289    'pwg.images.delete',
290    'ws_images_delete',
291    array(
292      'image_id'=>array('default'=>0),
293      'pwg_token' => array(),
294      ),
295    'Delete photos. You can give several image_ids, comma separated'
296    );
297
298  $service->addMethod(
299    'pwg.categories.getAdminList',
300    'ws_categories_getAdminList',
301    array(),
302    'administration method only'
303    );
304
305  $service->addMethod(
306    'pwg.categories.add',
307    'ws_categories_add',
308    array(
309      'name' => array(),
310      'parent' => array('default' => null),
311      'comment' => array('default' => null),
312      'visible' => array('default' => boolean_to_string($conf['newcat_default_visible'])),
313      'status' => array('default' => $conf['newcat_default_status']),
314      'commentable' => array('default' => 'true'),
315      ),
316    'administration method only'
317    );
318
319  $service->addMethod(
320    'pwg.categories.delete',
321    'ws_categories_delete',
322    array(
323      'category_id'=>array('default'=>0),
324      'pwg_token' => array(),
325      'photo_deletion_mode' => array('default' => 'delete_orphans'),
326      ),
327    'Delete categories. You can give several category_ids, comma separated.
328<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)'
329    );
330
331  $service->addMethod(
332    'pwg.categories.move',
333    'ws_categories_move',
334    array(
335      'category_id'=>array('default'=>0),
336      'parent'=>array('default'=>0),
337      'pwg_token' => array(),
338      ),
339    '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.'
340    );
341
342  $service->addMethod(
343    'pwg.categories.setRepresentative',
344    'ws_categories_setRepresentative',
345    array(
346      'category_id'=>array('default'=>0),
347      'image_id'=>array('default'=>0),
348      ),
349    'Set the representative photo for an album. The photo doesn\'t have to belong to the album. POST method only. Administration method only.'
350    );
351
352  $service->addMethod(
353    'pwg.tags.getAdminList',
354    'ws_tags_getAdminList',
355    array(),
356    'administration method only'
357    );
358
359  $service->addMethod(
360    'pwg.tags.add',
361    'ws_tags_add',
362    array(
363      'name' => array(),
364      ),
365    'administration method only'
366    );
367
368  $service->addMethod(
369    'pwg.images.exist',
370    'ws_images_exist',
371    array(
372      'md5sum_list'=> array('default' => null),
373      'filename_list' => array('default' => null),
374      ),
375    'check existence of a photo list'
376    );
377
378  $service->addMethod(
379    'pwg.images.checkFiles',
380    'ws_images_checkFiles',
381    array(
382      'image_id' => array(),
383      'thumbnail_sum' => array('default' => null),
384      'file_sum' => array('default' => null),
385      'high_sum' => array('default' => null),
386      ),
387    '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"'
388    );
389
390  $service->addMethod(
391    'pwg.images.checkUpload',
392    'ws_images_checkUpload',
393    null,
394    'check if Piwigo is ready for upload'
395    );
396
397  $service->addMethod(
398    'pwg.images.setInfo',
399    'ws_images_setInfo',
400    array(
401      'image_id' => array(),
402
403      'file' => array('default' => null),
404      'name' => array('default' => null),
405      'author' => array('default' => null),
406      'date_creation' => array('default' => null),
407      'comment' => array('default' => null),
408      'categories' => array('default' => null),
409      'tag_ids' => array('default' => null),
410      'level' => array(
411        'default' => null,
412        'maxValue' => $conf['available_permission_levels']
413        ),
414      'single_value_mode' => array('default' => 'fill_if_empty'),
415      'multiple_value_mode' => array('default' => 'append'),
416      ),
417    'POST method only. Admin only
418<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.
419<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
420<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'
421    );
422
423  $service->addMethod(
424    'pwg.categories.setInfo',
425    'ws_categories_setInfo',
426    array(
427      'category_id' => array(),
428
429      'name' => array('default' => null),
430      'comment' => array('default' => null),
431      ),
432    'POST method only.'
433    );
434 
435  $service->addMethod(
436    'pwg.plugins.getList',
437    'ws_plugins_getList',
438    array(),
439    'get the list of plugin with id, name, version, state and description
440<br>administration status required'
441    );
442
443  $service->addMethod(
444    'pwg.plugins.performAction',
445    'ws_plugins_performAction',
446    array(
447      'action' => array(),
448      'plugin' => array(),
449      'pwg_token' => array(),
450      ),
451    'install/activate/deactivate/uninstall/delete a plugin
452<br>administration status required'
453    );
454
455  $service->addMethod(
456    'pwg.themes.performAction',
457    'ws_themes_performAction',
458    array(
459      'action' => array(),
460      'theme' => array(),
461      'pwg_token' => array(),
462      ),
463    'activate/deactivate/delete/set_default a theme<br>administration status required'
464    );
465
466  $service->addMethod(
467    'pwg.extensions.update',
468    'ws_extensions_update',
469    array(
470      'type' => array(),
471      'id' => array(),
472      'revision'=> array(),
473      'pwg_token' => array(),
474    ),
475    'Update an extension. Webmaster only.
476<br>Parameter type must be "plugins", "languages" or "themes".'
477  );
478
479  $service->addMethod(
480    'pwg.extensions.ignoreUpdate',
481    'ws_extensions_ignoreupdate',
482    array(
483      'type' => array('default'=>null),
484      'id' => array('default'=>null),
485      'reset' => array('default'=>null),
486      'pwg_token' => array(),
487    ),
488    'Ignore an extension if it need update.
489<br>Parameter type must be "plugins", "languages" or "themes".
490<br>If reset parameter is true, all ignored extensions will be reinitilized.'
491  );
492
493  $service->addMethod(
494    'pwg.extensions.checkUpdates',
495    'ws_extensions_checkupdates',
496    array(),
497    'Check if piwigo or extensions are up to date.'
498  );
499}
500
501add_event_handler('ws_add_methods', 'ws_addDefaultMethods');
502
503
504add_event_handler('ws_invoke_allowed', 'ws_isInvokeAllowed', EVENT_HANDLER_PRIORITY_NEUTRAL, 3);
505
506$requestFormat = 'rest';
507$responseFormat = null;
508
509if ( isset($_GET['format']) )
510{
511  $responseFormat = $_GET['format'];
512}
513
514if ( !isset($responseFormat) and isset($requestFormat) )
515{
516  $responseFormat = $requestFormat;
517}
518
519$service = new PwgServer();
520
521if (!is_null($requestFormat))
522{
523  $handler = null;
524  switch ($requestFormat)
525  {
526    case 'rest':
527      include_once(PHPWG_ROOT_PATH.'include/ws_protocols/rest_handler.php');
528      $handler = new PwgRestRequestHandler();
529      break;
530  }
531  $service->setHandler($requestFormat, $handler);
532}
533
534if (!is_null($responseFormat))
535{
536  $encoder = null;
537  switch ($responseFormat)
538  {
539    case 'rest':
540      include_once(PHPWG_ROOT_PATH.'include/ws_protocols/rest_encoder.php');
541      $encoder = new PwgRestEncoder();
542      break;
543    case 'php':
544      include_once(PHPWG_ROOT_PATH.'include/ws_protocols/php_encoder.php');
545      $encoder = new PwgSerialPhpEncoder();
546      break;
547    case 'json':
548      include_once(PHPWG_ROOT_PATH.'include/ws_protocols/json_encoder.php');
549      $encoder = new PwgJsonEncoder();
550      break;
551    case 'xmlrpc':
552      include_once(PHPWG_ROOT_PATH.'include/ws_protocols/xmlrpc_encoder.php');
553      $encoder = new PwgXmlRpcEncoder();
554      break;
555  }
556  $service->setEncoder($responseFormat, $encoder);
557}
558
559set_make_full_url();
560$service->run();
561
562?>
Note: See TracBrowser for help on using the repository browser.