get( $node['cond'] ); if ( ( $def && 'audience' === $def['risk'] ) || ( ! $def && ( ! isset( $node['risk'] ) || 'location' !== $node['risk'] ) ) ) { $terms[ $node['cond'] ] = true; } } ); // 2. Every identifier and variable in the whole string. foreach ( $tokens as $tok ) { if ( 'ident' === $tok['type'] ) { $hit = self::term_for_identifier( $tok['value'] ); if ( '' !== $hit ) { $terms[ $hit ] = true; } } elseif ( 'var' === $tok['type'] ) { $v = strtolower( $tok['value'] ); if ( in_array( $v, self::VARIABLES, true ) ) { $terms[ $v ] = true; } } } // 3. All text inside unknown fragments, including string literals and comments. $fragments = array(); self::walk_unknowns( $tree, static function ( $node ) use ( &$fragments ) { $fragments[] = (string) $node['unknown']; } ); foreach ( $fragments as $text ) { $lex = Lexer::tokenize( $text ); $raw = strtolower( $text ); if ( $lex['ok'] ) { foreach ( $lex['tokens'] as $tok ) { if ( in_array( $tok['type'], array( 'sq', 'dq', 'comment_line', 'comment_block' ), true ) ) { $hit = self::scan_text( strtolower( (string) $tok['value'] ) ); if ( '' !== $hit ) { $terms[ $hit ] = true; } } } } $hit = self::scan_text( $raw ); if ( '' !== $hit ) { $terms[ $hit ] = true; } } $list = array_keys( $terms ); sort( $list ); return array( 'risk' => empty( $list ) ? 'location' : 'audience', 'terms' => $list, ); } /** * Scan free text (inside unknown fragments only). * * @param string $text Lower-case text. * @return string Matched term or ''. */ private static function scan_text( $text ) { foreach ( self::IDENTIFIERS as $id ) { if ( false !== strpos( $text, $id ) ) { return $id; } } foreach ( self::VARIABLES as $v ) { if ( false !== strpos( $text, $v ) ) { return $v; } } foreach ( self::PREFIXES as $p ) { if ( false !== strpos( $text, $p ) ) { return $p; } } return self::word_in( $text ); } /** * Word list match. * * @param string $text Text. * @return string */ private static function word_in( $text ) { foreach ( self::WORDS as $w ) { if ( false !== strpos( $text, $w ) ) { return $w; } } return ''; } /** * Identifier term match. * * @param string $id Lower-case identifier. * @return string */ private static function term_for_identifier( $id ) { if ( in_array( $id, self::IDENTIFIERS, true ) ) { return $id; } foreach ( self::PREFIXES as $p ) { if ( 0 === strpos( $id, $p ) ) { return $p; } } return ''; } /** * Walk cond nodes. * * @param mixed $node Node. * @param callable $fn Visitor. */ private static function walk_conds( $node, callable $fn ) { if ( ! is_array( $node ) ) { return; } if ( isset( $node['cond'] ) ) { $fn( $node ); return; } if ( isset( $node['items'] ) && is_array( $node['items'] ) ) { foreach ( $node['items'] as $c ) { self::walk_conds( $c, $fn ); } } if ( isset( $node['item'] ) ) { self::walk_conds( $node['item'], $fn ); } } /** * Walk unknown nodes. * * @param mixed $node Node. * @param callable $fn Visitor. */ private static function walk_unknowns( $node, callable $fn ) { if ( ! is_array( $node ) ) { return; } if ( array_key_exists( 'unknown', $node ) ) { $fn( $node ); return; } if ( isset( $node['items'] ) && is_array( $node['items'] ) ) { foreach ( $node['items'] as $c ) { self::walk_unknowns( $c, $fn ); } } if ( isset( $node['item'] ) ) { self::walk_unknowns( $node['item'], $fn ); } } }