Issues (2963)

app/Checks.php (4 issues)

1
<?php
2
/**
3
 * Checks.php
4
 *
5
 * Pre-flight checks at various stages of booting
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  2018 Tony Murray
23
 * @author     Tony Murray <[email protected]>
24
 */
25
26
namespace App;
27
28
use App\Models\Device;
29
use App\Models\Notification;
30
use Cache;
31
use Carbon\Carbon;
32
use Illuminate\Support\Facades\Auth;
33
use LibreNMS\Config;
34
use Toastr;
35
36
class Checks
37
{
38
    /**
39
     * Post boot Toast messages
40
     */
41
    public static function postAuth()
42
    {
43
        // limit popup messages frequency
44
        if (Cache::get('checks_popup_timeout') || ! Auth::check()) {
45
            return;
46
        }
47
48
        Cache::put('checks_popup_timeout', true, Config::get('checks_popup_timer', 5) * 60);
49
50
        $user = Auth::user();
51
52
        if ($user->isAdmin()) {
0 ignored issues
show
The method isAdmin() does not exist on Illuminate\Contracts\Auth\Authenticatable. It seems like you code against a sub-type of Illuminate\Contracts\Auth\Authenticatable such as Illuminate\Foundation\Auth\User. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

52
        if ($user->/** @scrutinizer ignore-call */ isAdmin()) {
Loading history...
53
            $notifications = Notification::isUnread($user)->where('severity', '>', \LibreNMS\Enum\Alert::OK)->get();
54
            foreach ($notifications as $notification) {
55
                Toastr::error("<a href='notifications/'>$notification->body</a>", $notification->title);
56
            }
57
58
            $warn_sec = Config::get('rrd.step', 300) * 3;
59
            if (Device::isUp()->where('last_polled', '<=', Carbon::now()->subSeconds($warn_sec))->exists()) {
60
                $warn_min = $warn_sec / 60;
61
                Toastr::warning('<a href="poller/log?filter=unpolled/">It appears as though you have some devices that haven\'t completed polling within the last ' . $warn_min . ' minutes, you may want to check that out :)</a>', 'Devices unpolled');
62
            }
63
64
            // Directory access checks
65
            $rrd_dir = Config::get('rrd_dir');
66
            if (! is_dir($rrd_dir)) {
0 ignored issues
show
It seems like $rrd_dir can also be of type null; however, parameter $filename of is_dir() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

66
            if (! is_dir(/** @scrutinizer ignore-type */ $rrd_dir)) {
Loading history...
67
                Toastr::error("RRD Directory is missing ($rrd_dir).  Graphing may fail. <a href=" . url('validate') . '>Validate your install</a>');
68
            }
69
70
            $temp_dir = Config::get('temp_dir');
71
            if (! is_dir($temp_dir)) {
72
                Toastr::error("Temp Directory is missing ($temp_dir).  Graphing may fail. <a href=" . url('validate') . '>Validate your install</a>');
73
            } elseif (! is_writable($temp_dir)) {
0 ignored issues
show
It seems like $temp_dir can also be of type null; however, parameter $filename of is_writable() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

73
            } elseif (! is_writable(/** @scrutinizer ignore-type */ $temp_dir)) {
Loading history...
74
                Toastr::error("Temp Directory is not writable ($temp_dir).  Graphing may fail. <a href='" . url('validate') . "'>Validate your install</a>");
75
            }
76
        }
77
    }
78
79
    /**
80
     * Check the script is running as the right user (works before config is available)
81
     */
82
    public static function runningUser()
83
    {
84
        if (function_exists('posix_getpwuid') && posix_getpwuid(posix_geteuid())['name'] !== get_current_user()) {
85
            if (get_current_user() == 'root') {
86
                self::printMessage(
87
                    'Error: lnms file is owned by root, it should be owned and ran by a non-privileged user.',
88
                    null,
89
                    true
90
                );
91
            }
92
93
            self::printMessage(
94
                'Error: You must run lnms as the user ' . get_current_user(),
95
                null,
96
                true
97
            );
98
        }
99
    }
100
101
    private static function printMessage($title, $content, $exit = false)
102
    {
103
        $content = (array) $content;
104
105
        if (PHP_SAPI == 'cli') {
106
            $format = "%s\n\n%s\n\n";
107
            $message = implode(PHP_EOL, $content);
108
        } else {
109
            $format = "<h3 style='color: firebrick;'>%s</h3><p>%s</p>";
110
            $message = '';
111
            foreach ($content as $line) {
112
                $message .= "<p style='margin:0.5em'>$line</p>\n";
113
            }
114
        }
115
116
        printf($format, $title, $message);
117
118
        if ($exit) {
119
            exit(1);
0 ignored issues
show
Using exit here is not recommended.

In general, usage of exit should be done with care and only when running in a scripting context like a CLI script.

Loading history...
120
        }
121
    }
122
}
123