dolibarr 25.0.0-alpha
api_cronjobs.class.php
1<?php
2/* Copyright (C) 2026 Frédéric France <frederic.france@free.fr>
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 3 of the License, or
7 * (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program. If not, see <https://www.gnu.org/licenses/>.
16 */
17
18use Luracast\Restler\RestException;
19
20require_once DOL_DOCUMENT_ROOT.'/cron/class/cronjob.class.php';
21
28class Cronjobs extends DolibarrApi
29{
33 public static $FIELDS = array(
34 'jobtype',
35 'label',
36 );
37
41 public $cronjob;
42
46 public function __construct()
47 {
48 global $db;
49 $this->db = $db;
50 $this->cronjob = new Cronjob($this->db);
51 }
52
63 public function get($id)
64 {
65 if (!DolibarrApiAccess::$user->hasRight('cron', 'read')) {
66 throw new RestException(403);
67 }
68
69 $result = $this->cronjob->fetch($id);
70 if (!$result) {
71 throw new RestException(404, 'Cron job not found');
72 }
73
74 if (!DolibarrApi::_checkAccessToResource('cronjob', $this->cronjob->id)) {
75 throw new RestException(403, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
76 }
77
78 return $this->_cleanObjectDatas($this->cronjob);
79 }
80
100 public function index($sortfield = "t.rowid", $sortorder = 'ASC', $limit = 100, $page = 0, $status = -1, $sqlfilters = '', $properties = '', $pagination_data = false)
101 {
102 if (!DolibarrApiAccess::$user->hasRight('cron', 'read')) {
103 throw new RestException(403);
104 }
105
106 $obj_ret = array();
107
108 if ($page < 0) {
109 $page = 0;
110 }
111 $offset = $limit * $page;
112
113 $result = $this->cronjob->fetchAll($sortorder, $sortfield, $limit, $offset, $status, $sqlfilters);
114 if ($result < 0) {
115 throw new RestException(503, 'Error when retrieving cron job list: '.$this->cronjob->errorsToString());
116 }
117
118 foreach ($this->cronjob->lines as $cronjob_static) {
119 $obj_ret[] = $this->_filterObjectProperties($this->_cleanObjectDatas($cronjob_static), $properties);
120 }
121
122 if ($pagination_data) {
123 $tmp = $obj_ret;
124 $obj_ret = [];
125
126 $obj_ret['data'] = $tmp;
127 $obj_ret['pagination'] = [
128 'total' => count($tmp),
129 'page' => $page,
130 'page_count' => ceil(count($tmp) / $limit),
131 'limit' => $limit
132 ];
133 }
134
135 return $obj_ret;
136 }
137
148 public function post($request_data = null)
149 {
150 if (!DolibarrApiAccess::$user->hasRight('cron', 'write') || !DolibarrApiAccess::$user->admin) {
151 throw new RestException(403, "Insufficient rights - creating a cron job requires an admin user");
152 }
153
154 $result = $this->_validate($request_data);
155
156 foreach ($request_data as $field => $value) {
157 $this->cronjob->$field = $this->_checkValForAPI($field, $value, $this->cronjob);
158 }
159
160 if ($this->cronjob->create(DolibarrApiAccess::$user) < 0) {
161 throw new RestException(500, "Error creating cron job", array_merge(array($this->cronjob->error), $this->cronjob->errors));
162 }
163
164 return $this->cronjob->id;
165 }
166
178 public function put($id, $request_data = null)
179 {
180 if (!DolibarrApiAccess::$user->hasRight('cron', 'write') || !DolibarrApiAccess::$user->admin) {
181 throw new RestException(403, "Insufficient rights - updating a cron job requires an admin user");
182 }
183
184 $result = $this->cronjob->fetch($id);
185 if (!$result) {
186 throw new RestException(404, 'Cron job not found');
187 }
188
189 if (!DolibarrApi::_checkAccessToResource('cronjob', $this->cronjob->id)) {
190 throw new RestException(403, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
191 }
192
193 foreach ($request_data as $field => $value) {
194 if ($field == 'id') {
195 continue;
196 }
197 $this->cronjob->$field = $this->_checkValForAPI($field, $value, $this->cronjob);
198 }
199
200 if ($this->cronjob->update(DolibarrApiAccess::$user) > 0) {
201 return $this->get($id);
202 } else {
203 throw new RestException(500, $this->cronjob->errorsToString());
204 }
205 }
206
217 public function delete($id)
218 {
219 if (!DolibarrApiAccess::$user->hasRight('cron', 'delete')) {
220 throw new RestException(403);
221 }
222
223 $result = $this->cronjob->fetch($id);
224 if (!$result) {
225 throw new RestException(404, 'Cron job not found');
226 }
227
228 if (!DolibarrApi::_checkAccessToResource('cronjob', $this->cronjob->id)) {
229 throw new RestException(403, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
230 }
231
232 if (!$this->cronjob->delete(DolibarrApiAccess::$user)) {
233 throw new RestException(500, 'Error when deleting cron job: '.$this->cronjob->errorsToString());
234 }
235
236 return array(
237 'success' => array(
238 'code' => 200,
239 'message' => 'Cron job deleted'
240 )
241 );
242 }
243
261 public function run($id, $securitykey = '')
262 {
263 if (!DolibarrApiAccess::$user->hasRight('cron', 'write') || !DolibarrApiAccess::$user->admin) {
264 throw new RestException(403, "Insufficient rights - running a cron job requires an admin user");
265 }
266
267 if (getDolGlobalString('CRON_KEY') && getDolGlobalString('CRON_KEY') != $securitykey) {
268 throw new RestException(403, 'Security key is wrong');
269 }
270
271 $result = $this->cronjob->fetch($id);
272 if (!$result) {
273 throw new RestException(404, 'Cron job not found');
274 }
275
276 if (!DolibarrApi::_checkAccessToResource('cronjob', $this->cronjob->id)) {
277 throw new RestException(403, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
278 }
279
280 $now = dol_now();
281
282 $result = $this->cronjob->run_jobs(DolibarrApiAccess::$user->login);
283 if ($result < 0) {
284 throw new RestException(500, 'Error when running cron job: '.$this->cronjob->errorsToString());
285 }
286
287 $result = $this->cronjob->reprogram_jobs(DolibarrApiAccess::$user->login, $now);
288 if ($result <= 0) {
289 throw new RestException(500, 'Job ran but failed to reprogram next run: '.$this->cronjob->errorsToString());
290 }
291
292 return $this->_cleanObjectDatas($this->cronjob);
293 }
294
295 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.PublicUnderscore
305 protected function _cleanObjectDatas($object)
306 {
307 // phpcs:enable
308 $object = parent::_cleanObjectDatas($object);
309
310 unset($object->md5params);
311
312 return $object;
313 }
314
322 private function _validate($data)
323 {
324 if ($data === null) {
325 $data = array();
326 }
327 $cronjob = array();
328 foreach (Cronjobs::$FIELDS as $field) {
329 if (!isset($data[$field])) {
330 throw new RestException(400, $field." field missing");
331 }
332 $cronjob[$field] = $data[$field];
333 }
334 return $cronjob;
335 }
336}
$id
Support class for third parties, contacts, members, users or resources.
Definition account.php:47
if(! $sortfield) if(! $sortorder) $object
Definition account.php:100
Cron Job class.
run($id, $securitykey='')
Run a cron job now.
index($sortfield="t.rowid", $sortorder='ASC', $limit=100, $page=0, $status=-1, $sqlfilters='', $properties='', $pagination_data=false)
List cron jobs.
__construct()
Constructor.
put($id, $request_data=null)
Update cron job fields.
_cleanObjectDatas($object)
Clean sensible object datas @phpstan-template T.
post($request_data=null)
Create cron job object.
_validate($data)
Validate fields before create or update object.
Class for API REST v1.
Definition api.class.php:35
_filterObjectProperties($object, $properties)
Filter properties that will be returned on object.
_checkValForAPI($field, $value, $object)
Check and convert a string depending on its type/name.
static _checkAccessToResource($resource, $resource_id=0, $dbtablename='', $feature2='', $dbt_keyfield='fk_soc', $dbt_select='rowid', $parenttableforentity='')
Check access by user to a given resource.
dol_now($mode='gmt')
Return date for now.
getDolGlobalString($key, $default='')
Return a Dolibarr global constant string value.