api.vlan.php 84 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369
  1. <?php
  2. /**
  3. * Vlan MAC history class
  4. */
  5. class VlanMacHistory {
  6. const MODULE = "vlan_mac_history";
  7. const MODULE_URL = "?module=vlan_mac_history";
  8. const DB_NAME = "vlan_mac_history";
  9. const FLAGPREFIX = 'exports/ONLINEVLANS';
  10. /**
  11. * Contains all available switches data
  12. *
  13. * @var array
  14. */
  15. protected $AllSwitches = array();
  16. /**
  17. * Contains all available switch models data
  18. *
  19. * @var array
  20. */
  21. protected $AllSwitchModels = array();
  22. /**
  23. * Contains all vlan terminators data
  24. *
  25. * @var array
  26. */
  27. protected $allTerminators = array();
  28. /**
  29. * Contains all vlan hosts data
  30. *
  31. * @var array
  32. */
  33. protected $allVlanHosts = array();
  34. /**
  35. * Contains system alter config as key=>value
  36. *
  37. * @var array
  38. */
  39. protected $altCfg = array();
  40. /**
  41. * Contains all vlan and mac history
  42. *
  43. * @var array
  44. */
  45. public $allHistory = array();
  46. public function __construct() {
  47. $this->LoadTerminators();
  48. $this->LoadAlter();
  49. $this->LoadVlanHosts();
  50. $this->LoadAllSwitches();
  51. $this->LoadAllSwitchModels();
  52. $this->LoadVlanMacHistory();
  53. }
  54. /**
  55. * load all data from `vlan_terminators` to $allTerminators
  56. *
  57. * @return void
  58. */
  59. protected function LoadTerminators() {
  60. $query = "SELECT * FROM " . VlanTerminator::DB_TABLE;
  61. $data = simple_queryall($query);
  62. if (!empty($data)) {
  63. foreach ($data as $each) {
  64. $this->allTerminators[$each['vlanpoolid']] = $each;
  65. }
  66. }
  67. }
  68. /**
  69. * load alter.ini config
  70. *
  71. * @return void
  72. */
  73. protected function LoadAlter() {
  74. global $ubillingConfig;
  75. $this->altCfg = $ubillingConfig->getAlter();
  76. }
  77. /**
  78. * load all data from vlanhosts to $allVlanHosts
  79. *
  80. * @return void
  81. */
  82. protected function LoadVlanHosts() {
  83. $query = "SELECT * FROM " . VlanGen::DB_TABLE;
  84. $data = simple_queryall($query);
  85. if (!empty($data)) {
  86. foreach ($data as $each) {
  87. $this->allVlanHosts[$each['login']] = $each;
  88. }
  89. }
  90. }
  91. /**
  92. * Function for getting all switches and place them to $AllSwitches
  93. *
  94. * @return void
  95. */
  96. protected function LoadAllSwitches() {
  97. $data = zb_SwitchesGetAll();
  98. if (!empty($data)) {
  99. foreach ($data as $each) {
  100. $this->AllSwitches[$each['ip']] = $each;
  101. }
  102. }
  103. }
  104. /**
  105. * Function for getting all switch models and place them to $AllSwitchModels
  106. *
  107. * @return void
  108. */
  109. protected function LoadAllSwitchModels() {
  110. $query = "SELECT * FROM `switchmodels`";
  111. $data = simple_queryall($query);
  112. if (!empty($data)) {
  113. foreach ($data as $each) {
  114. $this->AllSwitchModels[$each['id']] = $each['snmptemplate'];
  115. }
  116. }
  117. }
  118. /**
  119. * load all from `vlan_mac_history` to $allHistory
  120. *
  121. * @return void
  122. */
  123. protected function LoadVlanMacHistory() {
  124. $query = "SELECT * FROM " . self::DB_NAME;
  125. $data = simple_queryall($query);
  126. if (!empty($data)) {
  127. foreach ($data as $each) {
  128. $this->allHistory[$each['login']] = $each;
  129. }
  130. }
  131. }
  132. /**
  133. * Find vlan terminators snmp template
  134. *
  135. * @param string $login
  136. *
  137. * @return string
  138. */
  139. protected function GetTerminatorSnmpTemplate($login) {
  140. $data = $this->AllSwitchModels[$this->AllSwitches[$this->allTerminators[$this->allVlanHosts[$login]['vlanpoolid']]['ip']]['modelid']];
  141. return $data;
  142. }
  143. /**
  144. * Read online detect oid from snmp template
  145. *
  146. * @param string $login
  147. * @param int $vlan
  148. *
  149. * @return string
  150. */
  151. protected function GetOnlineDetectOid($login, $vlan = false) {
  152. $oid = false;
  153. $template = $this->GetTerminatorSnmpTemplate($login);
  154. $snmpData = rcms_parse_ini_file(CONFIG_PATH . "/snmptemplates/" . $template, true);
  155. if (isset($snmpData['define']['ONLINEVLAN'])) {
  156. if ($vlan) {
  157. $oid = $snmpData['define']['ONLINEVLAN'] . "." . $vlan;
  158. } else {
  159. $oid = $snmpData['define']['ONLINEVLAN'];
  160. }
  161. }
  162. return ($oid);
  163. }
  164. /**
  165. * Check weather user online
  166. *
  167. * @param string $login
  168. * @param int $vlan
  169. *
  170. * @return string
  171. */
  172. public function GetUserVlanOnline($login, $vlan) {
  173. snmp_set_oid_output_format(SNMP_OID_OUTPUT_NUMERIC);
  174. if ($this->GetOnlineDetectOid($login, $vlan)) {
  175. $data = @snmp2_real_walk($this->allTerminators[$this->allVlanHosts[$login]['vlanpoolid']]['ip'], $this->AllSwitches[$this->allTerminators[$this->allVlanHosts[$login]['vlanpoolid']]['ip']]['snmp'], $this->GetOnlineDetectOid($login, $vlan));
  176. if (empty($data)) {
  177. return "Offline" . " " . wf_img_sized('skins/icon_inactive.gif', '', '', '12');
  178. } else {
  179. return "Online" . " " . wf_img_sized('skins/icon_active.gif', '', '', '12');
  180. }
  181. } else {
  182. return 'empty';
  183. }
  184. }
  185. /**
  186. * Parse snmp reply and pass it to function which writes data to DB
  187. *
  188. * @return void
  189. */
  190. public function WriteVlanMacData() {
  191. $count = 0;
  192. if (!empty($this->allTerminators) AND ! empty($this->allVlanHosts)) {
  193. foreach ($this->allTerminators as $eachTerminator) {
  194. $ip = $eachTerminator["ip"];
  195. $vlanPoolId = $eachTerminator['vlanpoolid'];
  196. $data = @snmp2_real_walk($ip, $this->AllSwitches[$ip]['snmp'], '.1.3.6.1.4.1.9.9.380.1.4.1.1.3');
  197. foreach ($data as $each => $value) {
  198. $decmac = str_replace('.1.3.6.1.4.1.9.9.380.1.4.1.1.3.', '', $each);
  199. $vlanPlusMac = explode(".", $decmac, 2);
  200. $vlan = $vlanPlusMac[0];
  201. $mac = $this->dec2mac($vlanPlusMac[1]);
  202. foreach ($this->allVlanHosts as $eachHost) {
  203. if ($eachHost['vlanpoolid'] == $vlanPoolId AND $eachHost['vlan'] == $vlan) {
  204. $login = $eachHost['login'];
  205. }
  206. }
  207. if (!empty($this->allHistory)) {
  208. if ($this->allHistory[$login]['mac'] != $mac) {
  209. $this->WriteHistory($login, $vlan, $mac);
  210. }
  211. } else {
  212. $this->WriteHistory($login, $vlan, $mac);
  213. }
  214. $count++;
  215. }
  216. }
  217. }
  218. file_put_contents(self::FLAGPREFIX, $count);
  219. }
  220. /**
  221. * Get parsed snmp data about vlan, mac and time when device was assigned IP address and write it to DB
  222. *
  223. * @param string $login
  224. * @param int $vlan
  225. * @param string $mac
  226. *
  227. * @return void
  228. */
  229. protected function WriteHistory($login, $vlan, $mac) {
  230. $query = "INSERT INTO " . self::DB_NAME . " (`id`, `login`, `vlan`, `mac`, `date`) VALUES (NULL,'" . $login . "','" . $vlan . "','" . $mac . "', NULL);";
  231. nr_query($query);
  232. }
  233. /**
  234. * Converts decimal (delimiter is dot) MAC to heximal (delimiter is semicolon)
  235. *
  236. * @param string $mac
  237. *
  238. * @return string
  239. */
  240. protected function dec2mac($mac) {
  241. $res = array();
  242. $args = explode(".", $mac);
  243. foreach ($args as $each) {
  244. $each = dechex($each);
  245. strlen($each) < 2 ? $res[] = "0$each" : $res[] = $each;
  246. }
  247. $string = implode(":", $res);
  248. return ($string);
  249. }
  250. /**
  251. * Gether web form and return in in table view
  252. *
  253. * @param string $login
  254. *
  255. * @return void
  256. */
  257. public function RenderHistory($login) {
  258. $history = $this->allHistory;
  259. $tablecells = wf_TableCell(__('ID'));
  260. $tablecells .= wf_TableCell(__('Login'));
  261. $tablecells .= wf_TableCell(__('VLAN'));
  262. $tablecells .= wf_TableCell(__('MAC'));
  263. $tablecells .= wf_TableCell(__('Date'));
  264. $tablerows = wf_TableRow($tablecells, 'row1');
  265. if (!empty($history)) {
  266. $tablecells = wf_TableCell($history[$login]['id']);
  267. $tablecells .= wf_TableCell($history[$login]['login']);
  268. $tablecells .= wf_TableCell($history[$login]['vlan']);
  269. $tablecells .= wf_TableCell($history[$login]['mac']);
  270. $tablecells .= wf_TableCell($history[$login]['date']);
  271. $tablerows .= wf_TableRow($tablecells, 'row3');
  272. }
  273. $result = wf_TableBody($tablerows, '100%', '0', 'sortable');
  274. show_window(__('History'), $result);
  275. }
  276. }
  277. /**
  278. * VLAN gen - nothing to see here!
  279. */
  280. class VlanGen {
  281. /**
  282. * Contains all vlan pools as vlanPool['id'] => vlanPool['desc'] for vlan pool selector
  283. *
  284. * @var array
  285. */
  286. protected $VlanPoolsSelector = array();
  287. /**
  288. * Contains all vlan hosts data
  289. *
  290. * @var array
  291. */
  292. protected $AllVlanHosts = array();
  293. /**
  294. * Contains all vlan pools data
  295. *
  296. * @var array
  297. */
  298. protected $AllVlanPools = array();
  299. /**
  300. * Contains all vlan terminators data
  301. *
  302. * @var array
  303. */
  304. protected $AllTerminators = array();
  305. /**
  306. * Contains system alter config as key=>value
  307. *
  308. * @var array
  309. */
  310. protected $AltCfg = array();
  311. const MODULE = 'VlanGen';
  312. const MODULE_URL = '?module=pl_vlangen';
  313. const DB_TABLE = 'vlanhosts';
  314. const QINQ_DB_TABLE = 'vlanhosts_qinq';
  315. const SCRIPT_PATH = './config/scripts/';
  316. const MODULE_ADDVLAN = 'addvlan';
  317. const MODULE_URL_ADDVLAN = '?module=addvlan';
  318. const POOL_DB_TABLE = 'vlan_pools';
  319. public function __construct() {
  320. $this->LoadVlanHosts();
  321. $this->LoadVlanPoolsSelector();
  322. $this->LoadTerminators();
  323. $this->loadAlter();
  324. }
  325. /**
  326. * select all data from vlan_terminators and load to $AllTerminators
  327. *
  328. * @return void
  329. */
  330. protected function LoadTerminators() {
  331. $query = "SELECT * FROM " . VlanTerminator::DB_TABLE;
  332. $data = simple_queryall($query);
  333. if (!empty($data)) {
  334. foreach ($data as $each) {
  335. $this->AllTerminators[$each['id']] = $each;
  336. }
  337. }
  338. }
  339. /**
  340. * select all data from vlanhosts and load it to $allVlanHosts
  341. *
  342. * @return void
  343. */
  344. protected function LoadVlanHosts() {
  345. $query = "SELECT * FROM " . self::DB_TABLE;
  346. $data = simple_queryall($query);
  347. if (!empty($data)) {
  348. foreach ($data as $each) {
  349. $this->AllVlanHosts[$each['id']] = $each;
  350. }
  351. }
  352. }
  353. /**
  354. * select data from vlan_pools and load data to $AllVlanPools and loading data for vlan pool selector
  355. *
  356. * @return void
  357. */
  358. protected function LoadVlanPoolsSelector() {
  359. $query = "SELECT * FROM " . self::POOL_DB_TABLE;
  360. $data = simple_queryall($query);
  361. if (!empty($data)) {
  362. foreach ($data as $each) {
  363. $this->AllVlanPools[$each['id']] = $each;
  364. $this->VlanPoolsSelector[$each['id']] = $each['desc'];
  365. }
  366. }
  367. }
  368. /**
  369. * load alter.ini config
  370. *
  371. * @return void
  372. */
  373. protected function loadAlter() {
  374. global $ubillingConfig;
  375. $this->AltCfg = $ubillingConfig->getAlter();
  376. }
  377. /**
  378. * Searching vlan by login in AllVlanHosts
  379. *
  380. * @param string $login
  381. *
  382. * @return int $vlan
  383. */
  384. public function GetVlan($login) {
  385. if (!empty($this->AllVlanHosts)) {
  386. foreach ($this->AllVlanHosts as $each => $io) {
  387. if ($io['login'] == $login) {
  388. return($io['vlan']);
  389. }
  390. }
  391. }
  392. }
  393. /**
  394. * Searching QinQ value for vlan pool in AllVlanPools
  395. *
  396. * @param type $VlanPoolID
  397. *
  398. * @return int $QinQ
  399. */
  400. protected function GetVlanPoolQinQ($VlanPoolID) {
  401. foreach ($this->AllVlanPools as $Pool => $each) {
  402. if ($each['id'] == $VlanPoolID) {
  403. return ($each['qinq']);
  404. }
  405. }
  406. }
  407. /**
  408. * Fills array by every possible int value between $first and $end
  409. *
  410. * @param int $first
  411. * @param int $end
  412. *
  413. * @return array $pool
  414. */
  415. protected function VlanPoolExpand($first, $end) {
  416. for ($i = $first; $i <= $end; $i++) {
  417. $total[] = $i;
  418. }
  419. if (!empty($total)) {
  420. foreach ($total as $EachVlan) {
  421. $pool[] = $EachVlan;
  422. }
  423. }
  424. return($pool);
  425. }
  426. /**
  427. * Find all unused values from all possible values from VlanPoolExpand
  428. *
  429. * @param int $VlanPoolID
  430. *
  431. * @return array $freePool
  432. */
  433. protected function GetAllFreeVlan($VlanPoolID) {
  434. $poolData = $this->AllVlanPools[$VlanPoolID];
  435. $clearVlans = array();
  436. $fullPool = $this->VlanPoolExpand($poolData['firstvlan'], $poolData['endvlan']);
  437. $queryUsed = "SELECT `vlan` from " . self::DB_TABLE;
  438. $allUsedVlan = simple_queryall($queryUsed);
  439. if (!empty($allUsedVlan)) {
  440. foreach ($allUsedVlan as $io => $usedVlan) {
  441. $clearVlans[] = $usedVlan['vlan'];
  442. }
  443. $freePool = array_diff($fullPool, $clearVlans);
  444. } else {
  445. $freePool = $fullPool;
  446. }
  447. return($freePool);
  448. }
  449. /**
  450. * Find all unused values from all possible values from VlanPoolExpand
  451. *
  452. * @param int $vlanPoolID
  453. *
  454. * @return array $freePool
  455. */
  456. protected function GetAllFreeVlanQinQ($vlanPoolID) {
  457. $poolData = $this->AllVlanPools[$vlanPoolID];
  458. $first = $poolData['firstvlan'];
  459. $end = $poolData['endvlan'];
  460. $clearVlans = array();
  461. $fullPool = $this->VlanPoolExpand($first, $end);
  462. $usedQuery = "SELECT `svlan`,`cvlan`, FROM" . self::QINQ_DB_TABLE;
  463. $allUsedVlan = simple_queryall($usedQuery);
  464. if (!empty($allUsedVlan)) {
  465. foreach ($allUsedVlan as $io => $usedVlan) {
  466. $clearVlans[] = $usedVlan['cvlan'];
  467. }
  468. $freePool = array_diff($fullPool, $clearVlans);
  469. } else {
  470. $freePool = $fullPool;
  471. }
  472. return($freePool);
  473. }
  474. /**
  475. * Getting first unused value in all possible values of vlan pool
  476. *
  477. * @param int $vlanPoolID
  478. *
  479. * @return int $allFreeVlans[$tmp[0]
  480. */
  481. protected function GetNextFreeVlan($vlanPoolID) {
  482. $allFreeVlans = $this->GetAllFreeVlan($vlanPoolID);
  483. $tmp = array_keys($allFreeVlans);
  484. return($allFreeVlans[$tmp[0]]);
  485. }
  486. /**
  487. * Getting first unused value in all possible values of vlan pool
  488. *
  489. * @param int $vlanPoolID
  490. *
  491. * @return int $allFreeVlans[$tmp[0]
  492. */
  493. protected function GetNextFreeVlanQinQ($vlanPoolID) {
  494. $allFreeVlans = $this->GetAllFreeVlanQinQ($vlanPoolID);
  495. $tmp = array_keys($allFreeVlans);
  496. return($allFreeVlans[$tmp[0]]);
  497. }
  498. /**
  499. * Find netid by user's IP
  500. *
  501. * @param string $ip
  502. *
  503. * @return int $data['netid']
  504. */
  505. protected function GetNetidByIP($ip) {
  506. $query = "SELECT `netid` FROM `nethosts` WHERE `ip`='" . $ip . "'";
  507. $data = simple_query($query);
  508. return($data['netid']);
  509. }
  510. /**
  511. * Find vlan terminator id by netid
  512. *
  513. * @param int $netid
  514. *
  515. * @return int $data['id']
  516. */
  517. protected function GetTermIdByNetid($netid) {
  518. $query = "SELECT `id` FROM `vlan_terminators` WHERE `netid`='" . $netid . "'";
  519. $data = simple_query($query);
  520. return($data['id']);
  521. }
  522. /**
  523. * Apply vlan to user and write to DB
  524. *
  525. * @param int $VlanPoolID
  526. * @param int $vlan
  527. * @param string $login
  528. *
  529. * @return void
  530. */
  531. protected function AddVlanHost($VlanPoolID, $vlan, $login) {
  532. $query = "INSERT INTO `vlanhosts` (`id` , `vlanpoolid` , `vlan` , `login`)
  533. VALUES (NULL , '" . $VlanPoolID . "', '" . $vlan . "', '" . $login . "');";
  534. nr_query($query);
  535. }
  536. /**
  537. * Adding vlan pool data to DB
  538. *
  539. * @param string $Desc
  540. * @param int $FirstVlan
  541. * @param int $LastVlan
  542. * @param int $QinQ
  543. * @param int $sVlan
  544. *
  545. * @return void
  546. */
  547. public function AddVlanPool($Desc, $FirstVlan, $LastVlan, $QinQ, $sVlan) {
  548. $Desc = vf(mysql_real_escape_string($Desc));
  549. $FirstVlan = vf(trim($FirstVlan), 3);
  550. $LastVlan = vf(trim($LastVlan), 3);
  551. $QinQ = vf(trim($QinQ), 3);
  552. $sVlan = vf(trim($sVlan), 3);
  553. if (empty($sVlan)) {
  554. $sVlan = 'NULL';
  555. }
  556. $query = " INSERT INTO " . self::POOL_DB_TABLE . " (`id`, `desc`, `firstvlan`, `endvlan`, `qinq`, `svlan`)
  557. VALUES (NULL, '" . $Desc . "', '" . $FirstVlan . "', '" . $LastVlan . "', '" . $QinQ . "', '" . $sVlan . "')";
  558. nr_query($query);
  559. log_register('ADD VlanPool `' . $Desc . '`');
  560. }
  561. /**
  562. * Apply vlan to user and write to DB
  563. *
  564. * @param int $vlanpoolid
  565. * @param int $svlan
  566. * @param int $cvlan
  567. * @param string $login
  568. *
  569. * @return void
  570. */
  571. protected function AddVlanHostQinQ($vlanpoolid, $svlan, $cvlan, $login) {
  572. $query = "INSERT INTO `vlanhosts_qinq` (`id` , `vlanpoolid` , `svlan` , `cvlan` , `login`)
  573. VALUES (NULL , '" . $vlanpoolid . "', '" . $svlan . "', '" . $cvlan . "', '" . $login . "');";
  574. nr_query($query);
  575. }
  576. /**
  577. * Show all available data in vlan pools
  578. *
  579. * @return void
  580. */
  581. public function ShowVlanPools() {
  582. $allVlanPool = $this->AllVlanPools;
  583. $tablecells = wf_TableCell(__('ID'));
  584. $tablecells .= wf_TableCell(__('First Vlan'));
  585. $tablecells .= wf_TableCell(__('Last Vlan'));
  586. $tablecells .= wf_TableCell(__('Desc'));
  587. $tablecells .= wf_TableCell(__('qinq'));
  588. $tablecells .= wf_TableCell(__('svlan'));
  589. $tablecells .= wf_TableCell(__('Actions'));
  590. $tablerows = wf_TableRow($tablecells, 'row1');
  591. if (!empty($allVlanPool)) {
  592. foreach ($allVlanPool as $eachVlan => $io) {
  593. $tablecells = wf_TableCell($io['id']);
  594. $tablecells .= wf_TableCell($io['firstvlan']);
  595. $tablecells .= wf_TableCell($io['endvlan']);
  596. $tablecells .= wf_TableCell($io['desc']);
  597. $tablecells .= wf_TableCell($io ['qinq']);
  598. if (isset($io['qinq'])) {
  599. $tablecells .= wf_TableCell($io['svlan']);
  600. }
  601. $actionlinks = wf_JSAlert(self::MODULE_URL_ADDVLAN . '&DeleteVlanPool=' . $io['id'], web_delete_icon(), 'Removing this may lead to irreparable results');
  602. $actionlinks .= wf_Link(self::MODULE_URL_ADDVLAN . '&EditVlanPool=' . $io['id'], web_edit_icon(), false);
  603. $tablecells .= wf_TableCell($actionlinks);
  604. $tablerows .= wf_TableRow($tablecells, 'row3');
  605. }
  606. }
  607. $result = wf_TableBody($tablerows, '100%', '0', 'sortable');
  608. show_window(__('Vlans'), $result);
  609. }
  610. /**
  611. * Form for adding vlan pool data
  612. *
  613. * @return void
  614. */
  615. public function AddVlanPoolForm() {
  616. $useQinQArr = array('0' => __('No'), '1' => __('Yes'));
  617. $sup = wf_tag('sup') . '*' . wf_tag('sup', true);
  618. $inputs = wf_HiddenInput('AddVlan', 'true');
  619. $inputs .= wf_TextInput('FirstVlan', __('First Vlan') . $sup, '', true, '20');
  620. $inputs .= wf_TextInput('LastVlan', __('Last Vlan') . $sup, '', true, '20');
  621. $inputs .= wf_TextInput('Desc', __('Desc') . $sup, '', true, '20');
  622. $inputs .= wf_Selector('UseQinQ', $useQinQArr, __('Use qinq'), '', true);
  623. $inputs .= wf_TextInput('sVlan', __('Svlan') . $sup, '', true, '20');
  624. $inputs .= wf_Tag('br');
  625. $inputs .= wf_Submit(__('Add'));
  626. $form = wf_Form("", 'POST', $inputs, 'glamour');
  627. show_window(__('Add Vlan'), $form);
  628. }
  629. /**
  630. * Delete users vlan data from DB
  631. *
  632. * @param string $login
  633. *
  634. * @return void
  635. */
  636. public function DeleteVlanHost($login) {
  637. $query = "DELETE from " . self::DB_TABLE . " WHERE `login`='" . $login . "'";
  638. nr_query($query);
  639. log_register("DELETE VLanHost (" . $login . ")");
  640. }
  641. /**
  642. * Delete users vlan data from DB
  643. *
  644. * @param type $login
  645. *
  646. * @return void
  647. */
  648. public function DeleteVlanHostQinQ($login) {
  649. $query = "DELETE from " . self::QINQ_DB_TABLE . " WHERE `login`='" . $login . "'";
  650. nr_query($query);
  651. log_register("DELETE VLanHost (" . $login . ")");
  652. }
  653. /**
  654. * Delete vlan pool data from DB
  655. *
  656. * @param int $VlanPoolID
  657. *
  658. * @return void
  659. */
  660. public function DeleteVlanPool($VlanPoolID) {
  661. $query = "DELETE FROM " . self::POOL_DB_TABLE . " WHERE `id`='" . $VlanPoolID . "'";
  662. nr_query($query);
  663. log_register('DELETE VlanPool [' . $VlanPoolID . ']');
  664. }
  665. /**
  666. * Gather web form and returns it.
  667. * Form is used for applying vlan on onu.
  668. *
  669. * @return string
  670. */
  671. public function ChangeOnOnuForm() {
  672. $Inputs = wf_SubmitClassed('true', 'vlanButton', 'ChangeOnuPvid', __('Change pvid on onu port'));
  673. $Form = wf_Form("", 'POST', $Inputs);
  674. return($Form);
  675. }
  676. /**
  677. * For for changing pvid on switch port
  678. *
  679. * @return string
  680. */
  681. public function ChangeOnPortForm() {
  682. $inputs = wf_SubmitClassed('true', 'vlanButton', 'ChangeVlanOnPort', __('Change vlan on switch port'));
  683. $form = wf_Form("", 'POST', $inputs);
  684. return($form);
  685. }
  686. /**
  687. * Returns form for change\apply vlan on user
  688. *
  689. * @return string
  690. */
  691. public function ChangeForm() {
  692. $inputs = wf_tag('label', false, 'vlanLabel');
  693. $inputs .= wf_SelectorClassed('VlanPoolSelected', $this->VlanPoolsSelector, '', '', false, 'vlanSelector');
  694. $inputs .= wf_tag('label', true);
  695. $inputs .= wf_delimiter();
  696. $inputs .= wf_SubmitClassed('true', 'vlanButton', 'AddVlanHost', __('Change user Vlan'));
  697. $inputs .= wf_delimiter(2);
  698. $result = wf_Form("", 'POST', $inputs);
  699. return($result);
  700. }
  701. /**
  702. * Returns form for delete users vlan
  703. *
  704. * @return string
  705. */
  706. public function DeleteForm() {
  707. $inputs = wf_SubmitClassed('true', 'vlanButton', 'DeleteVlanHost', __('Delete user Vlan'));
  708. $inputs .= wf_delimiter(2);
  709. $result = wf_form("", 'POST', $inputs);
  710. return($result);
  711. }
  712. /**
  713. * Edit vlan pool data in DB
  714. *
  715. * @param int $first
  716. * @param int $last
  717. * @param string $desc
  718. * @param int $qinq
  719. * @param int $svlan
  720. * @param int $id
  721. *
  722. * @return void
  723. */
  724. public function EditVlanPool($first, $last, $desc, $qinq, $svlan, $id) {
  725. $first = vf(trim($first), 3);
  726. $last = vf(trim($last), 3);
  727. $desc = vf($desc);
  728. $qinq = vf(trim($qinq), 3);
  729. $svlan = vf(trim($svlan), 3);
  730. if (empty($sVlan)) {
  731. $svlan = 'NULL';
  732. }
  733. simple_update_field(self::POOL_DB_TABLE, 'firstvlan', $first, "WHERE `id`='" . $id . "'");
  734. simple_update_field(self::POOL_DB_TABLE, 'endvlan', $last, "WHERE `id`='" . $id . "'");
  735. simple_update_field(self::POOL_DB_TABLE, 'desc', $desc, "WHERE `id`='" . $id . "'");
  736. simple_update_field(self::POOL_DB_TABLE, 'qinq', $qinq, "WHERE `id`='" . $id . "'");
  737. simple_update_field(self::POOL_DB_TABLE, 'svlan', $svlan, "WHERE `id`='" . $id . "'");
  738. log_register('MODIFY VlanPool [' . $id . ']');
  739. }
  740. /**
  741. * Form for editing vlan pool data
  742. *
  743. * @param int $PoolID
  744. *
  745. * @return string
  746. */
  747. public function VlanPoolEditForm($PoolID) {
  748. $PoolData = $this->AllVlanPools[$PoolID];
  749. $useQinQArr = array('0' => __('No'), '1' => __('Yes'));
  750. $sup = wf_tag('sup') . '*' . wf_tag('sup', true);
  751. $inputs = wf_HiddenInput('EditVlanPool', 'true');
  752. $inputs .= wf_TextInput('FirstVlan', __('First Vlan') . $sup, $PoolData['firstvlan'], true, '20');
  753. $inputs .= wf_TextInput('LastVlan', __('Last Vlan') . $sup, $PoolData['endvlan'], true, '20');
  754. $inputs .= wf_TextInput('Desc', __('Desc') . $sup, $PoolData['desc'], true, '20');
  755. $inputs .= wf_Selector('UseQinQ', $useQinQArr, __('Use qinq'), $PoolData['qinq'], true);
  756. $inputs .= wf_TextInput('sVlan', __('sVlan') . $sup, $PoolData['svlan'], true, '20');
  757. $inputs .= wf_Tag('br');
  758. $inputs .= wf_Submit(__('Save'));
  759. $form = wf_Form('', "POST", $inputs, 'glamour');
  760. $form .= wf_BackLink(self::MODULE_URL_ADDVLAN, '', true);
  761. show_window(__('Edit'), $form);
  762. }
  763. /**
  764. * Apply vlan on vlan terminator
  765. *
  766. * @param string $ip
  767. * @param int $vlan
  768. *
  769. * @return void
  770. */
  771. protected function OnVlanConnect($ip, $vlan) {
  772. multinet_rebuild_all_handlers();
  773. $networkID = $this->GetNetidByIP($ip);
  774. $terminatorID = $this->GetTermIdByNetid($networkID);
  775. $termData = $this->AllTerminators[$terminatorID];
  776. $term_ip = $termData ['ip'];
  777. $term_type = $termData['type'];
  778. $term_user = $termData['username'];
  779. $term_pass = $termData['password'];
  780. $term_int = $termData['interface'];
  781. $relay = $termData['relay'];
  782. if ($term_ip == '127.0.0.1') {
  783. if ($term_type == 'FreeBSD') {
  784. $res = shell_exec(self::SCRIPT_PATH . "bsd.local.sh $term_int $ip $vlan");
  785. }
  786. if ($term_type == 'Linux') {
  787. $res = shell_exec(self::SCRIPT_PATH . "linux.local.sh");
  788. }
  789. } else {
  790. if ($term_type == 'FreeBSD') {
  791. $res = shell_exec(self::SCRIPT_PATH . "bsd.remote.sh $term_user $term_pass $term_int $ip $vlan");
  792. }
  793. if ($term_type == 'Linux') {
  794. $res = shell_exec(self::SCRIPT_PATH . "linux.remote.sh $term_user $term_pass $term_int $ip $vlan");
  795. }
  796. if ($term_type == 'Cisco') {
  797. $res = shell_exec(self::SCRIPT_PATH . "cisco.sh $term_user $term_pass $vlan $term_int $relay $term_ip");
  798. }
  799. if ($term_type == 'Cisco_static') {
  800. $res = shell_exec(self::SCRIPT_PATH . "cisco_static.sh $term_user $term_pass $vlan $term_int $relay $term_ip");
  801. }
  802. }
  803. }
  804. /**
  805. * Changes\applies users vlan on vlan terminator
  806. *
  807. * @param int $newVlanPoolID
  808. * @param string $login
  809. *
  810. * @return void
  811. */
  812. public function VlanChange($newVlanPoolID, $login) {
  813. $QinQ = $this->GetVlanPoolQinQ($newVlanPoolID);
  814. $ip = zb_UserGetIP($login);
  815. $this->DeleteVlanHost($login);
  816. $this->DeleteVlanHostQinQ($login);
  817. if ($QinQ == 0) {
  818. $newVlan = $this->GetNextFreeVlan($newVlanPoolID);
  819. if (empty($newVlan)) {
  820. $alert = wf_JSAlert(self::MODULE_URL_ADDVLAN, __("Error"), __("No free Vlan available in selected pool"));
  821. print($alert);
  822. rcms_redirect(self::MODULE_URL_ADDVLAN);
  823. }
  824. $this->AddVlanHost($newVlanPoolID, $newVlan, $login);
  825. } else {
  826. $poolData = $this->AllVlanPools[$newVlanPoolID];
  827. $svlan = $poolData['svlan'];
  828. $this->AddVlanHostQinQ($newVlanPoolID, $svlan, $newVlan, $login);
  829. }
  830. $this->OnVlanConnect($ip, $newVlan);
  831. log_register(__("Change vlan") . " " . "(" . $login . ")" . " " . __("ON") . " " . $newVlan);
  832. }
  833. }
  834. /**
  835. * VLAN terminator class
  836. */
  837. class VlanTerminator {
  838. const MODULE = 'Vlan Terminator';
  839. const MODULE_URL = '?module=nas';
  840. const DB_TABLE = 'vlan_terminators';
  841. /**
  842. * Contains all vlan terminators data
  843. *
  844. * @var array
  845. */
  846. protected $AllTerminators = array();
  847. /**
  848. * Contains all vlan pools as vlanPool['id'] => vlanPool['desc'] for vlan pool selector
  849. *
  850. * @var array
  851. */
  852. protected $VlanPoolsSelector = array();
  853. /**
  854. * Contains all networks pool as network['id'] => network['desc'] for networks selector
  855. *
  856. * @var type
  857. */
  858. protected $NetworkSelector = array();
  859. public function __construct() {
  860. $this->LoadTerminators();
  861. $this->LoadVlanPoolsSelector();
  862. $this->LoadNetworkSelecor();
  863. }
  864. /**
  865. * Load all from `vlan_terminator` to $allTerminators
  866. *
  867. * @return void
  868. */
  869. protected function LoadTerminators() {
  870. $query = "SELECT * FROM " . self::DB_TABLE;
  871. $data = simple_queryall($query);
  872. if (!empty($data)) {
  873. foreach ($data as $each) {
  874. $this->AllTerminators[$each['id']] = $each;
  875. }
  876. }
  877. }
  878. /**
  879. * Load data for making web form (select) for selecting vlan pool
  880. *
  881. * @return void
  882. */
  883. protected function LoadVlanPoolsSelector() {
  884. $query = "SELECT * FROM " . VlanGen::POOL_DB_TABLE;
  885. $data = simple_queryall($query);
  886. if (!empty($data)) {
  887. foreach ($data as $each) {
  888. $this->VlanPoolsSelector[$each['id']] = $each['desc'];
  889. }
  890. }
  891. }
  892. /**
  893. * Load data for making web form (select) for selecting network.
  894. *
  895. * @return void
  896. */
  897. protected function LoadNetworkSelecor() {
  898. $query = "SELECT * FROM `networks`";
  899. $data = simple_queryall($query);
  900. if (!empty($data)) {
  901. foreach ($data as $each) {
  902. $this->NetworkSelector[$each['id']] = $each['desc'];
  903. }
  904. }
  905. }
  906. /**
  907. * Delete vlan terminator data from DB
  908. *
  909. * @param int $id
  910. *
  911. * @return void
  912. */
  913. public function delete($id) {
  914. $query = "DELETE FROM " . self::DB_TABLE . " WHERE `id`='" . $id . "'";
  915. nr_query($query);
  916. log_register('DELETE Terminator [' . $id . ']');
  917. }
  918. /**
  919. * Add vlan terminator data to DB
  920. *
  921. * @param int $netid
  922. * @param int $vlanpoolid
  923. * @param string $ip
  924. * @param string $type
  925. * @param string $username
  926. * @param string $password
  927. * @param string $remote
  928. * @param string $interface
  929. * @param string $relay
  930. *
  931. * @return void
  932. */
  933. public function add($netid, $vlanpoolid, $ip, $type, $username, $password, $remote, $interface, $relay) {
  934. $query = "INSERT INTO " . self::DB_TABLE . " (`id`, `netid`, `vlanpoolid`, `ip`, `type`, `username`, `password`, `remote-id`, `interface`, `relay`)
  935. VALUES (NULL, '" . $netid . "', '" . $vlanpoolid . "', '" . $ip . "', '" . $type . "', '" . $username . "', '" . $password . "', '" . $remote . "', '" . $interface . "', '" . $relay . "' )";
  936. nr_query($query);
  937. log_register('ADD Terminator `' . $type . '`');
  938. }
  939. /**
  940. * Editing vlan terminator data in DB
  941. *
  942. * @param int $NetID
  943. * @param int $VlanPool
  944. * @param string $TerminatorIP
  945. * @param string $TerminatorType
  946. * @param string $TerminatorLogin
  947. * @param string $TerminatorPass
  948. * @param string $RemoteID
  949. * @param string $Interface
  950. * @param string $Relay
  951. * @param int $id
  952. *
  953. * @return void
  954. */
  955. public function edit($NetID, $VlanPool, $TerminatorIP, $TerminatorType, $TerminatorLogin, $TerminatorPass, $RemoteID, $Interface, $Relay, $id) {
  956. simple_update_field(self::DB_TABLE, 'netid', $NetID, "WHERE `id`='" . $id . "'");
  957. simple_update_field(self::DB_TABLE, 'vlanpoolid', $VlanPool, "WHERE `id`='" . $id . "'");
  958. simple_update_field(self::DB_TABLE, 'ip', $TerminatorIP, "WHERE `id`='" . $id . "'");
  959. simple_update_field(self::DB_TABLE, 'type', $TerminatorType, "WHERE `id`='" . $id . "'");
  960. simple_update_field(self::DB_TABLE, 'username', $TerminatorLogin, "WHERE `id`='" . $id . "'");
  961. simple_update_field(self::DB_TABLE, 'password', $TerminatorPass, "WHERE `id`='" . $id . "'");
  962. simple_update_field(self::DB_TABLE, 'remote-id', $RemoteID, "WHERE `id`='" . $id . "'");
  963. simple_update_field(self::DB_TABLE, 'interface', $Interface, "WHERE `id`='" . $id . "'");
  964. simple_update_field(self::DB_TABLE, 'relay', $Relay, "WHERE `id`='" . $id . "'");
  965. log_register('MODIFY Vlan Terminator [' . $id . ']');
  966. }
  967. /**
  968. * Show's all vlan terminators data
  969. *
  970. * @return string
  971. */
  972. public function RenderTerminators() {
  973. $tablecells = wf_TableCell(__('ID'));
  974. $tablecells .= wf_TableCell(__('Network'));
  975. $tablecells .= wf_TableCell(__('Vlan pool'));
  976. $tablecells .= wf_TableCell(__('IP'));
  977. $tablecells .= wf_TableCell(__('Type'));
  978. $tablecells .= wf_TableCell(__('Username'));
  979. $tablecells .= wf_TableCell(__('Password'));
  980. $tablecells .= wf_TableCell(__('Remote-ID'));
  981. $tablecells .= wf_TableCell(__('Interface'));
  982. $tablecells .= wf_TableCell(__('Relay Address'));
  983. $tablecells .= wf_TableCell(__('Actions'));
  984. $tablerows = wf_TableRow($tablecells, 'row1');
  985. if (!empty($this->AllTerminators)) {
  986. foreach ($this->AllTerminators as $each => $term) {
  987. $tablecells = wf_TableCell($term['id']);
  988. $tablecells .= wf_TableCell($this->NetworkSelector[$term ['netid']]);
  989. $tablecells .= wf_TableCell($this->VlanPoolsSelector[$term['vlanpoolid']]);
  990. $tablecells .= wf_TableCell($term['ip']);
  991. $tablecells .= wf_TableCell($term['type']);
  992. $tablecells .= wf_TableCell($term['username']);
  993. $tablecells .= wf_TableCell($term['password']);
  994. $tablecells .= wf_TableCell($term['remote-id']);
  995. $tablecells .= wf_TableCell($term['interface']);
  996. $tablecells .= wf_TableCell($term['relay']);
  997. $actionlinks = wf_JSAlert(self::MODULE_URL . '&DeleteTerminator=' . $term['id'], web_delete_icon(), 'Removing this may lead to irreparable results');
  998. $actionlinks .= wf_Link(self::MODULE_URL . '&EditTerminator=' . $term['id'], web_edit_icon(), false);
  999. $tablecells .= wf_TableCell($actionlinks);
  1000. $tablerows .= wf_TableRow($tablecells, 'row3');
  1001. }
  1002. }
  1003. $result = wf_TableBody($tablerows, '100%', '0', 'sortable');
  1004. show_window(__('Terminators'), $result);
  1005. }
  1006. /**
  1007. * Returns form for adding vlan terminator to DB
  1008. *
  1009. * @return string
  1010. */
  1011. public function AddForm() {
  1012. $type = array('FreeBSD' => 'FreeBSD', 'Linux' => 'Linux', 'Cisco' => 'Cisco', 'Cisco_static' => 'Cisco_static');
  1013. $sup = wf_tag('sup') . '*' . wf_tag('sup', true);
  1014. $inputs = wf_HiddenInput('AddTerminator', 'true');
  1015. $inputs .= wf_Selector('NetworkSelected', $this->NetworkSelector, __('Network'), '', true);
  1016. $inputs .= wf_Selector('VlanPoolSelected', $this->VlanPoolsSelector, __('Vlan Pool ID'), '', true);
  1017. $inputs .= wf_TextInput('IP', __('IP') . $sup, '', true, '20');
  1018. $inputs .= wf_Selector('Type', $type, __('Type'), '', true);
  1019. $inputs .= wf_TextInput('Username', __('Username') . $sup, '', true, '20');
  1020. $inputs .= wf_TextInput('Password', __('Password') . $sup, '', true, '20');
  1021. $inputs .= wf_TextInput('RemoteID', __('Remote-ID') . $sup, '', true, '20');
  1022. $inputs .= wf_TextInput('Interface', __('Interface') . $sup, '', true, '20');
  1023. $inputs .= wf_TextInput('Relay', __('Relay Address') . $sup, '', true, '20');
  1024. $inputs .= wf_Tag('br');
  1025. $inputs .= wf_Submit(__('Add'));
  1026. $form = wf_Form("", 'POST', $inputs, 'glamour');
  1027. show_window(__('ADD Terminator'), $form);
  1028. }
  1029. /**
  1030. * Returns form for editing vlan terminators data in DB
  1031. *
  1032. * @param int $id
  1033. *
  1034. * @return string
  1035. */
  1036. public function EditForm($id) {
  1037. $TermData = $this->AllTerminators[$id];
  1038. $type = array('FreeBSD' => 'FreeBSD', 'Linux' => 'Linux', 'Cisco' => 'Cisco', 'Cisco_static' => 'Cisco_static');
  1039. $sup = wf_tag('sup') . '*' . wf_tag('sup', true);
  1040. $inputs = wf_HiddenInput('TerminatorEdit', 'true');
  1041. $inputs .= wf_Selector('NetworkSelected', $this->NetworkSelector, __('Network'), $TermData['netid'], true);
  1042. $inputs .= wf_Selector('VlanPoolSelected', $this->VlanPoolsSelector, __('Vlan Pool ID'), $TermData['vlanpoolid'], true);
  1043. $inputs .= wf_TextInput('IP', __('IP') . $sup, $TermData['ip'], true, '20');
  1044. $inputs .= wf_Selector('Type', $type, __('Type'), $TermData['type'], true);
  1045. $inputs .= wf_TextInput('Username', __('Username') . $sup, $TermData['username'], true, '20');
  1046. $inputs .= wf_TextInput('Password', __('Password') . $sup, $TermData['password'], true, '20');
  1047. $inputs .= wf_TextInput('RemoteID', __('Remote-ID') . $sup, $TermData['remote-id'], true, '20');
  1048. $inputs .= wf_TextInput('Interface', __('Interface') . $sup, $TermData['interface'], true, '20');
  1049. $inputs .= wf_TextInput('Relay', __('Relay Address') . $sup, $TermData['relay'], true, '20');
  1050. $inputs .= wf_Tag('br');
  1051. $inputs .= wf_Submit(__('Save'));
  1052. $form = wf_Form("", 'POST', $inputs, 'glamour');
  1053. $form .= wf_BackLink(self::MODULE_URL, '', true);
  1054. show_window(__('Edit'), $form);
  1055. }
  1056. }
  1057. /**
  1058. * Network devices automatic configuration
  1059. */
  1060. class AutoConfigurator {
  1061. const AUTOCONFIG = 'config/autoconfig/';
  1062. /**
  1063. * Contains all available switches data
  1064. *
  1065. * @var array
  1066. */
  1067. protected $AllSwitches = array();
  1068. /**
  1069. * Contains all available switch models data
  1070. *
  1071. * @var array
  1072. */
  1073. protected $AllSwitchModels = array();
  1074. /**
  1075. * Contain all available switch ports data
  1076. *
  1077. * @var array
  1078. */
  1079. protected $AllSwitchPort = array();
  1080. /**
  1081. * Contains all available switch login data
  1082. *
  1083. * @var array
  1084. */
  1085. protected $AllSwitchLogin = array();
  1086. /**
  1087. * Contains all available vlan terminators
  1088. *
  1089. * @var array
  1090. */
  1091. protected $AllTerminators = array();
  1092. /**
  1093. * Placeholder for SNMPHelper() object
  1094. *
  1095. * @var object
  1096. */
  1097. protected $SnmpHelper = '';
  1098. /**
  1099. * Containt config of alter.ini
  1100. *
  1101. * @var object
  1102. */
  1103. protected $AltCfg = '';
  1104. public function __construct() {
  1105. $this->LoadAllSwitches();
  1106. $this->LoadAllSwitchPort();
  1107. $this->LoadAllSwitchLogin();
  1108. $this->LoadTerminators();
  1109. $this->SnmpHelper = new SNMPHelper();
  1110. }
  1111. /**
  1112. * Function for getting all switches and place them to $AllSwitches
  1113. *
  1114. * @return void
  1115. */
  1116. protected function LoadAllSwitches() {
  1117. $data = zb_SwitchesGetAll();
  1118. if (!empty($data)) {
  1119. foreach ($data as $each) {
  1120. $this->AllSwitches[$each['id']] = $each;
  1121. }
  1122. }
  1123. }
  1124. /**
  1125. * Function for getting all switchport data and place it to $AllSwitchPort
  1126. *
  1127. * @return void
  1128. */
  1129. protected function LoadAllSwitchPort() {
  1130. $query = "SELECT * FROM `switchportassign`";
  1131. $data = simple_queryall($query);
  1132. if (!empty($data)) {
  1133. foreach ($data as $each) {
  1134. $this->AllSwitchPort[$each['id']] = $each;
  1135. }
  1136. }
  1137. }
  1138. /**
  1139. * Function for getting all switch login data and place it to $AllSwitchLogin
  1140. *
  1141. * @return void
  1142. */
  1143. protected function LoadAllSwitchLogin() {
  1144. $query = "SELECT * FROM " . SwitchLogin::TABLE_NAME;
  1145. $data = simple_queryall($query);
  1146. if (!empty($data)) {
  1147. foreach ($data as $each) {
  1148. $this->AllSwitchLogin[$each['id']] = $each;
  1149. }
  1150. }
  1151. }
  1152. /**
  1153. * Get all available vlan terminators data and place it to $AllTerminators
  1154. *
  1155. * @return void
  1156. */
  1157. protected function LoadTerminators() {
  1158. $query = "SELECT * FROM " . VlanTerminator::DB_TABLE;
  1159. $data = simple_queryall($query);
  1160. if (!empty($data)) {
  1161. foreach ($data as $each) {
  1162. $this->AllTerminators[$each['id']] = $each;
  1163. }
  1164. }
  1165. }
  1166. /**
  1167. * load alter.ini config
  1168. *
  1169. * @return void
  1170. */
  1171. protected function loadAlter() {
  1172. global $ubillingConfig;
  1173. $this->AltCfg = $ubillingConfig->getAlter();
  1174. }
  1175. /**
  1176. * Load all from `switchmodels` to $AllSwitchModels.
  1177. *
  1178. * @return void
  1179. */
  1180. protected function LoadAllSwitchModels() {
  1181. $query = "SELECT * FROM `switchmodels`";
  1182. $data = simple_queryall($query);
  1183. if (!empty($data)) {
  1184. foreach ($data as $each) {
  1185. $this->AllSwitchModels[$each['id']] = $each['modelname'];
  1186. }
  1187. }
  1188. }
  1189. /**
  1190. * Function for getting switchport data by login
  1191. *
  1192. * @param string $login
  1193. *
  1194. * @return array/bool bool if false
  1195. */
  1196. protected function GetSwitchPortData($login) {
  1197. $data = $this->AllSwitchPort;
  1198. if (!empty($data)) {
  1199. foreach ($data as $each) {
  1200. if ($each['login'] == $login) {
  1201. $SwitchPortData = $each;
  1202. }
  1203. }
  1204. if (isset($SwitchPortData)) {
  1205. return($SwitchPortData);
  1206. } else {
  1207. return(false);
  1208. }
  1209. } else {
  1210. return(false);
  1211. }
  1212. }
  1213. /**
  1214. * Function for getting switch login data by switchid
  1215. *
  1216. * @param integer $switchid
  1217. *
  1218. * @return array
  1219. */
  1220. protected function GetSwitchLoginData($switchid) {
  1221. $data = $this->AllSwitchLogin;
  1222. if (!empty($data)) {
  1223. foreach ($data as $each) {
  1224. if ($each['swid'] == $switchid) {
  1225. $SwitchLoginData = $each;
  1226. }
  1227. }
  1228. if (isset($SwitchLoginData)) {
  1229. return ($SwitchLoginData);
  1230. } else {
  1231. return (false);
  1232. }
  1233. } else {
  1234. return (false);
  1235. }
  1236. }
  1237. /**
  1238. * Function for getting switch data by switchid
  1239. *
  1240. * @param integer $switchid
  1241. *
  1242. * @return array or false
  1243. */
  1244. protected function GetSwitchesData($switchid) {
  1245. $data = $this->AllSwitches;
  1246. if (!empty($data)) {
  1247. foreach ($data as $each) {
  1248. if ($each['id'] == $switchid) {
  1249. $SwitchesData = $each;
  1250. }
  1251. }
  1252. if (isset($SwitchesData)) {
  1253. return($SwitchesData);
  1254. } else {
  1255. return(false);
  1256. }
  1257. } else {
  1258. return (false);
  1259. }
  1260. }
  1261. /**
  1262. * Get switch IP by ID
  1263. *
  1264. * @param integer $parentid
  1265. *
  1266. * @return string or false
  1267. */
  1268. protected function GetSwUplinkIP($parentid) {
  1269. $data = $this->AllSwitches;
  1270. if (!empty($data)) {
  1271. foreach ($data as $each) {
  1272. if ($each['id'] == $parentid) {
  1273. $result = $each['ip'];
  1274. }
  1275. }
  1276. if (isset($result)) {
  1277. return($result);
  1278. } else {
  1279. return(false);
  1280. }
  1281. } else {
  1282. return (false);
  1283. }
  1284. }
  1285. /**
  1286. * Check if IP belongs to vlan terminator
  1287. *
  1288. * @param string $ip
  1289. *
  1290. * @return bool
  1291. */
  1292. protected function CheckTermIP($ip) {
  1293. $result = false;
  1294. if (!empty($this->AllTerminators)) {
  1295. foreach ($this->AllTerminators as $each) {
  1296. if ($each['ip'] == $ip) {
  1297. $result = true;
  1298. }
  1299. }
  1300. }
  1301. return($result);
  1302. }
  1303. /**
  1304. * Check weather vlan already created on switch
  1305. *
  1306. * @param string $ip
  1307. * @param string $community
  1308. * @param string $oid
  1309. *
  1310. * @return bool
  1311. */
  1312. protected function CheckVlan($ip, $community, $oid) {
  1313. $query = $this->SnmpHelper->walk($ip, $community, $oid, false);
  1314. $query = trim($query);
  1315. $tmp = explode("=", $query);
  1316. if (isset($tmp[1])) {
  1317. if (!empty($tmp[1])) {
  1318. return(true);
  1319. } else {
  1320. return (false);
  1321. }
  1322. } else {
  1323. return(false);
  1324. }
  1325. }
  1326. /**
  1327. * Check if vlan was applied on port
  1328. *
  1329. * @param string $ip
  1330. * @param string $community
  1331. * @param string $oid
  1332. * @param integer $vlan
  1333. *
  1334. * @return bool
  1335. */
  1336. protected function CheckPvid($ip, $community, $oid, $vlan) {
  1337. $query = $this->SnmpHelper->walk($ip, $community, $oid, false);
  1338. $query = trim($query);
  1339. $tmp = explode(":", $query);
  1340. if (isset($tmp[1])) {
  1341. if ($tmp[1] == $vlan) {
  1342. return(true);
  1343. } else {
  1344. return(false);
  1345. }
  1346. } else {
  1347. return(false);
  1348. }
  1349. }
  1350. /**
  1351. * Change pvid on users port
  1352. *
  1353. * @param string $login
  1354. * @param integer $vlan
  1355. *
  1356. * @return void
  1357. */
  1358. public function ChangePvid($login, $vlan) {
  1359. if ($this->GetSwitchPortData($login)) {
  1360. $SwitchPortData = $this->GetSwitchPortData($login);
  1361. $port = $SwitchPortData['port'];
  1362. $SwitchId = $SwitchPortData['switchid'];
  1363. $ModelId = $this->AllSwitches[$SwitchId]['modelid'];
  1364. if ($this->GetSwitchLoginData($SwitchId)) {
  1365. $SwitchLoginData = $this->GetSwitchLoginData($SwitchId);
  1366. $method = $SwitchLoginData['method'];
  1367. if ($method == 'SNMP') {
  1368. $community = $SwitchLoginData['community'];
  1369. $snmpTemplate = $SwitchLoginData['snmptemplate'];
  1370. if ($this->GetSwitchesData($SwitchId)) {
  1371. $SwitchesData = $this->GetSwitchesData($SwitchId);
  1372. $ip = $SwitchesData['ip'];
  1373. $ParentId = $SwitchesData['parentid'];
  1374. if (file_exists(self::AUTOCONFIG . $snmpTemplate)) {
  1375. $SNMPData = rcms_parse_ini_file(self::AUTOCONFIG . $snmpTemplate, true);
  1376. if (isset($SNMPData['define']['HEX'])) {
  1377. $group = 0;
  1378. if ($port > 4) {
  1379. $portPlace = $port % 4;
  1380. if ($portPlace == 0) {
  1381. $portPlace = 4;
  1382. }
  1383. $counter = $port;
  1384. while ($counter > 0) {
  1385. $group++;
  1386. $counter -= 4;
  1387. }
  1388. } else {
  1389. $group = 1;
  1390. $portPlace = $port;
  1391. }
  1392. switch ($portPlace) {
  1393. case 1:
  1394. $portPlaceHex = 8;
  1395. break;
  1396. case 2:
  1397. $portPlaceHex = 4;
  1398. break;
  1399. case 3:
  1400. $portPlaceHex = 2;
  1401. break;
  1402. case 4:
  1403. $portPlaceHex = 1;
  1404. break;
  1405. }
  1406. $hexString = $SNMPData['define']['HEX'];
  1407. $hexString = str_replace(' ', '', $hexString);
  1408. $hexString[$group - 1] = $portPlaceHex;
  1409. $split = str_split($hexString);
  1410. $hexString = '';
  1411. $stringCounter = 1;
  1412. foreach ($split as $each) {
  1413. if (($stringCounter % 2) == 0) {
  1414. $hexString .= $each . " ";
  1415. } else {
  1416. $hexString .= $each;
  1417. }
  1418. $stringCounter++;
  1419. }
  1420. $pattern = array('/PORT/', '/VLAN/', '/HEX/', '/LOGIN/');
  1421. $replace = array($port, $vlan, $hexString, $login);
  1422. } else {
  1423. $pattern = array('/PORT/', '/VLAN/', '/LOGIN/');
  1424. $replace = array($port, $vlan, $login);
  1425. }
  1426. if ($SNMPData['define']['TYPE'] == 'simple') {
  1427. foreach ($SNMPData as $section => $eachpoll) {
  1428. if ($section != 'define') {
  1429. if ($this->CheckVlan($ip, $community, $SNMPData['define']['CHECK'] . "." . $vlan)) {
  1430. if ($section != 'create') {
  1431. $data[] = array(
  1432. 'oid' => preg_replace($pattern, $replace, $eachpoll['OID']),
  1433. 'type' => $eachpoll['TYPE'],
  1434. 'value' => preg_replace($pattern, $replace, $eachpoll['VALUE'])
  1435. );
  1436. }
  1437. } else {
  1438. $data[] = array(
  1439. 'oid' => preg_replace($pattern, $replace, $eachpoll['OID']),
  1440. 'type' => $eachpoll['TYPE'],
  1441. 'value' => preg_replace($pattern, $replace, $eachpoll['VALUE'])
  1442. );
  1443. }
  1444. }
  1445. }
  1446. $result = $this->SnmpHelper->set($ip, $community, $data);
  1447. if (isset($result)) {
  1448. $CheckOid = preg_replace($pattern, $replace, $SNMPData['change']['OID']);
  1449. if ($this->CheckPvid($ip, $community, $CheckOid, $vlan)) {
  1450. if (!empty($ParentId)) {
  1451. $this->CreateVlanLooped($ParentId, $vlan);
  1452. } else {
  1453. show_warning(__("Switch has no uplink"));
  1454. }
  1455. log_register(__("Change PVID to") . " " . $vlan . " vlan " . __("on port") . " " . $port . " " . __("switch") . " " . $ip . " " . __("for") . " " . $login);
  1456. show_success($result);
  1457. } else {
  1458. show_error(__("Something goes wrong, vlan wasnt applied on port"));
  1459. }
  1460. } else {
  1461. show_error(__('Nothing happend'));
  1462. }
  1463. }
  1464. if ($SNMPData['define']['TYPE'] == 'alcatel') {
  1465. $data = '';
  1466. foreach ($SNMPData as $section => $eachpoll) {
  1467. if ($section != 'define') {
  1468. if ($this->CheckVlan($ip, $community, $SNMPData['define']['CHECK'] . "." . $vlan)) {
  1469. if ($section != 'create') {
  1470. $oid = preg_replace($pattern, $replace, $eachpoll['OID']);
  1471. $type = $eachpoll['TYPE'];
  1472. $value = preg_replace($pattern, $replace, $eachpoll['VALUE']);
  1473. $data .= $oid . ' ' . $type . ' ' . $value . ' ';
  1474. }
  1475. } else {
  1476. $oid = preg_replace($pattern, $replace, $eachpoll['OID']);
  1477. $type = $eachpoll['TYPE'];
  1478. $value = preg_replace($pattern, $replace, $eachpoll['VALUE']);
  1479. $data .= $oid . ' ' . $type . ' ' . $value . ' ';
  1480. }
  1481. }
  1482. }
  1483. $this->loadAlter();
  1484. if ($this->AltCfg['SNMP_MODE'] != 'system') {
  1485. $snmpSet = $this->AltCfg['SNMPSET_PATH'];
  1486. $snmpSet .= ' -c ' . $community . ' ' . $ip . ' ' . $data;
  1487. $result = shell_exec($snmpSet);
  1488. } else {
  1489. $result = $this->SnmpHelper->set($ip, $community, $data);
  1490. }
  1491. if (isset($result)) {
  1492. $CheckOid = preg_replace($pattern, $replace, $SNMPData['change']['OID']);
  1493. if ($this->CheckPvid($ip, $community, $CheckOid, $vlan)) {
  1494. if (!empty($ParentId)) {
  1495. $this->CreateVlanLooped($ParentId, $vlan);
  1496. } else {
  1497. show_warning(__("Switch has no uplink"));
  1498. }
  1499. log_register(__("Change PVID to") . " " . $vlan . " vlan " . __("on port") . " " . $port . " " . __("switch") . " " . $ip . " " . __("for") . " " . $login);
  1500. show_success($result);
  1501. } else {
  1502. show_error(__("Something goes wrong, vlan wasnt applied on port"));
  1503. }
  1504. } else {
  1505. show_error(__('Nothing happend'));
  1506. }
  1507. }
  1508. } else {
  1509. show_error(__("No suitable SNMP template found"));
  1510. }
  1511. } else {
  1512. show_error(__('Swich has no ip or parent for switchid' . ' ' . $SwitchId));
  1513. }
  1514. } else {
  1515. $swlogin = $SwitchLoginData['swlogin'];
  1516. $swpass = $SwitchLoginData['swpass'];
  1517. if ($this->GetSwitchesData($SwitchId)) {
  1518. $SwitchesData = $this->GetSwitchesData($SwitchId);
  1519. $ip = $SwitchesData['ip'];
  1520. $this->LoadAllSwitchModels();
  1521. $swmodel = $this->AllSwitchModels[$ModelId];
  1522. shell_exec(CONFIG_PATH . "scripts/$swmodel $swlogin $swpass $ip $vlan $port");
  1523. show_success(__("Success"));
  1524. }
  1525. }
  1526. } else {
  1527. show_error(__('No switch login data found for switchid' . ' ' . $SwitchId));
  1528. }
  1529. } else {
  1530. show_error(__('No switchport data found'));
  1531. }
  1532. }
  1533. /**
  1534. * Create vlan on transit switches
  1535. *
  1536. * @param integer $SwitchId
  1537. * @param integer $vlan
  1538. *
  1539. * @return void
  1540. */
  1541. public function CreateVlanLooped($SwitchId, $vlan, $parent = true) {
  1542. if (!$parent) {
  1543. if ($this->GetSwitchesData($SwitchId)) {
  1544. $SwitchesDataParent = $this->GetSwitchesData($SwitchId);
  1545. $ParentIdtmp = $SwitchesDataParent['parentid'];
  1546. $SwitchId = $ParentIdtmp;
  1547. }
  1548. }
  1549. $result = '';
  1550. while (!empty($SwitchId)) {
  1551. $SwitchIp = $this->GetSwUplinkIP($SwitchId);
  1552. if ($this->CheckTermIP($SwitchIp)) {
  1553. break;
  1554. }
  1555. if ($this->GetSwitchLoginData($SwitchId)) {
  1556. $SwitchLoginData = $this->GetSwitchLoginData($SwitchId);
  1557. $method = $SwitchLoginData['method'];
  1558. if ($method == 'SNMP') {
  1559. $community = $SwitchLoginData['community'];
  1560. $snmpTemplate = $SwitchLoginData['snmptemplate'];
  1561. if ($this->GetSwitchesData($SwitchId)) {
  1562. $SwitchesData = $this->GetSwitchesData($SwitchId);
  1563. $ip = $SwitchesData['ip'];
  1564. $ParentId = $SwitchesData['parentid'];
  1565. if (file_exists(self::AUTOCONFIG . $snmpTemplate)) {
  1566. $SNMPData = rcms_parse_ini_file(self::AUTOCONFIG . $snmpTemplate, true);
  1567. $pattern = '/VLAN/';
  1568. $replace = $vlan;
  1569. $CheckOid = preg_replace($pattern, $replace, $SNMPData['create']['OID']);
  1570. if ($SNMPData['define']['TYPE'] == 'simple') {
  1571. if ($this->CheckVlan($ip, $community, $CheckOid)) {
  1572. foreach ($SNMPData as $section => $eachpoll) {
  1573. if ($section == 'save') {
  1574. $data[] = array(
  1575. 'oid' => preg_replace($pattern, $replace, $eachpoll['OID']),
  1576. 'type' => $eachpoll['TYPE'],
  1577. 'value' => preg_replace($pattern, $replace, $eachpoll['VALUE'])
  1578. );
  1579. }
  1580. }
  1581. log_register(__("VLAN") . " " . $vlan . " " . __("already created") . " " . __("on switch") . " " . $ip);
  1582. } else {
  1583. foreach ($SNMPData as $section => $eachpoll) {
  1584. if ($section != 'define') {
  1585. if ($section != 'change') {
  1586. $data[] = array(
  1587. 'oid' => preg_replace($pattern, $replace, $eachpoll['OID']),
  1588. 'type' => $eachpoll['TYPE'],
  1589. 'value' => preg_replace($pattern, $replace, $eachpoll['VALUE'])
  1590. );
  1591. }
  1592. }
  1593. }
  1594. }
  1595. $result .= $this->SnmpHelper->set($ip, $community, $data);
  1596. if ($this->CheckVlan($ip, $community, $CheckOid)) {
  1597. log_register(__("Created vlan") . " " . $vlan . " " . __("on switch") . " " . $ip);
  1598. }
  1599. $SwitchId = $ParentId;
  1600. }
  1601. } else {
  1602. break;
  1603. }
  1604. } else {
  1605. show_error(__('Swich has no ip or parent for switchid' . ' ' . $SwitchId));
  1606. }
  1607. }
  1608. } else {
  1609. show_error(__('No switch login data found for switchid' . ' ' . $SwitchId));
  1610. }
  1611. }
  1612. return ($result);
  1613. }
  1614. }
  1615. /**
  1616. * PON ONU configuration class
  1617. */
  1618. class OnuConfigurator {
  1619. /**
  1620. * Contains all onu data
  1621. *
  1622. * @var array
  1623. */
  1624. protected $allOnu = array();
  1625. /**
  1626. * Contains all OLT's data
  1627. *
  1628. * @var array
  1629. */
  1630. protected $allOlt = array();
  1631. /**
  1632. * Contains all olt models and snmptemplates
  1633. *
  1634. * @var array
  1635. */
  1636. protected $allOltModels = array();
  1637. /**
  1638. * Contains alter.ini config file
  1639. *
  1640. * @var array
  1641. */
  1642. protected $altCfg = array();
  1643. /**
  1644. * Placeholder for AutoConfigurator() class
  1645. *
  1646. * @var placeholder
  1647. */
  1648. protected $AutoConfig = array();
  1649. public function __construct() {
  1650. $this->loadOnu();
  1651. $this->LoadAllOlt();
  1652. $this->loadOltModels();
  1653. $this->snmp = new SNMPHelper();
  1654. $this->AutoConfig = new AutoConfigurator;
  1655. $this->loadAlter();
  1656. }
  1657. /**
  1658. * Read and load alter.ini to $AltCfg.
  1659. *
  1660. * @global type $ubillingConfig
  1661. *
  1662. * @return void
  1663. */
  1664. protected function loadAlter() {
  1665. global $ubillingConfig;
  1666. $this->AltCfg = $ubillingConfig->getAlter();
  1667. }
  1668. /**
  1669. * Load all from `switches` to $allswitches
  1670. *
  1671. * @return void
  1672. */
  1673. protected function LoadAllOlt() {
  1674. $query = "SELECT `id`,`ip`,`snmp`,`modelid` from `switches` WHERE `desc` LIKE '%OLT%'";
  1675. $raw = simple_queryall($query);
  1676. if (!empty($raw)) {
  1677. foreach ($raw as $io => $each) {
  1678. if (!empty($each['snmp'])) {
  1679. $this->allOlt[$each['id']]['ip'] = $each['ip'];
  1680. $this->allOlt[$each['id']]['snmp'] = $each['snmp'];
  1681. $this->allOlt[$each['id']]['modelid'] = $each['modelid'];
  1682. }
  1683. }
  1684. }
  1685. }
  1686. /**
  1687. * Load all from `pononu` to $allOnu
  1688. *
  1689. * @return void
  1690. */
  1691. protected function loadOnu() {
  1692. $query = "SELECT * from `pononu`";
  1693. $all = simple_queryall($query);
  1694. if (!empty($all)) {
  1695. foreach ($all as $io => $each) {
  1696. $this->allOnu[$each['id']] = $each;
  1697. }
  1698. }
  1699. }
  1700. /**
  1701. * Loads all available snmp models data into private data property
  1702. *
  1703. * @return void
  1704. */
  1705. protected function loadOltModels() {
  1706. $rawModels = zb_SwitchModelsGetAll();
  1707. if (!empty($rawModels)) {
  1708. foreach ($rawModels as $io => $each) {
  1709. $this->allOltModels[$each['id']]['modelname'] = $each['modelname'];
  1710. $this->allOltModels[$each['id']]['snmptemplate'] = $each['snmptemplate'];
  1711. }
  1712. }
  1713. }
  1714. /**
  1715. * Getting olt's snmptemplate by it's ID
  1716. *
  1717. * @param int $modelid
  1718. *
  1719. * @return string
  1720. */
  1721. protected function GetOltModelTemplate($modelid) {
  1722. $result = '';
  1723. if (!empty($this->allOltModels)) {
  1724. $data = $this->allOltModels[$modelid];
  1725. $result = $data['snmptemplate'];
  1726. }
  1727. return($result);
  1728. }
  1729. /**
  1730. * get olt data like ip and snmp community
  1731. *
  1732. * @param int $id
  1733. *
  1734. * @return array
  1735. */
  1736. protected function GetOltData($id) {
  1737. $result = array();
  1738. if (!empty($this->allOlt)) {
  1739. $Olt = $this->allOlt[$id];
  1740. $result[] = $Olt['ip'];
  1741. $result[] = $Olt['snmp'];
  1742. $result[] = $Olt['modelid'];
  1743. }
  1744. return($result);
  1745. }
  1746. /**
  1747. * Get onu data mac and olt ID to which onu is linked
  1748. *
  1749. * @param string $login
  1750. *
  1751. * @return array
  1752. */
  1753. protected function GetOnuMac($login) {
  1754. $allOnu = $this->allOnu;
  1755. $result = array();
  1756. if (!empty($allOnu)) {
  1757. foreach ($allOnu as $eachOnu => $each) {
  1758. if ($each['login'] == $login) {
  1759. $result[] = $each['mac'];
  1760. $result[] = $each['oltid'];
  1761. }
  1762. }
  1763. }
  1764. return $result;
  1765. }
  1766. /**
  1767. * Format heximal mac address to decimal or show error
  1768. *
  1769. * @param string $macOnu
  1770. *
  1771. * @return string
  1772. */
  1773. protected function MacHexToDec($macOnu) {
  1774. if (check_mac_format($macOnu)) {
  1775. $res = array();
  1776. $args = explode(":", $macOnu);
  1777. foreach ($args as $each) {
  1778. $res[] = hexdec($each);
  1779. }
  1780. $string = implode(".", $res);
  1781. return ($string);
  1782. } else {
  1783. show_error("Wrong mac format (shoud be XX:XX:XX:XX:XX:XX)");
  1784. }
  1785. }
  1786. /**
  1787. * Get snmp index which linked to onu
  1788. *
  1789. * @param string $macOnu
  1790. * @param string $oltIp
  1791. * @param string $oltCommunity
  1792. *
  1793. * @return int
  1794. */
  1795. protected function GetClientIface($macOnu, $oltIp, $oltCommunity, $ifindex) {
  1796. $macOnuRew = $this->MacHexToDec($macOnu);
  1797. $interface = ($ifindex . "." . $macOnuRew);
  1798. $OltInt = @snmp2_get($oltIp, $oltCommunity, $interface);
  1799. $index = explode(":", $OltInt);
  1800. if (isset($index[1])) {
  1801. $tmp = trim($index[1]);
  1802. return($tmp);
  1803. } else {
  1804. return (false);
  1805. }
  1806. }
  1807. /**
  1808. * Check wheather vlan already exists (if exists return false, if not return true)
  1809. *
  1810. * @param int $vlan
  1811. * @param string $oltIp
  1812. * @param string $oltCommunity
  1813. *
  1814. * @return bool
  1815. */
  1816. protected function CheckOltVlan($vlan, $oltIp, $oltCommunity, $oid) {
  1817. $tmp = @snmp2_get($oltIp, $oltCommunity, $oid . "." . $vlan);
  1818. $tmp = trim(str_replace("INTEGER:", '', $tmp));
  1819. if ($tmp == '1') {
  1820. $res = false;
  1821. } else {
  1822. $res = true;
  1823. }
  1824. return ($res);
  1825. }
  1826. /**
  1827. * Changes onu pvid by snmp query and if needed creates vlan
  1828. *
  1829. * @param string $login
  1830. * @param int $vlan
  1831. *
  1832. * @return string
  1833. */
  1834. public function ChangeOnuPvid($login, $vlan, $onu_port = '1') {
  1835. $OnuData = $this->GetOnuMac($login);
  1836. if (!empty($OnuData)) {
  1837. $OnuMac = $OnuData[0];
  1838. $oltId = $OnuData[1];
  1839. $oltData = $this->GetOltData($oltId);
  1840. if (!empty($oltData)) {
  1841. $oltIp = $oltData[0];
  1842. $oltCommunity = $oltData[1];
  1843. $template = $this->GetOltModelTemplate($oltData[2]);
  1844. if (!empty($template)) {
  1845. if (file_exists('config/snmptemplates/' . $template)) {
  1846. $iniData = rcms_parse_ini_file('config/snmptemplates/' . $template, true);
  1847. if ($iniData['vlan']['VLANMODE'] == 'BDCOM_B') {
  1848. $vlanCreateOid = $iniData['vlan']['CREATE'];
  1849. $ChangeOnuPvidOid = $iniData['vlan']['PVID'];
  1850. $SaveConfigOid = $iniData['vlan']['SAVE'];
  1851. $CheckVlanOid = $iniData['vlan']['CHECK'];
  1852. $IfIndexOid = $iniData['vlan']['IFINDEX'];
  1853. $IfIndex = $this->GetClientIface($OnuMac, $oltIp, $oltCommunity, $IfIndexOid);
  1854. if ($IfIndex) {
  1855. $VlanCheck = $this->CheckOltVlan($vlan, $oltIp, $oltCommunity, $CheckVlanOid);
  1856. $data = array();
  1857. if ($VlanCheck) {
  1858. //create vlan on OLT
  1859. $data[] = array(
  1860. 'oid' => $vlanCreateOid . "." . $vlan,
  1861. 'type' => 'i',
  1862. 'value' => '4'
  1863. );
  1864. }
  1865. //Change pvid on onu port by default port 1
  1866. $data[] = array(
  1867. 'oid' => $ChangeOnuPvidOid . "." . $IfIndex . "." . $onu_port,
  1868. 'type' => 'i',
  1869. 'value' => "$vlan"
  1870. );
  1871. $data[] = array(
  1872. 'oid' => $SaveConfigOid,
  1873. 'type' => 'i',
  1874. 'value' => '1'
  1875. );
  1876. $result = $this->snmp->set($oltIp, $oltCommunity, $data);
  1877. $result .= $this->AutoConfig->CreateVlanLooped($oltId, $vlan, false);
  1878. return ($result);
  1879. } else {
  1880. show_error(__('Cant find onu'));
  1881. }
  1882. }
  1883. //hacks for BDCOM 3310C
  1884. if ($iniData['vlan']['VLANMODE'] == 'BDCOM_C') {
  1885. $vlanCreateOid = $iniData['vlan']['CREATE'];
  1886. $ChangeOnuPvidOid = $iniData['vlan']['PVID'];
  1887. $SaveConfigOid = $iniData['vlan']['SAVE'];
  1888. $CheckVlanOid = $iniData['vlan']['CHECK'];
  1889. $allOnuOid = $iniData['signal']['MACINDEX'];
  1890. snmp_set_oid_output_format(SNMP_OID_OUTPUT_NUMERIC);
  1891. $allOnu = @snmp2_real_walk($oltIp, $oltCommunity, $allOnuOid);
  1892. $searchArray = array();
  1893. if (!empty($allOnu)) {
  1894. foreach ($allOnu as $eachIndex => $eachOnu) {
  1895. $eachIndex = trim(str_replace($allOnuOid . '.', '', $eachIndex));
  1896. $eachOnu = strtolower(trim(str_replace($iniData['signal']['MACVALUE'], '', $eachOnu)));
  1897. $eachOnuMacArray = explode(" ", $eachOnu);
  1898. $eachOnuMac = implode(":", $eachOnuMacArray);
  1899. $searchArray[$eachOnuMac] = $eachIndex;
  1900. }
  1901. if (!empty($searchArray) and isset($searchArray[$OnuMac])) {
  1902. $IfIndex = $searchArray[$OnuMac];
  1903. $VlanCheck = $this->CheckOltVlan($vlan, $oltIp, $oltCommunity, $CheckVlanOid);
  1904. $data = array();
  1905. if ($VlanCheck) {
  1906. //create vlan on OLT
  1907. $data[] = array(
  1908. 'oid' => $vlanCreateOid . "." . $vlan,
  1909. 'type' => 'i',
  1910. 'value' => '4'
  1911. );
  1912. }
  1913. //Change pvid on onu port by default port 1
  1914. $data[] = array(
  1915. 'oid' => $ChangeOnuPvidOid . "." . $IfIndex . "." . $onu_port,
  1916. 'type' => 'i',
  1917. 'value' => "$vlan"
  1918. );
  1919. $data[] = array(
  1920. 'oid' => $SaveConfigOid,
  1921. 'type' => 'i',
  1922. 'value' => '1'
  1923. );
  1924. $result = $this->snmp->set($oltIp, $oltCommunity, $data);
  1925. $result .= $this->AutoConfig->CreateVlanLooped($oltId, $vlan, false);
  1926. return ($result);
  1927. } else {
  1928. show_error(__('Cant find onu'));
  1929. }
  1930. }
  1931. }
  1932. if ($iniData['signal']['SIGNALMODE'] == 'ZTE') {
  1933. $UniIndex = '';
  1934. $allCards = array();
  1935. $allCardsRange = array();
  1936. $OnuUniRange = array();
  1937. $FoundCardData = '';
  1938. $ChangeOnuPvidOid = $iniData['vlan']['PVID'];
  1939. $ChangeOnuPvidAddOid = $iniData['vlan']['ADDUNI'];
  1940. $GetAllOnuOid = $iniData['vlan']['ALLONU'];
  1941. $AllCardsOid = $iniData['vlan']['ALLCARDS'];
  1942. $ApplyOnuPvidOid = $iniData['vlan']['PVID'];
  1943. $ChangeOnuTrunkOid = $iniData['vlan']['TRUNK'];
  1944. $ChangeOnuTrunkAddOid = $iniData['vlan']['ADDPON'];
  1945. $AllOnu = @snmp2_real_walk($oltIp, $oltCommunity, $GetAllOnuOid);
  1946. $zteFormat = explode(":", $OnuMac);
  1947. $zteFormatMac = $zteFormat[0] . $zteFormat[1] . '.' . $zteFormat[2] . $zteFormat[3] . '.' . $zteFormat[4] . $zteFormat[5];
  1948. foreach ($AllOnu as $index => $eachOnu) {
  1949. $eachOnu = trim(str_replace('Hex-STRING:', '', $eachOnu));
  1950. if ($eachOnu == $zteFormatMac) {
  1951. $UniIndex = str_replace($GetAllOnuOid, '', $index);
  1952. }
  1953. }
  1954. if (empty($UniIndex)) {
  1955. show_error(__('Cant find onu'));
  1956. die();
  1957. }
  1958. if (!empty($UniIndex)) {
  1959. //Need to find out what cards installed into OLT.
  1960. //Then need expand array of all possible UNI IDs.
  1961. //Then need expand array of all possible PON port IDs.
  1962. //Maybe make some advanced search by found out slot and port and expand array for onu pon ports
  1963. //only for founded slot and port. It could be usefull if many slots and ports are used.
  1964. $allCards = @snmp2_real_walk($oltIp, $oltCommunity, $AllCardsOid);
  1965. if (!empty($allCards)) {
  1966. foreach ($allCards as $cardIndex => $eachCard) {
  1967. $startId = 805830912;
  1968. $searchIndex[] = $AllCardsOid . '.0.0.';
  1969. $searchIndex[] = $AllCardsOid . '.1.1.';
  1970. $cardIndex = str_replace($searchIndex, '', $cardIndex);
  1971. $searchCard4[] = '/EPFC/';
  1972. $searchCard4[] = '/EPFCB/';
  1973. $searchCard8[] = '/ETGO/';
  1974. if (preg_match($searchCard4, $eachCard)) {
  1975. $counter = $startId + (524288 * ($cardIndex - 1));
  1976. for ($i = 1; $i <= 4; $i++) {
  1977. $allCardsRange[$cardIndex][$i][]['start'] = $counter;
  1978. $allCardsRange[$cardIndex][$i][]['end'] = $counter + (64 * 256);
  1979. $counter += 65536;
  1980. }
  1981. }
  1982. if (preg_match($searchCard8, $eachCard)) {
  1983. $counter = $startId + (524288 * ($cardIndex - 1));
  1984. for ($i = 1; $i <= 8; $i++) {
  1985. $allCardsRange[$cardIndex][$i][]['start'] = $counter;
  1986. $allCardsRange[$cardIndex][$i][]['end'] = $counter + (64 * 256);
  1987. $counter += 65536;
  1988. }
  1989. }
  1990. }
  1991. } else {
  1992. show_error(__('No cards found on OLT'));
  1993. die();
  1994. }
  1995. if (!empty($allCardsRange)) {
  1996. foreach ($allCardsRange as $CardNumber) {
  1997. foreach ($CardNumber as $PortNumber => $range) {
  1998. if ($UniIndex > $range['start'] AND $UniIndex < $range['end']) {
  1999. $FoundCardData['card'] = $CardNumber;
  2000. $FoundCardData['port'] = $PortNumber;
  2001. }
  2002. }
  2003. }
  2004. } else {
  2005. show_error(__('No suitable cards found on OLT. Supported: EPFC, EPFCB, ETGO.'));
  2006. die();
  2007. }
  2008. if (!empty($FoundCardData)) {
  2009. //Make array of all available onu number for onu and find it out
  2010. $UniIdStart = 805830912;
  2011. $UniIdCard = $UniIdStart + (524288 * ($FoundCardData['card'] - 1));
  2012. $UniIdPort = $UniIdCard + (65536 * ($FoundCardData['port'] - 1));
  2013. for ($i = 1; $i <= 64; $i++) {
  2014. $OnuUniRange[$UniIdCard] = $i;
  2015. $UniIdCard += 256;
  2016. }
  2017. $onuNumber = $OnuUniRange[$UniIndex];
  2018. //ONU PON PORT FIND PART
  2019. $OnuPonStartId = 1073741824;
  2020. $OnuPonPortOltCardId = $OnuPonStartId + (524288 * ($FoundCardData['card'] - 1));
  2021. $OnuPonPortOltPortId = $OnuPonPortOltCardId + (65536 * ($FoundCardData['port'] - 1));
  2022. $OnuPonPortId = $OnuPonPortOltPortId + (256 * ($onuNumber - 1));
  2023. $snmpSet = $this->altCfg['SNMPSET_PATH'];
  2024. $result = '';
  2025. $result .= shell_exec($snmpSet . ' -c ' . $oltCommunity . ' ' . $oltIp . ' ' . $ChangeOnuPvidAddOid . $UniIndex . '.1' . ' i 2 ' . $ChangeOnuPvidOid . $UniIndex . '.1' . ' i ' . $vlan);
  2026. $result .= shell_exec($snmpSet . ' -c ' . $oltCommunity . ' ' . $oltIp . ' ' . $ApplyOnuPvidOid . $UniIndex . '.1' . ' i 2');
  2027. $result .= shell_exec($snmpSet . ' -c ' . $oltCommunity . ' ' . $oltIp . ' ' . $ChangeOnuTrunkAddOid . $OnuPonPortId . ' i 1 ' . $ChangeOnuTrunkOid . ' i ' . $vlan);
  2028. $result .= $this->AutoConfig->CreateVlanLooped($oltId, $vlan, false);
  2029. return($result);
  2030. } else {
  2031. show_error(__('Onu is out of range of supported cards.'));
  2032. die();
  2033. }
  2034. }
  2035. }
  2036. } else {
  2037. show_error(__('SNMP template for OTL file not exists for modelid' . ' ' . $oltData[2]));
  2038. }
  2039. } else {
  2040. show_error(__('No snmp template for OLT found') . ' modelid ' . $oltData[2]);
  2041. }
  2042. } else {
  2043. show_error(__('No olt data found for oltid' . ' ' . $oltId));
  2044. }
  2045. } else {
  2046. show_error(__('No pair onu->login found in PONizer'));
  2047. }
  2048. }
  2049. }
  2050. /**
  2051. * Get users login by IP
  2052. *
  2053. * @param string $ip
  2054. *
  2055. * @return string
  2056. */
  2057. function UserGetLoginByIP($ip) {
  2058. $login = '';
  2059. $query = "SELECT * FROM `users` WHERE `ip`='" . $ip . "'";
  2060. $res = simple_query($query);
  2061. if (!empty($res)) {
  2062. $login = $res['login'];
  2063. }
  2064. return($login);
  2065. }
  2066. function GetAllUserIp() {
  2067. $query = "SELECT ip,login FROM `users`";
  2068. $data = simple_queryall($query);
  2069. $result = array();
  2070. if (!empty($data)) {
  2071. foreach ($data as $each) {
  2072. $result[$each['ip']] = $each['login'];
  2073. }
  2074. }
  2075. return($result);
  2076. }
  2077. /**
  2078. *
  2079. * @return type
  2080. */
  2081. function GetAllUserVlan() {
  2082. $query = "SELECT * FROM `vlanhosts`";
  2083. $result = array();
  2084. $data = simple_queryall($query);
  2085. if (!empty($data)) {
  2086. foreach ($data as $each) {
  2087. $result[$each['login']] = $each['vlan'];
  2088. }
  2089. }
  2090. return($result);
  2091. }
  2092. function GetAllUserOnu() {
  2093. $query = "SELECT * FROM `pononu`";
  2094. $result = array();
  2095. $data = simple_queryall($query);
  2096. if (!empty($data)) {
  2097. foreach ($data as $each) {
  2098. $result[$each['login']] = $each;
  2099. }
  2100. }
  2101. return($result);
  2102. }
  2103. function loadOltDevices() {
  2104. $allOlts = array();
  2105. $query = "SELECT `switches`.`id`,`switches`.`modelid`,`switchmodels`.`snmptemplate` from `switches` LEFT JOIN `switchmodels` ON (`switches`.`modelid` = `switchmodels`.`id`) WHERE `switches`.`desc` LIKE '%OLT%';";
  2106. $raw = simple_queryall($query);
  2107. if (!empty($raw)) {
  2108. foreach ($raw as $io => $each) {
  2109. $allOlts[$each['id']] = $each;
  2110. }
  2111. }
  2112. return($allOlts);
  2113. }
  2114. function loadOltSnmpTemplates() {
  2115. $allOlts = loadOltDevices();
  2116. $allOltSnmpTemplates = array();
  2117. if (!empty($allOlts)) {
  2118. foreach ($allOlts as $oltId => $eachOltData) {
  2119. $templateFile = 'config/snmptemplates/' . $eachOltData['snmptemplate'];
  2120. $privateTemplateFile = DATA_PATH . 'documents/mysnmptemplates/' . $eachOltData['snmptemplate'];
  2121. if (file_exists($templateFile)) {
  2122. $allOltSnmpTemplates[$eachOltData['id']] = rcms_parse_ini_file($templateFile, true);
  2123. if (file_exists($privateTemplateFile)) {
  2124. $allOltSnmpTemplates[$eachOltData['id']] = rcms_parse_ini_file($privateTemplateFile, true);
  2125. }
  2126. }
  2127. }
  2128. }
  2129. return($allOltSnmpTemplates);
  2130. }
  2131. /**
  2132. * Get users vlan by login
  2133. *
  2134. * @param string $login
  2135. *
  2136. * @return int
  2137. */
  2138. function UserGetVlan($login) {
  2139. $query = "select vlan from vlanhosts where login='" . $login . "'";
  2140. $vlan = simple_query($query);
  2141. $result = '';
  2142. if (!empty($vlan)) {
  2143. $result = $vlan['vlan'];
  2144. }
  2145. return $result;
  2146. }
  2147. /**
  2148. * Get network id by ip
  2149. *
  2150. * @param string $ip
  2151. *
  2152. * @return int
  2153. */
  2154. function GetNetidByIP($ip) {
  2155. $query = "SELECT `netid` FROM `nethosts` WHERE `ip`='" . $ip . "'";
  2156. $res = simple_query($query);
  2157. $result = '';
  2158. if (!empty($res)) {
  2159. $result = $res['netid'];
  2160. }
  2161. return $result;
  2162. }
  2163. /**
  2164. * Get vlan terminators remote id by network id
  2165. *
  2166. * @param int $netid
  2167. *
  2168. * @return string
  2169. */
  2170. function GetTermRemoteByNetid($netid) {
  2171. $query = "SELECT `remote-id` FROM `vlan_terminators` where `netid`='" . $netid . "'";
  2172. $remote = simple_query($query);
  2173. $result = '';
  2174. if (!empty($remote)) {
  2175. $result = $remote['remote-id'];
  2176. }
  2177. return $result;
  2178. }
  2179. /**
  2180. *
  2181. * @param string $login
  2182. *
  2183. * @return string
  2184. */
  2185. function web_ProfileVlanControlForm($login) {
  2186. $login = mysql_real_escape_string($login);
  2187. $query = "SELECT * from `vlanhosts` WHERE `login`='" . $login . "'";
  2188. $formStyle = 'glamour';
  2189. $alterconf = rcms_parse_ini_file(CONFIG_PATH . 'alter.ini');
  2190. if ($alterconf['VLAN_IN_PROFILE'] == 1) {
  2191. $data = simple_query($query);
  2192. if (!empty($data)) {
  2193. $current_vlan = $data ['vlan'];
  2194. $current_vlan_pool = $data['vlanpoolid'];
  2195. $query_desc = "SELECT * FROM `vlan_pools` WHERE `id`='" . $current_vlan_pool . "'";
  2196. $current_vlan_pool_desc = simple_query($query_desc);
  2197. $current_vlan_pool_descr = $current_vlan_pool_desc['desc'];
  2198. $cells = wf_TableCell(__('Vlan Pool'), '30%', 'row2');
  2199. $cells .= wf_TableCell($current_vlan_pool_descr);
  2200. $rows = wf_TableRow($cells, 'row3');
  2201. $cells = wf_TableCell(__('Vlan'), '30%', 'row2');
  2202. $cells .= wf_TableCell($current_vlan);
  2203. $rows .= wf_TableRow($cells, 'row3');
  2204. $result = wf_TableBody($rows, '100%', '0');
  2205. return($result);
  2206. }
  2207. }
  2208. }
  2209. /**
  2210. * Function for deleting entry from DB
  2211. *
  2212. * @param string $login
  2213. *
  2214. * @return void
  2215. */
  2216. function vlan_delete_host($login) {
  2217. $query = "DELETE FROM `vlanhosts` WHERE `login`='" . $login . "'";
  2218. nr_query($query);
  2219. log_register("DELETE VLanHost (" . $login . ")");
  2220. }