Added confirmation dialog functionality when disabling hub.

Refactored server setup check into check and var updates.
Moved bytes string parsing out of server setup check and fixed naming convention.
Fixed indentation to match library core instead of Moodle style.
pull/38/head
thomasmars 2017-02-21 17:37:01 +01:00
parent c70f8521bb
commit c4affb23dd
2 changed files with 155 additions and 72 deletions

View File

@ -2759,82 +2759,99 @@ class H5PCore {
$token === substr(hash('md5', $action . ($time_factor - 1) . $_SESSION['h5p_token']), -16, 13); // Between 12-24 hours
}
/**
* Check that all H5P requirements for the server setup is met.
*/
/**
* Check if the current server setup is valid and set error messages
*
* @return bool True if errors was found
*/
public function checkSetupErrorMessage() {
$disable_hub = FALSE;
if (!class_exists('ZipArchive')) {
$this->h5pF->setErrorMessage('Your PHP version does not support ZipArchive.');
$disable_hub = TRUE;
}
if (!extension_loaded('mbstring')) {
$this->h5pF->setErrorMessage(
$this->h5pF->t('The mbstring PHP extension is not loaded. H5P need this to function properly')
);
$disable_hub = TRUE;
}
// Check php version >= 5.2
$php_version = explode('.', phpversion());
if ($php_version[0] < 5 || ($php_version[0] === 5 && $php_version[1] < 2)) {
$this->h5pF->setErrorMessage(
$this->h5pF->t('Your PHP version is too old. H5P needs at least version 5.2 to function properly')
);
$disable_hub = TRUE;
}
$max_upload_size = ini_get('upload_max_filesize');
$max_post_size = ini_get('post_max_size');
$byte_threshold = 5000000; // 5MB
if (self::returnBytes($max_upload_size) < $byte_threshold) {
$this->h5pF->setErrorMessage(
$this->h5pF->t('Your PHP max upload size option is too small. You should consider to increase it to more than 5MB.')
);
}
if (self::returnBytes($max_post_size) < $byte_threshold) {
$this->h5pF->setErrorMessage(
$this->h5pF->t('Your PHP max post size option is too small. You should consider to increase it to more than 5MB.')
);
}
// Check SSL
if (!empty($_SERVER['HTTPS']) && $_SERVER['HTTPS'] != 'off') {
$this->h5pF->setErrorMessage(
$this->h5pF->t('Your server does not have SSL enabled. SSL should be enabled to ensure a secure connection with the H5P hub.')
);
$disable_hub = TRUE;
}
return $disable_hub;
}
/**
* Check that all H5P requirements for the server setup is met.
*/
public function checkSetupForRequirements() {
$disable_hub = FALSE;
$disable_hub = $this->checkSetupErrorMessage();
if (!class_exists('ZipArchive')) {
$this->h5pF->setErrorMessage('Your PHP version does not support ZipArchive.');
$disable_hub = TRUE;
}
// Disable hub, and inform how to re-enable it
$this->h5pF->setOption('disable_hub', $disable_hub);
if ($disable_hub) {
$this->h5pF->setErrorMessage(
$this->h5pF->t('H5P hub communication has been disabled because one or more H5P requirements failed.')
);
$this->h5pF->setErrorMessage(
$this->h5pF->t('When you have revised your server setup you may re-enable H5P hub communication in H5P Settings.')
);
}
}
if (!extension_loaded('mbstring')) {
$this->h5pF->setErrorMessage(
$this->h5pF->t('The mbstring PHP extension is not loaded. H5P need this to function properly')
);
$disable_hub = TRUE;
}
/**
* Return bytes from php_ini string value
*
* @param string $val
*
* @return int|string
*/
public static function returnBytes($val) {
$val = trim($val);
$last = strtolower($val[strlen($val) - 1]);
switch ($last) {
case 'g':
$val *= 1024;
case 'm':
$val *= 1024;
case 'k':
$val *= 1024;
}
// Check php version >= 5.2
$php_version = explode('.', phpversion());
if ($php_version[0] < 5 || ($php_version[0] === 5 && $php_version[1] < 2)) {
$this->h5pF->setErrorMessage(
$this->h5pF->t('Your PHP version is too old. H5P needs at least version 5.2 to function properly')
);
$disable_hub = TRUE;
}
// Check max upload and post size
function return_bytes($val) {
$val = trim($val);
$last = strtolower($val[strlen($val)-1]);
switch($last) {
case 'g':
$val *= 1024;
case 'm':
$val *= 1024;
case 'k':
$val *= 1024;
}
return $val;
}
$max_upload_size = ini_get('upload_max_filesize');
$max_post_size = ini_get('post_max_size');
$byte_threshold = 5000000; // 5MB
if (return_bytes($max_upload_size) < $byte_threshold) {
$this->h5pF->setErrorMessage(
$this->h5pF->t('Your PHP max upload size option is too small. You should consider to increase it to more than 5MB.')
);
}
if (return_bytes($max_post_size) < $byte_threshold) {
$this->h5pF->setErrorMessage(
$this->h5pF->t('Your PHP max post size option is too small. You should consider to increase it to more than 5MB.')
);
}
// Check SSL
if (!empty($_SERVER['HTTPS']) && $_SERVER['HTTPS'] != 'off') {
$this->h5pF->setErrorMessage(
$this->h5pF->t('Your server does not have SSL enabled. SSL should be enabled to ensure a secure connection with the H5P hub.')
);
$disable_hub = TRUE;
}
// Disable hub, and inform how to re-enable it
$this->h5pF->setOption('disable_hub', $disable_hub);
if ($disable_hub) {
$this->h5pF->setErrorMessage(
$this->h5pF->t('H5P hub communication has been disabled because one or more H5P requirements failed.')
);
$this->h5pF->setErrorMessage(
$this->h5pF->t('When you have revised your server setup you may re-enable H5P hub communication in H5P Settings.')
);
}
return $val;
}
}

