api.switchportassign.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397
  1. <?php
  2. /**
  3. * Users to switch port assign basic implementation
  4. */
  5. class SwitchPortAssign {
  6. /**
  7. * Contains system alter config as key=>value
  8. *
  9. * @var array
  10. */
  11. protected $altCfg = array();
  12. /**
  13. * Assigns database abstraction layer
  14. *
  15. * @var object
  16. */
  17. protected $assignsDb = '';
  18. /**
  19. * Contains all available switches data
  20. *
  21. * @var array
  22. */
  23. protected $allSwitches = array();
  24. /**
  25. * Contains all available switchport assigns as id=>id/login/switchid/port
  26. *
  27. * @var array
  28. */
  29. protected $allAssigns = array();
  30. /**
  31. * Some predefined stuff
  32. */
  33. const TABLE_ASSIGNS = 'switchportassign';
  34. const PROUTE_LOGIN = 'swassignlogin';
  35. const PROUTE_SWITCH = 'swassignswid';
  36. const PROUTE_PORT = 'swassignswport';
  37. const PROUTE_DELETE = 'swassigndelete';
  38. public function __construct() {
  39. $this->loadAlter();
  40. $this->initDb();
  41. $this->loadSwitches();
  42. $this->loadAssigns();
  43. }
  44. /**
  45. * Loads system alter config into protected prop
  46. *
  47. * @global object $ubillingConfig
  48. *
  49. * @return void
  50. */
  51. protected function loadAlter() {
  52. global $ubillingConfig;
  53. $this->altCfg = $ubillingConfig->getAlter();
  54. }
  55. /**
  56. * Inits assigns database abstraction layer
  57. *
  58. * @return void
  59. */
  60. protected function initDb() {
  61. $this->assignsDb = new NyanORM(self::TABLE_ASSIGNS);
  62. }
  63. /**
  64. *
  65. * @return void
  66. */
  67. protected function loadSwitches() {
  68. $this->allSwitches = $this->getAllSwitches();
  69. }
  70. /**
  71. * Returns coinfurable switches array
  72. *
  73. * @return array
  74. */
  75. protected function getAllSwitches() {
  76. $result = array();
  77. //optional switches arrange
  78. switch ($this->altCfg['SWITCHPORT_IN_PROFILE']) {
  79. //switch selector arranged by id (default)
  80. case 1:
  81. $result = zb_SwitchesGetAll();
  82. break;
  83. //switch selector arranged by location
  84. case 2:
  85. $result = zb_SwitchesGetAll('ORDER BY `location` ASC');
  86. break;
  87. //switch selector arranged by ip
  88. case 3:
  89. $result = zb_SwitchesGetAll('ORDER BY `ip` ASC');
  90. break;
  91. //switch selector arranged by id (default)
  92. case 4:
  93. $result = zb_SwitchesGetAll();
  94. break;
  95. default :
  96. $result = zb_SwitchesGetAll();
  97. break;
  98. }
  99. return($result);
  100. }
  101. /**
  102. * Loads all assigns data into protected prop
  103. *
  104. * @return void
  105. */
  106. protected function loadAssigns() {
  107. $this->allAssigns = $this->assignsDb->getAll('id');
  108. }
  109. /**
  110. * Returns some user assign data if it exists
  111. *
  112. * @param string $login
  113. *
  114. * @return array
  115. */
  116. public function getAssignData($login) {
  117. $result = array();
  118. if (!empty($this->allAssigns)) {
  119. foreach ($this->allAssigns as $io => $each) {
  120. if ($each['login'] == $login) {
  121. $result = $each;
  122. break;
  123. }
  124. }
  125. }
  126. return($result);
  127. }
  128. /**
  129. * Checks is switch-port pair free or not
  130. *
  131. * @param int $switchId
  132. * @param int $port
  133. *
  134. * @return bool
  135. */
  136. protected function isPortFree($switchId, $port) {
  137. $result = true;
  138. if (!empty($this->allSwitches)) {
  139. if ((!empty($switchId)) AND (!empty($port))) {
  140. foreach ($this->allAssigns as $io => $each) {
  141. if ($each['switchid'] == $switchId AND $each['port'] == $port) {
  142. $result = false;
  143. }
  144. }
  145. }
  146. } else {
  147. $result = false;
  148. }
  149. return($result);
  150. }
  151. /**
  152. * Returns all other users with assigned same port on the same switch
  153. *
  154. * @param string $login
  155. * @param int $currentSwitchPort
  156. * @param int $currentSwitchId
  157. *
  158. * @return array
  159. */
  160. protected function getSameUsers($login, $currentSwitchPort, $currentSwitchId) {
  161. $result = array();
  162. $currentSwitchId = ubRouting::filters($currentSwitchId, 'int');
  163. $currentSwitchPort = ubRouting::filters($currentSwitchPort, 'int');
  164. if (!empty($login) AND !empty($currentSwitchId) AND !empty($currentSwitchPort)) {
  165. if (!empty($this->allAssigns)) {
  166. foreach ($this->allAssigns as $io => $each) {
  167. if ($each['switchid'] == $currentSwitchId AND $each['port'] == $currentSwitchPort) {
  168. if ($each['login'] != $login) {
  169. $result[$each['login']] = $each;
  170. }
  171. }
  172. }
  173. }
  174. }
  175. return($result);
  176. }
  177. /**
  178. * Deletes existing assign database record by user login
  179. *
  180. * @param string $login
  181. *
  182. * @return void
  183. */
  184. public function delete($login) {
  185. $login = ubRouting::filters($login, 'mres');
  186. $this->assignsDb->where('login', '=', $login);
  187. $this->assignsDb->delete();
  188. log_register('SWITCHPORT DELETE (' . $login . ')');
  189. // Rebuild DHCP configuration if switch used on option82
  190. if ($this->altCfg['OPT82_ENABLED']) {
  191. $loginNetType = multinet_get_network_params_by_login($login);
  192. if (!empty($loginNetType) and $loginNetType['nettype'] = 'dhcp82') {
  193. multinet_rebuild_all_handlers();
  194. }
  195. }
  196. }
  197. /**
  198. * Creates new or updates existing assign record
  199. *
  200. * @param string $login
  201. * @param int $switchId
  202. * @param int $port
  203. *
  204. * @return void/string on error
  205. */
  206. public function save($login, $switchId, $port) {
  207. $result = '';
  208. $newAssignLoging = ubRouting::filters($login, 'mres');
  209. $newSwitchId = ubRouting::filters($switchId, 'int');
  210. $newPort = ubRouting::filters($port, 'int');
  211. if ($this->isPortFree($newSwitchId, $newPort)) {
  212. $currenAssignData = $this->getAssignData($login);
  213. //creating new assign
  214. if (empty($currenAssignData)) {
  215. $this->assignsDb->data('login', $login);
  216. $this->assignsDb->data('switchid', $newSwitchId);
  217. $this->assignsDb->data('port', $newPort);
  218. $this->assignsDb->create();
  219. } else {
  220. //updating existing record
  221. $this->assignsDb->where('login', '=', $login);
  222. $this->assignsDb->data('switchid', $newSwitchId);
  223. $this->assignsDb->data('port', $newPort);
  224. $this->assignsDb->save();
  225. }
  226. log_register('SWITCHPORT CHANGE (' . $login . ') ON SWITCHID [' . $newSwitchId . '] PORT [' . $newPort . ']');
  227. // Rebuild DHCP configs if switch used on option82
  228. if ($this->altCfg['OPT82_ENABLED']) {
  229. $loginNetType = multinet_get_network_params_by_login($login);
  230. if (!empty($loginNetType) and $loginNetType['nettype'] = 'dhcp82') {
  231. multinet_rebuild_all_handlers();
  232. }
  233. }
  234. } else {
  235. log_register('SWITCHPORT FAIL (' . $login . ') ON SWITCHID [' . $newSwitchId . '] PORT [' . $newPort . ']');
  236. $result .= __('Port already assigned for another user');
  237. }
  238. return($result);
  239. }
  240. /**
  241. * Switchport modification controller
  242. *
  243. * @param string $returnUrl
  244. *
  245. * @return void
  246. */
  247. public function catchChangeRequest($returnUrl = '') {
  248. //update
  249. if (ubRouting::checkPost(array(self::PROUTE_LOGIN, self::PROUTE_SWITCH, self::PROUTE_PORT))) {
  250. $updateResult = $this->save(ubRouting::post(self::PROUTE_LOGIN), ubRouting::post(self::PROUTE_SWITCH), ubRouting::post(self::PROUTE_PORT));
  251. if (empty($updateResult)) {
  252. if (!empty($returnUrl)) {
  253. ubRouting::nav($returnUrl);
  254. }
  255. } else {
  256. show_error($updateResult);
  257. }
  258. }
  259. //delete
  260. if (ubRouting::post(self::PROUTE_DELETE)) {
  261. $assignToDelete = ubRouting::post(self::PROUTE_LOGIN);
  262. $this->delete($assignToDelete);
  263. if (!empty($returnUrl)) {
  264. ubRouting::nav($returnUrl);
  265. }
  266. }
  267. }
  268. /**
  269. * Returns users switch port assign form
  270. *
  271. * @param string $login
  272. *
  273. * @return string
  274. */
  275. public function renderEditForm($login) {
  276. $result = '';
  277. $login = ubRouting::filters($login, 'mres');
  278. $switcharr = array();
  279. $switcharrFull = array();
  280. $switchswpoll = array();
  281. $switchgeo = array();
  282. $sameArr = array();
  283. $sameUsers = '';
  284. $currentSwitchPort = '';
  285. $currentSwitchId = '';
  286. $assignData = $this->getAssignData($login);
  287. if (!empty($this->allSwitches)) {
  288. foreach ($this->allSwitches as $io => $eachswitch) {
  289. $switcharrFull[$eachswitch['id']] = $eachswitch['ip'] . ' - ' . $eachswitch['location'];
  290. if (mb_strlen($eachswitch['location']) > 32) {
  291. $switcharr[$eachswitch['id']] = $eachswitch['ip'] . ' - ' . mb_substr($eachswitch['location'], 0, 32, 'utf-8') . '...';
  292. } else {
  293. $switcharr[$eachswitch['id']] = $eachswitch['ip'] . ' - ' . $eachswitch['location'];
  294. }
  295. if (ispos($eachswitch['desc'], 'SWPOLL')) {
  296. $switchswpoll[$eachswitch['id']] = $eachswitch['ip'];
  297. }
  298. if (!empty($eachswitch['geo'])) {
  299. $switchgeo[$eachswitch['id']] = $eachswitch['geo'];
  300. }
  301. }
  302. }
  303. if (!empty($assignData)) {
  304. $currentSwitchPort = $assignData['port'];
  305. $currentSwitchId = $assignData['switchid'];
  306. $sameArr = $this->getSameUsers($login, $currentSwitchPort, $currentSwitchId);
  307. }
  308. //rendering other users with same switch+port
  309. if ((!empty($currentSwitchId)) AND (!empty($currentSwitchPort))) {
  310. if (!empty($sameArr)) {
  311. foreach ($sameArr as $ip => $each) {
  312. $sameUsers .= ' ' . wf_Link(UserProfile::URL_PROFILE . $each['login'], web_profile_icon() . ' ' . $each['login'], false, '');
  313. }
  314. }
  315. }
  316. //control form construct
  317. $formStyle = 'glamour';
  318. $inputs = wf_HiddenInput(self::PROUTE_LOGIN, $login);
  319. if ($this->altCfg['SWITCHPORT_IN_PROFILE'] != 4) {
  320. $inputs .= wf_Selector(self::PROUTE_SWITCH, $switcharr, __('Switch'), $currentSwitchId, true);
  321. } else {
  322. $inputs .= wf_JuiComboBox(self::PROUTE_SWITCH, $switcharr, __('Switch'), $currentSwitchId, true);
  323. $formStyle = 'floatpanelswide';
  324. }
  325. $inputs .= wf_TextInput(self::PROUTE_PORT, __('Port'), $currentSwitchPort, false, 2, 'digits');
  326. $inputs .= wf_CheckInput(self::PROUTE_DELETE, __('Delete'), true, false);
  327. $inputs .= wf_Submit('Save');
  328. $controlForm = wf_Form('', "POST", $inputs, $formStyle);
  329. //form end
  330. $switchAssignController = wf_modalAuto(web_edit_icon(), __('Switch port assign'), $controlForm);
  331. //switch location and polling controls
  332. $switchLocators = '';
  333. if (!empty($currentSwitchId)) {
  334. $switchProfileIcon = wf_img_sized('skins/menuicons/switches.png', __('Switch'), 10, 10);
  335. $switchLocators .= wf_Link('?module=switches&edit=' . $currentSwitchId, $switchProfileIcon, false, '');
  336. }
  337. if (isset($switchswpoll[$currentSwitchId])) {
  338. $snmpSwitchLocatorIcon = wf_img_sized('skins/snmp.png', __('SNMP query'), 10, 10);
  339. $switchLocators .= wf_Link('?module=switchpoller&switchid=' . $currentSwitchId, $snmpSwitchLocatorIcon, false, '');
  340. }
  341. if (isset($switchgeo[$currentSwitchId])) {
  342. $geoSwitchLocatorIcon = wf_img_sized('skins/icon_search_small.gif', __('Find on map'), 10, 10);
  343. $switchLocators .= wf_Link('?module=switchmap&finddevice=' . $switchgeo[$currentSwitchId], $geoSwitchLocatorIcon, false, '');
  344. }
  345. $cells = wf_TableCell(__('Switch'), '30%', 'row2');
  346. $cells .= wf_TableCell(@$switcharrFull[$currentSwitchId] . ' ' . $switchLocators);
  347. $rows = wf_TableRow($cells, 'row3');
  348. $cells = wf_TableCell(__('Port'), '30%', 'row2');
  349. $cells .= wf_TableCell($currentSwitchPort);
  350. $rows .= wf_TableRow($cells, 'row3');
  351. $cells = wf_TableCell(__('Change'), '30%', 'row2');
  352. $cells .= wf_TableCell($switchAssignController . ' ' . $sameUsers);
  353. $rows .= wf_TableRow($cells, 'row3');
  354. $result = wf_TableBody($rows, '100%', '0');
  355. return($result);
  356. }
  357. }