diff --git a/modules/backend/widgets/MediaManager.php b/modules/backend/widgets/MediaManager.php index b3a841905..2d35b29bc 100644 --- a/modules/backend/widgets/MediaManager.php +++ b/modules/backend/widgets/MediaManager.php @@ -40,6 +40,11 @@ class MediaManager extends WidgetBase protected $brokenImageHash = null; + /** + * @var boolean Determines whether the widget is in readonly mode or not + */ + public $readOnly = false; + /** * @var boolean Determines whether the bottom toolbar is visible. */ @@ -53,9 +58,10 @@ class MediaManager extends WidgetBase /** * Constructor. */ - public function __construct($controller, $alias) + public function __construct($controller, $alias, $readOnly = false) { $this->alias = $alias; + $this->readOnly = $readOnly; parent::__construct($controller, []); @@ -73,6 +79,16 @@ class MediaManager extends WidgetBase $this->addJs('js/mediamanager-browser-min.js', 'core'); } + /** + * Abort the request with an access-denied code if readOnly mode is active + */ + protected function abortIfReadOnly() + { + if ($this->readOnly) { + abort(403); + } + } + /** * Renders the widget. * @return string @@ -230,6 +246,8 @@ class MediaManager extends WidgetBase public function onDeleteItem() { + $this->abortIfReadOnly(); + $paths = Input::get('paths'); if (!is_array($paths)) { @@ -290,6 +308,8 @@ class MediaManager extends WidgetBase public function onLoadRenamePopup() { + $this->abortIfReadOnly(); + $path = Input::get('path'); $path = MediaLibrary::validatePath($path); @@ -303,6 +323,8 @@ class MediaManager extends WidgetBase public function onApplyName() { + $this->abortIfReadOnly(); + $newName = trim(Input::get('name')); if (!strlen($newName)) { throw new ApplicationException(Lang::get('cms::lang.asset.name_cant_be_empty')); @@ -352,6 +374,8 @@ class MediaManager extends WidgetBase public function onCreateFolder() { + $this->abortIfReadOnly(); + $name = trim(Input::get('name')); if (!strlen($name)) { throw new ApplicationException(Lang::get('cms::lang.asset.name_cant_be_empty')); @@ -395,6 +419,8 @@ class MediaManager extends WidgetBase public function onLoadMovePopup() { + $this->abortIfReadOnly(); + $exclude = Input::get('exclude', []); if (!is_array($exclude)) { throw new ApplicationException('Invalid input data'); @@ -425,6 +451,8 @@ class MediaManager extends WidgetBase public function onMoveItems() { + $this->abortIfReadOnly(); + $dest = trim(Input::get('dest')); if (!strlen($dest)) { throw new ApplicationException(Lang::get('backend::lang.media.please_select_move_dest')); @@ -498,6 +526,8 @@ class MediaManager extends WidgetBase public function onLoadImageCropPopup() { + $this->abortIfReadOnly(); + $path = Input::get('path'); $path = MediaLibrary::validatePath($path); $cropSessionKey = md5(FormHelper::getSessionKey()); @@ -521,6 +551,8 @@ class MediaManager extends WidgetBase public function onEndCroppingSession() { + $this->abortIfReadOnly(); + $cropSessionKey = Input::get('cropSessionKey'); if (!preg_match('/^[0-9a-z]+$/', $cropSessionKey)) { throw new ApplicationException('Invalid input data'); @@ -531,6 +563,8 @@ class MediaManager extends WidgetBase public function onCropImage() { + $this->abortIfReadOnly(); + $imageSrcPath = trim(Input::get('img')); $selectionData = Input::get('selection'); $cropSessionKey = Input::get('cropSessionKey'); @@ -562,6 +596,8 @@ class MediaManager extends WidgetBase public function onResizeImage() { + $this->abortIfReadOnly(); + $cropSessionKey = Input::get('cropSessionKey'); if (!preg_match('/^[0-9a-z]+$/', $cropSessionKey)) { throw new ApplicationException('Invalid input data'); @@ -589,7 +625,7 @@ class MediaManager extends WidgetBase } // - // Methods for th internal use + // Methods for internal use // protected function prepareVars() @@ -1078,6 +1114,10 @@ class MediaManager extends WidgetBase protected function checkUploadPostback() { + if ($this->readOnly) { + return; + } + $fileName = null; $quickMode = false; diff --git a/modules/backend/widgets/mediamanager/assets/js/mediamanager-browser-min.js b/modules/backend/widgets/mediamanager/assets/js/mediamanager-browser-min.js index bb39b484a..fbeb575de 100644 --- a/modules/backend/widgets/mediamanager/assets/js/mediamanager-browser-min.js +++ b/modules/backend/widgets/mediamanager/assets/js/mediamanager-browser-min.js @@ -274,7 +274,7 @@ this.selectionMarker=document.createElement('div') this.selectionMarker.setAttribute('data-control','selection-marker') this.scrollContentElement.insertBefore(this.selectionMarker,this.scrollContentElement.firstChild)} MediaManager.prototype.doObjectsCollide=function(aTop,aLeft,aWidth,aHeight,bTop,bLeft,bWidth,bHeight){return!(((aTop+aHeight)<(bTop))||(aTop>(bTop+bHeight))||((aLeft+aWidth)(bLeft+bWidth)))} -MediaManager.prototype.initUploader=function(){if(!this.itemListElement) +MediaManager.prototype.initUploader=function(){if(!this.itemListElement||this.options.readOnly) return var uploaderOptions={clickable:this.$el.find('[data-control="upload"]').get(0),url:this.options.url,paramName:'file_data',headers:{},createImageThumbnails:false} if(this.options.uniqueId){uploaderOptions.headers['X-OCTOBER-FILEUPLOAD']=this.options.uniqueId} diff --git a/modules/backend/widgets/mediamanager/assets/js/mediamanager.js b/modules/backend/widgets/mediamanager/assets/js/mediamanager.js index 92bee495f..736b28476 100644 --- a/modules/backend/widgets/mediamanager/assets/js/mediamanager.js +++ b/modules/backend/widgets/mediamanager/assets/js/mediamanager.js @@ -708,7 +708,7 @@ // MediaManager.prototype.initUploader = function() { - if (!this.itemListElement) + if (!this.itemListElement || this.options.readOnly) return var uploaderOptions = { diff --git a/modules/backend/widgets/mediamanager/partials/_body.htm b/modules/backend/widgets/mediamanager/partials/_body.htm index 7c74a0cf9..fef45ae21 100644 --- a/modules/backend/widgets/mediamanager/partials/_body.htm +++ b/modules/backend/widgets/mediamanager/partials/_body.htm @@ -10,11 +10,13 @@ data-selection-not-image="" data-bottom-toolbar="bottomToolbar ? 'true' : 'false' ?>" data-crop-and-insert-button="cropAndInsertButton ? 'true' : 'false' ?>" + data-read-only="readOnly ? 'true' : 'false'; ?>" tabindex="0" > makePartial('toolbar') ?> - makePartial('upload-progress') ?> + + readOnly) ? $this->makePartial('upload-progress') : '' ?>
@@ -65,4 +67,4 @@
makePartial('new-folder-form') ?> -
\ No newline at end of file + diff --git a/modules/backend/widgets/mediamanager/partials/_bottom-toolbar.htm b/modules/backend/widgets/mediamanager/partials/_bottom-toolbar.htm index 8c0ea5ad6..0d114009e 100644 --- a/modules/backend/widgets/mediamanager/partials/_bottom-toolbar.htm +++ b/modules/backend/widgets/mediamanager/partials/_bottom-toolbar.htm @@ -9,13 +9,15 @@ - + readOnly) : ?> + + - - + readOnly) : ?> +
+ + +
+ -
- - -
+ readOnly) : ?> +
+ + +
+
makePartial('view-mode-buttons') ?> @@ -38,4 +42,4 @@
- \ No newline at end of file + diff --git a/modules/system/classes/MediaLibrary.php b/modules/system/classes/MediaLibrary.php index 11331cfe5..30505dfcf 100644 --- a/modules/system/classes/MediaLibrary.php +++ b/modules/system/classes/MediaLibrary.php @@ -21,13 +21,17 @@ class MediaLibrary { use \October\Rain\Support\Traits\Singleton; - const CACHE_KEY = 'system-media-library-contents'; const SORT_BY_TITLE = 'title'; const SORT_BY_SIZE = 'size'; const SORT_BY_MODIFIED = 'modified'; const SORT_DIRECTION_ASC = 'asc'; const SORT_DIRECTION_DESC = 'desc'; + /** + * @var string Cache key + */ + protected $cacheKey = 'system-media-library-contents'; + /** * @var string Relative or absolute URL of the Library root folder. */ @@ -79,6 +83,26 @@ class MediaLibrary $this->storageFolderNameLength = strlen($this->storageFolder); } + /** + * Set the cache key + * + * @param string $cacheKey The key to set as the cache key for this instance + */ + public function setCacheKey($cacheKey) + { + $this->cacheKey = $cacheKey; + } + + /** + * Get the cache key + * + * @return string The cache key to set as the cache key for this instance + */ + public function getCacheKey() + { + return $this->cacheKey; + } + /** * Returns a list of folders and files in a Library folder. * @@ -99,7 +123,7 @@ class MediaLibrary * Try to load the contents from cache */ - $cached = Cache::get(self::CACHE_KEY, false); + $cached = Cache::get($this->cacheKey, false); $cached = $cached ? @unserialize(@base64_decode($cached)) : []; if (!is_array($cached)) { @@ -114,7 +138,7 @@ class MediaLibrary $cached[$fullFolderPath] = $folderContents; Cache::put( - self::CACHE_KEY, + $this->cacheKey, base64_encode(serialize($cached)), Config::get('cms.storage.media.ttl', 10) ); @@ -429,7 +453,7 @@ class MediaLibrary */ public function resetCache() { - Cache::forget(self::CACHE_KEY); + Cache::forget($this->cacheKey); } /**