Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support for Multibyte Strings #225

Closed
wants to merge 17 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions plugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,15 @@
if (!defined('WPINC')) {
exit('Do NOT access this file directly: '.basename(__FILE__));
}
$GLOBALS['wp_php_rv'] = '5.3.2'; //php-required-version// // Leaving this at v5.3.2 so that we can have more control over Dashboard messages below.
$GLOBALS['wp_php_rv']['rv'] = '5.3.2'; //php-required-version// // Leaving this at v5.3.2 so that we can have more control over Dashboard messages below.
$GLOBALS['wp_php_rv']['re'] = array('mbstring');

if (require(dirname(__FILE__).'/src/vendor/websharks/wp-php-rv/src/includes/check.php')) {
if (!empty($_REQUEST['comet_cache_mbstring_deprecated_warning_bypass']) && is_admin()) {
update_site_option('comet_cache_mbstring_deprecated_warning_bypass', time());
}

${__FILE__}['apc_enabled'] = (extension_loaded('apc') && filter_var(ini_get('apc.enabled'), FILTER_VALIDATE_BOOLEAN) && filter_var(ini_get('apc.cache_by_default'), FILTER_VALIDATE_BOOLEAN) && stripos((string) ini_get('apc.filters'), 'comet-cache') === false) ? true : false;
${__FILE__}['apc_enabled'] = (extension_loaded('apc') && filter_var(ini_get('apc.enabled'), FILTER_VALIDATE_BOOLEAN) && filter_var(ini_get('apc.cache_by_default'), FILTER_VALIDATE_BOOLEAN) && mb_stripos((string) ini_get('apc.filters'), 'comet-cache') === false) ? true : false;

if ((!version_compare(PHP_VERSION, '5.4', '>=') || ${__FILE__}['apc_enabled'])) { // If PHP <= 5.4 or APC is enabled

Expand Down
4 changes: 2 additions & 2 deletions src/includes/classes/Actions.php
Original file line number Diff line number Diff line change
Expand Up @@ -250,8 +250,8 @@ protected function ajaxClearCacheUrl($args)
}
$is_multisite = is_multisite();
$is_home = rtrim($url, '/') === rtrim($home_url, '/');
$url_host = strtolower(parse_url($url, PHP_URL_HOST));
$home_host = strtolower(parse_url($home_url, PHP_URL_HOST));
$url_host = mb_strtolower(parse_url($url, PHP_URL_HOST));
$home_host = mb_strtolower(parse_url($home_url, PHP_URL_HOST));
$is_offsite_host = !$is_multisite && $url_host !== $home_host;

if (!$this->plugin->currentUserCanClearCache()) {
Expand Down
6 changes: 3 additions & 3 deletions src/includes/classes/AdvCacheBackCompat.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,16 +44,16 @@ public static function zcRequestVars()
*/
public static function zenCacheConstants()
{
$_global_ns = strtoupper(GLOBAL_NS);
$_global_ns = mb_strtoupper(GLOBAL_NS);

if (!($constants = get_defined_constants(true)) || empty($constants['user'])) {
return; // Nothing to do; i.e. no user-defined constants.
}
foreach ($constants['user'] as $_constant => $_value) {
if (stripos($_constant, 'ZENCACHE_') !== 0) {
if (mb_stripos($_constant, 'ZENCACHE_') !== 0) {
continue; // Nothing to do here.
}
if (!($_constant_sub_name = substr($_constant, 9))) {
if (!($_constant_sub_name = mb_substr($_constant, 9))) {
continue; // Nothing to do here.
}
if (!defined($_global_ns.'_'.$_constant_sub_name)) {
Expand Down
10 changes: 5 additions & 5 deletions src/includes/classes/AutoCache.php
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ protected function autoCacheUrl($url)
if (!($url = trim((string) $url))) {
return; // Nothing to do.
}
if (!$this->plugin->options['get_requests'] && strpos($url, '?') !== false) {
if (!$this->plugin->options['get_requests'] && mb_strpos($url, '?') !== false) {
return; // We're NOT caching URLs with a query string.
}
$cache_path = $this->plugin->buildCachePath($url);
Expand Down Expand Up @@ -178,7 +178,7 @@ protected function logAutoCacheUrl($url, $wp_remote_get_response)
{
$cache_dir = $this->plugin->cacheDir();
$cache_lock = $this->plugin->cacheLock();
$auto_cache_log_file = $cache_dir.'/'.strtolower(SHORT_NAME).'-auto-cache.log';
$auto_cache_log_file = $cache_dir.'/'.mb_strtolower(SHORT_NAME).'-auto-cache.log';

if (is_file($auto_cache_log_file) && !is_writable($auto_cache_log_file)) {
throw new \Exception(sprintf(__('Auto-cache log file is NOT writable: `%1$s`. Please set permissions to `644` (or higher). `666` might be needed in some cases.', SLUG_TD), $auto_cache_log_file));
Expand All @@ -196,7 +196,7 @@ protected function logAutoCacheUrl($url, $wp_remote_get_response)
file_put_contents($auto_cache_log_file, $log_entry, FILE_APPEND);

if (filesize($auto_cache_log_file) > 2097152) {
rename($auto_cache_log_file, substr($auto_cache_log_file, 0, -4).'-archived-'.time().'.log');
rename($auto_cache_log_file, mb_substr($auto_cache_log_file, 0, -4).'-archived-'.time().'.log');
}
$this->plugin->cacheUnlock($cache_lock); // Release.
}
Expand All @@ -215,7 +215,7 @@ protected function logAutoCacheRun($total_urls, $total_time)
{
$cache_dir = $this->plugin->cacheDir();
$cache_lock = $this->plugin->cacheLock();
$auto_cache_log_file = $cache_dir.'/'.strtolower(SHORT_NAME).'-auto-cache.log';
$auto_cache_log_file = $cache_dir.'/'.mb_strtolower(SHORT_NAME).'-auto-cache.log';

if (is_file($auto_cache_log_file) && !is_writable($auto_cache_log_file)) {
throw new \Exception(sprintf(__('Auto-cache log file is NOT writable: `%1$s`. Please set permissions to `644` (or higher). `666` might be needed in some cases.', SLUG_TD), $auto_cache_log_file));
Expand All @@ -228,7 +228,7 @@ protected function logAutoCacheRun($total_urls, $total_time)
file_put_contents($auto_cache_log_file, $log_entry, FILE_APPEND);

if (filesize($auto_cache_log_file) > 2097152) {
rename($auto_cache_log_file, substr($auto_cache_log_file, 0, -4).'-archived-'.time().'.log');
rename($auto_cache_log_file, mb_substr($auto_cache_log_file, 0, -4).'-archived-'.time().'.log');
}
$this->plugin->cacheUnlock($cache_lock); // Release.
}
Expand Down
50 changes: 25 additions & 25 deletions src/includes/classes/CdnFilters.php
Original file line number Diff line number Diff line change
Expand Up @@ -138,9 +138,9 @@ public function __construct()

// Host-related properties.

$this->local_host = strtolower($this->plugin->hostToken());
$this->cdn_host = strtolower($this->plugin->options['cdn_host']);
$this->cdn_hosts = strtolower($this->plugin->options['cdn_hosts']);
$this->local_host = mb_strtolower($this->plugin->hostToken());
$this->cdn_host = mb_strtolower($this->plugin->options['cdn_host']);
$this->cdn_hosts = mb_strtolower($this->plugin->options['cdn_hosts']);
$this->parseCdnHosts(); // Convert CDN hosts to an array.

// Configure invalidation-related properties.
Expand All @@ -158,34 +158,34 @@ public function __construct()
if (!($cdn_whitelisted_extensions = trim($this->plugin->options['cdn_whitelisted_extensions']))) {
$cdn_whitelisted_extensions = implode('|', static::defaultWhitelistedExtensions());
}
$this->cdn_whitelisted_extensions = trim(strtolower($cdn_whitelisted_extensions), "\r\n\t\0\x0B".' |;,');
$this->cdn_whitelisted_extensions = trim(mb_strtolower($cdn_whitelisted_extensions), "\r\n\t\0\x0B".' |;,');
$this->cdn_whitelisted_extensions = preg_split('/[|;,\s]+/', $this->cdn_whitelisted_extensions, -1, PREG_SPLIT_NO_EMPTY);
$this->cdn_whitelisted_extensions = array_unique($this->cdn_whitelisted_extensions);

// Blacklisted extensions; if applicable.

$cdn_blacklisted_extensions = $this->plugin->options['cdn_blacklisted_extensions'];

$this->cdn_blacklisted_extensions = trim(strtolower($cdn_blacklisted_extensions), "\r\n\t\0\x0B".' |;,');
$this->cdn_blacklisted_extensions = trim(mb_strtolower($cdn_blacklisted_extensions), "\r\n\t\0\x0B".' |;,');
$this->cdn_blacklisted_extensions = preg_split('/[|;,\s]+/', $this->cdn_blacklisted_extensions, -1, PREG_SPLIT_NO_EMPTY);
$this->cdn_blacklisted_extensions[] = 'php'; // Always exclude.

$this->cdn_blacklisted_extensions = array_unique($this->cdn_blacklisted_extensions);

// Whitelisted URI patterns; if applicable.

$cdn_whitelisted_uri_patterns = trim(strtolower($this->plugin->options['cdn_whitelisted_uri_patterns']));
$cdn_whitelisted_uri_patterns = trim(mb_strtolower($this->plugin->options['cdn_whitelisted_uri_patterns']));
$cdn_whitelisted_uri_patterns = preg_split('/['."\r\n".']+/', $cdn_whitelisted_uri_patterns, null, PREG_SPLIT_NO_EMPTY);
$cdn_whitelisted_uri_patterns = array_unique($cdn_whitelisted_uri_patterns);

if ($cdn_whitelisted_uri_patterns) {
$this->cdn_whitelisted_uri_patterns = '/(?:'.implode('|', array_map(function ($pattern) {
return preg_replace(['/\\\\\*/', '/\\\\\^/'], ['.*?', '[^\/]*?'], preg_quote('/'.ltrim($pattern, '/'), '/'));
}, $cdn_whitelisted_uri_patterns)).')/i'; // CaSe inSensitive.
return preg_replace(['/\\\\\*/u', '/\\\\\^/u'], ['.*?', '[^\/]*?'], preg_quote('/'.ltrim($pattern, '/'), '/'));
}, $cdn_whitelisted_uri_patterns)).')/ui'; // CaSe inSensitive.
}
// Blacklisted URI patterns; if applicable.

$cdn_blacklisted_uri_patterns = trim(strtolower($this->plugin->options['cdn_blacklisted_uri_patterns']));
$cdn_blacklisted_uri_patterns = trim(mb_strtolower($this->plugin->options['cdn_blacklisted_uri_patterns']));
$cdn_blacklisted_uri_patterns = preg_split('/['."\r\n".']+/', $cdn_blacklisted_uri_patterns, null, PREG_SPLIT_NO_EMPTY);
$cdn_blacklisted_uri_patterns[] = '*/wp-admin/*'; // Always.

Expand All @@ -199,8 +199,8 @@ public function __construct()

if ($cdn_blacklisted_uri_patterns) {
$this->cdn_blacklisted_uri_patterns = '/(?:'.implode('|', array_map(function ($pattern) {
return preg_replace(['/\\\\\*/', '/\\\\\^/'], ['.*?', '[^\/]*?'], preg_quote('/'.ltrim($pattern, '/'), '/'));
}, $cdn_blacklisted_uri_patterns)).')/i'; // CaSe inSensitive.
return preg_replace(['/\\\\\*/u', '/\\\\\^/u'], ['.*?', '[^\/]*?'], preg_quote('/'.ltrim($pattern, '/'), '/'));
}, $cdn_blacklisted_uri_patterns)).')/ui'; // CaSe inSensitive.
}
// Maybe attach filters.

Expand Down Expand Up @@ -337,7 +337,7 @@ public function contentFilter($string)
if (!($string = (string) $string)) {
return $string; // Nothing to do.
}
if (strpos($string, '<') === false) {
if (mb_strpos($string, '<') === false) {
return $string; // Nothing to do.
}

Expand All @@ -357,7 +357,7 @@ public function contentFilter($string)

'(\>)'.// Tag close; group #9.

'/i'; // End regex pattern; case insensitive.
'/ui'; // End regex pattern; case insensitive.

$orig_string = $string; // In case of regex errors.
$string = preg_replace_callback($regex_url_attrs, function ($m) {
Expand Down Expand Up @@ -462,27 +462,27 @@ protected function localFile($url_uri_qsl)
if (empty($parsed['host']) && empty($this->cdn_hosts[$this->local_host])) {
return; // Not on this host name.
}
if (!empty($parsed['host']) && empty($this->cdn_hosts[strtolower($parsed['host'])])) {
if (!empty($parsed['host']) && empty($this->cdn_hosts[mb_strtolower($parsed['host'])])) {
return; // Not on this host name.
}
if (!isset($parsed['path'][0]) || $parsed['path'][0] !== '/') {
return; // Missing or unexpected path.
}
if (substr($parsed['path'], -1) === '/') {
if (mb_substr($parsed['path'], -1) === '/') {
return; // Directory, not a file.
}
if (strpos($parsed['path'], '..') !== false || strpos($parsed['path'], './') !== false) {
if (mb_strpos($parsed['path'], '..') !== false || mb_strpos($parsed['path'], './') !== false) {
return; // A relative path that is not absolute.
}
$scheme = null; // Default scheme handling.
$host = $this->local_host; // Default host name.
$uri = $parsed['path']; // Put URI together.

if (!empty($parsed['scheme'])) {
$scheme = strtolower($parsed['scheme']);
$scheme = mb_strtolower($parsed['scheme']);
}
if (!empty($parsed['host'])) {
$host = strtolower($parsed['host']);
$host = mb_strtolower($parsed['host']);
}
if (!empty($parsed['query'])) {
$uri .= '?'.$parsed['query'];
Expand Down Expand Up @@ -512,7 +512,7 @@ protected function extension($path)
return ''; // No path.
}

return strtolower(ltrim((string) strrchr(basename($path), '.'), '.'));
return mb_strtolower(ltrim((string) strrchr(basename($path), '.'), '.'));
}

/**
Expand All @@ -526,14 +526,14 @@ protected function parseCdnHosts()
$this->cdn_hosts = []; // Initialize.

$lines = str_replace(["\r\n", "\r"], "\n", $lines);
$lines = trim(strtolower($lines)); // Force all mappings to lowercase.
$lines = trim(mb_strtolower($lines)); // Force all mappings to lowercase.
$lines = preg_split('/['."\r\n".']+/', $lines, null, PREG_SPLIT_NO_EMPTY);

foreach ($lines as $_line) {
if (!($_line = trim($_line))) {
continue; // Invalid line.
}
if (strpos($_line, '=') !== false) {
if (mb_strpos($_line, '=') !== false) {
$_parts = explode('=', $_line, 2);
} else {
$_parts = [$this->local_host, $_line];
Expand All @@ -556,7 +556,7 @@ protected function parseCdnHosts()

if (empty($this->cdn_hosts[$this->local_host])) {
if ($this->cdn_host && (!is_multisite() || is_main_site())) {
$this->cdn_hosts[strtolower((string) $this->plugin->parseUrl(network_home_url(), PHP_URL_HOST))][] = $this->cdn_host;
$this->cdn_hosts[mb_strtolower((string) $this->plugin->parseUrl(network_home_url(), PHP_URL_HOST))][] = $this->cdn_host;
}
}
}
Expand All @@ -571,13 +571,13 @@ protected function parseCdnHosts()
public static function defaultWhitelistedExtensions()
{
$extensions = array_keys(wp_get_mime_types());
$extensions = explode('|', strtolower(implode('|', $extensions)));
$extensions = explode('|', mb_strtolower(implode('|', $extensions)));
$extensions = array_merge($extensions, ['eot', 'ttf', 'otf', 'woff']);

if (($permalink_structure = get_option('permalink_structure'))) {
if (strcasecmp(substr($permalink_structure, -5), '.html') === 0) {
if (strcasecmp(mb_substr($permalink_structure, -5), '.html') === 0) {
$extensions = array_diff($extensions, ['html']);
} elseif (strcasecmp(substr($permalink_structure, -4), '.htm') === 0) {
} elseif (strcasecmp(mb_substr($permalink_structure, -4), '.htm') === 0) {
$extensions = array_diff($extensions, ['htm']);
}
}
Expand Down
6 changes: 3 additions & 3 deletions src/includes/classes/Conflicts.php
Original file line number Diff line number Diff line change
Expand Up @@ -77,9 +77,9 @@ protected function maybeEnqueueNotice()
return; // Already did this in one plugin or the other.
}
$construct_name = function ($slug_or_ns) {
$name = trim(strtolower((string) $slug_or_ns));
$name = preg_replace('/[_\-]+(?:lite|pro)$/', '', $name);
$name = preg_replace('/[^a-z0-9]/', ' ', $name);
$name = trim(mb_strtolower((string) $slug_or_ns));
$name = preg_replace('/[_\-]+(?:lite|pro)$/u', '', $name);
$name = preg_replace('/[^a-z0-9]/u', ' ', $name);
$name = str_replace('cache', 'Cache', ucwords($name));

return $name; // e.g., `x-cache` becomes `X Cache`.
Expand Down
14 changes: 7 additions & 7 deletions src/includes/classes/FeedUtils.php
Original file line number Diff line number Diff line change
Expand Up @@ -174,14 +174,14 @@ public function postTermFeedLinkVariations(\WP_Post $post, $include_regex_wildca
$_term_feed_link = get_term_feed_link($_post_term->term_id, $_post_term->taxonomy, $_feed_type);
$variations[] = $_term_feed_link; // Add this variation; always.

if ($include_regex_wildcard_keys && $_term_feed_link && strpos($_term_feed_link, '?') === false) {
if ($include_regex_wildcard_keys && $_term_feed_link && mb_strpos($_term_feed_link, '?') === false) {
// Quick example: `(?:123|slug)`; to consider both of these variations.
$_term_id_or_slug = '(?:'.preg_quote($_post_term->term_id, '/').
'|'.preg_quote(preg_replace('/[^a-z0-9\/.]/i', '-', $_post_term->slug), '/').')';
'|'.preg_quote(preg_replace('/[^a-z0-9\/.]/ui', '-', $_post_term->slug), '/').')';

// Quick example: `http://www.example.com/tax/term/feed`;
// with a wildcard this becomes: `http://www.example.com/tax/*/feed`.
$_term_feed_link_with_wildcard = preg_replace('/\/[^\/]+\/feed([\/?#]|$)/', '/*/feed'.'${1}', $_term_feed_link);
$_term_feed_link_with_wildcard = preg_replace('/\/[^\/]+\/feed([\/?#]|$)/u', '/*/feed'.'${1}', $_term_feed_link);

// Quick example: `http://www.example.com/tax/*/feed`;
// becomes: `\/http\/www\.example\.com\/tax\/.*?(?=[\/\-]?(?:123|slug)[\/\-]).*?\/feed`
Expand Down Expand Up @@ -250,14 +250,14 @@ public function convertVariationsToHostCachePathRegexFrags(array $variations)
$_host_url = rtrim('http://'.$_url_parts['host'].$_host_base_dir_tokens, '/');
$_host_cache_path = $this->plugin->buildCachePath($_host_url, '', '', $flags);

if (is_string($_key) && strpos($_key, '::') !== false && strpos($_url, '*') !== false) {
if (is_string($_key) && mb_strpos($_key, '::') !== false && mb_strpos($_url, '*') !== false) {
list($_feed_type, $_wildcard_regex) = explode('::', $_key, 2); // This regex replaces wildcards.
$_cache_path = $this->plugin->buildCachePath($_url, '', '', $flags | $this::CACHE_PATH_ALLOW_WILDCARDS);
$_relative_cache_path = preg_replace('/^'.preg_quote($_host_cache_path, '/').'(?:\/|$)/i', '', $_cache_path);
$_relative_cache_path_regex = preg_replace('/\\\\\*/', $_wildcard_regex, preg_quote($_relative_cache_path, '/'));
$_relative_cache_path = preg_replace('/^'.preg_quote($_host_cache_path, '/').'(?:\/|$)/ui', '', $_cache_path);
$_relative_cache_path_regex = preg_replace('/\\\\\*/u', $_wildcard_regex, preg_quote($_relative_cache_path, '/'));
} else {
$_cache_path = $this->plugin->buildCachePath($_url, '', '', $flags); // Default flags.
$_relative_cache_path = preg_replace('/^'.preg_quote($_host_cache_path, '/').'(?:\/|$)/i', '', $_cache_path);
$_relative_cache_path = preg_replace('/^'.preg_quote($_host_cache_path, '/').'(?:\/|$)/ui', '', $_cache_path);
$_relative_cache_path_regex = preg_quote($_relative_cache_path, '/');
}
if ($_relative_cache_path_regex) {
Expand Down
Loading