api_setup.class.php 64 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754
  1. <?php
  2. /* Copyright (C) 2016 Xebax Christy <xebax@wanadoo.fr>
  3. * Copyright (C) 2016 Laurent Destailleur <eldy@users.sourceforge.net>
  4. * Copyright (C) 2017 Regis Houssin <regis.houssin@inodbox.com>
  5. * Copyright (C) 2017 Neil Orley <neil.orley@oeris.fr>
  6. * Copyright (C) 2018 Frédéric France <frederic.france@netlogic.fr>
  7. * Copyright (C) 2018-2019 Thibault FOUCART <support@ptibogxiv.net>
  8. *
  9. *
  10. * This program is free software; you can redistribute it and/or modify
  11. * it under the terms of the GNU General Public License as published by
  12. * the Free Software Foundation; either version 3 of the License, or
  13. * (at your option) any later version.
  14. *
  15. * This program is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. * GNU General Public License for more details.
  19. *
  20. * You should have received a copy of the GNU General Public License
  21. * along with this program. If not, see <https://www.gnu.org/licenses/>.
  22. */
  23. use Luracast\Restler\RestException;
  24. require_once DOL_DOCUMENT_ROOT.'/main.inc.php';
  25. require_once DOL_DOCUMENT_ROOT.'/core/class/ccountry.class.php';
  26. /**
  27. * API class for dictionaries
  28. *
  29. * @access protected
  30. * @class DolibarrApiAccess {@requires user,external}
  31. */
  32. class Setup extends DolibarrApi
  33. {
  34. private $translations = null;
  35. /**
  36. * Constructor
  37. */
  38. public function __construct()
  39. {
  40. global $db;
  41. $this->db = $db;
  42. }
  43. /**
  44. * Get the list of ordering methods.
  45. *
  46. * @param string $sortfield Sort field
  47. * @param string $sortorder Sort order
  48. * @param int $limit Number of items per page
  49. * @param int $page Page number {@min 0}
  50. * @param int $active Payment type is active or not {@min 0} {@max 1}
  51. * @param string $sqlfilters SQL criteria to filter with. Syntax example "(t.code:=:'OrderByWWW')"
  52. *
  53. * @url GET dictionary/ordering_methods
  54. *
  55. * @return array [List of ordering methods]
  56. *
  57. * @throws RestException 400
  58. */
  59. public function getOrderingMethods($sortfield = "code", $sortorder = 'ASC', $limit = 100, $page = 0, $active = 1, $sqlfilters = '')
  60. {
  61. $list = array();
  62. $sql = "SELECT rowid, code, libelle as label, module";
  63. $sql .= " FROM ".MAIN_DB_PREFIX."c_input_method as t";
  64. $sql .= " WHERE t.active = ".$active;
  65. // Add sql filters
  66. if ($sqlfilters)
  67. {
  68. if (!DolibarrApi::_checkFilters($sqlfilters))
  69. {
  70. throw new RestException(400, 'error when validating parameter sqlfilters '.$sqlfilters);
  71. }
  72. $regexstring = '\(([^:\'\(\)]+:[^:\'\(\)]+:[^:\(\)]+)\)';
  73. $sql .= " AND (".preg_replace_callback('/'.$regexstring.'/', 'DolibarrApi::_forge_criteria_callback', $sqlfilters).")";
  74. }
  75. $sql .= $this->db->order($sortfield, $sortorder);
  76. if ($limit) {
  77. if ($page < 0) {
  78. $page = 0;
  79. }
  80. $offset = $limit * $page;
  81. $sql .= $this->db->plimit($limit, $offset);
  82. }
  83. $result = $this->db->query($sql);
  84. if ($result) {
  85. $num = $this->db->num_rows($result);
  86. $min = min($num, ($limit <= 0 ? $num : $limit));
  87. for ($i = 0; $i < $min; $i++) {
  88. $list[] = $this->db->fetch_object($result);
  89. }
  90. } else {
  91. throw new RestException(400, $this->db->lasterror());
  92. }
  93. return $list;
  94. }
  95. /**
  96. * Get the list of payments types.
  97. *
  98. * @param string $sortfield Sort field
  99. * @param string $sortorder Sort order
  100. * @param int $limit Number of items per page
  101. * @param int $page Page number {@min 0}
  102. * @param int $active Payment type is active or not {@min 0} {@max 1}
  103. * @param string $sqlfilters SQL criteria to filter with. Syntax example "(t.code:=:'CHQ')"
  104. *
  105. * @url GET dictionary/payment_types
  106. *
  107. * @return array [List of payment types]
  108. *
  109. * @throws RestException 400
  110. */
  111. public function getPaymentTypes($sortfield = "code", $sortorder = 'ASC', $limit = 100, $page = 0, $active = 1, $sqlfilters = '')
  112. {
  113. $list = array();
  114. $sql = "SELECT id, code, type, libelle as label, module";
  115. $sql .= " FROM ".MAIN_DB_PREFIX."c_paiement as t";
  116. $sql .= " WHERE t.entity IN (".getEntity('c_paiement').")";
  117. $sql .= " AND t.active = ".$active;
  118. // Add sql filters
  119. if ($sqlfilters)
  120. {
  121. if (!DolibarrApi::_checkFilters($sqlfilters))
  122. {
  123. throw new RestException(400, 'error when validating parameter sqlfilters '.$sqlfilters);
  124. }
  125. $regexstring = '\(([^:\'\(\)]+:[^:\'\(\)]+:[^:\(\)]+)\)';
  126. $sql .= " AND (".preg_replace_callback('/'.$regexstring.'/', 'DolibarrApi::_forge_criteria_callback', $sqlfilters).")";
  127. }
  128. $sql .= $this->db->order($sortfield, $sortorder);
  129. if ($limit) {
  130. if ($page < 0) {
  131. $page = 0;
  132. }
  133. $offset = $limit * $page;
  134. $sql .= $this->db->plimit($limit, $offset);
  135. }
  136. $result = $this->db->query($sql);
  137. if ($result) {
  138. $num = $this->db->num_rows($result);
  139. $min = min($num, ($limit <= 0 ? $num : $limit));
  140. for ($i = 0; $i < $min; $i++) {
  141. $list[] = $this->db->fetch_object($result);
  142. }
  143. } else {
  144. throw new RestException(400, $this->db->lasterror());
  145. }
  146. return $list;
  147. }
  148. /**
  149. * Get the list of countries.
  150. *
  151. * The names of the countries will be translated to the given language if
  152. * the $lang parameter is provided. The value of $lang must be a language
  153. * code supported by Dolibarr, for example 'en_US' or 'fr_FR'.
  154. * The returned list is sorted by country ID.
  155. *
  156. * @param string $sortfield Sort field
  157. * @param string $sortorder Sort order
  158. * @param int $limit Number of items per page
  159. * @param int $page Page number (starting from zero)
  160. * @param string $filter To filter the countries by name
  161. * @param string $lang Code of the language the label of the countries must be translated to
  162. * @param string $sqlfilters Other criteria to filter answers separated by a comma. Syntax example "(t.code:like:'A%') and (t.active:>=:0)"
  163. * @return array List of countries
  164. *
  165. * @url GET dictionary/countries
  166. *
  167. * @throws RestException
  168. */
  169. public function getListOfCountries($sortfield = "code", $sortorder = 'ASC', $limit = 100, $page = 0, $filter = '', $lang = '', $sqlfilters = '')
  170. {
  171. $list = array();
  172. // Note: The filter is not applied in the SQL request because it must
  173. // be applied to the translated names, not to the names in database.
  174. $sql = "SELECT rowid FROM ".MAIN_DB_PREFIX."c_country as t";
  175. $sql .= " WHERE 1 = 1";
  176. // Add sql filters
  177. if ($sqlfilters)
  178. {
  179. if (!DolibarrApi::_checkFilters($sqlfilters))
  180. {
  181. throw new RestException(503, 'Error when validating parameter sqlfilters '.$sqlfilters);
  182. }
  183. $regexstring = '\(([^:\'\(\)]+:[^:\'\(\)]+:[^:\(\)]+)\)';
  184. $sql .= " AND (".preg_replace_callback('/'.$regexstring.'/', 'DolibarrApi::_forge_criteria_callback', $sqlfilters).")";
  185. }
  186. $sql .= $this->db->order($sortfield, $sortorder);
  187. if ($limit) {
  188. if ($page < 0) {
  189. $page = 0;
  190. }
  191. $offset = $limit * $page;
  192. $sql .= $this->db->plimit($limit, $offset);
  193. }
  194. $result = $this->db->query($sql);
  195. if ($result) {
  196. $num = $this->db->num_rows($result);
  197. $min = min($num, ($limit <= 0 ? $num : $limit));
  198. for ($i = 0; $i < $min; $i++) {
  199. $obj = $this->db->fetch_object($result);
  200. $country = new Ccountry($this->db);
  201. if ($country->fetch($obj->rowid) > 0) {
  202. // Translate the name of the country if needed
  203. // and then apply the filter if there is one.
  204. $this->translateLabel($country, $lang, 'Country');
  205. if (empty($filter) || stripos($country->label, $filter) !== false) {
  206. $list[] = $this->_cleanObjectDatas($country);
  207. }
  208. }
  209. }
  210. } else {
  211. throw new RestException(503, 'Error when retrieving list of countries');
  212. }
  213. return $list;
  214. }
  215. /**
  216. * Get country by ID.
  217. *
  218. * @param int $id ID of country
  219. * @param string $lang Code of the language the name of the
  220. * country must be translated to
  221. * @return array Array of cleaned object properties
  222. *
  223. * @url GET dictionary/countries/{id}
  224. *
  225. * @throws RestException
  226. */
  227. public function getCountryByID($id, $lang = '')
  228. {
  229. return $this->_fetchCcountry($id, '', '', $lang);
  230. }
  231. /**
  232. * Get country by Code.
  233. *
  234. * @param string $code Code of country
  235. * @param string $lang Code of the language the name of the
  236. * country must be translated to
  237. * @return array Array of cleaned object properties
  238. *
  239. * @url GET dictionary/countries/byCode/{code}
  240. *
  241. * @throws RestException
  242. */
  243. public function getCountryByCode($code, $lang = '')
  244. {
  245. return $this->_fetchCcountry('', $code, '', $lang);
  246. }
  247. /**
  248. * Get country by Iso.
  249. *
  250. * @param string $iso ISO of country
  251. * @param string $lang Code of the language the name of the
  252. * country must be translated to
  253. * @return array Array of cleaned object properties
  254. *
  255. * @url GET dictionary/countries/byISO/{iso}
  256. *
  257. * @throws RestException
  258. */
  259. public function getCountryByISO($iso, $lang = '')
  260. {
  261. return $this->_fetchCcountry('', '', $iso, $lang);
  262. }
  263. /**
  264. * Get country.
  265. *
  266. * @param int $id ID of country
  267. * @param string $code Code of country
  268. * @param string $iso ISO of country
  269. * @param string $lang Code of the language the name of the
  270. * country must be translated to
  271. * @return array Array of cleaned object properties
  272. *
  273. * @throws RestException
  274. */
  275. private function _fetchCcountry($id, $code = '', $iso = '', $lang = '')
  276. {
  277. $country = new Ccountry($this->db);
  278. $result = $country->fetch($id, $code, $iso);
  279. if ($result < 0) {
  280. throw new RestException(503, 'Error when retrieving country : '.$country->error);
  281. } elseif ($result == 0) {
  282. throw new RestException(404, 'country not found');
  283. }
  284. $this->translateLabel($country, $lang, 'Country');
  285. return $this->_cleanObjectDatas($country);
  286. }
  287. /**
  288. * Get the list of delivery times.
  289. *
  290. * @param string $sortfield Sort field
  291. * @param string $sortorder Sort order
  292. * @param int $limit Number of items per page
  293. * @param int $page Page number {@min 0}
  294. * @param int $active Delivery times is active or not {@min 0} {@max 1}
  295. * @param string $sqlfilters SQL criteria to filter with.
  296. *
  297. * @url GET dictionary/availability
  298. *
  299. * @return array [List of availability]
  300. *
  301. * @throws RestException 400
  302. */
  303. public function getAvailability($sortfield = "code", $sortorder = 'ASC', $limit = 100, $page = 0, $active = 1, $sqlfilters = '')
  304. {
  305. $list = array();
  306. $sql = "SELECT rowid, code, label";
  307. $sql .= " FROM ".MAIN_DB_PREFIX."c_availability as t";
  308. $sql .= " WHERE t.active = ".$active;
  309. // Add sql filters
  310. if ($sqlfilters)
  311. {
  312. if (!DolibarrApi::_checkFilters($sqlfilters))
  313. {
  314. throw new RestException(400, 'error when validating parameter sqlfilters '.$sqlfilters);
  315. }
  316. $regexstring = '\(([^:\'\(\)]+:[^:\'\(\)]+:[^:\(\)]+)\)';
  317. $sql .= " AND (".preg_replace_callback('/'.$regexstring.'/', 'DolibarrApi::_forge_criteria_callback', $sqlfilters).")";
  318. }
  319. $sql .= $this->db->order($sortfield, $sortorder);
  320. if ($limit) {
  321. if ($page < 0) {
  322. $page = 0;
  323. }
  324. $offset = $limit * $page;
  325. $sql .= $this->db->plimit($limit, $offset);
  326. }
  327. $result = $this->db->query($sql);
  328. if ($result) {
  329. $num = $this->db->num_rows($result);
  330. $min = min($num, ($limit <= 0 ? $num : $limit));
  331. for ($i = 0; $i < $min; $i++) {
  332. $list[] = $this->db->fetch_object($result);
  333. }
  334. } else {
  335. throw new RestException(400, $this->db->lasterror());
  336. }
  337. return $list;
  338. }
  339. // phpcs:disable PEAR.NamingConventions.ValidFunctionName.PublicUnderscore
  340. /**
  341. * Clean sensible object datas
  342. *
  343. * @param object $object Object to clean
  344. * @return array Array of cleaned object properties
  345. */
  346. protected function _cleanObjectDatas($object)
  347. {
  348. // phpcs:enable
  349. $object = parent::_cleanObjectDatas($object);
  350. unset($object->error);
  351. unset($object->errors);
  352. return $object;
  353. }
  354. /**
  355. * Translate the name of the object to the given language.
  356. *
  357. * @param object $object Object with label to translate
  358. * @param string $lang Code of the language the name of the object must be translated to
  359. * @param string $prefix Prefix for translation key
  360. *
  361. * @return void
  362. */
  363. private function translateLabel($object, $lang, $prefix = 'Country')
  364. {
  365. if (!empty($lang)) {
  366. // Load the translations if this is a new language.
  367. if ($this->translations == null || $this->translations->getDefaultLang() !== $lang) {
  368. global $conf;
  369. $this->translations = new Translate('', $conf);
  370. $this->translations->setDefaultLang($lang);
  371. $this->translations->load('dict');
  372. }
  373. if ($object->code) {
  374. $key = $prefix.$object->code;
  375. $translation = $this->translations->trans($key);
  376. if ($translation != $key) {
  377. $object->label = html_entity_decode($translation);
  378. }
  379. }
  380. }
  381. }
  382. /**
  383. * Get the list of shipment methods.
  384. *
  385. * @param string $sortfield Sort field
  386. * @param string $sortorder Sort order
  387. * @param int $limit Number of items per page
  388. * @param int $page Page number (starting from zero)
  389. * @param int $active Payment term is active or not {@min 0} {@max 1}
  390. * @param string $sqlfilters Other criteria to filter answers separated by a comma. Syntax example "(t.code:like:'A%') and (t.active:>=:0)"
  391. *
  392. * @return array List of shipment methods
  393. *
  394. * @url GET dictionary/shipment_methods
  395. *
  396. * @throws RestException
  397. */
  398. public function getListOfShipmentMethods($sortfield = "rowid", $sortorder = 'ASC', $limit = 100, $page = 0, $active = 1, $sqlfilters = '')
  399. {
  400. $list = array();
  401. $sql = "SELECT t.rowid, t.code, t.libelle, t.description, t.tracking";
  402. $sql .= " FROM ".MAIN_DB_PREFIX."c_shipment_mode as t";
  403. $sql .= " WHERE t.active = ".$active;
  404. // Add sql filters
  405. if ($sqlfilters)
  406. {
  407. if (!DolibarrApi::_checkFilters($sqlfilters))
  408. {
  409. throw new RestException(503, 'Error when validating parameter sqlfilters '.$sqlfilters);
  410. }
  411. $regexstring = '\(([^:\'\(\)]+:[^:\'\(\)]+:[^:\(\)]+)\)';
  412. $sql .= " AND (".preg_replace_callback('/'.$regexstring.'/', 'DolibarrApi::_forge_criteria_callback', $sqlfilters).")";
  413. }
  414. $sql .= $this->db->order($sortfield, $sortorder);
  415. if ($limit) {
  416. if ($page < 0) {
  417. $page = 0;
  418. }
  419. $offset = $limit * $page;
  420. $sql .= $this->db->plimit($limit, $offset);
  421. }
  422. $result = $this->db->query($sql);
  423. if ($result) {
  424. $num = $this->db->num_rows($result);
  425. $min = min($num, ($limit <= 0 ? $num : $limit));
  426. for ($i = 0; $i < $min; $i++) {
  427. $list[] = $this->db->fetch_object($result);
  428. }
  429. } else {
  430. throw new RestException(503, 'Error when retrieving list of shipment methods : '.$this->db->lasterror());
  431. }
  432. return $list;
  433. }
  434. /**
  435. * Get the list of events types.
  436. *
  437. * @param string $sortfield Sort field
  438. * @param string $sortorder Sort order
  439. * @param int $limit Number of items per page
  440. * @param int $page Page number (starting from zero)
  441. * @param string $type To filter on type of event
  442. * @param string $module To filter on module events
  443. * @param int $active Event's type is active or not {@min 0} {@max 1}
  444. * @param string $sqlfilters Other criteria to filter answers separated by a comma. Syntax example "(t.code:like:'A%') and (t.active:>=:0)"
  445. * @return array List of events types
  446. *
  447. * @url GET dictionary/event_types
  448. *
  449. * @throws RestException
  450. */
  451. public function getListOfEventTypes($sortfield = "code", $sortorder = 'ASC', $limit = 100, $page = 0, $type = '', $module = '', $active = 1, $sqlfilters = '')
  452. {
  453. $list = array();
  454. $sql = "SELECT id, code, type, libelle as label, module";
  455. $sql .= " FROM ".MAIN_DB_PREFIX."c_actioncomm as t";
  456. $sql .= " WHERE t.active = ".$active;
  457. if ($type) $sql .= " AND t.type LIKE '%".$this->db->escape($type)."%'";
  458. if ($module) $sql .= " AND t.module LIKE '%".$this->db->escape($module)."%'";
  459. // Add sql filters
  460. if ($sqlfilters)
  461. {
  462. if (!DolibarrApi::_checkFilters($sqlfilters))
  463. {
  464. throw new RestException(503, 'Error when validating parameter sqlfilters '.$sqlfilters);
  465. }
  466. $regexstring = '\(([^:\'\(\)]+:[^:\'\(\)]+:[^:\(\)]+)\)';
  467. $sql .= " AND (".preg_replace_callback('/'.$regexstring.'/', 'DolibarrApi::_forge_criteria_callback', $sqlfilters).")";
  468. }
  469. $sql .= $this->db->order($sortfield, $sortorder);
  470. if ($limit) {
  471. if ($page < 0) {
  472. $page = 0;
  473. }
  474. $offset = $limit * $page;
  475. $sql .= $this->db->plimit($limit, $offset);
  476. }
  477. $result = $this->db->query($sql);
  478. if ($result) {
  479. $num = $this->db->num_rows($result);
  480. $min = min($num, ($limit <= 0 ? $num : $limit));
  481. for ($i = 0; $i < $min; $i++) {
  482. $list[] = $this->db->fetch_object($result);
  483. }
  484. } else {
  485. throw new RestException(503, 'Error when retrieving list of events types : '.$this->db->lasterror());
  486. }
  487. return $list;
  488. }
  489. /**
  490. * Get the list of Expense Report types.
  491. *
  492. * @param string $sortfield Sort field
  493. * @param string $sortorder Sort order
  494. * @param int $limit Number of items per page
  495. * @param int $page Page number (starting from zero)
  496. * @param string $module To filter on module
  497. * @param int $active Event's type is active or not {@min 0} {@max 1}
  498. * @param string $sqlfilters Other criteria to filter answers separated by a comma. Syntax example "(t.code:like:'A%') and (t.active:>=:0)"
  499. * @return array List of expense report types
  500. *
  501. * @url GET dictionary/expensereport_types
  502. *
  503. * @throws RestException
  504. */
  505. public function getListOfExpenseReportsTypes($sortfield = "code", $sortorder = 'ASC', $limit = 100, $page = 0, $module = '', $active = 1, $sqlfilters = '')
  506. {
  507. $list = array();
  508. $sql = "SELECT id, code, label, accountancy_code, active, module, position";
  509. $sql .= " FROM ".MAIN_DB_PREFIX."c_type_fees as t";
  510. $sql .= " WHERE t.active = ".$active;
  511. if ($module) $sql .= " AND t.module LIKE '%".$this->db->escape($module)."%'";
  512. // Add sql filters
  513. if ($sqlfilters)
  514. {
  515. if (!DolibarrApi::_checkFilters($sqlfilters))
  516. {
  517. throw new RestException(503, 'Error when validating parameter sqlfilters '.$sqlfilters);
  518. }
  519. $regexstring = '\(([^:\'\(\)]+:[^:\'\(\)]+:[^:\(\)]+)\)';
  520. $sql .= " AND (".preg_replace_callback('/'.$regexstring.'/', 'DolibarrApi::_forge_criteria_callback', $sqlfilters).")";
  521. }
  522. $sql .= $this->db->order($sortfield, $sortorder);
  523. if ($limit) {
  524. if ($page < 0) {
  525. $page = 0;
  526. }
  527. $offset = $limit * $page;
  528. $sql .= $this->db->plimit($limit, $offset);
  529. }
  530. $result = $this->db->query($sql);
  531. if ($result) {
  532. $num = $this->db->num_rows($result);
  533. $min = min($num, ($limit <= 0 ? $num : $limit));
  534. for ($i = 0; $i < $min; $i++) {
  535. $list[] = $this->db->fetch_object($result);
  536. }
  537. } else {
  538. throw new RestException(503, 'Error when retrieving list of expense report types : '.$this->db->lasterror());
  539. }
  540. return $list;
  541. }
  542. /**
  543. * Get the list of contacts types.
  544. *
  545. * @param string $sortfield Sort field
  546. * @param string $sortorder Sort order
  547. * @param int $limit Number of items per page
  548. * @param int $page Page number (starting from zero)
  549. * @param string $type To filter on type of contact
  550. * @param string $module To filter on module contacts
  551. * @param int $active Contact's type is active or not {@min 0} {@max 1}
  552. * @param string $sqlfilters Other criteria to filter answers separated by a comma. Syntax example "(t.code:like:'A%') and (t.active:>=:0)"
  553. * @return array List of Contacts types
  554. *
  555. * @url GET dictionary/contact_types
  556. *
  557. * @throws RestException
  558. */
  559. public function getListOfContactTypes($sortfield = "code", $sortorder = 'ASC', $limit = 100, $page = 0, $type = '', $module = '', $active = 1, $sqlfilters = '')
  560. {
  561. $list = array();
  562. $sql = "SELECT rowid, code, element as type, libelle as label, source, module, position";
  563. $sql .= " FROM ".MAIN_DB_PREFIX."c_type_contact as t";
  564. $sql .= " WHERE t.active = ".$active;
  565. if ($type) $sql .= " AND type LIKE '%".$this->db->escape($type)."%'";
  566. if ($module) $sql .= " AND t.module LIKE '%".$this->db->escape($module)."%'";
  567. // Add sql filters
  568. if ($sqlfilters)
  569. {
  570. if (!DolibarrApi::_checkFilters($sqlfilters))
  571. {
  572. throw new RestException(503, 'Error when validating parameter sqlfilters '.$sqlfilters);
  573. }
  574. $regexstring = '\(([^:\'\(\)]+:[^:\'\(\)]+:[^:\(\)]+)\)';
  575. $sql .= " AND (".preg_replace_callback('/'.$regexstring.'/', 'DolibarrApi::_forge_criteria_callback', $sqlfilters).")";
  576. }
  577. $sql .= $this->db->order($sortfield, $sortorder);
  578. if ($limit) {
  579. if ($page < 0) {
  580. $page = 0;
  581. }
  582. $offset = $limit * $page;
  583. $sql .= $this->db->plimit($limit, $offset);
  584. }
  585. $result = $this->db->query($sql);
  586. if ($result) {
  587. $num = $this->db->num_rows($result);
  588. $min = min($num, ($limit <= 0 ? $num : $limit));
  589. for ($i = 0; $i < $min; $i++) {
  590. $list[] = $this->db->fetch_object($result);
  591. }
  592. } else {
  593. throw new RestException(503, 'Error when retrieving list of contacts types : '.$this->db->lasterror());
  594. }
  595. return $list;
  596. }
  597. /**
  598. * Get the list of civilities.
  599. *
  600. * @param string $sortfield Sort field
  601. * @param string $sortorder Sort order
  602. * @param int $limit Number of items per page
  603. * @param int $page Page number (starting from zero)
  604. * @param string $module To filter on module events
  605. * @param int $active Civility is active or not {@min 0} {@max 1}
  606. * @param string $sqlfilters Other criteria to filter answers separated by a comma. Syntax example "(t.code:like:'A%') and (t.active:>=:0)"
  607. * @return array List of civility types
  608. *
  609. * @url GET dictionary/civilities
  610. *
  611. * @throws RestException
  612. */
  613. public function getListOfCivilities($sortfield = "code", $sortorder = 'ASC', $limit = 100, $page = 0, $module = '', $active = 1, $sqlfilters = '')
  614. {
  615. $list = array();
  616. $sql = "SELECT rowid, code, label, module";
  617. $sql .= " FROM ".MAIN_DB_PREFIX."c_civility as t";
  618. $sql .= " WHERE t.active = ".$active;
  619. if ($module) $sql .= " AND t.module LIKE '%".$this->db->escape($module)."%'";
  620. // Add sql filters
  621. if ($sqlfilters)
  622. {
  623. if (!DolibarrApi::_checkFilters($sqlfilters))
  624. {
  625. throw new RestException(503, 'Error when validating parameter sqlfilters '.$sqlfilters);
  626. }
  627. $regexstring = '\(([^:\'\(\)]+:[^:\'\(\)]+:[^:\(\)]+)\)';
  628. $sql .= " AND (".preg_replace_callback('/'.$regexstring.'/', 'DolibarrApi::_forge_criteria_callback', $sqlfilters).")";
  629. }
  630. $sql .= $this->db->order($sortfield, $sortorder);
  631. if ($limit) {
  632. if ($page < 0) {
  633. $page = 0;
  634. }
  635. $offset = $limit * $page;
  636. $sql .= $this->db->plimit($limit, $offset);
  637. }
  638. $result = $this->db->query($sql);
  639. if ($result) {
  640. $num = $this->db->num_rows($result);
  641. $min = min($num, ($limit <= 0 ? $num : $limit));
  642. for ($i = 0; $i < $min; $i++) {
  643. $list[] = $this->db->fetch_object($result);
  644. }
  645. } else {
  646. throw new RestException(503, 'Error when retrieving list of civility : '.$this->db->lasterror());
  647. }
  648. return $list;
  649. }
  650. /**
  651. * Get the list of currencies.
  652. *
  653. * @param int $multicurrency Multicurrency rates (0: no multicurrency, 1: last rate, 2: all rates) {@min 0} {@max 2}
  654. * @param string $sortfield Sort field
  655. * @param string $sortorder Sort order
  656. * @param int $limit Number of items per page
  657. * @param int $page Page number (starting from zero)
  658. * @param int $active Payment term is active or not {@min 0} {@max 1}
  659. * @param string $sqlfilters Other criteria to filter answers separated by a comma. Syntax example "(t.code:like:'A%') and (t.active:>=:0)"
  660. * @return array List of currencies
  661. *
  662. * @url GET dictionary/currencies
  663. *
  664. * @throws RestException
  665. */
  666. public function getListOfCurrencies($multicurrency = 0, $sortfield = "code_iso", $sortorder = 'ASC', $limit = 100, $page = 0, $active = 1, $sqlfilters = '')
  667. {
  668. $list = array();
  669. $sql = "SELECT t.code_iso, t.label, t.unicode";
  670. if (!empty($multicurrency)) $sql .= " , cr.date_sync, cr.rate ";
  671. $sql .= " FROM ".MAIN_DB_PREFIX."c_currencies as t";
  672. if (!empty($multicurrency)) {
  673. $sql .= " JOIN ".MAIN_DB_PREFIX."multicurrency as m ON m.code=t.code_iso";
  674. $sql .= " JOIN ".MAIN_DB_PREFIX."multicurrency_rate as cr ON (m.rowid = cr.fk_multicurrency)";
  675. }
  676. $sql .= " WHERE t.active = ".$active;
  677. if (!empty($multicurrency)) {
  678. $sql .= " AND m.entity IN (".getEntity('multicurrency').")";
  679. if (!empty($multicurrency) && $multicurrency != 2) {
  680. $sql .= " AND cr.date_sync = (SELECT MAX(cr2.date_sync) FROM ".MAIN_DB_PREFIX."multicurrency_rate AS cr2 WHERE cr2.fk_multicurrency = m.rowid)";
  681. }
  682. }
  683. // Add sql filters
  684. if ($sqlfilters)
  685. {
  686. if (!DolibarrApi::_checkFilters($sqlfilters))
  687. {
  688. throw new RestException(503, 'Error when validating parameter sqlfilters '.$sqlfilters);
  689. }
  690. $regexstring = '\(([^:\'\(\)]+:[^:\'\(\)]+:[^:\(\)]+)\)';
  691. $sql .= " AND (".preg_replace_callback('/'.$regexstring.'/', 'DolibarrApi::_forge_criteria_callback', $sqlfilters).")";
  692. }
  693. $sql .= $this->db->order($sortfield, $sortorder);
  694. if ($limit) {
  695. if ($page < 0) {
  696. $page = 0;
  697. }
  698. $offset = $limit * $page;
  699. $sql .= $this->db->plimit($limit, $offset);
  700. }
  701. $result = $this->db->query($sql);
  702. if ($result) {
  703. $num = $this->db->num_rows($result);
  704. $min = min($num, ($limit <= 0 ? $num : $limit));
  705. for ($i = 0; $i < $min; $i++) {
  706. $list[] = $this->db->fetch_object($result);
  707. }
  708. } else {
  709. throw new RestException(503, 'Error when retrieving list of currency : '.$this->db->lasterror());
  710. }
  711. return $list;
  712. }
  713. /**
  714. * Get the list of extra fields.
  715. *
  716. * @param string $sortfield Sort field
  717. * @param string $sortorder Sort order
  718. * @param string $type Type of element ('adherent', 'commande', 'thirdparty', 'facture', 'propal', 'product', ...)
  719. * @param string $sqlfilters Other criteria to filter answers separated by a comma. Syntax example "(t.label:like:'SO-%')"
  720. * @return array List of extra fields
  721. *
  722. * @url GET extrafields
  723. *
  724. * @throws RestException
  725. */
  726. public function getListOfExtrafields($sortfield = "t.pos", $sortorder = 'ASC', $type = '', $sqlfilters = '')
  727. {
  728. $list = array();
  729. if ($type == 'thirdparty') $type = 'societe';
  730. if ($type == 'contact') $type = 'socpeople';
  731. $sql = "SELECT t.rowid, t.name, t.label, t.type, t.size, t.elementtype, t.fieldunique, t.fieldrequired, t.param, t.pos, t.alwayseditable, t.perms, t.list, t.fielddefault, t.fieldcomputed";
  732. $sql .= " FROM ".MAIN_DB_PREFIX."extrafields as t";
  733. $sql .= " WHERE t.entity IN (".getEntity('extrafields').")";
  734. if (!empty($type)) $sql .= " AND t.elementtype = '".$this->db->escape($type)."'";
  735. // Add sql filters
  736. if ($sqlfilters)
  737. {
  738. if (!DolibarrApi::_checkFilters($sqlfilters))
  739. {
  740. throw new RestException(503, 'Error when validating parameter sqlfilters '.$sqlfilters);
  741. }
  742. $regexstring = '\(([^:\'\(\)]+:[^:\'\(\)]+:[^:\(\)]+)\)';
  743. $sql .= " AND (".preg_replace_callback('/'.$regexstring.'/', 'DolibarrApi::_forge_criteria_callback', $sqlfilters).")";
  744. }
  745. $sql .= $this->db->order($sortfield, $sortorder);
  746. $resql = $this->db->query($sql);
  747. if ($resql)
  748. {
  749. if ($this->db->num_rows($resql))
  750. {
  751. while ($tab = $this->db->fetch_object($resql))
  752. {
  753. // New usage
  754. $list[$tab->elementtype][$tab->name]['type'] = $tab->type;
  755. $list[$tab->elementtype][$tab->name]['label'] = $tab->label;
  756. $list[$tab->elementtype][$tab->name]['size'] = $tab->size;
  757. $list[$tab->elementtype][$tab->name]['elementtype'] = $tab->elementtype;
  758. $list[$tab->elementtype][$tab->name]['default'] = $tab->fielddefault;
  759. $list[$tab->elementtype][$tab->name]['computed'] = $tab->fieldcomputed;
  760. $list[$tab->elementtype][$tab->name]['unique'] = $tab->fieldunique;
  761. $list[$tab->elementtype][$tab->name]['required'] = $tab->fieldrequired;
  762. $list[$tab->elementtype][$tab->name]['param'] = ($tab->param ? unserialize($tab->param) : '');
  763. $list[$tab->elementtype][$tab->name]['pos'] = $tab->pos;
  764. $list[$tab->elementtype][$tab->name]['alwayseditable'] = $tab->alwayseditable;
  765. $list[$tab->elementtype][$tab->name]['perms'] = $tab->perms;
  766. $list[$tab->elementtype][$tab->name]['list'] = $tab->list;
  767. }
  768. }
  769. } else {
  770. throw new RestException(503, 'Error when retrieving list of extra fields : '.$this->db->lasterror());
  771. }
  772. if (!count($list))
  773. {
  774. throw new RestException(404, 'No extrafield found');
  775. }
  776. return $list;
  777. }
  778. /**
  779. * Get the list of towns.
  780. *
  781. * @param string $sortfield Sort field
  782. * @param string $sortorder Sort order
  783. * @param int $limit Number of items per page
  784. * @param int $page Page number (starting from zero)
  785. * @param string $zipcode To filter on zipcode
  786. * @param string $town To filter on city name
  787. * @param int $active Payment term is active or not {@min 0} {@max 1}
  788. * @param string $sqlfilters Other criteria to filter answers separated by a comma. Syntax example "(t.code:like:'A%') and (t.active:>=:0)"
  789. * @return array List of towns
  790. *
  791. * @url GET dictionary/towns
  792. *
  793. * @throws RestException
  794. */
  795. public function getListOfTowns($sortfield = "zip,town", $sortorder = 'ASC', $limit = 100, $page = 0, $zipcode = '', $town = '', $active = 1, $sqlfilters = '')
  796. {
  797. $list = array();
  798. $sql = "SELECT rowid AS id, zip, town, fk_county, fk_pays AS fk_country";
  799. $sql .= " FROM ".MAIN_DB_PREFIX."c_ziptown as t";
  800. $sql .= " AND t.active = ".$active;
  801. if ($zipcode) $sql .= " AND t.zip LIKE '%".$this->db->escape($zipcode)."%'";
  802. if ($town) $sql .= " AND t.town LIKE '%".$this->db->escape($town)."%'";
  803. // Add sql filters
  804. if ($sqlfilters)
  805. {
  806. if (!DolibarrApi::_checkFilters($sqlfilters))
  807. {
  808. throw new RestException(503, 'Error when validating parameter sqlfilters '.$sqlfilters);
  809. }
  810. $regexstring = '\(([^:\'\(\)]+:[^:\'\(\)]+:[^:\(\)]+)\)';
  811. $sql .= " AND (".preg_replace_callback('/'.$regexstring.'/', 'DolibarrApi::_forge_criteria_callback', $sqlfilters).")";
  812. }
  813. $sql .= $this->db->order($sortfield, $sortorder);
  814. if ($limit) {
  815. if ($page < 0) {
  816. $page = 0;
  817. }
  818. $offset = $limit * $page;
  819. $sql .= $this->db->plimit($limit, $offset);
  820. }
  821. $result = $this->db->query($sql);
  822. if ($result) {
  823. $num = $this->db->num_rows($result);
  824. $min = min($num, ($limit <= 0 ? $num : $limit));
  825. for ($i = 0; $i < $min; $i++) {
  826. $list[] = $this->db->fetch_object($result);
  827. }
  828. } else {
  829. throw new RestException(503, 'Error when retrieving list of towns : '.$this->db->lasterror());
  830. }
  831. return $list;
  832. }
  833. /**
  834. * Get the list of payments terms.
  835. *
  836. * @param string $sortfield Sort field
  837. * @param string $sortorder Sort order
  838. * @param int $limit Number of items per page
  839. * @param int $page Page number {@min 0}
  840. * @param int $active Payment term is active or not {@min 0} {@max 1}
  841. * @param string $sqlfilters SQL criteria to filter. Syntax example "(t.code:=:'CHQ')"
  842. *
  843. * @url GET dictionary/payment_terms
  844. *
  845. * @return array List of payment terms
  846. *
  847. * @throws RestException 400
  848. */
  849. public function getPaymentTerms($sortfield = "sortorder", $sortorder = 'ASC', $limit = 100, $page = 0, $active = 1, $sqlfilters = '')
  850. {
  851. $list = array();
  852. $sql = "SELECT rowid as id, code, sortorder, libelle as label, libelle_facture as descr, type_cdr, nbjour, decalage, module";
  853. $sql .= " FROM ".MAIN_DB_PREFIX."c_payment_term as t";
  854. $sql .= " WHERE t.entity IN (".getEntity('c_payment_term').")";
  855. $sql .= " AND t.active = ".$active;
  856. // Add sql filters
  857. if ($sqlfilters)
  858. {
  859. if (!DolibarrApi::_checkFilters($sqlfilters))
  860. {
  861. throw new RestException(400, 'Error when validating parameter sqlfilters '.$sqlfilters);
  862. }
  863. $regexstring = '\(([^:\'\(\)]+:[^:\'\(\)]+:[^:\(\)]+)\)';
  864. $sql .= " AND (".preg_replace_callback('/'.$regexstring.'/', 'DolibarrApi::_forge_criteria_callback', $sqlfilters).")";
  865. }
  866. $sql .= $this->db->order($sortfield, $sortorder);
  867. if ($limit) {
  868. if ($page < 0) {
  869. $page = 0;
  870. }
  871. $offset = $limit * $page;
  872. $sql .= $this->db->plimit($limit, $offset);
  873. }
  874. $result = $this->db->query($sql);
  875. if ($result) {
  876. $num = $this->db->num_rows($result);
  877. $min = min($num, ($limit <= 0 ? $num : $limit));
  878. for ($i = 0; $i < $min; $i++) {
  879. $list[] = $this->db->fetch_object($result);
  880. }
  881. } else {
  882. throw new RestException(400, $this->db->lasterror());
  883. }
  884. return $list;
  885. }
  886. /**
  887. * Get the list of shipping methods.
  888. *
  889. * @param int $limit Number of items per page
  890. * @param int $page Page number {@min 0}
  891. * @param int $active Shipping methodsm is active or not {@min 0} {@max 1}
  892. * @param string $sqlfilters SQL criteria to filter. Syntax example "(t.code:=:'CHQ')"
  893. *
  894. * @url GET dictionary/shipping_methods
  895. *
  896. * @return array List of shipping methods
  897. *
  898. * @throws RestException 400
  899. */
  900. public function getShippingModes($limit = 100, $page = 0, $active = 1, $sqlfilters = '')
  901. {
  902. $list = array();
  903. $sql = "SELECT rowid as id, code, libelle as label, description, tracking, module";
  904. $sql .= " FROM ".MAIN_DB_PREFIX."c_shipment_mode as t";
  905. $sql .= " WHERE t.entity IN (".getEntity('c_shipment_mode').")";
  906. $sql .= " AND t.active = ".$active;
  907. // Add sql filters
  908. if ($sqlfilters)
  909. {
  910. if (!DolibarrApi::_checkFilters($sqlfilters))
  911. {
  912. throw new RestException(400, 'Error when validating parameter sqlfilters '.$sqlfilters);
  913. }
  914. $regexstring = '\(([^:\'\(\)]+:[^:\'\(\)]+:[^:\(\)]+)\)';
  915. $sql .= " AND (".preg_replace_callback('/'.$regexstring.'/', 'DolibarrApi::_forge_criteria_callback', $sqlfilters).")";
  916. }
  917. //$sql.= $this->db->order($sortfield, $sortorder);
  918. if ($limit) {
  919. if ($page < 0) {
  920. $page = 0;
  921. }
  922. $offset = $limit * $page;
  923. $sql .= $this->db->plimit($limit, $offset);
  924. }
  925. $result = $this->db->query($sql);
  926. if ($result) {
  927. $num = $this->db->num_rows($result);
  928. $min = min($num, ($limit <= 0 ? $num : $limit));
  929. for ($i = 0; $i < $min; $i++) {
  930. $list[] = $this->db->fetch_object($result);
  931. }
  932. } else {
  933. throw new RestException(400, $this->db->lasterror());
  934. }
  935. return $list;
  936. }
  937. /**
  938. * Get the list of measuring units.
  939. *
  940. * @param string $sortfield Sort field
  941. * @param string $sortorder Sort order
  942. * @param int $limit Number of items per page
  943. * @param int $page Page number (starting from zero)
  944. * @param int $active Measuring unit is active or not {@min 0} {@max 1}
  945. * @param string $sqlfilters Other criteria to filter answers separated by a comma. Syntax example "(t.code:like:'A%') and (t.active:>=:0)"
  946. * @return array List of measuring unit
  947. *
  948. * @url GET dictionary/units
  949. *
  950. * @throws RestException
  951. */
  952. public function getListOfMeasuringUnits($sortfield = "rowid", $sortorder = 'ASC', $limit = 100, $page = 0, $active = 1, $sqlfilters = '')
  953. {
  954. $list = array();
  955. //TODO link with multicurrency module
  956. $sql = "SELECT t.rowid, t.code, t.label,t.short_label, t.active, t.scale, t.unit_type";
  957. $sql .= " FROM ".MAIN_DB_PREFIX."c_units as t";
  958. $sql .= " WHERE t.active = ".$active;
  959. // Add sql filters
  960. if ($sqlfilters)
  961. {
  962. if (!DolibarrApi::_checkFilters($sqlfilters))
  963. {
  964. throw new RestException(503, 'Error when validating parameter sqlfilters '.$sqlfilters);
  965. }
  966. $regexstring = '\(([^:\'\(\)]+:[^:\'\(\)]+:[^:\(\)]+)\)';
  967. $sql .= " AND (".preg_replace_callback('/'.$regexstring.'/', 'DolibarrApi::_forge_criteria_callback', $sqlfilters).")";
  968. }
  969. $sql .= $this->db->order($sortfield, $sortorder);
  970. if ($limit) {
  971. if ($page < 0) {
  972. $page = 0;
  973. }
  974. $offset = $limit * $page;
  975. $sql .= $this->db->plimit($limit, $offset);
  976. }
  977. $result = $this->db->query($sql);
  978. if ($result) {
  979. $num = $this->db->num_rows($result);
  980. $min = min($num, ($limit <= 0 ? $num : $limit));
  981. for ($i = 0; $i < $min; $i++) {
  982. $list[] = $this->db->fetch_object($result);
  983. }
  984. } else {
  985. throw new RestException(503, 'Error when retrieving list of measuring units: '.$this->db->lasterror());
  986. }
  987. return $list;
  988. }
  989. /**
  990. * Get the list of social networks.
  991. *
  992. * @param string $sortfield Sort field
  993. * @param string $sortorder Sort order
  994. * @param int $limit Number of items per page
  995. * @param int $page Page number (starting from zero)
  996. * @param int $active Social network is active or not {@min 0} {@max 1}
  997. * @param string $sqlfilters Other criteria to filter answers separated by a comma. Syntax example "(t.code:like:'A%') and (t.active:>=:0)"
  998. * @return array List of social networks
  999. *
  1000. * @url GET dictionary/socialnetworks
  1001. *
  1002. * @throws RestException
  1003. */
  1004. public function getListOfsocialNetworks($sortfield = "rowid", $sortorder = 'ASC', $limit = 100, $page = 0, $active = 1, $sqlfilters = '')
  1005. {
  1006. global $conf;
  1007. if (empty($conf->socialnetworks->enabled)) {
  1008. throw new RestException(400, 'API not available: this dictionary is not enabled by setup');
  1009. }
  1010. $list = array();
  1011. //TODO link with multicurrency module
  1012. $sql = "SELECT t.rowid, t.entity, t.code, t.label, t.url, t.icon, t.active";
  1013. $sql .= " FROM ".MAIN_DB_PREFIX."c_socialnetworks as t";
  1014. $sql .= " WHERE t.entity IN (".getEntity('c_socialnetworks').")";
  1015. $sql .= " AND t.active = ".$active;
  1016. // Add sql filters
  1017. if ($sqlfilters)
  1018. {
  1019. if (!DolibarrApi::_checkFilters($sqlfilters))
  1020. {
  1021. throw new RestException(503, 'Error when validating parameter sqlfilters '.$sqlfilters);
  1022. }
  1023. $regexstring = '\(([^:\'\(\)]+:[^:\'\(\)]+:[^:\(\)]+)\)';
  1024. $sql .= " AND (".preg_replace_callback('/'.$regexstring.'/', 'DolibarrApi::_forge_criteria_callback', $sqlfilters).")";
  1025. }
  1026. $sql .= $this->db->order($sortfield, $sortorder);
  1027. if ($limit) {
  1028. if ($page < 0) {
  1029. $page = 0;
  1030. }
  1031. $offset = $limit * $page;
  1032. $sql .= $this->db->plimit($limit, $offset);
  1033. }
  1034. $result = $this->db->query($sql);
  1035. if ($result) {
  1036. $num = $this->db->num_rows($result);
  1037. $min = min($num, ($limit <= 0 ? $num : $limit));
  1038. for ($i = 0; $i < $min; $i++) {
  1039. $list[] = $this->db->fetch_object($result);
  1040. }
  1041. } else {
  1042. throw new RestException(503, 'Error when retrieving list of social networks: '.$this->db->lasterror());
  1043. }
  1044. return $list;
  1045. }
  1046. /**
  1047. * Get the list of tickets categories.
  1048. *
  1049. * @param string $sortfield Sort field
  1050. * @param string $sortorder Sort order
  1051. * @param int $limit Number of items per page
  1052. * @param int $page Page number (starting from zero)
  1053. * @param int $active Payment term is active or not {@min 0} {@max 1}
  1054. * @param string $sqlfilters Other criteria to filter answers separated by a comma. Syntax example "(t.code:like:'A%') and (t.active:>=:0)"
  1055. * @return array List of ticket categories
  1056. *
  1057. * @url GET dictionary/ticket_categories
  1058. *
  1059. * @throws RestException
  1060. */
  1061. public function getTicketsCategories($sortfield = "code", $sortorder = 'ASC', $limit = 100, $page = 0, $active = 1, $sqlfilters = '')
  1062. {
  1063. $list = array();
  1064. $sql = "SELECT rowid, code, pos, label, use_default, description";
  1065. $sql .= " FROM ".MAIN_DB_PREFIX."c_ticket_category as t";
  1066. $sql .= " WHERE t.active = ".$active;
  1067. // Add sql filters
  1068. if ($sqlfilters)
  1069. {
  1070. if (!DolibarrApi::_checkFilters($sqlfilters))
  1071. {
  1072. throw new RestException(503, 'Error when validating parameter sqlfilters '.$sqlfilters);
  1073. }
  1074. $regexstring = '\(([^:\'\(\)]+:[^:\'\(\)]+:[^:\(\)]+)\)';
  1075. $sql .= " AND (".preg_replace_callback('/'.$regexstring.'/', 'DolibarrApi::_forge_criteria_callback', $sqlfilters).")";
  1076. }
  1077. $sql .= $this->db->order($sortfield, $sortorder);
  1078. if ($limit) {
  1079. if ($page < 0) {
  1080. $page = 0;
  1081. }
  1082. $offset = $limit * $page;
  1083. $sql .= $this->db->plimit($limit, $offset);
  1084. }
  1085. $result = $this->db->query($sql);
  1086. if ($result) {
  1087. $num = $this->db->num_rows($result);
  1088. $min = min($num, ($limit <= 0 ? $num : $limit));
  1089. for ($i = 0; $i < $min; $i++) {
  1090. $list[] = $this->db->fetch_object($result);
  1091. }
  1092. } else {
  1093. throw new RestException(503, 'Error when retrieving list of ticket categories : '.$this->db->lasterror());
  1094. }
  1095. return $list;
  1096. }
  1097. /**
  1098. * Get the list of tickets severity.
  1099. *
  1100. * @param string $sortfield Sort field
  1101. * @param string $sortorder Sort order
  1102. * @param int $limit Number of items per page
  1103. * @param int $page Page number (starting from zero)
  1104. * @param int $active Payment term is active or not {@min 0} {@max 1}
  1105. * @param string $sqlfilters Other criteria to filter answers separated by a comma. Syntax example "(t.code:like:'A%') and (t.active:>=:0)"
  1106. * @return array List of ticket severities
  1107. *
  1108. * @url GET dictionary/ticket_severities
  1109. *
  1110. * @throws RestException
  1111. */
  1112. public function getTicketsSeverities($sortfield = "code", $sortorder = 'ASC', $limit = 100, $page = 0, $active = 1, $sqlfilters = '')
  1113. {
  1114. $list = array();
  1115. $sql = "SELECT rowid, code, pos, label, use_default, color, description";
  1116. $sql .= " FROM ".MAIN_DB_PREFIX."c_ticket_severity as t";
  1117. $sql .= " WHERE t.active = ".$active;
  1118. // Add sql filters
  1119. if ($sqlfilters)
  1120. {
  1121. if (!DolibarrApi::_checkFilters($sqlfilters))
  1122. {
  1123. throw new RestException(503, 'Error when validating parameter sqlfilters '.$sqlfilters);
  1124. }
  1125. $regexstring = '\(([^:\'\(\)]+:[^:\'\(\)]+:[^:\(\)]+)\)';
  1126. $sql .= " AND (".preg_replace_callback('/'.$regexstring.'/', 'DolibarrApi::_forge_criteria_callback', $sqlfilters).")";
  1127. }
  1128. $sql .= $this->db->order($sortfield, $sortorder);
  1129. if ($limit) {
  1130. if ($page < 0) {
  1131. $page = 0;
  1132. }
  1133. $offset = $limit * $page;
  1134. $sql .= $this->db->plimit($limit, $offset);
  1135. }
  1136. $result = $this->db->query($sql);
  1137. if ($result) {
  1138. $num = $this->db->num_rows($result);
  1139. $min = min($num, ($limit <= 0 ? $num : $limit));
  1140. for ($i = 0; $i < $min; $i++) {
  1141. $list[] = $this->db->fetch_object($result);
  1142. }
  1143. } else {
  1144. throw new RestException(503, 'Error when retrieving list of ticket severities : '.$this->db->lasterror());
  1145. }
  1146. return $list;
  1147. }
  1148. /**
  1149. * Get the list of tickets types.
  1150. *
  1151. * @param string $sortfield Sort field
  1152. * @param string $sortorder Sort order
  1153. * @param int $limit Number of items per page
  1154. * @param int $page Page number (starting from zero)
  1155. * @param int $active Payment term is active or not {@min 0} {@max 1}
  1156. * @param string $sqlfilters Other criteria to filter answers separated by a comma. Syntax example "(t.code:like:'A%') and (t.active:>=:0)"
  1157. * @return array List of ticket types
  1158. *
  1159. * @url GET dictionary/ticket_types
  1160. *
  1161. * @throws RestException
  1162. */
  1163. public function getTicketsTypes($sortfield = "code", $sortorder = 'ASC', $limit = 100, $page = 0, $active = 1, $sqlfilters = '')
  1164. {
  1165. $list = array();
  1166. $sql = "SELECT rowid, code, pos, label, use_default, description";
  1167. $sql .= " FROM ".MAIN_DB_PREFIX."c_ticket_type as t";
  1168. $sql .= " WHERE t.active = ".(int) $active;
  1169. // if ($type) $sql .= " AND t.type LIKE '%".$this->db->escape($type)."%'";
  1170. // if ($module) $sql .= " AND t.module LIKE '%".$this->db->escape($module)."%'";
  1171. // Add sql filters
  1172. if ($sqlfilters)
  1173. {
  1174. if (!DolibarrApi::_checkFilters($sqlfilters))
  1175. {
  1176. throw new RestException(503, 'Error when validating parameter sqlfilters '.$sqlfilters);
  1177. }
  1178. $regexstring = '\(([^:\'\(\)]+:[^:\'\(\)]+:[^:\(\)]+)\)';
  1179. $sql .= " AND (".preg_replace_callback('/'.$regexstring.'/', 'DolibarrApi::_forge_criteria_callback', $sqlfilters).")";
  1180. }
  1181. $sql .= $this->db->order($sortfield, $sortorder);
  1182. if ($limit) {
  1183. if ($page < 0) {
  1184. $page = 0;
  1185. }
  1186. $offset = $limit * $page;
  1187. $sql .= $this->db->plimit($limit, $offset);
  1188. }
  1189. $result = $this->db->query($sql);
  1190. if ($result) {
  1191. $num = $this->db->num_rows($result);
  1192. $min = min($num, ($limit <= 0 ? $num : $limit));
  1193. for ($i = 0; $i < $min; $i++) {
  1194. $list[] = $this->db->fetch_object($result);
  1195. }
  1196. } else {
  1197. throw new RestException(503, 'Error when retrieving list of ticket types : '.$this->db->lasterror());
  1198. }
  1199. return $list;
  1200. }
  1201. /**
  1202. * Get properties of company
  1203. *
  1204. * @url GET /company
  1205. *
  1206. * @return array|mixed Data without useless information
  1207. *
  1208. */
  1209. public function getCompany()
  1210. {
  1211. global $mysoc;
  1212. return $this->_cleanObjectDatas($mysoc);
  1213. }
  1214. /**
  1215. * Get value of a setup variables
  1216. *
  1217. * Note that conf variables that stores security key or password hashes can't be loaded with API.
  1218. *
  1219. * @param string $constantname Name of conf variable to get
  1220. * @return array|mixed Data without useless information
  1221. *
  1222. * @url GET conf/{constantname}
  1223. *
  1224. * @throws RestException 403 Forbidden
  1225. * @throws RestException 500 Error Bad or unknown value for constantname
  1226. */
  1227. public function getConf($constantname)
  1228. {
  1229. global $conf;
  1230. if (!DolibarrApiAccess::$user->admin
  1231. && (empty($conf->global->API_LOGIN_ALLOWED_FOR_ADMIN_CHECK) || DolibarrApiAccess::$user->login != $conf->global->API_LOGIN_ALLOWED_FOR_ADMIN_CHECK)) {
  1232. throw new RestException(403, 'Error API open to admin users only or to the login user defined with constant API_LOGIN_ALLOWED_FOR_ADMIN_CHECK');
  1233. }
  1234. if (!preg_match('/^[a-zA-Z0-9_]+$/', $constantname) || !isset($conf->global->$constantname)) {
  1235. throw new RestException(500, 'Error Bad or unknown value for constantname');
  1236. }
  1237. if (preg_match('/(_pass|_pw|password|secret|_key|key$)/i', $constantname)) {
  1238. throw new RestException(403, 'Forbidden');
  1239. }
  1240. return $conf->global->$constantname;
  1241. }
  1242. /**
  1243. * Do a test of integrity for files and setup.
  1244. *
  1245. * @param string $target Can be 'local' or 'default' or Url of the signatures file to use for the test. Must be reachable by the tested Dolibarr.
  1246. * @return array Result of file and setup integrity check
  1247. *
  1248. * @url GET checkintegrity
  1249. *
  1250. * @throws RestException 404 Signature file not found
  1251. * @throws RestException 500 Technical error
  1252. * @throws RestException 503 Forbidden
  1253. */
  1254. public function getCheckIntegrity($target)
  1255. {
  1256. global $langs, $conf;
  1257. if (!DolibarrApiAccess::$user->admin
  1258. && (empty($conf->global->API_LOGIN_ALLOWED_FOR_INTEGRITY_CHECK) || DolibarrApiAccess::$user->login != $conf->global->API_LOGIN_ALLOWED_FOR_INTEGRITY_CHECK))
  1259. {
  1260. throw new RestException(503, 'Error API open to admin users only or to the login user defined with constant API_LOGIN_ALLOWED_FOR_INTEGRITY_CHECK');
  1261. }
  1262. require_once DOL_DOCUMENT_ROOT.'/core/lib/files.lib.php';
  1263. require_once DOL_DOCUMENT_ROOT.'/core/lib/geturl.lib.php';
  1264. $langs->load("admin");
  1265. $outexpectedchecksum = '';
  1266. $outcurrentchecksum = '';
  1267. // Modified or missing files
  1268. $file_list = array('missing' => array(), 'updated' => array());
  1269. // Local file to compare to
  1270. $xmlshortfile = GETPOST('xmlshortfile') ?GETPOST('xmlshortfile') : '/install/filelist-'.DOL_VERSION.'.xml';
  1271. $xmlfile = DOL_DOCUMENT_ROOT.$xmlshortfile;
  1272. // Remote file to compare to
  1273. $xmlremote = ($target == 'default' ? '' : $target);
  1274. if (empty($xmlremote) && !empty($conf->global->MAIN_FILECHECK_URL)) $xmlremote = $conf->global->MAIN_FILECHECK_URL;
  1275. $param = 'MAIN_FILECHECK_URL_'.DOL_VERSION;
  1276. if (empty($xmlremote) && !empty($conf->global->$param)) $xmlremote = $conf->global->$param;
  1277. if (empty($xmlremote)) $xmlremote = 'https://www.dolibarr.org/files/stable/signatures/filelist-'.DOL_VERSION.'.xml';
  1278. if ($target == 'local')
  1279. {
  1280. if (dol_is_file($xmlfile))
  1281. {
  1282. $xml = simplexml_load_file($xmlfile);
  1283. } else {
  1284. throw new RestException(500, $langs->trans('XmlNotFound').': '.$xmlfile);
  1285. }
  1286. } else {
  1287. $xmlarray = getURLContent($xmlremote);
  1288. // Return array('content'=>response,'curl_error_no'=>errno,'curl_error_msg'=>errmsg...)
  1289. if (!$xmlarray['curl_error_no'] && $xmlarray['http_code'] != '404')
  1290. {
  1291. $xmlfile = $xmlarray['content'];
  1292. //print "xmlfilestart".$xmlfile."endxmlfile";
  1293. $xml = simplexml_load_string($xmlfile);
  1294. } else {
  1295. $errormsg = $langs->trans('XmlNotFound').': '.$xmlremote.' - '.$xmlarray['http_code'].' '.$xmlarray['curl_error_no'].' '.$xmlarray['curl_error_msg'];
  1296. throw new RestException(500, $errormsg);
  1297. }
  1298. }
  1299. if ($xml)
  1300. {
  1301. $checksumconcat = array();
  1302. $file_list = array();
  1303. $out = '';
  1304. // Forced constants
  1305. if (is_object($xml->dolibarr_constants[0]))
  1306. {
  1307. $out .= load_fiche_titre($langs->trans("ForcedConstants"));
  1308. $out .= '<div class="div-table-responsive-no-min">';
  1309. $out .= '<table class="noborder">';
  1310. $out .= '<tr class="liste_titre">';
  1311. $out .= '<td>#</td>';
  1312. $out .= '<td>'.$langs->trans("Constant").'</td>';
  1313. $out .= '<td class="center">'.$langs->trans("ExpectedValue").'</td>';
  1314. $out .= '<td class="center">'.$langs->trans("Value").'</td>';
  1315. $out .= '</tr>'."\n";
  1316. $i = 0;
  1317. foreach ($xml->dolibarr_constants[0]->constant as $constant) // $constant is a simpleXMLElement
  1318. {
  1319. $constname = $constant['name'];
  1320. $constvalue = (string) $constant;
  1321. $constvalue = (empty($constvalue) ? '0' : $constvalue);
  1322. // Value found
  1323. $value = '';
  1324. if ($constname && $conf->global->$constname != '') $value = $conf->global->$constname;
  1325. $valueforchecksum = (empty($value) ? '0' : $value);
  1326. $checksumconcat[] = $valueforchecksum;
  1327. $i++;
  1328. $out .= '<tr class="oddeven">';
  1329. $out .= '<td>'.$i.'</td>'."\n";
  1330. $out .= '<td>'.$constname.'</td>'."\n";
  1331. $out .= '<td class="center">'.$constvalue.'</td>'."\n";
  1332. $out .= '<td class="center">'.$valueforchecksum.'</td>'."\n";
  1333. $out .= "</tr>\n";
  1334. }
  1335. if ($i == 0)
  1336. {
  1337. $out .= '<tr class="oddeven"><td colspan="4" class="opacitymedium">'.$langs->trans("None").'</td></tr>';
  1338. }
  1339. $out .= '</table>';
  1340. $out .= '</div>';
  1341. $out .= '<br>';
  1342. }
  1343. // Scan htdocs
  1344. if (is_object($xml->dolibarr_htdocs_dir[0]))
  1345. {
  1346. //var_dump($xml->dolibarr_htdocs_dir[0]['includecustom']);exit;
  1347. $includecustom = (empty($xml->dolibarr_htdocs_dir[0]['includecustom']) ? 0 : $xml->dolibarr_htdocs_dir[0]['includecustom']);
  1348. // Defined qualified files (must be same than into generate_filelist_xml.php)
  1349. $regextoinclude = '\.(php|php3|php4|php5|phtml|phps|phar|inc|css|scss|html|xml|js|json|tpl|jpg|jpeg|png|gif|ico|sql|lang|txt|yml|bak|md|mp3|mp4|wav|mkv|z|gz|zip|rar|tar|less|svg|eot|woff|woff2|ttf|manifest)$';
  1350. $regextoexclude = '('.($includecustom ? '' : 'custom|').'documents|conf|install|public\/test|Shared\/PCLZip|nusoap\/lib\/Mail|php\/example|php\/test|geoip\/sample.*\.php|ckeditor\/samples|ckeditor\/adapters)$'; // Exclude dirs
  1351. $scanfiles = dol_dir_list(DOL_DOCUMENT_ROOT, 'files', 1, $regextoinclude, $regextoexclude);
  1352. // Fill file_list with files in signature, new files, modified files
  1353. $ret = getFilesUpdated($file_list, $xml->dolibarr_htdocs_dir[0], '', DOL_DOCUMENT_ROOT, $checksumconcat); // Fill array $file_list
  1354. // Complete with list of new files
  1355. foreach ($scanfiles as $keyfile => $valfile)
  1356. {
  1357. $tmprelativefilename = preg_replace('/^'.preg_quote(DOL_DOCUMENT_ROOT, '/').'/', '', $valfile['fullname']);
  1358. if (!in_array($tmprelativefilename, $file_list['insignature']))
  1359. {
  1360. $md5newfile = @md5_file($valfile['fullname']); // Can fails if we don't have permission to open/read file
  1361. $file_list['added'][] = array('filename'=>$tmprelativefilename, 'md5'=>$md5newfile);
  1362. }
  1363. }
  1364. // Files missings
  1365. $out .= load_fiche_titre($langs->trans("FilesMissing"));
  1366. $out .= '<div class="div-table-responsive-no-min">';
  1367. $out .= '<table class="noborder">';
  1368. $out .= '<tr class="liste_titre">';
  1369. $out .= '<td>#</td>';
  1370. $out .= '<td>'.$langs->trans("Filename").'</td>';
  1371. $out .= '<td class="center">'.$langs->trans("ExpectedChecksum").'</td>';
  1372. $out .= '</tr>'."\n";
  1373. $tmpfilelist = dol_sort_array($file_list['missing'], 'filename');
  1374. if (is_array($tmpfilelist) && count($tmpfilelist))
  1375. {
  1376. $i = 0;
  1377. foreach ($tmpfilelist as $file)
  1378. {
  1379. $i++;
  1380. $out .= '<tr class="oddeven">';
  1381. $out .= '<td>'.$i.'</td>'."\n";
  1382. $out .= '<td>'.$file['filename'].'</td>'."\n";
  1383. $out .= '<td class="center">'.$file['expectedmd5'].'</td>'."\n";
  1384. $out .= "</tr>\n";
  1385. }
  1386. } else {
  1387. $out .= '<tr class="oddeven"><td colspan="3" class="opacitymedium">'.$langs->trans("None").'</td></tr>';
  1388. }
  1389. $out .= '</table>';
  1390. $out .= '</div>';
  1391. $out .= '<br>';
  1392. // Files modified
  1393. $out .= load_fiche_titre($langs->trans("FilesModified"));
  1394. $totalsize = 0;
  1395. $out .= '<div class="div-table-responsive-no-min">';
  1396. $out .= '<table class="noborder">';
  1397. $out .= '<tr class="liste_titre">';
  1398. $out .= '<td>#</td>';
  1399. $out .= '<td>'.$langs->trans("Filename").'</td>';
  1400. $out .= '<td class="center">'.$langs->trans("ExpectedChecksum").'</td>';
  1401. $out .= '<td class="center">'.$langs->trans("CurrentChecksum").'</td>';
  1402. $out .= '<td class="right">'.$langs->trans("Size").'</td>';
  1403. $out .= '<td class="right">'.$langs->trans("DateModification").'</td>';
  1404. $out .= '</tr>'."\n";
  1405. $tmpfilelist2 = dol_sort_array($file_list['updated'], 'filename');
  1406. if (is_array($tmpfilelist2) && count($tmpfilelist2))
  1407. {
  1408. $i = 0;
  1409. foreach ($tmpfilelist2 as $file)
  1410. {
  1411. $i++;
  1412. $out .= '<tr class="oddeven">';
  1413. $out .= '<td>'.$i.'</td>'."\n";
  1414. $out .= '<td>'.$file['filename'].'</td>'."\n";
  1415. $out .= '<td class="center">'.$file['expectedmd5'].'</td>'."\n";
  1416. $out .= '<td class="center">'.$file['md5'].'</td>'."\n";
  1417. $size = dol_filesize(DOL_DOCUMENT_ROOT.'/'.$file['filename']);
  1418. $totalsize += $size;
  1419. $out .= '<td class="right">'.dol_print_size($size).'</td>'."\n";
  1420. $out .= '<td class="right">'.dol_print_date(dol_filemtime(DOL_DOCUMENT_ROOT.'/'.$file['filename']), 'dayhour').'</td>'."\n";
  1421. $out .= "</tr>\n";
  1422. }
  1423. $out .= '<tr class="liste_total">';
  1424. $out .= '<td></td>'."\n";
  1425. $out .= '<td>'.$langs->trans("Total").'</td>'."\n";
  1426. $out .= '<td align="center"></td>'."\n";
  1427. $out .= '<td align="center"></td>'."\n";
  1428. $out .= '<td class="right">'.dol_print_size($totalsize).'</td>'."\n";
  1429. $out .= '<td class="right"></td>'."\n";
  1430. $out .= "</tr>\n";
  1431. } else {
  1432. $out .= '<tr class="oddeven"><td colspan="5" class="opacitymedium">'.$langs->trans("None").'</td></tr>';
  1433. }
  1434. $out .= '</table>';
  1435. $out .= '</div>';
  1436. $out .= '<br>';
  1437. // Files added
  1438. $out .= load_fiche_titre($langs->trans("FilesAdded"));
  1439. $totalsize = 0;
  1440. $out .= '<div class="div-table-responsive-no-min">';
  1441. $out .= '<table class="noborder">';
  1442. $out .= '<tr class="liste_titre">';
  1443. $out .= '<td>#</td>';
  1444. $out .= '<td>'.$langs->trans("Filename").'</td>';
  1445. $out .= '<td class="center">'.$langs->trans("ExpectedChecksum").'</td>';
  1446. $out .= '<td class="center">'.$langs->trans("CurrentChecksum").'</td>';
  1447. $out .= '<td class="right">'.$langs->trans("Size").'</td>';
  1448. $out .= '<td class="right">'.$langs->trans("DateModification").'</td>';
  1449. $out .= '</tr>'."\n";
  1450. $tmpfilelist3 = dol_sort_array($file_list['added'], 'filename');
  1451. if (is_array($tmpfilelist3) && count($tmpfilelist3))
  1452. {
  1453. $i = 0;
  1454. foreach ($tmpfilelist3 as $file)
  1455. {
  1456. $i++;
  1457. $out .= '<tr class="oddeven">';
  1458. $out .= '<td>'.$i.'</td>'."\n";
  1459. $out .= '<td>'.$file['filename'].'</td>'."\n";
  1460. $out .= '<td class="center">'.$file['expectedmd5'].'</td>'."\n";
  1461. $out .= '<td class="center">'.$file['md5'].'</td>'."\n";
  1462. $size = dol_filesize(DOL_DOCUMENT_ROOT.'/'.$file['filename']);
  1463. $totalsize += $size;
  1464. $out .= '<td class="right">'.dol_print_size($size).'</td>'."\n";
  1465. $out .= '<td class="right">'.dol_print_date(dol_filemtime(DOL_DOCUMENT_ROOT.'/'.$file['filename']), 'dayhour').'</td>'."\n";
  1466. $out .= "</tr>\n";
  1467. }
  1468. $out .= '<tr class="liste_total">';
  1469. $out .= '<td></td>'."\n";
  1470. $out .= '<td>'.$langs->trans("Total").'</td>'."\n";
  1471. $out .= '<td align="center"></td>'."\n";
  1472. $out .= '<td align="center"></td>'."\n";
  1473. $out .= '<td class="right">'.dol_print_size($totalsize).'</td>'."\n";
  1474. $out .= '<td class="right"></td>'."\n";
  1475. $out .= "</tr>\n";
  1476. } else {
  1477. $out .= '<tr class="oddeven"><td colspan="5" class="opacitymedium">'.$langs->trans("None").'</td></tr>';
  1478. }
  1479. $out .= '</table>';
  1480. $out .= '</div>';
  1481. // Show warning
  1482. if (empty($tmpfilelist) && empty($tmpfilelist2) && empty($tmpfilelist3))
  1483. {
  1484. //setEventMessages($langs->trans("FileIntegrityIsStrictlyConformedWithReference"), null, 'mesgs');
  1485. } else {
  1486. //setEventMessages($langs->trans("FileIntegritySomeFilesWereRemovedOrModified"), null, 'warnings');
  1487. }
  1488. } else {
  1489. throw new RestException(500, 'Error: Failed to found dolibarr_htdocs_dir into XML file '.$xmlfile);
  1490. }
  1491. // Scan scripts
  1492. asort($checksumconcat); // Sort list of checksum
  1493. //var_dump($checksumconcat);
  1494. $checksumget = md5(join(',', $checksumconcat));
  1495. $checksumtoget = trim((string) $xml->dolibarr_htdocs_dir_checksum);
  1496. $outexpectedchecksum = ($checksumtoget ? $checksumtoget : $langs->trans("Unknown"));
  1497. if ($checksumget == $checksumtoget)
  1498. {
  1499. if (count($file_list['added']))
  1500. {
  1501. $resultcode = 'warning';
  1502. $resultcomment = 'FileIntegrityIsOkButFilesWereAdded';
  1503. //$outcurrentchecksum = $checksumget.' - <span class="'.$resultcode.'">'.$langs->trans("FileIntegrityIsOkButFilesWereAdded").'</span>';
  1504. $outcurrentchecksum = $checksumget;
  1505. } else {
  1506. $resultcode = 'ok';
  1507. $resultcomment = 'Success';
  1508. //$outcurrentchecksum = '<span class="'.$resultcode.'">'.$checksumget.'</span>';
  1509. $outcurrentchecksum = $checksumget;
  1510. }
  1511. } else {
  1512. $resultcode = 'error';
  1513. $resultcomment = 'Error';
  1514. //$outcurrentchecksum = '<span class="'.$resultcode.'">'.$checksumget.'</span>';
  1515. $outcurrentchecksum = $checksumget;
  1516. }
  1517. } else {
  1518. throw new RestException(404, 'No signature file known');
  1519. }
  1520. return array('resultcode'=>$resultcode, 'resultcomment'=>$resultcomment, 'expectedchecksum'=> $outexpectedchecksum, 'currentchecksum'=> $outcurrentchecksum, 'out'=>$out);
  1521. }
  1522. }