-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathajaxopen.php
More file actions
190 lines (169 loc) · 5.77 KB
/
Copy pathajaxopen.php
File metadata and controls
190 lines (169 loc) · 5.77 KB
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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
<?php
require_once "keepassphpui/main.php";
// alias KeePassPHP
use \KeePassPHP\KeePassPHP as KeePassPHP;
/**
* A class that manages the answer that will be sent as a stringified json
* object. This object has three properties: 'status', containing an integer
* describing the result; 'result', containing a string which is the result;
* and 'debug' if there is debug information to send.
*/
class AjaxAnswer
{
private $status;
private $result;
private $debug;
/** Status: unexpected fail. */
const FAIL = 0;
/** Status: operation succeeded (either database opening or password extracting). */
const SUCCESS = 1;
/** Status: wrong password for the database. */
const BAD_PASSWORD = 2;
/** Status: the given database ID does not exist. */
const NO_SUCH_ID = 3;
/** Status: a POST parameter is missing or empty. */
const SOMETHING_EMPTY = 4;
/** Status: the requested password does not exist in the database (this is weird). */
const PASSWORD_NOT_FOUND = 5;
public function __construct()
{
$this->status = self::FAIL;
$this->result = null;
$this->debug = null;
}
/**
* Sets the 'status' and 'result' fields of the answer.
*/
public function set($status, $result = "")
{
$this->status = $status;
$this->result = $result;
}
/**
* Sets the 'debug' field of the answer.
*/
public function setDebug($debug)
{
$this->debug = $debug;
}
/**
* Sends the answer. You should not output something anymore after
* calling this method.
*/
public function send()
{
header('Content-Type: application/json; charset=utf-8');
$out = array("status" => $this->status, "result" => $this->result);
if(!empty($this->debug))
$out["debug"] = $this->debug;
echo json_encode($out);
}
}
function visitDatabase(\KeePassPHP\Database $db)
{
$s = '<table class="table table-hover form-inline"><thead><tr><th> </th><th>'
. KPHPUI::l(KPHPUI::LANG_SEE_ENTRY_TITLE) . '</th><th>'
. KPHPUI::l(KPHPUI::LANG_SEE_ENTRY_URL) . '</th><th>'
. KPHPUI::l(KPHPUI::LANG_SEE_ENTRY_USERNAME) . '</th><th>'
. KPHPUI::l(KPHPUI::LANG_SEE_ENTRY_PASSWORD) . '</th></tr></thead><tbody>';
$groups = $db->getGroups();
if($groups != null)
{
foreach($groups as &$g)
$s .= visitGroup($db, $g);
}
return $s . '</tbody></table>';
}
function visitGroup(\KeePassPHP\Database $db, \KeePassPHP\Group $group)
{
$s = "";
if($group->groups != null)
{
foreach($group->groups as &$g)
$s .= visitGroup($db, $g);
}
if($group->entries != null)
{
foreach($group->entries as &$e)
$s .= visitEntry($db, $e);
}
return $s;
}
function visitEntry(\KeePassPHP\Database $db, \KeePassPHP\Entry $entry)
{
$icon = null;
if(!empty($entry->customIcon))
$icon = $db->getCustomIcon($entry->customIcon);
if(empty($icon) && !empty($entry->icon))
$icon = KPHPUI::iconPath($entry->icon);
$uuid = bin2hex(base64_decode($entry->uuid));
$url = $entry->getStringField(\KeePassPHP\Database::KEY_URL);
$protoSep = strpos($url, "://");
$proto = $protoSep === false ? null : substr($url, 0, $protoSep);
$isHttp = $proto == "http" || $proto == "https";
$displayed = $isHttp ? substr($url, $protoSep + 3) : $url;
return '<tr><td>' . ($icon == null ? '' : '<img src="' . KPHPUI::htmlify($icon) . '" />')
. '</td><td>' . KPHPUI::htmlify($entry->getStringField(\KeePassPHP\Database::KEY_TITLE)) . '</td>'
. '<td>' . ($isHttp ? '<a href="' : '<span title="') . KPHPUI::htmlify($url) . '">'
. KPHPUI::htmlify(strlen($displayed) > 20 ? substr($displayed, 0, 17) . '...' : $displayed)
. ($isHttp ? '</a>' : '</span>') . '</td>'
. '<td><input type="text" class="col-sm-3 form-control selectOnFocus" value="' . KPHPUI::htmlify($entry->getStringField(\KeePassPHP\Database::KEY_USERNAME)) . '" /></td>'
. '<td id="pwd_' . $uuid . '">'
. '<button type="button" class="btn btn-primary passwordLoader" data-uuid="' . $uuid . '">' . KPHPUI::l(KPHPUI::LANG_SEE_ENTRY_LOAD) . '</button>'
. '<button type="button" class="btn btn-primary disabled loading-button" disabled="disabled"><span class="spinner"> </span></button>'
. '</td></tr>';
}
$answer = new AjaxAnswer();
$dbid = KPHPUI::getPost("dbid");
$mainPwd = KPHPUI::getPost("main_pwd");
$usePwdInKey = KPHPUI::getPost("use_pwd_in_key") == "true";
$otherPwd = KPHPUI::getPost("open_other_pwd");
if(empty($dbid))
$answer->set(AjaxAnswer::SOMETHING_EMPTY, "dbid");
elseif(empty($mainPwd))
$answer->set(AjaxAnswer::SOMETHING_EMPTY, "main_pwd");
elseif(!$usePwdInKey && empty($otherPwd))
$answer->set(AjaxAnswer::SOMETHING_EMPTY, "open_other_pwd");
else
{
require_once KEEPASSPHP_LOCATION;
if(KPHPUI::isKeePassPHPVersionSupported())
{
KeePassPHP::init(null, KEEPASSPHP_DEBUG);
if(KeePassPHP::existsKphpDB($dbid))
{
$uuid = KPHPUI::getPost("uuid");
$getPasswords = !empty($uuid);
$db = KeePassPHP::getDatabase($dbid,
$usePwdInKey ? KeePassPHP::extractHalfPassword($mainPwd) : $mainPwd,
$usePwdInKey ? $mainPwd : $otherPwd,
$getPasswords);
if($db != null)
{
if($getPasswords)
{
$pwd = $db->getPassword(base64_encode(hex2bin($uuid)));
if($pwd != null)
$answer->set(AjaxAnswer::SUCCESS, '<input type="text" class="verysmall selectOnFocus form-control" value="' . KPHPUI::htmlify($pwd) . '" style="font-size:3px !important;"/>');
else
$answer->set(AjaxAnswer::PASSWORD_NOT_FOUND, '<span class="label label-danger">' . KPHPUI::l(KPHPUI::LANG_SEE_PWD_DOES_NOT_EXIST) . '</span>');
}
else
$answer->set(AjaxAnswer::SUCCESS, visitDatabase($db));
}
else
$answer->set(AjaxAnswer::BAD_PASSWORD);
}
else
$answer->set(AjaxAnswer::NO_SUCH_ID);
if(KeePassPHP::$debug && !empty(KeePassPHP::$debugData))
$answer->setDebug(KeePassPHP::$debugData);
}
else
{
$answer->set(AjaxAnswer::FAIL, KPHPUI::l(KPHPUI::LANG_INTERNAL_ERROR));
$answer->setDebug("The version of KeePassPHP is not supported.");
}
}
$answer->send();
?>