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