| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | namespace HM\BackUpWordPress; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  |  * The base Backup Engine | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  |  * Base Backup Engine types should extend this class and call parent::__construct in | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  |  * there constructor. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  |  * Defines base functionality shared across all types of backups | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  |  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  | abstract class Backup_Engine { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  | 	/** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  | 	 * An array of backup errors. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  | 	 * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  | 	 * @var array | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  | 	 */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  | 	private $errors = array(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  | 	/** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  | 	 * An array of backup warnings. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  | 	 * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  | 	 * @var array | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  | 	 */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  | 	private $warnings = array(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  | 	public function __construct() { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  | 		/** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  | 		 * Raise the `memory_limit` and `max_execution time` | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  | 		 * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  | 		 * Respects the WP_MAX_MEMORY_LIMIT Constant and the `admin_memory_limit` | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  | 		 * filter. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  | 		 */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  | 		@ini_set( 'memory_limit', apply_filters( 'admin_memory_limit', WP_MAX_MEMORY_LIMIT ) ); | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  | 		@set_time_limit( 0 ); | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  | 		// Set a custom error handler so we can track errors | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  | 		set_error_handler( array( $this, 'error_handler' ) ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  | 	/** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  | 	 * Backup Engine Types should always implement the `verify_backup` method. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  | 	 * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  | 	 * @return bool Whether the backup completed successfully or not. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  | 	 */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  | 	abstract public function verify_backup(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  | 	/** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  | 	 * Get the full filepath to the backup file. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  | 	 * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  | 	 * @return string The backup filepath. | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 56 |  |  | 	 */ | 
            
                                                                        
                            
            
                                    
            
            
                | 57 |  |  | 	public function get_backup_filepath() { | 
            
                                                                        
                            
            
                                    
            
            
                | 58 |  |  | 		return trailingslashit( Path::get_path() ) . $this->get_backup_filename(); | 
            
                                                                        
                            
            
                                    
            
            
                | 59 |  |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  | 	/** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  | 	 * Get the filename of the backup. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  | 	 * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  | 	 * @return string The backup filename. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  | 	 */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  | 	public function get_backup_filename() { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  | 		return $this->backup_filename; | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  | 	/** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  | 	 * Set the filename of the backup. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  | 	 * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  | 	 * @param string $filename The backup filename. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 |  |  | 	 */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 |  |  | 	public function set_backup_filename( $filename ) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 |  |  | 		$this->backup_filename = strtolower( sanitize_file_name( remove_accents( $filename ) ) ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 |  |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 |  |  | 	/** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 |  |  | 	 * Get the array of errors encountered during the backup process. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 |  |  | 	 * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 |  |  | 	 * @param  string $context The context for the error, usually the Backup | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 |  |  | 	 *                         Engine that encountered the error. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 |  |  | 	 * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 85 |  |  | 	 * @return array           The array of errors. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 86 |  |  | 	 */ | 
            
                                                                                                            
                            
            
                                                                    
                                                                                                        
            
            
                | 87 |  | View Code Duplication | 	public function get_errors( $context = null ) { | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 88 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 89 |  |  | 		// Only return a specific contexts errors. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 90 |  |  | 		if ( ! empty( $context ) ) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 91 |  |  | 			return isset( $this->errors[ $context ] ) ? $this->errors[ $context ] : array(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 92 |  |  | 		} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 93 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 94 |  |  | 		return $this->errors; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 95 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 96 |  |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 97 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 98 |  |  | 	/** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 99 |  |  | 	 * Add an error to the errors array. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 100 |  |  | 	 * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 101 |  |  | 	 * An error is always treat as fatal and should only be used for unrecoverable | 
            
                                                                                                            
                            
            
                                    
            
            
                | 102 |  |  | 	 * issues with the backup process. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 103 |  |  | 	 * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 104 |  |  | 	 * @param  string $context The context for the error. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 105 |  |  | 	 * @param  string $error   The error that was encountered. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 106 |  |  | 	 */ | 
            
                                                                                                            
                            
            
                                                                    
                                                                                                        
            
            
                | 107 |  | View Code Duplication | 	public function error( $context, $error ) { | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 108 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 109 |  |  | 		if ( empty( $context ) || empty( $error ) ) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 110 |  |  | 			return; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 111 |  |  | 		} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 112 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 113 |  |  | 		// Ensure we don't store duplicate errors by md5'ing the error as the key | 
            
                                                                                                            
                            
            
                                    
            
            
                | 114 |  |  | 		$this->errors[ $context ][ $_key = md5( implode( ':', (array) $error ) ) ] = $error; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 115 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 116 |  |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 117 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 118 |  |  | 	/** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 119 |  |  | 	 * Get the array of warnings encountered during the backup process. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 120 |  |  | 	 * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 121 |  |  | 	 * @param  string $context The context for the warning, usually the Backup | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 122 |  |  | 	 *                         Engine that encountered the warning. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 123 |  |  | 	 * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 124 |  |  | 	 * @return array           The array of warnings. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 125 |  |  | 	 */ | 
            
                                                                                                            
                            
            
                                                                    
                                                                                                        
            
            
                | 126 |  | View Code Duplication | 	public function get_warnings( $context = null ) { | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 127 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 128 |  |  | 		// Only return a specific contexts errors. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 129 |  |  | 		if ( ! empty( $context ) ) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 130 |  |  | 			return isset( $this->warnings[ $context ] ) ? $this->warnings[ $context ] : array(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 131 |  |  | 		} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 132 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 133 |  |  | 		return $this->warnings; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 134 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 135 |  |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 136 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 137 |  |  | 	/** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 138 |  |  | 	 * Add an warning to the errors warnings. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 139 |  |  | 	 * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 140 |  |  | 	 * A warning is always treat as non-fatal and should only be used for recoverable | 
            
                                                                                                            
                            
            
                                    
            
            
                | 141 |  |  | 	 * issues with the backup process. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 142 |  |  | 	 * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 143 |  |  | 	 * @param  string $context The context for the warning. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 144 |  |  | 	 * @param  string $error   The warning that was encountered. | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 145 |  |  | 	 */ | 
            
                                                                                                            
                            
            
                                                                    
                                                                                                        
            
            
                | 146 |  | View Code Duplication | 	public function warning( $context, $warning ) { | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 147 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 148 |  |  | 		if ( empty( $context ) || empty( $warning ) ) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 149 |  |  | 			return; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 150 |  |  | 		} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 151 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 152 |  |  | 		// Ensure we don't store duplicate warnings by md5'ing the error as the key | 
            
                                                                                                            
                            
            
                                    
            
            
                | 153 |  |  | 		$this->warnings[ $context ][ $_key = md5( implode( ':', (array) $warning ) ) ] = $warning; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 154 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 155 |  |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 156 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 157 |  |  | 	/** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 158 |  |  | 	 * Hooked into `set_error_handler` to catch any PHP errors that happen during | 
            
                                                                                                            
                            
            
                                    
            
            
                | 159 |  |  | 	 * the backup process. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 160 |  |  | 	 * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 161 |  |  | 	 * PHP errors are always treat as warnings rather than errors. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 162 |  |  | 	 * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 163 |  |  | 	 * @param  int $type   The level of error raised | 
            
                                                                                                            
                            
            
                                    
            
            
                | 164 |  |  | 	 * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 165 |  |  | 	 * @return false       Return false to pass the error back to PHP so it can | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 166 |  |  | 	 *                     be handled natively. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 167 |  |  | 	 */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 168 |  |  | 	public function error_handler( $type ) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 169 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 170 |  |  | 		// Skip strict & deprecated warnings | 
            
                                                                                                            
                            
            
                                    
            
            
                | 171 |  |  | 		if ( ( defined( 'E_DEPRECATED' ) && $type === E_DEPRECATED ) || ( defined( 'E_STRICT' ) && $type === E_STRICT ) || error_reporting() === 0 ) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 172 |  |  | 			return false; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 173 |  |  | 		} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 174 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 175 |  |  | 		/** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 176 |  |  | 		 * Get the details of the error. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 177 |  |  | 		 * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 178 |  |  | 		 * These are: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 179 |  |  | 		 * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 180 |  |  | 		 * @param int    $errorno   The error level expressed as an integer/ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 181 |  |  | 		 * @param string $errstr    The error message. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 182 |  |  | 		 * @param string $errfile   The file that the error raised in. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 183 |  |  | 		 * @param string $errorline The line number the error was raised on. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 184 |  |  | 		 */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 185 |  |  | 		$args = func_get_args(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 186 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 187 |  |  | 		// Strip the error level | 
            
                                                                                                            
                            
            
                                    
            
            
                | 188 |  |  | 		array_shift( $args ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 189 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 190 |  |  | 		// Fire a warning for the PHP error passing the message, file and line number. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 191 |  |  | 		$this->warning( 'php', implode( ', ', array_splice( $args, 0, 3 ) ) ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 192 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 193 |  |  | 		return false; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 194 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 195 |  |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 196 |  |  |  | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 197 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 198 |  |  |  | 
            
                        
If you suppress an error, we recommend checking for the error condition explicitly: