1
|
|
|
<?php |
2
|
|
|
namespace Rap2hpoutre\LaravelLogViewer; |
3
|
|
|
use Illuminate\Support\Facades\Crypt; |
4
|
|
|
|
5
|
|
|
if (class_exists("\\Illuminate\\Routing\\Controller")) { |
6
|
|
|
class BaseController extends \Illuminate\Routing\Controller {} |
7
|
|
|
} elseif (class_exists("Laravel\\Lumen\\Routing\\Controller")) { |
8
|
|
|
class BaseController extends \Laravel\Lumen\Routing\Controller {} |
|
|
|
|
9
|
|
|
} |
10
|
|
|
|
11
|
|
|
/** |
12
|
|
|
* Class LogViewerController |
13
|
|
|
* @package Rap2hpoutre\LaravelLogViewer |
14
|
|
|
*/ |
15
|
|
|
class LogViewerController extends BaseController |
16
|
|
|
{ |
17
|
|
|
protected $request; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* LogViewerController constructor. |
21
|
|
|
*/ |
22
|
|
|
public function __construct () |
23
|
|
|
{ |
24
|
|
|
$this->request = app('request'); |
25
|
|
|
} |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* @return array|mixed |
29
|
|
|
* @throws \Exception |
30
|
|
|
*/ |
31
|
|
|
public function index() |
32
|
|
|
{ |
33
|
|
|
if ($this->request->input('l')) { |
34
|
|
|
LaravelLogViewer::setFile(Crypt::decrypt($this->request->input('l'))); |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
if ($early_return = $this->earlyReturn()) { |
38
|
|
|
return $early_return; |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
$data = [ |
42
|
|
|
'logs' => LaravelLogViewer::all(), |
43
|
|
|
'files' => LaravelLogViewer::getFiles(true), |
44
|
|
|
'current_file' => LaravelLogViewer::getFileName(), |
45
|
|
|
'standardFormat' => true, |
46
|
|
|
]; |
47
|
|
|
|
48
|
|
|
if ($this->request->wantsJson()) { |
49
|
|
|
return $data; |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
$firstLog = reset($data['logs']); |
53
|
|
|
if (!$firstLog['context'] && !$firstLog['level']) { |
54
|
|
|
$data['standardFormat'] = false; |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
return app('view')->make('laravel-log-viewer::log', $data); |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* @return bool|mixed |
62
|
|
|
* @throws \Exception |
63
|
|
|
*/ |
64
|
|
|
private function earlyReturn() |
65
|
|
|
{ |
66
|
|
|
if ($this->request->input('dl')) { |
67
|
|
|
return $this->download($this->pathFromInput('dl')); |
68
|
|
|
} elseif ($this->request->has('clean')) { |
69
|
|
|
app('files')->put($this->pathFromInput('clean'), ''); |
70
|
|
|
return $this->redirect($this->request->url()); |
71
|
|
|
} elseif ($this->request->has('del')) { |
72
|
|
|
app('files')->delete($this->pathFromInput('del')); |
73
|
|
|
return $this->redirect($this->request->url()); |
74
|
|
|
} elseif ($this->request->has('delall')) { |
75
|
|
|
foreach(LaravelLogViewer::getFiles(true) as $file){ |
76
|
|
|
app('files')->delete(LaravelLogViewer::pathToLogFile($file)); |
77
|
|
|
} |
78
|
|
|
return $this->redirect($this->request->url()); |
79
|
|
|
} |
80
|
|
|
return false; |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* @param $input_string |
85
|
|
|
* @return string |
86
|
|
|
* @throws \Exception |
87
|
|
|
*/ |
88
|
|
|
private function pathFromInput($input_string) |
89
|
|
|
{ |
90
|
|
|
return LaravelLogViewer::pathToLogFile(Crypt::decrypt($this->request->input($input_string))); |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
/** |
94
|
|
|
* @param $to |
95
|
|
|
* @return mixed |
96
|
|
|
*/ |
97
|
|
|
private function redirect($to) |
98
|
|
|
{ |
99
|
|
|
if (function_exists('redirect')) { |
100
|
|
|
return redirect($to); |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
return app('redirect')->to($to); |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
/** |
107
|
|
|
* @param string $data |
108
|
|
|
* @return mixed |
109
|
|
|
*/ |
110
|
|
|
private function download($data) |
111
|
|
|
{ |
112
|
|
|
if (function_exists('response')) { |
113
|
|
|
return response()->download($data); |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
// For laravel 4.2 |
117
|
|
|
return app('\Illuminate\Support\Facades\Response')->download($data); |
118
|
|
|
} |
119
|
|
|
} |
120
|
|
|
|
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.