1 <?php
  2 
  3 /**
  4  * Cronjob Addon.
  5  *
  6  * @author gharlan[at]web[dot]de Gregor Harlan
  7  *
  8  * @package redaxo\cronjob
  9  */
 10 
 11 class rex_cronjob_manager
 12 {
 13     private static $types = [
 14         'rex_cronjob_phpcode',
 15         'rex_cronjob_phpcallback',
 16         'rex_cronjob_urlrequest',
 17     ];
 18 
 19     private $message = '';
 20     private $cronjob;
 21     private $name;
 22     private $id;
 23 
 24     public static function factory()
 25     {
 26         return new self();
 27     }
 28 
 29     public function setMessage($message)
 30     {
 31         $this->message = $message;
 32     }
 33 
 34     public function getMessage()
 35     {
 36         return $this->message;
 37     }
 38 
 39     public function hasMessage()
 40     {
 41         return !empty($this->message);
 42     }
 43 
 44     public function setCronjob($cronjob)
 45     {
 46         $this->cronjob = $cronjob;
 47     }
 48 
 49     public function tryExecute($cronjob, $name = '', $params = [], $log = true, $id = null)
 50     {
 51         $success = $cronjob instanceof rex_cronjob;
 52         if (!$success) {
 53             if (is_object($cronjob)) {
 54                 $message = 'Invalid cronjob class "' . get_class($cronjob) . '"';
 55             } else {
 56                 $message = 'Class "' . $cronjob . '" not found';
 57             }
 58         } else {
 59             $this->name = $name;
 60             $this->id = $id;
 61             $this->cronjob = $cronjob;
 62             $type = $cronjob->getType();
 63             if (is_array($params)) {
 64                 foreach ($params as $key => $value) {
 65                     $cronjob->setParam(str_replace($type . '_', '', $key), $value);
 66                 }
 67             }
 68 
 69             $message = '';
 70             try {
 71                 $success = $cronjob->execute();
 72                 $message = $cronjob->getMessage();
 73             } catch (Throwable $t) {
 74                 $success = false;
 75                 $message = $t->getMessage();
 76             } catch (Exception $e) {
 77                 $success = false;
 78                 $message = $e->getMessage();
 79             }
 80 
 81             if ($message == '' && !$success) {
 82                 $message = 'Unknown error';
 83             }
 84         }
 85 
 86         if ($log) {
 87             $this->log($success, $message);
 88         }
 89 
 90         $this->setMessage(rex_escape($message));
 91         $this->cronjob = null;
 92         $this->id = null;
 93 
 94         return $success;
 95     }
 96 
 97     public function log($success, $message)
 98     {
 99         $name = $this->name;
100         if (!$name) {
101             if ($this->cronjob instanceof rex_cronjob) {
102                 $name = rex::isBackend() ? $this->cronjob->getTypeName() : $this->cronjob->getType();
103             } else {
104                 $name = '[no name]';
105             }
106         }
107         $log = new rex_log_file(rex_path::addonData('cronjob', 'cronjob.log'), 2000000);
108         $data = [
109             ($success ? 'SUCCESS' : 'ERROR'),
110             ($this->id ?: '--'),
111             $name,
112             strip_tags($message),
113         ];
114         $log->add($data);
115     }
116 
117     public static function getTypes()
118     {
119         return self::$types;
120     }
121 
122     public static function registerType($class)
123     {
124         self::$types[] = $class;
125     }
126 
127     public static function getCurrentEnvironment()
128     {
129         if (defined('REX_CRONJOB_SCRIPT') && REX_CRONJOB_SCRIPT) {
130             return 'script';
131         }
132 
133         return rex::isBackend() ? 'backend' : 'frontend';
134     }
135 }
136