33namespace ether \logs ;
44
55use Craft ;
6+ use craft \helpers \UrlHelper ;
7+ use Exception ;
8+ use yii \web \HttpException ;
69
710class Controller extends \craft \web \Controller
811{
912
1013 public function actionStream ()
1114 {
12- $ logsDir = Craft::getAlias ('@storage/logs ' );
15+ try {
16+ $ logsDir = Craft::getAlias ('@storage/logs ' );
17+ $ currentLog = $ this ->_getLogFile ();
18+ $ log = file_get_contents ($ logsDir . DIRECTORY_SEPARATOR . $ currentLog );
19+
20+ exit ($ log );
21+ } catch (Exception $ e ) {
22+ if (strpos ($ e ->getMessage (), 'failed to open stream ' ) !== false )
23+ return '<p>Unable to find log file</p> ' ;
24+
25+ return '<p>You can only access .log files!</p> ' ;
26+ }
27+ }
28+
29+ public function actionTruncate ()
30+ {
31+ $ this ->requireAdmin (false );
32+ $ logFile = $ this ->_getLogFile ();
33+ Logs::getInstance ()->service ->truncate ($ logFile );
34+ }
35+
36+ public function actionDelete ()
37+ {
38+ $ this ->requireAdmin (false );
39+ $ logFile = $ this ->_getLogFile ();
40+ Logs::getInstance ()->service ->delete ($ logFile );
41+
42+ return $ this ->redirect (UrlHelper::cpUrl ('utilities/logs ' ));
43+ }
44+
45+ private function _getLogFile (): string
46+ {
1347 $ logFile = Craft::$ app ->request ->getParam ('log ' );
1448 $ currentLog = basename (Craft::$ app ->request ->get ('log ' , $ logFile ));
1549
1650 if (strpos ($ currentLog , '.log ' ) === false )
17- return '<p>You can only access <code>.log</code> files!</p> ' ;
18-
19- $ log = file_get_contents ($ logsDir . '/ ' . $ currentLog );
51+ throw new HttpException (403 );
2052
21- exit ( $ log ) ;
53+ return $ currentLog ;
2254 }
2355
2456}
0 commit comments