Skip to content

Commit

Permalink
MDL-83268 core_h5p: Upgrade core lib to 1.27
Browse files Browse the repository at this point in the history
This commit is upgrading the joubel/core library to the one
tagged as moodle-1.27.0 (which also increases the coreApi minorVersion).
That's why the previous commit was duplicating the existing
h5plib_v126.
  • Loading branch information
sarjona committed Sep 25, 2024
1 parent 2d930e6 commit 750b716
Show file tree
Hide file tree
Showing 26 changed files with 916 additions and 181 deletions.
35 changes: 35 additions & 0 deletions h5p/h5plib/v127/joubel/core/composer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
{
"name": "h5p/h5p-core",
"type": "library",
"description": "H5P Core functionality in PHP",
"keywords": ["h5p","hvp","interactive","content","quiz"],
"homepage": "https://h5p.org",
"license": "GPL-3.0",
"authors": [
{
"name": "Svein-Tore Griff With",
"email": "[email protected]",
"homepage": "http://joubel.com",
"role": "CEO"
},
{
"name": "Frode Petterson",
"email": "[email protected]",
"homepage": "http://joubel.com",
"role": "Developer"
}
],
"require": {
"php": ">=7.0.0"
},
"autoload": {
"files": [
"h5p.classes.php",
"h5p-development.class.php",
"h5p-file-storage.interface.php",
"h5p-default-storage.class.php",
"h5p-event-base.class.php",
"h5p-metadata.class.php"
]
}
}
Binary file removed h5p/h5plib/v127/joubel/core/fonts/h5p-core-28.eot
Binary file not shown.
114 changes: 0 additions & 114 deletions h5p/h5plib/v127/joubel/core/fonts/h5p-core-28.svg

This file was deleted.

Binary file removed h5p/h5plib/v127/joubel/core/fonts/h5p-core-28.ttf
Binary file not shown.
Binary file removed h5p/h5plib/v127/joubel/core/fonts/h5p-core-28.woff
Binary file not shown.
Binary file added h5p/h5plib/v127/joubel/core/fonts/h5p-core-29.eot
Binary file not shown.
599 changes: 599 additions & 0 deletions h5p/h5plib/v127/joubel/core/fonts/h5p-core-29.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added h5p/h5plib/v127/joubel/core/fonts/h5p-core-29.ttf
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
123 changes: 123 additions & 0 deletions h5p/h5plib/v127/joubel/core/fonts/h5p-core-30.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added h5p/h5plib/v127/joubel/core/fonts/h5p-core-30.ttf
Binary file not shown.
Binary file not shown.
Binary file not shown.
2 changes: 1 addition & 1 deletion h5p/h5plib/v127/joubel/core/fonts/h5p-hub-publish.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
24 changes: 13 additions & 11 deletions h5p/h5plib/v127/joubel/core/h5p-default-storage.class.php
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
<?php

namespace Moodle;

