1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace IonGhitun\MysqlEncryption; |
4
|
|
|
|
5
|
|
|
use Illuminate\Support\Facades\DB; |
6
|
|
|
use Illuminate\Support\Facades\Validator; |
7
|
|
|
use Illuminate\Support\ServiceProvider; |
8
|
|
|
|
9
|
|
|
/** |
10
|
|
|
* Class MysqlEncryptionServiceProvider |
11
|
|
|
* |
12
|
|
|
* @package IonGhitun\MysqlEncryption\Providers |
13
|
|
|
*/ |
14
|
|
|
class MysqlEncryptionServiceProvider extends ServiceProvider |
15
|
|
|
{ |
16
|
|
|
/** |
17
|
|
|
* Bootstrap the application services. |
18
|
|
|
*/ |
19
|
|
|
public function boot() |
20
|
|
|
{ |
21
|
|
|
$this->addValidators(); |
22
|
|
|
} |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* Add validators for unique and exists. |
26
|
|
|
*/ |
27
|
|
|
private function addValidators() |
28
|
|
|
{ |
29
|
|
|
/** |
30
|
|
|
* Validate unique binary encrypted |
31
|
|
|
*/ |
32
|
|
|
Validator::extend('unique_encrypted', function ($attribute, $value, array $parameters) { |
33
|
|
|
if (!isset($parameters[0])) { |
34
|
|
|
throw new \Exception('unique_encrypted requires at least one parameter'); |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
$field = isset($parameters[1]) ? $parameters[1] : $attribute; |
38
|
|
|
|
39
|
|
|
$items = DB::select("SELECT count(*) as aggregate FROM `" . $parameters[0] . "` WHERE AES_DECRYPT(`" . $field . "`, '" . env("ENCRYPTION_KEY") . "') LIKE '" . $value . "' COLLATE utf8mb4_general_ci"); |
40
|
|
|
|
41
|
|
|
return $items[0]->aggregate === 0; |
42
|
|
|
}); |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* Validate exists binary encrypted |
46
|
|
|
*/ |
47
|
|
|
Validator::extend('exists_encrypted', function ($attribute, $value, array $parameters) { |
48
|
|
|
if (!isset($parameters[0])) { |
49
|
|
|
throw new \Exception('exists_encrypted requires at least one parameter'); |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
$field = isset($parameters[1]) ? $parameters[1] : $attribute; |
53
|
|
|
|
54
|
|
|
$items = DB::select("SELECT count(*) as aggregate FROM `" . $parameters[0] . "` WHERE AES_DECRYPT(`" . $field . "`, '" . env("ENCRYPTION_KEY") . "') LIKE '" . $value . "' COLLATE utf8mb4_general_ci"); |
55
|
|
|
|
56
|
|
|
return $items[0]->aggregate > 0; |
57
|
|
|
}); |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* Register the application services. |
62
|
|
|
*/ |
63
|
|
|
public function register() |
64
|
|
|
{ |
65
|
|
|
} |
66
|
|
|
} |
67
|
|
|
|