Changeset 696 for trunk/lib


Ignore:
Timestamp:
Aug 8, 2019 10:03:59 PM (5 years ago)
Author:
anonymous
Message:

Add unicode flag (/u) to preg_* patterns. Remove usage of create_function().

Location:
trunk/lib
Files:
5 edited

Legend:

Unmodified
Added
Removed
  • trunk/lib/CSS.inc.php

    r603 r696  
    182182                // Strip whitespace and print file.
    183183                echo preg_replace(
    184                     array('!/\*.*?\*/!s', '/[\n\r]+/', '/([;:])\s+/m', '/\s*}[ \t]*/', '/\s*{\s*/', '/[ \t\n\r]*,[ \t\n\r]*/', '/^\s+/'),
     184                    array('!/\*.*?\*/!su', '/[\n\r]+/u', '/([;:])\s+/mu', '/\s*}[ \t]*/u', '/\s*{\s*/u', '/[ \t\n\r]*,[ \t\n\r]*/u', '/^\s+/u'),
    185185                    array('', "\n", '$1', '}', '{', ',', ''), file_get_contents($file, true)
    186186                );
  • trunk/lib/Email.inc.php

    r684 r696  
    118118        . '(?:\s*>\s*|>\s+\([^,@]+\)\s*)'                               // TRUE, ensure ending >
    119119        . '|'
    120         . '(?:|\s*|\s+\([^,@]+\)\s*))$/i'));                            // FALSE ensure there is no ending >
     120        . '(?:|\s*|\s+\([^,@]+\)\s*))$/iu'));                            // FALSE ensure there is no ending >
    121121
    122122        if (isset($params)) {
     
    260260        // Apply regex pattern to search elements.
    261261        $search = array_keys($replacements);
    262         array_walk($search, create_function('&$v', '$v = "{" . mb_strtoupper($v) . "}";'));
     262        array_walk($search, function (&$v) {
     263            $v = '{' . mb_strtoupper($v) . '}';
     264        });
    263265
    264266        // Replacement values.
     
    274276    * You can also use this function to do post-processing on the email body before sending it,
    275277    * like removing extraneous lines:
    276     * $email->setString(preg_replace('/(?:(?:\r\n|\r|\n)\s*){2}/s', "\n\n", $email->getBody()));
     278    * $email->setString(preg_replace('/(?:(?:\r\n|\r|\n)\s*){2}/su', "\n\n", $email->getBody()));
    277279    *
    278280    * @access   public
     
    367369        foreach ($headers as $key => $val) {
    368370            // Validate key and values.
    369             if (empty($val)) {
     371            if (!strlen($val)) {
    370372                $app->logMsg(sprintf('Empty email header provided: %s', $key), LOG_NOTICE, __FILE__, __LINE__);
    371373                continue;
    372374            }
    373             if (empty($key) || !is_string($key) || !is_string($val) || preg_match("/[\n\r]/", $key . $val) || preg_match('/[^\w-]/', $key)) {
     375            if (!strlen($key) || preg_match("/[\n\r]/", $key . $val) || preg_match('/[^\w-]/', $key)) {
    374376                $app->logMsg(sprintf('Broken email header provided: %s=%s', $key, $val), LOG_WARNING, __FILE__, __LINE__);
    375377                continue;
     
    394396            $envelope_sender_address = sprintf('<%s>', trim($this->_params['envelope_sender_address'], '<>'));
    395397        } else {
    396             $envelope_sender_address = preg_replace('/^.*<?([^\s@\[\]<>()]+\@[A-Za-z0-9.-]{1,}\.[A-Za-z]{2,5})>?$/iU', '$1', $this->_params['from']);
     398            $envelope_sender_address = preg_replace('/^.*<?([^\s@\[\]<>()]+\@[A-Za-z0-9.-]{1,}\.[A-Za-z]{2,5})>?$/iUu', '$1', $this->_params['from']);
    397399        }
    398400        if ('' != $envelope_sender_address && $this->validEmail($envelope_sender_address)) {
  • trunk/lib/JS.inc.php

    r603 r696  
    182182                // Strip whitespace and print file.
    183183                echo preg_replace(
    184                     array('/(?<=^|;|{)\s*\/\/.*$/m', '/(?<=^|;|{)\s*\/\*.*?\*\//ms', '/[\n\r]+/', '/[ \t]+}[ \t]+/', '/[ \t]+{[ \t]+/', '/\s+=\s+/', '/^[ \t]+/m', '/[ \t]+$/m'),
     184                    array('/(?<=^|;|{)\s*\/\/.*$/mu', '/(?<=^|;|{)\s*\/\*.*?\*\//msu', '/[\n\r]+/u', '/[ \t]+}[ \t]+/u', '/[ \t]+{[ \t]+/u', '/\s+=\s+/u', '/^[ \t]+/mu', '/[ \t]+$/mu'),
    185185                    array('', '', "\n", '}', '{', '=', '', ''), file_get_contents($file, true)
    186186                );
  • trunk/lib/Utilities.inc.php

    r690 r696  
    122122    $d = ob_get_contents();
    123123    ob_end_clean();
    124     return $serialize ? preg_replace('/\s+/m', ' ', $d) : $d;
     124    return $serialize ? preg_replace('/\s+/mu', ' ', $d) : $d;
    125125}
    126126
     
    154154        $output .= sprintf("%s%s\n", $indent_str, $var);
    155155    }
    156     return preg_replace(['/^[ \t]+$/', '/\n\n+/', '/^(?:\S( ))?(?:\S( ))?(?:\S( ))?(?:\S( ))?(?:\S( ))?(?:\S( ))?(?:\S( ))?(?:\S( ))?(\S )/m'], ['', "\n", '$1$1$2$2$3$3$4$4$5$5$6$6$7$7$8$8$9'], $output);
     156    return preg_replace(['/^[ \t]+$/u', '/\n\n+/u', '/^(?:\S( ))?(?:\S( ))?(?:\S( ))?(?:\S( ))?(?:\S( ))?(?:\S( ))?(?:\S( ))?(?:\S( ))?(\S )/mu'], ['', "\n", '$1$1$2$2$3$3$4$4$5$5$6$6$7$7$8$8$9'], $output);
    157157}
    158158
     
    189189
    190190    // Make converted ampersand entities into normal ampersands (they will be done manually later) to retain HTML entities.
    191     $search['retain_ampersand']     = '/&amp;/';
     191    $search['retain_ampersand']     = '/&amp;/u';
    192192    $replace['retain_ampersand']    = '&';
    193193
    194194    if ($preserve_html) {
    195195        // Convert characters that must remain non-entities for displaying HTML.
    196         $search['retain_left_angle']       = '/&lt;/';
     196        $search['retain_left_angle']       = '/&lt;/u';
    197197        $replace['retain_left_angle']      = '<';
    198198
    199         $search['retain_right_angle']      = '/&gt;/';
     199        $search['retain_right_angle']      = '/&gt;/u';
    200200        $replace['retain_right_angle']     = '>';
    201201
    202         $search['retain_single_quote']     = '/&#039;/';
     202        $search['retain_single_quote']     = '/&#039;/u';
    203203        $replace['retain_single_quote']    = "'";
    204204
    205         $search['retain_double_quote']     = '/&quot;/';
     205        $search['retain_double_quote']     = '/&quot;/u';
    206206        $replace['retain_double_quote']    = '"';
    207207    }
     
    309309            // If not truncating, or URL was not truncated.
    310310            // Remove http schemas, and any single trailing / to make the display URL.
    311             $display_url = preg_replace(['!^https?://!', '!^([^/]+)/$!'], ['', '$1'], $url);
     311            $display_url = preg_replace(['!^https?://!u', '!^([^/]+)/$!u'], ['', '$1'], $url);
    312312            return sprintf('<a href="%s">%s</a>', oTxt($absolute_url), $display_url);
    313313        } else {
    314314            // Truncated URL.
    315315            // Remove http schemas, and any single trailing / to make the display URL.
    316             $display_url = preg_replace(['!^https?://!', '!^([^/]+)/$!'], ['', '$1'], trim($truncated_url));
     316            $display_url = preg_replace(['!^https?://!u', '!^([^/]+)/$!u'], ['', '$1'], trim($truncated_url));
    317317            return sprintf('<a href="%s">%s%s</a>', oTxt($absolute_url), $display_url, $delim);
    318318        }
     
    337337    foreach ($words as $w) {
    338338        if ('' != trim($w)) {
    339             $search[] = '/\b(' . preg_quote($w) . ')\b/i';
     339            $search[] = '/\b(' . preg_quote($w) . ')\b/iu';
    340340            $replace[] = '<span class="' . $class . '">$1</span>';
    341341        }
     
    368368    default :
    369369        // Reduce all hex values slightly to avoid all white.
    370         array_walk($rgb, create_function('&$v', "\$v = dechex(round(hexdec(\$v) * $n));"));
     370        array_walk($rgb, function (&$v) use ($n) {
     371            $v = dechex(round(hexdec($v) * $n));
     372        });
    371373        break;
     374
    372375    case 2 :
    373376        foreach ($rgb as $i => $v) {
     
    411414function encodeEmail($email, $at=' at ', $dot=' dot ')
    412415{
    413     $search = array('/@/', '/\./');
     416    $search = array('/@/u', '/\./u');
    414417    $replace = array($at, $dot);
    415418    return preg_replace($search, $replace, $email);
     
    529532function URLSlug($str)
    530533{
    531     $slug = preg_replace(array('/\W+/u', '/^-+|-+$/'), array('-', ''), $str);
     534    $slug = preg_replace(array('/\W+/u', '/^-+|-+$/u'), array('-', ''), $str);
    532535    $slug = strtolower($slug);
    533536    return $slug;
     
    11381141function hash64($string, $length=18)
    11391142{
    1140     return mb_substr(preg_replace('/[^\w]/', '', base64_encode(hash('sha512', $string, true))), 0, $length);
     1143    return mb_substr(preg_replace('/[^\w]/u', '', base64_encode(hash('sha512', $string, true))), 0, $length);
    11411144}
    11421145
     
    11681171    switch ($app->getParam('signing_method')) {
    11691172    case 'sha512+base64':
    1170         return $val . '-' . mb_substr(preg_replace('/[^\w]/', '', base64_encode(hash('sha512', $val . $salt, true))), 0, $length);
     1173        return $val . '-' . mb_substr(preg_replace('/[^\w]/u', '', base64_encode(hash('sha512', $val . $salt, true))), 0, $length);
    11711174
    11721175    case 'md5':
     
    13951398function stripQuery($url)
    13961399{
    1397     return preg_replace('/[?#].*$/', '', $url);
     1400    return preg_replace('/[?#].*$/u', '', $url);
    13981401}
    13991402
     
    14051408function absoluteMe()
    14061409{
    1407     $safe_http_host = preg_replace('/[^a-z\d.:-]/', '', getenv('HTTP_HOST'));
     1410    $safe_http_host = preg_replace('/[^a-z\d.:-]/u', '', getenv('HTTP_HOST'));
    14081411    return sprintf('%s://%s%s', (getenv('HTTPS') ? 'https' : 'http'), $safe_http_host, getenv('REQUEST_URI'));
    14091412}
     
    14221425    // If one of the hostnames is an IP address, compare only the path of both.
    14231426    if (preg_match('/\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}/', parse_url($current_url, PHP_URL_HOST)) || preg_match('/\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}/', parse_url($referrer_url, PHP_URL_HOST))) {
    1424         $current_url = preg_replace('@^https?://[^/]+@', '', $current_url);
    1425         $referrer_url = preg_replace('@^https?://[^/]+@', '', $referrer_url);
     1427        $current_url = preg_replace('@^https?://[^/]+@u', '', $current_url);
     1428        $referrer_url = preg_replace('@^https?://[^/]+@u', '', $referrer_url);
    14261429    }
    14271430
  • trunk/lib/Validator.inc.php

    r635 r696  
    453453        $app =& App::getInstance();
    454454        // Get rid of any non-digits
    455         $cc_num = preg_replace('/[^\d]/', '', $val);
     455        $cc_num = preg_replace('/[^\d]/u', '', $val);
    456456
    457457        // Perform card-specific checks, if applicable
Note: See TracChangeset for help on using the changeset viewer.