/**
* File info?
*/
Expand All @@ -11,13 +9,13 @@
* operations using PHP's standard file operation functions.
*
* Some implementations of H5P that doesn't use the standard file system will
* want to create their own implementation of the H5P\FileStorage interface.
* want to create their own implementation of the \H5P\FileStorage interface.
*
* @package H5P
* @copyright 2016 Joubel AS
* @license MIT
*/
class H5PDefaultStorage implements H5PFileStorage {
class H5PDefaultStorage implements \H5PFileStorage {
private $path, $alteditorpath;

/**
Expand All @@ -41,10 +39,10 @@ function __construct($path, $alteditorpath = NULL) {
* Library properties
*/
public function saveLibrary($library) {
$dest = $this->path . '/libraries/' . H5PCore::libraryToFolderName($library);
$dest = $this->path . '/libraries/' . \H5PCore::libraryToFolderName($library);

// Make sure destination dir doesn't exist
H5PCore::deleteFileTree($dest);
\H5PCore::deleteFileTree($dest);

// Move library folder
self::copyFileTree($library['uploadDirectory'], $dest);
Expand All @@ -66,7 +64,7 @@ public function saveContent($source, $content) {
$dest = "{$this->path}/content/{$content['id']}";

// Remove any old content
H5PCore::deleteFileTree($dest);
\H5PCore::deleteFileTree($dest);

self::copyFileTree($source, $dest);
}
Expand All @@ -78,7 +76,7 @@ public function saveContent($source, $content) {
* Content properties
*/
public function deleteContent($content) {
H5PCore::deleteFileTree("{$this->path}/content/{$content['id']}");
\H5PCore::deleteFileTree("{$this->path}/content/{$content['id']}");
}

/**
Expand Down Expand Up @@ -139,10 +137,14 @@ public function exportContent($id, $target) {
* Folder that library resides in
*/
public function exportLibrary($library, $target, $developmentPath=NULL) {
$folder = H5PCore::libraryToFolderName($library);
$srcFolder = \H5PCore::libraryToFolderName($library);
$srcPath = ($developmentPath === NULL ? "/libraries/{$srcFolder}" : $developmentPath);

// Library folders inside the H5P zip file shall not contain patch version in the folder name
$library['patchVersionInFolderName'] = false;
$destinationFolder = \H5PCore::libraryToFolderName($library);

$srcPath = ($developmentPath === NULL ? "/libraries/{$folder}" : $developmentPath);
self::copyFileTree("{$this->path}{$srcPath}", "{$target}/{$folder}");
self::copyFileTree("{$this->path}{$srcPath}", "{$target}/{$destinationFolder}");
}

/**
Expand Down
2 changes: 0 additions & 2 deletions h5p/h5plib/v127/joubel/core/h5p-development.class.php
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
<?php

namespace Moodle;

/**
* This is a data layer which uses the file system so it isn't specific to any framework.
*/
Expand Down
2 changes: 0 additions & 2 deletions h5p/h5plib/v127/joubel/core/h5p-event-base.class.php
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
<?php

namespace Moodle;

/**
* The base class for H5P events. Extend to track H5P events in your system.
*
Expand Down
2 changes: 0 additions & 2 deletions h5p/h5plib/v127/joubel/core/h5p-file-storage.interface.php
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
<?php

namespace Moodle;

/**
* File info?
*/
Expand Down
3 changes: 0 additions & 3 deletions h5p/h5plib/v127/joubel/core/h5p-metadata.class.php
Original file line number Diff line number Diff line change
@@ -1,7 +1,4 @@
<?php

namespace Moodle;

/**
* Utility class for handling metadata
*/
Expand Down
108 changes: 76 additions & 32 deletions h5p/h5plib/v127/joubel/core/h5p.classes.php
Original file line number Diff line number Diff line change
@@ -1,9 +1,4 @@
<?php

namespace Moodle;

use ZipArchive;

/**
* Interface defining functions the h5p library needs the framework to implement
*/
Expand Down Expand Up @@ -1655,14 +1650,12 @@ private function saveLibraries() {
H5PMetadata::boolifyAndEncodeSettings($library['metadataSettings']) :
NULL;

// MOODLE PATCH: The library needs to be saved in database first before creating the files, because the libraryid is used
// as itemid for the files.
// Update our DB
$this->h5pF->saveLibraryData($library, $new);

// Save library folder
$this->h5pC->fs->saveLibrary($library);

// Update our DB
$this->h5pF->saveLibraryData($library, $new);

// Remove cached assets that uses this library
if ($this->h5pC->aggregateAssets && isset($library['libraryId'])) {
$removedKeys = $this->h5pF->deleteCachedAssets($library['libraryId']);
Expand Down Expand Up @@ -2079,13 +2072,14 @@ class H5PCore {

public static $coreApi = array(
'majorVersion' => 1,
'minorVersion' => 26
'minorVersion' => 27
);
public static $styles = array(
'styles/h5p.css',
'styles/h5p-confirmation-dialog.css',
'styles/h5p-core-button.css',
'styles/h5p-tooltip.css',
'styles/h5p-table.css',
);
public static $scripts = array(
'js/jquery.js',
Expand All @@ -2104,8 +2098,8 @@ class H5PCore {
'js/h5p-utils.js',
);

public static $defaultContentWhitelist = 'json png jpg jpeg gif bmp tif tiff svg eot ttf woff woff2 otf webm mp4 ogg mp3 m4a wav txt pdf rtf doc docx xls xlsx ppt pptx odt ods odp xml csv diff patch swf md textile vtt webvtt gltf glb';
public static $defaultLibraryWhitelistExtras = 'js css';
public static $defaultContentWhitelist = 'json png jpg jpeg gif bmp tif tiff eot ttf woff woff2 otf webm mp4 ogg mp3 m4a wav txt pdf rtf doc docx xls xlsx ppt pptx odt ods odp csv diff patch swf md textile vtt webvtt gltf glb';
public static $defaultLibraryWhitelistExtras = 'js css svg xml';

public $librariesJsonData, $contentJsonData, $mainJsonData, $h5pF, $fs, $h5pD, $disableFileCheck;
const SECONDS_IN_WEEK = 604800;
Expand Down Expand Up @@ -2155,15 +2149,15 @@ class H5PCore {
*
* @param H5PFrameworkInterface $H5PFramework
* The frameworks implementation of the H5PFrameworkInterface
* @param string|H5PFileStorage $path H5P file storage directory or class.
* @param string|\H5PFileStorage $path H5P file storage directory or class.
* @param string $url To file storage directory.
* @param string $language code. Defaults to english.
* @param boolean $export enabled?
*/
public function __construct(H5PFrameworkInterface $H5PFramework, $path, $url, $language = 'en', $export = FALSE) {
$this->h5pF = $H5PFramework;

$this->fs = ($path instanceof H5PFileStorage ? $path : new H5PDefaultStorage($path));
$this->fs = ($path instanceof \H5PFileStorage ? $path : new \H5PDefaultStorage($path));

$this->url = $url;
$this->exportEnabled = $export;
Expand Down Expand Up @@ -3330,22 +3324,21 @@ private static function getTimeFactor() {
* @return string
*/
private static function hashToken($action, $time_factor) {
global $SESSION;
if (!isset($SESSION->h5p_token)) {
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 @@ -3788,7 +3781,12 @@ public function getLocalization() {
'keywordsExits' => $this->h5pF->t('Keywords already exists!'),
'someKeywordsExits' => $this->h5pF->t('Some of these keywords already exist'),
'width' => $this->h5pF->t('width'),
'height' => $this->h5pF->t('height')
'height' => $this->h5pF->t('height'),
'rotateLeft' => $this->h5pF->t('Rotate Left'),
'rotateRight' => $this->h5pF->t('Rotate Right'),
'cropImage' => $this->h5pF->t('Crop Image'),
'confirmCrop' => $this->h5pF->t('Confirm Crop'),
'cancelCrop' => $this->h5pF->t('Cancel Crop')
);
}

Expand Down Expand Up @@ -3966,7 +3964,6 @@ public function hubAccountInfo() {
}

if (empty($siteUuid) || empty($secret)) {
$this->h5pF->setErrorMessage($this->h5pF->t('Missing Site UUID or Hub Secret. Please check your Hub registration.'));
return false;
}

Expand Down Expand Up @@ -4170,7 +4167,20 @@ class H5PContentValidator {
public $h5pF;
public $h5pC;
private $typeMap, $libraries, $dependencies, $nextWeight;
private static $allowed_styleable_tags = array('span', 'p', 'div','h1','h2','h3', 'td');
private static $allowed_styleable_tags = [
'span',
'p',
'div',
'h1',
'h2',
'h3',
'table',
'col',
'figure',
'td',
'th',
'li'
];

/** @var bool Allowed styles status. */
protected $allowedStyles;
Expand Down Expand Up @@ -4270,7 +4280,7 @@ public function validateText(&$text, $semantics) {

// Add related tags for table etc.
if (in_array('table', $tags)) {
$tags = array_merge($tags, array('tr', 'td', 'th', 'colgroup', 'thead', 'tbody', 'tfoot'));
$tags = array_merge($tags, array('tr', 'td', 'th', 'colgroup', 'col', 'thead', 'tbody', 'tfoot', 'figure', 'figcaption'));
}
if (in_array('b', $tags) && ! in_array('strong', $tags)) {
$tags[] = 'strong';
Expand All @@ -4293,13 +4303,13 @@ public function validateText(&$text, $semantics) {
$stylePatterns[] = '/^font-size: *[0-9.]+(em|px|%) *;?$/i';
}
if (isset($semantics->font->family) && $semantics->font->family) {
$stylePatterns[] = '/^font-family: *[-a-z0-9," ]+;?$/i';
$stylePatterns[] = '/^font-family: *[-a-z0-9,\'&; ]+;?$/i';
}
if (isset($semantics->font->color) && $semantics->font->color) {
$stylePatterns[] = '/^color: *(#[a-f0-9]{3}[a-f0-9]{3}?|rgba?\([0-9, ]+\)) *;?$/i';
$stylePatterns[] = '/^color: *(#[a-f0-9]{3}[a-f0-9]{3}?|rgba?\([0-9, ]+\)|hsla?\([0-9,.% ]+\)) *;?$/i';
}
if (isset($semantics->font->background) && $semantics->font->background) {
$stylePatterns[] = '/^background-color: *(#[a-f0-9]{3}[a-f0-9]{3}?|rgba?\([0-9, ]+\)) *;?$/i';
$stylePatterns[] = '/^background-color: *(#[a-f0-9]{3}[a-f0-9]{3}?|rgba?\([0-9, ]+\)|hsla?\([0-9,.% ]+\)) *;?$/i';
}
if (isset($semantics->font->spacing) && $semantics->font->spacing) {
$stylePatterns[] = '/^letter-spacing: *[0-9.]+(em|px|%) *;?$/i';
Expand All @@ -4309,6 +4319,28 @@ public function validateText(&$text, $semantics) {
}
}

// Allow styling of tables if they are allowed
if (isset($semantics->tags) && in_array('table', $semantics->tags)) {
// CKEditor outputs border as width style color
$stylePatterns[] = '/^border: *[0-9.]+(em|px|%|) *(none|solid|dotted|dashed|double|groove|ridge|inset|outset) *(#[a-f0-9]{3}[a-f0-9]{3}?|rgba?\([0-9, ]+\)|hsla?\([0-9,.% ]+\)) *;?$/i';
$stylePatterns[] = '/^border-style: *(none|solid|dotted|dashed|double|groove|ridge|inset|outset) *;?$/i';
$stylePatterns[] = '/^border-width: *[0-9.]+(em|px|%|) *;?$/i';
$stylePatterns[] = '/^border-color: *(#[a-f0-9]{3}[a-f0-9]{3}?|rgba?\([0-9, ]+\)|hsla?\([0-9,.% ]+\)) *;?$/i';

$stylePatterns[] = '/^vertical-align: *(middle|top|bottom);?$/i';
$stylePatterns[] = '/^padding: *[0-9.]+(em|px|%|) *;?$/i';
$stylePatterns[] = '/^width: *[0-9.]+(em|px|%|) *;?$/i';
$stylePatterns[] = '/^height: *[0-9.]+(em|px|%|) *;?$/i';
$stylePatterns[] = '/^float: *(right|left|none) *;?$/i';

// Needed for backwards compatibility
$stylePatterns[] = '/^border-collapse: *collapse *;?$/i';

// Table can have background color when font bgcolor is disabled
// Double entry of bgcolor in stylePatterns shouldn't matter
$stylePatterns[] = '/^background-color: *(#[a-f0-9]{3}[a-f0-9]{3}?|rgba?\([0-9, ]+\)|hsla?\([0-9,.% ]+\)) *;?$/i';
}

// Alignment is allowed for all wysiwyg texts
$stylePatterns[] = '/^text-align: *(center|left|right);?$/i';

Expand Down Expand Up @@ -5005,13 +5037,25 @@ private function _filter_xss_attributes($attr, $allowedStyles = FALSE) {
if (preg_match('/^"([^"]*)"(\s+|$)/', $attr, $match)) {
if ($allowedStyles && $attrName === 'style') {
// Allow certain styles

// Prevent font family from getting split wrong because of the ; in &quot;
if (str_contains($match[1], 'font-family')) {
$match[1] = str_replace('&quot;', "'", $match[1]);
}

$validatedStyles = [];
$styles = explode(';', $match[1]);

foreach ($allowedStyles as $pattern) {
if (preg_match($pattern, $match[1])) {
// All patterns are start to end patterns, and CKEditor adds one span per style
$attrArr[] = 'style="' . $match[1] . '"';
break;
foreach ($styles as $style) {
$style = trim($style);
if (preg_match($pattern, $style)) {
$validatedStyles[] = $style;
}
}
}

$attrArr[] = 'style="' . implode(';', $validatedStyles) . ';"';
break;
}

Expand Down
2 changes: 1 addition & 1 deletion h5p/h5plib/v127/joubel/core/images/h5p.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading

0 comments on commit 750b716

Please sign in to comment.