├── LICENSE ├── composer.json ├── phpunit.xml.dist └── src ├── ErrorHandler.php ├── Factory.php ├── Interfaces ├── ErrorHandlerInterface.php ├── RegexRuntimeInterface.php └── SandboxInterface.php ├── RegexException.php ├── RegexGuard.php └── Sandbox.php /LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2013 Márcio Almada 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy of 6 | this software and associated documentation files (the "Software"), to deal in 7 | the Software without restriction, including without limitation the rights to 8 | use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of 9 | the Software, and to permit persons to whom the Software is furnished to do so, 10 | subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS 17 | FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR 18 | COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER 19 | IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 20 | CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 21 | -------------------------------------------------------------------------------- /composer.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "regex-guard/regex-guard", 3 | "description": "A wrapper that allows you to validate regular expressions and handle normally uncatchable PCRE compilation warnings", 4 | "keywords": ["regex", "regexp", "PCRE", "preg", "support", "utils"], 5 | "license": "MIT", 6 | "authors": [ 7 | { 8 | "name": "Márcio Almada", 9 | "email": "marcio3w@gmail.com", 10 | "homepage": "https://github.com/marcioAlmada/" 11 | } 12 | ], 13 | "autoload": { 14 | "psr-4": { 15 | "RegexGuard\\": "src/" 16 | } 17 | }, 18 | "require-dev": { 19 | "mockery/mockery": "dev-master" 20 | } 21 | } 22 | -------------------------------------------------------------------------------- /phpunit.xml.dist: -------------------------------------------------------------------------------- 1 | 2 | 3 | 13 | 14 | 15 | 16 | test/ 17 | 18 | 19 | 20 | 21 | 22 | src/ 23 | 24 | 25 | 26 | 27 | 31 | 35 | 42 | 46 | 50 | 51 | 52 | -------------------------------------------------------------------------------- /src/ErrorHandler.php: -------------------------------------------------------------------------------- 1 | enabled) { 14 | $this->enabled = true; 15 | 16 | return set_error_handler(function ($errno, $errstr) use ($throwOnError) { 17 | if ($throwOnError) { 18 | $this->disable(); 19 | throw new RegexException('Invalid regex given: ' . str_replace('preg_', '', $errstr), 1); 20 | } 21 | 22 | return true; 23 | }, $severity); 24 | } 25 | 26 | return false; 27 | } 28 | 29 | public function disable() 30 | { 31 | if ($this->enabled) { 32 | $this->enabled = false; 33 | 34 | return restore_error_handler(); // cleans temporary error handler 35 | } 36 | 37 | return false; 38 | } 39 | } 40 | -------------------------------------------------------------------------------- /src/Factory.php: -------------------------------------------------------------------------------- 1 | sandbox = $sandbox; 22 | } 23 | 24 | /** 25 | * Wether exception should be thrown when invalid regex is given 26 | * 27 | * @param boolean $option 28 | * @return \RegexGuard\RegexHelper self 29 | */ 30 | public function throwOnError($option = true) 31 | { 32 | $this->throw = (bool) $option; 33 | 34 | return $this; 35 | } 36 | 37 | /** 38 | * Validates a given pcre string 39 | * 40 | * @param string $pattern pcre string 41 | * @return boolean true when pcre string is valid, false otherwise 42 | */ 43 | public function isRegexValid($pattern) 44 | { 45 | return false !== $this->sandbox->run('preg_match', [$pattern, ''], false); 46 | } 47 | 48 | /** 49 | * Validates a given perl compatible regular expression or throw exception 50 | * 51 | * @param string $pattern pcre string 52 | * @throws \InvalidArgumentException If invalid regexp is given 53 | */ 54 | public function validateRegexOrFail($pattern) 55 | { 56 | $this->sandbox->run('preg_match', [$pattern, '']); 57 | } 58 | 59 | /** 60 | * Same as \preg_filter but throws exception when an invalid pcre string is given 61 | * 62 | * @link http://php.net/manual/en/function.preg-filter.php 63 | */ 64 | public function filter($pattern, $subject, $limit = -1, $flags = 0) 65 | { 66 | return $this->sandbox->run('preg_filter', func_get_args(), $this->throw); 67 | } 68 | 69 | /** 70 | * Same as \preg_grep but throws exception when an invalid pcre string is given 71 | * 72 | * @link http://php.net/manual/en/function.preg-grep.php 73 | */ 74 | public function grep($pattern, $input, $flags = 0) 75 | { 76 | return $this->sandbox->run('preg_grep', func_get_args(), $this->throw); 77 | } 78 | 79 | /** 80 | * Same as \preg_match but throws exception when an invalid pcre string is given 81 | * 82 | * @link http://php.net/manual/en/function.preg-match.php 83 | */ 84 | public function match($pattern, $subject, &$matches = null, $flags = 0, $offset = 0) 85 | { 86 | return $this->sandbox->run('preg_match', [$pattern, $subject, &$matches, $flags, $offset], $this->throw); 87 | } 88 | 89 | /** 90 | * Same as \preg_match_all but throws exception when an invalid pcre string is given 91 | * 92 | * @link http://php.net/manual/en/function.preg-match-all.php 93 | */ 94 | public function matchAll($pattern, $subject, &$matches = null, $flags = PREG_PATTERN_ORDER, $offset = 0) 95 | { 96 | return $this->sandbox->run('preg_match_all', [$pattern, $subject, &$matches, $flags, $offset], $this->throw); 97 | } 98 | 99 | /** 100 | * Same as \preg_replace but throws exception when an invalid pcre string is given 101 | * 102 | * @link http://php.net/manual/en/function.preg-replace.php 103 | */ 104 | public function replace($pattern, $replacement , $subject, $limit = -1, &$count = null) 105 | { 106 | return $this->sandbox->run('preg_replace', func_get_args(), $this->throw); 107 | } 108 | 109 | /** 110 | * Same as \preg_split but throws exception when an invalid pcre string is given 111 | * 112 | * @link http://php.net/manual/en/function.preg-split.php 113 | */ 114 | public function split($pattern, $subject, $limit = -1, $flags = 0) 115 | { 116 | return $this->sandbox->run('preg_split', func_get_args(), $this->throw); 117 | } 118 | 119 | } 120 | -------------------------------------------------------------------------------- /src/Sandbox.php: -------------------------------------------------------------------------------- 1 | errorHandler = $errorHandler; 15 | } 16 | 17 | public function run(callable $closure, array $args = [], $throwOnError = true, $severity = E_WARNING) 18 | { 19 | $this->errorHandler->enable($throwOnError, $severity); 20 | $result = call_user_func_array($closure, $args); 21 | $this->errorHandler->disable(); 22 | 23 | return $result; 24 | } 25 | } 26 | --------------------------------------------------------------------------------