7bc8060cad228821ed0ebd76d389f561b2c8ad82
[web/PetoskeyRobotics.git] /
1 <?php
2
3 /***
4 {
5         Module: photocrati-nextgen_xmlrpc
6 }
7 ***/
8 class M_NextGen_XmlRpc extends C_Base_Module
9 {
10         function define()
11         {
12                 parent::define(
13                         'photocrati-nextgen_xmlrpc',
14                         'NextGEN Gallery XML-RPC',
15                         'Provides an XML-RPC API for NextGEN Gallery',
16                         '0.4',
17                         'http://www.nextgen-gallery.com',
18                         'Photocrati Media',
19                         'http://www.photocrati.com'
20                 );
21         }
22
23         function _register_hooks()
24         {
25                 add_filter('xmlrpc_methods', array(&$this, 'add_methods') );
26         }
27
28         function add_methods($methods)
29         {
30                 $methods['ngg.installed'] = array(&$this, 'get_version');
31                 // Image methods
32                 $methods['ngg.getImage'] = array(&$this, 'get_image');
33                 $methods['ngg.getImages'] = array(&$this, 'get_images');
34                 $methods['ngg.uploadImage'] = array(&$this, 'upload_image');
35                 $methods['ngg.editImage'] = array(&$this, 'edit_image');
36                 $methods['ngg.deleteImage'] = array(&$this, 'delete_image');
37                 // Gallery methods
38                 $methods['ngg.getGallery'] = array(&$this, 'get_gallery');
39                 $methods['ngg.getGalleries'] = array(&$this, 'get_galleries');
40                 $methods['ngg.newGallery'] = array(&$this, 'create_gallery');
41                 $methods['ngg.editGallery'] = array(&$this, 'edit_gallery');
42                 $methods['ngg.deleteGallery'] = array(&$this, 'delete_gallery');
43                 
44                 // Album methods
45                 $methods['ngg.getAlbum'] = array(&$this, 'get_album');
46                 $methods['ngg.getAlbums'] = array(&$this, 'get_albums');
47                 $methods['ngg.newAlbum'] = array(&$this, 'create_album');
48                 $methods['ngg.editAlbum'] = array(&$this, 'edit_album');
49                 $methods['ngg.deleteAlbum'] = array(&$this, 'delete_album');
50
51                 return $methods;
52         }
53
54         /**
55          * Gets the version of NextGEN Gallery installed
56          * @return string
57          */
58         function get_version()
59         {
60                 return array('version' => NGG_PLUGIN_VERSION);
61         }
62
63         /**
64          * Login a user
65          * @param $username
66          * @param $password
67          * @return bool|WP_Error|WP_User
68          */
69         function _login($username, $password, $blog_id=1)
70         {
71                 $retval = FALSE;
72
73                 if (!is_a(($user_obj = wp_authenticate($username, $password)), 'WP_Error')) {
74                         wp_set_current_user($user_obj->ID);
75                         $retval = $user_obj;
76
77                         if (is_multisite()) switch_to_blog($blog_id);
78                 }
79
80                 return $retval;
81         }
82
83         function _can_manage_gallery($gallery_id_or_obj, $check_upload_capability=FALSE)
84         {
85                 $retval = FALSE;
86
87                 // Get the gallery object, if we don't have it already
88                 $gallery = NULL;
89                 if (is_int($gallery_id_or_obj)) {
90                         $gallery_mapper = C_Gallery_Mapper::get_instance();
91                         $gallery = $gallery_mapper->find($gallery_id_or_obj);
92                 }
93                 else $gallery = $gallery_id_or_obj;
94
95                 if ($gallery) {
96                         $security = $this->get_registry()->get_utility('I_Security_Manager');
97                         $actor    = $security->get_current_actor();
98                         if ($actor->get_entity_id() == $gallery->author)                        $retval = TRUE;
99                         elseif ($actor->is_allowed('nextgen_edit_gallery_unowned')) $retval = TRUE;
100
101                         // Optionally, check if the user can upload to this gallery
102                         if ($retval && $check_upload_capability) {
103                                 $retval = $actor->is_allowed('nextgen_upload_image');
104                         }
105                 }
106
107                 return $retval;
108         }
109
110         function _add_gallery_properties($gallery)
111         {
112                 if (is_object($gallery)) {
113
114                         $image_mapper   = C_Image_Mapper::get_instance();
115                         $storage                = C_Gallery_Storage::get_instance();
116
117                         // Vladimir's Lightroom plugins requires the 'id' to be a string
118                         // Ask if he can accept integers as well. Currently, integers break
119                         // his plugin
120                         $gallery->gid = (string) $gallery->gid;
121
122                         // Set other gallery properties
123                         $image_counter = array_pop($image_mapper->select('DISTINCT COUNT(*) as counter')->where(array("galleryid = %d", $gallery->gid))->run_query(FALSE, TRUE));
124                         $gallery->counter = $image_counter->counter;
125                         $gallery->abspath = $storage->get_gallery_abspath($gallery);
126                 }
127                 else return FALSE;
128
129                 return TRUE;
130         }
131
132         /**
133          * Returns a single image object
134          * @param $args (blog_id, username, password, pid)
135          */
136         function get_image($args, $return_model=FALSE)
137         {
138                 $retval         = new IXR_Error(403, 'Invalid username or password');
139                 $blog_id        = intval($args[0]);
140                 $username       = strval($args[1]);
141                 $password   = strval($args[2]);
142                 $image_id       = intval($args[3]);
143
144                 // Authenticate the user
145                 if ($this->_login($username, $password, $blog_id)) {
146
147                         // Try to find the image
148                         $image_mapper = C_Image_Mapper::get_instance();
149                         if (($image = $image_mapper->find($image_id, TRUE))) {
150
151                                 // Try to find the gallery that the image belongs to
152                                 $gallery_mapper = C_Gallery_Mapper::get_instance();
153                                 if (($gallery = $gallery_mapper->find($image->galleryid))) {
154
155                                         // Does the user have sufficient capabilities?
156                                         if ($this->_can_manage_gallery($gallery)) {
157                                                 $storage = C_Gallery_Storage::get_instance();
158                                                 $image->imageURL        = $storage->get_image_url($image,'full', TRUE);
159                                                 $image->thumbURL        = $storage->get_thumb_url($image, TRUE);
160                                                 $image->imagePath       = $storage->get_image_abspath($image);
161                                                 $image->thumbPath       = $storage->get_thumb_abspath($image);
162                                                 $retval = $return_model ? $image : $image->get_entity();
163                                         }
164
165                                         else {
166                                                 $retval = new IXR_Error(403, "You don't have permission to manage gallery #{$image->galleryid}");
167                                         }
168                                 }
169
170                                 // No gallery found
171                                 else {
172                                         $retval = new IXR_Error(404, "Gallery not found (with id #{$image->gallerid}");
173                                 }
174                         }
175
176                         // No image found
177                         else $retval = FALSE;
178                 }
179
180                 return $retval;
181         }
182
183         /**
184          * Returns a collection of images
185          * @param $args (blog_id, username, password, gallery_id
186          */
187         function get_images($args)
188         {
189                 $retval         = new IXR_Error(403, 'Invalid username or password');
190                 $blog_id        = intval($args[0]);
191                 $username       = strval($args[1]);
192                 $password   = strval($args[2]);
193                 $gallery_id     = intval($args[3]);
194
195                 // Authenticate the user
196                 if ($this->_login($username, $password, $blog_id)) {
197
198                         // Try to find the gallery
199                         $mapper = C_Gallery_Mapper::get_instance();
200                         if (($gallery = $mapper->find($gallery_id, TRUE))) {
201
202                                 // Does the user have sufficient capabilities?
203                                 if ($this->_can_manage_gallery($gallery)) {
204                                         $retval = $gallery->get_images();
205                                 }
206                                 else {
207                                         $retval = new IXR_Error(403, "You don't have permission to manage gallery #{$image->galleryid}");
208                                 }
209                         }
210
211                         // No gallery found
212                         else {
213                                 $retval = new IXR_Error(404, "Gallery not found (with id #{$image->gallerid}");
214                         }
215                 }
216
217                 return $retval;
218         }
219
220         /**
221          * Uploads an image to a particular gallery
222          * @param $args (blog_id, username, password, data)
223          *
224          * Data is an assoc array:
225          *                o string name
226          *            o string type (optional)
227          *                o base64 bits
228          *                o bool overwrite (optional)
229          *                        o int gallery
230          *                        o int image_id  (optional)
231          * @return image
232          */
233         function upload_image($args)
234         {
235                 $retval         = new IXR_Error(403, 'Invalid username or password');
236                 $blog_id        = intval($args[0]);
237                 $username       = strval($args[1]);
238                 $password   = strval($args[2]);
239                 $data           = $args[3];
240                 $gallery_id = isset($data['gallery_id']) ? $data['gallery_id'] : $data['gallery'];
241
242                 // Authenticate the user
243                 if ($this->_login($username, $password, $blog_id)) {
244
245                         // Try to find the gallery
246                         $mapper = C_Gallery_Mapper::get_instance();
247                         if (($gallery = $mapper->find($gallery_id, TRUE))) {
248
249                                 // Does the user have sufficient capabilities?
250                                 if ($this->_can_manage_gallery($gallery, TRUE)) {
251
252                                         // Upload the image
253                                         $storage        = C_Gallery_Storage::get_instance();
254                                         $image          = $storage->upload_base64_image($gallery, $data['bits'], $data['name'], $data['image_id']);
255                                         if ($image) {
256                                                 $storage = C_Gallery_Storage::get_instance();
257                                                 $image->imageURL        = $storage->get_image_url($image);
258                                                 $image->thumbURL        = $storage->get_thumb_url($image);
259                                                 $image->imagePath       = $storage->get_image_abspath($image);
260                                                 $image->thumbPath       = $storage->get_thumb_abspath($image);
261                                                 $retval = $image->get_entity();
262                                         }
263                                         else
264                                                 $retval = new IXR_Error(500, "Could not upload image");
265                                 }
266                                 else {
267                                         $retval = new IXR_Error(403, "You don't have permission to upload to gallery #{$image->galleryid}");
268                                 }
269                         }
270
271                         // No gallery found
272                         else {
273                                 $retval = new IXR_Error(404, "Gallery not found (with id #{$image->gallerid}");
274                         }
275                 }
276
277                 return $retval;
278         }
279
280         /**
281          * Edits an image object
282          * @param $args (blog_id, username, password, image_id, alttext, description, exclude, other_properties
283          */
284         function edit_image($args)
285         {
286                 $alttext                = strval($args[4]);
287                 $description    = strval($args[5]);
288                 $exclude                = intval($args[6]);
289                 $properties             = isset($args[7]) ? (array)$args[7] : array();
290
291                 $retval = $this->get_image($args, TRUE);
292                 if (!($retval instanceof IXR_Error)) {
293                         $retval->alttext                = $alttext;
294                         $retval->description    = $description;
295                         $retval->exclude                = $exclude;
296
297                         // Other properties can be specified using an associative array
298                         foreach ($properties as $key => $value) {
299                                 $retval->$key = $value;
300                         }
301
302                         // Unset any dynamic properties not part of the schema
303                         foreach (array('imageURL', 'thumbURL', 'imagePath', 'thumbPath') as $key) {
304                                 unset($retval->$key);
305                         }
306
307                         $retval = $retval->save();
308                 }
309
310                 return $retval;
311         }
312
313         /**
314          * Deletes an existing image from a gallery
315          * @param $args (blog_id, username, password, image_id)
316          */
317         function delete_image($args)
318         {
319                 $retval = $this->get_image($args, TRUE);
320                 if (!($retval instanceof IXR_Error)) {
321                         $retval = $retval->destroy();
322                 }
323                 return $retval;
324         }
325
326         /**
327          * Creates a new gallery
328          * @param $args (blog_id, username, password, title)
329          */
330         function create_gallery($args)
331         {
332                 $retval         = new IXR_Error(403, 'Invalid username or password');
333                 $blog_id        = intval($args[0]);
334                 $username       = strval($args[1]);
335                 $password   = strval($args[2]);
336                 $title          = strval($args[3]);
337
338                 // Authenticate the user
339                 if ($this->_login($username, $password, $blog_id)) {
340
341                         $security = $this->get_registry()->get_utility('I_Security_Manager');
342                         if ($security->is_allowed('nextgen_edit_gallery')) {
343                                 $mapper = C_Gallery_Mapper::get_instance();
344                                 if (($gallery = $mapper->create(array('title'   =>      $title))) && $gallery->save()) {
345                                         $retval = $gallery->id();
346                                 }
347                                 else $retval = new IXR_Error(500, "Unable to create gallery");
348
349                         }
350                         else $retval = new IXR_Error(403, "Sorry, but you must be able to manage galleries. Check your roles/capabilities.");
351                 }
352
353                 return $retval;
354         }
355
356         /**
357          * Edits an existing gallery
358          * @param $args (blog_id, username, password, gallery_id, name, title, description, preview_pic_id)
359          */
360         function edit_gallery($args)
361         {
362                 $retval         = new IXR_Error(403, 'Invalid username or password');
363                 $blog_id        = intval($args[0]);
364                 $username       = strval($args[1]);
365                 $password   = strval($args[2]);
366                 $gallery_id = intval($args[3]);
367                 $name           = strval($args[4]);
368                 $title          = strval($args[5]);
369                 $galdesc    = strval($args[6]);
370                 $image_id       = intval($args[7]);
371                 $properties = isset($args[8]) ? (array) $args[8] : array();
372
373                 // Authenticate the user
374                 if ($this->_login($username, $password, $blog_id)) {
375
376                         $mapper = C_Gallery_Mapper::get_instance();
377                         if (($gallery = $mapper->find($gallery_id, TRUE))) {
378                                 if ($this->_can_manage_gallery($gallery)) {
379                                         $gallery->name  = $name;
380                                         $gallery->title = $title;
381                                         $gallery->galdesc = $galdesc;
382                                         $gallery->previewpic = $image_id;
383                                         foreach ($properties as $key => $value) {
384                                                 $gallery->$key = $value;
385                                         }
386
387                                         // Unset dynamic properties not part of the schema
388                                         unset($gallery->counter);
389                                         unset($gallery->abspath);
390
391                                         $retval = $gallery->save();
392                                 }
393                                 else $retval = new IXR_Error(403, "You don't have permission to modify this gallery");
394                         }
395                         else $retval = new IXR_Error(404, "Gallery #{$gallery_id} doesn't exist");
396                 }
397
398                 return $retval;
399         }
400
401         /**
402          * Returns all galleries
403          * @param $args (blog_id, username, password)
404          */
405         function get_galleries($args)
406         {
407                 $retval         = new IXR_Error(403, 'Invalid username or password');
408                 $blog_id        = intval($args[0]);
409                 $username       = strval($args[1]);
410                 $password   = strval($args[2]);
411
412                 // Authenticate the user
413                 if ($this->_login($username, $password, $blog_id)) {
414
415                         // Do we have permission?
416                         $security = $this->get_registry()->get_utility('I_Security_Manager');
417                         if ($security->is_allowed('nextgen_edit_gallery')) {
418                                 $mapper                 = C_Gallery_Mapper::get_instance();
419                                 $retval                 = array();
420                                 foreach ($mapper->find_all() as $gallery) {
421                                         $this->_add_gallery_properties($gallery);
422                                         $retval[$gallery->{$gallery->id_field}] = (array)$gallery;
423                                 }
424                         }
425                         else $retval = new IXR_Error( 401, __( 'Sorry, you must be able to manage galleries' ) );
426                 }
427
428                 return $retval;
429         }
430
431         /**
432          * Gets a single gallery instance
433          * @param $args (blog_id, username, password, gallery_id)
434          */
435         function get_gallery($args, $return_model=FALSE)
436         {
437                 $retval         = new IXR_Error(403, 'Invalid username or password');
438                 $blog_id        = intval($args[0]);
439                 $username       = strval($args[1]);
440                 $password   = strval($args[2]);
441                 $gallery_id     = intval($args[3]);
442
443                 // Authenticate the user
444                 if ($this->_login($username, $password, $blog_id)) {
445                         $mapper = C_Gallery_Mapper::get_instance();
446                         if (($gallery = $mapper->find($gallery_id, TRUE))) {
447                                 if ($this->_can_manage_gallery($gallery)) {
448                                         $this->_add_gallery_properties($gallery);
449                                         $retval = $return_model ? $gallery : $gallery->get_entity();
450                                 }
451                                 else $retval = new IXR_Error(403, "Sorry, but you don't have permission to manage gallery #{$gallery->gid}");
452                         }
453                         else $retval = FALSE;
454                 }
455
456                 return $retval;
457         }
458
459         /**
460          * Deletes a gallery
461          * @param $args (blog_id, username, password, gallery_id)
462          */
463         function delete_gallery($args)
464         {
465                 $retval = $this->get_gallery($args, TRUE);
466
467                 if (!($retval instanceof IXR_Error) and is_object($retval)) {
468                         $retval = $retval->destroy();
469                 }
470
471                 return $retval;
472         }
473
474         /**
475          * Creates a new album
476          * @param $args (blog_id, username, password, title, previewpic, description, galleries
477          */
478         function create_album($args)
479         {
480                 $retval         = new IXR_Error(403, 'Invalid username or password');
481                 $blog_id        = intval($args[0]);
482                 $username       = strval($args[1]);
483                 $password   = strval($args[2]);
484                 $title          = strval($args[3]);
485                 $previewpic = isset($args[4]) ? intval($args[4]): 0;
486                 $desc           = isset($args[5]) ? strval($args[5]) : '';
487                 $sortorder  = isset($args[6]) ? $args[6] : '';
488                 $page_id        = isset($args[7]) ? intval($args[7]) : 0;
489
490                 // Authenticate the user
491                 if ($this->_login($username, $password, $blog_id)) {
492
493                         // Is request allowed?
494                         $security = $this->get_registry()->get_utility('I_Security_Manager');
495                         if ($security->is_allowed('nextgen_edit_album')) {
496
497                                 $mapper = C_Album_Mapper::get_instance();
498                                 $album = $mapper->create(array(
499                                         'name'                  =>      $title,
500                                         'previewpic'    =>      $previewpic,
501                                         'albumdesc'             =>      $desc,
502                                         'sortorder'             =>      $sortorder,
503                                         'pageid'                =>      $page_id
504                                 ));
505
506                                 if ($album->save()) $retval = $album->id();
507                                 else $retval = new IXR_Error(500, "Unable to create album");
508                         }
509                 }
510
511                 return $retval;
512         }
513
514
515         /**
516          * Returns all albums
517          * @param $args (blog_id, username, password)
518          * @return IXR_Error
519          */
520         function get_albums($args)
521         {
522                 $retval         = new IXR_Error(403, 'Invalid username or password');
523                 $blog_id        = intval($args[0]);
524                 $username       = strval($args[1]);
525                 $password   = strval($args[2]);
526
527                 // Authenticate the user
528                 if ($this->_login($username, $password, $blog_id)) {
529
530                         // Are we allowed?
531                         $security = $this->get_registry()->get_utility('I_Security_Manager');
532                         if ($security->is_allowed('nextgen_edit_album')) {
533
534                                 // Fetch all albums
535                                 $mapper = C_Album_Mapper::get_instance();
536                                 $retval = array();
537                                 foreach ($mapper->find_all() as $album) {
538                                         // Vladimir's Lightroom plugins requires the 'id' to be a string
539                                         // Ask if he can accept integers as well. Currently, integers break
540                                         // his plugin
541                                         $album->id = (string) $album->id;
542                                         $album->galleries = $album->sortorder;
543
544                                         $retval[$album->{$album->id_field}] = (array) $album;
545                                 }
546                         }
547                         else $retval = new IXR_Error(403, "Sorry, you must be able to manage albums");
548
549
550                 }
551
552                 return $retval;
553         }
554
555         /**
556          * Gets a single album
557          * @param $args (blog_id, username, password, album_id)
558          */
559         function get_album($args, $return_model=FALSE)
560         {
561                 $retval         = new IXR_Error(403, 'Invalid username or password');
562                 $blog_id        = intval($args[0]);
563                 $username       = strval($args[1]);
564                 $password   = strval($args[2]);
565                 $album_id       = intval($args[3]);
566
567                 // Authenticate the user
568                 if ($this->_login($username, $password, $blog_id)) {
569
570                         // Are we allowed?
571                         $security = $this->get_registry()->get_utility('I_Security_Manager');
572                         if ($security->is_allowed('nextgen_edit_album')) {
573                                 $mapper = C_Album_Mapper::get_instance();
574                                 if (($album = $mapper->find($album_id, TRUE))) {
575                                         // Vladimir's Lightroom plugins requires the 'id' to be a string
576                                         // Ask if he can accept integers as well. Currently, integers break
577                                         // his plugin
578                                         $album->id = (string) $album->id;
579                                         $album->galleries = $album->sortorder;
580
581                                         $retval = $return_model ? $album : $album->get_entity();
582                                 }
583                                 else $retval = FALSE;
584
585                         }
586                         else $retval = new IXR_Error(403, "Sorry, you must be able to manage albums");
587                 }
588
589                 return $retval;
590         }
591
592         /**
593          * Deletes an existing album
594          * @param $args (blog_id, username, password, album_id)
595          */
596         function delete_album($args)
597         {
598                 $retval = $this->get_album($args, TRUE);
599
600                 if (!($retval instanceof IXR_Error)) {
601                         $retval = $retval->destroy();
602                 }
603
604                 return $retval;
605         }
606
607         /**
608          * Edit an existing album
609          * @param $args (blog_id, username, password, album_id, name, preview pic id, description, galleries)
610          */
611         function edit_album($args)
612         {
613                 $retval = $this->get_album($args, TRUE);
614
615                 if (!($retval instanceof IXR_Error)) {
616                         $retval->name           = strval($args[4]);
617                         $retval->previewpic = intval($args[5]);
618                         $retval->albumdesc      = strval($args[6]);
619                         $retval->sortorder  = $args[7];
620
621                         $properties = isset($args[8]) ? $args[8] : array();
622                         foreach ($properties as $key => $value) $retval->$key = $value;
623                         unset($retval->galleries);
624
625                         $retval = $retval->save();
626                 }
627
628                 return $retval;
629         }
630 }
631
632 new M_NextGen_XmlRpc;