PageRenderTime 47ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

/wp-content/plugins/backupbuddy/lib/textreplacebuddy/textreplacebuddy.php

https://bitbucket.org/anneivycat/california-tour
PHP | 318 lines | 133 code | 71 blank | 114 comment | 32 complexity | 779830f6d89a823cd049e0645301ea7f MD5 | raw file
  1. <?php
  2. /* pb_backupbuddy_textreplacebuddy class
  3. *
  4. * @author Dustin Bolton < http://dustinbolton.com >
  5. * @since 3.0.0
  6. *
  7. * Text replacements using command line if available. There is no good way to do text replacements
  8. * on a large scale in PHP efficiently.
  9. *
  10. * Resulting file stored at $file . '.tmp'. Original file is NOT replaced.
  11. *
  12. */
  13. class pb_backupbuddy_textreplacebuddy {
  14. /********** Properties **********/
  15. private $_version = '0.0.2'; // Internal version number for this library.
  16. private $_methods = array(); // Available mechanisms for dumping in order of preference.
  17. private $_commandbuddy;
  18. /********** Methods **********/
  19. /* __construct()
  20. *
  21. * Default constructor.
  22. *
  23. * @param array $force_methods Optional parameter to override automatic method detection. Skips test and runs first method first. Falls back to other methods if any failure.
  24. * @return
  25. */
  26. public function __construct( $force_methods = array() ) {
  27. pb_backupbuddy::status( 'details', 'textreplacebuddy: Loading textreplacebuddy library.' );
  28. // Handles command line execution.
  29. require_once( pb_backupbuddy::plugin_path() . '/lib/commandbuddy/commandbuddy.php' );
  30. $this->_commandbuddy = new pb_backupbuddy_commandbuddy();
  31. // Set mechanism for dumping / restoring.
  32. if ( count( $force_methods ) > 0 ) { // Mechanism forced. Overriding automatic check.
  33. pb_backupbuddy::status( 'message', 'textreplacebuddy: Settings overriding automatic detection of available database dump methods. Using forced override methods: `' . implode( ',', $force_methods ) . '`.' );
  34. $this->_methods = $force_methods;
  35. } else { // No method defined; auto-detect the best.
  36. $this->_methods = $this->available_textreplace_methods();
  37. }
  38. pb_backupbuddy::status( 'message', 'textreplacebuddy: Detected text replacement methods: `' . implode( ',', $this->_methods ) . '`.' );
  39. } // End __construct().
  40. /* available_dump_methods()
  41. *
  42. * function description
  43. *
  44. * @param
  45. * @return string Possible returns: mysqldump, php
  46. */
  47. public function available_textreplace_methods() {
  48. pb_backupbuddy::status( 'details', 'textreplace test: Testing available text replacement methods.' );
  49. if ( function_exists( 'exec' ) ) { // Exec is available so test mysqldump from here.
  50. pb_backupbuddy::status( 'details', 'textreplace test: exec() function exists. Testing running sed (text replace command) via exec().' );
  51. /********** Begin preparing command **********/
  52. // Handle Windows wanting .exe.
  53. if ( stristr( PHP_OS, 'WIN' ) && !stristr( PHP_OS, 'DARWIN' ) ) { // Running Windows. (not darwin)
  54. return array( 'php' ); // Nothing good in Windows available to text replace.
  55. } else {
  56. $command = "echo backup | sed 's/backup/buddy/'"; // Will attempt to replace backup with buddy
  57. }
  58. // Redirect STDERR to STDOUT.
  59. $command .= ' 2>&1';
  60. /********** End preparing command **********/
  61. // Run command.
  62. pb_backupbuddy::status( 'details', 'textreplace test: Running test command next.' );
  63. list( $exec_output, $exec_exit_code ) = $this->_commandbuddy->execute( $command );
  64. if ( stristr( implode( ' ', $exec_output ), 'buddy' ) !== false ) { // String Ver appeared in response or usage explanations. Some version dont give version.
  65. pb_backupbuddy::status( 'details', 'textreplace test: Command appears to be accessible and returns expected response.' );
  66. return array( 'commandline', 'php' ); // mysqldump best, php next.
  67. }
  68. } else { // No exec() so must fall back to PHP method only.
  69. pb_backupbuddy::status( 'message', 'textreplace test: Falling back to textreplace compatibility mode (PHP replace). This is slower and more memory intensive.' );
  70. return array( 'php' );
  71. }
  72. return array( 'php' );
  73. } // End available_dump_method().
  74. /* replace()
  75. *
  76. * Replace all instances of a string within a file. Automatically falls back.
  77. * Resulting file stored in $file . 'tmp'. Original file is NOT replaced.
  78. *
  79. * @param string $file Full file path to file to search. A .tmp version is made with the final results. Original file is NOT replaced.
  80. * @param string $search String to search for.
  81. * @param string $replacement String to replace with.
  82. * @param string $regex_condition Condition when replacements may happen. Optional.
  83. * @param string $regex_replace If fallen back into PHP mode then this is needed IF $regex_condition is passed. Optional.
  84. * @return
  85. */
  86. public function string_replace( $file, $search, $replacement, $regex_condition = '', $regex_replace = '' ) {
  87. $return = false;
  88. pb_backupbuddy::status( 'message', 'Starting text replacement procedure.' );
  89. pb_backupbuddy::status( 'details', "textreplace being performed on file `{$file}` replacing `{$search}` with `{$replacement}`." );
  90. // Attempt each method in order.
  91. pb_backupbuddy::status( 'details', 'Preparing to textreplace using available method(s) by priority. Methods: `' . implode( ',', $this->_methods ) . '`' );
  92. foreach( $this->_methods as $method ) {
  93. if ( method_exists( $this, "_stringreplace_{$method}" ) ) {
  94. pb_backupbuddy::status( 'details', 'textreplacebuddy: Attempting replace method `' . $method . '`.' );
  95. $result = call_user_func( array( $this, "_stringreplace_{$method}" ), $file, $search, $replacement, $regex_condition, $regex_replace );
  96. if ( $result === true ) { // Dump completed succesfully with this method.
  97. pb_backupbuddy::status( 'details', 'textreplacebuddy: Replace method `' . $method . '` completed successfully.' );
  98. $return = true;
  99. break;
  100. } elseif ( $result === false ) { // Dump failed this method. Will try compatibility fallback to next mode if able.
  101. // Do nothing. Will try next mode next loop.
  102. pb_backupbuddy::status( 'details', 'textreplacebuddy: Replace method `' . $method . '` failed. Trying another compatibility mode next if able.' );
  103. } else {
  104. pb_backupbuddy::status( 'details', 'textreplacebuddy: Unexepected response: `' . implode( ',', $result ) . '`' );
  105. }
  106. }
  107. }
  108. if ( $return === true ) { // Success.
  109. pb_backupbuddy::status( 'message', 'Text replace procedure succeeded.' );
  110. return true;
  111. } else { // Overall failure.
  112. pb_backupbuddy::status( 'error', 'Text replace procedure failed.' );
  113. return false;
  114. }
  115. } // End dump().
  116. /* _stringreplace_commandline()
  117. *
  118. * Performs actual command line string replacement. VIA sed (command line replace).
  119. * Case sensitive.
  120. * Resulting file stored in $file . 'tmp'. Original file is NOT replaced.
  121. *
  122. * @param string $file Full file path to file to replace in. a .tmp version is temporarily made.
  123. * @param string $search String to search for.
  124. * @param string $replacement String to replace with.
  125. * @param string $regex_condition Condition when replacements may happen. Optional.
  126. * @param string $regex_replace NOT used in commandline mode.
  127. * @param boolean $global Whether or not to globally replace in regex. Default: false (only replace first instance per line).
  128. * @return boolean True on success; else false.
  129. */
  130. private function _stringreplace_commandline( $file, $search, $replacement, $regex_condition = '', $regex_replace = '', $global = false ) {
  131. pb_backupbuddy::status( 'details', 'textreplacebuddy: Preparing to run command line sed (replacement comment) via exec().' );
  132. // Handle optional global replacement.
  133. if ( $global === true ) {
  134. $global_flag = 'g';
  135. pb_backupbuddy::status( 'details', 'textreplacebuddy: Using global replace per line.' );
  136. } else {
  137. $global_flag = '';
  138. pb_backupbuddy::status( 'details', 'textreplacebuddy: Using first instance replace per line.' );
  139. }
  140. /********** Begin preparing command **********/
  141. // Handle Windows wanting .exe.
  142. if ( stristr( PHP_OS, 'WIN' ) && !stristr( PHP_OS, 'DARWIN' ) ) { // Running Windows. (not darwin)
  143. return false;
  144. } else {
  145. if ( $regex_condition == '' ) { // Normal string replace.
  146. $command = "sed s/{$search}/{$replacement}/{$global_flag} {$file} > {$file}.tmp";
  147. } else { // Custom regex conditions.
  148. $command = "sed -E '/{$regex_condition}/s/{$search}/{$replacement}/{$global_flag}' {$file} > {$file}.tmp";
  149. }
  150. }
  151. // Redirect STDERR to STDOUT.
  152. $command .= ' 2>&1';
  153. /********** End preparing command **********/
  154. // Run command.
  155. pb_backupbuddy::status( 'details', 'textreplacebuddy: Running replace command via exec next.' );
  156. list( $exec_output, $exec_exit_code ) = $this->_commandbuddy->execute( $command );
  157. // Check the result.
  158. if ( $exec_exit_code == '0' ) {
  159. pb_backupbuddy::status( 'details', 'textreplacebuddy: Command appears to succeeded and returned proper response.' );
  160. if ( file_exists( $file . '.tmp' ) ) { // Temp replacement file found. SUCCESS!
  161. pb_backupbuddy::status( 'message', 'textreplacebuddy: Temporary text replacement file creation verified.' );
  162. /*
  163. if ( true === rename( $file . '.tmp', $file ) ) {
  164. pb_backupbuddy::status( 'message', 'textreplacebuddy: Temporary moved back to original file. Success.' );
  165. return true;
  166. } else {
  167. pb_backupbuddy::status( 'error', 'textreplacebuddy: Temporary could not be moved back to original file. Failure. Verify permissions.' );
  168. return false;
  169. }
  170. */
  171. return true;
  172. } else { // SQL file MISSING. FAILURE!
  173. pb_backupbuddy::status( 'error', 'textreplacebuddy: Though command reported success temporary replacement file is missing: `' . $output_file . '`.' );
  174. return false;
  175. }
  176. } else {
  177. pb_backupbuddy::status( 'error', 'textreplacebuddy: Command did not exit normally. Falling back to text replacement compatibility modes.' );
  178. return false;
  179. }
  180. // Should never get to here.
  181. pb_backupbuddy::status( 'error', 'textreplacebuddy: Uncaught exception #45323890.' );
  182. return false;
  183. } // End _stringreplace_commandline().
  184. /* _stringreplace_php()
  185. *
  186. * Performs actual command line string replacement. VIA PHP.
  187. * Case sensitive.
  188. * Resulting file stored in $file . 'tmp'.
  189. *
  190. * @param string $file Full file path to file to replace in. a .tmp version is temporarily made.
  191. * @param string $search String to search for.
  192. * @param string $replacement String to replace with.
  193. * @param string $regex_condition Condition when replacements may happen. Optional.
  194. * @param string $regex_replace If fallen back into PHP mode then this is needed IF $regex_condition is passed. Optional.
  195. * @return boolean True on success; else false.
  196. */
  197. private function _stringreplace_php( $file, $search, $replacement, $regex_condition = '', $regex_replace = '' ) {
  198. pb_backupbuddy::status( 'details', 'textreplacebuddy: Starting comptibility mode PHP text replacement.' );
  199. $file_o = fopen( $file, 'r' );
  200. $temp = $file . '.tmp';
  201. if ( !is_writable( $temp ) ) {
  202. pb_backupbuddy::status( 'error', 'textreplacebuddy: Permission denied writing temporary file `' . $temp . '`.' );
  203. return false;
  204. }
  205. if ( is_resource( $file_o ) === true ) {
  206. while ( feof( $file_o ) === false ) {
  207. if ( $regex_condition != '' ) { // regex
  208. $content = preg_replace( "/^{$regex_condition}/i", $regex_replace, fgets( $file_o ) );
  209. } else { // standard string
  210. $content = str_replace( $search, $replacement, fgets( $file_o ) );
  211. }
  212. file_put_contents( $temp, $content, FILE_APPEND );
  213. }
  214. fclose( $file_o );
  215. }
  216. //unlink($file);
  217. /*
  218. $result = rename( $temp, $file );
  219. if ( $result === false ) {
  220. pb_backupbuddy::status( 'error', 'textreplacebuddy: Unable to move temporary file back to original file. Failure.' );
  221. } else {
  222. pb_backupbuddy::status( 'details', 'textreplacebuddy: Moved temporary file back to original file. Success.' );
  223. }
  224. */
  225. return $result;
  226. } // End _stringreplace_php().
  227. /* get_methods()
  228. *
  229. * Get an array of methods. Note: If force overriding methods then detected methods will not be able to display.
  230. *
  231. * @return array Array of methods.
  232. */
  233. public function get_methods() {
  234. return $this->_methods;
  235. } // End get_methods().
  236. /* set_methods()
  237. *
  238. * Set methods. Overrides detected.
  239. *
  240. * @param array $methods Array of methods to set.
  241. * @return null
  242. */
  243. public function set_methods( $methods = array() ) {
  244. $this->_methods = $methods;
  245. } // End set_methods().
  246. } // End pb_backupbuddy_mysqlbuddy class.
  247. ?>