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

201 lines
8.2 KiB
PHP
Raw Normal View History

2018-11-10 09:56:06 +01:00
<?php
/**
* Application installer.
*/
ini_set('display_errors', 'On');
$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-11-10 09:56:06 +01:00
2018-11-12 10:12:57 +01:00
// Get app key
2018-11-10 09:56:06 +01: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';
2018-11-10 09:56:06 +01:00
2018-11-12 10:12:57 +01:00
if (!empty($config)) {
if (!empty($config['app']['key'])) {
return $config['app']['key'];
} else {
return '';
}
}
}
2018-11-10 09:56:06 +01:00
2018-11-12 10:12:57 +01:00
// 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
}
2018-11-10 09:56:06 +01:00
2018-11-12 10:12:57 +01:00
if (!empty($_ENV['APP_KEY'])) {
return $_ENV['APP_KEY'];
} else {
return '';
}
2018-11-10 09:56:06 +01:00
}
2019-07-03 15:12:46 +02:00
function clearCache($root_dir, $php_path)
2018-11-10 09:56:06 +01:00
{
2018-11-12 10:12:57 +01:00
if (file_exists($root_dir.'bootstrap/cache/config.php')) {
unlink($root_dir.'bootstrap/cache/config.php');
}
2019-07-03 15:12:46 +02:00
return shell_exec($php_path.' '.$root_dir.'artisan freescout:clear-cache');
2018-11-10 09:56:06 +01:00
}
$alerts = [];
$errors = [];
$app_key = $_POST['app_key'] ?? '';
if (!empty($_POST)) {
2019-07-03 15:12:46 +02:00
$php_path = 'php';
if (!empty($_POST['php_path'])) {
$php_path = $_POST['php_path'];
}
2018-11-12 10:12:57 +01:00
if (trim($app_key) != trim(getAppKey($root_dir))) {
$errors['app_key'] = 'Invalid App Key';
} else {
2019-07-03 15:12:46 +02:00
$cc_output = clearCache($root_dir, $php_path);
2018-11-12 10:12:57 +01:00
if ($_POST['action'] == 'cc') {
$alerts[] = [
'type' => 'success',
2019-07-03 15:12:46 +02:00
'text' => 'Cache cleared: <br/><pre>'.htmlspecialchars($cc_output).'</pre>',
2018-11-12 10:12:57 +01:00
];
} else {
if (!function_exists('shell_exec')) {
$alerts[] = [
'type' => 'danger',
'text' => '<code>shell_exec</code> function is unavailable. Can not run updating.',
];
} else {
try {
2018-11-10 09:56:06 +01:00
2018-11-12 10:12:57 +01:00
// First check PHP version
$version_output = shell_exec($php_path.' -v');
2018-11-10 09:56:06 +01:00
2018-11-12 10:12:57 +01:00
if (!strstr($version_output, 'PHP 7.')) {
$alerts[] = [
'type' => 'danger',
'text' => 'Incorrect PHP version (7.x is required):<br/><br/><pre>'.htmlspecialchars($version_output).'</pre>',
];
} else {
if ($_POST['action'] == 'update') {
// Update Now
$output = shell_exec($php_path.' '.$root_dir.'artisan freescout:update --force');
if (strstr($output, 'Broadcasting queue restart signal')) {
$alerts[] = [
'type' => 'success',
'text' => 'Updating finished:<br/><pre>'.htmlspecialchars($output).'</pre>',
];
} else {
$alerts[] = [
'type' => 'danger',
'text' => 'Something went wrong... Please <strong><a href="https://freescout.net/download/" target="_blank">download</a></strong> the latest version and extract it into your application folder replacing existing files. After that click "Migrate DB" button.<br/><br/><pre>'.htmlspecialchars($output).'</pre>',
];
}
} else {
// Migreate DB
$output = shell_exec($php_path.' '.$root_dir.'artisan migrate --force');
$alerts[] = [
'type' => 'success',
'text' => 'Migrating finished:<br/><br/><pre>'.htmlspecialchars($output).'</pre>',
];
}
}
} catch (\Exception $e) {
$alerts[] = [
'type' => 'danger',
'text' => 'Error occured: '.htmlspecialchars($e->getMessage()),
];
}
}
}
}
2018-11-10 09:56:06 +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">
2018-11-10 11:01:20 +01:00
<title>FreeScout Tools</title>
2018-11-10 09:56:06 +01:00
<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">
2018-11-10 11:01:20 +01:00
<h1 class="header__title">FreeScout Tools</h1>
2018-11-10 09:56:06 +01:00
</div>
<div class="main">
<?php if (!empty($alerts)): ?>
<?php foreach ($alerts as $alert): ?>
<div class="alert alert-<?php echo $alert['type'] ?>">
<?php echo $alert['text']; ?>
</div>
<?php endforeach ?>
<?php endif ?>
<form method="post" action="">
<div class="form-group <?php if (!empty($errors['app_key'])):?>has-error<?php endif ?>">
<label for="app_key">
<strong>App Key</strong> (from .env file)
</label>
<input type="text" name="app_key" value="<?php echo htmlentities($app_key); ?>" required="required"/>
<?php if (!empty($errors['app_key'])): ?>
<span class="error-block">
<i class="fa fa-fw fa-exclamation-triangle" aria-hidden="true"></i>
<?php echo $errors['app_key']; ?>
</span>
<?php endif ?>
</div>
<div class="form-group <?php if (!empty($errors['php_path'])):?>has-error<?php endif ?>">
<label for="php_path">
<strong>Path to PHP 7.x</strong> (example: /usr/local/php72/bin/php)
</label>
<input type="text" name="php_path" value="<?php echo htmlentities($_POST['php_path'] ?? ''); ?>" placeholder="(optional)"/>
<?php if (!empty($errors['php_path'])): ?>
<span class="error-block">
<i class="fa fa-fw fa-exclamation-triangle" aria-hidden="true"></i>
<?php echo $errors['php_path']; ?>
</span>
<?php endif ?>
</div>
<div class="buttons">
2018-11-10 11:01:20 +01:00
<button class="button" type="submit" name="action" value="update">
Update Now
</button>
<br/>
<button class="button" type="submit" name="action" value="cc">
Clear Cache
</button>
<button class="button" type="submit" name="action" value="migrate">
Migrate DB
2018-11-10 09:56:06 +01:00
</button>
</div>
</form>
</div>
</div>
</div>
</body>
</html>