dolibarr  9.0.0
api_setup.class.php
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  *
8  *
9  * This program is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 3 of the License, or
12  * (at your option) any later version.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17  * GNU General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License
20  * along with this program. If not, see <http://www.gnu.org/licenses/>.
21  */
22 
24 
25 require_once DOL_DOCUMENT_ROOT.'/main.inc.php';
26 require_once DOL_DOCUMENT_ROOT.'/core/class/ccountry.class.php';
27 
34 class Setup extends DolibarrApi
35 {
36  private $translations = null;
37 
41  function __construct()
42  {
43  global $db;
44  $this->db = $db;
45  }
46 
64  function getPaymentTypes($sortfield = "code", $sortorder = 'ASC', $limit = 100, $page = 0, $active = 1, $sqlfilters = '')
65  {
66  $list = array();
67 
68  $sql = "SELECT id, code, type, libelle as label, module";
69  $sql.= " FROM ".MAIN_DB_PREFIX."c_paiement as t";
70  $sql.= " WHERE t.entity IN (".getEntity('c_paiement').")";
71  $sql.= " AND t.active = ".$active;
72  // Add sql filters
73  if ($sqlfilters)
74  {
75  if (! DolibarrApi::_checkFilters($sqlfilters))
76  {
77  throw new RestException(400, 'error when validating parameter sqlfilters '.$sqlfilters);
78  }
79  $regexstring='\(([^:\'\(\)]+:[^:\'\(\)]+:[^:\(\)]+)\)';
80  $sql.=" AND (".preg_replace_callback('/'.$regexstring.'/', 'DolibarrApi::_forge_criteria_callback', $sqlfilters).")";
81  }
82 
83 
84  $sql.= $this->db->order($sortfield, $sortorder);
85 
86  if ($limit) {
87  if ($page < 0) {
88  $page = 0;
89  }
90  $offset = $limit * $page;
91 
92  $sql .= $this->db->plimit($limit, $offset);
93  }
94 
95  $result = $this->db->query($sql);
96 
97  if ($result) {
98  $num = $this->db->num_rows($result);
99  $min = min($num, ($limit <= 0 ? $num : $limit));
100  for ($i = 0; $i < $min; $i++) {
101  $list[] = $this->db->fetch_object($result);
102  }
103  } else {
104  throw new RestException(400, $this->db->lasterror());
105  }
106 
107  return $list;
108  }
109 
131  function getListOfCountries($sortfield = "code", $sortorder = 'ASC', $limit = 100, $page = 0, $filter = '', $lang = '', $sqlfilters = '')
132  {
133  $list = array();
134 
135  // Note: The filter is not applied in the SQL request because it must
136  // be applied to the translated names, not to the names in database.
137  $sql = "SELECT rowid FROM ".MAIN_DB_PREFIX."c_country as t";
138  $sql.=" WHERE 1 = 1";
139  // Add sql filters
140  if ($sqlfilters)
141  {
142  if (! DolibarrApi::_checkFilters($sqlfilters))
143  {
144  throw new RestException(503, 'Error when validating parameter sqlfilters '.$sqlfilters);
145  }
146  $regexstring='\(([^:\'\(\)]+:[^:\'\(\)]+:[^:\(\)]+)\)';
147  $sql.=" AND (".preg_replace_callback('/'.$regexstring.'/', 'DolibarrApi::_forge_criteria_callback', $sqlfilters).")";
148  }
149 
150  $sql.= $this->db->order($sortfield, $sortorder);
151 
152  if ($limit) {
153  if ($page < 0) {
154  $page = 0;
155  }
156  $offset = $limit * $page;
157 
158  $sql .= $this->db->plimit($limit, $offset);
159  }
160 
161  $result = $this->db->query($sql);
162 
163  if ($result) {
164  $num = $this->db->num_rows($result);
165  $min = min($num, ($limit <= 0 ? $num : $limit));
166  for ($i = 0; $i < $min; $i++) {
167  $obj = $this->db->fetch_object($result);
168  $country = new Ccountry($this->db);
169  if ($country->fetch($obj->rowid) > 0) {
170  // Translate the name of the country if needed
171  // and then apply the filter if there is one.
172  $this->translateLabel($country, $lang);
173 
174  if (empty($filter) || stripos($country->label, $filter) !== false) {
175  $list[] = $this->_cleanObjectDatas($country);
176  }
177  }
178  }
179  } else {
180  throw new RestException(503, 'Error when retrieving list of countries : '.$country->error);
181  }
182 
183  return $list;
184  }
185 
198  function getCountryByID($id, $lang = '')
199  {
200  $country = new Ccountry($this->db);
201 
202  if ($country->fetch($id) < 0) {
203  throw new RestException(503, 'Error when retrieving country : '.$country->error);
204  }
205  else if ($country->fetch($id) == 0) {
206  throw new RestException(404, 'country not found');
207  }
208 
209  $this->translateLabel($country, $lang);
210 
211  return $this->_cleanObjectDatas($country);
212  }
213 
231  function getAvailability($sortfield = "code", $sortorder = 'ASC', $limit = 100, $page = 0, $active = 1, $sqlfilters = '')
232  {
233  $list = array();
234 
235  $sql = "SELECT rowid, code, label";
236  $sql.= " FROM ".MAIN_DB_PREFIX."c_availability as t";
237  $sql.= " WHERE t.active = ".$active;
238  // Add sql filters
239  if ($sqlfilters)
240  {
241  if (! DolibarrApi::_checkFilters($sqlfilters))
242  {
243  throw new RestException(400, 'error when validating parameter sqlfilters '.$sqlfilters);
244  }
245  $regexstring='\(([^:\'\(\)]+:[^:\'\(\)]+:[^:\(\)]+)\)';
246  $sql.=" AND (".preg_replace_callback('/'.$regexstring.'/', 'DolibarrApi::_forge_criteria_callback', $sqlfilters).")";
247  }
248 
249 
250  $sql.= $this->db->order($sortfield, $sortorder);
251 
252  if ($limit) {
253  if ($page < 0) {
254  $page = 0;
255  }
256  $offset = $limit * $page;
257 
258  $sql .= $this->db->plimit($limit, $offset);
259  }
260 
261  $result = $this->db->query($sql);
262 
263  if ($result) {
264  $num = $this->db->num_rows($result);
265  $min = min($num, ($limit <= 0 ? $num : $limit));
266  for ($i = 0; $i < $min; $i++) {
267  $list[] = $this->db->fetch_object($result);
268  }
269  } else {
270  throw new RestException(400, $this->db->lasterror());
271  }
272 
273  return $list;
274  }
275 
282  function _cleanObjectDatas($object)
283  {
284  $object = parent::_cleanObjectDatas($object);
285 
286  unset($object->error);
287  unset($object->errors);
288 
289  return $object;
290  }
291 
300  private function translateLabel($country, $lang)
301  {
302  if (!empty($lang)) {
303  // Load the translations if this is a new language.
304  if ($this->translations == null || $this->translations->getDefaultLang() !== $lang) {
305  global $conf;
306  $this->translations = new Translate('', $conf);
307  $this->translations->setDefaultLang($lang);
308  $this->translations->load('dict');
309  }
310  if ($country->code) {
311  $key = 'Country'.$country->code;
312  $translation = $this->translations->trans($key);
313  if ($translation != $key) {
314  $country->label = html_entity_decode($translation);
315  }
316  }
317  }
318  }
319 
336  function getListOfEventTypes($sortfield = "code", $sortorder = 'ASC', $limit = 100, $page = 0, $type = '', $module = '', $sqlfilters = '')
337  {
338  $list = array();
339 
340  $sql = "SELECT id, code, type, libelle as label, module";
341  $sql.= " FROM ".MAIN_DB_PREFIX."c_actioncomm as t";
342  $sql.= " WHERE t.active = 1";
343  if ($type) $sql.=" AND t.type LIKE '%" . $this->db->escape($type) . "%'";
344  if ($module) $sql.=" AND t.module LIKE '%" . $this->db->escape($module) . "%'";
345  // Add sql filters
346  if ($sqlfilters)
347  {
348  if (! DolibarrApi::_checkFilters($sqlfilters))
349  {
350  throw new RestException(503, 'Error when validating parameter sqlfilters '.$sqlfilters);
351  }
352  $regexstring='\(([^:\'\(\)]+:[^:\'\(\)]+:[^:\(\)]+)\)';
353  $sql.=" AND (".preg_replace_callback('/'.$regexstring.'/', 'DolibarrApi::_forge_criteria_callback', $sqlfilters).")";
354  }
355 
356 
357  $sql.= $this->db->order($sortfield, $sortorder);
358 
359  if ($limit) {
360  if ($page < 0) {
361  $page = 0;
362  }
363  $offset = $limit * $page;
364 
365  $sql .= $this->db->plimit($limit, $offset);
366  }
367 
368  $result = $this->db->query($sql);
369 
370  if ($result) {
371  $num = $this->db->num_rows($result);
372  $min = min($num, ($limit <= 0 ? $num : $limit));
373  for ($i = 0; $i < $min; $i++) {
374  $list[] = $this->db->fetch_object($result);
375  }
376  } else {
377  throw new RestException(503, 'Error when retrieving list of events types : '.$this->db->lasterror());
378  }
379 
380  return $list;
381  }
382 
398  function getListOfCivility($sortfield = "code", $sortorder = 'ASC', $limit = 100, $page = 0, $module = '', $sqlfilters = '')
399  {
400  $list = array();
401 
402  $sql = "SELECT rowid, code, label, module";
403  $sql.= " FROM ".MAIN_DB_PREFIX."c_civility as t";
404  $sql.= " WHERE t.active = 1";
405  if ($module) $sql.=" AND t.module LIKE '%" . $this->db->escape($module) . "%'";
406  // Add sql filters
407  if ($sqlfilters)
408  {
409  if (! DolibarrApi::_checkFilters($sqlfilters))
410  {
411  throw new RestException(503, 'Error when validating parameter sqlfilters '.$sqlfilters);
412  }
413  $regexstring='\(([^:\'\(\)]+:[^:\'\(\)]+:[^:\(\)]+)\)';
414  $sql.=" AND (".preg_replace_callback('/'.$regexstring.'/', 'DolibarrApi::_forge_criteria_callback', $sqlfilters).")";
415  }
416 
417 
418  $sql.= $this->db->order($sortfield, $sortorder);
419 
420  if ($limit) {
421  if ($page < 0) {
422  $page = 0;
423  }
424  $offset = $limit * $page;
425 
426  $sql .= $this->db->plimit($limit, $offset);
427  }
428 
429  $result = $this->db->query($sql);
430 
431  if ($result) {
432  $num = $this->db->num_rows($result);
433  $min = min($num, ($limit <= 0 ? $num : $limit));
434  for ($i = 0; $i < $min; $i++) {
435  $list[] = $this->db->fetch_object($result);
436  }
437  } else {
438  throw new RestException(503, 'Error when retrieving list of civility : '.$this->db->lasterror());
439  }
440 
441  return $list;
442  }
443 
457  function getListOfExtrafields($sortfield = "t.pos", $sortorder = 'ASC', $type = '', $sqlfilters = '')
458  {
459  $list = array();
460 
461  if ($type == 'thirdparty') $type='societe';
462  if ($type == 'contact') $type='socpeople';
463 
464  $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";
465  $sql.= " FROM ".MAIN_DB_PREFIX."extrafields as t";
466  $sql.= " WHERE t.entity IN (".getEntity('extrafields').")";
467  if (! empty($type)) $sql.= " AND t.elementtype = '".$this->db->escape($type)."'";
468  // Add sql filters
469  if ($sqlfilters)
470  {
471  if (! DolibarrApi::_checkFilters($sqlfilters))
472  {
473  throw new RestException(503, 'Error when validating parameter sqlfilters '.$sqlfilters);
474  }
475  $regexstring='\(([^:\'\(\)]+:[^:\'\(\)]+:[^:\(\)]+)\)';
476  $sql.=" AND (".preg_replace_callback('/'.$regexstring.'/', 'DolibarrApi::_forge_criteria_callback', $sqlfilters).")";
477  }
478 
479  $sql.= $this->db->order($sortfield, $sortorder);
480 
481  $resql=$this->db->query($sql);
482  if ($resql)
483  {
484  if ($this->db->num_rows($resql))
485  {
486  while ($tab = $this->db->fetch_object($resql))
487  {
488  // New usage
489  $list[$tab->elementtype][$tab->name]['type']=$tab->type;
490  $list[$tab->elementtype][$tab->name]['label']=$tab->label;
491  $list[$tab->elementtype][$tab->name]['size']=$tab->size;
492  $list[$tab->elementtype][$tab->name]['elementtype']=$tab->elementtype;
493  $list[$tab->elementtype][$tab->name]['default']=$tab->fielddefault;
494  $list[$tab->elementtype][$tab->name]['computed']=$tab->fieldcomputed;
495  $list[$tab->elementtype][$tab->name]['unique']=$tab->fieldunique;
496  $list[$tab->elementtype][$tab->name]['required']=$tab->fieldrequired;
497  $list[$tab->elementtype][$tab->name]['param']=($tab->param ? unserialize($tab->param) : '');
498  $list[$tab->elementtype][$tab->name]['pos']=$tab->pos;
499  $list[$tab->elementtype][$tab->name]['alwayseditable']=$tab->alwayseditable;
500  $list[$tab->elementtype][$tab->name]['perms']=$tab->perms;
501  $list[$tab->elementtype][$tab->name]['list']=$tab->list;
502  }
503  }
504  }
505  else
506  {
507  throw new RestException(503, 'Error when retrieving list of extra fields : '.$this->db->lasterror());
508  }
509 
510  if (! count($list))
511  {
512  throw new RestException(404, 'No extrafield found');
513  }
514 
515  return $list;
516  }
517 
518 
535  function getListOfTowns($sortfield = "zip,town", $sortorder = 'ASC', $limit = 100, $page = 0, $zipcode = '', $town = '', $sqlfilters = '')
536  {
537  $list = array();
538 
539  $sql = "SELECT rowid AS id, zip, town, fk_county, fk_pays AS fk_country";
540  $sql.= " FROM ".MAIN_DB_PREFIX."c_ziptown as t";
541  $sql.= " WHERE t.active = 1";
542  if ($zipcode) $sql.=" AND t.zip LIKE '%" . $this->db->escape($zipcode) . "%'";
543  if ($town) $sql.=" AND t.town LIKE '%" . $this->db->escape($town) . "%'";
544  // Add sql filters
545  if ($sqlfilters)
546  {
547  if (! DolibarrApi::_checkFilters($sqlfilters))
548  {
549  throw new RestException(503, 'Error when validating parameter sqlfilters '.$sqlfilters);
550  }
551  $regexstring='\(([^:\'\(\)]+:[^:\'\(\)]+:[^:\(\)]+)\)';
552  $sql.=" AND (".preg_replace_callback('/'.$regexstring.'/', 'DolibarrApi::_forge_criteria_callback', $sqlfilters).")";
553  }
554 
555 
556  $sql.= $this->db->order($sortfield, $sortorder);
557 
558  if ($limit) {
559  if ($page < 0) {
560  $page = 0;
561  }
562  $offset = $limit * $page;
563 
564  $sql .= $this->db->plimit($limit, $offset);
565  }
566 
567  $result = $this->db->query($sql);
568 
569  if ($result) {
570  $num = $this->db->num_rows($result);
571  $min = min($num, ($limit <= 0 ? $num : $limit));
572  for ($i = 0; $i < $min; $i++) {
573  $list[] = $this->db->fetch_object($result);
574  }
575  } else {
576  throw new RestException(503, 'Error when retrieving list of towns : '.$this->db->lasterror());
577  }
578 
579  return $list;
580  }
581 
599  function getPaymentTerms($sortfield = "sortorder", $sortorder = 'ASC', $limit = 100, $page = 0, $active = 1, $sqlfilters = '')
600  {
601  $list = array();
602 
603  $sql = "SELECT rowid as id, code, sortorder, libelle as label, libelle_facture as descr, type_cdr, nbjour, decalage, module";
604  $sql.= " FROM ".MAIN_DB_PREFIX."c_payment_term as t";
605  $sql.= " WHERE t.entity IN (".getEntity('c_payment_term').")";
606  $sql.= " AND t.active = ".$active;
607  // Add sql filters
608  if ($sqlfilters)
609  {
610  if (! DolibarrApi::_checkFilters($sqlfilters))
611  {
612  throw new RestException(400, 'Error when validating parameter sqlfilters '.$sqlfilters);
613  }
614  $regexstring='\(([^:\'\(\)]+:[^:\'\(\)]+:[^:\(\)]+)\)';
615  $sql.=" AND (".preg_replace_callback('/'.$regexstring.'/', 'DolibarrApi::_forge_criteria_callback', $sqlfilters).")";
616  }
617 
618 
619  $sql.= $this->db->order($sortfield, $sortorder);
620 
621  if ($limit) {
622  if ($page < 0) {
623  $page = 0;
624  }
625  $offset = $limit * $page;
626 
627  $sql .= $this->db->plimit($limit, $offset);
628  }
629 
630  $result = $this->db->query($sql);
631 
632  if ($result) {
633  $num = $this->db->num_rows($result);
634  $min = min($num, ($limit <= 0 ? $num : $limit));
635  for ($i = 0; $i < $min; $i++) {
636  $list[] = $this->db->fetch_object($result);
637  }
638  } else {
639  throw new RestException(400, $this->db->lasterror());
640  }
641 
642  return $list;
643  }
644 
659  function getTicketsCategories($sortfield = "code", $sortorder = 'ASC', $limit = 100, $page = 0, $sqlfilters = '')
660  {
661  $list = array();
662 
663  $sql = "SELECT rowid, code, pos, label, use_default, description";
664  $sql.= " FROM ".MAIN_DB_PREFIX."c_ticket_category as t";
665  $sql.= " WHERE t.active = 1";
666  // Add sql filters
667  if ($sqlfilters)
668  {
669  if (! DolibarrApi::_checkFilters($sqlfilters))
670  {
671  throw new RestException(503, 'Error when validating parameter sqlfilters '.$sqlfilters);
672  }
673  $regexstring='\(([^:\'\(\)]+:[^:\'\(\)]+:[^:\(\)]+)\)';
674  $sql.=" AND (".preg_replace_callback('/'.$regexstring.'/', 'DolibarrApi::_forge_criteria_callback', $sqlfilters).")";
675  }
676 
677 
678  $sql.= $this->db->order($sortfield, $sortorder);
679 
680  if ($limit) {
681  if ($page < 0) {
682  $page = 0;
683  }
684  $offset = $limit * $page;
685 
686  $sql .= $this->db->plimit($limit, $offset);
687  }
688 
689  $result = $this->db->query($sql);
690 
691  if ($result) {
692  $num = $this->db->num_rows($result);
693  $min = min($num, ($limit <= 0 ? $num : $limit));
694  for ($i = 0; $i < $min; $i++) {
695  $list[] = $this->db->fetch_object($result);
696  }
697  } else {
698  throw new RestException(503, 'Error when retrieving list of ticket categories : '.$this->db->lasterror());
699  }
700 
701  return $list;
702  }
703 
718  function getTicketsSeverities($sortfield = "code", $sortorder = 'ASC', $limit = 100, $page = 0, $sqlfilters = '')
719  {
720  $list = array();
721 
722  $sql = "SELECT rowid, code, pos, label, use_default, color, description";
723  $sql.= " FROM ".MAIN_DB_PREFIX."c_ticket_severity as t";
724  $sql.= " WHERE t.active = 1";
725  // Add sql filters
726  if ($sqlfilters)
727  {
728  if (! DolibarrApi::_checkFilters($sqlfilters))
729  {
730  throw new RestException(503, 'Error when validating parameter sqlfilters '.$sqlfilters);
731  }
732  $regexstring='\(([^:\'\(\)]+:[^:\'\(\)]+:[^:\(\)]+)\)';
733  $sql.=" AND (".preg_replace_callback('/'.$regexstring.'/', 'DolibarrApi::_forge_criteria_callback', $sqlfilters).")";
734  }
735 
736 
737  $sql.= $this->db->order($sortfield, $sortorder);
738 
739  if ($limit) {
740  if ($page < 0) {
741  $page = 0;
742  }
743  $offset = $limit * $page;
744 
745  $sql .= $this->db->plimit($limit, $offset);
746  }
747 
748  $result = $this->db->query($sql);
749 
750  if ($result) {
751  $num = $this->db->num_rows($result);
752  $min = min($num, ($limit <= 0 ? $num : $limit));
753  for ($i = 0; $i < $min; $i++) {
754  $list[] = $this->db->fetch_object($result);
755  }
756  } else {
757  throw new RestException(503, 'Error when retrieving list of ticket severities : '.$this->db->lasterror());
758  }
759 
760  return $list;
761  }
762 
777  function getTicketsTypes($sortfield = "code", $sortorder = 'ASC', $limit = 100, $page = 0, $sqlfilters = '')
778  {
779  $list = array();
780 
781  $sql = "SELECT rowid, code, pos, label, use_default, description";
782  $sql.= " FROM ".MAIN_DB_PREFIX."c_ticket_type as t";
783  $sql.= " WHERE t.active = 1";
784  if ($type) $sql.=" AND t.type LIKE '%" . $this->db->escape($type) . "%'";
785  if ($module) $sql.=" AND t.module LIKE '%" . $this->db->escape($module) . "%'";
786  // Add sql filters
787  if ($sqlfilters)
788  {
789  if (! DolibarrApi::_checkFilters($sqlfilters))
790  {
791  throw new RestException(503, 'Error when validating parameter sqlfilters '.$sqlfilters);
792  }
793  $regexstring='\(([^:\'\(\)]+:[^:\'\(\)]+:[^:\(\)]+)\)';
794  $sql.=" AND (".preg_replace_callback('/'.$regexstring.'/', 'DolibarrApi::_forge_criteria_callback', $sqlfilters).")";
795  }
796 
797 
798  $sql.= $this->db->order($sortfield, $sortorder);
799 
800  if ($limit) {
801  if ($page < 0) {
802  $page = 0;
803  }
804  $offset = $limit * $page;
805 
806  $sql .= $this->db->plimit($limit, $offset);
807  }
808 
809  $result = $this->db->query($sql);
810 
811  if ($result) {
812  $num = $this->db->num_rows($result);
813  $min = min($num, ($limit <= 0 ? $num : $limit));
814  for ($i = 0; $i < $min; $i++) {
815  $list[] = $this->db->fetch_object($result);
816  }
817  } else {
818  throw new RestException(503, 'Error when retrieving list of ticket types : '.$this->db->lasterror());
819  }
820 
821  return $list;
822  }
823 
824 
835  function getCheckIntegrity($target)
836  {
837  global $langs, $conf;
838 
839  if (! DolibarrApiAccess::$user->admin
840  && (empty($conf->global->API_LOGIN_ALLOWED_FOR_INTEGRITY_CHECK) || DolibarrApiAccess::$user->login != $conf->global->API_LOGIN_ALLOWED_FOR_INTEGRITY_CHECK))
841  {
842  throw new RestException(503, 'Error API open to admin users only or to login user defined with constant API_LOGIN_ALLOWED_FOR_INTEGRITY_CHECK');
843  }
844 
845  require_once DOL_DOCUMENT_ROOT.'/core/lib/files.lib.php';
846  require_once DOL_DOCUMENT_ROOT.'/core/lib/geturl.lib.php';
847 
848  $langs->load("admin");
849 
850  $outexpectedchecksum = '';
851  $outcurrentchecksum = '';
852 
853  // Modified or missing files
854  $file_list = array('missing' => array(), 'updated' => array());
855 
856  // Local file to compare to
857  $xmlshortfile = GETPOST('xmlshortfile')?GETPOST('xmlshortfile'):'/install/filelist-'.DOL_VERSION.'.xml';
858  $xmlfile = DOL_DOCUMENT_ROOT.$xmlshortfile;
859  // Remote file to compare to
860  $xmlremote = ($target == 'default' ? '' : $target);
861  if (empty($xmlremote) && ! empty($conf->global->MAIN_FILECHECK_URL)) $xmlremote = $conf->global->MAIN_FILECHECK_URL;
862  $param='MAIN_FILECHECK_URL_'.DOL_VERSION;
863  if (empty($xmlremote) && ! empty($conf->global->$param)) $xmlremote = $conf->global->$param;
864  if (empty($xmlremote)) $xmlremote = 'https://www.dolibarr.org/files/stable/signatures/filelist-'.DOL_VERSION.'.xml';
865 
866  if ($target == 'local')
867  {
868  if (dol_is_file($xmlfile))
869  {
870  $xml = simplexml_load_file($xmlfile);
871  }
872  else
873  {
874  throw new RestException(500, $langs->trans('XmlNotFound') . ': ' . $xmlfile);
875  }
876  }
877  else
878  {
879  $xmlarray = getURLContent($xmlremote);
880 
881  // Return array('content'=>response,'curl_error_no'=>errno,'curl_error_msg'=>errmsg...)
882  if (! $xmlarray['curl_error_no'] && $xmlarray['http_code'] != '404')
883  {
884  $xmlfile = $xmlarray['content'];
885  //print "xmlfilestart".$xmlfile."endxmlfile";
886  $xml = simplexml_load_string($xmlfile);
887  }
888  else
889  {
890  $errormsg=$langs->trans('XmlNotFound') . ': ' . $xmlremote.' - '.$xmlarray['http_code'].' '.$xmlarray['curl_error_no'].' '.$xmlarray['curl_error_msg'];
891  throw new RestException(500, $errormsg);
892  }
893  }
894 
895 
896 
897  if ($xml)
898  {
899  $checksumconcat = array();
900  $file_list = array();
901  $out = '';
902 
903  // Forced constants
904  if (is_object($xml->dolibarr_constants[0]))
905  {
906  $out.=load_fiche_titre($langs->trans("ForcedConstants"));
907 
908  $out.='<div class="div-table-responsive-no-min">';
909  $out.='<table class="noborder">';
910  $out.='<tr class="liste_titre">';
911  $out.='<td>#</td>';
912  $out.='<td>' . $langs->trans("Constant") . '</td>';
913  $out.='<td align="center">' . $langs->trans("ExpectedValue") . '</td>';
914  $out.='<td align="center">' . $langs->trans("Value") . '</td>';
915  $out.='</tr>'."\n";
916 
917  $i = 0;
918  foreach ($xml->dolibarr_constants[0]->constant as $constant) // $constant is a simpleXMLElement
919  {
920  $constname=$constant['name'];
921  $constvalue=(string) $constant;
922  $constvalue = (empty($constvalue)?'0':$constvalue);
923  // Value found
924  $value='';
925  if ($constname && $conf->global->$constname != '') $value=$conf->global->$constname;
926  $valueforchecksum=(empty($value)?'0':$value);
927 
928  $checksumconcat[]=$valueforchecksum;
929 
930  $i++;
931  $out.='<tr class="oddeven">';
932  $out.='<td>'.$i.'</td>' . "\n";
933  $out.='<td>'.$constname.'</td>' . "\n";
934  $out.='<td align="center">'.$constvalue.'</td>' . "\n";
935  $out.='<td align="center">'.$valueforchecksum.'</td>' . "\n";
936  $out.="</tr>\n";
937  }
938 
939  if ($i==0)
940  {
941  $out.='<tr class="oddeven"><td colspan="4" class="opacitymedium">'.$langs->trans("None").'</td></tr>';
942  }
943  $out.='</table>';
944  $out.='</div>';
945 
946  $out.='<br>';
947  }
948 
949  // Scan htdocs
950  if (is_object($xml->dolibarr_htdocs_dir[0]))
951  {
952  //var_dump($xml->dolibarr_htdocs_dir[0]['includecustom']);exit;
953  $includecustom=(empty($xml->dolibarr_htdocs_dir[0]['includecustom'])?0:$xml->dolibarr_htdocs_dir[0]['includecustom']);
954 
955  // Defined qualified files (must be same than into generate_filelist_xml.php)
956  $regextoinclude='\.(php|css|html|js|json|tpl|jpg|png|gif|sql|lang)$';
957  $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
958  $scanfiles = dol_dir_list(DOL_DOCUMENT_ROOT, 'files', 1, $regextoinclude, $regextoexclude);
959 
960  // Fill file_list with files in signature, new files, modified files
961  $ret = getFilesUpdated($file_list, $xml->dolibarr_htdocs_dir[0], '', DOL_DOCUMENT_ROOT, $checksumconcat, $scanfiles); // Fill array $file_list
962  // Complete with list of new files
963  foreach ($scanfiles as $keyfile => $valfile)
964  {
965  $tmprelativefilename=preg_replace('/^'.preg_quote(DOL_DOCUMENT_ROOT,'/').'/','', $valfile['fullname']);
966  if (! in_array($tmprelativefilename, $file_list['insignature']))
967  {
968  $md5newfile=@md5_file($valfile['fullname']); // Can fails if we don't have permission to open/read file
969  $file_list['added'][]=array('filename'=>$tmprelativefilename, 'md5'=>$md5newfile);
970  }
971  }
972 
973  // Files missings
974  $out.=load_fiche_titre($langs->trans("FilesMissing"));
975 
976  $out.='<div class="div-table-responsive-no-min">';
977  $out.='<table class="noborder">';
978  $out.='<tr class="liste_titre">';
979  $out.='<td>#</td>';
980  $out.='<td>' . $langs->trans("Filename") . '</td>';
981  $out.='<td align="center">' . $langs->trans("ExpectedChecksum") . '</td>';
982  $out.='</tr>'."\n";
983  $tmpfilelist = dol_sort_array($file_list['missing'], 'filename');
984  if (is_array($tmpfilelist) && count($tmpfilelist))
985  {
986  $i = 0;
987  foreach ($tmpfilelist as $file)
988  {
989  $i++;
990  $out.='<tr class="oddeven">';
991  $out.='<td>'.$i.'</td>' . "\n";
992  $out.='<td>'.$file['filename'].'</td>' . "\n";
993  $out.='<td align="center">'.$file['expectedmd5'].'</td>' . "\n";
994  $out.="</tr>\n";
995  }
996  }
997  else
998  {
999  $out.='<tr class="oddeven"><td colspan="3" class="opacitymedium">'.$langs->trans("None").'</td></tr>';
1000  }
1001  $out.='</table>';
1002  $out.='</div>';
1003 
1004  $out.='<br>';
1005 
1006  // Files modified
1007  $out.=load_fiche_titre($langs->trans("FilesModified"));
1008 
1009  $totalsize=0;
1010  $out.='<div class="div-table-responsive-no-min">';
1011  $out.='<table class="noborder">';
1012  $out.='<tr class="liste_titre">';
1013  $out.='<td>#</td>';
1014  $out.='<td>' . $langs->trans("Filename") . '</td>';
1015  $out.='<td align="center">' . $langs->trans("ExpectedChecksum") . '</td>';
1016  $out.='<td align="center">' . $langs->trans("CurrentChecksum") . '</td>';
1017  $out.='<td align="right">' . $langs->trans("Size") . '</td>';
1018  $out.='<td align="right">' . $langs->trans("DateModification") . '</td>';
1019  $out.='</tr>'."\n";
1020  $tmpfilelist2 = dol_sort_array($file_list['updated'], 'filename');
1021  if (is_array($tmpfilelist2) && count($tmpfilelist2))
1022  {
1023  $i = 0;
1024  foreach ($tmpfilelist2 as $file)
1025  {
1026  $i++;
1027  $out.='<tr class="oddeven">';
1028  $out.='<td>'.$i.'</td>' . "\n";
1029  $out.='<td>'.$file['filename'].'</td>' . "\n";
1030  $out.='<td align="center">'.$file['expectedmd5'].'</td>' . "\n";
1031  $out.='<td align="center">'.$file['md5'].'</td>' . "\n";
1032  $size = dol_filesize(DOL_DOCUMENT_ROOT.'/'.$file['filename']);
1033  $totalsize += $size;
1034  $out.='<td align="right">'.dol_print_size($size).'</td>' . "\n";
1035  $out.='<td align="right">'.dol_print_date(dol_filemtime(DOL_DOCUMENT_ROOT.'/'.$file['filename']),'dayhour').'</td>' . "\n";
1036  $out.="</tr>\n";
1037  }
1038  $out.='<tr class="liste_total">';
1039  $out.='<td></td>' . "\n";
1040  $out.='<td>'.$langs->trans("Total").'</td>' . "\n";
1041  $out.='<td align="center"></td>' . "\n";
1042  $out.='<td align="center"></td>' . "\n";
1043  $out.='<td align="right">'.dol_print_size($totalsize).'</td>' . "\n";
1044  $out.='<td align="right"></td>' . "\n";
1045  $out.="</tr>\n";
1046  }
1047  else
1048  {
1049  $out.='<tr class="oddeven"><td colspan="5" class="opacitymedium">'.$langs->trans("None").'</td></tr>';
1050  }
1051  $out.='</table>';
1052  $out.='</div>';
1053 
1054  $out.='<br>';
1055 
1056  // Files added
1057  $out.=load_fiche_titre($langs->trans("FilesAdded"));
1058 
1059  $totalsize = 0;
1060  $out.='<div class="div-table-responsive-no-min">';
1061  $out.='<table class="noborder">';
1062  $out.='<tr class="liste_titre">';
1063  $out.='<td>#</td>';
1064  $out.='<td>' . $langs->trans("Filename") . '</td>';
1065  $out.='<td align="center">' . $langs->trans("ExpectedChecksum") . '</td>';
1066  $out.='<td align="center">' . $langs->trans("CurrentChecksum") . '</td>';
1067  $out.='<td align="right">' . $langs->trans("Size") . '</td>';
1068  $out.='<td align="right">' . $langs->trans("DateModification") . '</td>';
1069  $out.='</tr>'."\n";
1070  $tmpfilelist3 = dol_sort_array($file_list['added'], 'filename');
1071  if (is_array($tmpfilelist3) && count($tmpfilelist3))
1072  {
1073  $i = 0;
1074  foreach ($tmpfilelist3 as $file)
1075  {
1076  $i++;
1077  $out.='<tr class="oddeven">';
1078  $out.='<td>'.$i.'</td>' . "\n";
1079  $out.='<td>'.$file['filename'].'</td>' . "\n";
1080  $out.='<td align="center">'.$file['expectedmd5'].'</td>' . "\n";
1081  $out.='<td align="center">'.$file['md5'].'</td>' . "\n";
1082  $size = dol_filesize(DOL_DOCUMENT_ROOT.'/'.$file['filename']);
1083  $totalsize += $size;
1084  $out.='<td align="right">'.dol_print_size($size).'</td>' . "\n";
1085  $out.='<td align="right">'.dol_print_date(dol_filemtime(DOL_DOCUMENT_ROOT.'/'.$file['filename']),'dayhour').'</td>' . "\n";
1086  $out.="</tr>\n";
1087  }
1088  $out.='<tr class="liste_total">';
1089  $out.='<td></td>' . "\n";
1090  $out.='<td>'.$langs->trans("Total").'</td>' . "\n";
1091  $out.='<td align="center"></td>' . "\n";
1092  $out.='<td align="center"></td>' . "\n";
1093  $out.='<td align="right">'.dol_print_size($totalsize).'</td>' . "\n";
1094  $out.='<td align="right"></td>' . "\n";
1095  $out.="</tr>\n";
1096  }
1097  else
1098  {
1099  $out.='<tr class="oddeven"><td colspan="5" class="opacitymedium">'.$langs->trans("None").'</td></tr>';
1100  }
1101  $out.='</table>';
1102  $out.='</div>';
1103 
1104 
1105  // Show warning
1106  if (empty($tmpfilelist) && empty($tmpfilelist2) && empty($tmpfilelist3))
1107  {
1108  //setEventMessages($langs->trans("FileIntegrityIsStrictlyConformedWithReference"), null, 'mesgs');
1109  }
1110  else
1111  {
1112  //setEventMessages($langs->trans("FileIntegritySomeFilesWereRemovedOrModified"), null, 'warnings');
1113  }
1114  }
1115  else
1116  {
1117  throw new RestException(500, 'Error: Failed to found dolibarr_htdocs_dir into XML file '.$xmlfile);
1118  }
1119 
1120 
1121  // Scan scripts
1122 
1123 
1124  asort($checksumconcat); // Sort list of checksum
1125  //var_dump($checksumconcat);
1126  $checksumget = md5(join(',',$checksumconcat));
1127  $checksumtoget = trim((string) $xml->dolibarr_htdocs_dir_checksum);
1128 
1129  $outexpectedchecksum = ($checksumtoget ? $checksumtoget : $langs->trans("Unknown"));
1130  if ($checksumget == $checksumtoget)
1131  {
1132  if (count($file_list['added']))
1133  {
1134  $resultcode = 'warning';
1135  $resultcomment='FileIntegrityIsOkButFilesWereAdded';
1136  //$outcurrentchecksum = $checksumget.' - <span class="'.$resultcode.'">'.$langs->trans("FileIntegrityIsOkButFilesWereAdded").'</span>';
1137  $outcurrentchecksum = $checksumget;
1138  }
1139  else
1140  {
1141  $resultcode = 'ok';
1142  $resultcomment='Success';
1143  //$outcurrentchecksum = '<span class="'.$resultcode.'">'.$checksumget.'</span>';
1144  $outcurrentchecksum = $checksumget;
1145  }
1146  }
1147  else
1148  {
1149  $resultcode = 'error';
1150  $resultcomment='Error';
1151  //$outcurrentchecksum = '<span class="'.$resultcode.'">'.$checksumget.'</span>';
1152  $outcurrentchecksum = $checksumget;
1153  }
1154  }
1155  else {
1156  throw new RestException(404, 'No signature file known');
1157  }
1158 
1159  return array('resultcode'=>$resultcode, 'resultcomment'=>$resultcomment, 'expectedchecksum'=> $outexpectedchecksum, 'currentchecksum'=> $outcurrentchecksum, 'out'=>$out);
1160  }
1161 }
getPaymentTypes($sortfield="code", $sortorder='ASC', $limit=100, $page=0, $active=1, $sqlfilters='')
Get the list of payments types.
load_fiche_titre($titre, $morehtmlright='', $picto='title_generic.png', $pictoisfullpath=0, $id='', $morecssontable='', $morehtmlcenter='')
Load a title with picto.
GETPOST($paramname, $check='none', $method=0, $filter=null, $options=null, $noreplace=0)
Return value of a param into GET or POST supervariable.
if(! empty($conf->facture->enabled) && $user->rights->facture->lire) if(! empty($conf->fournisseur->enabled) && $user->rights->fournisseur->facture->lire) if(! empty($conf->don->enabled) && $user->rights->societe->lire) if(! empty($conf->tax->enabled) && $user->rights->tax->charges->lire) if(! empty($conf->facture->enabled) &&! empty($conf->commande->enabled) && $user->rights->commande->lire &&empty($conf->global->WORKFLOW_DISABLE_CREATE_INVOICE_FROM_ORDER)) if(! empty($conf->facture->enabled) && $user->rights->facture->lire) if(! empty($conf->fournisseur->enabled) && $user->rights->fournisseur->facture->lire) $resql
Social contributions to pay.
Definition: index.php:1053
getCheckIntegrity($target)
Do a test of integrity for files and setup.
getPaymentTerms($sortfield="sortorder", $sortorder='ASC', $limit=100, $page=0, $active=1, $sqlfilters='')
Get the list of payments terms.
dol_sort_array(&$array, $index, $order='asc', $natsort=0, $case_sensitive=0, $keepindex=0)
Advanced sort array by second index function, which produces ascending (default) or descending output...
getListOfExtrafields($sortfield="t.pos", $sortorder='ASC', $type='', $sqlfilters='')
Get the list of extra fields.
getListOfCountries($sortfield="code", $sortorder='ASC', $limit=100, $page=0, $filter='', $lang='', $sqlfilters='')
Get the list of countries.
getTicketsSeverities($sortfield="code", $sortorder='ASC', $limit=100, $page=0, $sqlfilters='')
Get the list of tickets severity.
dol_filesize($pathoffile)
Return size of a file.
Definition: files.lib.php:552
getListOfTowns($sortfield="zip,town", $sortorder='ASC', $limit=100, $page=0, $zipcode='', $town='', $sqlfilters='')
Get the list of towns.
Class to manage dictionary Countries (used by imports)
__construct()
Constructor.
_checkFilters($sqlfilters)
Return if a $sqlfilters parameter is valid.
Definition: api.class.php:251
Class for API REST v1.
Definition: api.class.php:29
getTicketsTypes($sortfield="code", $sortorder='ASC', $limit=100, $page=0, $sqlfilters='')
Get the list of tickets types.
getURLContent($url, $postorget='GET', $param='', $followlocation=1, $addheaders=array())
Function get content from an URL (use proxy if proxy defined)
Definition: geturl.lib.php:34
getCountryByID($id, $lang='')
Get country by ID.
translateLabel($country, $lang)
Translate the name of the country to the given language.
Class to manage translations.
if(GETPOST('cancel', 'alpha')) if(! GETPOST( 'confirmmassaction', 'alpha') &&$massaction !='presend' &&$massaction !='confirm_presend')
Draft customers invoices.
Definition: list.php:156
dol_dir_list($path, $types="all", $recursive=0, $filter="", $excludefilter=null, $sortcriteria="name", $sortorder=SORT_ASC, $mode=0, $nohook=0, $relativename="", $donotfollowsymlinks=0)
Scan a directory and return a list of files/directories.
Definition: files.lib.php:59
dol_is_file($pathoffile)
Return if path is a file.
Definition: files.lib.php:451
getAvailability($sortfield="code", $sortorder='ASC', $limit=100, $page=0, $active=1, $sqlfilters='')
Get the list of delivery times.
dol_filemtime($pathoffile)
Return time of a file.
Definition: files.lib.php:564
getFilesUpdated(&$file_list, SimpleXMLElement $dir, $path='', $pathref='', &$checksumconcat=array())
Function to get list of updated or modified files.
Definition: files.lib.php:2922
_cleanObjectDatas($object)
Clean sensible object datas.
getListOfCivility($sortfield="code", $sortorder='ASC', $limit=100, $page=0, $module='', $sqlfilters='')
Get the list of civility.
getListOfEventTypes($sortfield="code", $sortorder='ASC', $limit=100, $page=0, $type='', $module='', $sqlfilters='')
Get the list of events types.
getTicketsCategories($sortfield="code", $sortorder='ASC', $limit=100, $page=0, $sqlfilters='')
Get the list of tickets categories.