nggAPI

REST Application Programming Interface PHP class for the WordPress plugin NextGEN Gallery Should emulate some kind of Flickr JSON callback : ?callback=json&format=json&api_key=1234567890&method=search&term=myterm.

Defined (1)

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

/xml/json.php  
  1. class nggAPI { 
  2.  
  3. /** 
  4. * $_GET Variables  
  5. *  
  6. * @since 1.5.0 
  7. * @access private 
  8. * @var string 
  9. */ 
  10. var $format = false; // $_GET['format'] : Return a XML oder JSON output 
  11. var $api_key = false; // $_GET['api_key'] : Protect the access via a random key (required if user is not logged into backend) 
  12. var $method = false; // $_GET['method'] : search | gallery | image | album | tag | autocomplete 
  13. var $term = false; // $_GET['term'] : The search term (required for method search | tag) 
  14. var $id = false; // $_GET['id'] : object id (required for method gallery | image | album ) 
  15. var $limit = false; // $_GET['limit'] : maximum of images which we request 
  16. var $type = false; // $_GET['type'] : gallery | image | album (required for method autocomplete) 
  17.  
  18. /** 
  19. * Contain the final output 
  20. * @since 1.5.0 
  21. * @access private 
  22. * @var string 
  23. */  
  24. var $output = ''; 
  25.  
  26. /** 
  27. * Holds the requested information as array 
  28. * @since 1.5.0 
  29. * @access private 
  30. * @var array 
  31. */  
  32. var $result = ''; 
  33.  
  34. /** 
  35. * Init the variables 
  36. *  
  37. */  
  38. function __construct() { 
  39.  
  40. if ( !defined('ABSPATH') ) 
  41. die('You are not allowed to call this page directly.'); 
  42.  
  43. if ( !function_exists('json_encode') ) 
  44. wp_die('Json_encode not available. You need to use PHP 5.2'); 
  45.  
  46. // Read the parameter on init 
  47. $this->format = isset($_GET['format']) ? strtolower( $_GET['format'] ) : false; 
  48. $this->api_key = isset($_GET['api_key'])? $_GET['api_key'] : false;  
  49. $this->method = isset($_GET['method']) ? strtolower( $_GET['method'] ) : false;  
  50. $this->term = isset($_GET['term']) ? urldecode( $_GET['term'] ) : false;  
  51. $this->id = isset($_GET['id']) ? (int) $_GET['id'] : 0; 
  52. $this->limit = isset($_GET['limit']) ? (int) $_GET['limit'] : 0; 
  53. $this->type = isset($_GET['type']) ? strtolower( $_GET['type'] ) : false;  
  54. $this->result = array(); 
  55. $this->list = false; 
  56.  
  57. $this->start_process(); 
  58. $this->render_output(); 
  59.  
  60. function start_process() { 
  61.  
  62. global $ngg, $nggdb; 
  63.  
  64. if ( !$this->valid_access() )  
  65. return; 
  66.  
  67. switch ( $this->method ) { 
  68. case 'search' : 
  69. //search for some images 
  70. //20140106:shouldn't call it statically when is not... 
  71. //$this->result['images'] = array_merge( (array) nggdb::search_for_images( $this->term ), (array) nggTags::find_images_for_tags( $this->term , 'ASC' )); 
  72. $this->result['images'] = array_merge( (array) $nggdb->search_for_images( $this->term ), (array) nggTags::find_images_for_tags( $this->term , 'ASC' )); 
  73. break; 
  74. case 'album' : 
  75. //search for some album //TODO : Get images for each gallery, could end in a big db query 
  76. $this->result['album'] = nggdb::find_album( $this->id ); 
  77. break;  
  78. case 'gallery' : 
  79. //search for some gallery 
  80. //20140106:shouldn't call it statically when is not... 
  81. //$this->result['images'] = ($this->id == 0) ? nggdb::find_last_images( 0 , 100 ) : nggdb::get_gallery( $this->id, $ngg->options['galSort'], $ngg->options['galSortDir'], true, 0, 0, true ); 
  82. $this->result['images'] = ($this->id == 0) ? nggdb::find_last_images( 0 , 100 ) : $nggdb->get_gallery( $this->id, $ngg->options['galSort'], $ngg->options['galSortDir'], true, 0, 0, true ); 
  83. break; 
  84. case 'image' : 
  85. //search for some image 
  86. $this->result['images'] = nggdb::find_image( $this->id ); 
  87. break; 
  88. case 'tag' : 
  89. //search for images based on tags 
  90. $this->result['images'] = nggTags::find_images_for_tags( $this->term , 'ASC' ); 
  91. break; 
  92. case 'recent' : 
  93. //search for images based on tags 
  94. $this->result['images'] = nggdb::find_last_images( 0 , $this->limit ); 
  95. break; 
  96. case 'autocomplete' : 
  97. //return images, galleries or albums for autocomplete drop down list 
  98. return $this->autocomplete();  
  99. break; 
  100. case 'version' : 
  101. $this->result = array ('stat' => 'ok', 'version' => $ngg->version); 
  102. return;  
  103. break; 
  104. default : 
  105. $this->result = array ('stat' => 'fail', 'code' => '98', 'message' => 'Method not known.'); 
  106. return false;  
  107. break;  
  108.  
  109. // result should be fine  
  110. $this->result['stat'] = 'ok';  
  111.  
  112. function valid_access() { 
  113.  
  114. // if we are logged in, then we can go on 
  115. if ( is_user_logged_in() ) 
  116. return true; 
  117.  
  118. //TODO:Implement an API KEY check later 
  119. if ($this->api_key != false) 
  120. return true; 
  121.  
  122. $this->result = array ('stat' => 'fail', 'code' => '99', 'message' => 'Insufficient permissions. Method requires read privileges; none granted.'); 
  123. return false; 
  124.  
  125. /** 
  126. * return search result for autocomplete request from backend 
  127. *  
  128. * @since 1.7.0 
  129. * @return void 
  130. */ 
  131. function autocomplete() { 
  132. global $nggdb; 
  133.  
  134. switch ( $this->type ) { 
  135. case 'image' : 
  136.  
  137. // return the last entries in case of an empty search string 
  138. if ( empty($this->term) ) 
  139. $list = $nggdb->find_last_images(0, $this->limit, false); 
  140. else 
  141. //$list = $nggdb->search_for_images($this->term, $this->limit); 
  142. $list = $nggdb->find_images_in_album($this->term); 
  143.  
  144. if( is_array($list) ) { 
  145. foreach($list as $image) { 
  146. // reorder result to array-object 
  147. $obj = new stdClass(); 
  148. $obj->id = $image->pid; 
  149. $name = ( empty($image->alttext) ? $image->filename : $image->alttext ); 
  150. //TODO : need to rework save/load  
  151. $name = stripslashes( htmlspecialchars_decode($name, ENT_QUOTES)); 
  152. $obj->label = $image->pid . ' - ' . $name; 
  153. $obj->value = $image->pid . ' - ' . $name; 
  154. $this->result[] = $obj; 
  155.  
  156. return $this->result; 
  157. break; 
  158. case 'gallery' : 
  159.  
  160. if ( empty($this->term) ) 
  161. $list = $nggdb->find_all_galleries('gid', 'DESC', false, $this->limit ); 
  162. else 
  163. $list = $nggdb->search_for_galleries($this->term, $this->limit);  
  164.  
  165. if( is_array($list) ) { 
  166. foreach($list as $gallery) { 
  167. // reorder result to array-object 
  168. $obj = new stdClass(); 
  169. $obj->id = $gallery->gid; 
  170. $name = ( empty($gallery->title) ) ? $gallery->name : $gallery->title; 
  171. $name = stripslashes( htmlspecialchars_decode($name, ENT_QUOTES)); 
  172. $obj->label = $gallery->gid . ' - ' . $name; 
  173. $obj->value = $name; 
  174. $this->result[] = $obj; 
  175. return $this->result; 
  176. break; 
  177. case 'album' : 
  178.  
  179. if ( empty($this->term) ) 
  180. $list = $nggdb->find_all_album('id', 'DESC', $this->limit ); 
  181. else 
  182. $list = $nggdb->search_for_albums($this->term, $this->limit);  
  183.  
  184. if( is_array($list) ) { 
  185. foreach($list as $album) { 
  186. // reorder result to array-object  
  187. $obj = new stdClass(); 
  188. $obj->id = $album->id; 
  189. $album->name = stripslashes( htmlspecialchars_decode($album->name, ENT_QUOTES)); 
  190. $obj->label = $album->id . ' - ' . $album->name; 
  191. $obj->value = $album->name; 
  192. $this->result[] = $obj; 
  193. return $this->result; 
  194. break; 
  195. default : 
  196. $this->result = array ('stat' => 'fail', 'code' => '98', 'message' => 'Type not known.'); 
  197. return false;  
  198. break;  
  199.  
  200. /** 
  201. * Iterates through a multidimensional array 
  202. *  
  203. * @author Boris Glumpler 
  204. * @param array $arr 
  205. * @return void 
  206. */ 
  207. function create_xml_array( &$arr ) 
  208. $xml = ''; 
  209.  
  210. if( is_object( $arr ) ) 
  211. $arr = get_object_vars( $arr ); 
  212.  
  213. foreach( (array)$arr as $k => $v ) { 
  214. if( is_object( $v ) ) 
  215. $v = get_object_vars( $v ); 
  216. //nodes must contain letters  
  217. if( is_numeric( $k ) ) 
  218. $k = 'id-'.$k;  
  219. if( is_array( $v ) ) 
  220. $xml .= "<$k>\n". $this->create_xml_array( $v ). "</$k>\n"; 
  221. else 
  222. $xml .= "<$k>$v</$k>\n"; 
  223.  
  224. return $xml; 
  225.  
  226. function render_output() { 
  227.  
  228. if ($this->format == 'json') { 
  229. header('Content-Type: application/json; charset=' . get_option('blog_charset'), true); 
  230. $this->output = json_encode($this->result); 
  231. } else { 
  232. header('Content-Type: text/xml; charset=' . get_option('blog_charset'), true); 
  233. $this->output = "<?xml version='1.0' encoding='UTF-8' standalone='yes'?>\n"; 
  234. $this->output .= "<nextgen-gallery>" . $this->create_xml_array( $this->result ) . "</nextgen-gallery>\n"; 
  235. }  
  236.  
  237.  
  238. /** 
  239. * PHP5 style destructor and will run when the class is finished. 
  240. * @return output 
  241. */ 
  242. function __destruct() { 
  243. echo $this->output; 
  244.