api.dhcppl.php 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281
  1. <?php
  2. /**
  3. * User DHCP log viewer
  4. */
  5. class DHCPPL {
  6. /**
  7. * Contains billing.ini config as key=>value
  8. *
  9. * @var array
  10. */
  11. protected $billCfg = array();
  12. /**
  13. * Contains alter.ini config as key=>value
  14. *
  15. * @var array
  16. */
  17. protected $altCfg = array();
  18. /**
  19. * Default datasource file to read
  20. *
  21. * @var string
  22. */
  23. protected $logPath = '/var/log/messages';
  24. /**
  25. * Default flow identifier to ignore self requests
  26. *
  27. * @var string
  28. */
  29. protected $flowId = 'pldhzjcb';
  30. /**
  31. * Count of lines to render in viewport
  32. *
  33. * @var int
  34. */
  35. protected $linesRender = 30;
  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 system cat path
  56. *
  57. * @var string
  58. */
  59. protected $cat = '';
  60. /**
  61. * Contains sudo command path
  62. *
  63. * @var string
  64. */
  65. protected $sudo = '';
  66. /**
  67. * DHCP option82 enabled flag
  68. *
  69. * @var bool
  70. */
  71. protected $opt82Flag = false;
  72. /**
  73. * Contains current instance user login
  74. *
  75. * @var string
  76. */
  77. protected $userLogin = '';
  78. /**
  79. * Contains current instance user IP
  80. *
  81. * @var string
  82. */
  83. protected $userIp = '';
  84. /**
  85. * Contains current instance user MAC
  86. *
  87. * @var string
  88. */
  89. protected $userMac = '';
  90. /**
  91. * Dynamic view-port default style
  92. *
  93. * @var string
  94. */
  95. protected $renderStyle = 'font-family: monospace;';
  96. /**
  97. * System messages helper instance
  98. *
  99. * @var object
  100. */
  101. protected $messages = '';
  102. /**
  103. * Black wings will grow when you`re dead
  104. *
  105. * @param string $userLogin
  106. * @param string $userIp
  107. * @param string $userMac
  108. *
  109. */
  110. public function __construct($userLogin = '', $userIp = '', $userMac = '') {
  111. $this->initMessages();
  112. $this->loadConfigs();
  113. $this->setOptions($userLogin, $userIp, $userMac);
  114. }
  115. /**
  116. * Predefined routes etc..
  117. */
  118. const URL_ME = '?module=pl_dhcp';
  119. /**
  120. * Preloads required configs for further usage
  121. *
  122. * @global object $ubillingConfig
  123. *
  124. * @return void
  125. */
  126. protected function loadConfigs() {
  127. global $ubillingConfig;
  128. $this->billCfg = $ubillingConfig->getBilling();
  129. $this->altCfg = $ubillingConfig->getAlter();
  130. $this->sudo = $this->billCfg['SUDO'];
  131. $this->grep = $this->billCfg['GREP'];
  132. $this->tail = $this->billCfg['TAIL'];
  133. $this->cat = $this->billCfg['CAT'];
  134. $this->opt82Flag = $this->altCfg['OPT82_ENABLED'];
  135. $this->logPath = $ubillingConfig->getAlterParam('NMLEASES');
  136. }
  137. /**
  138. * Sets instance properties
  139. *
  140. * @param string $userLogin
  141. * @param string $userIp
  142. * @param string $userMac
  143. *
  144. * @return void
  145. */
  146. protected function setOptions($userLogin = '', $userIp = '', $userMac = '') {
  147. $this->userLogin = $userLogin;
  148. $this->userIp = $userIp;
  149. $this->userMac = $userMac;
  150. }
  151. /**
  152. * Inits system message helper instance for further usage
  153. *
  154. * @return void
  155. */
  156. protected function initMessages() {
  157. $this->messages = new UbillingMessageHelper();
  158. }
  159. /**
  160. * Checks is datasource file exists
  161. *
  162. * @return bool
  163. */
  164. protected function dataSourceExists() {
  165. $result = false;
  166. if (file_exists($this->logPath)) {
  167. $result = true;
  168. }
  169. return($result);
  170. }
  171. /**
  172. * Returns parsed data source records as string
  173. *
  174. * @return string
  175. */
  176. protected function getLogData() {
  177. $result = '';
  178. if ($this->userIp AND $this->userMac) {
  179. $macParse = $this->userMac;
  180. $grepPath = $this->grep;
  181. if ($this->opt82Flag) {
  182. $grepPath = $this->grep . ' -E';
  183. $macParse = '"( ' . $this->userIp . '(:)? )|(' . $this->userMac . ')"';
  184. }
  185. $command = $this->sudo . ' ' . $this->cat . ' ' . $this->logPath . ' | ' . $grepPath . ' ' . $macParse . ' | ' . $this->tail . ' -n ' . $this->linesRender;
  186. $result = shell_exec($command);
  187. }
  188. return($result);
  189. }
  190. /**
  191. * Returns user mac label
  192. *
  193. * @return string
  194. */
  195. public function getMacLabel() {
  196. $result = '';
  197. $result = $this->messages->getStyledMessage(wf_tag('h2') . __('Current MAC') . ': ' . $this->userMac . wf_tag('h2', true), 'info');
  198. return($result);
  199. }
  200. /**
  201. * Renders the parsed log data from data source.
  202. *
  203. * @return string
  204. */
  205. public function render() {
  206. /**
  207. * Commence, Destroy, Exploit, Enjoy, A world to spoil, The pistols recoil
  208. */
  209. $result = '';
  210. if ($this->dataSourceExists()) {
  211. $resultRaw = $this->getLogData();
  212. if (!empty($resultRaw)) {
  213. $rows = '';
  214. $resultRaw = explodeRows($resultRaw);
  215. $resultRaw= array_reverse($resultRaw);
  216. if (!empty($resultRaw)) {
  217. foreach ($resultRaw as $io => $eachLine) {
  218. if (!empty($eachLine)) {
  219. $cells = wf_TableCell(htmlentities(strip_tags($eachLine)));
  220. $rows .= wf_TableRow($cells, 'row5');
  221. }
  222. }
  223. }
  224. $result .= wf_TableBody($rows, '100%', 0, '', 'style="' . $this->renderStyle . '"');
  225. } else {
  226. $result .= $this->messages->getStyledMessage(__('Nothing to show'), 'warning');
  227. }
  228. } else {
  229. $result .= $this->messages->getStyledMessage(__('File not exist') . ': ' . $this->logPath, 'error');
  230. }
  231. /**
  232. * Command, Ignite, Commence, The Fight, A strike of spite, The rush of the fright
  233. */
  234. return($result);
  235. }
  236. /**
  237. * Returns current container flowID
  238. *
  239. * @return string
  240. */
  241. public function getFlowId() {
  242. return($this->flowId);
  243. }
  244. /**
  245. * Returns current instance refresh timeout
  246. *
  247. * @return int
  248. */
  249. public function getTimeout() {
  250. return($this->timeout);
  251. }
  252. }