Added upgrade.php and version checks

This commit is contained in:
Lukas Metzger 2016-02-06 13:23:28 +01:00
parent d13ea918cc
commit 6d93cbd628
5 changed files with 261 additions and 0 deletions

69
api/upgrade.php Normal file
View file

@ -0,0 +1,69 @@
<?php
/*
* Copyright 2016 Lukas Metzger <developer@lukas-metzger.com>.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
require_once '../config/config-default.php';
require_once '../lib/database.php';
require_once '../lib/checkversion.php';
$input = json_decode(file_get_contents('php://input'));
if(isset($input->action) && $input->action == "getVersions") {
$retval['from'] = getVersion($db);
$retval['to'] = getExpectedVersion();
}
if(isset($input->action) && $input->action == "requestUpgrade") {
$currentVersion = getVersion($db);
if($currentVersion < 1) {
$sql = "
CREATE TABLE IF NOT EXISTS remote (
id int(11) NOT NULL AUTO_INCREMENT,
record int(11) NOT NULL,
description varchar(255) NOT NULL,
type varchar(20) NOT NULL,
security varchar(2000) NOT NULL,
nonce varchar(255) DEFAULT NULL,
PRIMARY KEY (id),
KEY record (record)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
ALTER TABLE `remote`
ADD CONSTRAINT `remote_ibfk_1` FOREIGN KEY (`record`) REFERENCES `records` (`id`);
CREATE TABLE IF NOT EXISTS options (
name varchar(255) NOT NULL,
value varchar(2000) DEFAULT NULL,
PRIMARY KEY (name)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
INSERT INTO options(name,value) VALUES ('schema_version', 1);
";
$db->multi_query($sql);
while ($db->next_result()) {;}
}
$retval['status'] = "success";
}
if(isset($retval)) {
echo json_encode($retval);
} else {
echo "{}";
}

View file

@ -16,6 +16,13 @@ limitations under the License.
-->
<?php
require_once 'lib/headers.php';
require_once 'config/config-default.php';
require_once 'lib/database.php';
require_once 'lib/checkversion.php';
if(!checkVersion($db)) {
Header("Location: upgrade.php");
}
?>
<html>
<head>

55
js/upgrade.js Normal file
View file

@ -0,0 +1,55 @@
/*
* Copyright 2016 Lukas Metzger <developer@lukas-metzger.com>.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
$(document).ready(function() {
$('#button-start').click(function() {
$('#row-button-start').hide();
requestVersions();
});
});
function requestVersions() {
var data = {
action: "getVersions"
};
$.post(
"api/upgrade.php",
JSON.stringify(data),
function(data) {
$('#status').append('<p class="bg-warning">Upgrading from version ' + data.from + ' to ' + data.to + '</p>');
requestUpdate();
},
"json"
);
}
function requestUpdate() {
var data = {
action: "requestUpgrade"
};
$.post(
"api/upgrade.php",
JSON.stringify(data),
function(data) {
$('#status').append('<p class="bg-warning">Upgrade successfull!</p>');
$('#row-button-home').show();
},
"json"
);
}

47
lib/checkversion.php Normal file
View file

@ -0,0 +1,47 @@
<?php
/*
* Copyright 2016 Lukas Metzger <developer@lukas-metzger.com>.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
function getExpectedVersion() {
return 1;
}
function checkVersion($db) {
if(getVersion($db) == getExpectedVersion()) {
return true;
} else {
return false;
}
}
function getVersion($db) {
$stmt = $db->prepare("SHOW TABLES LIKE 'options'");
$stmt->execute();
$stmt->store_result();
if($stmt->num_rows() < 1) {
return 0;
}
$stmt->close();
$stmt = $db->prepare("SELECT value FROM options WHERE name='schema_version'");
$stmt->execute();
$stmt->bind_result($version);
$stmt->fetch();
$stmt->close();
return $version;
}

83
upgrade.php Normal file
View file

@ -0,0 +1,83 @@
<!DOCTYPE html>
<!--
Copyright 2016 Lukas Metzger <developer@lukas-metzger.com>.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
-->
<?php
require_once 'lib/headers.php';
require_once 'config/config-default.php';
require_once 'lib/database.php';
require_once 'lib/checkversion.php';
if(checkVersion($db)) {
Header("Location: index.php");
}
?>
<html>
<head>
<title>PDNS Manager - Upgrade</title>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link href="include/bootstrap/css/bootstrap.min.css" rel="stylesheet">
<link href="include/bootstrap/css/bootstrap-theme.min.css" rel="stylesheet">
<link href="include/custom.css" rel="stylesheet">
<script src="include/jquery.js"></script>
<script src="include/bootstrap/js/bootstrap.min.js"></script>
<script src="js/upgrade.js"></script>
</head>
<body>
<nav class="navbar navbar-inverse navbar-static-top">
<div class="container">
<div class="navbar-brand">
PDNS Manager
</div>
<ul class="nav navbar-nav">
<li><a href="#">Upgrade</a></li>
</ul>
</div>
</nav>
<div class="container">
<row>
<h2>Upgrade PDNS Manager</h2>
</row>
<row>
An upgrade for your PDNS Manager database is available and must be installed!
</row>
<div class="row vspacer-20"></div>
<div class="col-md-6">
<row class="row" id="row-button-start">
<button id="button-start" class="btn btn-primary">Start</button>
</row>
<row class="row" id="status">
</row>
<row class="row defaulthidden" id="row-button-home">
<a href="index.php" class="btn btn-primary">Login</a>
</row>
</div>
</div>
</body>
</html>