24use Luracast\Restler\Restler;
25use Luracast\Restler\Defaults;
26use Luracast\Restler\RestException;
28require_once DOL_DOCUMENT_ROOT.
'/user/class/user.class.php';
53 public function __construct($db, $cachedir =
'', $refreshCache =
false)
57 if (empty($cachedir)) {
58 $cachedir =
$conf->api->dir_temp;
60 Defaults::$cacheDirectory = $cachedir;
66 if ($production_mode) {
68 include_once DOL_DOCUMENT_ROOT.
'/core/lib/files.lib.php';
70 dol_mkdir(Defaults::$cacheDirectory, DOL_DATA_ROOT);
73 dol_syslog(
"Debug API construct::cacheDirectory=".Defaults::$cacheDirectory, LOG_DEBUG, 0,
'_api');
77 $this->r =
new Restler($production_mode, $refreshCache);
80 $urlwithroot = $urlwithouturlroot.DOL_URL_ROOT;
82 $urlwithouturlrootautodetect = preg_replace(
'/'.preg_quote(DOL_URL_ROOT,
'/').
'$/i',
'', trim(DOL_MAIN_URL_ROOT));
83 $urlwithrootautodetect = $urlwithouturlroot.DOL_URL_ROOT;
85 $this->r->setBaseUrls($urlwithouturlroot, $urlwithouturlrootautodetect);
86 $this->r->setAPIVersion(1);
104 if (!preg_match(
'/^[a-zA-Z0-9_]+$/', $field)) {
105 throw new RestException(400,
'Parameter '.$field.
' is not allowed in request');
108 if (!is_array($value)) {
110 if (in_array($field, array(
111 'db',
'table_element',
'table_rowid',
'table_ref_field',
'table_element_line',
'element',
'fk_element',
'element_for_permission',
'class_element_line',
112 'fields',
'TRIGGER_PREFIX',
'picto',
113 'restrictiononfksoc',
'ismultientitymanaged',
'isextrafieldmanaged',
114 'module',
'error',
'errorhidden',
'errors',
'warning',
'warnings',
'validateFieldsErrors',
115 'oldcopy',
'oldref',
'newref',
'context',
116 'actionmsg',
'actionmsg2',
'thirdparty',
'user',
117 'tpl',
'extraparams',
118 'childtables',
'childtablesoncascade'
120 throw new RestException(400,
'Parameter '.$field.
' is not allowed in request');
122 if (in_array($field, array(
'specimen'))) {
124 dol_syslog(
'Debug API _checkValForAPI, found use of field specimen', LOG_DEBUG, 0,
'_api');
128 if (!empty(
$object->fields) && !empty(
$object->fields[$field]) && !empty(
$object->fields[$field][
'type'])) {
129 if (strpos(
$object->fields[$field][
'type'],
'int') === 0 || strpos(
$object->fields[$field][
'type'],
'double') === 0 || in_array(
$object->fields[$field][
'type'], array(
'real',
'price',
'stock'))) {
132 if (
$object->fields[$field][
'type'] ==
'html') {
135 if (
$object->fields[$field][
'type'] ==
'select') {
139 if (
$object->fields[$field][
'type'] ==
'sellist' ||
$object->fields[$field][
'type'] ==
'checkbox') {
142 if (
$object->fields[$field][
'type'] ==
'boolean' ||
$object->fields[$field][
'type'] ==
'radio') {
145 if (
$object->fields[$field][
'type'] ==
'email') {
148 if (
$object->fields[$field][
'type'] ==
'password') {
157 if (preg_match(
'/^fk_/i', $field)) {
161 if (in_array($field, array(
'note',
'note_private',
'note_public',
'desc',
'description'))) {
167 $newarrayvalue = array();
168 foreach ($value as $tmpkey => $tmpvalue) {
172 return $newarrayvalue;
190 if (!preg_match(
'/^[a-zA-Z0-9_]+$/', $field)) {
191 throw new RestException(400,
'Parameter '.$field.
' is not allowed in request');
194 if (!is_array($value)) {
196 $typeOfExtraField =
'';
197 if (!empty($extrafields->attributes) && !empty($extrafields->attributes[
$object->table_element])
198 && !empty($extrafields->attributes[
$object->table_element][
'type'])
199 && !empty($extrafields->attributes[
$object->table_element][
'type'][$field])) {
200 $typeOfExtraField = $extrafields->attributes[
$object->table_element][
'type'][$field];
203 if ($typeOfExtraField) {
204 if (strpos($typeOfExtraField,
'int') === 0 || strpos($typeOfExtraField,
'double') === 0 || in_array($typeOfExtraField, array(
'real',
'price',
'stock'))) {
207 if ($typeOfExtraField ==
'html') {
210 if ($typeOfExtraField ==
'select') {
214 if ($typeOfExtraField ==
'sellist' || $typeOfExtraField ==
'checkbox') {
217 if ($typeOfExtraField ==
'boolean' || $typeOfExtraField ==
'radio') {
220 if ($typeOfExtraField ==
'email') {
223 if ($typeOfExtraField ==
'password') {
231 $newarrayvalue = array();
232 foreach ($value as $tmpkey => $tmpvalue) {
236 return $newarrayvalue;
256 if (empty($properties)) {
261 $arr_properties = explode(
',', $properties);
262 $magic_properties = array();
263 $real_properties = get_object_vars(
$object);
267 foreach ($arr_properties as $key) {
268 if (!array_key_exists($key, $real_properties)) {
272 $magic_properties[$key] =
$object->$key;
278 foreach (get_object_vars(
$object) as $key => $value) {
279 if (!in_array($key, $arr_properties)) {
285 foreach ($magic_properties as $key => $value) {
308 unset(
$object->isextrafieldmanaged);
309 unset(
$object->ismultientitymanaged);
310 unset(
$object->restrictiononfksoc);
313 unset(
$object->pass_indatabase);
314 unset(
$object->pass_indatabase_crypted);
328 unset(
$object->TRIGGER_PREFIX);
334 unset(
$object->barcode_type_code);
335 unset(
$object->barcode_type_label);
337 unset(
$object->mode_reglement);
338 unset(
$object->cond_reglement);
346 unset(
$object->timespent_old_duration);
348 unset(
$object->timespent_duration);
349 unset(
$object->timespent_date);
350 unset(
$object->timespent_datehour);
351 unset(
$object->timespent_withhour);
352 unset(
$object->timespent_fk_user);
353 unset(
$object->timespent_note);
354 unset(
$object->fk_delivery_address);
355 unset(
$object->fk_multicurrency);
364 unset(
$object->showphoto_on_popup);
372 unset(
$object->stats_commande);
375 unset(
$object->stats_commande_fournisseur);
376 unset(
$object->stats_reception);
377 unset(
$object->stats_mrptoconsume);
378 unset(
$object->stats_mrptoproduce);
380 unset(
$object->fieldsforcombobox);
383 unset(
$object->skip_update_total);
385 unset(
$object->next_prev_filter);
393 unset(
$object->departement_code);
395 unset(
$object->libelle_statut);
396 unset(
$object->libelle_paiement);
398 unset(
$object->labelStatusShort);
405 if (!isset(
$object->table_element) || ! in_array(
$object->table_element, array(
'expensereport_det',
'ticket'))) {
413 unset(
$object->element_for_permission);
416 unset(
$object->table_element_line);
417 unset(
$object->class_element_line);
419 unset(
$object->linked_objects);
434 $nboflines = count(
$object->lines);
435 for ($i = 0; $i < $nboflines; $i++) {
438 unset(
$object->lines[$i]->contact);
439 unset(
$object->lines[$i]->contact_id);
440 unset(
$object->lines[$i]->country);
441 unset(
$object->lines[$i]->country_id);
442 unset(
$object->lines[$i]->country_code);
443 unset(
$object->lines[$i]->deposit_percent);
444 unset(
$object->lines[$i]->mode_reglement_id);
445 unset(
$object->lines[$i]->mode_reglement_code);
446 unset(
$object->lines[$i]->mode_reglement);
447 unset(
$object->lines[$i]->cond_reglement_id);
448 unset(
$object->lines[$i]->cond_reglement_supplier_id);
449 unset(
$object->lines[$i]->cond_reglement_code);
450 unset(
$object->lines[$i]->cond_reglement);
451 unset(
$object->lines[$i]->fk_delivery_address);
452 unset(
$object->lines[$i]->fk_projet);
453 unset(
$object->lines[$i]->fk_project);
455 unset(
$object->lines[$i]->thirdparty);
456 unset(
$object->lines[$i]->user);
457 unset(
$object->lines[$i]->product);
459 unset(
$object->lines[$i]->model_pdf);
460 unset(
$object->lines[$i]->note_public);
461 unset(
$object->lines[$i]->note_private);
462 unset(
$object->lines[$i]->fk_incoterms);
463 unset(
$object->lines[$i]->label_incoterms);
464 unset(
$object->lines[$i]->location_incoterms);
465 unset(
$object->lines[$i]->name);
466 unset(
$object->lines[$i]->lastname);
467 unset(
$object->lines[$i]->firstname);
468 unset(
$object->lines[$i]->civility_id);
469 unset(
$object->lines[$i]->fk_multicurrency);
470 unset(
$object->lines[$i]->multicurrency_code);
471 unset(
$object->lines[$i]->shipping_method_id);
475 if (!empty(
$object->thirdparty) && is_object(
$object->thirdparty)) {
499 protected static function _checkAccessToResource($resource, $resource_id = 0, $dbtablename =
'', $feature2 =
'', $dbt_keyfield =
'fk_soc', $dbt_select =
'rowid', $parenttableforentity =
'')
503 $featuresarray = array($resource);
504 if (preg_match(
'/&/', $resource)) {
505 $featuresarray = explode(
"&", $resource);
506 } elseif (preg_match(
'/\|/', $resource)) {
507 $featuresarray = explode(
"|", $resource);
511 if (!empty($feature2)) {
512 $feature2 = explode(
"|", $feature2);
515 return checkUserAccessToObject(DolibarrApiAccess::$user, $featuresarray, $resource_id, $dbtablename, $feature2, $dbt_keyfield, $dbt_select, $parenttableforentity);
530 $firstandlastparenthesis = 0;
if(! $sortfield) if(! $sortorder) $object
global $dolibarr_main_url_root
__construct($db, $cachedir='', $refreshCache=false)
Constructor.
_checkValExtrafieldsForAPI($field, $value, $object)
Check and convert a string depending on its type/name.
_filterObjectProperties($object, $properties)
Filter properties that will be returned on object.
_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.
_cleanObjectDatas($object)
Clean sensitive object data @phpstan-template T.
static _checkAccessToResource($resource, $resource_id=0, $dbtablename='', $feature2='', $dbt_keyfield='fk_soc', $dbt_select='rowid', $parenttableforentity='')
Check access by user to a given resource.
static _forge_criteria_callback($matches)
Function to forge a SQL criteria from a Generic filter string.
dol_is_dir($folder)
Test if filename is a directory.
dolCheckFilters($sqlfilters, &$error='', &$parenthesislevel=0)
Return if a $sqlfilters parameter has a valid balance of parenthesis.
dolForgeSQLCriteriaCallback($matches)
Function to forge a SQL criteria from a USF (Universal Filter Syntax) string.
getDolGlobalBool($key, $default=false)
Return a Dolibarr global constant boolean value.
getDolGlobalString($key, $default='')
Return a 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.
dol_syslog($message, $level=LOG_INFO, $ident=0, $suffixinfilename='', $restricttologhandler='', $logcontext=null)
Write log message into outputs.
dol_mkdir($dir, $dataroot='', $newmask='')
Creation of a directory (this can create recursive subdir)
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.