/** * Initialize this singleton. */ protected function init() { $this->storageFolder = self::validatePath(Config::get('cms.storage.media.folder', 'media'), true); $this->storagePath = rtrim(Config::get('cms.storage.media.path', '/storage/app/media'), '/'); if (!starts_with($this->storagePath, ['//', 'http://', 'https://'])) { $this->storagePath = Request::getBasePath() . $this->storagePath; } $this->ignoreNames = Config::get('cms.storage.media.ignore', FileDefinitions::get('ignoreFiles')); $this->storageFolderNameLength = strlen($this->storageFolder); }
/** * Initialize this singleton. */ protected function init() { $this->storageFolder = self::validatePath(Config::get('cms.storage.media.folder', 'media'), true); $this->storagePath = rtrim(Config::get('cms.storage.media.path', '/storage/app/media'), '/'); if (!preg_match("/(\\/\\/|http|https)/", $this->storagePath)) { $this->storagePath = Request::getBasePath() . $this->storagePath; } $this->ignoreNames = Config::get('cms.storage.media.ignore', FileDefinitions::get('ignoreFiles')); $this->storageFolderNameLength = strlen($this->storageFolder); }
/** * Checks the current request to see if it is a postback containing a file upload * for this particular widget. */ protected function checkUploadPostback() { $fileName = null; try { $uploadedFile = Input::file('file_data'); if (!is_object($uploadedFile)) { return; } $fileName = $uploadedFile->getClientOriginalName(); // Don't rely on Symfony's mime guessing implementation, it's not accurate enough. // Use the simple extension validation. $allowedAssetTypes = FileDefinitions::get('assetExtensions'); $maxSize = UploadedFile::getMaxFilesize(); if ($uploadedFile->getSize() > $maxSize) { throw new ApplicationException(Lang::get('cms::lang.asset.too_large', ['max_size ' => File::sizeToString($maxSize)])); } $ext = strtolower(pathinfo($uploadedFile->getClientOriginalName(), PATHINFO_EXTENSION)); if (!in_array($ext, $allowedAssetTypes)) { throw new ApplicationException(Lang::get('cms::lang.asset.type_not_allowed', ['allowed_types' => implode(', ', $allowedAssetTypes)])); } if (!$uploadedFile->isValid()) { throw new ApplicationException(Lang::get('cms::lang.asset.file_not_valid')); } $uploadedFile->move($this->getCurrentPath(), $uploadedFile->getClientOriginalName()); die('success'); } catch (Exception $ex) { $message = $fileName !== null ? Lang::get('cms::lang.asset.error_uploading_file', ['name' => $fileName, 'error' => $ex->getMessage()]) : $ex->getMessage(); die($message); } }
/** * Returns the file type by its name. * The known file types are: image, video, audio, document * @return string Returns the file type or NULL if the item is a folder. */ public function getFileType() { if (!$this->isFile()) { return null; } if (!self::$imageExtensions) { self::$imageExtensions = Config::get('cms.storage.media.imageExtensions', FileDefinitions::get('imageExtensions')); self::$videoExtensions = Config::get('cms.storage.media.videoExtensions', FileDefinitions::get('videoExtensions')); self::$audioExtensions = Config::get('cms.storage.media.audioExtensions', FileDefinitions::get('audioExtensions')); } $extension = pathinfo($this->path, PATHINFO_EXTENSION); if (!strlen($extension)) { return self::FILE_TYPE_DOCUMENT; } if (in_array($extension, self::$imageExtensions)) { return self::FILE_TYPE_IMAGE; } if (in_array($extension, self::$videoExtensions)) { return self::FILE_TYPE_VIDEO; } if (in_array($extension, self::$audioExtensions)) { return self::FILE_TYPE_AUDIO; } return self::FILE_TYPE_DOCUMENT; }
/** * Check for blocked / unsafe file extensions * @param string * @return bool */ protected function validateFileType($name) { $extension = strtolower(File::extension($name)); $blockedFileTypes = FileDefinitions::get('blockedExtensions'); if (in_array($extension, $blockedFileTypes)) { return false; } return true; }
/** * Returns the specified accepted file types, or the default * based on the mode. Image mode will return: * - jpg,jpeg,bmp,png,gif,svg * @return string */ public function getAcceptedFileTypes($includeDot = false) { $types = $this->fileTypes; if ($types === false) { $isImage = starts_with($this->getDisplayMode(), 'image'); $types = implode(',', FileDefinitions::get($isImage ? 'imageExtensions' : 'defaultExtensions')); } if (!$types || $types == '*') { return null; } if (!is_array($types)) { $types = explode(',', $types); } $types = array_map(function ($value) use($includeDot) { $value = trim($value); if (substr($value, 0, 1) == '.') { $value = substr($value, 1); } if ($includeDot) { $value = '.' . $value; } return $value; }, $types); return implode(',', $types); }
protected function checkUploadPostback() { $fileName = null; $quickMode = false; if ((!($uniqueId = Request::header('X-OCTOBER-FILEUPLOAD')) || $uniqueId != $this->getId()) && !($quickMode = post('X_OCTOBER_MEDIA_MANAGER_QUICK_UPLOAD'))) { return; } try { if (!Input::hasFile('file_data')) { throw new ApplicationException('File missing from request'); } $uploadedFile = Input::file('file_data'); $fileName = $uploadedFile->getClientOriginalName(); /* * Convert uppcare case file extensions to lower case */ $extension = strtolower($uploadedFile->getClientOriginalExtension()); $fileName = File::name($fileName) . '.' . $extension; /* * Check for unsafe file extensions */ $blockedFileTypes = FileDefinitions::get('blockedExtensions'); if (in_array($extension, $blockedFileTypes)) { throw new ApplicationException(Lang::get('cms::lang.media.type_blocked')); } /* * File name contains non-latin characters, attempt to slug the value */ if (!$this->validateFileName($fileName)) { $fileNameClean = $this->cleanFileName(File::name($fileName)); $fileName = $fileNameClean . '.' . $extension; } // See mime type handling in the asset manager if (!$uploadedFile->isValid()) { throw new ApplicationException($uploadedFile->getErrorMessage()); } $path = $quickMode ? '/uploaded-files' : Input::get('path'); $path = MediaLibrary::validatePath($path); $filePath = $path . '/' . $fileName; MediaLibrary::instance()->put($filePath, File::get($uploadedFile->getRealPath())); Response::json(['link' => MediaLibrary::url($filePath), 'result' => 'success'])->send(); } catch (Exception $ex) { Response::json($ex->getMessage(), 400)->send(); } exit; }