function pwa_webpush_requirements in Progressive Web App 7.2
Implements hook_requirements().
File
- modules/
pwa_webpush/ pwa_webpush.install, line 9
Code
function pwa_webpush_requirements($phase) {
$requirements = [];
$t = get_t();
// Include composer dependencies here to make it available everywhere.
if (!class_exists('\\Minishlink\\WebPush\\WebPush')) {
$folders = [
// The vendor repository will be at the PWA module root.
__DIR__ . '/../..',
DRUPAL_ROOT,
DRUPAL_ROOT . '/..',
];
foreach ($folders as $folder) {
if (file_exists($folder . '/vendor/autoload.php')) {
require $folder . '/vendor/autoload.php';
break;
}
}
}
// Make sure the PHP library is available to be able to use the module.
$php_library = class_exists('\\Minishlink\\WebPush\\WebPush');
if (!$php_library) {
$requirements['pwa_webpush_library'] = [
'title' => $t('WebPush PHP library'),
'severity' => REQUIREMENT_WARNING,
'description' => $t('PWA WebPush requires the <a href="@library_url">minishlink/web-push</a> library to be installed. Users can register for push notification but it is not possible to send push notifications from the website.', [
'@library_url' => 'https://github.com/web-push-libs/web-push-php',
]),
];
}
if ($phase !== 'runtime') {
return $requirements;
}
if ($php_library) {
$version = FALSE;
// Try to find the version of the library by looking into the
// 'installed.json' file create by composer.
if (class_exists('\\Composer\\Autoload\\ClassLoader')) {
$reflector = new \ReflectionClass('\\Composer\\Autoload\\ClassLoader');
$composer_path = pathinfo($reflector
->getFileName(), PATHINFO_DIRNAME);
$installed = drupal_json_decode(file_get_contents($composer_path . '/installed.json'));
foreach ($installed as $package) {
if ($package['name'] === 'minishlink/web-push') {
$version = $package['version'];
break;
}
}
}
$requirements['pwa_webpush_library'] = [
'title' => $t('WebPush PHP library'),
'value' => $version,
'severity' => REQUIREMENT_OK,
];
}
$requirements['pwa_webpush_vapid_key'] = [
'title' => $t('WebPush VAPID Private Key'),
];
// Make sure that the keys are not stored in the database.
$result = db_query("SELECT value FROM {variable} WHERE name = 'pwa_webpush_vapid_private'")
->fetchField();
if ($result) {
// Check that the value in the Database is not overwritten in the settings.
$db_key = unserialize($result);
$key_is_in_db = variable_get('pwa_webpush_vapid_private') === $db_key;
if ($key_is_in_db) {
$requirements['pwa_webpush_vapid_key'] += [
'severity' => REQUIREMENT_ERROR,
'description' => $t('The private key is stored in the database. Move the private key in the settings file and remove the corresponding row in the variable table.'),
];
}
else {
$requirements['pwa_webpush_vapid_key'] += [
'severity' => REQUIREMENT_WARNING,
'description' => $t('An outdated private key is stored in the database, delete the <code>pwa_webpush_vapid_private</code> row from the <code>variable</code> table.'),
];
}
}
else {
$requirements['pwa_webpush_vapid_key'] += [
'severity' => REQUIREMENT_OK,
'description' => $t('The VAPID private key is not stored in the DB. Make sure the key is not saved alongside the source code.'),
];
}
return $requirements;
}