forked from h5p/h5p-editor-php-library
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathh5peditor.class.php
More file actions
424 lines (380 loc) · 13.9 KB
/
h5peditor.class.php
File metadata and controls
424 lines (380 loc) · 13.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
<?php
class H5peditor {
public static $styles = array(
'libs/darkroom.css',
'styles/css/application.css',
'styles/css/h5peditor-image.css',
'styles/css/h5peditor-image-popup.css'
);
public static $scripts = array(
'scripts/h5peditor.js',
'scripts/h5peditor-semantic-structure.js',
'scripts/h5peditor-editor.js',
'scripts/h5peditor-library-selector.js',
'scripts/h5peditor-form.js',
'scripts/h5peditor-text.js',
'scripts/h5peditor-html.js',
'scripts/h5peditor-number.js',
'scripts/h5peditor-textarea.js',
'scripts/h5peditor-file.js',
'scripts/h5peditor-image.js',
'scripts/h5peditor-image-popup.js',
'scripts/h5peditor-av.js',
'scripts/h5peditor-group.js',
'scripts/h5peditor-boolean.js',
'scripts/h5peditor-list.js',
'scripts/h5peditor-list-editor.js',
'scripts/h5peditor-library.js',
'scripts/h5peditor-library-list-cache.js',
'scripts/h5peditor-select.js',
'scripts/h5peditor-dimensions.js',
'scripts/h5peditor-coordinates.js',
'scripts/h5peditor-none.js',
'ckeditor/ckeditor.js',
);
private $h5p, $storage, $files_directory, $basePath, $relativePathRegExp;
/**
* Constructor for the core editor library.
*
* @param \H5PCore $h5p Instance of core.
* @param mixed $storage Instance of h5peditor storage.
* @param string $basePath Url path to prefix assets with.
* @param string $filesDir H5P files directory.
* @param string $editorFilesDir Optional custom editor files directory outside h5p files directory.
*/
function __construct($h5p, $storage, $basePath, $filesDir, $editorFilesDir = NULL, $relativePathRegExp = '/^(\.\.\/){1,2}(\d+|editor)\/(.+)$/') {
$this->h5p = $h5p;
$this->storage = $storage;
$this->basePath = $basePath;
$this->contentFilesDir = $filesDir . DIRECTORY_SEPARATOR . 'content';
$this->editorFilesDir = ($editorFilesDir === NULL ? $filesDir . DIRECTORY_SEPARATOR . 'editor' : $editorFilesDir);
$this->relativePathRegExp = $relativePathRegExp;
}
/**
* Get list of libraries.
*
* @return array
*/
public function getLibraries() {
if (isset($_POST['libraries'])) {
// Get details for the specified libraries.
$libraries = array();
foreach ($_POST['libraries'] as $libraryName) {
$matches = array();
preg_match_all('/(.+)\s(\d)+\.(\d)$/', $libraryName, $matches);
if ($matches) {
$libraries[] = (object) array(
'uberName' => $libraryName,
'name' => $matches[1][0],
'majorVersion' => $matches[2][0],
'minorVersion' => $matches[3][0]
);
}
}
}
$libraries = $this->storage->getLibraries(!isset($libraries) ? NULL : $libraries);
if ($this->h5p->development_mode & H5PDevelopment::MODE_LIBRARY) {
$devLibs = $this->h5p->h5pD->getLibraries();
// Replace libraries with devlibs
for ($i = 0, $s = count($libraries); $i < $s; $i++) {
$lid = $libraries[$i]->name . ' ' . $libraries[$i]->majorVersion . '.' . $libraries[$i]->minorVersion;
if (isset($devLibs[$lid])) {
$libraries[$i] = (object) array(
'uberName' => $lid,
'name' => $devLibs[$lid]['machineName'],
'title' => $devLibs[$lid]['title'],
'majorVersion' => $devLibs[$lid]['majorVersion'],
'minorVersion' => $devLibs[$lid]['minorVersion'],
'runnable' => $devLibs[$lid]['runnable'],
);
}
}
}
return json_encode($libraries);
}
/**
* Keep track of temporary files.
*
* @param object file
*/
public function addTmpFile($file) {
$this->storage->addTmpFile($file);
}
/**
* Create directories for uploaded content.
*
* @param int $id
* @return boolean
*/
public function createDirectories($id) {
$this->content_directory = $this->contentFilesDir . DIRECTORY_SEPARATOR . $id . DIRECTORY_SEPARATOR;
if (!is_dir($this->contentFilesDir)) {
mkdir($this->contentFilesDir, 0777, true);
}
$sub_directories = array('', 'files', 'images', 'videos', 'audios');
foreach ($sub_directories AS $sub_directory) {
$sub_directory = $this->content_directory . $sub_directory;
if (!is_dir($sub_directory) && !mkdir($sub_directory)) {
return FALSE;
}
}
return TRUE;
}
/**
* Move uploaded files, remove old files and update library usage.
*
* @param string $oldLibrary
* @param string $oldParameters
* @param object $newLibrary
* @param string $newParameters
*/
public function processParameters($contentId, $newLibrary, $newParameters, $oldLibrary = NULL, $oldParameters = NULL) {
$newFiles = array();
$oldFiles = array();
// Find new libraries/content dependencies and files.
// Start by creating a fake library field to process. This way we get all the dependencies of the main library as well.
$field = (object) array(
'type' => 'library'
);
$libraryParams = (object) array(
'library' => H5PCore::libraryToString($newLibrary),
'params' => $newParameters
);
$this->processField($field, $libraryParams, $newFiles);
if ($oldLibrary !== NULL) {
// Find old files and libraries.
$this->processSemantics($oldFiles, $this->h5p->loadLibrarySemantics($oldLibrary['name'], $oldLibrary['majorVersion'], $oldLibrary['minorVersion']), $oldParameters);
// Remove old files.
for ($i = 0, $s = count($oldFiles); $i < $s; $i++) {
if (!in_array($oldFiles[$i], $newFiles) &&
preg_match('/^(\w+:\/\/|\.\.\/)/i', $oldFiles[$i]) === 0) {
$removeFile = $this->content_directory . $oldFiles[$i];
unlink($removeFile);
$this->storage->removeFile($removeFile);
}
}
}
}
/**
* Recursive function that moves the new files in to the h5p content folder and generates a list over the old files.
* Also locates all the librares.
*
* @param array $files
* @param array $libraries
* @param array $semantics
* @param array $params
*/
private function processSemantics(&$files, $semantics, &$params) {
for ($i = 0, $s = count($semantics); $i < $s; $i++) {
$field = $semantics[$i];
if (!isset($params->{$field->name})) {
continue;
}
$this->processField($field, $params->{$field->name}, $files);
}
}
/**
* Process a single field.
*
* @staticvar string $h5peditor_path
* @param object $field
* @param mixed $params
* @param array $files
*/
private function processField(&$field, &$params, &$files) {
switch ($field->type) {
case 'file':
case 'image':
if (isset($params->path)) {
$this->processFile($params, $files);
// Process original image
if (isset($params->originalImage) && isset($params->originalImage->path)) {
$this->processFile($params->originalImage, $files);
}
}
break;
case 'video':
case 'audio':
if (is_array($params)) {
for ($i = 0, $s = count($params); $i < $s; $i++) {
$this->processFile($params[$i], $files);
}
}
break;
case 'library':
if (isset($params->library) && isset($params->params)) {
$library = H5PCore::libraryFromString($params->library);
$semantics = $this->h5p->loadLibrarySemantics($library['machineName'], $library['majorVersion'], $library['minorVersion']);
// Process parameters for the library.
$this->processSemantics($files, $semantics, $params->params);
}
break;
case 'group':
if (isset($params)) {
if (count($field->fields) == 1) {
$params = (object) array($field->fields[0]->name => $params);
}
$this->processSemantics($files, $field->fields, $params);
}
break;
case 'list':
if (is_array($params)) {
for ($j = 0, $t = count($params); $j < $t; $j++) {
$this->processField($field->field, $params[$j], $files);
}
}
break;
}
}
/**
* @param mixed $params
* @param array $files
*/
private function processFile(&$params, &$files) {
static $h5peditor_path;
if (!$h5peditor_path) {
$h5peditor_path = $this->editorFilesDir . DIRECTORY_SEPARATOR;
}
// File could be copied from another content folder.
$matches = array();
if (preg_match($this->relativePathRegExp, $params->path, $matches)) {
// Create copy of file
$source = $this->content_directory . $params->path;
$destination = $this->content_directory . $matches[3];
if (file_exists($source) && !file_exists($destination)) {
copy($source, $destination);
}
$params->path = $matches[3];
}
else {
// Check if tmp file
$oldPath = $h5peditor_path . $params->path;
$newPath = $this->content_directory . $params->path;
if (file_exists($newPath)) {
// Uploaded to content folder, make sure the cleanup script doesn't get it.
$this->storage->keepFile($newPath, $newPath);
}
elseif (file_exists($oldPath)) {
// Copy file from editor tmp folder to content folder
copy($oldPath, $newPath);
// Not moved in-case it has been copied to multiple content.
}
}
$files[] = $params->path;
}
/**
* TODO: Consider moving to core.
*/
public function getLibraryLanguage($machineName, $majorVersion, $minorVersion, $languageCode) {
if ($this->h5p->development_mode & H5PDevelopment::MODE_LIBRARY) {
// Try to get language development library first.
$language = $this->h5p->h5pD->getLanguage($machineName, $majorVersion, $minorVersion, $languageCode);
}
if (isset($language) === FALSE) {
$language = $this->storage->getLanguage($machineName, $majorVersion, $minorVersion, $languageCode);
}
return ($language === FALSE ? NULL : $language);
}
/**
* Return all libraries used by the given editor library.
*
* @param string $machineName Library identfier part 1
* @param int $majorVersion Library identfier part 2
* @param int $minorVersion Library identfier part 3
*/
public function findEditorLibraries($machineName, $majorVersion, $minorVersion) {
$library = $this->h5p->loadLibrary($machineName, $majorVersion, $minorVersion);
$dependencies = array();
$this->h5p->findLibraryDependencies($dependencies, $library);
// Order dependencies by weight
$orderedDependencies = array();
for ($i = 1, $s = count($dependencies); $i <= $s; $i++) {
foreach ($dependencies as $dependency) {
if ($dependency['weight'] === $i && $dependency['type'] === 'editor') {
// Only load editor libraries.
$orderedDependencies[$dependency['library']['libraryId']] = $dependency['library'];
break;
}
}
}
return $orderedDependencies;
}
/**
* Get all scripts, css and semantics data for a library
*
* @param string $library_name
* Name of the library we want to fetch data for
* @param string $prefix Optional. Files are relative to another dir.
*/
public function getLibraryData($machineName, $majorVersion, $minorVersion, $languageCode, $path = '', $prefix = '') {
$libraryData = new stdClass();
$libraries = $this->findEditorLibraries($machineName, $majorVersion, $minorVersion);
$libraryData->semantics = $this->h5p->loadLibrarySemantics($machineName, $majorVersion, $minorVersion);
$libraryData->language = $this->getLibraryLanguage($machineName, $majorVersion, $minorVersion, $languageCode);
$files = $this->h5p->getDependenciesFiles($libraries, $prefix);
$this->storage->alterLibraryFiles($files, $libraries);
if ($path) {
$path .= '/';
}
// Javascripts
if (!empty($files['scripts'])) {
foreach ($files['scripts'] as $script) {
if (preg_match ('/:\/\//', $script->path) === 1) {
// External file
$libraryData->javascript[$script->path . $script->version] = "\n" . file_get_contents($script->path);
}
else {
// Local file
$libraryData->javascript[$this->h5p->url . $script->path . $script->version] = "\n" . file_get_contents($path . $script->path);
}
}
}
// Stylesheets
if (!empty($files['styles'])) {
foreach ($files['styles'] as $css) {
if (preg_match ('/:\/\//', $css->path) === 1) {
// External file
$libraryData->css[$css->path. $css->version] = file_get_contents($css->path);
}
else {
// Local file
H5peditor::buildCssPath(NULL, $this->h5p->url . dirname($css->path) . '/');
$libraryData->css[$this->h5p->url . $css->path . $css->version] = preg_replace_callback('/url\([\'"]?(?![a-z]+:|\/+)([^\'")]+)[\'"]?\)/i', 'H5peditor::buildCssPath', file_get_contents($path . $css->path));
}
}
}
// Add translations for libraries.
foreach ($libraries as $library) {
$language = $this->getLibraryLanguage($library['machineName'], $library['majorVersion'], $library['minorVersion'], $languageCode);
if ($language !== NULL) {
$lang = '; H5PEditor.language["' . $library['machineName'] . '"] = ' . $language . ';';
$libraryData->javascript[md5($lang)] = $lang;
}
}
return json_encode($libraryData);
}
/**
* This function will prefix all paths within a CSS file.
* Copied from Drupal 6.
*
* @staticvar type $_base
* @param type $matches
* @param type $base
* @return type
*/
public static function buildCssPath($matches, $base = NULL) {
static $_base;
// Store base path for preg_replace_callback.
if (isset($base)) {
$_base = $base;
}
// Prefix with base and remove '../' segments where possible.
$path = $_base . $matches[1];
$last = '';
while ($path != $last) {
$last = $path;
$path = preg_replace('`(^|/)(?!\.\./)([^/]+)/\.\./`', '$1', $path);
}
return 'url('. $path .')';
}
}