From 51076631fe455c856ec72e92b718ff5f9f41795b Mon Sep 17 00:00:00 2001 From: franzz Date: Wed, 30 Oct 2013 21:00:25 +0100 Subject: [PATCH] fixing preg_replace / preg_match (UTF-8) --- config.php | 71 +++++++++++++++++++++++++++--------------------------- index.php | 7 ++++-- 2 files changed, 41 insertions(+), 37 deletions(-) diff --git a/config.php b/config.php index 1a88a08..e00e7f9 100755 --- a/config.php +++ b/config.php @@ -479,7 +479,7 @@ class Databap extends PhpObject break; } $asResult = $this->oMySql->selectRows(array('select'=>array('id_message', 'nickname', 'message', 'led'), 'from'=>'messages', 'constraint'=>array('message'=>$sRegEx), 'constOpe'=>array('message'=>' REGEXP '))); - $sPattern = '/(https?\:\/\/|www\.)[\S]+\.[a-zA-Z]{2,4}([\S]*)/i'; + $sPattern = '/(https?\:\/\/|www\.)[\S]+\.[a-zA-Z]{2,4}([\S]*)/ui'; foreach($asResult as $iLinkId=>$asRow) { @@ -825,7 +825,7 @@ class Databap extends PhpObject $sDescription = isset($asPost['description'])?$asPost['description']:''; //Get docs - $sImagePattern = '/c1_(?P\d+)_image_(?P\w+)/'; + $sImagePattern = '/c1_(?P\d+)_image_(?P\w+)/u'; foreach($asPost as $sFormId=>$sValue) { preg_match($sImagePattern, $sFormId, $asMatches); @@ -1227,7 +1227,7 @@ class Databap extends PhpObject { $asResult = array('is_pm'=>false, 'chan_name'=>$sChanSafeName); - preg_match('/(?P\d+)'.self::PM_SEP.'(?P\d+)/', $sChanSafeName, $asMatch); + preg_match('/(?P\d+)'.self::PM_SEP.'(?P\d+)/u', $sChanSafeName, $asMatch); if(!empty($asMatch)) { $asResult['is_pm'] = true; @@ -1247,9 +1247,9 @@ class Databap extends PhpObject $iCompChan = array_search($sChanSafeName, array_map(array('self', 'getChanSafeName'), $asCompanies)); $asPm = $this->isPmChan($sChanSafeName); - return $sChanSafeName!='' && //Empty channel name + return $sChanSafeName!='' && //Empty channel name ($iCompChan===false || $asUserInfo['company']==self::getCompanyFormat($asCompanies[$iCompChan])) && //Test Company Channel - (!$asPm['is_pm'] || $iUserId==$asPm['from'] || $iUserId==$asPm['to']); //Test PM + (!$asPm['is_pm'] || $iUserId==$asPm['from'] || $iUserId==$asPm['to']); //Test PM } public function joinChan($sChanName, $bFirstConn=false, $asAttendees=array()) @@ -1507,7 +1507,7 @@ class Databap extends PhpObject } elseif(mb_substr($sMessage, 0, 6) == '/mail ' && mb_strlen($sMessage)>6) { - $sImagePattern = '/\/mail (?P\w+) (?P.*)/'; + $sImagePattern = '/\/mail (?P\w+) (?P.*)/u'; preg_match($sImagePattern, $sMessage, $asMatches); //Looking for user Id @@ -1744,13 +1744,15 @@ class Databap extends PhpObject } else //Normal message { + //TODO Internal links + //Dynamic web link - $asPatterns = array('`((?:https?|ftp)://\S+[[:alnum:]]/?)`si', '`((?$1 ', '$1'); $asMessages['messages'][$iMessageId]['message'] = preg_replace($asPatterns, $asLinks, $asMessages['messages'][$iMessageId]['message']); //Dynamic chan link - $asPatterns = '/(^|\s)#(\w*[^\s]+\w*)/'; + $asPatterns = '/(^|\s)#(\w*[^\s]+\w*)/u'; $asLinks = '\1#\2'; $asMessages['messages'][$iMessageId]['message'] = preg_replace($asPatterns, $asLinks, $asMessages['messages'][$iMessageId]['message']); } @@ -2215,9 +2217,23 @@ class Databap extends PhpObject return md5($sPass); } + public function resetChanSafeNames() + { + $iChanIdCol = MySqlManager::getId(MySqlManager::CHAN_TABLE); + $asChans = $this->oMySql->selectRows(array('select'=>array($iChanIdCol, MySqlManager::getText(MySqlManager::CHAN_TABLE)), 'from'=>MySqlManager::CHAN_TABLE), true, $iChanIdCol); + + $asResult = array(); + foreach($asChans as $iChanId=>$sChanName) + { + $asResult[$iChanId] = ($this->oMySql->updateRow(MySqlManager::CHAN_TABLE, $iChanId, array('safe_name'=>self::getChanSafeName($sChanName))) > 0)?'Fixed':'Not Fixed'; + } + return MySqlManager::implodeAll($asResult, ' : ', "\n", 'Result reset ID channel ', '.'); + } + public static function getChanSafeName($sChanName) { - $sChanSafeName = preg_replace('/[^a-z0-9]/', '_', mb_strtolower($sChanName)); + //TODO replace unsafe chars with unique id + $sChanSafeName = preg_replace('/[^a-z0-9]/u', '_', mb_strtolower($sChanName)); //Sort PM chans $asPm = self::isPmChan($sChanSafeName); @@ -2418,8 +2434,8 @@ class Procedure extends PhpObject $this->sDescription = isset($asPost['description'])?$asPost['description']:''; //Steps - $sStepPattern = '/c(?P\d+)_step_text/'; - $sImagePattern = '/c(?P\d+)_(?P\d+)_image_(?P\w+)/'; + $sStepPattern = '/c(?P\d+)_step_text/u'; + $sImagePattern = '/c(?P\d+)_(?P\d+)_image_(?P\w+)/u'; foreach($asPost as $sFormId=>$sValue) { //Step Text @@ -2706,7 +2722,6 @@ class SearchEngine extends PhpObject } $sWords = implode(self::KEYWORDS_SEPARATOR, $asWords); $sWords = mb_strtolower(str_replace("\n", self::KEYWORDS_SEPARATOR, $sWords)); - //TODO Fix char encoding $sWords = preg_replace('/(\W+)/u', self::KEYWORDS_SEPARATOR, $sWords); //remove all non-word characters //Add / Modify search database @@ -2869,20 +2884,6 @@ class SearchEngine extends PhpObject } } - private function formatCode($sCode, $sPattern) - { - preg_match_all('/\n(.*)\n(.*)'.$sPattern.'(.*)\n(.*)\n/i', $sCode, $asMatches); - if(!array_key_exists(0, $asMatches) || !is_array($asMatches[0]) || count($asMatches[0])==0) - { - $asResult = array(implode("\n", array_slice(explode("\n", $sCode), 0, 3))."\n[...]"); - } - else - { - $asResult = $asMatches[0]; - } - return $asResult; - } - private function getParsedWords($sSearchWords) { return array_unique(array_filter(explode(' ', $sSearchWords), array($this, 'checkSearchedWords'))); @@ -3084,28 +3085,28 @@ class Reader //Strings foreach($this->getWords('cString') as $sStringWord) { - $sPattern = '/>([^<]*?)'.$sStringWord.'([^<]*?)'.$sStringWord.'/'; + $sPattern = '/>([^<]*?)'.$sStringWord.'([^<]*?)'.$sStringWord.'/u'; $sCode = preg_replace($sPattern, '>$1'.$sStringWord.'$2'.$sStringWord.'', $sCode); } //Part comment - $sPattern = '/>([^<]*)\"\;([^<]*)\<\/span\>\<\/li\>/'; + $sPattern = '/>([^<]*)\"\;([^<]*)\<\/span\>\<\/li\>/u'; $sCode = preg_replace($sPattern, '>$1"$2', $sCode); //Internal Url - //$sPattern = '/>([^<]*)'.preg_quote($_GET['serv_name'], '/').'\/r\-([^<]*)/'; - $sPattern = '/>([^<\/]*)(http:\/\/'.$sSafeRexExServName.'|'.$sSafeRexExServName.')(c|code|p|proc|procedure)\-([^<]*)/'; + //$sPattern = '/>([^<]*)'.preg_quote($_GET['serv_name'], '/').'\/r\-([^<]*)/u'; + $sPattern = '/>([^<\/]*)(http:\/\/'.$sSafeRexExServName.'|'.$sSafeRexExServName.')(c|code|p|proc|procedure)\-([^<]*)/u'; $sCode = preg_replace($sPattern, '>$1', $sCode); //Global / Main - $sPattern = '/>\*\ \[('.implode('|', $this->getWords('wCodePart')).')\]/'; + $sPattern = '/>\*\ \[('.implode('|', $this->getWords('wCodePart')).')\]/u'; $sCode = preg_replace($sPattern, '>> $1', $sCode); //Core Words foreach($this->getWords('wCore') as $sCoreWord) { $sCoreWord = mb_strtolower($sCoreWord); - $sPattern = '/>(([^<]*)([^\w&<]{1})|.{0})('.$sCoreWord.')([\W])/'; + $sPattern = '/>(([^<]*)([^\w&<]{1})|.{0})('.$sCoreWord.')([\W])/u'; $sCode = preg_replace($sPattern, '>$1$4$5', $sCode); } //$sCoreWords = str_replace(' ', '\ ', implode('|', array_map('mb_strtolower', $this->getWords('wCore')))); @@ -3117,7 +3118,7 @@ class Reader $asOperators = array_unique(array_merge($this->getWords('cOperator'), $this->getWords('cCalculation'))); foreach($asOperators as $sOpWord) { - $sPattern = '/>([^<]*)\\'.$sOpWord.'/'; + $sPattern = '/>([^<]*)\\'.$sOpWord.'/u'; $sCode = preg_replace($sPattern, '>$1'.$sOpWord.'', $sCode); } //$sPattern = '/>([^<]*)['.implode(array_map('mb_strtolower', ($this->getWords('cOperator')))).']/'; @@ -3126,7 +3127,7 @@ class Reader //Numbers $sPreChar = '\\'.implode('\\', $this->getWords('cCalculation')).'\s'; - $sPattern = '/>((([^<]*)['.$sPreChar.']{1})|.{0})([0-9]+)([\W])/'; + $sPattern = '/>((([^<]*)['.$sPreChar.']{1})|.{0})([0-9]+)([\W])/u'; $sCode = preg_replace($sPattern, '>$1$4$5', $sCode); return $sCode; @@ -3230,7 +3231,7 @@ class Mask extends PhpObject private function setParts() { - while(preg_match('/\<\!-- \[PART\] (?P\S+) \[START\] --\>/', $this->sMask, $asMatch)) + while(preg_match('/\<\!-- \[PART\] (?P\S+) \[START\] --\>/u', $this->sMask, $asMatch)) { $sPartName = $asMatch['part']; diff --git a/index.php b/index.php index fc9d72d..2ae4a9c 100644 --- a/index.php +++ b/index.php @@ -155,7 +155,7 @@ if($bUserOk && $sAction!=Databap::EXT_ACCESS) { case 'add_user': $asInfo = explode('-', strtolower($oUser)); - $oDatabap->addUser($asInfo[0], $asInfo[1], $asInfo[2]); + $sResult = 'User added. Id='.$oDatabap->addUser($asInfo[0], $asInfo[1], $asInfo[2]); break; case 'reset_token': $sResult = print_r($oDatabap->resetToken(), true); @@ -164,10 +164,13 @@ if($bUserOk && $sAction!=Databap::EXT_ACCESS) $oDatabap->buildCompleteIndex(); $sResult = 'Index ok'; break; - case 'install': + case 'install_queries': $oMySqlInstall = new MySqlManager(); $sResult = $oMySqlInstall->getFullInstallQuery(); break; + case 'reset_chan_safe_names': + $sResult = $oDatabap->resetChanSafeNames(); + break; } } }