api.apachezen.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386
  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=report_sysload';
  97. const URL_TASKBAR='?module=taskbar';
  98. const URL_CODE = 'https://github.com/nightflyza/Ubilling/';
  99. const ROUTE_ERRORS = 'errorlog';
  100. const ROUTE_PHPERR = 'phperrors';
  101. const ERROR_FILTER = 'on line';
  102. /**
  103. * Preloads required configs for further usage
  104. *
  105. * @global object $ubillingConfig
  106. *
  107. * @return void
  108. */
  109. protected function loadConfigs() {
  110. global $ubillingConfig;
  111. $this->billCfg = $ubillingConfig->getBilling();
  112. $this->grep = $this->billCfg['GREP'];
  113. $this->tail = $this->billCfg['TAIL'];
  114. $this->cat = $this->billCfg['CAT'];
  115. }
  116. /**
  117. * Sets current instance log type
  118. *
  119. * @param bool $errorLogFlag
  120. *
  121. * return void
  122. */
  123. protected function setLogType($errorLogFlag = false) {
  124. $this->errorLogFlag = $errorLogFlag;
  125. }
  126. /**
  127. * Sets alternative datasource path
  128. *
  129. * @return void
  130. */
  131. protected function setDataSource() {
  132. //access logs
  133. if (!file_exists($this->logPath)) {
  134. $alternatePath = '/var/log/apache2/access.log';
  135. if (file_exists($alternatePath)) {
  136. //Debian Linux?
  137. $this->logPath = $alternatePath;
  138. }
  139. }
  140. //errors log
  141. if (!file_exists($this->errorLogPath)) {
  142. $alternateErrorsPath = '/var/log/apache2/error.log';
  143. if (file_exists($alternateErrorsPath)) {
  144. $this->errorLogPath = $alternateErrorsPath;
  145. }
  146. }
  147. }
  148. /**
  149. * Checks is datasource file exists
  150. *
  151. * @param bool $errorLog
  152. *
  153. * @return bool
  154. */
  155. protected function dataSourceExists($errorLog = false) {
  156. $result = false;
  157. if ($errorLog) {
  158. if (file_exists($this->errorLogPath)) {
  159. $result = true;
  160. }
  161. } else {
  162. if (file_exists($this->logPath)) {
  163. $result = true;
  164. }
  165. }
  166. return($result);
  167. }
  168. /**
  169. * Renders the few last lines from data source.
  170. *
  171. * @param bool $errorLog
  172. *
  173. * @return string
  174. */
  175. public function render() {
  176. $result = '';
  177. $readSource = ($this->errorLogFlag) ? $this->errorLogPath : $this->logPath;
  178. if ($this->dataSourceExists($this->errorLogFlag)) {
  179. $this->currentSource = $readSource;
  180. $filters = $this->grep . ' -v ' . $this->flowId; //ignore itself
  181. $filters .= '| ' . $this->grep . ' -v fwtbt'; //ignore For Whom The Bell Tolls
  182. $command = $this->tail . ' -n ' . $this->linesRead . ' ' . $readSource . ' | ' . $filters . ' | ' . $this->tail . ' -n ' . $this->linesRender;
  183. $resultRaw = shell_exec($command);
  184. if (!empty($resultRaw)) {
  185. $rows = '';
  186. $resultRaw = explodeRows($resultRaw);
  187. $resultRaw = array_reverse($resultRaw);
  188. if (!empty($resultRaw)) {
  189. foreach ($resultRaw as $io => $eachLine) {
  190. if (!empty($eachLine)) {
  191. $cells = wf_TableCell(htmlentities(strip_tags($eachLine)));
  192. $rows .= wf_TableRow($cells, 'row5');
  193. }
  194. }
  195. }
  196. $result .= wf_TableBody($rows, '100%', 0, '', 'style="' . $this->renderStyle . '"');
  197. } else {
  198. $messages = new UbillingMessageHelper();
  199. $result .= $messages->getStyledMessage(__('Nothing to show'), 'warning');
  200. }
  201. } else {
  202. $messages = new UbillingMessageHelper();
  203. $result .= $messages->getStyledMessage(__('File not exist') . ': ' . $readSource, 'error');
  204. }
  205. return($result);
  206. }
  207. /**
  208. * Renders latest PHP errors in scripts
  209. *
  210. * @return string
  211. */
  212. public function renderPHPErrors() {
  213. $result = '';
  214. $readSource = ($this->errorLogFlag) ? $this->errorLogPath : $this->logPath;
  215. if ($this->dataSourceExists(true)) {
  216. $this->currentSource = $readSource;
  217. $filters = ' ' . $this->grep . ' "' . self::ERROR_FILTER . '"';
  218. $command = $this->tail . ' -n ' . ($this->linesRead * 10) . ' ' . $readSource . ' | ' . $filters . ' | ' . $this->tail . ' -n ' . $this->linesRender;
  219. $resultRaw = shell_exec($command);
  220. $stripPaths = array(
  221. '/usr/local/www/apache22',
  222. '/usr/local/www/apache24',
  223. '/var/www/html/',
  224. '/data/',
  225. 'dev/ubilling/',
  226. 'billing/'
  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. $backUrl = self::URL_BACK;
  334. if (ubRouting::get('back') == 'tb') {
  335. $backUrl = self::URL_TASKBAR;
  336. }
  337. $result .= wf_BackLink($backUrl) . ' ';
  338. $result .= wf_Link(self::URL_ME, wf_img('skins/zen.png') . ' Access ' . __('Zen'), false, 'ubButton');
  339. $result .= wf_Link(self::URL_ME . '&' . self::ROUTE_ERRORS . '=true', wf_img('skins/zen.png') . ' Error ' . __('Zen'), false, 'ubButton');
  340. $result .= wf_Link(self::URL_ME . '&' . self::ROUTE_PHPERR . '=true', wf_img('skins/icon_php.png') . ' ' . __('PHP errors'), false, 'ubButton');
  341. return($result);
  342. }
  343. /**
  344. * Returns current data source
  345. *
  346. * @return string
  347. */
  348. public function getCurrentSource() {
  349. return($this->currentSource);
  350. }
  351. }