1
0
mirror of https://github.com/dg/dibi.git synced 2025-08-05 05:37:39 +02:00

implemented PCRE error checking and PcreException

This commit is contained in:
David Grudl
2010-05-16 22:06:29 +02:00
parent 555e825bb2
commit 553f7da5f9
4 changed files with 28 additions and 5 deletions

View File

@@ -62,6 +62,25 @@ if (!class_exists('FileNotFoundException', FALSE)) {
class FileNotFoundException extends IOException {} class FileNotFoundException extends IOException {}
} }
if (!class_exists('PcreException', FALSE)) {
/** @package exceptions */
class PcreException extends Exception {
public function __construct()
{
static $messages = array(
PREG_INTERNAL_ERROR => 'Internal error.',
PREG_BACKTRACK_LIMIT_ERROR => 'Backtrack limit was exhausted.',
PREG_RECURSION_LIMIT_ERROR => 'Recursion limit was exhausted.',
PREG_BAD_UTF8_ERROR => 'Malformed UTF-8 data.',
5 => 'Offset didn\'t correspond to the begin of a valid UTF-8 code point.', // PREG_BAD_UTF8_OFFSET_ERROR
);
$code = preg_last_error();
parent::__construct(isset($messages[$code]) ? $messages[$code] : 'Unknown error.', $code);
}
}
}
if (!interface_exists(/*Nette\*/'IDebugPanel', FALSE)) { if (!interface_exists(/*Nette\*/'IDebugPanel', FALSE)) {
require_once dirname(__FILE__) . '/Nette/IDebugPanel.php'; require_once dirname(__FILE__) . '/Nette/IDebugPanel.php';
} }

View File

@@ -175,6 +175,8 @@ class DibiMySqlDriver extends DibiObject implements IDibiDriver
{ {
$res = array(); $res = array();
preg_match_all('#(.+?): +(\d+) *#', mysql_info($this->connection), $matches, PREG_SET_ORDER); preg_match_all('#(.+?): +(\d+) *#', mysql_info($this->connection), $matches, PREG_SET_ORDER);
if (preg_last_error()) throw new PcreException;
foreach ($matches as $m) { foreach ($matches as $m) {
$res[$m[1]] = (int) $m[2]; $res[$m[1]] = (int) $m[2];
} }

View File

@@ -159,6 +159,8 @@ class DibiMySqliDriver extends DibiObject implements IDibiDriver
{ {
$res = array(); $res = array();
preg_match_all('#(.+?): +(\d+) *#', mysqli_info($this->connection), $matches, PREG_SET_ORDER); preg_match_all('#(.+?): +(\d+) *#', mysqli_info($this->connection), $matches, PREG_SET_ORDER);
if (preg_last_error()) throw new PcreException;
foreach ($matches as $m) { foreach ($matches as $m) {
$res[$m[1]] = (int) $m[2]; $res[$m[1]] = (int) $m[2];
} }

View File

@@ -124,7 +124,7 @@ final class DibiTranslator extends DibiObject
array($this, 'cb'), array($this, 'cb'),
substr($arg, $toSkip) substr($arg, $toSkip)
); );
if (preg_last_error()) throw new PcreException;
} }
continue; continue;
} }
@@ -381,16 +381,16 @@ final class DibiTranslator extends DibiObject
$value = (string) $value; $value = (string) $value;
// speed-up - is regexp required? // speed-up - is regexp required?
$toSkip = strcspn($value, '`[\'":'); $toSkip = strcspn($value, '`[\'":');
if (strlen($value) === $toSkip) { // needn't be translated if (strlen($value) !== $toSkip) {
return $value; $value = substr($value, 0, $toSkip)
} else {
return substr($value, 0, $toSkip)
. preg_replace_callback( . preg_replace_callback(
'/(?=[`[\'":])(?:`(.+?)`|\[(.+?)\]|(\')((?:\'\'|[^\'])*)\'|(")((?:""|[^"])*)"|(\'|")|:(\S*?:)([a-zA-Z0-9._]?))/s', '/(?=[`[\'":])(?:`(.+?)`|\[(.+?)\]|(\')((?:\'\'|[^\'])*)\'|(")((?:""|[^"])*)"|(\'|")|:(\S*?:)([a-zA-Z0-9._]?))/s',
array($this, 'cb'), array($this, 'cb'),
substr($value, $toSkip) substr($value, $toSkip)
); );
if (preg_last_error()) throw new PcreException;
} }
return $value;
case 'SQL': // preserve as real SQL (TODO: rename to %sql) case 'SQL': // preserve as real SQL (TODO: rename to %sql)
return (string) $value; return (string) $value;