1
0
mirror of https://github.com/freescout-helpdesk/freescout.git synced 2024-11-24 19:33:07 +01:00
freescout/public/install.php

213 lines
7.0 KiB
PHP
Raw Normal View History

2018-10-22 15:02:33 +02:00
<?php
/**
* Application installer.
*/
ini_set('display_errors', 'Off');
2019-07-12 22:49:52 +02:00
if (preg_match("#^/public\/(.*)#", $_SERVER['REQUEST_URI'], $m) && !empty($m[1])) {
header("Location: /".$m[1]);
exit();
}
2018-10-22 15:02:33 +02:00
$root_dir = realpath(__DIR__.'/..').'/';
// Dotenv library for reading .env files
2018-11-12 10:12:57 +01:00
require_once $root_dir.'vendor/vlucas/phpdotenv/src/Dotenv.php';
require_once $root_dir.'vendor/vlucas/phpdotenv/src/Loader.php';
require_once $root_dir.'vendor/vlucas/phpdotenv/src/Validator.php';
require_once $root_dir.'vendor/vlucas/phpdotenv/src/Exception/ExceptionInterface.php';
require_once $root_dir.'vendor/vlucas/phpdotenv/src/Exception/InvalidCallbackException.php';
require_once $root_dir.'vendor/vlucas/phpdotenv/src/Exception/InvalidFileException.php';
require_once $root_dir.'vendor/vlucas/phpdotenv/src/Exception/InvalidPathException.php';
require_once $root_dir.'vendor/vlucas/phpdotenv/src/Exception/ValidationException.php';
2018-10-22 15:02:33 +02:00
// Symfony proces
//require_once($root_dir.'vendor/symfony/process/Process.php');
// Laravel Encrypter
// require_once($root_dir.'vendor/laravel/framework/src/Illuminate/Contracts/Encryption/Encrypter.php');
// require_once($root_dir.'vendor/laravel/framework/src/Illuminate/Encryption/Encrypter.php');
function generateRandomKey()
{
return 'base64:'.base64_encode(
//Encrypter::generateKey('AES-256-CBC');
random_bytes(32)
2018-10-22 15:02:33 +02:00
);
}
function writeNewEnvironmentFileWith($key, $environmentFilePath)
{
2018-11-12 10:12:57 +01:00
file_put_contents($environmentFilePath, preg_replace(
'/^APP_KEY=/m',
2018-10-22 15:02:33 +02:00
'APP_KEY='.$key,
file_get_contents($environmentFilePath)
));
}
2018-11-12 10:12:57 +01:00
// Get app key
2018-10-22 15:02:33 +02:00
function getAppKey($root_dir, $check_cache = true)
{
2018-11-12 10:12:57 +01:00
// First check APP_KEY in cache
if ($check_cache && file_exists($root_dir.'bootstrap/cache/config.php')) {
$config = include $root_dir.'bootstrap/cache/config.php';
if (!empty($config)) {
if (!empty($config['app']['key'])) {
return $config['app']['key'];
} else {
return '';
}
}
}
// Read .env file into $_ENV
try {
$dotenv = new Dotenv\Dotenv($root_dir);
// If using load() if $_ENV['APP_KEY'] was present in .env before it will not be updated when reading
$dotenv->overload();
} catch (\Exception $e) {
// Do nothing
}
if (!empty($_ENV['APP_KEY'])) {
return $_ENV['APP_KEY'];
} else {
return '';
}
2018-10-22 15:02:33 +02:00
}
function clearCache($root_dir)
{
2018-11-12 10:12:57 +01:00
if (file_exists($root_dir.'bootstrap/cache/config.php')) {
unlink($root_dir.'bootstrap/cache/config.php');
}
2018-10-22 15:02:33 +02:00
}
2018-11-10 06:31:45 +01:00
function showError($msg)
{
2018-11-12 10:12:57 +01:00
echo <<<HTML
2018-11-10 06:31:45 +01:00
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>FreeScout Installer</title>
<link href="/css/fonts.css" rel="stylesheet"/>
<link href="/installer/css/fontawesome.css" rel="stylesheet"/>
<link href="/installer/css/style.min.css" rel="stylesheet"/>
</head>
<body>
<div class="master">
<div class="box">
<div class="header">
<h1 class="header__title">FreeScout Installer</h1>
</div>
<div class="main">
$msg
</div>
</div>
</div>
</body>
</html>
HTML;
}
function showPermissionsError()
{
2018-11-12 10:12:57 +01:00
$root_dir_no_slash = realpath(__DIR__.'/..');
2018-11-10 06:31:45 +01:00
2018-11-12 10:12:57 +01:00
showError('Web installer could not write data into <strong>'.$root_dir_no_slash.'/.env</strong> file. Please give your web server user (<strong>'.get_current_user().'</strong>) write permissions in <code>'.$root_dir_no_slash.'</code> folder:<br/><br/>
2018-11-10 06:31:45 +01:00
<textarea rows="4" readonly="readonly" style="font-size:12px;">sudo chgrp '.get_current_user().' '.$root_dir_no_slash.'
sudo chmod ug+rwx '.$root_dir_no_slash.'</textarea><br/>If it does not help, please follow <a href="http://freescout.net/install/#82-manual-installation" target="_blank">Manual installation</a> instructions.');
}
2019-06-15 16:52:34 +02:00
function getSubdirectory()
{
$subdirectory = $_SERVER['PHP_SELF'];
$filename = basename($_SERVER['SCRIPT_FILENAME']);
if (basename($_SERVER['SCRIPT_NAME']) === $filename) {
$subdirectory = $_SERVER['SCRIPT_NAME'];
} elseif (basename($_SERVER['PHP_SELF']) === $filename) {
$subdirectory = $_SERVER['PHP_SELF'];
} elseif (basename($_SERVER['ORIG_SCRIPT_NAME']) === $filename) {
$subdirectory = $_SERVER['ORIG_SCRIPT_NAME']; // 1and1 shared hosting compatibility
} else {
// Backtrack up the script_filename to find the portion matching
// php_self
$path = $_SERVER['PHP_SELF'];
$file = $_SERVER['SCRIPT_FILENAME'];
$segs = explode('/', trim($file, '/'));
$segs = array_reverse($segs);
$index = 0;
$last = \count($segs);
$subdirectory = '';
do {
$seg = $segs[$index];
$subdirectory = '/'.$seg.$subdirectory;
++$index;
} while ($last > $index && (false !== $pos = strpos($path, $subdirectory)) && 0 != $pos);
}
$subdirectory = str_replace('public/install.php', '', $subdirectory);
$subdirectory = str_replace('install.php', '', $subdirectory);
if (!$subdirectory) {
$subdirectory = '/';
}
return $subdirectory;
}
2018-10-22 15:02:33 +02:00
$app_key = getAppKey($root_dir);
// Generate APP_KEY
if (empty($app_key)) {
2018-11-12 10:12:57 +01:00
// Copy .env.example
if (!file_exists($root_dir.'.env')) {
2019-06-15 16:52:34 +02:00
// Check if .env.example eixists
if (!file_exists($root_dir.'.env.example')) {
showError('File <strong>'.$root_dir.'.env.example</strong> not found. Please make sure to copy this file from the application dist.');
exit();
}
2018-11-12 10:12:57 +01:00
copy($root_dir.'.env.example', $root_dir.'.env');
if (!file_exists($root_dir.'.env')) {
//echo 'Please copy <code>.env.example</code> file to <code>.env</code> and reload this page.';
showPermissionsError();
exit();
}
}
// Add APP_KEY= to the .env file if needed
// Without APP_KEY= the key will not be generated
if (!preg_match('/^APP_KEY=/m', file_get_contents($root_dir.'.env'))) {
$append_result = file_put_contents($root_dir.'.env', PHP_EOL.'APP_KEY=', FILE_APPEND);
if (!$append_result) {
//showError('Could not write APP_KEY to .env file. Please run the following commands in SSH console:<br/><code>php artisan key:generate</code><br/><code>php artisan freescout:clear-cache</code>');
showPermissionsError();
exit();
}
}
writeNewEnvironmentFileWith(generateRandomKey(), $root_dir.'.env');
// Clear cache
// We have to clear cache to avoid infinite redirects
clearCache($root_dir);
$app_key = getAppKey($root_dir, false);
2018-10-22 15:02:33 +02:00
}
if (!empty($app_key)) {
2018-11-12 10:12:57 +01:00
// When APP_KEY generated, redirect to /install
2019-06-15 16:52:34 +02:00
header('Location: '.getSubdirectory().'install');
2018-10-22 15:02:33 +02:00
} else {
2018-11-12 10:12:57 +01:00
showPermissionsError();
2018-10-22 15:02:33 +02:00
}
2018-11-12 10:12:57 +01:00
exit();