2018-05-09 08:05:17 -05:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace App\Providers;
|
|
|
|
|
2018-09-24 02:07:00 -05:00
|
|
|
use Illuminate\Database\Eloquent\Relations\Relation;
|
2018-05-09 08:05:17 -05:00
|
|
|
use Illuminate\Support\Facades\Blade;
|
|
|
|
use Illuminate\Support\Facades\Log;
|
|
|
|
use Illuminate\Support\ServiceProvider;
|
|
|
|
use LibreNMS\Config;
|
2019-03-19 08:14:01 -05:00
|
|
|
use LibreNMS\Permissions;
|
2019-01-25 15:30:58 -06:00
|
|
|
use LibreNMS\Util\IP;
|
|
|
|
use LibreNMS\Util\Validate;
|
|
|
|
use Validator;
|
2018-05-09 08:05:17 -05:00
|
|
|
|
|
|
|
class AppServiceProvider extends ServiceProvider
|
|
|
|
{
|
|
|
|
/**
|
2019-03-19 08:14:01 -05:00
|
|
|
* Register any application services.
|
2018-05-09 08:05:17 -05:00
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
2019-03-19 08:14:01 -05:00
|
|
|
public function register()
|
2018-05-09 08:05:17 -05:00
|
|
|
{
|
2019-03-19 08:14:01 -05:00
|
|
|
$this->registerFacades();
|
|
|
|
$this->registerGeocoder();
|
2018-05-09 08:05:17 -05:00
|
|
|
|
2019-03-19 08:14:01 -05:00
|
|
|
$this->app->singleton('permissions', function ($app) {
|
|
|
|
return new Permissions();
|
|
|
|
});
|
2019-11-14 21:56:06 +00:00
|
|
|
$this->app->singleton('device-cache', function ($app) {
|
|
|
|
return new \LibreNMS\Cache\Device();
|
|
|
|
});
|
2018-05-09 08:05:17 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2019-03-19 08:14:01 -05:00
|
|
|
* Bootstrap any application services.
|
2018-05-09 08:05:17 -05:00
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
2019-03-19 08:14:01 -05:00
|
|
|
public function boot()
|
2018-05-09 08:05:17 -05:00
|
|
|
{
|
2019-03-19 08:14:01 -05:00
|
|
|
$this->app->booted('\LibreNMS\DB\Eloquent::initLegacyListeners');
|
|
|
|
$this->app->booted('\LibreNMS\Config::load');
|
|
|
|
|
|
|
|
$this->bootCustomBladeDirectives();
|
|
|
|
$this->bootCustomValidators();
|
|
|
|
$this->configureMorphAliases();
|
2018-05-09 08:05:17 -05:00
|
|
|
}
|
2018-09-24 02:07:00 -05:00
|
|
|
|
2019-02-12 17:45:04 -06:00
|
|
|
private function bootCustomBladeDirectives()
|
|
|
|
{
|
|
|
|
Blade::if('config', function ($key) {
|
|
|
|
return \LibreNMS\Config::get($key);
|
|
|
|
});
|
|
|
|
Blade::if('notconfig', function ($key) {
|
|
|
|
return !\LibreNMS\Config::get($key);
|
|
|
|
});
|
|
|
|
Blade::if('admin', function () {
|
|
|
|
return auth()->check() && auth()->user()->isAdmin();
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2018-09-24 02:07:00 -05:00
|
|
|
private function configureMorphAliases()
|
|
|
|
{
|
|
|
|
Relation::morphMap([
|
|
|
|
'interface' => \App\Models\Port::class,
|
|
|
|
'sensor' => \App\Models\Sensor::class,
|
2019-01-03 16:42:12 -06:00
|
|
|
'device' => \App\Models\Device::class,
|
|
|
|
'device_group' => \App\Models\DeviceGroup::class,
|
2018-09-24 02:07:00 -05:00
|
|
|
]);
|
|
|
|
}
|
Refactored and update Location Geocoding (#9359)
- Fix location so it is a regular database relation (this allows multiple devices to be accurately linked to one location and saves api calls)
- Parse coordinates from the location more consistently
- Add settings to webui
- ~~Used [PHP Geocoder](http://geocoder-php.org/), which has lots of backends and is well tested. (also includes reverse and geoip)~~
- Google Maps, Bing, Mapquest, and OpenStreetMap supported initially.
- Default to OpenStreetMap, which doesn't require a key. They will liberally hand out bans if you exceed 1 query per second though.
- All other Geocoding APIs require an API key. (Google requires a credit card on file, but seems to be the most accurate)
- Update all (I think) sql queries to handle the new structure
- Remove final vestiges of override_sysLocation as a device attribute
- Update existing device groups and rules in DB
- Tested all APIs with good/bad location, no/bad/good key, and no connection.
- Cannot fix advanced queries that use location
This blocks #8868
DO NOT DELETE THIS TEXT
#### Please note
> Please read this information carefully. You can run `./scripts/pre-commit.php` to check your code before submitting.
- [x] Have you followed our [code guidelines?](http://docs.librenms.org/Developing/Code-Guidelines/)
#### Testers
If you would like to test this pull request then please run: `./scripts/github-apply <pr_id>`, i.e `./scripts/github-apply 5926`
After you are done testing, you can remove the changes with `./scripts/github-remove`. If there are schema changes, you can ask on discord how to revert.
2018-11-28 16:49:18 -06:00
|
|
|
|
2019-03-12 23:59:03 -05:00
|
|
|
private function registerFacades()
|
|
|
|
{
|
|
|
|
// replace log manager so we can add the event function
|
|
|
|
$this->app->bind('log', function ($app) {
|
|
|
|
return new \App\Facades\LogManager($app);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
Refactored and update Location Geocoding (#9359)
- Fix location so it is a regular database relation (this allows multiple devices to be accurately linked to one location and saves api calls)
- Parse coordinates from the location more consistently
- Add settings to webui
- ~~Used [PHP Geocoder](http://geocoder-php.org/), which has lots of backends and is well tested. (also includes reverse and geoip)~~
- Google Maps, Bing, Mapquest, and OpenStreetMap supported initially.
- Default to OpenStreetMap, which doesn't require a key. They will liberally hand out bans if you exceed 1 query per second though.
- All other Geocoding APIs require an API key. (Google requires a credit card on file, but seems to be the most accurate)
- Update all (I think) sql queries to handle the new structure
- Remove final vestiges of override_sysLocation as a device attribute
- Update existing device groups and rules in DB
- Tested all APIs with good/bad location, no/bad/good key, and no connection.
- Cannot fix advanced queries that use location
This blocks #8868
DO NOT DELETE THIS TEXT
#### Please note
> Please read this information carefully. You can run `./scripts/pre-commit.php` to check your code before submitting.
- [x] Have you followed our [code guidelines?](http://docs.librenms.org/Developing/Code-Guidelines/)
#### Testers
If you would like to test this pull request then please run: `./scripts/github-apply <pr_id>`, i.e `./scripts/github-apply 5926`
After you are done testing, you can remove the changes with `./scripts/github-remove`. If there are schema changes, you can ask on discord how to revert.
2018-11-28 16:49:18 -06:00
|
|
|
private function registerGeocoder()
|
|
|
|
{
|
|
|
|
$this->app->alias(\LibreNMS\Interfaces\Geocoder::class, 'geocoder');
|
|
|
|
$this->app->bind(\LibreNMS\Interfaces\Geocoder::class, function ($app) {
|
|
|
|
$engine = Config::get('geoloc.engine');
|
|
|
|
|
|
|
|
switch ($engine) {
|
|
|
|
case 'mapquest':
|
|
|
|
Log::debug('MapQuest geocode engine');
|
|
|
|
return $app->make(\App\ApiClients\MapquestApi::class);
|
|
|
|
case 'bing':
|
|
|
|
Log::debug('Bing geocode engine');
|
|
|
|
return $app->make(\App\ApiClients\BingApi::class);
|
|
|
|
case 'openstreetmap':
|
|
|
|
Log::debug('OpenStreetMap geocode engine');
|
|
|
|
return $app->make(\App\ApiClients\NominatimApi::class);
|
|
|
|
default:
|
|
|
|
case 'google':
|
|
|
|
Log::debug('Google Maps geocode engine');
|
|
|
|
return $app->make(\App\ApiClients\GoogleMapsApi::class);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
2019-01-25 15:30:58 -06:00
|
|
|
|
|
|
|
private function bootCustomValidators()
|
|
|
|
{
|
2019-08-21 20:36:22 -05:00
|
|
|
Validator::extend('alpha_space', function ($attribute, $value) {
|
|
|
|
return preg_match('/^[\w\s]+$/u', $value);
|
|
|
|
});
|
|
|
|
|
2019-01-25 15:30:58 -06:00
|
|
|
Validator::extend('ip_or_hostname', function ($attribute, $value, $parameters, $validator) {
|
|
|
|
$ip = substr($value, 0, strpos($value, '/') ?: strlen($value)); // allow prefixes too
|
|
|
|
return IP::isValid($ip) || Validate::hostname($value);
|
|
|
|
}, __('The :attribute must a valid IP address/network or hostname.'));
|
2019-10-16 21:22:05 +00:00
|
|
|
|
|
|
|
Validator::extend('zero_or_exists', function ($attribute, $value, $parameters, $validator) {
|
|
|
|
if ($value === 0) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
$validator = Validator::make([$attribute => $value], [$attribute => 'exists:' . implode(',', $parameters)]);
|
|
|
|
return $validator->passes();
|
|
|
|
}, __('validation.exists'));
|
2019-01-25 15:30:58 -06:00
|
|
|
}
|
2018-05-09 08:05:17 -05:00
|
|
|
}
|