@@ -55,23 +55,27 @@ public function __construct()
5555 public function setFolder ($ folder )
5656 {
5757 if (app ('files ' )->exists ($ folder )) {
58+
5859 $ this ->folder = $ folder ;
5960 }
60- if (is_array ($ this ->storage_path )) {
61+ else if (is_array ($ this ->storage_path )) {
62+
6163 foreach ($ this ->storage_path as $ value ) {
64+
6265 $ logsPath = $ value . '/ ' . $ folder ;
66+
6367 if (app ('files ' )->exists ($ logsPath )) {
6468 $ this ->folder = $ folder ;
6569 break ;
6670 }
6771 }
6872 } else {
69- if ( $ this -> storage_path ) {
73+
7074 $ logsPath = $ this ->storage_path . '/ ' . $ folder ;
7175 if (app ('files ' )->exists ($ logsPath )) {
7276 $ this ->folder = $ folder ;
7377 }
74- }
78+
7579 }
7680 }
7781
@@ -97,9 +101,11 @@ public function pathToLogFile($file)
97101 {
98102
99103 if (app ('files ' )->exists ($ file )) { // try the absolute path
104+
100105 return $ file ;
101106 }
102107 if (is_array ($ this ->storage_path )) {
108+
103109 foreach ($ this ->storage_path as $ folder ) {
104110 if (app ('files ' )->exists ($ folder . '/ ' . $ file )) { // try the absolute path
105111 $ file = $ folder . '/ ' . $ file ;
@@ -114,8 +120,9 @@ public function pathToLogFile($file)
114120 $ file = $ logsPath . '/ ' . $ file ;
115121 // check if requested file is really in the logs directory
116122 if (dirname ($ file ) !== $ logsPath ) {
117- throw new \Exception ('No such log file ' );
123+ throw new \Exception ('No such log file: ' . $ file );
118124 }
125+
119126 return $ file ;
120127 }
121128
@@ -228,29 +235,51 @@ public function all()
228235 return array_reverse ($ log );
229236 }
230237
231- /**
232- * @return array
233- */
234- public function getFolders ()
238+ /**Creates a multidimensional array
239+ * of subdirectories and files
240+ *
241+ * @param null $path
242+ *
243+ * @return array
244+ */
245+ public function foldersAndFiles ($ path = null )
235246 {
236- $ folders = glob ($ this ->storage_path . '/* ' , GLOB_ONLYDIR );
237- if (is_array ($ this ->storage_path )) {
238- foreach ($ this ->storage_path as $ value ) {
239- $ folders = array_merge (
240- $ folders ,
241- glob ($ value . '/* ' , GLOB_ONLYDIR )
242- );
243- }
244- }
247+ $ contents = array ();
248+ $ dir = $ path ? $ path : $ this ->storage_path ;
249+ foreach (scandir ($ dir ) as $ node ) {
250+ if ($ node == '. ' || $ node == '.. ' ) continue ;
251+ $ path = $ dir . '\\' . $ node ;
252+ if (is_dir ($ path )) {
253+ $ contents [$ path ] = $ this ->foldersAndFiles ($ path );
254+ } else {
255+ $ contents [] = $ path ;
256+ }
257+ }
258+
259+ return $ contents ;
260+ }
245261
246- if (is_array ($ folders )) {
247- foreach ($ folders as $ k => $ folder ) {
248- $ folders [$ k ] = basename ($ folder );
249- }
250- }
251- return array_values ($ folders );
262+ /**Returns an array of
263+ * all subdirectories of specified directory
264+ *
265+ * @param string $folder
266+ *
267+ * @return array
268+ */
269+ public function getFolders ($ folder = '' )
270+ {
271+ $ folders = [];
272+ $ listObject = new \RecursiveIteratorIterator (
273+ new \RecursiveDirectoryIterator ($ this ->storage_path .'/ ' .$ folder , \RecursiveDirectoryIterator::SKIP_DOTS ),
274+ \RecursiveIteratorIterator::CHILD_FIRST
275+ );
276+ foreach ($ listObject as $ fileinfo ) {
277+ if ($ fileinfo ->isDir ()) $ folders [] = $ fileinfo ->getRealPath ();
278+ }
279+ return $ folders ;
252280 }
253281
282+
254283 /**
255284 * @param bool $basename
256285 * @return array
@@ -267,30 +296,81 @@ public function getFolderFiles($basename = false)
267296 */
268297 public function getFiles ($ basename = false , $ folder = '' )
269298 {
270- $ pattern = function_exists ('config ' ) ? config ('logviewer.pattern ' , '*.log ' ) : '*.log ' ;
271- $ files = glob (
272- $ this ->storage_path . '/ ' . $ folder . '/ ' . $ pattern ,
273- preg_match ($ this ->pattern ->getPattern ('files ' ), $ pattern ) ? GLOB_BRACE : 0
274- );
275- if (is_array ($ this ->storage_path )) {
276- foreach ($ this ->storage_path as $ value ) {
277- $ files = array_merge (
278- $ files ,
279- glob (
280- $ value . '/ ' . $ folder . '/ ' . $ pattern ,
281- preg_match ($ this ->pattern ->getPattern ('files ' ), $ pattern ) ? GLOB_BRACE : 0
282- )
283- );
284- }
285- }
299+ $ files = [];
300+ $ pattern = function_exists ('config ' ) ? config ('logviewer.pattern ' , '*.log ' ) : '*.log ' ;
301+ $ fullPath = $ this ->storage_path .'/ ' .$ folder ;
286302
287- $ files = array_reverse ($ files );
288- $ files = array_filter ($ files , 'is_file ' );
289- if ($ basename && is_array ($ files )) {
290- foreach ($ files as $ k => $ file ) {
291- $ files [$ k ] = basename ($ file );
292- }
293- }
294- return array_values ($ files );
303+ $ listObject = new \RecursiveIteratorIterator (
304+ new \RecursiveDirectoryIterator ($ fullPath , \RecursiveDirectoryIterator::SKIP_DOTS ),
305+ \RecursiveIteratorIterator::CHILD_FIRST
306+ );
307+
308+ foreach ($ listObject as $ fileinfo ) {
309+ if (!$ fileinfo ->isDir () && strtolower (pathinfo ($ fileinfo ->getRealPath (), PATHINFO_EXTENSION )) == explode ('. ' , $ pattern )[1 ])
310+ $ files [] = $ basename ? basename ($ fileinfo ->getRealPath ()) : $ fileinfo ->getRealPath ();
311+ }
312+ return $ files ;
313+
314+ }
315+
316+ /**
317+ * @return string
318+ */
319+ public function getStoragePath ()
320+ {
321+ return $ this ->storage_path ;
295322 }
323+
324+ /**
325+ * @param $path
326+ *
327+ * @return void
328+ */
329+ public function setStoragePath ($ path )
330+ {
331+ $ this ->storage_path = $ path ;
332+ }
333+
334+ public static function directoryTreeStructure ($ storage_path , array $ array )
335+ {
336+ foreach ($ array as $ k => $ v ) {
337+ if (is_dir ( $ k )) {
338+
339+ $ exploded = explode ( "\\" , $ k );
340+ $ show = last ( $ exploded );
341+
342+ echo '<div class="list-group folder">
343+ <a href="?f= ' . \Illuminate \Support \Facades \Crypt::encrypt ($ k ).'">
344+ <span> </span><span
345+ class="fa fa-folder"></span> ' .$ show .'
346+ </a>
347+ </div> ' ;
348+
349+ if ( is_array ( $ v ) ) {
350+ self ::directoryTreeStructure ( $ storage_path , $ v );
351+ }
352+
353+ }
354+ else {
355+
356+ $ exploded = explode ( "\\" , $ v );
357+ $ show2 = last ( $ exploded );
358+ $ folder = str_replace ( $ storage_path , "" , rtrim ( str_replace ( $ show2 , "" , $ v ), "\\" ) );
359+ $ file = $ v ;
360+
361+
362+ echo '<div class="list-group">
363+ <a href="?l= ' .\Illuminate \Support \Facades \Crypt::encrypt ($ file ).'&f= ' .\Illuminate \Support \Facades \Crypt::encrypt ($ folder ).'">
364+ <span> </span> <span
365+ class="fa fa-file"></span> ' .$ show2 .'
366+ </a>
367+ </div> ' ;
368+
369+ }
370+ }
371+
372+ return ;
373+ }
374+
375+
296376}
0 commit comments