C_NextGen_API_XMLRPC

Class C_NextGen_API_XMLRPC.

Defined (1)

The class is defined in the following location(s).

/products/photocrati_nextgen/modules/nextgen_xmlrpc/package.module.nextgen_xmlrpc.php  
  1. class C_NextGen_API_XMLRPC extends C_Component 
  2. public static $_instances = array(); 
  3. function define($context = false) 
  4. parent::define($context); 
  5. $this->implement('I_NextGen_API_XMLRPC'); 
  6. public static function get_instance($context = false) 
  7. if (!isset(self::$_instances[$context])) { 
  8. self::$_instances[$context] = new self($context); 
  9. return self::$_instances[$context]; 
  10. /** 
  11. * Gets the version of NextGEN Gallery installed 
  12. * @return string 
  13. */ 
  14. function get_version() 
  15. return array('version' => NGG_PLUGIN_VERSION); 
  16. /** 
  17. * Login a user 
  18. * @param $username 
  19. * @param $password 
  20. * @return bool|WP_Error|WP_User 
  21. */ 
  22. function _login($username, $password, $blog_id = 1) 
  23. $retval = FALSE; 
  24. if (!is_a($user_obj = wp_authenticate($username, $password), 'WP_Error')) { 
  25. wp_set_current_user($user_obj->ID); 
  26. $retval = $user_obj; 
  27. if (is_multisite()) { 
  28. switch_to_blog($blog_id); 
  29. return $retval; 
  30. function _can_manage_gallery($gallery_id_or_obj, $check_upload_capability = FALSE) 
  31. $retval = FALSE; 
  32. // Get the gallery object, if we don't have it already 
  33. $gallery = NULL; 
  34. if (is_int($gallery_id_or_obj)) { 
  35. $gallery_mapper = C_Gallery_Mapper::get_instance(); 
  36. $gallery = $gallery_mapper->find($gallery_id_or_obj); 
  37. } else { 
  38. $gallery = $gallery_id_or_obj; 
  39. if ($gallery) { 
  40. $security = $this->get_registry()->get_utility('I_Security_Manager'); 
  41. $actor = $security->get_current_actor(); 
  42. if ($actor->get_entity_id() == $gallery->author) { 
  43. $retval = TRUE; 
  44. } elseif ($actor->is_allowed('nextgen_edit_gallery_unowned')) { 
  45. $retval = TRUE; 
  46. // Optionally, check if the user can upload to this gallery 
  47. if ($retval && $check_upload_capability) { 
  48. $retval = $actor->is_allowed('nextgen_upload_image'); 
  49. return $retval; 
  50. function _add_gallery_properties($gallery) 
  51. if (is_object($gallery)) { 
  52. $image_mapper = C_Image_Mapper::get_instance(); 
  53. $storage = C_Gallery_Storage::get_instance(); 
  54. // Vladimir's Lightroom plugins requires the 'id' to be a string 
  55. // Ask if he can accept integers as well. Currently, integers break 
  56. // his plugin 
  57. $gallery->gid = (string) $gallery->gid; 
  58. // Set other gallery properties 
  59. $image_counter = array_pop($image_mapper->select('DISTINCT COUNT(*) as counter')->where(array("galleryid = %d", $gallery->gid))->run_query(FALSE, FALSE, TRUE)); 
  60. $gallery->counter = $image_counter->counter; 
  61. $gallery->abspath = $storage->get_gallery_abspath($gallery); 
  62. } else { 
  63. return FALSE; 
  64. return TRUE; 
  65. /** 
  66. * Returns a single image object 
  67. * @param $args (blog_id, username, password, pid) 
  68. */ 
  69. function get_image($args, $return_model = FALSE) 
  70. $retval = new IXR_Error(403, 'Invalid username or password'); 
  71. $blog_id = intval($args[0]); 
  72. $username = strval($args[1]); 
  73. $password = strval($args[2]); 
  74. $image_id = intval($args[3]); 
  75. // Authenticate the user 
  76. if ($this->_login($username, $password, $blog_id)) { 
  77. // Try to find the image 
  78. $image_mapper = C_Image_Mapper::get_instance(); 
  79. if ($image = $image_mapper->find($image_id, TRUE)) { 
  80. // Try to find the gallery that the image belongs to 
  81. $gallery_mapper = C_Gallery_Mapper::get_instance(); 
  82. if ($gallery = $gallery_mapper->find($image->galleryid)) { 
  83. // Does the user have sufficient capabilities? 
  84. if ($this->_can_manage_gallery($gallery)) { 
  85. $storage = C_Gallery_Storage::get_instance(); 
  86. $image->imageURL = $storage->get_image_url($image, 'full', TRUE); 
  87. $image->thumbURL = $storage->get_thumb_url($image, TRUE); 
  88. $image->imagePath = $storage->get_image_abspath($image); 
  89. $image->thumbPath = $storage->get_thumb_abspath($image); 
  90. $retval = $return_model ? $image : $image->get_entity(); 
  91. } else { 
  92. $retval = new IXR_Error(403, "You don't have permission to manage gallery #{$image->galleryid}"); 
  93. } else { 
  94. // No gallery found 
  95. $retval = new IXR_Error(404, "Gallery not found (with id #{$image->gallerid})"); 
  96. } else { 
  97. // No image found 
  98. $retval = new IXR_Error(404, "Image not found (with id #{$image_id})"); 
  99. return $retval; 
  100. /** 
  101. * Returns a collection of images 
  102. * @param $args (blog_id, username, password, gallery_id 
  103. */ 
  104. function get_images($args) 
  105. $retval = new IXR_Error(403, 'Invalid username or password'); 
  106. $blog_id = intval($args[0]); 
  107. $username = strval($args[1]); 
  108. $password = strval($args[2]); 
  109. $gallery_id = intval($args[3]); 
  110. // Authenticate the user 
  111. if ($this->_login($username, $password, $blog_id)) { 
  112. // Try to find the gallery 
  113. $mapper = C_Gallery_Mapper::get_instance(); 
  114. if ($gallery = $mapper->find($gallery_id, TRUE)) { 
  115. // Does the user have sufficient capabilities? 
  116. if ($this->_can_manage_gallery($gallery)) { 
  117. $retval = $gallery->get_images(); 
  118. } else { 
  119. $retval = new IXR_Error(403, "You don't have permission to manage gallery #{$image->galleryid}"); 
  120. } else { 
  121. $retval = new IXR_Error(404, "Gallery not found (with id #{$image->gallerid}"); 
  122. return $retval; 
  123. /** 
  124. * Uploads an image to a particular gallery 
  125. * @param $args (blog_id, username, password, data) 
  126. * Data is an assoc array: 
  127. * o string name 
  128. * o string type (optional) 
  129. * o base64 bits 
  130. * o bool overwrite (optional) 
  131. * o int gallery 
  132. * o int image_id (optional) 
  133. * @return image 
  134. */ 
  135. function upload_image($args) 
  136. $retval = new IXR_Error(403, 'Invalid username or password'); 
  137. $blog_id = intval($args[0]); 
  138. $username = strval($args[1]); 
  139. $password = strval($args[2]); 
  140. $data = $args[3]; 
  141. $gallery_id = isset($data['gallery_id']) ? $data['gallery_id'] : $data['gallery']; 
  142. if (!isset($data['override'])) { 
  143. $data['override'] = FALSE; 
  144. if (!isset($data['overwrite'])) { 
  145. $data['overwrite'] = FALSE; 
  146. $data['override'] = $data['overwrite']; 
  147. // Authenticate the user 
  148. if ($this->_login($username, $password, $blog_id)) { 
  149. // Try to find the gallery 
  150. $mapper = C_Gallery_Mapper::get_instance(); 
  151. if ($gallery = $mapper->find($gallery_id, TRUE)) { 
  152. // Does the user have sufficient capabilities? 
  153. if ($this->_can_manage_gallery($gallery, TRUE)) { 
  154. // Upload the image 
  155. $storage = C_Gallery_Storage::get_instance(); 
  156. $image = $storage->upload_base64_image($gallery, $data['bits'], $data['name'], $data['image_id'], $data['override']); 
  157. if ($image) { 
  158. $storage = C_Gallery_Storage::get_instance(); 
  159. $image->imageURL = $storage->get_image_url($image); 
  160. $image->thumbURL = $storage->get_thumb_url($image); 
  161. $image->imagePath = $storage->get_image_abspath($image); 
  162. $image->thumbPath = $storage->get_thumb_abspath($image); 
  163. $retval = $image->get_entity(); 
  164. } else { 
  165. $retval = new IXR_Error(500, "Could not upload image"); 
  166. } else { 
  167. $retval = new IXR_Error(403, "You don't have permission to upload to gallery #{$image->galleryid}"); 
  168. } else { 
  169. $retval = new IXR_Error(404, "Gallery not found (with id #{$image->gallerid}"); 
  170. return $retval; 
  171. /** 
  172. * Edits an image object 
  173. * @param $args (blog_id, username, password, image_id, alttext, description, exclude, other_properties 
  174. */ 
  175. function edit_image($args) 
  176. $alttext = strval($args[4]); 
  177. $description = strval($args[5]); 
  178. $exclude = intval($args[6]); 
  179. $properties = isset($args[7]) ? (array) $args[7] : array(); 
  180. $retval = $this->get_image($args, TRUE); 
  181. if (!$retval instanceof IXR_Error) { 
  182. $retval->alttext = $alttext; 
  183. $retval->description = $description; 
  184. $retval->exclude = $exclude; 
  185. // Other properties can be specified using an associative array 
  186. foreach ($properties as $key => $value) { 
  187. $retval->{$key} = $value; 
  188. // Unset any dynamic properties not part of the schema 
  189. foreach (array('imageURL', 'thumbURL', 'imagePath', 'thumbPath') as $key) { 
  190. unset($retval->{$key}); 
  191. $retval = $retval->save(); 
  192. return $retval; 
  193. /** 
  194. * Deletes an existing image from a gallery 
  195. * @param $args (blog_id, username, password, image_id) 
  196. */ 
  197. function delete_image($args) 
  198. $retval = $this->get_image($args, TRUE); 
  199. if (!$retval instanceof IXR_Error) { 
  200. $retval = $retval->destroy(); 
  201. return $retval; 
  202. /** 
  203. * Creates a new gallery 
  204. * @param $args (blog_id, username, password, title) 
  205. */ 
  206. function create_gallery($args) 
  207. $retval = new IXR_Error(403, 'Invalid username or password'); 
  208. $blog_id = intval($args[0]); 
  209. $username = strval($args[1]); 
  210. $password = strval($args[2]); 
  211. $title = strval($args[3]); 
  212. // Authenticate the user 
  213. if ($this->_login($username, $password, $blog_id)) { 
  214. $security = $this->get_registry()->get_utility('I_Security_Manager'); 
  215. if ($security->is_allowed('nextgen_edit_gallery')) { 
  216. $mapper = C_Gallery_Mapper::get_instance(); 
  217. if (($gallery = $mapper->create(array('title' => $title))) && $gallery->save()) { 
  218. $retval = $gallery->id(); 
  219. } else { 
  220. $retval = new IXR_Error(500, "Unable to create gallery"); 
  221. } else { 
  222. $retval = new IXR_Error(403, "Sorry, but you must be able to manage galleries. Check your roles/capabilities."); 
  223. return $retval; 
  224. /** 
  225. * Edits an existing gallery 
  226. * @param $args (blog_id, username, password, gallery_id, name, title, description, preview_pic_id) 
  227. */ 
  228. function edit_gallery($args) 
  229. $retval = new IXR_Error(403, 'Invalid username or password'); 
  230. $blog_id = intval($args[0]); 
  231. $username = strval($args[1]); 
  232. $password = strval($args[2]); 
  233. $gallery_id = intval($args[3]); 
  234. $name = strval($args[4]); 
  235. $title = strval($args[5]); 
  236. $galdesc = strval($args[6]); 
  237. $image_id = intval($args[7]); 
  238. $properties = isset($args[8]) ? (array) $args[8] : array(); 
  239. // Authenticate the user 
  240. if ($this->_login($username, $password, $blog_id)) { 
  241. $mapper = C_Gallery_Mapper::get_instance(); 
  242. if ($gallery = $mapper->find($gallery_id, TRUE)) { 
  243. if ($this->_can_manage_gallery($gallery)) { 
  244. $gallery->name = $name; 
  245. $gallery->title = $title; 
  246. $gallery->galdesc = $galdesc; 
  247. $gallery->previewpic = $image_id; 
  248. foreach ($properties as $key => $value) { 
  249. $gallery->{$key} = $value; 
  250. // Unset dynamic properties not part of the schema 
  251. unset($gallery->counter); 
  252. unset($gallery->abspath); 
  253. $retval = $gallery->save(); 
  254. } else { 
  255. $retval = new IXR_Error(403, "You don't have permission to modify this gallery"); 
  256. } else { 
  257. $retval = new IXR_Error(404, "Gallery #{$gallery_id} doesn't exist"); 
  258. return $retval; 
  259. /** 
  260. * Returns all galleries 
  261. * @param $args (blog_id, username, password) 
  262. */ 
  263. function get_galleries($args) 
  264. $retval = new IXR_Error(403, 'Invalid username or password'); 
  265. $blog_id = intval($args[0]); 
  266. $username = strval($args[1]); 
  267. $password = strval($args[2]); 
  268. // Authenticate the user 
  269. if ($this->_login($username, $password, $blog_id)) { 
  270. // Do we have permission? 
  271. $security = $this->get_registry()->get_utility('I_Security_Manager'); 
  272. if ($security->is_allowed('nextgen_edit_gallery')) { 
  273. $mapper = C_Gallery_Mapper::get_instance(); 
  274. $retval = array(); 
  275. foreach ($mapper->find_all() as $gallery) { 
  276. $this->_add_gallery_properties($gallery); 
  277. $retval[$gallery->{$gallery->id_field}] = (array) $gallery; 
  278. } else { 
  279. $retval = new IXR_Error(401, __('Sorry, you must be able to manage galleries')); 
  280. return $retval; 
  281. /** 
  282. * Gets a single gallery instance 
  283. * @param $args (blog_id, username, password, gallery_id) 
  284. */ 
  285. function get_gallery($args, $return_model = FALSE) 
  286. $retval = new IXR_Error(403, 'Invalid username or password'); 
  287. $blog_id = intval($args[0]); 
  288. $username = strval($args[1]); 
  289. $password = strval($args[2]); 
  290. $gallery_id = intval($args[3]); 
  291. // Authenticate the user 
  292. if ($this->_login($username, $password, $blog_id)) { 
  293. $mapper = C_Gallery_Mapper::get_instance(); 
  294. if ($gallery = $mapper->find($gallery_id, TRUE)) { 
  295. if ($this->_can_manage_gallery($gallery)) { 
  296. $this->_add_gallery_properties($gallery); 
  297. $retval = $return_model ? $gallery : $gallery->get_entity(); 
  298. } else { 
  299. $retval = new IXR_Error(403, "Sorry, but you don't have permission to manage gallery #{$gallery->gid}"); 
  300. } else { 
  301. $retval = FALSE; 
  302. return $retval; 
  303. /** 
  304. * Deletes a gallery 
  305. * @param $args (blog_id, username, password, gallery_id) 
  306. */ 
  307. function delete_gallery($args) 
  308. $retval = $this->get_gallery($args, TRUE); 
  309. if (!$retval instanceof IXR_Error and is_object($retval)) { 
  310. $retval = $retval->destroy(); 
  311. return $retval; 
  312. /** 
  313. * Creates a new album 
  314. * @param $args (blog_id, username, password, title, previewpic, description, galleries 
  315. */ 
  316. function create_album($args) 
  317. $retval = new IXR_Error(403, 'Invalid username or password'); 
  318. $blog_id = intval($args[0]); 
  319. $username = strval($args[1]); 
  320. $password = strval($args[2]); 
  321. $title = strval($args[3]); 
  322. $previewpic = isset($args[4]) ? intval($args[4]) : 0; 
  323. $desc = isset($args[5]) ? strval($args[5]) : ''; 
  324. $sortorder = isset($args[6]) ? $args[6] : ''; 
  325. $page_id = isset($args[7]) ? intval($args[7]) : 0; 
  326. // Authenticate the user 
  327. if ($this->_login($username, $password, $blog_id)) { 
  328. // Is request allowed? 
  329. $security = $this->get_registry()->get_utility('I_Security_Manager'); 
  330. if ($security->is_allowed('nextgen_edit_album')) { 
  331. $mapper = C_Album_Mapper::get_instance(); 
  332. $album = $mapper->create(array('name' => $title, 'previewpic' => $previewpic, 'albumdesc' => $desc, 'sortorder' => $sortorder, 'pageid' => $page_id)); 
  333. if ($album->save()) { 
  334. $retval = $album->id(); 
  335. } else { 
  336. $retval = new IXR_Error(500, "Unable to create album"); 
  337. return $retval; 
  338. /** 
  339. * Returns all albums 
  340. * @param $args (blog_id, username, password) 
  341. * @return IXR_Error 
  342. */ 
  343. function get_albums($args) 
  344. $retval = new IXR_Error(403, 'Invalid username or password'); 
  345. $blog_id = intval($args[0]); 
  346. $username = strval($args[1]); 
  347. $password = strval($args[2]); 
  348. // Authenticate the user 
  349. if ($this->_login($username, $password, $blog_id)) { 
  350. // Are we allowed? 
  351. $security = $this->get_registry()->get_utility('I_Security_Manager'); 
  352. if ($security->is_allowed('nextgen_edit_album')) { 
  353. // Fetch all albums 
  354. $mapper = C_Album_Mapper::get_instance(); 
  355. $retval = array(); 
  356. foreach ($mapper->find_all() as $album) { 
  357. // Vladimir's Lightroom plugins requires the 'id' to be a string 
  358. // Ask if he can accept integers as well. Currently, integers break 
  359. // his plugin 
  360. $album->id = (string) $album->id; 
  361. $album->galleries = $album->sortorder; 
  362. $retval[$album->{$album->id_field}] = (array) $album; 
  363. } else { 
  364. $retval = new IXR_Error(403, "Sorry, you must be able to manage albums"); 
  365. return $retval; 
  366. /** 
  367. * Gets a single album 
  368. * @param $args (blog_id, username, password, album_id) 
  369. */ 
  370. function get_album($args, $return_model = FALSE) 
  371. $retval = new IXR_Error(403, 'Invalid username or password'); 
  372. $blog_id = intval($args[0]); 
  373. $username = strval($args[1]); 
  374. $password = strval($args[2]); 
  375. $album_id = intval($args[3]); 
  376. // Authenticate the user 
  377. if ($this->_login($username, $password, $blog_id)) { 
  378. // Are we allowed? 
  379. $security = $this->get_registry()->get_utility('I_Security_Manager'); 
  380. if ($security->is_allowed('nextgen_edit_album')) { 
  381. $mapper = C_Album_Mapper::get_instance(); 
  382. if ($album = $mapper->find($album_id, TRUE)) { 
  383. // Vladimir's Lightroom plugins requires the 'id' to be a string 
  384. // Ask if he can accept integers as well. Currently, integers break 
  385. // his plugin 
  386. $album->id = (string) $album->id; 
  387. $album->galleries = $album->sortorder; 
  388. $retval = $return_model ? $album : $album->get_entity(); 
  389. } else { 
  390. $retval = FALSE; 
  391. } else { 
  392. $retval = new IXR_Error(403, "Sorry, you must be able to manage albums"); 
  393. return $retval; 
  394. /** 
  395. * Deletes an existing album 
  396. * @param $args (blog_id, username, password, album_id) 
  397. */ 
  398. function delete_album($args) 
  399. $retval = $this->get_album($args, TRUE); 
  400. if (!$retval instanceof IXR_Error) { 
  401. $retval = $retval->destroy(); 
  402. return $retval; 
  403. /** 
  404. * Edit an existing album 
  405. * @param $args (blog_id, username, password, album_id, name, preview pic id, description, galleries) 
  406. */ 
  407. function edit_album($args) 
  408. $retval = $this->get_album($args, TRUE); 
  409. if (!$retval instanceof IXR_Error) { 
  410. $retval->name = strval($args[4]); 
  411. $retval->previewpic = intval($args[5]); 
  412. $retval->albumdesc = strval($args[6]); 
  413. $retval->sortorder = $args[7]; 
  414. $properties = isset($args[8]) ? $args[8] : array(); 
  415. foreach ($properties as $key => $value) { 
  416. $retval->{$key} = $value; 
  417. unset($retval->galleries); 
  418. $retval = $retval->save(); 
  419. return $retval; 
  420. /** 
  421. * Sets the post thumbnail for a post to a NextGEN Gallery image 
  422. * @param $args (blog_id, username, password, post_id, image_id) 
  423. * @return IXR_Error|int attachment id 
  424. */ 
  425. function set_post_thumbnail($args) 
  426. $retval = new IXR_Error(403, 'Invalid username or password'); 
  427. $blog_id = intval($args[0]); 
  428. $username = strval($args[1]); 
  429. $password = strval($args[2]); 
  430. $post_ID = intval($args[3]); 
  431. $image_id = intval($args[4]); 
  432. // Authenticate the user 
  433. if ($this->_login($username, $password, $blog_id)) { 
  434. if (current_user_can('edit_post', $post_ID)) { 
  435. $retval = C_Gallery_Storage::get_instance()->set_post_thumbnail($post_ID, $image_id); 
  436. } else { 
  437. $retval = new IXR_Error(403, "Sorry but you need permission to do this"); 
  438. return $retval;