length(); } /** * @see https://www.php.net/strpos * * @param string $haystack Input haystack to search * @param string $needle Input needle to search for * @param int $offset Offset at which to begin searching * * @return int Position of needle within haystack */ public static function strpos($haystack, $needle, $offset = 0) { return Stringy::create($haystack)->indexOf($needle, $offset); } /** * @see https://www.php.net/strrpos * * @param string $haystack Haystack to search * @param string $needle Needle to search haystack for * * @return int Last index of Needle in Haystack */ public static function strrpos($haystack, $needle) { return Stringy::create($haystack)->indexOfLast($needle); } /** * @see https://www.php.net/substr * * @param string $string Subject to extract substring from * @param int $start Position to start from * @param int $length Length to extract, or false for entire string from start position * * @return string Substring of $string */ public static function substr($string, $start, $length = null) { return (string) Stringy::create($string)->substr($start, $length); } /** * @see https://www.php.net/strtolower * * @param string $string Input string * * @return string Lower case version of input string */ public static function strtolower($string) { return (string) Stringy::create($string)->toLowerCase(); } /** * @see https://www.php.net/strtoupper * * @param string $string Input string * * @return string Upper case version of input string */ public static function strtoupper($string) { return (string) Stringy::create($string)->toUpperCase(); } /** * @see https://www.php.net/ucfirst * * @param string $string Input string * * @return string ucfirst version of input string */ public static function ucfirst($string) { return (string) Stringy::create($string)->upperCaseFirst(); } /** * @see https://www.php.net/substr_count * * @param string $haystack Input string to search * @param string $needle String to search within $haystack for * * @return int Count of number of times $needle appeared in $haystack */ public static function substr_count($haystack, $needle) { return Stringy::create($haystack)->countSubstr($needle); } /** * @see https://www.php.net/encode_mime_header * * @param string $string Input MIME header to encode. * * @return string Encoded MIME header. */ public static function encode_mime_header($string) { static::initialize(); return static::hasMBString() ? mb_encode_mimeheader($string, mb_internal_encoding(), 'B', Core::isWindows() ? "\r\n" : "\n") : $string; } // // Wrappers for PCRE-compatible regular expression routines. // See the php.net documentation for usage. // /** * @see https://www.php.net/preg_quote * * @param string $string String to quote * @param string $delimiter Delimiter for regular expression * * @return string Quoted equivalent of $string */ public static function regexp_quote($string, $delimiter = '/') { return preg_quote($string, $delimiter); } /** * @see https://www.php.net/preg_grep * * @param string $pattern Regular expression * @param array $input Input * * @return array */ public static function regexp_grep($pattern, $input) { return preg_grep($pattern . 'u', $input); } /** * @see https://www.php.net/preg_match * * @param string $pattern Regular expression * @param string $subject String to apply regular expression to * * @return int */ public static function regexp_match($pattern, $subject) { return preg_match($pattern . 'u', $subject); } /** * @see https://www.php.net/preg_match_get * * @param string $pattern Regular expression * @param string $subject String to apply regular expression to * @param array $matches Reference to receive matches * * @return int|boolean Returns 1 if the pattern matches given subject, 0 if it does not, or FALSE if an error occurred. */ public static function regexp_match_get($pattern, $subject, &$matches) { return preg_match($pattern . 'u', $subject, $matches); } /** * @see https://www.php.net/preg_match_all * * @param string $pattern Regular expression * @param string $subject String to apply regular expression to * @param array $matches Reference to receive matches * * @return int|boolean Returns number of full matches of given subject, or FALSE if an error occurred. */ public static function regexp_match_all($pattern, $subject, &$matches) { return preg_match_all($pattern . 'u', $subject, $matches); } /** * @see https://www.php.net/preg_replace * * @param string $pattern Regular expression * @param string $replacement String to replace matches in $subject with * @param string $subject String to apply regular expression to * @param int $limit Number of replacements to perform, maximum, or -1 for no limit. */ public static function regexp_replace($pattern, $replacement, $subject, $limit = -1) { return preg_replace($pattern . 'u', (string) $replacement, (string) $subject, $limit); } /** * @see https://www.php.net/preg_replace_callback * * @param string $pattern Regular expression * @param callable $callback PHP callback to generate content to replace matches with * @param string $subject String to apply regular expression to * @param int $limit Number of replacements to perform, maximum, or -1 for no limit. */ public static function regexp_replace_callback($pattern, $callback, $subject, $limit = -1) { return preg_replace_callback($pattern . 'u', $callback, $subject, $limit); } /** * @see https://www.php.net/preg_split * * @param string $pattern Regular expression * @param string $subject String to apply regular expression to * @param int $limit Number of times to match; -1 for unlimited * * @return array Resulting string segments */ public static function regexp_split($pattern, $subject, $limit = -1) { return preg_split($pattern . 'u', $subject, $limit); } /** * @see https://www.php.net/mime_content_type * * @param string $filename Filename to test. * @param string $suggestedExtension Suggested file extension (used for common misconfigurations) * * @return string Detected MIME type */ public static function mime_content_type($filename, $suggestedExtension = '') { $result = null; if (function_exists('finfo_open')) { $fi = & Registry::get('fileInfo', true, null); if ($fi === null) { $fi = finfo_open(FILEINFO_MIME, Config::getVar('finfo', 'mime_database_path')); } if ($fi !== false) { $result = strtok(finfo_file($fi, $filename), ' ;'); } } if (!$result && function_exists('mime_content_type')) { $result = mime_content_type($filename); // mime_content_type appears to return a charset // (erroneously?) in recent versions of PHP5 if (($i = strpos($result, ';')) !== false) { $result = trim(substr($result, 0, $i)); } } if (!$result) { // Fall back on an external "file" tool $f = escapeshellarg($filename); $result = trim(`file --brief --mime $f`); // Make sure we just return the mime type. if (($i = strpos($result, ';')) !== false) { $result = trim(substr($result, 0, $i)); } } // Check ambiguous mimetypes against extension $exploded = explode('.', $filename); $ext = array_pop($exploded); if ($suggestedExtension) { $ext = $suggestedExtension; } $ambiguities = self::getAmbiguousExtensionsMap(); if (isset($ambiguities[strtolower($ext . ':' . $result)])) { $result = $ambiguities[strtolower($ext . ':' . $result)]; } return $result; } /** * @return string[] * * @brief overrides for ambiguous mime types returned by finfo * SUGGESTED_EXTENSION:DETECTED_MIME_TYPE => OVERRIDE_MIME_TYPE */ public static function getAmbiguousExtensionsMap() { return [ 'html:text/xml' => 'text/html', 'css:text/x-c' => 'text/css', 'css:text/plain' => 'text/css', 'csv:text/plain' => 'text/csv', 'js:text/plain' => 'text/javascript', 'xlsx:application/zip' => 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet', 'xltx:application/zip' => 'application/vnd.openxmlformats-officedocument.spreadsheetml.template', 'potx:application/zip' => 'application/vnd.openxmlformats-officedocument.presentationml.template', 'ppsx:application/zip' => 'application/vnd.openxmlformats-officedocument.presentationml.slideshow', 'pptx:application/zip' => 'application/vnd.openxmlformats-officedocument.presentationml.presentation', 'sldx:application/zip' => 'application/vnd.openxmlformats-officedocument.presentationml.slide', 'docm:application/vnd.openxmlformats-officedocument.wordprocessingml.document' => 'application/vnd.ms-word.document.macroEnabled.12', 'docx:application/zip' => 'application/vnd.openxmlformats-officedocument.wordprocessingml.document', 'dotx:application/zip' => 'application/vnd.openxmlformats-officedocument.wordprocessingml.template', 'wma:video/x-ms-asf' => 'audio/x-ms-wma', 'wmv:video/x-ms-asf' => 'video/x-ms-wmv', ]; } /** * Strip unsafe HTML from the input text. Covers XSS attacks like scripts, * onclick(...) attributes, javascript: urls, and special characters. * * @param string $input input string * @param string $configKey The config section key['allowed_html', 'allowed_title_html'] * * @return string */ public static function stripUnsafeHtml($input, $configKey = 'allowed_html') { static $purifier; if (!isset($purifier)) { $config = HTMLPurifier_Config::createDefault(); $config->set('Core.Encoding', 'utf-8'); $config->set('HTML.Doctype', 'HTML 4.01 Transitional'); $config->set('HTML.Allowed', Config::getVar('security', $configKey)); $config->set('Cache.SerializerPath', 'cache'); $purifier = new HTMLPurifier($config); } return $purifier->purify((string) $input); } /** * Convert limited HTML into a string. * * @param string $html * * @return string */ public static function html2text($html) { $html = self::regexp_replace('/<[\/]?p>/', "\n", $html); $html = self::regexp_replace('/