PageRenderTime 40ms CodeModel.GetById 13ms RepoModel.GetById 0ms app.codeStats 0ms

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

https://gitlab.com/blueprintmrk/bladencountyrecords
PHP | 274 lines | 167 code | 38 blank | 69 comment | 22 complexity | 25184865001012f0d17b3d604a15072a 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. * @copyright Copyright 2010
  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 | tag | autocomplete
  23. var $term = false; // $_GET['term'] : The search term (required for method search | tag)
  24. var $id = false; // $_GET['id'] : gallery or image id (required for method gallery | image)
  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 'gallery' :
  75. //search for some gallery
  76. $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 );
  77. break;
  78. case 'image' :
  79. //search for some image
  80. $this->result['images'] = nggdb::find_image( $this->id );
  81. break;
  82. case 'tag' :
  83. //search for images based on tags
  84. $this->result['images'] = nggTags::find_images_for_tags( $this->term , 'ASC' );
  85. break;
  86. case 'recent' :
  87. //search for images based on tags
  88. $this->result['images'] = nggdb::find_last_images( 0 , $this->limit );
  89. break;
  90. case 'autocomplete' :
  91. //return images, galleries or albums for autocomplete drop down list
  92. return $this->autocomplete();
  93. break;
  94. case 'version' :
  95. $this->result = array ('stat' => 'ok', 'version' => $ngg->version);
  96. return;
  97. break;
  98. default :
  99. $this->result = array ('stat' => 'fail', 'code' => '98', 'message' => 'Method not known.');
  100. return false;
  101. break;
  102. }
  103. // result should be fine
  104. $this->result['stat'] = 'ok';
  105. }
  106. function valid_access() {
  107. // if we are logged in, then we can go on
  108. if ( is_user_logged_in() )
  109. return true;
  110. //TODO:Implement an API KEY check later
  111. if ($this->api_key != false)
  112. return true;
  113. $this->result = array ('stat' => 'fail', 'code' => '99', 'message' => 'Insufficient permissions. Method requires read privileges; none granted.');
  114. return false;
  115. }
  116. /**
  117. * return search result for autocomplete request from backend
  118. *
  119. * @since 1.7.0
  120. * @return void
  121. */
  122. function autocomplete() {
  123. global $nggdb;
  124. switch ( $this->type ) {
  125. case 'image' :
  126. // return the last entries in case of an empty search string
  127. if ( empty($this->term) )
  128. $list = $nggdb->find_last_images(0, $this->limit, false);
  129. else
  130. $list = $nggdb->search_for_images($this->term, $this->limit);
  131. if( is_array($list) ) {
  132. foreach($list as $image) {
  133. // reorder result to array-object
  134. $obj = new stdClass();
  135. $obj->id = $image->pid;
  136. $name = ( empty($image->alttext) ? $image->filename : $image->alttext );
  137. //TODO : need to rework save/load
  138. $name = stripslashes( htmlspecialchars_decode($name, ENT_QUOTES));
  139. $obj->label = $image->pid . ' - ' . $name;
  140. $obj->value = $name;
  141. $this->result[] = $obj;
  142. }
  143. }
  144. return $this->result;
  145. break;
  146. case 'gallery' :
  147. if ( empty($this->term) )
  148. $list = $nggdb->find_all_galleries('gid', 'DESC', false, $this->limit );
  149. else
  150. $list = $nggdb->search_for_galleries($this->term, $this->limit);
  151. if( is_array($list) ) {
  152. foreach($list as $gallery) {
  153. // reorder result to array-object
  154. $obj = new stdClass();
  155. $obj->id = $gallery->gid;
  156. $name = ( empty($gallery->title) ) ? $gallery->name : $gallery->title;
  157. $name = stripslashes( htmlspecialchars_decode($name, ENT_QUOTES));
  158. $obj->label = $gallery->gid . ' - ' . $name;
  159. $obj->value = $name;
  160. $this->result[] = $obj;
  161. }
  162. }
  163. return $this->result;
  164. break;
  165. case 'album' :
  166. if ( empty($this->term) )
  167. $list = $nggdb->find_all_album('id', 'DESC', $this->limit );
  168. else
  169. $list = $nggdb->search_for_albums($this->term, $this->limit);
  170. if( is_array($list) ) {
  171. foreach($list as $album) {
  172. // reorder result to array-object
  173. $obj = new stdClass();
  174. $obj->id = $album->id;
  175. $album->name = stripslashes( htmlspecialchars_decode($album->name, ENT_QUOTES));
  176. $obj->label = $album->id . ' - ' . $album->name;
  177. $obj->value = $album->name;
  178. $this->result[] = $obj;
  179. }
  180. }
  181. return $this->result;
  182. break;
  183. default :
  184. $this->result = array ('stat' => 'fail', 'code' => '98', 'message' => 'Type not known.');
  185. return false;
  186. break;
  187. }
  188. }
  189. /**
  190. * Iterates through a multidimensional array
  191. *
  192. * @author Boris Glumpler
  193. * @param array $arr
  194. * @return void
  195. */
  196. function create_xml_array( &$arr )
  197. {
  198. if( is_object( $arr ) )
  199. $arr = get_object_vars( $arr );
  200. foreach( (array)$arr as $k => $v ) {
  201. if( is_object( $v ) )
  202. $v = get_object_vars( $v );
  203. if( ! is_array( $v ) )
  204. $xml .= "<$k>$v</$k>\n";
  205. else
  206. {
  207. if( is_numeric( $k ) )
  208. $k = 'job';
  209. $xml .= "<$k>\n";
  210. $xml .= $this->create_xml_array( $v );
  211. $xml .= "</$k>\n";
  212. }
  213. }
  214. return $xml;
  215. }
  216. function render_output() {
  217. if ($this->format == 'json') {
  218. header('Content-Type: application/json; charset=' . get_option('blog_charset'), true);
  219. $this->output = json_encode($this->result);
  220. } else {
  221. header('Content-Type: text/xml; charset=' . get_option('blog_charset'), true);
  222. $this->output = "<?xml version='1.0' encoding='UTF-8' standalone='yes'?>\n";
  223. $this->output .= "<nextgen-gallery>" . $this->create_xml_array( $this->result ) . "</nextgen-gallery>\n";
  224. }
  225. }
  226. /**
  227. * PHP5 style destructor and will run when the class is finished.
  228. *
  229. * @return output
  230. */
  231. function __destruct() {
  232. echo $this->output;
  233. }
  234. }
  235. // let's use it
  236. $nggAPI = new nggAPI;