Skip to content

Commit

Permalink
MDL-71408 lib: Add required changes after lib upgrade
Browse files Browse the repository at this point in the history
- The pull-request for fixing some minor PHP7.4 problems has been
integrated so this patch is not required
  • Loading branch information
sarjona authored and vmdef committed Apr 30, 2021
1 parent 45cd4e3 commit cd89f55
Show file tree
Hide file tree
Showing 6 changed files with 51 additions and 32 deletions.
2 changes: 1 addition & 1 deletion lib/h5p/h5p-metadata.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ public static function toDBArray($metadata, $include_title = true, $include_miss
switch ($config['type']) {
case 'text':
if ($value !== null && strlen($value) > $config['maxLength']) {
$value = mb_substr($value, 0, $config['maxLength']);
$value = \core_text::substr($value, 0, $config['maxLength']);
}
$types[] = '%s';
break;
Expand Down
44 changes: 33 additions & 11 deletions lib/h5p/h5p.classes.php
Original file line number Diff line number Diff line change
Expand Up @@ -758,11 +758,14 @@ public function isValidPackage($skipContent = FALSE, $upgradeOnly = FALSE) {
unlink($tmpPath);
return FALSE;
}
// Moodle: the extension mbstring is optional.
/*
if (!extension_loaded('mbstring')) {
$this->h5pF->setErrorMessage($this->h5pF->t('The mbstring PHP extension is not loaded. H5P need this to function properly'), 'mbstring-unsupported');
unlink($tmpPath);
return FALSE;
}
*/

// Create a temporary dir to extract package in.
$tmpDir = $this->h5pF->getUploadedH5pFolderPath();
Expand Down Expand Up @@ -809,7 +812,7 @@ public function isValidPackage($skipContent = FALSE, $upgradeOnly = FALSE) {
}
$totalSize += $fileStat['size'];

$fileName = mb_strtolower($fileStat['name']);
$fileName = \core_text::strtolower($fileStat['name']);
if (preg_match('/(^[\._]|\/[\._])/', $fileName) !== 0) {
continue; // Skip any file or folder starting with a . or _
}
Expand Down Expand Up @@ -2432,7 +2435,7 @@ public function getDependenciesFiles($dependencies, $prefix = '') {
// Using content dependencies
foreach ($dependencies as $dependency) {
if (isset($dependency['path']) === FALSE) {
$dependency['path'] = 'libraries/' . H5PCore::libraryToString($dependency, TRUE);
$dependency['path'] = $this->getDependencyPath($dependency);
$dependency['preloadedJs'] = explode(',', $dependency['preloadedJs']);
$dependency['preloadedCss'] = explode(',', $dependency['preloadedCss']);
}
Expand All @@ -2452,6 +2455,16 @@ public function getDependenciesFiles($dependencies, $prefix = '') {
return $files;
}

/**
* Get the path to the dependency.
*
* @param stdClass $dependency
* @return string
*/
protected function getDependencyPath(array $dependency): string {
return H5PCore::libraryToString($dependency, TRUE);
}

private static function getDependenciesHash(&$dependencies) {
// Build hash of dependencies
$toHash = array();
Expand Down Expand Up @@ -3202,21 +3215,23 @@ private static function getTimeFactor() {
* @return string
*/
private static function hashToken($action, $time_factor) {
if (!isset($_SESSION['h5p_token'])) {
global $SESSION;

if (!isset($SESSION->h5p_token)) {
// Create an unique key which is used to create action tokens for this session.
if (function_exists('random_bytes')) {
$_SESSION['h5p_token'] = base64_encode(random_bytes(15));
$SESSION->h5p_token = base64_encode(random_bytes(15));
}
else if (function_exists('openssl_random_pseudo_bytes')) {
$_SESSION['h5p_token'] = base64_encode(openssl_random_pseudo_bytes(15));
$SESSION->h5p_token = base64_encode(openssl_random_pseudo_bytes(15));
}
else {
$_SESSION['h5p_token'] = uniqid('', TRUE);
$SESSION->h5p_token = uniqid('', TRUE);
}
}

// Create hash and return
return substr(hash('md5', $action . $time_factor . $_SESSION['h5p_token']), -16, 13);
return substr(hash('md5', $action . $time_factor . $SESSION->h5p_token), -16, 13);
}

/**
Expand Down Expand Up @@ -3303,12 +3318,15 @@ public function checkSetupErrorMessage() {
$setup->disable_hub = TRUE;
}

// Moodle: the extension mbstring is optional.
/*
if (!extension_loaded('mbstring')) {
$setup->errors[] = $this->h5pF->t(
'The mbstring PHP extension is not loaded. H5P needs this to function properly'
);
$setup->disable_hub = TRUE;
}
*/

// Check php version >= 5.2
$php_version = explode('.', phpversion());
Expand Down Expand Up @@ -3656,12 +3674,13 @@ public function validateText(&$text, $semantics) {

// Check if string is within allowed length
if (isset($semantics->maxLength)) {
// Moodle: the extension mbstring is optional.
/*
if (!extension_loaded('mbstring')) {
$this->h5pF->setErrorMessage($this->h5pF->t('The mbstring PHP extension is not loaded. H5P need this to function properly'), 'mbstring-unsupported');
}
else {
$text = mb_substr($text, 0, $semantics->maxLength);
}
*/
$text = \core_text::substr($text, 0, $semantics->maxLength);
}

// Check if string is according to optional regexp in semantics
Expand Down Expand Up @@ -3711,11 +3730,14 @@ public function validateContentFiles($contentPath, $isLibrary = FALSE) {
// file name, 2. testing against a returned error array that could
// never be more than 1 element long anyway, 3. recreating the regex
// for every file.
// Moodle: the extension mbstring is optional.
/*
if (!extension_loaded('mbstring')) {
$this->h5pF->setErrorMessage($this->h5pF->t('The mbstring PHP extension is not loaded. H5P need this to function properly'), 'mbstring-unsupported');
$valid = FALSE;
}
else if (!preg_match($wl_regex, mb_strtolower($file))) {
*/
if (!preg_match($wl_regex, \core_text::strtolower($file))) {
$this->h5pF->setErrorMessage($this->h5pF->t('File "%filename" not allowed. Only files with the following extensions are allowed: %files-allowed.', array('%filename' => $file, '%files-allowed' => $whitelist)), 'not-in-whitelist');
$valid = FALSE;
}
Expand Down
11 changes: 2 additions & 9 deletions lib/h5p/js/jquery.js

Large diffs are not rendered by default.

21 changes: 11 additions & 10 deletions lib/h5p/readme_moodle.txt
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ Removed:
Added:
* readme_moodle.txt

Downloaded version: 1.24 release
Downloaded version: 1.24.2 release

Changes:
1. In order to allow the dependency path to be overridden by child H5PCore classes, a couple of minor changes have been added to the
Expand All @@ -41,23 +41,25 @@ Hopefully, when upgrading, these patch won't be needed because it will be includ
2.2. Change all the mb_uses straight to the core_text() alternatives. Version 1.24 has 3 ocurrences in h5p.classes.php
and 1 ocurrence in h5p-metadata.class.php.

3. Another PR has been sent to H5P library (https://github.com/h5p/h5p-php-library/pull/69) to fix some php74 minor problems. The same fix is being applied locally by MDL-67077. Once we import a new version, if it includes de fix, this won't be needed to reapply and can be removed.
These points (2.1 and 2.2) won't be needed once the mbstring extension becomes mandatory in Moodle. A request was
sent to MDL-65809 and it's required from Moodle 3.9 onwards.


3. Replace the $_SESSION references to $SESSION. That implies that the information is saved to backends, so only the Moodle one
should be used by core (core should be free from $_SESSION and always use $SESSION).

4. Replace the $_SESSION references to $SESSION. That implies that the information is saved to backends, so only the Moodle one should be used by core (core should be free from $_SESSION and always use $SESSION).
h5p.classes.php file:
- Into hashToken method:
Declare the global $SESSION.
Change all the $_SESSION by $SESSION.
A script for testing this part can be found in MDL-68068

The point 2 from above won't be needed once the mbstring extension becomes mandatory in Moodle. A request has been
sent to MDL-65809.

5. Upgrade and patch JQuery library.
4. Upgrade and patch JQuery library.
Once https://github.com/h5p/h5p-php-library/issues/83 gets integrated in
H5P PHP Library (upgrading the JQuery version), this change won't be needed.

5.1. Prepare the patched JQuery 1.12.4 library following these steps:
4.1. Prepare the patched JQuery 1.12.4 library following these steps:
a) Download the uncompressed JQuery Core 1.12.4 from https://code.jquery.com/jquery-1.12.4.js
b) Add the patch in https://snyk.io/vuln/SNYK-JS-JQUERY-174006 to the downloaded file.
You'll need to replace this code (line 212):
Expand All @@ -75,15 +77,14 @@ H5P PHP Library (upgrading the JQuery version), this change won't be needed.
}
c) Minify the patched jquery-1-12.4.js.

5.2. Edit h5p/h5plib/v124/joubel/core/js/jquery.js and replace the JQuery piece of code
4.2. Edit h5p/h5plib/v124/joubel/core/js/jquery.js and replace the JQuery piece of code
(at the beginning of the file, above the comment "// Snap this specific version of jQuery into H5P. jQuery.noConflict will")
with the previous patched and minified JQuery version.

5.3. Remove the following comment in h5p/h5plib/v124/joubel/core/js/jquery.js:
4.3. Remove the following comment in h5p/h5plib/v124/joubel/core/js/jquery.js:

/**
* jQuery v1.9.1
*
* @member
*/

2 changes: 1 addition & 1 deletion lib/thirdpartylibs.xml
Original file line number Diff line number Diff line change
Expand Up @@ -324,7 +324,7 @@
<location>h5p</location>
<name>h5p-php-library</name>
<license>GPL-3.0</license>
<version>1.24</version>
<version>1.24.2</version>
</library>
<library>
<location>mdn-polyfills</location>
Expand Down
3 changes: 3 additions & 0 deletions lib/upgrade.txt
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
This files describes API changes in core libraries and APIs,
information provided here is intended especially for developers.

=== 3.8.9 ===
* The third-party library h5p/h5plib/v124/core has been updated to version 1.24.2.

=== 3.8.6 ===
* New DML function $DB->delete_records_subquery() to delete records based on a subquery in a way
that will work across databases.
Expand Down

0 comments on commit cd89f55

Please sign in to comment.