PageRenderTime 1032ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

/df_home/static/test/portalbkd/wp-content/plugins/envato-wordpress-toolkit/includes/class-envato-backup.php

https://gitlab.com/darmawan.fatria/df-skp-2014
PHP | 901 lines | 306 code | 200 blank | 395 comment | 90 complexity | 40e3bbb1b415f4f2c19ba9a2857780ea MD5 | raw file
  1. <?php if ( ! defined( 'EWPT_PLUGIN_VER') ) exit( 'No direct script access allowed' );
  2. /**
  3. * Envato file backup class
  4. *
  5. * Original class created by Human Made Limited http://hmn.md/. This version has
  6. * removed database backups and altered namespacing for inclusion in the
  7. * Envato WordPress Toolkit plugin to prevent conflicts.
  8. *
  9. * @package WordPress
  10. * @subpackage Envato WordPress Toolkit
  11. * @author Derek Herman <derek@envato.com>, Human Made Limited
  12. * @since 1.4
  13. */
  14. if ( ! class_exists( 'Envato_Backup' ) ) {
  15. class Envato_Backup {
  16. /**
  17. * The path where the backup file should be saved
  18. *
  19. * @access public
  20. * @since 1.4
  21. *
  22. * @var string
  23. */
  24. public $path;
  25. /**
  26. * The filename of the backup file
  27. *
  28. * @access public
  29. * @since 1.4
  30. *
  31. * @var string
  32. */
  33. public $archive_filename;
  34. /**
  35. * The path to the zip command
  36. *
  37. * @access public
  38. * @since 1.4
  39. *
  40. * @var string
  41. */
  42. public $zip_command_path;
  43. /**
  44. * An array of exclude rules
  45. *
  46. * @access public
  47. * @since 1.4
  48. *
  49. * @var array
  50. */
  51. public $excludes;
  52. /**
  53. * The path that should be backed up
  54. *
  55. * @access public
  56. * @since 1.4
  57. *
  58. * @var string
  59. */
  60. public $root;
  61. /**
  62. * Store the current backup instance
  63. *
  64. * @access public
  65. * @since 1.4
  66. *
  67. * @var object
  68. * @static
  69. */
  70. private static $instance;
  71. /**
  72. * An array of all the files in root
  73. * excluding excludes
  74. *
  75. * @access private
  76. * @since 1.4
  77. *
  78. * @var array
  79. */
  80. private $files;
  81. /**
  82. * Contains an array of error
  83. *
  84. * @access private
  85. * @since 1.4
  86. *
  87. * @var mixed
  88. */
  89. private $errors;
  90. /**
  91. * Contains an array of warnings
  92. *
  93. * @access private
  94. * @since 1.4
  95. *
  96. * @var mixed
  97. */
  98. private $warnings;
  99. /**
  100. * The archive method used
  101. *
  102. * @access private
  103. * @since 1.4
  104. *
  105. * @var string
  106. */
  107. private $archive_method;
  108. /**
  109. * PHP5 constructor method.
  110. *
  111. * Sets up the default properties
  112. *
  113. * @access public
  114. * @since 1.4
  115. *
  116. * @return null
  117. */
  118. public function __construct() {
  119. /* Raise the memory limit and max_execution_time time */
  120. @ini_set( 'memory_limit', apply_filters( 'admin_memory_limit', WP_MAX_MEMORY_LIMIT ) );
  121. @set_time_limit( 0 );
  122. $this->errors = array();
  123. set_error_handler( array( $this, 'error_handler' ) );
  124. /* Defaults */
  125. $this->root = $this->conform_dir( ABSPATH );
  126. $this->path = $this->conform_dir( WP_CONTENT_DIR . '/envato-backups' );
  127. $this->archive_filename = strtolower( sanitize_file_name( get_bloginfo( 'name' ) . '.backup.' . date( 'Y-m-d-H-i-s', time() + ( current_time( 'timestamp' ) - time() ) ) . '.zip' ) );
  128. $this->zip_command_path = $this->guess_zip_command_path();
  129. }
  130. /**
  131. * Return the current instance
  132. *
  133. * @access public
  134. * @since 1.4
  135. *
  136. * @static
  137. * @return object
  138. */
  139. public static function get_instance() {
  140. if ( empty( self::$instance ) )
  141. self::$instance = new Envato_Backup();
  142. return self::$instance;
  143. }
  144. /**
  145. * The full filepath to the archive file.
  146. *
  147. * @access public
  148. * @since 1.4
  149. *
  150. * @return string
  151. */
  152. public function archive_filepath() {
  153. return trailingslashit( $this->path() ) . $this->archive_filename();
  154. }
  155. /**
  156. * Helper function to sanitize archive filename.
  157. *
  158. * @access public
  159. * @since 1.4
  160. *
  161. * @return string
  162. */
  163. public function archive_filename() {
  164. return strtolower( sanitize_file_name( remove_accents( $this->archive_filename ) ) );
  165. }
  166. /**
  167. * Helper function to sanitize the root directory path.
  168. *
  169. * @access public
  170. * @since 1.4
  171. *
  172. * @return string
  173. */
  174. public function root() {
  175. return $this->conform_dir( $this->root );
  176. }
  177. /**
  178. * Helper function to sanitize the archive directory path.
  179. *
  180. * @access public
  181. * @since 1.4
  182. *
  183. * @return string
  184. */
  185. public function path() {
  186. return $this->conform_dir( $this->path );
  187. }
  188. /**
  189. * Helper function to return the archive method.
  190. *
  191. * @access public
  192. * @since 1.4
  193. *
  194. * @return string
  195. */
  196. public function archive_method() {
  197. return $this->archive_method;
  198. }
  199. /**
  200. * Kick off a backup
  201. *
  202. * @access public
  203. * @since 1.4
  204. *
  205. * @return bool
  206. */
  207. public function backup() {
  208. do_action( 'envato_backup_started', $this );
  209. /* Zip everything up */
  210. $this->archive();
  211. do_action( 'envato_backup_complete', $this );
  212. }
  213. /**
  214. * Zip up all the files.
  215. *
  216. * Attempts to use the shell zip command, if
  217. * thats not available then it fallsback to
  218. * PHP ZipArchive and finally PclZip.
  219. *
  220. * @access public
  221. * @since 1.4
  222. *
  223. * @return null
  224. */
  225. public function archive() {
  226. do_action( 'envato_archive_started' );
  227. /* Do we have the path to the zip command */
  228. if ( $this->zip_command_path )
  229. $this->zip();
  230. /* If not or if the shell zip failed then use ZipArchive */
  231. if ( empty( $this->archive_verified ) && class_exists( 'ZipArchive' ) && empty( $this->skip_zip_archive ) )
  232. $this->zip_archive();
  233. /* If ZipArchive is unavailable or one of the above failed */
  234. if ( empty( $this->archive_verified ) )
  235. $this->pcl_zip();
  236. do_action( 'envato_archive_finished' );
  237. }
  238. /**
  239. * Zip using the native zip command
  240. *
  241. * @access public
  242. * @since 1.4
  243. *
  244. * @return null
  245. */
  246. public function zip() {
  247. $this->archive_method = 'zip';
  248. $this->warning( $this->archive_method, shell_exec( 'cd ' . escapeshellarg( $this->root() ) . ' && ' . escapeshellarg( $this->zip_command_path ) . ' -rq ' . escapeshellarg( $this->archive_filepath() ) . ' ./' . ' 2>&1' ) );
  249. $this->check_archive();
  250. }
  251. /**
  252. * Fallback for creating zip archives if zip command is unnavailable.
  253. *
  254. * @access public
  255. * @since 1.4
  256. *
  257. * @param string $path
  258. * @return null
  259. */
  260. public function zip_archive() {
  261. $this->errors_to_warnings( $this->archive_method );
  262. $this->archive_method = 'ziparchive';
  263. if ( ! class_exists( 'ZipArchive' ) )
  264. return;
  265. $zip = new ZipArchive();
  266. if ( ! $zip->open( $this->archive_filepath(), ZIPARCHIVE::CREATE ) )
  267. return;
  268. $files_added = 0;
  269. foreach ( $this->files() as $file ) {
  270. if ( is_dir( trailingslashit( $this->root() ) . $file ) )
  271. $zip->addEmptyDir( trailingslashit( $file ) );
  272. elseif ( is_file( trailingslashit( $this->root() ) . $file ) )
  273. $zip->addFile( trailingslashit( $this->root() ) . $file, $file );
  274. if ( ++$files_added % 500 === 0 )
  275. if ( ! $zip->close() || ! $zip->open( $this->archive_filepath(), ZIPARCHIVE::CREATE ) )
  276. return;
  277. }
  278. if ( $zip->status )
  279. $this->warning( $this->archive_method, $zip->status );
  280. if ( $zip->statusSys )
  281. $this->warning( $this->archive_method, $zip->statusSys );
  282. $zip->close();
  283. $this->check_archive();
  284. }
  285. /**
  286. * Fallback for creating zip archives if both zip command
  287. * and ZipArchive are unnavailable.
  288. *
  289. * Uses the PclZip library that ships with WordPress
  290. *
  291. * @access public
  292. * @since 1.4
  293. *
  294. * @param string $path
  295. * @return null
  296. */
  297. public function pcl_zip() {
  298. $this->errors_to_warnings( $this->archive_method );
  299. $this->archive_method = 'pclzip';
  300. global $_envato_backup_exclude_string;
  301. $_envato_backup_exclude_string = $this->exclude_string( 'regex' );
  302. $this->load_pclzip();
  303. $archive = new PclZip( $this->archive_filepath() );
  304. /* Zip up everything */
  305. if ( ! $archive->add( $this->root(), PCLZIP_OPT_REMOVE_PATH, $this->root(), PCLZIP_CB_PRE_ADD, array( $this, 'pcl_zip_callback' ) ) )
  306. $this->warning( $this->archive_method, $archive->errorInfo( true ) );
  307. unset( $GLOBALS['_envato_backup_exclude_string'] );
  308. $this->check_archive();
  309. }
  310. /**
  311. * Add file callback for PclZip, excludes files
  312. * and sets the database dump to be stored in the root
  313. * of the zip
  314. *
  315. * @access private
  316. * @since 1.4
  317. * @since 1.7.2 (moved into class)
  318. *
  319. * @param string $event
  320. * @param array &$file
  321. * @return bool
  322. */
  323. function pcl_zip_callback( $event, &$file ) {
  324. global $_envato_backup_exclude_string;
  325. /* Don't try to add unreadable files. */
  326. if ( ! is_readable( $file['filename'] ) || ! file_exists( $file['filename'] ) )
  327. return false;
  328. /* Match everything else past the exclude list */
  329. elseif ( $_envato_backup_exclude_string && preg_match( '(' . $_envato_backup_exclude_string . ')', $file['stored_filename'] ) )
  330. return false;
  331. return true;
  332. }
  333. /**
  334. * Verify that the archive is valid and contains all the files it should contain.
  335. *
  336. * @access public
  337. * @since 1.4
  338. *
  339. * @return bool
  340. */
  341. public function check_archive() {
  342. /* If we've already passed then no need to check again */
  343. if ( ! empty( $this->archive_verified ) )
  344. return true;
  345. if ( ! file_exists( $this->archive_filepath() ) )
  346. $this->error( $this->archive_method(), __( 'The backup file was not created', 'envato-wordpress-toolkit' ) );
  347. /* Verify using the zip command if possible */
  348. if ( $this->zip_command_path ) {
  349. $verify = shell_exec( escapeshellarg( $this->zip_command_path ) . ' -T ' . escapeshellarg( $this->archive_filepath() ) . ' 2> /dev/null' );
  350. if ( strpos( $verify, 'OK' ) === false )
  351. $this->error( $this->archive_method(), $verify );
  352. }
  353. /* If there are errors delete the backup file. */
  354. if ( $this->errors( $this->archive_method() ) && file_exists( $this->archive_filepath() ) )
  355. unlink( $this->archive_filepath() );
  356. if ( $this->errors( $this->archive_method() ) )
  357. return false;
  358. return $this->archive_verified = true;
  359. }
  360. /**
  361. * Generate the array of files to be backed up by looping through
  362. * root, ignored unreadable files and excludes
  363. *
  364. * @access public
  365. * @since 1.4
  366. *
  367. * @return array
  368. */
  369. public function files() {
  370. if ( ! empty( $this->files ) )
  371. return $this->files;
  372. $this->files = array();
  373. if ( defined( 'RecursiveDirectoryIterator::FOLLOW_SYMLINKS' ) ) {
  374. $filesystem = new RecursiveIteratorIterator( new RecursiveDirectoryIterator( $this->root(), RecursiveDirectoryIterator::FOLLOW_SYMLINKS ), RecursiveIteratorIterator::SELF_FIRST, RecursiveIteratorIterator::CATCH_GET_CHILD );
  375. $excludes = $this->exclude_string( 'regex' );
  376. foreach ( $filesystem as $file ) {
  377. if ( ! $file->isReadable() ) {
  378. $this->unreadable_files[] = $file->getPathName();
  379. continue;
  380. }
  381. $pathname = str_ireplace( trailingslashit( $this->root() ), '', $this->conform_dir( $file->getPathname() ) );
  382. /* Excludes */
  383. if ( $excludes && preg_match( '(' . $excludes . ')', $pathname ) )
  384. continue;
  385. $this->files[] = $pathname;
  386. }
  387. } else {
  388. $this->files = $this->files_fallback( $this->root() );
  389. }
  390. if ( ! empty( $this->unreadable_files ) )
  391. $this->warning( $this->archive_method(), __( 'The following files are unreadable and could not be backed up: ', 'envato-wordpress-toolkit' ) . implode( ', ', $this->unreadable_files ) );
  392. return $this->files;
  393. }
  394. /**
  395. * Fallback function for generating a filesystem array
  396. *
  397. * Used if RecursiveDirectoryIterator::FOLLOW_SYMLINKS isn't available
  398. *
  399. * @access private
  400. * @since 1.4
  401. *
  402. * @param string $dir
  403. * @param array $files. (default: array())
  404. * @return array
  405. */
  406. private function files_fallback( $dir, $files = array() ) {
  407. $handle = opendir( $dir );
  408. $excludes = $this->exclude_string( 'regex' );
  409. while ( $file = readdir( $handle ) ) :
  410. /* Ignore current dir and containing dir and any unreadable files or directories */
  411. if ( $file == '.' || $file == '..' )
  412. continue;
  413. $filepath = $this->conform_dir( trailingslashit( $dir ) . $file );
  414. $file = str_ireplace( trailingslashit( $this->root() ), '', $filepath );
  415. if ( ! is_readable( $filepath ) ) {
  416. $this->unreadable_files[] = $filepath;
  417. continue;
  418. }
  419. /* Skip the backups dir and any excluded paths */
  420. if ( ( $excludes && preg_match( '(' . $excludes . ')', $file ) ) )
  421. continue;
  422. $files[] = $file;
  423. if ( is_dir( $filepath ) )
  424. $files = $this->files_fallback( $filepath, $files );
  425. endwhile;
  426. return $files;
  427. }
  428. /**
  429. * Helper function to load the PclZip library.
  430. *
  431. * @access private
  432. * @since 1.4
  433. *
  434. * @return null
  435. */
  436. private function load_pclzip() {
  437. /* Load PclZip */
  438. if ( ! defined( 'PCLZIP_TEMPORARY_DIR' ) )
  439. define( 'PCLZIP_TEMPORARY_DIR', trailingslashit( $this->path() ) );
  440. require_once( ABSPATH . 'wp-admin/includes/class-pclzip.php' );
  441. }
  442. /**
  443. * Attempt to work out the path to the zip command
  444. *
  445. * @access public
  446. * @since 1.4
  447. *
  448. * @return string
  449. */
  450. public function guess_zip_command_path() {
  451. /* Check shell_exec is available and hasn't been explicitly bypassed */
  452. if ( ! $this->shell_exec_available() )
  453. return '';
  454. /* List of possible zip locations */
  455. $zip_locations = array(
  456. '/usr/bin/zip'
  457. );
  458. if ( is_null( shell_exec( 'hash zip 2>&1' ) ) )
  459. return 'zip';
  460. /* Find the one which works */
  461. foreach ( $zip_locations as $location )
  462. if ( @file_exists( $this->conform_dir( $location ) ) )
  463. return $location;
  464. return '';
  465. }
  466. /**
  467. * Generate the exclude param string for the zip backup
  468. *
  469. * Takes the exclude rules and formats them for use with either
  470. * the shell zip command or pclzip
  471. *
  472. * @access public
  473. * @since 1.4
  474. *
  475. * @param string $context. (default: 'zip')
  476. * @return string
  477. */
  478. public function exclude_string( $context = 'zip' ) {
  479. /* Return a comma separated list by default */
  480. $separator = ', ';
  481. $wildcard = '';
  482. /* The zip command */
  483. if ( $context == 'zip' ) {
  484. $wildcard = '*';
  485. $separator = ' -x ';
  486. /* The PclZip fallback library */
  487. } else if ( $context == 'regex' ) {
  488. $wildcard = '([\s\S]*?)';
  489. $separator = '|';
  490. }
  491. /* Sanitize the excludes */
  492. $excludes = array_filter( array_unique( array_map( 'trim', (array) $this->excludes ) ) );
  493. /* If path() is inside root(), exclude it */
  494. if ( strpos( $this->path(), $this->root() ) !== false )
  495. $excludes[] = trailingslashit( $this->path() );
  496. foreach( $excludes as $key => &$rule ) {
  497. $file = $absolute = $fragment = false;
  498. /* Files don't end with / */
  499. if ( ! in_array( substr( $rule, -1 ), array( '\\', '/' ) ) ) {
  500. $file = true;
  501. /* If rule starts with a / then treat as absolute path */
  502. } else if ( in_array( substr( $rule, 0, 1 ), array( '\\', '/' ) ) ) {
  503. $absolute = true;
  504. /* Otherwise treat as dir fragment */
  505. } else {
  506. $fragment = true;
  507. }
  508. /* Strip $this->root and conform */
  509. $rule = str_ireplace( $this->root(), '', untrailingslashit( $this->conform_dir( $rule ) ) );
  510. /* Strip the preceeding slash */
  511. if ( in_array( substr( $rule, 0, 1 ), array( '\\', '/' ) ) )
  512. $rule = substr( $rule, 1 );
  513. /* Escape string for regex */
  514. if ( $context == 'regex' )
  515. $rule = str_replace( '.', '\.', $rule );
  516. /* Convert any existing wildcards */
  517. if ( $wildcard != '*' && strpos( $rule, '*' ) !== false )
  518. $rule = str_replace( '*', $wildcard, $rule );
  519. /* Wrap directory fragments and files in wildcards for zip */
  520. if ( $context == 'zip' && ( $fragment || $file ) )
  521. $rule = $wildcard . $rule . $wildcard;
  522. /* Add a wildcard to the end of absolute url for zips */
  523. if ( $context == 'zip' && $absolute )
  524. $rule .= $wildcard;
  525. /* Add and end carrot to files for pclzip but only if it doesn't end in a wildcard */
  526. if ( $file && $context == 'regex' )
  527. $rule .= '$';
  528. /* Add a start carrot to absolute urls for pclzip */
  529. if ( $absolute && $context == 'regex' )
  530. $rule = '^' . $rule;
  531. }
  532. /* Escape shell args for zip command */
  533. if ( $context == 'zip' )
  534. $excludes = array_map( 'escapeshellarg', array_unique( $excludes ) );
  535. return implode( $separator, $excludes );
  536. }
  537. /**
  538. * Check whether safe mode is active or not
  539. *
  540. * @access public
  541. * @since 1.4
  542. *
  543. * @return bool
  544. */
  545. public function is_safe_mode_active() {
  546. if ( $safe_mode = ini_get( 'safe_mode' ) && strtolower( $safe_mode ) != 'off' )
  547. return true;
  548. return false;
  549. }
  550. /**
  551. * Check whether shell_exec has been disabled.
  552. *
  553. * @access private
  554. * @since 1.4
  555. *
  556. * @return bool
  557. */
  558. private function shell_exec_available() {
  559. /* Are we in Safe Mode */
  560. if ( $this->is_safe_mode_active() )
  561. return false;
  562. /* Is shell_exec disabled? */
  563. if ( in_array( 'shell_exec', array_map( 'trim', explode( ',', ini_get( 'disable_functions' ) ) ) ) )
  564. return false;
  565. /* Can we issue a simple echo command? */
  566. if ( ! @shell_exec( 'echo envatobackup' ) )
  567. return false;
  568. return true;
  569. }
  570. /**
  571. * Sanitize a directory path
  572. *
  573. * @access public
  574. * @since 1.4
  575. *
  576. * @param string $dir
  577. * @param bool $recursive. (default: false)
  578. * @return string $dir
  579. */
  580. public function conform_dir( $dir, $recursive = false ) {
  581. /* Assume empty dir is root */
  582. if ( ! $dir )
  583. $dir = '/';
  584. /* Replace single forward slash (looks like double slash because we have to escape it) */
  585. $dir = str_replace( '\\', '/', $dir );
  586. $dir = str_replace( '//', '/', $dir );
  587. /* Remove the trailing slash */
  588. if ( $dir !== '/' )
  589. $dir = untrailingslashit( $dir );
  590. /* Carry on until completely normalized */
  591. if ( ! $recursive && $this->conform_dir( $dir, true ) != $dir )
  592. return $this->conform_dir( $dir );
  593. return (string) $dir;
  594. }
  595. /**
  596. * Get the errors
  597. *
  598. * @access public
  599. * @since 1.4
  600. *
  601. * @param string $context
  602. * @return mixed
  603. */
  604. public function errors( $context = null ) {
  605. if ( ! empty( $context ) )
  606. return isset( $this->errors[$context] ) ? $this->errors[$context] : array();
  607. return $this->errors;
  608. }
  609. /**
  610. * Add an error to the errors stack
  611. *
  612. * @access private
  613. * @since 1.4
  614. *
  615. * @param string $context
  616. * @param mixed $error
  617. * @return null
  618. */
  619. private function error( $context, $error ) {
  620. if ( empty( $context ) || empty( $error ) )
  621. return;
  622. $this->errors[$context][$_key = md5( implode( ':' , (array) $error ) )] = $error;
  623. }
  624. /**
  625. * Migrate errors to warnings
  626. *
  627. * @access private
  628. * @since 1.4
  629. *
  630. * @param string $context. (default: null)
  631. * @return null
  632. */
  633. private function errors_to_warnings( $context = null ) {
  634. $errors = empty( $context ) ? $this->errors() : array( $context => $this->errors( $context ) );
  635. if ( empty( $errors ) )
  636. return;
  637. foreach ( $errors as $error_context => $errors )
  638. foreach( $errors as $error )
  639. $this->warning( $error_context, $error );
  640. if ( $context )
  641. unset( $this->errors[$context] );
  642. else
  643. $this->errors = array();
  644. }
  645. /**
  646. * Get the warnings
  647. *
  648. * @access public
  649. * @since 1.4
  650. *
  651. * @return null
  652. */
  653. public function warnings( $context = null ) {
  654. if ( ! empty( $context ) )
  655. return isset( $this->warnings[$context] ) ? $this->warnings[$context] : array();
  656. return $this->warnings;
  657. }
  658. /**
  659. * Add an warning to the warnings stack
  660. *
  661. * @access private
  662. * @since 1.4
  663. *
  664. * @param string $context
  665. * @param mixed $warning
  666. * @return null
  667. */
  668. private function warning( $context, $warning ) {
  669. if ( empty( $context ) || empty( $warning ) )
  670. return;
  671. $this->warnings[$context][$_key = md5( implode( ':' , (array) $warning ) )] = $warning;
  672. }
  673. /**
  674. * Custom error handler for catching errors
  675. *
  676. * @access private
  677. * @since 1.4
  678. *
  679. * @param string $type
  680. * @param string $message
  681. * @param string $file
  682. * @param string $line
  683. * @return null
  684. */
  685. public function error_handler( $type ) {
  686. if ( ( defined( 'E_DEPRECATED' ) && $type == E_DEPRECATED ) || ( defined( 'E_STRICT' ) && $type == E_STRICT ) || error_reporting() === 0 )
  687. return false;
  688. $args = func_get_args();
  689. $this->warning( 'php', array_splice( $args, 0, 4 ) );
  690. return false;
  691. }
  692. }
  693. }
  694. /* End of file class-envato-backup.php */
  695. /* Location: ./includes/class-envato-backup.php */