PHP code example of neelkanthk / laravel-surveillance
1. Go to this page and download the library: Download neelkanthk/laravel-surveillance library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
neelkanthk / laravel-surveillance example snippets
return [
/*
* The name of the header to be used for browser fingerprint
*/
"fingerprint-header-key" => "fingerprint",
/*
* This class is responsible enabling, disabling, blocking and unblocking.
* To override the default functionality extend the below class and provide its name here.
*/
"manager-repository" => 'Neelkanth\Laravel\Surveillance\Implementations\SurveillanceManagerRepository',
/*
* This class is responsible for logging the surveillance enabled requests
* To override the default functionality extend the below class and provide its name here.
*/
"log-repository" => 'Neelkanth\Laravel\Surveillance\Implementations\SurveillanceLogRepository',
/*
* The types which are allowed currently.
* DO NOT MODIFY THESE
*/
"allowed-types" => ["userid", "ip", "fingerprint"]
];
Route::middleware(["surveillance"])->get('/', function () {
});
use Neelkanth\Laravel\Surveillance\Services\Surveillance;
Surveillance::manager()->type("ip")->value("192.5.4.1")->enableSurveillance();
use Neelkanth\Laravel\Surveillance\Services\Surveillance;
Surveillance::manager()->type("userid")->value(2121)->blockAccess();
use Neelkanth\Laravel\Surveillance\Services\Surveillance;
Surveillance::logger()->writeLog();
//Example repository to use MongoDB instead of MySQL
namespace App;
use Neelkanth\Laravel\Surveillance\Implementations\SurveillanceManagerRepository;
use Illuminate\Support\Carbon;
class SurveillanceManagerMongoDbRepository extends SurveillanceManagerRepository
{
public function enableSurveillance()
{
$surveillance = $this->getRecord();
if (is_null($surveillance)) {
$surveillance["type"] = $this->getType();
$surveillance["value"] = $this->getValue();
}
$surveillance["surveillance_enabled"] = 1;
$surveillance["surveillance_enabled_at"] = Carbon::now()->toDateTimeString();
$collection = (new \MongoDB\Client)->surveillance->manager;
$insertOneResult = $collection->insertOne($surveillance);
return $insertOneResult;
}
}
/*
* This class is responsible enabling, disabling, blocking and unblocking.
* To override the default functionality extend the below class and provide its name here.
*/
"manager-repository" => 'App\SurveillanceManagerMongoDbRepository',
//Example repository to write Logs in MongoDB instead of MySQL
namespace App;
use Neelkanth\Laravel\Surveillance\Implementations\SurveillanceLogRepository;
class SurveillanceLogMongoDbRepository extends SurveillanceLogRepository
{
public function writeLog($dataToLog = null)
{
if (!is_null($dataToLog)) {
$this->setLogToWrite($dataToLog);
}
$log = $this->getLogToWrite();
if (!empty($log) && is_array($log)) {
$collection = (new \MongoDB\Client)->surveillance->logs;
$insertOneResult = $collection->insertOne($log);
}
}
}
/*
* This class is responsible for logging the surveillance enabled requests
* To override the default functionality extend the below class and provide its name here.
*/
"log-repository" => 'App\SurveillanceLogMongoDbRepository',