api.class.php 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  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. use Luracast\Restler\Restler;
  19. use Luracast\Restler\RestException;
  20. use Luracast\Restler\Defaults;
  21. use Luracast\Restler\Format\UploadFormat;
  22. require_once DOL_DOCUMENT_ROOT.'/user/class/user.class.php';
  23. /**
  24. * Class for API REST v1
  25. */
  26. class DolibarrApi
  27. {
  28. /**
  29. * @var DoliDb $db Database object
  30. */
  31. static protected $db;
  32. /**
  33. * @var Restler $r Restler object
  34. */
  35. var $r;
  36. /**
  37. * Constructor
  38. *
  39. * @param DoliDb $db Database handler
  40. * @param string $cachedir Cache dir
  41. */
  42. function __construct($db, $cachedir='')
  43. {
  44. global $conf;
  45. if (empty($cachedir)) $cachedir = $conf->api->dir_temp;
  46. Defaults::$cacheDirectory = $cachedir;
  47. $this->db = $db;
  48. $production_mode = ( empty($conf->global->API_PRODUCTION_MODE) ? false : true );
  49. $this->r = new Restler($production_mode);
  50. $this->r->setAPIVersion(1);
  51. }
  52. /**
  53. * Executed method when API is called without parameter
  54. *
  55. * Display a short message an return a http code 200
  56. *
  57. * @return array
  58. */
  59. /* Disabled, most APIs does not share same signature for method index
  60. function index()
  61. {
  62. return array(
  63. 'success' => array(
  64. 'code' => 200,
  65. 'message' => __class__.' is up and running!'
  66. )
  67. );
  68. }*/
  69. /**
  70. * Clean sensible object datas
  71. *
  72. * @param object $object Object to clean
  73. * @return array Array of cleaned object properties
  74. */
  75. function _cleanObjectDatas($object) {
  76. // Remove $db object property for object
  77. unset($object->db);
  78. // Remove linkedObjects. We should already have linkedObjectIds that avoid huge responses
  79. unset($object->linkedObjects);
  80. unset($object->lines); // should be ->lines
  81. unset($object->fields);
  82. unset($object->oldline);
  83. unset($object->error);
  84. unset($object->errors);
  85. unset($object->ref_previous);
  86. unset($object->ref_next);
  87. unset($object->ref_int);
  88. unset($object->projet); // Should be fk_project
  89. unset($object->project); // Should be fk_project
  90. unset($object->author); // Should be fk_user_author
  91. unset($object->timespent_old_duration);
  92. unset($object->timespent_id);
  93. unset($object->timespent_duration);
  94. unset($object->timespent_date);
  95. unset($object->timespent_datehour);
  96. unset($object->timespent_withhour);
  97. unset($object->timespent_fk_user);
  98. unset($object->timespent_note);
  99. unset($object->statuts);
  100. unset($object->statuts_short);
  101. unset($object->statuts_logo);
  102. unset($object->statuts_long);
  103. unset($object->element);
  104. unset($object->fk_element);
  105. unset($object->table_element);
  106. unset($object->table_element_line);
  107. unset($object->picto);
  108. // Remove the $oldcopy property because it is not supported by the JSON
  109. // encoder. The following error is generated when trying to serialize
  110. // it: "Error encoding/decoding JSON: Type is not supported"
  111. // Note: Event if this property was correctly handled by the JSON
  112. // encoder, it should be ignored because keeping it would let the API
  113. // have a very strange behavior: calling PUT and then GET on the same
  114. // resource would give different results:
  115. // PUT /objects/{id} -> returns object with oldcopy = previous version of the object
  116. // GET /objects/{id} -> returns object with oldcopy empty
  117. unset($object->oldcopy);
  118. // If object has lines, remove $db property
  119. if(isset($object->lines) && count($object->lines) > 0) {
  120. $nboflines = count($object->lines);
  121. for ($i=0; $i < $nboflines; $i++)
  122. {
  123. $this->_cleanObjectDatas($object->lines[$i]);
  124. }
  125. }
  126. // If object has linked objects, remove $db property
  127. /*
  128. if(isset($object->linkedObjects) && count($object->linkedObjects) > 0) {
  129. foreach($object->linkedObjects as $type_object => $linked_object) {
  130. foreach($linked_object as $object2clean) {
  131. $this->_cleanObjectDatas($object2clean);
  132. }
  133. }
  134. }*/
  135. return $object;
  136. }
  137. /**
  138. * Check user access to a resource
  139. *
  140. * Check access by user to a given resource
  141. *
  142. * @param string $resource element to check
  143. * @param int $resource_id Object ID if we want to check a particular record (optional) is linked to a owned thirdparty (optional).
  144. * @param type $dbtablename 'TableName&SharedElement' with Tablename is table where object is stored. SharedElement is an optional key to define where to check entity. Not used if objectid is null (optional)
  145. * @param string $feature2 Feature to check, second level of permission (optional). Can be or check with 'level1|level2'.
  146. * @param string $dbt_keyfield Field name for socid foreign key if not fk_soc. Not used if objectid is null (optional)
  147. * @param string $dbt_select Field name for select if not rowid. Not used if objectid is null (optional)
  148. * @throws RestException
  149. */
  150. static function _checkAccessToResource($resource, $resource_id=0, $dbtablename='', $feature2='', $dbt_keyfield='fk_soc', $dbt_select='rowid') {
  151. // Features/modules to check
  152. $featuresarray = array($resource);
  153. if (preg_match('/&/', $resource)) {
  154. $featuresarray = explode("&", $resource);
  155. }
  156. else if (preg_match('/\|/', $resource)) {
  157. $featuresarray = explode("|", $resource);
  158. }
  159. // More subfeatures to check
  160. if (! empty($feature2)) {
  161. $feature2 = explode("|", $feature2);
  162. }
  163. return checkUserAccessToObject(DolibarrApiAccess::$user, $featuresarray, $resource_id, $dbtablename, $feature2, $dbt_keyfield, $dbt_select);
  164. }
  165. /**
  166. * Return if a $sqlfilters parameter is valid
  167. *
  168. * @param string $sqlfilters sqlfilter string
  169. * @return boolean True if valid, False if not valid
  170. */
  171. function _checkFilters($sqlfilters)
  172. {
  173. //$regexstring='\(([^:\'\(\)]+:[^:\'\(\)]+:[^:\(\)]+)\)';
  174. //$tmp=preg_replace_all('/'.$regexstring.'/', '', $sqlfilters);
  175. $tmp=$sqlfilters;
  176. $ok=0;
  177. $i=0; $nb=count($tmp);
  178. $counter=0;
  179. while ($i < $nb)
  180. {
  181. if ($tmp[$i]=='(') $counter++;
  182. if ($tmp[$i]==')') $counter--;
  183. if ($counter < 0)
  184. {
  185. $error="Bad sqlfilters=".$sqlfilters;
  186. dol_syslog($error, LOG_WARNING);
  187. return false;
  188. }
  189. $i++;
  190. }
  191. return true;
  192. }
  193. /**
  194. * Function to forge a SQL criteria
  195. *
  196. * @param array $matches Array of found string by regex search
  197. * @return string Forged criteria. Example: "t.field like 'abc%'"
  198. */
  199. static function _forge_criteria_callback($matches)
  200. {
  201. global $db;
  202. //dol_syslog("Convert matches ".$matches[1]);
  203. if (empty($matches[1])) return '';
  204. $tmp=explode(':',$matches[1]);
  205. if (count($tmp) < 3) return '';
  206. $tmpescaped=$tmp[2];
  207. if (preg_match('/^\'(.*)\'$/', $tmpescaped, $regbis))
  208. {
  209. $tmpescaped = "'".$db->escape($regbis[1])."'";
  210. }
  211. else
  212. {
  213. $tmpescaped = $db->escape($tmpescaped);
  214. }
  215. return $db->escape($tmp[0]).' '.strtoupper($db->escape($tmp[1]))." ".$tmpescaped;
  216. }
  217. }