Issues (2963)

app/Providers/AppServiceProvider.php (1 issue)

Labels
Severity
1
<?php
2
3
namespace App\Providers;
4
5
use App\Models\Sensor;
6
use Illuminate\Database\Eloquent\Relations\Relation;
7
use Illuminate\Support\Facades\Blade;
8
use Illuminate\Support\Facades\Log;
9
use Illuminate\Support\ServiceProvider;
10
use LibreNMS\Cache\PermissionsCache;
11
use LibreNMS\Config;
12
use LibreNMS\Util\IP;
13
use LibreNMS\Util\Validate;
14
use Validator;
15
16
class AppServiceProvider extends ServiceProvider
17
{
18
    /**
19
     * Register any application services.
20
     *
21
     * @return void
22
     */
23
    public function register()
24
    {
25
        $this->registerFacades();
26
        $this->registerGeocoder();
27
28
        $this->app->singleton('permissions', function ($app) {
29
            return new PermissionsCache();
30
        });
31
        $this->app->singleton('device-cache', function ($app) {
32
            return new \LibreNMS\Cache\Device();
33
        });
34
    }
35
36
    /**
37
     * Bootstrap any application services.
38
     *
39
     * @return void
40
     */
41
    public function boot()
42
    {
43
        \Illuminate\Pagination\Paginator::useBootstrap();
44
45
        $this->app->booted('\LibreNMS\DB\Eloquent::initLegacyListeners');
46
        $this->app->booted('\LibreNMS\Config::load');
47
48
        $this->bootCustomBladeDirectives();
49
        $this->bootCustomValidators();
50
        $this->configureMorphAliases();
51
        $this->bootObservers();
52
    }
53
54
    private function bootCustomBladeDirectives()
55
    {
56
        Blade::if('config', function ($key) {
57
            return \LibreNMS\Config::get($key);
58
        });
59
        Blade::if('notconfig', function ($key) {
60
            return ! \LibreNMS\Config::get($key);
61
        });
62
        Blade::if('admin', function () {
63
            return auth()->check() && auth()->user()->isAdmin();
64
        });
65
66
        Blade::directive('deviceUrl', function ($arguments) {
67
            return "<?php echo \LibreNMS\Util\Url::deviceUrl($arguments); ?>";
68
        });
69
    }
70
71
    private function configureMorphAliases()
72
    {
73
        $sensor_types = [];
74
        foreach (Sensor::getTypes() as $sensor_type) {
75
            $sensor_types[$sensor_type] = \App\Models\Sensor::class;
76
        }
77
        Relation::morphMap(array_merge([
78
            'interface' => \App\Models\Port::class,
79
            'sensor' => \App\Models\Sensor::class,
80
            'device' => \App\Models\Device::class,
81
            'device_group' => \App\Models\DeviceGroup::class,
82
            'location' => \App\Models\Location::class,
83
        ], $sensor_types));
84
    }
85
86
    private function registerFacades()
87
    {
88
        // replace log manager so we can add the event function
89
        $this->app->bind('log', function ($app) {
90
            return new \App\Facades\LogManager($app);
91
        });
92
    }
93
94
    private function registerGeocoder()
95
    {
96
        $this->app->alias(\LibreNMS\Interfaces\Geocoder::class, 'geocoder');
97
        $this->app->bind(\LibreNMS\Interfaces\Geocoder::class, function ($app) {
98
            $engine = Config::get('geoloc.engine');
99
100
            switch ($engine) {
101
                case 'mapquest':
102
                    Log::debug('MapQuest geocode engine');
103
104
                    return $app->make(\App\ApiClients\MapquestApi::class);
105
                case 'bing':
106
                    Log::debug('Bing geocode engine');
107
108
                    return $app->make(\App\ApiClients\BingApi::class);
109
                case 'openstreetmap':
110
                    Log::debug('OpenStreetMap geocode engine');
111
112
                    return $app->make(\App\ApiClients\NominatimApi::class);
113
                default:
114
                case 'google':
115
                    Log::debug('Google Maps geocode engine');
116
117
                    return $app->make(\App\ApiClients\GoogleMapsApi::class);
118
            }
119
        });
120
    }
121
122
    private function bootObservers()
123
    {
124
        \App\Models\Device::observe(\App\Observers\DeviceObserver::class);
125
        \App\Models\Service::observe(\App\Observers\ServiceObserver::class);
126
    }
127
128
    private function bootCustomValidators()
129
    {
130
        Validator::extend('alpha_space', function ($attribute, $value) {
131
            return preg_match('/^[\w\s]+$/u', $value);
132
        });
133
134
        Validator::extend('ip_or_hostname', function ($attribute, $value, $parameters, $validator) {
135
            $ip = substr($value, 0, strpos($value, '/') ?: strlen($value)); // allow prefixes too
136
137
            return IP::isValid($ip) || Validate::hostname($value);
138
        });
139
140
        Validator::extend('is_regex', function ($attribute, $value) {
141
            return @preg_match($value, '') !== false;
142
        });
143
144
        Validator::extend('keys_in', function ($attribute, $value, $parameters, $validator) {
145
            $extra_keys = is_array($value) ? array_diff(array_keys($value), $parameters) : [];
146
147
            $validator->addReplacer('keys_in', function ($message, $attribute, $rule, $parameters) use ($extra_keys) {
148
                return str_replace(
149
                    [':extra', ':values'],
150
                    [implode(',', $extra_keys), implode(',', $parameters)],
151
                    $message);
152
            });
153
154
            return is_array($value) && empty($extra_keys);
155
        });
156
157
        Validator::extend('zero_or_exists', function ($attribute, $value, $parameters, $validator) {
158
            if ($value === 0 || $value === '0') {
159
                return true;
160
            }
161
162
            $validator = Validator::make([$attribute => $value], [$attribute => 'exists:' . implode(',', $parameters)]);
163
164
            return $validator->passes();
165
        }, trans('validation.exists'));
0 ignored issues
show
It seems like trans('validation.exists') can also be of type array and array; however, parameter $message of Illuminate\Support\Facades\Validator::extend() 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

165
        }, /** @scrutinizer ignore-type */ trans('validation.exists'));
Loading history...
166
    }
167
}
168