Commit cdee59f7 authored by totten's avatar totten

(#174) Implement CRM_Utils_Cache::assertValidKey

parent 4901c363
......@@ -214,4 +214,30 @@ class CRM_Utils_Cache {
throw new CRM_Core_Exception("Failed to instantiate cache. No supported cache type found. " . print_r($params, 1));
}
/**
* Assert that a key is well-formed.
*
* @param string $key
* @return string
* Same $key, if it's valid.
* @throws \CRM_Utils_Cache_InvalidArgumentException
*/
public static function assertValidKey($key) {
$strict = CRM_Utils_Constant::value('CIVICRM_PSR16_STRICT', FALSE) || defined('CIVICRM_TEST');
if (!is_string($key)) {
throw new CRM_Utils_Cache_InvalidArgumentException("Invalid cache key: Not a string");
}
if ($strict && !preg_match(';^[A-Za-z0-9_\-\. ]+$;', $key)) {
throw new CRM_Utils_Cache_InvalidArgumentException("Invalid cache key: Illegal characters");
}
if ($strict && strlen($key) > 255) {
throw new CRM_Utils_Cache_InvalidArgumentException("Invalid cache key: Too long");
}
return $key;
}
}
......@@ -399,6 +399,14 @@ if (!defined('CIVICRM_DB_CACHE_PREFIX')) {
define('CIVICRM_DB_CACHE_PREFIX', '');
}
/**
* The cache system traditionally allowed a wide range of cache-keys, but some
* cache-keys are prohibited by PSR-16.
*/
if (!defined('CIVICRM_PSR16_STRICT')) {
define('CIVICRM_PSR16_STRICT', FALSE);
}
/**
* If you have multilingual site and you are using the "inherit CMS language"
* configuration option, but wish to, for example, use fr_CA instead of the
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment