index.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  1. <?php
  2. $altCfg = $ubillingConfig->getAlter();
  3. if ($altCfg['MIKROTIK_SUPPORT']) {
  4. if (cfr('MTEXTCONF')) {
  5. class MTExtConf {
  6. // Private variables:
  7. private $_id = null;
  8. private $_ip = null;
  9. private $_if = array();
  10. private $api = null;
  11. private $form = null;
  12. private $UseVersionTelepathy = false;
  13. private $WEBPort = 80;
  14. private $SNMPCommunity = 'public';
  15. private $options = array();
  16. private $config = array();
  17. public $nasOptsForTitle = '';
  18. // Constants of class:
  19. const FORM_NAME = 'opts';
  20. const URL_NAS_LIST = '?module=nas';
  21. public function __construct() {
  22. /* Filter NAS'es id: */
  23. $this->_id = filter_input(INPUT_GET, 'nasid', FILTER_SANITIZE_NUMBER_INT);
  24. $this->_ip = zb_NasGetIpById($this->_id);
  25. /* Load APIs: */
  26. $this->api = new RouterOS();
  27. $this->form = new InputForm();
  28. /* Get NAS current options: */
  29. $this->options = zb_NasOptionsGet($this->_id);
  30. $tmpNASData = zb_NasGetData($this->_id);
  31. /* Get configurtion: */
  32. $alter = rcms_parse_ini_file(CONFIG_PATH . 'alter.ini');
  33. $this->config['PASSWORDSHIDE'] = ( !empty($alter['PASSWORDSHIDE']) ) ? true : false;
  34. $this->UseVersionTelepathy = ( !empty($alter['ROUTEROS_VERSION_TELEPATHY']) ) ? true : false;
  35. $this->WEBPort = ( !empty($alter['ROUTEROS_VERSION_GET_WEB_PORT']) ) ? $alter['ROUTEROS_VERSION_GET_WEB_PORT'] : '80';
  36. $this->SNMPCommunity = ( !empty($alter['ROUTEROS_VERSION_GET_SNMP_COMMUNITY']) ) ? $alter['ROUTEROS_VERSION_GET_SNMP_COMMUNITY'] : 'public';
  37. if (!empty($tmpNASData)) {
  38. $cidrDATA = multinet_get_network_params($tmpNASData['netid']);
  39. $this->nasOptsForTitle.= ': ' . wf_nbsp() . $tmpNASData['nasname'] . ' ' . $tmpNASData['nasip'];
  40. if (!empty($cidrDATA)) {
  41. $this->nasOptsForTitle.= wf_nbsp(3) . ' for network: ' . wf_nbsp() . $tmpNASData['netid'] . ': ' . ((empty($cidrDATA['desc'])) ? __('Network not found') : $cidrDATA['desc']);
  42. } else {
  43. $this->nasOptsForTitle.= wf_nbsp(3) . ' for network: ' . wf_nbsp() . $tmpNASData['netid'] . ': ' . __('Network not found');
  44. }
  45. }
  46. unset($alter);
  47. }
  48. public function save() {
  49. $value = serialize($_POST[self::FORM_NAME]);
  50. $value = base64_encode($value);
  51. simple_update_field('nas', 'options', $value, "WHERE `id` = '" . $this->_id . "'");
  52. // Re-new options current values:
  53. $this->options = zb_NasOptionsGet($this->_id);
  54. // Return
  55. return true;
  56. }
  57. private function get_ifaces() {
  58. if ($this->api->connected) {
  59. $result = $this->api->command('/interface/getall', array('.proplist' => 'name'));
  60. foreach ($result as $value) {
  61. $name = $value['name'];
  62. $this->_if[$name] = $name;
  63. }
  64. }
  65. return natsort($this->_if);
  66. }
  67. public function render() {
  68. $this->form->InputForm(null, 'POST', __('Save'), null, null, null, self::FORM_NAME, null);
  69. // Block 1: Authorization Data
  70. $this->form->addmessage(__('Authorization Data'));
  71. $inputs = array('username', 'password');
  72. foreach ($inputs as $input) {
  73. $_hide = ( $input == 'password' ) ? $this->config['PASSWORDSHIDE'] : false;
  74. $contents = $this->form->text_box(self::FORM_NAME . '[' . $input . ']', $this->options[$input], 0, 0, $_hide, null);
  75. $this->form->addrow(__($input), $contents);
  76. }
  77. if (!isset($this->options['apiport'])) {
  78. $this->options['apiport'] = '8728';
  79. }
  80. $contents = $this->form->text_box(self::FORM_NAME . '[apiport]', $this->options['apiport'], 0, 0, false, null);
  81. $this->form->addrow(__('API port'), $contents);
  82. $TelepathySup = '';
  83. $ConnTypeCheckBox = 'use_new_conn_mode';
  84. $ConnTypeCheckBoxDisabled = '';
  85. $name = self::FORM_NAME . '[' . $ConnTypeCheckBox . ']';
  86. if ($this->UseVersionTelepathy) {
  87. $Version = $this->api->determineVersion($this->_ip, $this->WEBPort, $this->SNMPCommunity);
  88. if ( !empty($Version) ) {
  89. if ($Version >= 6.43) {
  90. $tmpStr = __('Yes');
  91. $this->options[$ConnTypeCheckBox] = 1;
  92. $this->form->hidden($name, '1');
  93. } else {
  94. $tmpStr = __('No');
  95. $this->options[$ConnTypeCheckBox] = null;
  96. }
  97. //$ConnTypeCheckBoxDisabled = 'disabled';
  98. //$TelepathySup = wf_tag('sup') . ' (' . __('telepathically guessed') . ')' . wf_tag('sup', true);
  99. $ConnTypeCheckBoxDisabled = 'hidden';
  100. $TelepathySup = $tmpStr . ' (' . __('telepathically guessed') . ')';
  101. }
  102. }
  103. $UseNewConnMode = ( isset($this->options[$ConnTypeCheckBox]) ) ? true : false;
  104. $contents = $this->form->checkbox($name, true, $TelepathySup, $UseNewConnMode, $ConnTypeCheckBoxDisabled);
  105. $this->form->addrow(__('Use new connection mode (for RouterOS 6.43 and higher)'), $contents);
  106. unset($inputs);
  107. // Connection-sensetive options:
  108. if ( isset($this->options['username'])
  109. && isset($this->options['password'])
  110. && $this->api->connect($this->_ip, $this->options['username'], $this->options['password'], $UseNewConnMode, $this->options['apiport']) ) {
  111. // Block 2: Interface settings
  112. $this->form->addmessage(__('Interface settings'));
  113. $this->get_ifaces();
  114. $selects = array('users', 'graph');
  115. foreach ($selects as $select) {
  116. $opt = $select . '_interface';
  117. $name = self::FORM_NAME . '[' . $opt . ']';
  118. $current = ( isset($this->options[$opt]) ) ? $this->options[$opt] : null;
  119. $contents = $this->form->select_tag($name, $this->_if, $current);
  120. $this->form->addrow(__(ucwords($select) . ' Interface'), $contents);
  121. }
  122. unset($selects);
  123. // Block 3: Setting On* scripts behavior for this NAS
  124. $this->form->addmessage(__('Setting On* scripts behavior for this NAS'));
  125. $checkboxes = array('firewall', 'arp', 'queue', 'queue_tree', 'dhcp', 'ppp');
  126. foreach ($checkboxes as $checkbox) {
  127. $opt = 'manage_' . $checkbox;
  128. $name = self::FORM_NAME . '[' . $opt . ']';
  129. $current = ( isset($this->options[$opt]) ) ? true : false;
  130. $contents = $this->form->checkbox($name, true, null, $current, null);
  131. $this->form->addrow(__('Manage ' . $checkbox), $contents);
  132. }
  133. unset($checkboxes);
  134. // Block 4: MikroTik General Information
  135. $this->form->addmessage(__('MikroTik General Information'));
  136. $status = $this->api->command('/system/resource/print');
  137. foreach ($status[0] as $key => $value) {
  138. switch ($key) {
  139. case 'uptime':
  140. $parse = array(
  141. 'w' => '&nbsp;' . __('w') . '&nbsp;',
  142. 'd' => '&nbsp;' . __('d') . '&nbsp;'
  143. );
  144. $search = array_keys($parse);
  145. $replace = array_values($parse);
  146. $value = str_replace($search, $replace, $value);
  147. $this->form->addrow(__($key), $value);
  148. break;
  149. case 'version':
  150. $this->form->addrow(__($key), $value);
  151. list($value) = explode('.', $value);
  152. $this->form->hidden(self::FORM_NAME . '[' . $key . ']', $value);
  153. break;
  154. case 'free-memory':
  155. case 'total-memory':
  156. case 'free-hdd-space':
  157. case 'total-hdd-space':
  158. $value = stg_convert_size($value);
  159. $this->form->addrow(__($key), $value);
  160. break;
  161. case 'cpu-frequency':
  162. $this->form->addrow(__($key), $value . ' MHz');
  163. break;
  164. case 'cpu-load':
  165. $this->form->addrow(__($key), $value . ' %');
  166. break;
  167. case 'bad-blocks':
  168. $style = ( $value > 0 ) ? 'color:red;' : null;
  169. $this->form->addrow(__($key), '<span style="' . $style . '">' . $value . ' %</span>');
  170. break;
  171. default:
  172. if (!empty($value)) {
  173. $this->form->addrow(__($key), $value);
  174. }
  175. break;
  176. }
  177. }
  178. }
  179. /* Uncomment for debug window show:
  180. * if ( !is_null($this->api->debug_str) ) deb($this->api->debug_str);
  181. */
  182. return $this->form->show(true);
  183. }
  184. }
  185. if (isset($_GET['nasid'])) {
  186. $obj = new MTExtConf();
  187. if (isset($_POST[$obj::FORM_NAME])) {
  188. $obj->save();
  189. }
  190. $backLink = wf_Plate(wf_BackLink($obj::URL_NAS_LIST, __('Network Access Servers')), '100%', '', '', 'text-align: center;');
  191. $backLink.= wf_CleanDiv();
  192. show_window(__('MikroTik extended configuration') . $obj->nasOptsForTitle, $obj->render() . wf_delimiter(0) . $backLink);
  193. } else
  194. show_window(__('Error'), __('No NAS was selected to add options!'));
  195. } else
  196. show_error(__('You cant control this module'));
  197. } else {
  198. show_error(__('This module is disabled'));
  199. }