Skip to content

Instantly share code, notes, and snippets.

@jrockowitz
Created June 20, 2018 19:55
Show Gist options
  • Save jrockowitz/0e578d5c2e4156828265075c9c498ba8 to your computer and use it in GitHub Desktop.
Save jrockowitz/0e578d5c2e4156828265075c9c498ba8 to your computer and use it in GitHub Desktop.
/**
* Implements hook_requirements().
*/
function CUSTOM_requirements($phase) {
// Only check requirements during the run-time (aka Status Report).
if ($phase != 'runtime') {
return [];
}
$requirements = [];
/* PHP: Configuration */
// General.
$php_expected_config = [
'allow_url_fopen' => '1',
'date.timezone' => 'America/New_York',
'max_input_vars' => 2000,
];
// OPcache.
$php_expected_config += [
'opcache.memory_consumption' => ['512', '512M'],
'opcache.save_comments' => '1',
'opcache.enable_cli' => '0',
];
$php_config_errors = [];
foreach ($php_expected_config as $name => $expected_value) {
$actual_value = ini_get($name);
$found_value = (is_array($expected_value)) ? in_array($actual_value, $expected_value) : ($actual_value == $expected_value);
if (!$found_value) {
$t_args = [
'%name' => $name,
'%actual_value' => $actual_value,
'%expected_value' => (is_array($expected_value)) ? MskArray::toString($expected_value, 'or') : $expected_value,
];
$php_config_errors[] = [
'#prefix' => '<div>',
'#suffix' => '</div>',
'#markup' => t("%name is currently set to %actual_value and it must be set to %expected_value.", $t_args),
];
}
}
$requirements['custom_php_config'] = [
'title' => t('CUSTOM: PHP Configuration'),
'value' => $php_config_errors ? t('Incorrect') : t('Correct'),
];
if ($php_config_errors) {
$requirements['custom_php_config']['severity'] = REQUIREMENT_ERROR;
$requirements['custom_php_config']['description'] = $php_config_errors;
}
// PHP: HTML tidy.
$has_html_tidy = function_exists('tidy_get_output');
$requirements['custom_php_html_tidy'] = [
'title' => t('CUSTOM: PHP HTML tidy'),
'value' => $has_html_tidy ? t('Enabled') : t('Not found'),
];
if (!$has_html_tidy) {
$requirements['custom_php_html_tidy']['severity'] = REQUIREMENT_ERROR;
$requirements['custom_php_html_tidy']['description'] = t('<a href=":href">PECL Html tidy</a> is required to properly cleanup and convert migrated HTML to valid XHTML.', [':href' => 'http://php.net/manual/en/book.tidy.php']);
}
// PHP: LDAP.
$has_ldap = function_exists('ldap_connect');
$requirements['custom_php_ldap'] = [
'title' => t('CUSTOM: PHP LDAP'),
'value' => $has_ldap ? t('Enabled') : t('Not found'),
];
if (!$has_ldap) {
$requirements['custom_php_ldap']['severity'] = REQUIREMENT_ERROR;
$requirements['custom_php_ldap']['description'] = t('<a href=":href">Lightweight Directory Access Protocol</a> is required for user authenication.', [':href' => 'http://php.net/manual/en/book.ldap.php']);
}
// PHP: Memcache.
$has_memcache = class_exists('Memcache') || class_exists('Memcached');
$requirements['custom_php_memcache'] = [
'title' => t('CUSTOM: PHP Memcache'),
'value' => $has_memcache ? t('Enabled') : t('Not found'),
];
if (!$has_memcache) {
$requirements['custom_php_apc']['severity'] = REQUIREMENT_ERROR;
$requirements['custom_php_apc']['description'] = t('<a href=":href">PECL Memcache</a> is required', [':href' => 'http://php.net/manual/en/book.memcache.php']);
}
// PHP: Mcrypt.
$has_mcrypt = function_exists('mcrypt_encrypt');
$requirements['custom_php_mcrypt'] = [
'title' => t('CUSTOM: PHP Mcrypt'),
'value' => $has_mcrypt ? t('Enabled') : t('Not found'),
];
if (!$has_mcrypt) {
$requirements['custom_php_apc']['severity'] = REQUIREMENT_ERROR;
$requirements['custom_php_apc']['description'] = t('<a href=":href">PHP with Mcrypt</a> is required.', [':href' => 'http://php.net/manual/en/book.mcrypt.php']);
}
// PHP: OPcache.
$has_opcache = extension_loaded('Zend OPcache');
$requirements['custom_php_opcache'] = [
'title' => t('CUSTOM: PHP OPcache'),
'value' => $has_opcache ? t('Enabled') : t('Not found'),
];
if (!$has_opcache) {
$requirements['custom_php_opcache']['severity'] = REQUIREMENT_ERROR;
$requirements['custom_php_opcache']['description'] = t('<a href=":href">OPcache</a> is required for caching and optimizing PHP intermediate code.', [':href' => 'http://php.net/manual/en/book.opcache.php']);
}
// Command: Unzip.
$has_unzip = MskCommand::exists('unzip');
$requirements['custom_command_unzip'] = [
'title' => t('CUSTOM: Unzip command'),
'value' => $has_unzip ? t('Enabled') : t('Not found'),
];
if (!$has_unzip) {
$requirements['custom_command_unzip']['severity'] = REQUIREMENT_ERROR;
$requirements['custom_command_unzip']['description'] = t('The <a href=":href">unzip</a> command is required.', [':href' => 'http://linux.about.com/od/commands/l/blcmdl1_unzip.htm']);
}
// Command: Whtmltopdf.
$has_wkhtmltopdf = MskCommand::exists('wkhtmltopdf');
$requirements['custom_command_wkhtmltopdf'] = [
'title' => t('CUSTOM: Wkhtmltopdf command'),
'value' => $has_wkhtmltopdf ? t('Enabled') . ' - ' . MskCommand::version('wkhtmltopdf') : t('Not found'),
];
if (!$has_wkhtmltopdf) {
$requirements['custom_command_wkhtmltopdf']['severity'] = REQUIREMENT_ERROR;
$requirements['custom_command_wkhtmltopdf']['description'] = t('The <a href=":href">wkhtmltopdf</a> command is required to generate PDF documents from HTML.', [':href' => 'http://wkhtmltopdf.org']);
}
// MySQL: Config
// Note: Only checking key variables where if they are mis-configured it would have a sever impact on the application.
$mysql_expected_settings = [
'query_cache_type' => 'ON',
'query_cache_limit' => 33554432, // 32 MB
'query_cache_size' => 100663296, // 96 MB
'max_allowed_packet' => 16777216, // 16 MB
'storage_engine' => 'InnoDB',
];
$result = db_query('SHOW VARIABLES WHERE Variable_name IN (:names[])', [':names[]' => array_keys($mysql_expected_settings)]);
$mysql_config_errors = [];
while ($record = $result->fetchAssoc()) {
$name = $record['Variable_name'];
$actual_value = $record['Value'];
$expected_value = $mysql_expected_settings[$name];
if (is_numeric($expected_value)) {
if ($actual_value < $expected_value) {
$t_args = ['%name' => $name, '%value' => format_size($actual_value), '%value_recommendation' => format_size($expected_value)];
$mysql_config_errors[] = [
'#prefix' => '<div>',
'#suffix' => '</div>',
'#markup' => t("MySQL's %name is currently set to %value and it should be greater than or equal to %value_recommendation.", $t_args),
];
}
}
elseif ($actual_value != $expected_value) {
$t_args = ['%name' => $name, '%value' => $actual_value, '%default_value' => $expected_value];
$mysql_config_errors[] = [
'#prefix' => '<div>',
'#suffix' => '</div>',
'#markup' => t("MySQL's %name is currently set to %value and it must be set to %default_value.", $t_args),
];
}
}
$requirements['custom_mysql_config'] = [
'title' => t('CUSTOM: MySQL Configuration'),
'value' => $mysql_config_errors ? t('Incorrect') : t('Correct'),
];
if ($mysql_config_errors) {
$requirements['custom_mysql_config']['severity'] = REQUIREMENT_ERROR;
$requirements['custom_mysql_config']['description'] = $mysql_config_errors;
}
return $requirements;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment