Skip to content

Commit d4ca1f7

Browse files
authored
feat: check for openssl with all required functions (#389)
1 parent fd2d54a commit d4ca1f7

File tree

4 files changed

+74
-13
lines changed

4 files changed

+74
-13
lines changed

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,10 @@ As it is standardized, you don't have to worry about what server type it relies
1010
## Requirements
1111

1212
PHP 8.1+ and the following extensions:
13-
* gmp (optional but better for performance)
13+
* bcmath and/or gmp (optional but better for performance)
1414
* mbstring
1515
* curl
16-
* openssl
16+
* openssl (with elliptic curve support)
1717

1818
There is no support and maintenance for older PHP versions, however you are free to use the following compatible versions:
1919
- PHP 5.6 or HHVM: `v1.x`

composer.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
"spomky-labs/base64url": "^2.0.4"
3636
},
3737
"suggest": {
38+
"ext-bcmath": "Optional for performance.",
3839
"ext-gmp": "Optional for performance."
3940
},
4041
"require-dev": {

src/Utils.php

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,4 +63,74 @@ public static function unserializePublicKey(string $data): array
6363
hex2bin(mb_substr($data, $dataLength / 2, null, '8bit')),
6464
];
6565
}
66+
67+
/**
68+
* Generates user warning/notice if some requirements are not met.
69+
* Does not throw exception to allow unusual or polyfill environments.
70+
*/
71+
public static function checkRequirement(): void
72+
{
73+
self::checkRequirementExtension();
74+
self::checkRequirementKeyCipherHash();
75+
}
76+
77+
public static function checkRequirementExtension(): void
78+
{
79+
$requiredExtensions = [
80+
'curl' => '[WebPush] curl extension is not loaded but is required. You can fix this in your php.ini.',
81+
'mbstring' => '[WebPush] mbstring extension is not loaded but is required for sending push notifications with payload or for VAPID authentication. You can fix this in your php.ini.',
82+
'openssl' => '[WebPush] openssl extension is not loaded but is required for sending push notifications with payload or for VAPID authentication. You can fix this in your php.ini.',
83+
];
84+
foreach($requiredExtensions as $extension => $message) {
85+
if(!extension_loaded($extension)) {
86+
trigger_error($message, E_USER_WARNING);
87+
}
88+
}
89+
90+
// Check optional extensions.
91+
if(!extension_loaded("bcmath") && !extension_loaded("gmp")) {
92+
trigger_error("It is highly recommended to install the GMP or BCMath extension to speed up calculations. The fastest available calculator implementation will be automatically selected at runtime.", E_USER_NOTICE);
93+
}
94+
}
95+
96+
public static function checkRequirementKeyCipherHash(): void
97+
{
98+
// Print your current openssl version with: OPENSSL_VERSION_TEXT
99+
// Check for outdated openssl without EC support.
100+
$requiredCurves = [
101+
'prime256v1' => '[WebPush] Openssl does not support required curve prime256v1.',
102+
];
103+
$availableCurves = openssl_get_curve_names();
104+
if($availableCurves === false) {
105+
trigger_error('[WebPush] Openssl does not support curves.', E_USER_WARNING);
106+
} else {
107+
foreach($requiredCurves as $curve => $message) {
108+
if(!in_array($curve, $availableCurves, true)) {
109+
trigger_error($message, E_USER_WARNING);
110+
}
111+
}
112+
}
113+
114+
// Check for unusual openssl without cipher support.
115+
$requiredCiphers = [
116+
'aes-128-gcm' => '[WebPush] Openssl does not support required cipher aes-128-gcm.',
117+
];
118+
$availableCiphers = openssl_get_cipher_methods();
119+
foreach($requiredCiphers as $cipher => $message) {
120+
if(!in_array($cipher, $availableCiphers, true)) {
121+
trigger_error($message, E_USER_WARNING);
122+
}
123+
}
124+
125+
// Check for unusual php without hash algo support.
126+
$requiredHash = [
127+
'sha256' => '[WebPush] Php does not support required hmac hash sha256.',
128+
];
129+
$availableHash = hash_hmac_algos();
130+
foreach($requiredHash as $hash => $message) {
131+
if(!in_array($hash, $availableHash, true)) {
132+
trigger_error($message, E_USER_WARNING);
133+
}
134+
}
135+
}
66136
}

src/WebPush.php

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -60,17 +60,7 @@ class WebPush
6060
*/
6161
public function __construct(array $auth = [], array $defaultOptions = [], ?int $timeout = 30, array $clientOptions = [])
6262
{
63-
$extensions = [
64-
'curl' => '[WebPush] curl extension is not loaded but is required. You can fix this in your php.ini.',
65-
'mbstring' => '[WebPush] mbstring extension is not loaded but is required for sending push notifications with payload or for VAPID authentication. You can fix this in your php.ini.',
66-
'openssl' => '[WebPush] openssl extension is not loaded but is required for sending push notifications with payload or for VAPID authentication. You can fix this in your php.ini.',
67-
];
68-
69-
foreach ($extensions as $extension => $message) {
70-
if (!extension_loaded($extension)) {
71-
trigger_error($message, E_USER_WARNING);
72-
}
73-
}
63+
Utils::checkRequirement();
7464

7565
if (isset($auth['VAPID'])) {
7666
$auth['VAPID'] = VAPID::validate($auth['VAPID']);

0 commit comments

Comments
 (0)