1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Rap2hpoutre\LaravelLogViewer; |
4
|
|
|
|
5
|
|
|
use Illuminate\Support\Facades\Crypt; |
6
|
|
|
|
7
|
|
|
if (class_exists("\\Illuminate\\Routing\\Controller")) { |
8
|
|
|
class BaseController extends \Illuminate\Routing\Controller {} |
9
|
|
|
} elseif (class_exists("Laravel\\Lumen\\Routing\\Controller")) { |
10
|
|
|
class BaseController extends \Laravel\Lumen\Routing\Controller {} |
|
|
|
|
11
|
|
|
} |
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* Class LogViewerController |
15
|
|
|
* @package Rap2hpoutre\LaravelLogViewer |
16
|
|
|
*/ |
17
|
|
|
class LogViewerController extends BaseController |
18
|
|
|
{ |
19
|
|
|
/** |
20
|
|
|
* @var \Illuminate\Http\Request |
21
|
|
|
*/ |
22
|
|
|
protected $request; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* @var LaravelLogViewer |
26
|
|
|
*/ |
27
|
|
|
private $log_viewer; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* @var string |
31
|
|
|
*/ |
32
|
|
|
protected $view_log = 'laravel-log-viewer::log'; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* LogViewerController constructor. |
36
|
|
|
*/ |
37
|
|
|
public function __construct() |
38
|
|
|
{ |
39
|
|
|
$this->log_viewer = new LaravelLogViewer(); |
40
|
|
|
$this->request = app('request'); |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* @return array|mixed |
45
|
|
|
* @throws \Exception |
46
|
|
|
*/ |
47
|
|
|
public function index() |
48
|
|
|
{ |
49
|
|
|
$folderFiles = []; |
50
|
|
|
if ($this->request->input('f')) { |
51
|
|
|
$this->log_viewer->setFolder(Crypt::decrypt($this->request->input('f'))); |
|
|
|
|
52
|
|
|
$folderFiles = $this->log_viewer->getFolderFiles(true); |
53
|
|
|
} |
54
|
|
|
if ($this->request->input('l')) { |
55
|
|
|
$this->log_viewer->setFile(Crypt::decrypt($this->request->input('l'))); |
|
|
|
|
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
if ($early_return = $this->earlyReturn()) { |
59
|
|
|
return $early_return; |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
$data = [ |
63
|
|
|
'logs' => $this->log_viewer->all(), |
64
|
|
|
'folders' => $this->log_viewer->getFolders(), |
65
|
|
|
'current_folder' => $this->log_viewer->getFolderName(), |
66
|
|
|
'folder_files' => $folderFiles, |
67
|
|
|
'files' => $this->log_viewer->getFiles(true), |
68
|
|
|
'current_file' => $this->log_viewer->getFileName(), |
69
|
|
|
'standardFormat' => true, |
70
|
|
|
]; |
71
|
|
|
|
72
|
|
|
if ($this->request->wantsJson()) { |
73
|
|
|
return $data; |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
if (is_array($data['logs']) && count($data['logs']) > 0) { |
77
|
|
|
$firstLog = reset($data['logs']); |
78
|
|
|
if (!$firstLog['context'] && !$firstLog['level']) { |
79
|
|
|
$data['standardFormat'] = false; |
80
|
|
|
} |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
return app('view')->make($this->view_log, $data); |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* @return bool|mixed |
88
|
|
|
* @throws \Exception |
89
|
|
|
*/ |
90
|
|
|
private function earlyReturn() |
91
|
|
|
{ |
92
|
|
|
if ($this->request->input('f')) { |
93
|
|
|
$this->log_viewer->setFolder(Crypt::decrypt($this->request->input('f'))); |
|
|
|
|
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
if ($this->request->input('dl')) { |
97
|
|
|
return $this->download($this->pathFromInput('dl')); |
98
|
|
|
} elseif ($this->request->has('clean')) { |
99
|
|
|
app('files')->put($this->pathFromInput('clean'), ''); |
100
|
|
|
return $this->redirect(url()->previous()); |
101
|
|
|
} elseif ($this->request->has('del')) { |
102
|
|
|
app('files')->delete($this->pathFromInput('del')); |
103
|
|
|
return $this->redirect($this->request->url()); |
104
|
|
|
} elseif ($this->request->has('delall')) { |
105
|
|
|
$files = ($this->log_viewer->getFolderName()) |
106
|
|
|
? $this->log_viewer->getFolderFiles(true) |
107
|
|
|
: $this->log_viewer->getFiles(true); |
108
|
|
|
foreach ($files as $file) { |
109
|
|
|
app('files')->delete($this->log_viewer->pathToLogFile($file)); |
110
|
|
|
} |
111
|
|
|
return $this->redirect($this->request->url()); |
112
|
|
|
} |
113
|
|
|
return false; |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
/** |
117
|
|
|
* @param string $input_string |
118
|
|
|
* @return string |
119
|
|
|
* @throws \Exception |
120
|
|
|
*/ |
121
|
|
|
private function pathFromInput($input_string) |
122
|
|
|
{ |
123
|
|
|
return $this->log_viewer->pathToLogFile(Crypt::decrypt($this->request->input($input_string))); |
|
|
|
|
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
/** |
127
|
|
|
* @param $to |
128
|
|
|
* @return mixed |
129
|
|
|
*/ |
130
|
|
|
private function redirect($to) |
131
|
|
|
{ |
132
|
|
|
if (function_exists('redirect')) { |
133
|
|
|
return redirect($to); |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
return app('redirect')->to($to); |
137
|
|
|
} |
138
|
|
|
|
139
|
|
|
/** |
140
|
|
|
* @param string $data |
141
|
|
|
* @return mixed |
142
|
|
|
*/ |
143
|
|
|
private function download($data) |
144
|
|
|
{ |
145
|
|
|
if (function_exists('response')) { |
146
|
|
|
return response()->download($data); |
|
|
|
|
147
|
|
|
} |
148
|
|
|
|
149
|
|
|
// For laravel 4.2 |
150
|
|
|
return app('\Illuminate\Support\Facades\Response')->download($data); |
151
|
|
|
} |
152
|
|
|
} |
153
|
|
|
|
This check looks for classes that have been defined more than once in the same file.
If you can, we would recommend to use standard object-oriented programming techniques. For example, to avoid multiple types, it might make sense to create a common interface, and then multiple, different implementations for that interface.
This also has the side-effect of providing you with better IDE auto-completion, static analysis and also better OPCode caching from PHP.