ordersprinter/webapp/install/installer.php

441 lines
13 KiB
PHP
Raw Normal View History

2020-11-19 22:47:44 +01:00
<?php
error_reporting(E_ALL);
2020-11-19 23:01:01 +01:00
define ('IS_INSTALLMODE', '1');
2020-11-19 22:47:44 +01:00
if (is_readable("../php/config1.php")) {
require_once( "../php/config1.php" );
} else {
require_once( "../php/config.php" );
}
require_once ('../php/utilities/basedb.php');
2020-11-19 23:03:48 +01:00
require_once ('../php/utilities/HistFiller.php');
2020-11-19 23:00:05 +01:00
require_once ('../php/utilities/decimaldefs.php');
2020-11-19 23:03:48 +01:00
require_once ('../php/utilities/roles.php');
2020-11-19 23:10:26 +01:00
require_once ('../php/utilities/version.php');
2020-11-19 22:47:44 +01:00
require_once ('../php/admin.php');
2020-11-19 23:12:07 +01:00
require_once ('../php/closing.php');
2020-11-19 22:47:44 +01:00
class ConfigWriter {
function getConfigVals() {
if (!is_readable("../php/config.php") && (!is_readable("../php/config1.php"))) {
echo json_encode(array("status" => "Failed"));
}
$retArray = array(
"host" => MYSQL_HOST,
"db" => MYSQL_DB,
"user" => MYSQL_USER,
"password" => MYSQL_PASSWORD,
"tabprefix" => TAB_PREFIX);
echo json_encode(array("status" => "OK","result" => $retArray));
}
}
class InstallAdmin {
var $pdo;
var $basedb;
var $timezone;
function __construct() {
$this->basedb = new Basedb();
}
function setPrefix($pre) {
$this->basedb->setPrefix($pre);
}
function setPdo($pdo) {
$this->pdo = $pdo;
}
function setTimeZone($zone) {
$this->timezone = $zone;
}
2020-11-19 22:59:47 +01:00
function openDbAndReturnPdo ($host,$db,$user,$password) {
$dsn = 'mysql:host=' . $host . ';dbname=' . $db;
$pdo = null;
2020-11-19 22:47:44 +01:00
try {
$pdo = new PDO($dsn, $user, $password);
$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
}
catch (PDOException $e) {
echo 'Verbindungsproblem: ' . $e->getMessage();
$pdo = null;
2020-11-19 22:59:47 +01:00
}
return $pdo;
2020-11-19 22:47:44 +01:00
}
function checkPhpStatus() {
2020-11-19 23:03:56 +01:00
$extensions = array("gd","mysqli","openssl","pdo_mysql","PDO","session","zlib","curl","zip","ftp","xml");
2020-11-19 22:47:44 +01:00
$missing = array();
$extensions_status = 1;
foreach($extensions as $anExtension) {
if (!extension_loaded($anExtension)) {
$missing[] = $anExtension;
$extensions_status = 0;
}
}
set_time_limit(60*5+1);
ini_set('session.gc_maxlifetime',65535);
session_set_cookie_params(65535);
$max_execution_status = 1;
// 5 minutes = 5*60
if (ini_get('max_execution_time') < (5*60)) {
$max_execution_status = 0;
}
$session_lifetime_status = 1;
if (ini_get('session.gc_maxlifetime') < (10*60*60)) {
$session_lifetime_status = 0;
}
$ret = array("extensions_status" => $extensions_status, "missing_extensions" => join(",",$missing),
"max_execution_status" => $max_execution_status, "max_execution_time" => ini_get('max_execution_time'),
"session_lifetime_status" => $session_lifetime_status, "session_gc_maxlifetime" => ini_get('session.gc_maxlifetime')
);
echo json_encode($ret);
}
2020-11-19 23:03:43 +01:00
2020-11-19 22:47:44 +01:00
function setVersion($prefix,$theVersion) {
$pdo = $this->pdo;
try {
$adminCl = new Admin();
DbUtils::overrulePrefix($prefix);
2020-11-19 23:10:26 +01:00
Version::updateVersion($pdo, $theVersion);
2020-11-19 22:47:44 +01:00
return true;
} catch (PDOException $e) {
return false;
}
}
function signLastBillId() {
$pdo = $this->pdo;
$this->basedb->signLastBillid($pdo);
}
2020-11-19 23:03:04 +01:00
2020-11-19 22:47:44 +01:00
public function getCurrentVersion() {
try {
$pdo = $this->pdo;
$sql = "SELECT setting FROM %config% WHERE name=?";
$stmt = $pdo->prepare($this->basedb->resolveTablenamesInSqlString($sql));
$stmt->execute(array("version"));
$row = $stmt->fetchObject();
return($row->setting);
} catch (Exception $e) {
return null;
}
}
2020-11-19 22:50:09 +01:00
public function isTherePreviousVersion($db,$prefix) {
try {
$pdo = $this->pdo;
$sql = "SELECT count(*) as thecount FROM information_schema.tables WHERE table_schema = '$db' AND table_name = '" . $prefix . "config' LIMIT 1";
$stmt = $pdo->prepare($this->basedb->resolveTablenamesInSqlString($sql));
$stmt->execute();
$row = $stmt->fetchObject();
if ($row->thecount == 1) {
return true;
} else {
return false;
}
} catch (Exception $e) {
return false;
}
}
2020-11-19 22:59:47 +01:00
2020-11-19 22:47:44 +01:00
2020-11-19 23:03:48 +01:00
function insertUser($username,$adminpass,$roleid,$lang,$prefertablemap) {
2020-11-19 22:47:44 +01:00
$md5adminpass = md5($adminpass);
$pdo = $this->pdo;
2020-11-19 23:03:48 +01:00
$userInsertSql = "INSERT INTO `%user%` (`username` , `userpassword`, `roleid`,`language`,`prefertablemap`,`keeptypelevel`,`extrasapplybtnpos`,`showplusminus`,`preferimgdesk`,`preferimgmobile`,`active`) "
. "VALUES (?,?,?,?,?,?,'1','1','1','1','1')";
2020-11-19 22:47:44 +01:00
$stmt = $pdo->prepare(DbUtils::substTableAlias($userInsertSql));
2020-11-19 23:10:13 +01:00
$stmt->execute(array($username,$md5adminpass,$roleid,$lang,$prefertablemap,1));
2020-11-19 22:47:44 +01:00
$newUserIdForHist = $pdo->lastInsertId();
2020-11-19 23:03:48 +01:00
HistFiller::createUserInHist($pdo, $newUserIdForHist);
2020-11-19 22:47:44 +01:00
}
2020-11-19 22:59:47 +01:00
2020-11-19 22:47:44 +01:00
function testDbConnection($host,$dbname,$user,$pass) {
2020-11-19 22:59:47 +01:00
$pdo = $this->openDbAndReturnPdo($host,$dbname,$user,$pass);
if (!is_null($pdo)) {
echo json_encode("OK");
} else {
echo json_encode("ERROR");
}
2020-11-19 22:47:44 +01:00
}
function writeConfigFile($host,$db,$user,$password,$prefix) {
2020-11-19 23:03:59 +01:00
$errorlevel = "<?php\nerror_reporting(E_ERROR);\n\n"; // development: E_ALL
2020-11-19 22:47:44 +01:00
2020-11-19 22:59:47 +01:00
$hostlines = "// Zum Aufbau der Verbindung zur Datenbank\n";
2020-11-19 22:47:44 +01:00
$hostlines .= "// die Daten erhalten Sie von Ihrem Provider\n";
$hostlines .= "defined('MYSQL_HOST') || define ( 'MYSQL_HOST','$host' );";
2020-11-19 22:59:47 +01:00
$userlines = "defined('MYSQL_USER') || define ( 'MYSQL_USER', '$user' );";
2020-11-19 22:47:44 +01:00
$dbpasslines = "defined('MYSQL_PASSWORD') || define ( 'MYSQL_PASSWORD', '$password' );";
2020-11-19 22:59:47 +01:00
$dblines = "defined('MYSQL_DB') || define ( 'MYSQL_DB', '$db' );";
2020-11-19 22:47:44 +01:00
$dbloglines = "defined('LOG') || define ( 'LOG', false );";
$prefixlines = "defined('TAB_PREFIX') || define ('TAB_PREFIX', '$prefix');";
$installstatusline = "defined('INSTALLSTATUS') || define ('INSTALLSTATUS', 'installed');";
$configText = "$errorlevel\n$hostlines\n$userlines\n$dbpasslines\n$dblines\n$dbloglines\n$prefixlines\n$installstatusline\n?>";
file_put_contents("../php/config.php", $configText);
try {
file_put_contents("../php/config1.php", $configText);
} catch (Exception $e) {
// nothing
}
}
static function insertSampleMenu($pdo,$adminCl) {
2020-11-19 23:03:38 +01:00
Basedb::loadSampleProdImages($pdo);
2020-11-19 22:47:44 +01:00
$menu = file_get_contents("../customer/speisekarte.txt");
2020-11-19 23:03:38 +01:00
$adminCl->fillSpeisekarteCore($pdo, $menu, false);
2020-11-19 22:47:44 +01:00
}
function insertSample($level,$lang,$adminpass,$workflow,$timezone) {
$pdo = $this->pdo;
$adminCl = new Admin();
$adminCl::overruleTimeZone($timezone);
$adminCl->changeOneConfigDbItem($pdo,"workflowconfig",$workflow,"%config%",true);
if ($level == 1) {
// nothing to do - empty db
} else {
$roomTxt1 = array("Raum 1 (Tischkarte)","Room 1 (table map)","Espacio 1 (mapa de mesas)");
$roomTxt2 = array("Raum 2 (Tischbuttons)","Room 2 (table buttons)","Espacio (botones des mesas)");
$tableTxt = array("Tisch","Table","Mesa");
$waiterTxt = array("Karl Kellner","Walter Waiter","Carlo Camarero");
$cookTxt = array("Koch 1","Charlie Cook","Cocinero 1");
$bossTxt = array("Charlie Chef","Maggy Manager","Jefe");
$sql = "INSERT INTO `%room%` (`id`, `roomname`) VALUES (?,?)";
$stmt = $pdo->prepare(DbUtils::substTableAlias($sql));
$stmt->execute(array(1,$roomTxt1[$lang]));
if ($level == 3) {
$stmt->execute(array(2,$roomTxt2[$lang]));
}
$sql = "INSERT INTO `%resttables%` (`id` , `tableno`, `roomid`) VALUES (? ,?,?)";
$stmt = $pdo->prepare(DbUtils::substTableAlias($sql));
for ($i=1;$i<7;$i++) {
$stmt->execute(array($i,$tableTxt[$lang] . " $i",1));
if ($level == 3) {
$stmt->execute(array($i + 6,$tableTxt[$lang] . " " . ($i + 6),2));
}
}
if ($level == 3) {
$sql = "INSERT INTO `%tablemaps%` (`id` , `roomid`, `img`,`sizex`,`sizey`) VALUES (NULL ,?,?,?,?)";
$stmt = $pdo->prepare(DbUtils::substTableAlias($sql));
$room = file_get_contents("../customer/innenraum.png");
$stmt->execute(array(1,$room,739,490));
$sql = "INSERT INTO `%tablepos%` (`id` , `tableid`, `x`,`y`) VALUES (NULL ,?,?,?)";
$stmt = $pdo->prepare(DbUtils::substTableAlias($sql));
$stmt->execute(array(1,70,74));
$stmt->execute(array(2,9,57));
$stmt->execute(array(3,19,37));
$stmt->execute(array(4,30,21));
$stmt->execute(array(5,49,21));
$stmt->execute(array(6,76,22));
}
if ($workflow == 2) {
2020-11-19 23:03:48 +01:00
$roleid = Roles::insertWorkWaiterRole($pdo);
$this->insertUser($waiterTxt[$lang], $adminpass, $roleid, $lang, 1);
2020-11-19 22:47:44 +01:00
if ($level == 3) {
2020-11-19 23:03:48 +01:00
$roleid = Roles::insertWorkManagerRole($pdo);
$this->insertUser($bossTxt[$lang], $adminpass, $roleid, $lang, 1);
2020-11-19 22:47:44 +01:00
}
} else {
2020-11-19 23:03:48 +01:00
$roleid = Roles::insertDigiWaiterRole($pdo);
$this->insertUser($waiterTxt[$lang], $adminpass, $roleid, $lang, 1);
2020-11-19 22:47:44 +01:00
if ($level == 3) {
2020-11-19 23:03:48 +01:00
$roleid = Roles::insertCookRole($pdo);
$this->insertUser($cookTxt[$lang], $adminpass, $roleid, $lang, 1);
$roleid = Roles::insertDigiManagerRole($pdo);
$this->insertUser($bossTxt[$lang], $adminpass, $roleid, $lang, 1);
2020-11-19 22:47:44 +01:00
}
}
2020-11-19 23:10:06 +01:00
$this->basedb->initTableOrder($pdo);
$this->basedb->initRoomOrder($pdo);
2020-11-19 22:47:44 +01:00
$logoimg = file_get_contents("../customer/logo.png");
$sql = "INSERT INTO %logo% (id,name,setting) VALUES(1,?,?)";
$stmt = $pdo->prepare(DbUtils::substTableAlias($sql));
$stmt->execute(array("logoimg",$logoimg));
self::insertSampleMenu($pdo,$adminCl);
}
2020-11-19 23:03:38 +01:00
if ($level == 1) {
$sql = "UPDATE %user% SET preferimgdesk=?,preferimgmobile=?";
$stmt = $pdo->prepare(DbUtils::substTableAlias($sql));
$stmt->execute(array(0,0));
}
2020-11-19 22:47:44 +01:00
}
}
$command = $_GET["command"];
if ($command == 'checkWriteAccess') {
$checker = new Checks();
$checker->checkWriteAccess();
} else if ($command == 'checkPhpStatus') {
$checker = new InstallAdmin();
$checker->checkPhpStatus();
} else if ($command == 'testDbConnection') {
$admin = new InstallAdmin();
try {
2020-11-19 22:59:47 +01:00
if (isset($_POST['host']) && isset($_POST['dbname']) && isset($_POST['user']) && isset($_POST['pass'])) {
2020-11-19 22:47:44 +01:00
$admin->testDbConnection($_POST['host'],$_POST['dbname'],$_POST['user'],$_POST['pass']);
} else {
echo json_encode("ERROR");
}
} catch (Exception $e) {
echo json_encode("ERROR");
}
} else if ($command == 'getConfig') {
$configWriter = new ConfigWriter();
$configWriter->getConfigVals();
} else if ($command == 'install') {
2020-11-19 23:10:26 +01:00
DbUtils::overrulePrefix($_POST['prefix']);
2020-11-19 23:11:36 +01:00
DbUtils::overruleDbName($_POST['db']);
2020-11-19 22:47:44 +01:00
$admin = new InstallAdmin();
$pdo = $admin->openDbAndReturnPdo($_POST['host'],$_POST['db'],$_POST['user'],$_POST['password']);
$admin->setPdo($pdo);
$admin->setPrefix($_POST['prefix']);
$admin->setTimeZone($_POST['timezone']);
DbUtils::overruleTimeZone($_POST['timezone']);
DbUtils::overrulePrefix($_POST['prefix']);
2020-11-19 23:10:15 +01:00
set_time_limit(60*10);
2020-11-19 23:10:26 +01:00
$basedb = new Basedb();
$basedb->createAndIntializeTables($pdo,$_POST['point'],$_POST['lang'],$_POST['currency'],$_POST['timezone']);
$updResult = Version::runUpdateProcess($pdo, $_POST['prefix'], $_POST['db'],null,false);
if ($updResult["status"] != "OK") {
echo json_encode("Fehler beim Update: " . $updResult["msg"]);
return;
}
2020-11-19 22:47:44 +01:00
$admin->signLastBillId();
2020-11-19 23:12:07 +01:00
2020-11-19 23:03:48 +01:00
$roleid = Roles::insertAdminRole($pdo);
$admin->insertUser("admin", $_POST['adminpass'], $roleid, $_POST['lang'], 1);
2020-11-19 22:47:44 +01:00
$admin->writeConfigFile($_POST['host'],$_POST['db'],$_POST['user'],$_POST['password'],$_POST['prefix']);
2020-11-19 23:00:46 +01:00
2020-11-19 22:47:44 +01:00
if(session_id() == '') {
session_start();
}
session_destroy();
2020-11-19 23:10:26 +01:00
$ok = Admin::optimizeCore($pdo);
if ($ok["status"] == "OK") {
2020-11-19 22:47:44 +01:00
echo json_encode("OK");
2020-11-19 23:10:26 +01:00
} else {
echo json_encode("Fehler beim Update: " . $ok["msg"]);
}
2020-11-19 22:47:44 +01:00
} else if ($command == 'insertsamplecontent') {
try {
2020-11-19 23:10:26 +01:00
DbUtils::overrulePrefix($_POST['prefix']);
2020-11-19 22:47:44 +01:00
$admin = new InstallAdmin();
$pdo = $admin->openDbAndReturnPdo($_POST['host'],$_POST['db'],$_POST['user'],$_POST['password']);
$admin->setPdo($pdo);
$admin->setPrefix($_POST['prefix']);
$admin->setTimeZone($_POST["timezone"]);
$admin->insertSample(intval($_POST["level"]),intval($_POST["lang"]),$_POST['adminpass'],$_POST["workflow"],$_POST["timezone"]);
echo json_encode("OK");
}
catch (PDOException $e) {
echo json_encode("ERROR: $e");
}
} else if ($command == 'gettimezones') {
$timezone_identifiers = DateTimeZone::listIdentifiers();
$zones = array();
for ($i=0; $i < count($timezone_identifiers); $i++) {
$zones[] = $timezone_identifiers[$i];
}
echo json_encode($zones);
} else if ($command == 'update') {
2020-11-19 23:10:15 +01:00
set_time_limit(60*30);
2020-11-19 23:12:07 +01:00
$installerVersion = "1.5.14";
2020-11-19 22:58:17 +01:00
2020-11-19 22:47:44 +01:00
$admin = new InstallAdmin();
$pdo = $admin->openDbAndReturnPdo($_POST['host'],$_POST['db'],$_POST['user'],$_POST['password']);
$admin->setPdo($pdo);
$admin->setPrefix($_POST['prefix']);
2020-11-19 23:10:26 +01:00
DbUtils::overrulePrefix($_POST['prefix']);
2020-11-19 22:47:44 +01:00
2020-11-19 22:50:09 +01:00
$isPreviousInstallation = $admin->isTherePreviousVersion($_POST['db'],$_POST['prefix']);
if (!$isPreviousInstallation) {
echo json_encode("Stimmt der Tabellenpräfix?");
return;
}
2020-11-19 22:47:44 +01:00
$version = $admin->getCurrentVersion();
2020-11-19 22:58:17 +01:00
if ($version == $installerVersion) {
echo json_encode("Version bereits installiert");
return;
}
2020-11-19 22:47:44 +01:00
if (is_null($version)) {
echo json_encode("Version nicht bestimmbar");
return;
}
2020-11-19 23:10:26 +01:00
$updResult = Version::runUpdateProcess($pdo, $_POST['prefix'], $_POST['db'],null,true);
2020-11-19 22:52:55 +01:00
2020-11-19 22:47:44 +01:00
if(session_id() == '') {
session_start();
}
session_destroy();
2020-11-19 23:00:46 +01:00
2020-11-19 23:02:42 +01:00
$autoupdate = $_POST["autoupdate"];
try {
if ($autoupdate == 1) {
unlink("../install/installer.php");
if (file_exists("../install/phpinfo.php")) {
unlink("../install/phpinfo.php");
}
rmdir("../install");
}
} catch (Exception $e) {
echo json_encode("Install-Verzeichnis lässt sich nicht löschen: ". $e->getMessage());
return;
}
2020-11-19 23:10:26 +01:00
if ($updResult["status"] == "OK") {
2020-11-19 22:47:44 +01:00
$admin->writeConfigFile($_POST['host'],$_POST['db'],$_POST['user'],$_POST['password'],$_POST['prefix']);
2020-11-19 23:10:26 +01:00
$ok = Admin::optimizeCore($pdo);
if ($ok["status"] == "OK") {
2020-11-19 22:47:44 +01:00
echo json_encode("OK");
} else {
2020-11-19 23:10:26 +01:00
echo json_encode("Fehler beim Update: " . $ok["msg"]);
}
} else {
echo json_encode("Fehler beim Update: " . $updResult["msg"]);
2020-11-19 22:47:44 +01:00
}
2020-11-19 23:03:38 +01:00
}