Skip to content

Commit e666d53

Browse files
[ADD] Added compatibility for Zabbix 5.2 and 5.4
1 parent 57c7fac commit e666d53

File tree

7 files changed

+76
-20
lines changed

7 files changed

+76
-20
lines changed

README.md

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,11 @@ This software is licensed under the GNU Lesser General Public License v3.0.
1010

1111
## Changelog
1212

13+
### Version 5.4.2
14+
15+
* Added support for Zabbix 5.0 and 5.2
16+
* Non-functional menu entry for non-superadmins is now hidden
17+
1318
### Version 5.0.2
1419

1520
* Added support for templates
@@ -24,7 +29,7 @@ This software is licensed under the GNU Lesser General Public License v3.0.
2429

2530
## Requirements
2631

27-
- Zabbix 5.4
32+
- Zabbix 5.0 to 5.4
2833
- File write access to the Zabbix frontend server
2934
- Super admin permissions for the Zabbix users that want to use the frontend module
3035

@@ -63,5 +68,5 @@ Additional hints:
6368
* The columns must be in the first line of the CSV file.
6469
* The separator character must be ";".
6570

66-
The CSV file can then imported in the same menu entry. You get a chance to preview the host list before the actual import.
71+
The CSV file can then be imported in the same menu entry. You get a chance to preview the host list before the actual import.
6772

modules/csv-host-importer/Module.php

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
/**
33
* Zabbix CSV Import Frontend Module
44
*
5-
* @version 5.0.2
5+
* @version 5.0.0
66
* @author Wolfgang Alper <[email protected]>
77
* @copyright IntelliTrend GmbH, https://www.intellitrend.de
88
* @license GNU Lesser General Public License v3.0
@@ -19,6 +19,7 @@
1919

2020
use APP;
2121
use CController as CAction;
22+
use CWebUser;
2223

2324
/**
2425
* Please see Core\CModule class for additional reference.
@@ -29,6 +30,10 @@ class Module extends \Core\CModule {
2930
* Initialize module.
3031
*/
3132
public function init(): void {
33+
// Only super admins should see the menu entry
34+
if (CWebUser::getType() != USER_TYPE_SUPER_ADMIN) {
35+
return;
36+
}
3237
// Initialize main menu (CMenu class instance).
3338
APP::Component()->get('menu.main')->findOrAdd(_('Configuration'))->getSubmenu()->add((new \CMenuItem(_('Host CSV Importer')))->setAction('ichi.import'));
3439
}

modules/csv-host-importer/actions/CsvHostImport.php

Lines changed: 46 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
/**
33
* Zabbix CSV Import Frontend Module
44
*
5-
* @version 5.0.2
5+
* @version 5.0.0
66
* @author Wolfgang Alper <[email protected]>
77
* @copyright IntelliTrend GmbH, https://www.intellitrend.de
88
* @license GNU Lesser General Public License v3.0
@@ -23,6 +23,8 @@
2323
use CRoleHelper;
2424
use CUploadFile;
2525
use API;
26+
use CSession; // 5.0.0
27+
use CSessionHelper; // 5.2.0+
2628

2729
/**
2830
* Host CSV importer module action.
@@ -61,6 +63,8 @@ class CsvHostImport extends CAction {
6163
8 => 'A PHP extension stopped the file upload.',
6264
];
6365

66+
const HOSTLIST_KEY = 'ichi.hostlist';
67+
6468
private $hostlist = [];
6569
private $step = 0;
6670

@@ -101,7 +105,11 @@ protected function checkInput(): bool {
101105
* @return bool
102106
*/
103107
protected function checkPermissions(): bool {
104-
return $this->checkAccess(CRoleHelper::UI_ADMINISTRATION_GENERAL);
108+
if (version_compare(ZABBIX_VERSION, '5.4.0', '>=')) {
109+
return $this->checkAccess(CRoleHelper::UI_ADMINISTRATION_GENERAL);
110+
} else {
111+
return ($this->getUserType() == USER_TYPE_SUPER_ADMIN);
112+
}
105113
}
106114

