backup
6 years ago
database
6 years ago
exception
6 years ago
extension
6 years ago
log
6 years ago
notice
6 years ago
restore
6 years ago
schedule
6 years ago
storage
6 years ago
widget
6 years ago
SGBoot.php
6 years ago
SGConfig.php
6 years ago
SGPing.php
6 years ago
functions.php
6 years ago
SGBoot.php
285 lines
| 1 | <?php |
| 2 | //check PHP version |
| 3 | if (version_compare(PHP_VERSION, '5.3.3', '<')) { |
| 4 | die('PHP >=5.3.3 version required.'); |
| 5 | } |
| 6 | |
| 7 | require_once(SG_EXCEPTION_PATH.'SGException.php'); |
| 8 | require_once(SG_CORE_PATH.'functions.php'); |
| 9 | @include_once(SG_CORE_PATH.'functions.silver.php'); |
| 10 | @include_once(SG_CORE_PATH.'functions.gold.php'); |
| 11 | @include_once(SG_CORE_PATH.'functions.platinum.php'); |
| 12 | require_once(SG_CORE_PATH.'SGPing.php'); |
| 13 | require_once(SG_DATABASE_PATH.'SGDatabase.php'); |
| 14 | require_once(SG_CORE_PATH.'SGConfig.php'); |
| 15 | require_once(SG_NOTICE_PATH.'SGNotice.php'); |
| 16 | require_once(SG_NOTICE_PATH.'SGNoticeHandler.php'); |
| 17 | @include_once(SG_BACKUP_PATH.'SGBackupSchedule.php'); |
| 18 | @include_once(SG_EXTENSION_PATH.'SGExtension.php'); |
| 19 | |
| 20 | class SGBoot |
| 21 | { |
| 22 | public static $executionTimeLimit = 0; |
| 23 | public static $memoryLimit = 0; |
| 24 | |
| 25 | public static function init() |
| 26 | { |
| 27 | //get current execution time limit |
| 28 | self::$executionTimeLimit = ini_get('max_execution_time'); |
| 29 | |
| 30 | //get current memory limit |
| 31 | self::$memoryLimit = ini_get('memory_limit'); |
| 32 | |
| 33 | //remove execution time limit |
| 34 | @set_time_limit(0); |
| 35 | |
| 36 | //change initial memory limit |
| 37 | @ini_set('memory_limit', '512M'); |
| 38 | |
| 39 | //don't let server to abort scripts |
| 40 | @ignore_user_abort(true); |
| 41 | |
| 42 | //load all config variables from database |
| 43 | SGConfig::getAll(); |
| 44 | |
| 45 | try { |
| 46 | //check minimum requirements |
| 47 | self::checkMinimumRequirements(); |
| 48 | |
| 49 | //prepare directory for backups |
| 50 | self::prepare(); |
| 51 | } |
| 52 | catch (SGException $exception) { |
| 53 | die($exception); |
| 54 | } |
| 55 | } |
| 56 | |
| 57 | public static function didInstallForFirstTime() |
| 58 | { |
| 59 | self::setPluginInstallUpdateDate(); |
| 60 | } |
| 61 | |
| 62 | public static function didUpdatePluginVersion() |
| 63 | { |
| 64 | self::setPluginInstallUpdateDate(); |
| 65 | } |
| 66 | |
| 67 | public static function setPluginInstallUpdateDate() |
| 68 | { |
| 69 | SGConfig::set('SG_PLUGIN_INSTALL_UPDATE_DATE', time()); |
| 70 | } |
| 71 | |
| 72 | private static function installConfigTable($sgdb) |
| 73 | { |
| 74 | //drop config table |
| 75 | $sgdb->query('DROP TABLE IF EXISTS `'.SG_CONFIG_TABLE_NAME.'`;'); |
| 76 | |
| 77 | //create config table |
| 78 | $res = $sgdb->query( |
| 79 | 'CREATE TABLE IF NOT EXISTS `'.SG_CONFIG_TABLE_NAME.'` ( |
| 80 | `ckey` varchar(100) NOT NULL, |
| 81 | `cvalue` text NOT NULL, |
| 82 | PRIMARY KEY (`ckey`) |
| 83 | ) DEFAULT CHARSET=utf8;' |
| 84 | ); |
| 85 | if ($res===false) { |
| 86 | return false; |
| 87 | } |
| 88 | |
| 89 | //delete all content from config table (just in case if wasn't dropped) |
| 90 | $sgdb->query('DELETE FROM `'.SG_CONFIG_TABLE_NAME.'`;'); |
| 91 | |
| 92 | //populate config table |
| 93 | $res = $sgdb->query( |
| 94 | "INSERT INTO `".SG_CONFIG_TABLE_NAME."` VALUES |
| 95 | ('SG_BACKUP_GUARD_VERSION','".SG_BACKUP_GUARD_VERSION."'), |
| 96 | ('SG_BACKUP_WITH_RELOADINGS', '1'), |
| 97 | ('SG_BACKUP_SYNCHRONOUS_STORAGE_UPLOAD','1'), |
| 98 | ('SG_NOTIFICATIONS_ENABLED','0'), |
| 99 | ('SG_SHOW_STATISTICS_WIDGET','1'), |
| 100 | ('SG_NOTIFICATIONS_EMAIL_ADDRESS',''), |
| 101 | ('SG_STORAGE_BACKUPS_FOLDER_NAME','sg_backups');" |
| 102 | ); |
| 103 | if ($res===false) { |
| 104 | return false; |
| 105 | } |
| 106 | |
| 107 | return true; |
| 108 | } |
| 109 | |
| 110 | private static function installScheduleTable($sgdb) |
| 111 | { |
| 112 | //drop schedule table |
| 113 | $sgdb->query('DROP TABLE IF EXISTS `'.SG_SCHEDULE_TABLE_NAME.'`;'); |
| 114 | |
| 115 | //create schedule table |
| 116 | $res = $sgdb->query( |
| 117 | 'CREATE TABLE IF NOT EXISTS `'.SG_SCHEDULE_TABLE_NAME.'` ( |
| 118 | `id` int(10) unsigned NOT NULL AUTO_INCREMENT, |
| 119 | `label` varchar(255) NOT NULL, |
| 120 | `status` tinyint(3) unsigned NOT NULL, |
| 121 | `schedule_options` varchar(255) NOT NULL, |
| 122 | `backup_options` text NOT NULL, |
| 123 | PRIMARY KEY (`id`) |
| 124 | ) DEFAULT CHARSET=utf8;' |
| 125 | ); |
| 126 | if ($res===false) { |
| 127 | return false; |
| 128 | } |
| 129 | |
| 130 | return true; |
| 131 | } |
| 132 | |
| 133 | private static function installActionTable($sgdb) |
| 134 | { |
| 135 | //drop action table |
| 136 | $sgdb->query('DROP TABLE IF EXISTS `'.SG_ACTION_TABLE_NAME.'`;'); |
| 137 | |
| 138 | //create action table |
| 139 | $res = $sgdb->query( |
| 140 | "CREATE TABLE IF NOT EXISTS `".SG_ACTION_TABLE_NAME."` ( |
| 141 | `id` int(10) unsigned NOT NULL AUTO_INCREMENT, |
| 142 | `name` varchar(255) NOT NULL, |
| 143 | `type` tinyint(3) unsigned NOT NULL, |
| 144 | `subtype` tinyint(3) unsigned NOT NULL DEFAULT '0', |
| 145 | `status` tinyint(3) unsigned NOT NULL, |
| 146 | `progress` tinyint(3) unsigned NOT NULL DEFAULT '0', |
| 147 | `start_date` datetime NOT NULL, |
| 148 | `update_date` datetime DEFAULT NULL, |
| 149 | `options` text NOT NULL, |
| 150 | PRIMARY KEY (`id`) |
| 151 | ) DEFAULT CHARSET=utf8;" |
| 152 | ); |
| 153 | if ($res===false) { |
| 154 | return false; |
| 155 | } |
| 156 | |
| 157 | return true; |
| 158 | } |
| 159 | |
| 160 | public static function install() |
| 161 | { |
| 162 | $sgdb = SGDatabase::getInstance(); |
| 163 | |
| 164 | try { |
| 165 | if (!self::installConfigTable($sgdb)) { |
| 166 | throw new SGExceptionDatabaseError('Could not install config table'); |
| 167 | } |
| 168 | |
| 169 | if (!self::installScheduleTable($sgdb)) { |
| 170 | throw new SGExceptionDatabaseError('Could not install schedule table'); |
| 171 | } |
| 172 | |
| 173 | if (!self::installActionTable($sgdb)) { |
| 174 | throw new SGExceptionDatabaseError('Could not install action table'); |
| 175 | } |
| 176 | } |
| 177 | catch (SGException $exception) { |
| 178 | die($exception); |
| 179 | } |
| 180 | } |
| 181 | |
| 182 | private static function cleanupSchedules() |
| 183 | { |
| 184 | $schedules = SGBackupSchedule::getAllSchedules(); |
| 185 | foreach ($schedules as $schedule) { |
| 186 | SGBackupSchedule::remove($schedule['id']); |
| 187 | } |
| 188 | } |
| 189 | |
| 190 | public static function uninstall($deleteBackups = false) |
| 191 | { |
| 192 | try { |
| 193 | @unlink(SG_PING_FILE_PATH); |
| 194 | |
| 195 | if (self::isFeatureAvailable('SCHEDULE')) { |
| 196 | self::cleanupSchedules(); |
| 197 | } |
| 198 | |
| 199 | $sgdb = SGDatabase::getInstance(); |
| 200 | |
| 201 | //drop config table |
| 202 | $res = $sgdb->query('DROP TABLE IF EXISTS `'.SG_CONFIG_TABLE_NAME.'`;'); |
| 203 | if ($res===false) { |
| 204 | throw new SGExceptionDatabaseError('Could not execute query'); |
| 205 | } |
| 206 | |
| 207 | //drop schedule table |
| 208 | $res = $sgdb->query('DROP TABLE IF EXISTS `'.SG_SCHEDULE_TABLE_NAME.'`;'); |
| 209 | if ($res===false) { |
| 210 | throw new SGExceptionDatabaseError('Could not execute query'); |
| 211 | } |
| 212 | |
| 213 | //drop action table |
| 214 | $res = $sgdb->query('DROP TABLE IF EXISTS `'.SG_ACTION_TABLE_NAME.'`;'); |
| 215 | if ($res===false) { |
| 216 | throw new SGExceptionDatabaseError('Could not execute query'); |
| 217 | } |
| 218 | |
| 219 | //delete directory of backups |
| 220 | if ($deleteBackups) { |
| 221 | $backupPath = SGConfig::get('SG_BACKUP_DIRECTORY'); |
| 222 | backupGuardDeleteDirectory($backupPath); |
| 223 | } |
| 224 | } |
| 225 | catch (SGException $exception) { |
| 226 | die($exception); |
| 227 | } |
| 228 | } |
| 229 | |
| 230 | public static function checkRequirement($requirement) |
| 231 | { |
| 232 | if ($requirement=='ftp' && !extension_loaded('ftp')) { |
| 233 | throw new SGExceptionNotFound('FTP extension is not loaded.'); |
| 234 | } |
| 235 | else if ($requirement=='curl' && !function_exists('curl_version')) { |
| 236 | throw new SGExceptionNotFound('cURL extension is not loaded.'); |
| 237 | } |
| 238 | else if ($requirement=='intSize' && PHP_INT_SIZE < 8) { |
| 239 | throw new SGExceptionIO("BackupGuard uses 64-bit integers, but it looks like we're running on a version of PHP that doesn't support 64-bit integers (PHP_INT_MAX=" . ((string) PHP_INT_MAX) . ")"); |
| 240 | } |
| 241 | } |
| 242 | |
| 243 | public static function isFeatureAvailable($feature) |
| 244 | { |
| 245 | return (SGConfig::get('SG_FEATURE_'.strtoupper($feature))===1?true:false); |
| 246 | } |
| 247 | |
| 248 | private static function prepare() |
| 249 | { |
| 250 | $backupPath = SGConfig::get('SG_BACKUP_DIRECTORY'); |
| 251 | |
| 252 | //create directory for backups |
| 253 | if (!is_dir($backupPath)) { |
| 254 | if (!@mkdir($backupPath)) { |
| 255 | throw new SGExceptionMethodNotAllowed('Cannot create folder: '.$backupPath); |
| 256 | } |
| 257 | |
| 258 | if (!@file_put_contents($backupPath.'.htaccess', 'deny from all')) { |
| 259 | throw new SGExceptionMethodNotAllowed('Cannot create htaccess file'); |
| 260 | } |
| 261 | |
| 262 | if (!@file_put_contents($backupPath.'index.php', "<?php\n// Silence is golden")) { |
| 263 | throw new SGExceptionMethodNotAllowed('Cannot create index file'); |
| 264 | } |
| 265 | } |
| 266 | |
| 267 | //check permissions of backups directory |
| 268 | if (!is_writable($backupPath)) { |
| 269 | throw new SGExceptionForbidden('Permission denied. Directory is not writable: '.$backupPath); |
| 270 | } |
| 271 | |
| 272 | //prepare notices |
| 273 | $noticeHandler = new SGNoticeHandler(); |
| 274 | $noticeHandler->run(); |
| 275 | } |
| 276 | |
| 277 | private static function checkMinimumRequirements() |
| 278 | { |
| 279 | //check ZLib library |
| 280 | if (!function_exists('gzdeflate')) { |
| 281 | throw new SGExceptionNotFound('ZLib extension is not loaded.'); |
| 282 | } |
| 283 | } |
| 284 | } |
| 285 |