PageRenderTime 41ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 0ms

/wp-content/plugins/jetpack/class.json-api.php

https://github.com/sharpmachine/wakeupmedia.com
PHP | 371 lines | 291 code | 67 blank | 13 comment | 65 complexity | 222c23d6bbe9b6916643178e4c90a4c4 MD5 | raw file
  1. <?php
  2. defined( 'WPCOM_JSON_API__DEBUG' ) or define( 'WPCOM_JSON_API__DEBUG', false );
  3. class WPCOM_JSON_API {
  4. static $self = null;
  5. var $endpoints = array();
  6. var $token_details = array();
  7. var $method = '';
  8. var $url = '';
  9. var $path = '';
  10. var $query = array();
  11. var $post_body = null;
  12. var $files = null;
  13. var $content_type = null;
  14. var $accept = '';
  15. var $_server_https;
  16. var $exit = true;
  17. var $public_api_scheme = 'https';
  18. static function init( $method = null, $url = null, $post_body = null ) {
  19. if ( !self::$self ) {
  20. $class = function_exists( 'get_called_class' ) ? get_called_class() : __CLASS__;
  21. self::$self = new $class( $method, $url, $post_body );
  22. }
  23. return self::$self;
  24. }
  25. function add( WPCOM_JSON_API_Endpoint $endpoint ) {
  26. if ( !isset( $this->endpoints[$endpoint->path] ) ) {
  27. $this->endpoints[$endpoint->path] = array();
  28. }
  29. $this->endpoints[$endpoint->path][$endpoint->method] = $endpoint;
  30. }
  31. static function is_truthy( $value ) {
  32. switch ( strtolower( (string) $value ) ) {
  33. case '1' :
  34. case 't' :
  35. case 'true' :
  36. return true;
  37. }
  38. return false;
  39. }
  40. function __construct( $method = null, $url = null, $post_body = null ) {
  41. if ( is_null( $method ) ) {
  42. $this->method = strtoupper( $_SERVER['REQUEST_METHOD'] );
  43. } else {
  44. $this->method = strtoupper( $method );
  45. }
  46. if ( is_null( $url ) ) {
  47. $this->url = ( is_ssl() ? 'https' : 'http' ) . '://' . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'];
  48. } else {
  49. $this->url = $url;
  50. }
  51. $parsed = parse_url( $this->url );
  52. $this->path = $parsed['path'];
  53. if ( !empty( $parsed['query'] ) ) {
  54. wp_parse_str( $parsed['query'], $this->query );
  55. }
  56. if ( isset( $_SERVER['HTTP_ACCEPT'] ) && $_SERVER['HTTP_ACCEPT'] ) {
  57. $this->accept = $_SERVER['HTTP_ACCEPT'];
  58. }
  59. if ( 'POST' == $this->method ) {
  60. if ( is_null( $post_body ) ) {
  61. $this->post_body = file_get_contents( 'php://input' );
  62. if ( isset( $_SERVER['HTTP_CONTENT_TYPE'] ) && $_SERVER['HTTP_CONTENT_TYPE'] ) {
  63. $this->content_type = $_SERVER['HTTP_CONTENT_TYPE'];
  64. } elseif ( isset( $_SERVER['CONTENT_TYPE'] ) && $_SERVER['CONTENT_TYPE'] ) {
  65. $this->content_type = $_SERVER['CONTENT_TYPE'] ;
  66. } elseif ( '{' === $this->post_body[0] ) {
  67. $this->content_type = 'application/json';
  68. } else {
  69. $this->content_type = 'application/x-www-form-urlencoded';
  70. }
  71. if ( 0 === strpos( strtolower( $this->content_type ), 'multipart/' ) ) {
  72. $this->post_body = http_build_query( stripslashes_deep( $_POST ) );
  73. $this->files = $_FILES;
  74. $this->content_type = 'multipart/form-data';
  75. }
  76. } else {
  77. $this->post_body = $post_body;
  78. $this->content_type = '{' === $this->post_body[0] ? 'application/json' : 'application/x-www-form-urlencoded';
  79. }
  80. } else {
  81. $this->post_body = null;
  82. $this->content_type = null;
  83. }
  84. $this->_server_https = array_key_exists( 'HTTPS', $_SERVER ) ? $_SERVER['HTTPS'] : '--UNset--';
  85. }
  86. function initialize() {
  87. $this->token_details['blog_id'] = Jetpack::get_option( 'id' );
  88. }
  89. function serve( $exit = true ) {
  90. $this->exit = (bool) $exit;
  91. add_filter( 'home_url', array( $this, 'ensure_http_scheme_of_home_url' ), 10, 3 );
  92. add_filter( 'user_can_richedit', '__return_true' );
  93. add_filter( 'comment_edit_pre', array( $this, 'comment_edit_pre' ) );
  94. $this->initialize();
  95. // Normalize path
  96. $this->path = untrailingslashit( $this->path );
  97. $this->path = preg_replace( '#^/rest/v1#', '', $this->path );
  98. $allowed_methods = array( 'GET', 'POST' );
  99. $four_oh_five = false;
  100. $is_help = preg_match( '#/help/?$#i', $this->path );
  101. $matching_endpoints = array();
  102. if ( $is_help ) {
  103. $this->path = substr( rtrim( $this->path, '/' ), 0, -5 );
  104. // Show help for all matching endpoints regardless of method
  105. $methods = $allowed_methods;
  106. $find_all_matching_endpoints = true;
  107. // How deep to truncate each endpoint's path to see if it matches this help request
  108. $depth = substr_count( $this->path, '/' ) + 1;
  109. if ( false !== stripos( $this->accept, 'javascript' ) || false !== stripos( $this->accept, 'json' ) ) {
  110. $help_content_type = 'json';
  111. } else {
  112. $help_content_type = 'html';
  113. }
  114. } else {
  115. if ( in_array( $this->method, $allowed_methods ) ) {
  116. // Only serve requested method
  117. $methods = array( $this->method );
  118. $find_all_matching_endpoints = false;
  119. } else {
  120. // We don't allow this requested method - find matching endpoints and send 405
  121. $methods = $allowed_methods;
  122. $find_all_matching_endpoints = true;
  123. $four_oh_five = true;
  124. }
  125. }
  126. // Find which endpoint to serve
  127. $found = false;
  128. foreach ( $this->endpoints as $endpoint_path => $endpoints_by_method ) {
  129. foreach ( $methods as $method ) {
  130. if ( !isset( $endpoints_by_method[$method] ) ) {
  131. continue;
  132. }
  133. // Normalize
  134. $endpoint_path = untrailingslashit( $endpoint_path );
  135. if ( $is_help ) {
  136. // Truncate path at help depth
  137. $endpoint_path = join( '/', array_slice( explode( '/', $endpoint_path ), 0, $depth ) );
  138. }
  139. // Generate regular expression from sprintf()
  140. $endpoint_path_regex = str_replace( array( '%s', '%d' ), array( '([^/?&]+)', '(\d+)' ), $endpoint_path );
  141. if ( !preg_match( "#^$endpoint_path_regex\$#", $this->path, $path_pieces ) ) {
  142. // This endpoint does not match the requested path.
  143. continue;
  144. }
  145. $found = true;
  146. if ( $find_all_matching_endpoints ) {
  147. $matching_endpoints[] = array( $endpoints_by_method[$method], $path_pieces );
  148. } else {
  149. // The method parameters are now in $path_pieces
  150. $endpoint = $endpoints_by_method[$method];
  151. break 2;
  152. }
  153. }
  154. }
  155. if ( !$found ) {
  156. return $this->output( 404, '', 'text/plain' );
  157. }
  158. if ( $four_oh_five ) {
  159. $allowed_methods = array();
  160. foreach ( $matching_endpoints as $matching_endpoint ) {
  161. $allowed_methods[] = $matching_endpoint[0]->method;
  162. }
  163. header( 'Allow: ' . strtoupper( join( ',', array_unique( $allowed_methods ) ) ) );
  164. return $this->output( 405, array( 'error' => 'not_allowed', 'error_message' => 'Method not allowed' ) );
  165. }
  166. if ( $is_help ) {
  167. do_action( 'wpcom_json_api_output', 'help' );
  168. if ( 'json' === $help_content_type ) {
  169. $docs = array();
  170. foreach ( $matching_endpoints as $matching_endpoint ) {
  171. if ( !$matching_endpoint[0]->in_testing || WPCOM_JSON_API__DEBUG )
  172. $docs[] = call_user_func( array( $matching_endpoint[0], 'generate_documentation' ) );
  173. }
  174. return $this->output( 200, $docs );
  175. } else {
  176. status_header( 200 );
  177. foreach ( $matching_endpoints as $matching_endpoint ) {
  178. if ( !$matching_endpoint[0]->in_testing || WPCOM_JSON_API__DEBUG )
  179. call_user_func( array( $matching_endpoint[0], 'document' ) );
  180. }
  181. }
  182. exit;
  183. }
  184. if ( $endpoint->in_testing && !WPCOM_JSON_API__DEBUG ) {
  185. return $this->output( 404, '', 'text/plain' );
  186. }
  187. do_action( 'wpcom_json_api_output', $endpoint->stat );
  188. $response = $this->process_request( $endpoint, $path_pieces );
  189. if ( !$response ) {
  190. return $this->output( 500, '', 'text/plain' );
  191. } elseif ( is_wp_error( $response ) ) {
  192. $status_code = $response->get_error_data();
  193. if ( !$status_code ) {
  194. $status_code = 400;
  195. }
  196. $response = array(
  197. 'error' => $response->get_error_code(),
  198. 'message' => $response->get_error_message(),
  199. );
  200. return $this->output( $status_code, $response );
  201. }
  202. return $this->output( 200, $response );
  203. }
  204. function process_request( WPCOM_JSON_API_Endpoint $endpoint, $path_pieces ) {
  205. return call_user_func_array( array( $endpoint, 'callback' ), $path_pieces );
  206. }
  207. function output( $status_code, $response = null, $content_type = 'application/json' ) {
  208. if ( is_null( $response ) ) {
  209. $response = new stdClass;
  210. }
  211. if ( 'text/plain' === $content_type ) {
  212. status_header( (int) $status_code );
  213. header( 'Content-Type: text/plain' );
  214. echo $response;
  215. if ( $this->exit ) {
  216. exit;
  217. }
  218. return $content_type;
  219. }
  220. if ( isset( $this->query['http_envelope'] ) && self::is_truthy( $this->query['http_envelope'] ) ) {
  221. $response = array(
  222. 'code' => (int) $status_code,
  223. 'headers' => array(
  224. array(
  225. 'name' => 'Content-Type',
  226. 'value' => $content_type,
  227. ),
  228. ),
  229. 'body' => $response,
  230. );
  231. $status_code = 200;
  232. $content_type = 'application/json';
  233. }
  234. status_header( (int) $status_code );
  235. header( "Content-Type: $content_type" );
  236. if ( isset( $this->query['callback'] ) && is_string( $this->query['callback'] ) ) {
  237. $callback = preg_replace( '/[^a-z0-9_.]/i', '', $this->query['callback'] );
  238. } else {
  239. $callback = false;
  240. }
  241. if ( $callback ) {
  242. echo "$callback(";
  243. }
  244. echo $this->json_encode( $response );
  245. if ( $callback ) {
  246. echo ");";
  247. }
  248. if ( $this->exit ) {
  249. exit;
  250. }
  251. return $content_type;
  252. }
  253. function ensure_http_scheme_of_home_url( $url, $path, $original_scheme ) {
  254. if ( $original_scheme ) {
  255. return $url;
  256. }
  257. return preg_replace( '#^https:#', 'http:', $url );
  258. }
  259. function comment_edit_pre( $comment_content ) {
  260. return htmlspecialchars_decode( $comment_content, ENT_QUOTES );
  261. }
  262. function json_encode( $data ) {
  263. return json_encode( $data );
  264. }
  265. function ends_with( $haystack, $needle ) {
  266. return $needle === substr( $haystack, -strlen( $needle ) );
  267. }
  268. // Returns the site's blog_id in the WP.com ecosystem
  269. function get_blog_id_for_output() {
  270. return $this->token_details['blog_id'];
  271. }
  272. // Returns the site's local blog_id
  273. function get_blog_id( $blog_id ) {
  274. return $GLOBALS['blog_id'];
  275. }
  276. function switch_to_blog_and_validate_user( $blog_id = 0, $verify_token_for_blog = true ) {
  277. if ( -1 == get_option( 'blog_public' ) && !current_user_can( 'read' ) ) {
  278. return new WP_Error( 'unauthorized', 'User cannot access this private blog.', 403 );
  279. }
  280. return $blog_id;
  281. }
  282. function post_like_count() {
  283. return 0;
  284. }
  285. function get_avatar_url( $email ) {
  286. add_filter( 'pre_option_show_avatars', '__return_true', 999 );
  287. $_SERVER['HTTPS'] = 'off';
  288. $avatar_img_element = get_avatar( $email, 96, '' );
  289. if ( !$avatar_img_element || is_wp_error( $avatar_img_element ) ) {
  290. $return = '';
  291. } elseif ( !preg_match( '#src=([\'"])?(.*?)(?(1)\\1|\s)#', $avatar_img_element, $matches ) ) {
  292. $return = '';
  293. } else {
  294. $return = esc_url_raw( htmlspecialchars_decode( $matches[2] ) );
  295. }
  296. remove_filter( 'pre_option_show_avatars', '__return_true', 999 );
  297. if ( '--UNset--' === $this->_server_https ) {
  298. unset( $_SERVER['HTTPS'] );
  299. } else {
  300. $_SERVER['HTTPS'] = $this->_server_https;
  301. }
  302. return $return;
  303. }
  304. }