/** * onload-callback * initiate the fileupload */ public function onloadCbFileupload() { if (Input::get('mode') != 'fileupload') { return; } // Load language file $this->loadLanguageFile('tl_files'); // Album ID $intAlbumId = Input::get('id'); // Save uploaded files in $_FILES['file'] $strName = 'file'; // Get the album object $blnNoAlbum = false; $objAlb = GalleryCreatorAlbumsModel::findById($intAlbumId); if ($objAlb === null) { Message::addError('Album with ID ' . $intAlbumId . ' does not exist.'); $blnNoAlbum = true; } // Check for a valid upload directory $blnNoUploadDir = false; $objUploadDir = FilesModel::findByUuid($objAlb->assignedDir); if ($objUploadDir === null || !is_dir(TL_ROOT . '/' . $objUploadDir->path)) { Message::addError('No upload directory defined in the album settings!'); $blnNoUploadDir = true; } // Exit if there is no upload or the upload directory is missing if (!is_array($_FILES[$strName]) || $blnNoUploadDir || $blnNoAlbum) { return; } // Call the uploader script $arrUpload = \GalleryCreator\GcHelpers::fileupload($intAlbumId, $strName); foreach ($arrUpload as $strFileSrc) { // Add new datarecords into tl_gallery_creator_pictures \GalleryCreator\GcHelpers::createNewImage($objAlb->id, $strFileSrc); } // Do not exit script if html5_uploader is selected and Javascript is disabled if (!Input::post('submit')) { exit; } }
/** * move uploaded file to the album directory * * @param $intAlbumId * @param string $strName * @return array */ public static function fileupload($intAlbumId, $strName = 'file') { $blnIsError = false; // Get the album object $objAlb = \GalleryCreatorAlbumsModel::findById($intAlbumId); if ($objAlb === null) { $blnIsError = true; \Message::addError('Album with ID ' . $intAlbumId . ' does not exist.'); } // Check for a valid upload directory $objUploadDir = \FilesModel::findByUuid($objAlb->assignedDir); if ($objUploadDir === null || !is_dir(TL_ROOT . '/' . $objUploadDir->path)) { $blnIsError = true; \Message::addError('No upload directory defined in the album settings!'); } // Check if there are some files in $_FILES if (!is_array($_FILES[$strName])) { $blnIsError = true; \Message::addError('No Files selected for the uploader.'); } if ($blnIsError) { return array(); } // Adapt $_FILES if files are loaded up by jumploader (java applet) if (!is_array($_FILES[$strName]['name'])) { $arrFile = array('name' => $_FILES[$strName]['name'], 'type' => $_FILES[$strName]['type'], 'tmp_name' => $_FILES[$strName]['tmp_name'], 'error' => $_FILES[$strName]['error'], 'size' => $_FILES[$strName]['size']); unset($_FILES); //rebuild $_FILES for the Contao FileUpload class $_FILES[$strName]['name'][0] = $arrFile['name']; $_FILES[$strName]['type'][0] = $arrFile['type']; $_FILES[$strName]['tmp_name'][0] = $arrFile['tmp_name']; $_FILES[$strName]['error'][0] = $arrFile['error']; $_FILES[$strName]['size'][0] = $arrFile['size']; } // Do not overwrite files of the same filename $intCount = count($_FILES[$strName]['name']); for ($i = 0; $i < $intCount; $i++) { if (strlen($_FILES[$strName]['name'][$i])) { // Generate unique filename $_FILES[$strName]['name'][$i] = basename(self::generateUniqueFilename($objUploadDir->path . '/' . $_FILES[$strName]['name'][$i])); } } // Resize image if feature is enabled if (\Input::post('img_resolution') > 1) { \Config::set('imageWidth', \Input::post('img_resolution')); \Config::set('jpgQuality', \Input::post('img_quality')); } else { \Config::set('maxImageWidth', 999999999); } // Call the Contao FileUpload class $objUpload = new \FileUpload(); $objUpload->setName($strName); $arrUpload = $objUpload->uploadTo($objUploadDir->path); foreach ($arrUpload as $strFileSrc) { // Store file in tl_files \Dbafs::addResource($strFileSrc); } return $arrUpload; }
/** * @param integer * @param string * Bilder aus Verzeichnis auf dem Server in Album einlesen */ public static function importFromFilesystem($intAlbumId, $strMultiSRC) { $images = array(); $objFilesModel = \FilesModel::findMultipleByUuids(explode(',', $strMultiSRC)); if ($objFilesModel === null) { return; } while ($objFilesModel->next()) { // Continue if the file has been processed or does not exist if (isset($images[$objFilesModel->path]) || !file_exists(TL_ROOT . '/' . $objFilesModel->path)) { continue; } // If item is a file, then store it in the array if ($objFilesModel->type == 'file') { $objFile = new \File($objFilesModel->path); if ($objFile->isGdImage) { $images[$objFile->path] = array('uuid' => $objFilesModel->uuid, 'basename' => $objFile->basename, 'path' => $objFile->path); } } else { // If it is a directory, then store its files in the array $objSubfilesModel = \FilesModel::findMultipleFilesByFolder($objFilesModel->path); if ($objSubfilesModel === null) { continue; } while ($objSubfilesModel->next()) { // Skip subfolders if ($objSubfilesModel->type == 'folder' || !is_file(TL_ROOT . '/' . $objSubfilesModel->path)) { continue; } $objFile = new \File($objSubfilesModel->path); if ($objFile->isGdImage) { $images[$objFile->path] = array('uuid' => $objSubfilesModel->uuid, 'basename' => $objFile->basename, 'path' => $objFile->path); } } } } if (count($images)) { $arrPictures = array('uuid' => array(), 'path' => array(), 'basename' => array()); $objPictures = \Database::getInstance()->prepare('SELECT * FROM tl_gallery_creator_pictures WHERE pid=?')->execute($intAlbumId); $arrPictures['uuid'] = $objPictures->fetchEach('uuid'); $arrPictures['path'] = $objPictures->fetchEach('path'); foreach ($arrPictures['path'] as $path) { $arrPictures['basename'][] = basename($path); } $objAlb = \GalleryCreatorAlbumsModel::findById($intAlbumId); foreach ($images as $image) { // Prevent duplicate entries if (in_array($image['uuid'], $arrPictures['uuid'])) { continue; } // Prevent duplicate entries if (in_array($image['basename'], $arrPictures['basename'])) { continue; } \Input::setGet('importFromFilesystem', 'true'); if ($GLOBALS['TL_CONFIG']['gc_album_import_copy_files']) { $strSource = $image['path']; // Get the album upload directory $objFolderModel = \FilesModel::findByUuid($objAlb->assignedDir); $errMsg = 'Aborted import process, because there is no upload folder assigned to the album with ID ' . $objAlb->id . '.'; if ($objFolderModel === null) { die($errMsg); } if ($objFolderModel->type != 'folder') { die($errMsg); } if (!is_dir(TL_ROOT . '/' . $objFolderModel->path)) { die($errMsg); } $strDestination = self::generateUniqueFilename($objFolderModel->path . '/' . basename($strSource)); if (is_file(TL_ROOT . '/' . $strSource)) { //copy Image to the upload folder $objFile = new \File($strSource); $objFile->copyTo($strDestination); \Dbafs::addResource($strSource); } self::createNewImage($objAlb->id, $strDestination); } else { self::createNewImage($objAlb->id, $image['path']); } } } }