-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathahp-user-recover.php
executable file
·171 lines (153 loc) · 5.3 KB
/
ahp-user-recover.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
<?php
/*
* Restore user from the backup database
* User will still be deactivated when restored.
*
* Copyright (C) 2022 <Klaus D. Goepel>
*
* 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 <https://www.gnu.org/licenses/>.
*
* Revision: $Rev: 225 $
*
*/
include 'includes/config.php';
/*
* for sqlite database name has to include extension db
* TODO: error handling: check db connection to backup
* database successful!
*/
$backupDb = 'bpmsg_bck-2024-03-20';
//$backupDb = 'bpmsg_bck-2023-12-27';
//$backupDb = 'ahp_os.2022-01-30.db';
$storedUsers = array(); // users in active database
$deletedUsers = array(); // users in backup db but not in active db
$errMsg = "";
$userName= "";
$ahpUser = array();
$pageTitle ='AHP recover';
$title="AHP-OS User Recovery";
$subTitle = "User recovery from bachup database";
$version = substr('$LastChangedDate: 2024-09-12 16:02:00 +0800 (Do, 12 Sep 2024) $', 18, 10);
$rev = trim('$Rev: 225 $', "$");
// productive database
$login = new Login();
$ahpAdm = new AhpAdmin();
$ahpDb = new AhpDb();
// backup database
$errflg = false;
$fileList = array();
if(substr($backupDb,-3) == ".db"){
foreach ( glob( "db/*.*" ) as $filename){
$fileList[] = substr(strrchr($filename,'/'),1);
}
if(in_array($backupDb, $fileList)){
$ahpAdmBck = new AhpAdmin($backupDb);
$ahpDbBck = new AhpDb($backupDb);
} else {
$errFlg = true;
$errMsg = "<span class='err'>
SQLITE backup database not found</span>";
}
} else {
$ahpAdmBck = new AhpAdmin($backupDb);
$ahpDbBck = new AhpDb($backupDb);
}
/* --- MENU ACTIONS --- */
if (isset($_POST['EXIT']) || !$login->isUserLoggedIn()) {
header('HTTP/1.0 200 ok');
header("Location: " . "includes/login/do/do-user-admin.php");
}
if (isset($_POST['DEL']) || isset($_POST['OPEN']) || isset($_POST['REACT'])) {
$para = filter_input_array(INPUT_POST, FILTER_SANITIZE_STRING);
if (filter_has_var(INPUT_POST, 'userName')) {
$userName = mb_substr(preg_replace(
'~[^\p{L}\p{N}]++~u',
'',
$para['userName']
), 0, 64);
}
// TODO: add menu point to download $ahpUser JSON to
if (isset($_POST['REACT'])) {
$ahpUser = $ahpDbBck->getUser($userName);
// JSON encoded
$ahpJs = json_encode($ahpUser);
if ($ahpDb->restoreUser(
$ahpUser['user'],
$ahpUser['projects'],
$ahpUser['pwc'],
$ahpUser['a']
)) {
$errMsg = "<p class='msg'><br>Account of $userName was successfully
restored with " . count($ahpUser['projects']) . " projects</p>";
} else {
$errMsg = "<p class='err'><br>Restore error for $userName ";
$errMsg .= $ahpDb->getErrors() . "</p>";
}
$sessionName = "";
} else {
// view
}
} elseif (isset($_POST['CLOSE'])) {
$userName = "";
}
if(!$errFlg) {
$storedUsers = $ahpAdm->getUserNames();
$deletedUsers = $ahpAdmBck->getUserNames();
$deletedUsers = array_diff($deletedUsers, $storedUsers);
$storedUsers = array_values($deletedUsers);
if ($userName !="") {
$i = array_search($userName, $storedUsers);
if ($i >0) {
$storedUsers[$i] = $storedUsers[0];
$storedUsers[0] = $userName;
}
}
}
/* --- Web Page HTML OUTPUT --- */
$webHtml = new WebHtml($pageTitle);
include 'includes/login/form.login-hl.php';
if (!empty($login->errors)) {
echo $login->getErrors();
}
echo "<h1>$title</h1>";
echo "<h2>$subTitle</h2>";
echo "<h2>Deleted Users</h2>";
echo "<p>Prod. database: <span class='var'>" . DBNAME . "</span>";
echo "<br>Backup database: <span class='var'>$backupDb</span>. <br><span class='var'>",
count($storedUsers), "</span> Users can be recovered.</p>";
echo "<h2>AHP User Recovery Menu</h2>";
include 'views/ahpUserRecoverMenu.html';
echo $errMsg;
// --- TODO: add a menu point to export users as JSON file
//if(!empty($ahpJs))
// echo "<p>$ahpJs</p>";
echo "<h2>User Details</h2>";
if ($userName !="") {
echo "<p>User details for user <span class='hl'>$userName</span></p>";
echo "<h3>E-mail, registration and last login</h3>";
$userDetails = $ahpAdmBck->getUserDetails($userName);
$ahpAdm->displayUserTable(array($userDetails));
echo "<h3>Projects</h3>";
$projects = $ahpDbBck->getAllProjects($userName);
if (is_array($projects)) {
$pcnt = count($projects);
} else {
$pcnt = 0;
}
echo "<p class='msg'>User has $pcnt project(s)</p>";
$ahpDbBck->displaySessionTable($userName);
} else {
echo "<p>No data</p>";
}
$webHtml->webHtmlFooter($version);