source: trunk/ws.php @ 12906

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

feature 2548 (multiple sizes): adapt the web API method pwg.images.add (used
by pLoader, Digikam, Lightroom, iPhoto), pwg.images.checkFiles (pLoader only).

The "resize" parameter was removed for pwg.images.add since this behavior
becomes the default behavior in Piwigo 2.4.

Just like pwg.images.addSimple, pwg.images.add now uses the add_uploaded_file
function (next step is to merge pwg.images.add and pwg.images.addSimple)

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