{ $htaccessContent = file_get_contents($htaccessFile); } if (empty($htaccessContent)) { return false; } //if isset expires header in htacces if (strpos($htaccessContent, 'mod_expires') !== false || strpos($htaccessContent, 'ExpiresActive') !== false || strpos($htaccessContent, 'ExpiresDefault') !== false || strpos($htaccessContent, 'ExpiresByType') !== false) { return false; } $htaccessContent = $expires . $htaccessContent; file_put_contents($htaccessFile, $htaccessContent); return true; } else { if (!is_super_admin()) { return false; } //open htaccess file if (is_writable($htaccessFile)) { $htaccessContent = file_get_contents($htaccessFile); } if (empty($htaccessContent)) { return false; } $htaccessContent = str_replace($expires, '', $htaccessContent); file_put_contents($htaccessFile, $htaccessContent); return true; } } /** * Remove query string from static resources * * @param string $content Content raw * * @return mixed */ public function removeQueryStrings($content) { if ($content !== '') { $blog_regexp = self::blogDomainRootUrlRegexp(); if (!$blog_regexp) { return $content; } $pattern = '~(href|src)=?([\'"])((' . $blog_regexp . ')?(/[^\'"/][^\'"]*\.([a-z-_]+)([\?#][^\'"]*)?))[\'"]~Ui'; $content = preg_replace_callback( $pattern, array($this, 'queryStringsReplaceCallback'), $content ); } return $content; } /** * Callback replace for js and css file * * @param string $matches Matches of query string * * @return string */ public function queryStringsReplaceCallback($matches) { list ($match, $attr, $quote, $url, , , $extension) = $matches; if ($extension === 'js' || $extension === 'css') { $url = preg_replace('/[&\?]+(ver=([a-z0-9-_\.]+|[0-9-]+))+[&\?]*([a-z0-9-_=]*)*/i', '', $url); } return $attr . '=' . $quote . $url . $quote; } /** * Returns domain url regexp * * @return string */ public static function blogDomainRootUrlRegexp() { $home_url = get_option('home'); $parse_url = parse_url($home_url); if ($parse_url && isset($parse_url['scheme']) && isset($parse_url['host'])) { $scheme = $parse_url['scheme']; $host = $parse_url['host']; $port = (isset($parse_url['port']) && $parse_url['port'] !== 80 ? ':' . (int)$parse_url['port'] : ''); $domain_url = sprintf('[%s:]*//%s%s', $scheme, $host, $port); return $domain_url; } return false; } /** * Parse module info. * Based on https://gist.github.com/sbmzhcn/6255314 * * @return array */ public static function parsePhpinfo() { ob_start(); //phpcs:ignore WordPress.PHP.DevelopmentFunctions.prevent_path_disclosure_phpinfo -- Get info modules of phpinfo phpinfo(INFO_MODULES); $s = ob_get_contents(); ob_end_clean(); $s = strip_tags($s, '

'); $s = preg_replace('/]*>([^<]+)<\/th>/', '\1', $s); $s = preg_replace('/]*>([^<]+)<\/td>/', '\1', $s); $t = preg_split('/(]*>[^<]+<\/h2>)/', $s, -1, PREG_SPLIT_DELIM_CAPTURE); $r = array(); $count = count($t); $p1 = '([^<]+)<\/info>'; $p2 = '/'.$p1.'\s*'.$p1.'\s*'.$p1.'/'; $p3 = '/'.$p1.'\s*'.$p1.'/'; for ($i = 1; $i < $count; $i++) { if (preg_match('/]*>([^<]+)<\/h2>/', $t[$i], $matchs)) { $name = trim($matchs[1]); $vals = explode("\n", $t[$i + 1]); foreach ($vals as $val) { if (preg_match($p2, $val, $matchs)) { // 3cols $r[$name][trim($matchs[1])] = array(trim($matchs[2]), trim($matchs[3])); } elseif (preg_match($p3, $val, $matchs)) { // 2cols $r[$name][trim($matchs[1])] = trim($matchs[2]); } } } } return $r; } /** * Get header response * * @param string $url URL to get header * * @return array */ public static function getHeadersResponse($url) { $args = array( 'headers' => array( 'timeout' => 30, 'redirection' => 10, ) ); // Retrieve the raw response from the HTTP request $response = wp_remote_get($url, $args); if (is_wp_error($response)) { $headers = esc_html__('Some requirements can’t be checked :', 'wp-speed-of-light') . '
' . $response->get_error_message(); } else { $headers = wp_remote_retrieve_headers($response); } return $headers; } /** * Check modules enable in system * * @return void */ public static function modulesCheck() { check_ajax_referer('wpsolSpeedOptimizationSystem', 'ajaxnonce'); global $is_apache; $modules = array( 'mod_expires' => false, 'mod_headers' => false, 'mod_deflate' => false, 'mod_filter' => false, 'gzip'=> false, ); $check_success = 0; // Apache server if ($is_apache) { foreach ($modules as $mod => $val) { if ($mod === 'gzip') { continue; } if (apache_mod_loaded($mod)) { $check_success++; $modules[$mod] = true; } } } else { // Other server is not support and remove warning $modules = array( 'mod_expires' => true, 'mod_headers' => true, 'mod_deflate' => true, 'mod_filter' => true, 'gzip'=> true, ); echo json_encode(array('list_modules' => $modules, 'error' => false)); die(); } $headers = self::getHeadersResponse(WPSOL_PLUGIN_URL . '/assets/file.php'); if (is_string($headers)) { echo json_encode(array('list_modules' => $modules, 'error' => $headers)); die(); } //Get gzip status if (!empty($headers)) { foreach ($headers as $header => $value) { if (trim(strtolower($header)) === 'content-encoding') { $modules['gzip'] = true; break; } } } // If apache_get_modules disable and phpinfo can not detect if ($check_success < 4) { if (!empty($headers)) { foreach ($headers as $header => $value) { if (trim(strtolower($header)) === 'expires') { $modules['mod_expires'] = true; continue; } if (trim(strtolower($header)) === 'wpsol-header-module') { $modules['mod_headers'] = true; continue; } if (trim(strtolower($header)) === 'wpsol-deflate-module' || trim(strtolower($header)) === 'accept-ranges') { $modules['mod_filter'] = true; $modules['mod_deflate'] = true; continue; } } } } echo json_encode(array('list_modules' => $modules, 'error' => false)); die(); } /** * Return an instance of the current class, create one if it doesn't exist * * @since 1.0 * @return object */ public static function factory() { static $instance; if (!$instance) { $instance = new self(); $instance->setAction(); } return $instance; } }