Exemple #1
0
 public function testFilter()
 {
     $filter = new Regexp('/php/');
     $this->assertEquals(false, $filter->apply('foo bar'));
     $this->assertEquals(true, $filter->apply('foo php bar'));
     // test error message
     $this->assertErrorMessage($filter->getErrorMessage());
 }
 /**
  * Extracts tag attributes from a markup string.
  *
  * @param string $markup
  */
 protected function extractAttributes($markup)
 {
     $this->attributes = array();
     $attributeRegexp = new Regexp(Liquid::get('TAG_ATTRIBUTES'));
     $matches = $attributeRegexp->scan($markup);
     foreach ($matches as $match) {
         $this->attributes[$match[0]] = $match[1];
     }
 }
 /**
  * Replace all backreferences in the to pattern with the matched groups.
  * groups of the source.
  * @param string $source The source filename.
  */
 private function replaceReferences($source)
 {
     // FIXME
     // Can't we just use engine->replace() to handle this?  the Preg engine
     // will automatically convert \1 references to $1
     // the expression has already been processed (when ->matches() was run in Main())
     // so no need to pass $source again to the engine.
     $groups = (array) $this->reg->getGroups();
     // replace \1 with value of $groups[1] and return the modified "to" string
     return preg_replace('/\\\\([\\d]+)/e', "\$groups[\$1]", $this->to);
 }
Exemple #4
0
 /**
  * replace the current value according to the regexp rules, or block blacklisted regular expressions
  *
  * @param Model $Model
  * @param unknown_type $array
  */
 public function runRegexp(Model $Model, $type, $value)
 {
     $regexp = new Regexp();
     $allRegexp = $regexp->find('all');
     foreach ($allRegexp as $regexp) {
         if (!empty($regexp['Regexp']['replacement']) && !empty($regexp['Regexp']['regexp']) && ($regexp['Regexp']['type'] === 'ALL' || $regexp['Regexp']['type'] === $type)) {
             $value = preg_replace($regexp['Regexp']['regexp'], $regexp['Regexp']['replacement'], $value);
         }
         if (empty($regexp['Regexp']['replacement']) && preg_match($regexp['Regexp']['regexp'], $value) && ($regexp['Regexp']['type'] === 'ALL' || $regexp['Regexp']['type'] === $type)) {
             return false;
         }
     }
     return $value;
 }
 /**
  * The heart of the matter. This is where the selector gets to decide
  * on the inclusion of a file in a particular fileset.
  *
  * @param PhingFile $basedir base directory the scan is being done from
  * @param string $filename the name of the file to check
  * @param PhingFile $file PhingFile object the selector can use
  *
  * @throws BuildException
  *
  * @return bool whether the file should be selected or not
  */
 public function isSelected(PhingFile $basedir, $filename, PhingFile $file)
 {
     $this->validate();
     if ($file->isDirectory()) {
         return true;
     }
     if ($this->myRegExp === null) {
         $this->myRegExp = new RegularExpression();
         $this->myRegExp->setPattern($this->userProvidedExpression);
         if (!$this->casesensitive) {
             $this->myRegExp->setIgnoreCase(true);
         }
         $this->myExpression = $this->myRegExp->getRegexp($this->getProject());
     }
     $in = null;
     try {
         $in = new BufferedReader(new FileReader($file));
         $teststr = $in->readLine();
         while ($teststr !== null) {
             if ($this->myExpression->matches($teststr)) {
                 return true;
             }
             $teststr = $in->readLine();
         }
         $in->close();
         return false;
     } catch (IOException $ioe) {
         if ($in) {
             $in->close();
         }
         throw new BuildException("Could not read file " . $filename);
     }
 }
 /**
  * Resolves a given path to a full template file path, making sure it's valid
  *
  * @param string $templatePath
  *
  * @throws LiquidException
  * @return string
  */
 public function fullPath($templatePath)
 {
     $nameRegex = Liquid::get('INCLUDE_ALLOW_EXT') ? new Regexp('/^[^.\\/][a-zA-Z0-9_\\.\\/]+$/') : new Regexp('/^[^.\\/][a-zA-Z0-9_\\/]+$/');
     if (!$nameRegex->match($templatePath)) {
         throw new LiquidException("Illegal template name '{$templatePath}'");
     }
     if (strpos($templatePath, '/') !== false) {
         $fullPath = Liquid::get('INCLUDE_ALLOW_EXT') ? $this->root . dirname($templatePath) . '/' . basename($templatePath) : $this->root . dirname($templatePath) . '/' . Liquid::get('INCLUDE_PREFIX') . basename($templatePath) . '.' . Liquid::get('INCLUDE_SUFFIX');
     } else {
         $fullPath = Liquid::get('INCLUDE_ALLOW_EXT') ? $this->root . $templatePath : $this->root . Liquid::get('INCLUDE_PREFIX') . $templatePath . '.' . Liquid::get('INCLUDE_SUFFIX');
     }
     $rootRegex = new Regexp('/' . preg_quote(realpath($this->root), '/') . '/');
     if (!$rootRegex->match(realpath($fullPath))) {
         throw new LiquidException("Illegal template path '" . realpath($fullPath) . "'");
     }
     return $fullPath;
 }
