/** * Generate array representation for download * * @param bool $blnOrderPaid * * @return array */ public function getForTemplate($blnOrderPaid = false) { global $objPage; $objDownload = $this->getRelated('download_id'); if (null === $objDownload) { return array(); } $arrDownloads = array(); $allowedDownload = trimsplit(',', strtolower($GLOBALS['TL_CONFIG']['allowedDownload'])); foreach ($objDownload->getFiles() as $objFileModel) { $objFile = new \File($objFileModel->path, true); if (!in_array($objFile->extension, $allowedDownload) || preg_match('/^meta(_[a-z]{2})?\\.txt$/', $objFile->basename)) { continue; } // Send file to the browser if ($blnOrderPaid && $this->canDownload() && \Input::get('download') == $objDownload->id && \Input::get('file') == $objFileModel->path) { $this->download($objFileModel->path); } $arrMeta = \Frontend::getMetaData($objFileModel->meta, $objPage->language); // Use the file name as title if none is given if ($arrMeta['title'] == '') { $arrMeta['title'] = specialchars(str_replace('_', ' ', preg_replace('/^[0-9]+_/', '', $objFile->filename))); } $strHref = ''; if (TL_MODE == 'FE') { $strHref = \Haste\Util\Url::addQueryString('download=' . $objDownload->id . '&file=' . $objFileModel->path); } // Add the image $arrDownloads[] = array('id' => $this->id, 'name' => $objFile->basename, 'title' => $arrMeta['title'], 'link' => $arrMeta['title'], 'caption' => $arrMeta['caption'], 'href' => $strHref, 'filesize' => \System::getReadableSize($objFile->filesize, 1), 'icon' => TL_ASSETS_URL . 'assets/contao/images/' . $objFile->icon, 'mime' => $objFile->mime, 'meta' => $arrMeta, 'extension' => $objFile->extension, 'path' => $objFile->dirname, 'remaining' => $objDownload->downloads_allowed > 0 ? sprintf($GLOBALS['TL_LANG']['MSC']['downloadsRemaining'], intval($this->downloads_remaining)) : '', 'downloadable' => $blnOrderPaid && $this->canDownload()); } return $arrDownloads; }
protected static function addToPDFSearchIndex($strFile, $arrParentSet) { $objFile = new \File($strFile); if (!Validator::isValidPDF($objFile)) { return false; } $objDatabase = \Database::getInstance(); $objModel = $objFile->getModel(); $arrMeta = \Frontend::getMetaData($objModel->meta, $arrParentSet['language']); // Use the file name as title if none is given if ($arrMeta['title'] == '') { $arrMeta['title'] = specialchars($objFile->basename); } $strHref = \Environment::get('base') . \Environment::get('request'); // Remove an existing file parameter if (preg_match('/(&(amp;)?|\\?)file=/', $strHref)) { $strHref = preg_replace('/(&(amp;)?|\\?)file=[^&]+/', '', $strHref); } $strHref .= (\Config::get('disableAlias') || strpos($strHref, '?') !== false ? '&' : '?') . 'file=' . \System::urlEncode($objFile->value); $arrSet = array('pid' => $arrParentSet['pid'], 'tstamp' => time(), 'title' => $arrMeta['title'], 'url' => $strHref, 'filesize' => \System::getReadableSize($objFile->size, 2), 'checksum' => $objFile->hash, 'protected' => $arrParentSet['protected'], 'groups' => $arrParentSet['groups'], 'language' => $arrParentSet['language'], 'mime' => $objFile->mime); // Return if the file is indexed and up to date $objIndex = $objDatabase->prepare("SELECT * FROM tl_search WHERE pid=? AND checksum=?")->execute($arrSet['pid'], $arrSet['checksum']); // there are already indexed files containing this file (same checksum and filename) if ($objIndex->numRows) { // Return if the page with the file is indexed if (in_array($arrSet['pid'], $objIndex->fetchEach('pid'))) { return false; } $strContent = $objIndex->text; } else { try { // parse only for the first occurrence $parser = new \Smalot\PdfParser\Parser(); $objPDF = $parser->parseFile($strFile); $strContent = $objPDF->getText(); } catch (\Exception $e) { // Missing object refernce #... return false; } } // Put everything together $arrSet['text'] = $strContent; $arrSet['text'] = trim(preg_replace('/ +/', ' ', \String::decodeEntities($arrSet['text']))); // Update an existing old entry if ($objIndex->pid == $arrSet['pid']) { $objDatabase->prepare("UPDATE tl_search %s WHERE id=?")->set($arrSet)->execute($objIndex->id); $intInsertId = $objIndex->id; } else { $objInsertStmt = $objDatabase->prepare("INSERT INTO tl_search %s")->set($arrSet)->execute(); $intInsertId = $objInsertStmt->insertId; } static::indexContent($arrSet, $intInsertId); }
public static function addDownloadsFromProductDownloadsToTemplate($objTemplate) { $arrDownloads = array(); // array for downloadfiles from db $arrFiles = array(); // contains queryresults from db $strTable = "tl_iso_download"; // name of download table global $objPage; $arrOptions = array('order' => 'sorting ASC'); $arrFiles = \Isotope\Model\Download::findBy('pid', $objTemplate->id, $arrOptions); if ($arrFiles === null) { return $arrDownloads; } while ($arrFiles->next()) { $objModel = \FilesModel::findByUuid($arrFiles->singleSRC); if ($objModel === null) { if (!\Validator::isUuid($arrFiles->singleSRC)) { $objTemplate->text = '<p class="error">' . $GLOBALS['TL_LANG']['ERR']['version2format'] . '</p>'; } } elseif (is_file(TL_ROOT . '/' . $objModel->path)) { $objFile = new \File($objModel->path, true); $file = \Input::get('file', true); // Send the file to the browser and do not send a 404 header (see #4632) if ($file != '' && $file == $objFile->path) { \Controller::sendFileToBrowser($file); } $arrMeta = \Frontend::getMetaData($objModel->meta, $objPage->language); if (empty($arrMeta)) { if ($objPage->rootFallbackLanguage !== null) { $arrMeta = \Frontend::getMetaData($objModel->meta, $objPage->rootFallbackLanguage); } } $strHref = \Environment::get('request'); // Remove an existing file parameter (see #5683) if (preg_match('/(&(amp;)?|\\?)file=/', $strHref)) { $strHref = preg_replace('/(&(amp;)?|\\?)file=[^&]+/', '', $strHref); } $strHref .= (\Config::get('disableAlias') || strpos($strHref, '?') !== false ? '&' : '?') . 'file=' . \System::urlEncode($objFile->path); $objDownload = new \stdClass(); $objDownload->id = $objModel->id; $objDownload->uuid = $objModel->uuid; $objDownload->name = $objFile->basename; $objDownload->formedname = preg_replace(array('/_/', '/.\\w+$/'), array(' ', ''), $objFile->basename); $objDownload->title = specialchars(sprintf($GLOBALS['TL_LANG']['MSC']['download'], $objFile->basename)); $objDownload->link = $arrMeta['title']; $objDownload->filesize = \System::getReadableSize($objFile->filesize, 1); $objDownload->icon = TL_ASSETS_URL . 'assets/contao/images/' . $objFile->icon; $objDownload->href = $strHref; $objDownload->mime = $objFile->mime; $objDownload->extension = $objFile->extension; $objDownload->path = $objFile->dirname; $objDownload->class = 'isotope-download isotope-download-file'; $objT = new \FrontendTemplate('isotope_download_from_attribute'); $objT->setData((array) $objDownload); $objDownload->output = $objT->parse(); $arrDownloads[] = $objDownload; } } $objTemplate->downloads = $arrDownloads; }
/** * Get the images * * @param string $sorting * @param string $metaLanguage * @param bool $metaIgnore * @param string $metaFallbackLanguage * * @return array */ public function getImages($sorting, $metaLanguage, $metaIgnore = false, $metaFallbackLanguage = null) { $files = $this->getFileModels(); if ($files === null) { return []; } $images = []; while ($files->next()) { // Skip subfolders if ($files->type == 'folder') { continue; } $file = new \File($files->path, true); if (!$file->isImage) { continue; } $meta = \Frontend::getMetaData($files->meta, $metaLanguage); if (empty($meta)) { if ($metaIgnore) { continue; } elseif ($metaFallbackLanguage !== null) { $meta = \Frontend::getMetaData($files->meta, $metaFallbackLanguage); } } // Use the file name as title if none is given if ($meta['title'] == '') { $meta['title'] = specialchars($file->basename); } // Add the image $images[$files->path] = ['id' => $files->id, 'uuid' => $files->uuid, 'name' => $file->basename, 'singleSRC' => $files->path, 'alt' => $meta['title'], 'imageUrl' => $meta['link'], 'caption' => $meta['caption']]; } return $this->sortImages($images, $this->getMetaData(), $sorting); }
/** * 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) { if (!\Validator::isUuid($arrEnclosures[0])) { foreach (array('details', 'answer', 'text') as $key) { if (isset($objTemplate->{$key})) { $objTemplate->{$key} = '<p class="error">' . $GLOBALS['TL_LANG']['ERR']['version2format'] . '</p>'; } } } 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, true); $strHref = \Environment::get('request'); // Remove an existing file parameter (see #5683) if (preg_match('/(&(amp;)?|\\?)file=/', $strHref)) { $strHref = preg_replace('/(&(amp;)?|\\?)file=[^&]+/', '', $strHref); } $strHref .= (\Config::get('disableAlias') || 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, 'meta' => $arrMeta); } } $objTemplate->enclosure = $arrEnclosures; }
/** * Generate download attributes * @param IsotopeProduct * @return string */ public function generate(IsotopeProduct $objProduct, array $arrOptions = array()) { global $objPage; $arrFiles = deserialize($objProduct->{$this->field_name}); // Return if there are no files if (empty($arrFiles) || !is_array($arrFiles)) { return ''; } // Get the file entries from the database $objFiles = \FilesModel::findMultipleByIds($arrFiles); if (null === $objFiles) { return ''; } $file = \Input::get('file', true); // Send the file to the browser and do not send a 404 header (see #4632) if ($file != '' && !preg_match('/^meta(_[a-z]{2})?\\.txt$/', basename($file))) { while ($objFiles->next()) { if ($file == $objFiles->path || dirname($file) == $objFiles->path) { \Controller::sendFileToBrowser($file); } } $objFiles->reset(); } $files = array(); $auxDate = array(); $allowedDownload = trimsplit(',', strtolower($GLOBALS['TL_CONFIG']['allowedDownload'])); // Get all files while ($objFiles->next()) { // Continue if the files has been processed or does not exist if (isset($files[$objFiles->path]) || !file_exists(TL_ROOT . '/' . $objFiles->path)) { continue; } // Single files if ($objFiles->type == 'file') { $objFile = new \File($objFiles->path, true); if (!in_array($objFile->extension, $allowedDownload) || preg_match('/^meta(_[a-z]{2})?\\.txt$/', $objFile->basename)) { continue; } $arrMeta = \Frontend::getMetaData($objFiles->meta, $objPage->language); // Use the file name as title if none is given if ($arrMeta['title'] == '') { $arrMeta['title'] = specialchars(str_replace('_', ' ', preg_replace('/^[0-9]+_/', '', $objFile->filename))); } $strHref = \Environment::get('request'); // Remove an existing file parameter (see #5683) if (preg_match('/(&(amp;)?|\\?)file=/', $strHref)) { $strHref = preg_replace('/(&(amp;)?|\\?)file=[^&]+/', '', $strHref); } $strHref .= ($GLOBALS['TL_CONFIG']['disableAlias'] || strpos($strHref, '?') !== false ? '&' : '?') . 'file=' . \System::urlEncode($objFiles->path); // Add the image $files[$objFiles->path] = array('id' => $objFiles->id, 'uuid' => $objFiles->uuid, 'name' => $objFile->basename, 'title' => $arrMeta['title'], 'link' => $arrMeta['title'], 'caption' => $arrMeta['caption'], 'href' => $strHref, 'filesize' => \System::getReadableSize($objFile->filesize, 1), 'icon' => TL_ASSETS_URL . 'assets/contao/images/' . $objFile->icon, 'mime' => $objFile->mime, 'meta' => $arrMeta, 'extension' => $objFile->extension, 'path' => $objFile->dirname); $auxDate[] = $objFile->mtime; } else { $objSubfiles = \FilesModel::findByPid($objFiles->id); if ($objSubfiles === null) { continue; } while ($objSubfiles->next()) { // Skip subfolders if ($objSubfiles->type == 'folder') { continue; } $objFile = new \File($objSubfiles->path, true); if (!in_array($objFile->extension, $allowedDownload) || preg_match('/^meta(_[a-z]{2})?\\.txt$/', $objFile->basename)) { continue; } $arrMeta = \Frontend::getMetaData($objSubfiles->meta, $objPage->language); // Use the file name as title if none is given if ($arrMeta['title'] == '') { $arrMeta['title'] = specialchars(str_replace('_', ' ', preg_replace('/^[0-9]+_/', '', $objFile->filename))); } $strHref = \Environment::get('request'); // Remove an existing file parameter (see #5683) if (preg_match('/(&(amp;)?|\\?)file=/', $strHref)) { $strHref = preg_replace('/(&(amp;)?|\\?)file=[^&]+/', '', $strHref); } $strHref .= ($GLOBALS['TL_CONFIG']['disableAlias'] || strpos($strHref, '?') !== false ? '&' : '?') . 'file=' . \System::urlEncode($objSubfiles->path); // Add the image $files[$objSubfiles->path] = array('id' => $objSubfiles->id, 'uuid' => $objSubfiles->uuid, 'name' => $objFile->basename, 'title' => $arrMeta['title'], 'link' => $arrMeta['title'], 'caption' => $arrMeta['caption'], 'href' => $strHref, 'filesize' => \System::getReadableSize($objFile->filesize, 1), 'icon' => TL_ASSETS_URL . 'assets/contao/images/' . $objFile->icon, 'mime' => $objFile->mime, 'meta' => $arrMeta, 'extension' => $objFile->extension, 'path' => $objFile->dirname); $auxDate[] = $objFile->mtime; } } } // Sort array $sortBy = $arrOptions['sortBy'] ?: $this->sortBy; switch ($sortBy) { default: case 'name_asc': uksort($files, 'basename_natcasecmp'); break; case 'name_desc': uksort($files, 'basename_natcasercmp'); break; case 'date_asc': array_multisort($files, SORT_NUMERIC, $auxDate, SORT_ASC); break; case 'date_desc': array_multisort($files, SORT_NUMERIC, $auxDate, SORT_DESC); break; case 'custom': if ($objProduct->{$this->field_name . '_order'} != '') { $tmp = deserialize($objProduct->{$this->field_name . '_order'}); if (!empty($tmp) && is_array($tmp)) { // Remove all values $arrOrder = array_map(function () { }, array_flip($tmp)); // Move the matching elements to their position in $arrOrder foreach ($files as $k => $v) { if (array_key_exists($v['uuid'], $arrOrder)) { $arrOrder[$v['uuid']] = $v; unset($files[$k]); } } // Append the left-over files at the end if (!empty($files)) { $arrOrder = array_merge($arrOrder, array_values($files)); } // Remove empty (unreplaced) entries $files = array_values(array_filter($arrOrder)); unset($arrOrder); } } break; case 'random': shuffle($files); break; } $objTemplate = new \Isotope\Template('ce_downloads'); $objTemplate->class = $this->field_name; $objTemplate->files = array_values($files); return $objTemplate->parse(); }