array(), "filter" => array() ); # String: $str_reg # Regular expression representing a string. $str_reg = '(\"[^\"]+\"|\'[^\']+\')'; # String: $arr_reg # Regular expression representing an array construct. $arr_reg = 'array\(([^\)]+)\)'; /** * Function: scan_dir * Scans a directory in search of files or subdirectories. */ function scan_dir($pathname) { global $exclude; $dir = new DirectoryIterator($pathname); foreach ($dir as $item) { if (!$item->isDot()) { $item_path = $item->getPathname(); $extension = $item->getExtension(); switch ($item->getType()) { case "file": scan_file($item_path, $extension); break; case "dir": if (!in_array($item_path, $exclude)) scan_dir($item_path); break; } } } } /** * Function: scan_file * Scans a file in search of triggers. */ function scan_file($pathname, $extension) { if ($extension != "php" and $extension != "twig") return; $file = fopen($pathname, "r"); $line = 1; if ($file === false) return; while (!feof($file)) { $text = fgets($file); switch ($extension) { case "php": scan_call($pathname, $line, $text); scan_call_array($pathname, $line, $text); scan_filter($pathname, $line, $text); scan_filter_array($pathname, $line, $text); break; case "twig": scan_twig($pathname, $line, $text); break; } $line++; } fclose($file); } /** * Function: make_place * Makes a string detailing the place a trigger was found. */ function make_place($pathname, $line) { return str_replace( array(MAIN_DIR.DIR, DIR), array("", "/"), $pathname )." on line ".$line; } /** * Function: make_arguments * Makes an array from a string of arguments. */ function make_arguments($text) { $array = explode(",", $text); foreach ($array as &$arg) { $arg = trim($arg, ", "); } return array_diff($array, array("")); } /** * Function: scan_call * Scans text for trigger calls. */ function scan_call($pathname, $line, $text) { global $trigger; global $str_reg; if ( preg_match_all( "/(\\\$trigger|Trigger::current\(\))->call\($str_reg(,\s*(.+))?\)/", $text, $matches, PREG_SET_ORDER ) ) { foreach ($matches as $match) { $call = trim($match[2], "'\""); if (isset($trigger["call"][$call])) $trigger["call"][$call]["places"][] = make_place( $pathname, $line ); else $trigger["call"][$call] = array( "places" => array(make_place($pathname, $line)), "arguments" => make_arguments(fallback($match[4], "")) ); } } } /** * Function: scan_call_array * Scans text for trigger call arrays. */ function scan_call_array($pathname, $line, $text) { global $trigger; global $arr_reg; if ( preg_match_all( "/(\\\$trigger|Trigger::current\(\))->call\($arr_reg(,\s*(.+))?\)/", $text, $matches, PREG_SET_ORDER ) ) { foreach ($matches as $match) { $calls = explode(",", $match[2]); foreach ($calls as $call) { $call = trim($call, "'\" "); if (empty($call) or preg_match('/[^A-Za-z0-9_]/', $call)) continue; if (isset($trigger["call"][$call])) $trigger["call"][$call]["places"][] = make_place( $pathname, $line ); else $trigger["call"][$call] = array( "places" => array(make_place($pathname, $line)), "arguments" => make_arguments(fallback($match[4], "")) ); } } } } /** * Function: scan_filter * Scans text for trigger filters. */ function scan_filter($pathname, $line, $text) { global $trigger; global $str_reg; if ( preg_match_all( "/(\\\$trigger|Trigger::current\(\))->filter\(([^,]+),\s*$str_reg(,\s*(.+))?\)/", $text, $matches, PREG_SET_ORDER ) ) { foreach ($matches as $match) { $filter = trim($match[3], "'\""); if (isset($trigger["filter"][$filter])) $trigger["filter"][$filter]["places"][] = make_place( $pathname, $line ); else $trigger["filter"][$filter] = array( "places" => array(make_place($pathname, $line)), "target" => trim($match[2], ", "), "arguments" => make_arguments(fallback($match[5], "")) ); } } } /** * Function: scan_filter_array * Scans text for trigger filter arrays. */ function scan_filter_array($pathname, $line, $text) { global $trigger; global $arr_reg; if ( preg_match_all( "/(\\\$trigger|Trigger::current\(\))->filter\(([^,]+),\s*$arr_reg(,\s*(.+))?\)/", $text, $matches, PREG_SET_ORDER ) ) { foreach ($matches as $match) { $filters = explode(",", $match[3]); foreach ($filters as $filter) { $filter = trim($filter, "'\" "); if (empty($filter) or preg_match('/[^A-Za-z0-9_]/', $filter)) continue; if (isset($trigger["filter"][$filter])) $trigger["filter"][$filter]["places"][] = make_place( $pathname, $line ); else $trigger["filter"][$filter] = array( "places" => array(make_place($pathname, $line)), "target" => trim($match[2], ", "), "arguments" => make_arguments(fallback($match[5], "")) ); } } } } /** * Function: scan_twig * Scans text for trigger calls in Twig statements. */ function scan_twig($pathname, $line, $text) { global $trigger; global $str_reg; if ( preg_match_all( "/\{\{\s*trigger\.call\($str_reg(,\s*(.+))?\)\s*\}\}/", $text, $matches, PREG_SET_ORDER ) ) { foreach ($matches as $match) { $call = trim($match[1], "'\""); if (isset($trigger["call"][$call])) $trigger["call"][$call]["places"][] = make_place( $pathname, $line ); else $trigger["call"][$call] = array( "places" => array(make_place($pathname, $line)), "arguments" => make_arguments(fallback($match[3], "")) ); } } } /** * Function: create_file * Generates the triggers list and writes it to disk. */ function create_file() { global $trigger; $contents = "==============================================\n". " Trigger Calls\n". "==============================================\n"; foreach ($trigger["call"] as $call => $attributes) { $contents.= "\n\n"; $contents.= $call."\n"; $contents.= str_repeat("-", strlen($call))."\n"; $contents.= "Called from:\n"; foreach ($attributes["places"] as $place) $contents.= "\t".$place."\n"; if (!empty($attributes["arguments"])) { $contents.= "\nArguments:\n"; foreach ($attributes["arguments"] as $argument) $contents.= "\t".$argument."\n"; } } $contents.= "\n\n\n\n"; $contents.= "==============================================\n". " Trigger Filters\n". "==============================================\n"; foreach ($trigger["filter"] as $filter => $attributes) { $contents.= "\n\n"; $contents.= $filter."\n"; $contents.= str_repeat("-", strlen($filter))."\n"; $contents.= "Called from:\n"; foreach ($attributes["places"] as $place) $contents.= "\t".$place."\n"; $contents.= "\nTarget:\n"; $contents.= "\t".$attributes["target"]."\n"; if (!empty($attributes["arguments"])) { $contents.= "\nArguments:\n"; foreach ($attributes["arguments"] as $argument) $contents.= "\t".$argument."\n"; } } @file_put_contents( MAIN_DIR.DIR."tools".DIR."triggers_list.txt", $contents ); echo fix($contents); } #--------------------------------------------- # Output Starts #--------------------------------------------- ?> <?php echo "Triggers"; ?>