Exemple #7
0
 public function validate($value)
 {
     if ($this->unicode) {
         $this->regexp = '/^([^@\\s]+)@((?:[-\\p{L}\\p{N}]+\\.)+[a-z]{2,})$/iu';
     } else {
         $this->regexp = '/^[a-z0-9' . preg_quote("!#\$%&'*+/=?^_`{|}~.-", '/') . ']{1,}@((?:[-a-z0-9]+\\.)+[a-z]{2,})$/i';
     }
     return parent::validate($value);
 }
Exemple #8
0
 public function validate($value)
 {
     $whitespace = $this->whitespace ? '\\s' : '';
     // unicode characters for numerals can be 2/5 i.e. ⅖
     if ($this->unicode) {
         $this->regexp = '@^[\\p{N}.' . $whitespace . ']+$@u';
     } else {
         $this->regexp = '@^[0-9.' . $whitespace . ']+$@';
     }
     return parent::validate($value);
 }
 /**
  * Constructor
  *
  * @param string $markup
  */
 public function __construct($markup)
 {
     $this->markup = $markup;
     $quotedFragmentRegexp = new Regexp('/\\s*(' . Liquid::get('QUOTED_FRAGMENT') . ')/');
     $filterSeperatorRegexp = new Regexp('/' . Liquid::get('FILTER_SEPARATOR') . '\\s*(.*)/');
     $filterSplitRegexp = new Regexp('/' . Liquid::get('FILTER_SEPARATOR') . '/');
     $filterNameRegexp = new Regexp('/\\s*(\\w+)/');
     $filterArgumentRegexp = new Regexp('/(?:' . Liquid::get('FILTER_ARGUMENT_SEPARATOR') . '|' . Liquid::get('ARGUMENT_SEPARATOR') . ')\\s*(' . Liquid::get('QUOTED_FRAGMENT_FILTER_ARGUMENT') . ')/');
     $quotedFragmentRegexp->match($markup);
     $this->name = isset($quotedFragmentRegexp->matches[1]) ? $quotedFragmentRegexp->matches[1] : null;
     if ($filterSeperatorRegexp->match($markup)) {
         $filters = $filterSplitRegexp->split($filterSeperatorRegexp->matches[1]);
         foreach ($filters as $filter) {
             $filterNameRegexp->match($filter);
             $filtername = $filterNameRegexp->matches[1];
             $filterArgumentRegexp->matchAll($filter);
             $matches = Liquid::arrayFlatten($filterArgumentRegexp->matches[1]);
             $this->filters[] = array($filtername, $matches);
         }
     } else {
         $this->filters = array();
     }
 }
Exemple #10
0
 /**
  * @return string
  *
  * @throws BuildException
  */
 protected function doSelect()
 {
     $this->reg->setPattern($this->pattern);
     $this->reg->setModifiers($this->modifiers);
     $this->reg->setIgnoreCase(!$this->caseSensitive);
     $output = $this->defaultValue;
     try {
         if ($this->reg->matches($this->subject)) {
             $output = $this->reg->getGroup((int) ltrim($this->match, '$'));
         }
     } catch (Exception $e) {
         throw new BuildException($e);
     }
     return $output;
 }
