PageRenderTime 31ms CodeModel.GetById 22ms RepoModel.GetById 1ms app.codeStats 0ms

/wp-content/plugins/nextgen-gallery/xml/json.php

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