api.apachezen.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382
  1. <?php
  2. /**
  3. * Just meditative web-log viewer
  4. */
  5. class ApacheZen {
  6. /**
  7. * Contains billing.ini config as key=>value
  8. *
  9. * @var array
  10. */
  11. protected $billCfg = array();
  12. /**
  13. * Default datasource file to read
  14. *
  15. * @var string
  16. */
  17. protected $logPath = '/var/log/httpd-access.log';
  18. /**
  19. * Default error log path
  20. *
  21. * @var string
  22. */
  23. protected $errorLogPath = '/var/log/httpd-error.log';
  24. /**
  25. * Default flow identifier to ignore self requests
  26. *
  27. * @var string
  28. */
  29. protected $flowId = 'apzjcb';
  30. /**
  31. * Count of lines to read from log
  32. *
  33. * @var int
  34. */
  35. protected $linesRead = 500;
  36. /**
  37. * Count of lines to render in viewport
  38. *
  39. * @var int
  40. */
  41. protected $linesRender = 40;
  42. /**
  43. * Default container refresh timeout in ms.
  44. *
  45. * @var int
  46. */
  47. protected $timeout = 1000;
  48. /**
  49. * Current zen source
  50. *
  51. * @var string
  52. */
  53. protected $currentSource = '';
  54. /**
  55. * Contains system grep path
  56. *
  57. * @var string
  58. */
  59. protected $grep = '';
  60. /**
  61. * Contains system tail path
  62. *
  63. * @var string
  64. */
  65. protected $tail = '';
  66. /**
  67. * Contains system cat path
  68. *
  69. * @var string
  70. */
  71. protected $cat = '';
  72. /**
  73. * Dynamic view-port default style
  74. *
  75. * @var string
  76. */
  77. protected $renderStyle = 'font-family: monospace;';
  78. /**
  79. * Render access log or errorlog flag
  80. *
  81. * @var bool
  82. */
  83. protected $errorLogFlag = false;
  84. /**
  85. * owls are not what they seem
  86. */
  87. public function __construct($errorLogFlag = false) {
  88. $this->setLogType($errorLogFlag);
  89. $this->loadConfigs();
  90. $this->setDataSource();
  91. }
  92. /**
  93. * Predefined routes etc..
  94. */
  95. const URL_ME = '?module=apachezen';
  96. const URL_BACK = '?module=taskbar';
  97. const URL_CODE = 'https://github.com/nightflyza/WolfRecorder/';
  98. const ROUTE_ERRORS = 'errorlog';
  99. const ROUTE_PHPERR = 'phperrors';
  100. const ERROR_FILTER = 'on line';
  101. /**
  102. * Preloads required configs for further usage
  103. *
  104. * @global object $ubillingConfig
  105. *
  106. * @return void
  107. */
  108. protected function loadConfigs() {
  109. global $ubillingConfig;
  110. $this->billCfg = $ubillingConfig->getBinpaths();
  111. $this->grep = $this->billCfg['GREP'];
  112. $this->tail = $this->billCfg['TAIL'];
  113. $this->cat = $this->billCfg['CAT'];
  114. }
  115. /**
  116. * Sets current instance log type
  117. *
  118. * @param bool $errorLogFlag
  119. *
  120. * return void
  121. */
  122. protected function setLogType($errorLogFlag = false) {
  123. $this->errorLogFlag = $errorLogFlag;
  124. }
  125. /**
  126. * Sets alternative datasource path
  127. *
  128. * @return void
  129. */
  130. protected function setDataSource() {
  131. //access logs
  132. if (!file_exists($this->logPath)) {
  133. $alternatePath = '/var/log/apache2/access.log';
  134. if (file_exists($alternatePath)) {
  135. //Debian Linux?
  136. $this->logPath = $alternatePath;
  137. }
  138. }
  139. //errors log
  140. if (!file_exists($this->errorLogPath)) {
  141. $alternateErrorsPath = '/var/log/apache2/error.log';
  142. if (file_exists($alternateErrorsPath)) {
  143. $this->errorLogPath = $alternateErrorsPath;
  144. }
  145. }
  146. }
  147. /**
  148. * Checks is datasource file exists
  149. *
  150. * @param bool $errorLog
  151. *
  152. * @return bool
  153. */
  154. protected function dataSourceExists($errorLog = false) {
  155. $result = false;
  156. if ($errorLog) {
  157. if (file_exists($this->errorLogPath)) {
  158. $result = true;
  159. }
  160. } else {
  161. if (file_exists($this->logPath)) {
  162. $result = true;
  163. }
  164. }
  165. return($result);
  166. }
  167. /**
  168. * Renders the few last lines from data source.
  169. *
  170. * @param bool $errorLog
  171. *
  172. * @return string
  173. */
  174. public function render() {
  175. $result = '';
  176. $readSource = ($this->errorLogFlag) ? $this->errorLogPath : $this->logPath;
  177. if ($this->dataSourceExists($this->errorLogFlag)) {
  178. $this->currentSource = $readSource;
  179. $filters = $this->grep . ' -v ' . $this->flowId; //ignore itself
  180. $filters .= '| ' . $this->grep . ' -v fwtbt'; //ignore For Whom The Bell Tolls
  181. $command = $this->tail . ' -n ' . $this->linesRead . ' ' . $readSource . ' | ' . $filters . ' | ' . $this->tail . ' -n ' . $this->linesRender;
  182. $resultRaw = shell_exec($command);
  183. if (!empty($resultRaw)) {
  184. $rows = '';
  185. $resultRaw = explodeRows($resultRaw);
  186. $resultRaw = array_reverse($resultRaw);
  187. if (!empty($resultRaw)) {
  188. foreach ($resultRaw as $io => $eachLine) {
  189. if (!empty($eachLine)) {
  190. $cells = wf_TableCell(htmlentities(strip_tags($eachLine)));
  191. $rows .= wf_TableRow($cells, 'row5');
  192. }
  193. }
  194. }
  195. $result .= wf_TableBody($rows, '100%', 0, '', 'style="' . $this->renderStyle . '"');
  196. } else {
  197. $messages = new UbillingMessageHelper();
  198. $result .= $messages->getStyledMessage(__('Nothing to show'), 'warning');
  199. }
  200. } else {
  201. $messages = new UbillingMessageHelper();
  202. $result .= $messages->getStyledMessage(__('File not exist') . ': ' . $readSource, 'error');
  203. }
  204. return($result);
  205. }
  206. /**
  207. * Renders latest PHP errors in scripts
  208. *
  209. * @return string
  210. */
  211. public function renderPHPErrors() {
  212. $result = '';
  213. $readSource = ($this->errorLogFlag) ? $this->errorLogPath : $this->logPath;
  214. if ($this->dataSourceExists(true)) {
  215. $this->currentSource = $readSource;
  216. $filters = ' ' . $this->grep . ' "' . self::ERROR_FILTER . '"';
  217. $command = $this->tail . ' -n ' . ($this->linesRead * 10) . ' ' . $readSource . ' | ' . $filters . ' | ' . $this->tail . ' -n ' . $this->linesRender;
  218. $resultRaw = shell_exec($command);
  219. $stripPaths = array(
  220. '/usr/local/www/apache22',
  221. '/usr/local/www/apache24',
  222. '/var/www/html/',
  223. '/data/',
  224. 'dev/WolfRecorder/',
  225. 'wolfrecorder/',
  226. 'wr/'
  227. );
  228. $hlights = array(
  229. 'PHP Notice' => 'de6666',
  230. 'PHP Warning' => 'd04545',
  231. 'PHP Parse error' => 'ae0000',
  232. 'PHP Fatal error' => 'e00808'
  233. );
  234. if (!empty($resultRaw)) {
  235. $rows = '';
  236. $resultRaw = explodeRows($resultRaw);
  237. $resultRaw = array_reverse($resultRaw);
  238. if (!empty($resultRaw)) {
  239. foreach ($resultRaw as $io => $eachLine) {
  240. if (!empty($eachLine)) {
  241. $cleanMessage = strip_tags($eachLine);
  242. foreach ($stripPaths as $ia => $eachStripPath) {
  243. $cleanMessage = str_replace($eachStripPath, '', $cleanMessage);
  244. }
  245. //tryin to detect billing code line
  246. if (ispos($cleanMessage, 'in') AND ispos($cleanMessage, 'on line')) {
  247. preg_match('!in (.*?) on!si', $cleanMessage, $sourceFiles);
  248. preg_match('!on line (.*?),!si', $cleanMessage, $codeLines);
  249. $lineOfCode = '';
  250. if (isset($codeLines[1])) {
  251. $codeLines = explode(' ', $codeLines[1]);
  252. $codeLines = $codeLines[0];
  253. $lineOfCode = ubRouting::filters($codeLines, 'int');
  254. }
  255. if (isset($sourceFiles[1])) {
  256. if (file_exists($sourceFiles[1])) {
  257. $sourceUrl = self::URL_CODE . 'blob/master/' . $sourceFiles[1];
  258. $sourceLink = wf_Link($sourceUrl, $sourceFiles[1], false, '', 'target="_BLANK"');
  259. $cleanMessage = str_replace($sourceFiles[1], $sourceLink, $cleanMessage);
  260. if (!empty($lineOfCode)) {
  261. $lineUrl = $sourceUrl . '#L' . $lineOfCode;
  262. $lineMark = 'on line ' . $lineOfCode . ',';
  263. $lineLink = wf_Link($lineUrl, wf_tag('u') . $lineMark . wf_tag('u', true), false, '', 'target="_BLANK"');
  264. $cleanMessage = str_replace($lineMark, $lineLink, $cleanMessage);
  265. }
  266. }
  267. }
  268. }
  269. //coloring results
  270. foreach ($hlights as $eachString => $eachColor) {
  271. $cleanMessage = $this->colorize($cleanMessage, $eachString, $eachColor);
  272. }
  273. $cells = wf_TableCell($cleanMessage);
  274. $rows .= wf_TableRow($cells, 'row5');
  275. }
  276. }
  277. }
  278. $result .= wf_TableBody($rows, '100%', 0, '');
  279. } else {
  280. $messages = new UbillingMessageHelper();
  281. $result .= $messages->getStyledMessage(__('Nothing to show'), 'warning');
  282. }
  283. } else {
  284. $messages = new UbillingMessageHelper();
  285. $result .= $messages->getStyledMessage(__('File not exist') . ': ' . $readSource, 'error');
  286. }
  287. return($result);
  288. }
  289. /**
  290. * Paints some subStr into some color if its appears in text
  291. *
  292. * @param string $text
  293. * @param string $subStr
  294. * @param string $color
  295. *
  296. * @return string
  297. */
  298. protected function colorize($text, $subStr, $color) {
  299. $result = '';
  300. if (!empty($text) AND ! empty($subStr) AND ! empty($color)) {
  301. $colorizedSubstr = wf_tag('font', false, '', 'style="color:#' . $color . ';"');
  302. $colorizedSubstr .= $subStr;
  303. $colorizedSubstr .= wf_tag('font', true);
  304. $result = str_replace($subStr, $colorizedSubstr, $text);
  305. } else {
  306. $result = $text;
  307. }
  308. return($result);
  309. }
  310. /**
  311. * Returns current container flowID
  312. *
  313. * @return string
  314. */
  315. public function getFlowId() {
  316. return($this->flowId);
  317. }
  318. /**
  319. * Returns current instance refresh timeout
  320. *
  321. * @return int
  322. */
  323. public function getTimeout() {
  324. return($this->timeout);
  325. }
  326. /**
  327. * Just returns default module controls
  328. *
  329. * @return string
  330. */
  331. public function controls() {
  332. $result = '';
  333. $result .= wf_BackLink(self::URL_BACK) . ' ';
  334. $result .= wf_Link(self::URL_ME, wf_img('skins/zen.png') . ' Access ' . __('Zen'), false, 'ubButton');
  335. $result .= wf_Link(self::URL_ME . '&' . self::ROUTE_ERRORS . '=true', wf_img('skins/zen.png') . ' Error ' . __('Zen'), false, 'ubButton');
  336. $result .= wf_Link(self::URL_ME . '&' . self::ROUTE_PHPERR . '=true', wf_img('skins/icon_php.png') . ' ' . __('PHP errors'), false, 'ubButton');
  337. return($result);
  338. }
  339. /**
  340. * Returns current data source
  341. *
  342. * @return string
  343. */
  344. public function getCurrentSource() {
  345. return($this->currentSource);
  346. }
  347. }