Exemple #11
0
 /**
  * @param  string
  * @return void
  */
 public function parse($text)
 {
     $this->texy->invokeHandlers('beforeBlockParse', [$this, &$text]);
     // parser initialization
     $this->text = $text;
     $this->offset = 0;
     // parse loop
     $matches = [];
     $priority = 0;
     foreach ($this->patterns as $name => $pattern) {
         $ms = Regexp::match($text, $pattern['pattern'], Regexp::OFFSET_CAPTURE | Regexp::ALL);
         foreach ((array) $ms as $m) {
             $offset = $m[0][1];
             foreach ($m as $k => $v) {
                 $m[$k] = $v[0];
             }
             $matches[] = [$offset, $name, $m, $priority];
         }
         $priority++;
     }
     unset($name, $pattern, $ms, $m, $k, $v);
     usort($matches, function ($a, $b) {
         if ($a[0] === $b[0]) {
             return $a[3] < $b[3] ? -1 : 1;
         }
         if ($a[0] < $b[0]) {
             return -1;
         }
         return 1;
     });
     $matches[] = [strlen($text), NULL, NULL];
     // terminal cap
     // process loop
     $el = $this->element;
     $cursor = 0;
     do {
         do {
             list($mOffset, $mName, $mMatches) = $matches[$cursor];
             $cursor++;
             if ($mName === NULL || $mOffset >= $this->offset) {
                 break;
             }
         } while (1);
         // between-matches content
         if ($mOffset > $this->offset) {
             $s = trim(substr($text, $this->offset, $mOffset - $this->offset));
             if ($s !== '') {
                 $this->texy->paragraphModule->process($this, $s, $el);
             }
         }
         if ($mName === NULL) {
             break;
             // finito
         }
         $this->offset = $mOffset + strlen($mMatches[0]) + 1;
         // 1 = \n
         $res = call_user_func_array($this->patterns[$mName]['handler'], [$this, $mMatches, $mName]);
         if ($res === FALSE || $this->offset <= $mOffset) {
             // module rejects text
             // asi by se nemelo stat, rozdeli generic block
             $this->offset = $mOffset;
             // turn offset back
             continue;
         } elseif ($res instanceof HtmlElement) {
             $el->insert(NULL, $res);
         } elseif (is_string($res)) {
             $el->insert(NULL, $res);
         }
     } while (1);
 }
Exemple #12
0
 public function setup(Model $model, $config = null)
 {
     $regexp = new Regexp();
     $this->__allRegexp = $regexp->find('all');
 }
Exemple #13
0
 /**
  * {@inheritdoc}
  */
 protected function isValidComparison($value, $parameters)
 {
     $parameters = ['/^([a-z0-9])+$/i'];
     return parent::isValidComparison($value, $parameters);
 }
 public function testNoMatch()
 {
     $regexp = new Regexp('foo');
     $this->assertFalse($regexp->isMatch('bar'));
 }
Exemple #15
0
 /**
  * Converts internal string representation to final HTML code in UTF-8.
  * @return string
  */
 public final function stringToText($s)
 {
     $save = $this->htmlOutputModule->lineWrap;
     $this->htmlOutputModule->lineWrap = FALSE;
     $s = $this->stringToHtml($s);
     $this->htmlOutputModule->lineWrap = $save;
     // remove tags
     $s = Regexp::replace($s, '#<(script|style)(.*)</\\1>#Uis', '');
     $s = strip_tags($s);
     $s = Regexp::replace($s, '#\\n\\s*\\n\\s*\\n[\\n\\s]*\\n#', "\n\n");
     // entities -> chars
     $s = html_entity_decode($s, ENT_QUOTES, 'UTF-8');
     // convert nbsp to normal space and remove shy
     $s = strtr($s, ["­" => '', " " => ' ']);
     return $s;
 }
 /**
  * Gets the matched group from the Regexp engine.
  *
  * @param array $matches Matched elements.
  *
  * @return string
  */
 private function replaceReferencesCallback($matches)
 {
     return (string) $this->reg->getGroup($matches[1]);
 }
 /**
  * Create a variable for the given token
  *
  * @param string $token
  *
  * @throws \Liquid\LiquidException
  * @return Variable
  */
 private function createVariable($token)
 {
     $variableRegexp = new Regexp('/^' . Liquid::get('VARIABLE_START') . '(.*)' . Liquid::get('VARIABLE_END') . '$/');
     if ($variableRegexp->match($token)) {
         return new Variable($variableRegexp->matches[1]);
     }
     throw new LiquidException("Variable {$token} was not properly terminated");
 }
 public function testQuotedWordsInTheMiddle()
 {
     $this->assertEquals(array('arg1', 'arg2', '"arg 3"', 'arg4'), $this->regexp->scan('arg1 arg2 "arg 3" arg4   '));
 }