/** * Checks if file with name $filename has one of the extensions in groups $groups * * @see get_mimetypes_array() * @param string $filename name of the file to check * @param string|array $groups one group or array of groups to check * @param bool $checktype if true and extension check fails, find the mimetype and check if * file mimetype is in mimetypes in groups $groups * @return bool */ function file_extension_in_typegroup($filename, $groups, $checktype = false) { $extension = pathinfo($filename, PATHINFO_EXTENSION); if (!empty($extension) && in_array('.' . strtolower($extension), file_get_typegroup('extension', $groups))) { return true; } return $checktype && file_mimetype_in_typegroup(mimeinfo('type', $filename), $groups); }
/** * Verifies the file is a valid web image - gif, png and jpeg only. * * It should be ok to serve this image from server without any other security workarounds. * * @return bool true if file ok */ public function is_valid_image() { $mimetype = $this->get_mimetype(); if (!file_mimetype_in_typegroup($mimetype, 'web_image')) { return false; } if (!($info = $this->get_imageinfo())) { return false; } if ($info['mimetype'] !== $mimetype) { return false; } // ok, GD likes this image return true; }
/** * Renders a list of files attached to the submission * * If format==html, then format a html string. If format==text, then format a text-only string. * Otherwise, returns html for non-images and html to display the image inline. * * @param int $submissionid submission identifier * @param string format the format of the returned string - html|text * @return string formatted text to be echoed */ protected function helper_submission_attachments($submissionid, $format = 'html') { global $CFG; require_once $CFG->libdir . '/filelib.php'; $fs = get_file_storage(); $ctx = $this->page->context; $files = $fs->get_area_files($ctx->id, 'mod_workshop', 'submission_attachment', $submissionid); $outputimgs = ''; // images to be displayed inline $outputfiles = ''; // list of attachment files foreach ($files as $file) { if ($file->is_directory()) { continue; } $filepath = $file->get_filepath(); $filename = $file->get_filename(); $fileurl = moodle_url::make_pluginfile_url($ctx->id, 'mod_workshop', 'submission_attachment', $submissionid, $filepath, $filename, true); $embedurl = moodle_url::make_pluginfile_url($ctx->id, 'mod_workshop', 'submission_attachment', $submissionid, $filepath, $filename, false); $embedurl = new moodle_url($embedurl, array('preview' => 'bigthumb')); $type = $file->get_mimetype(); $image = $this->output->pix_icon(file_file_icon($file), get_mimetype_description($file), 'moodle', array('class' => 'icon')); $linkhtml = html_writer::link($fileurl, $image . substr($filepath, 1) . $filename); $linktxt = "{$filename} [{$fileurl}]"; if ($format == 'html') { if (file_mimetype_in_typegroup($type, 'web_image')) { $preview = html_writer::empty_tag('img', array('src' => $embedurl, 'alt' => '', 'class' => 'preview')); $preview = html_writer::tag('a', $preview, array('href' => $fileurl)); $outputimgs .= $this->output->container($preview); } else { $outputfiles .= html_writer::tag('li', $linkhtml, array('class' => $type)); } } else { if ($format == 'text') { $outputfiles .= $linktxt . PHP_EOL; } } if (!empty($CFG->enableplagiarism)) { require_once $CFG->libdir . '/plagiarismlib.php'; $outputfiles .= plagiarism_get_links(array('userid' => $file->get_userid(), 'file' => $file, 'cmid' => $this->page->cm->id, 'course' => $this->page->course->id)); } } if ($format == 'html') { if ($outputimgs) { $outputimgs = $this->output->container($outputimgs, 'images'); } if ($outputfiles) { $outputfiles = html_writer::tag('ul', $outputfiles, array('class' => 'files')); } return $this->output->container($outputimgs . $outputfiles, 'attachments'); } else { return $outputfiles; } }
/** * Handle a file that has been uploaded * @param object $uploadinfo details of the file / content that has been uploaded * @return int instance id of the newly created mod */ function label_dndupload_handle($uploadinfo) { global $USER; // Gather the required info. $data = new stdClass(); $data->course = $uploadinfo->course->id; $data->name = $uploadinfo->displayname; $data->intro = ''; $data->introformat = FORMAT_HTML; $data->coursemodule = $uploadinfo->coursemodule; // Extract the first (and only) file from the file area and add it to the label as an img tag. if (!empty($uploadinfo->draftitemid)) { $fs = get_file_storage(); $draftcontext = context_user::instance($USER->id); $context = context_module::instance($uploadinfo->coursemodule); $files = $fs->get_area_files($draftcontext->id, 'user', 'draft', $uploadinfo->draftitemid, '', false); if ($file = reset($files)) { if (file_mimetype_in_typegroup($file->get_mimetype(), 'web_image')) { // It is an image - resize it, if too big, then insert the img tag. $config = get_config('label'); $data->intro = label_generate_resized_image($file, $config->dndresizewidth, $config->dndresizeheight); } else { // We aren't supposed to be supporting non-image types here, but fallback to adding a link, just in case. $url = moodle_url::make_draftfile_url($file->get_itemid(), $file->get_filepath(), $file->get_filename()); $data->intro = html_writer::link($url, $file->get_filename()); } $data->intro = file_save_draft_area_files($uploadinfo->draftitemid, $context->id, 'mod_label', 'intro', 0, null, $data->intro); } } else { if (!empty($uploadinfo->content)) { $data->intro = $uploadinfo->content; if ($uploadinfo->type != 'text/html') { $data->introformat = FORMAT_PLAIN; } } } return label_add_instance($data, null); }
/** * Decide the best display format. * @param object $resource * @return int display type constant */ function resource_get_final_display_type($resource) { global $CFG, $PAGE; if ($resource->display != RESOURCELIB_DISPLAY_AUTO) { return $resource->display; } if (empty($resource->mainfile)) { return RESOURCELIB_DISPLAY_DOWNLOAD; } else { $mimetype = mimeinfo('type', $resource->mainfile); } if (file_mimetype_in_typegroup($mimetype, 'archive')) { return RESOURCELIB_DISPLAY_DOWNLOAD; } if (file_mimetype_in_typegroup($mimetype, array('web_image', '.htm', 'web_video', 'web_audio'))) { return RESOURCELIB_DISPLAY_EMBED; } // let the browser deal with it somehow return RESOURCELIB_DISPLAY_OPEN; }
/** * Returns what gallery type this falls into (image, audio, video). * @param bool $text If the function returns the internal or text representation of the item type. * @return int|null Returns the self::TYPE_* that most closely matches the content. Otherwise null. */ public function type($text = false) { if ($this->record->externalurl != '') { // External URLs are either youtube videos or images. if ($this->get_youtube_videoid()) { return $text ? 'video' : self::TYPE_VIDEO; } return $text ? 'image' : self::TYPE_IMAGE; } if (!($file = $this->get_file())) { return null; } $videogroups = array('web_video'); if (!empty($this->objectid)) { $videogroups[] = 'video'; } $mimetype = $this->file->get_mimetype(); if (file_mimetype_in_typegroup($mimetype, 'web_image')) { return $text ? 'image' : self::TYPE_IMAGE; } else { if (file_mimetype_in_typegroup($mimetype, 'web_audio')) { return $text ? 'audio' : self::TYPE_AUDIO; } else { if (file_mimetype_in_typegroup($mimetype, $videogroups)) { return $text ? 'video' : self::TYPE_VIDEO; } } } $texttotype = array('audio' => self::TYPE_AUDIO, 'image' => self::TYPE_IMAGE, 'video' => self::TYPE_VIDEO); if ($mimetype == 'document/unknown' && !empty($this->objectid)) { $ref = $this->file->get_reference_details(); if (isset($ref->type) && in_array($ref->type, array('audio', 'image', 'video'))) { return $text ? $ref->type : $texttotype[$ref->type]; } } return null; }
/** * Renders a list of files attached to the submission * * If format==html, then format a html string. If format==text, then format a text-only string. * Otherwise, returns html for non-images and html to display the image inline. * * @param int $submissionid submission identifier * @param string format the format of the returned string - html|text * @return string formatted text to be echoed */ protected function helper_submission_attachments($submissionid, $format = 'html') { global $CFG; require_once($CFG->libdir.'/filelib.php'); $fs = get_file_storage(); $ctx = $this->page->context; $files = $fs->get_area_files($ctx->id, 'mod_workshop', 'submission_attachment', $submissionid); $outputimgs = ''; // images to be displayed inline $outputfiles = ''; // list of attachment files foreach ($files as $file) { if ($file->is_directory()) { continue; } $filepath = $file->get_filepath(); $filename = $file->get_filename(); $fileurl = file_encode_url($CFG->wwwroot . '/pluginfile.php', '/' . $ctx->id . '/mod_workshop/submission_attachment/' . $submissionid . $filepath . $filename, true); $type = $file->get_mimetype(); $image = $this->output->pix_icon(file_file_icon($file), get_mimetype_description($file), 'moodle', array('class' => 'icon')); $linkhtml = html_writer::link($fileurl, $image) . substr($filepath, 1) . html_writer::link($fileurl, $filename); $linktxt = "$filename [$fileurl]"; if ($format == 'html') { if (file_mimetype_in_typegroup($type, 'web_image')) { $preview = html_writer::empty_tag('img', array('src' => $fileurl, 'alt' => '', 'class' => 'preview')); $preview = html_writer::tag('a', $preview, array('href' => $fileurl)); $outputimgs .= $this->output->container($preview); } else { $outputfiles .= html_writer::tag('li', $linkhtml, array('class' => $type)); } } else if ($format == 'text') { $outputfiles .= $linktxt . PHP_EOL; } } if ($format == 'html') { if ($outputimgs) { $outputimgs = $this->output->container($outputimgs, 'images'); } if ($outputfiles) { $outputfiles = html_writer::tag('ul', $outputfiles, array('class' => 'files')); } return $this->output->container($outputimgs . $outputfiles, 'attachments'); } else { return $outputfiles; } }
function toHtml() { global $CFG, $OUTPUT; $htmltable = new html_table(); $htmltable->head = array(get_string('deleteupload', 'wiki'), get_string('uploadname', 'wiki'), get_string('uploadactions', 'wiki')); $fs = get_file_storage(); $files = $fs->get_area_files($this->_fileinfo['contextid'], 'mod_wiki', 'attachments', $this->_fileinfo['itemid']); //TODO: verify where this is coming from, all params must be validated (skodak) if (count($files) < 2) { return get_string('noattachments', 'wiki'); } //get tags foreach (array('image', 'attach', 'link') as $tag) { $tags[$tag] = wiki_parser_get_token($this->_format, $tag); } foreach ($files as $file) { if (!$file->is_directory()) { $checkbox = '<input type="checkbox" name="' . $this->_attributes['name'] . '[]" value="' . $file->get_pathnamehash() . '"'; if (in_array($file->get_pathnamehash(), $this->_value)) { $checkbox .= ' checked="checked"'; } $checkbox .= " />"; //actions $icon = file_file_icon($file); $file_url = file_encode_url($CFG->wwwroot . '/pluginfile.php', "/{$this->_contextid}/mod_wiki/attachments/{$this->_fileareaitemid}/" . $file->get_filename()); $action_icons = ""; if (!empty($tags['attach'])) { $action_icons .= "<a href=\"javascript:void(0)\" class=\"wiki-attachment-attach\" " . $this->printInsertTags($tags['attach'], $file->get_filename()) . " title=\"" . get_string('attachmentattach', 'wiki') . "\">" . $OUTPUT->pix_icon($icon, "Attach") . "</a>"; //TODO: localize } $action_icons .= " <a href=\"javascript:void(0)\" class=\"wiki-attachment-link\" " . $this->printInsertTags($tags['link'], $file_url) . " title=\"" . get_string('attachmentlink', 'wiki') . "\">" . $OUTPUT->pix_icon($icon, "Link") . "</a>"; if (file_mimetype_in_typegroup($file->get_mimetype(), 'web_image')) { $action_icons .= " <a href=\"javascript:void(0)\" class=\"wiki-attachment-image\" " . $this->printInsertTags($tags['image'], $file->get_filename()) . " title=\"" . get_string('attachmentimage', 'wiki') . "\">" . $OUTPUT->pix_icon($icon, "Image") . "</a>"; //TODO: localize } $htmltable->data[] = array($checkbox, '<a href="' . $file_url . '">' . $file->get_filename() . '</a>', $action_icons); } } return html_writer::table($htmltable); }
/** * Renders an entry attachment * * Print link for non-images and returns images as HTML * * @param blog_entry_attachment $attachment * @return string List of attachments depending on the $return input */ public function render_blog_entry_attachment(blog_entry_attachment $attachment) { $syscontext = context_system::instance(); // Image attachments don't get printed as links. if (file_mimetype_in_typegroup($attachment->file->get_mimetype(), 'web_image')) { $attrs = array('src' => $attachment->url, 'alt' => ''); $o = html_writer::empty_tag('img', $attrs); $class = 'attachedimages'; } else { $image = $this->output->pix_icon(file_file_icon($attachment->file), $attachment->filename, 'moodle', array('class' => 'icon')); $o = html_writer::link($attachment->url, $image); $o .= format_text(html_writer::link($attachment->url, $attachment->filename), FORMAT_HTML, array('context' => $syscontext)); $class = 'attachments'; } return $this->output->container($o, $class); }
/** * (non-PHPdoc) * @param \cm_info $rawData * @see \intuitel\ResourceLOFactory::getMediaType() * @return string */ function getMediaType(\cm_info $rawData = null) { //return 'PresentationMedia'; $context = \context_module::instance($rawData->id); $fs = get_file_storage(); $files = $fs->get_area_files($context->id, 'mod_resource', 'content', 0, 'sortorder DESC, id ASC', false); $file = reset($files); // takes the first one that is the main file (the other ones can be uploaded and are shown when modifying but are not shown when displaying the resource -> think Moodle developers will amend this) unset($files); $mimetype = $file->get_mimetype(); // filesize in bytes if (file_mimetype_in_typegroup($mimetype, array('web_image', 'image'))) { //this is an imagen $mediatype = 'PhotoPresentation'; // could be also a DrawingPresentation. } else { if (file_mimetype_in_typegroup($mimetype, array('video', 'web_video'))) { $mediatype = 'VideoPresentation'; } else { if (file_mimetype_in_typegroup($mimetype, array('audio', 'web_audio'))) { $mediatype = 'AudioPresentation'; } else { if ($mimetype = 'text/plain') { $mediatype = 'TextPresentation'; } else { $mediatype = 'PresentationMedia'; } } } } return $mediatype; }
/** * Recupera bgimage das paginas de conteudo do plugin content * @param stdClass $context * @return string $fullpath */ function content_get_page_bgimage($context, $page) { global $CFG; $fs = get_file_storage(); $files = $fs->get_area_files($context->id, 'mod_content', 'bgpage', $page->id, 'sortorder DESC, id ASC', false); // TODO: this is not very efficient!! if (count($files) >= 1) { $file = reset($files); unset($files); $path = '/' . $context->id . '/mod_content/bgpage/' . $page->id . $file->get_filepath() . $file->get_filename(); $fullurl = file_encode_url($CFG->wwwroot . '/pluginfile.php', $path, false); $mimetype = $file->get_mimetype(); if (file_mimetype_in_typegroup($mimetype, 'web_image')) { // It's an image return $fullurl; } return false; } return false; }
/** * if return=html, then return a html string. * if return=text, then return a text-only string. * otherwise, print HTML for non-images, and return image HTML * * @param bool $return Whether to return or print the generated code * @return void */ public function print_attachments($return = false) { global $CFG, $OUTPUT; require_once $CFG->libdir . '/filelib.php'; $fs = get_file_storage(); $syscontext = get_context_instance(CONTEXT_SYSTEM); $files = $fs->get_area_files($syscontext->id, 'blog', 'attachment', $this->id); $imagereturn = ""; $output = ""; $strattachment = get_string("attachment", "forum"); foreach ($files as $file) { if ($file->is_directory()) { continue; } $filename = $file->get_filename(); $ffurl = file_encode_url($CFG->wwwroot . '/pluginfile.php', '/' . SYSCONTEXTID . '/blog/attachment/' . $this->id . '/' . $filename); $mimetype = $file->get_mimetype(); $image = $OUTPUT->pix_icon(file_file_icon($file), $filename, 'moodle', array('class' => 'icon')); if ($return == "html") { $output .= html_writer::link($ffurl, $image); $output .= html_writer::link($ffurl, $filename); } else { if ($return == "text") { $output .= "{$strattachment} {$filename}:\n{$ffurl}\n"; } else { if (file_mimetype_in_typegroup($file->get_mimetype(), 'web_image')) { // Image attachments don't get printed as links $imagereturn .= '<br /><img src="' . $ffurl . '" alt="" />'; } else { $imagereturn .= html_writer::link($ffurl, $image); $imagereturn .= format_text(html_writer::link($ffurl, $filename), FORMAT_HTML, array('context' => $syscontext)); } } } } if ($return) { return $output; } return $imagereturn; }