DolibarrModules.class.php 72 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456
  1. <?php
  2. /* Copyright (C) 2003-2007 Rodolphe Quiedeville <rodolphe@quiedeville.org>
  3. * Copyright (C) 2004 Sebastien Di Cintio <sdicintio@ressource-toi.org>
  4. * Copyright (C) 2004 Benoit Mortier <benoit.mortier@opensides.be>
  5. * Copyright (C) 2004 Eric Seigne <eric.seigne@ryxeo.com>
  6. * Copyright (C) 2005-2013 Laurent Destailleur <eldy@users.sourceforge.net>
  7. * Copyright (C) 2005-2012 Regis Houssin <regis.houssin@inodbox.com>
  8. * Copyright (C) 2014 Raphaël Doursenaud <rdoursenaud@gpcsolutions.fr>
  9. * Copyright (C) 2018 Josep Lluís Amador <joseplluis@lliuretic.cat>
  10. * Copyright (C) 2019 Frédéric France <frederic.france@netlogic.fr>
  11. *
  12. * This program is free software; you can redistribute it and/or modify
  13. * it under the terms of the GNU General Public License as published by
  14. * the Free Software Foundation; either version 3 of the License, or
  15. * (at your option) any later version.
  16. *
  17. * This program is distributed in the hope that it will be useful,
  18. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  19. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  20. * GNU General Public License for more details.
  21. *
  22. * You should have received a copy of the GNU General Public License
  23. * along with this program. If not, see <https://www.gnu.org/licenses/>.
  24. */
  25. /**
  26. * \file htdocs/core/modules/DolibarrModules.class.php
  27. * \brief File of parent class of module descriptor class files
  28. */
  29. /**
  30. * Class DolibarrModules
  31. *
  32. * Parent class for module descriptor class files
  33. */
  34. class DolibarrModules // Can not be abstract, because we need to instantiate it into unActivateModule to be able to disable a module whose files were removed.
  35. {
  36. /**
  37. * @var DoliDb Database handler
  38. */
  39. public $db;
  40. /**
  41. * @var int Module unique ID
  42. * @see https://wiki.dolibarr.org/index.php/List_of_modules_id
  43. */
  44. public $numero;
  45. /**
  46. * @var string Publisher name
  47. * @since 4.0.0
  48. */
  49. public $editor_name;
  50. /**
  51. * @var string URL of module at publisher site
  52. * @since 4.0.0
  53. */
  54. public $editor_url;
  55. /**
  56. * @var string Family
  57. * @see $familyinfo
  58. *
  59. * Native values: 'crm', 'financial', 'hr', 'projects', 'products', 'ecm', 'technic', 'other'.
  60. * Use familyinfo to declare a custom value.
  61. */
  62. public $family;
  63. /**
  64. * @var array Custom family informations
  65. * @see $family
  66. *
  67. * e.g.:
  68. * array(
  69. * 'myownfamily' => array(
  70. * 'position' => '001',
  71. * 'label' => $langs->trans("MyOwnFamily")
  72. * )
  73. * );
  74. */
  75. public $familyinfo;
  76. /**
  77. * @var string Module position on 2 digits
  78. */
  79. public $module_position = '50';
  80. /**
  81. * @var string Module name
  82. *
  83. * Only used if Module[ID]Name translation string is not found.
  84. *
  85. * You can use the following code to automatically derive it from your module's class name:
  86. * preg_replace('/^mod/i', '', get_class($this))
  87. */
  88. public $name;
  89. /**
  90. * @var string[] Paths to create when module is activated
  91. *
  92. * e.g.: array('/mymodule/temp')
  93. */
  94. public $dirs = array();
  95. /**
  96. * @var array Module boxes
  97. */
  98. public $boxes = array();
  99. /**
  100. * @var array Module constants
  101. */
  102. public $const = array();
  103. /**
  104. * @var array Module cron jobs entries
  105. */
  106. public $cronjobs = array();
  107. /**
  108. * @var array Module access rights
  109. */
  110. public $rights;
  111. /**
  112. * @var string Module access rights family
  113. */
  114. public $rights_class;
  115. /**
  116. * @var array|int Module menu entries (1 means the menu entries are not declared into module descriptor but are hardcoded into menu manager)
  117. */
  118. public $menu = array();
  119. /**
  120. * @var array Module parts
  121. * array(
  122. * // Set this to 1 if module has its own trigger directory (/mymodule/core/triggers)
  123. * 'triggers' => 0,
  124. * // Set this to 1 if module has its own login method directory (/mymodule/core/login)
  125. * 'login' => 0,
  126. * // Set this to 1 if module has its own substitution function file (/mymodule/core/substitutions)
  127. * 'substitutions' => 0,
  128. * // Set this to 1 if module has its own menus handler directory (/mymodule/core/menus)
  129. * 'menus' => 0,
  130. * // Set this to 1 if module has its own theme directory (/mymodule/theme)
  131. * 'theme' => 0,
  132. * // Set this to 1 if module overwrite template dir (/mymodule/core/tpl)
  133. * 'tpl' => 0,
  134. * // Set this to 1 if module has its own barcode directory (/mymodule/core/modules/barcode)
  135. * 'barcode' => 0,
  136. * // Set this to 1 if module has its own models directory (/mymodule/core/modules/xxx)
  137. * 'models' => 0,
  138. * // Set this to relative path of css file if module has its own css file
  139. * 'css' => '/mymodule/css/mymodule.css.php',
  140. * // Set this to relative path of js file if module must load a js on all pages
  141. * 'js' => '/mymodule/js/mymodule.js',
  142. * // Set here all hooks context managed by module
  143. * 'hooks' => array('hookcontext1','hookcontext2')
  144. * )
  145. */
  146. public $module_parts = array();
  147. /**
  148. * @var string Module documents ?
  149. * @deprecated Seems unused anywhere
  150. */
  151. public $docs;
  152. /**
  153. * @var string ?
  154. * @deprecated Seems unused anywhere
  155. */
  156. public $dbversion = "-";
  157. /**
  158. * @var string Error message
  159. */
  160. public $error;
  161. /**
  162. * @var string Module version
  163. * @see http://semver.org
  164. *
  165. * The following keywords can also be used:
  166. * 'development'
  167. * 'experimental'
  168. * 'dolibarr': only for core modules that share its version
  169. * 'dolibarr_deprecated': only for deprecated core modules
  170. */
  171. public $version;
  172. /**
  173. * Module last version
  174. * @var string $lastVersion
  175. */
  176. public $lastVersion = '';
  177. /**
  178. * true indicate this module need update
  179. * @var bool $needUpdate
  180. */
  181. public $needUpdate = false;
  182. /**
  183. * @var string Module description (short text)
  184. *
  185. * Only used if Module[ID]Desc translation string is not found.
  186. */
  187. public $description;
  188. /**
  189. * @var string Module description (long text)
  190. * @since 4.0.0
  191. *
  192. * HTML content supported.
  193. */
  194. public $descriptionlong;
  195. // For exports
  196. /**
  197. * @var string Module export code
  198. */
  199. public $export_code;
  200. /**
  201. * @var string Module export label
  202. */
  203. public $export_label;
  204. public $export_permission;
  205. public $export_fields_array;
  206. public $export_TypeFields_array; // Array of key=>type where type can be 'Numeric', 'Date', 'Text', 'Boolean', 'Status', 'List:xxx:login:rowid'
  207. public $export_entities_array;
  208. public $export_special_array; // special or computed field
  209. public $export_dependencies_array;
  210. public $export_sql_start;
  211. public $export_sql_end;
  212. public $export_sql_order;
  213. // For import
  214. /**
  215. * @var string Module import code
  216. */
  217. public $import_code;
  218. /**
  219. * @var string Module import label
  220. */
  221. public $import_label;
  222. /**
  223. * @var string Module constant name
  224. */
  225. public $const_name;
  226. /**
  227. * @var bool Module can't be disabled
  228. */
  229. public $always_enabled;
  230. /**
  231. * @var bool Module is disabled
  232. */
  233. public $disabled;
  234. /**
  235. * @var int Module is enabled globally (Multicompany support)
  236. */
  237. public $core_enabled;
  238. /**
  239. * @var string Name of image file used for this module
  240. *
  241. * If file is in theme/yourtheme/img directory under name object_pictoname.png use 'pictoname'
  242. * If file is in module/img directory under name object_pictoname.png use 'pictoname@module'
  243. */
  244. public $picto;
  245. /**
  246. * @var string[] List of config pages
  247. *
  248. * Name of php pages stored into module/admin directory, used to setup module.
  249. * e.g.: "admin.php@module"
  250. */
  251. public $config_page_url;
  252. /**
  253. * @var string[] List of module class names that must be enabled if this module is enabled. e.g.: array('modAnotherModule', 'FR'=>'modYetAnotherModule')
  254. * @see $requiredby
  255. */
  256. public $depends;
  257. /**
  258. * @var string[] List of module class names to disable if the module is disabled.
  259. * @see $depends
  260. */
  261. public $requiredby;
  262. /**
  263. * @var string[] List of module class names as string this module is in conflict with.
  264. * @see $depends
  265. */
  266. public $conflictwith;
  267. /**
  268. * @var string[] Module language files
  269. */
  270. public $langfiles;
  271. /**
  272. * @var array<string,string> Array of warnings to show when we activate the module
  273. *
  274. * array('always'='text') or array('FR'='text')
  275. */
  276. public $warnings_activation;
  277. /**
  278. * @var array<string,string> Array of warnings to show when we activate an external module
  279. *
  280. * array('always'='text') or array('FR'='text')
  281. */
  282. public $warnings_activation_ext;
  283. /**
  284. * @var array Minimum version of PHP required by module.
  285. * e.g.: PHP ≥ 5.6 = array(5, 6)
  286. */
  287. public $phpmin;
  288. /**
  289. * @var array Minimum version of Dolibarr required by module.
  290. * e.g.: Dolibarr ≥ 3.6 = array(3, 6)
  291. */
  292. public $need_dolibarr_version;
  293. /**
  294. * @var bool Whether to hide the module.
  295. */
  296. public $hidden = false;
  297. /**
  298. * @var string url to check for module update
  299. */
  300. public $url_last_version;
  301. /**
  302. * Constructor. Define names, constants, directories, boxes, permissions
  303. *
  304. * @param DoliDB $db Database handler
  305. */
  306. public function __construct($db)
  307. {
  308. $this->db = $db;
  309. }
  310. // We should but can't set this as abstract because this will make dolibarr hang
  311. // after migration due to old module not implementing. We must wait PHP is able to make
  312. // a try catch on Fatal error to manage this correctly.
  313. // We need constructor into function unActivateModule into admin.lib.php
  314. // phpcs:disable PEAR.NamingConventions.ValidFunctionName.PublicUnderscore
  315. /**
  316. * Enables a module.
  317. * Inserts all informations into database.
  318. *
  319. * @param array $array_sql SQL requests to be executed when enabling module
  320. * @param string $options String with options when disabling module:
  321. * - 'noboxes' = Do all actions but do not insert boxes
  322. * - 'newboxdefonly' = Do all actions but for boxes, insert def of boxes only and not boxes activation
  323. * @return int 1 if OK, 0 if KO
  324. */
  325. protected function _init($array_sql, $options = '')
  326. {
  327. // phpcs:enable
  328. global $conf;
  329. $err = 0;
  330. $this->db->begin();
  331. // Insert activation module constant
  332. if (!$err) {
  333. $err += $this->_active();
  334. }
  335. // Insert new pages for tabs (into llx_const)
  336. if (!$err) {
  337. $err += $this->insert_tabs();
  338. }
  339. // Insert activation of module's parts
  340. if (!$err) {
  341. $err += $this->insert_module_parts();
  342. }
  343. // Insert constant defined by modules (into llx_const)
  344. if (!$err && !preg_match('/newboxdefonly/', $options)) {
  345. $err += $this->insert_const(); // Test on newboxdefonly to avoid to erase value during upgrade
  346. }
  347. // Insert boxes def (into llx_boxes_def) and boxes setup (into llx_boxes)
  348. if (!$err && !preg_match('/noboxes/', $options)) {
  349. $err += $this->insert_boxes($options);
  350. }
  351. // Insert cron job entries (entry in llx_cronjobs)
  352. if (!$err) {
  353. $err += $this->insert_cronjobs();
  354. }
  355. // Insert permission definitions of module into llx_rights_def. If user is admin, grant this permission to user.
  356. if (!$err) {
  357. $err += $this->insert_permissions(1, null, 1);
  358. }
  359. // Insert specific menus entries into database
  360. if (!$err) {
  361. $err += $this->insert_menus();
  362. }
  363. // Create module's directories
  364. if (!$err) {
  365. $err += $this->create_dirs();
  366. }
  367. // Execute addons requests
  368. $num = count($array_sql);
  369. for ($i = 0; $i < $num; $i++) {
  370. if (!$err) {
  371. $val = $array_sql[$i];
  372. $sql = $val;
  373. $ignoreerror = 0;
  374. if (is_array($val)) {
  375. $sql = $val['sql'];
  376. $ignoreerror = $val['ignoreerror'];
  377. }
  378. // Add current entity id
  379. $sql = str_replace('__ENTITY__', $conf->entity, $sql);
  380. dol_syslog(get_class($this)."::_init ignoreerror=".$ignoreerror."", LOG_DEBUG);
  381. $result = $this->db->query($sql, $ignoreerror);
  382. if (!$result) {
  383. if (!$ignoreerror) {
  384. $this->error = $this->db->lasterror();
  385. $err++;
  386. } else {
  387. dol_syslog(get_class($this)."::_init Warning ".$this->db->lasterror(), LOG_WARNING);
  388. }
  389. }
  390. }
  391. }
  392. // Return code
  393. if (!$err) {
  394. $this->db->commit();
  395. return 1;
  396. } else {
  397. $this->db->rollback();
  398. return 0;
  399. }
  400. }
  401. // phpcs:disable PEAR.NamingConventions.ValidFunctionName.PublicUnderscore
  402. /**
  403. * Disable function. Deletes the module constants and boxes from the database.
  404. *
  405. * @param string[] $array_sql SQL requests to be executed when module is disabled
  406. * @param string $options Options when disabling module:
  407. *
  408. * @return int 1 if OK, 0 if KO
  409. */
  410. protected function _remove($array_sql, $options = '')
  411. {
  412. // phpcs:enable
  413. $err = 0;
  414. $this->db->begin();
  415. // Remove activation module line (constant MAIN_MODULE_MYMODULE in llx_const)
  416. if (!$err) {
  417. $err += $this->_unactive();
  418. }
  419. // Remove activation of module's new tabs (MAIN_MODULE_MYMODULE_TABS_XXX in llx_const)
  420. if (!$err) {
  421. $err += $this->delete_tabs();
  422. }
  423. // Remove activation of module's parts (MAIN_MODULE_MYMODULE_XXX in llx_const)
  424. if (!$err) {
  425. $err += $this->delete_module_parts();
  426. }
  427. // Remove constants defined by modules
  428. if (!$err) {
  429. $err += $this->delete_const();
  430. }
  431. // Remove list of module's available boxes (entry in llx_boxes)
  432. if (!$err && !preg_match('/(newboxdefonly|noboxes)/', $options)) {
  433. $err += $this->delete_boxes(); // We don't have to delete if option ask to keep boxes safe or ask to add new box def only
  434. }
  435. // Remove list of module's cron job entries (entry in llx_cronjobs)
  436. if (!$err) {
  437. $err += $this->delete_cronjobs();
  438. }
  439. // Remove module's permissions from list of available permissions (entries in llx_rights_def)
  440. if (!$err) {
  441. $err += $this->delete_permissions();
  442. }
  443. // Remove module's menus (entries in llx_menu)
  444. if (!$err) {
  445. $err += $this->delete_menus();
  446. }
  447. // Remove module's directories
  448. if (!$err) {
  449. $err += $this->delete_dirs();
  450. }
  451. // Run complementary sql requests
  452. $num = count($array_sql);
  453. for ($i = 0; $i < $num; $i++) {
  454. if (!$err) {
  455. dol_syslog(get_class($this)."::_remove", LOG_DEBUG);
  456. $result = $this->db->query($array_sql[$i]);
  457. if (!$result) {
  458. $this->error = $this->db->error();
  459. $err++;
  460. }
  461. }
  462. }
  463. // Return code
  464. if (!$err) {
  465. $this->db->commit();
  466. return 1;
  467. } else {
  468. $this->db->rollback();
  469. return 0;
  470. }
  471. }
  472. /**
  473. * Gives the translated module name if translation exists in admin.lang or into language files of module.
  474. * Otherwise return the module key name.
  475. *
  476. * @return string Translated module name
  477. */
  478. public function getName()
  479. {
  480. global $langs;
  481. $langs->load("admin");
  482. if ($langs->transnoentitiesnoconv("Module".$this->numero."Name") != ("Module".$this->numero."Name")) {
  483. // If module name translation exists
  484. return $langs->transnoentitiesnoconv("Module".$this->numero."Name");
  485. } else {
  486. // If module name translation using it's unique id does not exist, we try to use its name to find translation
  487. if (is_array($this->langfiles)) {
  488. foreach ($this->langfiles as $val) {
  489. if ($val) {
  490. $langs->load($val);
  491. }
  492. }
  493. }
  494. if ($langs->trans("Module".$this->name."Name") != ("Module".$this->name."Name")) {
  495. // If module name translation exists
  496. return $langs->transnoentitiesnoconv("Module".$this->name."Name");
  497. }
  498. // Last chance with simple label
  499. return $langs->transnoentitiesnoconv($this->name);
  500. }
  501. }
  502. /**
  503. * Gives the translated module description if translation exists in admin.lang or the default module description
  504. *
  505. * @return string Translated module description
  506. */
  507. public function getDesc()
  508. {
  509. global $langs;
  510. $langs->load("admin");
  511. if ($langs->transnoentitiesnoconv("Module".$this->numero."Desc") != ("Module".$this->numero."Desc")) {
  512. // If module description translation exists
  513. return $langs->transnoentitiesnoconv("Module".$this->numero."Desc");
  514. } else {
  515. // If module description translation does not exist using its unique id, we can use its name to find translation
  516. if (is_array($this->langfiles)) {
  517. foreach ($this->langfiles as $val) {
  518. if ($val) {
  519. $langs->load($val);
  520. }
  521. }
  522. }
  523. if ($langs->transnoentitiesnoconv("Module".$this->name."Desc") != ("Module".$this->name."Desc")) {
  524. // If module name translation exists
  525. return $langs->trans("Module".$this->name."Desc");
  526. }
  527. // Last chance with simple label
  528. return $langs->trans($this->description);
  529. }
  530. }
  531. /**
  532. * Gives the long description of a module. First check README-la_LA.md then README.md
  533. * If no markdown files found, it returns translated value of the key ->descriptionlong.
  534. *
  535. * @return string Long description of a module from README.md of from property.
  536. */
  537. public function getDescLong()
  538. {
  539. global $langs;
  540. $langs->load("admin");
  541. include_once DOL_DOCUMENT_ROOT.'/core/lib/files.lib.php';
  542. include_once DOL_DOCUMENT_ROOT.'/core/lib/geturl.lib.php';
  543. $content='';
  544. $pathoffile = $this->getDescLongReadmeFound();
  545. if ($pathoffile) { // Mostly for external modules
  546. $content = file_get_contents($pathoffile);
  547. if ((float) DOL_VERSION >= 6.0) {
  548. @include_once DOL_DOCUMENT_ROOT.'/core/lib/parsemd.lib.php';
  549. $content = dolMd2Html(
  550. $content,
  551. 'parsedown',
  552. array(
  553. 'doc/' => dol_buildpath(strtolower($this->name).'/doc/', 1),
  554. 'img/' => dol_buildpath(strtolower($this->name).'/img/', 1),
  555. 'images/' => dol_buildpath(strtolower($this->name).'/images/', 1),
  556. )
  557. );
  558. } else {
  559. $content = nl2br($content);
  560. }
  561. } else {
  562. // Mostly for internal modules
  563. if (!empty($this->descriptionlong)) {
  564. if (is_array($this->langfiles)) {
  565. foreach ($this->langfiles as $val) {
  566. if ($val) {
  567. $langs->load($val);
  568. }
  569. }
  570. }
  571. $content = $langs->transnoentitiesnoconv($this->descriptionlong);
  572. }
  573. }
  574. return $content;
  575. }
  576. /**
  577. * Return path of file if a README file was found.
  578. *
  579. * @return string Path of file if a README file was found.
  580. */
  581. public function getDescLongReadmeFound()
  582. {
  583. global $langs;
  584. $filefound = false;
  585. // Define path to file README.md.
  586. // First check README-la_LA.md then README-la.md then README.md
  587. $pathoffile = dol_buildpath(strtolower($this->name).'/README-'.$langs->defaultlang.'.md', 0);
  588. if (dol_is_file($pathoffile)) {
  589. $filefound = true;
  590. }
  591. if (!$filefound) {
  592. $tmp = explode('_', $langs->defaultlang);
  593. $pathoffile = dol_buildpath(strtolower($this->name).'/README-'.$tmp[0].'.md', 0);
  594. if (dol_is_file($pathoffile)) {
  595. $filefound = true;
  596. }
  597. }
  598. if (!$filefound) {
  599. $pathoffile = dol_buildpath(strtolower($this->name).'/README.md', 0);
  600. if (dol_is_file($pathoffile)) {
  601. $filefound = true;
  602. }
  603. }
  604. return ($filefound ? $pathoffile : '');
  605. }
  606. /**
  607. * Gives the changelog. First check ChangeLog-la_LA.md then ChangeLog.md
  608. *
  609. * @return string Content of ChangeLog
  610. */
  611. public function getChangeLog()
  612. {
  613. global $langs;
  614. $langs->load("admin");
  615. include_once DOL_DOCUMENT_ROOT.'/core/lib/files.lib.php';
  616. include_once DOL_DOCUMENT_ROOT.'/core/lib/geturl.lib.php';
  617. $filefound = false;
  618. // Define path to file README.md.
  619. // First check ChangeLog-la_LA.md then ChangeLog.md
  620. $pathoffile = dol_buildpath(strtolower($this->name).'/ChangeLog-'.$langs->defaultlang.'.md', 0);
  621. if (dol_is_file($pathoffile)) {
  622. $filefound = true;
  623. }
  624. if (!$filefound) {
  625. $pathoffile = dol_buildpath(strtolower($this->name).'/ChangeLog.md', 0);
  626. if (dol_is_file($pathoffile)) {
  627. $filefound = true;
  628. }
  629. }
  630. if ($filefound) { // Mostly for external modules
  631. $content = file_get_contents($pathoffile);
  632. if ((float) DOL_VERSION >= 6.0) {
  633. @include_once DOL_DOCUMENT_ROOT.'/core/lib/parsemd.lib.php';
  634. $content = dolMd2Html($content, 'parsedown', array('doc/'=>dol_buildpath(strtolower($this->name).'/doc/', 1)));
  635. } else {
  636. $content = nl2br($content);
  637. }
  638. }
  639. return $content;
  640. }
  641. /**
  642. * Gives the publisher name
  643. *
  644. * @return string Publisher name
  645. */
  646. public function getPublisher()
  647. {
  648. return $this->editor_name;
  649. }
  650. /**
  651. * Gives the publisher url
  652. *
  653. * @return string Publisher url
  654. */
  655. public function getPublisherUrl()
  656. {
  657. return $this->editor_url;
  658. }
  659. /**
  660. * Gives module version (translated if param $translated is on)
  661. * For 'experimental' modules, gives 'experimental' translation
  662. * For 'dolibarr' modules, gives Dolibarr version
  663. *
  664. * @param int $translated 1=Special version keys are translated, 0=Special version keys are not translated
  665. * @return string Module version
  666. */
  667. public function getVersion($translated = 1)
  668. {
  669. global $langs;
  670. $langs->load("admin");
  671. $ret = '';
  672. $newversion = preg_replace('/_deprecated/', '', $this->version);
  673. if ($newversion == 'experimental') {
  674. $ret = ($translated ? $langs->transnoentitiesnoconv("VersionExperimental") : $newversion);
  675. } elseif ($newversion == 'development') {
  676. $ret = ($translated ? $langs->transnoentitiesnoconv("VersionDevelopment") : $newversion);
  677. } elseif ($newversion == 'dolibarr') {
  678. $ret = DOL_VERSION;
  679. } elseif ($newversion) {
  680. $ret = $newversion;
  681. } else {
  682. $ret = ($translated ? $langs->transnoentitiesnoconv("VersionUnknown") : 'unknown');
  683. }
  684. if (preg_match('/_deprecated/', $this->version)) {
  685. $ret .= ($translated ? ' ('.$langs->transnoentitiesnoconv("Deprecated").')' : $this->version);
  686. }
  687. return $ret;
  688. }
  689. /**
  690. * Gives the module position
  691. *
  692. * @return int Module position (an external module should never return a value lower than 100000. 1-100000 are reserved for core)
  693. */
  694. public function getModulePosition()
  695. {
  696. if (in_array($this->version, array('dolibarr', 'experimental', 'development'))) { // core module
  697. return $this->module_position;
  698. } else { // external module
  699. if ($this->module_position >= 100000) {
  700. return $this->module_position;
  701. } else {
  702. return $this->module_position + 100000;
  703. }
  704. }
  705. }
  706. /**
  707. * Tells if module is core or external.
  708. * 'dolibarr' and 'dolibarr_deprecated' is core
  709. * 'experimental' and 'development' is core
  710. *
  711. * @return string 'core', 'external' or 'unknown'
  712. */
  713. public function isCoreOrExternalModule()
  714. {
  715. if ($this->version == 'dolibarr' || $this->version == 'dolibarr_deprecated') {
  716. return 'core';
  717. }
  718. if (!empty($this->version) && !in_array($this->version, array('experimental', 'development'))) {
  719. return 'external';
  720. }
  721. if (!empty($this->editor_name) || !empty($this->editor_url)) {
  722. return 'external';
  723. }
  724. if ($this->numero >= 100000) {
  725. return 'external';
  726. }
  727. return 'unknown';
  728. }
  729. /**
  730. * Gives module related language files list
  731. *
  732. * @return string[] Language files list
  733. */
  734. public function getLangFilesArray()
  735. {
  736. return $this->langfiles;
  737. }
  738. /**
  739. * Gives translated label of an export dataset
  740. *
  741. * @param int $r Dataset index
  742. *
  743. * @return string Translated databaset label
  744. */
  745. public function getExportDatasetLabel($r)
  746. {
  747. global $langs;
  748. $langstring = "ExportDataset_".$this->export_code[$r];
  749. if ($langs->trans($langstring) == $langstring) {
  750. // Translation not found
  751. return $langs->trans($this->export_label[$r]);
  752. } else {
  753. // Translation found
  754. return $langs->trans($langstring);
  755. }
  756. }
  757. /**
  758. * Gives translated label of an import dataset
  759. *
  760. * @param int $r Dataset index
  761. *
  762. * @return string Translated dataset label
  763. */
  764. public function getImportDatasetLabel($r)
  765. {
  766. global $langs;
  767. $langstring = "ImportDataset_".$this->import_code[$r];
  768. //print "x".$langstring;
  769. if ($langs->trans($langstring) == $langstring) {
  770. // Translation not found
  771. return $langs->transnoentitiesnoconv($this->import_label[$r]);
  772. } else {
  773. // Translation found
  774. return $langs->transnoentitiesnoconv($langstring);
  775. }
  776. }
  777. /**
  778. * Gives the last date of activation
  779. *
  780. * @return int|string Date of last activation or '' if module was never activated
  781. */
  782. public function getLastActivationDate()
  783. {
  784. global $conf;
  785. $err = 0;
  786. $sql = "SELECT tms FROM ".MAIN_DB_PREFIX."const";
  787. $sql .= " WHERE ".$this->db->decrypt('name')." = '".$this->db->escape($this->const_name)."'";
  788. $sql .= " AND entity IN (0, ".$conf->entity.")";
  789. dol_syslog(get_class($this)."::getLastActiveDate", LOG_DEBUG);
  790. $resql = $this->db->query($sql);
  791. if (!$resql) {
  792. $err++;
  793. } else {
  794. $obj = $this->db->fetch_object($resql);
  795. if ($obj) {
  796. return $this->db->jdate($obj->tms);
  797. }
  798. }
  799. return '';
  800. }
  801. /**
  802. * Gives the last author of activation
  803. *
  804. * @return array Array array('authorid'=>Id of last activation user, 'lastactivationdate'=>Date of last activation)
  805. */
  806. public function getLastActivationInfo()
  807. {
  808. global $conf;
  809. $err = 0;
  810. $sql = "SELECT tms, note FROM ".MAIN_DB_PREFIX."const";
  811. $sql .= " WHERE ".$this->db->decrypt('name')." = '".$this->db->escape($this->const_name)."'";
  812. $sql .= " AND entity IN (0, ".$conf->entity.")";
  813. dol_syslog(get_class($this)."::getLastActiveDate", LOG_DEBUG);
  814. $resql = $this->db->query($sql);
  815. if (!$resql) {
  816. $err++;
  817. } else {
  818. $obj = $this->db->fetch_object($resql);
  819. if ($obj) {
  820. $tmp = array();
  821. if ($obj->note) {
  822. $tmp = json_decode($obj->note, true);
  823. }
  824. return array(
  825. 'authorid' => $tmp['authorid'],
  826. 'ip' => $tmp['ip'],
  827. 'lastactivationdate' => $this->db->jdate($obj->tms),
  828. 'lastactivationversion' => (!empty($tmp['lastactivationversion']) ? $tmp['lastactivationversion'] : 'unknown'),
  829. );
  830. }
  831. }
  832. return array();
  833. }
  834. // phpcs:disable PEAR.NamingConventions.ValidFunctionName.PublicUnderscore
  835. /**
  836. * Insert constants for module activation
  837. *
  838. * @return int Error count (0 if OK)
  839. */
  840. protected function _active()
  841. {
  842. // phpcs:enable
  843. global $conf, $user;
  844. $err = 0;
  845. // Common module
  846. $entity = ((!empty($this->always_enabled) || !empty($this->core_enabled)) ? 0 : $conf->entity);
  847. $sql = "DELETE FROM ".MAIN_DB_PREFIX."const";
  848. $sql .= " WHERE ".$this->db->decrypt('name')." = '".$this->db->escape($this->const_name)."'";
  849. $sql .= " AND entity IN (0, ".$entity.")";
  850. dol_syslog(get_class($this)."::_active delete activation constant", LOG_DEBUG);
  851. $resql = $this->db->query($sql);
  852. if (!$resql) {
  853. $err++;
  854. }
  855. $note = json_encode(
  856. array(
  857. 'authorid' => (is_object($user) ? $user->id : 0),
  858. 'ip' => (empty($_SERVER['REMOTE_ADDR']) ? '' : $_SERVER['REMOTE_ADDR']),
  859. 'lastactivationversion' => $this->version,
  860. )
  861. );
  862. $sql = "INSERT INTO ".MAIN_DB_PREFIX."const (name, value, visible, entity, note) VALUES";
  863. $sql .= " (".$this->db->encrypt($this->const_name);
  864. $sql .= ", ".$this->db->encrypt('1');
  865. $sql .= ", 0, ".((int) $entity);
  866. $sql .= ", '".$this->db->escape($note)."')";
  867. dol_syslog(get_class($this)."::_active insert activation constant", LOG_DEBUG);
  868. $resql = $this->db->query($sql);
  869. if (!$resql) {
  870. $err++;
  871. }
  872. return $err;
  873. }
  874. // phpcs:disable PEAR.NamingConventions.ValidFunctionName.PublicUnderscore
  875. /**
  876. * Module deactivation
  877. *
  878. * @return int Error count (0 if OK)
  879. */
  880. protected function _unactive()
  881. {
  882. // phpcs:enable
  883. global $conf;
  884. $err = 0;
  885. // Common module
  886. $entity = ((!empty($this->always_enabled) || !empty($this->core_enabled)) ? 0 : $conf->entity);
  887. $sql = "DELETE FROM ".MAIN_DB_PREFIX."const";
  888. $sql .= " WHERE ".$this->db->decrypt('name')." = '".$this->db->escape($this->const_name)."'";
  889. $sql .= " AND entity IN (0, ".$entity.")";
  890. dol_syslog(get_class($this)."::_unactive", LOG_DEBUG);
  891. $this->db->query($sql);
  892. return $err;
  893. }
  894. // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps,PEAR.NamingConventions.ValidFunctionName.PublicUnderscore
  895. /**
  896. * Create tables and keys required by module:
  897. * - Files table.sql or table-module.sql with create table instructions
  898. * - Then table.key.sql or table-module.key.sql with create keys instructions
  899. * - Then data_xxx.sql (usualy provided by external modules only)
  900. * - Then update_xxx.sql (usualy provided by external modules only)
  901. * Files must be stored in subdirectory 'tables' or 'data' into directory $reldir (Example: '/install/mysql/' or '/module/sql/')
  902. * This function may also be called by :
  903. * - _load_tables('/install/mysql/', 'modulename') into the this->init() of core module descriptors.
  904. * - _load_tables('/mymodule/sql/') into the this->init() of external module descriptors.
  905. *
  906. * @param string $reldir Relative directory where to scan files. Example: '/install/mysql/' or '/module/sql/'
  907. * @param string $onlywithsuffix Only with the defined suffix
  908. * @return int <=0 if KO, >0 if OK
  909. */
  910. protected function _load_tables($reldir, $onlywithsuffix = '')
  911. {
  912. // phpcs:enable
  913. global $conf;
  914. $error = 0;
  915. $dirfound = 0;
  916. if (empty($reldir)) {
  917. return 1;
  918. }
  919. include_once DOL_DOCUMENT_ROOT.'/core/lib/admin.lib.php';
  920. $ok = 1;
  921. foreach ($conf->file->dol_document_root as $dirroot) {
  922. if ($ok) {
  923. $dirsql = $dirroot.$reldir;
  924. $ok = 0;
  925. // We will loop on xxx/, xxx/tables/, xxx/data/
  926. $listofsubdir = array('', 'tables/', 'data/');
  927. if ($this->db->type == 'pgsql') {
  928. $listofsubdir[] = '../pgsql/functions/';
  929. }
  930. foreach ($listofsubdir as $subdir) {
  931. $dir = $dirsql.$subdir;
  932. $handle = @opendir($dir); // Dir may not exists
  933. if (is_resource($handle)) {
  934. $dirfound++;
  935. // Run llx_mytable.sql files, then llx_mytable_*.sql
  936. $files = array();
  937. while (($file = readdir($handle)) !== false) {
  938. $files[] = $file;
  939. }
  940. sort($files);
  941. foreach ($files as $file) {
  942. if ($onlywithsuffix) {
  943. if (!preg_match('/\-'.preg_quote($onlywithsuffix, '/').'\./i', $file)) {
  944. //print 'File '.$file.' does not match suffix '.$onlywithsuffix.' so it is discarded<br>'."\n";
  945. continue;
  946. } else {
  947. //print 'File '.$file.' match suffix '.$onlywithsuffix.' so we keep it<br>'."\n";
  948. }
  949. }
  950. if (preg_match('/\.sql$/i', $file) && !preg_match('/\.key\.sql$/i', $file) && substr($file, 0, 4) == 'llx_') {
  951. $result = run_sql($dir.$file, empty($conf->global->MAIN_DISPLAY_SQL_INSTALL_LOG) ? 1 : 0, '', 1);
  952. if ($result <= 0) {
  953. $error++;
  954. }
  955. }
  956. }
  957. rewinddir($handle);
  958. // Run llx_mytable.key.sql files (Must be done after llx_mytable.sql) then then llx_mytable_*.key.sql
  959. $files = array();
  960. while (($file = readdir($handle)) !== false) {
  961. $files[] = $file;
  962. }
  963. sort($files);
  964. foreach ($files as $file) {
  965. if ($onlywithsuffix) {
  966. if (!preg_match('/\-'.preg_quote($onlywithsuffix, '/').'\./i', $file)) {
  967. //print 'File '.$file.' does not match suffix '.$onlywithsuffix.' so it is discarded<br>'."\n";
  968. continue;
  969. } else {
  970. //print 'File '.$file.' match suffix '.$onlywithsuffix.' so we keep it<br>'."\n";
  971. }
  972. }
  973. if (preg_match('/\.key\.sql$/i', $file) && substr($file, 0, 4) == 'llx_') {
  974. $result = run_sql($dir.$file, empty($conf->global->MAIN_DISPLAY_SQL_INSTALL_LOG) ? 1 : 0, '', 1);
  975. if ($result <= 0) {
  976. $error++;
  977. }
  978. }
  979. }
  980. rewinddir($handle);
  981. // Run functions-xxx.sql files (Must be done after llx_mytable.key.sql)
  982. $files = array();
  983. while (($file = readdir($handle)) !== false) {
  984. $files[] = $file;
  985. }
  986. sort($files);
  987. foreach ($files as $file) {
  988. if ($onlywithsuffix) {
  989. if (!preg_match('/\-'.preg_quote($onlywithsuffix, '/').'\./i', $file)) {
  990. //print 'File '.$file.' does not match suffix '.$onlywithsuffix.' so it is discarded<br>'."\n";
  991. continue;
  992. } else {
  993. //print 'File '.$file.' match suffix '.$onlywithsuffix.' so we keep it<br>'."\n";
  994. }
  995. }
  996. if (preg_match('/\.sql$/i', $file) && !preg_match('/\.key\.sql$/i', $file) && substr($file, 0, 9) == 'functions') {
  997. $result = run_sql($dir.$file, empty($conf->global->MAIN_DISPLAY_SQL_INSTALL_LOG) ? 1 : 0, '', 1);
  998. if ($result <= 0) {
  999. $error++;
  1000. }
  1001. }
  1002. }
  1003. rewinddir($handle);
  1004. // Run data_xxx.sql files (Must be done after llx_mytable.key.sql)
  1005. $files = array();
  1006. while (($file = readdir($handle)) !== false) {
  1007. $files[] = $file;
  1008. }
  1009. sort($files);
  1010. foreach ($files as $file) {
  1011. if ($onlywithsuffix) {
  1012. if (!preg_match('/\-'.preg_quote($onlywithsuffix, '/').'\./i', $file)) {
  1013. //print 'File '.$file.' does not match suffix '.$onlywithsuffix.' so it is discarded<br>'."\n";
  1014. continue;
  1015. } else {
  1016. //print 'File '.$file.' match suffix '.$onlywithsuffix.' so we keep it<br>'."\n";
  1017. }
  1018. }
  1019. if (preg_match('/\.sql$/i', $file) && !preg_match('/\.key\.sql$/i', $file) && substr($file, 0, 4) == 'data') {
  1020. $result = run_sql($dir.$file, empty($conf->global->MAIN_DISPLAY_SQL_INSTALL_LOG) ? 1 : 0, '', 1);
  1021. if ($result <= 0) {
  1022. $error++;
  1023. }
  1024. }
  1025. }
  1026. rewinddir($handle);
  1027. // Run update_xxx.sql files
  1028. $files = array();
  1029. while (($file = readdir($handle)) !== false) {
  1030. $files[] = $file;
  1031. }
  1032. sort($files);
  1033. foreach ($files as $file) {
  1034. if ($onlywithsuffix) {
  1035. if (!preg_match('/\-'.preg_quote($onlywithsuffix, '/').'\./i', $file)) {
  1036. //print 'File '.$file.' does not match suffix '.$onlywithsuffix.' so it is discarded<br>'."\n";
  1037. continue;
  1038. } else {
  1039. //print 'File '.$file.' match suffix '.$onlywithsuffix.' so we keep it<br>'."\n";
  1040. }
  1041. }
  1042. if (preg_match('/\.sql$/i', $file) && !preg_match('/\.key\.sql$/i', $file) && substr($file, 0, 6) == 'update') {
  1043. $result = run_sql($dir.$file, empty($conf->global->MAIN_DISPLAY_SQL_INSTALL_LOG) ? 1 : 0, '', 1);
  1044. if ($result <= 0) {
  1045. $error++;
  1046. }
  1047. }
  1048. }
  1049. closedir($handle);
  1050. }
  1051. }
  1052. if ($error == 0) {
  1053. $ok = 1;
  1054. }
  1055. }
  1056. }
  1057. if (!$dirfound) {
  1058. dol_syslog("A module ask to load sql files into ".$reldir." but this directory was not found.", LOG_WARNING);
  1059. }
  1060. return $ok;
  1061. }
  1062. // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
  1063. /**
  1064. * Adds boxes
  1065. *
  1066. * @param string $option Options when disabling module ('newboxdefonly'=insert only boxes definition)
  1067. *
  1068. * @return int Error count (0 if OK)
  1069. */
  1070. public function insert_boxes($option = '')
  1071. {
  1072. // phpcs:enable
  1073. include_once DOL_DOCUMENT_ROOT.'/core/class/infobox.class.php';
  1074. global $conf;
  1075. $err = 0;
  1076. if (is_array($this->boxes)) {
  1077. dol_syslog(get_class($this)."::insert_boxes", LOG_DEBUG);
  1078. $pos_name = InfoBox::getListOfPagesForBoxes();
  1079. foreach ($this->boxes as $key => $value) {
  1080. $file = isset($this->boxes[$key]['file']) ? $this->boxes[$key]['file'] : '';
  1081. $note = isset($this->boxes[$key]['note']) ? $this->boxes[$key]['note'] : '';
  1082. $enabledbydefaulton = isset($this->boxes[$key]['enabledbydefaulton']) ? $this->boxes[$key]['enabledbydefaulton'] : 'Home';
  1083. if (empty($file)) {
  1084. $file = isset($this->boxes[$key][1]) ? $this->boxes[$key][1] : ''; // For backward compatibility
  1085. }
  1086. if (empty($note)) {
  1087. $note = isset($this->boxes[$key][2]) ? $this->boxes[$key][2] : ''; // For backward compatibility
  1088. }
  1089. // Search if boxes def already present
  1090. $sql = "SELECT count(*) as nb FROM ".MAIN_DB_PREFIX."boxes_def";
  1091. $sql .= " WHERE file = '".$this->db->escape($file)."'";
  1092. $sql .= " AND entity = ".$conf->entity;
  1093. if ($note) {
  1094. $sql .= " AND note ='".$this->db->escape($note)."'";
  1095. }
  1096. $result = $this->db->query($sql);
  1097. if ($result) {
  1098. $obj = $this->db->fetch_object($result);
  1099. if ($obj->nb == 0) {
  1100. $this->db->begin();
  1101. if (!$err) {
  1102. $sql = "INSERT INTO ".MAIN_DB_PREFIX."boxes_def (file, entity, note)";
  1103. $sql .= " VALUES ('".$this->db->escape($file)."', ";
  1104. $sql .= $conf->entity.", ";
  1105. $sql .= $note ? "'".$this->db->escape($note)."'" : "null";
  1106. $sql .= ")";
  1107. dol_syslog(get_class($this)."::insert_boxes", LOG_DEBUG);
  1108. $resql = $this->db->query($sql);
  1109. if (!$resql) {
  1110. $err++;
  1111. }
  1112. }
  1113. if (!$err && !preg_match('/newboxdefonly/', $option)) {
  1114. $lastid = $this->db->last_insert_id(MAIN_DB_PREFIX."boxes_def", "rowid");
  1115. foreach ($pos_name as $key2 => $val2) {
  1116. //print 'key2='.$key2.'-val2='.$val2."<br>\n";
  1117. if ($enabledbydefaulton && $val2 != $enabledbydefaulton) {
  1118. continue; // Not enabled by default onto this page.
  1119. }
  1120. $sql = "INSERT INTO ".MAIN_DB_PREFIX."boxes (box_id, position, box_order, fk_user, entity)";
  1121. $sql .= " VALUES (".((int) $lastid).", ".((int) $key2).", '0', 0, ".((int) $conf->entity).")";
  1122. dol_syslog(get_class($this)."::insert_boxes onto page ".$key2."=".$val2."", LOG_DEBUG);
  1123. $resql = $this->db->query($sql);
  1124. if (!$resql) {
  1125. $err++;
  1126. }
  1127. }
  1128. }
  1129. if (!$err) {
  1130. $this->db->commit();
  1131. } else {
  1132. $this->error = $this->db->lasterror();
  1133. $this->db->rollback();
  1134. }
  1135. }
  1136. // else box already registered into database
  1137. } else {
  1138. $this->error = $this->db->lasterror();
  1139. $err++;
  1140. }
  1141. }
  1142. }
  1143. return $err;
  1144. }
  1145. // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
  1146. /**
  1147. * Removes boxes
  1148. *
  1149. * @return int Error count (0 if OK)
  1150. */
  1151. public function delete_boxes()
  1152. {
  1153. // phpcs:enable
  1154. global $conf;
  1155. $err = 0;
  1156. if (is_array($this->boxes)) {
  1157. foreach ($this->boxes as $key => $value) {
  1158. //$titre = $this->boxes[$key][0];
  1159. if (empty($this->boxes[$key]['file'])) {
  1160. $file = isset($this->boxes[$key][1]) ? $this->boxes[$key][1] : ''; // For backward compatibility
  1161. } else {
  1162. $file = $this->boxes[$key]['file'];
  1163. }
  1164. //$note = $this->boxes[$key][2];
  1165. // TODO If the box is also included by another module and the other module is still on, we should not remove it.
  1166. // For the moment, we manage this with hard coded exception
  1167. //print "Remove box ".$file.'<br>';
  1168. if ($file == 'box_graph_product_distribution.php') {
  1169. if (!empty($conf->product->enabled) || !empty($conf->service->enabled)) {
  1170. dol_syslog("We discard deleting module ".$file." because another module still active requires it.");
  1171. continue;
  1172. }
  1173. }
  1174. if ($this->db->type == 'sqlite3') {
  1175. // sqlite doesn't support "USING" syntax.
  1176. // TODO: remove this dependency.
  1177. $sql = "DELETE FROM ".MAIN_DB_PREFIX."boxes ";
  1178. $sql .= "WHERE ".MAIN_DB_PREFIX."boxes.box_id IN (";
  1179. $sql .= "SELECT ".MAIN_DB_PREFIX."boxes_def.rowid ";
  1180. $sql .= "FROM ".MAIN_DB_PREFIX."boxes_def ";
  1181. $sql .= "WHERE ".MAIN_DB_PREFIX."boxes_def.file = '".$this->db->escape($file)."') ";
  1182. $sql .= "AND ".MAIN_DB_PREFIX."boxes.entity = ".$conf->entity;
  1183. } else {
  1184. $sql = "DELETE FROM ".MAIN_DB_PREFIX."boxes";
  1185. $sql .= " USING ".MAIN_DB_PREFIX."boxes, ".MAIN_DB_PREFIX."boxes_def";
  1186. $sql .= " WHERE ".MAIN_DB_PREFIX."boxes.box_id = ".MAIN_DB_PREFIX."boxes_def.rowid";
  1187. $sql .= " AND ".MAIN_DB_PREFIX."boxes_def.file = '".$this->db->escape($file)."'";
  1188. $sql .= " AND ".MAIN_DB_PREFIX."boxes.entity = ".$conf->entity;
  1189. }
  1190. dol_syslog(get_class($this)."::delete_boxes", LOG_DEBUG);
  1191. $resql = $this->db->query($sql);
  1192. if (!$resql) {
  1193. $this->error = $this->db->lasterror();
  1194. $err++;
  1195. }
  1196. $sql = "DELETE FROM ".MAIN_DB_PREFIX."boxes_def";
  1197. $sql .= " WHERE file = '".$this->db->escape($file)."'";
  1198. $sql .= " AND entity = ".$conf->entity; // Do not use getEntity here, we want to delete only in current company
  1199. dol_syslog(get_class($this)."::delete_boxes", LOG_DEBUG);
  1200. $resql = $this->db->query($sql);
  1201. if (!$resql) {
  1202. $this->error = $this->db->lasterror();
  1203. $err++;
  1204. }
  1205. }
  1206. }
  1207. return $err;
  1208. }
  1209. // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
  1210. /**
  1211. * Adds cronjobs
  1212. *
  1213. * @return int Error count (0 if OK)
  1214. */
  1215. public function insert_cronjobs()
  1216. {
  1217. // phpcs:enable
  1218. include_once DOL_DOCUMENT_ROOT.'/core/class/infobox.class.php';
  1219. global $conf;
  1220. $err = 0;
  1221. if (is_array($this->cronjobs)) {
  1222. dol_syslog(get_class($this)."::insert_cronjobs", LOG_DEBUG);
  1223. foreach ($this->cronjobs as $key => $value) {
  1224. $entity = isset($this->cronjobs[$key]['entity']) ? $this->cronjobs[$key]['entity'] : $conf->entity;
  1225. $label = isset($this->cronjobs[$key]['label']) ? $this->cronjobs[$key]['label'] : '';
  1226. $jobtype = isset($this->cronjobs[$key]['jobtype']) ? $this->cronjobs[$key]['jobtype'] : '';
  1227. $class = isset($this->cronjobs[$key]['class']) ? $this->cronjobs[$key]['class'] : '';
  1228. $objectname = isset($this->cronjobs[$key]['objectname']) ? $this->cronjobs[$key]['objectname'] : '';
  1229. $method = isset($this->cronjobs[$key]['method']) ? $this->cronjobs[$key]['method'] : '';
  1230. $command = isset($this->cronjobs[$key]['command']) ? $this->cronjobs[$key]['command'] : '';
  1231. $parameters = isset($this->cronjobs[$key]['parameters']) ? $this->cronjobs[$key]['parameters'] : '';
  1232. $comment = isset($this->cronjobs[$key]['comment']) ? $this->cronjobs[$key]['comment'] : '';
  1233. $frequency = isset($this->cronjobs[$key]['frequency']) ? $this->cronjobs[$key]['frequency'] : '';
  1234. $unitfrequency = isset($this->cronjobs[$key]['unitfrequency']) ? $this->cronjobs[$key]['unitfrequency'] : '';
  1235. $priority = isset($this->cronjobs[$key]['priority']) ? $this->cronjobs[$key]['priority'] : '';
  1236. $datestart = isset($this->cronjobs[$key]['datestart']) ? $this->cronjobs[$key]['datestart'] : '';
  1237. $dateend = isset($this->cronjobs[$key]['dateend']) ? $this->cronjobs[$key]['dateend'] : '';
  1238. $status = isset($this->cronjobs[$key]['status']) ? $this->cronjobs[$key]['status'] : '';
  1239. $test = isset($this->cronjobs[$key]['test']) ? $this->cronjobs[$key]['test'] : ''; // Line must be enabled or not (so visible or not)
  1240. // Search if cron entry already present
  1241. $sql = "SELECT count(*) as nb FROM ".MAIN_DB_PREFIX."cronjob";
  1242. $sql .= " WHERE module_name = '".$this->db->escape(empty($this->rights_class) ?strtolower($this->name) : $this->rights_class)."'";
  1243. if ($class) {
  1244. $sql .= " AND classesname = '".$this->db->escape($class)."'";
  1245. }
  1246. if ($objectname) {
  1247. $sql .= " AND objectname = '".$this->db->escape($objectname)."'";
  1248. }
  1249. if ($method) {
  1250. $sql .= " AND methodename = '".$this->db->escape($method)."'";
  1251. }
  1252. if ($command) {
  1253. $sql .= " AND command = '".$this->db->escape($command)."'";
  1254. }
  1255. if ($parameters) {
  1256. $sql .= " AND params = '".$this->db->escape($parameters)."'";
  1257. }
  1258. $sql .= " AND entity = ".((int) $entity); // Must be exact entity
  1259. $now = dol_now();
  1260. $result = $this->db->query($sql);
  1261. if ($result) {
  1262. $obj = $this->db->fetch_object($result);
  1263. if ($obj->nb == 0) {
  1264. $this->db->begin();
  1265. if (!$err) {
  1266. $sql = "INSERT INTO ".MAIN_DB_PREFIX."cronjob (module_name, datec, datestart, dateend, label, jobtype, classesname, objectname, methodename, command, params, note,";
  1267. if (is_int($frequency)) {
  1268. $sql .= ' frequency,';
  1269. }
  1270. if (is_int($unitfrequency)) {
  1271. $sql .= ' unitfrequency,';
  1272. }
  1273. if (is_int($priority)) {
  1274. $sql .= ' priority,';
  1275. }
  1276. if (is_int($status)) {
  1277. $sql .= ' status,';
  1278. }
  1279. $sql .= " entity, test)";
  1280. $sql .= " VALUES (";
  1281. $sql .= "'".$this->db->escape(empty($this->rights_class) ?strtolower($this->name) : $this->rights_class)."', ";
  1282. $sql .= "'".$this->db->idate($now)."', ";
  1283. $sql .= ($datestart ? "'".$this->db->idate($datestart)."'" : "'".$this->db->idate($now)."'").", ";
  1284. $sql .= ($dateend ? "'".$this->db->idate($dateend)."'" : "NULL").", ";
  1285. $sql .= "'".$this->db->escape($label)."', ";
  1286. $sql .= "'".$this->db->escape($jobtype)."', ";
  1287. $sql .= ($class ? "'".$this->db->escape($class)."'" : "null").",";
  1288. $sql .= ($objectname ? "'".$this->db->escape($objectname)."'" : "null").",";
  1289. $sql .= ($method ? "'".$this->db->escape($method)."'" : "null").",";
  1290. $sql .= ($command ? "'".$this->db->escape($command)."'" : "null").",";
  1291. $sql .= ($parameters ? "'".$this->db->escape($parameters)."'" : "null").",";
  1292. $sql .= ($comment ? "'".$this->db->escape($comment)."'" : "null").",";
  1293. if (is_int($frequency)) {
  1294. $sql .= "'".$this->db->escape($frequency)."', ";
  1295. }
  1296. if (is_int($unitfrequency)) {
  1297. $sql .= "'".$this->db->escape($unitfrequency)."', ";
  1298. }
  1299. if (is_int($priority)) {
  1300. $sql .= "'".$this->db->escape($priority)."', ";
  1301. }
  1302. if (is_int($status)) {
  1303. $sql .= "'".$this->db->escape($status)."', ";
  1304. }
  1305. $sql .= $entity.",";
  1306. $sql .= "'".$this->db->escape($test)."'";
  1307. $sql .= ")";
  1308. $resql = $this->db->query($sql);
  1309. if (!$resql) {
  1310. $err++;
  1311. }
  1312. }
  1313. if (!$err) {
  1314. $this->db->commit();
  1315. } else {
  1316. $this->error = $this->db->lasterror();
  1317. $this->db->rollback();
  1318. }
  1319. }
  1320. // else box already registered into database
  1321. } else {
  1322. $this->error = $this->db->lasterror();
  1323. $err++;
  1324. }
  1325. }
  1326. }
  1327. return $err;
  1328. }
  1329. // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
  1330. /**
  1331. * Removes boxes
  1332. *
  1333. * @return int Error count (0 if OK)
  1334. */
  1335. public function delete_cronjobs()
  1336. {
  1337. // phpcs:enable
  1338. global $conf;
  1339. $err = 0;
  1340. if (is_array($this->cronjobs)) {
  1341. $sql = "DELETE FROM ".MAIN_DB_PREFIX."cronjob";
  1342. $sql .= " WHERE module_name = '".$this->db->escape(empty($this->rights_class) ?strtolower($this->name) : $this->rights_class)."'";
  1343. $sql .= " AND entity = ".$conf->entity;
  1344. $sql .= " AND test = '1'"; // We delete on lines that are not set with a complete test that is '$conf->module->enabled' so when module is disabled, the cron is also removed.
  1345. // For crons declared with a '$conf->module->enabled', there is no need to delete the line, so we don't loose setup if we reenable module.
  1346. dol_syslog(get_class($this)."::delete_cronjobs", LOG_DEBUG);
  1347. $resql = $this->db->query($sql);
  1348. if (!$resql) {
  1349. $this->error = $this->db->lasterror();
  1350. $err++;
  1351. }
  1352. }
  1353. return $err;
  1354. }
  1355. // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
  1356. /**
  1357. * Removes tabs
  1358. *
  1359. * @return int Error count (0 if OK)
  1360. */
  1361. public function delete_tabs()
  1362. {
  1363. // phpcs:enable
  1364. global $conf;
  1365. $err = 0;
  1366. $sql = "DELETE FROM ".MAIN_DB_PREFIX."const";
  1367. $sql .= " WHERE ".$this->db->decrypt('name')." like '".$this->db->escape($this->const_name)."_TABS_%'";
  1368. $sql .= " AND entity = ".$conf->entity;
  1369. dol_syslog(get_class($this)."::delete_tabs", LOG_DEBUG);
  1370. if (!$this->db->query($sql)) {
  1371. $this->error = $this->db->lasterror();
  1372. $err++;
  1373. }
  1374. return $err;
  1375. }
  1376. // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
  1377. /**
  1378. * Adds tabs
  1379. *
  1380. * @return int Error count (0 if ok)
  1381. */
  1382. public function insert_tabs()
  1383. {
  1384. // phpcs:enable
  1385. global $conf;
  1386. $err = 0;
  1387. if (!empty($this->tabs)) {
  1388. dol_syslog(get_class($this)."::insert_tabs", LOG_DEBUG);
  1389. $i = 0;
  1390. foreach ($this->tabs as $key => $value) {
  1391. if (is_array($value) && count($value) == 0) {
  1392. continue; // Discard empty arrays
  1393. }
  1394. $entity = $conf->entity;
  1395. $newvalue = $value;
  1396. if (is_array($value)) {
  1397. $newvalue = $value['data'];
  1398. if (isset($value['entity'])) {
  1399. $entity = $value['entity'];
  1400. }
  1401. }
  1402. if ($newvalue) {
  1403. $sql = "INSERT INTO ".MAIN_DB_PREFIX."const (";
  1404. $sql .= "name";
  1405. $sql .= ", type";
  1406. $sql .= ", value";
  1407. $sql .= ", note";
  1408. $sql .= ", visible";
  1409. $sql .= ", entity";
  1410. $sql .= ")";
  1411. $sql .= " VALUES (";
  1412. $sql .= $this->db->encrypt($this->const_name."_TABS_".$i);
  1413. $sql .= ", 'chaine'";
  1414. $sql .= ", ".$this->db->encrypt($newvalue);
  1415. $sql .= ", null";
  1416. $sql .= ", '0'";
  1417. $sql .= ", ".((int) $entity);
  1418. $sql .= ")";
  1419. $resql = $this->db->query($sql);
  1420. if (!$resql) {
  1421. dol_syslog($this->db->lasterror(), LOG_ERR);
  1422. if ($this->db->lasterrno() != 'DB_ERROR_RECORD_ALREADY_EXISTS') {
  1423. $this->error = $this->db->lasterror();
  1424. $this->errors[] = $this->db->lasterror();
  1425. $err++;
  1426. break;
  1427. }
  1428. }
  1429. }
  1430. $i++;
  1431. }
  1432. }
  1433. return $err;
  1434. }
  1435. // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
  1436. /**
  1437. * Adds constants
  1438. *
  1439. * @return int Error count (0 if OK)
  1440. */
  1441. public function insert_const()
  1442. {
  1443. // phpcs:enable
  1444. global $conf;
  1445. $err = 0;
  1446. if (empty($this->const)) {
  1447. return 0;
  1448. }
  1449. dol_syslog(get_class($this)."::insert_const", LOG_DEBUG);
  1450. foreach ($this->const as $key => $value) {
  1451. $name = $this->const[$key][0];
  1452. $type = $this->const[$key][1];
  1453. $val = $this->const[$key][2];
  1454. $note = isset($this->const[$key][3]) ? $this->const[$key][3] : '';
  1455. $visible = isset($this->const[$key][4]) ? $this->const[$key][4] : 0;
  1456. $entity = (!empty($this->const[$key][5]) && $this->const[$key][5] != 'current') ? 0 : $conf->entity;
  1457. // Clean
  1458. if (empty($visible)) {
  1459. $visible = '0';
  1460. }
  1461. if (empty($val) && $val != '0') {
  1462. $val = '';
  1463. }
  1464. $sql = "SELECT count(*)";
  1465. $sql .= " FROM ".MAIN_DB_PREFIX."const";
  1466. $sql .= " WHERE ".$this->db->decrypt('name')." = '".$this->db->escape($name)."'";
  1467. $sql .= " AND entity = ".((int) $entity);
  1468. $result = $this->db->query($sql);
  1469. if ($result) {
  1470. $row = $this->db->fetch_row($result);
  1471. if ($row[0] == 0) { // If not found
  1472. $sql = "INSERT INTO ".MAIN_DB_PREFIX."const (name,type,value,note,visible,entity)";
  1473. $sql .= " VALUES (";
  1474. $sql .= $this->db->encrypt($name);
  1475. $sql .= ",'".$this->db->escape($type)."'";
  1476. $sql .= ",".(($val != '') ? $this->db->encrypt($val) : "''");
  1477. $sql .= ",".($note ? "'".$this->db->escape($note)."'" : "null");
  1478. $sql .= ",'".$this->db->escape($visible)."'";
  1479. $sql .= ",".$entity;
  1480. $sql .= ")";
  1481. if (!$this->db->query($sql)) {
  1482. $err++;
  1483. }
  1484. } else {
  1485. dol_syslog(get_class($this)."::insert_const constant '".$name."' already exists", LOG_WARNING);
  1486. }
  1487. } else {
  1488. $err++;
  1489. }
  1490. }
  1491. return $err;
  1492. }
  1493. // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
  1494. /**
  1495. * Removes constants tagged 'deleteonunactive'
  1496. *
  1497. * @return int <0 if KO, 0 if OK
  1498. */
  1499. public function delete_const()
  1500. {
  1501. // phpcs:enable
  1502. global $conf;
  1503. $err = 0;
  1504. if (empty($this->const)) {
  1505. return 0;
  1506. }
  1507. foreach ($this->const as $key => $value) {
  1508. $name = $this->const[$key][0];
  1509. $deleteonunactive = (!empty($this->const[$key][6])) ? 1 : 0;
  1510. if ($deleteonunactive) {
  1511. $sql = "DELETE FROM ".MAIN_DB_PREFIX."const";
  1512. $sql .= " WHERE ".$this->db->decrypt('name')." = '".$this->db->escape($name)."'";
  1513. $sql .= " AND entity in (0, ".$conf->entity.")";
  1514. dol_syslog(get_class($this)."::delete_const", LOG_DEBUG);
  1515. if (!$this->db->query($sql)) {
  1516. $this->error = $this->db->lasterror();
  1517. $err++;
  1518. }
  1519. }
  1520. }
  1521. return $err;
  1522. }
  1523. // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
  1524. /**
  1525. * Adds access rights
  1526. *
  1527. * @param int $reinitadminperms If 1, we also grant them to all admin users
  1528. * @param int $force_entity Force current entity
  1529. * @param int $notrigger 1=Does not execute triggers, 0= execute triggers
  1530. * @return int Error count (0 if OK)
  1531. */
  1532. public function insert_permissions($reinitadminperms = 0, $force_entity = null, $notrigger = 0)
  1533. {
  1534. // phpcs:enable
  1535. global $conf, $user;
  1536. $err = 0;
  1537. $entity = (!empty($force_entity) ? $force_entity : $conf->entity);
  1538. dol_syslog(get_class($this)."::insert_permissions", LOG_DEBUG);
  1539. // Test if module is activated
  1540. $sql_del = "SELECT ".$this->db->decrypt('value')." as value";
  1541. $sql_del .= " FROM ".MAIN_DB_PREFIX."const";
  1542. $sql_del .= " WHERE ".$this->db->decrypt('name')." = '".$this->db->escape($this->const_name)."'";
  1543. $sql_del .= " AND entity IN (0,".$entity.")";
  1544. $resql = $this->db->query($sql_del);
  1545. if ($resql) {
  1546. $obj = $this->db->fetch_object($resql);
  1547. if ($obj !== null && !empty($obj->value) && !empty($this->rights)) {
  1548. include_once DOL_DOCUMENT_ROOT.'/user/class/user.class.php';
  1549. // If the module is active
  1550. foreach ($this->rights as $key => $value) {
  1551. $r_id = $this->rights[$key][0]; // permission id in llx_rights_def (not unique because primary key is couple id-entity)
  1552. $r_desc = $this->rights[$key][1];
  1553. $r_type = isset($this->rights[$key][2]) ? $this->rights[$key][2] : '';
  1554. $r_def = empty($this->rights[$key][3]) ? 0 : $this->rights[$key][3];
  1555. $r_perms = $this->rights[$key][4];
  1556. $r_subperms = isset($this->rights[$key][5]) ? $this->rights[$key][5] : '';
  1557. $r_modul = empty($this->rights_class) ? strtolower($this->name) : $this->rights_class;
  1558. if (empty($r_type)) {
  1559. $r_type = 'w';
  1560. }
  1561. // Search if perm already present
  1562. $sql = "SELECT count(*) as nb FROM ".MAIN_DB_PREFIX."rights_def";
  1563. $sql .= " WHERE id = ".((int) $r_id)." AND entity = ".((int) $entity);
  1564. $resqlselect = $this->db->query($sql);
  1565. if ($resqlselect) {
  1566. $objcount = $this->db->fetch_object($resqlselect);
  1567. if ($objcount && $objcount->nb == 0) {
  1568. if (dol_strlen($r_perms)) {
  1569. if (dol_strlen($r_subperms)) {
  1570. $sql = "INSERT INTO ".MAIN_DB_PREFIX."rights_def";
  1571. $sql .= " (id, entity, libelle, module, type, bydefault, perms, subperms)";
  1572. $sql .= " VALUES ";
  1573. $sql .= "(".$r_id.",".$entity.",'".$this->db->escape($r_desc)."','".$this->db->escape($r_modul)."','".$this->db->escape($r_type)."',".$r_def.",'".$this->db->escape($r_perms)."','".$this->db->escape($r_subperms)."')";
  1574. } else {
  1575. $sql = "INSERT INTO ".MAIN_DB_PREFIX."rights_def";
  1576. $sql .= " (id, entity, libelle, module, type, bydefault, perms)";
  1577. $sql .= " VALUES ";
  1578. $sql .= "(".$r_id.",".$entity.",'".$this->db->escape($r_desc)."','".$this->db->escape($r_modul)."','".$this->db->escape($r_type)."',".$r_def.",'".$this->db->escape($r_perms)."')";
  1579. }
  1580. } else {
  1581. $sql = "INSERT INTO ".MAIN_DB_PREFIX."rights_def ";
  1582. $sql .= " (id, entity, libelle, module, type, bydefault)";
  1583. $sql .= " VALUES ";
  1584. $sql .= "(".$r_id.",".$entity.",'".$this->db->escape($r_desc)."','".$this->db->escape($r_modul)."','".$this->db->escape($r_type)."',".$r_def.")";
  1585. }
  1586. $resqlinsert = $this->db->query($sql, 1);
  1587. if (!$resqlinsert) {
  1588. if ($this->db->errno() != "DB_ERROR_RECORD_ALREADY_EXISTS") {
  1589. $this->error = $this->db->lasterror();
  1590. $err++;
  1591. break;
  1592. } else {
  1593. dol_syslog(get_class($this)."::insert_permissions record already exists", LOG_INFO);
  1594. }
  1595. }
  1596. $this->db->free($resqlinsert);
  1597. }
  1598. $this->db->free($resqlselect);
  1599. }
  1600. // If we want to init permissions on admin users
  1601. if ($reinitadminperms) {
  1602. $sql = "SELECT rowid FROM ".MAIN_DB_PREFIX."user WHERE admin = 1";
  1603. dol_syslog(get_class($this)."::insert_permissions Search all admin users", LOG_DEBUG);
  1604. $resqlseladmin = $this->db->query($sql, 1);
  1605. if ($resqlseladmin) {
  1606. $num = $this->db->num_rows($resqlseladmin);
  1607. $i = 0;
  1608. while ($i < $num) {
  1609. $obj2 = $this->db->fetch_object($resqlseladmin);
  1610. dol_syslog(get_class($this)."::insert_permissions Add permission id '.$r_id.' to user id=".$obj2->rowid);
  1611. $tmpuser = new User($this->db);
  1612. $result = $tmpuser->fetch($obj2->rowid);
  1613. if ($result > 0) {
  1614. $tmpuser->addrights($r_id, '', '', 0, 1);
  1615. } else {
  1616. dol_syslog(get_class($this)."::insert_permissions Failed to add the permission to user because fetch return an error", LOG_ERR);
  1617. }
  1618. $i++;
  1619. }
  1620. } else {
  1621. dol_print_error($this->db);
  1622. }
  1623. }
  1624. }
  1625. if ($reinitadminperms && !empty($user->admin)) { // Reload permission for current user if defined
  1626. // We reload permissions
  1627. $user->clearrights();
  1628. $user->getrights();
  1629. }
  1630. }
  1631. $this->db->free($resql);
  1632. } else {
  1633. $this->error = $this->db->lasterror();
  1634. $err++;
  1635. }
  1636. return $err;
  1637. }
  1638. // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
  1639. /**
  1640. * Removes access rights
  1641. *
  1642. * @return int Error count (0 if OK)
  1643. */
  1644. public function delete_permissions()
  1645. {
  1646. // phpcs:enable
  1647. global $conf;
  1648. $err = 0;
  1649. $sql = "DELETE FROM ".MAIN_DB_PREFIX."rights_def";
  1650. $sql .= " WHERE module = '".$this->db->escape(empty($this->rights_class) ?strtolower($this->name) : $this->rights_class)."'";
  1651. $sql .= " AND entity = ".$conf->entity;
  1652. dol_syslog(get_class($this)."::delete_permissions", LOG_DEBUG);
  1653. if (!$this->db->query($sql)) {
  1654. $this->error = $this->db->lasterror();
  1655. $err++;
  1656. }
  1657. return $err;
  1658. }
  1659. // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
  1660. /**
  1661. * Adds menu entries
  1662. *
  1663. * @return int Error count (0 if OK)
  1664. */
  1665. public function insert_menus()
  1666. {
  1667. // phpcs:enable
  1668. global $user;
  1669. if (!is_array($this->menu) || empty($this->menu)) {
  1670. return 0;
  1671. }
  1672. include_once DOL_DOCUMENT_ROOT.'/core/class/menubase.class.php';
  1673. dol_syslog(get_class($this)."::insert_menus", LOG_DEBUG);
  1674. $err = 0;
  1675. $this->db->begin();
  1676. foreach ($this->menu as $key => $value) {
  1677. $menu = new Menubase($this->db);
  1678. $menu->menu_handler = 'all';
  1679. //$menu->module=strtolower($this->name); TODO When right_class will be same than module name
  1680. $menu->module = empty($this->rights_class) ?strtolower($this->name) : $this->rights_class;
  1681. if (!$this->menu[$key]['fk_menu']) {
  1682. $menu->fk_menu = 0;
  1683. } else {
  1684. $foundparent = 0;
  1685. $fk_parent = $this->menu[$key]['fk_menu'];
  1686. if (preg_match('/^r=/', $fk_parent)) { // old deprecated method
  1687. $fk_parent = str_replace('r=', '', $fk_parent);
  1688. if (isset($this->menu[$fk_parent]['rowid'])) {
  1689. $menu->fk_menu = $this->menu[$fk_parent]['rowid'];
  1690. $foundparent = 1;
  1691. }
  1692. } elseif (preg_match('/^fk_mainmenu=([a-zA-Z0-9_]+),fk_leftmenu=([a-zA-Z0-9_]+)$/', $fk_parent, $reg)) {
  1693. $menu->fk_menu = -1;
  1694. $menu->fk_mainmenu = $reg[1];
  1695. $menu->fk_leftmenu = $reg[2];
  1696. $foundparent = 1;
  1697. } elseif (preg_match('/^fk_mainmenu=([a-zA-Z0-9_]+)$/', $fk_parent, $reg)) {
  1698. $menu->fk_menu = -1;
  1699. $menu->fk_mainmenu = $reg[1];
  1700. $menu->fk_leftmenu = '';
  1701. $foundparent = 1;
  1702. }
  1703. if (!$foundparent) {
  1704. $this->error = "ErrorBadDefinitionOfMenuArrayInModuleDescriptor";
  1705. dol_syslog(get_class($this)."::insert_menus ".$this->error." ".$this->menu[$key]['fk_menu'], LOG_ERR);
  1706. $err++;
  1707. }
  1708. }
  1709. $menu->type = $this->menu[$key]['type'];
  1710. $menu->mainmenu = isset($this->menu[$key]['mainmenu']) ? $this->menu[$key]['mainmenu'] : (isset($menu->fk_mainmenu) ? $menu->fk_mainmenu : '');
  1711. $menu->leftmenu = isset($this->menu[$key]['leftmenu']) ? $this->menu[$key]['leftmenu'] : '';
  1712. $menu->title = $this->menu[$key]['titre'];
  1713. $menu->prefix = isset($this->menu[$key]['prefix']) ? $this->menu[$key]['prefix'] : '';
  1714. $menu->url = $this->menu[$key]['url'];
  1715. $menu->langs = isset($this->menu[$key]['langs']) ? $this->menu[$key]['langs'] : '';
  1716. $menu->position = $this->menu[$key]['position'];
  1717. $menu->perms = $this->menu[$key]['perms'];
  1718. $menu->target = isset($this->menu[$key]['target']) ? $this->menu[$key]['target'] : '';
  1719. $menu->user = $this->menu[$key]['user'];
  1720. $menu->enabled = isset($this->menu[$key]['enabled']) ? $this->menu[$key]['enabled'] : 0;
  1721. $menu->position = $this->menu[$key]['position'];
  1722. if (!$err) {
  1723. $result = $menu->create($user); // Save menu entry into table llx_menu
  1724. if ($result > 0) {
  1725. $this->menu[$key]['rowid'] = $result;
  1726. } else {
  1727. $this->error = $menu->error;
  1728. dol_syslog(get_class($this).'::insert_menus result='.$result." ".$this->error, LOG_ERR);
  1729. $err++;
  1730. break;
  1731. }
  1732. }
  1733. }
  1734. if (!$err) {
  1735. $this->db->commit();
  1736. } else {
  1737. dol_syslog(get_class($this)."::insert_menus ".$this->error, LOG_ERR);
  1738. $this->db->rollback();
  1739. }
  1740. return $err;
  1741. }
  1742. // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
  1743. /**
  1744. * Removes menu entries
  1745. *
  1746. * @return int Error count (0 if OK)
  1747. */
  1748. public function delete_menus()
  1749. {
  1750. // phpcs:enable
  1751. global $conf;
  1752. $err = 0;
  1753. //$module=strtolower($this->name); TODO When right_class will be same than module name
  1754. $module = empty($this->rights_class) ?strtolower($this->name) : $this->rights_class;
  1755. $sql = "DELETE FROM ".MAIN_DB_PREFIX."menu";
  1756. $sql .= " WHERE module = '".$this->db->escape($module)."'";
  1757. $sql .= " AND entity = ".$conf->entity;
  1758. dol_syslog(get_class($this)."::delete_menus", LOG_DEBUG);
  1759. $resql = $this->db->query($sql);
  1760. if (!$resql) {
  1761. $this->error = $this->db->lasterror();
  1762. $err++;
  1763. }
  1764. return $err;
  1765. }
  1766. // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
  1767. /**
  1768. * Creates directories
  1769. *
  1770. * @return int Error count (0 if OK)
  1771. */
  1772. public function create_dirs()
  1773. {
  1774. // phpcs:enable
  1775. global $langs, $conf;
  1776. $err = 0;
  1777. if (isset($this->dirs) && is_array($this->dirs)) {
  1778. foreach ($this->dirs as $key => $value) {
  1779. $addtodatabase = 0;
  1780. if (!is_array($value)) {
  1781. $dir = $value; // Default simple mode
  1782. } else {
  1783. $constname = $this->const_name."_DIR_";
  1784. $dir = $this->dirs[$key][1];
  1785. $addtodatabase = empty($this->dirs[$key][2]) ? '' : $this->dirs[$key][2]; // Create constante in llx_const
  1786. $subname = empty($this->dirs[$key][3]) ? '' : strtoupper($this->dirs[$key][3]); // Add submodule name (ex: $conf->module->submodule->dir_output)
  1787. $forcename = empty($this->dirs[$key][4]) ? '' : strtoupper($this->dirs[$key][4]); // Change the module name if different
  1788. if (!empty($forcename)) {
  1789. $constname = 'MAIN_MODULE_'.$forcename."_DIR_";
  1790. }
  1791. if (!empty($subname)) {
  1792. $constname = $constname.$subname."_";
  1793. }
  1794. $name = $constname.strtoupper($this->dirs[$key][0]);
  1795. }
  1796. // Define directory full path ($dir must start with "/")
  1797. if (empty($conf->global->MAIN_MODULE_MULTICOMPANY) || $conf->entity == 1) {
  1798. $fulldir = DOL_DATA_ROOT.$dir;
  1799. } else {
  1800. $fulldir = DOL_DATA_ROOT."/".$conf->entity.$dir;
  1801. }
  1802. // Create dir if it does not exists
  1803. if (!empty($fulldir) && !file_exists($fulldir)) {
  1804. if (dol_mkdir($fulldir, DOL_DATA_ROOT) < 0) {
  1805. $this->error = $langs->trans("ErrorCanNotCreateDir", $fulldir);
  1806. dol_syslog(get_class($this)."::_init ".$this->error, LOG_ERR);
  1807. $err++;
  1808. }
  1809. }
  1810. // Define the constant in database if requested (not the default mode)
  1811. if (!empty($addtodatabase)) {
  1812. $result = $this->insert_dirs($name, $dir);
  1813. if ($result) {
  1814. $err++;
  1815. }
  1816. }
  1817. }
  1818. }
  1819. return $err;
  1820. }
  1821. // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
  1822. /**
  1823. * Adds directories definitions
  1824. *
  1825. * @param string $name Name
  1826. * @param string $dir Directory
  1827. *
  1828. * @return int Error count (0 if OK)
  1829. */
  1830. public function insert_dirs($name, $dir)
  1831. {
  1832. // phpcs:enable
  1833. global $conf;
  1834. $err = 0;
  1835. $sql = "SELECT count(*)";
  1836. $sql .= " FROM ".MAIN_DB_PREFIX."const";
  1837. $sql .= " WHERE ".$this->db->decrypt('name')." = '".$this->db->escape($name)."'";
  1838. $sql .= " AND entity = ".$conf->entity;
  1839. dol_syslog(get_class($this)."::insert_dirs", LOG_DEBUG);
  1840. $result = $this->db->query($sql);
  1841. if ($result) {
  1842. $row = $this->db->fetch_row($result);
  1843. if ($row[0] == 0) {
  1844. $sql = "INSERT INTO ".MAIN_DB_PREFIX."const (name, type, value, note, visible, entity)";
  1845. $sql .= " VALUES (".$this->db->encrypt($name).", 'chaine', ".$this->db->encrypt($dir).", '".$this->db->escape("Directory for module ".$this->name)."', '0', ".((int) $conf->entity).")";
  1846. dol_syslog(get_class($this)."::insert_dirs", LOG_DEBUG);
  1847. $this->db->query($sql);
  1848. }
  1849. } else {
  1850. $this->error = $this->db->lasterror();
  1851. $err++;
  1852. }
  1853. return $err;
  1854. }
  1855. // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
  1856. /**
  1857. * Removes directories
  1858. *
  1859. * @return int Error count (0 if OK)
  1860. */
  1861. public function delete_dirs()
  1862. {
  1863. // phpcs:enable
  1864. global $conf;
  1865. $err = 0;
  1866. $sql = "DELETE FROM ".MAIN_DB_PREFIX."const";
  1867. $sql .= " WHERE ".$this->db->decrypt('name')." LIKE '".$this->db->escape($this->const_name)."_DIR_%'";
  1868. $sql .= " AND entity = ".$conf->entity;
  1869. dol_syslog(get_class($this)."::delete_dirs", LOG_DEBUG);
  1870. if (!$this->db->query($sql)) {
  1871. $this->error = $this->db->lasterror();
  1872. $err++;
  1873. }
  1874. return $err;
  1875. }
  1876. // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
  1877. /**
  1878. * Adds generic parts
  1879. *
  1880. * @return int Error count (0 if OK)
  1881. */
  1882. public function insert_module_parts()
  1883. {
  1884. // phpcs:enable
  1885. global $conf;
  1886. $error = 0;
  1887. if (is_array($this->module_parts) && !empty($this->module_parts)) {
  1888. foreach ($this->module_parts as $key => $value) {
  1889. if (is_array($value) && count($value) == 0) {
  1890. continue; // Discard empty arrays
  1891. }
  1892. $entity = $conf->entity; // Reset the current entity
  1893. $newvalue = $value;
  1894. // Serialize array parameters
  1895. if (is_array($value)) {
  1896. // Can defined other parameters
  1897. // Example when $key='hooks', then $value is an array('data'=>array('hookcontext1','hookcontext2'), 'entity'=>X)
  1898. if (isset($value['data']) && is_array($value['data'])) {
  1899. $newvalue = json_encode($value['data']);
  1900. if (isset($value['entity'])) {
  1901. $entity = $value['entity'];
  1902. }
  1903. } elseif (isset($value['data']) && !is_array($value['data'])) {
  1904. $newvalue = $value['data'];
  1905. if (isset($value['entity'])) {
  1906. $entity = $value['entity'];
  1907. }
  1908. } else { // when hook is declared with syntax 'hook'=>array('hookcontext1','hookcontext2',...)
  1909. $newvalue = json_encode($value);
  1910. }
  1911. }
  1912. $sql = "INSERT INTO ".MAIN_DB_PREFIX."const (";
  1913. $sql .= "name";
  1914. $sql .= ", type";
  1915. $sql .= ", value";
  1916. $sql .= ", note";
  1917. $sql .= ", visible";
  1918. $sql .= ", entity";
  1919. $sql .= ")";
  1920. $sql .= " VALUES (";
  1921. $sql .= " ".$this->db->encrypt($this->const_name."_".strtoupper($key));
  1922. $sql .= ", 'chaine'";
  1923. $sql .= ", ".$this->db->encrypt($newvalue);
  1924. $sql .= ", null";
  1925. $sql .= ", '0'";
  1926. $sql .= ", ".((int) $entity);
  1927. $sql .= ")";
  1928. dol_syslog(get_class($this)."::insert_module_parts for key=".$this->const_name."_".strtoupper($key), LOG_DEBUG);
  1929. $resql = $this->db->query($sql, 1);
  1930. if (!$resql) {
  1931. if ($this->db->lasterrno() != 'DB_ERROR_RECORD_ALREADY_EXISTS') {
  1932. $error++;
  1933. $this->error = $this->db->lasterror();
  1934. } else {
  1935. dol_syslog(get_class($this)."::insert_module_parts for ".$this->const_name."_".strtoupper($key)." Record already exists.", LOG_WARNING);
  1936. }
  1937. }
  1938. }
  1939. }
  1940. return $error;
  1941. }
  1942. // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
  1943. /**
  1944. * Removes generic parts
  1945. *
  1946. * @return int Error count (0 if OK)
  1947. */
  1948. public function delete_module_parts()
  1949. {
  1950. // phpcs:enable
  1951. global $conf;
  1952. $err = 0;
  1953. $entity = $conf->entity;
  1954. if (is_array($this->module_parts) && !empty($this->module_parts)) {
  1955. dol_syslog(get_class($this)."::delete_module_parts", LOG_DEBUG);
  1956. foreach ($this->module_parts as $key => $value) {
  1957. // If entity is defined
  1958. if (is_array($value) && isset($value['entity'])) {
  1959. $entity = $value['entity'];
  1960. }
  1961. $sql = "DELETE FROM ".MAIN_DB_PREFIX."const";
  1962. $sql .= " WHERE ".$this->db->decrypt('name')." LIKE '".$this->db->escape($this->const_name)."_".strtoupper($key)."'";
  1963. $sql .= " AND entity = ".((int) $entity);
  1964. if (!$this->db->query($sql)) {
  1965. $this->error = $this->db->lasterror();
  1966. $err++;
  1967. }
  1968. }
  1969. }
  1970. return $err;
  1971. }
  1972. /**
  1973. * Function called when module is enabled.
  1974. * The init function adds tabs, constants, boxes, permissions and menus (defined in constructor) into Dolibarr database.
  1975. * It also creates data directories
  1976. *
  1977. * @param string $options Options when enabling module ('', 'newboxdefonly', 'noboxes', 'menuonly')
  1978. * 'noboxes' = Do not insert boxes 'newboxdefonly' = For boxes, insert def of boxes only and not boxes activation
  1979. * @return int 1 if OK, 0 if KO
  1980. */
  1981. public function init($options = '')
  1982. {
  1983. return $this->_init(array(), $options);
  1984. }
  1985. /**
  1986. * Function called when module is disabled.
  1987. * The remove function removes tabs, constants, boxes, permissions and menus from Dolibarr database.
  1988. * Data directories are not deleted
  1989. *
  1990. * @param string $options Options when enabling module ('', 'noboxes')
  1991. * @return int 1 if OK, 0 if KO
  1992. */
  1993. public function remove($options = '')
  1994. {
  1995. return $this->_remove(array(), $options);
  1996. }
  1997. /**
  1998. * Return Kanban view of a module
  1999. *
  2000. * @param string $codeenabledisable HTML code for button to enable/disable module
  2001. * @param string $codetoconfig HTML code to go to config page
  2002. * @return string HTML code of Kanban view
  2003. */
  2004. public function getKanbanView($codeenabledisable = '', $codetoconfig = '')
  2005. {
  2006. global $conf, $langs;
  2007. // Define imginfo
  2008. $imginfo = "info";
  2009. if ($this->isCoreOrExternalModule() == 'external') {
  2010. $imginfo = "info_black";
  2011. }
  2012. $const_name = 'MAIN_MODULE_'.strtoupper(preg_replace('/^mod/i', '', get_class($this)));
  2013. $version = $this->getVersion(0);
  2014. $versiontrans = '';
  2015. if (preg_match('/development/i', $version)) {
  2016. $versiontrans .= 'warning';
  2017. }
  2018. if (preg_match('/experimental/i', $version)) {
  2019. $versiontrans .= 'warning';
  2020. }
  2021. if (preg_match('/deprecated/i', $version)) {
  2022. $versiontrans .= 'warning';
  2023. }
  2024. print '
  2025. <div class="box-flex-item info-box-module'
  2026. .(empty($conf->global->$const_name) ? ' --disabled' : '')
  2027. .($this->isCoreOrExternalModule() == 'external' ? ' --external' : '')
  2028. .($this->needUpdate ? ' --need-update' : '')
  2029. .'">
  2030. <div class="info-box info-box-sm info-box-module">
  2031. <div class="info-box-icon'.(empty($conf->global->$const_name) ? '' : ' info-box-icon-module-enabled'.($versiontrans ? ' info-box-icon-module-warning' : '')).'">';
  2032. $alttext = '';
  2033. //if (is_array($objMod->need_dolibarr_version)) $alttext.=($alttext?' - ':'').'Dolibarr >= '.join('.',$objMod->need_dolibarr_version);
  2034. //if (is_array($objMod->phpmin)) $alttext.=($alttext?' - ':'').'PHP >= '.join('.',$objMod->phpmin);
  2035. if (!empty($this->picto)) {
  2036. if (preg_match('/^\//i', $this->picto)) {
  2037. print img_picto($alttext, $this->picto, 'class="inline-block valignmiddle"', 1);
  2038. } else {
  2039. print img_object($alttext, $this->picto, 'class="inline-block valignmiddle"');
  2040. }
  2041. } else {
  2042. print img_object($alttext, 'generic', 'class="inline-block valignmiddle"');
  2043. }
  2044. if ($this->isCoreOrExternalModule() == 'external' || preg_match('/development|experimental|deprecated/i', $version)) {
  2045. $versionTitle = $langs->trans("Version").' '.$this->getVersion(1);
  2046. if ($this->needUpdate) {
  2047. $versionTitle.= '<br>'.$langs->trans('ModuleUpdateAvailable').' : '.$this->lastVersion;
  2048. }
  2049. print '<span class="info-box-icon-version'.($versiontrans ? ' '.$versiontrans : '').' classfortooltip" title="'.dol_escape_js($versionTitle).'" >';
  2050. print $this->getVersion(1);
  2051. print '</span>';
  2052. }
  2053. print '</div>
  2054. <div class="info-box-content info-box-text-module'.(empty($conf->global->$const_name) ? '' : ' info-box-module-enabled'.($versiontrans ? ' info-box-content-warning' : '')).'">
  2055. <span class="info-box-title">'.$this->getName().'</span>
  2056. <span class="info-box-desc twolinesmax opacitymedium" title="'.dol_escape_htmltag($this->getDesc()).'">'.nl2br($this->getDesc()).'</span>';
  2057. print '<div class="valignmiddle inline-block info-box-more">';
  2058. //if ($versiontrans) print img_warning($langs->trans("Version").' '.$this->getVersion(1)).' ';
  2059. print '<a class="valignmiddle inline-block" href="javascript:document_preview(\''.DOL_URL_ROOT.'/admin/modulehelp.php?id='.$this->numero.'\',\'text/html\',\''.dol_escape_js($langs->trans("Module")).'\')">'.img_picto(($this->isCoreOrExternalModule() == 'external' ? $langs->trans("ExternalModule").' - ' : '').$langs->trans("ClickToShowDescription"), $imginfo).'</a>';
  2060. print '</div><br>';
  2061. print '<div class="valignmiddle inline-block info-box-actions">';
  2062. print '<div class="valignmiddle inline-block info-box-setup">';
  2063. print $codetoconfig;
  2064. print '</div>';
  2065. print '<div class="valignmiddle inline-block marginleftonly marginrightonly">';
  2066. print $codeenabledisable;
  2067. print '</div>';
  2068. print '</div>';
  2069. print '
  2070. </div><!-- /.info-box-content -->
  2071. </div><!-- /.info-box -->
  2072. </div>';
  2073. }
  2074. /**
  2075. * Check for module update
  2076. * TODO : store results for $this->url_last_version and $this->needUpdate
  2077. * Add a cron task to monitor for updates
  2078. *
  2079. * @return int <0 if Error, 0 == no update needed, >0 if need update
  2080. */
  2081. public function checkForUpdate()
  2082. {
  2083. require_once DOL_DOCUMENT_ROOT.'/core/lib/geturl.lib.php';
  2084. if (!empty($this->url_last_version)) {
  2085. $lastVersion = getURLContent($this->url_last_version, 'GET', '', 1, array(), array('http', 'https'), 0); // Accept http or https links on external remote server only
  2086. if (isset($lastVersion['content']) && strlen($lastVersion['content']) < 30) {
  2087. // Security warning : be careful with remote data content, the module editor could be hacked (or evil) so limit to a-z A-Z 0-9 _ . -
  2088. $this->lastVersion = preg_replace("/[^a-zA-Z0-9_\.\-]+/", "", $lastVersion['content']);
  2089. if (version_compare($this->lastVersion, $this->version) > 0) {
  2090. $this->needUpdate = true;
  2091. return 1;
  2092. } else {
  2093. $this->needUpdate = false;
  2094. return 0;
  2095. }
  2096. } else {
  2097. return -1;
  2098. }
  2099. }
  2100. return 0;
  2101. }
  2102. }