api_zapier.class.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380
  1. <?php
  2. /* Copyright (C) 2015 Jean-François Ferry <jfefe@aternatik.fr>
  3. * Copyright (C) 2019-2020 Frédéric France <frederic.france@netlogic.fr>
  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 <https://www.gnu.org/licenses/>.
  17. */
  18. /**
  19. * \file htdocs/zapier/class/api_zapier.class.php
  20. * \ingroup zapier
  21. * \brief File for API management of Zapier hooks.
  22. */
  23. use Luracast\Restler\RestException;
  24. require_once DOL_DOCUMENT_ROOT.'/zapier/class/hook.class.php';
  25. /**
  26. * API class for zapier hook
  27. *
  28. * @access protected
  29. * @class DolibarrApiAccess {@requires user,external}
  30. */
  31. class Zapier extends DolibarrApi
  32. {
  33. /**
  34. * @var array $FIELDS Mandatory fields, checked when create and update object
  35. */
  36. public static $FIELDS = array(
  37. 'url',
  38. );
  39. /**
  40. * @var Hook $hook {@type Hook}
  41. */
  42. public $hook;
  43. /**
  44. * Constructor
  45. *
  46. * @url GET /
  47. *
  48. */
  49. public function __construct()
  50. {
  51. global $db, $conf;
  52. $this->db = $db;
  53. $this->hook = new Hook($this->db);
  54. }
  55. /**
  56. * Get properties of a hook object
  57. *
  58. * Return an array with hook informations
  59. *
  60. * @param int $id ID of hook
  61. * @return Object Object with cleaned properties
  62. *
  63. * @url GET /hooks/{id}
  64. * @throws RestException
  65. */
  66. public function get($id)
  67. {
  68. if (!DolibarrApiAccess::$user->rights->zapier->read) {
  69. throw new RestException(401);
  70. }
  71. $result = $this->hook->fetch($id);
  72. if (!$result) {
  73. throw new RestException(404, 'Hook not found');
  74. }
  75. if (!DolibarrApi::_checkAccessToResource('hook', $this->hook->id)) {
  76. throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
  77. }
  78. return $this->_cleanObjectDatas($this->hook);
  79. }
  80. /**
  81. * Get list of possibles choices for module
  82. *
  83. * Return an array with hook informations
  84. *
  85. * @return array data
  86. *
  87. * @url GET /getmoduleschoices/
  88. * @throws RestException
  89. */
  90. public function getModulesChoices()
  91. {
  92. if (!DolibarrApiAccess::$user->rights->zapier->read) {
  93. throw new RestException(401);
  94. }
  95. $arraychoices = array(
  96. 'invoices' => 'Invoices',
  97. 'orders' => 'Orders',
  98. 'thirdparties' => 'Thirparties',
  99. 'contacts' => 'Contacts',
  100. 'users' => 'Users',
  101. );
  102. // $result = $this->hook->fetch($id);
  103. // if (! $result ) {
  104. // throw new RestException(404, 'Hook not found');
  105. // }
  106. // if (! DolibarrApi::_checkAccessToResource('hook', $this->hook->id)) {
  107. // throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
  108. // }
  109. return $arraychoices;
  110. }
  111. /**
  112. * List hooks
  113. *
  114. * Get a list of hooks
  115. *
  116. * @param string $sortfield Sort field
  117. * @param string $sortorder Sort order
  118. * @param int $limit Limit for list
  119. * @param int $page Page number
  120. * @param string $sqlfilters Other criteria to filter answers separated by a comma. Syntax example "(t.ref:like:'SO-%') and (t.date_creation:<:'20160101')"
  121. * @return array Array of order objects
  122. *
  123. * @throws RestException
  124. *
  125. * @url GET /hooks/
  126. */
  127. public function index($sortfield = "t.rowid", $sortorder = 'ASC', $limit = 100, $page = 0, $sqlfilters = '')
  128. {
  129. global $db, $conf;
  130. if (!DolibarrApiAccess::$user->rights->zapier->read) {
  131. throw new RestException(401);
  132. }
  133. $obj_ret = array();
  134. $socid = DolibarrApiAccess::$user->socid ? DolibarrApiAccess::$user->socid : '';
  135. // Set to 1 if there is a field socid in table of object
  136. $restrictonsocid = 0;
  137. // If the internal user must only see his customers, force searching by him
  138. $search_sale = 0;
  139. if ($restrictonsocid && !DolibarrApiAccess::$user->rights->societe->client->voir && !$socid) {
  140. $search_sale = DolibarrApiAccess::$user->id;
  141. }
  142. $sql = "SELECT t.rowid";
  143. if ($restrictonsocid && (!DolibarrApiAccess::$user->rights->societe->client->voir && !$socid) || $search_sale > 0) {
  144. // We need these fields in order to filter by sale (including the case where the user can only see his prospects)
  145. $sql .= ", sc.fk_soc, sc.fk_user";
  146. }
  147. $sql .= " FROM ".MAIN_DB_PREFIX."hook_mytable as t";
  148. if ($restrictonsocid && (!DolibarrApiAccess::$user->rights->societe->client->voir && !$socid) || $search_sale > 0) {
  149. $sql .= ", ".MAIN_DB_PREFIX."societe_commerciaux as sc"; // We need this table joined to the select in order to filter by sale
  150. }
  151. $sql .= " WHERE 1 = 1";
  152. // Example of use $mode
  153. //if ($mode == 1) $sql.= " AND s.client IN (1, 3)";
  154. //if ($mode == 2) $sql.= " AND s.client IN (2, 3)";
  155. $tmpobject = new Hook($this->db);
  156. if ($tmpobject->ismultientitymanaged) {
  157. $sql .= ' AND t.entity IN ('.getEntity('hook').')';
  158. }
  159. if ($restrictonsocid && (!DolibarrApiAccess::$user->rights->societe->client->voir && !$socid) || $search_sale > 0) {
  160. $sql .= " AND t.fk_soc = sc.fk_soc";
  161. }
  162. if ($restrictonsocid && $socid) {
  163. $sql .= " AND t.fk_soc = ".((int) $socid);
  164. }
  165. if ($restrictonsocid && $search_sale > 0) {
  166. // Join for the needed table to filter by sale
  167. $sql .= " AND t.rowid = sc.fk_soc";
  168. }
  169. // Insert sale filter
  170. if ($restrictonsocid && $search_sale > 0) {
  171. $sql .= " AND sc.fk_user = ".((int) $search_sale);
  172. }
  173. if ($sqlfilters) {
  174. $errormessage = '';
  175. $sql .= forgeSQLFromUniversalSearchCriteria($sqlfilters, $errormessage);
  176. if ($errormessage) {
  177. throw new RestException(400, 'Error when validating parameter sqlfilters -> '.$errormessage);
  178. }
  179. }
  180. $sql .= $this->db->order($sortfield, $sortorder);
  181. if ($limit) {
  182. if ($page < 0) {
  183. $page = 0;
  184. }
  185. $offset = $limit * $page;
  186. $sql .= $this->db->plimit($limit + 1, $offset);
  187. }
  188. $result = $this->db->query($sql);
  189. $i = 0;
  190. if ($result) {
  191. $num = $this->db->num_rows($result);
  192. while ($i < $num) {
  193. $obj = $this->db->fetch_object($result);
  194. $hook_static = new Hook($this->db);
  195. if ($hook_static->fetch($obj->rowid)) {
  196. $obj_ret[] = $this->_cleanObjectDatas($hook_static);
  197. }
  198. $i++;
  199. }
  200. } else {
  201. throw new RestException(503, 'Error when retrieve hook list');
  202. }
  203. if (!count($obj_ret)) {
  204. throw new RestException(404, 'No hook found');
  205. }
  206. return $obj_ret;
  207. }
  208. /**
  209. * Create hook object
  210. *
  211. * @param array $request_data Request datas
  212. * @return array ID of hook
  213. *
  214. * @url POST /hook/
  215. */
  216. public function post($request_data = null)
  217. {
  218. if (!DolibarrApiAccess::$user->rights->zapier->write) {
  219. throw new RestException(401);
  220. }
  221. // Check mandatory fields
  222. $fields = array(
  223. 'url',
  224. );
  225. dol_syslog("API Zapier create hook receive : ".print_r($request_data, true), LOG_DEBUG);
  226. $result = $this->validate($request_data, $fields);
  227. foreach ($request_data as $field => $value) {
  228. $this->hook->$field = $value;
  229. }
  230. $this->hook->fk_user = DolibarrApiAccess::$user->id;
  231. // we create the hook into database
  232. if (!$this->hook->create(DolibarrApiAccess::$user)) {
  233. throw new RestException(500, "Error creating Hook", array_merge(array($this->hook->error), $this->hook->errors));
  234. }
  235. return array(
  236. 'id' => $this->hook->id,
  237. );
  238. }
  239. // /**
  240. // * Update hook
  241. // *
  242. // * @param int $id Id of hook to update
  243. // * @param array $request_data Datas
  244. // * @return int
  245. // *
  246. // * @url PUT /hooks/{id}
  247. // */
  248. /*public function put($id, $request_data = null)
  249. {
  250. if (! DolibarrApiAccess::$user->rights->zapier->write) {
  251. throw new RestException(401);
  252. }
  253. $result = $this->hook->fetch($id);
  254. if( ! $result ) {
  255. throw new RestException(404, 'Hook not found');
  256. }
  257. if( ! DolibarrApi::_checkAccessToResource('hook', $this->hook->id)) {
  258. throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
  259. }
  260. foreach($request_data as $field => $value) {
  261. if ($field == 'id') {
  262. continue;
  263. }
  264. $this->hook->$field = $value;
  265. }
  266. if ($this->hook->update($id, DolibarrApiAccess::$user) > 0) {
  267. return $this->get($id);
  268. } else {
  269. throw new RestException(500, $this->hook->error);
  270. }
  271. }*/
  272. /**
  273. * Delete hook
  274. *
  275. * @param int $id Hook ID
  276. * @return array
  277. *
  278. * @url DELETE /hook/{id}
  279. */
  280. public function delete($id)
  281. {
  282. if (!DolibarrApiAccess::$user->rights->zapier->delete) {
  283. throw new RestException(401);
  284. }
  285. $result = $this->hook->fetch($id);
  286. if (!$result) {
  287. throw new RestException(404, 'Hook not found');
  288. }
  289. if (!DolibarrApi::_checkAccessToResource('hook', $this->hook->id)) {
  290. throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
  291. }
  292. if (!$this->hook->delete(DolibarrApiAccess::$user)) {
  293. throw new RestException(500, 'Error when deleting Hook : '.$this->hook->error);
  294. }
  295. return array(
  296. 'success' => array(
  297. 'code' => 200,
  298. 'message' => 'Hook deleted'
  299. )
  300. );
  301. }
  302. // phpcs:disable PEAR.NamingConventions.ValidFunctionName.PublicUnderscore
  303. /**
  304. * Clean sensible object datas
  305. *
  306. * @param Object $object Object to clean
  307. * @return Object Object with cleaned properties
  308. */
  309. public function _cleanObjectDatas($object)
  310. {
  311. // phpcs:disable
  312. $object = parent::_cleanObjectDatas($object);
  313. return $object;
  314. }
  315. /**
  316. * Validate fields before create or update object
  317. *
  318. * @param array $data Array of data to validate
  319. * @param array $fields Array of fields needed
  320. * @return array
  321. *
  322. * @throws RestException
  323. */
  324. private function validate($data, $fields)
  325. {
  326. $hook = array();
  327. foreach ($fields as $field) {
  328. if (!isset($data[$field])) {
  329. throw new RestException(400, $field." field missing");
  330. }
  331. $hook[$field] = $data[$field];
  332. }
  333. return $hook;
  334. }
  335. }