/** * Handle file uploads through AJAX. * * @since 1.1.0 */ public function ajax_files() { $plugin = H5P_Plugin::get_instance(); $files_directory = $plugin->get_h5p_path(); if (!wp_verify_nonce(filter_input(INPUT_GET, 'token', FILTER_SANITIZE_STRING), 'h5p_editor_ajax')) { H5PCore::ajaxError(__('Invalid security token. Please reload the editor.', $this->plugin_slug)); exit; } $contentId = filter_input(INPUT_POST, 'contentId', FILTER_SANITIZE_NUMBER_INT); if ($contentId) { $files_directory .= '/content/' . $contentId; } else { $files_directory .= '/editor'; } $editor = $this->get_h5peditor_instance(); $interface = $plugin->get_h5p_instance('interface'); $file = new H5peditorFile($interface, $files_directory); if (!$file->isLoaded()) { H5PCore::ajaxError(__('File not found on server. Check file upload settings.', $this->plugin_slug)); exit; } if ($file->validate() && $file->copy()) { // Keep track of temporary files so they can be cleaned up later. $editor->addTmpFile($file); } header('Cache-Control: no-cache'); // Must support IE, so cannot use application/json header('Content-type: text/plain; charset=utf-8'); print $file->getResult(); exit; }
public function files() { // $files_directory = file_directory_path(); $files_directory = JPATH_ROOT . DIRECTORY_SEPARATOR . 'media'; if (isset($_POST['contentId']) && $_POST['contentId']) { $files_directory .= DIRECTORY_SEPARATOR . 'h5p/content/' . $_POST['contentId']; } else { $files_directory .= DIRECTORY_SEPARATOR . 'h5peditor'; } $file = new H5peditorFile($files_directory); if (!$file->isLoaded()) { return; } if ($file->validate() && $file->copy()) { $storage = H5PJoomla::getInstance('editorstorage'); $storage->addTempFile($file); } print $file->getResult(); }
* Parameters: * int contentId * int contextId */ case 'files': global $DB; // TODO: Check permissions if (!\H5PCore::validToken('editorajax', required_param('token', PARAM_RAW))) { \H5PCore::ajaxError(get_string('invalidtoken', 'hvp')); exit; } // Get Content ID and Context ID for upload $contentid = required_param('contentId', PARAM_INT); $contextid = required_param('contextId', PARAM_INT); // Create file $file = new H5peditorFile(\mod_hvp\framework::instance('interface')); if (!$file->isLoaded()) { H5PCore::ajaxError(get_string('filenotfound', 'hvp')); break; } // Make sure file is valid if ($file->validate()) { $core = \mod_hvp\framework::instance('core'); // Save the valid file $file_id = $core->fs->saveFile($file, $contentid, $contextid); // Track temporary files for later cleanup $DB->insert_record_raw('hvp_tmpfiles', array('id' => $file_id), false, false, true); } $file->printResult(); break; /*
/** * Save files uploaded through the editor. * * @param \H5peditorFile $file * @param int $contentid * @param \stdClass $context Course Context ID */ public function saveFile($file, $contentid, $contextid = null) { if ($contentid !== 0) { // Grab cm context $cm = \get_coursemodule_from_instance('hvp', $contentid); $context = \context_module::instance($cm->id); $contextid = $context->id; } // Files not yet related to any activities are stored in a course context // (These are temporary files and should not be part of backups.) $record = array('contextid' => $contextid, 'component' => 'mod_hvp', 'filearea' => $contentid === 0 ? 'editor' : 'content', 'itemid' => $contentid, 'filepath' => '/' . $file->getType() . 's/', 'filename' => $file->getName()); $fs = get_file_storage(); $filedata = $file->getData(); if ($filedata) { $stored_file = $fs->create_file_from_string($record, $filedata); } else { $stored_file = $fs->create_file_from_pathname($record, $_FILES['file']['tmp_name']); } return $stored_file->get_id(); }
/** * Handle file uploads through AJAX. * * @since 1.1.0 */ public function ajax_files() { global $wpdb; $plugin = H5P_Plugin::get_instance(); $files_directory = $plugin->get_h5p_path(); $contentId = filter_input(INPUT_POST, 'contentId', FILTER_SANITIZE_NUMBER_INT); if ($contentId) { $files_directory .= '/content/' . $contentId; } else { $files_directory .= '/editor'; } $editor = $this->get_h5peditor_instance(); $interface = $plugin->get_h5p_instance('interface'); $file = new H5peditorFile($interface, $files_directory); if (!$file->isLoaded()) { exit; } if ($file->validate() && $file->copy()) { // Keep track of temporary files so they can be cleaned up later. $editor->addTmpFile($file); } header('Cache-Control: no-cache'); header('Content-type: application/json; charset=utf-8'); print $file->getResult(); exit; }