fp = $fp; $this->chunk = $chunk; fseek($fp, $offset); $this->base = $offset; } /** * Make sure at least $n bytes from the current position are buffered (unless the file ends) */ private function need($n) { while (!$this->eof && strlen($this->buffer) - $this->pos < $n) { $data = fread($this->fp, $this->chunk); if ($data === false || $data === '') { $this->eof = true; break; } $this->buffer .= $data; } return strlen($this->buffer) - $this->pos >= $n; } private function peek() { return $this->need(1) ? $this->buffer[$this->pos] : null; } private function skip_whitespace_and_commas() { while (($c = $this->peek()) !== null && ($c === ' ' || $c === "\n" || $c === "\r" || $c === "\t" || $c === ',')) { $this->pos++; } return $c; } /** * Position after the string starting at $this->pos (which must be a quote) */ private function skip_string() { $this->pos++; while (true) { if (!$this->need(1)) { return false; } $run = strcspn($this->buffer, "\"\\", $this->pos); $this->pos += $run; if ($this->pos >= strlen($this->buffer)) { // End of the buffered data, not a delimiter: read more and keep scanning continue; } $c = $this->buffer[$this->pos]; if ($c === '\\') { $this->pos += 2; continue; } $this->pos++; return true; } } /** * Position after the object/array starting at $this->pos */ private function skip_container() { $depth = 0; while (true) { if (!$this->need(1)) { return false; } $run = strcspn($this->buffer, "\"{}[]", $this->pos); $this->pos += $run; if ($this->pos >= strlen($this->buffer)) { continue; } $c = $this->buffer[$this->pos]; if ($c === '"') { if (!$this->skip_string()) { return false; } continue; } $this->pos++; if ($c === '{' || $c === '[') { $depth++; } else { $depth--; if ($depth === 0) { return true; } } } } private function skip_scalar() { while (($c = $this->peek()) !== null && strpos(",}] \n\r\t", $c) === false) { $this->pos++; } } /** * Drop consumed bytes from the buffer */ private function compact() { if ($this->pos > 65536) { $this->buffer = substr($this->buffer, $this->pos); $this->base += $this->pos; $this->pos = 0; } } public function offset() { return $this->base + $this->pos; } /** * Read the opening of the document. Returns 'object', 'array' or null for invalid input. */ public function open() { $c = $this->skip_whitespace_and_commas(); if ($c === '{' || $c === '[') { $this->pos++; return $c === '{' ? 'object' : 'array'; } return null; } /** * Call $callback($raw_json, $key) for each object/array member until the deadline. * Returns array('offset' => next offset, 'done' => bool) or WP_Error for truncated input. */ public function each($top, $deadline, $callback) { $count = 0; while (true) { if ((++$count % 200) === 0 && microtime(true) >= $deadline) { return array('offset' => $this->offset(), 'done' => false); } $this->compact(); $c = $this->skip_whitespace_and_commas(); if ($c === null) { return new WP_Error('aowpcs_json_truncated', __('The data feed ended unexpectedly.', 'all-in-one-wp-content-security')); } if (($top === 'object' && $c === '}') || ($top === 'array' && $c === ']')) { return array('offset' => $this->offset(), 'done' => true); } $key = null; if ($top === 'object') { if ($c !== '"') { return new WP_Error('aowpcs_json_invalid', __('The data feed is not valid JSON.', 'all-in-one-wp-content-security')); } $start = $this->pos; if (!$this->skip_string()) { return new WP_Error('aowpcs_json_truncated', __('The data feed ended unexpectedly.', 'all-in-one-wp-content-security')); } $key = json_decode(substr($this->buffer, $start, $this->pos - $start)); while (($c = $this->peek()) !== null && ($c === ' ' || $c === "\n" || $c === "\r" || $c === "\t")) { $this->pos++; } if ($c !== ':') { return new WP_Error('aowpcs_json_invalid', __('The data feed is not valid JSON.', 'all-in-one-wp-content-security')); } $this->pos++; while (($c = $this->peek()) !== null && ($c === ' ' || $c === "\n" || $c === "\r" || $c === "\t")) { $this->pos++; } } if ($c === '{' || $c === '[') { $start = $this->pos; if (!$this->skip_container()) { return new WP_Error('aowpcs_json_truncated', __('The data feed ended unexpectedly.', 'all-in-one-wp-content-security')); } call_user_func($callback, substr($this->buffer, $start, $this->pos - $start), $key); } elseif ($c === '"') { $this->skip_string(); } else { $this->skip_scalar(); } } } /** * Process a JSON file from $offset. $top is null on the first call. * Returns array(offset, done, top) or WP_Error. */ public static function process_file($file, $offset, $top, $deadline, $callback) { $fp = @fopen($file, 'rb'); if (!$fp) { return new WP_Error('aowpcs_json_open', __('The downloaded data file could not be opened.', 'all-in-one-wp-content-security')); } $stream = new self($fp, $top === null ? 0 : $offset); if ($top === null) { $top = $stream->open(); if ($top === null) { fclose($fp); return new WP_Error('aowpcs_json_invalid', __('The data feed is not valid JSON.', 'all-in-one-wp-content-security')); } } $result = $stream->each($top, $deadline, $callback); fclose($fp); if (is_wp_error($result)) { return $result; } $result['top'] = $top; return $result; } }