diff options
author | Ian C <ianc@noddybox.co.uk> | 2018-07-07 13:26:02 +0000 |
---|---|---|
committer | Ian C <ianc@noddybox.co.uk> | 2018-07-07 13:26:02 +0000 |
commit | 62d926e022aaf35be029f20568e55533caee3bf7 (patch) | |
tree | 58b4dcb2de9f91f7af0f1b5893cea5aa07d7a4ea | |
parent | 9bd1ad2bfb807d7714e17112a9dc51f2416a1e6c (diff) |
Added up/down links to reorder entries.
-rw-r--r-- | INSTRUCTIONS | 2 | ||||
-rw-r--r-- | sql/INSTALL | 4 | ||||
-rwxr-xr-x | sql/create_db.sh | 1 | ||||
-rwxr-xr-x | sql/upgrade_from_1.0_to_1.1.sh | 26 | ||||
-rw-r--r-- | www/add.php | 10 | ||||
-rw-r--r-- | www/get.php | 3 | ||||
-rw-r--r-- | www/reorder.php | 75 | ||||
-rw-r--r-- | www/scripts/main.js | 123 |
8 files changed, 231 insertions, 13 deletions
diff --git a/INSTRUCTIONS b/INSTRUCTIONS index 1332305..26b23ae 100644 --- a/INSTRUCTIONS +++ b/INSTRUCTIONS @@ -20,3 +20,5 @@ The Copy Username and Copy Password links can be used to copy the username or password respectively to the clipboard. The Show link can be used to reveal the password while the button is down. + +The Up/Down links move the entry up/down as appropriate in the list. diff --git a/sql/INSTALL b/sql/INSTALL index 340d656..624a1c8 100644 --- a/sql/INSTALL +++ b/sql/INSTALL @@ -1,2 +1,6 @@ Run the ./create_db.sh shell script to create the database. You will have to enter the MySQL root user password when prompted. + +If you are upgrading from a previous version run the appropriate upgrade script: + +upgrade_from_1.0_to_1.1.sh - Upgrade from V1.0 to V1.1 diff --git a/sql/create_db.sh b/sql/create_db.sh index 69639db..9d55d63 100755 --- a/sql/create_db.sh +++ b/sql/create_db.sh @@ -52,6 +52,7 @@ grant all on *.* to '$uname'@'localhost' identified by '$pword'; create table pm_store ( id int not null auto_increment, + display int not null, description varchar(1024) not null, groupname varchar(512) not null, username varchar(512) not null, diff --git a/sql/upgrade_from_1.0_to_1.1.sh b/sql/upgrade_from_1.0_to_1.1.sh new file mode 100755 index 0000000..78c1468 --- /dev/null +++ b/sql/upgrade_from_1.0_to_1.1.sh @@ -0,0 +1,26 @@ +#!/bin/sh + +dbname="passman" +echo "Name of database to use [$dbname]:" +read x + +if [ "$x" ] ; then + dbname=$x +fi + +script=/tmp/$$ +rm -f $script +touch $script + +cat >> $script << END_OF_SQL + +use $dbname; +alter table pm_store add column display int not null; +update pm_store set display = id; + +END_OF_SQL + +cat -n $script +mysql -u root -p < $script + +rm -f $script diff --git a/www/add.php b/www/add.php index 7b9d99f..e5c1a18 100644 --- a/www/add.php +++ b/www/add.php @@ -34,7 +34,7 @@ try $cmd = $db->prepare("INSERT INTO pm_store " . "(description, groupname, username, password) ". - "values " . + "VALUES " . "(:description, :group, :username, :password)"); $cmd->bindParam(":description", $description); $cmd->bindParam(":group", $group); @@ -42,7 +42,13 @@ try $cmd->bindParam(":password", $password); $cmd->execute(); - $result["id"] = $db->lastInsertId(); + $id = $db->lastInsertId(); + + $result["id"] = $id; + + $update = $db->prepare("UPDATE pm_store SET display = :id WHERE id = :id"); + $update->bindParam(":id", $id); + $update->execute(); $db = null; } diff --git a/www/get.php b/www/get.php index d42e57e..c41fafc 100644 --- a/www/get.php +++ b/www/get.php @@ -29,7 +29,7 @@ try $index = 0; - foreach ($db->query("SELECT * FROM pm_store ORDER BY id") as $row) + foreach ($db->query("SELECT * FROM pm_store ORDER BY display") as $row) { $data = array(); $data["id"] = $row["id"]; @@ -37,6 +37,7 @@ try $data["group"] = $row["groupname"]; $data["username"] = $row["username"]; $data["password"] = $row["password"]; + $data["display"] = $row["display"]; $result[$index++] = $data; } diff --git a/www/reorder.php b/www/reorder.php new file mode 100644 index 0000000..375ee62 --- /dev/null +++ b/www/reorder.php @@ -0,0 +1,75 @@ +<?php + +/* + PassMan - simple password manager + Copyright (C) 2018 Ian Cowburn + + This program is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program. If not, see <http://www.gnu.org/licenses/>. +*/ + +require 'config.php'; +require 'common.php'; + +$result = array(); + +try +{ + $db = ConnectDB(); + + $from_id = $_REQUEST["from_id"]; + $to_id = $_REQUEST["to_id"]; + $from_display = 0; + $to_display = 0; + + $cmd = $db->prepare("SELECT display FROM pm_store ". + "WHERE id = :id"); + $cmd->bindParam(":id", $from_id); + $cmd->execute(); + + $data = $cmd->fetch(); + + $from_display = $data["display"]; + + $cmd->bindParam(":id", $to_id); + $cmd->execute(); + + $data = $cmd->fetch(); + + $to_display = $data["display"]; + + $from_update = $db->prepare("UPDATE pm_store SET display = :to_display " . + "WHERE id = :from_id"); + $from_update->bindParam(":to_display", $to_display); + $from_update->bindParam(":from_id", $from_id); + $from_update->execute(); + + $to_update = $db->prepare("UPDATE pm_store SET display = :from_display " . + "WHERE id = :to_id"); + $to_update->bindParam(":from_display", $from_display); + $to_update->bindParam(":to_id", $to_id); + $to_update->execute(); + + $result["status"] = true; + + $db = null; +} +catch(PDOException $e) +{ + $result["error"] = "DB error:" . $e->getMessage(); +} + +header("Content-type:application/json"); +echo json_encode($result); + +?> diff --git a/www/scripts/main.js b/www/scripts/main.js index 0ed2cf5..79fb0b7 100644 --- a/www/scripts/main.js +++ b/www/scripts/main.js @@ -170,6 +170,21 @@ function AddSmallLink(node, text, func, id) node.appendChild(anchor); } +function AddSmallLinkPrevNext(node, text, func, id, prev, next) +{ + var anchor = document.createElement("a"); + + anchor.href = "#"; + anchor.onclick = func; + anchor.text = text; + anchor.className = "smalltext"; + anchor.dataset.pmId = id; + anchor.dataset.pmPrevId = prev; + anchor.dataset.pmNextId = next; + + node.appendChild(anchor); +} + function AddSmallLinkUpDown(node, text, down_func, up_func, id) { var anchor = document.createElement("a"); @@ -197,7 +212,7 @@ function AddElement(node, elementname) node.appendChild(elem); } -function AddRow(table, id, desc, username, password, rowcount) +function AddRow(table, id, prev_id, next_id, desc, username, password, rowcount) { var tr = document.createElement("tr"); @@ -232,7 +247,26 @@ function AddRow(table, id, desc, username, password, rowcount) AddText(td, "\u00a0"); AddText(td, "\u00a0"); AddSmallLinkUpDown(td, "Show", DoShow, DoHide, id); + + if (next_id != -1 || prev_id != -1) + { + if (prev_id != -1) + { + AddText(td, "\u00a0"); + AddText(td, "\u00a0"); + AddSmallLinkPrevNext(td, "Up", DoUp, id, prev_id, next_id); + } + + if (next_id != -1) + { + AddText(td, "\u00a0"); + AddText(td, "\u00a0"); + AddSmallLinkPrevNext(td, "Down", DoDown, id, prev_id, next_id); + } + } + AddElement(td, "br"); + AddSmallLink(td, "Copy Username", DoCopyUsername, id); AddText(td, "\u00a0"); AddText(td, "\u00a0"); @@ -244,6 +278,21 @@ function AddRow(table, id, desc, username, password, rowcount) table.appendChild(tr); } +function GetGroupRows() +{ + var result = []; + + for(var f = 0; f < globalDb.length; f++) + { + if (globalDb[f].group == globalCurrentGroup) + { + result.push(globalDb[f]); + } + } + + return result; +} + function LoadTable() { var table = document.getElementById("DataTable"); @@ -252,20 +301,34 @@ function LoadTable() var rowcount = 0; - for(var f = 0; f < globalDb.length; f++) + var rows = GetGroupRows(); + + for(var f = 0; f < rows.length; f++) { - if (globalDb[f].group == globalCurrentGroup) + var prevId = -1; + var nextId = -1; + + if (f > 0) { - AddRow(table, - globalDb[f].id, - globalDb[f].description, - globalDb[f].username, - Decrypt(globalDb[f].password), - rowcount++); + prevId = rows[f-1].id; } + + if (f < rows.length - 1) + { + nextId = rows[f+1].id; + } + + AddRow(table, + rows[f].id, + prevId, + nextId, + rows[f].description, + rows[f].username, + Decrypt(rows[f].password), + rowcount++); } - AddRow(table, -1, "", "", "", rowcount++); + AddRow(table, -1, -1, -1, "", "", "", rowcount++); } function RefreshDataAsync() @@ -466,4 +529,44 @@ function DoHide() elem.type = "password"; } +function DoReorderAsync() +{ + var response = JSON.parse(this.responseText); + + if (response.error) + { + Error(response.error); + } + else + { + RefreshData(); + } +} + +function DoUp() +{ + var id = this.dataset.pmId; + var prev = this.dataset.pmPrevId; + + WebRequest("reorder.php", DoReorderAsync, + [ + "from_id", id, + "to_id", prev + ] + ); +} + +function DoDown() +{ + var id = this.dataset.pmId; + var next = this.dataset.pmNextId; + + WebRequest("reorder.php", DoReorderAsync, + [ + "from_id", id, + "to_id", next + ] + ); +} + // vim: sw=4 ts=4 |