forked from 418sec/TeamPass
-
Notifications
You must be signed in to change notification settings - Fork 0
/
admin.settings_api.php
476 lines (450 loc) · 16.4 KB
/
admin.settings_api.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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
<?php
/**
*
* @package admin.settings_api.php
* @author Nils Laumaillé <nils@teampass.net>
* @version 2.1.27
* @copyright 2009-2019 Nils Laumaillé
* @license GNU GPL-3.0
* @link https://www.teampass.net
*
* This library 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.
*/
require_once 'sources/SecureHandler.php';
session_start();
// Load config
if (file_exists('../includes/config/tp.config.php')) {
include_once '../includes/config/tp.config.php';
} elseif (file_exists('./includes/config/tp.config.php')) {
include_once './includes/config/tp.config.php';
} else {
throw new Exception("Error file '/includes/config/tp.config.php' not exists", 1);
}
if (!isset($_SESSION['CPM']) || $_SESSION['CPM'] != 1 ||
!isset($_SESSION['user_id']) || empty($_SESSION['user_id']) ||
!isset($_SESSION['key']) || empty($_SESSION['key'])
) {
die('Hacking attempt...');
}
/* do checks */
require_once $SETTINGS['cpassman_dir'].'/sources/checks.php';
if (checkUser($_SESSION['user_id'], $_SESSION['key'], "manage_settings") === false) {
$_SESSION['error']['code'] = ERR_NOT_ALLOWED; //not allowed page
include $SETTINGS['cpassman_dir'].'/error.php';
exit();
}
require_once $SETTINGS['cpassman_dir'].'/includes/language/'.$_SESSION['user_language'].'.php';
require_once $SETTINGS['cpassman_dir'].'/includes/config/settings.php';
require_once $SETTINGS['cpassman_dir'].'/includes/config/include.php';
header('Content-type: text/html; charset=utf-8');
require_once $SETTINGS['cpassman_dir'].'/sources/main.functions.php';
require_once $SETTINGS['cpassman_dir'].'/sources/SplClassLoader.php';
// connect to the server
require_once $SETTINGS['cpassman_dir'].'/includes/libraries/Database/Meekrodb/db.class.php';
$pass = defuse_return_decrypted($pass);
DB::$host = $server;
DB::$user = $user;
DB::$password = $pass;
DB::$dbName = $database;
DB::$port = $port;
DB::$encoding = $encoding;
DB::$error_handler = true;
$link = mysqli_connect($server, $user, $pass, $database, $port);
$link->set_charset($encoding);
echo '
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html><head><title>API Settings</title>
<style type="text/css">
table {
width: 100%;
}
td {
vertical-align: top;
}
th:nth-child(1) {
width: 25%;
}
th:nth-child(2) {
width: 60%;
}
th:nth-child(3) {
width: 10%;
display: hidden;
}
.maintable-left {
width: 40%;
}
.maintable-right {
width: 60%;
}
.fa-chevron-right {
margin-right: .8em;
}
.tip {
cursor:pointer;
}
.keytable tr {
background-color: white;
padding: 0 5px 0 5px;
}
.keytable td:nth-child(3) {
text-align: center;
}
</style>
</head><body>
<div id="tabs-9">
<table>
<tbody>
<!-- Enable API (toggle) -->
<tr>
<td class="maintable-left">
<label for="api">
<i class="fa fa-chevron-right mi-grey-1"></i>' .
$LANG['settings_api'].'
<i class="fa fa-question-circle tip" title="'.htmlentities(strip_tags($LANG['settings_api_tip']), ENT_QUOTES).'"></i>
</label>
</td>
<td class="maintable-right">
<div class="toggle toggle-modern" id="api" data-toggle-on="', isset($SETTINGS['api']) && $SETTINGS['api'] == 1 ? 'true' : 'false', '">
</div>
<div><input type="hidden" id="api_input" name="api_input" value="', isset($SETTINGS['api']) && $SETTINGS['api'] == 1 ? '1' : '0', '" />
</div>
</td>
</tr>
<tr>
<td colspan="2"><hr />
</td>
</tr>
<!-- API Keys (table) -->
<tr class="hideable">
<td>
<i class="fa fa-chevron-right mi-grey-1"></i>
<label for="api_keys_list">' .$LANG['settings_api_keys_list'].'
<i class="fa fa-question-circle tip" title="'.htmlentities(strip_tags($LANG['settings_api_keys_list_tip']), ENT_QUOTES).'"></i>
</label>
</td>
<td>
<div id="api_keys_list">
<table id="tbl_keys">
<thead>
<tr>
<th>'.$LANG['label'].'</th>
<th>'.$LANG['settings_api_key'].'</th>
<th></th>
</tr>
</thead>
<tbody class="keytable">
<tr style="display: none ! important;">
<td>HTML validation placeholder</td>
</tr>';
$rows = DB::query(
"SELECT id, label, value FROM ".prefix_table("api")."
WHERE type = %s
ORDER BY timestamp ASC",
'key'
);
foreach ($rows as $record) {
echo '
<tr id="apiid'.$record['id'].'">
<td id="apiid'.$record['id'].'label">'.$record['label'].'</td>
<td id="apiid'.$record['id'].'value">'.$record['value'].'</td>
<td>
<i class="fa fa-pencil tip" onclick="key_update(\''.$record['id'].'\', $(\'#apiid'.$record['id'].'label\').text(), $(\'#apiid'.$record['id'].'value\').text())" title="'.htmlentities(strip_tags($LANG['edit']), ENT_QUOTES).'"></i>
<i class="fa fa-trash mi-red tip" onclick="deleteApiKey(\''.$record['id'].'\')" title="'.htmlentities(strip_tags($LANG['del_button']), ENT_QUOTES).'"></i></td>
</td>
</tr>';
}
echo '
</tbody>
</table>
</div>
<br /><input type="button" id="but_add_new_key" value="'.$LANG['settings_api_add_key'].'" onclick="newKeyDB()" class="ui-state-default ui-corner-all" />
</td>
</tr>
<tr class="hideable">
<td colspan="2"><hr />
</td>
</tr>
<!-- API IP Whitelist (table) -->
<tr class="hideable">
<td>
<i class="fa fa-chevron-right mi-grey-1"></i>
<label for="api">
'.$LANG['settings_api_ip_whitelist'].' <i class="fa fa-question-circle tip" title="'.htmlentities(strip_tags($LANG['settings_api_ip_whitelist_tip']), ENT_QUOTES).'"></i>
</label>
</td>
<td>
<div id="api_ips_list">';
$data = DB::query(
"SELECT id, label, value FROM ".prefix_table("api")."
WHERE type = %s",
'ip'
);
$counter = DB::count();
if ($counter != 0) {
echo '
<table id="tbl_ips">
<thead>
<tr>
<th>'.$LANG['label'].'</th>
<th>'.$LANG['settings_api_ip'].'</th>
<th></th>
</tr>
</thead>
<tbody class="keytable">';
$rows = DB::query(
"SELECT id, label, value FROM ".prefix_table("api")."
WHERE type = %s
ORDER BY timestamp ASC",
'ip'
);
foreach ($rows as $record) {
echo '
<tr id="apiid'.$record['id'].'">
<td id="apiid'.$record['id'].'label">'.$record['label'].'</td>
<td id="apiid'.$record['id'].'value">'.$record['value'].'</td>
<td>
<i class="fa fa-pencil tip" onclick="ip_update(\''.$record['id'].'\', $(\'#apiid'.$record['id'].'label\').text(), $(\'#apiid'.$record['id'].'value\').text())" title="'.htmlentities(strip_tags($LANG['edit']), ENT_QUOTES).'"></i>
<i class="fa fa-trash mi-red tip" onclick="deleteApiKey(\''.$record['id'].'\')" title="'.htmlentities(strip_tags($LANG['del_button']), ENT_QUOTES).'"></i></td>
</tr>';
}
echo '
</tbody>
</table>
';
} else {
echo $LANG['settings_api_world_open'];
}
echo '
</div>
<br />
<input type="button" id="but_add_new_ip" value="'.$LANG['settings_api_add_ip'].'" onclick="newIPDB()" class="ui-state-default ui-corner-all" />
</td>
</tr>
</tbody>
</table>
</div>';
// dialog box
echo '
<div id="api_db" style="display:none;">
<input type="hidden" id="api_db_type" />
<input type="hidden" id="api_db_action" />
<input type="hidden" id="api_db_id" />
<div id="api_db_message" style="display:none;"></div>
<div id="api_db_intro"></div>
<div>
<span id="api_db_label_span"></span>
<input type="text" id="api_db_label_input" size="50" />
</div>
<div id="div_key">
<span id="api_db_key_span" ></span>
<input type="text" id="api_db_key_input" disabled="disabled" size="50" />
</div>
</div>';
echo '
<script type="text/javascript">
//<![CDATA[
$(".tip").tooltipster({
maxWidth: 400,
contentAsHTML: true
});
/* FUNCTIONS FOR KEYS */
function newKeyDB()
{
$("#api_db_type").val("admin_action_api_save_key");
$("#api_db_action").val("add");
$("#api_db_intro").html("'.$LANG['settings_api_db_intro'].'");
$("#api_db_label_span").html("'.$LANG['label'].'");
$("#api_db_key_span").html("'.$LANG['settings_api_key'].'");
$("#api_db_label_input, #api_db_key_input").val("");
$("#api_db_key_input").prop("disabled", true);
$("#div_key").show();
generateApiKey();
$("#api_db").dialog("open");
}
function key_update(id, value, key)
{
$("#api_db_type").val("admin_action_api_save_key");
$("#api_db_id").val(id);
$("#api_db_action").val("update");
$("#api_db_intro").html("'.$LANG['settings_api_db_intro'].'");
$("#api_db_label_span").html("'.$LANG['label'].'");
$("#api_db_key_span").html("'.$LANG['settings_api_key'].'");
$("#api_db_label_input").val(value);
$("#div_key").show();
$("#api_db_key_input").prop("disabled", true).val(key);
$("#api_db").dialog("open");
}
function generateApiKey()
{
$.post(
"sources/main.queries.php",
{
type : "generate_a_password",
size : "39",
lowercase : "true",
numerals : "true",
capitalize : "true",
symbols : "false",
secure : "false"
},
function(data) {
data = prepareExchangedData(data, "decode", "'.$_SESSION["key"].'");
if (data.error == "true") {
$("#api_db_message").html(data.error_msg);
} else {
$("#api_db_key_input").val(data.key).focus();
}
}
);
}
function deleteApiKey(id)
{
$.post(
"sources/admin.queries.php",
{
type : "admin_action_api_save_key",
action : "delete",
label : "",
key : "",
id : id,
session_key : "'.$_SESSION['key'].'"
},
function(data) {
var current_index = $("#tabs").tabs("option","active");
$("#tabs").tabs("load",current_index);
$("#div_loading").hide();
},
"json"
);
}
/* FUNCTIONS FOR IPS */
function newIPDB()
{
$("#api_db_type").val("admin_action_api_save_ip");
$("#api_db_action").val("add");
$("#api_db_intro").html("'.$LANG['settings_api_db_intro'].'");
$("#api_db_label_span").html("'.$LANG['label'].'");
$("#api_db_key_span").html("'.$LANG['settings_api_ip'].'");
$("#api_db_label_input, #api_db_key_input").val("");
$("#api_db_key_input").prop("disabled", false);
$("#div_key").show();
$("#api_db").dialog("open");
}
function ip_update(id, value, ip)
{
$("#api_db_type").val("admin_action_api_save_ip");
$("#api_db_id").val(id);
$("#api_db_action").val("update");
$("#div_key").show();
$("#api_db_intro").html("'.$LANG['settings_api_db_intro'].'");
$("#api_db_label_span").html("'.$LANG['label'].'");
$("#api_db_key_span").html("'.$LANG['settings_api_ip'].'");
$("#api_db_label_input").val(value);
$("#api_db_key_input").prop("disabled", false);
$("#api_db_key_input").val(ip);
$("#api_db").dialog("open");
}
$(function() {
$("#api_db").dialog({
bgiframe: true,
modal: true,
autoOpen: false,
width: 400,
height: 250,
title: "'.$LANG['confirm'].'",
buttons: {
"'.$LANG['confirm'].'": function() {
$("#api_db_message").html("");
if ($("#api_db_label_input").val().length > 255 || $("#api_db_key_input").val().length > 255) {
$("#api_db_message").html("'.$LANG['error_too_long'].'");
exit;
}
$("#div_loading").show();
var $this = $(this);
// send query
$.post(
"sources/admin.queries.php",
{
type : $("#api_db_type").val(),
action : $("#api_db_action").val(),
label : sanitizeString($("#api_db_label_input").val()),
key : sanitizeString($("#api_db_key_input").val()),
id : $("#api_db_id").val(),
session_key : "'.$_SESSION['key'].'"
},
function(data) {
// reload tab
if ($("#api_db_action").val() == "add") {
var current_index = $("#tabs").tabs("option","active");
$("#tabs").tabs("load",current_index);
} else if ($("#api_db_action").val() == "update") {
var current_index = $("#tabs").tabs("option","active");
$("#tabs").tabs("load",current_index);
}
$("#div_loading").hide();
$("#api_db").dialog("close");
},
"json"
);
},
"'.$LANG['cancel_button'].'": function() {
$("#div_loading").hide();
$(this).dialog("close");
}
}
});
$(".toggle").toggles({
drag: true, // allow dragging the toggle between positions
click: true, // allow clicking on the toggle
text: {
on: "'.$LANG['yes'].'", // text for the ON position
off: "'.$LANG['no'].'" // and off
},
on: true, // is the toggle ON on init
animate: 250, // animation time (ms)
easing: "swing", // animation transition easing function
width: 50, // width used if not set in css
height: 20, // height if not set in css
type: "compact" // if this is set to "select" then the select style toggle will be used
});
$(".toggle").on("toggle", function(e, active) {
if (active) {
$("#"+e.target.id+"_input").val(1);
if(e.target.id == "api") $(".hideable").show();
} else {
$("#"+e.target.id+"_input").val(0);
if(e.target.id == "api") $(".hideable").hide();
}
// store in DB
var data = "{\"field\":\""+e.target.id+"\", \"value\":\""+$("#"+e.target.id+"_input").val()+"\"}";
$.post(
"sources/admin.queries.php",
{
type : "save_option_change",
data : prepareExchangedData(data, "encode", "'.$_SESSION['key'].'"),
key : "'.$_SESSION['key'].'"
},
function(data) {
//decrypt data
try {
data = prepareExchangedData(data , "decode", "'.$_SESSION['key'].'");
} catch (e) {
// error
$("#message_box").html("Error: Server reply cannot be parsed!<br />Returned data:<br />"+data).show().fadeOut(4000);
return;
}
console.log(data);
if (data.error == "") {
$("#"+e.target.id).before("<i class=\"fa fa-check fa-lg mi-green new_check\" style=\"float:right;\"></i>");
$(".new_check").fadeOut(4000);
setTimeout("$(\".new_check\").remove()", 4000);
}
}
);
});
});
//]]>
</script></body></html>';