From 6d93cbd62812559e79adf41937c18e90333257f5 Mon Sep 17 00:00:00 2001 From: Lukas Metzger Date: Sat, 6 Feb 2016 13:23:28 +0100 Subject: [PATCH] Added upgrade.php and version checks --- api/upgrade.php | 69 ++++++++++++++++++++++++++++++++++++ index.php | 7 ++++ js/upgrade.js | 55 +++++++++++++++++++++++++++++ lib/checkversion.php | 47 +++++++++++++++++++++++++ upgrade.php | 83 ++++++++++++++++++++++++++++++++++++++++++++ 5 files changed, 261 insertions(+) create mode 100644 api/upgrade.php create mode 100644 js/upgrade.js create mode 100644 lib/checkversion.php create mode 100644 upgrade.php diff --git a/api/upgrade.php b/api/upgrade.php new file mode 100644 index 0000000..e977c8f --- /dev/null +++ b/api/upgrade.php @@ -0,0 +1,69 @@ +. + * + * 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 "{}"; +} diff --git a/index.php b/index.php index 452db34..c1e93d2 100644 --- a/index.php +++ b/index.php @@ -16,6 +16,13 @@ limitations under the License. --> diff --git a/js/upgrade.js b/js/upgrade.js new file mode 100644 index 0000000..d6d2443 --- /dev/null +++ b/js/upgrade.js @@ -0,0 +1,55 @@ +/* + * Copyright 2016 Lukas Metzger . + * + * 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('

Upgrading from version ' + data.from + ' to ' + data.to + '

'); + requestUpdate(); + }, + "json" + ); +} + +function requestUpdate() { + var data = { + action: "requestUpgrade" + }; + + $.post( + "api/upgrade.php", + JSON.stringify(data), + function(data) { + $('#status').append('

Upgrade successfull!

'); + $('#row-button-home').show(); + }, + "json" + ); +} \ No newline at end of file diff --git a/lib/checkversion.php b/lib/checkversion.php new file mode 100644 index 0000000..6a8af8b --- /dev/null +++ b/lib/checkversion.php @@ -0,0 +1,47 @@ +. + * + * 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; +} \ No newline at end of file diff --git a/upgrade.php b/upgrade.php new file mode 100644 index 0000000..57875e6 --- /dev/null +++ b/upgrade.php @@ -0,0 +1,83 @@ + + + + + + PDNS Manager - Upgrade + + + + + + + + + + + + + + + +
+ + +

Upgrade PDNS Manager

+
+ + + An upgrade for your PDNS Manager database is available and must be installed! + + +
+ +
+ + + + + + + + + + Login + +
+
+ + + +