-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathajax.php
executable file
·62 lines (57 loc) · 2.43 KB
/
ajax.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
<?php
include 'phpqrcode/qrlib.php';
class AjaxHandler
{
private $aHandlers = [];
private $sKeyWordName = 'operation';
public function RegisterHandler($sKeyWord, Closure $fnHandler)
{
$this->aHandlers[$sKeyWord] = $fnHandler;
}
public function Execute()
{
try {
require_once('../../approot.inc.php');
require_once(APPROOT . '/application/application.inc.php');
require_once(APPROOT . '/application/webpage.class.inc.php');
require_once(APPROOT . '/application/ajaxwebpage.class.inc.php');
require_once(APPROOT . '/application/startup.inc.php');
require_once(APPROOT . '/application/loginwebpage.class.inc.php');
LoginWebPage::DoLoginEx(null /* any portal */, false);
$oPage = new ajax_page("");
$oPage->no_cache();
$sKeyWord = utils::ReadParam($this->sKeyWordName, '');
if (isset($this->aHandlers[$sKeyWord]) && $this->aHandlers[$sKeyWord] instanceof Closure) {
$fnHandler = $this->aHandlers[$sKeyWord];
$fnHandler($oPage);
} else {
$oPage->p("Missing argument 'operation' or route not found");
}
$oPage->output();
} catch (Exception $e) {
// note: transform to cope with XSS attacks
echo htmlentities($e->GetMessage(), ENT_QUOTES, 'utf-8');
IssueLog::Error($e->getMessage());
}
}
}
$oHandler = new AjaxHandler();
$oHandler->RegisterHandler('generate_qr', function (ajax_page $oP) {
$sObjClass = stripslashes(utils::ReadParam('obj_class', '', false, 'class'));
$iObjKey = (int)utils::ReadParam('obj_key', 0);
if (empty($sObjClass) || $iObjKey <= 0) {
throw new ApplicationException(Dict::Format('UI:Error:2ParametersMissing', 'obj_class', 'obj_key'));
}
$oObj = MetaModel::GetObject($sObjClass, $iObjKey);
$oP->SetContentType('image/png');
$oP->SetContentDisposition('attachment', "QR_{$sObjClass}_{$iObjKey}.png");
$sData = '';
if (!MetaModel::GetModuleSetting('knowitop-qr-code', 'url_only', true)) {
$sData .= 'name: ' . $oObj->GetName() . "\n";
$sData .= 's/n: ' . $oObj->Get('serialnumber') . "\n";
$sData .= 'asset: ' . $oObj->Get('asset_number') . "\n";
}
$sData .= ApplicationContext::MakeObjectUrl($sObjClass, $iObjKey, null, false);
QRcode::png($sData);
});
$oHandler->Execute();