multicompany.lib.php 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532
  1. <?php
  2. /* Copyright (C) 2011-2022 Regis Houssin <regis.houssin@inodbox.com>
  3. *
  4. * This program is free software; you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License as published by
  6. * the Free Software Foundation; either version 3 of the License, or
  7. * (at your option) any later version.
  8. *
  9. * This program is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program; if not, write to the Free Software
  16. * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  17. * or see http://www.gnu.org/
  18. */
  19. /**
  20. * \file /multicompany/lib/multicompany.lib.php
  21. * \brief Ensemble de fonctions de base pour le module Multi-Company
  22. * \ingroup multicompany
  23. */
  24. function multicompany_prepare_head()
  25. {
  26. global $langs, $conf;
  27. $langs->load('multicompany@multicompany');
  28. $h = 0;
  29. $head = array();
  30. $head[$h][0] = dol_buildpath("/multicompany/admin/multicompany.php",1);
  31. $head[$h][1] = $langs->trans("Entities");
  32. $head[$h][2] = 'entities';
  33. $h++;
  34. $head[$h][0] = dol_buildpath("/multicompany/admin/options.php",1);
  35. $head[$h][1] = $langs->trans("Options");
  36. $head[$h][2] = 'options';
  37. $h++;
  38. if (!empty($conf->global->MULTICOMPANY_SHARING_BYELEMENT_ENABLED)) {
  39. $head[$h][0] = dol_buildpath("/multicompany/admin/granularity.php",1);
  40. $head[$h][1] = $langs->trans("SharingsByElement");
  41. $head[$h][2] = 'granularity';
  42. $h++;
  43. }
  44. $head[$h][0] = dol_buildpath("/multicompany/admin/caches.php",1);
  45. $head[$h][1] = $langs->trans("Caches");
  46. $head[$h][2] = 'caches';
  47. $h++;
  48. $head[$h][0] = dol_buildpath("/multicompany/admin/multicompany_extrafields.php", 1);
  49. $head[$h][1] = $langs->trans("ExtraFields");
  50. $head[$h][2] = 'attributes';
  51. $h ++;
  52. // Show more tabs from modules
  53. // Entries must be declared in modules descriptor with line
  54. // $this->tabs = array('entity:+tabname:Title:@mymodule:/mymodule/mypage.php?id=__ID__'); to add new tab
  55. // $this->tabs = array('entity:-tabname:Title:@mymodule:/mymodule/mypage.php?id=__ID__'); to remove a tab
  56. complete_head_from_modules($conf,$langs,null,$head,$h,'multicompany');
  57. $head[$h][0] = dol_buildpath("/multicompany/admin/about.php",1);
  58. $head[$h][1] = $langs->trans("About");
  59. $head[$h][2] = 'about';
  60. $h++;
  61. return $head;
  62. }
  63. /**
  64. * Prepare array with list of tabs
  65. *
  66. * @param Object $object Object related to tabs
  67. * @param array $aEntities Entities array
  68. * @return array Array of tabs
  69. */
  70. function entity_prepare_head($object, $aEntities)
  71. {
  72. global $conf, $langs, $user, $mc;
  73. $head = array();
  74. $i = 0;
  75. foreach($aEntities as $entity)
  76. {
  77. $mc->getInfo($entity);
  78. if (empty($conf->global->MULTICOMPANY_TEMPLATE_MANAGEMENT) && $mc->visible == 2) continue;
  79. if ($object->element == 'user' && $mc->visible == 2) continue;
  80. if ($mc->visible == 2 && (empty($user->admin) || !empty($user->entity))) continue; // Only visible by superadmin
  81. $head[$entity][0] = $_SERVER['PHP_SELF'].'?id='.$object->id.'&amp;entity='.$entity;
  82. if ($mc->visible == 2) {
  83. $head[$entity][1] = '<span id="template_' . $object->id . '" class="fas fa-clone multicompany-button-template" title="'.$langs->trans("TemplateOfEntity").'"></span>'.$mc->label;
  84. } else {
  85. $head[$entity][1] = '<span id="template_' . $object->id . '" class="fas fa-globe multicompany-button-template" title="'.$langs->trans("Entity").'"></span>'.$mc->label;
  86. }
  87. $head[$entity][2] = $entity;
  88. $i++;
  89. }
  90. if (!empty($conf->global->MULTICOMPANY_TEMPLATE_MANAGEMENT) && $object->element == 'usergroup' && !empty($user->admin) && empty($user->entity)) { // Only visible by superadmin
  91. $i = $i + $entity;
  92. $head[$i][0] = '';
  93. $head[$i][1] = '<span id="clonerights" class="fas fa-sitemap multicompany-button-clonerights" title="'.$langs->trans("DuplicateRights").'"></span>';
  94. $head[$i][2] = 'image';
  95. }
  96. return $head;
  97. }
  98. /**
  99. *
  100. */
  101. function getTablesWithField($searchfield, $exclude=false, $include=false)
  102. {
  103. global $db;
  104. $out=array();
  105. $tables = $db->DDLListTables($db->database_name);
  106. if (is_array($tables))
  107. {
  108. // Pour chaque table : vérif si présence d'un champ
  109. foreach($tables as $table)
  110. {
  111. if (is_array($exclude))
  112. {
  113. if (! in_array($table, $exclude, true))
  114. {
  115. $datas = $db->DDLInfoTable($table);
  116. foreach ($datas as $key => $fields)
  117. {
  118. if (is_array($fields) && in_array($searchfield, $fields))
  119. {
  120. $out[] = $table;
  121. }
  122. }
  123. }
  124. }
  125. else if (!empty($exclude))
  126. {
  127. if (! preg_match($exclude, $table))
  128. {
  129. $datas = $db->DDLInfoTable($table);
  130. foreach ($datas as $key => $fields)
  131. {
  132. if (is_array($fields) && in_array($searchfield, $fields))
  133. {
  134. $out[] = $table;
  135. }
  136. }
  137. }
  138. }
  139. else if (!empty($include))
  140. {
  141. if (preg_match($include, $table))
  142. {
  143. $datas = $db->DDLInfoTable($table);
  144. foreach ($datas as $key => $fields)
  145. {
  146. if (is_array($fields) && in_array($searchfield, $fields))
  147. {
  148. $out[] = $table;
  149. }
  150. }
  151. }
  152. }
  153. }
  154. }
  155. return $out;
  156. }
  157. /**
  158. *
  159. */
  160. function getSqlInitFilePath($name)
  161. {
  162. return dol_buildpath("/multicompany/sql/init/llx_" . basename($name) . ".sql");
  163. }
  164. function isSharingAllByDefault($element)
  165. {
  166. global $conf;
  167. if (empty($conf->global->MULTICOMPANY_SHARING_BYELEMENT_ENABLED)) {
  168. return false;
  169. }
  170. $sharingbyelementname = 'MULTICOMPANY_'.strtoupper($element).'_SHARING_BYELEMENT_ENABLED';
  171. $shareallbydefaultname = 'MULTICOMPANY_'.strtoupper($element).'_SHARE_ALL_BY_DEFAULT';
  172. if (!empty($conf->global->$sharingbyelementname) && !empty($conf->global->$shareallbydefaultname)) {
  173. return true;
  174. }
  175. return false;
  176. }
  177. /**
  178. *
  179. * @param unknown $url
  180. * @param unknown $param
  181. * @return mixed
  182. */
  183. function removeParam($url, $param) {
  184. $url = preg_replace('/(&|\?)'.preg_quote($param).'=[^&]*$/', '', $url);
  185. $url = preg_replace('/(&|\?)'.preg_quote($param).'=[^&]*&/', '$1', $url);
  186. return $url;
  187. }
  188. /**
  189. * Get a value from the store
  190. *
  191. * @param string $key Data Key
  192. *
  193. * @return string|null
  194. */
  195. function getCache($key)
  196. {
  197. global $conf, $sessionname;
  198. $key = $sessionname . '_' . date("Y-m-d-H") . '_' . $key; // 1 hour validity
  199. if (!empty($conf->global->MULTICOMPANY_MEMCACHED_ENABLED))
  200. {
  201. $serveraddress = (!empty($conf->global->MULTICOMPANY_MEMCACHED_SERVER)?$conf->global->MULTICOMPANY_MEMCACHED_SERVER:(!empty($conf->global->MEMCACHED_SERVER)?$conf->global->MEMCACHED_SERVER:'127.0.0.1:11211'));
  202. $tmparray = explode(':',$serveraddress);
  203. $host = $tmparray[0];
  204. $port = (!empty($tmparray[1])?$tmparray[1]:11211);
  205. // Using a memcached server
  206. if (class_exists('Memcached'))
  207. {
  208. $m = new Memcached();
  209. $result = $m->addServer($host, $port);
  210. $data = $m->get($key);
  211. $rescode = $m->getResultCode();
  212. if ($rescode == 0) {
  213. return $data;
  214. }
  215. }
  216. else if (class_exists('Memcache'))
  217. {
  218. $m = new Memcache();
  219. $result = $m->addServer($host, $port);
  220. $data = $m->get($key);
  221. if ($data) {
  222. return $data;
  223. }
  224. }
  225. }
  226. else if (!empty($conf->global->MULTICOMPANY_SHMOP_ENABLED))
  227. {
  228. if (function_exists("shmop_read"))
  229. {
  230. $shmkey = base_convert(hash("crc32b", $key), 16, 10);
  231. $handle = @shmop_open($shmkey,'a',0,0);
  232. if ($handle)
  233. {
  234. $my_string = trim(@shmop_read($handle,0,0));
  235. if (!empty($conf->global->MULTICOMPANY_SHMOP_MEMCOMPRESS_LEVEL) && function_exists('gzuncompress')){
  236. $my_string = @gzuncompress($my_string);
  237. }
  238. if ($my_string)
  239. {
  240. @shmop_close($handle);
  241. return unserialize(base64_decode($my_string));
  242. }
  243. }
  244. }
  245. }
  246. else if (!empty($conf->global->MULTICOMPANY_SESSION_ENABLED))
  247. {
  248. if (isset($_SESSION[$key])) {
  249. return unserialize(base64_decode($_SESSION[$key]));
  250. }
  251. }
  252. return false;
  253. }
  254. /**
  255. * Set a value in the store
  256. * @param string $key Data Key
  257. * @param string $value Data Value
  258. *
  259. * @return void
  260. */
  261. function setCache($key, $value)
  262. {
  263. global $conf, $sessionname;
  264. $key = $sessionname . '_' . date("Y-m-d-H") . '_' . $key; // 1 hour validity
  265. if (!empty($conf->global->MULTICOMPANY_MEMCACHED_ENABLED))
  266. {
  267. $serveraddress = (!empty($conf->global->MULTICOMPANY_MEMCACHED_SERVER)?$conf->global->MULTICOMPANY_MEMCACHED_SERVER:(!empty($conf->global->MEMCACHED_SERVER)?$conf->global->MEMCACHED_SERVER:'127.0.0.1:11211'));
  268. $tmparray = explode(':',$serveraddress);
  269. $host = $tmparray[0];
  270. $port = (!empty($tmparray[1])?$tmparray[1]:11211);
  271. // Using a memcached server
  272. if (class_exists('Memcached'))
  273. {
  274. $m = new Memcached();
  275. $result = $m->addServer($host, $port);
  276. $m->set($key, $value);
  277. $rescode = $m->getResultCode();
  278. if ($rescode == 0) {
  279. return true;
  280. }
  281. }
  282. else if (class_exists('Memcache'))
  283. {
  284. $m = new Memcache();
  285. $result = $m->addServer($host, $port);
  286. $result = $m->set($key, $value);
  287. if ($result) {
  288. return true;
  289. }
  290. }
  291. }
  292. else if (!empty($conf->global->MULTICOMPANY_SHMOP_ENABLED))
  293. {
  294. if (function_exists("shmop_write"))
  295. {
  296. $shmkey = base_convert(hash("crc32b", $key), 16, 10);
  297. $fdata = base64_encode(serialize($value));
  298. if (!empty($conf->global->MULTICOMPANY_SHMOP_MEMCOMPRESS_LEVEL) && function_exists('gzcompress')){
  299. $fdata = @gzcompress($fdata, (int) $conf->global->MULTICOMPANY_SHMOP_MEMCOMPRESS_LEVEL);
  300. }
  301. $fsize = strlen($fdata);
  302. $handle = @shmop_open($shmkey,'c',0644,$fsize);
  303. if ($handle)
  304. {
  305. $shm_bytes_written = @shmop_write($handle, $fdata, 0);
  306. if ($shm_bytes_written == $fsize)
  307. {
  308. @shmop_close($handle);
  309. return true;
  310. }
  311. }
  312. }
  313. }
  314. else if (!empty($conf->global->MULTICOMPANY_SESSION_ENABLED))
  315. {
  316. $_SESSION[$key] = base64_encode(serialize($value));
  317. return true;
  318. }
  319. return false;
  320. }
  321. /**
  322. * Clear the key from the store
  323. *
  324. * @param $key Data Key
  325. *
  326. * @return void
  327. */
  328. function clearCache($key)
  329. {
  330. global $conf, $sessionname;
  331. $key = $sessionname . '_' . date("Y-m-d-H") . '_' . $key; // 1 hour validity
  332. if (!empty($conf->global->MULTICOMPANY_MEMCACHED_ENABLED))
  333. {
  334. $serveraddress = (!empty($conf->global->MULTICOMPANY_MEMCACHED_SERVER)?$conf->global->MULTICOMPANY_MEMCACHED_SERVER:(!empty($conf->global->MEMCACHED_SERVER)?$conf->global->MEMCACHED_SERVER:'127.0.0.1:11211'));
  335. $tmparray = explode(':',$serveraddress);
  336. $host = $tmparray[0];
  337. $port = (!empty($tmparray[1])?$tmparray[1]:11211);
  338. // Using a memcached server
  339. if (class_exists('Memcached'))
  340. {
  341. $m = new Memcached();
  342. $result = $m->addServer($host, $port);
  343. $m->delete($key);
  344. $rescode = $m->getResultCode();
  345. if ($rescode == 0) {
  346. return true;
  347. }
  348. }
  349. else if (class_exists('Memcache'))
  350. {
  351. $m = new Memcache();
  352. $result = $m->addServer($host, $port);
  353. $result = $m->delete($key);
  354. if ($result) {
  355. return true;
  356. }
  357. }
  358. }
  359. else if (!empty($conf->global->MULTICOMPANY_SHMOP_ENABLED))
  360. {
  361. if (function_exists("shmop_delete"))
  362. {
  363. $shmkey = base_convert(hash("crc32b", $key), 16, 10);
  364. $handle = @shmop_open($shmkey,'a',0,0);
  365. if ($handle)
  366. {
  367. if (!@shmop_delete($handle))
  368. {
  369. @shmop_close($handle);
  370. return false;
  371. }
  372. else
  373. {
  374. @shmop_close($handle);
  375. return true;
  376. }
  377. }
  378. }
  379. }
  380. else if (!empty($conf->global->MULTICOMPANY_SESSION_ENABLED))
  381. {
  382. if (isset($_SESSION[$key])) {
  383. unset($_SESSION[$key]);
  384. return true;
  385. }
  386. }
  387. return false;
  388. }
  389. /**
  390. * On/off button for constant
  391. *
  392. * @param string $code Name of constant
  393. * @param array $input Array of type->list of CSS element to switch. Example: array('disabled'=>array(0=>'cssid'))
  394. * @param int $entity Entity to set
  395. * @param int $revertonoff Revert on/off
  396. * @return void
  397. */
  398. function ajax_mcconstantonoff($code, $input = array(), $entity = null, $revertonoff = 0, $strict = 0, $forcereload = 0, $marginleftonlyshort = 2, $forcenoajax = 0)
  399. {
  400. global $conf, $langs, $user;
  401. $entity = ((isset($entity) && is_numeric($entity) && $entity >= 0) ? $entity : $conf->entity);
  402. $out= "\n<!-- Ajax code to switch constant ".$code." -->".'
  403. <script type="text/javascript">
  404. $(document).ready(function() {
  405. var input = '.json_encode($input).';
  406. var url = \''.DOL_URL_ROOT.'/core/ajax/constantonoff.php\';
  407. var code = \''.$code.'\';
  408. var entity = \''.$entity.'\';
  409. var strict = \''.$strict.'\';
  410. var userid = \''.$user->id.'\';
  411. var yesButton = "'.dol_escape_js($langs->transnoentities("Yes")).'";
  412. var noButton = "'.dol_escape_js($langs->transnoentities("No")).'";
  413. var token = \''.currentToken().'\';
  414. // Set constant
  415. $("#set_" + code).click(function() {
  416. if (input.alert && input.alert.set) {
  417. if (input.alert.set.yesButton) yesButton = input.alert.set.yesButton;
  418. if (input.alert.set.noButton) noButton = input.alert.set.noButton;
  419. confirmMulticompanyConstantAction("set", url, code, input, input.alert.set, entity, yesButton, noButton, strict, userid, token);
  420. } else {
  421. setMulticompanyConstant(url, code, input, entity, 0, '.$forcereload.', userid, token);
  422. }
  423. });
  424. // Del constant
  425. $("#del_" + code).click(function() {
  426. if (input.alert && input.alert.del) {
  427. if (input.alert.del.yesButton) yesButton = input.alert.del.yesButton;
  428. if (input.alert.del.noButton) noButton = input.alert.del.noButton;
  429. confirmMulticompanyConstantAction("del", url, code, input, input.alert.del, entity, yesButton, noButton, strict, userid, token);
  430. } else {
  431. delMulticompanyConstant(url, code, input, entity, 0, '.$forcereload.', userid, token);
  432. }
  433. });
  434. });
  435. </script>'."\n";
  436. $out.= '<div id="confirm_'.$code.'" title="" style="display: none;"></div>';
  437. $out.= '<span id="set_'.$code.'" class="linkobject '.(!empty($conf->global->$code)?'hideobject':'').'">'.($revertonoff?img_picto($langs->trans("Enabled"),'switch_on', '', false, 0, 0, '', '', $marginleftonlyshort):img_picto($langs->trans("Disabled"),'switch_off', '', false, 0, 0, '', '', $marginleftonlyshort)).'</span>';
  438. $out.= '<span id="del_'.$code.'" class="linkobject '.(!empty($conf->global->$code)?'':'hideobject').'">'.($revertonoff?img_picto($langs->trans("Disabled"),'switch_off', '', false, 0, 0, '', '', $marginleftonlyshort):img_picto($langs->trans("Enabled"),'switch_on', '', false, 0, 0, '', '', $marginleftonlyshort)).'</span>';
  439. $out.="\n";
  440. return $out;
  441. }
  442. /**
  443. * Check multicompany version
  444. *
  445. * @return int 0 = OK
  446. * 1 = Multicompany is older than Dolibarr
  447. * -1 = Dolibarr is older than MultiCompany
  448. * -2 = Multicompany need upgrade (disable/enable module)
  449. */
  450. function checkMultiCompanyVersion()
  451. {
  452. global $conf;
  453. $out = 0;
  454. $dolversion = explode('.', DOL_VERSION);
  455. if (empty($conf->global->MULTICOMPANY_MAIN_VERSION) || (version_compare($dolversion[0], '15', '=') && version_compare($conf->global->MULTICOMPANY_MAIN_VERSION, '15.0.3', '<'))) {
  456. $out = -2;
  457. } else {
  458. $out = version_compare($dolversion[0], '15');
  459. }
  460. return $out;
  461. }
  462. /**
  463. * Check mc authentication
  464. */
  465. function checkMulticompanyAutentication()
  466. {
  467. global $conf;
  468. if (isset($conf->file->main_authentication) && preg_match('/^mc$/',$conf->file->main_authentication)) return true;
  469. else return false;
  470. }