os( $host_port, ':' ); if ( $pos === false ) return $host_port; return substr( $host_port, 0, $pos ); } /** * Returns WP config file path * * @return string */ static public function wp_config_path() { $search = array( ABSPATH . 'wp-config.php', dirname( ABSPATH ) . DIRECTORY_SEPARATOR . 'wp-config.php' ); foreach ( $search as $path ) { if ( file_exists( $path ) ) { return $path; } } return false; } /** * Parses path * * @param string $path * @return mixed */ static public function parse_path( $path ) { $path = str_replace( array( '%BLOG_ID%', '%POST_ID%', '%BLOG_ID%', '%HOST%' ), array( ( isset( $GLOBALS['blog_id'] ) ? (int) $GLOBALS['blog_id'] : 0 ), ( isset( $GLOBALS['post_id'] ) ? (int) $GLOBALS['post_id'] : 0 ), Util_Environment::blog_id(), Util_Environment::host() ), $path ); return $path; } /** * Normalizes file name * * Relative to site root! * * @param string $file * @return string */ static public function normalize_file( $file ) { if ( Util_Environment::is_url( $file ) ) { if ( strstr( $file, '?' ) === false ) { $home_url_regexp = '~' . Util_Environment::home_url_regexp() . '~i'; $file = preg_replace( $home_url_regexp, '', $file ); } } if ( !Util_Environment::is_url( $file ) ) { $file = Util_Environment::normalize_path( $file ); $file = str_replace( Util_Environment::site_root(), '', $file ); $file = ltrim( $file, '/' ); } return $file; } /** * Normalizes file name for minify * * Relative to document root! * * @param string $file * @return string */ static public function normalize_file_minify( $file ) { if ( Util_Environment::is_url( $file ) ) { if ( strstr( $file, '?' ) === false ) { $domain_url_regexp = '~' . Util_Environment::home_domain_root_url_regexp() . '~i'; $file = preg_replace( $domain_url_regexp, '', $file ); } } if ( !Util_Environment::is_url( $file ) ) { $file = Util_Environment::normalize_path( $file ); $file = str_replace( Util_Environment::document_root(), '', $file ); $file = ltrim( $file, '/' ); } return $file; } /** * Normalizes file name for minify * Relative to document root! * * @param string $file * @return string */ static public function url_to_docroot_filename( $url ) { $data = array( 'home_url' => get_home_url(), 'url' => $url ); $data = apply_filters( 'w3tc_url_to_docroot_filename', $data ); $home_url = $data['home_url']; $normalized_url = $data['url']; $normalized_url = Util_Environment::remove_query_all( $normalized_url ); // cut protocol $normalized_url = preg_replace( '~^http(s)?://~', '//', $normalized_url ); $home_url = preg_replace( '~^http(s)?://~', '//', $home_url ); if ( substr( $normalized_url, 0, strlen( $home_url ) ) != $home_url ) { // not a home url, return unchanged since cant be // converted to filename return $url; } else { $path_relative_to_home = str_replace( $home_url, '', $normalized_url ); $home = set_url_scheme( get_option( 'home' ), 'http' ); $siteurl = set_url_scheme( get_option( 'siteurl' ), 'http' ); $home_path = rtrim( Util_Environment::site_path(), '/' ); // adjust home_path if site is not is home if ( ! empty( $home ) && 0 !== strcasecmp( $home, $siteurl ) ) { // $siteurl - $home $wp_path_rel_to_home = rtrim( str_ireplace( $home, '', $siteurl ), '/' ); if ( substr( $home_path, -strlen( $wp_path_rel_to_home ) ) == $wp_path_rel_to_home ) { $home_path = substr( $home_path, 0, -strlen( $wp_path_rel_to_home ) ); } } // common encoded characters $path_relative_to_home = str_replace( '%20', ' ', $path_relative_to_home ); $full_filename = $home_path . DIRECTORY_SEPARATOR . trim( $path_relative_to_home, DIRECTORY_SEPARATOR ); $docroot = Util_Environment::document_root(); if ( substr( $full_filename, 0, strlen( $docroot ) ) == $docroot ) $docroot_filename = substr( $full_filename, strlen( $docroot ) ); else $docroot_filename = $path_relative_to_home; } // sometimes urls (coming from other plugins/themes) // contain multiple "/" like "my-folder//myfile.js" which // fails to recognize by filesystem, while url is accessible $docroot_filename = str_replace( '//', DIRECTORY_SEPARATOR, $docroot_filename ); return ltrim( $docroot_filename, DIRECTORY_SEPARATOR ); } /** * Translates remote file to local file * * @param string $file * @return string */ static public function translate_file( $file ) { return $file; } /** * Removes WP query string from URL */ static public function remove_query( $url ) { $url = preg_replace( '~[&\?]+(ver=([a-z0-9-_\.]+|[0-9-]+))~i', '', $url ); return $url; } /** * Removes all query strings from url */ static public function remove_query_all( $url ) { $pos = strpos( $url, '?' ); if ( $pos === false ) return $url; return substr( $url, 0, $pos ); } /** * Converts win path to unix * * @param string $path * @return string */ static public function normalize_path( $path ) { $path = preg_replace( '~[/\\\]+~', '/', $path ); $path = rtrim( $path, '/' ); return $path; } /** * Returns real path of given path * * @param string $path * @return string */ static public function realpath( $path ) { $path = Util_Environment::normalize_path( $path ); $parts = explode( '/', $path ); $absolutes = array(); foreach ( $parts as $part ) { if ( '.' == $part ) { continue; } if ( '..' == $part ) { array_pop( $absolutes ); } else { $absolutes[] = $part; } } return implode( '/', $absolutes ); } /** * Returns real path of given path * * @param string $path * @return string */ static public function path_remove_dots( $path ) { $parts = explode( '/', $path ); $absolutes = array(); foreach ( $parts as $part ) { if ( '.' == $part ) { continue; } if ( '..' == $part ) { array_pop( $absolutes ); } else { $absolutes[] = $part; } } return implode( '/', $absolutes ); } /** * Returns full URL from relative one */ static public function url_relative_to_full( $relative_url ) { $relative_url = Util_Environment::path_remove_dots( $relative_url ); if (version_compare(PHP_VERSION, '5.4.7') < 0) { if ( substr( $relative_url, 0, 2) == '//' ) { $relative_url = ( Util_Environment::is_https() ? 'https' : 'http' ) . ':' . $relative_url; } } $rel = parse_url( $relative_url ); // it's full url already if ( isset( $rel['scheme'] ) || isset( $rel['host'] ) ) return $relative_url; if ( !isset( $rel['host'] ) ) $rel['host'] = parse_url( get_home_url(), PHP_URL_HOST ); $scheme = isset( $rel['scheme'] ) ? $rel['scheme'] . '://' : '//'; $host = isset( $rel['host'] ) ? $rel['host'] : ''; $port = isset( $rel['port'] ) ? ':' . $rel['port'] : ''; $path = isset( $rel['path'] ) ? $rel['path'] : ''; $query = isset( $rel['query'] ) ? '?' . $rel['query'] : ''; return "$scheme$host$port$path$query"; } /** * Redirects to URL * * @param string $url * @param array $params * @return string */ static public function redirect( $url = '', $params = array() ) { $url = Util_Environment::url_format( $url, $params ); if ( function_exists( 'do_action' ) ) do_action( 'w3tc_redirect' ); @header( 'Location: ' . $url ); exit(); } /** * Redirects to URL * * @param string $url * @param array $params * * @return string */ static public function redirect_temp( $url = '', $params = array() ) { $url = Util_Environment::url_format( $url, $params ); if ( function_exists( 'do_action' ) ) do_action( 'w3tc_redirect' ); $status_code = 301; $protocol = $_SERVER["SERVER_PROTOCOL"]; if ( 'HTTP/1.1' === $protocol ) { $status_code = 307; } $text = get_status_header_desc( $status_code ); if ( !empty( $text ) ) { $status_header = "$protocol $status_code $text"; @header( $status_header, true, $status_code ); } @header( 'Cache-Control: no-cache' ); @header( 'Location: ' . $url, true, $status_code ); exit(); } /** * Detects post ID * * @return integer */ static public function detect_post_id() { global $posts, $comment_post_ID, $post_ID; if ( $post_ID ) { return $post_ID; } elseif ( $comment_post_ID ) { return $comment_post_ID; } elseif ( ( is_single() || is_page() ) && is_array( $posts ) ) { return $posts[0]->ID; } elseif ( is_object( $posts ) && property_exists( $posts, 'ID' ) ) { return $posts->ID; } elseif ( isset( $_REQUEST['p'] ) ) { return (integer) $_REQUEST['p']; } return 0; } static public function instance_id() { static $instance_id; if ( !isset( $instance_id ) ) { $config = Dispatcher::config(); $instance_id = $config->get_integer( 'common.instance_id', 0 ); } return $instance_id; } /** * * * @var Config $config * @return string */ static public function w3tc_edition( $config = null ) { if ( Util_Environment::is_w3tc_enterprise( $config ) ) return 'enterprise'; if ( Util_Environment::is_w3tc_pro( $config ) && Util_Environment::is_w3tc_pro_dev() ) return 'pro development'; if ( Util_Environment::is_w3tc_pro( $config ) ) return 'pro'; return 'community'; } /** * * * @param Config $config * @return bool */ static public function is_w3tc_pro( $config = null ) { if ( defined( 'W3TC_PRO' ) && W3TC_PRO ) return true; if ( is_object( $config ) ) { $plugin_type = $config->get_string( 'plugin.type' ); if ( $plugin_type == 'pro' || $plugin_type == 'pro_dev' ) return true; } if ( Util_Environment::is_w3tc_enterprise( $config ) ) return true; return false; } /** * Enable Pro Dev mode support * * @return bool */ static public function is_w3tc_pro_dev() { return defined( 'W3TC_PRO_DEV_MODE' ) && W3TC_PRO_DEV_MODE; } /** * * * @param Config $config * @return bool */ static public function is_w3tc_enterprise( $config = null ) { if ( defined( 'W3TC_ENTERPRISE' ) && W3TC_ENTERPRISE ) return true; if ( is_object( $config ) && $config->get_string( 'plugin.type' ) == 'enterprise' ) return true; return false; } /** * Checks if site is using edge mode. * * @return bool */ static public function is_w3tc_edge( $config ) { return $config->get_boolean( 'common.edge' ); } /** * Quotes regular expression string * * @param string $string * @param string $delimiter * @return string */ static public function preg_quote( $string, $delimiter = '~' ) { $string = preg_quote( $string, $delimiter ); $string = strtr( $string, array( ' ' => '\ ' ) ); return $string; } /** * Returns true if zlib output compression is enabled otherwise false * * @return boolean */ static public function is_zlib_enabled() { return Util_Environment::to_boolean( ini_get( 'zlib.output_compression' ) ); } /** * Recursive strips slahes from the var * * @param mixed $var * @return mixed */ static public function stripslashes( $var ) { if ( is_string( $var ) ) { return stripslashes( $var ); } elseif ( is_array( $var ) ) { $var = array_map( array( '\W3TC\Util_Environment', 'stripslashes' ), $var ); } return $var; } /** * Checks if post should be flushed or not. Returns true if it should not be flushed * * @param unknown $post * @param string $module which cache module to check against (pgcache, varnish, dbcache or objectcache) * @param Config $config * @return bool */ static public function is_flushable_post( $post, $module, $config ) { if ( is_numeric( $post ) ) $post = get_post( $post ); $post_status = array( 'publish' ); // dont flush when we have post "attachment" // its child of the post and is flushed always when post is published, while not changed in fact $post_type = array( 'revision', 'attachment' ); switch ( $module ) { case 'pgcache': case 'varnish': case 'posts': // means html content of post pages if ( !$config->get_boolean( 'pgcache.reject.logged' ) ) $post_status[] = 'private'; break; case 'dbcache': if ( !$config->get_boolean( 'dbcache.reject.logged' ) ) $post_status[] = 'private'; break; } $flushable = is_object( $post ) && !in_array( $post->post_type, $post_type ) && in_array( $post->post_status, $post_status ); return apply_filters( 'w3tc_flushable_post', $flushable, $post, $module ); } /** * Converts value to boolean * * @param mixed $value * @return boolean */ static public function to_boolean( $value ) { if ( is_string( $value ) ) { switch ( strtolower( $value ) ) { case '+': case '1': case 'y': case 'on': case 'yes': case 'true': case 'enabled': return true; case '-': case '0': case 'n': case 'no': case 'off': case 'false': case 'disabled': return false; } } return (boolean) $value; } }