api.dhcpzen.php 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. <?php
  2. /**
  3. * Just meditative dhcp-log viewer
  4. */
  5. class DHCPZen {
  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/messages';
  18. /**
  19. * Default flow identifier to ignore self requests
  20. *
  21. * @var string
  22. */
  23. protected $flowId = 'dhzjcb';
  24. /**
  25. * Count of lines to read from log
  26. *
  27. * @var int
  28. */
  29. protected $linesRead = 200;
  30. /**
  31. * Count of lines to render in viewport
  32. *
  33. * @var int
  34. */
  35. protected $linesRender = 40;
  36. /**
  37. * Default container refresh timeout in ms.
  38. *
  39. * @var int
  40. */
  41. protected $timeout = 1000;
  42. /**
  43. * Contains system grep path
  44. *
  45. * @var string
  46. */
  47. protected $grep = '';
  48. /**
  49. * Contains system tail path
  50. *
  51. * @var string
  52. */
  53. protected $tail = '';
  54. /**
  55. * Contains sudo command path
  56. *
  57. * @var string
  58. */
  59. protected $sudo = '';
  60. /**
  61. * Dynamic view-port default style
  62. *
  63. * @var string
  64. */
  65. protected $renderStyle = 'font-family: monospace;';
  66. /**
  67. * owls are not what they seem
  68. */
  69. public function __construct() {
  70. $this->loadConfigs();
  71. }
  72. /**
  73. * Predefined routes etc..
  74. */
  75. const URL_ME = '?module=dhcpdzen';
  76. /**
  77. * Preloads required configs for further usage
  78. *
  79. * @global object $ubillingConfig
  80. *
  81. * @return void
  82. */
  83. protected function loadConfigs() {
  84. global $ubillingConfig;
  85. $this->billCfg = $ubillingConfig->getBilling();
  86. $this->sudo = $this->billCfg['SUDO'];
  87. $this->grep = $this->billCfg['GREP'];
  88. $this->tail = $this->billCfg['TAIL'];
  89. $this->logPath = $ubillingConfig->getAlterParam('NMLEASES');
  90. }
  91. /**
  92. * Checks is datasource file exists
  93. *
  94. * @return bool
  95. */
  96. protected function dataSourceExists() {
  97. $result = false;
  98. if (file_exists($this->logPath)) {
  99. $result = true;
  100. }
  101. return($result);
  102. }
  103. /**
  104. * Renders the few last lines from data source.
  105. *
  106. * @return string
  107. */
  108. public function render() {
  109. $result = '';
  110. if ($this->dataSourceExists()) {
  111. $command = $this->sudo . ' ' . $this->tail . ' -n ' . $this->linesRead . ' ' . $this->logPath . ' | ' . $this->tail . ' -n ' . $this->linesRender;
  112. $resultRaw = shell_exec($command);
  113. if (!empty($resultRaw)) {
  114. $rows = '';
  115. $resultRaw = explodeRows($resultRaw);
  116. $resultRaw = array_reverse($resultRaw);
  117. if (!empty($resultRaw)) {
  118. foreach ($resultRaw as $io => $eachLine) {
  119. if (!empty($eachLine)) {
  120. $cells = wf_TableCell(htmlentities(strip_tags($eachLine)));
  121. $rows .= wf_TableRow($cells, 'row5');
  122. }
  123. }
  124. }
  125. $result .= wf_TableBody($rows, '100%', 0, '', 'style="' . $this->renderStyle . '"');
  126. } else {
  127. $messages = new UbillingMessageHelper();
  128. $result .= $messages->getStyledMessage(__('Nothing to show'), 'warning');
  129. }
  130. } else {
  131. $messages = new UbillingMessageHelper();
  132. $result .= $messages->getStyledMessage(__('File not exist') . ': ' . $this->logPath, 'error');
  133. }
  134. return($result);
  135. }
  136. /**
  137. * Returns current container flowID
  138. *
  139. * @return string
  140. */
  141. public function getFlowId() {
  142. return($this->flowId);
  143. }
  144. /**
  145. * Returns current instance refresh timeout
  146. *
  147. * @return int
  148. */
  149. public function getTimeout() {
  150. return($this->timeout);
  151. }
  152. }