index.php 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  1. <?php
  2. if (cfr('OPTION82')) {
  3. $altercfg = $ubillingConfig->getAlter();
  4. if (@$altercfg['OPT82_ENABLED']) {
  5. $billing_config = $ubillingConfig->getBilling();
  6. /**
  7. * Parse option 82 leases from dhcpd.log
  8. *
  9. * @param $billing_config - preprocessed billing.ini
  10. * @param $altercfg - preprocessed alter.ini
  11. *
  12. * @return array
  13. */
  14. function opt82_LoadLeases($billing_config, $altercfg, $busyLeases) {
  15. $sudo = $billing_config['SUDO'];
  16. $cat = $billing_config['CAT'];
  17. $grep = $billing_config['GREP'];
  18. $tail = $billing_config['TAIL'];
  19. $leasePath = $altercfg['NMLEASES'];
  20. $leasemark = '(with opt82)';
  21. $command = $sudo . ' ' . $cat . ' ' . $leasePath . ' | ' . $grep . ' "' . $leasemark . '" | ' . $tail . ' -n 100';
  22. $rawData = shell_exec($command);
  23. $rawArr = array();
  24. $rawArr = explodeRows($rawData);
  25. $result = array();
  26. if (!empty($rawArr)) {
  27. foreach ($rawArr as $eachline) {
  28. $explodeLine = preg_split('/\s+/', $eachline);
  29. //log have normal format
  30. if (isset($explodeLine[9]) AND ( isset($explodeLine[11])) AND ( isset($explodeLine[7]))) {
  31. $leaseIp = $explodeLine[7];
  32. $remoteId = $explodeLine[9];
  33. $circuitID = $explodeLine[11];
  34. //check for new lease?
  35. if (!isset($busyLeases[$remoteId . '|' . $circuitID])) {
  36. $result[$remoteId . '|' . $circuitID] = $remoteId . '->' . $circuitID;
  37. }
  38. }
  39. }
  40. }
  41. return ($result);
  42. }
  43. /**
  44. * get all busy option 82 leases
  45. *
  46. * @return array - list of uses leases for isset check
  47. */
  48. function opt82_GetAllUsedLeases() {
  49. $query = "SELECT * from `nethosts` WHERE `option` IS NOT NULL";
  50. $allNethosts = simple_queryall($query);
  51. $result = array();
  52. if (!empty($allNethosts)) {
  53. foreach ($allNethosts as $io => $each) {
  54. if (!empty($each['option'])) {
  55. $result[] = $each['option'];
  56. }
  57. }
  58. $result = array_flip($result);
  59. }
  60. return ($result);
  61. }
  62. /**
  63. * Get current nethost options by user`s login
  64. *
  65. * @param $login - user`s login
  66. *
  67. * @return array
  68. */
  69. function opt82_GetCurrentOptions($login) {
  70. $login = mysql_real_escape_string($login);
  71. $userIp = zb_UserGetIP($login);
  72. $nethost_q = "SELECT * from `nethosts` WHERE `ip`='" . $userIp . "'";
  73. $nethostRaw = simple_query($nethost_q);
  74. $result = array();
  75. $result['hostid'] = '';
  76. $result['hostip'] = '';
  77. $result['netid'] = '';
  78. $result['remoteid'] = '';
  79. $result['circuitid'] = '';
  80. if (!empty($nethostRaw)) {
  81. $result['hostid'] = $nethostRaw['id'];
  82. $result['hostip'] = $nethostRaw['ip'];
  83. $result['netid'] = $nethostRaw['netid'];
  84. if (!empty($nethostRaw['option'])) {
  85. $explode = explode('|', $nethostRaw['option']);
  86. if (isset($explode[1])) {
  87. $result['remoteid'] = $explode[0];
  88. $result['circuitid'] = $explode[1];
  89. }
  90. }
  91. }
  92. return ($result);
  93. }
  94. /**
  95. * Returns nethost option modify form
  96. *
  97. * @param $allLeases - all available leases parsed from log
  98. * @param $login - user`s login
  99. *
  100. * @return string
  101. */
  102. function web_opt82_ShowForm($allLeases, $login) {
  103. $result = '';
  104. $currentData = opt82_GetCurrentOptions($login);
  105. $cells = wf_TableCell(__('IP'));
  106. $cells .= wf_TableCell($currentData['hostip']);
  107. $rows = wf_TableRow($cells, 'row3');
  108. $cells = wf_TableCell(__('Remote-ID'));
  109. $cells .= wf_TableCell($currentData['remoteid']);
  110. $rows .= wf_TableRow($cells, 'row3');
  111. $cells = wf_TableCell(__('Circuit-ID'));
  112. $cells .= wf_TableCell($currentData['circuitid']);
  113. $rows .= wf_TableRow($cells, 'row3');
  114. $currentTable = wf_TableBody($rows, '30%', '0', '');
  115. $result .= $currentTable;
  116. $result .= wf_delimiter();
  117. $inputs = wf_Selector('newopt82', $allLeases, __('New DHCP option 82'), '', true);
  118. $inputs .= wf_HiddenInput('edithostid', $currentData['hostid']);
  119. $inputs .= wf_HiddenInput('edithostip', $currentData['hostip']);
  120. $inputs .= wf_CheckInput('setrandomopt82', __('Set random'), true, false);
  121. $inputs .= wf_Submit(__('Save'));
  122. $form = wf_Form('', 'POST', $inputs, 'glamour');
  123. $result .= $form;
  124. $result .= wf_delimiter();
  125. $result .= web_UserControls($login);
  126. return ($result);
  127. }
  128. /**
  129. * Update some option in "remote-id|circuit-id" view into the nethosts
  130. *
  131. * @param $ip - nethost ip
  132. * @param $option - nethost option to set
  133. *
  134. * @return void
  135. */
  136. function opt82_SetOption($ip, $option) {
  137. $query = "UPDATE `nethosts` SET `option`='" . $option . "' WHERE `ip`='" . $ip . "'";
  138. nr_query($query);
  139. log_register("OPT82 SET " . $ip . ' `' . $option . '`');
  140. }
  141. /**
  142. * checks for available dhcp82 networks
  143. *
  144. * @return bool
  145. */
  146. function opt82_NetsAvailable() {
  147. $query = "SELECT `id` from `networks` WHERE `nettype`='dhcp82';";
  148. $rawData = simple_query($query);
  149. if (!empty($rawData)) {
  150. return (true);
  151. } else {
  152. return (false);
  153. }
  154. }
  155. if (wf_CheckGet(array('username'))) {
  156. $login = $_GET['username'];
  157. if (opt82_NetsAvailable()) {
  158. $busyLeases = opt82_GetAllUsedLeases();
  159. $allLeases = opt82_LoadLeases($billing_config, $altercfg, $busyLeases);
  160. if (wf_CheckPost(array('edithostid', 'edithostip'))) {
  161. if (!isset($_POST['setrandomopt82'])) {
  162. if (wf_CheckPost(array('newopt82'))) {
  163. opt82_SetOption($_POST['edithostip'], $_POST['newopt82']);
  164. } else {
  165. show_error(__('No option 82 get'));
  166. }
  167. } else {
  168. $randomRemoteId = '14:88:' . rand(10000, 99999);
  169. $randomCircuitId = rand(10000, 99999);
  170. $randomNewOpt = $randomRemoteId . '|' . $randomCircuitId;
  171. opt82_SetOption($_POST['edithostip'], $randomNewOpt);
  172. }
  173. multinet_rebuild_all_handlers();
  174. rcms_redirect("?module=pl_option82&username=" . $login);
  175. }
  176. show_window(__('Current value'), web_opt82_ShowForm($allLeases, $login));
  177. } else {
  178. show_error(__('No DHCP option 82 networks available'));
  179. }
  180. }
  181. } else {
  182. show_error(__('This module is disabled'));
  183. }
  184. } else {
  185. show_error(__('You cant control this module'));
  186. }
  187. ?>