2004-10-20 00:24:10 +02:00
< ? php
2016-02-22 18:45:00 +01:00
/* Copyright ( C ) 2004 Rodolphe Quiedeville < rodolphe @ quiedeville . org >
* Copyright ( C ) 2004 - 2010 Laurent Destailleur < eldy @ users . sourceforge . net >
* Copyright ( C ) 2015 Cedric GROSS < c . gross @ kreiz - it . fr >
* Copyright ( C ) 2015 - 2016 Raphaël Doursenaud < rdoursenaud @ gpcsolutions . fr >
2025-02-03 14:28:54 +01:00
* Copyright ( C ) 2024 - 2025 MDW < mdeweerd @ users . noreply . github . com >
2004-02-05 00:11:46 +01:00
*
* This program is free software ; you can redistribute it and / or modify
* it under the terms of the GNU General Public License as published by
2013-01-16 15:36:08 +01:00
* the Free Software Foundation ; either version 3 of the License , or
2004-02-05 00:11:46 +01:00
* ( at your option ) any later version .
*
* This program is distributed in the hope that it will be useful ,
* but WITHOUT ANY WARRANTY ; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE . See the
* GNU General Public License for more details .
*
* You should have received a copy of the GNU General Public License
2019-09-23 21:55:30 +02:00
* along with this program . If not , see < https :// www . gnu . org / licenses />.
2004-02-05 00:11:46 +01:00
*/
2005-03-18 01:57:30 +01:00
2005-03-26 13:49:59 +01:00
/**
2015-07-06 15:29:58 +02:00
* \file htdocs / install / step2 . php
* \ingroup install
2010-11-15 20:08:35 +01:00
* \brief Create tables , primary keys , foreign keys , indexes and functions into database and then load reference data
2009-10-25 17:30:26 +01:00
*/
2005-03-26 13:49:59 +01:00
2012-08-23 02:46:16 +02:00
include 'inc.php' ;
2012-08-22 23:11:24 +02:00
require_once $dolibarr_main_document_root . '/core/class/conf.class.php' ;
require_once $dolibarr_main_document_root . '/core/lib/admin.lib.php' ;
2023-12-13 09:41:20 +01:00
require_once $dolibarr_main_document_root . '/core/lib/security.lib.php' ;
2005-02-12 22:25:39 +01:00
2016-02-22 18:45:00 +01:00
global $langs ;
2015-07-06 15:29:58 +02:00
$step = 2 ;
2005-10-03 03:14:50 +02:00
$ok = 0 ;
2005-03-26 13:49:59 +01:00
2022-03-12 14:49:54 +01:00
// This page can be long. We increase the time allowed. / Cette page peut etre longue. On augmente le delai autorise.
// Only works if you are not in safe_mode. / Ne fonctionne que si on est pas en safe_mode.
2020-04-10 10:59:32 +02:00
$err = error_reporting ();
2022-03-12 14:49:54 +01:00
error_reporting ( 0 ); // Disable all errors
2009-11-09 23:49:28 +01:00
//error_reporting(E_ALL);
2022-03-12 14:49:54 +01:00
@ set_time_limit ( 1800 ); // Need 1800 on some very slow OS like Windows 7/64
2005-09-24 01:36:48 +02:00
error_reporting ( $err );
2005-03-26 13:49:59 +01:00
2023-12-04 12:36:19 +01:00
$action = GETPOST ( 'action' , 'aZ09' ) ? GETPOST ( 'action' , 'aZ09' ) : ( empty ( $argv [ 1 ]) ? '' : $argv [ 1 ]);
$setuplang = GETPOST ( 'selectlang' , 'aZ09' , 3 ) ? GETPOST ( 'selectlang' , 'aZ09' , 3 ) : ( empty ( $argv [ 2 ]) ? 'auto' : $argv [ 2 ]);
2005-10-30 02:07:00 +01:00
$langs -> setDefaultLang ( $setuplang );
2018-10-01 08:22:23 +02:00
$langs -> loadLangs ( array ( " admin " , " install " ));
2005-02-12 22:25:39 +01:00
2022-03-12 14:49:54 +01:00
2022-03-12 14:53:15 +01:00
// Choice of DBMS
2022-03-12 14:49:54 +01:00
2020-04-10 10:59:32 +02:00
$choix = 0 ;
2021-02-22 15:18:01 +01:00
if ( $dolibarr_main_db_type == " mysqli " ) {
$choix = 1 ;
}
if ( $dolibarr_main_db_type == " pgsql " ) {
$choix = 2 ;
}
if ( $dolibarr_main_db_type == " mssql " ) {
$choix = 3 ;
}
if ( $dolibarr_main_db_type == " sqlite " ) {
$choix = 4 ;
}
if ( $dolibarr_main_db_type == " sqlite3 " ) {
$choix = 5 ;
}
2024-01-20 09:22:38 +01:00
//if (empty($choix)) dol_print_error(null,'Database type '.$dolibarr_main_db_type.' not supported into step2.php page');
2005-04-02 22:56:18 +02:00
2022-03-12 14:49:54 +01:00
2018-07-07 23:50:41 +02:00
// Now we load forced values from install.forced.php file.
2022-03-12 14:49:54 +01:00
2020-04-10 10:59:32 +02:00
$useforcedwizard = false ;
$forcedfile = " ./install.forced.php " ;
2021-02-22 15:18:01 +01:00
if ( $conffile == " /etc/dolibarr/conf.php " ) {
$forcedfile = " /etc/dolibarr/install.forced.php " ;
}
2016-02-22 18:45:00 +01:00
if ( @ file_exists ( $forcedfile )) {
$useforcedwizard = true ;
include_once $forcedfile ;
2018-07-26 13:49:12 +02:00
// test for travis
2021-02-22 15:18:01 +01:00
if ( ! empty ( $argv [ 1 ]) && $argv [ 1 ] == " set " ) {
$action = " set " ;
}
2016-02-22 18:45:00 +01:00
}
2008-04-21 09:21:13 +02:00
2023-10-25 17:03:37 +02:00
dolibarr_install_syslog ( " --- step2: entering step2.php page " );
2004-02-12 17:44:49 +01:00
2024-03-19 21:46:02 +01:00
'@phan-var-force string $dolibarr_main_db_prefix' ; // From configuraiotn file or install/inc.php
2004-01-30 12:55:53 +01:00
2024-08-31 12:12:59 +02:00
2024-09-03 15:22:50 +02:00
/*
* Actions
*/
// None
2008-04-21 09:21:13 +02:00
/*
2009-10-26 01:34:09 +01:00
* View
*/
2008-04-21 09:21:13 +02:00
2024-09-05 11:49:33 +02:00
pHeader ( $langs -> trans ( " DolibarrSetup " ) . ' - ' . $langs -> trans ( " CreateDatabaseObjects " ), " step4 " );
2004-01-27 15:36:40 +01:00
2010-09-29 10:09:17 +02:00
// Test if we can run a first install process
2021-02-22 15:18:01 +01:00
if ( ! is_writable ( $conffile )) {
2020-10-31 14:32:18 +01:00
print $langs -> trans ( " ConfFileIsNotWritable " , $conffiletoshow );
pFooter ( 1 , $setuplang , 'jscheckparam' );
exit ;
2010-09-29 10:09:17 +02:00
}
2005-10-03 03:14:50 +02:00
2024-09-03 15:22:50 +02:00
if ( $action == " set " ) { // Test on permission not required. Already managed by test in inc.php
2021-04-30 15:34:02 +02:00
print '<h3><img class="valignmiddle inline-block paddingright" src="../theme/common/octicons/build/svg/database.svg" width="20" alt="Database"> ' . $langs -> trans ( " Database " ) . '</h3>' ;
2020-10-31 14:32:18 +01:00
print '<table cellspacing="0" style="padding: 4px 4px 4px 0" border="0" width="100%">' ;
$error = 0 ;
2023-06-24 04:04:02 +02:00
$db = getDoliDBInstance ( $conf -> db -> type , $conf -> db -> host , $conf -> db -> user , $conf -> db -> pass , $conf -> db -> name , ( int ) $conf -> db -> port );
2020-10-31 14:32:18 +01:00
2021-02-22 15:18:01 +01:00
if ( $db -> connected ) {
2020-10-31 14:32:18 +01:00
print " <tr><td> " ;
print $langs -> trans ( " ServerConnection " ) . " : " . $conf -> db -> host . '</td><td><img src="../theme/eldy/img/tick.png" alt="Ok"></td></tr>' ;
$ok = 1 ;
} else {
print " <tr><td>Failed to connect to server : " . $conf -> db -> host . '</td><td><img src="../theme/eldy/img/error.png" alt="Error"></td></tr>' ;
}
2021-02-22 15:18:01 +01:00
if ( $ok ) {
if ( $db -> database_selected ) {
2020-10-31 14:32:18 +01:00
dolibarr_install_syslog ( " step2: successful connection to database: " . $conf -> db -> name );
} else {
dolibarr_install_syslog ( " step2: failed connection to database : " . $conf -> db -> name , LOG_ERR );
print " <tr><td>Failed to select database " . $conf -> db -> name . '</td><td><img src="../theme/eldy/img/error.png" alt="Error"></td></tr>' ;
$ok = 0 ;
}
}
2022-03-12 14:53:15 +01:00
2025-02-03 14:28:54 +01:00
$versionarray = array ();
2022-03-12 14:49:54 +01:00
// Display version / Affiche version
2021-02-22 15:18:01 +01:00
if ( $ok ) {
2020-10-31 14:32:18 +01:00
$version = $db -> getVersion ();
$versionarray = $db -> getVersionArray ();
print '<tr><td>' . $langs -> trans ( " DatabaseVersion " ) . '</td>' ;
print '<td>' . $version . '</td></tr>' ;
//print '<td class="right">'.join('.',$versionarray).'</td></tr>';
print '<tr><td>' . $langs -> trans ( " DatabaseName " ) . '</td>' ;
print '<td>' . $db -> database_name . '</td></tr>' ;
//print '<td class="right">'.join('.',$versionarray).'</td></tr>';
}
$requestnb = 0 ;
// To disable some code, so you can call step2 with url like
2021-09-19 18:03:38 +02:00
// http://localhost/dolibarrnew/install/step2.php?action=set&token='.newToken().'&createtables=0&createkeys=0&createfunctions=0&createdata=llx_20_c_departements
2022-07-26 12:54:19 +02:00
$createtables = GETPOSTISSET ( 'createtables' ) ? GETPOST ( 'createtables' ) : 1 ;
$createkeys = GETPOSTISSET ( 'createkeys' ) ? GETPOST ( 'createkeys' ) : 1 ;
$createfunctions = GETPOSTISSET ( 'createfunctions' ) ? GETPOST ( 'createfunction' ) : 1 ;
$createdata = GETPOSTISSET ( 'createdata' ) ? GETPOST ( 'createdata' ) : 1 ;
2020-10-31 14:32:18 +01:00
2023-10-05 11:27:00 +02:00
// To say that SQL we pass to query are already escaped for mysql, so we need to unescape them
if ( property_exists ( $db , 'unescapeslashquot' )) {
2025-02-03 14:28:54 +01:00
$db -> unescapeslashquot = true ; // @phan-suppress-current-line PhanUndeclaredProperty
2023-10-05 11:27:00 +02:00
}
2020-10-31 14:32:18 +01:00
/**************************************************************************************
2021-02-22 15:18:01 +01:00
*
2022-03-12 14:49:54 +01:00
* Load files tables /*. sql ( not the *. key . sql ) . Files with '-xxx' in name are excluded ( they will be loaded during activation of module 'xxx' ) .
2021-11-27 14:46:47 +01:00
* To do before the files *. key . sql
2021-02-22 15:18:01 +01:00
*
***************************************************************************************/
if ( $ok && $createtables ) {
2020-10-31 14:32:18 +01:00
// We always choose in mysql directory (Conversion is done by driver to translate SQL syntax)
$dir = " mysql/tables/ " ;
$ok = 0 ;
$handle = opendir ( $dir );
2024-03-16 21:43:32 +01:00
dolibarr_install_syslog ( " step2: open tables directory " . $dir . " handle= " . ( is_bool ( $handle ) ? json_encode ( $handle ) : $handle ));
2020-10-31 14:32:18 +01:00
$tablefound = 0 ;
$tabledata = array ();
2021-02-22 15:18:01 +01:00
if ( is_resource ( $handle )) {
while (( $file = readdir ( $handle )) !== false ) {
2021-11-27 14:46:47 +01:00
if ( preg_match ( '/\.sql$/i' , $file ) && preg_match ( '/^llx_/i' , $file ) && ! preg_match ( '/\.key\.sql$/i' , $file ) && ! preg_match ( '/\-/' , $file )) {
2020-10-31 14:32:18 +01:00
$tablefound ++ ;
$tabledata [] = $file ;
}
}
closedir ( $handle );
}
// Sort list of sql files on alphabetical order (load order is important)
sort ( $tabledata );
2021-02-22 15:18:01 +01:00
foreach ( $tabledata as $file ) {
2020-10-31 14:32:18 +01:00
$name = substr ( $file , 0 , dol_strlen ( $file ) - 4 );
$buffer = '' ;
$fp = fopen ( $dir . $file , " r " );
2021-02-22 15:18:01 +01:00
if ( $fp ) {
while ( ! feof ( $fp )) {
2020-10-31 14:32:18 +01:00
$buf = fgets ( $fp , 4096 );
2023-12-27 12:12:20 +01:00
if ( substr ( $buf , 0 , 2 ) != '--' ) {
2020-10-31 14:32:18 +01:00
$buf = preg_replace ( '/--(.+)*/' , '' , $buf );
$buffer .= $buf ;
}
}
fclose ( $fp );
$buffer = trim ( $buffer );
2021-02-22 15:18:01 +01:00
if ( $conf -> db -> type == 'mysql' || $conf -> db -> type == 'mysqli' ) { // For Mysql 5.5+, we must replace type=innodb with ENGINE=innodb
2020-10-31 14:32:18 +01:00
$buffer = preg_replace ( '/type=innodb/i' , 'ENGINE=innodb' , $buffer );
} else {
// Keyword ENGINE is MySQL-specific, so scrub it for
// other database types (mssql, pgsql)
$buffer = preg_replace ( '/type=innodb/i' , '' , $buffer );
$buffer = preg_replace ( '/ENGINE=innodb/i' , '' , $buffer );
}
// Replace the prefix tables
2021-02-22 15:18:01 +01:00
if ( $dolibarr_main_db_prefix != 'llx_' ) {
2020-10-31 14:32:18 +01:00
$buffer = preg_replace ( '/llx_/i' , $dolibarr_main_db_prefix , $buffer );
}
2022-03-12 14:49:54 +01:00
//print "<tr><td>Creation of table $name/td>";
2020-10-31 14:32:18 +01:00
$requestnb ++ ;
dolibarr_install_syslog ( " step2: request: " . $buffer );
$resql = $db -> query ( $buffer , 0 , 'dml' );
2021-02-22 15:18:01 +01:00
if ( $resql ) {
2022-03-12 14:49:54 +01:00
// print "<td>OK request ==== $buffer</td></tr>";
2020-10-31 14:32:18 +01:00
$db -> free ( $resql );
} else {
if ( $db -> errno () == 'DB_ERROR_TABLE_ALREADY_EXISTS' ||
2022-03-12 14:53:15 +01:00
$db -> errno () == 'DB_ERROR_TABLE_OR_KEY_ALREADY_EXISTS' ) {
2022-03-12 14:49:54 +01:00
//print "<td>already existing</td></tr>";
2020-10-31 14:32:18 +01:00
} else {
print " <tr><td> " . $langs -> trans ( " CreateTableAndPrimaryKey " , $name );
print " <br> \n " . $langs -> trans ( " Request " ) . ' ' . $requestnb . ' : ' . $buffer . ' <br>Executed query : ' . $db -> lastquery ;
print " \n </td> " ;
print '<td><span class="error">' . $langs -> trans ( " ErrorSQL " ) . " " . $db -> errno () . " " . $db -> error () . '</span></td></tr>' ;
$error ++ ;
}
}
} else {
print " <tr><td> " . $langs -> trans ( " CreateTableAndPrimaryKey " , $name );
print " </td> " ;
print '<td><span class="error">' . $langs -> trans ( " Error " ) . ' Failed to open file ' . $dir . $file . '</span></td></tr>' ;
$error ++ ;
dolibarr_install_syslog ( " step2: failed to open file " . $dir . $file , LOG_ERR );
}
}
2021-02-22 15:18:01 +01:00
if ( $tablefound ) {
if ( $error == 0 ) {
2020-10-31 14:32:18 +01:00
print '<tr><td>' ;
print $langs -> trans ( " TablesAndPrimaryKeysCreation " ) . '</td><td><img src="../theme/eldy/img/tick.png" alt="Ok"></td></tr>' ;
$ok = 1 ;
}
} else {
print '<tr><td>' . $langs -> trans ( " ErrorFailedToFindSomeFiles " , $dir ) . '</td><td><img src="../theme/eldy/img/error.png" alt="Error"></td></tr>' ;
dolibarr_install_syslog ( " step2: failed to find files to create database in directory " . $dir , LOG_ERR );
}
}
/***************************************************************************************
2021-02-22 15:18:01 +01:00
*
2022-03-12 14:49:54 +01:00
* Load files tables /*. key . sql . Files with '-xxx' in name are excluded ( they will be loaded during activation of module 'xxx' ) .
2021-11-27 14:46:47 +01:00
* To do after the files *. sql
2021-02-22 15:18:01 +01:00
*
***************************************************************************************/
if ( $ok && $createkeys ) {
2020-10-31 14:32:18 +01:00
// We always choose in mysql directory (Conversion is done by driver to translate SQL syntax)
$dir = " mysql/tables/ " ;
$okkeys = 0 ;
$handle = opendir ( $dir );
2024-03-16 21:43:32 +01:00
dolibarr_install_syslog ( " step2: open keys directory " . $dir . " handle= " . ( is_bool ( $handle ) ? json_encode ( $handle ) : $handle ));
2020-10-31 14:32:18 +01:00
$tablefound = 0 ;
$tabledata = array ();
2021-02-22 15:18:01 +01:00
if ( is_resource ( $handle )) {
while (( $file = readdir ( $handle )) !== false ) {
2021-11-27 14:46:47 +01:00
if ( preg_match ( '/\.sql$/i' , $file ) && preg_match ( '/^llx_/i' , $file ) && preg_match ( '/\.key\.sql$/i' , $file ) && ! preg_match ( '/\-/' , $file )) {
2020-10-31 14:32:18 +01:00
$tablefound ++ ;
$tabledata [] = $file ;
}
}
closedir ( $handle );
}
// Sort list of sql files on alphabetical order (load order is important)
sort ( $tabledata );
2021-02-22 15:18:01 +01:00
foreach ( $tabledata as $file ) {
2020-10-31 14:32:18 +01:00
$name = substr ( $file , 0 , dol_strlen ( $file ) - 4 );
2022-03-12 14:49:54 +01:00
//print "<tr><td>Creation of table $name</td>";
2020-10-31 14:32:18 +01:00
$buffer = '' ;
$fp = fopen ( $dir . $file , " r " );
2021-02-22 15:18:01 +01:00
if ( $fp ) {
while ( ! feof ( $fp )) {
2020-10-31 14:32:18 +01:00
$buf = fgets ( $fp , 4096 );
// Special case of lines allowed for some version only
2023-12-04 12:36:19 +01:00
// MySQL
2021-02-22 15:18:01 +01:00
if ( $choix == 1 && preg_match ( '/^--\sV([0-9\.]+)/i' , $buf , $reg )) {
2020-10-31 14:32:18 +01:00
$versioncommande = explode ( '.' , $reg [ 1 ]);
2022-05-17 15:10:20 +02:00
//var_dump($versioncommande);
//var_dump($versionarray);
2020-10-31 14:32:18 +01:00
if ( count ( $versioncommande ) && count ( $versionarray )
2021-02-22 15:18:01 +01:00
&& versioncompare ( $versioncommande , $versionarray ) <= 0 ) {
2020-10-31 14:32:18 +01:00
// Version qualified, delete SQL comments
$buf = preg_replace ( '/^--\sV([0-9\.]+)/i' , '' , $buf );
//print "Ligne $i qualifiee par version: ".$buf.'<br>';
}
}
2023-12-04 12:36:19 +01:00
// PGSQL
2021-02-22 15:18:01 +01:00
if ( $choix == 2 && preg_match ( '/^--\sPOSTGRESQL\sV([0-9\.]+)/i' , $buf , $reg )) {
2020-10-31 14:32:18 +01:00
$versioncommande = explode ( '.' , $reg [ 1 ]);
2022-05-17 15:10:20 +02:00
//var_dump($versioncommande);
//var_dump($versionarray);
2020-10-31 14:32:18 +01:00
if ( count ( $versioncommande ) && count ( $versionarray )
2021-02-22 15:18:01 +01:00
&& versioncompare ( $versioncommande , $versionarray ) <= 0 ) {
2020-10-31 14:32:18 +01:00
// Version qualified, delete SQL comments
$buf = preg_replace ( '/^--\sPOSTGRESQL\sV([0-9\.]+)/i' , '' , $buf );
//print "Ligne $i qualifiee par version: ".$buf.'<br>';
}
}
2022-03-12 14:49:54 +01:00
// Add line if no comment
2021-02-22 15:18:01 +01:00
if ( ! preg_match ( '/^--/i' , $buf )) {
$buffer .= $buf ;
}
2020-10-31 14:32:18 +01:00
}
fclose ( $fp );
2022-03-12 14:53:15 +01:00
// If several requests, we loop on each
2020-10-31 14:32:18 +01:00
$listesql = explode ( ';' , $buffer );
2021-02-22 15:18:01 +01:00
foreach ( $listesql as $req ) {
2020-10-31 14:32:18 +01:00
$buffer = trim ( $req );
2021-02-22 15:18:01 +01:00
if ( $buffer ) {
2020-10-31 14:32:18 +01:00
// Replace the prefix tables
2021-02-22 15:18:01 +01:00
if ( $dolibarr_main_db_prefix != 'llx_' ) {
2020-10-31 14:32:18 +01:00
$buffer = preg_replace ( '/llx_/i' , $dolibarr_main_db_prefix , $buffer );
}
2022-03-12 14:49:54 +01:00
//print "<tr><td>Creation of keys and table index $name: '$buffer'</td>";
2020-10-31 14:32:18 +01:00
$requestnb ++ ;
dolibarr_install_syslog ( " step2: request: " . $buffer );
$resql = $db -> query ( $buffer , 0 , 'dml' );
2021-02-22 15:18:01 +01:00
if ( $resql ) {
2022-03-12 14:49:54 +01:00
//print "<td>OK request ==== $buffer</td></tr>";
2020-10-31 14:32:18 +01:00
$db -> free ( $resql );
} else {
if ( $db -> errno () == 'DB_ERROR_KEY_NAME_ALREADY_EXISTS' ||
$db -> errno () == 'DB_ERROR_CANNOT_CREATE' ||
$db -> errno () == 'DB_ERROR_PRIMARY_KEY_ALREADY_EXISTS' ||
$db -> errno () == 'DB_ERROR_TABLE_OR_KEY_ALREADY_EXISTS' ||
2021-02-22 15:18:01 +01:00
preg_match ( '/duplicate key name/i' , $db -> error ())) {
2020-10-31 14:32:18 +01:00
//print "<td>Deja existante</td></tr>";
$key_exists = 1 ;
} else {
print " <tr><td> " . $langs -> trans ( " CreateOtherKeysForTable " , $name );
print " <br> \n " . $langs -> trans ( " Request " ) . ' ' . $requestnb . ' : ' . $db -> lastqueryerror ();
print " \n </td> " ;
print '<td><span class="error">' . $langs -> trans ( " ErrorSQL " ) . " " . $db -> errno () . " " . $db -> error () . '</span></td></tr>' ;
$error ++ ;
}
}
}
}
} else {
print " <tr><td> " . $langs -> trans ( " CreateOtherKeysForTable " , $name );
print " </td> " ;
print '<td><span class="error">' . $langs -> trans ( " Error " ) . " Failed to open file " . $dir . $file . " </span></td></tr> " ;
$error ++ ;
dolibarr_install_syslog ( " step2: failed to open file " . $dir . $file , LOG_ERR );
}
}
2021-02-22 15:18:01 +01:00
if ( $tablefound && $error == 0 ) {
2020-10-31 14:32:18 +01:00
print '<tr><td>' ;
print $langs -> trans ( " OtherKeysCreation " ) . '</td><td><img src="../theme/eldy/img/tick.png" alt="Ok"></td></tr>' ;
$okkeys = 1 ;
}
}
/***************************************************************************************
2021-02-22 15:18:01 +01:00
*
2022-03-12 14:49:54 +01:00
* Load the file 'functions.sql'
2021-02-22 15:18:01 +01:00
*
***************************************************************************************/
if ( $ok && $createfunctions ) {
2020-10-31 14:32:18 +01:00
// For this file, we use a directory according to database type
2025-02-03 14:28:54 +01:00
$dir = null ;
2021-02-22 15:18:01 +01:00
if ( $choix == 1 ) {
$dir = " mysql/functions/ " ;
} elseif ( $choix == 2 ) {
$dir = " pgsql/functions/ " ;
} elseif ( $choix == 3 ) {
$dir = " mssql/functions/ " ;
} elseif ( $choix == 4 ) {
$dir = " sqlite3/functions/ " ;
}
2011-09-29 22:21:57 +02:00
2022-03-12 14:49:54 +01:00
// Creation of data
2020-10-31 14:32:18 +01:00
$file = " functions.sql " ;
2025-02-03 14:28:54 +01:00
if ( $dir !== null && file_exists ( $dir . $file )) {
2020-10-31 14:32:18 +01:00
$fp = fopen ( $dir . $file , " r " );
2024-03-16 21:43:32 +01:00
dolibarr_install_syslog ( " step2: open function file " . $dir . $file . " handle= " . ( is_bool ( $fp ) ? json_encode ( $fp ) : $fp ));
2025-02-03 14:28:54 +01:00
$buffer = '' ;
2021-02-22 15:18:01 +01:00
if ( $fp ) {
while ( ! feof ( $fp )) {
2020-10-31 14:32:18 +01:00
$buf = fgets ( $fp , 4096 );
2023-12-27 12:12:20 +01:00
if ( substr ( $buf , 0 , 2 ) != '--' ) {
2020-10-31 14:32:18 +01:00
$buffer .= $buf . " § " ;
}
}
fclose ( $fp );
}
//$buffer=preg_replace('/;\';/',";'§",$buffer);
// If several requests, we loop on each of them
$listesql = explode ( '§' , $buffer );
2021-02-22 15:18:01 +01:00
foreach ( $listesql as $buffer ) {
2020-10-31 14:32:18 +01:00
$buffer = trim ( $buffer );
2021-02-22 15:18:01 +01:00
if ( $buffer ) {
2020-10-31 14:32:18 +01:00
// Replace the prefix in table names
2021-02-22 15:18:01 +01:00
if ( $dolibarr_main_db_prefix != 'llx_' ) {
2020-10-31 14:32:18 +01:00
$buffer = preg_replace ( '/llx_/i' , $dolibarr_main_db_prefix , $buffer );
}
dolibarr_install_syslog ( " step2: request: " . $buffer );
print " <!-- Insert line : " . $buffer . " <br>--> \n " ;
$resql = $db -> query ( $buffer , 0 , 'dml' );
2021-02-22 15:18:01 +01:00
if ( $resql ) {
2020-10-31 14:32:18 +01:00
$ok = 1 ;
$db -> free ( $resql );
} else {
if ( $db -> errno () == 'DB_ERROR_RECORD_ALREADY_EXISTS'
2021-02-22 15:18:01 +01:00
|| $db -> errno () == 'DB_ERROR_KEY_NAME_ALREADY_EXISTS' ) {
2020-10-31 14:32:18 +01:00
//print "Insert line : ".$buffer."<br>\n";
} else {
$ok = 0 ;
print " <tr><td> " . $langs -> trans ( " FunctionsCreation " );
print " <br> \n " . $langs -> trans ( " Request " ) . ' ' . $requestnb . ' : ' . $buffer ;
print " \n </td> " ;
print '<td><span class="error">' . $langs -> trans ( " ErrorSQL " ) . " " . $db -> errno () . " " . $db -> error () . '</span></td></tr>' ;
$error ++ ;
}
}
}
}
print " <tr><td> " . $langs -> trans ( " FunctionsCreation " ) . " </td> " ;
2021-02-22 15:18:01 +01:00
if ( $ok ) {
2020-10-31 14:32:18 +01:00
print '<td><img src="../theme/eldy/img/tick.png" alt="Ok"></td></tr>' ;
} else {
print '<td><img src="../theme/eldy/img/error.png" alt="Error"></td></tr>' ;
$ok = 1 ;
}
}
}
/***************************************************************************************
2021-02-22 15:18:01 +01:00
*
2022-03-12 14:49:54 +01:00
* Load files data /*. sql . Files with '-xxx' in name are excluded ( they will be loaded during activation of module 'xxx' ) .
2021-02-22 15:18:01 +01:00
*
***************************************************************************************/
if ( $ok && $createdata ) {
2020-10-31 14:32:18 +01:00
// We always choose in mysql directory (Conversion is done by driver to translate SQL syntax)
$dir = " mysql/data/ " ;
// Insert data
$handle = opendir ( $dir );
2024-03-16 21:43:32 +01:00
dolibarr_install_syslog ( " step2: open directory data " . $dir . " handle= " . ( is_bool ( $handle ) ? json_encode ( $handle ) : $handle ));
2020-10-31 14:32:18 +01:00
$tablefound = 0 ;
$tabledata = array ();
2021-02-22 15:18:01 +01:00
if ( is_resource ( $handle )) {
while (( $file = readdir ( $handle )) !== false ) {
2021-11-27 14:46:47 +01:00
if ( preg_match ( '/\.sql$/i' , $file ) && preg_match ( '/^llx_/i' , $file ) && ! preg_match ( '/\-/' , $file )) {
2021-02-22 15:18:01 +01:00
if ( preg_match ( '/^llx_accounting_account_/' , $file )) {
2022-03-12 14:49:54 +01:00
continue ; // We discard data file of chart of account. This will be loaded when a chart is selected.
2021-02-22 15:18:01 +01:00
}
2020-10-31 14:32:18 +01:00
//print 'x'.$file.'-'.$createdata.'<br>';
2021-02-22 15:18:01 +01:00
if ( is_numeric ( $createdata ) || preg_match ( '/' . preg_quote ( $createdata ) . '/i' , $file )) {
2020-10-31 14:32:18 +01:00
$tablefound ++ ;
$tabledata [] = $file ;
}
}
}
closedir ( $handle );
}
// Sort list of data files on alphabetical order (load order is important)
sort ( $tabledata );
2021-02-22 15:18:01 +01:00
foreach ( $tabledata as $file ) {
2020-10-31 14:32:18 +01:00
$name = substr ( $file , 0 , dol_strlen ( $file ) - 4 );
$fp = fopen ( $dir . $file , " r " );
2024-03-16 21:43:32 +01:00
dolibarr_install_syslog ( " step2: open data file " . $dir . $file . " handle= " . ( is_bool ( $fp ) ? json_encode ( $fp ) : $fp ));
2021-02-22 15:18:01 +01:00
if ( $fp ) {
2020-10-31 14:32:18 +01:00
$arrayofrequests = array ();
$linefound = 0 ;
$linegroup = 0 ;
$sizeofgroup = 1 ; // Grouping request to have 1 query for several requests does not works with mysql, so we use 1.
// Load all requests
2021-02-22 15:18:01 +01:00
while ( ! feof ( $fp )) {
2020-10-31 14:32:18 +01:00
$buffer = fgets ( $fp , 4096 );
$buffer = trim ( $buffer );
2021-02-22 15:18:01 +01:00
if ( $buffer ) {
if ( substr ( $buffer , 0 , 2 ) == '--' ) {
continue ;
}
2020-10-31 14:32:18 +01:00
2021-02-22 15:18:01 +01:00
if ( $linefound && ( $linefound % $sizeofgroup ) == 0 ) {
2020-10-31 14:32:18 +01:00
$linegroup ++ ;
}
2021-02-22 15:18:01 +01:00
if ( empty ( $arrayofrequests [ $linegroup ])) {
$arrayofrequests [ $linegroup ] = $buffer ;
} else {
$arrayofrequests [ $linegroup ] .= " " . $buffer ;
}
2020-10-31 14:32:18 +01:00
$linefound ++ ;
}
}
fclose ( $fp );
dolibarr_install_syslog ( " step2: found " . $linefound . " records, defined " . count ( $arrayofrequests ) . " group(s). " );
$okallfile = 1 ;
$db -> begin ();
// We loop on each requests of file
2021-02-22 15:18:01 +01:00
foreach ( $arrayofrequests as $buffer ) {
2022-03-12 14:49:54 +01:00
// Replace the tables prefixes
2021-02-22 15:18:01 +01:00
if ( $dolibarr_main_db_prefix != 'llx_' ) {
2020-10-31 14:32:18 +01:00
$buffer = preg_replace ( '/llx_/i' , $dolibarr_main_db_prefix , $buffer );
}
2024-07-27 19:51:25 +02:00
// Replace __ENTITY__ tag with 1 (master entity), this is only for dictionaries.
$buffer = preg_replace ( '/__ENTITY__/i' , '1' , $buffer );
2020-10-31 14:32:18 +01:00
//dolibarr_install_syslog("step2: request: " . $buffer);
$resql = $db -> query ( $buffer , 1 );
2021-02-22 15:18:01 +01:00
if ( $resql ) {
2020-10-31 14:32:18 +01:00
//$db->free($resql); // Not required as request we launch here does not return memory needs.
} else {
2021-02-22 15:18:01 +01:00
if ( $db -> lasterrno () == 'DB_ERROR_RECORD_ALREADY_EXISTS' ) {
2020-10-31 14:32:18 +01:00
//print "<tr><td>Insertion ligne : $buffer</td><td>";
} else {
$ok = 0 ;
$okallfile = 0 ;
print '<span class="error">' . $langs -> trans ( " ErrorSQL " ) . " : " . $db -> lasterrno () . " - " . $db -> lastqueryerror () . " - " . $db -> lasterror () . " </span><br> " ;
}
}
}
2021-02-22 15:18:01 +01:00
if ( $okallfile ) {
$db -> commit ();
} else {
$db -> rollback ();
}
2020-10-31 14:32:18 +01:00
}
}
print " <tr><td> " . $langs -> trans ( " ReferenceDataLoading " ) . " </td> " ;
2021-02-22 15:18:01 +01:00
if ( $ok ) {
2020-10-31 14:32:18 +01:00
print '<td><img src="../theme/eldy/img/tick.png" alt="Ok"></td></tr>' ;
} else {
print '<td><img src="../theme/eldy/img/error.png" alt="Error"></td></tr>' ;
$ok = 1 ; // Data loading are not blocking errors
}
}
print '</table>' ;
2020-05-21 15:05:19 +02:00
} else {
2020-10-31 14:32:18 +01:00
print 'Parameter action=set not defined' ;
2004-01-27 15:36:40 +01:00
}
2005-06-26 00:18:06 +02:00
2018-04-12 19:13:06 +02:00
2020-04-10 10:59:32 +02:00
$ret = 0 ;
2021-02-22 15:18:01 +01:00
if ( ! $ok && isset ( $argv [ 1 ])) {
$ret = 1 ;
}
2018-04-12 19:13:06 +02:00
dolibarr_install_syslog ( " Exit " . $ret );
2018-07-07 23:50:41 +02:00
dolibarr_install_syslog ( " - step2: end " );
2011-06-08 13:23:55 +02:00
2022-03-12 14:49:54 +01:00
2021-10-24 14:23:35 +02:00
// Force here a value we need after because master.inc.php is not loaded into step2.
// This code must be similar with the one into main.inc.php
2022-03-12 14:49:54 +01:00
2021-10-24 14:23:35 +02:00
$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 ); // Unique id of instance
2019-08-23 16:23:31 +02:00
2023-12-08 23:45:18 +01:00
$hash_unique_id = dol_hash ( 'dolibarr' . $conf -> file -> instance_unique_id , 'sha256' ); // Note: if the global salt changes, this hash changes too so ping may be counted twice. We don't mind. It is for statistics purpose only.
2019-08-23 16:23:31 +02:00
2023-10-08 23:19:49 +02:00
$out = '<input type="checkbox" name="dolibarrpingno" id="dolibarrpingno"' . (( getDolGlobalString ( 'MAIN_FIRST_PING_OK_ID' ) == 'disabled' ) ? '' : ' value="checked" checked="true"' ) . '> ' ;
2021-04-18 20:18:38 +02:00
$out .= '<label for="dolibarrpingno">' . $langs -> trans ( " MakeAnonymousPing " ) . '</label>' ;
2019-08-23 16:23:31 +02:00
$out .= '<!-- Add js script to manage the uncheck of option to not send the ping -->' ;
$out .= '<script type="text/javascript">' ;
$out .= 'jQuery(document).ready(function(){' ;
2021-10-24 14:23:35 +02:00
$out .= ' document.cookie = "DOLINSTALLNOPING_' . $hash_unique_id . '=0; path=/"' . " \n " ;
2019-08-23 16:23:31 +02:00
$out .= ' jQuery("#dolibarrpingno").click(function() {' ;
$out .= ' if (! $(this).is(\':checked\')) {' ;
$out .= ' console.log("We uncheck anonymous ping");' ;
2021-10-24 14:23:35 +02:00
$out .= ' document.cookie = "DOLINSTALLNOPING_' . $hash_unique_id . '=1; path=/"' . " \n " ;
$out .= ' } else {' . " \n " ;
$out .= ' console.log("We check anonymous ping");' ;
$out .= ' document.cookie = "DOLINSTALLNOPING_' . $hash_unique_id . '=0; path=/"' . " \n " ;
$out .= ' }' . " \n " ;
2019-08-23 16:23:31 +02:00
$out .= ' });' ;
$out .= '});' ;
$out .= '</script>' ;
print $out ;
2020-04-10 10:59:32 +02:00
pFooter ( $ok ? 0 : 1 , $setuplang );
2012-01-04 12:32:44 +01:00
2021-02-22 15:18:01 +01:00
if ( isset ( $db ) && is_object ( $db )) {
$db -> close ();
}
2018-04-12 19:13:06 +02:00
// Return code if ran from command line
2021-02-22 15:18:01 +01:00
if ( $ret ) {
exit ( $ret );
}