1
|
|
|
<?php |
2
|
|
|
/* |
3
|
|
|
* MikoPBX - free phone system for small business |
4
|
|
|
* Copyright © 2017-2023 Alexey Portnov and Nikolay Beketov |
5
|
|
|
* |
6
|
|
|
* This program is free software: you can redistribute it and/or modify |
7
|
|
|
* it under the terms of the GNU General Public License as published by |
8
|
|
|
* the Free Software Foundation; either version 3 of the License, or |
9
|
|
|
* (at your option) any later version. |
10
|
|
|
* |
11
|
|
|
* This program is distributed in the hope that it will be useful, |
12
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
13
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
14
|
|
|
* GNU General Public License for more details. |
15
|
|
|
* |
16
|
|
|
* You should have received a copy of the GNU General Public License along with this program. |
17
|
|
|
* If not, see <https://www.gnu.org/licenses/>. |
18
|
|
|
*/ |
19
|
|
|
|
20
|
|
|
declare(strict_types=1); |
21
|
|
|
|
22
|
|
|
|
23
|
|
|
namespace MikoPBX\AdminCabinet\Providers; |
24
|
|
|
|
25
|
|
|
|
26
|
|
|
use MikoPBX\Common\Models\PbxSettings; |
27
|
|
|
use MikoPBX\Common\Models\PbxSettingsConstants; |
28
|
|
|
use Phalcon\Crypt; |
29
|
|
|
use Phalcon\Di\DiInterface; |
30
|
|
|
use Phalcon\Di\ServiceProviderInterface; |
31
|
|
|
use Phalcon\Security\Random; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* Initializes Crypt provider |
35
|
|
|
* |
36
|
|
|
* @package MikoPBX\AdminCabinet\Providers |
37
|
|
|
*/ |
38
|
|
|
class CryptProvider implements ServiceProviderInterface |
39
|
|
|
{ |
40
|
|
|
public const SERVICE_NAME = 'crypt'; |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* Register elements service provider |
44
|
|
|
* |
45
|
|
|
* @param DiInterface $di The DI container. |
46
|
|
|
*/ |
47
|
|
|
public function register(DiInterface $di): void |
48
|
|
|
{ |
49
|
|
|
$di->setShared( |
50
|
|
|
self::SERVICE_NAME, |
51
|
|
|
function () { |
52
|
|
|
$encryptionKey = self::getEncryptionKey(); |
53
|
|
|
$crypt = new Crypt(); |
54
|
|
|
// Set a global encryption key |
55
|
|
|
$crypt->setKey( |
56
|
|
|
$encryptionKey |
57
|
|
|
); |
58
|
|
|
return $crypt; |
59
|
|
|
} |
60
|
|
|
); |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* Retrieve the encryption key from settings. |
65
|
|
|
* Generate a new one if it doesn't exist. |
66
|
|
|
* |
67
|
|
|
* @return string The encryption key. |
68
|
|
|
*/ |
69
|
|
|
private static function getEncryptionKey():string |
70
|
|
|
{ |
71
|
|
|
$encryptionKey = PbxSettings::getValueByKey(PbxSettingsConstants::WWW_ENCRYPTION_KEY); |
72
|
|
|
if (empty($encryptionKey)){ |
73
|
|
|
$record = PbxSettings::findFirstByKey(PbxSettingsConstants::WWW_ENCRYPTION_KEY); |
74
|
|
|
if ($record===null){ |
75
|
|
|
$random = new Random(); |
76
|
|
|
try { |
77
|
|
|
// Try generating a new encryption key. |
78
|
|
|
$encryptionKey = $random->base64Safe(16); |
79
|
|
|
} catch (\Throwable $e) { |
80
|
|
|
// If something goes wrong, fall back to a default encryption key. |
81
|
|
|
$encryptionKey = md5(microtime()); |
82
|
|
|
} |
83
|
|
|
$record = new PbxSettings(); |
84
|
|
|
$record->key = PbxSettingsConstants::WWW_ENCRYPTION_KEY; |
85
|
|
|
$record->value = $encryptionKey; |
86
|
|
|
$record->save(); |
87
|
|
|
} |
88
|
|
|
} |
89
|
|
|
return $encryptionKey; |
90
|
|
|
} |
91
|
|
|
} |