-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathModule.php
118 lines (97 loc) · 3.03 KB
/
Module.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
<?php
namespace Modules\SqlExplorer;
if (version_compare(ZABBIX_VERSION, '6.4.0', '>')) {
class_exists('\Core\CModule', false) or class_alias('\Zabbix\Core\CModule', '\Core\CModule');
class_exists('\CWidget', false) or class_alias('\CHtmlPage', '\CWidget');
}
use APP;
use CMenu;
use CWebUser;
use Core\CModule as CModule;
use CController as CAction;
use CMenuItem;
use Modules\SqlExplorer\Actions\BaseAction;
use Modules\SqlExplorer\Helpers\Html\CFormGrid;
use Modules\SqlExplorer\Helpers\Html\CFormField;
class Module extends CModule {
public function init(): void {
$this->registerMenuEntry();
$this->setCompatibilityMode(ZABBIX_VERSION);
}
/**
* Before action event handler.
*
* @param CAction $action Current request handler object.
*/
public function onBeforeAction(CAction $action): void {
if (is_a($action, BaseAction::class)) {
$action->module = $this;
}
}
/**
* For login/logout actions update user seession state in multiple databases.
*/
public function onTerminate(CAction $action): void {
}
/**
* Get array of database configuration.
*
* @return array
*/
public function getDatabase() {
global $DB;
return [
'type' => $DB['TYPE'],
'table' => $DB['DATABASE'],
'schema' => $DB['SCHEMA']
];
}
public function dbSelect(string $query) {
global $DB;
$db = null;
$error = null;
$rows = [];
$config = $this->getManifest();
if (array_key_exists('connection', $config) && strpos($config['connection'], ':')) {
unset($DB['DB']);
$db = $DB;
list($DB['USER'], $DB['PASSWORD']) = explode(':', $config['connection']);
DBconnect($error);
}
if ($error === null) {
$resource = DBselect($query);
$rows = $resource === false ? [] : DBfetchArray($resource);
}
if ($db === null) {
return $rows;
}
if ($error !== null) {
error($error);
}
$DB = $db;
DBconnect($error);
return $rows;
}
public function getAssetsUrl() {
return version_compare(ZABBIX_VERSION, '6.4', '>=')
? $this->getRelativePath().'/public/'
: 'modules/'.basename($this->getDir()).'/public/';
}
protected function registerMenuEntry() {
if (CWebUser::getType() != USER_TYPE_SUPER_ADMIN) {
return;
}
/** @var CMenu $menu */
$menu = APP::Component()->get('menu.main');
$menu
->find(_('Administration'))
->getSubMenu()
->add((new CMenuItem(_('SQL Explorer')))->setAction('sqlexplorer.form'));
}
protected function setCompatibilityMode($version) {
if (version_compare($version, '6.0', '>=')) {
class_alias('\\CFormGrid', CFormGrid::class, true);
class_alias('\\CFormField', CFormField::class, true);
}
}
}