PageRenderTime 48ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

/htdocs/install/etape2.php

http://github.com/Dolibarr/dolibarr
PHP | 611 lines | 446 code | 68 blank | 97 comment | 117 complexity | 9795308d800ea8f21d97d2457c77cb75 MD5 | raw file
Possible License(s): GPL-2.0, AGPL-3.0, LGPL-2.0, CC-BY-SA-4.0, BSD-3-Clause, MPL-2.0-no-copyleft-exception, LGPL-3.0, GPL-3.0, LGPL-2.1, MIT
  1. <?php
  2. /* Copyright (C) 2004 Rodolphe Quiedeville <rodolphe@quiedeville.org>
  3. * Copyright (C) 2004-2010 Laurent Destailleur <eldy@users.sourceforge.net>
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation; either version 2 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  17. */
  18. /**
  19. * \file htdocs/install/etape2.php
  20. * \ingroup install
  21. * \brief Create tables, primary keys, foreign keys, indexes and functions into database and then load reference data
  22. */
  23. include("./inc.php");
  24. require_once($dolibarr_main_document_root."/core/class/conf.class.php");
  25. require_once($dolibarr_main_document_root."/core/lib/admin.lib.php");
  26. $etape = 2;
  27. $ok = 0;
  28. // Cette page peut etre longue. On augmente le delai autorise.
  29. // Ne fonctionne que si on est pas en safe_mode.
  30. $err=error_reporting();
  31. error_reporting(0); // Disable all errors
  32. //error_reporting(E_ALL);
  33. @set_time_limit(300); // Need more than 240 on Windows 7/64
  34. error_reporting($err);
  35. $action=GETPOST('action');
  36. $setuplang=isset($_POST["selectlang"])?$_POST["selectlang"]:(isset($_GET["selectlang"])?$_GET["selectlang"]:'auto');
  37. $langs->setDefaultLang($setuplang);
  38. $langs->load("admin");
  39. $langs->load("install");
  40. $choix=0;
  41. if ($dolibarr_main_db_type == "mysql") $choix=1;
  42. if ($dolibarr_main_db_type == "mysqli") $choix=1;
  43. if ($dolibarr_main_db_type == "pgsql") $choix=2;
  44. if ($dolibarr_main_db_type == "mssql") $choix=3;
  45. //if (empty($choix)) dol_print_error('','Database type '.$dolibarr_main_db_type.' not supported into etape2.php page');
  46. // Now we load forced value from install.forced.php file.
  47. $useforcedwizard=false;
  48. $forcedfile="./install.forced.php";
  49. if ($conffile == "/etc/dolibarr/conf.php") $forcedfile="/etc/dolibarr/install.forced.php";
  50. if (@file_exists($forcedfile)) { $useforcedwizard=true; include_once($forcedfile); }
  51. dolibarr_install_syslog("--- etape2: Entering etape2.php page");
  52. /*
  53. * View
  54. */
  55. pHeader($langs->trans("CreateDatabaseObjects"),"etape4");
  56. // Test if we can run a first install process
  57. if (! is_writable($conffile))
  58. {
  59. print $langs->trans("ConfFileIsNotWritable",$conffiletoshow);
  60. pFooter(1,$setuplang,'jscheckparam');
  61. exit;
  62. }
  63. if ($action == "set")
  64. {
  65. print '<h3>'.$langs->trans("Database").'</h3>';
  66. print '<table cellspacing="0" style="padding: 4px 4px 4px 0px" border="0" width="100%">';
  67. $error=0;
  68. $db=getDoliDBInstance($conf->db->type,$conf->db->host,$conf->db->user,$conf->db->pass,$conf->db->name,$conf->db->port);
  69. if ($db->connected == 1)
  70. {
  71. print "<tr><td>";
  72. print $langs->trans("ServerConnection")." : ".$conf->db->host."</td><td>".$langs->trans("OK")."</td></tr>";
  73. $ok = 1 ;
  74. }
  75. else
  76. {
  77. print "<tr><td>Failed to connect to server : ".$conf->db->host."</td><td>".$langs->trans("Error")."</td></tr>";
  78. }
  79. if ($ok)
  80. {
  81. if($db->database_selected == 1)
  82. {
  83. dolibarr_install_syslog("etape2: Connexion successful to database : ".$conf->db->name);
  84. }
  85. else
  86. {
  87. dolibarr_install_syslog("etape2: Connexion failed to database : ".$conf->db->name);
  88. print "<tr><td>Failed to select database ".$conf->db->name."</td><td>".$langs->trans("Error")."</td></tr>";
  89. $ok = 0 ;
  90. }
  91. }
  92. // Affiche version
  93. if ($ok)
  94. {
  95. $version=$db->getVersion();
  96. $versionarray=$db->getVersionArray();
  97. print '<tr><td>'.$langs->trans("DatabaseVersion").'</td>';
  98. print '<td>'.$version.'</td></tr>';
  99. //print '<td align="right">'.join('.',$versionarray).'</td></tr>';
  100. print '<tr><td>'.$langs->trans("DatabaseName").'</td>';
  101. print '<td>'.$db->database_name.'</td></tr>';
  102. //print '<td align="right">'.join('.',$versionarray).'</td></tr>';
  103. }
  104. $requestnb=0;
  105. // To disable some code, so you can call step2 with url like
  106. // http://localhost/dolibarrnew/install/etape2.php?action=set&createtables=0&createkeys=0&createfunctions=0&createdata=llx_20_c_departements
  107. $createtables=isset($_GET['createtables'])?GETPOST('createtables'):1;
  108. $createkeys=isset($_GET['createkeys'])?GETPOST('createkeys'):1;
  109. $createfunctions=isset($_GET['createfunctions'])?GETPOST('createfunction'):1;
  110. $createdata=isset($_GET['createdata'])?GETPOST('createdata'):1;
  111. // To say sql requests are escaped for mysql so we need to unescape them
  112. $db->unescapeslashquot=1;
  113. /**************************************************************************************
  114. *
  115. * Chargement fichiers tables/*.sql (non *.key.sql)
  116. * A faire avant les fichiers *.key.sql
  117. *
  118. ***************************************************************************************/
  119. if ($ok && $createtables)
  120. {
  121. // We always choose in mysql directory (Conversion is done by driver to translate SQL syntax)
  122. $dir = "mysql/tables/";
  123. $ok = 0;
  124. $handle=opendir($dir);
  125. dolibarr_install_syslog("Open tables directory ".$dir." handle=".$handle,LOG_DEBUG);
  126. $tablefound = 0;
  127. $tabledata=array();
  128. if (is_resource($handle))
  129. {
  130. while (($file = readdir($handle))!==false)
  131. {
  132. if (preg_match('/\.sql$/i',$file) && preg_match('/^llx_/i',$file) && ! preg_match('/\.key\.sql$/i',$file))
  133. {
  134. $tablefound++;
  135. $tabledata[]=$file;
  136. }
  137. }
  138. closedir($handle);
  139. }
  140. // Sort list of sql files on alphabetical order (load order is important)
  141. sort($tabledata);
  142. foreach($tabledata as $file)
  143. {
  144. $name = substr($file, 0, dol_strlen($file) - 4);
  145. $buffer = '';
  146. $fp = fopen($dir.$file,"r");
  147. if ($fp)
  148. {
  149. while (!feof($fp))
  150. {
  151. $buf = fgets($fp, 4096);
  152. if (substr($buf, 0, 2) <> '--')
  153. {
  154. $buf=preg_replace('/--(.+)*/','',$buf);
  155. $buffer .= $buf;
  156. }
  157. }
  158. fclose($fp);
  159. $buffer=trim($buffer);
  160. if ($conf->db->type == 'mysql' || $conf->db->type == 'mysqli') // For Mysql 5.5+, we must replace type=innodb
  161. {
  162. $buffer=preg_replace('/type=innodb/i','ENGINE=innodb',$buffer);
  163. }
  164. // Replace the prefix tables
  165. if ($dolibarr_main_db_prefix != 'llx_')
  166. {
  167. $buffer=preg_replace('/llx_/i',$dolibarr_main_db_prefix,$buffer);
  168. }
  169. //print "<tr><td>Creation de la table $name/td>";
  170. $requestnb++;
  171. dolibarr_install_syslog("Request: ".$buffer,LOG_DEBUG);
  172. $resql=$db->query($buffer,0,'dml');
  173. if ($resql)
  174. {
  175. // print "<td>OK requete ==== $buffer</td></tr>";
  176. $db->free($resql);
  177. }
  178. else
  179. {
  180. if ($db->errno() == 'DB_ERROR_TABLE_ALREADY_EXISTS' ||
  181. $db->errno() == 'DB_ERROR_TABLE_OR_KEY_ALREADY_EXISTS')
  182. {
  183. //print "<td>Deja existante</td></tr>";
  184. }
  185. else
  186. {
  187. print "<tr><td>".$langs->trans("CreateTableAndPrimaryKey",$name);
  188. print "<br>\n".$langs->trans("Request").' '.$requestnb.' : '.$buffer;
  189. print "\n</td>";
  190. print '<td><font class="error">'.$langs->trans("ErrorSQL")." ".$db->errno()." ".$db->error().'</font></td></tr>';
  191. $error++;
  192. }
  193. }
  194. }
  195. else
  196. {
  197. print "<tr><td>".$langs->trans("CreateTableAndPrimaryKey",$name);
  198. print "</td>";
  199. print '<td><font class="error">'.$langs->trans("Error").' Failed to open file '.$dir.$file.'</td></tr>';
  200. $error++;
  201. dolibarr_install_syslog("Failed to open file ".$dir.$file,LOG_ERR);
  202. }
  203. }
  204. if ($tablefound)
  205. {
  206. if ($error == 0)
  207. {
  208. print '<tr><td>';
  209. print $langs->trans("TablesAndPrimaryKeysCreation").'</td><td>'.$langs->trans("OK").'</td></tr>';
  210. $ok = 1;
  211. }
  212. }
  213. else
  214. {
  215. print '<tr><td>'.$langs->trans("ErrorFailedToFindSomeFiles",$dir).'</td><td><font class="error">'.$langs->trans("Error").'</font></td></tr>';
  216. dolibarr_install_syslog("Failed to find files to create database in directory ".$dir,LOG_ERR);
  217. }
  218. }
  219. /***************************************************************************************
  220. *
  221. * Chargement fichiers tables/*.key.sql
  222. * A faire apres les fichiers *.sql
  223. *
  224. ***************************************************************************************/
  225. if ($ok && $createkeys)
  226. {
  227. // We always choose in mysql directory (Conversion is done by driver to translate SQL syntax)
  228. $dir = "mysql/tables/";
  229. $okkeys = 0;
  230. $handle=opendir($dir);
  231. dolibarr_install_syslog("Open keys directory ".$dir." handle=".$handle,LOG_DEBUG);
  232. $tablefound = 0;
  233. $tabledata=array();
  234. if (is_resource($handle))
  235. {
  236. while (($file = readdir($handle))!==false)
  237. {
  238. if (preg_match('/\.sql$/i',$file) && preg_match('/^llx_/i',$file) && preg_match('/\.key\.sql$/i',$file))
  239. {
  240. $tablefound++;
  241. $tabledata[]=$file;
  242. }
  243. }
  244. closedir($handle);
  245. }
  246. // Sort list of sql files on alphabetical order (load order is important)
  247. sort($tabledata);
  248. foreach($tabledata as $file)
  249. {
  250. $name = substr($file, 0, dol_strlen($file) - 4);
  251. //print "<tr><td>Creation de la table $name</td>";
  252. $buffer = '';
  253. $fp = fopen($dir.$file,"r");
  254. if ($fp)
  255. {
  256. while (!feof($fp))
  257. {
  258. $buf = fgets($fp, 4096);
  259. // Cas special de lignes autorisees pour certaines versions uniquement
  260. if ($choix == 1 && preg_match('/^--\sV([0-9\.]+)/i',$buf,$reg))
  261. {
  262. $versioncommande=explode('.',$reg[1]);
  263. //print var_dump($versioncommande);
  264. //print var_dump($versionarray);
  265. if (count($versioncommande) && count($versionarray)
  266. && versioncompare($versioncommande,$versionarray) <= 0)
  267. {
  268. // Version qualified, delete SQL comments
  269. $buf=preg_replace('/^--\sV([0-9\.]+)/i','',$buf);
  270. //print "Ligne $i qualifiee par version: ".$buf.'<br>';
  271. }
  272. }
  273. if ($choix == 2 && preg_match('/^--\sPOSTGRESQL\sV([0-9\.]+)/i',$buf,$reg))
  274. {
  275. $versioncommande=explode('.',$reg[1]);
  276. //print var_dump($versioncommande);
  277. //print var_dump($versionarray);
  278. if (count($versioncommande) && count($versionarray)
  279. && versioncompare($versioncommande,$versionarray) <= 0)
  280. {
  281. // Version qualified, delete SQL comments
  282. $buf=preg_replace('/^--\sPOSTGRESQL\sV([0-9\.]+)/i','',$buf);
  283. //print "Ligne $i qualifiee par version: ".$buf.'<br>';
  284. }
  285. }
  286. // Ajout ligne si non commentaire
  287. if (! preg_match('/^--/i',$buf)) $buffer .= $buf;
  288. }
  289. fclose($fp);
  290. // Si plusieurs requetes, on boucle sur chaque
  291. $listesql=explode(';',$buffer);
  292. foreach ($listesql as $req)
  293. {
  294. $buffer=trim($req);
  295. if ($buffer)
  296. {
  297. // Replace the prefix tables
  298. if ($dolibarr_main_db_prefix != 'llx_')
  299. {
  300. $buffer=preg_replace('/llx_/i',$dolibarr_main_db_prefix,$buffer);
  301. }
  302. //print "<tr><td>Creation des cles et index de la table $name: '$buffer'</td>";
  303. $requestnb++;
  304. dolibarr_install_syslog("Request: ".$buffer,LOG_DEBUG);
  305. $resql=$db->query($buffer,0,'dml');
  306. if ($resql)
  307. {
  308. //print "<td>OK requete ==== $buffer</td></tr>";
  309. $db->free($resql);
  310. }
  311. else
  312. {
  313. if ($db->errno() == 'DB_ERROR_KEY_NAME_ALREADY_EXISTS' ||
  314. $db->errno() == 'DB_ERROR_CANNOT_CREATE' ||
  315. $db->errno() == 'DB_ERROR_PRIMARY_KEY_ALREADY_EXISTS' ||
  316. $db->errno() == 'DB_ERROR_TABLE_OR_KEY_ALREADY_EXISTS' ||
  317. preg_match('/duplicate key name/i',$db->error()))
  318. {
  319. //print "<td>Deja existante</td></tr>";
  320. $key_exists = 1;
  321. }
  322. else
  323. {
  324. print "<tr><td>".$langs->trans("CreateOtherKeysForTable",$name);
  325. print "<br>\n".$langs->trans("Request").' '.$requestnb.' : '.$db->lastqueryerror();
  326. print "\n</td>";
  327. print '<td><font class="error">'.$langs->trans("ErrorSQL")." ".$db->errno()." ".$db->error().'</font></td></tr>';
  328. $error++;
  329. }
  330. }
  331. }
  332. }
  333. }
  334. else
  335. {
  336. print "<tr><td>".$langs->trans("CreateOtherKeysForTable",$name);
  337. print "</td>";
  338. print '<td><font class="error">'.$langs->trans("Error")." Failed to open file ".$dir.$file."</font></td></tr>";
  339. $error++;
  340. dolibarr_install_syslog("Failed to open file ".$dir.$file,LOG_ERR);
  341. }
  342. }
  343. if ($tablefound && $error == 0)
  344. {
  345. print '<tr><td>';
  346. print $langs->trans("OtherKeysCreation").'</td><td>'.$langs->trans("OK").'</td></tr>';
  347. $okkeys = 1;
  348. }
  349. }
  350. /***************************************************************************************
  351. *
  352. * Chargement fichier functions.sql
  353. *
  354. ***************************************************************************************/
  355. if ($ok && $createfunctions)
  356. {
  357. // For this file, we use directory according to database type
  358. if ($choix==1) $dir = "mysql/functions/";
  359. elseif ($choix==2) $dir = "pgsql/functions/";
  360. elseif ($choix==3) $dir = "mssql/functions/";
  361. // Creation donnees
  362. $file = "functions.sql";
  363. if (file_exists($dir.$file))
  364. {
  365. $fp = fopen($dir.$file,"r");
  366. dolibarr_install_syslog("Open function file ".$dir.$file." handle=".$fp,LOG_DEBUG);
  367. if ($fp)
  368. {
  369. $buffer='';
  370. while (!feof($fp))
  371. {
  372. $buf = fgets($fp, 4096);
  373. if (substr($buf, 0, 2) <> '--')
  374. {
  375. $buffer .= $buf."??";
  376. }
  377. }
  378. fclose($fp);
  379. }
  380. //$buffer=preg_replace('/;\';/',";'??",$buffer);
  381. // If several requests, we loop on each of them
  382. $listesql=explode('??',$buffer);
  383. foreach ($listesql as $buffer)
  384. {
  385. $buffer=trim($buffer);
  386. if ($buffer)
  387. {
  388. dolibarr_install_syslog("Request: ".$buffer,LOG_DEBUG);
  389. print "<!-- Insert line : ".$buffer."<br>-->\n";
  390. $resql=$db->query($buffer,0,'dml');
  391. if ($resql)
  392. {
  393. $ok = 1;
  394. $db->free($resql);
  395. }
  396. else
  397. {
  398. if ($db->errno() == 'DB_ERROR_RECORD_ALREADY_EXISTS'
  399. || $db->errno() == 'DB_ERROR_KEY_NAME_ALREADY_EXISTS')
  400. {
  401. //print "Insert line : ".$buffer."<br>\n";
  402. }
  403. else
  404. {
  405. $ok = 0;
  406. print "<tr><td>".$langs->trans("FunctionsCreation");
  407. print "<br>\n".$langs->trans("Request").' '.$requestnb.' : '.$buffer;
  408. print "\n</td>";
  409. print '<td><font class="error">'.$langs->trans("ErrorSQL")." ".$db->errno()." ".$db->error().'</font></td></tr>';
  410. $error++;
  411. }
  412. }
  413. }
  414. }
  415. print "<tr><td>".$langs->trans("FunctionsCreation")."</td>";
  416. if ($ok)
  417. {
  418. print "<td>".$langs->trans("OK")."</td></tr>";
  419. }
  420. else
  421. {
  422. print '<td><font class="error">'.$langs->trans("Error").'</font></td></tr>';
  423. $ok = 1 ;
  424. }
  425. }
  426. }
  427. /***************************************************************************************
  428. *
  429. * Load files data/*.sql
  430. *
  431. ***************************************************************************************/
  432. if ($ok && $createdata)
  433. {
  434. // We always choose in mysql directory (Conversion is done by driver to translate SQL syntax)
  435. $dir = "mysql/data/";
  436. // Insert data
  437. $handle=opendir($dir);
  438. dolibarr_install_syslog("Open directory data ".$dir." handle=".$handle,LOG_DEBUG);
  439. $tablefound = 0;
  440. $tabledata=array();
  441. if (is_resource($handle))
  442. {
  443. while (($file = readdir($handle))!==false)
  444. {
  445. if (preg_match('/\.sql$/i',$file) && preg_match('/^llx_/i',$file))
  446. {
  447. //print 'x'.$file.'-'.$createdata.'<br>';
  448. if (is_numeric($createdata) || preg_match('/'.preg_quote($createdata).'/i',$file))
  449. {
  450. $tablefound++;
  451. $tabledata[]=$file;
  452. }
  453. }
  454. }
  455. closedir($handle);
  456. }
  457. // Sort list of data files on alphabetical order (load order is important)
  458. sort($tabledata);
  459. foreach($tabledata as $file)
  460. {
  461. $name = substr($file, 0, dol_strlen($file) - 4);
  462. $fp = fopen($dir.$file,"r");
  463. dolibarr_install_syslog("Open data file ".$dir.$file." handle=".$fp,LOG_DEBUG);
  464. if ($fp)
  465. {
  466. $arrayofrequests=array();
  467. $linefound=0;
  468. $linegroup=0;
  469. $sizeofgroup=1; // Grouping request to have 1 query for several requests does not works with mysql, so we use 1.
  470. // Load all requests
  471. while (!feof($fp))
  472. {
  473. $buffer = fgets($fp, 4096);
  474. $buffer = trim($buffer);
  475. if ($buffer)
  476. {
  477. if (substr($buffer, 0, 2) == '--') continue;
  478. if ($linefound && ($linefound % $sizeofgroup) == 0)
  479. {
  480. $linegroup++;
  481. }
  482. if (empty($arrayofrequests[$linegroup])) $arrayofrequests[$linegroup]=$buffer;
  483. else $arrayofrequests[$linegroup].=" ".$buffer;
  484. $linefound++;
  485. }
  486. }
  487. fclose($fp);
  488. dolibarr_install_syslog("Found ".$linefound." records, defined ".count($arrayofrequests)." group(s).",LOG_DEBUG);
  489. $okallfile=1;
  490. $db->begin();
  491. // We loop on each requests of file
  492. foreach($arrayofrequests as $buffer)
  493. {
  494. // Replace the prefix tables
  495. if ($dolibarr_main_db_prefix != 'llx_')
  496. {
  497. $buffer=preg_replace('/llx_/i',$dolibarr_main_db_prefix,$buffer);
  498. }
  499. //dolibarr_install_syslog("Request: ".$buffer,LOG_DEBUG);
  500. $resql=$db->query($buffer,1);
  501. if ($resql)
  502. {
  503. //$db->free($resql); // Not required as request we launch here does not return memory needs.
  504. }
  505. else
  506. {
  507. if ($db->lasterrno() == 'DB_ERROR_RECORD_ALREADY_EXISTS')
  508. {
  509. //print "<tr><td>Insertion ligne : $buffer</td><td>";
  510. }
  511. else
  512. {
  513. $ok = 0;
  514. $okallfile = 0;
  515. print '<font class="error">'.$langs->trans("ErrorSQL")." : ".$db->lasterrno()." - ".$db->lastqueryerror()." - ".$db->lasterror()."</font><br>";
  516. }
  517. }
  518. }
  519. if ($okallfile) $db->commit();
  520. else $db->rollback();
  521. }
  522. }
  523. print "<tr><td>".$langs->trans("ReferenceDataLoading")."</td>";
  524. if ($ok)
  525. {
  526. print "<td>".$langs->trans("OK")."</td></tr>";
  527. }
  528. else
  529. {
  530. print '<td><font class="error">'.$langs->trans("Error").'</font></td></tr>';
  531. $ok = 1; // Data loading are not blocking errors
  532. }
  533. }
  534. print '</table>';
  535. }
  536. else
  537. {
  538. print 'Parameter action=set not defined';
  539. }
  540. dolibarr_install_syslog("--- install/etape2.php end", LOG_INFO);
  541. pFooter(!$ok,$setuplang);
  542. $db->close();
  543. ?>