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
|
<?php
/*
** Zabbix
** Copyright (C) 2001-2014 Zabbix SIA
**
** This program is free software; you can redistribute it and/or modify
** it under the terms of the GNU General Public License as published by
** the Free Software Foundation; either version 2 of the License, or
** (at your option) any later version.
**
** This program 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. See the
** GNU General Public License for more details.
**
** You should have received a copy of the GNU General Public License
** along with this program; if not, write to the Free Software
** Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
**/
/**
* Class containing methods for operations with icon maps.
*
* Icon maps work only with map elements that represent hosts, and used for automatic changing
* icons based on host inventory data.
* Icon maps has read access for everyone and write access only for superamins
*
* @package API
*/
class CIconMap extends CApiService {
protected $tableName = 'icon_map';
protected $tableAlias = 'im';
protected $sortColumns = array('iconmapid', 'name');
/**
* Get IconMap data.
* @param array $options
* @param array $options['iconmapids']
* @param array $options['sysmapids']
* @param array $options['editable']
* @param array $options['count']
* @param array $options['limit']
* @param array $options['order']
* @return array
*/
public function get(array $options = array()) {
$result = array();
$sqlParts = array(
'select' => array('icon_map' => 'im.iconmapid'),
'from' => array('icon_map' => 'icon_map im'),
'where' => array(),
'order' => array(),
'limit' => null
);
$defOptions = array(
'iconmapids' => null,
'sysmapids' => null,
'nopermissions' => null,
'editable' => null,
// filter
'filter' => null,
'search' => null,
'searchByAny' => null,
'startSearch' => null,
'excludeSearch' => null,
'searchWildcardsEnabled' => null,
// output
'output' => API_OUTPUT_EXTEND,
'selectMappings' => null,
'countOutput' => null,
'preservekeys' => null,
'sortfield' => '',
'sortorder' => '',
'limit' => null
);
$options = zbx_array_merge($defOptions, $options);
// editable + PERMISSION CHECK
if ($options['editable'] && self::$userData['type'] != USER_TYPE_SUPER_ADMIN) {
return array();
}
// iconmapids
if (!is_null($options['iconmapids'])) {
zbx_value2array($options['iconmapids']);
$sqlParts['where'][] = dbConditionInt('im.iconmapid', $options['iconmapids']);
}
// sysmapids
if (!is_null($options['sysmapids'])) {
zbx_value2array($options['sysmapids']);
$sqlParts['from']['sysmaps'] = 'sysmaps s';
$sqlParts['where'][] = dbConditionInt('s.sysmapid', $options['sysmapids']);
$sqlParts['where']['ims'] = 'im.iconmapid=s.iconmapid';
}
// filter
if (is_array($options['filter'])) {
$this->dbFilter('icon_map im', $options, $sqlParts);
}
// search
if (is_array($options['search'])) {
zbx_db_search('icon_map im', $options, $sqlParts);
}
// limit
if (zbx_ctype_digit($options['limit']) && $options['limit']) {
$sqlParts['limit'] = $options['limit'];
}
$sqlParts = $this->applyQueryOutputOptions($this->tableName(), $this->tableAlias(), $options, $sqlParts);
$sqlParts = $this->applyQuerySortOptions($this->tableName(), $this->tableAlias(), $options, $sqlParts);
$dbRes = DBselect($this->createSelectQueryFromParts($sqlParts), $sqlParts['limit']);
while ($iconMap = DBfetch($dbRes)) {
if ($options['countOutput']) {
$result = $iconMap['rowscount'];
}
else {
$result[$iconMap['iconmapid']] = $iconMap;
}
}
if (!is_null($options['countOutput'])) {
return $result;
}
if ($result) {
$result = $this->addRelatedObjects($options, $result);
}
// removing keys (hash -> array)
if (is_null($options['preservekeys'])) {
$result = zbx_cleanHashes($result);
}
return $result;
}
/**
* Add IconMap.
* @param array $iconMaps
* @return array
*/
public function create(array $iconMaps) {
if (USER_TYPE_SUPER_ADMIN != self::$userData['type']) {
self::exception(ZBX_API_ERROR_PERMISSIONS, _('Only Super Admins can create icon maps.'));
}
$iconMaps = zbx_toArray($iconMaps);
$iconMapRequiredFields = array('name' => null);
$duplicates = array();
foreach ($iconMaps as $iconMap) {
if (!check_db_fields($iconMapRequiredFields, $iconMap)) {
self::exception(ZBX_API_ERROR_PARAMETERS, _s('Incorrect parameter is used for icon map "%s".', $iconMap['name']));
}
if (zbx_empty($iconMap['name'])) {
self::exception(ZBX_API_ERROR_PARAMETERS, _s('Icon map name cannot be empty.'));
}
if (isset($duplicates[$iconMap['name']])) {
self::exception(ZBX_API_ERROR_PARAMETERS, _s('Cannot create icon maps with identical name "%s".', $iconMap['name']));
}
else {
$duplicates[$iconMap['name']] = $iconMap['name'];
}
}
$this->validateMappings($iconMaps);
$options = array(
'filter' => array('name' => $duplicates),
'output' => array('name'),
'editable' => true,
'nopermissions' => true
);
$dbIconMaps = $this->get($options);
foreach ($dbIconMaps as $dbIconMap) {
self::exception(ZBX_API_ERROR_PARAMETERS, _s('Icon map "%s" already exists.', $dbIconMap['name']));
}
$iconMapids = DB::insert('icon_map', $iconMaps);
$mappings = array();
foreach ($iconMaps as $imnum => $iconMap) {
foreach ($iconMap['mappings'] as $mapping) {
$mapping['iconmapid'] = $iconMapids[$imnum];
$mappings[] = $mapping;
}
}
DB::insert('icon_mapping', $mappings);
return array('iconmapids' => $iconMapids);
}
/**
* Update IconMap.
* @param array $iconMaps
* @return array
*/
public function update(array $iconMaps) {
if (USER_TYPE_SUPER_ADMIN != self::$userData['type']) {
self::exception(ZBX_API_ERROR_PERMISSIONS, _('Only Super Admins can update icon maps.'));
}
$iconMaps = zbx_toArray($iconMaps);
$iconMapids = zbx_objectValues($iconMaps, 'iconmapid');
$updates = array();
$duplicates = array();
foreach ($iconMaps as $iconMap) {
if (!check_db_fields(array('iconmapid' => null), $iconMap)) {
self::exception(ZBX_API_ERROR_PARAMETERS, _s('Incorrect parameters for icon map update method "%s".', $iconMap['name']));
}
if (isset($iconMap['name'])) {
if (zbx_empty($iconMap['name'])) {
self::exception(ZBX_API_ERROR_PARAMETERS, _s('Icon map name cannot be empty.'));
}
elseif (isset($duplicates[$iconMap['name']])) {
self::exception(ZBX_API_ERROR_PARAMETERS, _s('Cannot create icon maps with identical name "%s".', $iconMap['name']));
}
else {
$duplicates[$iconMap['name']] = $iconMap['name'];
}
}
}
$this->validateMappings($iconMaps, false);
$iconMapsUpd = API::IconMap()->get(array(
'iconmapids' => $iconMapids,
'output' => API_OUTPUT_EXTEND,
'preservekeys' => true,
'selectMappings' => API_OUTPUT_EXTEND
));
$oldIconMappings = array();
$newIconMappings = array();
foreach ($iconMaps as $iconMap) {
if (!isset($iconMapsUpd[$iconMap['iconmapid']])) {
self::exception(ZBX_API_ERROR_PARAMETERS, _s('Icon map with iconmapid "%s" does not exist.', $iconMap['iconmapid']));
}
// Existence
if (isset($iconMap['name'])) {
$iconMapExists = $this->get(array(
'filter' => array('name' => $iconMap['name']),
'output' => array('iconmapid'),
'editable' => true,
'nopermissions' => true,
'preservekeys' => true
));
if (($iconMapExists = reset($iconMapExists)) && (bccomp($iconMapExists['iconmapid'], $iconMap['iconmapid']) != 0)) {
self::exception(ZBX_API_ERROR_PARAMETERS, _s('Icon map "%s" already exists.', $iconMap['name']));
}
}
if (isset($iconMap['mappings'])) {
$mappingsDb = $iconMapsUpd[$iconMap['iconmapid']]['mappings'];
foreach ($mappingsDb as $mapping) {
$oldIconMappings[] = $mapping;
}
foreach ($iconMap['mappings'] as $mapping) {
$mapping['iconmapid'] = $iconMap['iconmapid'];
$newIconMappings[] = $mapping;
}
}
$iconMapid = $iconMap['iconmapid'];
unset($iconMap['iconmapid']);
if (!empty($iconMap)) {
$updates[] = array(
'values' => $iconMap,
'where' => array('iconmapid' => $iconMapid)
);
}
}
DB::save('icon_map', $iconMaps);
DB::replace('icon_mapping', $oldIconMappings, $newIconMappings);
return array('iconmapids' => $iconMapids);
}
/**
* Delete IconMap.
*
* @param array $iconmapids
*
* @return array
*/
public function delete(array $iconmapids) {
if (empty($iconmapids)) {
self::exception(ZBX_API_ERROR_PARAMETERS, _('Empty input parameter.'));
}
if (!$this->isWritable($iconmapids)) {
self::exception(ZBX_API_ERROR_PERMISSIONS, _('No permissions to referred object or it does not exist!'));
}
$sql = 'SELECT m.name AS mapname, im.name as iconmapname'.
' FROM sysmaps m, icon_map im'.
' WHERE m.iconmapid=im.iconmapid'.
' AND '.dbConditionInt('m.iconmapid', $iconmapids);
if ($names = DBfetch(DBselect($sql))) {
self::exception(ZBX_API_ERROR_PARAMETERS,
_s('Icon map "%1$s" cannot be deleted. Used in map "%2$s".', $names['iconmapname'], $names['mapname'])
);
}
DB::delete('icon_map', array('iconmapid' => $iconmapids));
return array('iconmapids' => $iconmapids);
}
/**
* Check if user has read permissions for given icon map IDs.
* @param $ids
* @return bool
*/
public function isReadable($ids) {
if (!is_array($ids)) {
return false;
}
if (empty($ids)) {
return true;
}
$ids = array_unique($ids);
$count = $this->get(array(
'iconmapids' => $ids,
'countOutput' => true
));
return (count($ids) == $count);
}
/**
* Check if user has write permissions for given icon map IDs.
* @param $ids
* @return bool
*/
public function isWritable($ids) {
if (!is_array($ids)) {
return false;
}
if (empty($ids)) {
return true;
}
$ids = array_unique($ids);
$count = $this->get(array(
'iconmapids' => $ids,
'editable' => true,
'countOutput' => true
));
return count($ids) == $count;
}
/**
* Checks icon maps.
* @throws APIException
* @param $iconMaps
* @param bool $mustExist if icon map should be checked against having at least one mapping
* @return void
*/
protected function validateMappings($iconMaps, $mustExist = true) {
$inventoryFields = getHostInventories();
$imageids = API::Image()->get(array(
'output' => array('imageid'),
'preservekeys' => true,
'filter' => array('imagetype' => IMAGE_TYPE_ICON)
));
foreach ($iconMaps as $iconMap) {
if (isset($iconMap['mappings']) && empty($iconMap['mappings'])) {
self::exception(ZBX_API_ERROR_PARAMETERS,
_s('Icon map "%s" must have at least one mapping.', $iconMap['name']));
}
elseif (!isset($iconMap['mappings'])) {
if ($mustExist) {
self::exception(ZBX_API_ERROR_PARAMETERS,
_s('Icon map "%s" must have at least one mapping.', $iconMap['name']));
}
else {
continue;
}
}
$uniqField = array();
foreach ($iconMap['mappings'] as $mapping) {
if (!isset($mapping['expression'])) {
self::exception(ZBX_API_ERROR_PARAMETERS, _('Required field "expression" is missing in icon mapping.'));
}
elseif (!isset($mapping['inventory_link'])) {
self::exception(ZBX_API_ERROR_PARAMETERS, _('Required field "inventory_link" is missing in icon mapping.'));
}
elseif (!isset($mapping['iconid'])) {
self::exception(ZBX_API_ERROR_PARAMETERS, _('Required field "iconid" is missing in icon mapping.'));
}
elseif (!isset($inventoryFields[$mapping['inventory_link']])) {
self::exception(ZBX_API_ERROR_PARAMETERS,
_s('Icon map "%1$s" has mapping with incorrect inventory link "%2$s".', $iconMap['name'], $mapping['inventory_link']));
}
elseif (!isset($imageids[$mapping['iconid']])) {
self::exception(ZBX_API_ERROR_PARAMETERS,
_s('Icon map "%1$s" has mapping with incorrect iconid "%2$s".', $iconMap['name'], $mapping['iconid']));
}
try {
GlobalRegExp::isValid($mapping['expression']);
}
catch (Exception $e) {
switch ($e->getCode()) {
case GlobalRegExp::ERROR_REGEXP_EMPTY:
self::exception(ZBX_API_ERROR_PARAMETERS,
_s('Icon map "%s" cannot have mapping with empty expression.', $iconMap['name']));
break;
case GlobalRegExp::ERROR_REGEXP_NOT_EXISTS:
self::exception(ZBX_API_ERROR_PARAMETERS,
_s('Icon map "%s" cannot have mapping with global expression that does not exist.', $iconMap['name']));
break;
default:
self::exception(ZBX_API_ERROR_PARAMETERS,
_s('Icon map "%s" has incorrect expression.', $iconMap['name']));
}
}
if (isset($uniqField[$mapping['inventory_link'].$mapping['expression']])) {
self::exception(ZBX_API_ERROR_PARAMETERS,
_s('Icon mapping entry "%1$s" against "%2$s" already exists.',
$mapping['expression'],
$inventoryFields[$mapping['inventory_link']]['title'])
);
}
$uniqField[$mapping['inventory_link'].$mapping['expression']] = true;
}
}
}
protected function addRelatedObjects(array $options, array $result) {
$result = parent::addRelatedObjects($options, $result);
$iconMapIds = array_keys($result);
if ($options['selectMappings'] !== null && $options['selectMappings'] != API_OUTPUT_COUNT) {
$mappings = API::getApiService()->select('icon_mapping', array(
'output' => $this->outputExtend($options['selectMappings'], array('iconmapid', 'iconmappingid')),
'filter' => array('iconmapid' => $iconMapIds),
'preservekeys' => true
));
$relationMap = $this->createRelationMap($mappings, 'iconmapid', 'iconmappingid');
$mappings = $this->unsetExtraFields($mappings, array('iconmapid', 'iconmappingid'), $options['selectMappings']);
$result = $relationMap->mapMany($result, $mappings, 'mappings');
}
return $result;
}
}
|