PageRenderTime 60ms CodeModel.GetById 27ms RepoModel.GetById 0ms app.codeStats 0ms

/wp-content/themes/usaitv/lib/ReduxCore/inc/class.p.php

https://gitlab.com/thisishayat/itv-2016
PHP | 303 lines | 109 code | 48 blank | 146 comment | 37 complexity | 75e9626a3979a2dc024f1f7205aa973d MD5 | raw file
  1. <?php
  2. class Redux_P {
  3. public function __construct() {
  4. add_action( "wp_ajax_nopriv_redux_p", array( $this, 'proxy' ) );
  5. add_action( "wp_ajax_redux_p", array( $this, 'proxy' ) );
  6. }
  7. public function proxy() {
  8. if ( ! isset( $_GET['nonce'] ) || ( isset( $_GET['nonce'] ) && ! wp_verify_nonce( $_GET['nonce'], "redux-ads-nonce" ) ) ) {
  9. die();
  10. }
  11. // Script: Simple PHP Proxy: Get external HTML, JSON and more!
  12. //
  13. // *Version: 1.6, Last updated: 1/24/2009*
  14. //
  15. // Project Home - http://benalman.com/projects/php-simple-proxy/
  16. // GitHub - http://github.com/cowboy/php-simple-proxy/
  17. // Source - http://github.com/cowboy/php-simple-proxy/raw/master/ba-simple-proxy.php
  18. //
  19. // About: License
  20. //
  21. // Copyright (c) 2010 "Cowboy" Ben Alman,
  22. // Dual licensed under the MIT and GPL licenses.
  23. // http://benalman.com/about/license/
  24. //
  25. // About: Examples
  26. //
  27. // This working example, complete with fully commented code, illustrates one way
  28. // in which this PHP script can be used.
  29. //
  30. // Simple - http://benalman.com/code/projects/php-simple-proxy/examples/simple/
  31. //
  32. // About: Release History
  33. //
  34. // 1.6 - (1/24/2009) Now defaults to JSON mode, which can now be changed to
  35. // native mode by specifying ?mode=native. Native and JSONP modes are
  36. // disabled by default because of possible XSS vulnerability issues, but
  37. // are configurable in the PHP script along with a url validation regex.
  38. // 1.5 - (12/27/2009) Initial release
  39. //
  40. // Topic: GET Parameters
  41. //
  42. // Certain GET (query string) parameters may be passed into ba-simple-proxy.php
  43. // to control its behavior, this is a list of these parameters.
  44. //
  45. // url - The remote URL resource to fetch. Any GET parameters to be passed
  46. // through to the remote URL resource must be urlencoded in this parameter.
  47. // mode - If mode=native, the response will be sent using the same content
  48. // type and headers that the remote URL resource returned. If omitted, the
  49. // response will be JSON (or JSONP). <Native requests> and <JSONP requests>
  50. // are disabled by default, see <Configuration Options> for more information.
  51. // callback - If specified, the response JSON will be wrapped in this named
  52. // function call. This parameter and <JSONP requests> are disabled by
  53. // default, see <Configuration Options> for more information.
  54. // user_agent - This value will be sent to the remote URL request as the
  55. // `User-Agent:` HTTP request header. If omitted, the browser user agent
  56. // will be passed through.
  57. // send_cookies - If send_cookies=1, all cookies will be forwarded through to
  58. // the remote URL request.
  59. // send_session - If send_session=1 and send_cookies=1, the SID cookie will be
  60. // forwarded through to the remote URL request.
  61. // full_headers - If a JSON request and full_headers=1, the JSON response will
  62. // contain detailed header information.
  63. // full_status - If a JSON request and full_status=1, the JSON response will
  64. // contain detailed cURL status information, otherwise it will just contain
  65. // the `http_code` property.
  66. //
  67. // Topic: POST Parameters
  68. //
  69. // All POST parameters are automatically passed through to the remote URL
  70. // request.
  71. //
  72. // Topic: JSON requests
  73. //
  74. // This request will return the contents of the specified url in JSON format.
  75. //
  76. // Request:
  77. //
  78. // > ba-simple-proxy.php?url=http://example.com/
  79. //
  80. // Response:
  81. //
  82. // > { "contents": "<html>...</html>", "headers": {...}, "status": {...} }
  83. //
  84. // JSON object properties:
  85. //
  86. // contents - (String) The contents of the remote URL resource.
  87. // headers - (Object) A hash of HTTP headers returned by the remote URL
  88. // resource.
  89. // status - (Object) A hash of status codes returned by cURL.
  90. //
  91. // Topic: JSONP requests
  92. //
  93. // This request will return the contents of the specified url in JSONP format
  94. // (but only if $enable_jsonp is enabled in the PHP script).
  95. //
  96. // Request:
  97. //
  98. // > ba-simple-proxy.php?url=http://example.com/&callback=foo
  99. //
  100. // Response:
  101. //
  102. // > foo({ "contents": "<html>...</html>", "headers": {...}, "status": {...} })
  103. //
  104. // JSON object properties:
  105. //
  106. // contents - (String) The contents of the remote URL resource.
  107. // headers - (Object) A hash of HTTP headers returned by the remote URL
  108. // resource.
  109. // status - (Object) A hash of status codes returned by cURL.
  110. //
  111. // Topic: Native requests
  112. //
  113. // This request will return the contents of the specified url in the format it
  114. // was received in, including the same content-type and other headers (but only
  115. // if $enable_native is enabled in the PHP script).
  116. //
  117. // Request:
  118. //
  119. // > ba-simple-proxy.php?url=http://example.com/&mode=native
  120. //
  121. // Response:
  122. //
  123. // > <html>...</html>
  124. //
  125. // Topic: Notes
  126. //
  127. // * Assumes magic_quotes_gpc = Off in php.ini
  128. //
  129. // Topic: Configuration Options
  130. //
  131. // These variables can be manually edited in the PHP file if necessary.
  132. //
  133. // $enable_jsonp - Only enable <JSONP requests> if you really need to. If you
  134. // install this script on the same server as the page you're calling it
  135. // from, plain JSON will work. Defaults to false.
  136. // $enable_native - You can enable <Native requests>, but you should only do
  137. // this if you also whitelist specific URLs using $valid_url_regex, to avoid
  138. // possible XSS vulnerabilities. Defaults to false.
  139. // $valid_url_regex - This regex is matched against the url parameter to
  140. // ensure that it is valid. This setting only needs to be used if either
  141. // $enable_jsonp or $enable_native are enabled. Defaults to '/.*/' which
  142. // validates all URLs.
  143. //
  144. // ############################################################################
  145. $_GET['mode'] = "native";
  146. $_GET['full_headers'] = 1;
  147. $_GET['full_status'] = 1;
  148. $_GET['send_cookies'] = 1;
  149. // Change these configuration options if needed, see above descriptions for info.
  150. $enable_jsonp = false;
  151. $enable_native = true;
  152. $valid_url_regex = '/.*/';
  153. // ############################################################################
  154. $url = $_GET['url'];
  155. if ( isset( $_GET['nonce'] ) ) {
  156. $url = str_replace( 'nonce=' . $_GET['nonce'] . '&', '', $url );
  157. }
  158. if ( ! $url ) {
  159. // Passed url not specified.
  160. $contents = 'ERROR: url not specified';
  161. $status = array( 'http_code' => 'ERROR' );
  162. } else if ( ! preg_match( $valid_url_regex, $url ) ) {
  163. // Passed url doesn't match $valid_url_regex.
  164. $contents = 'ERROR: invalid url';
  165. $status = array( 'http_code' => 'ERROR' );
  166. } else {
  167. $url = urldecode( $url );
  168. if ( isset( $_GET['proxy'] ) ) {
  169. $url .= '&proxy=' . $_GET['proxy'];
  170. }
  171. // Ad URL rewrite
  172. if ( strpos( $url, 'http' ) === false ) {
  173. $url = 'http:' . $url;
  174. }
  175. if ( isset( $_GET['callback'] ) ) {
  176. foreach ( $_GET as $key => $value ) {
  177. if ( in_array( $key, array( 'url', 'mode', 'full_headers', 'full_status', 'send_cookies' ) ) ) {
  178. continue;
  179. }
  180. $url .= "&" . $key . '=' . $value;
  181. }
  182. }
  183. $args = array(
  184. 'user-agent' => isset( $_GET['user_agent'] ) ? $_GET['user_agent'] : $_SERVER['HTTP_USER_AGENT'],
  185. 'method' => 'GET',
  186. );
  187. if ( isset( $_GET['send_cookies'] ) && $_GET['send_cookies'] ) {
  188. $cookie = array();
  189. foreach ( $_COOKIE as $key => $value ) {
  190. $cookie[] = $key . '=' . $value;
  191. }
  192. if ( isset( $_GET['send_session'] ) && $_GET['send_session'] ) {
  193. $cookie[] = SID;
  194. }
  195. $args['cookies'] = $cookie;
  196. }
  197. if ( strtolower( $_SERVER['REQUEST_METHOD'] ) == 'post' ) {
  198. $args['body'] = $_POST;
  199. $args['method'] = 'POST';
  200. }
  201. $response = wp_remote_request(
  202. $url,
  203. $args
  204. );
  205. if ( ! is_wp_error( $response ) ) {
  206. $status = $response['response']['code'];
  207. $contents = $response['body'];
  208. }
  209. }
  210. if ( isset( $_GET['mode'] ) && $_GET['mode'] == 'native' ) {
  211. if ( ! $enable_native ) {
  212. $contents = 'ERROR: invalid mode';
  213. $status = array( 'http_code' => 'ERROR' );
  214. }
  215. if ( ! is_wp_error( $response ) && isset( $response['headers']['content-type'] ) ) {
  216. header( 'Content-Type: ' . $response['headers']['content-type'] );
  217. }
  218. if ( ! is_wp_error( $response ) && isset( $response['headers']['content-language'] ) ) {
  219. header( 'Content-Language: ' . $response['headers']['content-language'] );
  220. }
  221. if ( ! is_wp_error( $response ) && isset( $response['headers']['set-cookie'] ) ) {
  222. header( 'Set-Cookie: ' . $response['headers']['set-cookie'] );
  223. }
  224. if ( isset( $contents ) ) {
  225. print str_replace( 'ads.reduxframework.com', 'look.reduxframework.com', $contents );
  226. }
  227. } else {
  228. // $data will be serialized into JSON data.
  229. $data = array();
  230. // Propagate all HTTP headers into the JSON data object.
  231. if ( isset( $_GET['full_headers'] ) && $_GET['full_headers'] ) {
  232. $data['headers'] = array();
  233. }
  234. // Propagate all cURL request / response info to the JSON data object.
  235. if ( isset( $_GET['full_status'] ) && $_GET['full_status'] ) {
  236. $data['status'] = $status;
  237. } else {
  238. $data['status'] = array();
  239. $data['status']['http_code'] = $status['http_code'];
  240. }
  241. // Set the JSON data object contents, decoding it from JSON if possible.
  242. $decoded_json = json_decode( $contents );
  243. $data['contents'] = str_replace( 'e(window).width()', 'window.innerWidth||e(window).width()', $decoded_json ? $decoded_json : $contents );
  244. // Generate appropriate content-type header.
  245. $is_xhr = isset( $_SERVER['HTTP_X_REQUESTED_WITH'] ) ? strtolower( $_SERVER['HTTP_X_REQUESTED_WITH'] ) : 'xmlhttprequest';
  246. header( 'Content-type: application/' . ( $is_xhr ? 'json' : 'x-javascript' ) );
  247. // Get JSONP callback.
  248. $jsonp_callback = $enable_jsonp && isset( $_GET['callback'] ) ? $_GET['callback'] : null;
  249. // Generate JSON/JSONP string
  250. $json = json_encode( $data );
  251. print $jsonp_callback ? "$jsonp_callback($json)" : $json;
  252. }
  253. }
  254. }
  255. new Redux_P();