Skip to content

Commit

Permalink
implemented PCRE error checking and TexyPcreException
Browse files Browse the repository at this point in the history
  • Loading branch information
dg committed Aug 3, 2011
1 parent 126ef4c commit 09b3d0f
Show file tree
Hide file tree
Showing 7 changed files with 51 additions and 0 deletions.
27 changes: 27 additions & 0 deletions Texy/Texy.php
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,27 @@



/**
* Checking preg_last_error()
*/
class TexyPcreException extends Exception
{
public function __construct($message = '%msg.')
{
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(str_replace('%msg', isset($messages[$code]) ? $messages[$code] : 'Unknown error', $message), $code);
}
}



/**
* For Texy 1 backward compatibility.
*/
Expand Down Expand Up @@ -478,6 +499,9 @@ public function process($text, $singleLine = FALSE)
$this->tabWidth = max(1, (int) $this->tabWidth);
while (strpos($text, "\t") !== FALSE) {
$text = preg_replace_callback('#^([^\t\n]*+)\t#mU', array($this, 'tabCb'), $text);
if (preg_last_error()) {
throw new TexyPcreException;
}
}

// user before handler
Expand Down Expand Up @@ -626,6 +650,9 @@ final public function stringToText($s)

// remove tags
$s = preg_replace('#<(script|style)(.*)</\\1>#Uis', '', $s);
if (preg_last_error()) {
throw new TexyPcreException;
}
$s = strip_tags($s);
$s = preg_replace('#\n\s*\n\s*\n[\n\s]*\n#', "\n\n", $s);

Expand Down
9 changes: 9 additions & 0 deletions Texy/libs/TexyParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,9 @@ public function next($pattern, &$matches)
if ($ok) {
$this->offset += strlen($matches[0][0]) + 1; // 1 = "\n"
foreach ($matches as $key => $value) $matches[$key] = $value[0];

} elseif (preg_last_error()) {
throw new TexyPcreException("%msg; pattern: {$pattern}Am");
}
return $ok;
}
Expand Down Expand Up @@ -155,6 +158,9 @@ public function parse($text)
$ms,
PREG_OFFSET_CAPTURE | PREG_SET_ORDER
);
if (preg_last_error()) {
throw new TexyPcreException("%msg; pattern: $pattern[pattern]");
}
foreach ($ms as $m) {
$offset = $m[0][1];
Expand Down Expand Up @@ -295,6 +301,9 @@ public function parse($text)
$arrOffset[$name] = $m[0][1];
foreach ($m as $keyx => $value) $m[$keyx] = $value[0];

} elseif (preg_last_error()) {
throw new TexyPcreException("%msg; pattern: {$pl[$name]['pattern']}");

} else {
// try next time?
if (!$pl[$name]['again'] || !preg_match($pl[$name]['again'], $text, $foo, NULL, $offset + $delta)) {
Expand Down
3 changes: 3 additions & 0 deletions Texy/modules/TexyBlockModule.php
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,9 @@ public function beforeBlockParse($parser, & $text)
"\$1\$2\n\\--",
$text
);
if (preg_last_error()) {
throw new TexyPcreException;
}
}


Expand Down
3 changes: 3 additions & 0 deletions Texy/modules/TexyHtmlOutputModule.php
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,9 @@ public function postProcess($texy, & $s)
array($this, 'cb'),
$s . '</end/>'
);
if (preg_last_error()) {
throw new TexyPcreException;
}

// empty out stack
foreach ($this->tagStack as $item) $s .= $item['close'];
Expand Down
3 changes: 3 additions & 0 deletions Texy/modules/TexyImageModule.php
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,9 @@ public function beforeParse($texy, & $text)
array($this, 'patternReferenceDef'),
$text
);
if (preg_last_error()) {
throw new TexyPcreException;
}
}
}

Expand Down
3 changes: 3 additions & 0 deletions Texy/modules/TexyLinkModule.php
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,9 @@ public function beforeParse($texy, & $text)
array($this, 'patternReferenceDef'),
$text
);
if (preg_last_error()) {
throw new TexyPcreException;
}
}
}

Expand Down
3 changes: 3 additions & 0 deletions Texy/modules/TexyParagraphModule.php
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,9 @@ public function process($parser, $content, $el)
if ($s === '') continue;
$mod = new TexyModifier;
$mod->setProperties($mMod);

} elseif (preg_last_error()) {
throw new TexyPcreException;
}

$res = $tx->invokeAroundHandlers('paragraph', $parser, array($s, $mod));
Expand Down

0 comments on commit 09b3d0f

Please sign in to comment.