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 | } |