Debug.php 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623
  1. <?php
  2. Lib::loadClass('RouteBase');
  3. /**
  4. * TODO: debug to file based on session_id (/tmp/se-debug-{$date("Y-m-d")}-{$login}_{$ip}_{$session_id}.log)
  5. * TODO: function if loggind enabled, then save to file and print in default page
  6. * - DBG::log(string $msg or Exception $e)
  7. * TODO: better wfs log by fingerprint (User::getLogin() + IP + UserAgent)
  8. */
  9. class Route_Debug extends RouteBase {
  10. public $logPathPrefix = '/tmp/se-todo-';
  11. public function __construct() {
  12. $this->logPathPrefix = '/tmp/' . ('production' == V::get('P5_ENV', 'production', $_SERVER) ? "" : "dev-");
  13. }
  14. public function handleAuth() {
  15. if (!User::logged()) {
  16. throw new HttpException('Unauthorized', 401);
  17. }
  18. if (!User::hasAccess('dbg')) {// User::get('ADM_ADMIN_LEVEL') == 0
  19. throw new HttpException('Unauthorized - required dbg access', 401);
  20. }
  21. }
  22. public function defaultAction() {
  23. session_write_close();
  24. $this->defaultView();
  25. }
  26. public function defaultView($flashMsg = '') {
  27. UI::gora();
  28. UI::menu();
  29. UI::setTitle("Debug");
  30. if (!empty($_POST)) echo UI::h('script', [], "history.replaceState(null, 'Debug', window.location.href);");
  31. try {
  32. echo UI::h('div', ['class'=>"breadcrumb"], [
  33. "Debug ",
  34. UI::h('strong', [], "(" . (DBG::isActive() ? "włączony" : "wyłączony") . ") "),
  35. UI::h('a', [
  36. 'href' => $this->getLink(),
  37. 'class' => "btn btn-xs btn-default"
  38. ], "Odśwież"),
  39. " ",
  40. UI::hButtonPost((DBG::isActive()) ? "Wyłącz debug" : "Włącz debug", [
  41. 'class' => "btn-success btn-xs",
  42. 'data' => [
  43. '_route' => 'Debug',
  44. '_task' => DBG::isActive() ? 'deactivateDebug' : 'activateDebug'
  45. ]
  46. ]),
  47. " ",
  48. UI::h('a', [
  49. 'href' => $this->getLink('auth'),
  50. 'class' => "btn btn-xs btn-link"
  51. ], "auth log"),
  52. ]);
  53. echo $flashMsg;
  54. // DBG::nicePrint($_SERVER, '$_SERVER');
  55. // DBG::nicePrint([
  56. // 'date' => date("Y-m-d"),
  57. // 'login' => User::getLogin(),
  58. // 'ip' => Request::getUserIp(),
  59. // 'session_id' => session_id()
  60. // ], 'dbg');
  61. UI::table([
  62. 'caption' => "Log Files " . UI::h('a', [
  63. 'href' => $this->getLink("viewLatestUserLog"),
  64. 'class' => "btn btn-xs btn-primary"
  65. ], "Pokaż ostatni log") . " " . UI::hButtonPost("Usuń wszystkie swoje pliki", [
  66. 'class' => "btn-danger btn-xs",
  67. 'data' => [
  68. '_route' => 'Debug',
  69. '_task' => 'rmAllUserLogFiles'
  70. ]
  71. ]) . " " . UI::hButtonPost("Usuń stare pliki", [
  72. 'class' => "btn-warning btn-xs",
  73. 'data' => [
  74. '_route' => 'Debug',
  75. '_task' => 'rmOldUserLogFiles'
  76. ]
  77. ]),
  78. 'cols' => [
  79. '#',
  80. 'file',
  81. 'user',
  82. 'request date',
  83. 'ip',
  84. 'rm',
  85. ],
  86. 'rows' => array_map(
  87. function ($logFile) {
  88. // /tmp/se-debug-2017-01-25-plabudda-192.168.61.206-4qqrd0.log
  89. try {
  90. if ("{$this->logPathPrefix}se-debug-" != substr($logFile, 0, strlen("{$this->logPathPrefix}se-debug-"))) throw new Exception("Wrong log file name '{$logFile}'");
  91. if ('.log' != substr($logFile, -4)) throw new Exception("Wrong log file name extension '{$logFile}'");
  92. $logName = substr($logFile, strlen("{$this->logPathPrefix}se-debug-"), -4);
  93. list($logYear, $logMonth, $logDay, $logUser, $logIP, $logSessId, $logReqDate) = explode('-', $logName);
  94. return [
  95. '#' => UI::h('a', [
  96. 'href' => $this->getLink("viewLog", ['name' => $logName])
  97. ], "Pokaż"),
  98. 'request date' => ($logReqDate) ? date("Y-m-d H:i:s", $logReqDate) : '',// 1485775975
  99. 'file' => $logFile,
  100. 'user' => $logUser,
  101. 'ip' => $logIP,
  102. 'rm' => UI::hButtonPost("Usuń", [
  103. 'class' => 'btn-default btn-xs',
  104. 'data' => [
  105. '_route' => 'Debug',
  106. '_task' => 'rmLogFile',
  107. 'logName' => $logName
  108. ]
  109. ]),
  110. ];
  111. } catch (Exception $e) {
  112. return [
  113. '#' => '',
  114. 'file' => $e->getMessage(),
  115. ];
  116. }
  117. }
  118. , glob("{$this->logPathPrefix}se-debug-*.log", GLOB_NOSORT)
  119. )
  120. ]);
  121. echo UI::hButtonPost("Test dbg with sleep", [
  122. 'class' => "btn-warning btn-xs",
  123. 'data' => [
  124. '_route' => 'Debug',
  125. '_task' => 'testDebugWithSleep'
  126. ]
  127. ]);
  128. } catch (Exception $e) {
  129. UI::alert('danger', $e->getMessage());
  130. DBG::log($e);
  131. }
  132. UI::dol();
  133. }
  134. public function authAction() {
  135. session_write_close();
  136. $this->authView();
  137. }
  138. public function authView($flashMsg = '') {
  139. UI::gora();
  140. UI::menu();
  141. UI::setTitle("Debug");
  142. if (!empty($_POST)) echo UI::h('script', [], "history.replaceState(null, 'Debug', window.location.href);");
  143. try {
  144. echo UI::h('div', ['class'=>"breadcrumb"], [
  145. UI::h('a', [
  146. 'href' => $this->getLink(),
  147. 'class' => "btn btn-link"
  148. ], "Debug"),
  149. " / ",
  150. UI::h('a', [
  151. 'href' => $this->getLink('auth'),
  152. 'class' => "btn btn-link"
  153. ], "Auth (Odśwież)"),
  154. ]);
  155. echo $flashMsg;
  156. UI::table([
  157. 'caption' => "Log Files " . UI::h('a', [
  158. 'href' => $this->getLink("viewLatestAuthLog"),
  159. 'class' => "btn btn-xs btn-primary"
  160. ], "Pokaż ostatni log") . " " . UI::hButtonPost("Usuń wszystkie pliki", [
  161. 'class' => "btn-danger btn-xs",
  162. 'data' => [
  163. '_route' => 'Debug',
  164. '_task' => 'rmAllAuthLogFiles'
  165. ]
  166. ]) . " " . UI::hButtonPost("Usuń stare pliki", [
  167. 'class' => "btn-warning btn-xs",
  168. 'data' => [
  169. '_route' => 'Debug',
  170. '_task' => 'rmOldAuthLogFiles'
  171. ]
  172. ]),
  173. 'cols' => [
  174. '#',
  175. 'file',
  176. 'user',
  177. 'request date',
  178. 'ip',
  179. 'rm',
  180. ],
  181. 'rows' => array_map(
  182. function ($logFile) {
  183. // /tmp/se-debug-2017-01-25-plabudda-192.168.61.206-4qqrd0.log
  184. try {
  185. if ("{$this->logPathPrefix}se-auth-" != substr($logFile, 0, strlen("{$this->logPathPrefix}se-auth-"))) throw new Exception("Wrong log file name '{$logFile}'");
  186. if ('.log' != substr($logFile, -4)) throw new Exception("Wrong log file name extension '{$logFile}'");
  187. $logName = substr($logFile, strlen("{$this->logPathPrefix}se-auth-"), -4);
  188. list($logYear, $logMonth, $logDay, $logIP, $logReqDate) = explode('-', $logName);
  189. return [
  190. '#' => UI::h('a', [
  191. 'href' => $this->getLink('viewAuthLog', ['name'=>$logName])
  192. ], "Pokaż"),
  193. 'request date' => ($logReqDate) ? date("Y-m-d H:i:s", $logReqDate) : '',// 1485775975
  194. 'file' => $logFile,
  195. 'user' => $logUser,
  196. 'ip' => $logIP,
  197. 'rm' => UI::hButtonPost("Usuń", [
  198. 'class' => 'btn-default btn-xs',
  199. 'data' => [
  200. '_route' => 'Debug',
  201. '_task' => 'rmAuthLogFile',
  202. 'logName' => $logName
  203. ]
  204. ]),
  205. ];
  206. } catch (Exception $e) {
  207. return [
  208. '#' => '',
  209. 'file' => $e->getMessage(),
  210. ];
  211. }
  212. }
  213. , glob("{$this->logPathPrefix}se-auth-*.log", GLOB_NOSORT)
  214. )
  215. ]);
  216. } catch (Exception $e) {
  217. UI::alert('danger', $e->getMessage());
  218. DBG::log($e);
  219. }
  220. UI::dol();
  221. }
  222. public function viewLatestUserLogAction() {
  223. session_write_close();
  224. UI::gora();
  225. // UI::menu();
  226. UI::setTitle("Debug");
  227. echo UI::h('div', ['class'=>'container'], [
  228. UI::h('a', ['href'=>$this->getLink()], "wróć")
  229. ]);
  230. try {
  231. $filerUser = V::get('user', '', $_REQUEST);// TODO: show another user debug
  232. $logName = V::get('name', '', $_REQUEST);
  233. if (!$logName) {
  234. $today = date("Y-m-d");
  235. $cmd = "ls -1rt {$this->logPathPrefix}se-debug-{$today}-*.log | tail -5";
  236. V::exec($cmd, $out, $ret);
  237. if (empty($out)) {
  238. UI::alert('warning', "No logs today. Searching previous...");
  239. $cmd = "ls -1rt {$this->logPathPrefix}se-debug-*.log | tail -5";
  240. V::exec($cmd, $out, $ret);
  241. if (empty($out)) throw new Exception("Log files not found");
  242. }
  243. echo UI::h('div', ['class'=>"alert alert-info"], [
  244. "Last log files",
  245. " (return code: {$ret})",
  246. "<br>cmd: <code>{$cmd}</code>",
  247. UI::h('pre', [], implode("\n", $out))
  248. ]);
  249. $logName = end($out);// /tmp/se-debug-2017-01-30-plabudda-192.168.61.206-4qqrd0-1485775975.log
  250. {
  251. if ("{$this->logPathPrefix}se-debug-" != substr($logName, 0, strlen("{$this->logPathPrefix}se-debug-"))) throw new Exception("Wrong log name prefix");
  252. if ('.log' != substr($logName, -1 * strlen('.log'))) throw new Exception("Wrong log name suffix");
  253. $logName = substr($logName, strlen("{$this->logPathPrefix}se-debug-"), -1 * strlen('.log'));
  254. }
  255. }
  256. $this->printLogFileView('debug', $logName);
  257. } catch (Exception $e) {
  258. UI::alert('danger', $e->getMessage());
  259. }
  260. UI::dol();
  261. }
  262. public function viewLatestAuthLogAction() {
  263. session_write_close();
  264. UI::gora();
  265. // UI::menu();
  266. UI::setTitle("Debug");
  267. echo UI::h('div', ['class'=>'container'], [
  268. UI::h('a', ['href'=>$this->getLink('auth')], "wróć")
  269. ]);
  270. try {
  271. $filerUser = V::get('user', '', $_REQUEST);// TODO: show another user debug
  272. $logName = V::get('name', '', $_REQUEST);
  273. if (!$logName) {
  274. $today = date("Y-m-d");
  275. $cmd = "ls -1rt {$this->logPathPrefix}se-auth-{$today}-*.log | tail -5";
  276. V::exec($cmd, $out, $ret);
  277. if (empty($out)) {
  278. UI::alert('warning', "No logs today. Searching previous...");
  279. $cmd = "ls -1rt {$this->logPathPrefix}se-auth-*.log | tail -5";
  280. V::exec($cmd, $out, $ret);
  281. if (empty($out)) throw new Exception("Log files not found");
  282. }
  283. echo UI::h('div', ['class'=>"alert alert-info"], [
  284. "Last log files",
  285. " (return code: {$ret})",
  286. "<br>cmd: <code>{$cmd}</code>",
  287. UI::h('pre', [], implode("\n", $out))
  288. ]);
  289. $logName = end($out);// /tmp/se-debug-2017-01-30-plabudda-192.168.61.206-4qqrd0-1485775975.log
  290. {
  291. if ("{$this->logPathPrefix}se-auth-" != substr($logName, 0, strlen("{$this->logPathPrefix}se-auth-"))) throw new Exception("Wrong log name prefix");
  292. if ('.log' != substr($logName, -1 * strlen('.log'))) throw new Exception("Wrong log name suffix");
  293. $logName = substr($logName, strlen("{$this->logPathPrefix}se-auth-"), -1 * strlen('.log'));
  294. }
  295. }
  296. $this->printLogFileView('auth', $logName);
  297. } catch (Exception $e) {
  298. UI::alert('danger', $e->getMessage());
  299. }
  300. UI::dol();
  301. }
  302. public function viewLogAction() {
  303. session_write_close();
  304. UI::gora();
  305. // UI::menu();
  306. UI::setTitle("Debug");
  307. echo UI::h('div', ['class'=>'container'], [
  308. UI::h('a', ['href'=>$this->getLink()], "wróć")
  309. ]);
  310. try {
  311. $logName = V::get('name', '', $_REQUEST);
  312. $this->printLogFileView('debug', $logName);
  313. } catch (Exception $e) {
  314. UI::alert('danger', $e->getMessage());
  315. }
  316. UI::dol();
  317. }
  318. public function viewAuthLogAction() {
  319. session_write_close();
  320. UI::gora();
  321. // UI::menu();
  322. UI::setTitle("Debug");
  323. echo UI::h('div', ['class'=>'container'], [
  324. UI::h('a', ['href'=>$this->getLink('auth')], "wróć")
  325. ]);
  326. try {
  327. $logName = V::get('name', '', $_REQUEST);
  328. $this->printLogFileView('auth', $logName);
  329. } catch (Exception $e) {
  330. UI::alert('danger', $e->getMessage());
  331. }
  332. UI::dol();
  333. }
  334. public function printLogFileView($type, $logName) {
  335. if (empty($logName)) throw new Exception("Missing name");
  336. $logName = $this->validateParamLogName($logName);
  337. $logPath = "{$this->logPathPrefix}se-{$type}-{$logName}.log";
  338. if (!file_exists($logPath)) throw new Exception("Log file not exists");
  339. $content = file_get_contents($logPath);
  340. UI::table([
  341. 'cols' => [
  342. 'date',
  343. 'type',
  344. 'msg',
  345. 'log',
  346. 'trace',
  347. ],
  348. 'cols_help' => [
  349. 'trace' => "Cick to show trace"
  350. ],
  351. 'rows' => array_map(
  352. function ($line) {
  353. if (empty($line)) return [];
  354. $dbg = @json_decode($line, $assoc = true);
  355. if (null == $dbg && 0 !== json_last_error()) {
  356. return [
  357. 'type' => 'decode json error',
  358. 'msg' => "Error Processing Request - Parse json error: " . json_last_error()
  359. ];
  360. }
  361. return [
  362. 'date' => '<nobr>' . $dbg['date'] . '</nobr>',
  363. '@style[date]' => "width:1%",
  364. 'type' => $dbg['type'],
  365. '@style[type]' => "width:1%",
  366. 'msg' => UI::h('div', ['style'=>'max-width:360px; overflow-x:auto'], $dbg['msg']),
  367. // 'log' => (!empty($dbg['log'])) ? json_encode($dbg['log']) : '',
  368. '@style[msg]' => "width:360px",
  369. 'log' => UI::h('div', [], [
  370. UI::h('div', [
  371. 'title' => htmlspecialchars( ('sql' == $dbg['type'] && is_string($dbg['log'])) ? $dbg['log'] : var_export($dbg['log'], true) ),
  372. 'onClick' => "return p5DBG__showLogTrace(this, event, '600px')",
  373. 'style' => "cursor:pointer"
  374. ], str_replace(array('\n', '\t'), ' ', substr(htmlspecialchars(json_encode($dbg['log'])), 0, 100)) . ' ...')
  375. ]),
  376. 'trace' => UI::h('div', [], [
  377. UI::h('div', [
  378. 'title' => htmlspecialchars($dbg['trace']),
  379. 'onClick' => "return p5DBG__showLogTrace(this, event)",
  380. 'style' => "cursor:pointer"
  381. ], ('Exception' == $dbg['type'])
  382. ? "Code: {$dbg['log']['code']}, File: {$dbg['log']['file']}"
  383. : '...'
  384. )
  385. ]),
  386. ];
  387. },
  388. explode("\n", $content)
  389. ),
  390. ]);
  391. echo UI::h('script', [], "
  392. function p5DBG__showLogTrace(n, e, maxWidth) {
  393. var maxWidth = maxWidth || null
  394. if (!e) return false;
  395. if (e.target && 'PRE' == e.target.tagName) return false;
  396. var preNode = n.parentNode.lastChild
  397. if (preNode.tagName == 'PRE') {
  398. if ('none' == preNode.style.display) {
  399. preNode.style.display = 'block'
  400. } else {
  401. preNode.style.display = 'none'
  402. }
  403. } else {
  404. var pre = document.createElement('pre')
  405. pre.appendChild( document.createTextNode(n.title.replace(/=>\W*array \(/g, '=> [')) )
  406. if (maxWidth) pre.style.maxWidth = maxWidth
  407. pre.style.fontSize = 'x-small'
  408. n.parentNode.appendChild(pre)
  409. }
  410. }
  411. ");
  412. }
  413. public function activateDebugAction() {
  414. DBG::activate();
  415. $this->defaultView();
  416. }
  417. public function deactivateDebugAction() {
  418. DBG::deactivate();
  419. $this->defaultView();
  420. }
  421. public function testDebugWithSleepAction() {
  422. session_write_close();
  423. UI::gora();
  424. UI::setTitle("Debug Test Sleep");
  425. flush();
  426. for ($i = 0; $i < 10; $i++) {
  427. echo "TEST {$i}<br>";
  428. DBG::log("TEST {$i}");
  429. flush();
  430. sleep(2);
  431. }
  432. echo "DONE";
  433. DBG::log("DONE");
  434. UI::dol();
  435. }
  436. public function validateParamLogName($logName) {
  437. if (empty($logName)) throw new Exception("Missing log file name");
  438. if (!preg_match('/^[\-\.a-zA-Z0-9]+$/', $logName)) throw new Exception("Wrong log file name format");
  439. return $logName;
  440. }
  441. public function rmAllLogFilesAction() {
  442. session_write_close();
  443. try {
  444. $today = date("Y-m-d");
  445. $cmd = "rm -v {$this->logPathPrefix}se-debug-*.log 2>&1";
  446. V::exec($cmd, $out, $ret);
  447. $this->defaultView(UI::h('div', ['class'=>"alert alert-success alert-dismissible"], [
  448. '<button type="button" class="close" data-dismiss="alert" aria-label="Close"><span aria-hidden="true">&times;</span></button>',
  449. (0 === $ret) ? "All Log Files Removed" : "Error?",
  450. " (return code: {$ret})",
  451. "<br>cmd: <code>{$cmd}</code>",
  452. UI::h('pre', [], implode("\n", $out))
  453. ]));
  454. } catch (AlertSuccessException $e) {
  455. $this->defaultView(UI::h('div', ['class'=>"alert alert-success"], $e->getMessage()));
  456. } catch (AlertWarningException $e) {
  457. $this->defaultView(UI::h('div', ['class'=>"alert alert-warning"], $e->getMessage()));
  458. } catch (Exception $e) {
  459. $this->defaultView(UI::h('div', ['class'=>"alert alert-danger"], $e->getMessage()));
  460. }
  461. }
  462. public function rmOldLogFilesAction() {
  463. session_write_close();
  464. try {
  465. $today = date("Y-m-d");
  466. $cmd = "ls -1 {$this->logPathPrefix}se-debug-*.log | grep -v '{$this->logPathPrefix}se-debug-{$today}-' | xargs rm -v 2>&1";
  467. V::exec($cmd, $out, $ret);
  468. $this->defaultView(UI::h('div', ['class'=>"alert alert-success alert-dismissible"], [
  469. '<button type="button" class="close" data-dismiss="alert" aria-label="Close"><span aria-hidden="true">&times;</span></button>',
  470. (0 === $ret) ? "Old Log Files Removed" : "Error?",
  471. " (return code: {$ret})",
  472. "<br>cmd: <code>{$cmd}</code>",
  473. UI::h('pre', [], implode("\n", $out))
  474. ]));
  475. } catch (AlertSuccessException $e) {
  476. $this->defaultView(UI::h('div', ['class'=>"alert alert-success"], $e->getMessage()));
  477. } catch (AlertWarningException $e) {
  478. $this->defaultView(UI::h('div', ['class'=>"alert alert-warning"], $e->getMessage()));
  479. } catch (Exception $e) {
  480. $this->defaultView(UI::h('div', ['class'=>"alert alert-danger"], $e->getMessage()));
  481. }
  482. }
  483. public function rmAllUserLogFilesAction() {
  484. session_write_close();
  485. try {
  486. $userLogin = User::getLogin();
  487. $today = date("Y-m-d");
  488. $cmd = "rm -v {$this->logPathPrefix}se-debug-*-{$userLogin}-*.log 2>&1";
  489. V::exec($cmd, $out, $ret);
  490. $this->defaultView(UI::h('div', ['class'=>"alert alert-success alert-dismissible"], [
  491. '<button type="button" class="close" data-dismiss="alert" aria-label="Close"><span aria-hidden="true">&times;</span></button>',
  492. (0 === $ret) ? "All Log Files Removed" : "Error?",
  493. " (return code: {$ret})",
  494. "<br>cmd: <code>{$cmd}</code>",
  495. UI::h('pre', [], implode("\n", $out))
  496. ]));
  497. } catch (AlertSuccessException $e) {
  498. $this->defaultView(UI::h('div', ['class'=>"alert alert-success"], $e->getMessage()));
  499. } catch (AlertWarningException $e) {
  500. $this->defaultView(UI::h('div', ['class'=>"alert alert-warning"], $e->getMessage()));
  501. } catch (Exception $e) {
  502. $this->defaultView(UI::h('div', ['class'=>"alert alert-danger"], $e->getMessage()));
  503. }
  504. }
  505. public function rmOldUserLogFilesAction() {
  506. session_write_close();
  507. try {
  508. $userLogin = User::getLogin();
  509. $today = date("Y-m-d");
  510. $cmd = "ls -1 {$this->logPathPrefix}se-debug-*-{$userLogin}-*.log | grep -v '{$this->logPathPrefix}se-debug-{$today}-' | xargs rm -v 2>&1";
  511. V::exec($cmd, $out, $ret);
  512. $this->defaultView(UI::h('div', ['class'=>"alert alert-success alert-dismissible"], [
  513. '<button type="button" class="close" data-dismiss="alert" aria-label="Close"><span aria-hidden="true">&times;</span></button>',
  514. (0 === $ret) ? "Old Log Files Removed" : "Error?",
  515. " (return code: {$ret})",
  516. "<br>cmd: <code>{$cmd}</code>",
  517. UI::h('pre', [], implode("\n", $out))
  518. ]));
  519. } catch (AlertSuccessException $e) {
  520. $this->defaultView(UI::h('div', ['class'=>"alert alert-success"], $e->getMessage()));
  521. } catch (AlertWarningException $e) {
  522. $this->defaultView(UI::h('div', ['class'=>"alert alert-warning"], $e->getMessage()));
  523. } catch (Exception $e) {
  524. $this->defaultView(UI::h('div', ['class'=>"alert alert-danger"], $e->getMessage()));
  525. }
  526. }
  527. public function rmLogFileAction() {
  528. session_write_close();
  529. try {
  530. $logName = $this->validateParamLogName(V::get('logName', '', $_REQUEST));
  531. $logPath = "{$this->logPathPrefix}se-debug-{$logName}.log";
  532. if (!file_exists($logPath)) throw new AlertWarningException("Log file not exists");
  533. unlink($logPath);
  534. throw new AlertSuccessException("File Removed");
  535. } catch (AlertSuccessException $e) {
  536. $this->defaultView(UI::h('div', ['class'=>"alert alert-success"], $e->getMessage()));
  537. } catch (AlertWarningException $e) {
  538. $this->defaultView(UI::h('div', ['class'=>"alert alert-warning"], $e->getMessage()));
  539. } catch (Exception $e) {
  540. $this->defaultView(UI::h('div', ['class'=>"alert alert-danger"], $e->getMessage()));
  541. }
  542. }
  543. public function rmAllAuthLogFilesAction() {
  544. session_write_close();
  545. try {
  546. $userLogin = User::getLogin();
  547. $today = date("Y-m-d");
  548. $cmd = "rm -v {$this->logPathPrefix}se-auth-*.log 2>&1";
  549. V::exec($cmd, $out, $ret);
  550. $this->authView(UI::h('div', ['class'=>"alert alert-success alert-dismissible"], [
  551. '<button type="button" class="close" data-dismiss="alert" aria-label="Close"><span aria-hidden="true">&times;</span></button>',
  552. (0 === $ret) ? "All Log Files Removed" : "Error?",
  553. " (return code: {$ret})",
  554. "<br>cmd: <code>{$cmd}</code>",
  555. UI::h('pre', [], implode("\n", $out))
  556. ]));
  557. } catch (AlertSuccessException $e) {
  558. $this->authView(UI::h('div', ['class'=>"alert alert-success"], $e->getMessage()));
  559. } catch (AlertWarningException $e) {
  560. $this->authView(UI::h('div', ['class'=>"alert alert-warning"], $e->getMessage()));
  561. } catch (Exception $e) {
  562. $this->authView(UI::h('div', ['class'=>"alert alert-danger"], $e->getMessage()));
  563. }
  564. }
  565. public function rmOldAuthLogFilesAction() {
  566. session_write_close();
  567. try {
  568. $userLogin = User::getLogin();
  569. $today = date("Y-m-d");
  570. $cmd = "ls -1 {$this->logPathPrefix}se-auth-*.log | grep -v '{$this->logPathPrefix}se-auth-{$today}-' | xargs rm -v 2>&1";
  571. V::exec($cmd, $out, $ret);
  572. $this->authView(UI::h('div', ['class'=>"alert alert-success alert-dismissible"], [
  573. '<button type="button" class="close" data-dismiss="alert" aria-label="Close"><span aria-hidden="true">&times;</span></button>',
  574. (0 === $ret) ? "Old Log Files Removed" : "Error?",
  575. " (return code: {$ret})",
  576. "<br>cmd: <code>{$cmd}</code>",
  577. UI::h('pre', [], implode("\n", $out))
  578. ]));
  579. } catch (AlertSuccessException $e) {
  580. $this->authView(UI::h('div', ['class'=>"alert alert-success"], $e->getMessage()));
  581. } catch (AlertWarningException $e) {
  582. $this->authView(UI::h('div', ['class'=>"alert alert-warning"], $e->getMessage()));
  583. } catch (Exception $e) {
  584. $this->authView(UI::h('div', ['class'=>"alert alert-danger"], $e->getMessage()));
  585. }
  586. }
  587. public function rmAuthLogFileAction() {
  588. session_write_close();
  589. try {
  590. $logName = $this->validateParamLogName(V::get('logName', '', $_REQUEST));
  591. $logPath = "{$this->logPathPrefix}se-auth-{$logName}.log";
  592. if (!file_exists($logPath)) throw new AlertWarningException("Log file not exists");
  593. unlink($logPath);
  594. throw new AlertSuccessException("File Removed");
  595. } catch (AlertSuccessException $e) {
  596. $this->authView(UI::h('div', ['class'=>"alert alert-success"], $e->getMessage()));
  597. } catch (AlertWarningException $e) {
  598. $this->authView(UI::h('div', ['class'=>"alert alert-warning"], $e->getMessage()));
  599. } catch (Exception $e) {
  600. $this->authView(UI::h('div', ['class'=>"alert alert-danger"], $e->getMessage()));
  601. }
  602. }
  603. }