PluginProbe ʕ •ᴥ•ʔ
JetBackup – Backup, Restore & Migrate / 3.1.13.4
JetBackup – Backup, Restore & Migrate v3.1.13.4
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 / src / JetBackup / Crontab / Crontab.php
backup / src / JetBackup / Crontab Last commit date
.htaccess 9 months ago Crontab.php 9 months ago index.html 9 months ago web.config 9 months ago
Crontab.php
125 lines
1 <?php
2
3 namespace JetBackup\Crontab;
4
5 use JetBackup\Alert\Alert;
6 use JetBackup\Factory;
7 use JetBackup\IO\Execute;
8 use JetBackup\JetBackup;
9 use JetBackup\Wordpress\Wordpress;
10
11 if (!defined( '__JETBACKUP__')) die('Direct access is not allowed');
12
13 class Crontab {
14
15 const CRON_URL = "%s/%s/%s/backup/public/cron/cron.php?token=%s";
16 const CRON_FILE = JetBackup::CRON_PATH . JetBackup::SEP . 'cron.php';
17 const TEMP_FILE = '.temp_cron.php';
18
19 private string $_temp_cron;
20 private ?array $_crontab=null;
21 private ?bool $_crontab_status=null;
22
23 public function __construct () {
24 $this->_temp_cron = Factory::getLocations()->getTempDir() . '/' . self::TEMP_FILE;
25 }
26
27 public function getCrontabStatus():bool {
28 if($this->_crontab_status === null) {
29 $this->_crontab_status = Factory::getSettingsAutomation()->isCronStatusEnabled();
30 }
31 return $this->_crontab_status;
32 }
33
34 public static function getPublicCron():string {
35 return sprintf(self::CRON_URL, Wordpress::getSiteURL(), Wordpress::WP_CONTENT, Wordpress::WP_PLUGINS, Factory::getConfig()->getCronToken());
36 }
37
38 public static function getCommand():string {
39 $php = Factory::getSettingsGeneral()->getPHPCLILocation();
40 return sprintf("* * * * * %s %s > /dev/null 2>&1 &", $php, self::CRON_FILE);
41 }
42
43 public function getCrontab():array {
44 if($this->_crontab === null && !Execute::run('crontab -l', $output)) $this->_crontab = $output;
45 return $this->_crontab ?? [];
46 }
47
48 private function _addCron():array {
49 $output = [];
50 foreach($this->getCrontab() as $line) {
51 $line = trim($line);
52 if(
53 !$line ||
54 preg_match('/no\s*crontab\s*for/i', $line) ||
55 strpos($line, '#') !== false
56 ) continue;
57 $output[] = $line;
58 }
59 $output[] = $this->getCommand();
60 return $output;
61 }
62
63 private function _removeCron():array {
64 $output = [];
65 foreach($this->getCrontab() as $line) if (strpos($line, self::CRON_FILE) === false) $output[] = $line;
66 return $output;
67 }
68
69 public function buildCrontab ($remove = false):string {
70 $output = $remove ? $this->_removeCron() : $this->_addCron();
71 return implode(PHP_EOL, $output) . PHP_EOL;
72 }
73
74
75 private function _createTempCron($remove = false): bool {
76 $crontab = $this->buildCrontab($remove);
77 $old = umask(077);
78 $created = file_put_contents($this->_temp_cron, $crontab);
79 umask ($old);
80 return $created;
81 }
82
83
84 public function crontabExists():bool {
85 foreach($this->getCrontab() as $line) if (strpos($line, self::CRON_FILE) !== false) return true;
86 return false;
87 }
88
89 public function removeCrontab():void {
90
91 if(
92 !$this->crontabExists() || // not found in crontab / cannot read - nothing to do
93 !$this->_createTempCron(true) // cannot create tempcron, cannot continue
94 ) return;
95
96 if(!Execute::run('crontab ' . $this->_temp_cron)) {
97 $config = Factory::getSettingsAutomation();
98 $config->setCronStatusEnabled(0);
99 $config->save();
100 Alert::add('Crontab Removed', 'System level crontab successfully removed', Alert::LEVEL_INFORMATION);
101 }
102 }
103
104 public function addCrontab():void {
105
106 if( $this->crontabExists() || !$this->_createTempCron() ) return;
107
108 if (!Execute::run('crontab ' . $this->_temp_cron)) {
109 $config = Factory::getSettingsAutomation();
110 $config->setCronStatusEnabled(1);
111 $config->save();
112 Alert::add('Crontab Added', 'System level crontab successfully added', Alert::LEVEL_INFORMATION);
113 }
114 }
115
116
117 private function isLinux():bool {
118 return stristr(PHP_OS, 'Linux');
119 }
120
121 private function cleanup():void {
122 if (file_exists($this->_temp_cron)) @unlink($this->_temp_cron);
123 }
124
125 }