OSDN Git Service

BugTrack/2512 preg_replace with PCRE_UTF8 (/u) option (AutoLink) bugtrack_2518_preg_u_opt
authorumorigu <umorigu@gmail.com>
Thu, 20 Aug 2020 17:15:36 +0000 (02:15 +0900)
committerumorigu <umorigu@gmail.com>
Sun, 2 May 2021 14:38:15 +0000 (23:38 +0900)
* BugTrack/2512 AutoAlias fail for some specific chars
* BugTrack/2518 AutoLink fail for some specific chars

Set /xu option for preg_replace() with /x

lib/func.php
lib/init.php
lib/make_link.php

index 1d50316..333e32c 100644 (file)
@@ -800,6 +800,7 @@ function get_autoaliases()
 {
        global $aliaspage, $autoalias_max_words;
        static $pairs;
+       $preg_u = get_preg_u();
 
        if (! isset($pairs)) {
                $pairs = array();
@@ -813,7 +814,8 @@ EOD;
                $matches  = array();
                $count = 0;
                $max   = max($autoalias_max_words, 0);
-               if (preg_match_all('/' . $pattern . '/x', $postdata, $matches, PREG_SET_ORDER)) {
+               if (preg_match_all('/' . $pattern . '/x' . get_preg_u(), $postdata,
+                       $matches, PREG_SET_ORDER)) {
                        foreach($matches as $key => $value) {
                                if ($count ==  $max) break;
                                $name = trim($value[1]);
@@ -1157,6 +1159,22 @@ function manage_page_redirect() {
        return FALSE;
 }
 
+/**
+ * Return 'u' (PCRE_UTF8) if PHP7+ and UTF-8.
+ */
+function get_preg_u() {
+       static $utf8u; // 'u'(PCRE_UTF8) or ''
+       if (! isset($utf8u)) {
+               if (version_compare('7.0.0', PHP_VERSION, '<=')
+                       && defined('PKWK_UTF8_ENABLE')) {
+                       $utf8u = 'u';
+               } else {
+                       $utf8u = '';
+               }
+       }
+       return $utf8u;
+}
+
 //// Compat ////
 
 // is_a --  Returns TRUE if the object is of this class or has this class as one of its parents
index e3bb30f..0b4ba41 100644 (file)
@@ -425,7 +425,8 @@ $BracketName = '(?!\s):?[^\r\n\t\f\[\]<>#&":]+:?(?<!\s)';
 $InterWikiName = '(\[\[)?((?:(?!\s|:|\]\]).)+):(.+)(?(1)\]\])';
 
 // 注釈
-$NotePattern = '/\(\(((?:(?>(?:(?!\(\()(?!\)\)(?:[^\)]|$)).)+)|(?R))*)\)\)/x';
+$NotePattern = '/\(\(((?:(?>(?:(?!\(\()(?!\)\)(?:[^\)]|$)).)+)|(?R))*)\)\)/x'
+       . get_preg_u();
 
 /////////////////////////////////////////////////
 // 初期設定(ユーザ定義ルール読み込み)
index 36063c6..73ade5f 100644 (file)
@@ -117,7 +117,7 @@ class InlineConverter
                $this->page   = $page;
                $this->result = array();
 
-               $string = preg_replace_callback('/' . $this->pattern . '/x',
+               $string = preg_replace_callback('/' . $this->pattern . '/x' . get_preg_u(),
                        array(& $this, 'replace'), $string);
 
                $arr = explode("\x08", make_line_rules(htmlsc($string)));
@@ -141,7 +141,8 @@ class InlineConverter
        function get_objects($string, $page)
        {
                $matches = $arr = array();
-               preg_match_all('/' . $this->pattern . '/x', $string, $matches, PREG_SET_ORDER);
+               preg_match_all('/' . $this->pattern . '/x' . get_preg_u(),
+                       $string, $matches, PREG_SET_ORDER);
                foreach ($matches as $match) {
                        $obj = $this->get_converter($match);
                        if ($obj->set($match, $page) !== FALSE) {
@@ -281,7 +282,7 @@ EOD;
 
                // Re-get true plugin name and patameters (for PHP 4.1.2)
                $matches = array();
-               if (preg_match('/^' . $this->pattern . '/x', $all, $matches)
+               if (preg_match('/^' . $this->pattern . '/x' . get_preg_u(), $all, $matches)
                        && $matches[1] != $this->plain) 
                        list(, $this->plain, $name, $this->param) = $matches;