/** * add uuids to tl_gallery_creator_pictures version added in 4.8.0 */ public static function addUuids() { // add field if (!\Database::getInstance()->fieldExists('uuid', 'tl_gallery_creator_pictures')) { \Database::getInstance()->query("ALTER TABLE `tl_gallery_creator_pictures` ADD `uuid` BINARY(16) NULL"); } $objDB = \Database::getInstance()->execute("SELECT * FROM tl_gallery_creator_pictures WHERE uuid IS NULL"); while ($objDB->next()) { if ($objDB->path == '') { continue; } if (is_file(TL_ROOT . '/' . $objDB->path)) { \Dbafs::addResource($objDB->path); } else { continue; } $oFile = new \File($objDB->path); $oFile->close(); $fileModel = $oFile->getModel(); if (\Validator::isUuid($fileModel->uuid)) { \Database::getInstance()->prepare("UPDATE tl_gallery_creator_pictures SET uuid=? WHERE id=?")->execute($fileModel->uuid, $objDB->id); $_SESSION["TL_CONFIRM"][] = "Added a valid file-uuid into tl_gallery_creator_pictures.uuid ID " . $objDB->id . ". Please check if all the galleries are running properly."; } } }
protected static function addCssFileToGroup($path, $groupId) { // create Files Model $objFile = new \File($path); if (!in_array(strtolower($objFile->extension), array('css', 'less'))) { return false; } $objFile->close(); $objFileModel = new ExtCssFileModel(); $objFileModel->pid = $groupId; $objFileModel->tstamp = time(); $objNextSorting = \Database::getInstance()->prepare("SELECT MAX(sorting) AS sorting FROM tl_extcss_file WHERE pid=?")->execute($groupId); $objFileModel->sorting = intval($objNextSorting->sorting) + 64; $objFileModel->src = $objFile->getModel()->uuid; $objFileModel->save(); return $objFileModel; }
/** * Get the meta file * * @return \File|null * * @throws \Exception */ public function getMetaFile() { $folder = $this->getAlbumFolder(); if ($folder === null) { return null; } $file = new \File($folder->path . '/' . $this->metaFileName . '.json'); // Synchronize the file with database if ($file->getModel() === null) { \Dbafs::addResource($file->path); } return $file; }
protected function createSingleFile($varValue) { if ($this->sourceDir === null || $this->targetDir === null || $varValue == '') { return false; } $objSourceDir = \FilesModel::findByUuid($this->sourceDir); if ($objSourceDir === null) { return false; } $objTargetDir = \FilesModel::findByUuid($this->targetDir); if ($objTargetDir === null) { return false; } $strRelFile = $objSourceDir->path . '/' . ltrim($varValue, '/'); if (is_dir(TL_ROOT . '/' . $strRelFile) || !file_exists(TL_ROOT . '/' . $strRelFile)) { return null; } $objFile = new \File($strRelFile); $objFile->copyTo($objTargetDir->path . '/' . $objFile->name); $objModel = $objFile->getModel(); return $objModel->uuid; }
/** * Generate the meta information for a given file * * @param $objFile * * @return array The meta information with i18n support */ protected function getMetaFromFile(\File $objFile) { global $objPage; $objModel = $objFile->getModel(); $arrMeta = $this->getMetaData($objModel->meta, $objPage->language); if (empty($arrMeta) && $objPage->rootFallbackLanguage !== null) { $arrMeta = $this->getMetaData($objModel->meta, $objPage->rootFallbackLanguage); } // Use the file name as title if none is given if ($arrMeta['title'] == '') { $arrMeta['title'] = specialchars($objFile->basename); } return $arrMeta; }
protected function createEnclosures(&$objItem) { if ($this->sourceDir === null || $this->targetDir === null) { return false; } $objSourceDir = \FilesModel::findByUuid($this->sourceDir); if ($objSourceDir === null) { return false; } $objTargetDir = \FilesModel::findByUuid($this->targetDir); if ($objTargetDir === null) { return false; } $arrSource = deserialize($objItem->enclosure, true); $arrTarget = array(); foreach ($arrSource as $strFile) { $strRelFile = $objSourceDir->path . '/' . ltrim($strFile, '/'); if (is_dir(TL_ROOT . '/' . $strRelFile) || !file_exists(TL_ROOT . '/' . $strRelFile)) { continue; } $objFile = new \File($strRelFile); $objFile->copyTo($objTargetDir->path . '/' . $objFile->name); $objModel = $objFile->getModel(); $arrTarget[] = $objModel->uuid; } if (!empty($arrTarget)) { $objItem->addEnclosure = true; $objItem->enclosure = $arrTarget; } }
/** * Upload a file, store to $strUploadFolder and create database entry * * @param $arrFile The $_FILES array * @param $strUploadFolder The upload target folder within contao files folder * @param $strField * * @return array|bool Returns array with file information on success. Returns false if no valid file, file cannot be moved or destination lies outside the * contao upload directory. */ protected function uploadFile($arrFile, $strUploadFolder, $strField) { if (!$arrFile['error']) { $error = false; $strTempFile = $arrFile['tmp_name']; $arrPath = pathinfo($arrFile['name']); $strTargetFileName = standardize($arrPath['filename']) . '_' . uniqid() . '.' . strtolower($arrPath['extension']); $strTargetFile = $strUploadFolder . '/' . $strTargetFileName; if (!move_uploaded_file($strTempFile, TL_ROOT . '/' . $strTargetFile)) { $error =& $GLOBALS['TL_LANG']['ERR']['moveUploadFile']; } $arrData = array('filename' => $strTargetFileName, 'filenameOrig' => $arrFile['name']); if (!$error) { try { // add db record $objFile = new \File($strTargetFile); $objFile->close(); $strUuid = $objFile->getModel()->uuid; } catch (\InvalidArgumentException $e) { $error =& $GLOBALS['TL_LANG']['ERR']['outsideUploadDirectory']; } } if (!$error && $objFile instanceof \File) { $error = $this->validateUpload($objFile); // upload_path_callback if (is_array($this->validate_upload_callback)) { foreach ($this->validate_upload_callback as $callback) { if (!class_exists($callback[0])) { continue; } $objCallback = \System::importStatic($callback[0]); if (!method_exists($objCallback, $callback[1])) { continue; } if ($errorCallback = $objCallback->{$callback[1]}($objFile, $this)) { $error = $errorCallback; break; // stop validation on first error } } } } if ($error === false && ($arrInfo = $this->objUploader->prepareFile($strUuid)) !== false) { $arrData = array_merge($arrData, $arrInfo); return $arrData; } $arrData['error'] = $error; // remove invalid files from tmp folder if ($objFile instanceof \File) { $objFile->delete(); } return $arrData; } return array('error' => $arrFile['error'], 'filenameOrig' => $arrFile['name']); }