Skip to content

Commit 385f545

Browse files
author
Marius Cramer
committed
- added utf8 conversion to db escape function
1 parent d5b5d3d commit 385f545

File tree

2 files changed

+65
-8
lines changed

2 files changed

+65
-8
lines changed

interface/lib/classes/db_mysql.inc.php

Lines changed: 32 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -425,6 +425,34 @@ public function affected() {
425425
}
426426

427427

428+
/**
429+
* check if a utf8 string is valid
430+
*
431+
* @access public
432+
* @param string $string the string to check
433+
* @return bool true if it is valid utf8, false otherwise
434+
*/
435+
private function check_utf8($str) {
436+
$len = strlen($str);
437+
for($i = 0; $i < $len; $i++){
438+
$c = ord($str[$i]);
439+
if ($c > 128) {
440+
if (($c > 247)) return false;
441+
elseif ($c > 239) $bytes = 4;
442+
elseif ($c > 223) $bytes = 3;
443+
elseif ($c > 191) $bytes = 2;
444+
else return false;
445+
if (($i + $bytes) > $len) return false;
446+
while ($bytes > 1) {
447+
$i++;
448+
$b = ord($str[$i]);
449+
if ($b < 128 || $b > 191) return false;
450+
$bytes--;
451+
}
452+
}
453+
}
454+
return true;
455+
} // end of check_utf8
428456

429457
/**
430458
* Escape a string for usage in a query
@@ -442,16 +470,16 @@ public function escape($sString) {
442470
$sString = '';
443471
}
444472

445-
/*$cur_encoding = mb_detect_encoding($sString);
473+
$cur_encoding = mb_detect_encoding($sString);
446474
if($cur_encoding != "UTF-8") {
447475
if($cur_encoding != 'ASCII') {
448-
$app->log('String ' . substr($sString, 0, 25) . '... is ' . $cur_encoding . '.', LOGLEVEL_WARN);
476+
$app->log('String ' . substr($sString, 0, 25) . '... is ' . $cur_encoding . '.', LOGLEVEL_INFO);
449477
if($cur_encoding) $sString = mb_convert_encoding($sString, 'UTF-8', $cur_encoding);
450478
else $sString = mb_convert_encoding($sString, 'UTF-8');
451479
}
452-
} elseif(!PXBase::check_utf8($sString)) {
480+
} elseif(!$this->check_utf8($sString)) {
453481
$sString = utf8_encode($sString);
454-
}*/
482+
}
455483

456484
if($this->_iConnId) return mysqli_real_escape_string($this->_iConnId, $sString);
457485
else return addslashes($sString);

server/lib/classes/db_mysql.inc.php

Lines changed: 33 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -389,6 +389,35 @@ public function affected() {
389389

390390

391391

392+
/**
393+
* check if a utf8 string is valid
394+
*
395+
* @access public
396+
* @param string $string the string to check
397+
* @return bool true if it is valid utf8, false otherwise
398+
*/
399+
private function check_utf8($str) {
400+
$len = strlen($str);
401+
for($i = 0; $i < $len; $i++){
402+
$c = ord($str[$i]);
403+
if ($c > 128) {
404+
if (($c > 247)) return false;
405+
elseif ($c > 239) $bytes = 4;
406+
elseif ($c > 223) $bytes = 3;
407+
elseif ($c > 191) $bytes = 2;
408+
else return false;
409+
if (($i + $bytes) > $len) return false;
410+
while ($bytes > 1) {
411+
$i++;
412+
$b = ord($str[$i]);
413+
if ($b < 128 || $b > 191) return false;
414+
$bytes--;
415+
}
416+
}
417+
}
418+
return true;
419+
} // end of check_utf8
420+
392421
/**
393422
* Escape a string for usage in a query
394423
*
@@ -405,16 +434,16 @@ public function escape($sString) {
405434
$sString = '';
406435
}
407436

408-
/*$cur_encoding = mb_detect_encoding($sString);
437+
$cur_encoding = mb_detect_encoding($sString);
409438
if($cur_encoding != "UTF-8") {
410439
if($cur_encoding != 'ASCII') {
411-
$app->log('String ' . substr($sString, 0, 25) . '... is ' . $cur_encoding . '.', LOGLEVEL_WARN);
440+
$app->log('String ' . substr($sString, 0, 25) . '... is ' . $cur_encoding . '.', LOGLEVEL_INFO);
412441
if($cur_encoding) $sString = mb_convert_encoding($sString, 'UTF-8', $cur_encoding);
413442
else $sString = mb_convert_encoding($sString, 'UTF-8');
414443
}
415-
} elseif(!PXBase::check_utf8($sString)) {
444+
} elseif(!$this->check_utf8($sString)) {
416445
$sString = utf8_encode($sString);
417-
}*/
446+
}
418447

419448
if($this->_iConnId) return mysqli_real_escape_string($this->_iConnId, $sString);
420449
else return addslashes($sString);

0 commit comments

Comments
 (0)