Advertisement
PirataNervo

intval to (int)

May 7th, 2014
237
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 3.44 KB | None | 0 0
  1. <?php
  2.  
  3. // (c) 2014 Diogo Parrinha
  4. //
  5. // Replaces all intval(...) with (int)(...)
  6. //
  7. // USAGE: intval.php?dir=path_to_dir
  8. // NOTE: Browsing is recursive, meaning that any files inside subfolders will also be altered (if needed).
  9. //
  10. // LICENCE: Just give me credit, that's all I ask for.
  11. // WEBSITE: www.mybb-plugins.com
  12.  
  13. $dir = $_GET['dir'];
  14.  
  15. $replacements = 0;
  16. $fixes = 0;
  17. $skipped = 0;
  18.  
  19. echo "<h2>Starting replacement (intval(...) -> (int)(...)) process...</h2>";
  20.  
  21. $tabs = '';
  22. while(($msg = browse($dir, $tabs)) === true);
  23.  
  24. echo $msg;
  25.  
  26. exit;
  27.  
  28. function browse($dir, &$tabs)
  29. {
  30.     $tabs .= '+';
  31.    
  32.     // Directory?
  33.     if (is_dir($dir))
  34.     {
  35.         if ($directory = opendir($dir))
  36.         {
  37.             echo "<br /><strong>$tabs Entering directory $dir</strong>";
  38.             while (($file = readdir($directory)) !== false)
  39.             {
  40.                 if($file == '..' || $file == '.')
  41.                     continue;
  42.                
  43.                 if(is_dir($dir.'/'.$file))
  44.                 {
  45.                     browse($dir.'/'.$file, $tabs);
  46.                     $tabs = substr_replace($tabs, '', strlen($tabs)-1, 1);
  47.                 }
  48.                 else
  49.                 {
  50.                     if(substr($file, -3, 3) != 'php')
  51.                         continue;
  52.                    
  53.                     fix_file($dir.'/'.$file, $tabs);
  54.                 }
  55.             }
  56.             closedir($directory);
  57.         }
  58.     }
  59.     else
  60.         return "Error: $dir is not a directory.";
  61.        
  62.     global $fixes, $skipped, $replacements;
  63.     return "<br /><br />
  64.     <h3>
  65.     Success. Everything seems to be done.<br />
  66.     Total intval's replaced: $replacements<br />
  67.     Total intval's skipped: $skipped (because of array_map)<br />
  68.     Total intval's fixed: $fixes (because of parse errors)<br />
  69.     </h3>";
  70. }
  71.  
  72. function fix_file($filepath, $tabs)
  73. {
  74.     echo "<br />$tabs Fixing file $filepath...";
  75.     $fd = fopen($filepath, "r+");
  76.     if($fd == NULL)
  77.     {
  78.         echo "Could not open $filepath for reading.";
  79.         return false;
  80.     }
  81.        
  82.     $buffer = '';
  83.     $file = '';
  84.     $fixed = false;
  85.     while (($buffer = fgets($fd, 4096)) !== false) {
  86.         while(($pos = strpos($buffer, "intval")) !== false)
  87.         {
  88.             if($buffer[$pos-1] == '\'' || $buffer[$pos+6] == '\'')
  89.             {
  90.                 $buffer = substr_replace($buffer, 'INTVAL', $pos, 6);
  91.                 global $skipped;
  92.                 ++$skipped;
  93.             }
  94.             else
  95.             {
  96.                 global $replacements;
  97.                 ++$replacements;
  98.                
  99.                 // Remove intval(
  100.                 $buffer = substr_replace($buffer, '(int)', $pos, 7);
  101.            
  102.                 // Now we need to remove the ) that closed intval(
  103.                 $c = $pos+7;
  104.                 $number = 0;
  105.                 while(isset($buffer[$c]))
  106.                 {
  107.                     if($buffer[$c] == ')' && $number == 0)
  108.                         break;
  109.                    
  110.                     if($buffer[$c] == '(')
  111.                         ++$number;
  112.                    
  113.                     if($buffer[$c] == ')')
  114.                         --$number;
  115.                        
  116.                     ++$c;
  117.                 }
  118.                
  119.                 // we don't want to get parse errors due to things like this: ".(int)bla(do_something)+number."
  120.                 if(is_numeric($buffer[$c-1]))
  121.                 {  
  122.                     // insert ( back
  123.                     $buffer = substr_replace($buffer, '(int)(', $pos, 5);
  124.                    
  125.                     global $fixes;
  126.                     ++$fixes;
  127.                 }
  128.                 else
  129.                 {
  130.                     // remove )
  131.                     $buffer = substr_replace($buffer, '', $c, 1);
  132.                 }
  133.                
  134.                 $fixed = true;
  135.             }
  136.         }
  137.        
  138.         $file .= $buffer;
  139.     }
  140.  
  141.     if (!feof($fd)) {
  142.         echo "Unexpected EOF error.";
  143.         fclose($fd);
  144.         return false;
  145.     }
  146.  
  147.     fclose($fd);
  148.  
  149.     if($fixed === true)
  150.     {
  151.         // Open it again but for writing (will erase the file as well)
  152.         $fd = fopen($filepath, "w");
  153.         if($fd == NULL)
  154.         {
  155.             echo "Could not open $filepath for writing.";
  156.             return false;
  157.         }
  158.        
  159.         fwrite($fd, str_replace('INTVAL', 'intval', $file));
  160.         fclose($fd);
  161.        
  162.         echo "DONE.";
  163.     }
  164.     else
  165.         echo "nothing to fix.";
  166.    
  167.     return true;
  168. }
  169.  
  170. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement