@@ -26,6 +26,13 @@ class MediaManager extends Extension
2626 */
2727 protected $ storage ;
2828
29+ /**
30+ * List of allowed extensions.
31+ *
32+ * @var string
33+ */
34+ protected $ allowed = [];
35+
2936 /**
3037 * @var array
3138 */
@@ -50,6 +57,10 @@ public function __construct($path = '/')
5057 {
5158 $ this ->path = $ path ;
5259
60+ if (!empty (config ('admin.extensions.media-manager.allowed_ext ' ))) {
61+ $ this ->allowed = explode (', ' , config ('admin.extensions.media-manager.allowed_ext ' ));
62+ }
63+
5364 $ this ->initStorage ();
5465 }
5566
@@ -77,10 +88,10 @@ public function ls()
7788 $ directories = $ this ->storage ->directories ($ this ->path );
7889
7990 return $ this ->formatDirectories ($ directories )
80- ->merge ($ this ->formatFiles ($ files ))
81- ->sort (function ($ item ) {
82- return $ item ['name ' ];
83- })->all ();
91+ ->merge ($ this ->formatFiles ($ files ))
92+ ->sort (function ($ item ) {
93+ return $ item ['name ' ];
94+ })->all ();
8495 }
8596
8697 /**
@@ -92,7 +103,12 @@ public function ls()
92103 */
93104 protected function getFullPath ($ path )
94105 {
95- return $ this ->storage ->getDriver ()->getAdapter ()->applyPathPrefix ($ path );
106+ $ path = $ this ->storage ->getDriver ()->getAdapter ()->applyPathPrefix ($ path );
107+ if (strstr ($ fullPath , '.. ' )) {
108+ throw new \Exception ('Incorrect path ' );
109+ }
110+
111+ return $ path ;
96112 }
97113
98114 public function download ()
@@ -125,6 +141,11 @@ public function delete($path)
125141
126142 public function move ($ new )
127143 {
144+ $ ext = pathinfo ($ new , PATHINFO_EXTENSION );
145+ if ($ this ->allowed && !in_array ($ ext , $ this ->allowed )) {
146+ throw new \Exception ('File extension ' .$ ext .' is not allowed ' );
147+ }
148+
128149 return $ this ->storage ->move ($ this ->path , $ new );
129150 }
130151
@@ -137,6 +158,10 @@ public function move($new)
137158 public function upload ($ files = [])
138159 {
139160 foreach ($ files as $ file ) {
161+ if ($ this ->allowed && !in_array ($ file ->getClientOriginalExtension (), $ this ->allowed )) {
162+ throw new \Exception ('File extension ' .$ file ->getClientOriginalExtension ().' is not allowed ' );
163+ }
164+
140165 $ this ->storage ->putFileAs ($ this ->path , $ file , $ file ->getClientOriginalName ());
141166 }
142167
0 commit comments