-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathClassHandler.php
executable file
·125 lines (114 loc) · 3.78 KB
/
ClassHandler.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
<?php
/*
* This file is part of MythicalSystemsFramework.
* Please view the LICENSE file that was distributed with this source code.
*
* (c) MythicalSystems <mythicalsystems.xyz> - All rights reserved
* (c) NaysKutzu <nayskutzu.xyz> - All rights reserved
* (c) Cassian Gherman <nayskutzu.xyz> - All rights reserved
*
* You should have received a copy of the MIT License
* along with this program. If not, see <https://opensource.org/licenses/MIT>.
*/
use MythicalSystemsFramework\Cli\Kernel;
use MythicalSystemsFramework\Backup\Backup;
use MythicalSystemsFramework\Kernel\Logger;
use MythicalSystemsFramework\Database\MySQL;
use MythicalSystemsFramework\Kernel\LoggerTypes;
use MythicalSystemsFramework\Kernel\LoggerLevels;
class ClassHandler
{
public const TABLE_NAME = 'framework_addon_backup24_data';
public static $columns = [
'id',
'last_backup',
];
/**
* Create the table if it doesn't exist.
*
* @return void
*/
public static function createTableIfNotExist()
{
$mysql = new MySQL();
$conn = $mysql->connectMYSQLI();
$conn->query('CREATE TABLE IF NOT EXISTS `' . self::TABLE_NAME . '` (`id` INT NOT NULL AUTO_INCREMENT , `last_backup` DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP , PRIMARY KEY (`id`)) ENGINE = InnoDB;');
self::updateLastBackup();
}
/**
* Drop the table.
*
* @return void
*/
public static function dropTable()
{
$mysql = new MySQL();
$conn = $mysql->connectMYSQLI();
$conn->query('DROP TABLE IF EXISTS ' . self::TABLE_NAME . '');
}
/**
* Update the last backup date.
*
* @return void
*/
public static function updateLastBackup()
{
$mysql = new MySQL();
$conn = $mysql->connectMYSQLI();
if ($conn->query('SELECT * FROM ' . self::TABLE_NAME . ' WHERE `id` = 1')->num_rows === 0) {
$conn->query('INSERT INTO `' . self::TABLE_NAME . '` (`id`, `last_backup`) VALUES (1, CURRENT_TIMESTAMP)');
return;
} else {
$conn->query('UPDATE ' . self::TABLE_NAME . ' SET `last_backup` = CURRENT_TIMESTAMP WHERE `id` = 1');
}
}
/**
* Get the last backup date.
*/
public static function getLastBackup(): string
{
$mysql = new MySQL();
$conn = $mysql->connectMYSQLI();
$result = $conn->query('SELECT `' . self::$columns[1] . '` FROM ' . self::TABLE_NAME . ' WHERE `id` = ' . self::$columns[0] . '');
$row = $result->fetch_assoc();
if (empty($row)) {
self::updateLastBackup();
}
return $row['last_backup'];
}
/**
* Get the current system time.
*/
public static function getCurrentSystemTime(): string
{
return date('Y-m-d H:i:s');
}
/**
* Check if it's time for a backup.
*/
public static function isTimeForBackup(): bool
{
$lastBackup = self::getLastBackup();
$nowTime = self::getCurrentSystemTime();
$lastBackupTime = strtotime($lastBackup);
$currentTime = strtotime($nowTime);
if (($currentTime - $lastBackupTime) >= 86400) {
return true;
}
return false;
}
/**
* Take a backup.
*/
public static function takeBackup(): void
{
if (self::isTimeForBackup()) {
self::updateLastBackup();
Backup::setBackupStatus(Backup::take(), MythicalSystemsFramework\Backup\Status::DONE);
Logger::log(LoggerLevels::INFO, LoggerTypes::BACKUP, '[Backup 24] Backup taken successfully');
echo Kernel::translateColorsCode('&a[Backup 24] &7Backup taken successfully.');
} else {
echo Kernel::translateColorsCode("&c[Backup 24] &7Backup not taken, it's not time yet.");
}
}
}