banner
2 years ago
check
2 years ago
cli
2 years ago
cron
2 years ago
dashboard
2 years ago
database
2 years ago
extracter
2 years ago
htaccess
2 years ago
progress
2 years ago
scanner
2 years ago
staging
2 years ago
uploader
2 years ago
zipper
2 years ago
.htaccess
2 years ago
activation.php
2 years ago
ajax.php
2 years ago
analyst.php
2 years ago
bypasser.php
2 years ago
cli-handler.php
2 years ago
compatibility.php
2 years ago
config.php
2 years ago
constants.php
2 years ago
initializer.php
2 years ago
logger.php
2 years ago
middleware-backup-proxy.php
2 years ago
restore-batching.php
2 years ago
cli-handler.php
260 lines
| 1 | <?php |
| 2 | |
| 3 | // Namespace |
| 4 | namespace BMI\Plugin; |
| 5 | |
| 6 | // Use classes |
| 7 | use BMI\Plugin\BMI_Logger as Logger; |
| 8 | use BMI\Plugin\Backup_Migration_Plugin as BMP; |
| 9 | use BMI\Plugin\Extracter\BMI_Extracter as Extracter; |
| 10 | use BMI\Plugin\Progress\BMI_MigrationProgress as MigrationProgress; |
| 11 | use BMI\Plugin AS BMI; |
| 12 | |
| 13 | // Allow only PHP CLI to use this script |
| 14 | if (php_sapi_name() !== 'cli') { |
| 15 | echo 'This script it dedicated for PHP CLI'; |
| 16 | exit; |
| 17 | } |
| 18 | |
| 19 | function isFunctionEnabled($func) { |
| 20 | $disabled = explode(',', ini_get('disable_functions')); |
| 21 | $isDisabled = in_array($func, $disabled); |
| 22 | if (!$isDisabled && function_exists($func)) return true; |
| 23 | else return false; |
| 24 | } |
| 25 | |
| 26 | // Find WordPress Path |
| 27 | function bmi_find_wordpress_base_path() { |
| 28 | |
| 29 | $dir = dirname(__FILE__); |
| 30 | $previous = null; |
| 31 | |
| 32 | do { |
| 33 | |
| 34 | if (file_exists($dir . '/wp-load.php') && file_exists($dir . '/wp-config.php')) return $dir; |
| 35 | if ($previous == $dir) break; |
| 36 | $previous = $dir; |
| 37 | |
| 38 | } while ($dir = dirname($dir)); |
| 39 | |
| 40 | return null; |
| 41 | |
| 42 | } |
| 43 | |
| 44 | // Tell WP to not use Themes and set Base Path |
| 45 | define('BASE_PATH', bmi_find_wordpress_base_path() . '/'); |
| 46 | define('BMI_USING_CLI_FUNCTIONALITY', true); |
| 47 | if (isset($argv[1])) define('BMI_CLI_FUNCTION', $argv[1]); |
| 48 | else { |
| 49 | |
| 50 | echo "\n\n========= BACKUP MIGRATION PLUGIN =========\n\n"; |
| 51 | echo "Please specify CLI function: bmi_restore [<backup_name>.zip], bmi_backup or bmi_quick_migration <backup URL>\n\n"; |
| 52 | echo "Examples: \n"; |
| 53 | echo " – php -f cli-handler.php bmi_backup\n"; |
| 54 | echo " – php -f cli-handler.php bmi_backup BMI_12-12-12_nameOfMySite_nameOfBackup.zip\n"; |
| 55 | echo " – php -f cli-handler.php bmi_restore BMI_12-12-12_nameOfMySite_nameOfBackup.zip\n"; |
| 56 | echo " – php -f cli-handler.php bmi_quick_migration \"http://localhost/site/linkToMyBackup.zip\"\n"; |
| 57 | echo "\n========= BACKUP MIGRATION PLUGIN =========\n\n"; |
| 58 | exit(); |
| 59 | |
| 60 | } |
| 61 | |
| 62 | if (isset($argv[2])) define('BMI_CLI_ARGUMENT', $argv[2]); |
| 63 | if (isset($argv[3])) define('BMI_CLI_ARGUMENT_2', $argv[3]); |
| 64 | |
| 65 | // Pseudo Server variables |
| 66 | $_SERVER['REQUEST_METHOD'] = 'CLI'; |
| 67 | |
| 68 | // Increase max execution time |
| 69 | if (isFunctionEnabled('headers_sent')) { |
| 70 | if (!headers_sent()) { |
| 71 | if (isFunctionEnabled('set_time_limit')) @set_time_limit(259200); |
| 72 | if (isFunctionEnabled('ini_set')) { |
| 73 | @ini_set('max_input_time', '259200'); |
| 74 | @ini_set('max_execution_time', '259200'); |
| 75 | @ini_set('session.gc_maxlifetime', '1200'); |
| 76 | } |
| 77 | } |
| 78 | } |
| 79 | |
| 80 | // Response |
| 81 | ob_start(); |
| 82 | echo '100101011101' . "\n"; |
| 83 | |
| 84 | @header('Connection: close'); |
| 85 | @header('Content-Length: ' . ob_get_length()); |
| 86 | |
| 87 | ob_end_clean(); |
| 88 | flush(); |
| 89 | |
| 90 | if (isFunctionEnabled('fastcgi_finish_request') && isFunctionEnabled('is_callable') && is_callable('fastcgi_finish_request')) { |
| 91 | fastcgi_finish_request(); |
| 92 | } |
| 93 | |
| 94 | // Let the server know it's server-side script |
| 95 | if (isFunctionEnabled('ignore_user_abort')) { |
| 96 | @ignore_user_abort(true); |
| 97 | } |
| 98 | |
| 99 | if (isFunctionEnabled('session_write_close')) { |
| 100 | @session_write_close(); |
| 101 | } |
| 102 | |
| 103 | function fixSlashes($str, $slash = false) { |
| 104 | // Old version |
| 105 | // $str = str_replace('\\\\', DIRECTORY_SEPARATOR, $str); |
| 106 | // $str = str_replace('\\', DIRECTORY_SEPARATOR, $str); |
| 107 | // $str = str_replace('\/', DIRECTORY_SEPARATOR, $str); |
| 108 | // $str = str_replace('/', DIRECTORY_SEPARATOR, $str); |
| 109 | |
| 110 | // if ($str[strlen($str) - 1] == DIRECTORY_SEPARATOR) { |
| 111 | // $str = substr($str, 0, -1); |
| 112 | // } |
| 113 | |
| 114 | // Since 1.3.2 |
| 115 | $protocol = ''; |
| 116 | if ($slash == false) $slash = DIRECTORY_SEPARATOR; |
| 117 | if (substr($str, 0, 7) == 'http://') $protocol = 'http://'; |
| 118 | else if (substr($str, 0, 8) == 'https://') $protocol = 'https://'; |
| 119 | |
| 120 | $str = substr($str, strlen($protocol)); |
| 121 | $str = preg_replace('/[\\\\\/]+/', $slash, $str); |
| 122 | $str = rtrim($str, '/\\' ); |
| 123 | |
| 124 | return $protocol . $str; |
| 125 | } |
| 126 | |
| 127 | function bmiLoadWordPressAndBackupPlugin() { |
| 128 | |
| 129 | // Define how WP should load |
| 130 | define('WP_USE_THEMES', false); |
| 131 | define('SHORTINIT', true); |
| 132 | |
| 133 | // Set path to our plugin's main file |
| 134 | $bmiPluginPathToLoad = fixSlashes(dirname(__DIR__) . '/backup-backup.php'); |
| 135 | $bmiPluginPathToLoadPro = fixSlashes(dirname(dirname(__DIR__)) . '/backup-backup-pro/backup-backup-pro.php'); |
| 136 | |
| 137 | // Use WP Globals and load WordPress |
| 138 | global $wp, $wp_query, $wp_the_query, $wp_rewrite, $wp_did_header; |
| 139 | require_once bmi_find_wordpress_base_path() . DIRECTORY_SEPARATOR . 'wp-load.php'; |
| 140 | global $wp_version; |
| 141 | |
| 142 | // Load directory WordPress constants |
| 143 | require_once ABSPATH . WPINC . '/formatting.php'; |
| 144 | require_once ABSPATH . WPINC . '/meta.php'; |
| 145 | wp_plugin_directory_constants(); |
| 146 | |
| 147 | // Allow to register activation hook and realpath |
| 148 | $GLOBALS['wp_plugin_paths'] = array(); |
| 149 | $GLOBALS['shortcode_tags'] = array(); |
| 150 | |
| 151 | // Load all dependencies of WordPress for Backup plugin |
| 152 | $dependencies = [ |
| 153 | ABSPATH . WPINC . '/l10n.php', |
| 154 | ABSPATH . WPINC . '/plugin.php', |
| 155 | ABSPATH . WPINC . '/link-template.php', |
| 156 | ABSPATH . WPINC . '/class-wp-textdomain-registry.php', |
| 157 | ABSPATH . WPINC . '/class-wp-locale.php', |
| 158 | ABSPATH . WPINC . '/class-wp-locale-switcher.php', |
| 159 | ABSPATH . WPINC . '/session.php', |
| 160 | ABSPATH . WPINC . '/pluggable.php', |
| 161 | ABSPATH . WPINC . '/class-wp-ajax-response.php', |
| 162 | ABSPATH . WPINC . '/capabilities.php', |
| 163 | ABSPATH . WPINC . '/class-wp-roles.php', |
| 164 | ABSPATH . WPINC . '/class-wp-role.php', |
| 165 | ABSPATH . WPINC . '/class-wp-user.php', |
| 166 | ABSPATH . WPINC . '/class-wp-query.php', |
| 167 | ABSPATH . WPINC . '/query.php', |
| 168 | ABSPATH . WPINC . '/general-template.php', |
| 169 | ABSPATH . WPINC . '/http.php', |
| 170 | ABSPATH . WPINC . '/class-http.php', |
| 171 | ABSPATH . WPINC . '/class-wp-http.php', |
| 172 | ABSPATH . WPINC . '/class-wp-http-streams.php', |
| 173 | ABSPATH . WPINC . '/class-wp-http-curl.php', |
| 174 | ABSPATH . WPINC . '/class-wp-http-proxy.php', |
| 175 | ABSPATH . WPINC . '/class-wp-http-cookie.php', |
| 176 | ABSPATH . WPINC . '/class-wp-http-encoding.php', |
| 177 | ABSPATH . WPINC . '/class-wp-http-response.php', |
| 178 | ABSPATH . WPINC . '/class-wp-http-requests-response.php', |
| 179 | ABSPATH . WPINC . '/class-wp-http-requests-hooks.php', |
| 180 | ABSPATH . WPINC . '/widgets.php', |
| 181 | ABSPATH . WPINC . '/class-wp-widget.php', |
| 182 | ABSPATH . WPINC . '/class-wp-widget-factory.php', |
| 183 | ABSPATH . WPINC . '/class-wp-user-request.php', |
| 184 | ABSPATH . WPINC . '/user.php', |
| 185 | ABSPATH . WPINC . '/class-wp-user-query.php', |
| 186 | ABSPATH . WPINC . '/class-wp-session-tokens.php', |
| 187 | ABSPATH . WPINC . '/class-wp-user-meta-session-tokens.php', |
| 188 | ABSPATH . WPINC . '/rest-api.php', |
| 189 | ABSPATH . WPINC . '/kses.php', |
| 190 | ABSPATH . WPINC . '/theme.php', |
| 191 | ABSPATH . WPINC . '/rewrite.php', |
| 192 | ABSPATH . WPINC . '/class-wp-block-editor-context.php', |
| 193 | ABSPATH . WPINC . '/class-wp-block-type.php', |
| 194 | ABSPATH . WPINC . '/class-wp-block-pattern-categories-registry.php', |
| 195 | ABSPATH . WPINC . '/class-wp-block-patterns-registry.php', |
| 196 | ABSPATH . WPINC . '/class-wp-block-styles-registry.php', |
| 197 | ABSPATH . WPINC . '/class-wp-block-type-registry.php', |
| 198 | ABSPATH . WPINC . '/class-wp-block.php', |
| 199 | ABSPATH . WPINC . '/class-wp-block-list.php', |
| 200 | ABSPATH . WPINC . '/class-wp-block-parser-block.php', |
| 201 | ABSPATH . WPINC . '/class-wp-block-parser-frame.php', |
| 202 | ABSPATH . WPINC . '/class-wp-block-parser.php', |
| 203 | ABSPATH . WPINC . '/blocks.php', |
| 204 | ABSPATH . WPINC . '/blocks/index.php', |
| 205 | ]; |
| 206 | |
| 207 | for ($i = 0; $i < sizeof($dependencies); ++$i) { |
| 208 | $dependency = $dependencies[$i]; |
| 209 | if (strpos($dependency, 'class-http.php') && version_compare($wp_version, '5.9.0', '>=')) { |
| 210 | continue; |
| 211 | } |
| 212 | if (strpos($dependency, 'session.php') && version_compare($wp_version, '4.7.0', '>=')) { |
| 213 | continue; |
| 214 | } |
| 215 | if (file_exists($dependency)) require_once $dependency; |
| 216 | } |
| 217 | |
| 218 | // Load Cookie Constants |
| 219 | wp_cookie_constants(); |
| 220 | |
| 221 | // Load SSL Constants for DB export |
| 222 | wp_ssl_constants(); |
| 223 | |
| 224 | // Register Translation |
| 225 | if (class_exists('WP_Textdomain_Registry')) { |
| 226 | $GLOBALS['wp_textdomain_registry'] = new \WP_Textdomain_Registry(); |
| 227 | } |
| 228 | |
| 229 | if (file_exists($bmiPluginPathToLoadPro) && is_readable($bmiPluginPathToLoadPro)) { |
| 230 | wp_register_plugin_realpath($bmiPluginPathToLoadPro); |
| 231 | include_once $bmiPluginPathToLoadPro; |
| 232 | |
| 233 | require_once BMI_PRO_ROOT_DIR . '/classes/core' . ((BMI_PRO_DEBUG) ? '.to-enc' : '') . '.php'; |
| 234 | $bmi_pro_instance = new BMI_Pro_Core(); |
| 235 | $bmi_pro_instance->initialize(); |
| 236 | } |
| 237 | |
| 238 | // Register our backup plugin and load its contents |
| 239 | wp_register_plugin_realpath($bmiPluginPathToLoad); |
| 240 | include_once $bmiPluginPathToLoad; |
| 241 | |
| 242 | // Enable our plugin WITHOUT calling plugins_loaded hook – it's important |
| 243 | require_once BMI_ROOT_DIR . DIRECTORY_SEPARATOR . 'includes' . DIRECTORY_SEPARATOR . 'constants.php'; |
| 244 | |
| 245 | // Initialize backup-migration |
| 246 | if (!class_exists('Backup_Migration_Plugin')) { |
| 247 | |
| 248 | // Require initializator |
| 249 | require_once BMI_ROOT_DIR . DIRECTORY_SEPARATOR . 'includes' . DIRECTORY_SEPARATOR . 'initializer.php'; |
| 250 | |
| 251 | // Initialize entire plugin |
| 252 | $bmi_instance = new BMI\Backup_Migration_Plugin(); |
| 253 | $bmi_instance->initialize(); |
| 254 | |
| 255 | } |
| 256 | |
| 257 | } |
| 258 | |
| 259 | bmiLoadWordPressAndBackupPlugin(); |
| 260 |