Debug.php 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627
  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. UI::h('span', ['style'=>'padding:0 6px'], "|"),
  230. UI::h('span', ['class' => 'label label-'.(DBG::isActive() ? "success" : "danger")], "(Debug - " . (DBG::isActive() ? "włączony" : "wyłączony") . ") "),
  231. UI::h('span', ['style'=>'padding:0 6px'], "|"),
  232. UI::h('button', ['onClick'=>'location.reload()', 'class'=>"btn btn-xs btn-default"], "odśwież"),
  233. ]);
  234. try {
  235. $filerUser = V::get('user', '', $_REQUEST);// TODO: show another user debug
  236. $logName = V::get('name', '', $_REQUEST);
  237. if (!$logName) {
  238. $today = date("Y-m-d");
  239. $cmd = "ls -1rt {$this->logPathPrefix}se-debug-{$today}-*.log | tail -5";
  240. V::exec($cmd, $out, $ret);
  241. if (empty($out)) {
  242. UI::alert('warning', "No logs today. Searching previous...");
  243. $cmd = "ls -1rt {$this->logPathPrefix}se-debug-*.log | tail -5";
  244. V::exec($cmd, $out, $ret);
  245. if (empty($out)) throw new Exception("Log files not found");
  246. }
  247. echo UI::h('div', ['class'=>"alert alert-info"], [
  248. "Last log files",
  249. " (return code: {$ret})",
  250. "<br>cmd: <code>{$cmd}</code>",
  251. UI::h('pre', [], implode("\n", $out))
  252. ]);
  253. $logName = end($out);// /tmp/se-debug-2017-01-30-plabudda-192.168.61.206-4qqrd0-1485775975.log
  254. {
  255. if ("{$this->logPathPrefix}se-debug-" != substr($logName, 0, strlen("{$this->logPathPrefix}se-debug-"))) throw new Exception("Wrong log name prefix");
  256. if ('.log' != substr($logName, -1 * strlen('.log'))) throw new Exception("Wrong log name suffix");
  257. $logName = substr($logName, strlen("{$this->logPathPrefix}se-debug-"), -1 * strlen('.log'));
  258. }
  259. }
  260. $this->printLogFileView('debug', $logName);
  261. } catch (Exception $e) {
  262. UI::alert('danger', $e->getMessage());
  263. }
  264. UI::dol();
  265. }
  266. public function viewLatestAuthLogAction() {
  267. session_write_close();
  268. UI::gora();
  269. // UI::menu();
  270. UI::setTitle("Debug");
  271. echo UI::h('div', ['class'=>'container'], [
  272. UI::h('a', ['href'=>$this->getLink('auth')], "wróć")
  273. ]);
  274. try {
  275. $filerUser = V::get('user', '', $_REQUEST);// TODO: show another user debug
  276. $logName = V::get('name', '', $_REQUEST);
  277. if (!$logName) {
  278. $today = date("Y-m-d");
  279. $cmd = "ls -1rt {$this->logPathPrefix}se-auth-{$today}-*.log | tail -5";
  280. V::exec($cmd, $out, $ret);
  281. if (empty($out)) {
  282. UI::alert('warning', "No logs today. Searching previous...");
  283. $cmd = "ls -1rt {$this->logPathPrefix}se-auth-*.log | tail -5";
  284. V::exec($cmd, $out, $ret);
  285. if (empty($out)) throw new Exception("Log files not found");
  286. }
  287. echo UI::h('div', ['class'=>"alert alert-info"], [
  288. "Last log files",
  289. " (return code: {$ret})",
  290. "<br>cmd: <code>{$cmd}</code>",
  291. UI::h('pre', [], implode("\n", $out))
  292. ]);
  293. $logName = end($out);// /tmp/se-debug-2017-01-30-plabudda-192.168.61.206-4qqrd0-1485775975.log
  294. {
  295. if ("{$this->logPathPrefix}se-auth-" != substr($logName, 0, strlen("{$this->logPathPrefix}se-auth-"))) throw new Exception("Wrong log name prefix");
  296. if ('.log' != substr($logName, -1 * strlen('.log'))) throw new Exception("Wrong log name suffix");
  297. $logName = substr($logName, strlen("{$this->logPathPrefix}se-auth-"), -1 * strlen('.log'));
  298. }
  299. }
  300. $this->printLogFileView('auth', $logName);
  301. } catch (Exception $e) {
  302. UI::alert('danger', $e->getMessage());
  303. }
  304. UI::dol();
  305. }
  306. public function viewLogAction() {
  307. session_write_close();
  308. UI::gora();
  309. // UI::menu();
  310. UI::setTitle("Debug");
  311. echo UI::h('div', ['class'=>'container'], [
  312. UI::h('a', ['href'=>$this->getLink()], "wróć")
  313. ]);
  314. try {
  315. $logName = V::get('name', '', $_REQUEST);
  316. $this->printLogFileView('debug', $logName);
  317. } catch (Exception $e) {
  318. UI::alert('danger', $e->getMessage());
  319. }
  320. UI::dol();
  321. }
  322. public function viewAuthLogAction() {
  323. session_write_close();
  324. UI::gora();
  325. // UI::menu();
  326. UI::setTitle("Debug");
  327. echo UI::h('div', ['class'=>'container'], [
  328. UI::h('a', ['href'=>$this->getLink('auth')], "wróć")
  329. ]);
  330. try {
  331. $logName = V::get('name', '', $_REQUEST);
  332. $this->printLogFileView('auth', $logName);
  333. } catch (Exception $e) {
  334. UI::alert('danger', $e->getMessage());
  335. }
  336. UI::dol();
  337. }
  338. public function printLogFileView($type, $logName) {
  339. if (empty($logName)) throw new Exception("Missing name");
  340. $logName = $this->validateParamLogName($logName);
  341. $logPath = "{$this->logPathPrefix}se-{$type}-{$logName}.log";
  342. if (!file_exists($logPath)) throw new Exception("Log file not exists");
  343. $content = file_get_contents($logPath);
  344. UI::table([
  345. 'cols' => [
  346. 'date',
  347. 'type',
  348. 'msg',
  349. 'log',
  350. 'trace',
  351. ],
  352. 'cols_help' => [
  353. 'trace' => "Cick to show trace"
  354. ],
  355. 'rows' => array_map(
  356. function ($line) {
  357. if (empty($line)) return [];
  358. $dbg = @json_decode($line, $assoc = true);
  359. if (null == $dbg && 0 !== json_last_error()) {
  360. return [
  361. 'type' => 'decode json error',
  362. 'msg' => "Error Processing Request - Parse json error: " . json_last_error()
  363. ];
  364. }
  365. return [
  366. 'date' => '<nobr>' . $dbg['date'] . '</nobr>',
  367. '@style[date]' => "width:1%",
  368. 'type' => $dbg['type'],
  369. '@style[type]' => "width:1%",
  370. 'msg' => UI::h('div', ['style'=>'max-width:360px; overflow-x:auto'], $dbg['msg']),
  371. // 'log' => (!empty($dbg['log'])) ? json_encode($dbg['log']) : '',
  372. '@style[msg]' => "width:360px",
  373. 'log' => UI::h('div', [], [
  374. UI::h('div', [
  375. 'title' => htmlspecialchars( ('sql' == $dbg['type'] && is_string($dbg['log'])) ? $dbg['log'] : var_export($dbg['log'], true) ),
  376. 'onClick' => "return p5DBG__showLogTrace(this, event, '600px')",
  377. 'style' => "cursor:pointer"
  378. ], str_replace(array('\n', '\t'), ' ', substr(htmlspecialchars(json_encode($dbg['log'])), 0, 100)) . ' ...')
  379. ]),
  380. 'trace' => UI::h('div', [], [
  381. UI::h('div', [
  382. 'title' => htmlspecialchars($dbg['trace']),
  383. 'onClick' => "return p5DBG__showLogTrace(this, event)",
  384. 'style' => "cursor:pointer"
  385. ], ('Exception' == $dbg['type'])
  386. ? "Code: {$dbg['log']['code']}, File: {$dbg['log']['file']}"
  387. : '...'
  388. )
  389. ]),
  390. ];
  391. },
  392. explode("\n", $content)
  393. ),
  394. ]);
  395. echo UI::h('script', [], "
  396. function p5DBG__showLogTrace(n, e, maxWidth) {
  397. var maxWidth = maxWidth || null
  398. if (!e) return false;
  399. if (e.target && 'PRE' == e.target.tagName) return false;
  400. var preNode = n.parentNode.lastChild
  401. if (preNode.tagName == 'PRE') {
  402. if ('none' == preNode.style.display) {
  403. preNode.style.display = 'block'
  404. } else {
  405. preNode.style.display = 'none'
  406. }
  407. } else {
  408. var pre = document.createElement('pre')
  409. pre.appendChild( document.createTextNode(n.title.replace(/=>\W*array \(/g, '=> [')) )
  410. if (maxWidth) pre.style.maxWidth = maxWidth
  411. pre.style.fontSize = 'x-small'
  412. n.parentNode.appendChild(pre)
  413. }
  414. }
  415. ");
  416. }
  417. public function activateDebugAction() {
  418. DBG::activate();
  419. $this->defaultView();
  420. }
  421. public function deactivateDebugAction() {
  422. DBG::deactivate();
  423. $this->defaultView();
  424. }
  425. public function testDebugWithSleepAction() {
  426. session_write_close();
  427. UI::gora();
  428. UI::setTitle("Debug Test Sleep");
  429. flush();
  430. for ($i = 0; $i < 10; $i++) {
  431. echo "TEST {$i}<br>";
  432. DBG::log("TEST {$i}");
  433. flush();
  434. sleep(2);
  435. }
  436. echo "DONE";
  437. DBG::log("DONE");
  438. UI::dol();
  439. }
  440. public function validateParamLogName($logName) {
  441. if (empty($logName)) throw new Exception("Missing log file name");
  442. if (!preg_match('/^[\-\.a-zA-Z0-9]+$/', $logName)) throw new Exception("Wrong log file name format");
  443. return $logName;
  444. }
  445. public function rmAllLogFilesAction() {
  446. session_write_close();
  447. try {
  448. $today = date("Y-m-d");
  449. $cmd = "rm -v {$this->logPathPrefix}se-debug-*.log 2>&1";
  450. V::exec($cmd, $out, $ret);
  451. $this->defaultView(UI::h('div', ['class'=>"alert alert-success alert-dismissible"], [
  452. '<button type="button" class="close" data-dismiss="alert" aria-label="Close"><span aria-hidden="true">&times;</span></button>',
  453. (0 === $ret) ? "All Log Files Removed" : "Error?",
  454. " (return code: {$ret})",
  455. "<br>cmd: <code>{$cmd}</code>",
  456. UI::h('pre', [], implode("\n", $out))
  457. ]));
  458. } catch (AlertSuccessException $e) {
  459. $this->defaultView(UI::h('div', ['class'=>"alert alert-success"], $e->getMessage()));
  460. } catch (AlertWarningException $e) {
  461. $this->defaultView(UI::h('div', ['class'=>"alert alert-warning"], $e->getMessage()));
  462. } catch (Exception $e) {
  463. $this->defaultView(UI::h('div', ['class'=>"alert alert-danger"], $e->getMessage()));
  464. }
  465. }
  466. public function rmOldLogFilesAction() {
  467. session_write_close();
  468. try {
  469. $today = date("Y-m-d");
  470. $cmd = "ls -1 {$this->logPathPrefix}se-debug-*.log | grep -v '{$this->logPathPrefix}se-debug-{$today}-' | xargs rm -v 2>&1";
  471. V::exec($cmd, $out, $ret);
  472. $this->defaultView(UI::h('div', ['class'=>"alert alert-success alert-dismissible"], [
  473. '<button type="button" class="close" data-dismiss="alert" aria-label="Close"><span aria-hidden="true">&times;</span></button>',
  474. (0 === $ret) ? "Old Log Files Removed" : "Error?",
  475. " (return code: {$ret})",
  476. "<br>cmd: <code>{$cmd}</code>",
  477. UI::h('pre', [], implode("\n", $out))
  478. ]));
  479. } catch (AlertSuccessException $e) {
  480. $this->defaultView(UI::h('div', ['class'=>"alert alert-success"], $e->getMessage()));
  481. } catch (AlertWarningException $e) {
  482. $this->defaultView(UI::h('div', ['class'=>"alert alert-warning"], $e->getMessage()));
  483. } catch (Exception $e) {
  484. $this->defaultView(UI::h('div', ['class'=>"alert alert-danger"], $e->getMessage()));
  485. }
  486. }
  487. public function rmAllUserLogFilesAction() {
  488. session_write_close();
  489. try {
  490. $userLogin = User::getLogin();
  491. $today = date("Y-m-d");
  492. $cmd = "rm -v {$this->logPathPrefix}se-debug-*-{$userLogin}-*.log 2>&1";
  493. V::exec($cmd, $out, $ret);
  494. $this->defaultView(UI::h('div', ['class'=>"alert alert-success alert-dismissible"], [
  495. '<button type="button" class="close" data-dismiss="alert" aria-label="Close"><span aria-hidden="true">&times;</span></button>',
  496. (0 === $ret) ? "All Log Files Removed" : "Error?",
  497. " (return code: {$ret})",
  498. "<br>cmd: <code>{$cmd}</code>",
  499. UI::h('pre', [], implode("\n", $out))
  500. ]));
  501. } catch (AlertSuccessException $e) {
  502. $this->defaultView(UI::h('div', ['class'=>"alert alert-success"], $e->getMessage()));
  503. } catch (AlertWarningException $e) {
  504. $this->defaultView(UI::h('div', ['class'=>"alert alert-warning"], $e->getMessage()));
  505. } catch (Exception $e) {
  506. $this->defaultView(UI::h('div', ['class'=>"alert alert-danger"], $e->getMessage()));
  507. }
  508. }
  509. public function rmOldUserLogFilesAction() {
  510. session_write_close();
  511. try {
  512. $userLogin = User::getLogin();
  513. $today = date("Y-m-d");
  514. $cmd = "ls -1 {$this->logPathPrefix}se-debug-*-{$userLogin}-*.log | grep -v '{$this->logPathPrefix}se-debug-{$today}-' | xargs rm -v 2>&1";
  515. V::exec($cmd, $out, $ret);
  516. $this->defaultView(UI::h('div', ['class'=>"alert alert-success alert-dismissible"], [
  517. '<button type="button" class="close" data-dismiss="alert" aria-label="Close"><span aria-hidden="true">&times;</span></button>',
  518. (0 === $ret) ? "Old Log Files Removed" : "Error?",
  519. " (return code: {$ret})",
  520. "<br>cmd: <code>{$cmd}</code>",
  521. UI::h('pre', [], implode("\n", $out))
  522. ]));
  523. } catch (AlertSuccessException $e) {
  524. $this->defaultView(UI::h('div', ['class'=>"alert alert-success"], $e->getMessage()));
  525. } catch (AlertWarningException $e) {
  526. $this->defaultView(UI::h('div', ['class'=>"alert alert-warning"], $e->getMessage()));
  527. } catch (Exception $e) {
  528. $this->defaultView(UI::h('div', ['class'=>"alert alert-danger"], $e->getMessage()));
  529. }
  530. }
  531. public function rmLogFileAction() {
  532. session_write_close();
  533. try {
  534. $logName = $this->validateParamLogName(V::get('logName', '', $_REQUEST));
  535. $logPath = "{$this->logPathPrefix}se-debug-{$logName}.log";
  536. if (!file_exists($logPath)) throw new AlertWarningException("Log file not exists");
  537. unlink($logPath);
  538. throw new AlertSuccessException("File Removed");
  539. } catch (AlertSuccessException $e) {
  540. $this->defaultView(UI::h('div', ['class'=>"alert alert-success"], $e->getMessage()));
  541. } catch (AlertWarningException $e) {
  542. $this->defaultView(UI::h('div', ['class'=>"alert alert-warning"], $e->getMessage()));
  543. } catch (Exception $e) {
  544. $this->defaultView(UI::h('div', ['class'=>"alert alert-danger"], $e->getMessage()));
  545. }
  546. }
  547. public function rmAllAuthLogFilesAction() {
  548. session_write_close();
  549. try {
  550. $userLogin = User::getLogin();
  551. $today = date("Y-m-d");
  552. $cmd = "rm -v {$this->logPathPrefix}se-auth-*.log 2>&1";
  553. V::exec($cmd, $out, $ret);
  554. $this->authView(UI::h('div', ['class'=>"alert alert-success alert-dismissible"], [
  555. '<button type="button" class="close" data-dismiss="alert" aria-label="Close"><span aria-hidden="true">&times;</span></button>',
  556. (0 === $ret) ? "All Log Files Removed" : "Error?",
  557. " (return code: {$ret})",
  558. "<br>cmd: <code>{$cmd}</code>",
  559. UI::h('pre', [], implode("\n", $out))
  560. ]));
  561. } catch (AlertSuccessException $e) {
  562. $this->authView(UI::h('div', ['class'=>"alert alert-success"], $e->getMessage()));
  563. } catch (AlertWarningException $e) {
  564. $this->authView(UI::h('div', ['class'=>"alert alert-warning"], $e->getMessage()));
  565. } catch (Exception $e) {
  566. $this->authView(UI::h('div', ['class'=>"alert alert-danger"], $e->getMessage()));
  567. }
  568. }
  569. public function rmOldAuthLogFilesAction() {
  570. session_write_close();
  571. try {
  572. $userLogin = User::getLogin();
  573. $today = date("Y-m-d");
  574. $cmd = "ls -1 {$this->logPathPrefix}se-auth-*.log | grep -v '{$this->logPathPrefix}se-auth-{$today}-' | xargs rm -v 2>&1";
  575. V::exec($cmd, $out, $ret);
  576. $this->authView(UI::h('div', ['class'=>"alert alert-success alert-dismissible"], [
  577. '<button type="button" class="close" data-dismiss="alert" aria-label="Close"><span aria-hidden="true">&times;</span></button>',
  578. (0 === $ret) ? "Old Log Files Removed" : "Error?",
  579. " (return code: {$ret})",
  580. "<br>cmd: <code>{$cmd}</code>",
  581. UI::h('pre', [], implode("\n", $out))
  582. ]));
  583. } catch (AlertSuccessException $e) {
  584. $this->authView(UI::h('div', ['class'=>"alert alert-success"], $e->getMessage()));
  585. } catch (AlertWarningException $e) {
  586. $this->authView(UI::h('div', ['class'=>"alert alert-warning"], $e->getMessage()));
  587. } catch (Exception $e) {
  588. $this->authView(UI::h('div', ['class'=>"alert alert-danger"], $e->getMessage()));
  589. }
  590. }
  591. public function rmAuthLogFileAction() {
  592. session_write_close();
  593. try {
  594. $logName = $this->validateParamLogName(V::get('logName', '', $_REQUEST));
  595. $logPath = "{$this->logPathPrefix}se-auth-{$logName}.log";
  596. if (!file_exists($logPath)) throw new AlertWarningException("Log file not exists");
  597. unlink($logPath);
  598. throw new AlertSuccessException("File Removed");
  599. } catch (AlertSuccessException $e) {
  600. $this->authView(UI::h('div', ['class'=>"alert alert-success"], $e->getMessage()));
  601. } catch (AlertWarningException $e) {
  602. $this->authView(UI::h('div', ['class'=>"alert alert-warning"], $e->getMessage()));
  603. } catch (Exception $e) {
  604. $this->authView(UI::h('div', ['class'=>"alert alert-danger"], $e->getMessage()));
  605. }
  606. }
  607. }