@@ -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 );
0 commit comments