<?php
define('FROM', 'https://time2.emphasize.de');
if ( (! empty($_SERVER['REQUEST_SCHEME']) && $_SERVER['REQUEST_SCHEME'] == 'https') ||
     (! empty($_SERVER['HTTPS']) && $_SERVER['HTTPS'] == 'on') ||
     (! empty($_SERVER['SERVER_PORT']) && $_SERVER['SERVER_PORT'] == '443') ) {  
  define('SCHEME', 'https');
} else {
  define('SCHEME', 'http');
}
define('DOMAIN', SCHEME . '://' . $_SERVER['SERVER_NAME']);

header('Content-Type: text/html; charset=utf-8');
ob_flush(); flush();

echo('checking requirements' . "<br/>\n"); ob_flush(); flush();
// check for required dependencies
if (!in_array('sqlite', PDO::getAvailableDrivers())) {
    echo('php sqlite extension not available, required for initial installation, thereafter mysql and pqsql can be configured.'. "<br/>\n"); ob_flush(); flush();
    exit();
}
echo('installing for self-hosting latest version from ' . FROM . "<br/>\n"); ob_flush(); flush();
$md5online = file_get_contents(FROM . '/updates/release.md5');
if (strlen(trim($md5online)) === 0) {
    echo('latest version fetch failed' . "<br/>\n"); ob_flush(); flush();
    exit();
}
file_put_contents(__DIR__ . '/release.zip', fopen(FROM . '/updates/release.zip', 'r'));
$md5downloaded = md5_file(__DIR__ . '/release.zip');
if (trim($md5downloaded) !== trim($md5online)) {
    echo('download corrupt, md5 check failed, aborting' . "<br/>\n");
    echo($md5downloaded . "<br/>\n");
    echo($md5online . "<br/>\n"); ob_flush(); flush();
    exit();
}
echo('md5 check success' . "<br/>\n"); ob_flush(); flush();
$zip = new ZipArchive;
$res = $zip->open(__DIR__ . '/release.zip');
if ($res === TRUE) {
  $zip->extractTo(__DIR__);
  $zip->close();
} else {
  echo('unzipping ' . __DIR__ . '/release.zip' . ' failed' . "<br/>\n"); ob_flush(); flush();
  exit();
}
echo('unzip success' . "<br/>\n"); ob_flush(); flush();

file_put_contents(__DIR__ . '/updates/release.md5', $md5downloaded);
unlink(__DIR__ . '/release.zip');
unlink(__FILE__);
unlink(__DIR__ . '/updates/self-hosting.php');
unlink(__DIR__ . '/updates/install.php');
$str = file_get_contents(__DIR__ . '/bag/index.php');
$str = preg_replace('/\$allowed = \[[^\]]*\];/', '$allowed = [\'' . DOMAIN. '\'];', $str);
file_put_contents(__DIR__ . '/bag/index.php', $str);
$str = file_get_contents(__DIR__ . '/event/index.php');
$str = preg_replace('/\$allowed = \[[^\]]*\];/', '$allowed = [\'' . DOMAIN. '\'];', $str);
file_put_contents(__DIR__ . '/event/index.php', $str);
$str = file_get_contents(__DIR__ . '/invoicing/index.php');
$str = preg_replace('/\$allowed = \[[^\]]*\];/', '$allowed = [\'' . DOMAIN. '\'];', $str);
file_put_contents(__DIR__ . '/invoicing/index.php', $str);
$str = file_get_contents(__DIR__ . '/storage/common.php');
$str = preg_replace('/\$allowed = \[[^\]]*\];/', '$allowed = [\'' . DOMAIN. '\'];', $str);
file_put_contents(__DIR__ . '/storage/common.php', $str);
$str = file_get_contents(__DIR__ . '/share/index.php');
$str = preg_replace('/\$allowed = \[[^\]]*\];/', '$allowed = [\'' . DOMAIN. '\'];', $str);
file_put_contents(__DIR__ . '/share/index.php', $str);
echo('adjusted allowed domains to ' . DOMAIN . "<br/>\n"); ob_flush(); flush();
echo('install completed' . "<br/>\n"); ob_flush(); flush();
sleep(5);
echo('<a href="./">redirecting to app</a>' . "<br/>\n"); ob_flush(); flush();
echo('<script>document.location.href="./";</script>' . "<br/>\n"); ob_flush(); flush();