dolibarr  13.0.2
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  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 3 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program. If not, see <https://www.gnu.org/licenses/>.
18  */
19 
20 use Luracast\Restler\Restler;
21 use Luracast\Restler\RestException;
22 use Luracast\Restler\Defaults;
23 use Luracast\Restler\Format\UploadFormat;
24 
25 require_once DOL_DOCUMENT_ROOT.'/user/class/user.class.php';
26 
31 {
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)) $cachedir = $conf->api->dir_temp;
55  Defaults::$cacheDirectory = $cachedir;
56 
57  $this->db = $db;
58  $production_mode = (empty($conf->global->API_PRODUCTION_MODE) ? false : true);
59  $this->r = new Restler($production_mode, $refreshCache);
60 
61  $urlwithouturlroot = preg_replace('/'.preg_quote(DOL_URL_ROOT, '/').'$/i', '', trim($dolibarr_main_url_root));
62  $urlwithroot = $urlwithouturlroot.DOL_URL_ROOT; // This is to use external domain name found into config file
63 
64  $urlwithouturlrootautodetect = preg_replace('/'.preg_quote(DOL_URL_ROOT, '/').'$/i', '', trim(DOL_MAIN_URL_ROOT));
65  $urlwithrootautodetect = $urlwithouturlroot.DOL_URL_ROOT; // This is to use local domain autodetected by dolibarr from url
66 
67  $this->r->setBaseUrls($urlwithouturlroot, $urlwithouturlrootautodetect);
68  $this->r->setAPIVersion(1);
69  //$this->r->setSupportedFormats('json');
70  //$this->r->setSupportedFormats('jsonFormat');
71  }
72 
80  /* Disabled, most APIs does not share same signature for method index
81  function index()
82  {
83  return array(
84  'success' => array(
85  'code' => 200,
86  'message' => __class__.' is up and running!'
87  )
88  );
89  }*/
90 
91  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.PublicUnderscore
98  protected function _cleanObjectDatas($object)
99  {
100  // phpcs:enable
101  // Remove $db object property for object
102  unset($object->db);
103  unset($object->isextrafieldmanaged);
104  unset($object->ismultientitymanaged);
105  unset($object->restrictiononfksoc);
106  unset($object->table_rowid);
107 
108  // Remove linkedObjects. We should already have linkedObjectsIds that avoid huge responses
109  unset($object->linkedObjects);
110 
111  unset($object->fields);
112  unset($object->oldline);
113 
114  unset($object->error);
115  unset($object->errors);
116  unset($object->errorhidden);
117 
118  unset($object->ref_previous);
119  unset($object->ref_next);
120  unset($object->ref_int);
121 
122  unset($object->projet); // Should be fk_project
123  unset($object->project); // Should be fk_project
124  unset($object->author); // Should be fk_user_author
125  unset($object->timespent_old_duration);
126  unset($object->timespent_id);
127  unset($object->timespent_duration);
128  unset($object->timespent_date);
129  unset($object->timespent_datehour);
130  unset($object->timespent_withhour);
131  unset($object->timespent_fk_user);
132  unset($object->timespent_note);
133  unset($object->fk_delivery_address);
134 
135  unset($object->statuts);
136  unset($object->statuts_short);
137  unset($object->statuts_logo);
138  unset($object->statuts_long);
139  unset($object->labelStatus);
140  unset($object->labelStatusShort);
141 
142  unset($object->stats_propale);
143  unset($object->stats_commande);
144  unset($object->stats_contrat);
145  unset($object->stats_facture);
146  unset($object->stats_commande_fournisseur);
147  unset($object->stats_reception);
148  unset($object->stats_mrptoconsume);
149  unset($object->stats_mrptoproduce);
150 
151  unset($object->element);
152  unset($object->fk_element);
153  unset($object->table_element);
154  unset($object->table_element_line);
155  unset($object->class_element_line);
156  unset($object->picto);
157 
158  unset($object->fieldsforcombobox);
159 
160  unset($object->skip_update_total);
161  unset($object->context);
162  unset($object->next_prev_filter);
163 
164  unset($object->region);
165  unset($object->region_code);
166 
167  unset($object->libelle_statut);
168  unset($object->libelle_paiement);
169 
170  unset($object->prefix_comm);
171 
172  unset($object->sendtoid);
173  unset($object->name_bis);
174  unset($object->newref);
175 
176  if (!isset($object->table_element) || $object->table_element != 'ticket') {
177  unset($object->comments);
178  }
179 
180  // Remove the $oldcopy property because it is not supported by the JSON
181  // encoder. The following error is generated when trying to serialize
182  // it: "Error encoding/decoding JSON: Type is not supported"
183  // Note: Event if this property was correctly handled by the JSON
184  // encoder, it should be ignored because keeping it would let the API
185  // have a very strange behavior: calling PUT and then GET on the same
186  // resource would give different results:
187  // PUT /objects/{id} -> returns object with oldcopy = previous version of the object
188  // GET /objects/{id} -> returns object with oldcopy empty
189  unset($object->oldcopy);
190 
191  // If object has lines, remove $db property
192  if (isset($object->lines) && is_array($object->lines) && count($object->lines) > 0) {
193  $nboflines = count($object->lines);
194  for ($i = 0; $i < $nboflines; $i++)
195  {
196  $this->_cleanObjectDatas($object->lines[$i]);
197 
198  unset($object->lines[$i]->contact);
199  unset($object->lines[$i]->contact_id);
200  unset($object->lines[$i]->country);
201  unset($object->lines[$i]->country_id);
202  unset($object->lines[$i]->country_code);
203  unset($object->lines[$i]->mode_reglement_id);
204  unset($object->lines[$i]->mode_reglement_code);
205  unset($object->lines[$i]->mode_reglement);
206  unset($object->lines[$i]->cond_reglement_id);
207  unset($object->lines[$i]->cond_reglement_code);
208  unset($object->lines[$i]->cond_reglement);
209  unset($object->lines[$i]->fk_delivery_address);
210  unset($object->lines[$i]->fk_projet);
211  unset($object->lines[$i]->fk_project);
212  unset($object->lines[$i]->thirdparty);
213  unset($object->lines[$i]->user);
214  unset($object->lines[$i]->model_pdf);
215  unset($object->lines[$i]->modelpdf);
216  unset($object->lines[$i]->note_public);
217  unset($object->lines[$i]->note_private);
218  unset($object->lines[$i]->fk_incoterms);
219  unset($object->lines[$i]->label_incoterms);
220  unset($object->lines[$i]->location_incoterms);
221  unset($object->lines[$i]->name);
222  unset($object->lines[$i]->lastname);
223  unset($object->lines[$i]->firstname);
224  unset($object->lines[$i]->civility_id);
225  unset($object->lines[$i]->fk_multicurrency);
226  unset($object->lines[$i]->multicurrency_code);
227  unset($object->lines[$i]->shipping_method_id);
228  }
229  }
230 
231  if (!empty($object->thirdparty) && is_object($object->thirdparty)) {
232  $this->_cleanObjectDatas($object->thirdparty);
233  }
234  return $object;
235  }
236 
237  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.PublicUnderscore
252  protected static function _checkAccessToResource($resource, $resource_id = 0, $dbtablename = '', $feature2 = '', $dbt_keyfield = 'fk_soc', $dbt_select = 'rowid')
253  {
254  // phpcs:enable
255  // Features/modules to check
256  $featuresarray = array($resource);
257  if (preg_match('/&/', $resource)) {
258  $featuresarray = explode("&", $resource);
259  } elseif (preg_match('/\|/', $resource)) {
260  $featuresarray = explode("|", $resource);
261  }
262 
263  // More subfeatures to check
264  if (!empty($feature2)) {
265  $feature2 = explode("|", $feature2);
266  }
267 
268  return checkUserAccessToObject(DolibarrApiAccess::$user, $featuresarray, $resource_id, $dbtablename, $feature2, $dbt_keyfield, $dbt_select);
269  }
270 
271  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.PublicUnderscore
278  protected function _checkFilters($sqlfilters)
279  {
280  // phpcs:enable
281  //$regexstring='\(([^:\'\(\)]+:[^:\'\(\)]+:[^:\(\)]+)\)';
282  //$tmp=preg_replace_all('/'.$regexstring.'/', '', $sqlfilters);
283  $tmp = $sqlfilters;
284  $ok = 0;
285  $i = 0; $nb = strlen($tmp);
286  $counter = 0;
287  while ($i < $nb)
288  {
289  if ($tmp[$i] == '(') $counter++;
290  if ($tmp[$i] == ')') $counter--;
291  if ($counter < 0)
292  {
293  $error = "Bad sqlfilters=".$sqlfilters;
294  dol_syslog($error, LOG_WARNING);
295  return false;
296  }
297  $i++;
298  }
299  return true;
300  }
301 
302  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
303  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.PublicUnderscore
310  protected static function _forge_criteria_callback($matches)
311  {
312  // phpcs:enable
313  global $db;
314 
315  //dol_syslog("Convert matches ".$matches[1]);
316  if (empty($matches[1])) return '';
317  $tmp = explode(':', $matches[1]);
318  if (count($tmp) < 3) return '';
319 
320  $tmpescaped = $tmp[2];
321  $regbis = array();
322  if (preg_match('/^\'(.*)\'$/', $tmpescaped, $regbis))
323  {
324  $tmpescaped = "'".$db->escape($regbis[1])."'";
325  } else {
326  $tmpescaped = $db->escape($tmpescaped);
327  }
328  return $db->escape($tmp[0]).' '.strtoupper($db->escape($tmp[1]))." ".$tmpescaped;
329  }
330 }
checkUserAccessToObject($user, $featuresarray, $objectid=0, $tableandshare= '', $feature2= '', $dbt_keyfield= '', $dbt_select= 'rowid', $parenttableforentity= '')
Check access by user to object.
_cleanObjectDatas($object)
Executed method when API is called without parameter.
Definition: api.class.php:98
$conf db
API class for accounts.
Definition: inc.php:54
_checkFilters($sqlfilters)
Return if a $sqlfilters parameter is valid.
Definition: api.class.php:278
Class for API REST v1.
Definition: api.class.php:30
dol_syslog($message, $level=LOG_INFO, $ident=0, $suffixinfilename= '', $restricttologhandler= '', $logcontext=null)
Write log message into outputs.
static _checkAccessToResource($resource, $resource_id=0, $dbtablename= '', $feature2= '', $dbt_keyfield= 'fk_soc', $dbt_select= 'rowid')
Check user access to a resource.
Definition: api.class.php:252
static _forge_criteria_callback($matches)
Function to forge a SQL criteria.
Definition: api.class.php:310
__construct($db, $cachedir= '', $refreshCache=false)
Constructor.
Definition: api.class.php:50