Debug.php 25 KB

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