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 (!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') || strpos(
$object->fields[$field][
'type'],
'double') || 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 (!is_array($value)) {
192 $typeOfExtraField =
'';
193 if (!empty($extrafields->attributes) && !empty($extrafields->attributes[
$object->table_element])
194 && !empty($extrafields->attributes[
$object->table_element][
'type'])
195 && !empty($extrafields->attributes[
$object->table_element][
'type'][$field])) {
196 $typeOfExtraField = $extrafields->attributes[
$object->table_element][
'type'][$field];
199 if ($typeOfExtraField) {
200 if (strpos($typeOfExtraField,
'int') || strpos($typeOfExtraField,
'double') || in_array($typeOfExtraField, array(
'real',
'price',
'stock'))) {
203 if ($typeOfExtraField ==
'html') {
206 if ($typeOfExtraField ==
'select') {
210 if ($typeOfExtraField ==
'sellist' || $typeOfExtraField ==
'checkbox') {
213 if ($typeOfExtraField ==
'boolean' || $typeOfExtraField ==
'radio') {
216 if ($typeOfExtraField ==
'email') {
219 if ($typeOfExtraField ==
'password') {
227 $newarrayvalue = array();
228 foreach ($value as $tmpkey => $tmpvalue) {
232 return $newarrayvalue;
252 if (empty($properties)) {
257 $arr_properties = explode(
',', $properties);
258 $magic_properties = array();
259 $real_properties = get_object_vars(
$object);
263 foreach ($arr_properties as $key) {
264 if (!array_key_exists($key, $real_properties)) {
268 $magic_properties[$key] =
$object->$key;
274 foreach (get_object_vars(
$object) as $key => $value) {
275 if (!in_array($key, $arr_properties)) {
281 foreach ($magic_properties as $key => $value) {
304 unset(
$object->isextrafieldmanaged);
305 unset(
$object->ismultientitymanaged);
306 unset(
$object->restrictiononfksoc);
309 unset(
$object->pass_indatabase);
323 unset(
$object->TRIGGER_PREFIX);
329 unset(
$object->barcode_type_code);
330 unset(
$object->barcode_type_label);
332 unset(
$object->mode_reglement);
333 unset(
$object->cond_reglement);
341 unset(
$object->timespent_old_duration);
343 unset(
$object->timespent_duration);
344 unset(
$object->timespent_date);
345 unset(
$object->timespent_datehour);
346 unset(
$object->timespent_withhour);
347 unset(
$object->timespent_fk_user);
348 unset(
$object->timespent_note);
349 unset(
$object->fk_delivery_address);
350 unset(
$object->fk_multicurrency);
359 unset(
$object->showphoto_on_popup);
367 unset(
$object->stats_commande);
370 unset(
$object->stats_commande_fournisseur);
371 unset(
$object->stats_reception);
372 unset(
$object->stats_mrptoconsume);
373 unset(
$object->stats_mrptoproduce);
375 unset(
$object->fieldsforcombobox);
378 unset(
$object->skip_update_total);
380 unset(
$object->next_prev_filter);
388 unset(
$object->departement_code);
390 unset(
$object->libelle_statut);
391 unset(
$object->libelle_paiement);
393 unset(
$object->labelStatusShort);
400 if (!isset(
$object->table_element) || ! in_array(
$object->table_element, array(
'expensereport_det',
'ticket'))) {
408 unset(
$object->element_for_permission);
411 unset(
$object->table_element_line);
412 unset(
$object->class_element_line);
414 unset(
$object->linked_objects);
429 $nboflines = count(
$object->lines);
430 for ($i = 0; $i < $nboflines; $i++) {
433 unset(
$object->lines[$i]->contact);
434 unset(
$object->lines[$i]->contact_id);
435 unset(
$object->lines[$i]->country);
436 unset(
$object->lines[$i]->country_id);
437 unset(
$object->lines[$i]->country_code);
438 unset(
$object->lines[$i]->deposit_percent);
439 unset(
$object->lines[$i]->mode_reglement_id);
440 unset(
$object->lines[$i]->mode_reglement_code);
441 unset(
$object->lines[$i]->mode_reglement);
442 unset(
$object->lines[$i]->cond_reglement_id);
443 unset(
$object->lines[$i]->cond_reglement_supplier_id);
444 unset(
$object->lines[$i]->cond_reglement_code);
445 unset(
$object->lines[$i]->cond_reglement);
446 unset(
$object->lines[$i]->fk_delivery_address);
447 unset(
$object->lines[$i]->fk_projet);
448 unset(
$object->lines[$i]->fk_project);
450 unset(
$object->lines[$i]->thirdparty);
451 unset(
$object->lines[$i]->user);
452 unset(
$object->lines[$i]->product);
454 unset(
$object->lines[$i]->model_pdf);
455 unset(
$object->lines[$i]->note_public);
456 unset(
$object->lines[$i]->note_private);
457 unset(
$object->lines[$i]->fk_incoterms);
458 unset(
$object->lines[$i]->label_incoterms);
459 unset(
$object->lines[$i]->location_incoterms);
460 unset(
$object->lines[$i]->name);
461 unset(
$object->lines[$i]->lastname);
462 unset(
$object->lines[$i]->firstname);
463 unset(
$object->lines[$i]->civility_id);
464 unset(
$object->lines[$i]->fk_multicurrency);
465 unset(
$object->lines[$i]->multicurrency_code);
466 unset(
$object->lines[$i]->shipping_method_id);
470 if (!empty(
$object->thirdparty) && is_object(
$object->thirdparty)) {
493 protected static function _checkAccessToResource($resource, $resource_id = 0, $dbtablename =
'', $feature2 =
'', $dbt_keyfield =
'fk_soc', $dbt_select =
'rowid')
497 $featuresarray = array($resource);
498 if (preg_match(
'/&/', $resource)) {
499 $featuresarray = explode(
"&", $resource);
500 } elseif (preg_match(
'/\|/', $resource)) {
501 $featuresarray = explode(
"|", $resource);
505 if (!empty($feature2)) {
506 $feature2 = explode(
"|", $feature2);
509 return checkUserAccessToObject(DolibarrApiAccess::$user, $featuresarray, $resource_id, $dbtablename, $feature2, $dbt_keyfield, $dbt_select);
524 $firstandlastparenthesis = 0;
525 return dolCheckFilters($sqlfilters, $error, $firstandlastparenthesis);
541 return dolForgeSQLCriteriaCallback($matches);
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.
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.
_cleanObjectDatas($object)
Clean sensitive object data @phpstan-template T.
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.
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.