dolibarr 19.0.4
api.class.php
1<?php
2/* Copyright (C) 2015 Jean-François Ferry <jfefe@aternatik.fr>
3 * Copyright (C) 2016 Laurent Destailleur <eldy@users.sourceforge.net>
4 * Copyright (C) 2020 Frédéric France <frederic.france@netlogic.fr>
5 * Copyright (C) 2025 William Mead <william@m34d.com>
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 3 of the License, or
10 * (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program. If not, see <https://www.gnu.org/licenses/>.
19 */
20
21use Luracast\Restler\Restler;
22use Luracast\Restler\RestException;
23use Luracast\Restler\Defaults;
24use Luracast\Restler\Format\UploadFormat;
25
26require_once DOL_DOCUMENT_ROOT.'/user/class/user.class.php';
27
32{
36 protected $db;
37
41 public $r;
42
50 public function __construct($db, $cachedir = '', $refreshCache = false)
51 {
52 global $conf, $dolibarr_main_url_root;
53
54 if (empty($cachedir)) {
55 $cachedir = $conf->api->dir_temp;
56 }
57 Defaults::$cacheDirectory = $cachedir;
58
59 $this->db = $db;
60 $production_mode = (!getDolGlobalString('API_PRODUCTION_MODE') ? false : true);
61 $this->r = new Restler($production_mode, $refreshCache);
62
63 $urlwithouturlroot = preg_replace('/'.preg_quote(DOL_URL_ROOT, '/').'$/i', '', trim($dolibarr_main_url_root));
64 $urlwithroot = $urlwithouturlroot.DOL_URL_ROOT; // This is to use external domain name found into config file
65
66 $urlwithouturlrootautodetect = preg_replace('/'.preg_quote(DOL_URL_ROOT, '/').'$/i', '', trim(DOL_MAIN_URL_ROOT));
67 $urlwithrootautodetect = $urlwithouturlroot.DOL_URL_ROOT; // This is to use local domain autodetected by dolibarr from url
68
69 $this->r->setBaseUrls($urlwithouturlroot, $urlwithouturlrootautodetect);
70 $this->r->setAPIVersion(1);
71 //$this->r->setSupportedFormats('json');
72 //$this->r->setSupportedFormats('jsonFormat');
73 }
74
75 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.PublicUnderscore
86 protected function _checkValForAPI($field, $value, $object)
87 {
88 // phpcs:enable
89 if (!is_array($value)) {
90 // TODO Use type detected in $object->fields if $object known and we can
91 if (in_array($field, array('note', 'note_private', 'note_public', 'desc', 'description'))) {
92 return sanitizeVal($value, 'restricthtml');
93 } else {
94 return sanitizeVal($value, 'alphanohtml');
95 }
96 } else {
97 // TODO Recall _checkValForAPI for each element of array
98
99 return $value;
100 }
101 }
102
103 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.PublicUnderscore
111 protected function _filterObjectProperties($object, $properties)
112 {
113 // If properties is empty, we return all properties
114 if (empty($properties)) {
115 return $object;
116 }
117 // Else we filter properties
118 foreach (get_object_vars($object) as $key => $value) {
119 if (!in_array($key, explode(',', $properties))) {
120 unset($object->$key);
121 }
122 }
123 return $object;
124 }
125
126 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.PublicUnderscore
133 protected function _cleanObjectDatas($object)
134 {
135 // phpcs:enable
136 // Remove $db object property for object
137 unset($object->db);
138 unset($object->isextrafieldmanaged);
139 unset($object->ismultientitymanaged);
140 unset($object->restrictiononfksoc);
141 unset($object->table_rowid);
142 unset($object->pass);
143 unset($object->pass_indatabase);
144
145 // Remove linkedObjects. We should already have and keep only linkedObjectsIds that avoid huge responses
146 unset($object->linkedObjects);
147 //unset($object->lines[$i]->linked_objects); // This is the array to create linked object during create
148
149 unset($object->fields);
150 unset($object->oldline);
151
152 unset($object->error);
153 unset($object->errors);
154 unset($object->errorhidden);
155
156 unset($object->ref_previous);
157 unset($object->ref_next);
158 unset($object->imgWidth);
159 unset($object->imgHeight);
160 unset($object->barcode_type_code);
161 unset($object->barcode_type_label);
162
163 unset($object->mode_reglement); // We use mode_reglement_id now
164 unset($object->cond_reglement); // We use cond_reglement_id now
165 unset($object->note); // We use note_public or note_private now
166 unset($object->contact); // We use contact_id now
167 unset($object->thirdparty); // We use thirdparty_id or fk_soc or socid now
168
169 unset($object->projet); // Should be fk_project
170 unset($object->project); // Should be fk_project
171 unset($object->fk_projet); // Should be fk_project
172 unset($object->author); // Should be fk_user_author
173 unset($object->timespent_old_duration);
174 unset($object->timespent_id);
175 unset($object->timespent_duration);
176 unset($object->timespent_date);
177 unset($object->timespent_datehour);
178 unset($object->timespent_withhour);
179 unset($object->timespent_fk_user);
180 unset($object->timespent_note);
181 unset($object->fk_delivery_address);
182 unset($object->modelpdf);
183 unset($object->sendtoid);
184 unset($object->name_bis);
185 unset($object->newref);
186 unset($object->alreadypaid);
187 unset($object->openid);
188
189 //unset($object->labelStatus);
190 //unset($object->labelStatusShort);
191
192 unset($object->stats_propale);
193 unset($object->stats_commande);
194 unset($object->stats_contrat);
195 unset($object->stats_facture);
196 unset($object->stats_commande_fournisseur);
197 unset($object->stats_reception);
198 unset($object->stats_mrptoconsume);
199 unset($object->stats_mrptoproduce);
200
201 unset($object->fieldsforcombobox);
202 unset($object->regeximgext);
203
204 unset($object->skip_update_total);
205 unset($object->context);
206 unset($object->next_prev_filter);
207
208 unset($object->region);
209 unset($object->region_code);
210 unset($object->country);
211 unset($object->state);
212 unset($object->state_code);
213 unset($object->departement);
214 unset($object->departement_code);
215
216 unset($object->libelle_statut);
217 unset($object->libelle_paiement);
218
219 unset($object->prefix_comm);
220
221 if (!isset($object->table_element) || ! in_array($object->table_element, array('expensereport_det', 'ticket'))) {
222 unset($object->comments);
223 }
224
225 unset($object->element);
226 unset($object->element_for_permission);
227 unset($object->fk_element);
228 unset($object->table_element);
229 unset($object->table_element_line);
230 unset($object->class_element_line);
231 unset($object->picto);
232
233 // Remove the $oldcopy property because it is not supported by the JSON
234 // encoder. The following error is generated when trying to serialize
235 // it: "Error encoding/decoding JSON: Type is not supported"
236 // Note: Event if this property was correctly handled by the JSON
237 // encoder, it should be ignored because keeping it would let the API
238 // have a very strange behavior: calling PUT and then GET on the same
239 // resource would give different results:
240 // PUT /objects/{id} -> returns object with oldcopy = previous version of the object
241 // GET /objects/{id} -> returns object with oldcopy empty
242 unset($object->oldcopy);
243
244 // If object has lines, remove $db property
245 if (isset($object->lines) && is_array($object->lines) && count($object->lines) > 0) {
246 $nboflines = count($object->lines);
247 for ($i = 0; $i < $nboflines; $i++) {
248 $this->_cleanObjectDatas($object->lines[$i]);
249
250 unset($object->lines[$i]->contact);
251 unset($object->lines[$i]->contact_id);
252 unset($object->lines[$i]->country);
253 unset($object->lines[$i]->country_id);
254 unset($object->lines[$i]->country_code);
255 unset($object->lines[$i]->mode_reglement_id);
256 unset($object->lines[$i]->mode_reglement_code);
257 unset($object->lines[$i]->mode_reglement);
258 unset($object->lines[$i]->cond_reglement_id);
259 unset($object->lines[$i]->cond_reglement_code);
260 unset($object->lines[$i]->cond_reglement);
261 unset($object->lines[$i]->fk_delivery_address);
262 unset($object->lines[$i]->fk_projet);
263 unset($object->lines[$i]->fk_project);
264 unset($object->lines[$i]->thirdparty);
265 unset($object->lines[$i]->user);
266 unset($object->lines[$i]->model_pdf);
267 unset($object->lines[$i]->modelpdf);
268 unset($object->lines[$i]->note_public);
269 unset($object->lines[$i]->note_private);
270 unset($object->lines[$i]->fk_incoterms);
271 unset($object->lines[$i]->label_incoterms);
272 unset($object->lines[$i]->location_incoterms);
273 unset($object->lines[$i]->name);
274 unset($object->lines[$i]->lastname);
275 unset($object->lines[$i]->firstname);
276 unset($object->lines[$i]->civility_id);
277 unset($object->lines[$i]->fk_multicurrency);
278 unset($object->lines[$i]->multicurrency_code);
279 unset($object->lines[$i]->shipping_method_id);
280 }
281 }
282
283 if (!empty($object->thirdparty) && is_object($object->thirdparty)) {
284 $this->_cleanObjectDatas($object->thirdparty);
285 }
286
287 if (!empty($object->product) && is_object($object->product)) {
288 $this->_cleanObjectDatas($object->product);
289 }
290
291 return $object;
292 }
293
294 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.PublicUnderscore
306 protected static function _checkAccessToResource($resource, $resource_id = 0, $dbtablename = '', $feature2 = '', $dbt_keyfield = 'fk_soc', $dbt_select = 'rowid')
307 {
308 // phpcs:enable
309 // Features/modules to check
310 $featuresarray = array($resource);
311 if (preg_match('/&/', $resource)) {
312 $featuresarray = explode("&", $resource);
313 } elseif (preg_match('/\|/', $resource)) {
314 $featuresarray = explode("|", $resource);
315 }
316
317 // More subfeatures to check
318 if (!empty($feature2)) {
319 $feature2 = explode("|", $feature2);
320 }
321
322 return checkUserAccessToObject(DolibarrApiAccess::$user, $featuresarray, $resource_id, $dbtablename, $feature2, $dbt_keyfield, $dbt_select);
323 }
324
325 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.PublicUnderscore
334 protected function _checkFilters($sqlfilters, &$error = '')
335 {
336 // phpcs:enable
337
338 return dolCheckFilters($sqlfilters, $error);
339 }
340
341 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
342 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.PublicUnderscore
352 protected static function _forge_criteria_callback($matches)
353 {
354 return dolForgeCriteriaCallback($matches);
355 }
356}
Class for API REST v1.
Definition api.class.php:32
__construct($db, $cachedir='', $refreshCache=false)
Constructor.
Definition api.class.php:50
_filterObjectProperties($object, $properties)
Filter properties that will be returned on object.
static _checkAccessToResource($resource, $resource_id=0, $dbtablename='', $feature2='', $dbt_keyfield='fk_soc', $dbt_select='rowid')
Check access by user to a given resource.
_checkFilters($sqlfilters, &$error='')
Return if a $sqlfilters parameter is valid Function no more used.
_checkValForAPI($field, $value, $object)
Check and convert a string depending on its type/name.
Definition api.class.php:86
_cleanObjectDatas($object)
Clean sensible object datas.
static _forge_criteria_callback($matches)
Function to forge a SQL criteria from a Generic filter string.
dolCheckFilters($sqlfilters, &$error='')
Return if a $sqlfilters parameter has a valid balance of parenthesis.
dolForgeCriteriaCallback($matches)
Function to forge a SQL criteria from a Dolibarr filter syntax string.
getDolGlobalString($key, $default='')
Return dolibarr global constant string value.
sanitizeVal($out='', $check='alphanohtml', $filter=null, $options=null)
Return a sanitized or empty value after checking value against a rule.
checkUserAccessToObject($user, array $featuresarray, $object=0, $tableandshare='', $feature2='', $dbt_keyfield='', $dbt_select='rowid', $parenttableforentity='')
Check that access by a given user to an object is ok.