65 lines
1.5 KiB
PHP
65 lines
1.5 KiB
PHP
|
|
<?php
|
||
|
|
|
||
|
|
namespace App\Http\Requests\LDCNetworks;
|
||
|
|
|
||
|
|
use App\Rules\CidrRule;
|
||
|
|
use Illuminate\Foundation\Http\FormRequest;
|
||
|
|
use Illuminate\Support\Facades\Auth;
|
||
|
|
use Illuminate\Support\Facades\Session;
|
||
|
|
|
||
|
|
class StoreLDCNetwork extends FormRequest
|
||
|
|
{
|
||
|
|
/**
|
||
|
|
* Determine if the user is authorized to make this request.
|
||
|
|
*
|
||
|
|
* @return boolean
|
||
|
|
*/
|
||
|
|
public function authorize()
|
||
|
|
{
|
||
|
|
return Auth::check();
|
||
|
|
}
|
||
|
|
|
||
|
|
/**
|
||
|
|
* Prepare the data for validation.
|
||
|
|
*
|
||
|
|
* @return void
|
||
|
|
*/
|
||
|
|
protected function prepareForValidation()
|
||
|
|
{
|
||
|
|
$this->merge($this->cidrToRange());
|
||
|
|
}
|
||
|
|
|
||
|
|
/**
|
||
|
|
* Get the validation rules that apply to the request.
|
||
|
|
*
|
||
|
|
* @return array
|
||
|
|
*/
|
||
|
|
public function rules()
|
||
|
|
{
|
||
|
|
return [
|
||
|
|
'cidr' => ['required', new CidrRule()],
|
||
|
|
'first_ip' => 'required|ipv4',
|
||
|
|
'last_ip' => 'required|ipv4'
|
||
|
|
];
|
||
|
|
}
|
||
|
|
|
||
|
|
/**
|
||
|
|
* cidr To Ranges
|
||
|
|
*
|
||
|
|
* @return array ['first_ip', 'last_ip']
|
||
|
|
*/
|
||
|
|
private function cidrToRange(): array
|
||
|
|
{
|
||
|
|
$first_ip = '';
|
||
|
|
$last_ip = '';
|
||
|
|
$cidr = collect(explode('/', \strval($this->cidr)));
|
||
|
|
try {
|
||
|
|
$first_ip = long2ip(intval(ip2long($cidr->get(0))) & ((-1 << (32 - intval($cidr->get(1))))));
|
||
|
|
$last_ip = long2ip(intval(ip2long(\strval($first_ip))) + pow(2, (32 - intval($cidr->get(1)))) - 1);
|
||
|
|
} catch (\Error $e) {
|
||
|
|
Session::flash('warning', trans('Le CIDR est mal formé'));
|
||
|
|
}
|
||
|
|
return \compact('first_ip', 'last_ip');
|
||
|
|
}
|
||
|
|
}
|