SGExternalRestore.php
163 lines
| 1 | <?php |
| 2 | |
| 3 | abstract class SGExternalRestore |
| 4 | { |
| 5 | private static $instance = null; |
| 6 | |
| 7 | public static function getInstance() |
| 8 | { |
| 9 | if (!self::$instance) { |
| 10 | self::$instance = self::createChildInstance(); |
| 11 | } |
| 12 | |
| 13 | return self::$instance; |
| 14 | } |
| 15 | |
| 16 | private static function createChildInstance() |
| 17 | { |
| 18 | $className = 'SGExternalRestore'.SG_ENV_ADAPTER; |
| 19 | require_once(dirname(__FILE__).'/'.$className.'.php'); |
| 20 | $child = new $className(); |
| 21 | return $child; |
| 22 | } |
| 23 | |
| 24 | protected function __construct() |
| 25 | { |
| 26 | |
| 27 | } |
| 28 | |
| 29 | private function __clone() |
| 30 | { |
| 31 | |
| 32 | } |
| 33 | |
| 34 | public function getSourceFilePath() |
| 35 | { |
| 36 | return SG_PUBLIC_PATH.'restore_'.strtolower(SG_ENV_ADAPTER).'.php'; |
| 37 | } |
| 38 | |
| 39 | public function getDestinationFilePath() |
| 40 | { |
| 41 | //get already saved restore path |
| 42 | $path = SGConfig::get('SG_EXTERNAL_RESTORE_PATH', true); |
| 43 | |
| 44 | if (!$path) { |
| 45 | $path = $this->getDestinationPath().SG_EXTERNAL_RESTORE_FILE; |
| 46 | SGConfig::set('SG_EXTERNAL_RESTORE_PATH', $path, true); |
| 47 | } |
| 48 | |
| 49 | return $path; |
| 50 | } |
| 51 | |
| 52 | public function getDestinationFileUrl(&$key = '') |
| 53 | { |
| 54 | //we use this key to deny direct access to the file |
| 55 | $key = SGConfig::get('SG_BACKUP_CURRENT_KEY', true); |
| 56 | |
| 57 | //get already saved restore url |
| 58 | $url = SGConfig::get('SG_EXTERNAL_RESTORE_URL', true); |
| 59 | |
| 60 | if (!$url) { |
| 61 | $url = $this->getDestinationUrl().SG_EXTERNAL_RESTORE_FILE.'?k='.$key; |
| 62 | SGConfig::set('SG_EXTERNAL_RESTORE_URL', $url, true); |
| 63 | } |
| 64 | |
| 65 | return $url; |
| 66 | } |
| 67 | |
| 68 | public static function isEnabled() |
| 69 | { |
| 70 | return SGConfig::get('SG_EXTERNAL_RESTORE_ENABLED')?true:false; |
| 71 | } |
| 72 | |
| 73 | protected static function setEnabled($enabled) |
| 74 | { |
| 75 | SGConfig::set('SG_EXTERNAL_RESTORE_ENABLED', ($enabled?1:0), true); |
| 76 | } |
| 77 | |
| 78 | private function getConstants($actionId) |
| 79 | { |
| 80 | $key = ''; |
| 81 | $destinationUrl = $this->getDestinationFileUrl($key); |
| 82 | $isMultisite = backupGuardIsMultisite(); |
| 83 | |
| 84 | return array( |
| 85 | 'SG_ACTION_ID' => $actionId, |
| 86 | 'SG_PLUGIN_NAME' => SG_PLUGIN_NAME, |
| 87 | 'SG_ENV_DB_PREFIX' => SG_ENV_DB_PREFIX, |
| 88 | 'SG_SITE_URL' => SG_SITE_URL, |
| 89 | 'SG_BACKUP_SITE_URL' => SG_BACKUP_SITE_URL, |
| 90 | 'SG_PUBLIC_URL' => SG_PUBLIC_URL, |
| 91 | 'SG_BACKUP_DIRECTORY' => SG_BACKUP_DIRECTORY, |
| 92 | 'SG_PING_FILE_PATH' => SG_PING_FILE_PATH, |
| 93 | 'BG_PLUGIN_URL' => SG_PUBLIC_BACKUPS_URL, |
| 94 | 'BG_RESTORE_KEY' => $key, |
| 95 | 'BG_RESTORE_URL' => $destinationUrl, |
| 96 | 'BG_IS_MULTISITE' => $isMultisite, |
| 97 | 'SG_MISC_MIGRATABLE_TABLES' => SG_MISC_MIGRATABLE_TABLES, |
| 98 | 'SG_MULTISITE_TABLES_TO_MIGRATE' => SG_MULTISITE_TABLES_TO_MIGRATE, |
| 99 | 'SG_DB_NAME' => SG_DB_NAME, |
| 100 | 'DOMAIN_CURRENT_SITE' => defined('DOMAIN_CURRENT_SITE')?DOMAIN_CURRENT_SITE:'', |
| 101 | 'PATH_CURRENT_SITE' => defined('PATH_CURRENT_SITE')?PATH_CURRENT_SITE:'', |
| 102 | 'SG_SUBDOMAIN_INSTALL' => SG_SUBDOMAIN_INSTALL, |
| 103 | 'WP_CONTENT_DIR' => WP_CONTENT_DIR, |
| 104 | 'SG_BACKUP_DATABASE_EXCLUDE' => SG_BACKUP_DATABASE_EXCLUDE, |
| 105 | 'SG_MYSQL_VERSION' => SG_MYSQL_VERSION, |
| 106 | 'SG_WP_OPTIONS_MIGRATABLE_VALUES' => SG_WP_OPTIONS_MIGRATABLE_VALUES, |
| 107 | 'SG_WP_USERMETA_MIGRATABLE_VALUES' => SG_WP_USERMETA_MIGRATABLE_VALUES |
| 108 | ); |
| 109 | } |
| 110 | |
| 111 | public function prepare($actionId) |
| 112 | { |
| 113 | $res = false; |
| 114 | |
| 115 | //reset everything |
| 116 | self::setEnabled(false); |
| 117 | SGConfig::set('SG_EXTERNAL_RESTORE_URL', '', true); |
| 118 | SGConfig::set('SG_EXTERNAL_RESTORE_PATH', '', true); |
| 119 | |
| 120 | if ($this->canPrepare()) { |
| 121 | $contents = @file_get_contents($this->getSourceFilePath()); |
| 122 | if ($contents) { |
| 123 | $constants = $this->getConstants($actionId); |
| 124 | $customConstants = $this->getCustomConstants(); |
| 125 | $allConstants = array_merge($constants, $customConstants); |
| 126 | |
| 127 | $defines = ''; |
| 128 | foreach ($allConstants as $key => $val) { |
| 129 | $defines .= "define('$key', '$val');\n"; |
| 130 | } |
| 131 | |
| 132 | //put all defines inside the file |
| 133 | $contents = str_replace('#SG_DYNAMIC_DEFINES#', $defines, $contents); |
| 134 | |
| 135 | //create new copy |
| 136 | $res = (bool)@file_put_contents($this->getDestinationFilePath(), $contents); |
| 137 | } |
| 138 | } |
| 139 | |
| 140 | self::setEnabled($res); |
| 141 | |
| 142 | return $res; |
| 143 | } |
| 144 | |
| 145 | public function cleanup() |
| 146 | { |
| 147 | if (file_exists($this->getDestinationFilePath())) { |
| 148 | $actions = SGBackup::getRunningActions(); |
| 149 | if (empty($actions)) { |
| 150 | @unlink($this->getDestinationFilePath()); |
| 151 | } |
| 152 | } |
| 153 | } |
| 154 | |
| 155 | abstract protected function canPrepare(); |
| 156 | |
| 157 | abstract protected function getCustomConstants(); |
| 158 | |
| 159 | abstract public function getDestinationPath(); |
| 160 | |
| 161 | abstract public function getDestinationUrl(); |
| 162 | } |
| 163 |