summaryrefslogtreecommitdiffstats
path: root/library/Nagvis/RestrictionHelper.php
blob: a757b9d5969f1e8b70caee73d714fb4c83ba936f (plain)
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
<?php

namespace Icinga\Module\Nagvis;

use Icinga\Authentication\Auth;

/**
 * NagVis restriction helper
 */
class RestrictionHelper
{
    /**
     * Get the regular expression for validating map names
     *
     * @return  string|null
     */
    public static function getRegex()
    {
        $mapFilters = array();
        foreach (Auth::getInstance()->getRestrictions('nagvis/map/filter') as $mapFilter) {
            if ($mapFilter !== '') {
                $mapFilters = array_merge($mapFilters, array_map('trim', explode(',', $mapFilter)));
            }
        }

        if (! empty($mapFilters)) {
            $mapRegexParts = array();
            foreach (array_unique($mapFilters) as $mapFilter) {
                $nonWildcards = array();
                foreach (explode('*', $mapFilter) as $nonWildcard) {
                    $nonWildcards[] = preg_quote($nonWildcard, '/');
                }
                $mapRegexParts[] = implode('.*', $nonWildcards);
            }

            return '/^(?:' . implode('|', $mapRegexParts) . ')$/i';
        }

        return null;
    }
}