107115
private function csvParse(): bool {
@@ -261,6 +269,39 @@ private function importHosts(): bool {
261269
return true;
262270
}
263271

272+
private function loadSession() {
273+
if (version_compare(ZABBIX_VERSION, '5.2.0', '>=')) {
274+
if (!CSessionHelper::has(self::HOSTLIST_KEY)) {
275+
return false;
276+
}
277+
$this->hostlist = CSessionHelper::get(self::HOSTLIST_KEY);
278+
return true;
279+
} else {
280+
if (!CSession::keyExists(self::HOSTLIST_KEY)) {
281+
return false;
282+
}
283+
$this->hostlist = CSession::getValue(self::HOSTLIST_KEY);
284+
return true;
285+
}
286+
}
287+
288+
private function saveSession() {
289+
if (version_compare(ZABBIX_VERSION, '5.2.0', '>=')) {
290+
CSessionHelper::set(self::HOSTLIST_KEY, $this->hostlist);
291+
} else {
292+
CSession::setValue(self::HOSTLIST_KEY, $this->hostlist);
293+
}
294+
}
295+
296+
private function clearSession() {
297+
if (version_compare(ZABBIX_VERSION, '5.2.0', '>=')) {
298+
CSessionHelper::unset([self::HOSTLIST_KEY]);
299+
} else {
300+
CSession::unsetValue([self::HOSTLIST_KEY]);
301+
}
302+
}
303+
304+
264305
/**
265306
* Prepare the response object for the view. Method called by Zabbix core.
266307
*
@@ -287,17 +328,16 @@ protected function doAction() {
287328
if (!$this->csvParse()) {
288329
$this->step = 0;
289330
}
290-
$_SESSION['ichi.hostlist'] = $this->hostlist;
331+
$this->saveSession();
291332
break;
292333
case 2:
293334
// import
294-
if (!isset($_SESSION['ichi.hostlist'])) {
335+
if (!$this->loadSession()) {
295336
error(_('Missing host list in session.'));
296337
break;
297338
}
298-
$this->hostlist = $_SESSION['ichi.hostlist'];
299339
if ($this->importHosts()) {
300-
unset($_SESSION['ichi.hostlist']);
340+
$this->clearSession();
301341
}
302342
break;
303343
}

modules/csv-host-importer/actions/CsvHostImportExample.php

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
/**
33
* Zabbix CSV Import Frontend Module
44
*
5-
* @version 5.0.2
5+
* @version 5.0.0
66
* @author Wolfgang Alper <[email protected]>
77
* @copyright IntelliTrend GmbH, https://www.intellitrend.de
88
* @license GNU Lesser General Public License v3.0
@@ -56,7 +56,11 @@ protected function checkInput(): bool {
5656
* @return bool
5757
*/
5858
protected function checkPermissions(): bool {
59-
return $this->checkAccess(CRoleHelper::UI_ADMINISTRATION_GENERAL);
59+
if (version_compare(ZABBIX_VERSION, '5.4.0', '>=')) {
60+
return $this->checkAccess(CRoleHelper::UI_ADMINISTRATION_GENERAL);
61+
} else {
62+
return ($this->getUserType() == USER_TYPE_SUPER_ADMIN);
63+
}
6064
}
6165

6266
/**

modules/csv-host-importer/manifest.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
"manifest_version": 1.0,
33
"id": "intellitrend_csv_host_importer",
44
"name": "CSV Host Importer",
5-
"version": "5.0.2",
5+
"version": "5.4.2",
66
"namespace": "ichi",
77
"author": "IntelliTrend GmbH",
88
"url": "https://www.intellitrend.de/",

modules/csv-host-importer/views/layout.ichi.example.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
/**
33
* Zabbix CSV Import Frontend Module
44
*
5-
* @version 5.0.2
5+
* @version 5.0.0
66
* @author Wolfgang Alper <[email protected]>
77
* @copyright IntelliTrend GmbH, https://www.intellitrend.de
88
* @license GNU Lesser General Public License v3.0

modules/csv-host-importer/views/module.ichi.import.php

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
/**
33
* Zabbix CSV Import Frontend Module
44
*
5-
* @version 5.0.2
5+
* @version 5.0.0
66
* @author Wolfgang Alper <[email protected]>
77
* @copyright IntelliTrend GmbH, https://www.intellitrend.de
88
* @license GNU Lesser General Public License v3.0
@@ -66,9 +66,10 @@
6666
);
6767
$hostlist = $data['hostlist'];
6868

69-
$table = (new CTable())
70-
->setId('hostlist-table')
71-
->addClass(ZBX_STYLE_VALUEMAP_LIST_TABLE);
69+
$table = (new CTable())->setId('hostlist-table');
70+
if (defined('ZBX_STYLE_VALUEMAP_LIST_TABLE')) {
71+
$table->addClass(ZBX_STYLE_VALUEMAP_LIST_TABLE);
72+
}
7273

7374
$cols = [];
7475

@@ -99,9 +100,10 @@
99100
// import
100101
$hostlist = $data['hostlist'];
101102

102-
$table = (new CTable())
103-
->setId('hostlist-table')
104-
->addClass(ZBX_STYLE_VALUEMAP_LIST_TABLE);
103+
$table = (new CTable())->setId('hostlist-table');
104+
if (defined('ZBX_STYLE_VALUEMAP_LIST_TABLE')) {
105+
$table->addClass(ZBX_STYLE_VALUEMAP_LIST_TABLE);
106+
}
105107

106108
$table->setColumns([
107109
(new CTableColumn(_('Name')))

0 commit comments

Comments
 (0)