View File

@ -0,0 +1,66 @@
/* global H5PDisableHubData */
/**
* Global data for disable hub functionality
*
* @typedef {object} H5PDisableHubData Data passed in from the backend
*
* @property {string} selector Selector for the disable hub check-button
* @property {string} overlaySelector Selector for the element that the confirmation dialog will mask
* @property {Array} errors Errors found with the current server setup
*
* @property {string} header Header of the confirmation dialog
* @property {string} confirmationDialogMsg Body of the confirmation dialog
* @property {string} cancelLabel Cancel label of the confirmation dialog
* @property {string} confirmLabel Confirm button label of the confirmation dialog
*
*/
/**
* Utility that makes it possible to force the user to confirm that he really
* wants to use the H5P hub without proper server settings.
*/
(function ($) {
$(document).on('ready', function () {
// No data found
if (!H5PDisableHubData) {
return;
}
// No errors found, no need for confirmation dialog
if (!H5PDisableHubData.errors || !H5PDisableHubData.errors.length) {
return;
}
var selector = H5PDisableHubData.selector;
var dialogHtml = '<div>' +
'<p>' + H5PDisableHubData.errors.join('</p><p>') + '</p>' +
'<p>' + H5PDisableHubData.confirmationDialogMsg + '</p>';
// Create confirmation dialog, make sure to include translations
var confirmationDialog = new H5P.ConfirmationDialog({
headerText: H5PDisableHubData.header,
dialogText: dialogHtml,
cancelText: H5PDisableHubData.cancelLabel,
confirmText: H5PDisableHubData.confirmLabel
}).appendTo($(H5PDisableHubData.overlaySelector).get(0));
confirmationDialog.on('confirmed', function () {
disableButton.get(0).checked = false;
});
confirmationDialog.on('canceled', function () {
disableButton.get(0).checked = true;
});
var disableButton = $(selector);
disableButton.change(function () {
if (!$(this).is(':checked')) {
confirmationDialog.show(disableButton.offset().top);
}
});
});
})(H5P.jQuery);