api.class.php 8.0 KB

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