/** * Get the parent album trail as an array * @param $AlbumId * @return array */ public static function getParentAlbums($AlbumId) { $arrParentAlbums = array(); $objAlb = \GalleryCreatorAlbumsModel::findByPk($AlbumId); if ($objAlb !== null) { $pid = $objAlb->pid; while ($pid > 0) { $parentAlb = \GalleryCreatorAlbumsModel::findByPk($pid); if ($parentAlb !== null) { $arrParentAlbums[] = $parentAlb->id; $pid = $parentAlb->pid; } } } return $arrParentAlbums; }
/** * Generate module */ protected function compile() { // use a private template if (TL_MODE == 'FE' && $this->gc_template != '') { $this->Template->style = count($this->arrStyle) ? implode(' ', $this->arrStyle) : ''; $this->Template->cssID = strlen($this->cssID[0]) ? ' id="' . $this->cssID[0] . '"' : ''; $this->Template->class = trim('mod_' . $this->type . ' ' . $this->cssID[1]); } // check for excluded albums in the module settings $arrExcludedAlbums = deserialize($this->gc_excludedAlbums); $strExcludedAlbums = is_array($arrExcludedAlbums) && !empty($arrExcludedAlbums) ? implode(',', $arrExcludedAlbums) : '0'; // redirect to the detailview if there is only 1 album if (!\Input::get('items') && $this->gc_redirectSingleAlb) { $objAlbum = $this->Database->prepare('SELECT * FROM tl_gallery_creator_albums WHERE published=? AND id NOT IN (' . $strExcludedAlbums . ')')->execute('1'); if ($objAlbum->numRows === 1) { \Input::setGet('items', $objAlbum->alias); } } if (\Input::get('items')) { $this->strAlbumalias = \Input::get('items'); // authenticate user if album is protected $this->authenticate($this->strAlbumalias); // get the album id from the album alias $objAlbum = $this->Database->prepare('SELECT id FROM tl_gallery_creator_albums WHERE alias=?')->execute($this->strAlbumalias); $this->intAlbumId = $objAlbum->id; } $switch = strlen(\Input::get('items')) ? 'detailview' : 'albumlisting'; $switch = strlen(\Input::get('jw_imagerotator')) ? 'jw_imagerotator' : $switch; switch ($switch) { case 'albumlisting': // get all published albums $arrAllowedAlbums = array(); if ($this->gc_hierarchicalOutput) { $objAlbum = $this->Database->prepare('SELECT * FROM tl_gallery_creator_albums WHERE published=? AND pid=? AND id NOT IN (' . $strExcludedAlbums . ')')->execute('1', '0'); } else { $objAlbum = $this->Database->prepare('SELECT * FROM tl_gallery_creator_albums WHERE published=? AND id NOT IN (' . $strExcludedAlbums . ')')->execute('1'); } while ($objAlbum->next()) { if (TL_MODE == 'FE' && $objAlbum->protected == true) { $this->import('FrontendUser', 'User'); // check if the frontend user is allowed if (FE_USER_LOGGED_IN && is_array(unserialize($this->User->allGroups))) { if (array_intersect(unserialize($this->User->allGroups), unserialize($objAlbum->groups))) { // user is allowed $arrAllowedAlbums[] = $objAlbum->id; } } continue; } // album is not protected $arrAllowedAlbums[] = $objAlbum->id; } // pagination settings $limit = $this->gc_AlbumsPerPage; if ($limit > 0) { $page = \Input::get('page') ? \Input::get('page') : 1; $offset = ($page - 1) * $limit; $itemsTotal = count($arrAllowedAlbums); // add pagination menu $numberOfLinks = $this->gc_PaginationNumberOfLinks < 1 ? 7 : $this->gc_PaginationNumberOfLinks; $objPagination = new \Pagination($itemsTotal, $limit, $numberOfLinks); $this->Template->pagination = $objPagination->generate("\n "); } // get all published albums $objAlbum = $this->Database->prepare('SELECT * FROM tl_gallery_creator_albums WHERE id IN(' . implode(",", $arrAllowedAlbums) . ') ORDER BY sorting ASC'); if ($limit > 0) { $objAlbum->limit($limit, $offset); } $objAlbum = $objAlbum->execute('1', '0'); // album array $arrAlbums = array(); while ($objAlbum->next()) { $arrAlbums[$objAlbum->id] = GcHelpers::getAlbumInformationArray($objAlbum->id, $this); } $this->Template->imagemargin = $this->generateMargin(unserialize($this->gc_imagemargin_albumlisting)); $this->Template->arrAlbums = $arrAlbums; $this->getAlbumTemplateVars($objAlbum->id); // Call gcGenerateFrontendTemplateHook $this->Template = $this->callGcGenerateFrontendTemplateHook($this); break; case 'detailview': $objAlbum = \GalleryCreatorAlbumsModel::findByPk($this->intAlbumId); $published = $objAlbum->published ? true : false; if ($published) { $published = in_array($this->intAlbumId, explode(',', $strExcludedAlbums)) ? false : true; } // for security reasons... if (!$published) { die("Gallery with alias " . \Input::get('items') . " is either not published or not available!!!"); } // generate the subalbum array if ($this->gc_hierarchicalOutput) { $arrSubalbums = GcHelpers::getSubalbumsInformationArray($this->intAlbumId, $this); $this->Template->subalbums = count($arrSubalbums) ? $arrSubalbums : null; } // pagination settings $limit = $this->gc_ThumbsPerPage; if ($limit > 0) { $page = \Input::get('page') ? \Input::get('page') : 1; $offset = ($page - 1) * $limit; // count albums $objTotal = $this->Database->prepare('SELECT COUNT(id) as itemsTotal FROM tl_gallery_creator_pictures WHERE published=? AND pid=?')->execute('1', $this->intAlbumId); $itemsTotal = $objTotal->itemsTotal; // add pagination menu $numberOfLinks = $this->gc_PaginationNumberOfLinks < 1 ? 7 : $this->gc_PaginationNumberOfLinks; $objPagination = new \Pagination($itemsTotal, $limit, $numberOfLinks); $this->Template->pagination = $objPagination->generate("\n "); } $objPictures = $this->Database->prepare('SELECT * FROM tl_gallery_creator_pictures WHERE published=? AND pid=? ORDER BY sorting'); if ($limit > 0) { $objPictures->limit($limit, $offset); } $objPictures = $objPictures->execute('1', $this->intAlbumId); $arrPictures = array(); while ($objPictures->next()) { // picture array $arrPictures[$objPictures->id] = GcHelpers::getPictureInformationArray($objPictures->id, $this); } // add picture array to the template $this->Template->arrPictures = $arrPictures; // add some other useful template vars $this->getAlbumTemplateVars($this->intAlbumId); // init the counter $this->initCounter($this->intAlbumId); // Call gcGenerateFrontendTemplateHook $this->Template = $this->callGcGenerateFrontendTemplateHook($this); break; case 'jw_imagerotator': header("content-type:text/xml;charset=utf-8"); echo $this->getJwImagerotatorXml($this->strAlbumalias); exit; break; } //end switch }
/** * Returns the path to the preview-thumbnail of an album * @param $intAlbumId * @return array */ public function getAlbumPreviewThumb($intAlbumId) { $thumbSRC = $this->defaultThumb; // Check for an alternate thumbnail if (\Config::get('gc_error404_thumb') !== '') { $objFile = \FilesModel::findByUuid(\Config::get('gc_error404_thumb')); if ($objFile !== null) { if (\Validator::isUuid(\Config::get('gc_error404_thumb'))) { if (is_file(TL_ROOT . '/' . $objFile->path)) { $thumbSRC = $objFile->path; } } } } // Predefine thumb $arrThumb = array('name' => basename($thumbSRC), 'path' => $thumbSRC); $objAlb = \GalleryCreatorAlbumsModel::findByPk($intAlbumId); if ($objAlb->thumb !== null) { $objPreviewThumb = \GalleryCreatorPicturesModel::findByPk($objAlb->thumb); } else { $objPreviewThumb = \GalleryCreatorPicturesModel::findOneByPid($intAlbumId); } if ($objPreviewThumb !== null) { $oFile = \FilesModel::findByUuid($objPreviewThumb->uuid); if ($oFile !== null) { if (is_file(TL_ROOT . '/' . $oFile->path)) { $arrThumb = array('name' => basename($oFile->path), 'path' => $oFile->path); } } } return $arrThumb; }
/** * Generate module */ protected function compile() { // Get the album object $objAlbum = \GalleryCreatorAlbumsModel::findByPk($this->intAlbumId); // Init the counter ContentGalleryCreator::initCounter($this->intAlbumId); // Pagination settings $limit = $this->gc_ThumbsPerPage; if ($limit > 0) { $page = \Input::get('page') ? \Input::get('page') : 1; $offset = ($page - 1) * $limit; // Count pictures $objPictures = $this->Database->prepare('SELECT * FROM tl_gallery_creator_pictures WHERE published=? AND pid=?')->execute('1', $this->intAlbumId); $itemsTotal = $objPictures->numRows; // Create the pagination menu $numberOfLinks = $this->gc_PaginationNumberOfLinks < 1 ? 7 : $this->gc_PaginationNumberOfLinks; $objPagination = new \Pagination($itemsTotal, $limit, $numberOfLinks); $this->Template->pagination = $objPagination->generate("\n "); } // Picture sorting $str_sorting = $this->gc_picture_sorting == '' || $this->gc_picture_sorting_direction == '' ? 'sorting ASC' : $this->gc_picture_sorting . ' ' . $this->gc_picture_sorting_direction; // Sort by name is done below $str_sorting = str_replace('name', 'id', $str_sorting); $objPictures = $this->Database->prepare('SELECT * FROM tl_gallery_creator_pictures WHERE published=? AND pid=? ORDER BY ' . $str_sorting); if ($limit > 0) { $objPictures->limit($limit, $offset); } $objPictures = $objPictures->execute('1', $this->intAlbumId); // Build up $arrPictures $arrPictures = array(); $auxBasename = array(); while ($objPictures->next()) { $objFilesModel = \FilesModel::findByUuid($objPictures->uuid); $basename = 'undefined'; if ($objFilesModel !== null) { $basename = $objFilesModel->name; } $auxBasename[] = $basename; $arrPictures[$objPictures->id] = GcHelpers::getPictureInformationArray($objPictures->id, $this); } // Sort by basename if ($this->gc_picture_sorting == 'name') { if ($this->gc_picture_sorting_direction == 'ASC') { array_multisort($arrPictures, SORT_STRING, $auxBasename, SORT_ASC); } else { array_multisort($arrPictures, SORT_STRING, $auxBasename, SORT_DESC); } } $arrPictures = array_values($arrPictures); // Store $arrPictures in the template variable $this->Template->arrPictures = $arrPictures; // Generate other template variables $this->getAlbumTemplateVars($this->intAlbumId); // HOOK: modify the page or template object if (isset($GLOBALS['TL_HOOKS']['gc_generateFrontendTemplate']) && is_array($GLOBALS['TL_HOOKS']['gc_generateFrontendTemplate'])) { foreach ($GLOBALS['TL_HOOKS']['gc_generateFrontendTemplate'] as $callback) { $this->import($callback[0]); $this->Template = $this->{$callback}[0]->{$callback}[1]($this, $objAlbum); } } }
/** * generate an albumalias based on the albumname and create a directory of the same name * and register the directory in tl files * @param $strAlias * @param \Contao\DataContainer $dc * @return mixed|string */ public function saveCbGenerateAlias($strAlias, \Contao\DataContainer $dc) { $blnDoNotCreateDir = false; // get current row $objAlbum = GalleryCreatorAlbumsModel::findByPk($dc->id); if ($objAlbum === null) { return; } // Save assigned Dir if it was defined. if ($this->Input->post('FORM_SUBMIT') && strlen($this->Input->post('assignedDir'))) { $objAlbum->assignedDir = $this->Input->post('assignedDir'); $objAlbum->save(); $blnDoNotCreateDir = true; } $strAlias = standardize($strAlias); // if there isn't an existing albumalias generate one from the albumname if (!strlen($strAlias)) { $strAlias = standardize($dc->activeRecord->name); } // limit alias to 50 characters $strAlias = substr($strAlias, 0, 43); // remove invalid characters $strAlias = preg_replace("/[^a-z0-9\\_\\-]/", "", $strAlias); // if alias already exists add the album-id to the alias $objAlb = $this->Database->prepare('SELECT * FROM tl_gallery_creator_albums WHERE id!=? AND alias=?')->execute($dc->activeRecord->id, $strAlias); if ($objAlb->numRows) { $strAlias = 'id-' . $dc->id . '-' . $strAlias; } // Create default upload folder if ($blnDoNotCreateDir === false) { // create the new folder and register it in tl_files $objFolder = new Folder($this->uploadPath . '/' . $strAlias); $oFolder = Dbafs::addResource($objFolder->path, true); $objAlbum->assignedDir = $oFolder->uuid; $objAlbum->save(); // Important Input::setPost('assignedDir', String::binToUuid($objAlbum->assignedDir)); } return $strAlias; }
/** * initCounter * * @param integer * @return string */ public static function initCounter($intAlbumId) { if (preg_match('/bot|sp[iy]der|crawler|lib(?:cur|www)|search|archive/i', $_SERVER['HTTP_USER_AGENT'])) { // do not count spiders/bots return; } if (TL_MODE == 'FE') { $objAlbum = \GalleryCreatorAlbumsModel::findByPk($intAlbumId); if (strpos($objAlbum->visitors_details, $_SERVER['REMOTE_ADDR'])) { // return if the visitor is allready registered return; } $arrVisitors = deserialize($objAlbum->visitors_details, true); // keep visiors data in the db unless 50 other users have visited the album if (count($arrVisitors) == 50) { // slice the last position $arrVisitors = array_slice($arrVisitors, 0, count($arrVisitors) - 1); } //build up the array $newVisitor = array($_SERVER['REMOTE_ADDR'] => array('ip' => $_SERVER['REMOTE_ADDR'], 'pid' => $intAlbumId, 'user_agent' => $_SERVER['HTTP_USER_AGENT'], 'tstamp' => time(), 'url' => \Environment::get('request'))); if (!empty($arrVisitors)) { // insert the element to the beginning of the array array_unshift($arrVisitors, $newVisitor); } else { $arrVisitors[] = array($_SERVER['REMOTE_ADDR'] => $newVisitor); } // update database $objAlbum->visitors = $objAlbum->visitors += 1; $objAlbum->visitors_details = serialize($arrVisitors); $objAlbum->save(); } }
/** * ondelete-callback * prevents deleting images by unauthorised users */ public function ondeleteCb(\Contao\DC_Table $dc) { $objImg = GalleryCreatorPicturesModel::findByPk($dc->id); $pid = $objImg->pid; if ($objImg->owner == $this->User->id || $this->User->isAdmin || $GLOBALS['TL_CONFIG']['gc_disable_backend_edit_protection']) { // Datensatz löschen $uuid = $objImg->uuid; $objImg->delete(); //Nur Bilder innerhalb des gallery_creator_albums und wenn sie nicht in einem anderen Datensatz noch Verwendung finden, werden vom Server geloescht // Prüfen, ob das Bild noch mit einem anderen Datensatz verknüpft ist $objPictureModel = GalleryCreatorPicturesModel::findByUuid($uuid); if ($objPictureModel === null) { // Wenn nein darf gelöscht werden... $oFile = FilesModel::findByUuid($uuid); $objAlbum = GalleryCreatorAlbumsModel::findByPk($pid); $oFolder = FilesModel::findByUuid($objAlbum->assignedDir); // Bild nur löschen, wenn es im Verzeichnis liegt, das dem Album zugewiesen ist if ($oFile !== null && strstr($oFile->path, $oFolder->path)) { // delete file from filesystem $file = new File($oFile->path, true); $file->delete(); } } } elseif (!$this->User->isAdmin && $objImg->owner != $this->User->id) { $this->log('Datensatz mit ID ' . $dc->id . ' wurde vom Benutzer mit ID ' . $this->User->id . ' versucht aus tl_gallery_creator_pictures zu loeschen.', __METHOD__, TL_ERROR); Message::addError('No permission to delete picture with ID ' . $dc->id . '.'); $this->redirect('contao/main.php?do=error'); } }
/** * return the level of an album or subalbum (level_0, level_1, level_2,...) * @param integer * @return integer */ public static function getAlbumLevel($pid) { $level = 0; if ($pid == '0') { return $level; } $hasParent = true; while ($hasParent) { $level++; $objAlbum = \GalleryCreatorAlbumsModel::findByPk($pid); if ($objAlbum->pid < 1) { $hasParent = false; } $pid = $objAlbum->pid; } return $level; }