Guest User

Untitled

a guest
Jun 19th, 2020
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 10.79 KB | None | 0 0
  1. <?php
  2. /*
  3.  *  Based on some work of https://github.com/tlovett1/simple-cache/blob/master/inc/dropins/file-based-page-cache.php
  4.  */
  5. defined( 'ABSPATH' ) || exit;
  6.  
  7. // Load helper functions.
  8. require_once dirname( __DIR__ ) . '/functions.php';
  9.  
  10. // Don't cache robots.txt or htacesss
  11. if ( strpos( $_SERVER['REQUEST_URI'], 'robots.txt' ) !== false || strpos( $_SERVER['REQUEST_URI'], '.htaccess' ) !== false ) {
  12.     return;
  13. }
  14.  
  15. // Don't cache non-GET requests
  16. if ( ! isset( $_SERVER['REQUEST_METHOD'] ) || $_SERVER['REQUEST_METHOD'] !== 'GET' ) {
  17.     return;
  18. }
  19.  
  20. $file_extension = $_SERVER['REQUEST_URI'];
  21. $file_extension = preg_replace( '#^(.*?)\?.*$#', '$1', $file_extension );
  22. $file_extension = trim( preg_replace( '#^.*\.(.*)$#', '$1', $file_extension ) );
  23.  
  24. // Don't cache disallowed extensions. Prevents wp-cron.php, xmlrpc.php, etc.
  25. if ( ! preg_match( '#index\.php$#i', $_SERVER['REQUEST_URI'] ) && in_array( $file_extension, array( 'php', 'xml', 'xsl' ) ) ) {
  26.     return;
  27. }
  28.  
  29. // @TODO: Remove debugging code.
  30. if ( isset( $_GET['debug_config'] ) ) {
  31.     var_dump( $GLOBALS['breeze_config'] );
  32.     exit;
  33. }
  34.  
  35. // Don't cache
  36. if ( ! empty( $_COOKIE ) ) {
  37.     if ( ! empty( $_COOKIE['breeze_commented_posts'] ) ) {
  38.         foreach ( $_COOKIE['breeze_commented_posts'] as $path ) {
  39.             if ( rtrim( $path, '/' ) === rtrim( $_SERVER['REQUEST_URI'], '/' ) ) {
  40.                 // User commented on this post
  41.                 return;
  42.             }
  43.         }
  44.     }
  45. }
  46.  
  47. //check disable cache for page
  48. $domain = ( ( ( ! empty( $_SERVER['HTTPS'] ) && $_SERVER['HTTPS'] !== 'off' ) || $_SERVER['SERVER_PORT'] == 443 ) ? 'https://' : 'http://' ) . $_SERVER['HTTP_HOST'];
  49. //decode url with russian language
  50. $current_url   = $domain . rawurldecode( $_SERVER['REQUEST_URI'] );
  51. $opts_config   = $GLOBALS['breeze_config'];
  52. $check_exclude = check_exclude_page( $opts_config, $current_url );
  53.  
  54. //load cache
  55. if ( ! $check_exclude ) {
  56.     list(
  57.         $filename,
  58.         $url_path,
  59.         $X1,
  60.         $devices,
  61.         $path
  62.     ) = breeze_get_cache_params();
  63.  
  64.     breeze_serve_cache( $filename, $path, $X1, $devices );
  65.     ob_start( 'breeze_cache' );
  66. } else {
  67.     header( 'Cache-Control: no-cache' );
  68. }
  69.  
  70. /**
  71.  * Get cache parameters (file, path, url, ...)
  72.  *
  73.  * @since  1.1.8
  74.  * @return array
  75.  */
  76. function breeze_get_cache_params() {
  77.     $url_path = breeze_get_url_path();
  78.     $nameuser = 'guest';
  79.  
  80.     if( function_exists( 'is_user_logged_in' ) && is_user_logged_in() ){
  81.         $current_user = wp_get_current_user();
  82.         if ( $current_user->user_login ) {
  83.             $nameuser = $current_user->user_login;
  84.         }
  85.     } elseif( ! empty($_COOKIE) ) {
  86.         $user_logged = false;
  87.  
  88.         foreach ( $_COOKIE as $key => $value ) {
  89.             // Logged in!
  90.             if ( strpos( $key, 'wordpress_logged_in_' ) !== false ) {
  91.                 $user_logged = true;
  92.             }
  93.    
  94.         }
  95.    
  96.         if ( $user_logged ) {
  97.             foreach ( $_COOKIE as $k => $v ) {
  98.                 if ( strpos( $k, 'wordpress_logged_in_' ) !== false ) {
  99.                     $nameuser = substr( $v, 0, strpos( $v, '|' ) );
  100.                 }
  101.             }
  102.         }  
  103.     }
  104.  
  105.     if( substr_count( $url_path, '?' ) > 0 ){
  106.         $filename = $url_path . '&' . strtolower( $nameuser );
  107.     } else {
  108.         $filename = $url_path . '?' . strtolower( $nameuser );
  109.     }
  110.  
  111.     require_once 'Mobile-Detect-2.8.25/Mobile_Detect.php';
  112.     $detect = new \Cloudways\Breeze\Mobile_Detect\Mobile_Detect;
  113.  
  114.     global $opts_config;
  115.     $devices = $opts_config['cache_options'];
  116.     $X1      = '';
  117.     // Detect devices
  118.     if ( $detect->isMobile() && ! $detect->isTablet() ) {
  119.         //        The first X will be D for Desktop cache
  120.         //                            M for Mobile cache
  121.         //                            T for Tablet cache
  122.         if ( (int) $devices['breeze-mobile-cache'] == 1 ) {
  123.             $X1       = 'D';
  124.             $filename .= '_breeze_cache_desktop';
  125.         }
  126.         if ( (int) $devices['breeze-mobile-cache'] == 2 ) {
  127.             $X1       = 'M';
  128.             $filename .= '_breeze_cache_mobile';
  129.         }
  130.  
  131.     } else {
  132.         if ( (int) $devices['breeze-desktop-cache'] == 1 ) {
  133.             $X1       = 'D';
  134.             $filename .= '_breeze_cache_desktop';
  135.         }
  136.     }
  137.  
  138.     if ( function_exists( 'gzencode' ) && ! empty( $GLOBALS['breeze_config']['cache_options']['breeze-gzip-compression'] ) ) {
  139.         $file_name = md5( $filename . '/index.gzip.html' ) . '.php';
  140.     } else {
  141.         $file_name = md5( $filename . '/index.html' ) . '.php';
  142.     }
  143.  
  144.     $path = breeze_get_cache_base_path() . md5( $url_path ) . '/' . $file_name;
  145.  
  146.     return array(
  147.         $filename,
  148.         $url_path,
  149.         $X1,
  150.         $devices,
  151.         $path
  152.     );
  153. }
  154.  
  155. /**
  156.  * Cache output before it goes to the browser
  157.  *
  158.  * @param  string $buffer
  159.  * @param  int $flags
  160.  *
  161.  * @since  1.0
  162.  * @return string
  163.  */
  164. function breeze_cache( $buffer, $flags ) {
  165.     // No cache for pages without 200 response status
  166.     if ( http_response_code() !== 200 ) {
  167.         return $buffer;
  168.     }
  169.  
  170.     //not cache per administrator if option disable optimization for admin users clicked
  171.     if ( ! empty( $GLOBALS['breeze_config'] ) && (int) $GLOBALS['breeze_config']['disable_per_adminuser'] ) {
  172.         if ( function_exists( 'is_user_logged_in' ) && is_user_logged_in() ) {
  173.             $current_user = wp_get_current_user();
  174.             if ( in_array( 'administrator', $current_user->roles ) ) {
  175.                 return $buffer;
  176.             }
  177.         }
  178.     }
  179.  
  180.     if ( strlen( $buffer ) < 255 ) {
  181.         return $buffer;
  182.     }
  183.  
  184.     // Don't cache search, 404, or password protected
  185.     if ( is_404() || is_search() || post_password_required() ) {
  186.         return $buffer;
  187.     }
  188.     global $wp_filesystem;
  189.     if ( empty( $wp_filesystem ) ) {
  190.         require_once( ABSPATH . '/wp-admin/includes/file.php' );
  191.         WP_Filesystem();
  192.     }
  193.  
  194.     list(
  195.         $filename,
  196.         $url_path,
  197.         $X1,
  198.         $devices,
  199.         $path
  200.     ) = breeze_get_cache_params();
  201.  
  202.     // Make sure we can read/write files and that proper folders exist
  203.     if ( ! wp_mkdir_p( dirname($path) ) ) {
  204.         // Can not cache!
  205.         return $buffer;
  206.     }
  207.  
  208.     $modified_time = time(); // Make sure modified time is consistent
  209.  
  210.     if ( preg_match( '#</html>#i', $buffer ) ) {
  211.         $buffer .= "\n<!-- Cache served by breeze CACHE - Last modified: " . gmdate( 'D, d M Y H:i:s', $modified_time ) . " GMT -->\n";
  212.     }
  213.     $headers = array(
  214.         array(
  215.             'name'  => 'Content-Length',
  216.             'value' => strlen( $buffer )
  217.         ),
  218.         array(
  219.             'name'  => 'Content-Type',
  220.             'value' => 'text/html; charset=utf-8'
  221.         ),
  222.         array(
  223.             'name'  => 'Last-Modified',
  224.             'value' => gmdate( 'D, d M Y H:i:s', $modified_time ) . ' GMT'
  225.         )
  226.     );
  227.  
  228.     if ( ! isset( $_SERVER['HTTP_X_VARNISH'] ) ) {
  229.         $headers = array_merge( array(
  230.             array(
  231.                 'name'  => 'Expires',
  232.                 'value' => 'Wed, 17 Aug 2005 00:00:00 GMT'
  233.             ),
  234.             array(
  235.                 'name'  => 'Cache-Control',
  236.                 'value' => 'no-store, no-cache, must-revalidate, post-check=0, pre-check=0'
  237.             ),
  238.             array(
  239.                 'name'  => 'Pragma',
  240.                 'value' => 'no-cache'
  241.             )
  242.         ) );
  243.     }
  244.  
  245.     $data = serialize( array( 'body' => $buffer, 'headers' => $headers ) );
  246.  
  247.     if ( strpos( $filename, '_breeze_cache_' ) !== false ) {
  248.         $wp_filesystem->put_contents( $path, $data );
  249.         $wp_filesystem->touch( $path, $modified_time );
  250.     } else {
  251.         return $buffer;
  252.     }
  253.  
  254.     //set cache provider header if not exists cache file
  255.     header( 'Cache-Provider:CLOUDWAYS-CACHE-' . $X1 . 'C' );
  256.  
  257.     // Do not send this header in case we are behind a varnish proxy
  258.     if ( ! isset( $_SERVER['HTTP_X_VARNISH'] ) ) {
  259.         header( 'Cache-Control: no-cache' ); // Check back every time to see if re-download is necessary
  260.     }
  261.  
  262.     header( 'Last-Modified: ' . gmdate( 'D, d M Y H:i:s', $modified_time ) . ' GMT' );
  263.  
  264.     if ( function_exists( 'ob_gzhandler' ) && ! empty( $GLOBALS['breeze_config']['cache_options']['breeze-gzip-compression'] ) ) {
  265.         $ini_output_compression = ini_get( 'zlib.output_compression' );
  266.         $array_values           = array( '1', 'On', 'on' );
  267.         if ( in_array( $ini_output_compression, $array_values ) ) {
  268.             return $buffer;
  269.         } else {
  270.             return ob_gzhandler( $buffer, $flags );
  271.         }
  272.     } else {
  273.         return $buffer;
  274.     }
  275. }
  276.  
  277. /**
  278.  * Get URL path for caching
  279.  *
  280.  * @since  1.0
  281.  * @return string
  282.  */
  283. function breeze_get_url_path() {
  284.  
  285.     $host   = ( isset( $_SERVER['HTTP_HOST'] ) ) ? $_SERVER['HTTP_HOST'] : '';
  286.     $domain = ( ( ( ! empty( $_SERVER['HTTPS'] ) && $_SERVER['HTTPS'] !== 'off' ) || ( ! empty( $_SERVER['SERVER_PORT'] ) && $_SERVER['SERVER_PORT'] == 443 ) ) ? 'https://' : 'http://' );
  287.  
  288.     return $domain . rtrim( $host, '/' ) . $_SERVER['REQUEST_URI'];
  289. }
  290.  
  291. /**
  292.  * Optionally serve cache and exit
  293.  *
  294.  * @since 1.0
  295.  */
  296. function breeze_serve_cache( $filename, $path, $X1, $opts ) {
  297.     if ( strpos( $filename, '_breeze_cache_' ) === false ) {
  298.         return;
  299.     }
  300.  
  301.     $modified_time = (int) @filemtime( $path );
  302.  
  303.     if ( ! empty( $opts['breeze-browser-cache'] ) && ! empty( $modified_time ) && ! empty( $_SERVER['HTTP_IF_MODIFIED_SINCE'] ) && strtotime( $_SERVER['HTTP_IF_MODIFIED_SINCE'] ) === $modified_time ) {
  304.         header( $_SERVER['SERVER_PROTOCOL'] . ' 304 Not Modified', true, 304 );
  305.         exit;
  306.     }
  307.  
  308.     if ( @file_exists( $path ) ) {
  309.  
  310.         $cacheFile = file_get_contents( $path );
  311.  
  312.  
  313.         if ( $cacheFile != false ) {
  314.             $datas = unserialize( $cacheFile );
  315.             foreach ( $datas['headers'] as $data ) {
  316.                 header( $data['name'] . ': ' . $data['value'] );
  317.             }
  318.             //set cache provider header
  319.             header( 'Cache-Provider:CLOUDWAYS-CACHE-' . $X1 . 'E' );
  320.  
  321.             $client_support_gzip = true;
  322.  
  323.             //check gzip request from client
  324.             if ( isset( $_SERVER['HTTP_ACCEPT_ENCODING'] ) && ( strpos( $_SERVER['HTTP_ACCEPT_ENCODING'], 'gzip' ) === false || strpos( $_SERVER['HTTP_ACCEPT_ENCODING'], 'deflate' ) === false ) ) {
  325.                 $client_support_gzip = false;
  326.             }
  327.  
  328.             if ( $client_support_gzip && function_exists( 'gzdecode' ) && ! empty( $GLOBALS['breeze_config']['cache_options']['breeze-gzip-compression'] ) ) {
  329.                 //if file is zip
  330.  
  331.                 $content = gzencode( $datas['body'], 9 );
  332.                 header( 'Content-Encoding: gzip' );
  333.                 header( "cache-control: must-revalidate" );
  334.                 header( 'Content-Length: ' . strlen( $content ) );
  335.                 header( 'Vary: Accept-Encoding' );
  336.                 echo $content;
  337.             } else {
  338.                 //render page cache
  339.                 echo $datas['body'];
  340.             }
  341.             exit;
  342.         }
  343.     }
  344. }
  345.  
  346. function check_exclude_page( $opts_config, $current_url ) {
  347.     //check disable cache for page
  348.     if ( ! empty( $opts_config['exclude_url'] ) ) {
  349.         foreach ( $opts_config['exclude_url'] as $v ) {
  350.             // Clear blank character
  351.             $v = trim( $v );
  352.             if ( preg_match( '/(\&?\/?\(\.?\*\)|\/\*|\*)$/', $v, $matches ) ) {
  353.                 // End of rules is *, /*, [&][/](*) , [&][/](.*)
  354.                 $pattent = substr( $v, 0, strpos( $v, $matches[0] ) );
  355.                 if ( $v[0] == '/' ) {
  356.                     // A path of exclude url with regex
  357.                     if ( ( @preg_match( '@' . $pattent . '@', $current_url, $matches ) > 0 ) ) {
  358.                         return true;
  359.                     }
  360.                 } else {
  361.                     // Full exclude url with regex
  362.                     if ( strpos( $current_url, $pattent ) !== false ) {
  363.                         return true;
  364.                     }
  365.                 }
  366.  
  367.             } else {
  368.                 if ( $v[0] == '/' ) {
  369.                     // A path of exclude
  370.                     if ( ( @preg_match( '@' . $v . '@', $current_url, $matches ) > 0 ) ) {
  371.                         return true;
  372.                     }
  373.                 } else { // Whole path
  374.                     if ( $v == $current_url ) {
  375.                         return true;
  376.                     }
  377.                 }
  378.             }
  379.         }
  380.     }
  381.  
  382.     return false;
  383. }
Add Comment
Please, Sign In to add comment