@@ -16,335 +16,335 @@ discard block |
||
| 16 | 16 | */ |
| 17 | 17 | abstract class Minify |
| 18 | 18 | { |
| 19 | - /** |
|
| 20 | - * The data to be minified. |
|
| 21 | - * |
|
| 22 | - * @var string[] |
|
| 23 | - */ |
|
| 24 | - protected $data = array(); |
|
| 25 | - |
|
| 26 | - /** |
|
| 27 | - * Array of patterns to match. |
|
| 28 | - * |
|
| 29 | - * @var string[] |
|
| 30 | - */ |
|
| 31 | - protected $patterns = array(); |
|
| 32 | - |
|
| 33 | - /** |
|
| 34 | - * This array will hold content of strings and regular expressions that have |
|
| 35 | - * been extracted from the JS source code, so we can reliably match "code", |
|
| 36 | - * without having to worry about potential "code-like" characters inside. |
|
| 37 | - * |
|
| 38 | - * @var string[] |
|
| 39 | - */ |
|
| 40 | - public $extracted = array(); |
|
| 41 | - |
|
| 42 | - /** |
|
| 43 | - * Init the minify class - optionally, code may be passed along already. |
|
| 44 | - */ |
|
| 45 | - public function __construct(/* $data = null, ... */) |
|
| 46 | - { |
|
| 47 | - // it's possible to add the source through the constructor as well ;) |
|
| 48 | - if (func_num_args()) { |
|
| 49 | - call_user_func_array(array($this, 'add'), func_get_args()); |
|
| 50 | - } |
|
| 51 | - } |
|
| 52 | - |
|
| 53 | - /** |
|
| 54 | - * Add a file or straight-up code to be minified. |
|
| 55 | - * |
|
| 56 | - * @param string|string[] $data |
|
| 57 | - */ |
|
| 58 | - public function add($data /* $data = null, ... */) |
|
| 59 | - { |
|
| 60 | - // bogus "usage" of parameter $data: scrutinizer warns this variable is |
|
| 61 | - // not used (we're using func_get_args instead to support overloading), |
|
| 62 | - // but it still needs to be defined because it makes no sense to have |
|
| 63 | - // this function without argument :) |
|
| 64 | - $args = array($data) + func_get_args(); |
|
| 65 | - |
|
| 66 | - // this method can be overloaded |
|
| 67 | - foreach ($args as $data) { |
|
| 68 | - if (is_array($data)) { |
|
| 69 | - call_user_func_array(array($this, 'add'), $data); |
|
| 70 | - continue; |
|
| 71 | - } |
|
| 72 | - |
|
| 73 | - // redefine var |
|
| 74 | - $data = (string) $data; |
|
| 75 | - |
|
| 76 | - // load data |
|
| 77 | - $value = $this->load($data); |
|
| 78 | - $key = ($data != $value) ? $data : count($this->data); |
|
| 79 | - |
|
| 80 | - // replace CR linefeeds etc. |
|
| 81 | - // @see https://github.com/matthiasmullie/minify/pull/139 |
|
| 82 | - $value = str_replace(array("\r\n", "\r"), "\n", $value); |
|
| 83 | - |
|
| 84 | - // store data |
|
| 85 | - $this->data[$key] = $value; |
|
| 86 | - } |
|
| 87 | - } |
|
| 88 | - |
|
| 89 | - /** |
|
| 90 | - * Minify the data & (optionally) saves it to a file. |
|
| 91 | - * |
|
| 92 | - * @param string[optional] $path Path to write the data to |
|
| 93 | - * |
|
| 94 | - * @return string The minified data |
|
| 95 | - */ |
|
| 96 | - public function minify($path = null) |
|
| 97 | - { |
|
| 98 | - $content = $this->execute($path); |
|
| 99 | - |
|
| 100 | - // save to path |
|
| 101 | - if ($path !== null) { |
|
| 102 | - $this->save($content, $path); |
|
| 103 | - } |
|
| 104 | - |
|
| 105 | - return $content; |
|
| 106 | - } |
|
| 107 | - |
|
| 108 | - /** |
|
| 109 | - * Minify & gzip the data & (optionally) saves it to a file. |
|
| 110 | - * |
|
| 111 | - * @param string[optional] $path Path to write the data to |
|
| 112 | - * @param int[optional] $level Compression level, from 0 to 9 |
|
| 113 | - * |
|
| 114 | - * @return string The minified & gzipped data |
|
| 115 | - */ |
|
| 116 | - public function gzip($path = null, $level = 9) |
|
| 117 | - { |
|
| 118 | - $content = $this->execute($path); |
|
| 119 | - $content = gzencode($content, $level, FORCE_GZIP); |
|
| 120 | - |
|
| 121 | - // save to path |
|
| 122 | - if ($path !== null) { |
|
| 123 | - $this->save($content, $path); |
|
| 124 | - } |
|
| 125 | - |
|
| 126 | - return $content; |
|
| 127 | - } |
|
| 128 | - |
|
| 129 | - /** |
|
| 130 | - * Minify the data & write it to a CacheItemInterface object. |
|
| 131 | - * |
|
| 132 | - * @param CacheItemInterface $item Cache item to write the data to |
|
| 133 | - * |
|
| 134 | - * @return CacheItemInterface Cache item with the minifier data |
|
| 135 | - */ |
|
| 136 | - public function cache(CacheItemInterface $item) |
|
| 137 | - { |
|
| 138 | - $content = $this->execute(); |
|
| 139 | - $item->set($content); |
|
| 140 | - |
|
| 141 | - return $item; |
|
| 142 | - } |
|
| 143 | - |
|
| 144 | - /** |
|
| 145 | - * Minify the data. |
|
| 146 | - * |
|
| 147 | - * @param string[optional] $path Path to write the data to |
|
| 148 | - * |
|
| 149 | - * @return string The minified data |
|
| 150 | - */ |
|
| 151 | - abstract public function execute($path = null); |
|
| 152 | - |
|
| 153 | - /** |
|
| 154 | - * Load data. |
|
| 155 | - * |
|
| 156 | - * @param string $data Either a path to a file or the content itself |
|
| 157 | - * |
|
| 158 | - * @return string |
|
| 159 | - */ |
|
| 160 | - protected function load($data) |
|
| 161 | - { |
|
| 162 | - // check if the data is a file |
|
| 163 | - if ($this->canImportFile($data)) { |
|
| 164 | - $data = file_get_contents($data); |
|
| 165 | - |
|
| 166 | - // strip BOM, if any |
|
| 167 | - if (substr($data, 0, 3) == "\xef\xbb\xbf") { |
|
| 168 | - $data = substr($data, 3); |
|
| 169 | - } |
|
| 170 | - } |
|
| 171 | - |
|
| 172 | - return $data; |
|
| 173 | - } |
|
| 174 | - |
|
| 175 | - /** |
|
| 176 | - * Save to file. |
|
| 177 | - * |
|
| 178 | - * @param string $content The minified data |
|
| 179 | - * @param string $path The path to save the minified data to |
|
| 180 | - * |
|
| 181 | - * @throws IOException |
|
| 182 | - */ |
|
| 183 | - protected function save($content, $path) |
|
| 184 | - { |
|
| 185 | - $handler = $this->openFileForWriting($path); |
|
| 186 | - |
|
| 187 | - $this->writeToFile($handler, $content); |
|
| 188 | - |
|
| 189 | - @fclose($handler); |
|
| 190 | - } |
|
| 191 | - |
|
| 192 | - /** |
|
| 193 | - * Register a pattern to execute against the source content. |
|
| 194 | - * |
|
| 195 | - * @param string $pattern PCRE pattern |
|
| 196 | - * @param string|callable $replacement Replacement value for matched pattern |
|
| 197 | - */ |
|
| 198 | - protected function registerPattern($pattern, $replacement = '') |
|
| 199 | - { |
|
| 200 | - // study the pattern, we'll execute it more than once |
|
| 201 | - $pattern .= 'S'; |
|
| 202 | - |
|
| 203 | - $this->patterns[] = array($pattern, $replacement); |
|
| 204 | - } |
|
| 205 | - |
|
| 206 | - /** |
|
| 207 | - * We can't "just" run some regular expressions against JavaScript: it's a |
|
| 208 | - * complex language. E.g. having an occurrence of // xyz would be a comment, |
|
| 209 | - * unless it's used within a string. Of you could have something that looks |
|
| 210 | - * like a 'string', but inside a comment. |
|
| 211 | - * The only way to accurately replace these pieces is to traverse the JS one |
|
| 212 | - * character at a time and try to find whatever starts first. |
|
| 213 | - * |
|
| 214 | - * @param string $content The content to replace patterns in |
|
| 215 | - * |
|
| 216 | - * @return string The (manipulated) content |
|
| 217 | - */ |
|
| 218 | - protected function replace($content) |
|
| 219 | - { |
|
| 220 | - $processed = ''; |
|
| 221 | - $positions = array_fill(0, count($this->patterns), -1); |
|
| 222 | - $matches = array(); |
|
| 223 | - |
|
| 224 | - while ($content) { |
|
| 225 | - // find first match for all patterns |
|
| 226 | - foreach ($this->patterns as $i => $pattern) { |
|
| 227 | - list($pattern, $replacement) = $pattern; |
|
| 228 | - |
|
| 229 | - // no need to re-run matches that are still in the part of the |
|
| 230 | - // content that hasn't been processed |
|
| 231 | - if ($positions[$i] >= 0) { |
|
| 232 | - continue; |
|
| 233 | - } |
|
| 234 | - |
|
| 235 | - $match = null; |
|
| 236 | - if (preg_match($pattern, $content, $match)) { |
|
| 237 | - $matches[$i] = $match; |
|
| 238 | - |
|
| 239 | - // we'll store the match position as well; that way, we |
|
| 240 | - // don't have to redo all preg_matches after changing only |
|
| 241 | - // the first (we'll still know where those others are) |
|
| 242 | - $positions[$i] = strpos($content, $match[0]); |
|
| 243 | - } else { |
|
| 244 | - // if the pattern couldn't be matched, there's no point in |
|
| 245 | - // executing it again in later runs on this same content; |
|
| 246 | - // ignore this one until we reach end of content |
|
| 247 | - unset($matches[$i]); |
|
| 248 | - $positions[$i] = strlen($content); |
|
| 249 | - } |
|
| 250 | - } |
|
| 251 | - |
|
| 252 | - // no more matches to find: everything's been processed, break out |
|
| 253 | - if (!$matches) { |
|
| 254 | - $processed .= $content; |
|
| 255 | - break; |
|
| 256 | - } |
|
| 257 | - |
|
| 258 | - // see which of the patterns actually found the first thing (we'll |
|
| 259 | - // only want to execute that one, since we're unsure if what the |
|
| 260 | - // other found was not inside what the first found) |
|
| 261 | - $discardLength = min($positions); |
|
| 262 | - $firstPattern = array_search($discardLength, $positions); |
|
| 263 | - $match = $matches[$firstPattern][0]; |
|
| 264 | - |
|
| 265 | - // execute the pattern that matches earliest in the content string |
|
| 266 | - list($pattern, $replacement) = $this->patterns[$firstPattern]; |
|
| 267 | - $replacement = $this->replacePattern($pattern, $replacement, $content); |
|
| 268 | - |
|
| 269 | - // figure out which part of the string was unmatched; that's the |
|
| 270 | - // part we'll execute the patterns on again next |
|
| 271 | - $content = substr($content, $discardLength); |
|
| 272 | - $unmatched = (string) substr($content, strpos($content, $match) + strlen($match)); |
|
| 273 | - |
|
| 274 | - // move the replaced part to $processed and prepare $content to |
|
| 275 | - // again match batch of patterns against |
|
| 276 | - $processed .= substr($replacement, 0, strlen($replacement) - strlen($unmatched)); |
|
| 277 | - $content = $unmatched; |
|
| 278 | - |
|
| 279 | - // first match has been replaced & that content is to be left alone, |
|
| 280 | - // the next matches will start after this replacement, so we should |
|
| 281 | - // fix their offsets |
|
| 282 | - foreach ($positions as $i => $position) { |
|
| 283 | - $positions[$i] -= $discardLength + strlen($match); |
|
| 284 | - } |
|
| 285 | - } |
|
| 286 | - |
|
| 287 | - return $processed; |
|
| 288 | - } |
|
| 289 | - |
|
| 290 | - /** |
|
| 291 | - * This is where a pattern is matched against $content and the matches |
|
| 292 | - * are replaced by their respective value. |
|
| 293 | - * This function will be called plenty of times, where $content will always |
|
| 294 | - * move up 1 character. |
|
| 295 | - * |
|
| 296 | - * @param string $pattern Pattern to match |
|
| 297 | - * @param string|callable $replacement Replacement value |
|
| 298 | - * @param string $content Content to match pattern against |
|
| 299 | - * |
|
| 300 | - * @return string |
|
| 301 | - */ |
|
| 302 | - protected function replacePattern($pattern, $replacement, $content) |
|
| 303 | - { |
|
| 304 | - if (is_callable($replacement)) { |
|
| 305 | - return preg_replace_callback($pattern, $replacement, $content, 1, $count); |
|
| 306 | - } else { |
|
| 307 | - return preg_replace($pattern, $replacement, $content, 1, $count); |
|
| 308 | - } |
|
| 309 | - } |
|
| 310 | - |
|
| 311 | - /** |
|
| 312 | - * Strings are a pattern we need to match, in order to ignore potential |
|
| 313 | - * code-like content inside them, but we just want all of the string |
|
| 314 | - * content to remain untouched. |
|
| 315 | - * |
|
| 316 | - * This method will replace all string content with simple STRING# |
|
| 317 | - * placeholder text, so we've rid all strings from characters that may be |
|
| 318 | - * misinterpreted. Original string content will be saved in $this->extracted |
|
| 319 | - * and after doing all other minifying, we can restore the original content |
|
| 320 | - * via restoreStrings(). |
|
| 321 | - * |
|
| 322 | - * @param string[optional] $chars |
|
| 323 | - */ |
|
| 324 | - protected function extractStrings($chars = '\'"') |
|
| 325 | - { |
|
| 326 | - // PHP only supports $this inside anonymous functions since 5.4 |
|
| 327 | - $minifier = $this; |
|
| 328 | - $callback = function ($match) use ($minifier) { |
|
| 329 | - // check the second index here, because the first always contains a quote |
|
| 330 | - if ($match[2] === '') { |
|
| 331 | - /* |
|
| 19 | + /** |
|
| 20 | + * The data to be minified. |
|
| 21 | + * |
|
| 22 | + * @var string[] |
|
| 23 | + */ |
|
| 24 | + protected $data = array(); |
|
| 25 | + |
|
| 26 | + /** |
|
| 27 | + * Array of patterns to match. |
|
| 28 | + * |
|
| 29 | + * @var string[] |
|
| 30 | + */ |
|
| 31 | + protected $patterns = array(); |
|
| 32 | + |
|
| 33 | + /** |
|
| 34 | + * This array will hold content of strings and regular expressions that have |
|
| 35 | + * been extracted from the JS source code, so we can reliably match "code", |
|
| 36 | + * without having to worry about potential "code-like" characters inside. |
|
| 37 | + * |
|
| 38 | + * @var string[] |
|
| 39 | + */ |
|
| 40 | + public $extracted = array(); |
|
| 41 | + |
|
| 42 | + /** |
|
| 43 | + * Init the minify class - optionally, code may be passed along already. |
|
| 44 | + */ |
|
| 45 | + public function __construct(/* $data = null, ... */) |
|
| 46 | + { |
|
| 47 | + // it's possible to add the source through the constructor as well ;) |
|
| 48 | + if (func_num_args()) { |
|
| 49 | + call_user_func_array(array($this, 'add'), func_get_args()); |
|
| 50 | + } |
|
| 51 | + } |
|
| 52 | + |
|
| 53 | + /** |
|
| 54 | + * Add a file or straight-up code to be minified. |
|
| 55 | + * |
|
| 56 | + * @param string|string[] $data |
|
| 57 | + */ |
|
| 58 | + public function add($data /* $data = null, ... */) |
|
| 59 | + { |
|
| 60 | + // bogus "usage" of parameter $data: scrutinizer warns this variable is |
|
| 61 | + // not used (we're using func_get_args instead to support overloading), |
|
| 62 | + // but it still needs to be defined because it makes no sense to have |
|
| 63 | + // this function without argument :) |
|
| 64 | + $args = array($data) + func_get_args(); |
|
| 65 | + |
|
| 66 | + // this method can be overloaded |
|
| 67 | + foreach ($args as $data) { |
|
| 68 | + if (is_array($data)) { |
|
| 69 | + call_user_func_array(array($this, 'add'), $data); |
|
| 70 | + continue; |
|
| 71 | + } |
|
| 72 | + |
|
| 73 | + // redefine var |
|
| 74 | + $data = (string) $data; |
|
| 75 | + |
|
| 76 | + // load data |
|
| 77 | + $value = $this->load($data); |
|
| 78 | + $key = ($data != $value) ? $data : count($this->data); |
|
| 79 | + |
|
| 80 | + // replace CR linefeeds etc. |
|
| 81 | + // @see https://github.com/matthiasmullie/minify/pull/139 |
|
| 82 | + $value = str_replace(array("\r\n", "\r"), "\n", $value); |
|
| 83 | + |
|
| 84 | + // store data |
|
| 85 | + $this->data[$key] = $value; |
|
| 86 | + } |
|
| 87 | + } |
|
| 88 | + |
|
| 89 | + /** |
|
| 90 | + * Minify the data & (optionally) saves it to a file. |
|
| 91 | + * |
|
| 92 | + * @param string[optional] $path Path to write the data to |
|
| 93 | + * |
|
| 94 | + * @return string The minified data |
|
| 95 | + */ |
|
| 96 | + public function minify($path = null) |
|
| 97 | + { |
|
| 98 | + $content = $this->execute($path); |
|
| 99 | + |
|
| 100 | + // save to path |
|
| 101 | + if ($path !== null) { |
|
| 102 | + $this->save($content, $path); |
|
| 103 | + } |
|
| 104 | + |
|
| 105 | + return $content; |
|
| 106 | + } |
|
| 107 | + |
|
| 108 | + /** |
|
| 109 | + * Minify & gzip the data & (optionally) saves it to a file. |
|
| 110 | + * |
|
| 111 | + * @param string[optional] $path Path to write the data to |
|
| 112 | + * @param int[optional] $level Compression level, from 0 to 9 |
|
| 113 | + * |
|
| 114 | + * @return string The minified & gzipped data |
|
| 115 | + */ |
|
| 116 | + public function gzip($path = null, $level = 9) |
|
| 117 | + { |
|
| 118 | + $content = $this->execute($path); |
|
| 119 | + $content = gzencode($content, $level, FORCE_GZIP); |
|
| 120 | + |
|
| 121 | + // save to path |
|
| 122 | + if ($path !== null) { |
|
| 123 | + $this->save($content, $path); |
|
| 124 | + } |
|
| 125 | + |
|
| 126 | + return $content; |
|
| 127 | + } |
|
| 128 | + |
|
| 129 | + /** |
|
| 130 | + * Minify the data & write it to a CacheItemInterface object. |
|
| 131 | + * |
|
| 132 | + * @param CacheItemInterface $item Cache item to write the data to |
|
| 133 | + * |
|
| 134 | + * @return CacheItemInterface Cache item with the minifier data |
|
| 135 | + */ |
|
| 136 | + public function cache(CacheItemInterface $item) |
|
| 137 | + { |
|
| 138 | + $content = $this->execute(); |
|
| 139 | + $item->set($content); |
|
| 140 | + |
|
| 141 | + return $item; |
|
| 142 | + } |
|
| 143 | + |
|
| 144 | + /** |
|
| 145 | + * Minify the data. |
|
| 146 | + * |
|
| 147 | + * @param string[optional] $path Path to write the data to |
|
| 148 | + * |
|
| 149 | + * @return string The minified data |
|
| 150 | + */ |
|
| 151 | + abstract public function execute($path = null); |
|
| 152 | + |
|
| 153 | + /** |
|
| 154 | + * Load data. |
|
| 155 | + * |
|
| 156 | + * @param string $data Either a path to a file or the content itself |
|
| 157 | + * |
|
| 158 | + * @return string |
|
| 159 | + */ |
|
| 160 | + protected function load($data) |
|
| 161 | + { |
|
| 162 | + // check if the data is a file |
|
| 163 | + if ($this->canImportFile($data)) { |
|
| 164 | + $data = file_get_contents($data); |
|
| 165 | + |
|
| 166 | + // strip BOM, if any |
|
| 167 | + if (substr($data, 0, 3) == "\xef\xbb\xbf") { |
|
| 168 | + $data = substr($data, 3); |
|
| 169 | + } |
|
| 170 | + } |
|
| 171 | + |
|
| 172 | + return $data; |
|
| 173 | + } |
|
| 174 | + |
|
| 175 | + /** |
|
| 176 | + * Save to file. |
|
| 177 | + * |
|
| 178 | + * @param string $content The minified data |
|
| 179 | + * @param string $path The path to save the minified data to |
|
| 180 | + * |
|
| 181 | + * @throws IOException |
|
| 182 | + */ |
|
| 183 | + protected function save($content, $path) |
|
| 184 | + { |
|
| 185 | + $handler = $this->openFileForWriting($path); |
|
| 186 | + |
|
| 187 | + $this->writeToFile($handler, $content); |
|
| 188 | + |
|
| 189 | + @fclose($handler); |
|
| 190 | + } |
|
| 191 | + |
|
| 192 | + /** |
|
| 193 | + * Register a pattern to execute against the source content. |
|
| 194 | + * |
|
| 195 | + * @param string $pattern PCRE pattern |
|
| 196 | + * @param string|callable $replacement Replacement value for matched pattern |
|
| 197 | + */ |
|
| 198 | + protected function registerPattern($pattern, $replacement = '') |
|
| 199 | + { |
|
| 200 | + // study the pattern, we'll execute it more than once |
|
| 201 | + $pattern .= 'S'; |
|
| 202 | + |
|
| 203 | + $this->patterns[] = array($pattern, $replacement); |
|
| 204 | + } |
|
| 205 | + |
|
| 206 | + /** |
|
| 207 | + * We can't "just" run some regular expressions against JavaScript: it's a |
|
| 208 | + * complex language. E.g. having an occurrence of // xyz would be a comment, |
|
| 209 | + * unless it's used within a string. Of you could have something that looks |
|
| 210 | + * like a 'string', but inside a comment. |
|
| 211 | + * The only way to accurately replace these pieces is to traverse the JS one |
|
| 212 | + * character at a time and try to find whatever starts first. |
|
| 213 | + * |
|
| 214 | + * @param string $content The content to replace patterns in |
|
| 215 | + * |
|
| 216 | + * @return string The (manipulated) content |
|
| 217 | + */ |
|
| 218 | + protected function replace($content) |
|
| 219 | + { |
|
| 220 | + $processed = ''; |
|
| 221 | + $positions = array_fill(0, count($this->patterns), -1); |
|
| 222 | + $matches = array(); |
|
| 223 | + |
|
| 224 | + while ($content) { |
|
| 225 | + // find first match for all patterns |
|
| 226 | + foreach ($this->patterns as $i => $pattern) { |
|
| 227 | + list($pattern, $replacement) = $pattern; |
|
| 228 | + |
|
| 229 | + // no need to re-run matches that are still in the part of the |
|
| 230 | + // content that hasn't been processed |
|
| 231 | + if ($positions[$i] >= 0) { |
|
| 232 | + continue; |
|
| 233 | + } |
|
| 234 | + |
|
| 235 | + $match = null; |
|
| 236 | + if (preg_match($pattern, $content, $match)) { |
|
| 237 | + $matches[$i] = $match; |
|
| 238 | + |
|
| 239 | + // we'll store the match position as well; that way, we |
|
| 240 | + // don't have to redo all preg_matches after changing only |
|
| 241 | + // the first (we'll still know where those others are) |
|
| 242 | + $positions[$i] = strpos($content, $match[0]); |
|
| 243 | + } else { |
|
| 244 | + // if the pattern couldn't be matched, there's no point in |
|
| 245 | + // executing it again in later runs on this same content; |
|
| 246 | + // ignore this one until we reach end of content |
|
| 247 | + unset($matches[$i]); |
|
| 248 | + $positions[$i] = strlen($content); |
|
| 249 | + } |
|
| 250 | + } |
|
| 251 | + |
|
| 252 | + // no more matches to find: everything's been processed, break out |
|
| 253 | + if (!$matches) { |
|
| 254 | + $processed .= $content; |
|
| 255 | + break; |
|
| 256 | + } |
|
| 257 | + |
|
| 258 | + // see which of the patterns actually found the first thing (we'll |
|
| 259 | + // only want to execute that one, since we're unsure if what the |
|
| 260 | + // other found was not inside what the first found) |
|
| 261 | + $discardLength = min($positions); |
|
| 262 | + $firstPattern = array_search($discardLength, $positions); |
|
| 263 | + $match = $matches[$firstPattern][0]; |
|
| 264 | + |
|
| 265 | + // execute the pattern that matches earliest in the content string |
|
| 266 | + list($pattern, $replacement) = $this->patterns[$firstPattern]; |
|
| 267 | + $replacement = $this->replacePattern($pattern, $replacement, $content); |
|
| 268 | + |
|
| 269 | + // figure out which part of the string was unmatched; that's the |
|
| 270 | + // part we'll execute the patterns on again next |
|
| 271 | + $content = substr($content, $discardLength); |
|
| 272 | + $unmatched = (string) substr($content, strpos($content, $match) + strlen($match)); |
|
| 273 | + |
|
| 274 | + // move the replaced part to $processed and prepare $content to |
|
| 275 | + // again match batch of patterns against |
|
| 276 | + $processed .= substr($replacement, 0, strlen($replacement) - strlen($unmatched)); |
|
| 277 | + $content = $unmatched; |
|
| 278 | + |
|
| 279 | + // first match has been replaced & that content is to be left alone, |
|
| 280 | + // the next matches will start after this replacement, so we should |
|
| 281 | + // fix their offsets |
|
| 282 | + foreach ($positions as $i => $position) { |
|
| 283 | + $positions[$i] -= $discardLength + strlen($match); |
|
| 284 | + } |
|
| 285 | + } |
|
| 286 | + |
|
| 287 | + return $processed; |
|
| 288 | + } |
|
| 289 | + |
|
| 290 | + /** |
|
| 291 | + * This is where a pattern is matched against $content and the matches |
|
| 292 | + * are replaced by their respective value. |
|
| 293 | + * This function will be called plenty of times, where $content will always |
|
| 294 | + * move up 1 character. |
|
| 295 | + * |
|
| 296 | + * @param string $pattern Pattern to match |
|
| 297 | + * @param string|callable $replacement Replacement value |
|
| 298 | + * @param string $content Content to match pattern against |
|
| 299 | + * |
|
| 300 | + * @return string |
|
| 301 | + */ |
|
| 302 | + protected function replacePattern($pattern, $replacement, $content) |
|
| 303 | + { |
|
| 304 | + if (is_callable($replacement)) { |
|
| 305 | + return preg_replace_callback($pattern, $replacement, $content, 1, $count); |
|
| 306 | + } else { |
|
| 307 | + return preg_replace($pattern, $replacement, $content, 1, $count); |
|
| 308 | + } |
|
| 309 | + } |
|
| 310 | + |
|
| 311 | + /** |
|
| 312 | + * Strings are a pattern we need to match, in order to ignore potential |
|
| 313 | + * code-like content inside them, but we just want all of the string |
|
| 314 | + * content to remain untouched. |
|
| 315 | + * |
|
| 316 | + * This method will replace all string content with simple STRING# |
|
| 317 | + * placeholder text, so we've rid all strings from characters that may be |
|
| 318 | + * misinterpreted. Original string content will be saved in $this->extracted |
|
| 319 | + * and after doing all other minifying, we can restore the original content |
|
| 320 | + * via restoreStrings(). |
|
| 321 | + * |
|
| 322 | + * @param string[optional] $chars |
|
| 323 | + */ |
|
| 324 | + protected function extractStrings($chars = '\'"') |
|
| 325 | + { |
|
| 326 | + // PHP only supports $this inside anonymous functions since 5.4 |
|
| 327 | + $minifier = $this; |
|
| 328 | + $callback = function ($match) use ($minifier) { |
|
| 329 | + // check the second index here, because the first always contains a quote |
|
| 330 | + if ($match[2] === '') { |
|
| 331 | + /* |
|
| 332 | 332 | * Empty strings need no placeholder; they can't be confused for |
| 333 | 333 | * anything else anyway. |
| 334 | 334 | * But we still needed to match them, for the extraction routine |
| 335 | 335 | * to skip over this particular string. |
| 336 | 336 | */ |
| 337 | - return $match[0]; |
|
| 338 | - } |
|
| 337 | + return $match[0]; |
|
| 338 | + } |
|
| 339 | 339 | |
| 340 | - $count = count($minifier->extracted); |
|
| 341 | - $placeholder = $match[1].$count.$match[1]; |
|
| 342 | - $minifier->extracted[$placeholder] = $match[1].$match[2].$match[1]; |
|
| 340 | + $count = count($minifier->extracted); |
|
| 341 | + $placeholder = $match[1].$count.$match[1]; |
|
| 342 | + $minifier->extracted[$placeholder] = $match[1].$match[2].$match[1]; |
|
| 343 | 343 | |
| 344 | - return $placeholder; |
|
| 345 | - }; |
|
| 344 | + return $placeholder; |
|
| 345 | + }; |
|
| 346 | 346 | |
| 347 | - /* |
|
| 347 | + /* |
|
| 348 | 348 | * The \\ messiness explained: |
| 349 | 349 | * * Don't count ' or " as end-of-string if it's escaped (has backslash |
| 350 | 350 | * in front of it) |
@@ -356,75 +356,75 @@ discard block |
||
| 356 | 356 | * considered as escape-char (times 2) and to get it in the regex, |
| 357 | 357 | * escaped (times 2) |
| 358 | 358 | */ |
| 359 | - $this->registerPattern('/(['.$chars.'])(.*?(?<!\\\\)(\\\\\\\\)*+)\\1/s', $callback); |
|
| 360 | - } |
|
| 361 | - |
|
| 362 | - /** |
|
| 363 | - * This method will restore all extracted data (strings, regexes) that were |
|
| 364 | - * replaced with placeholder text in extract*(). The original content was |
|
| 365 | - * saved in $this->extracted. |
|
| 366 | - * |
|
| 367 | - * @param string $content |
|
| 368 | - * |
|
| 369 | - * @return string |
|
| 370 | - */ |
|
| 371 | - protected function restoreExtractedData($content) |
|
| 372 | - { |
|
| 373 | - if (!$this->extracted) { |
|
| 374 | - // nothing was extracted, nothing to restore |
|
| 375 | - return $content; |
|
| 376 | - } |
|
| 377 | - |
|
| 378 | - $content = strtr($content, $this->extracted); |
|
| 379 | - |
|
| 380 | - $this->extracted = array(); |
|
| 381 | - |
|
| 382 | - return $content; |
|
| 383 | - } |
|
| 384 | - |
|
| 385 | - /** |
|
| 386 | - * Check if the path is a regular file and can be read. |
|
| 387 | - * |
|
| 388 | - * @param string $path |
|
| 389 | - * |
|
| 390 | - * @return bool |
|
| 391 | - */ |
|
| 392 | - protected function canImportFile($path) |
|
| 393 | - { |
|
| 394 | - return strlen($path) < PHP_MAXPATHLEN && @is_file($path) && is_readable($path); |
|
| 395 | - } |
|
| 396 | - |
|
| 397 | - /** |
|
| 398 | - * Attempts to open file specified by $path for writing. |
|
| 399 | - * |
|
| 400 | - * @param string $path The path to the file |
|
| 401 | - * |
|
| 402 | - * @return resource Specifier for the target file |
|
| 403 | - * |
|
| 404 | - * @throws IOException |
|
| 405 | - */ |
|
| 406 | - protected function openFileForWriting($path) |
|
| 407 | - { |
|
| 408 | - if (($handler = @fopen($path, 'w')) === false) { |
|
| 409 | - throw new IOException('The file "'.$path.'" could not be opened for writing. Check if PHP has enough permissions.'); |
|
| 410 | - } |
|
| 411 | - |
|
| 412 | - return $handler; |
|
| 413 | - } |
|
| 414 | - |
|
| 415 | - /** |
|
| 416 | - * Attempts to write $content to the file specified by $handler. $path is used for printing exceptions. |
|
| 417 | - * |
|
| 418 | - * @param resource $handler The resource to write to |
|
| 419 | - * @param string $content The content to write |
|
| 420 | - * @param string $path The path to the file (for exception printing only) |
|
| 421 | - * |
|
| 422 | - * @throws IOException |
|
| 423 | - */ |
|
| 424 | - protected function writeToFile($handler, $content, $path = '') |
|
| 425 | - { |
|
| 426 | - if (($result = @fwrite($handler, $content)) === false || ($result < strlen($content))) { |
|
| 427 | - throw new IOException('The file "'.$path.'" could not be written to. Check your disk space and file permissions.'); |
|
| 428 | - } |
|
| 429 | - } |
|
| 359 | + $this->registerPattern('/(['.$chars.'])(.*?(?<!\\\\)(\\\\\\\\)*+)\\1/s', $callback); |
|
| 360 | + } |
|
| 361 | + |
|
| 362 | + /** |
|
| 363 | + * This method will restore all extracted data (strings, regexes) that were |
|
| 364 | + * replaced with placeholder text in extract*(). The original content was |
|
| 365 | + * saved in $this->extracted. |
|
| 366 | + * |
|
| 367 | + * @param string $content |
|
| 368 | + * |
|
| 369 | + * @return string |
|
| 370 | + */ |
|
| 371 | + protected function restoreExtractedData($content) |
|
| 372 | + { |
|
| 373 | + if (!$this->extracted) { |
|
| 374 | + // nothing was extracted, nothing to restore |
|
| 375 | + return $content; |
|
| 376 | + } |
|
| 377 | + |
|
| 378 | + $content = strtr($content, $this->extracted); |
|
| 379 | + |
|
| 380 | + $this->extracted = array(); |
|
| 381 | + |
|
| 382 | + return $content; |
|
| 383 | + } |
|
| 384 | + |
|
| 385 | + /** |
|
| 386 | + * Check if the path is a regular file and can be read. |
|
| 387 | + * |
|
| 388 | + * @param string $path |
|
| 389 | + * |
|
| 390 | + * @return bool |
|
| 391 | + */ |
|
| 392 | + protected function canImportFile($path) |
|
| 393 | + { |
|
| 394 | + return strlen($path) < PHP_MAXPATHLEN && @is_file($path) && is_readable($path); |
|
| 395 | + } |
|
| 396 | + |
|
| 397 | + /** |
|
| 398 | + * Attempts to open file specified by $path for writing. |
|
| 399 | + * |
|
| 400 | + * @param string $path The path to the file |
|
| 401 | + * |
|
| 402 | + * @return resource Specifier for the target file |
|
| 403 | + * |
|
| 404 | + * @throws IOException |
|
| 405 | + */ |
|
| 406 | + protected function openFileForWriting($path) |
|
| 407 | + { |
|
| 408 | + if (($handler = @fopen($path, 'w')) === false) { |
|
| 409 | + throw new IOException('The file "'.$path.'" could not be opened for writing. Check if PHP has enough permissions.'); |
|
| 410 | + } |
|
| 411 | + |
|
| 412 | + return $handler; |
|
| 413 | + } |
|
| 414 | + |
|
| 415 | + /** |
|
| 416 | + * Attempts to write $content to the file specified by $handler. $path is used for printing exceptions. |
|
| 417 | + * |
|
| 418 | + * @param resource $handler The resource to write to |
|
| 419 | + * @param string $content The content to write |
|
| 420 | + * @param string $path The path to the file (for exception printing only) |
|
| 421 | + * |
|
| 422 | + * @throws IOException |
|
| 423 | + */ |
|
| 424 | + protected function writeToFile($handler, $content, $path = '') |
|
| 425 | + { |
|
| 426 | + if (($result = @fwrite($handler, $content)) === false || ($result < strlen($content))) { |
|
| 427 | + throw new IOException('The file "'.$path.'" could not be written to. Check your disk space and file permissions.'); |
|
| 428 | + } |
|
| 429 | + } |
|
| 430 | 430 | } |
@@ -31,73 +31,73 @@ |
||
| 31 | 31 | */ |
| 32 | 32 | class RequestParameters |
| 33 | 33 | { |
| 34 | - /** |
|
| 35 | - * Site secret. |
|
| 36 | - * @var string |
|
| 37 | - */ |
|
| 38 | - private $secret; |
|
| 34 | + /** |
|
| 35 | + * Site secret. |
|
| 36 | + * @var string |
|
| 37 | + */ |
|
| 38 | + private $secret; |
|
| 39 | 39 | |
| 40 | - /** |
|
| 41 | - * Form response. |
|
| 42 | - * @var string |
|
| 43 | - */ |
|
| 44 | - private $response; |
|
| 40 | + /** |
|
| 41 | + * Form response. |
|
| 42 | + * @var string |
|
| 43 | + */ |
|
| 44 | + private $response; |
|
| 45 | 45 | |
| 46 | - /** |
|
| 47 | - * Remote user's IP address. |
|
| 48 | - * @var string |
|
| 49 | - */ |
|
| 50 | - private $remoteIp; |
|
| 46 | + /** |
|
| 47 | + * Remote user's IP address. |
|
| 48 | + * @var string |
|
| 49 | + */ |
|
| 50 | + private $remoteIp; |
|
| 51 | 51 | |
| 52 | - /** |
|
| 53 | - * Client version. |
|
| 54 | - * @var string |
|
| 55 | - */ |
|
| 56 | - private $version; |
|
| 52 | + /** |
|
| 53 | + * Client version. |
|
| 54 | + * @var string |
|
| 55 | + */ |
|
| 56 | + private $version; |
|
| 57 | 57 | |
| 58 | - /** |
|
| 59 | - * Initialise parameters. |
|
| 60 | - * |
|
| 61 | - * @param string $secret Site secret. |
|
| 62 | - * @param string $response Value from g-captcha-response form field. |
|
| 63 | - * @param string $remoteIp User's IP address. |
|
| 64 | - * @param string $version Version of this client library. |
|
| 65 | - */ |
|
| 66 | - public function __construct($secret, $response, $remoteIp = null, $version = null) |
|
| 67 | - { |
|
| 68 | - $this->secret = $secret; |
|
| 69 | - $this->response = $response; |
|
| 70 | - $this->remoteIp = $remoteIp; |
|
| 71 | - $this->version = $version; |
|
| 72 | - } |
|
| 58 | + /** |
|
| 59 | + * Initialise parameters. |
|
| 60 | + * |
|
| 61 | + * @param string $secret Site secret. |
|
| 62 | + * @param string $response Value from g-captcha-response form field. |
|
| 63 | + * @param string $remoteIp User's IP address. |
|
| 64 | + * @param string $version Version of this client library. |
|
| 65 | + */ |
|
| 66 | + public function __construct($secret, $response, $remoteIp = null, $version = null) |
|
| 67 | + { |
|
| 68 | + $this->secret = $secret; |
|
| 69 | + $this->response = $response; |
|
| 70 | + $this->remoteIp = $remoteIp; |
|
| 71 | + $this->version = $version; |
|
| 72 | + } |
|
| 73 | 73 | |
| 74 | - /** |
|
| 75 | - * Array representation. |
|
| 76 | - * |
|
| 77 | - * @return array Array formatted parameters. |
|
| 78 | - */ |
|
| 79 | - public function toArray() |
|
| 80 | - { |
|
| 81 | - $params = array('secret' => $this->secret, 'response' => $this->response); |
|
| 74 | + /** |
|
| 75 | + * Array representation. |
|
| 76 | + * |
|
| 77 | + * @return array Array formatted parameters. |
|
| 78 | + */ |
|
| 79 | + public function toArray() |
|
| 80 | + { |
|
| 81 | + $params = array('secret' => $this->secret, 'response' => $this->response); |
|
| 82 | 82 | |
| 83 | - if (!is_null($this->remoteIp)) { |
|
| 84 | - $params['remoteip'] = $this->remoteIp; |
|
| 85 | - } |
|
| 83 | + if (!is_null($this->remoteIp)) { |
|
| 84 | + $params['remoteip'] = $this->remoteIp; |
|
| 85 | + } |
|
| 86 | 86 | |
| 87 | - if (!is_null($this->version)) { |
|
| 88 | - $params['version'] = $this->version; |
|
| 89 | - } |
|
| 87 | + if (!is_null($this->version)) { |
|
| 88 | + $params['version'] = $this->version; |
|
| 89 | + } |
|
| 90 | 90 | |
| 91 | - return $params; |
|
| 92 | - } |
|
| 91 | + return $params; |
|
| 92 | + } |
|
| 93 | 93 | |
| 94 | - /** |
|
| 95 | - * Query string representation for HTTP request. |
|
| 96 | - * |
|
| 97 | - * @return string Query string formatted parameters. |
|
| 98 | - */ |
|
| 99 | - public function toQueryString() |
|
| 100 | - { |
|
| 101 | - return http_build_query($this->toArray(), '', '&'); |
|
| 102 | - } |
|
| 94 | + /** |
|
| 95 | + * Query string representation for HTTP request. |
|
| 96 | + * |
|
| 97 | + * @return string Query string formatted parameters. |
|
| 98 | + */ |
|
| 99 | + public function toQueryString() |
|
| 100 | + { |
|
| 101 | + return http_build_query($this->toArray(), '', '&'); |
|
| 102 | + } |
|
| 103 | 103 | } |
@@ -32,11 +32,11 @@ |
||
| 32 | 32 | interface RequestMethod |
| 33 | 33 | { |
| 34 | 34 | |
| 35 | - /** |
|
| 36 | - * Submit the request with the specified parameters. |
|
| 37 | - * |
|
| 38 | - * @param RequestParameters $params Request parameters |
|
| 39 | - * @return string Body of the reCAPTCHA response |
|
| 40 | - */ |
|
| 41 | - public function submit(RequestParameters $params); |
|
| 35 | + /** |
|
| 36 | + * Submit the request with the specified parameters. |
|
| 37 | + * |
|
| 38 | + * @param RequestParameters $params Request parameters |
|
| 39 | + * @return string Body of the reCAPTCHA response |
|
| 40 | + */ |
|
| 41 | + public function submit(RequestParameters $params); |
|
| 42 | 42 | } |
@@ -36,53 +36,53 @@ |
||
| 36 | 36 | */ |
| 37 | 37 | class CurlPost implements RequestMethod |
| 38 | 38 | { |
| 39 | - /** |
|
| 40 | - * URL to which requests are sent via cURL. |
|
| 41 | - * @const string |
|
| 42 | - */ |
|
| 43 | - const SITE_VERIFY_URL = 'https://www.google.com/recaptcha/api/siteverify'; |
|
| 39 | + /** |
|
| 40 | + * URL to which requests are sent via cURL. |
|
| 41 | + * @const string |
|
| 42 | + */ |
|
| 43 | + const SITE_VERIFY_URL = 'https://www.google.com/recaptcha/api/siteverify'; |
|
| 44 | 44 | |
| 45 | - /** |
|
| 46 | - * Curl connection to the reCAPTCHA service |
|
| 47 | - * @var Curl |
|
| 48 | - */ |
|
| 49 | - private $curl; |
|
| 45 | + /** |
|
| 46 | + * Curl connection to the reCAPTCHA service |
|
| 47 | + * @var Curl |
|
| 48 | + */ |
|
| 49 | + private $curl; |
|
| 50 | 50 | |
| 51 | - public function __construct(Curl $curl = null) |
|
| 52 | - { |
|
| 53 | - if (!is_null($curl)) { |
|
| 54 | - $this->curl = $curl; |
|
| 55 | - } else { |
|
| 56 | - $this->curl = new Curl(); |
|
| 57 | - } |
|
| 58 | - } |
|
| 51 | + public function __construct(Curl $curl = null) |
|
| 52 | + { |
|
| 53 | + if (!is_null($curl)) { |
|
| 54 | + $this->curl = $curl; |
|
| 55 | + } else { |
|
| 56 | + $this->curl = new Curl(); |
|
| 57 | + } |
|
| 58 | + } |
|
| 59 | 59 | |
| 60 | - /** |
|
| 61 | - * Submit the cURL request with the specified parameters. |
|
| 62 | - * |
|
| 63 | - * @param RequestParameters $params Request parameters |
|
| 64 | - * @return string Body of the reCAPTCHA response |
|
| 65 | - */ |
|
| 66 | - public function submit(RequestParameters $params) |
|
| 67 | - { |
|
| 68 | - $handle = $this->curl->init(self::SITE_VERIFY_URL); |
|
| 60 | + /** |
|
| 61 | + * Submit the cURL request with the specified parameters. |
|
| 62 | + * |
|
| 63 | + * @param RequestParameters $params Request parameters |
|
| 64 | + * @return string Body of the reCAPTCHA response |
|
| 65 | + */ |
|
| 66 | + public function submit(RequestParameters $params) |
|
| 67 | + { |
|
| 68 | + $handle = $this->curl->init(self::SITE_VERIFY_URL); |
|
| 69 | 69 | |
| 70 | - $options = array( |
|
| 71 | - CURLOPT_POST => true, |
|
| 72 | - CURLOPT_POSTFIELDS => $params->toQueryString(), |
|
| 73 | - CURLOPT_HTTPHEADER => array( |
|
| 74 | - 'Content-Type: application/x-www-form-urlencoded' |
|
| 75 | - ), |
|
| 76 | - CURLINFO_HEADER_OUT => false, |
|
| 77 | - CURLOPT_HEADER => false, |
|
| 78 | - CURLOPT_RETURNTRANSFER => true, |
|
| 79 | - CURLOPT_SSL_VERIFYPEER => true |
|
| 80 | - ); |
|
| 81 | - $this->curl->setoptArray($handle, $options); |
|
| 70 | + $options = array( |
|
| 71 | + CURLOPT_POST => true, |
|
| 72 | + CURLOPT_POSTFIELDS => $params->toQueryString(), |
|
| 73 | + CURLOPT_HTTPHEADER => array( |
|
| 74 | + 'Content-Type: application/x-www-form-urlencoded' |
|
| 75 | + ), |
|
| 76 | + CURLINFO_HEADER_OUT => false, |
|
| 77 | + CURLOPT_HEADER => false, |
|
| 78 | + CURLOPT_RETURNTRANSFER => true, |
|
| 79 | + CURLOPT_SSL_VERIFYPEER => true |
|
| 80 | + ); |
|
| 81 | + $this->curl->setoptArray($handle, $options); |
|
| 82 | 82 | |
| 83 | - $response = $this->curl->exec($handle); |
|
| 84 | - $this->curl->close($handle); |
|
| 83 | + $response = $this->curl->exec($handle); |
|
| 84 | + $this->curl->close($handle); |
|
| 85 | 85 | |
| 86 | - return $response; |
|
| 87 | - } |
|
| 86 | + return $response; |
|
| 87 | + } |
|
| 88 | 88 | } |
@@ -32,74 +32,74 @@ |
||
| 32 | 32 | */ |
| 33 | 33 | class Socket |
| 34 | 34 | { |
| 35 | - private $handle = null; |
|
| 35 | + private $handle = null; |
|
| 36 | 36 | |
| 37 | - /** |
|
| 38 | - * fsockopen |
|
| 39 | - * |
|
| 40 | - * @see http://php.net/fsockopen |
|
| 41 | - * @param string $hostname |
|
| 42 | - * @param int $port |
|
| 43 | - * @param int $errno |
|
| 44 | - * @param string $errstr |
|
| 45 | - * @param float $timeout |
|
| 46 | - * @return resource |
|
| 47 | - */ |
|
| 48 | - public function fsockopen($hostname, $port = -1, &$errno = 0, &$errstr = '', $timeout = null) |
|
| 49 | - { |
|
| 50 | - $this->handle = fsockopen($hostname, $port, $errno, $errstr, (is_null($timeout) ? ini_get("default_socket_timeout") : $timeout)); |
|
| 37 | + /** |
|
| 38 | + * fsockopen |
|
| 39 | + * |
|
| 40 | + * @see http://php.net/fsockopen |
|
| 41 | + * @param string $hostname |
|
| 42 | + * @param int $port |
|
| 43 | + * @param int $errno |
|
| 44 | + * @param string $errstr |
|
| 45 | + * @param float $timeout |
|
| 46 | + * @return resource |
|
| 47 | + */ |
|
| 48 | + public function fsockopen($hostname, $port = -1, &$errno = 0, &$errstr = '', $timeout = null) |
|
| 49 | + { |
|
| 50 | + $this->handle = fsockopen($hostname, $port, $errno, $errstr, (is_null($timeout) ? ini_get("default_socket_timeout") : $timeout)); |
|
| 51 | 51 | |
| 52 | - if ($this->handle != false && $errno === 0 && $errstr === '') { |
|
| 53 | - return $this->handle; |
|
| 54 | - } else { |
|
| 55 | - return false; |
|
| 56 | - } |
|
| 57 | - } |
|
| 52 | + if ($this->handle != false && $errno === 0 && $errstr === '') { |
|
| 53 | + return $this->handle; |
|
| 54 | + } else { |
|
| 55 | + return false; |
|
| 56 | + } |
|
| 57 | + } |
|
| 58 | 58 | |
| 59 | - /** |
|
| 60 | - * fwrite |
|
| 61 | - * |
|
| 62 | - * @see http://php.net/fwrite |
|
| 63 | - * @param string $string |
|
| 64 | - * @param int $length |
|
| 65 | - * @return int | bool |
|
| 66 | - */ |
|
| 67 | - public function fwrite($string, $length = null) |
|
| 68 | - { |
|
| 69 | - return fwrite($this->handle, $string, (is_null($length) ? strlen($string) : $length)); |
|
| 70 | - } |
|
| 59 | + /** |
|
| 60 | + * fwrite |
|
| 61 | + * |
|
| 62 | + * @see http://php.net/fwrite |
|
| 63 | + * @param string $string |
|
| 64 | + * @param int $length |
|
| 65 | + * @return int | bool |
|
| 66 | + */ |
|
| 67 | + public function fwrite($string, $length = null) |
|
| 68 | + { |
|
| 69 | + return fwrite($this->handle, $string, (is_null($length) ? strlen($string) : $length)); |
|
| 70 | + } |
|
| 71 | 71 | |
| 72 | - /** |
|
| 73 | - * fgets |
|
| 74 | - * |
|
| 75 | - * @see http://php.net/fgets |
|
| 76 | - * @param int $length |
|
| 77 | - * @return string |
|
| 78 | - */ |
|
| 79 | - public function fgets($length = null) |
|
| 80 | - { |
|
| 81 | - return fgets($this->handle, $length); |
|
| 82 | - } |
|
| 72 | + /** |
|
| 73 | + * fgets |
|
| 74 | + * |
|
| 75 | + * @see http://php.net/fgets |
|
| 76 | + * @param int $length |
|
| 77 | + * @return string |
|
| 78 | + */ |
|
| 79 | + public function fgets($length = null) |
|
| 80 | + { |
|
| 81 | + return fgets($this->handle, $length); |
|
| 82 | + } |
|
| 83 | 83 | |
| 84 | - /** |
|
| 85 | - * feof |
|
| 86 | - * |
|
| 87 | - * @see http://php.net/feof |
|
| 88 | - * @return bool |
|
| 89 | - */ |
|
| 90 | - public function feof() |
|
| 91 | - { |
|
| 92 | - return feof($this->handle); |
|
| 93 | - } |
|
| 84 | + /** |
|
| 85 | + * feof |
|
| 86 | + * |
|
| 87 | + * @see http://php.net/feof |
|
| 88 | + * @return bool |
|
| 89 | + */ |
|
| 90 | + public function feof() |
|
| 91 | + { |
|
| 92 | + return feof($this->handle); |
|
| 93 | + } |
|
| 94 | 94 | |
| 95 | - /** |
|
| 96 | - * fclose |
|
| 97 | - * |
|
| 98 | - * @see http://php.net/fclose |
|
| 99 | - * @return bool |
|
| 100 | - */ |
|
| 101 | - public function fclose() |
|
| 102 | - { |
|
| 103 | - return fclose($this->handle); |
|
| 104 | - } |
|
| 95 | + /** |
|
| 96 | + * fclose |
|
| 97 | + * |
|
| 98 | + * @see http://php.net/fclose |
|
| 99 | + * @return bool |
|
| 100 | + */ |
|
| 101 | + public function fclose() |
|
| 102 | + { |
|
| 103 | + return fclose($this->handle); |
|
| 104 | + } |
|
| 105 | 105 | } |
@@ -34,37 +34,37 @@ |
||
| 34 | 34 | */ |
| 35 | 35 | class Post implements RequestMethod |
| 36 | 36 | { |
| 37 | - /** |
|
| 38 | - * URL to which requests are POSTed. |
|
| 39 | - * @const string |
|
| 40 | - */ |
|
| 41 | - const SITE_VERIFY_URL = 'https://www.google.com/recaptcha/api/siteverify'; |
|
| 37 | + /** |
|
| 38 | + * URL to which requests are POSTed. |
|
| 39 | + * @const string |
|
| 40 | + */ |
|
| 41 | + const SITE_VERIFY_URL = 'https://www.google.com/recaptcha/api/siteverify'; |
|
| 42 | 42 | |
| 43 | - /** |
|
| 44 | - * Submit the POST request with the specified parameters. |
|
| 45 | - * |
|
| 46 | - * @param RequestParameters $params Request parameters |
|
| 47 | - * @return string Body of the reCAPTCHA response |
|
| 48 | - */ |
|
| 49 | - public function submit(RequestParameters $params) |
|
| 50 | - { |
|
| 51 | - /** |
|
| 52 | - * PHP 5.6.0 changed the way you specify the peer name for SSL context options. |
|
| 53 | - * Using "CN_name" will still work, but it will raise deprecated errors. |
|
| 54 | - */ |
|
| 55 | - $peer_key = version_compare(PHP_VERSION, '5.6.0', '<') ? 'CN_name' : 'peer_name'; |
|
| 56 | - $options = array( |
|
| 57 | - 'http' => array( |
|
| 58 | - 'header' => "Content-type: application/x-www-form-urlencoded\r\n", |
|
| 59 | - 'method' => 'POST', |
|
| 60 | - 'content' => $params->toQueryString(), |
|
| 61 | - // Force the peer to validate (not needed in 5.6.0+, but still works |
|
| 62 | - 'verify_peer' => true, |
|
| 63 | - // Force the peer validation to use www.google.com |
|
| 64 | - $peer_key => 'www.google.com', |
|
| 65 | - ), |
|
| 66 | - ); |
|
| 67 | - $context = stream_context_create($options); |
|
| 68 | - return file_get_contents(self::SITE_VERIFY_URL, false, $context); |
|
| 69 | - } |
|
| 43 | + /** |
|
| 44 | + * Submit the POST request with the specified parameters. |
|
| 45 | + * |
|
| 46 | + * @param RequestParameters $params Request parameters |
|
| 47 | + * @return string Body of the reCAPTCHA response |
|
| 48 | + */ |
|
| 49 | + public function submit(RequestParameters $params) |
|
| 50 | + { |
|
| 51 | + /** |
|
| 52 | + * PHP 5.6.0 changed the way you specify the peer name for SSL context options. |
|
| 53 | + * Using "CN_name" will still work, but it will raise deprecated errors. |
|
| 54 | + */ |
|
| 55 | + $peer_key = version_compare(PHP_VERSION, '5.6.0', '<') ? 'CN_name' : 'peer_name'; |
|
| 56 | + $options = array( |
|
| 57 | + 'http' => array( |
|
| 58 | + 'header' => "Content-type: application/x-www-form-urlencoded\r\n", |
|
| 59 | + 'method' => 'POST', |
|
| 60 | + 'content' => $params->toQueryString(), |
|
| 61 | + // Force the peer to validate (not needed in 5.6.0+, but still works |
|
| 62 | + 'verify_peer' => true, |
|
| 63 | + // Force the peer validation to use www.google.com |
|
| 64 | + $peer_key => 'www.google.com', |
|
| 65 | + ), |
|
| 66 | + ); |
|
| 67 | + $context = stream_context_create($options); |
|
| 68 | + return file_get_contents(self::SITE_VERIFY_URL, false, $context); |
|
| 69 | + } |
|
| 70 | 70 | } |
@@ -36,86 +36,86 @@ |
||
| 36 | 36 | */ |
| 37 | 37 | class SocketPost implements RequestMethod |
| 38 | 38 | { |
| 39 | - /** |
|
| 40 | - * reCAPTCHA service host. |
|
| 41 | - * @const string |
|
| 42 | - */ |
|
| 43 | - const RECAPTCHA_HOST = 'www.google.com'; |
|
| 44 | - |
|
| 45 | - /** |
|
| 46 | - * @const string reCAPTCHA service path |
|
| 47 | - */ |
|
| 48 | - const SITE_VERIFY_PATH = '/recaptcha/api/siteverify'; |
|
| 49 | - |
|
| 50 | - /** |
|
| 51 | - * @const string Bad request error |
|
| 52 | - */ |
|
| 53 | - const BAD_REQUEST = '{"success": false, "error-codes": ["invalid-request"]}'; |
|
| 54 | - |
|
| 55 | - /** |
|
| 56 | - * @const string Bad response error |
|
| 57 | - */ |
|
| 58 | - const BAD_RESPONSE = '{"success": false, "error-codes": ["invalid-response"]}'; |
|
| 59 | - |
|
| 60 | - /** |
|
| 61 | - * Socket to the reCAPTCHA service |
|
| 62 | - * @var Socket |
|
| 63 | - */ |
|
| 64 | - private $socket; |
|
| 65 | - |
|
| 66 | - /** |
|
| 67 | - * Constructor |
|
| 68 | - * |
|
| 69 | - * @param \ReCaptcha\RequestMethod\Socket $socket optional socket, injectable for testing |
|
| 70 | - */ |
|
| 71 | - public function __construct(Socket $socket = null) |
|
| 72 | - { |
|
| 73 | - if (!is_null($socket)) { |
|
| 74 | - $this->socket = $socket; |
|
| 75 | - } else { |
|
| 76 | - $this->socket = new Socket(); |
|
| 77 | - } |
|
| 78 | - } |
|
| 79 | - |
|
| 80 | - /** |
|
| 81 | - * Submit the POST request with the specified parameters. |
|
| 82 | - * |
|
| 83 | - * @param RequestParameters $params Request parameters |
|
| 84 | - * @return string Body of the reCAPTCHA response |
|
| 85 | - */ |
|
| 86 | - public function submit(RequestParameters $params) |
|
| 87 | - { |
|
| 88 | - $errno = 0; |
|
| 89 | - $errstr = ''; |
|
| 90 | - |
|
| 91 | - if (false === $this->socket->fsockopen('ssl://' . self::RECAPTCHA_HOST, 443, $errno, $errstr, 30)) { |
|
| 92 | - return self::BAD_REQUEST; |
|
| 93 | - } |
|
| 94 | - |
|
| 95 | - $content = $params->toQueryString(); |
|
| 96 | - |
|
| 97 | - $request = "POST " . self::SITE_VERIFY_PATH . " HTTP/1.1\r\n"; |
|
| 98 | - $request .= "Host: " . self::RECAPTCHA_HOST . "\r\n"; |
|
| 99 | - $request .= "Content-Type: application/x-www-form-urlencoded\r\n"; |
|
| 100 | - $request .= "Content-length: " . strlen($content) . "\r\n"; |
|
| 101 | - $request .= "Connection: close\r\n\r\n"; |
|
| 102 | - $request .= $content . "\r\n\r\n"; |
|
| 103 | - |
|
| 104 | - $this->socket->fwrite($request); |
|
| 105 | - $response = ''; |
|
| 106 | - |
|
| 107 | - while (!$this->socket->feof()) { |
|
| 108 | - $response .= $this->socket->fgets(4096); |
|
| 109 | - } |
|
| 110 | - |
|
| 111 | - $this->socket->fclose(); |
|
| 112 | - |
|
| 113 | - if (0 !== strpos($response, 'HTTP/1.1 200 OK')) { |
|
| 114 | - return self::BAD_RESPONSE; |
|
| 115 | - } |
|
| 116 | - |
|
| 117 | - $parts = preg_split("#\n\s*\n#Uis", $response); |
|
| 118 | - |
|
| 119 | - return $parts[1]; |
|
| 120 | - } |
|
| 39 | + /** |
|
| 40 | + * reCAPTCHA service host. |
|
| 41 | + * @const string |
|
| 42 | + */ |
|
| 43 | + const RECAPTCHA_HOST = 'www.google.com'; |
|
| 44 | + |
|
| 45 | + /** |
|
| 46 | + * @const string reCAPTCHA service path |
|
| 47 | + */ |
|
| 48 | + const SITE_VERIFY_PATH = '/recaptcha/api/siteverify'; |
|
| 49 | + |
|
| 50 | + /** |
|
| 51 | + * @const string Bad request error |
|
| 52 | + */ |
|
| 53 | + const BAD_REQUEST = '{"success": false, "error-codes": ["invalid-request"]}'; |
|
| 54 | + |
|
| 55 | + /** |
|
| 56 | + * @const string Bad response error |
|
| 57 | + */ |
|
| 58 | + const BAD_RESPONSE = '{"success": false, "error-codes": ["invalid-response"]}'; |
|
| 59 | + |
|
| 60 | + /** |
|
| 61 | + * Socket to the reCAPTCHA service |
|
| 62 | + * @var Socket |
|
| 63 | + */ |
|
| 64 | + private $socket; |
|
| 65 | + |
|
| 66 | + /** |
|
| 67 | + * Constructor |
|
| 68 | + * |
|
| 69 | + * @param \ReCaptcha\RequestMethod\Socket $socket optional socket, injectable for testing |
|
| 70 | + */ |
|
| 71 | + public function __construct(Socket $socket = null) |
|
| 72 | + { |
|
| 73 | + if (!is_null($socket)) { |
|
| 74 | + $this->socket = $socket; |
|
| 75 | + } else { |
|
| 76 | + $this->socket = new Socket(); |
|
| 77 | + } |
|
| 78 | + } |
|
| 79 | + |
|
| 80 | + /** |
|
| 81 | + * Submit the POST request with the specified parameters. |
|
| 82 | + * |
|
| 83 | + * @param RequestParameters $params Request parameters |
|
| 84 | + * @return string Body of the reCAPTCHA response |
|
| 85 | + */ |
|
| 86 | + public function submit(RequestParameters $params) |
|
| 87 | + { |
|
| 88 | + $errno = 0; |
|
| 89 | + $errstr = ''; |
|
| 90 | + |
|
| 91 | + if (false === $this->socket->fsockopen('ssl://' . self::RECAPTCHA_HOST, 443, $errno, $errstr, 30)) { |
|
| 92 | + return self::BAD_REQUEST; |
|
| 93 | + } |
|
| 94 | + |
|
| 95 | + $content = $params->toQueryString(); |
|
| 96 | + |
|
| 97 | + $request = "POST " . self::SITE_VERIFY_PATH . " HTTP/1.1\r\n"; |
|
| 98 | + $request .= "Host: " . self::RECAPTCHA_HOST . "\r\n"; |
|
| 99 | + $request .= "Content-Type: application/x-www-form-urlencoded\r\n"; |
|
| 100 | + $request .= "Content-length: " . strlen($content) . "\r\n"; |
|
| 101 | + $request .= "Connection: close\r\n\r\n"; |
|
| 102 | + $request .= $content . "\r\n\r\n"; |
|
| 103 | + |
|
| 104 | + $this->socket->fwrite($request); |
|
| 105 | + $response = ''; |
|
| 106 | + |
|
| 107 | + while (!$this->socket->feof()) { |
|
| 108 | + $response .= $this->socket->fgets(4096); |
|
| 109 | + } |
|
| 110 | + |
|
| 111 | + $this->socket->fclose(); |
|
| 112 | + |
|
| 113 | + if (0 !== strpos($response, 'HTTP/1.1 200 OK')) { |
|
| 114 | + return self::BAD_RESPONSE; |
|
| 115 | + } |
|
| 116 | + |
|
| 117 | + $parts = preg_split("#\n\s*\n#Uis", $response); |
|
| 118 | + |
|
| 119 | + return $parts[1]; |
|
| 120 | + } |
|
| 121 | 121 | } |
@@ -32,43 +32,43 @@ |
||
| 32 | 32 | class Curl |
| 33 | 33 | { |
| 34 | 34 | |
| 35 | - /** |
|
| 36 | - * @see http://php.net/curl_init |
|
| 37 | - * @param string $url |
|
| 38 | - * @return resource cURL handle |
|
| 39 | - */ |
|
| 40 | - public function init($url = null) |
|
| 41 | - { |
|
| 42 | - return curl_init($url); |
|
| 43 | - } |
|
| 35 | + /** |
|
| 36 | + * @see http://php.net/curl_init |
|
| 37 | + * @param string $url |
|
| 38 | + * @return resource cURL handle |
|
| 39 | + */ |
|
| 40 | + public function init($url = null) |
|
| 41 | + { |
|
| 42 | + return curl_init($url); |
|
| 43 | + } |
|
| 44 | 44 | |
| 45 | - /** |
|
| 46 | - * @see http://php.net/curl_setopt_array |
|
| 47 | - * @param resource $ch |
|
| 48 | - * @param array $options |
|
| 49 | - * @return bool |
|
| 50 | - */ |
|
| 51 | - public function setoptArray($ch, array $options) |
|
| 52 | - { |
|
| 53 | - return curl_setopt_array($ch, $options); |
|
| 54 | - } |
|
| 45 | + /** |
|
| 46 | + * @see http://php.net/curl_setopt_array |
|
| 47 | + * @param resource $ch |
|
| 48 | + * @param array $options |
|
| 49 | + * @return bool |
|
| 50 | + */ |
|
| 51 | + public function setoptArray($ch, array $options) |
|
| 52 | + { |
|
| 53 | + return curl_setopt_array($ch, $options); |
|
| 54 | + } |
|
| 55 | 55 | |
| 56 | - /** |
|
| 57 | - * @see http://php.net/curl_exec |
|
| 58 | - * @param resource $ch |
|
| 59 | - * @return mixed |
|
| 60 | - */ |
|
| 61 | - public function exec($ch) |
|
| 62 | - { |
|
| 63 | - return curl_exec($ch); |
|
| 64 | - } |
|
| 56 | + /** |
|
| 57 | + * @see http://php.net/curl_exec |
|
| 58 | + * @param resource $ch |
|
| 59 | + * @return mixed |
|
| 60 | + */ |
|
| 61 | + public function exec($ch) |
|
| 62 | + { |
|
| 63 | + return curl_exec($ch); |
|
| 64 | + } |
|
| 65 | 65 | |
| 66 | - /** |
|
| 67 | - * @see http://php.net/curl_close |
|
| 68 | - * @param resource $ch |
|
| 69 | - */ |
|
| 70 | - public function close($ch) |
|
| 71 | - { |
|
| 72 | - curl_close($ch); |
|
| 73 | - } |
|
| 66 | + /** |
|
| 67 | + * @see http://php.net/curl_close |
|
| 68 | + * @param resource $ch |
|
| 69 | + */ |
|
| 70 | + public function close($ch) |
|
| 71 | + { |
|
| 72 | + curl_close($ch); |
|
| 73 | + } |
|
| 74 | 74 | } |
@@ -31,72 +31,72 @@ |
||
| 31 | 31 | */ |
| 32 | 32 | class Response |
| 33 | 33 | { |
| 34 | - /** |
|
| 35 | - * Succes or failure. |
|
| 36 | - * @var boolean |
|
| 37 | - */ |
|
| 38 | - private $success = false; |
|
| 34 | + /** |
|
| 35 | + * Succes or failure. |
|
| 36 | + * @var boolean |
|
| 37 | + */ |
|
| 38 | + private $success = false; |
|
| 39 | 39 | |
| 40 | - /** |
|
| 41 | - * Error code strings. |
|
| 42 | - * @var array |
|
| 43 | - */ |
|
| 44 | - private $errorCodes = array(); |
|
| 40 | + /** |
|
| 41 | + * Error code strings. |
|
| 42 | + * @var array |
|
| 43 | + */ |
|
| 44 | + private $errorCodes = array(); |
|
| 45 | 45 | |
| 46 | - /** |
|
| 47 | - * Build the response from the expected JSON returned by the service. |
|
| 48 | - * |
|
| 49 | - * @param string $json |
|
| 50 | - * @return \ReCaptcha\Response |
|
| 51 | - */ |
|
| 52 | - public static function fromJson($json) |
|
| 53 | - { |
|
| 54 | - $responseData = json_decode($json, true); |
|
| 46 | + /** |
|
| 47 | + * Build the response from the expected JSON returned by the service. |
|
| 48 | + * |
|
| 49 | + * @param string $json |
|
| 50 | + * @return \ReCaptcha\Response |
|
| 51 | + */ |
|
| 52 | + public static function fromJson($json) |
|
| 53 | + { |
|
| 54 | + $responseData = json_decode($json, true); |
|
| 55 | 55 | |
| 56 | - if (!$responseData) { |
|
| 57 | - return new Response(false, array('invalid-json')); |
|
| 58 | - } |
|
| 56 | + if (!$responseData) { |
|
| 57 | + return new Response(false, array('invalid-json')); |
|
| 58 | + } |
|
| 59 | 59 | |
| 60 | - if (isset($responseData['success']) && $responseData['success'] == true) { |
|
| 61 | - return new Response(true); |
|
| 62 | - } |
|
| 60 | + if (isset($responseData['success']) && $responseData['success'] == true) { |
|
| 61 | + return new Response(true); |
|
| 62 | + } |
|
| 63 | 63 | |
| 64 | - if (isset($responseData['error-codes']) && is_array($responseData['error-codes'])) { |
|
| 65 | - return new Response(false, $responseData['error-codes']); |
|
| 66 | - } |
|
| 64 | + if (isset($responseData['error-codes']) && is_array($responseData['error-codes'])) { |
|
| 65 | + return new Response(false, $responseData['error-codes']); |
|
| 66 | + } |
|
| 67 | 67 | |
| 68 | - return new Response(false); |
|
| 69 | - } |
|
| 68 | + return new Response(false); |
|
| 69 | + } |
|
| 70 | 70 | |
| 71 | - /** |
|
| 72 | - * Constructor. |
|
| 73 | - * |
|
| 74 | - * @param boolean $success |
|
| 75 | - * @param array $errorCodes |
|
| 76 | - */ |
|
| 77 | - public function __construct($success, array $errorCodes = array()) |
|
| 78 | - { |
|
| 79 | - $this->success = $success; |
|
| 80 | - $this->errorCodes = $errorCodes; |
|
| 81 | - } |
|
| 71 | + /** |
|
| 72 | + * Constructor. |
|
| 73 | + * |
|
| 74 | + * @param boolean $success |
|
| 75 | + * @param array $errorCodes |
|
| 76 | + */ |
|
| 77 | + public function __construct($success, array $errorCodes = array()) |
|
| 78 | + { |
|
| 79 | + $this->success = $success; |
|
| 80 | + $this->errorCodes = $errorCodes; |
|
| 81 | + } |
|
| 82 | 82 | |
| 83 | - /** |
|
| 84 | - * Is success? |
|
| 85 | - * |
|
| 86 | - * @return boolean |
|
| 87 | - */ |
|
| 88 | - public function isSuccess() |
|
| 89 | - { |
|
| 90 | - return $this->success; |
|
| 91 | - } |
|
| 83 | + /** |
|
| 84 | + * Is success? |
|
| 85 | + * |
|
| 86 | + * @return boolean |
|
| 87 | + */ |
|
| 88 | + public function isSuccess() |
|
| 89 | + { |
|
| 90 | + return $this->success; |
|
| 91 | + } |
|
| 92 | 92 | |
| 93 | - /** |
|
| 94 | - * Get error codes. |
|
| 95 | - * |
|
| 96 | - * @return array |
|
| 97 | - */ |
|
| 98 | - public function getErrorCodes() |
|
| 99 | - { |
|
| 100 | - return $this->errorCodes; |
|
| 101 | - } |
|
| 93 | + /** |
|
| 94 | + * Get error codes. |
|
| 95 | + * |
|
| 96 | + * @return array |
|
| 97 | + */ |
|
| 98 | + public function getErrorCodes() |
|
| 99 | + { |
|
| 100 | + return $this->errorCodes; |
|
| 101 | + } |
|
| 102 | 102 | } |