Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- require(__DIR__ . '/rewrite.php');
- // Для начала, оставим только те файлы, которые содержат паттерн, который мы собираемся заменять
- $files = exec('grep -RF \'$db->sql_query(\' * | awk -F: \'{print $1;}\'', $out, $retval);
- if ($retval) exit(1);
- // Наш скрипт будет жить в папке replacer/
- $excludes = array('includes/db/', 'replacer/');
- $num_lines = 0;
- foreach (array_unique($out) as $filename) {
- foreach ($excludes as $excl) {
- if (strpos($filename, $excl) === 0) continue(2);
- }
- $contents = file_get_contents($filename);
- if ($contents === false) exit(1);
- echo "$filename\n";
- $lines = explode("\n", $contents);
- // Получаем массив всех токенов в файле
- $tokens = token_get_all($contents);
- $num = count($tokens);
- $line = 1;
- $type = $text = '';
- // Нам нужно найти 5 идущих подряд токенов: '$db', '->', 'sql_query', '(' и начало запроса
- // Сам запрос должен начинаться с токена с типом «строка»
- $accepted_value_types = array(T_CONSTANT_ENCAPSED_STRING, T_ENCAPSED_AND_WHITESPACE, '"');
- $result_tokens = $tokens;
- foreach ($tokens as $cur_idx => $tok) {
- parse_token($tok, $type, $text, $line);
- // Пропускаем токены в самом конце, где точно не могут уместиться наши 5 токенов
- if ($cur_idx >= $num - 5) continue;
- $ln = $line;
- $i = $cur_idx + 1;
- // Список токенов доступен в документации (http://php.net/manual/tokens.php)
- if ($type !== T_VARIABLE || $text !== '$db') continue;
- parse_token($tokens[$i++], $type, $text, $ln);
- if ($type !== T_OBJECT_OPERATOR || $text !== '->') continue;
- parse_token($tokens[$i++], $type, $text, $ln);
- if ($type !== T_STRING || $text !== 'sql_query') continue;
- $sql_query_idx = $i - 1;
- parse_token($tokens[$i++], $type, $text, $ln);
- if ($type !== '(') continue;
- parse_token($tokens[$i++], $type, $text, $ln);
- if (!in_array($type, $accepted_value_types, true)) continue;
- $query_idx = $i - 1;
- // Теперь получим весь запрос: нам нужно всё до закрывающей скобки
- $depth = 1;
- $query = '';
- $query_tokens = array();
- for ($i = $query_idx; $i < $num; $i++) {
- parse_token($tokens[$i], $type, $text, $ln);
- if ($type === '(') $depth++;
- else if ($type === ')') $depth--;
- if ($depth == 0) break;
- $query_tokens[] = $tokens[$i];
- }
- $last_token_idx = $i - 1;
- $rewrite_result = rewrite_tokens($query_tokens);
- if (!is_array($rewrite_result)) {
- echo " Error: $rewrite_result\n";
- } else {
- for ($i = $query_idx; $i <= $last_token_idx; $i++) {
- parse_token($tokens[$i], $type, $text, $ln);
- if ($i > $query_idx) unset($result_tokens[$i]);
- }
- $result_tokens[$sql_query_idx] = 'sql_query_escaped';
- $result_tokens[$query_idx] = tokens_to_string($rewrite_result['tokens'], true);
- foreach ($rewrite_result['params'] as $par) $result_tokens[$query_idx] .= ', ' . trim($par);
- $num_lines++;
- }
- }
- file_put_contents($filename, tokens_to_string($result_tokens, true));
- exec("php -l " . escapeshellarg($filename), $out, $retval);
- if ($retval) {
- fwrite(STDERR, "PHP Syntax error in $filename\n");
- exit(1);
- }
- }
- echo "Total lines recognized: $num_lines\n";
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement