api.multigenecn.php 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294
  1. <?php
  2. /**
  3. * MultiGen Custom NAS configuration implementation
  4. */
  5. class MultigenECN {
  6. /**
  7. * Database abstraction layer placeholder
  8. *
  9. * @var object
  10. */
  11. protected $nasDb = '';
  12. /**
  13. * System message helper placeholder
  14. *
  15. * @var object
  16. */
  17. protected $messages = '';
  18. /**
  19. * Contains all available custom-NAS data as id=>data
  20. *
  21. * @var array
  22. */
  23. protected $allNasData = array();
  24. /**
  25. * some predefined URLs, routes, etc...
  26. */
  27. const URL_ME = '?module=multigennascustom';
  28. const DATA_TABLE = 'mlg_nascustom';
  29. const ROUTE_DELETE = 'deleteecnid';
  30. const PROUTE_NEWIP = 'newecnip';
  31. const PROUTE_NEWNAME = 'newecnname';
  32. const PROUTE_NEWSECRET = 'newecnsecret';
  33. const PROUTE_EDID = 'editecnid';
  34. const PROUTE_EDNAME = 'editecnname';
  35. const PROUTE_EDSECRET = 'editecnsecret';
  36. /**
  37. * Creates new extra chromosome NAS instance
  38. */
  39. public function __construct() {
  40. $this->initMessages();
  41. $this->initDb();
  42. $this->loadAllNasData();
  43. }
  44. /**
  45. * Inits system message helper for further usage
  46. *
  47. * @return void
  48. */
  49. protected function initMessages() {
  50. $this->messages = new UbillingMessageHelper();
  51. }
  52. /**
  53. * Inits database abstraction layer
  54. *
  55. * @return void
  56. */
  57. protected function initDb() {
  58. $this->nasDb = new NyanORM(self::DATA_TABLE);
  59. }
  60. /**
  61. * Loads all available custom NAS data from database
  62. *
  63. * @return void
  64. */
  65. protected function loadAllNasData() {
  66. $this->allNasData = $this->nasDb->getAll('id');
  67. }
  68. /**
  69. * Renders available custom NAS-es list
  70. *
  71. * @return string
  72. */
  73. public function renderList() {
  74. $result = '';
  75. if (!empty($this->allNasData)) {
  76. /**
  77. * O o O o O o
  78. * | O o | | O o | | O o |
  79. * | | O | | | | O | | | | O | |
  80. * | o O | | o O | | o O |
  81. * o O o O o O
  82. */
  83. $cells = wf_TableCell(__('ID'));
  84. $cells .= wf_TableCell(__('IP'));
  85. $cells .= wf_TableCell(__('NAS name'));
  86. $cells .= wf_TableCell(__('RADIUS secret'));
  87. $cells .= wf_TableCell(__('Actions'));
  88. $rows = wf_TableRow($cells, 'row1');
  89. foreach ($this->allNasData as $nasId => $eachNasData) {
  90. $cells = wf_TableCell($nasId);
  91. $cells .= wf_TableCell($eachNasData['ip']);
  92. $cells .= wf_TableCell($eachNasData['name']);
  93. $cells .= wf_TableCell($eachNasData['secret']);
  94. $deleteUrl = self::URL_ME . '&' . self::ROUTE_DELETE . '=' . $nasId;
  95. $cancelUrl = self::URL_ME;
  96. $delDialogTitle = __('Delete') . ' ' . __('NAS') . ' ' . $eachNasData['ip'] . '?';
  97. $actLinks = '';
  98. $actLinks .= wf_ConfirmDialog($deleteUrl, web_delete_icon(), $this->messages->getDeleteAlert(), '', $cancelUrl, $delDialogTitle);
  99. $editDialogTitle = __('Edit') . ' ' . __('NAS') . ' ' . $eachNasData['ip'];
  100. $actLinks .= wf_modalAuto(web_edit_icon(), $editDialogTitle, $this->renderEditForm($nasId));
  101. $cells .= wf_TableCell($actLinks);
  102. $rows .= wf_TableRow($cells, 'row5');
  103. }
  104. $result .= wf_TableBody($rows, '100%', 0, 'sortable');
  105. } else {
  106. $result .= $this->messages->getStyledMessage(__('Nothing to show'), 'info');
  107. }
  108. return($result);
  109. }
  110. /**
  111. * Renders new custom NAS creation form
  112. *
  113. * @return string
  114. */
  115. public function renderCreateForm() {
  116. $result = '';
  117. $inputs = wf_TextInput(self::PROUTE_NEWIP, __('IP'), '', true, 15, 'ip');
  118. $inputs .= wf_TextInput(self::PROUTE_NEWNAME, __('NAS name'), '', true, 20, '');
  119. $inputs .= wf_TextInput(self::PROUTE_NEWSECRET, __('RADIUS secret'), '', true, 20, 'alphanumeric');
  120. $inputs .= wf_delimiter(0);
  121. $inputs .= wf_Submit(__('Create'));
  122. $result .= wf_Form('', 'POST', $inputs, 'glamour');
  123. return($result);
  124. }
  125. /**
  126. * Renders custom NAS editing form
  127. *
  128. * @param int $nasId
  129. *
  130. * @return string
  131. */
  132. protected function renderEditForm($nasId) {
  133. $result = '';
  134. if (isset($this->allNasData[$nasId])) {
  135. $nasData = $this->allNasData[$nasId];
  136. $inputs = wf_HiddenInput(self::PROUTE_EDID, $nasId);
  137. $inputs .= wf_TextInput(self::PROUTE_EDNAME, __('NAS name'), $nasData['name'], true, 20, '');
  138. $inputs .= wf_TextInput(self::PROUTE_EDSECRET, __('RADIUS secret'), $nasData['secret'], true, 20, 'alphanumeric');
  139. $inputs .= wf_delimiter(0);
  140. $inputs .= wf_Submit(__('Save'));
  141. $result .= wf_Form('', 'POST', $inputs, 'glamour');
  142. } else {
  143. $result .= $this->messages->getStyledMessage(__('NAS') . ' [' . $nasId . '] ' . __('Not exists'), 'error');
  144. }
  145. return($result);
  146. }
  147. /**
  148. * Checks is NAS IP address not used for any of other custom NAS
  149. *
  150. * @param string $ip
  151. *
  152. * @return bool
  153. */
  154. protected function isIpFree($ip) {
  155. $result = true;
  156. $ip = trim($ip);
  157. if (!empty($this->allNasData)) {
  158. foreach ($this->allNasData as $io => $each) {
  159. if ($each['ip'] == $ip) {
  160. $result = false;
  161. }
  162. }
  163. }
  164. return($result);
  165. }
  166. /**
  167. * Returns something to indicate that NAS have custom configuration
  168. *
  169. * @param string $ip
  170. *
  171. * @return string
  172. */
  173. public function getIndicator($ip) {
  174. $result = '';
  175. if (!$this->isIpFree($ip)) {
  176. $result .= ' ' . wf_img_sized('skins/dna_icon.png', __('Extra chromosome NAS'), 12);
  177. }
  178. return($result);
  179. }
  180. /**
  181. * Creates new custom NAS in database
  182. *
  183. * @param string $ip
  184. * @param string $name
  185. * @param string $secret
  186. *
  187. * @return void/string on error
  188. */
  189. public function create($ip, $name, $secret) {
  190. $ipF = ubRouting::filters($ip, 'mres');
  191. $nameF = ubRouting::filters($name, 'mres');
  192. $secretF = ubRouting::filters($secret, 'mres');
  193. $result = '';
  194. if (!empty($ipF) AND ! empty($nameF) AND ! empty($secretF)) {
  195. if (zb_isIPValid($ipF)) {
  196. if ($this->isIpFree($ipF)) {
  197. $this->nasDb->data('ip', $ipF);
  198. $this->nasDb->data('name', $nameF);
  199. $this->nasDb->data('secret', $secret);
  200. $this->nasDb->create();
  201. $newId = $this->nasDb->getLastId();
  202. log_register('MULTIGEN ECN CREATE SUCCESS [' . $newId . '] `' . $ip . '`');
  203. } else {
  204. $result = __('IP duplicate') . ': ' . $ipF;
  205. log_register('MULTIGEN ECN CREATE FAIL `' . $ip . '` DUPLICATE');
  206. }
  207. } else {
  208. $result = __('Wrong IP') . ': ' . $ipF;
  209. log_register('MULTIGEN ECN CREATE FAIL `' . $ip . '` WRONG');
  210. }
  211. } else {
  212. $result = __('Not all of required fields are filled');
  213. log_register('MULTIGEN ECN CREATE FAIL INCOMPLETE_DATA');
  214. }
  215. return($result);
  216. }
  217. /**
  218. * Saves changes for some custom NAS in database
  219. *
  220. * @param int $nasId
  221. * @param string $name
  222. * @param string $secret
  223. *
  224. * @return void/string on error
  225. */
  226. public function save($nasId, $name, $secret) {
  227. $nasId = ubRouting::filters($nasId, 'int');
  228. $nameF = ubRouting::filters($name, 'mres');
  229. $secretF = ubRouting::filters($secret, 'mres');
  230. $result = '';
  231. if (!empty($nasId) AND ! empty($nameF) AND ! empty($secretF)) {
  232. if (isset($this->allNasData[$nasId])) {
  233. $this->nasDb->where('id', '=', $nasId);
  234. $this->nasDb->data('name', $nameF);
  235. $this->nasDb->data('secret', $secretF);
  236. $this->nasDb->save();
  237. log_register('MULTIGEN ECN SAVE SUCCESS [' . $nasId . ']');
  238. } else {
  239. $result = __('NAS') . ' [' . $nasId . '] ' . __('Not exists');
  240. log_register('MULTIGEN ECN SAVE FAIL [' . $nasId . '] NOT_EXISTS');
  241. }
  242. } else {
  243. $result = __('Not all of required fields are filled');
  244. log_register('MULTIGEN ECN SAVE FAIL INCOMPLETE_DATA');
  245. }
  246. return($result);
  247. }
  248. /**
  249. * Deletes existing custom NAS configuration from database
  250. *
  251. * @param int $nasId
  252. *
  253. * @return void/string on error
  254. */
  255. public function delete($nasId) {
  256. $nasId = ubRouting::filters($nasId, 'int');
  257. $result = '';
  258. if (!empty($nasId)) {
  259. if (isset($this->allNasData[$nasId])) {
  260. $nasData = $this->allNasData[$nasId];
  261. $nasIp = $nasData['ip'];
  262. $this->nasDb->where('id', '=', $nasId);
  263. $this->nasDb->delete();
  264. log_register('MULTIGEN ECN DELETE SUCCESS [' . $nasId . '] `' . $nasIp . '`');
  265. } else {
  266. $result = __('NAS') . ' [' . $nasId . '] ' . __('Not exists');
  267. log_register('MULTIGEN ECN DELETE FAIL [' . $nasId . '] NOT_EXISTS');
  268. }
  269. } else {
  270. $result = __('ID') . ' ' . __('NAS') . ' ' . __('is empty');
  271. log_register('MULTIGEN ECN CREATE FAIL ID_EMPTY');
  272. }
  273. return($result);
  274. }
  275. }