Debug.php 23 KB

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