inc.php 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762
  1. <?php
  2. /* Copyright (C) 2004 Rodolphe Quiedeville <rodolphe@quiedeville.org>
  3. * Copyright (C) 2004 Benoit Mortier <benoit.mortier@opensides.be>
  4. * Copyright (C) 2004 Sebastien DiCintio <sdicintio@ressource-toi.org>
  5. * Copyright (C) 2007-2012 Laurent Destailleur <eldy@users.sourceforge.net>
  6. * Copyright (C) 2012 Marcos García <marcosgdf@gmail.com>
  7. * Copyright (C) 2016 Raphaël Doursenaud <rdoursenaud@gpcsolutions.fr>
  8. * Copyright (C) 2021 Charlene Benke <charlene@patas-monkey.com>
  9. * Copyright (C) 2023 Alexandre Janniaux <alexandre.janniaux@gmail.com>
  10. *
  11. * This program is free software; you can redistribute it and/or modify
  12. * it under the terms of the GNU General Public License as published by
  13. * the Free Software Foundation; either version 3 of the License, or
  14. * (at your option) any later version.
  15. *
  16. * This program is distributed in the hope that it will be useful,
  17. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  18. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  19. * GNU General Public License for more details.
  20. *
  21. * You should have received a copy of the GNU General Public License
  22. * along with this program. If not, see <https://www.gnu.org/licenses/>.
  23. */
  24. /**
  25. * \file htdocs/install/inc.php
  26. * \ingroup core
  27. * \brief File that define environment for support pages
  28. */
  29. // Just to define version DOL_VERSION
  30. if (!defined('DOL_INC_FOR_VERSION_ERROR')) {
  31. define('DOL_INC_FOR_VERSION_ERROR', '1');
  32. }
  33. require_once '../filefunc.inc.php';
  34. // Define DOL_DOCUMENT_ROOT used for install/upgrade process
  35. if (!defined('DOL_DOCUMENT_ROOT')) {
  36. define('DOL_DOCUMENT_ROOT', '..');
  37. }
  38. require_once DOL_DOCUMENT_ROOT.'/core/class/conf.class.php';
  39. require_once DOL_DOCUMENT_ROOT.'/core/class/translate.class.php';
  40. require_once DOL_DOCUMENT_ROOT.'/core/lib/functions.lib.php';
  41. require_once DOL_DOCUMENT_ROOT.'/core/lib/admin.lib.php';
  42. require_once DOL_DOCUMENT_ROOT.'/core/lib/files.lib.php';
  43. $conf = new Conf();
  44. // Force $_REQUEST["logtohtml"]
  45. $_REQUEST["logtohtml"] = 1;
  46. // Correction PHP_SELF (ex pour apache via caudium) car PHP_SELF doit valoir URL relative
  47. // et non path absolu.
  48. if (isset($_SERVER["DOCUMENT_URI"]) && $_SERVER["DOCUMENT_URI"]) {
  49. $_SERVER["PHP_SELF"] = $_SERVER["DOCUMENT_URI"];
  50. }
  51. $includeconferror = '';
  52. // Define vars
  53. $conffiletoshowshort = "conf.php";
  54. // Define localization of conf file
  55. $conffile = "../conf/conf.php";
  56. $conffiletoshow = "htdocs/conf/conf.php";
  57. // For debian/redhat like systems
  58. //$conffile = "/etc/dolibarr/conf.php";
  59. //$conffiletoshow = "/etc/dolibarr/conf.php";
  60. $short_options = "c:h";
  61. $long_options = array(
  62. "config:",
  63. "help",
  64. );
  65. /**
  66. * Print the usage when executing scripts from install/.
  67. *
  68. * Print the help text exposing the available options when executing
  69. * update or install script (ie. from htdocs/install/) from CLI with
  70. * the `php` executable. This function does not `exit` the program and
  71. * the caller should then call `exit` themselves since they should
  72. * determine whether it was an error or not.
  73. *
  74. * @param string $program the script that was originally run
  75. * @param string $header the message to signal to the user
  76. * @return void
  77. */
  78. function usage($program, $header)
  79. {
  80. echo $header."\n";
  81. echo " php ".$program." [options] [script options]\n";
  82. echo "\n";
  83. echo "Script syntax when using step2.php:\n";
  84. echo " php ".$program." [options] [action] [selectlang]\n";
  85. echo "\n";
  86. echo " action:\n";
  87. echo " Specify the action to execute for the file among the following ones.\n";
  88. echo " - set: Create tables, keys, functions and data for the instance.\n";
  89. echo "\n";
  90. echo " selectlang:\n";
  91. echo " Setup the default lang to use, default to 'auto'.\n";
  92. echo "\n";
  93. echo "Script syntax when using upgrade.php:\n";
  94. echo " php ".$program." [options] previous_version new_version [script options]\n";
  95. echo "\n";
  96. echo " dirmodule:\n";
  97. echo " Specify dirmodule to provide a path for an external module\n";
  98. echo " so the migration is done using a script from a module.\n";
  99. echo "\n";
  100. echo " ignoredbversion:\n";
  101. echo " Allow to run migration even if database version does\n";
  102. echo " not match start version of migration.\n";
  103. echo "\n";
  104. echo "Script syntax when using upgrade2.php:\n";
  105. echo " php ".$program." [options] previous_version new_version [module list]\n";
  106. echo "\n";
  107. echo " MAIN_MODULE_NAME1,MAIN_MODULE_NAME2:\n";
  108. echo " Specify a list of module-name to enable, in upper case, with MAIN_MODULE_ prefix, joined by comma.\n";
  109. echo "\n";
  110. echo "Options:\n";
  111. echo " -c, --config <filename>:\n";
  112. echo " Provide a different conf.php file to use.\n";
  113. echo "\n";
  114. echo " -h, --help:\n";
  115. echo " Display this help message.\n";
  116. }
  117. if (php_sapi_name() === "cli" && (float) PHP_VERSION > 7.0) {
  118. $rest_index = 0;
  119. $opts = getopt($short_options, $long_options, $rest_index);
  120. foreach ($opts as $opt => $arg) {
  121. switch ($opt) {
  122. case 'c':
  123. case 'config':
  124. $conffile = $arg;
  125. $conffiletoshow = $arg;
  126. break;
  127. case 'h':
  128. case 'help':
  129. usage($argv[0], "Usage:");
  130. exit(0);
  131. }
  132. }
  133. // Parse the arguments to find the options.
  134. $args_options = array_filter(array_slice($argv, 0, $rest_index), function ($arg) {
  135. return strlen($arg) >= 2 && $arg[0] == '-';
  136. });
  137. $parsed_options = array_map(function ($arg) {
  138. if (strlen($arg) > 1) {
  139. return "--" . $arg;
  140. }
  141. return "-" . $arg;
  142. }, array_keys($opts));
  143. // Find options (dash-prefixed) that were not parsed.
  144. $unknown_options = array_diff($args_options, $parsed_options);
  145. // In the following test, only dash-prefixed arguments will trigger an
  146. // error, given that scripts options can allow a variable number of
  147. // additional non-prefixed argument and we mostly want to check for
  148. // typo right now.
  149. if (count($unknown_options) > 0) {
  150. echo "Unknown option: ".array_values($unknown_options)[0]."\n";
  151. usage($argv[0], "Usage:");
  152. exit(1);
  153. }
  154. // Tricky argument list hack, should be removed someday.
  155. // Reset argv to remove the argument that were parsed. This is needed
  156. // currently because some install code, like in upgrade.php, are using
  157. // $argv[] directly with fixed index to fetch some arguments.
  158. $argv = array_merge(array($argv[0]), array_slice($argv, $rest_index));
  159. $argc = count($argv);
  160. }
  161. // Load conf file if it is already defined
  162. if (!defined('DONOTLOADCONF') && file_exists($conffile) && filesize($conffile) > 8) { // Test on filesize is to ensure that conf file is more that an empty template with just <?php in first line
  163. $result = include_once $conffile; // Load conf file
  164. if ($result) {
  165. if (empty($dolibarr_main_db_type)) {
  166. $dolibarr_main_db_type = 'mysqli'; // For backward compatibility
  167. }
  168. //Mysql driver support has been removed in favor of mysqli
  169. if ($dolibarr_main_db_type == 'mysql') {
  170. $dolibarr_main_db_type = 'mysqli';
  171. }
  172. if (empty($dolibarr_main_db_port) && ($dolibarr_main_db_type == 'mysqli')) {
  173. $dolibarr_main_db_port = '3306'; // For backward compatibility
  174. }
  175. // Clean parameters
  176. $dolibarr_main_data_root = isset($dolibarr_main_data_root) ? trim($dolibarr_main_data_root) : DOL_DOCUMENT_ROOT.'/../documents';
  177. $dolibarr_main_url_root = isset($dolibarr_main_url_root) ? trim($dolibarr_main_url_root) : '';
  178. $dolibarr_main_url_root_alt = isset($dolibarr_main_url_root_alt) ? trim($dolibarr_main_url_root_alt) : '';
  179. $dolibarr_main_document_root = isset($dolibarr_main_document_root) ? trim($dolibarr_main_document_root) : '';
  180. $dolibarr_main_document_root_alt = isset($dolibarr_main_document_root_alt) ? trim($dolibarr_main_document_root_alt) : '';
  181. // Remove last / or \ on directories or url value
  182. if (!empty($dolibarr_main_document_root) && !preg_match('/^[\\/]+$/', $dolibarr_main_document_root)) {
  183. $dolibarr_main_document_root = preg_replace('/[\\/]+$/', '', $dolibarr_main_document_root);
  184. }
  185. if (!empty($dolibarr_main_url_root) && !preg_match('/^[\\/]+$/', $dolibarr_main_url_root)) {
  186. $dolibarr_main_url_root = preg_replace('/[\\/]+$/', '', $dolibarr_main_url_root);
  187. }
  188. if (!empty($dolibarr_main_data_root) && !preg_match('/^[\\/]+$/', $dolibarr_main_data_root)) {
  189. $dolibarr_main_data_root = preg_replace('/[\\/]+$/', '', $dolibarr_main_data_root);
  190. }
  191. if (!empty($dolibarr_main_document_root_alt) && !preg_match('/^[\\/]+$/', $dolibarr_main_document_root_alt)) {
  192. $dolibarr_main_document_root_alt = preg_replace('/[\\/]+$/', '', $dolibarr_main_document_root_alt);
  193. }
  194. if (!empty($dolibarr_main_url_root_alt) && !preg_match('/^[\\/]+$/', $dolibarr_main_url_root_alt)) {
  195. $dolibarr_main_url_root_alt = preg_replace('/[\\/]+$/', '', $dolibarr_main_url_root_alt);
  196. }
  197. // Create conf object
  198. if (!empty($dolibarr_main_document_root)) {
  199. $result = conf($dolibarr_main_document_root);
  200. }
  201. // Load database driver
  202. if ($result) {
  203. if (!empty($dolibarr_main_document_root) && !empty($dolibarr_main_db_type)) {
  204. $result = include_once $dolibarr_main_document_root."/core/db/".$dolibarr_main_db_type.'.class.php';
  205. if (!$result) {
  206. $includeconferror = 'ErrorBadValueForDolibarrMainDBType';
  207. }
  208. }
  209. } else {
  210. $includeconferror = 'ErrorBadValueForDolibarrMainDocumentRoot';
  211. }
  212. } else {
  213. $includeconferror = 'ErrorBadFormatForConfFile';
  214. }
  215. }
  216. $conf->global->MAIN_ENABLE_LOG_TO_HTML = 1;
  217. // Define prefix
  218. if (!isset($dolibarr_main_db_prefix) || !$dolibarr_main_db_prefix) {
  219. $dolibarr_main_db_prefix = 'llx_';
  220. }
  221. define('MAIN_DB_PREFIX', (isset($dolibarr_main_db_prefix) ? $dolibarr_main_db_prefix : ''));
  222. define('DOL_CLASS_PATH', 'class/'); // Filsystem path to class dir
  223. define('DOL_DATA_ROOT', (isset($dolibarr_main_data_root) ? $dolibarr_main_data_root : DOL_DOCUMENT_ROOT.'/../documents'));
  224. define('DOL_MAIN_URL_ROOT', (isset($dolibarr_main_url_root) ? $dolibarr_main_url_root : '')); // URL relative root
  225. $uri = preg_replace('/^http(s?):\/\//i', '', constant('DOL_MAIN_URL_ROOT')); // $uri contains url without http*
  226. $suburi = strstr($uri, '/'); // $suburi contains url without domain
  227. if ($suburi == '/') {
  228. $suburi = ''; // If $suburi is /, it is now ''
  229. }
  230. define('DOL_URL_ROOT', $suburi); // URL relative root ('', '/dolibarr', ...)
  231. if (empty($conf->file->character_set_client)) {
  232. $conf->file->character_set_client = "utf-8";
  233. }
  234. if (empty($conf->db->character_set)) {
  235. $conf->db->character_set = 'utf8';
  236. }
  237. if (empty($conf->db->dolibarr_main_db_collation)) {
  238. $conf->db->dolibarr_main_db_collation = 'utf8_unicode_ci';
  239. }
  240. if (empty($conf->db->dolibarr_main_db_encryption)) {
  241. $conf->db->dolibarr_main_db_encryption = 0;
  242. }
  243. if (empty($conf->db->dolibarr_main_db_cryptkey)) {
  244. $conf->db->dolibarr_main_db_cryptkey = '';
  245. }
  246. if (empty($conf->db->user)) {
  247. $conf->db->user = '';
  248. }
  249. // Define array of document root directories
  250. $conf->file->dol_document_root = array(DOL_DOCUMENT_ROOT);
  251. if (!empty($dolibarr_main_document_root_alt)) {
  252. // dolibarr_main_document_root_alt contains several directories
  253. $values = preg_split('/[;,]/', $dolibarr_main_document_root_alt);
  254. foreach ($values as $value) {
  255. $conf->file->dol_document_root[] = $value;
  256. }
  257. }
  258. // Check install.lock (for both install and upgrade)
  259. $lockfile = DOL_DATA_ROOT.'/install.lock'; // To lock all /install pages
  260. $lockfile2 = DOL_DOCUMENT_ROOT.'/install.lock'; // To lock all /install pages (recommended)
  261. $upgradeunlockfile = DOL_DATA_ROOT.'/upgrade.unlock'; // To unlock upgrade process
  262. $upgradeunlockfile2 = DOL_DOCUMENT_ROOT.'/upgrade.unlock'; // To unlock upgrade process
  263. if (constant('DOL_DATA_ROOT') === null) {
  264. // We don't have a configuration file yet
  265. // Try to detect any lockfile in the default documents path
  266. $lockfile = '../../documents/install.lock';
  267. $upgradeunlockfile = '../../documents/upgrade.unlock';
  268. }
  269. $islocked=false;
  270. if (@file_exists($lockfile) || @file_exists($lockfile2)) {
  271. if (!defined('ALLOWED_IF_UPGRADE_UNLOCK_FOUND') || (! @file_exists($upgradeunlockfile) && ! @file_exists($upgradeunlockfile2))) {
  272. // If this is a dangerous install page (ALLOWED_IF_UPGRADE_UNLOCK_FOUND not defined) or
  273. // if there is no upgrade unlock files, we lock the pages.
  274. $islocked = true;
  275. }
  276. }
  277. if ($islocked) { // Pages are locked
  278. if (!isset($langs) || !is_object($langs)) {
  279. $langs = new Translate('..', $conf);
  280. $langs->setDefaultLang('auto');
  281. }
  282. $langs->load("install");
  283. header("X-Content-Type-Options: nosniff");
  284. header("X-Frame-Options: SAMEORIGIN"); // Frames allowed only if on same domain (stop some XSS attacks)
  285. if (GETPOST('action') != 'upgrade') {
  286. print $langs->trans("YouTryInstallDisabledByFileLock").'<br>';
  287. } else {
  288. print $langs->trans("YouTryUpgradeDisabledByMissingFileUnLock").'<br>';
  289. }
  290. if (!empty($dolibarr_main_url_root)) {
  291. if (GETPOST('action') != 'upgrade' && (!file_exists($conffile) || !isset($dolibarr_main_url_root))) {
  292. print $langs->trans("ClickOnLinkOrRemoveManualy").'<br>';
  293. } else {
  294. print $langs->trans("ClickOnLinkOrCreateUnlockFileManualy").'<br>';
  295. }
  296. print '<a href="'.$dolibarr_main_url_root.'/admin/index.php?mainmenu=home&leftmenu=setup'.(GETPOSTISSET("login") ? '&username='.urlencode(GETPOST("login")) : '').'">';
  297. print $langs->trans("ClickHereToGoToApp");
  298. print '</a>';
  299. } else {
  300. print 'If you always reach this page, you must remove the install.lock file manually.<br>';
  301. }
  302. exit;
  303. }
  304. // Force usage of log file for install and upgrades
  305. $conf->modules['syslog'] = 'syslog';
  306. $conf->global->SYSLOG_LEVEL = constant('LOG_DEBUG');
  307. if (!defined('SYSLOG_HANDLERS')) {
  308. define('SYSLOG_HANDLERS', '["mod_syslog_file"]');
  309. }
  310. if (!defined('SYSLOG_FILE')) { // To avoid warning on systems with constant already defined
  311. if (@is_writable('/tmp')) {
  312. define('SYSLOG_FILE', '/tmp/dolibarr_install.log');
  313. } elseif (!empty($_ENV["TMP"]) && @is_writable($_ENV["TMP"])) {
  314. define('SYSLOG_FILE', $_ENV["TMP"].'/dolibarr_install.log');
  315. } elseif (!empty($_ENV["TEMP"]) && @is_writable($_ENV["TEMP"])) {
  316. define('SYSLOG_FILE', $_ENV["TEMP"].'/dolibarr_install.log');
  317. } elseif (@is_writable('../../../../') && @file_exists('../../../../startdoliwamp.bat')) {
  318. define('SYSLOG_FILE', '../../../../dolibarr_install.log'); // For DoliWamp
  319. } elseif (@is_writable('../../')) {
  320. define('SYSLOG_FILE', '../../dolibarr_install.log'); // For others
  321. }
  322. //print 'SYSLOG_FILE='.SYSLOG_FILE;exit;
  323. }
  324. if (defined('SYSLOG_FILE')) {
  325. $conf->global->SYSLOG_FILE = constant('SYSLOG_FILE');
  326. }
  327. if (!defined('SYSLOG_FILE_NO_ERROR')) {
  328. define('SYSLOG_FILE_NO_ERROR', 1);
  329. }
  330. // We init log handler for install
  331. $handlers = array('mod_syslog_file');
  332. foreach ($handlers as $handler) {
  333. $file = DOL_DOCUMENT_ROOT.'/core/modules/syslog/'.$handler.'.php';
  334. if (!file_exists($file)) {
  335. throw new Exception('Missing log handler file '.$handler.'.php');
  336. }
  337. require_once $file;
  338. $loghandlerinstance = new $handler();
  339. if (!$loghandlerinstance instanceof LogHandlerInterface) {
  340. throw new Exception('Log handler does not extend LogHandlerInterface');
  341. }
  342. if (empty($conf->loghandlers[$handler])) {
  343. $conf->loghandlers[$handler] = $loghandlerinstance;
  344. }
  345. }
  346. // Define object $langs
  347. $langs = new Translate('..', $conf);
  348. if (GETPOST('lang', 'aZ09')) {
  349. $langs->setDefaultLang(GETPOST('lang', 'aZ09'));
  350. } else {
  351. $langs->setDefaultLang('auto');
  352. }
  353. /**
  354. * Load conf file (file must exists)
  355. *
  356. * @param string $dolibarr_main_document_root Root directory of Dolibarr bin files
  357. * @return int Return integer <0 if KO, >0 if OK
  358. */
  359. function conf($dolibarr_main_document_root)
  360. {
  361. global $conf;
  362. global $dolibarr_main_db_type;
  363. global $dolibarr_main_db_host;
  364. global $dolibarr_main_db_port;
  365. global $dolibarr_main_db_name;
  366. global $dolibarr_main_db_user;
  367. global $dolibarr_main_db_pass;
  368. global $character_set_client;
  369. global $dolibarr_main_instance_unique_id;
  370. global $dolibarr_main_cookie_cryptkey;
  371. $return = include_once $dolibarr_main_document_root.'/core/class/conf.class.php';
  372. if (!$return) {
  373. return -1;
  374. }
  375. $conf = new Conf();
  376. $conf->db->type = trim($dolibarr_main_db_type);
  377. $conf->db->host = trim($dolibarr_main_db_host);
  378. $conf->db->port = trim($dolibarr_main_db_port);
  379. $conf->db->name = trim($dolibarr_main_db_name);
  380. $conf->db->user = trim($dolibarr_main_db_user);
  381. $conf->db->pass = (empty($dolibarr_main_db_pass) ? '' : trim($dolibarr_main_db_pass));
  382. // Mysql driver support has been removed in favor of mysqli
  383. if ($conf->db->type == 'mysql') {
  384. $conf->db->type = 'mysqli';
  385. }
  386. if (empty($character_set_client)) {
  387. $character_set_client = "UTF-8";
  388. }
  389. $conf->file->character_set_client = strtoupper($character_set_client);
  390. // Unique id of instance
  391. $conf->file->instance_unique_id = empty($dolibarr_main_instance_unique_id) ? (empty($dolibarr_main_cookie_cryptkey) ? '' : $dolibarr_main_cookie_cryptkey) : $dolibarr_main_instance_unique_id;
  392. if (empty($dolibarr_main_db_character_set)) {
  393. $dolibarr_main_db_character_set = ($conf->db->type == 'mysqli' ? 'utf8' : '');
  394. }
  395. $conf->db->character_set = $dolibarr_main_db_character_set;
  396. if (empty($dolibarr_main_db_collation)) {
  397. $dolibarr_main_db_collation = ($conf->db->type == 'mysqli' ? 'utf8_unicode_ci' : '');
  398. }
  399. $conf->db->dolibarr_main_db_collation = $dolibarr_main_db_collation;
  400. if (empty($dolibarr_main_db_encryption)) {
  401. $dolibarr_main_db_encryption = 0;
  402. }
  403. $conf->db->dolibarr_main_db_encryption = $dolibarr_main_db_encryption;
  404. if (empty($dolibarr_main_db_cryptkey)) {
  405. $dolibarr_main_db_cryptkey = '';
  406. }
  407. $conf->db->dolibarr_main_db_cryptkey = $dolibarr_main_db_cryptkey;
  408. // Force usage of log file for install and upgrades
  409. $conf->modules['syslog'] = 'syslog';
  410. $conf->global->SYSLOG_LEVEL = constant('LOG_DEBUG');
  411. if (!defined('SYSLOG_HANDLERS')) {
  412. define('SYSLOG_HANDLERS', '["mod_syslog_file"]');
  413. }
  414. if (!defined('SYSLOG_FILE')) { // To avoid warning on systems with constant already defined
  415. if (@is_writable('/tmp')) {
  416. define('SYSLOG_FILE', '/tmp/dolibarr_install.log');
  417. } elseif (!empty($_ENV["TMP"]) && @is_writable($_ENV["TMP"])) {
  418. define('SYSLOG_FILE', $_ENV["TMP"].'/dolibarr_install.log');
  419. } elseif (!empty($_ENV["TEMP"]) && @is_writable($_ENV["TEMP"])) {
  420. define('SYSLOG_FILE', $_ENV["TEMP"].'/dolibarr_install.log');
  421. } elseif (@is_writable('../../../../') && @file_exists('../../../../startdoliwamp.bat')) {
  422. define('SYSLOG_FILE', '../../../../dolibarr_install.log'); // For DoliWamp
  423. } elseif (@is_writable('../../')) {
  424. define('SYSLOG_FILE', '../../dolibarr_install.log'); // For others
  425. }
  426. //print 'SYSLOG_FILE='.SYSLOG_FILE;exit;
  427. }
  428. if (defined('SYSLOG_FILE')) {
  429. $conf->global->SYSLOG_FILE = constant('SYSLOG_FILE');
  430. }
  431. if (!defined('SYSLOG_FILE_NO_ERROR')) {
  432. define('SYSLOG_FILE_NO_ERROR', 1);
  433. }
  434. // We init log handler for install
  435. $handlers = array('mod_syslog_file');
  436. foreach ($handlers as $handler) {
  437. $file = DOL_DOCUMENT_ROOT.'/core/modules/syslog/'.$handler.'.php';
  438. if (!file_exists($file)) {
  439. throw new Exception('Missing log handler file '.$handler.'.php');
  440. }
  441. require_once $file;
  442. $loghandlerinstance = new $handler();
  443. if (!$loghandlerinstance instanceof LogHandlerInterface) {
  444. throw new Exception('Log handler does not extend LogHandlerInterface');
  445. }
  446. if (empty($conf->loghandlers[$handler])) {
  447. $conf->loghandlers[$handler] = $loghandlerinstance;
  448. }
  449. }
  450. return 1;
  451. }
  452. /**
  453. * Show HTML header of install pages
  454. *
  455. * @param string $subtitle Title
  456. * @param string $next Next
  457. * @param string $action Action code ('set' or 'upgrade')
  458. * @param string $param Param
  459. * @param string $forcejqueryurl Set jquery relative URL (must end with / if defined)
  460. * @param string $csstable Css for table
  461. * @return void
  462. */
  463. function pHeader($subtitle, $next, $action = 'set', $param = '', $forcejqueryurl = '', $csstable = 'main-inside')
  464. {
  465. global $conf;
  466. global $langs;
  467. $langs->load("main");
  468. $langs->load("admin");
  469. $langs->load("install");
  470. $jquerytheme = 'base';
  471. if ($forcejqueryurl) {
  472. $jQueryCustomPath = $forcejqueryurl;
  473. $jQueryUiCustomPath = $forcejqueryurl;
  474. } else {
  475. $jQueryCustomPath = (defined('JS_JQUERY') && constant('JS_JQUERY')) ? JS_JQUERY : false;
  476. $jQueryUiCustomPath = (defined('JS_JQUERY_UI') && constant('JS_JQUERY_UI')) ? JS_JQUERY_UI : false;
  477. }
  478. // We force the content charset
  479. header("Content-type: text/html; charset=".$conf->file->character_set_client);
  480. header("X-Content-Type-Options: nosniff");
  481. header("X-Frame-Options: SAMEORIGIN"); // Frames allowed only if on same domain (stop some XSS attacks)
  482. print '<!DOCTYPE HTML>'."\n";
  483. print '<html>'."\n";
  484. print '<head>'."\n";
  485. print '<meta charset="'.$conf->file->character_set_client.'">'."\n";
  486. print '<meta name="viewport" content="width=device-width, initial-scale=1.0">'."\n";
  487. print '<meta name="generator" content="Dolibarr installer">'."\n";
  488. print '<link rel="stylesheet" type="text/css" href="default.css">'."\n";
  489. print '<!-- Includes CSS for JQuery -->'."\n";
  490. if ($jQueryUiCustomPath) {
  491. print '<link rel="stylesheet" type="text/css" href="'.$jQueryUiCustomPath.'css/'.$jquerytheme.'/jquery-ui.min.css" />'."\n"; // JQuery
  492. } else {
  493. print '<link rel="stylesheet" type="text/css" href="../includes/jquery/css/'.$jquerytheme.'/jquery-ui.min.css" />'."\n"; // JQuery
  494. }
  495. print '<!-- Includes JS for JQuery -->'."\n";
  496. if ($jQueryCustomPath) {
  497. print '<script type="text/javascript" src="'.$jQueryCustomPath.'jquery.min.js"></script>'."\n";
  498. } else {
  499. print '<script type="text/javascript" src="../includes/jquery/js/jquery.min.js"></script>'."\n";
  500. }
  501. if ($jQueryUiCustomPath) {
  502. print '<script type="text/javascript" src="'.$jQueryUiCustomPath.'jquery-ui.min.js"></script>'."\n";
  503. } else {
  504. print '<script type="text/javascript" src="../includes/jquery/js/jquery-ui.min.js"></script>'."\n";
  505. }
  506. print '<title>'.$langs->trans("DolibarrSetup").'</title>'."\n";
  507. print '</head>'."\n";
  508. print '<body>'."\n";
  509. print '<div class="divlogoinstall" style="text-align:center">';
  510. print '<img class="imglogoinstall" src="../theme/dolibarr_logo.svg" alt="Dolibarr logo" width="300px"><br>';
  511. print DOL_VERSION;
  512. print '</div><br>';
  513. print '<span class="titre">'.$langs->trans("DolibarrSetup");
  514. if ($subtitle) {
  515. print ' - '.$subtitle;
  516. }
  517. print '</span>'."\n";
  518. print '<form name="forminstall" style="width: 100%" action="'.$next.'.php'.($param ? '?'.$param : '').'" method="POST"';
  519. if ($next == 'step5') {
  520. print ' autocomplete="off"';
  521. }
  522. print '>'."\n";
  523. print '<input type="hidden" name="testpost" value="ok">'."\n";
  524. print '<input type="hidden" name="action" value="'.$action.'">'."\n";
  525. print '<table class="main" width="100%"><tr><td>'."\n";
  526. print '<table class="'.$csstable.'" width="100%"><tr><td>'."\n";
  527. }
  528. /**
  529. * Print HTML footer of install pages
  530. *
  531. * @param integer $nonext 1=No button "Next step", 2=Show button but disabled with a link to enable
  532. * @param string $setuplang Language code
  533. * @param string $jscheckfunction Add a javascript check function
  534. * @param integer $withpleasewait Add also please wait tags
  535. * @param string $morehtml Add more HTML content
  536. * @return void
  537. */
  538. function pFooter($nonext = 0, $setuplang = '', $jscheckfunction = '', $withpleasewait = 0, $morehtml = '')
  539. {
  540. global $conf, $langs;
  541. $langs->loadLangs(array("main", "other", "admin"));
  542. print '</td></tr></table>'."\n";
  543. print '</td></tr></table>'."\n";
  544. print '<!-- pFooter -->'."\n";
  545. print $morehtml;
  546. if (!$nonext || ($nonext == '2')) {
  547. print '<div class="nextbutton" id="nextbutton">';
  548. if ($nonext == '2') {
  549. print '<span class="warning">';
  550. print $langs->trans("ErrorFoundDuringMigration", isset($_SERVER["REQUEST_URI"]) ? $_SERVER["REQUEST_URI"].'&ignoreerrors=1' : '');
  551. print '</span>';
  552. print '<br><br>';
  553. }
  554. print '<input type="submit" '.($nonext == '2' ? 'disabled="disabled" ' : '').'value="'.$langs->trans("NextStep").' ->"';
  555. if ($jscheckfunction) {
  556. print ' onClick="return '.$jscheckfunction.'();"';
  557. }
  558. print '></div>';
  559. if ($withpleasewait) {
  560. print '<div style="visibility: hidden;" class="pleasewait" id="pleasewait"><br>'.$langs->trans("NextStepMightLastALongTime").'<br><br><div class="blinkwait">'.$langs->trans("PleaseBePatient").'</div></div>';
  561. }
  562. }
  563. if ($setuplang) {
  564. print '<input type="hidden" name="selectlang" value="'.dol_escape_htmltag($setuplang).'">';
  565. }
  566. print '</form><br>'."\n";
  567. // If there is some logs in buffer to show
  568. if (isset($conf->logbuffer) && count($conf->logbuffer)) {
  569. print "\n";
  570. print "<!-- Start of log output\n";
  571. //print '<div class="hidden">'."\n";
  572. foreach ($conf->logbuffer as $logline) {
  573. print $logline."<br>\n";
  574. }
  575. //print '</div>'."\n";
  576. print "End of log output -->\n";
  577. print "\n";
  578. }
  579. print '</body>'."\n";
  580. print '</html>'."\n";
  581. }
  582. /**
  583. * Log function for install pages
  584. *
  585. * @param string $message Message
  586. * @param int $level Level of log
  587. * @return void
  588. */
  589. function dolibarr_install_syslog($message, $level = LOG_DEBUG)
  590. {
  591. if (!defined('LOG_DEBUG')) {
  592. define('LOG_DEBUG', 6);
  593. }
  594. dol_syslog($message, $level);
  595. }
  596. /**
  597. * Automatically detect Dolibarr's main document root
  598. *
  599. * @return string
  600. */
  601. function detect_dolibarr_main_document_root()
  602. {
  603. // If PHP is in CGI mode, SCRIPT_FILENAME is PHP's path.
  604. // Since that's not what we want, we suggest $_SERVER["DOCUMENT_ROOT"]
  605. if ($_SERVER["SCRIPT_FILENAME"] == 'php' || preg_match('/[\\/]php$/i', $_SERVER["SCRIPT_FILENAME"]) || preg_match('/php\.exe$/i', $_SERVER["SCRIPT_FILENAME"])) {
  606. $dolibarr_main_document_root = $_SERVER["DOCUMENT_ROOT"];
  607. if (!preg_match('/[\\/]dolibarr[\\/]htdocs$/i', $dolibarr_main_document_root)) {
  608. $dolibarr_main_document_root .= "/dolibarr/htdocs";
  609. }
  610. } else {
  611. // We assume /install to be under /htdocs, so we get the parent directory of the current directory
  612. $dolibarr_main_document_root = dirname(dirname($_SERVER["SCRIPT_FILENAME"]));
  613. }
  614. return $dolibarr_main_document_root;
  615. }
  616. /**
  617. * Automatically detect Dolibarr's main data root
  618. *
  619. * @param string $dolibarr_main_document_root Current main document root
  620. * @return string
  621. */
  622. function detect_dolibarr_main_data_root($dolibarr_main_document_root)
  623. {
  624. $dolibarr_main_data_root = preg_replace("/\/htdocs$/", "", $dolibarr_main_document_root);
  625. $dolibarr_main_data_root .= "/documents";
  626. return $dolibarr_main_data_root;
  627. }
  628. /**
  629. * Automatically detect Dolibarr's main URL root
  630. *
  631. * @return string
  632. */
  633. function detect_dolibarr_main_url_root()
  634. {
  635. // If defined (Ie: Apache with Linux)
  636. if (isset($_SERVER["SCRIPT_URI"])) {
  637. $dolibarr_main_url_root = $_SERVER["SCRIPT_URI"];
  638. } elseif (isset($_SERVER["SERVER_URL"]) && isset($_SERVER["DOCUMENT_URI"])) {
  639. // If defined (Ie: Apache with Caudium)
  640. $dolibarr_main_url_root = $_SERVER["SERVER_URL"].$_SERVER["DOCUMENT_URI"];
  641. } else {
  642. // If SCRIPT_URI, SERVER_URL, DOCUMENT_URI not defined (Ie: Apache 2.0.44 for Windows)
  643. $proto = ((!empty($_SERVER["HTTPS"]) && $_SERVER["HTTPS"] == 'on') || (!empty($_SERVER['SERVER_PORT']) && $_SERVER['SERVER_PORT'] == 443)) ? 'https' : 'http';
  644. if (!empty($_SERVER["HTTP_HOST"])) {
  645. $serverport = $_SERVER["HTTP_HOST"];
  646. } elseif (!empty($_SERVER["SERVER_NAME"])) {
  647. $serverport = $_SERVER["SERVER_NAME"];
  648. } else {
  649. $serverport = 'localhost';
  650. }
  651. $dolibarr_main_url_root = $proto."://".$serverport.$_SERVER["SCRIPT_NAME"];
  652. }
  653. // Clean proposed URL
  654. // We assume /install to be under /htdocs, so we get the parent path of the current URL
  655. $dolibarr_main_url_root = dirname(dirname($dolibarr_main_url_root));
  656. return $dolibarr_main_url_root;
  657. }
  658. /**
  659. * Replaces automatic database login by actual value
  660. *
  661. * @param string $force_install_databaserootlogin Login
  662. * @return string
  663. */
  664. function parse_database_login($force_install_databaserootlogin)
  665. {
  666. return preg_replace('/__SUPERUSERLOGIN__/', 'root', $force_install_databaserootlogin);
  667. }
  668. /**
  669. * Replaces automatic database password by actual value
  670. *
  671. * @param string $force_install_databaserootpass Password
  672. * @return string
  673. */
  674. function parse_database_pass($force_install_databaserootpass)
  675. {
  676. return preg_replace('/__SUPERUSERPASSWORD__/', '', $force_install_databaserootpass);
  677. }