/dd-includes/functions.core.php

https://github.com/DatumDroid/DatumDroid-API · PHP · 254 lines · 103 code · 44 blank · 107 comment · 20 complexity · 3eecc71af4eb7c094d13cfee30415a97 MD5 · raw file

  1. <?php
  2. /**
  3. * @package DatumDroid_API
  4. * @subpackage Core Functions
  5. */
  6. // Exit if accessed directly
  7. if ( !defined( 'DD_DIR' ) ) exit;
  8. /** _get functions ************************************************************/
  9. /**
  10. * Returns the search query
  11. *
  12. * @global string $dd_query Search Query
  13. * @return string Search Query
  14. */
  15. function dd_get_query() {
  16. global $dd_query;
  17. return $dd_query;
  18. }
  19. /**
  20. * Returns the per page option
  21. *
  22. * Min - 1
  23. * Max - {@see DD_MAX_PER_PAGE}
  24. *
  25. * @param string $service The service requested. If empty, the all are returned
  26. * @global int $dd_per_page Per page option
  27. * @return int Per page Option
  28. */
  29. function dd_get_per_page( $service = '' ) {
  30. global $dd_per_page;
  31. $service = dd_get_service( $service );
  32. $per_page = ( empty( $_REQUEST[$service] ) || ( $_REQUEST[$service] == 1 && isset( $dd_per_page ) ) ) ? $dd_per_page : $_REQUEST[$service];
  33. if ( $per_page < 1 )
  34. return 1;
  35. elseif ( $per_page > DD_MAX_PER_PAGE )
  36. return DD_MAX_PER_PAGE;
  37. else
  38. return $per_page;
  39. }
  40. /**
  41. * Returns the current page number
  42. *
  43. * @global int $dd_page Current page number
  44. * @return int Current page number
  45. */
  46. function dd_get_page() {
  47. global $dd_page;
  48. return $dd_page;
  49. }
  50. /**
  51. * Returns the language (like en)
  52. *
  53. * @global string $dd_lang Current language
  54. * @return string Language
  55. */
  56. function dd_get_lang() {
  57. global $dd_lang;
  58. return $dd_lang;
  59. }
  60. /**
  61. * Returns the api key for the requested service
  62. *
  63. * @param string $service The service requested. If empty, the all are returned
  64. * @global string $dd_api_keys API Keys
  65. * @return array|string API Key(s) (for the requested service)
  66. */
  67. function dd_get_api_key( $service = '' ) {
  68. global $dd_api_keys;
  69. if ( empty( $service ) )
  70. return $dd_api_keys;
  71. return !empty( $dd_api_keys[$service] ) ? $dd_api_keys[$service] : '';
  72. }
  73. /** Loaders & setters *********************************************************/
  74. /**
  75. * Gets the current service
  76. *
  77. * @param string $service Service name
  78. * @global string $dd_current_service Current service
  79. * @return string Current service
  80. */
  81. function dd_get_service( $service = '' ) {
  82. global $dd_current_service, $dd_services;
  83. if ( array_key_exists( $service, $dd_services ) )
  84. return $service;
  85. else
  86. return $dd_current_service;
  87. }
  88. /**
  89. * Set the current service global to the sent parameter
  90. *
  91. * @param string $service Service name
  92. * @global string $dd_current_service Current service
  93. * @global array $dd_services Registered services
  94. */
  95. function dd_set_service( $service = '' ) {
  96. global $dd_current_service, $dd_services;
  97. if ( array_key_exists( $service, $dd_services ) )
  98. $dd_current_service = $service;
  99. else
  100. dd_reset_service();
  101. }
  102. /**
  103. * Reset the current service global to empty string
  104. *
  105. * @global string $dd_current_service Current service
  106. */
  107. function dd_reset_service() {
  108. global $dd_current_service;
  109. $dd_current_service = '';
  110. }
  111. /**
  112. * Load the class file for a service
  113. *
  114. * @param string $service Service name
  115. */
  116. function dd_load_service( $service = '' ) {
  117. require_once( DD_DIR_INC . 'service.' . dd_get_service( $service ) . '.php' );
  118. }
  119. /** Formatting/misc ***********************************************************/
  120. /**
  121. * Returns the url query as associative array
  122. *
  123. * @param string $url URL
  124. * @return array Associative array of query
  125. */
  126. function dd_convert_url_query( $url = '' ) {
  127. $query = parse_url( $url, PHP_URL_QUERY );
  128. $query = html_entity_decode( $query );
  129. $queryParts = explode( '&', $query );
  130. $params = array();
  131. foreach ( $queryParts as $param ) {
  132. $item = explode( '=', $param );
  133. $params[$item[0]] = isset( $item[1] ) ? $item[1] : null;
  134. }
  135. return $params;
  136. }
  137. /**
  138. * Merge user defined arguments into defaults array.
  139. *
  140. * This function is used throughout WordPress to allow for both string or array
  141. * to be merged into another array.
  142. *
  143. * @param string|array $args Value to merge with $defaults
  144. * @param array $defaults Array that serves as the defaults.
  145. * @return array Merged user defined values with defaults.
  146. */
  147. function dd_parse_args( $args, $defaults = '' ) {
  148. if ( is_object( $args ) ) {
  149. $r = get_object_vars( $args );
  150. } elseif ( is_array( $args ) ) {
  151. $r =& $args;
  152. } else {
  153. //wp_parse_str( $args, $r );
  154. // Parses a string into variables to be stored in an array.
  155. parse_str( $args, $r );
  156. if ( get_magic_quotes_gpc() )
  157. $r = stripslashes_deep( $r );
  158. }
  159. if ( is_array( $defaults ) )
  160. return array_merge( $defaults, $r );
  161. return $r;
  162. }
  163. /** Search & Output ***********************************************************/
  164. /**
  165. * Perform the search
  166. *
  167. * @return array An array of results, keys are services, values are results
  168. */
  169. function dd_search() {
  170. global $dd_services;
  171. // Results array, which would be outputted as json encoded later
  172. $dd_results = array();
  173. // Get the services in the $dd_services array and check if they are required
  174. // If yes, add their results to the results array
  175. foreach ( (array) $dd_services as $service => $service_name ) {
  176. if ( ( !empty( $_REQUEST['all'] ) && $_REQUEST['all'] == 1 ) || isset( $_REQUEST[$service] ) ) {
  177. // Require the search results fetcher file
  178. dd_set_service( $service );
  179. dd_load_service();
  180. $service_class = 'DD_Service_' . $service_name;
  181. $search = new $service_class();
  182. $dd_results[dd_get_service()] = $search->search();
  183. }
  184. }
  185. return $dd_results;
  186. }
  187. /**
  188. * Output the results.
  189. *
  190. * If DD_DEBUG is true, output is just a print_r of the array.
  191. * Otherwise it is outputted after being json_encoded and proper headers being
  192. * sent.
  193. *
  194. * @param array $results The results to be outputted
  195. */
  196. function dd_output( $results = array() ) {
  197. // If none has been requested, add an error message
  198. if ( empty( $results ) )
  199. $results = array( 'responseDetails' => 'no service requested', 'responseStatus' => 400 );
  200. if ( true == DD_DEBUG ) {
  201. // If we're in debug mode, print in human-readable form
  202. echo '<pre>'; print_r( $results ); echo '</pre>';
  203. } else {
  204. // Set the correct MIME type for JSON.
  205. header( 'Content-type: application/json' );
  206. echo json_encode( $results );
  207. }
  208. exit;
  209. }
  210. ?>