/** * Add enclosures to a template * * @param object $objTemplate The template object to add the enclosures to * @param array $arrItem The element or module as array * @param string $strKey The name of the enclosures field in $arrItem */ public static function addEnclosuresToTemplate($objTemplate, $arrItem, $strKey = 'enclosure') { $arrEnclosures = deserialize($arrItem[$strKey]); if (!is_array($arrEnclosures) || empty($arrEnclosures)) { return; } $objFiles = \FilesModel::findMultipleByUuids($arrEnclosures); if ($objFiles === null) { return; } $file = \Input::get('file', true); // Send the file to the browser and do not send a 404 header (see #5178) if ($file != '') { while ($objFiles->next()) { if ($file == $objFiles->path) { static::sendFileToBrowser($file); } } $objFiles->reset(); } /** @var \PageModel $objPage */ global $objPage; $arrEnclosures = array(); $allowedDownload = trimsplit(',', strtolower(\Config::get('allowedDownload'))); // Add download links while ($objFiles->next()) { if ($objFiles->type == 'file') { if (!in_array($objFiles->extension, $allowedDownload) || !is_file(TL_ROOT . '/' . $objFiles->path)) { continue; } $objFile = new \File($objFiles->path); $strHref = \Environment::get('request'); // Remove an existing file parameter (see #5683) if (preg_match('/(&(amp;)?|\\?)file=/', $strHref)) { $strHref = preg_replace('/(&(amp;)?|\\?)file=[^&]+/', '', $strHref); } $strHref .= (strpos($strHref, '?') !== false ? '&' : '?') . 'file=' . \System::urlEncode($objFiles->path); $arrMeta = \Frontend::getMetaData($objFiles->meta, $objPage->language); if (empty($arrMeta) && $objPage->rootFallbackLanguage !== null) { $arrMeta = \Frontend::getMetaData($objFiles->meta, $objPage->rootFallbackLanguage); } // Use the file name as title if none is given if ($arrMeta['title'] == '') { $arrMeta['title'] = specialchars($objFile->basename); } $arrEnclosures[] = array('link' => $arrMeta['title'], 'filesize' => static::getReadableSize($objFile->filesize), 'title' => specialchars(sprintf($GLOBALS['TL_LANG']['MSC']['download'], $objFile->basename)), 'href' => $strHref, 'enclosure' => $objFiles->path, 'icon' => TL_ASSETS_URL . 'assets/contao/images/' . $objFile->icon, 'mime' => $objFile->mime, 'extension' => $objFile->extension, 'meta' => $arrMeta); } } $objTemplate->enclosure = $arrEnclosures; }
/** * Generate an image tag and return it as string * * @param string $src The image path * @param string $alt An optional alt attribute * @param string $attributes A string of other attributes * * @return string The image HTML tag */ public static function getHtml($src, $alt = '', $attributes = '') { $src = static::getPath($src); if ($src == '') { return ''; } if (!is_file(TL_ROOT . '/' . $src)) { // Handle public bundle resources if (file_exists(TL_ROOT . '/web/' . $src)) { $src = 'web/' . $src; } else { return ''; } } $objFile = new \File($src); // Strip the web/ prefix (see #337) if (strncmp($src, 'web/', 4) === 0) { $src = substr($src, 4); } $static = strncmp($src, 'assets/', 7) === 0 ? TL_ASSETS_URL : TL_FILES_URL; return '<img src="' . $static . \System::urlEncode($src) . '" width="' . $objFile->width . '" height="' . $objFile->height . '" alt="' . \StringUtil::specialchars($alt) . '"' . ($attributes != '' ? ' ' . $attributes : '') . '>'; }