This week only. Pastebin PRO Accounts Christmas Special! Don't miss out!Want more features on Pastebin? Sign Up, it's FREE!
Guest

CloCkWeRX

By: a guest on Oct 29th, 2008  |  syntax: None  |  size: 27.97 KB  |  views: 50  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. ? php5.diff.txt
  2. ? phpcs.diff.txt
  3. Index: SearchReplace.php
  4. ===================================================================
  5. RCS file: /repository/pear/File_SearchReplace/SearchReplace.php,v
  6. retrieving revision 1.16
  7. diff -u -r1.16 SearchReplace.php
  8. --- SearchReplace.php   27 Oct 2008 15:48:58 -0000      1.16
  9. +++ SearchReplace.php   29 Oct 2008 15:57:41 -0000
  10.  -1,53 +1,53 @@
  11.  <?php
  12. -// +-----------------------------------------------------------------------+
  13. -// | Copyright (c) 2002-2005, Richard Heyes                                |
  14. -// | All rights reserved.                                                  |
  15. -// |                                                                       |
  16. -// | Redistribution and use in source and binary forms, with or without    |
  17. -// | modification, are permitted provided that the following conditions    |
  18. -// | are met:                                                              |
  19. -// |                                                                       |
  20. -// | o Redistributions of source code must retain the above copyright      |
  21. -// |   notice, this list of conditions and the following disclaimer.       |
  22. -// | o Redistributions in binary form must reproduce the above copyright   |
  23. -// |   notice, this list of conditions and the following disclaimer in the |
  24. -// |   documentation and/or other materials provided with the distribution.|
  25. -// | o The names of the authors may not be used to endorse or promote      |
  26. -// |   products derived from this software without specific prior written  |
  27. -// |   permission.                                                         |
  28. -// |                                                                       |
  29. -// | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS   |
  30. -// | "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT     |
  31. -// | LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
  32. -// | A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT  |
  33. -// | OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
  34. -// | SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT      |
  35. -// | LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
  36. -// | DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
  37. -// | THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT   |
  38. -// | (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
  39. -// | OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.  |
  40. -// |                                                                       |
  41. -// +-----------------------------------------------------------------------+
  42. -// | Author: Richard Heyes <richard@phpguru.org>                           |
  43. -// +-----------------------------------------------------------------------+
  44. -//
  45. -// $Id: SearchReplace.php,v 1.16 2008/10/27 15:48:58 clockwerx Exp $
  46. -//
  47. -// Search and Replace Utility
  48. -//
  49. +/**
  50. + * Copyright (c) 2002-2005, Richard Heyes
  51. + * All rights reserved.
  52. + *
  53. + * PHP version 4, 5
  54. + *
  55. + * Redistribution and use in source and binary forms, with or without
  56. + * modification, are permitted provided that the following conditions
  57. + * are met:
  58. + *
  59. + *  o Redistributions of source code must retain the above copyright
  60. + *    notice, this list of conditions and the following disclaimer.
  61. + *  o Redistributions in binary form must reproduce the above copyright
  62. + *    notice, this list of conditions and the following disclaimer in the
  63. + *    documentation and/or other materials provided with the distribution.
  64. + *  o The names of the authors may not be used to endorse or promote
  65. + *    products derived from this software without specific prior written
  66. + *    permission.
  67. + *
  68. + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  69. + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  70. + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  71. + * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  72. + * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  73. + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  74. + * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  75. + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  76. + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  77. + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  78. + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  79. + *
  80. + * @category File
  81. + * @package  File_SearchReplace
  82. + * @author   Richard Heyes <richard@phpguru.org>
  83. + * @version  CVS: $Id: SearchReplace.php,v 1.16 2008/10/27 15:48:58 clockwerx Exp $
  84. + * @link     http://pear.php.net/File_SearchReplace
  85. + */
  86.  
  87.  /**
  88.   * Search and Replace Utility
  89.   *
  90. - *
  91. - * @author  Richard Heyes <richard@phpguru.org>
  92. - * @version 1.0
  93. - * @package File
  94. + * @category File
  95. + * @package  File_SearchReplace
  96. + * @author   Richard Heyes <richard@phpguru.org>
  97. + * @link     http://pear.php.net/File_SearchReplace
  98.   */
  99.  class File_SearchReplace
  100.  {
  101. -    
  102. +
  103.      // {{{ Properties (All private)
  104.  
  105.      var $find;
  106.  -68,28 +68,32 @@
  107.      /**
  108.       * Sets up the object
  109.       *
  110. +     * @param string $find           The string/regex to find.
  111. +     * @param string $replace        The string/regex to replace $find with.
  112. +     * @param array  $files          The file(s) to perform this operation on.
  113. +     * @param array  $directories    The directories to perform this operation on.
  114. +     * @param bool   $include_subdir If performing on directories, whether to
  115. +     *                               traverse subdirectories.
  116. +     * @param array  $ignore_lines   Ignore lines beginning with any of the strings
  117. +     *                               in this array. This
  118. +     *                               feature only works with the "normal" search.
  119. +     *
  120.       * @access public
  121. -     * @param string $find                      The string/regex to find.
  122. -     * @param string $replace                   The string/regex to replace $find with.
  123. -     * @param array  $files                     The file(s) to perform this operation on.
  124. -     * @param array  $directories    (optional) The directories to perform this operation on.
  125. -     * @param bool   $include_subdir            If performing on directories, whether to traverse subdirectories.
  126. -     * @param array  $ignore_lines              Ignore lines beginning with any of the strings in this array. This
  127. -     *                                          feature only works with the "normal" search.
  128. -     */
  129. -    function File_SearchReplace($find, $replace, $files, $directories = '', $include_subdir = TRUE, $ignore_lines = array())
  130. -    {
  131. -
  132. -        $this->find            = $find;
  133. -        $this->replace         = $replace;
  134. -        $this->files           = $files;
  135. -        $this->directories     = $directories;
  136. -        $this->include_subdir  = $include_subdir;
  137. -        $this->ignore_lines    = (array) $ignore_lines;
  138. +     */
  139. +    function File_SearchReplace($find, $replace, $files, $directories = '',
  140. +                                $include_subdir = true, $ignore_lines = array())
  141. +    {
  142. +
  143. +        $this->find           = $find;
  144. +        $this->replace        = $replace;
  145. +        $this->files          = $files;
  146. +        $this->directories    = $directories;
  147. +        $this->include_subdir = $include_subdir;
  148. +        $this->ignore_lines   = (array) $ignore_lines;
  149.  
  150.          $this->occurences      = 0;
  151.          $this->search_function = 'search';
  152. -        $this->php5            = (substr(PHP_VERSION, 0, 1) == 5) ? TRUE : FALSE;
  153. +        $this->php5            = substr(PHP_VERSION, 0, 1) == 5;
  154.          $this->last_error      = '';
  155.  
  156.      }
  157.  -128,8 +132,10 @@
  158.      /**
  159.       * Accessor for setting find variable.
  160.       *
  161. -     * @access public
  162.       * @param string $find The string/regex to find.
  163. +     *
  164. +     * @access public
  165. +     * @return void
  166.       */
  167.      function setFind($find)
  168.      {
  169.  -142,8 +148,11 @@
  170.      /**
  171.       * Accessor for setting replace variable.
  172.       *
  173. +     * @param string $replace The string/regex to replace the find
  174. +     * string/regex with.
  175. +     *
  176.       * @access public
  177. -     * @param string $replace The string/regex to replace the find string/regex with.
  178. +     * @return void
  179.       */
  180.      function setReplace($replace)
  181.      {
  182.  -156,8 +165,10 @@
  183.      /**
  184.       * Accessor for setting files variable.
  185.       *
  186. -     * @access public
  187.       * @param array $files The file(s) to perform this operation on.
  188. +     *
  189. +     * @access public
  190. +     * @return void
  191.       */
  192.      function setFiles($files)
  193.      {
  194.  -170,8 +181,10 @@
  195.      /**
  196.       * Accessor for setting directories variable.
  197.       *
  198. -     * @access public
  199.       * @param array $directories The directories to perform this operation on.
  200. +     *
  201. +     * @access public
  202. +     * @return void
  203.       */
  204.      function setDirectories($directories)
  205.      {
  206.  -184,8 +197,10 @@
  207.      /**
  208.       * Accessor for setting include_subdir variable.
  209.       *
  210. -     * @access public
  211.       * @param bool $include_subdir Whether to traverse subdirectories or not.
  212. +     *
  213. +     * @access public
  214. +     * @return void
  215.       */
  216.      function setIncludeSubdir($include_subdir)
  217.      {
  218.  -198,9 +213,12 @@
  219.      /**
  220.       * Accessor for setting ignore_lines variable.
  221.       *
  222. -     * @access public
  223. -     * @param array $ignore_lines Ignore lines beginning with any of the strings in this array. This
  224. +     * @param array $ignore_lines Ignore lines beginning with any of the
  225. +     *                            strings in this array. This
  226.       *                            feature only works with the "normal" search.
  227. +     *
  228. +     * @access public
  229. +     * @return void
  230.       */
  231.      function setIgnoreLines($ignore_lines)
  232.      {
  233.  -213,34 +231,45 @@
  234.      /**
  235.       * Function to determine which search function is used.
  236.       *
  237. +     * Can be any one of:
  238. +     *  normal - Default search. Goes line by line. Ignore lines feature
  239. +     *           only works with this type.
  240. +     *  quick  - Uses str_replace for straight replacement throughout
  241. +     *           file. Quickest of the lot.
  242. +     *  preg   - Uses preg_replace(), so any valid regex
  243. +     *  ereg   - Uses ereg_replace(), so any valid regex
  244. +     *
  245. +     * @param string $search_function The search function that should be used.
  246. +     *
  247.       * @access public
  248. -     * @param string The search function that should be used. Can be any one of:
  249. -     *               normal - Default search. Goes line by line. Ignore lines feature only works with this type.
  250. -     *               quick  - Uses str_replace for straight replacement throughout file. Quickest of the lot.
  251. -     *               preg   - Uses preg_replace(), so any regex valid with this function is valid here.
  252. -     *               ereg   - Uses ereg_replace(), so any regex valid with this function is valid here.
  253. +     * @return void  
  254.       */
  255.      function setSearchFunction($search_function)
  256.      {
  257.          switch($search_function) {
  258. -        case 'normal': $this->search_function = 'search';
  259. -            return TRUE;
  260. +        case 'normal':
  261. +            $this->search_function = 'search';
  262. +            return true;
  263.              break;
  264.  
  265. -        case 'quick' : $this->search_function = 'quickSearch';
  266. -            return TRUE;
  267. +        case 'quick' :
  268. +            $this->search_function = 'quickSearch';
  269. +            return true;
  270.              break;
  271.  
  272. -        case 'preg'  : $this->search_function = 'pregSearch';
  273. -            return TRUE;
  274. +        case 'preg'  :
  275. +            $this->search_function = 'pregSearch';
  276. +            return true;
  277.              break;
  278.  
  279. -        case 'ereg'  : $this->search_function = 'eregSearch';
  280. -            return TRUE;
  281. +        case 'ereg'  :
  282. +            $this->search_function = 'eregSearch';
  283. +            return true;
  284.              break;
  285.  
  286. -        default      : $this->last_error      = 'Invalid search function specified';
  287. -            return FALSE;
  288. +        default      :
  289. +            $this->last_error = 'Invalid search function specified';
  290. +            return false;
  291.              break;
  292.          }
  293.      }
  294.  -251,10 +280,12 @@
  295.      /**
  296.       * Default ("normal") search routine.
  297.       *
  298. -     * @access private
  299.       * @param string $filename The filename to search and replace upon.
  300. -     * @return array Will return an array containing the new file contents and the number of occurences.
  301. -     *               Will return FALSE if there are no occurences.
  302. +     *
  303. +     * @access private
  304. +     * @return array Will return an array containing the new file contents
  305. +     *               and the number of occurences.
  306. +     *               Will return false if there are no occurences.
  307.       */
  308.      function search($filename)
  309.      {
  310.  -262,11 +293,12 @@
  311.          $file_array = file($filename);
  312.  
  313.          // just for the sake of catching occurences
  314. -        $local_find    = array_values((array) $this->find);
  315. -        $local_replace = (is_array($this->replace)) ? array_values($this->replace) : $this->replace;
  316. +        $local_find    = $this->_getFind();
  317. +        $local_replace = $this->_getReplace();
  318.  
  319.          if (empty($this->ignore_lines) && $this->php5) { // PHP5 acceleration
  320. -            $file_array = str_replace($local_find, $local_replace, $file_array, $occurences);
  321. +            $file_array = str_replace($local_find, $local_replace,
  322. +                                      $file_array, $occurences);
  323.  
  324.          } else { // str_replace() doesn't return number of occurences in PHP4
  325.                   // so we need to count them manually and/or filter strings
  326.  -274,34 +306,45 @@
  327.  
  328.  
  329.  
  330. -            for ($i=0; $i < count($file_array); $i++) {
  331. +            foreach ($file_array as $i => $file) {
  332.  
  333.                  if ($ignore_lines_num > 0) {
  334. -                    for ($j=0; $j < $ignore_lines_num; $j++) {
  335. -                        if (substr($file_array[$i],0,strlen($this->ignore_lines[$j])) == $this->ignore_lines[$j]) continue 2;
  336. +                    for ($j = 0; $j < $ignore_lines_num; $j++) {
  337. +                        $text = substr($file, 0, strlen($this->ignore_lines[$j]));
  338. +                        if ($text == $this->ignore_lines[$j]) {
  339. +                            continue 2;
  340. +                        }
  341.                      }
  342.                  }
  343.  
  344.                  if ($this->php5) {
  345. -                    $file_array[$i] = str_replace($this->find, $this->replace, $file_array[$i], $counted);
  346. +                    $file_array[$i] = str_replace($local_find, $local_replace,
  347. +                                                  $file, $counted);
  348. +
  349.                      $occurences += $counted;
  350.                  } else {
  351.                      foreach ($local_find as $fk => $ff) {
  352. -                        $occurences += substr_count($file_array[$i], $ff);
  353. +                        $occurences += substr_count($file, $ff);
  354.                          if (!is_array($local_replace)) {
  355.                              $fr = $local_replace;
  356.                          } else {
  357. -                            $fr = (isset($local_replace[$fk])) ? $local_replace[$fk] : "";
  358. +                            $fr = "";
  359. +                            if (isset($local_replace[$fk])) {
  360. +                                $fr = $local_replace[$fk];
  361. +                            }
  362.                          }
  363. -                        $file_array[$i] = str_replace($ff, $fr, $file_array[$i]);
  364. +                        $file_array[$i] = str_replace($ff, $fr, $file);
  365.                      }
  366.                  }
  367.              }
  368.  
  369.          }
  370. -        if ($occurences > 0) $return = array($occurences, implode('', $file_array)); else $return = FALSE;
  371. -        return $return;
  372.  
  373. +        if ($occurences > 0) {
  374. +            return array($occurences, implode('', $file_array));
  375. +        }
  376. +
  377. +        return false;
  378.      }
  379.  
  380.      // }}}
  381.  -310,21 +353,24 @@
  382.      /**
  383.       * Quick search routine.
  384.       *
  385. -     * @access private
  386.       * @param string $filename The filename to search and replace upon.
  387. -     * @return array Will return an array containing the new file contents and the number of occurences.
  388. -     *               Will return FALSE if there are no occurences.
  389. +     *
  390. +     * @access private
  391. +     * @return array Will return an array containing the new file contents
  392. +     *               and the number of occurences.
  393. +     *               Will return false if there are no occurences.
  394.       */
  395.      function quickSearch($filename)
  396.      {
  397.  
  398.          clearstatcache();
  399.  
  400. -        $file          = fread($fp = fopen($filename, 'r'), max(1, filesize($filename))); fclose($fp);
  401. -        $local_find    = array_values((array) $this->find);
  402. -        $local_replace = (is_array($this->replace)) ? array_values($this->replace) : $this->replace;
  403. +        $file = file_get_contents($filename);
  404. +
  405. +        $local_find    = $this->_getFind();
  406. +        $local_replace = $this->_getReplace();
  407.  
  408. -        $occurences    = 0;
  409. +        $occurences = 0;
  410.  
  411.          // logic is the same as in str_replace function with one exception:
  412.          //   if <search> is a string and <replacement> is an array - substitution
  413.  -335,6 +381,7 @@
  414.  
  415.          if ($this->php5) {
  416.              $file = str_replace($this->find, $this->replace, $file, $counted);
  417. +
  418.              $occurences += $counted;
  419.          } else {
  420.              foreach ($local_find as $fk => $ff) {
  421.  -342,14 +389,17 @@
  422.                  if (!is_array($local_replace)) {
  423.                      $fr = $local_replace;
  424.                  } else {
  425. -                    $fr = (isset($local_replace[$fk])) ? $local_replace[$fk] : "";
  426. +                    $fr = isset($local_replace[$fk]) ? $local_replace[$fk] : "";
  427.                  }
  428.                  $file = str_replace($ff, $fr, $file);
  429.              }
  430.          }
  431.  
  432. -        if ($occurences > 0) $return = array($occurences, $file); else $return = FALSE;
  433. -        return $return;
  434. +        if ($occurences > 0) {
  435. +            return array($occurences, $file);
  436. +        }
  437. +
  438. +        return false;
  439.  
  440.      }
  441.  
  442.  -359,34 +409,40 @@
  443.      /**
  444.       * Preg search routine.
  445.       *
  446. -     * @access private
  447.       * @param string $filename The filename to search and replace upon.
  448. -     * @return array Will return an array containing the new file contents and the number of occurences.
  449. -     *               Will return FALSE if there are no occurences.
  450. +     *
  451. +     * @access private
  452. +     * @return array Will return an array containing the new file contents
  453. +     *               and the number of occurences.
  454. +     *               Will return false if there are no occurences.
  455.       */
  456.      function pregSearch($filename)
  457.      {
  458.  
  459.          clearstatcache();
  460.  
  461. -        $file       = fread($fp = fopen($filename, 'r'), max(1, filesize($filename))); fclose($fp);
  462. -        $local_find    = array_values((array) $this->find);
  463. -        $local_replace = (is_array($this->replace)) ? array_values($this->replace) : $this->replace;
  464. +        $file = file_get_contents($filename);
  465. +
  466. +        $local_find    = $this->_getFind();
  467. +        $local_replace = $this->_getReplace();
  468.  
  469.          $occurences = 0;
  470.  
  471. -        foreach($local_find as $fk => $ff) {
  472. +        foreach ($local_find as $fk => $ff) {
  473.              $occurences += preg_match_all($ff, $file, $matches);
  474.              if (!is_array($local_replace)) {
  475.                  $fr = $local_replace;
  476.              } else {
  477. -                $fr = (isset($local_replace[$fk])) ? $local_replace[$fk] : "";
  478. +                $fr = isset($local_replace[$fk]) ? $local_replace[$fk] : "";
  479.              }
  480.              $file = preg_replace($ff, $fr, $file);
  481.          }
  482.  
  483. -        if ($occurences > 0) $return = array($occurences, $file); else $return = FALSE;
  484. -        return $return;
  485. +        if ($occurences > 0) {
  486. +            return array($occurences, $file);
  487. +        }
  488. +
  489. +        return false;
  490.  
  491.      }
  492.  
  493.  -396,54 +452,61 @@
  494.      /**
  495.       * Ereg search routine.
  496.       *
  497. -     * @access private
  498.       * @param string $filename The filename to search and replace upon.
  499. -     * @return array Will return an array containing the new file contents and the number of occurences.
  500. -     *               Will return FALSE if there are no occurences.
  501. +     *
  502. +     * @access private
  503. +     * @return array Will return an array containing the new file contents
  504. +     *               and the number of occurences.
  505. +     *               Will return false if there are no occurences.
  506.       */
  507.      function eregSearch($filename)
  508.      {
  509.  
  510.          clearstatcache();
  511.  
  512. -        $file = fread($fp = fopen($filename, 'r'), max(1, filesize($filename))); fclose($fp);
  513. -        $local_find    = array_values((array) $this->find);
  514. -        $local_replace = (is_array($this->replace)) ? array_values($this->replace) : $this->replace;
  515. +        $file = file_get_contents($filename);
  516. +
  517. +        $local_find    = $this->_getFind();
  518. +        $local_replace = $this->_getReplace();
  519.  
  520.          $occurences = 0;
  521.  
  522. -        foreach($local_find as $fk => $ff) {
  523. +        foreach ($local_find as $fk => $ff) {
  524.              $occurences += count(split($ff, $file)) - 1;
  525.              if (!is_array($local_replace)) {
  526.                  $fr = $local_replace;
  527.              } else {
  528. -                $fr = (isset($local_replace[$fk])) ? $local_replace[$fk] : "";
  529. +                $fr = isset($local_replace[$fk]) ? $local_replace[$fk] : "";
  530.              }
  531.              $file = ereg_replace($ff, $fr, $file);
  532.          }
  533.  
  534. -        if ($occurences > 0) $return = array($occurences, $file); else $return = FALSE;
  535. -        return $return;
  536. +        if ($occurences > 0) {
  537. +            return array($occurences, $file);
  538. +        }
  539.  
  540. +        return false;
  541.      }
  542.  
  543.      // }}}
  544.      // {{{ writeout()
  545. -    
  546. +
  547.      /**
  548.       * Function to writeout the file contents.
  549.       *
  550. -     * @access private
  551.       * @param string $filename The filename of the file to write.
  552.       * @param string $contents The contents to write to the file.
  553. +     *
  554. +     * @access private
  555. +     * @return void
  556.       */
  557.      function writeout($filename, $contents)
  558.      {
  559.  
  560.          if ($fp = @fopen($filename, 'w')) {
  561. -            flock($fp,2);
  562. +            flock($fp, 2);
  563.              fwrite($fp, $contents);
  564. -            flock($fp,3);
  565. +            flock($fp, 3);
  566.              fclose($fp);
  567.          } else {
  568.              $this->last_error = 'Could not open file: '.$filename;
  569.  -457,18 +520,29 @@
  570.      /**
  571.       * Function called by doSearch() to go through any files that need searching.
  572.       *
  573. -     * @access private
  574.       * @param string $ser_func The search function to use.
  575. +     *
  576. +     * @access private
  577. +     * @return void
  578.       */
  579.      function doFiles($ser_func)
  580.      {
  581. -        if (!is_array($this->files)) $this->files = explode(',', $this->files);
  582. -        for ($i=0; $i<count($this->files); $i++) {
  583. -            if ($this->files[$i] == '.' OR $this->files[$i] == '..') continue;
  584. -            if (is_dir($this->files[$i]) == TRUE) continue;
  585. -            $newfile = $this->$ser_func($this->files[$i]);
  586. -            if (is_array($newfile) == TRUE){
  587. -                $this->writeout($this->files[$i], $newfile[1]);
  588. +        if (!is_array($this->files)) {
  589. +            $this->files = explode(',', $this->files);
  590. +        }
  591. +
  592. +        foreach ($this->files as $file) {
  593. +            if ($file == '.' OR $file == '..') {
  594. +                continue;
  595. +            }
  596. +
  597. +            if (is_dir($file)) {
  598. +                continue;
  599. +            }
  600. +
  601. +            $newfile = $this->$ser_func($file);
  602. +            if (is_array($newfile)) {
  603. +                $this->writeout($file, $newfile[1]);
  604.                  $this->occurences += $newfile[0];
  605.              }
  606.          }
  607.  -478,31 +552,39 @@
  608.      // {{{ doDirectories()
  609.  
  610.      /**
  611. -     * Function called by doSearch() to go through any directories that need searching.
  612. +     * Function called by doSearch() to go through any directories that
  613. +     * need searching.
  614.       *
  615. -     * @access private
  616.       * @param string $ser_func The search function to use.
  617. +     *
  618. +     * @access private
  619. +     * @return void
  620.       */
  621.      function doDirectories($ser_func)
  622.      {
  623. -        if (!is_array($this->directories)) $this->directories = explode(',', $this->directories);
  624. -        for ($i=0; $i<count($this->directories); $i++) {
  625. -            $dh = opendir($this->directories[$i]);
  626. +        if (!is_array($this->directories)) {
  627. +            $this->directories = explode(',', $this->directories);
  628. +        }
  629. +
  630. +        foreach ($this->directories as $directory) {
  631. +            $dh = opendir($directory);
  632.              while ($file = readdir($dh)) {
  633. -                if ($file == '.' OR $file == '..') continue;
  634. +                if ($file == '.' OR $file == '..') {
  635. +                    continue;
  636. +                }
  637.  
  638. -                if (is_dir($this->directories[$i].$file) == TRUE) {
  639. -                    if ($this->include_subdir == TRUE) {
  640. -                        $this->directories[] = $this->directories[$i].$file.'/';
  641. +                if (is_dir($directory.$file) == true) {
  642. +                    if ($this->include_subdir == true) {
  643. +                        $this->directories[] = $directory.$file.'/';
  644.                          continue;
  645.                      } else {
  646.                          continue;
  647.                      }
  648.                  }
  649.  
  650. -                $newfile = $this->$ser_func($this->directories[$i].$file);
  651. -                if (is_array($newfile) == TRUE) {
  652. -                    $this->writeout($this->directories[$i].$file, $newfile[1]);
  653. +                $newfile = $this->$ser_func($directory.$file);
  654. +                if (is_array($newfile) == true) {
  655. +                    $this->writeout($directory.$file, $newfile[1]);
  656.                      $this->occurences += $newfile[0];
  657.                  }
  658.              }
  659.  -511,39 +593,72 @@
  660.  
  661.      // }}}
  662.      // {{{ doSearch()
  663. -    
  664. +
  665.      /**
  666.       * This starts the search/replace off. The behavior of this function will likely
  667.       * to be changed in future versions to work in read only mode. If you want to do
  668. -     * actual replace with writing files - use doReplace method instead.
  669. +     * actual replace with writing files - use doReplace method instead.
  670.       *
  671.       * @access public
  672. +     * @return void
  673.       */
  674.      function doSearch()
  675.      {
  676.          $this->doReplace();
  677.      }
  678. -    
  679. +
  680.      // }}}
  681.      // {{{ doReplace()
  682. -    
  683. +
  684.      /**
  685.       * This starts the search/replace off. Call this to do the replace.
  686.       * First do whatever files are specified, and/or if directories are specified,
  687.       * do those too.
  688.       *
  689.       * @access public
  690. +     * @return void
  691.       */
  692.      function doReplace()
  693.      {
  694.          $this->occurences = 0;
  695. -        if ($this->find != '') {
  696. -            if ((is_array($this->files) AND count($this->files) > 0) OR $this->files != '') $this->doFiles($this->search_function);
  697. -            if ($this->directories != '')                                                   $this->doDirectories($this->search_function);
  698. +        if (!empty($this->find)) {
  699. +            if (!empty($this->files)) {
  700. +                $this->doFiles($this->search_function);
  701. +            }
  702. +
  703. +            if (!empty($this->directories)) {
  704. +                $this->doDirectories($this->search_function);
  705. +            }
  706.          }
  707.      }
  708. -    
  709. +
  710.      // }}}
  711.  
  712. +
  713. +    /**
  714. +     * Helper method to ensure we always have an array of things to find.
  715. +     *
  716. +     * @access private
  717. +     * @return array
  718. +     */
  719. +    function _getFind()
  720. +    {
  721. +        return array_values((array) $this->find);
  722. +    }
  723. +
  724. +    /**
  725. +     * Helper method to fetch replace
  726. +     *
  727. +     * @access private
  728. +     * @return mixed
  729. +     */
  730. +    function _getReplace()
  731. +    {
  732. +        if (is_array($this->replace)) {
  733. +            return array_values($this->replace);
  734. +        }
  735. +
  736. +        return $this->replace;
  737. +    }
  738.  }
  739.  ?>
clone this paste RAW Paste Data