phpnuxbill/update.php

296 lines
9.5 KiB
PHP
Raw Normal View History

2023-08-01 16:50:28 +08:00
<?php
/**
* PHP Mikrotik Billing (https://github.com/hotspotbilling/phpnuxbill/)
*
* This script is for updating PHPNuxBill
**/
session_start();
include "config.php";
2023-08-01 16:50:28 +08:00
2024-07-29 17:19:43 +08:00
if($db_password != null && ($db_pass == null || empty($db_pass))){
// compability for old version
$db_pass = $db_password;
}
2024-02-20 12:34:08 +08:00
if (empty($update_url)) {
$update_url = 'https://github.com/hotspotbilling/phpnuxbill/archive/refs/heads/master.zip';
}
2023-08-01 16:50:28 +08:00
2024-05-21 11:19:19 +08:00
if(isset($_REQUEST['update_url']) && !empty($_REQUEST['update_url'])){
$update_url = $_REQUEST['update_url'];
2024-05-21 11:56:11 +08:00
$_SESSION['update_url'] = $update_url;
}
if(isset($_SESSION['update_url']) && !empty($_SESSION['update_url']) && $_SESSION['update_url'] != $update_url){
$update_url = $_SESSION['update_url'];
2024-05-21 11:19:19 +08:00
}
2023-08-16 14:58:20 +08:00
2023-08-01 16:50:28 +08:00
if (!isset($_SESSION['aid']) || empty($_SESSION['aid'])) {
r2("./?_route=login&You_are_not_admin", 'e', 'You are not admin');
}
set_time_limit(-1);
if (!is_writeable(pathFixer('system/cache/'))) {
r2("./?_route=community", 'e', 'Folder system/cache/ is not writable');
}
if (!is_writeable(pathFixer('.'))) {
r2("./?_route=community", 'e', 'Folder web is not writable');
}
$step = $_GET['step'];
$continue = true;
if (!extension_loaded('zip')) {
$msg = "No PHP ZIP extension is available";
$msgType = "danger";
$continue = false;
}
$file = pathFixer('system/cache/phpnuxbill.zip');
2024-02-20 12:34:08 +08:00
$folder = pathFixer('system/cache/phpnuxbill-' . basename($update_url, ".zip") . '/');
2023-08-16 14:58:20 +08:00
2023-08-01 17:05:52 +08:00
if (empty($step)) {
2023-08-01 16:50:28 +08:00
$step++;
2023-08-01 17:05:52 +08:00
} else if ($step == 1) {
2023-08-01 16:50:28 +08:00
if (file_exists($file)) unlink($file);
// Download update
$fp = fopen($file, 'w+');
$ch = curl_init($update_url);
2023-08-01 16:50:28 +08:00
curl_setopt($ch, CURLOPT_POST, 0);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 600);
curl_setopt($ch, CURLOPT_TIMEOUT, 600);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_FILE, $fp);
curl_exec($ch);
curl_close($ch);
fclose($fp);
2023-08-01 17:05:52 +08:00
if (file_exists($file)) {
2023-08-01 16:50:28 +08:00
$step++;
2023-08-01 17:05:52 +08:00
} else {
2023-08-01 16:50:28 +08:00
$msg = "Failed to download Update file";
$msgType = "danger";
$continue = false;
}
2023-08-01 17:05:52 +08:00
} else if ($step == 2) {
2023-08-01 16:50:28 +08:00
$zip = new ZipArchive();
$zip->open($file);
$zip->extractTo(pathFixer('system/cache/'));
$zip->close();
2023-08-01 17:05:52 +08:00
if (file_exists($folder)) {
2023-08-01 16:50:28 +08:00
$step++;
2023-08-01 17:05:52 +08:00
} else {
2023-08-01 16:50:28 +08:00
$msg = "Failed to extract update file";
$msgType = "danger";
$continue = false;
}
// remove downloaded zip
if (file_exists($file)) unlink($file);
2023-08-01 17:05:52 +08:00
} else if ($step == 3) {
2024-02-27 14:10:06 +08:00
deleteFolder('system/autoload/');
deleteFolder('system/vendor/');
deleteFolder('ui/ui/');
2023-08-01 16:50:28 +08:00
copyFolder($folder, pathFixer('./'));
deleteFolder('install/');
deleteFolder($folder);
2023-08-01 17:05:52 +08:00
if (!file_exists($folder . pathFixer('/system/'))) {
2023-08-01 16:50:28 +08:00
$step++;
2023-08-01 17:05:52 +08:00
} else {
2023-08-01 16:50:28 +08:00
$msg = "Failed to install update file.";
$msgType = "danger";
$continue = false;
}
2023-08-09 11:02:34 +08:00
} else if ($step == 4) {
if (file_exists("system/updates.json")) {
$db = new pdo(
"mysql:host=$db_host;dbname=$db_name",
$db_user,
2024-07-29 10:06:27 +08:00
$db_pass,
2023-08-09 11:02:34 +08:00
array(PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION)
);
$updates = json_decode(file_get_contents("system/updates.json"), true);
$dones = [];
if (file_exists("system/cache/updates.done.json")) {
$dones = json_decode(file_get_contents("system/cache/updates.done.json"), true);
}
foreach ($updates as $version => $queries) {
if (!in_array($version, $dones)) {
foreach ($queries as $q) {
2024-02-20 12:34:08 +08:00
try {
$db->exec($q);
} catch (PDOException $e) {
2023-08-16 10:25:15 +08:00
//ignore, it exists already
}
2023-08-09 11:02:34 +08:00
}
$dones[] = $version;
}
}
file_put_contents("system/cache/updates.done.json", json_encode($dones));
}
$step++;
2023-08-01 17:05:52 +08:00
} else {
2024-02-20 11:07:07 +08:00
$path = 'ui/compiled/';
2024-02-20 12:34:08 +08:00
$files = scandir($path);
foreach ($files as $file) {
if (is_file($path . $file)) {
unlink($path . $file);
}
}
2023-08-01 16:54:24 +08:00
$version = json_decode(file_get_contents('version.json'), true)['version'];
$continue = false;
2023-08-01 16:50:28 +08:00
}
function pathFixer($path)
{
return str_replace("/", DIRECTORY_SEPARATOR, $path);
}
function r2($to, $ntype = 'e', $msg = '')
{
if ($msg == '') {
header("location: $to");
2024-04-22 11:16:21 +08:00
die();
2023-08-01 16:50:28 +08:00
}
$_SESSION['ntype'] = $ntype;
$_SESSION['notify'] = $msg;
header("location: $to");
2024-04-22 11:16:21 +08:00
die();
2023-08-01 16:50:28 +08:00
}
function copyFolder($from, $to, $exclude = [])
{
$files = scandir($from);
foreach ($files as $file) {
if (is_file($from . $file) && !in_array($file, $exclude)) {
if (file_exists($to . $file)) unlink($to . $file);
rename($from . $file, $to . $file);
} else if (is_dir($from . $file) && !in_array($file, ['.', '..'])) {
if (!file_exists($to . $file)) {
mkdir($to . $file);
}
copyFolder($from . $file . DIRECTORY_SEPARATOR, $to . $file . DIRECTORY_SEPARATOR);
}
}
}
function deleteFolder($path)
{
$files = scandir($path);
foreach ($files as $file) {
if (is_file($path . $file)) {
unlink($path . $file);
} else if (is_dir($path . $file) && !in_array($file, ['.', '..'])) {
2023-08-01 17:05:52 +08:00
deleteFolder($path . $file . DIRECTORY_SEPARATOR);
2023-08-01 16:50:28 +08:00
rmdir($path . $file);
}
}
rmdir($path);
}
?>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
<title>PHPNuxBill Updater</title>
<link rel="shortcut icon" href="ui/ui/images/logo.png" type="image/x-icon" />
<link rel="stylesheet" href="ui/ui/styles/bootstrap.min.css">
<link rel="stylesheet" href="ui/ui/fonts/ionicons/css/ionicons.min.css">
<link rel="stylesheet" href="ui/ui/fonts/font-awesome/css/font-awesome.min.css">
2023-12-21 14:44:10 +08:00
<link rel="stylesheet" href="ui/ui/styles/modern-AdminLTE.min.css">
2024-02-20 11:07:07 +08:00
2023-08-01 17:05:52 +08:00
<?php if ($continue) { ?>
2023-08-09 11:02:34 +08:00
<meta http-equiv="refresh" content="3; ./update.php?step=<?= $step ?>">
2023-08-01 16:50:28 +08:00
<?php } ?>
<style>
::-moz-selection {
/* Code for Firefox */
color: red;
background: yellow;
}
::selection {
color: red;
background: yellow;
}
</style>
</head>
<body class="hold-transition skin-blue">
<div class="container">
<section class="content-header">
<h1 class="text-center">
2023-08-28 16:46:25 +08:00
Update PHPNuxBill
2023-08-01 16:50:28 +08:00
</h1>
</section>
<section class="content">
<div class="row">
<div class="col-md-4"></div>
<div class="col-md-4">
2023-08-01 17:05:52 +08:00
<?php if (!empty($msgType) && !empty($msg)) { ?>
<div class="alert alert-<?= $msgType ?>" role="alert">
<?= $msg ?>
2023-08-01 16:50:28 +08:00
</div>
<?php } ?>
2023-08-16 10:27:36 +08:00
<?php if ($continue || $step == 5) { ?>
2023-08-01 17:05:52 +08:00
<?php if ($step == 1) { ?>
<div class="panel panel-primary">
<div class="panel-heading">Step 1</div>
<div class="panel-body">
Downloading update<br>
Please wait....
</div>
2023-08-01 16:50:28 +08:00
</div>
2023-08-01 17:05:52 +08:00
<?php } else if ($step == 2) { ?>
<div class="panel panel-primary">
<div class="panel-heading">Step 2</div>
<div class="panel-body">
extracting<br>
Please wait....
</div>
2023-08-01 16:50:28 +08:00
</div>
2023-08-01 17:05:52 +08:00
<?php } else if ($step == 3) { ?>
<div class="panel panel-primary">
<div class="panel-heading">Step 3</div>
<div class="panel-body">
Installing<br>
Please wait....
</div>
2023-08-01 16:50:28 +08:00
</div>
2023-08-01 17:05:52 +08:00
<?php } else if ($step == 4) { ?>
2023-08-09 11:02:34 +08:00
<div class="panel panel-primary">
2023-08-15 17:21:35 +08:00
<div class="panel-heading">Step 4</div>
2023-08-09 11:02:34 +08:00
<div class="panel-body">
Updating database...
</div>
</div>
<?php } else if ($step == 5) { ?>
2023-08-16 15:00:37 +08:00
<div class="panel panel-success">
<div class="panel-heading">Update Finished</div>
2023-08-01 17:05:52 +08:00
<div class="panel-body">
PHPNuxBill has been updated to Version <b><?= $version ?></b>
</div>
2023-08-01 16:54:24 +08:00
</div>
2023-08-01 17:05:52 +08:00
<meta http-equiv="refresh" content="5; ./index.php?_route=dashboard">
<?php } ?>
2023-08-01 16:50:28 +08:00
<?php } ?>
</div>
</div>
</section>
<footer class="footer text-center">
PHPNuxBill by <a href="https://github.com/hotspotbilling/phpnuxbill" rel="nofollow noreferrer noopener" target="_blank">iBNuX</a>
</footer>
</div>
</body>
</html>