Issues (2963)

includes/html/forms/sql-from-alert-rules.inc.php (1 issue)

1
<?php
2
/**
3
 * alert-rules.inc.php
4
 *
5
 * LibreNMS alert-rules.inc.php for processor
6
 *
7
 * This program is free software: you can redistribute it and/or modify
8
 * it under the terms of the GNU General Public License as published by
9
 * the Free Software Foundation, either version 3 of the License, or
10
 * (at your option) any later version.
11
 *
12
 * This program is distributed in the hope that it will be useful,
13
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.See the
15
 * GNU General Public License for more details.
16
 *
17
 * You should have received a copy of the GNU General Public License
18
 * along with this program.  If not, see <https://www.gnu.org/licenses/>.
19
 *
20
 * @link       https://www.librenms.org
21
 *
22
 * @copyright  2020 Thomas Berberich
23
 * @author     Thomas Berberich <[email protected]>
24
 */
25
26
use LibreNMS\Alerting\QueryBuilderParser;
27
use LibreNMS\Config;
0 ignored issues
show
This use statement conflicts with another class in this namespace, Config. Consider defining an alias.

Let?s assume that you have a directory layout like this:

.
|-- OtherDir
|   |-- Bar.php
|   `-- Foo.php
`-- SomeDir
    `-- Foo.php

and let?s assume the following content of Bar.php:

// Bar.php
namespace OtherDir;

use SomeDir\Foo; // This now conflicts the class OtherDir\Foo

If both files OtherDir/Foo.php and SomeDir/Foo.php are loaded in the same runtime, you will see a PHP error such as the following:

PHP Fatal error:  Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.php

However, as OtherDir/Foo.php does not necessarily have to be loaded and the error is only triggered if it is loaded before OtherDir/Bar.php, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias:

// Bar.php
namespace OtherDir;

use SomeDir\Foo as SomeDirFoo; // There is no conflict anymore.
Loading history...
28
29
header('Content-type: application/json');
30
31
if (! Auth::user()->hasGlobalAdmin()) {
32
    exit(json_encode([
33
        'status' => 'error',
34
        'message' => 'ERROR: You need to be admin',
35
    ]));
36
}
37
38
$rule_id = $vars['rule_id'];
39
40
if (is_numeric($rule_id)) {
41
    $rule = dbFetchRow('SELECT * FROM alert_rules where id=?', [$rule_id]);
42
43
    $default_extra = [
44
        'mute' => Config::get('alert_rule.mute_alerts'),
45
        'count' => Config::get('alert_rule.max_alerts'),
46
        'delay' => 60 * Config::get('alert_rule.delay'),
47
        'invert' => Config::get('alert_rule.invert_rule_match'),
48
        'interval' => 60 * Config::get('alert_rule.interval'),
49
        'recovery' => Config::get('alert_rule.recovery_alerts'),
50
    ];
51
    $output = [
52
        'status' => 'ok',
53
        'name' => $rule['name'] . ' - Copy',
54
        'builder' => QueryBuilderParser::fromJson($rule['builder']),
55
        'extra' => array_replace($default_extra, (array) json_decode($rule['extra'])),
56
        'severity' => $rule['severity'] ?: Config::get('alert_rule.severity'),
57
        'invert_map' => $rule['invert_map'],
58
    ];
59
} else {
60
    $output = [
61
        'status' => 'error',
62
        'message' => 'Invalid template',
63
    ];
64
}
65
66
exit(json_encode($output));
67