*/ private $map = array(); /** @var int Serialized-looking values that could not be parsed and were left untouched. */ private $unparsable = 0; /** @var int */ private $depth = 0; /** * @param array $pairs Search => replacement. */ public function __construct( array $pairs ) { foreach ( $pairs as $search => $replace ) { $search = (string) $search; $replace = (string) $replace; if ( '' === $search || $search === $replace ) { continue; } $this->map[ $search ] = $replace; $json_search = str_replace( '/', '\/', $search ); $json_replace = str_replace( '/', '\/', $replace ); if ( $json_search !== $search ) { $this->map[ $json_search ] = $json_replace; } // JSON also spells non-ASCII text as \u00e9, with or without escaped slashes (block editor attributes // and many plugins' settings are stored that way). The same text, written the way JSON writes it. foreach ( array( JSON_UNESCAPED_SLASHES, 0 ) as $flags ) { $encoded_search = json_encode( $search, $flags ); $encoded_replace = json_encode( $replace, $flags ); if ( false === $encoded_search || false === $encoded_replace ) { continue; // not valid UTF-8: there is no JSON spelling of it. } $encoded_search = substr( $encoded_search, 1, -1 ); $encoded_replace = substr( $encoded_replace, 1, -1 ); if ( $encoded_search !== $search && ! isset( $this->map[ $encoded_search ] ) ) { $this->map[ $encoded_search ] = $encoded_replace; } } } } public function isEmpty(): bool { return ! $this->map; } /** * Every search string, for pre-filtering rows in SQL. * * @return string[] */ public function needles(): array { return array_keys( $this->map ); } public function unparsableCount(): int { return $this->unparsable; } public function replace( string $value ): string { if ( '' === $value || ! $this->containsNeedle( $value ) ) { return $value; } if ( $this->looksSerialized( $value ) ) { $rewritten = $this->rewrite( $value ); if ( null === $rewritten ) { ++$this->unparsable; return $value; } return $rewritten; } // strtr scans once, longest key first, and never re-scans its own output. A // chain of str_replace calls could rewrite a replacement a second time. return strtr( $value, $this->map ); } private function containsNeedle( string $value ): bool { foreach ( $this->map as $search => $_ ) { if ( false !== strpos( $value, (string) $search ) ) { return true; } } return false; } private function looksSerialized( string $value ): bool { // Signature only. A value that starts like serialized data but does not parse // (truncated, corrupt) is left alone rather than corrupted a second way. return 1 === preg_match( '/^(?:a:\d+:\{|O:\d+:"|s:\d+:"|E:\d+:"|C:\d+:")/', $value ); } private function rewrite( string $serialized ): ?string { $pos = 0; try { $this->depth = 0; $out = $this->value( $serialized, $pos ); } catch ( \UnexpectedValueException $e ) { return null; } return $pos === strlen( $serialized ) ? $out : null; } /** * Rewrites the one serialized value starting at $pos and advances $pos past it. * * @throws \UnexpectedValueException On anything that is not well-formed. */ private function value( string $s, int &$pos ): string { if ( $pos >= strlen( $s ) ) { throw new \UnexpectedValueException( 'eof' ); } $type = $s[ $pos ]; switch ( $type ) { case 'N': return $this->literal( $s, $pos, '/\GN;/' ); case 'b': return $this->literal( $s, $pos, '/\Gb:[01];/' ); case 'i': return $this->literal( $s, $pos, '/\Gi:-?\d+;/' ); case 'd': return $this->literal( $s, $pos, '/\Gd:(?:-?\d+(?:\.\d+)?(?:[eE][+-]?\d+)?|-?INF|NAN);/' ); case 'r': case 'R': return $this->literal( $s, $pos, '/\G[rR]:\d+;/' ); case 'E': $text = $this->lengthPrefixed( $s, $pos, 'E' ); return 'E:' . strlen( $text ) . ':"' . $text . '";'; case 's': $text = $this->lengthPrefixed( $s, $pos, 's' ); $new = $this->replace( $text ); return 's:' . strlen( $new ) . ':"' . $new . '";'; case 'a': return $this->arrayValue( $s, $pos ); case 'O': return $this->objectValue( $s, $pos ); case 'C': return $this->customValue( $s, $pos ); } throw new \UnexpectedValueException( 'type' ); } private function literal( string $s, int &$pos, string $pattern ): string { if ( 1 !== preg_match( $pattern, $s, $m, 0, $pos ) ) { throw new \UnexpectedValueException( 'literal' ); } $pos += strlen( $m[0] ); return $m[0]; } /** * Reads `T:len:"payload";` and returns the payload. */ private function lengthPrefixed( string $s, int &$pos, string $type ): string { if ( 1 !== preg_match( '/\G' . $type . ':(\d+):"/', $s, $m, 0, $pos ) ) { throw new \UnexpectedValueException( 'prefix' ); } $length = (int) $m[1]; $from = $pos + strlen( $m[0] ); if ( $from + $length + 2 > strlen( $s ) || '";' !== substr( $s, $from + $length, 2 ) ) { throw new \UnexpectedValueException( 'length' ); } $pos = $from + $length + 2; return substr( $s, $from, $length ); } private function arrayValue( string $s, int &$pos ): string { if ( 1 !== preg_match( '/\Ga:(\d+):\{/', $s, $m, 0, $pos ) ) { throw new \UnexpectedValueException( 'array' ); } $pos += strlen( $m[0] ); return $m[0] . $this->members( $s, $pos, (int) $m[1] ) . '}'; } private function objectValue( string $s, int &$pos ): string { if ( 1 !== preg_match( '/\GO:(\d+):"/', $s, $m, 0, $pos ) ) { throw new \UnexpectedValueException( 'object' ); } $name_len = (int) $m[1]; $from = $pos + strlen( $m[0] ); $name = substr( $s, $from, $name_len ); if ( strlen( $name ) !== $name_len || 1 !== preg_match( '/\G":(\d+):\{/', $s, $n, 0, $from + $name_len ) ) { throw new \UnexpectedValueException( 'object header' ); } $pos = $from + $name_len + strlen( $n[0] ); // The class name is copied verbatim: it is an identifier, not content. return 'O:' . $name_len . ':"' . $name . $n[0] . $this->members( $s, $pos, (int) $n[1] ) . '}'; } /** * C:len:"Class":len:{payload} carries class-defined serialization we cannot rewrite * safely, so it is copied as is. */ private function customValue( string $s, int &$pos ): string { if ( 1 !== preg_match( '/\GC:(\d+):"/', $s, $m, 0, $pos ) ) { throw new \UnexpectedValueException( 'custom' ); } $name_len = (int) $m[1]; $from = $pos + strlen( $m[0] ); if ( 1 !== preg_match( '/\G":(\d+):\{/', $s, $n, 0, $from + $name_len ) ) { throw new \UnexpectedValueException( 'custom header' ); } $payload_from = $from + $name_len + strlen( $n[0] ); $end = $payload_from + (int) $n[1]; if ( $end >= strlen( $s ) || '}' !== $s[ $end ] ) { throw new \UnexpectedValueException( 'custom length' ); } $raw = substr( $s, $pos, $end + 1 - $pos ); $pos = $end + 1; return $raw; } /** * Reads $count key/value pairs and the closing brace. Keys are copied unchanged; * only values are searched, so array keys and property names stay stable. */ private function members( string $s, int &$pos, int $count ): string { if ( ++$this->depth > self::MAX_DEPTH ) { throw new \UnexpectedValueException( 'depth' ); } $out = ''; for ( $n = 0; $n < $count; $n++ ) { $out .= $this->key( $s, $pos ); $out .= $this->value( $s, $pos ); } if ( $pos >= strlen( $s ) || '}' !== $s[ $pos ] ) { throw new \UnexpectedValueException( 'close' ); } ++$pos; --$this->depth; return $out; } private function key( string $s, int &$pos ): string { if ( $pos < strlen( $s ) && 'i' === $s[ $pos ] ) { return $this->literal( $s, $pos, '/\Gi:-?\d+;/' ); } $text = $this->lengthPrefixed( $s, $pos, 's' ); return 's:' . strlen( $text ) . ':"' . $text . '";'; } }