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