/** * 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); }
/** * Get all scripts, css and semantics data for a library * * @param string $library_name * Name of the library we want to fetch data for */ public function getLibraryData($machineName, $majorVersion, $minorVersion) { $libraryData = new stdClass(); $libraryData->semantics = $this->storage->getSemantics($machineName, $majorVersion, $minorVersion); $language = $this->storage->getLanguage($machineName, $majorVersion, $minorVersion); if ($language) { $libraryData->language = $language; } $editorLibraryIds = $this->storage->getEditorLibraries($machineName, $majorVersion, $minorVersion); foreach ($editorLibraryIds as $editorLibraryId => $editorLibrary) { $filePaths = $this->storage->getFilePaths($editorLibraryId); if (!empty($filePaths['js'])) { foreach ($filePaths['js'] as $jsFilePath) { // TODO: rtrim and check substr(-1) === '}'? jsmin? if (!isset($libraryData->javascript[$jsFilePath])) { $libraryData->javascript[$jsFilePath] = ''; } $libraryData->javascript[$jsFilePath] .= "\n" . file_get_contents($jsFilePath); } } $language = $this->storage->getLanguage($editorLibrary['machineName'], $editorLibrary['majorVersion'], $editorLibrary['minorVersion']); if ($language) { $lang = '; H5PEditor.language["' . $editorLibrary['machineName'] . '"] = ' . $language . ';'; $libraryData->javascript[md5($lang)] = $lang; } if (!empty($filePaths['css'])) { foreach ($filePaths['css'] as $cssFilePath) { H5peditor::buildCssPath(NULL, $this->basePath . dirname($cssFilePath) . '/'); $css = preg_replace_callback('/url\\([\'"]?(?![a-z]+:|\\/+)([^\'")]+)[\'"]?\\)/i', 'H5peditor::buildCssPath', file_get_contents($cssFilePath)); $libraryData->css[$cssFilePath] = $css; } } } return json_encode($libraryData); }
public function save() { $input = JFactory::getApplication()->input; $contentId = $input->getVar('cid'); // Cannot use input->get() here. It eats all HTML, with no option to // tell it not to... H5P does its own XSS filtering of HTML fields, // but it does so when returning it from the database. $h5p_params = $_POST['edit-h5p-params']; $library = $input->get('edit-h5p-library', '', 'string'); $h5p_core = H5PJoomla::getInstance('core'); $h5p_joomla = H5PJoomla::getInstance('interface'); $library_data = $h5p_core->libraryFromString($library); $libraryId = $h5p_joomla->getLibraryId($library_data['machineName'], $library_data['majorVersion'], $library_data['minorVersion']); $db = JFactory::getDbo(); $jsonQuoted = $db->quote($h5p_params); $title = $input->get('h5p-title', 'My H5P', 'string'); if (!$title) { $title = 'Untitled ' . $library_data['machineName']; } $titleQuoted = $db->quote($title); $db->setQuery(sprintf("INSERT INTO #__h5p (h5p_id, title, json_content, embed_type, main_library_id)\n\t\t\t VALUES (%s, %s, %s, 'div, iframe', %d)\n\t\t\t ON DUPLICATE KEY UPDATE title=%s, json_content=%s, main_library_id=%d", $db->quote($contentId), $titleQuoted, $jsonQuoted, $libraryId, $titleQuoted, $jsonQuoted, $libraryId)); $db->query(); $storage = H5PJoomla::getInstance('editorstorage'); $h5pStorage = H5PJoomla::getInstance('storage'); $editor = new H5peditor($storage, JPATH_ROOT . DIRECTORY_SEPARATOR . 'media', JURI::root(true), $h5pStorage); if (!$editor->createDirectories($contentId)) { print "<p>Unable to create content directories on the server. Please contact the system administrator.</p>"; return; } // Move files. $editor->processParameters($contentId, $library_data, json_decode($h5p_params), NULL, NULL); // Response HTML header('Content-Type: text/html'); print "\n\t\t\t<script type=\"text/javascript\">\n\t\t\t\twindow.parent.insertH5P('{$contentId}', '{$title}');\n\t\t\t</script>\n\t\t"; }