5 Module: photocrati-nextgen_xmlrpc
8 class M_NextGen_XmlRpc extends C_Base_Module
13 'photocrati-nextgen_xmlrpc',
14 'NextGEN Gallery XML-RPC',
15 'Provides an XML-RPC API for NextGEN Gallery',
17 'http://www.nextgen-gallery.com',
19 'http://www.photocrati.com'
23 function _register_hooks()
25 add_filter('xmlrpc_methods', array(&$this, 'add_methods') );
28 function add_methods($methods)
30 $methods['ngg.installed'] = array(&$this, 'get_version');
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');
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');
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');
55 * Gets the version of NextGEN Gallery installed
58 function get_version()
60 return array('version' => NGG_PLUGIN_VERSION);
67 * @return bool|WP_Error|WP_User
69 function _login($username, $password, $blog_id=1)
73 if (!is_a(($user_obj = wp_authenticate($username, $password)), 'WP_Error')) {
74 wp_set_current_user($user_obj->ID);
77 if (is_multisite()) switch_to_blog($blog_id);
83 function _can_manage_gallery($gallery_id_or_obj, $check_upload_capability=FALSE)
87 // Get the gallery object, if we don't have it already
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);
93 else $gallery = $gallery_id_or_obj;
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;
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');
110 function _add_gallery_properties($gallery)
112 if (is_object($gallery)) {
114 $image_mapper = C_Image_Mapper::get_instance();
115 $storage = C_Gallery_Storage::get_instance();
117 // Vladimir's Lightroom plugins requires the 'id' to be a string
118 // Ask if he can accept integers as well. Currently, integers break
120 $gallery->gid = (string) $gallery->gid;
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);
133 * Returns a single image object
134 * @param $args (blog_id, username, password, pid)
136 function get_image($args, $return_model=FALSE)
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]);
144 // Authenticate the user
145 if ($this->_login($username, $password, $blog_id)) {
147 // Try to find the image
148 $image_mapper = C_Image_Mapper::get_instance();
149 if (($image = $image_mapper->find($image_id, TRUE))) {
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))) {
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();
166 $retval = new IXR_Error(403, "You don't have permission to manage gallery #{$image->galleryid}");
172 $retval = new IXR_Error(404, "Gallery not found (with id #{$image->gallerid}");
177 else $retval = FALSE;
184 * Returns a collection of images
185 * @param $args (blog_id, username, password, gallery_id
187 function get_images($args)
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]);
195 // Authenticate the user
196 if ($this->_login($username, $password, $blog_id)) {
198 // Try to find the gallery
199 $mapper = C_Gallery_Mapper::get_instance();
200 if (($gallery = $mapper->find($gallery_id, TRUE))) {
202 // Does the user have sufficient capabilities?
203 if ($this->_can_manage_gallery($gallery)) {
204 $retval = $gallery->get_images();
207 $retval = new IXR_Error(403, "You don't have permission to manage gallery #{$image->galleryid}");
213 $retval = new IXR_Error(404, "Gallery not found (with id #{$image->gallerid}");
221 * Uploads an image to a particular gallery
222 * @param $args (blog_id, username, password, data)
224 * Data is an assoc array:
226 * o string type (optional)
228 * o bool overwrite (optional)
230 * o int image_id (optional)
233 function upload_image($args)
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]);
240 $gallery_id = isset($data['gallery_id']) ? $data['gallery_id'] : $data['gallery'];
242 // Authenticate the user
243 if ($this->_login($username, $password, $blog_id)) {
245 // Try to find the gallery
246 $mapper = C_Gallery_Mapper::get_instance();
247 if (($gallery = $mapper->find($gallery_id, TRUE))) {
249 // Does the user have sufficient capabilities?
250 if ($this->_can_manage_gallery($gallery, TRUE)) {
253 $storage = C_Gallery_Storage::get_instance();
254 $image = $storage->upload_base64_image($gallery, $data['bits'], $data['name'], $data['image_id']);
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();
264 $retval = new IXR_Error(500, "Could not upload image");
267 $retval = new IXR_Error(403, "You don't have permission to upload to gallery #{$image->galleryid}");
273 $retval = new IXR_Error(404, "Gallery not found (with id #{$image->gallerid}");
281 * Edits an image object
282 * @param $args (blog_id, username, password, image_id, alttext, description, exclude, other_properties
284 function edit_image($args)
286 $alttext = strval($args[4]);
287 $description = strval($args[5]);
288 $exclude = intval($args[6]);
289 $properties = isset($args[7]) ? (array)$args[7] : array();
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;
297 // Other properties can be specified using an associative array
298 foreach ($properties as $key => $value) {
299 $retval->$key = $value;
302 // Unset any dynamic properties not part of the schema
303 foreach (array('imageURL', 'thumbURL', 'imagePath', 'thumbPath') as $key) {
304 unset($retval->$key);
307 $retval = $retval->save();
314 * Deletes an existing image from a gallery
315 * @param $args (blog_id, username, password, image_id)
317 function delete_image($args)
319 $retval = $this->get_image($args, TRUE);
320 if (!($retval instanceof IXR_Error)) {
321 $retval = $retval->destroy();
327 * Creates a new gallery
328 * @param $args (blog_id, username, password, title)
330 function create_gallery($args)
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]);
338 // Authenticate the user
339 if ($this->_login($username, $password, $blog_id)) {
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();
347 else $retval = new IXR_Error(500, "Unable to create gallery");
350 else $retval = new IXR_Error(403, "Sorry, but you must be able to manage galleries. Check your roles/capabilities.");
357 * Edits an existing gallery
358 * @param $args (blog_id, username, password, gallery_id, name, title, description, preview_pic_id)
360 function edit_gallery($args)
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();
373 // Authenticate the user
374 if ($this->_login($username, $password, $blog_id)) {
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;
387 // Unset dynamic properties not part of the schema
388 unset($gallery->counter);
389 unset($gallery->abspath);
391 $retval = $gallery->save();
393 else $retval = new IXR_Error(403, "You don't have permission to modify this gallery");
395 else $retval = new IXR_Error(404, "Gallery #{$gallery_id} doesn't exist");
402 * Returns all galleries
403 * @param $args (blog_id, username, password)
405 function get_galleries($args)
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]);
412 // Authenticate the user
413 if ($this->_login($username, $password, $blog_id)) {
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();
420 foreach ($mapper->find_all() as $gallery) {
421 $this->_add_gallery_properties($gallery);
422 $retval[$gallery->{$gallery->id_field}] = (array)$gallery;
425 else $retval = new IXR_Error( 401, __( 'Sorry, you must be able to manage galleries' ) );
432 * Gets a single gallery instance
433 * @param $args (blog_id, username, password, gallery_id)
435 function get_gallery($args, $return_model=FALSE)
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]);
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();
451 else $retval = new IXR_Error(403, "Sorry, but you don't have permission to manage gallery #{$gallery->gid}");
453 else $retval = FALSE;
461 * @param $args (blog_id, username, password, gallery_id)
463 function delete_gallery($args)
465 $retval = $this->get_gallery($args, TRUE);
467 if (!($retval instanceof IXR_Error) and is_object($retval)) {
468 $retval = $retval->destroy();
475 * Creates a new album
476 * @param $args (blog_id, username, password, title, previewpic, description, galleries
478 function create_album($args)
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;
490 // Authenticate the user
491 if ($this->_login($username, $password, $blog_id)) {
493 // Is request allowed?
494 $security = $this->get_registry()->get_utility('I_Security_Manager');
495 if ($security->is_allowed('nextgen_edit_album')) {
497 $mapper = C_Album_Mapper::get_instance();
498 $album = $mapper->create(array(
500 'previewpic' => $previewpic,
501 'albumdesc' => $desc,
502 'sortorder' => $sortorder,
506 if ($album->save()) $retval = $album->id();
507 else $retval = new IXR_Error(500, "Unable to create album");
517 * @param $args (blog_id, username, password)
520 function get_albums($args)
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]);
527 // Authenticate the user
528 if ($this->_login($username, $password, $blog_id)) {
531 $security = $this->get_registry()->get_utility('I_Security_Manager');
532 if ($security->is_allowed('nextgen_edit_album')) {
535 $mapper = C_Album_Mapper::get_instance();
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
541 $album->id = (string) $album->id;
542 $album->galleries = $album->sortorder;
544 $retval[$album->{$album->id_field}] = (array) $album;
547 else $retval = new IXR_Error(403, "Sorry, you must be able to manage albums");
556 * Gets a single album
557 * @param $args (blog_id, username, password, album_id)
559 function get_album($args, $return_model=FALSE)
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]);
567 // Authenticate the user
568 if ($this->_login($username, $password, $blog_id)) {
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
578 $album->id = (string) $album->id;
579 $album->galleries = $album->sortorder;
581 $retval = $return_model ? $album : $album->get_entity();
583 else $retval = FALSE;
586 else $retval = new IXR_Error(403, "Sorry, you must be able to manage albums");
593 * Deletes an existing album
594 * @param $args (blog_id, username, password, album_id)
596 function delete_album($args)
598 $retval = $this->get_album($args, TRUE);
600 if (!($retval instanceof IXR_Error)) {
601 $retval = $retval->destroy();
608 * Edit an existing album
609 * @param $args (blog_id, username, password, album_id, name, preview pic id, description, galleries)
611 function edit_album($args)
613 $retval = $this->get_album($args, TRUE);
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];
621 $properties = isset($args[8]) ? $args[8] : array();
622 foreach ($properties as $key => $value) $retval->$key = $value;
623 unset($retval->galleries);
625 $retval = $retval->save();
632 new M_NextGen_XmlRpc;