PluginProbe
VikBooking Hotel Booking Engine & PMS / trunk
VikBooking Hotel Booking Engine & PMS vtrunk
1.8.15 1.8.14 1.8.13 1.8.12 1.8.11 1.8.10 1.8.9 1.8.6 1.8.7 1.8.8 trunk 1.6.0 1.6.1 1.6.2 1.6.3 1.6.4 1.6.5 1.6.6 1.6.7 1.6.8 1.6.9 1.7.0 1.7.1 1.7.2 1.7.3 All 36 releases
vikbooking / admin / helpers / src / model / cronjob.php

cronjob.php in VikBooking Hotel Booking Engine & PMS trunk, at admin/helpers/src/model/cronjob.php

197 lines 5.2 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * @package VikBooking
4 * @subpackage core
5 * @author E4J s.r.l.
6 * @copyright Copyright (C) 2021 E4J s.r.l. All Rights Reserved.
7 * @license http://www.gnu.org/licenses/gpl-2.0.html GNU/GPL
8 * @link https://vikwp.com
9 */
10
11 // No direct access
12 defined('ABSPATH') or die('No script kiddies please!');
13
14 /**
15 * VikBooking cron job model.
16 *
17 * @since 1.5.10
18 */
19 class VBOModelCronjob extends VBOMvcModel
20 {
21 /**
22 * The database table name.
23 *
24 * @var string
25 */
26 protected $tableName = '#__vikbooking_cronjobs';
27
28 /**
29 * Basic item loading implementation.
30 *
31 * @param mixed $pk An optional primary key value to load the row by, or an array of fields to match.
32 * If not set the instance property value is used.
33 *
34 * @return mixed The record object on success, null otherwise.
35 */
36 public function getItem($pk)
37 {
38 $item = parent::getItem($pk);
39
40 if ($item)
41 {
42 // auto-decode parameters in array format for backward compatibility
43 $item->params = $item->params ? (array) json_decode($item->params, true) : [];
44 // auto-decode flag characters in array format for backward compatibility
45 $item->flag_char = $item->flag_char ? (array) json_decode($item->flag_char, true) : [];
46 }
47
48 return $item;
49 }
50
51 /**
52 * Runs before saving the given data.
53 * It is possible to inject here additional properties to save.
54 *
55 * @param array &$data A reference to the data to save.
56 *
57 * @return boolean True on success, false otherwise.
58 */
59 protected function preflight(array &$data)
60 {
61 if (isset($data['params']))
62 {
63 // sanitize cron job parameters before save
64 foreach ($data['params'] as $setting => $cont)
65 {
66 if (!$setting)
67 {
68 continue;
69 }
70
71 // replace any possible placeholder for special tags
72 $cont = preg_replace_callback("/(<strong class=\"vbo-editor-hl-specialtag\">)([^<]+)(<\/strong>)/", function($match)
73 {
74 return $match[2];
75 }, $cont);
76
77 // update parameter
78 $data['params'][$setting] = $cont;
79 }
80 }
81
82 if (isset($data['logs']))
83 {
84 // make sure the logs length does not exceed the maximum number of allowed characters (65535)
85 $data['logs'] = substr($data['logs'], 0, pow(2, 16) - 1);
86 }
87
88 return parent::preflight($data);
89 }
90
91 /**
92 * Method used to dispatch a cron job.
93 *
94 * @param int|object $cron Either a cron job ID or an object.
95 * @param array $options A configuration array:
96 * - debug bool whether to enter in debug mode;
97 * - key string the secret key to validate;
98 * - strict bool whether to take only published records.
99 *
100 * @return int|bool The cron response code on success, false otherwise.
101 */
102 public function dispatch($cron, array $options = [])
103 {
104 // fetch cron ID
105 $id_cron = is_object($cron) ? @$cron->id : $cron;
106
107 if (!$id_cron || !is_numeric($id_cron))
108 {
109 $this->setError(new InvalidArgumentException('Invalid cron ID', 400));
110 return false;
111 }
112
113 // validate the cron secure key only if explictly provided within the configuration array
114 if (array_key_exists('key', $options) && $options['key'] != md5(VikBooking::getCronKey()))
115 {
116 $this->setError(new InvalidArgumentException('The given secret key does not match', 401));
117 return false;
118 }
119
120 if (!is_object($cron))
121 {
122 // cron ID given, load item details
123 $cron = $this->getItem($id_cron);
124 }
125
126 if (!$cron)
127 {
128 $this->setError(new RuntimeException(sprintf('Unable to find cron [%d]', $id_cron), 404));
129 return false;
130 }
131
132 $options['strict'] = isset($options['strict']) ? (bool) $options['strict'] : true;
133
134 if (!$cron->published && $options['strict'])
135 {
136 $this->setError(new RuntimeException(sprintf('The cron [%d] is not published', $id_cron), 403));
137 return false;
138 }
139
140 try
141 {
142 // attempt to create a new job instance
143 $job = VBOFactory::getCronFactory()->createInstance($cron->class_file, $cron);
144 }
145 catch (Exception $e)
146 {
147 // an error has occurred
148 $this->setError($e);
149 return false;
150 }
151
152 // in case of debug mode, start buffering the text echoed by the cron job
153 if (!empty($options['debug']))
154 {
155 $job->setDebug(true);
156
157 ob_start();
158 }
159
160 $response = (int) $job->run();
161
162 if (!empty($options['debug']))
163 {
164 // register cron output in a property
165 $this->set('output', ob_get_contents());
166
167 ob_end_clean();
168 }
169
170 // register logs in a model property to allow the client to easily access it
171 $this->set('log', $job->getLog());
172
173 // fetch cron job data
174 $data = $job->getData();
175
176 // update last execution time
177 $data->last_exec = time();
178
179 if ($this->get('log'))
180 {
181 // prepend new logs to the existing ones
182 $data->logs = date('c') . "\n" . trim($this->get('log')) . "\n<hr />\n" . $data->logs;
183 }
184
185 // commit cron job changes
186 if (!$this->save((array) $data))
187 {
188 // something went wrong, try to display the error message within the ouput
189 $error = $this->getError($last = null, $string = true);
190 $output = "<p style=\"color: var(--vbo-red-color);\">An error has occurred while saving the cron job. $error</p>";
191 $this->set('output', $this->get('output', '') . $output);
192 }
193
194 return $response;
195 }
196 }
197