Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- // (c) 2014 Diogo Parrinha
- //
- // Replaces all intval(...) with (int)(...)
- //
- // USAGE: intval.php?dir=path_to_dir
- // NOTE: Browsing is recursive, meaning that any files inside subfolders will also be altered (if needed).
- //
- // LICENCE: Just give me credit, that's all I ask for.
- // WEBSITE: www.mybb-plugins.com
- $dir = $_GET['dir'];
- $replacements = 0;
- $fixes = 0;
- $skipped = 0;
- echo "<h2>Starting replacement (intval(...) -> (int)(...)) process...</h2>";
- $tabs = '';
- while(($msg = browse($dir, $tabs)) === true);
- echo $msg;
- exit;
- function browse($dir, &$tabs)
- {
- $tabs .= '+';
- // Directory?
- if (is_dir($dir))
- {
- if ($directory = opendir($dir))
- {
- echo "<br /><strong>$tabs Entering directory $dir</strong>";
- while (($file = readdir($directory)) !== false)
- {
- if($file == '..' || $file == '.')
- continue;
- if(is_dir($dir.'/'.$file))
- {
- browse($dir.'/'.$file, $tabs);
- $tabs = substr_replace($tabs, '', strlen($tabs)-1, 1);
- }
- else
- {
- if(substr($file, -3, 3) != 'php')
- continue;
- fix_file($dir.'/'.$file, $tabs);
- }
- }
- closedir($directory);
- }
- }
- else
- return "Error: $dir is not a directory.";
- global $fixes, $skipped, $replacements;
- return "<br /><br />
- <h3>
- Success. Everything seems to be done.<br />
- Total intval's replaced: $replacements<br />
- Total intval's skipped: $skipped (because of array_map)<br />
- Total intval's fixed: $fixes (because of parse errors)<br />
- </h3>";
- }
- function fix_file($filepath, $tabs)
- {
- echo "<br />$tabs Fixing file $filepath...";
- $fd = fopen($filepath, "r+");
- if($fd == NULL)
- {
- echo "Could not open $filepath for reading.";
- return false;
- }
- $buffer = '';
- $file = '';
- $fixed = false;
- while (($buffer = fgets($fd, 4096)) !== false) {
- while(($pos = strpos($buffer, "intval")) !== false)
- {
- if($buffer[$pos-1] == '\'' || $buffer[$pos+6] == '\'')
- {
- $buffer = substr_replace($buffer, 'INTVAL', $pos, 6);
- global $skipped;
- ++$skipped;
- }
- else
- {
- global $replacements;
- ++$replacements;
- // Remove intval(
- $buffer = substr_replace($buffer, '(int)', $pos, 7);
- // Now we need to remove the ) that closed intval(
- $c = $pos+7;
- $number = 0;
- while(isset($buffer[$c]))
- {
- if($buffer[$c] == ')' && $number == 0)
- break;
- if($buffer[$c] == '(')
- ++$number;
- if($buffer[$c] == ')')
- --$number;
- ++$c;
- }
- // we don't want to get parse errors due to things like this: ".(int)bla(do_something)+number."
- if(is_numeric($buffer[$c-1]))
- {
- // insert ( back
- $buffer = substr_replace($buffer, '(int)(', $pos, 5);
- global $fixes;
- ++$fixes;
- }
- else
- {
- // remove )
- $buffer = substr_replace($buffer, '', $c, 1);
- }
- $fixed = true;
- }
- }
- $file .= $buffer;
- }
- if (!feof($fd)) {
- echo "Unexpected EOF error.";
- fclose($fd);
- return false;
- }
- fclose($fd);
- if($fixed === true)
- {
- // Open it again but for writing (will erase the file as well)
- $fd = fopen($filepath, "w");
- if($fd == NULL)
- {
- echo "Could not open $filepath for writing.";
- return false;
- }
- fwrite($fd, str_replace('INTVAL', 'intval', $file));
- fclose($fd);
- echo "DONE.";
- }
- else
- echo "nothing to fix.";
- return true;
- }
- ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement