Advertisement
Guest User

Untitled

a guest
Aug 31st, 2019
1,138
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 13.82 KB | None | 0 0
  1. <?php
  2. class CdnWPFC{
  3. public static function cloudflare_clear_cache($email = false, $key = false, $zoneid = false){
  4. if(!$email && !$key && !$zoneid){
  5. if($cdn_values = get_option("WpFastestCacheCDN")){
  6. $std_obj = json_decode($cdn_values);
  7.  
  8. foreach ($std_obj as $key => $value) {
  9. if($value->id == "cloudflare"){
  10. $email = $value->cdnurl;
  11. $key = $value->originurl;
  12. break;
  13. }
  14. }
  15.  
  16. if($email && $key){
  17. $zone = self::cloudflare_get_zone_id($email, $key, false);
  18.  
  19. if($zone["success"]){
  20. $zoneid = $zone["zoneid"];
  21. }
  22. }
  23. }
  24. }
  25.  
  26. if($email && $key && $zoneid){
  27. $header = array("method" => "DELETE",
  28. 'headers' => array(
  29. "X-Auth-Email" => $email,
  30. "X-Auth-Key" => $key,
  31. "Content-Type" => "application/json"
  32. ),
  33. "body" => '{"purge_everything":true}'
  34. );
  35.  
  36. $response = wp_remote_request('https://api.cloudflare.com/client/v4/zones/'.$zoneid.'/purge_cache', $header);
  37. }
  38. }
  39.  
  40. public static function cloudflare_disable_rocket_loader($email = false, $key = false, $zoneid = false){
  41. if($email && $key && $zoneid){
  42. $header = array("method" => "PATCH",
  43. 'headers' => array(
  44. "X-Auth-Email" => $email,
  45. "X-Auth-Key" => $key,
  46. "Content-Type" => "application/json"
  47. ),
  48. 'body' => '{"value":"off"}'
  49. );
  50.  
  51. $response = wp_remote_request('https://api.cloudflare.com/client/v4/zones/'.$zoneid.'/settings/rocket_loader', $header);
  52.  
  53. if(!$response || is_wp_error($response)){
  54. return array("success" => false, "error_message" => "Unable to disable rocket loader option");
  55. }else{
  56. $body = json_decode(wp_remote_retrieve_body($response));
  57.  
  58. if($body->success){
  59. return array("success" => true);
  60. }else if(isset($body->errors) && isset($body->errors[0])){
  61. return array("success" => false, "error_message" => $body->errors[0]->message);
  62. }else{
  63. return array("success" => false, "error_message" => "Unknown error: 101");
  64. }
  65. }
  66.  
  67. return array("success" => false, "error_message" => "Unknown error");
  68. }
  69. }
  70.  
  71.  
  72. public static function cloudflare_set_browser_caching($email = false, $key = false, $zoneid = false){
  73. if($email && $key && $zoneid){
  74. $header = array("method" => "PATCH",
  75. 'headers' => array(
  76. "X-Auth-Email" => $email,
  77. "X-Auth-Key" => $key,
  78. "Content-Type" => "application/json"
  79. ),
  80. 'body' => '{"value":16070400}'
  81. );
  82.  
  83. $response = wp_remote_request('https://api.cloudflare.com/client/v4/zones/'.$zoneid.'/settings/browser_cache_ttl', $header);
  84.  
  85. if(!$response || is_wp_error($response)){
  86. return array("success" => false, "error_message" => "Unable to disable rocket loader option");
  87. }else{
  88. $body = json_decode(wp_remote_retrieve_body($response));
  89.  
  90. if($body->success){
  91. return array("success" => true);
  92. }else if(isset($body->errors) && isset($body->errors[0])){
  93. return array("success" => false, "error_message" => $body->errors[0]->message);
  94. }else{
  95. return array("success" => false, "error_message" => "Unknown error: 101");
  96. }
  97. }
  98.  
  99. return array("success" => false, "error_message" => "Unknown error");
  100. }
  101. }
  102.  
  103. public static function cloudflare_disable_minify($email = false, $key = false, $zoneid = false){
  104. if($email && $key && $zoneid){
  105. $header = array("method" => "PATCH",
  106. 'headers' => array(
  107. "X-Auth-Email" => $email,
  108. "X-Auth-Key" => $key,
  109. "Content-Type" => "application/json"
  110. ),
  111. 'body' => '{"value":{"css":"off","html":"off","js":"off"}}'
  112. );
  113.  
  114. $response = wp_remote_request('https://api.cloudflare.com/client/v4/zones/'.$zoneid.'/settings/minify', $header);
  115.  
  116. if(!$response || is_wp_error($response)){
  117. return array("success" => false, "error_message" => "Unable to disable minify options");
  118. }else{
  119. $body = json_decode(wp_remote_retrieve_body($response));
  120.  
  121. if($body->success){
  122. return array("success" => true);
  123. }else if(isset($body->errors) && isset($body->errors[0])){
  124. return array("success" => false, "error_message" => $body->errors[0]->message);
  125. }else{
  126. return array("success" => false, "error_message" => "Unknown error: 101");
  127. }
  128. }
  129.  
  130. return array("success" => false, "error_message" => "Unknown error");
  131. }else{
  132. wp_die("bad request");
  133. }
  134. }
  135.  
  136. public static function cloudflare_get_zone_id($email = false, $key = false){
  137. $hostname = preg_replace("/^(https?\:\/\/)?(www\d*\.)?/", "", $_SERVER["HTTP_HOST"]);
  138.  
  139. $header = array("method" => "GET",
  140. 'headers' => array(
  141. "X-Auth-Email" => $email,
  142. "X-Auth-Key" => $key,
  143. "Content-Type" => "application/json"
  144. ),
  145. );
  146.  
  147. /*
  148. status=active has been removed because status may be "pending"
  149. */
  150. $response = wp_remote_request('https://api.cloudflare.com/client/v4/zones/?page=1&per_page=1000', $header);
  151.  
  152. if(!$response || is_wp_error($response)){
  153. $res = array("success" => false, "error_message" => $response->get_error_message());
  154. }else{
  155. $zone = json_decode(wp_remote_retrieve_body($response));
  156.  
  157. if(isset($zone->errors) && isset($zone->errors[0])){
  158. $res = array("success" => false, "error_message" => $zone->errors[0]->message);
  159. }else{
  160. if(isset($zone->result) && isset($zone->result[0])){
  161. foreach ($zone->result as $zone_key => $zone_value) {
  162. if(preg_match("/".$zone_value->name."/", $hostname)){
  163. $res = array("success" => true,
  164. "zoneid" => $zone_value->id,
  165. "plan" => $zone_value->plan->legacy_id);
  166. }
  167. }
  168.  
  169. if(!$res["success"]){
  170. $res = array("success" => false, "error_message" => "No zone name ".$hostname);
  171. }
  172. }else{
  173. $res = array("success" => false, "error_message" => "There is no zone");
  174. }
  175. }
  176. }
  177.  
  178. return $res;
  179. }
  180.  
  181. public static function cloudflare_remove_webp(){
  182. $path = ABSPATH.".htaccess";
  183.  
  184. if(file_exists($path)){
  185. if(is_writable($path)){
  186. $htaccess = file_get_contents($path);
  187. $htaccess = preg_replace("/#\s?BEGIN\s?WEBPWpFastestCache.*?#\s?END\s?WEBPWpFastestCache/s", "", $htaccess);
  188.  
  189. file_put_contents($path, $htaccess);
  190. }
  191. }
  192. }
  193.  
  194.  
  195. public static function cloudflare_change_settings(){
  196. //admin OR author OR editor
  197. if(current_user_can('manage_options') || current_user_can('delete_published_posts') || current_user_can('edit_published_posts')){
  198. if(isset($_GET["url"]) && isset($_GET["origin_url"])){
  199. $email = $_GET["url"];
  200. $key = $_GET["origin_url"];
  201. }
  202.  
  203. $zone = CdnWPFC::cloudflare_get_zone_id($email, $key);
  204.  
  205. if($zone["success"]){
  206.  
  207. $minify = CdnWPFC::cloudflare_disable_minify($email, $key, $zone["zoneid"]);
  208. $rocket_loader = CdnWPFC::cloudflare_disable_rocket_loader($email, $key, $zone["zoneid"]);
  209. $purge_cache = CdnWPFC::cloudflare_clear_cache($email, $key, $zone["zoneid"]);
  210. $browser_caching = CdnWPFC::cloudflare_set_browser_caching($email, $key, $zone["zoneid"]);
  211.  
  212. if($zone["plan"] == "free"){
  213. CdnWPFC::cloudflare_remove_webp();;
  214. }
  215.  
  216.  
  217. if($minify["success"]){
  218. if($rocket_loader["success"]){
  219. if($browser_caching["success"]){
  220. $res = array("success" => true);
  221. }else{
  222. $res = array("success" => false, "error_message" => $browser_caching["error_message"]);
  223. }
  224. }else{
  225. $res = array("success" => false, "error_message" => $rocket_loader["error_message"]);
  226. }
  227. }else{
  228. $res = array("success" => false, "error_message" => $minify["error_message"]);
  229. }
  230. }else{
  231. $res = $zone;
  232. }
  233.  
  234. wp_send_json($res);
  235. }else{
  236. wp_die("Must be admin");
  237. }
  238. }
  239.  
  240. public static function check_url(){
  241. if(current_user_can('manage_options')){
  242. if(isset($_GET["type"]) && $_GET["type"] == "cloudflare"){
  243. CdnWPFC::cloudflare_change_settings();
  244. }
  245.  
  246. if(preg_match("/wp\.com/", $_GET["url"]) || $_GET["url"] == "random"){
  247. wp_send_json(array("success" => true));
  248. }
  249.  
  250. $host = str_replace("www.", "", $_SERVER["HTTP_HOST"]);
  251. $_GET["url"] = esc_url_raw($_GET["url"]);
  252.  
  253. if(!preg_match("/^http/", $_GET["url"])){
  254. $_GET["url"] = "http://".$_GET["url"];
  255. }
  256.  
  257. $response = wp_remote_get($_GET["url"], array('timeout' => 20, 'user-agent' => "Mozilla/5.0 (Macintosh; Intel Mac OS X 10.11; rv:64.0) Gecko/20100101 Firefox/64.0"));
  258.  
  259. $header = wp_remote_retrieve_headers($response);
  260.  
  261. if ( !$response || is_wp_error( $response ) ) {
  262. $res = array("success" => false, "error_message" => $response->get_error_message());
  263.  
  264. if($response->get_error_code() == "http_request_failed"){
  265. if($response->get_error_message() == "Failure when receiving data from the peer"){
  266. $res = array("success" => true);
  267. }else if(preg_match("/cURL\serror\s6/i", $response->get_error_message())){
  268. //cURL error 6: Couldn't resolve host
  269. if(preg_match("/".preg_quote($host, "/")."/i", $_GET["url"])){
  270. $res = array("success" => true);
  271. }
  272. }
  273. }
  274. }else{
  275. $response_code = wp_remote_retrieve_response_code( $response );
  276. if($response_code == 200){
  277. $res = array("success" => true);
  278. }else{
  279. if(method_exists($response, "get_error_message")){
  280. $res = array("success" => false, "error_message" => $response->get_error_message());
  281. }else{
  282. $res = array("success" => false, "error_message" => wp_remote_retrieve_response_message($response));
  283. }
  284.  
  285. if(isset($header["server"]) && preg_match("/squid/i", $header["server"])){
  286. $res = array("success" => true);
  287. }
  288.  
  289. if(($response_code == 401) && (preg_match("/res\.cloudinary\.com/i", $_GET["url"]))){
  290. $res = array("success" => true);
  291. }
  292.  
  293. if(($response_code == 403) && (preg_match("/stackpathdns\.com/i", $_GET["url"]))){
  294. $res = array("success" => true);
  295. }
  296. }
  297. }
  298.  
  299. wp_send_json($res);
  300. }else{
  301. wp_die("Must be admin");
  302. }
  303. }
  304.  
  305. public static function cdn_options(){
  306. if(current_user_can('manage_options')){
  307. $cdn_values = get_option("WpFastestCacheCDN");
  308. if($cdn_values){
  309. echo $cdn_values;
  310. }else{
  311. echo json_encode(array("success" => false));
  312. }
  313. exit;
  314. }else{
  315. wp_die("Must be admin");
  316. }
  317. }
  318.  
  319. public static function remove_cdn_integration(){
  320. if(current_user_can('manage_options')){
  321. $cdn_values = get_option("WpFastestCacheCDN");
  322.  
  323. if($cdn_values){
  324. $std_obj = json_decode($cdn_values);
  325. $cdn_values_arr = array();
  326.  
  327. if(is_array($std_obj)){
  328. $cdn_values_arr = $std_obj;
  329. }else{
  330. array_push($cdn_values_arr, $std_obj);
  331. }
  332.  
  333. foreach ($cdn_values_arr as $cdn_key => $cdn_value) {
  334. if($cdn_value->id == "amazonaws" || $cdn_value->id == "keycdn" || $cdn_value->id == "cdn77"){
  335. $cdn_value->id = "other";
  336. }
  337.  
  338. if($cdn_value->id == $_POST["id"]){
  339. unset($cdn_values_arr[$cdn_key]);
  340. }
  341. }
  342.  
  343. $cdn_values_arr = array_values($cdn_values_arr);
  344. }
  345.  
  346. if(count($cdn_values_arr) > 0){
  347. update_option("WpFastestCacheCDN", json_encode($cdn_values_arr));
  348. }else{
  349. delete_option("WpFastestCacheCDN");
  350. }
  351.  
  352. echo json_encode(array("success" => true));
  353. exit;
  354. }else{
  355. wp_die("Must be admin");
  356. }
  357. }
  358.  
  359. public static function cdn_template(){
  360. if(current_user_can('manage_options')){
  361. if($_POST["id"] == "maxcdn"){
  362. $path = WPFC_MAIN_PATH."templates/cdn/maxcdn.php";
  363. }else if($_POST["id"] == "other"){
  364. $path = WPFC_MAIN_PATH."templates/cdn/other.php";
  365. }else if($_POST["id"] == "photon"){
  366. $path = WPFC_MAIN_PATH."templates/cdn/photon.php";
  367. }else if($_POST["id"] == "cloudflare"){
  368. $path = WPFC_MAIN_PATH."templates/cdn/cloudflare.php";
  369. }else{
  370. die("Wrong cdn");
  371. }
  372.  
  373.  
  374. ob_start();
  375. include_once($path);
  376. $content = ob_get_contents();
  377. ob_end_clean();
  378.  
  379. $res = array("success" => false, "content" => "");
  380.  
  381. if($data = @file_get_contents($path)){
  382. $res["success"] = true;
  383. $res["content"] = $content;
  384. }
  385.  
  386. echo json_encode($res);
  387. exit;
  388. }else{
  389. wp_die("Must be admin");
  390. }
  391. }
  392.  
  393. public static function save_cdn_integration(){
  394. if(current_user_can('manage_options')){
  395. if(isset($_POST) && isset($_POST["values"])){
  396. foreach ($_POST["values"] as $val_key => &$val_value) {
  397. $val_value = sanitize_text_field($val_value);
  398. }
  399. }
  400.  
  401. if($data = get_option("WpFastestCacheCDN")){
  402. $cdn_exist = false;
  403. $arr = json_decode($data);
  404.  
  405. if(is_array($arr)){
  406. foreach ($arr as $cdn_key => &$cdn_value) {
  407. if($cdn_value->id == $_POST["values"]["id"]){
  408. $cdn_value = $_POST["values"];
  409. $cdn_exist = true;
  410. }
  411. }
  412.  
  413. if(!$cdn_exist){
  414. array_push($arr, $_POST["values"]);
  415. }
  416.  
  417. update_option("WpFastestCacheCDN", json_encode($arr));
  418. }else{
  419. $tmp_arr = array();
  420.  
  421. if($arr->id == $_POST["values"]["id"]){
  422. array_push($tmp_arr, $_POST["values"]);
  423. }else{
  424. array_push($tmp_arr, $arr);
  425. array_push($tmp_arr, $_POST["values"]);
  426. }
  427.  
  428. update_option("WpFastestCacheCDN", json_encode($tmp_arr));
  429. }
  430. }else{
  431. $arr = array();
  432. array_push($arr, $_POST["values"]);
  433.  
  434. add_option("WpFastestCacheCDN", json_encode($arr), null, "yes");
  435. }
  436. echo json_encode(array("success" => true));
  437. exit;
  438. }else{
  439. wp_die("Must be admin");
  440. }
  441. }
  442.  
  443. }
  444. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement