PluginProbe ʕ •ᴥ•ʔ
JetBackup – Backup, Restore & Migrate / 1.3.9
JetBackup – Backup, Restore & Migrate v1.3.9
3.1.22.3 1.4.3 1.4.4 1.4.5 1.4.6 1.4.7 1.4.8 1.4.8.1 1.4.9 1.5.0 1.5.1 1.5.1.1 1.5.2 1.5.3 1.5.4 1.5.5 1.5.6 1.5.7 1.5.8 1.6.0 1.6.10 1.6.11 1.6.12 1.6.13 1.6.15 1.6.5.1 1.6.8.8 1.6.9 1.6.9.1 2.0.3 2.0.4 2.0.5 2.0.6 2.0.7.5 2.0.8.7 2.0.9.11 2.0.9.14 2.0.9.15 2.0.9.6 2.0.9.7 2.0.9.9 3.1.10.7 3.1.11.1 3.1.12.3 3.1.13.4 3.1.14.17 3.1.15.4 3.1.16.1 3.1.17.5 3.1.18.10 3.1.18.8 3.1.18.9 3.1.19.8 3.1.20.3 3.1.21.3 3.1.7.9 3.1.9.2 trunk 1.1.90 1.1.91 1.2.0 1.2.5 1.2.6 1.2.7 1.2.8 1.2.9 1.3.0 1.3.1 1.3.2 1.3.3 1.3.4 1.3.6 1.3.7 1.3.8 1.3.9 1.4.0 1.4.1 1.4.2
backup / com / core / SGBoot.php
backup / com / core Last commit date
backup 5 years ago database 5 years ago exception 5 years ago extension 5 years ago log 5 years ago notice 5 years ago restore 5 years ago schedule 5 years ago storage 5 years ago widget 5 years ago SGBoot.php 5 years ago SGConfig.php 5 years ago SGPing.php 5 years ago functions.php 5 years ago
SGBoot.php
312 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 backupGuardIncludeFile(SG_CORE_PATH.'functions.silver.php');
10 backupGuardIncludeFile(SG_CORE_PATH.'functions.gold.php');
11 backupGuardIncludeFile(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 backupGuardIncludeFile(SG_BACKUP_PATH.'SGBackupSchedule.php');
18 backupGuardIncludeFile(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 self::installReviewSettings();
178 }
179 catch (SGException $exception) {
180 die($exception);
181 }
182 }
183
184 private static function installReviewSettings()
185 {
186 $usageDays = SGConfig::get('usageDays');
187 if (!$usageDays) {
188 SGConfig::set('usageDays', 0);
189
190 $timeDate = new \DateTime('now');
191 $installTime = strtotime($timeDate->format('Y-m-d H:i:s'));
192 SGConfig::set('installDate', $installTime);
193 $timeDate->modify('+'.SG_BACKUP_REVIEW_PERIOD.' day');
194
195 $timeNow = strtotime($timeDate->format('Y-m-d H:i:s'));
196 SGConfig::set('openNextTime', $timeNow);
197 }
198 $backupCountReview = SGConfig::get('backupReviewCount');
199 if (!$backupCountReview) {
200 SGConfig::set('backupReviewCount', SG_BACKUP_REVIEW_BACKUP_COUNT);
201 }
202
203 $restoreReviewCount = SGConfig::get('restoreReviewCount');
204 if (!$restoreReviewCount) {
205 SGConfig::set('restoreReviewCount', SG_BACKUP_REVIEW_RESTORE_COUNT);
206 }
207 }
208
209 private static function cleanupSchedules()
210 {
211 $schedules = SGBackupSchedule::getAllSchedules();
212 foreach ($schedules as $schedule) {
213 SGBackupSchedule::remove($schedule['id']);
214 }
215 }
216
217 public static function uninstall($deleteBackups = false)
218 {
219 try {
220 @unlink(SG_PING_FILE_PATH);
221
222 if (self::isFeatureAvailable('SCHEDULE')) {
223 self::cleanupSchedules();
224 }
225
226 $sgdb = SGDatabase::getInstance();
227
228 //drop config table
229 $res = $sgdb->query('DROP TABLE IF EXISTS `'.SG_CONFIG_TABLE_NAME.'`;');
230 if ($res===false) {
231 throw new SGExceptionDatabaseError('Could not execute query');
232 }
233
234 //drop schedule table
235 $res = $sgdb->query('DROP TABLE IF EXISTS `'.SG_SCHEDULE_TABLE_NAME.'`;');
236 if ($res===false) {
237 throw new SGExceptionDatabaseError('Could not execute query');
238 }
239
240 //drop action table
241 $res = $sgdb->query('DROP TABLE IF EXISTS `'.SG_ACTION_TABLE_NAME.'`;');
242 if ($res===false) {
243 throw new SGExceptionDatabaseError('Could not execute query');
244 }
245
246 //delete directory of backups
247 if ($deleteBackups) {
248 $backupPath = SGConfig::get('SG_BACKUP_DIRECTORY');
249 backupGuardDeleteDirectory($backupPath);
250 }
251 }
252 catch (SGException $exception) {
253 die($exception);
254 }
255 }
256
257 public static function checkRequirement($requirement)
258 {
259 if ($requirement=='ftp' && !extension_loaded('ftp')) {
260 throw new SGExceptionNotFound('FTP extension is not loaded.');
261 }
262 else if ($requirement=='curl' && !function_exists('curl_version')) {
263 throw new SGExceptionNotFound('cURL extension is not loaded.');
264 }
265 else if ($requirement=='intSize' && PHP_INT_SIZE < 8) {
266 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) . ")");
267 }
268 }
269
270 public static function isFeatureAvailable($feature)
271 {
272 return (SGConfig::get('SG_FEATURE_'.strtoupper($feature))===1?true:false);
273 }
274
275 private static function prepare()
276 {
277 $backupPath = SGConfig::get('SG_BACKUP_DIRECTORY');
278
279 //create directory for backups
280 if (!is_dir($backupPath)) {
281 if (!@mkdir($backupPath)) {
282 throw new SGExceptionMethodNotAllowed('Cannot create folder: '.$backupPath);
283 }
284
285 if (!@file_put_contents($backupPath.'.htaccess', 'deny from all')) {
286 throw new SGExceptionMethodNotAllowed('Cannot create htaccess file');
287 }
288
289 if (!@file_put_contents($backupPath.'index.php', "<?php\n// Silence is golden")) {
290 throw new SGExceptionMethodNotAllowed('Cannot create index file');
291 }
292 }
293
294 //check permissions of backups directory
295 if (!is_writable($backupPath)) {
296 throw new SGExceptionForbidden('Permission denied. Directory is not writable: '.$backupPath);
297 }
298
299 //prepare notices
300 $noticeHandler = new SGNoticeHandler();
301 $noticeHandler->run();
302 }
303
304 private static function checkMinimumRequirements()
305 {
306 //check ZLib library
307 if (!function_exists('gzdeflate')) {
308 throw new SGExceptionNotFound('ZLib extension is not loaded.');
309 }
310 }
311 }
312