self::MAX_EXT_BYTES ) { return self::invalid( 'ext_too_large' ); } $rule->state = self::STATE_OK; $rule->v = $v; $rule->read_only = $v > self::SCHEMA_VERSION; $rule->tree = $raw['tree']; $rule->ext = $ext; $fb = isset( $raw['fallback'] ) ? $raw['fallback'] : null; $rule->fallback = in_array( $fb, array( 'show', 'hide' ), true ) ? $fb : null; if ( 'widget' === $context && isset( $raw['import'] ) && is_array( $raw['import'] ) ) { $rule->import = $raw['import']; } return $rule; } /** * Build an invalid rule. * * @param string $reason Reason. * @return Rule */ public static function invalid( $reason ) { $rule = new self(); $rule->state = self::STATE_INVALID; $rule->invalid_reason = $reason; return $rule; } /** * Build a fresh rule from a tree. * * @param array $tree Tree. * @return Rule */ public static function from_tree( array $tree ) { $rule = new self(); $rule->state = self::STATE_OK; $rule->tree = $tree; return $rule; } /** * Does an array look like one node. * * @param array $node Node. * @return bool */ public static function looks_like_node( array $node ) { return isset( $node['op'] ) || isset( $node['cond'] ) || array_key_exists( 'unknown', $node ); } /** * Stored array form. * * @return array|null */ public function to_array() { if ( self::STATE_OK !== $this->state ) { return null; } return array( 'v' => $this->v, 'tree' => $this->tree, 'fallback' => $this->fallback, 'import' => $this->import, 'ext' => $this->ext, ); } /** * Canonical JSON (keys of objects sorted, lists kept in order), excluding * the save marker. Used for the save marker (prompt 5.9 c). * * @return string */ public function canonical_json() { $arr = $this->to_array(); if ( null === $arr ) { return ''; } if ( isset( $arr['ext']['gateforge']['saved_hash'] ) ) { unset( $arr['ext']['gateforge']['saved_hash'] ); if ( empty( $arr['ext']['gateforge'] ) ) { unset( $arr['ext']['gateforge'] ); } } return (string) self::json( self::canon( $arr ) ); } /** * Hash for the save marker. * * @return string */ public function compute_marker() { return hash( 'sha256', $this->canonical_json() ); } /** * Is the save marker present and matching. * * @return bool */ public function marker_valid() { $m = isset( $this->ext['gateforge']['saved_hash'] ) ? $this->ext['gateforge']['saved_hash'] : null; return is_string( $m ) && hash_equals( $this->compute_marker(), $m ); } /** * Measure the tree. * * @return array{nodes:int,depth:int,max_list:int} */ public function measure() { $stats = array( 'nodes' => 0, 'depth' => 0, 'max_list' => 0, ); self::walk_measure( $this->tree, 1, $stats ); return $stats; } /** * Is the rule over a limit. * * @return bool */ public function over_limit() { $m = $this->measure(); return $m['nodes'] > self::MAX_NODES || $m['depth'] > self::MAX_DEPTH || $m['max_list'] > Registry::MAX_LIST_ITEMS; } /** * Recursive measure with an early stop so a huge tree costs little. * * @param mixed $node Node. * @param int $depth Depth. * @param array $stats Stats (by reference). */ private static function walk_measure( $node, $depth, array &$stats ) { ++$stats['nodes']; if ( $depth > $stats['depth'] ) { $stats['depth'] = $depth; } if ( $stats['nodes'] > self::MAX_NODES || $depth > self::MAX_DEPTH || ! is_array( $node ) ) { return; } if ( isset( $node['args'] ) && is_array( $node['args'] ) ) { foreach ( $node['args'] as $arg ) { if ( is_array( $arg ) && count( $arg ) > $stats['max_list'] ) { $stats['max_list'] = count( $arg ); } } } if ( isset( $node['items'] ) && is_array( $node['items'] ) ) { foreach ( $node['items'] as $child ) { self::walk_measure( $child, $depth + 1, $stats ); if ( $stats['nodes'] > self::MAX_NODES ) { return; } } } if ( isset( $node['item'] ) ) { self::walk_measure( $node['item'], $depth + 1, $stats ); } } /** * Recursively sort object keys. * * @param mixed $value Value. * @return mixed */ public static function canon( $value ) { if ( ! is_array( $value ) ) { return $value; } $is_list = array_values( $value ) === $value; $out = array(); foreach ( $value as $k => $v ) { $out[ $k ] = self::canon( $v ); } if ( ! $is_list ) { ksort( $out, SORT_STRING ); } return $out; } /** * JSON encode. * * @param mixed $value Value. * @return string|false */ public static function json( $value ) { return json_encode( $value, JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE | JSON_PARTIAL_OUTPUT_ON_ERROR ); // phpcs:ignore WordPress.WP.AlternativeFunctions.json_encode_json_encode -- deterministic flags needed for the marker hash. } }