ordersprinter/webapp/install/installer.php

575 lines
17 KiB
PHP

<?php
error_reporting(E_ALL);
define ('IS_INSTALLMODE', '1');
if (is_readable("../php/config1.php")) {
require_once( "../php/config1.php" );
} else {
require_once( "../php/config.php" );
}
require_once ('../php/utilities/basedb.php');
require_once ('../php/utilities/HistFiller.php');
require_once ('../php/utilities/decimaldefs.php');
require_once ('../php/utilities/roles.php');
require_once ('../php/utilities/version.php');
require_once ('../php/admin.php');
require_once ('../php/closing.php');
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;
}
function openDbAndReturnPdo ($host,$db,$user,$password) {
$dsn = 'mysql:host=' . $host . ';dbname=' . $db;
$pdo = null;
try {
$pdo = new PDO($dsn, $user, $password);
$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
}
catch (PDOException $e) {
$pdo = null;
}
return $pdo;
}
function checkPhpStatus() {
$extensions = array("gd","mysqli","openssl","pdo_mysql","PDO","session","zlib","curl","zip","ftp","xml","iconv");
$missing = array();
$extensions_status = 1;
foreach($extensions as $anExtension) {
if (!extension_loaded($anExtension)) {
$missing[] = $anExtension;
$extensions_status = 0;
}
}
set_time_limit(60*5+1);
if(session_id() == '') {
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);
}
function setVersion($prefix,$theVersion) {
$pdo = $this->pdo;
try {
$adminCl = new Admin();
DbUtils::overrulePrefix($prefix);
Version::updateVersion($pdo, $theVersion);
return true;
} catch (PDOException $e) {
return false;
}
}
function signLastBillId() {
$pdo = $this->pdo;
$this->basedb->signLastBillid($pdo);
}
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;
}
}
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;
}
}
function insertUser($username,$adminpass,$roleid,$lang,$prefertablemap) {
$md5adminpass = md5($adminpass);
$pdo = $this->pdo;
$userInsertSql = "INSERT INTO `%user%` (`username` , `userpassword`, `roleid`,`language`,`prefertablemap`,`keeptypelevel`,`extrasapplybtnpos`,`showplusminus`,`preferimgdesk`,`preferimgmobile`,`mobiletheme`,`active`) "
. "VALUES (?,?,?,?,?,?,'1','1','1','1','8','1')";
$stmt = $pdo->prepare(DbUtils::substTableAlias($userInsertSql));
$stmt->execute(array($username,$md5adminpass,$roleid,$lang,$prefertablemap,1));
$newUserIdForHist = $pdo->lastInsertId();
HistFiller::createUserInHist($pdo, $newUserIdForHist);
}
function testDbConnection($host,$dbname,$user,$pass) {
$pdo = $this->openDbAndReturnPdo($host,$dbname,$user,$pass);
if (is_null($pdo)) {
echo json_encode(array("status" => "ERROR","msg" => "ERROR: DB-Zugriff"));
return;
}
$privileges = DbUtils::checkForInstallUpdateDbRights($pdo);
if ($privileges["status"] != "OK") {
echo json_encode(array("status" => "ERROR","msg" => "ERROR: Rechteabfrage"));
return;
}
$missingRights = "Fehlende Rechte:" . join(',',$privileges["msg"]);
echo json_encode(array("status" => "OK","msg" => $missingRights, "ok" => $privileges["ok"]));
}
function writeConfigFile($host,$db,$user,$password,$prefix) {
$errorlevel = "<?php\nerror_reporting(E_ERROR);\n\n"; // development: E_ALL
$hostlines = "// Zum Aufbau der Verbindung zur Datenbank\n";
$hostlines .= "// die Daten erhalten Sie von Ihrem Provider\n";
$hostlines .= "defined('MYSQL_HOST') || define ( 'MYSQL_HOST','$host' );";
$userlines = "defined('MYSQL_USER') || define ( 'MYSQL_USER', '$user' );";
$dbpasslines = "defined('MYSQL_PASSWORD') || define ( 'MYSQL_PASSWORD', '$password' );";
$dblines = "defined('MYSQL_DB') || define ( 'MYSQL_DB', '$db' );";
$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";
file_put_contents("../php/config.php", $configText);
try {
file_put_contents("../php/config1.php", $configText);
} catch (Exception $e) {
// nothing
}
}
static function insertSampleMenu($pdo,$adminCl) {
Basedb::loadSampleProdImages($pdo);
$menu = file_get_contents("../customer/speisekarte.txt");
$adminCl->fillSpeisekarteCore($pdo, $menu, false);
}
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) {
$roleid = Roles::insertWorkWaiterRole($pdo);
$this->insertUser($waiterTxt[$lang], $adminpass, $roleid, $lang, 1);
if ($level == 3) {
$roleid = Roles::insertWorkManagerRole($pdo);
$this->insertUser($bossTxt[$lang], $adminpass, $roleid, $lang, 1);
}
} else {
$roleid = Roles::insertDigiWaiterRole($pdo);
$this->insertUser($waiterTxt[$lang], $adminpass, $roleid, $lang, 1);
if ($level == 3) {
$roleid = Roles::insertCookRole($pdo);
$this->insertUser($cookTxt[$lang], $adminpass, $roleid, $lang, 1);
$roleid = Roles::insertDigiManagerRole($pdo);
$this->insertUser($bossTxt[$lang], $adminpass, $roleid, $lang, 1);
}
}
$this->basedb->initTableOrder($pdo);
$this->basedb->initRoomOrder($pdo);
$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);
}
if ($level == 1) {
$sql = "UPDATE %user% SET preferimgdesk=?,preferimgmobile=?";
$stmt = $pdo->prepare(DbUtils::substTableAlias($sql));
$stmt->execute(array(0,0));
}
}
}
$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 {
if (isset($_POST['host']) && isset($_POST['dbname']) && isset($_POST['user']) && isset($_POST['pass'])) {
$admin->testDbConnection($_POST['host'],$_POST['dbname'],$_POST['user'],$_POST['pass']);
} else {
echo json_encode(array("status" => "ERROR","msg" => "ERROR"));
}
} catch (Exception $e) {
echo json_encode(array("status" => "ERROR","msg" => "ERROR"));
}
} else if ($command == 'getConfig') {
$configWriter = new ConfigWriter();
$configWriter->getConfigVals();
} else if ($command == 'defaultinstall') {
$tabprefix = "os_";
if (isset($_GET['prefix'])) {
$tabprefix = $_GET['prefix'];
}
$db = "ordersprinter";
if (isset($_GET['db'])) {
$db = $_GET['db'];
}
$dbuser = "os";
if (isset($_GET['dbuser'])) {
$dbuser = $_GET['dbuser'];
}
$dbpass = "dbpass";
if (isset($_GET['dbpass'])) {
$dbpass = $_GET['dbpass'];
}
DbUtils::overrulePrefix($tabprefix);
DbUtils::overruleDbName($db);
$admin = new InstallAdmin();
$pdo = $admin->openDbAndReturnPdo("localhost",$db,$dbuser,$dbpass);
$admin->setPdo($pdo);
$admin->setPrefix($tabprefix);
$admin->setTimeZone("Europe/Berlin");
DbUtils::overruleTimeZone($_POST['timezone']);
DbUtils::overrulePrefix($tabprefix);
set_time_limit(60*10);
$basedb = new Basedb();
$basedb->createAndIntializeTables($pdo,",",0,"Euro","Europe/Berlin");
$updResult = Version::runUpdateProcess($pdo, $tabprefix, $db,null,false);
if ($updResult["status"] != "OK") {
echo json_encode("Fehler beim Update: " . $updResult["msg"]);
return;
}
$dsfinvk_name = "Musterrestaurant";
$dsfinvk_street = "Beispielstrasse 123";
$dsfinvk_postalcode = "12345";
$dsfinvk_city = "Beispielstadt";
$dsfinvk_country = "Deutschland";
$dsfinvk_stnr = "123-456";
$dsfinvk_ustid = "123-ABC";
$paydeskid = 1;
$companyinfo = "$dsfinvk_name\n$dsfinvk_street\n$dsfinvk_postalcode $dsfinvk_city\n$dsfinvk_country\nStNR: $dsfinvk_stnr\nUStID:$dsfinvk_ustid";
$restaurantmode = 1;
$cancelcode = "123";
$printpass = md5("123");
$defaultview = 0;
$basedb->changeInitialConfig($pdo,$restaurantmode,$dsfinvk_name,$dsfinvk_street,$dsfinvk_postalcode,$dsfinvk_city,$dsfinvk_country,$dsfinvk_stnr,$dsfinvk_ustid,$paydeskid,$companyinfo,$defaultview,$cancelcode,$printpass);
$admin->signLastBillId();
$roleid = Roles::insertAdminRole($pdo);
$admin->insertUser("admin", "123", $roleid, 0, 1);
$admin->writeConfigFile("localhost",$db,$dbuser,$dbpass,$tabprefix);
if(session_id() == '') {
session_start();
}
session_destroy();
$ok = Admin::optimizeCore($pdo);
if ($ok["status"] == "OK") {
echo json_encode("OK");
} else {
echo json_encode("Fehler beim Update: " . $ok["msg"]);
}
} else if ($command == 'install') {
DbUtils::overrulePrefix($_POST['prefix']);
DbUtils::overruleDbName($_POST['db']);
$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']);
set_time_limit(60*10);
$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;
}
$dsfinvk_name = $_POST["dsfinvk_name"];
$dsfinvk_street = $_POST["dsfinvk_street"];
$dsfinvk_postalcode = $_POST["dsfinvk_postalcode"];
$dsfinvk_city = $_POST["dsfinvk_city"];
$dsfinvk_country = $_POST["dsfinvk_country"];
$dsfinvk_stnr = $_POST["dsfinvk_stnr"];
$dsfinvk_ustid = $_POST["dsfinvk_ustid"];
$paydeskid = $_POST["paydeskid"];
$companyinfo = "$dsfinvk_name\n$dsfinvk_street\n$dsfinvk_postalcode $dsfinvk_city\n$dsfinvk_country\nStNR: $dsfinvk_stnr\nUStID:$dsfinvk_ustid";
$restaurantmode = $_POST["restaurantmode"];
$cancelcode = $_POST["cancelcode"];
$printpass = md5($_POST["printpass"]);
$defaultview = $_POST["defaultview"];
$basedb->changeInitialConfig($pdo,$restaurantmode,$dsfinvk_name,$dsfinvk_street,$dsfinvk_postalcode,$dsfinvk_city,$dsfinvk_country,$dsfinvk_stnr,$dsfinvk_ustid,$paydeskid,$companyinfo,$defaultview,$cancelcode,$printpass);
$admin->signLastBillId();
$roleid = Roles::insertAdminRole($pdo);
$admin->insertUser("admin", $_POST['adminpass'], $roleid, $_POST['lang'], 1);
$admin->writeConfigFile($_POST['host'],$_POST['db'],$_POST['user'],$_POST['password'],$_POST['prefix']);
if(session_id() == '') {
session_start();
}
session_destroy();
$ok = Admin::optimizeCore($pdo);
if ($ok["status"] == "OK") {
echo json_encode("OK");
} else {
echo json_encode("Fehler beim Update: " . $ok["msg"]);
}
} else if ($command == 'insertsamplecontent') {
try {
DbUtils::overrulePrefix($_POST['prefix']);
$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 == 'defaultinsertsamplecontent') {
$tabprefix = "os_";
if (isset($_GET['prefix'])) {
$tabprefix = $_GET['prefix'];
}
$db = "ordersprinter";
if (isset($_GET['db'])) {
$db = $_GET['db'];
}
$dbuser = "os";
if (isset($_GET['dbuser'])) {
$dbuser = $_GET['dbuser'];
}
$dbpass = "dbpass";
if (isset($_GET['dbpass'])) {
$dbpass = $_GET['dbpass'];
}
try {
DbUtils::overrulePrefix($tabprefix);
$admin = new InstallAdmin();
$pdo = $admin->openDbAndReturnPdo("localhost",$db,$dbuser,$dbpass);
$admin->setPdo($pdo);
$admin->setPrefix($tabprefix);
$admin->setTimeZone("Europe/Berlin");
$admin->insertSample(3,0,"123",0,"Europe/Berlin");
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') {
$configFile = __DIR__ . "/../php/config.php";
if (!is_writable($configFile)) {
echo json_encode("Datei config.php im php-Verzeichnis ist nicht beschreibbar - Update nicht möglich");
return;
}
set_time_limit(60*30);
$installerVersion = "2.0.7";
$admin = new InstallAdmin();
$pdo = $admin->openDbAndReturnPdo($_POST['host'],$_POST['db'],$_POST['user'],$_POST['password']);
$admin->setPdo($pdo);
$admin->setPrefix($_POST['prefix']);
DbUtils::overrulePrefix($_POST['prefix']);
$isPreviousInstallation = $admin->isTherePreviousVersion($_POST['db'],$_POST['prefix']);
if (!$isPreviousInstallation) {
echo json_encode("Stimmt der Tabellenpräfix?");
return;
}
$version = $admin->getCurrentVersion();
if ($version == $installerVersion) {
echo json_encode("Version bereits installiert");
return;
}
if (is_null($version)) {
echo json_encode("Version nicht bestimmbar");
return;
}
$updResult = Version::runUpdateProcess($pdo, $_POST['prefix'], $_POST['db'],null,true);
if(session_id() == '') {
session_start();
}
session_destroy();
$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;
}
if ($updResult["status"] == "OK") {
$admin->writeConfigFile($_POST['host'],$_POST['db'],$_POST['user'],$_POST['password'],$_POST['prefix']);
$ok = Admin::optimizeCore($pdo);
if ($ok["status"] == "OK") {
echo json_encode("OK");
} else {
echo json_encode("Fehler beim Update: " . $ok["msg"]);
}
} else {
echo json_encode("Fehler beim Update: " . $updResult["msg"]);
}
}