common.php 173 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196
  1. <?php
  2. if (!defined("CONFIG")) {
  3. die("Not defined");
  4. }
  5. require_once($_SERVER['DOCUMENT_ROOT'] . "/inc/consts.php");
  6. #ValidIpAddressRegex = "^(([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])\.){3}([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])$";
  7. #ValidHostnameRegex = "^(([a-zA-Z0-9]|[a-zA-Z0-9][a-zA-Z0-9\-]*[a-zA-Z0-9])\.)*([A-Za-z0-9]|[A-Za-z0-9][A-Za-z0-9\-]*[A-Za-z0-9])$";
  8. #$ValidMacAddressRegex="^([0-9A-Fa-f]{2}[:-]){5}[0-9A-Fa-f]{2}|([0-9a-fA-F]{4}[\\.-][0-9a-fA-F]{4}[\\.-][0-9a-fA-F]{4})|[0-9A-Fa-f]{12}$";
  9. $config["init"] = 0;
  10. // #### vendor id
  11. // 1, Unknown
  12. // 2, 'Eltex'
  13. // 3, 'Huawei'
  14. // 4, 'Zyxel'
  15. // 5, 'Raisecom'
  16. // 6, 'SNR'
  17. // 7, 'Dlink'
  18. // 8, 'Allied Telesis'
  19. // 9, 'Mikrotik'
  20. // 10, 'NetGear'
  21. // 11, 'Ubiquiti'
  22. // 15, 'HP'
  23. // 16, 'Cisco'
  24. // 17, 'Maipu'
  25. // 18, 'Asus'
  26. function randomPassword($length = 8)
  27. {
  28. $alphabet = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890';
  29. $pass = array(); //remember to declare $pass as an array
  30. $alphaLength = strlen($alphabet) - 1; //put the length -1 in cache
  31. for ($i = 0; $i < $length; $i++) {
  32. $n = rand(0, $alphaLength);
  33. $pass[] = $alphabet[$n];
  34. }
  35. return implode($pass); //turn the array into a string
  36. }
  37. function mb_ucfirst($str)
  38. {
  39. $str = mb_strtolower($str);
  40. $fc = mb_strtoupper(mb_substr($str, 0, 1));
  41. return $fc . mb_substr($str, 1);
  42. }
  43. function get_user_ip()
  44. {
  45. $auth_ip = getenv("HTTP_CLIENT_IP");
  46. if (empty($auth_ip)) {
  47. $auth_ip = getenv("HTTP_X_FORWARDED_FOR");
  48. if (empty($auth_ip)) {
  49. $auth_ip = getenv("REMOTE_ADDR");
  50. if (empty($auth_ip)) {
  51. $auth_ip = $_SERVER['REMOTE_ADDR'];
  52. }
  53. }
  54. }
  55. return $auth_ip;
  56. }
  57. function fbytes($traff)
  58. {
  59. $units_IEC = array(
  60. "",
  61. "Ki",
  62. "Mi",
  63. "Gi",
  64. "Ti"
  65. );
  66. $units_metric = array(
  67. "",
  68. "k",
  69. "M",
  70. "G",
  71. "T"
  72. );
  73. if (!empty($traff) and $traff > 0) {
  74. $KB = get_const('KB');
  75. if ($KB) {
  76. $KB = 1024;
  77. } else {
  78. $KB = 1000;
  79. }
  80. //IEC
  81. if ($KB == 1024) {
  82. $index = min(((int) log($traff, $KB)), count($units_IEC) - 1);
  83. $result = round($traff / pow($KB, $index), 3) . ' ' . $units_IEC[$index] . 'B';
  84. } else {
  85. $index = min(((int) log($traff, $KB)), count($units_metric) - 1);
  86. $result = round($traff / pow($KB, $index), 3) . ' ' . $units_metric[$index] . 'B';
  87. }
  88. } else {
  89. $result = '0 B';
  90. }
  91. return $result;
  92. }
  93. function fpkts($packets)
  94. {
  95. $units = array(
  96. "",
  97. "k",
  98. "M",
  99. "G",
  100. "T"
  101. );
  102. if (!empty($packets) and $packets > 0) {
  103. $index = min(((int) log($packets, 1000)), count($units) - 1);
  104. $result = round($packets / pow(1000, $index), 3) . ' ' . $units[$index] . 'pkt/s';
  105. } else {
  106. $result = '0 pkt/s';
  107. }
  108. return $result;
  109. }
  110. function checkValidIp($cidr)
  111. {
  112. // Checks for a valid IP address or optionally a cidr notation range
  113. // e.g. 1.2.3.4 or 1.2.3.0/24
  114. // if(!preg_match("^[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}(/[0-9]{1,2}){0,1}$", $cidr)) {
  115. $ip_pattern = '/^(?:25[0-5]|2[0-4]\d|1\d\d|[1-9]\d|\d)(?:[.](?:25[0-5]|2[0-4]\d|1\d\d|[1-9]\d|\d)){3}/';
  116. if (!preg_match($ip_pattern, $cidr)) {
  117. $return = FALSE;
  118. } else {
  119. $return = TRUE;
  120. }
  121. if ($return == TRUE) {
  122. $parts = explode("/", $cidr);
  123. $ip = $parts[0];
  124. if (empty($parts[1])) {
  125. $parts[1] = "32";
  126. }
  127. $netmask = $parts[1];
  128. $octets = explode(".", $ip);
  129. foreach ($octets as $octet) {
  130. if ($octet > 255) {
  131. $return = FALSE;
  132. }
  133. }
  134. if (($netmask != "") && ($netmask > 32)) {
  135. $return = FALSE;
  136. }
  137. }
  138. return $return;
  139. }
  140. function checkValidMac($mac)
  141. {
  142. $ValidMacAddressRegex = "/^([0-9A-Fa-f]{2}[:-]){5}[0-9A-Fa-f]{2}|([0-9a-fA-F]{4}[\\.-][0-9a-fA-F]{4}[\\.-][0-9a-fA-F]{4})|[0-9A-Fa-f]{12}$/";
  143. if (!preg_match($ValidMacAddressRegex, $mac)) {
  144. $return = FALSE;
  145. } else {
  146. $return = TRUE;
  147. }
  148. return $return;
  149. }
  150. function checkValidHostname($dnsname)
  151. {
  152. if (empty($dnsname)) { return TRUE; }
  153. $host_pattern = "/^(([a-zA-Z0-9]|[a-zA-Z0-9][a-zA-Z0-9\-]*[a-zA-Z0-9])\.)*([A-Za-z0-9]|[A-Za-z0-9][A-Za-z0-9\-]*[A-Za-z0-9])$/";
  154. if (!preg_match($host_pattern, $dnsname)) {
  155. $result = FALSE;
  156. } else {
  157. $result = TRUE;
  158. }
  159. return $result;
  160. }
  161. function searchHostname($db, $id, $hostname)
  162. {
  163. if (empty($hostname)) { return NULL; }
  164. $result = '';
  165. $domain_zone = get_option($db, 33);
  166. $a_search_filter = 'SELECT * FROM User_auth WHERE deleted=0 and id !="' . $id . '" and (dns_name ="' . mysqli_real_escape_string($db, $hostname) . '" or dns_name ="' . mysqli_real_escape_string($db, $hostname.'.'.$domain_zone) . '")';
  167. # LOG_DEBUG($db, "A search-filter: ".$a_search_filter);
  168. $a_records = get_records_sql($db, $a_search_filter);
  169. foreach ($a_records as $a_rec) {
  170. $result.='auth_id:'.$a_rec['id'].' ip: '.$a_rec['ip'].'; ';
  171. }
  172. if (!empty($result)) { $result = 'A-record: '.$result; }
  173. $result_cname = '';
  174. $cname_search_filter = 'SELECT * FROM User_auth_alias WHERE auth_id !="' . $id . '" and (alias ="' . mysqli_real_escape_string($db, $hostname) . '" or alias ="' . mysqli_real_escape_string($db, $hostname.'.'.$domain_zone) . '")';
  175. # LOG_DEBUG($db, "CNAME search-filter: ".$cname_search_filter);
  176. $a_records = get_records_sql($db, $cname_search_filter);
  177. foreach ($a_records as $a_rec) {
  178. $result_cname.='auth_id:'.$a_rec['auth_id'].';';
  179. }
  180. if (!empty($result_cname)) { $result_cname = 'CNAME-record: '. $result_cname; }
  181. $result = trim($result.' '.$result_cname);
  182. return $result;
  183. }
  184. function checkUniqHostname($db, $id, $hostname)
  185. {
  186. if (empty($hostname)) { return TRUE; }
  187. $domain_zone = get_option($db, 33);
  188. $check_A_filter = 'deleted=0 and id !="' . $id . '" and (dns_name ="' . mysqli_real_escape_string($db, $hostname) . '" or dns_name ="' . mysqli_real_escape_string($db, $hostname.'.'.$domain_zone) . '")';
  189. # LOG_DEBUG($db, "CNAME filter: ".$check_A_filter);
  190. $count = get_count_records($db, 'User_auth', $check_A_filter);
  191. if ($count > 0) { return FALSE; }
  192. $check_CNAME_filter = 'auth_id !="' . $id . '" and (alias ="' . mysqli_real_escape_string($db, $hostname) . '" or alias ="' . mysqli_real_escape_string($db, $hostname.'.'.$domain_zone) . '")';
  193. # LOG_DEBUG($db, "CNAME filter: ".$check_CNAME_filter);
  194. $count = get_count_records($db, 'User_auth_alias', $check_CNAME_filter);
  195. if ($count > 0) { return FALSE; }
  196. return TRUE;
  197. }
  198. // Транслитерация строк.
  199. function transliterate($string, $gost = false)
  200. {
  201. if ($gost) {
  202. $replace = array(
  203. "А" => "A",
  204. "а" => "a",
  205. "Б" => "B",
  206. "б" => "b",
  207. "В" => "V",
  208. "в" => "v",
  209. "Г" => "G",
  210. "г" => "g",
  211. "Д" => "D",
  212. "д" => "d",
  213. "Е" => "E",
  214. "е" => "e",
  215. "Ё" => "E",
  216. "ё" => "e",
  217. "Ж" => "Zh",
  218. "ж" => "zh",
  219. "З" => "Z",
  220. "з" => "z",
  221. "И" => "I",
  222. "и" => "i",
  223. "Й" => "I",
  224. "й" => "i",
  225. "К" => "K",
  226. "к" => "k",
  227. "Л" => "L",
  228. "л" => "l",
  229. "М" => "M",
  230. "м" => "m",
  231. "Н" => "N",
  232. "н" => "n",
  233. "О" => "O",
  234. "о" => "o",
  235. "П" => "P",
  236. "п" => "p",
  237. "Р" => "R",
  238. "р" => "r",
  239. "С" => "S",
  240. "с" => "s",
  241. "Т" => "T",
  242. "т" => "t",
  243. "У" => "U",
  244. "у" => "u",
  245. "Ф" => "F",
  246. "ф" => "f",
  247. "Х" => "Kh",
  248. "х" => "kh",
  249. "Ц" => "Tc",
  250. "ц" => "tc",
  251. "Ч" => "Ch",
  252. "ч" => "ch",
  253. "Ш" => "Sh",
  254. "ш" => "sh",
  255. "Щ" => "Shch",
  256. "щ" => "shch",
  257. "Ы" => "Y",
  258. "ы" => "y",
  259. "Э" => "E",
  260. "э" => "e",
  261. "Ю" => "Iu",
  262. "ю" => "iu",
  263. "Я" => "Ia",
  264. "я" => "ia",
  265. "ъ" => "",
  266. "ь" => ""
  267. );
  268. } else {
  269. $arStrES = array(
  270. "ае",
  271. "уе",
  272. "ое",
  273. "ые",
  274. "ие",
  275. "эе",
  276. "яе",
  277. "юе",
  278. "ёе",
  279. "ее",
  280. "ье",
  281. "ъе",
  282. "ый",
  283. "ий"
  284. );
  285. $arStrOS = array(
  286. "аё",
  287. "уё",
  288. "оё",
  289. "ыё",
  290. "иё",
  291. "эё",
  292. "яё",
  293. "юё",
  294. "ёё",
  295. "её",
  296. "ьё",
  297. "ъё",
  298. "ый",
  299. "ий"
  300. );
  301. $arStrRS = array(
  302. "а$",
  303. "у$",
  304. "о$",
  305. "ы$",
  306. "и$",
  307. "э$",
  308. "я$",
  309. "ю$",
  310. "ё$",
  311. "е$",
  312. "ь$",
  313. "ъ$",
  314. "@",
  315. "@"
  316. );
  317. $replace = array(
  318. "А" => "A",
  319. "а" => "a",
  320. "Б" => "B",
  321. "б" => "b",
  322. "В" => "V",
  323. "в" => "v",
  324. "Г" => "G",
  325. "г" => "g",
  326. "Д" => "D",
  327. "д" => "d",
  328. "Е" => "Ye",
  329. "е" => "e",
  330. "Ё" => "Ye",
  331. "ё" => "e",
  332. "Ж" => "Zh",
  333. "ж" => "zh",
  334. "З" => "Z",
  335. "з" => "z",
  336. "И" => "I",
  337. "и" => "i",
  338. "Й" => "Y",
  339. "й" => "y",
  340. "К" => "K",
  341. "к" => "k",
  342. "Л" => "L",
  343. "л" => "l",
  344. "М" => "M",
  345. "м" => "m",
  346. "Н" => "N",
  347. "н" => "n",
  348. "О" => "O",
  349. "о" => "o",
  350. "П" => "P",
  351. "п" => "p",
  352. "Р" => "R",
  353. "р" => "r",
  354. "С" => "S",
  355. "с" => "s",
  356. "Т" => "T",
  357. "т" => "t",
  358. "У" => "U",
  359. "у" => "u",
  360. "Ф" => "F",
  361. "ф" => "f",
  362. "Х" => "Kh",
  363. "х" => "kh",
  364. "Ц" => "Ts",
  365. "ц" => "ts",
  366. "Ч" => "Ch",
  367. "ч" => "ch",
  368. "Ш" => "Sh",
  369. "ш" => "sh",
  370. "Щ" => "Shch",
  371. "щ" => "shch",
  372. "Ъ" => "",
  373. "ъ" => "",
  374. "Ы" => "Y",
  375. "ы" => "y",
  376. "Ь" => "",
  377. "ь" => "",
  378. "Э" => "E",
  379. "э" => "e",
  380. "Ю" => "Yu",
  381. "ю" => "yu",
  382. "Я" => "Ya",
  383. "я" => "ya",
  384. "@" => "y",
  385. "$" => "ye"
  386. );
  387. $string = str_replace($arStrES, $arStrRS, $string);
  388. $string = str_replace($arStrOS, $arStrRS, $string);
  389. }
  390. return strtr($string, $replace);
  391. }
  392. function cidrToRange($cidr)
  393. {
  394. $range = array();
  395. $cidr = explode('/', $cidr);
  396. if (!isset($cidr[1])) {
  397. $cidr[1] = 32;
  398. }
  399. $start = (ip2long($cidr[0])) & ((-1 << (32 - (int) $cidr[1])));
  400. $stop = $start + pow(2, (32 - (int) $cidr[1])) - 1;
  401. $range[0] = long2ip($start);
  402. $range[1] = long2ip($stop);
  403. $range[2] = $cidr;
  404. #dhcp
  405. $dhcp_size = round(($stop - $start) / 2, PHP_ROUND_HALF_UP);
  406. $dhcp_start = $start + round($dhcp_size / 2, PHP_ROUND_HALF_UP);
  407. $range[3] = long2ip($dhcp_start);
  408. $range[4] = long2ip($dhcp_start + $dhcp_size);
  409. $range[5] = long2ip($start + 1);
  410. return $range;
  411. }
  412. function crypt_string($simple_string)
  413. {
  414. // Storin gthe cipher method
  415. $ciphering = "aes-128-cbc";
  416. // Using OpenSSl Encryption method
  417. $iv_length = openssl_cipher_iv_length($ciphering);
  418. $options = 0;
  419. // Using openssl_encrypt() function to encrypt the data
  420. return openssl_encrypt($simple_string, $ciphering, ENCRYPTION_KEY, $options, ENCRYPTION_IV);
  421. }
  422. function decrypt_string($crypted_string)
  423. {
  424. // Storin gthe cipher method
  425. $ciphering = "aes-128-cbc";
  426. // Using OpenSSl Encryption method
  427. $iv_length = openssl_cipher_iv_length($ciphering);
  428. $options = 0;
  429. // Using openssl_decrypt() function to decrypt the data
  430. return openssl_decrypt($crypted_string, $ciphering, ENCRYPTION_KEY, $options, ENCRYPTION_IV);
  431. }
  432. function print_ou_select($db, $ou_name, $ou_value)
  433. {
  434. print "<select name=\"$ou_name\" >\n";
  435. $t_ou = mysqli_query($db, "SELECT id,ou_name FROM OU ORDER BY ou_name");
  436. while (list($f_ou_id, $f_ou_name) = mysqli_fetch_array($t_ou)) {
  437. print_select_item($f_ou_name, $f_ou_id, $ou_value);
  438. }
  439. print "</select>\n";
  440. }
  441. function get_subnet_description($db,$subnet_id)
  442. {
  443. if (empty($subnet_id)) { return '';}
  444. $subnet = get_record_sql($db,'SELECT * FROM subnets WHERE id='.$subnet_id);
  445. if (empty($subnet)) { return ''; }
  446. $result = $subnet['subnet'].'&nbsp('.$subnet['comment'].')';
  447. return $result;
  448. }
  449. function print_add_gw_subnets($db, $device_id, $gs_name)
  450. {
  451. print "<select name=\"$gs_name\" >\n";
  452. $t_gs = mysqli_query($db, "SELECT id,subnet,comment FROM subnets WHERE subnets.free=0 AND subnets.id NOT IN (SELECT subnet_id FROM gateway_subnets WHERE gateway_subnets.device_id=".$device_id.") ORDER BY subnet");
  453. while (list($f_gs_id, $f_gs_name,$f_gs_comment) = mysqli_fetch_array($t_gs)) {
  454. print_select_item($f_gs_name.'('.$f_gs_comment.')', $f_gs_id, 0);
  455. }
  456. print "</select>\n";
  457. }
  458. function print_add_dev_interface($db, $device_id, $int_list, $int_name)
  459. {
  460. print "&nbsp<select name=\"$int_name\" >\n";
  461. $t_int = get_records_sql($db, "SELECT * FROM device_l3_interfaces WHERE device_id=".$device_id);
  462. $int_exists=[];
  463. foreach ($t_int as $interface) { $int_exists[$interface['snmpin']]=$interface; }
  464. foreach ($int_list as $interface) {
  465. if (!empty($int_exists[$interface['index']])) { continue; }
  466. $value = $interface['name'].';'.$interface['index'].';'.$interface['type'];
  467. if ($interface['type']==1) { $interface['type']=WEB_select_item_wan; }
  468. if ($interface['type']==0) { $interface['type']=WEB_select_item_lan; }
  469. $display_str = $interface['name'].'&nbsp|'.$interface['ip'].'|'.$interface['type'];
  470. print_select_item($display_str, $value, 0);
  471. }
  472. print "</select>\n";
  473. }
  474. function print_ou_set($db, $ou_name, $ou_value)
  475. {
  476. print "<select name=\"$ou_name\">\n";
  477. $t_ou = mysqli_query($db, "SELECT id,ou_name FROM OU WHERE id>=1 ORDER BY ou_name");
  478. while (list($f_ou_id, $f_ou_name) = mysqli_fetch_array($t_ou)) {
  479. print_select_item($f_ou_name, $f_ou_id, $ou_value);
  480. }
  481. print "</select>\n";
  482. }
  483. function print_subnet_select($db, $subnet_name, $subnet_value)
  484. {
  485. print "<select name=\"$subnet_name\" >\n";
  486. $t_subnet = mysqli_query($db, "SELECT id,subnet FROM subnets ORDER BY ip_int_start");
  487. print_select_item(WEB_select_item_all_ips, 0, $subnet_value);
  488. while (list($f_subnet_id, $f_subnet_name) = mysqli_fetch_array($t_subnet)) {
  489. print_select_item($f_subnet_name, $f_subnet_id, $subnet_value);
  490. }
  491. print "</select>\n";
  492. }
  493. function print_acl_select($db, $acl_name, $acl_value)
  494. {
  495. print "<select name=\"$acl_name\" >\n";
  496. $t_acl = mysqli_query($db, "SELECT id,name FROM acl ORDER BY id");
  497. while (list($f_acl_id, $f_acl_name) = mysqli_fetch_array($t_acl)) {
  498. print_select_item($f_acl_name, $f_acl_id, $acl_value);
  499. }
  500. print "</select>\n";
  501. }
  502. function print_device_ip_select($db, $ip_name, $ip, $user_id)
  503. {
  504. print "<select name=\"$ip_name\">\n";
  505. $auth_list = get_records_sql($db, "SELECT ip FROM User_auth WHERE user_id=$user_id AND deleted=0 ORDER BY ip_int");
  506. foreach ($auth_list as $row) {
  507. print_select_item($row['ip'], $row['ip'], $ip);
  508. }
  509. print "</select>\n";
  510. }
  511. function print_subnet_select_office($db, $subnet_name, $subnet_value)
  512. {
  513. print "<select name=\"$subnet_name\" >\n";
  514. $t_subnet = mysqli_query($db, "SELECT id,subnet FROM subnets WHERE office=1 ORDER BY ip_int_start");
  515. print_select_item(WEB_select_item_all_ips, 0, $subnet_value);
  516. while (list($f_subnet_id, $f_subnet_name) = mysqli_fetch_array($t_subnet)) {
  517. print_select_item($f_subnet_name, $f_subnet_id, $subnet_value);
  518. }
  519. print "</select>\n";
  520. }
  521. function print_subnet_select_office_splitted($db, $subnet_name, $subnet_value)
  522. {
  523. print "<select name=\"$subnet_name\" >\n";
  524. $t_subnet = mysqli_query($db, "SELECT id,subnet,ip_int_start,ip_int_stop FROM subnets WHERE office=1 ORDER BY ip_int_start");
  525. print_select_item(WEB_select_item_all_ips, 0, $subnet_value);
  526. while (list($f_subnet_id, $f_subnet_name,$f_start_ip,$f_stop_ip) = mysqli_fetch_array($t_subnet)) {
  527. print_select_item($f_subnet_name, $f_subnet_name, $subnet_value);
  528. $cidr = cidrToRange($f_subnet_name);
  529. if ($cidr[2][1]<24) {
  530. while ($f_start_ip<=$f_stop_ip) {
  531. print_select_item("&nbsp&nbsp-&nbsp".long2ip($f_start_ip)."/24", long2ip($f_start_ip)."/24", $subnet_value);
  532. $f_start_ip+=256;
  533. }
  534. }
  535. }
  536. print "</select>\n";
  537. }
  538. function print_loglevel_select($item_name, $value)
  539. {
  540. print "<select name=\"$item_name\">\n";
  541. print_select_item('Error', L_ERROR, $value);
  542. print_select_item('Warning', L_WARNING, $value);
  543. print_select_item('Info', L_INFO, $value);
  544. print_select_item('Verbose', L_VERBOSE, $value);
  545. print_select_item('Debug', L_DEBUG, $value);
  546. print "</select>\n";
  547. }
  548. function reencodeurl($url)
  549. {
  550. $url_arr = explode('?', $url);
  551. $fpage = $url_arr[0];
  552. if (isset($url_arr[1])) {
  553. $params = $url_arr[1];
  554. $params_arr = explode('&', $params);
  555. $new_params = '';
  556. foreach ($params_arr as $row) {
  557. $param = explode('=', $row);
  558. $key = $param[0];
  559. $value = urlencode(urldecode($param[1]));
  560. $new_params .= "&" . $key . "=" . $value;
  561. }
  562. $new_params = preg_replace('/^&/', '', $new_params);
  563. } else {
  564. $new_params = '=';
  565. }
  566. if ($new_params === '=') {
  567. $new_url = $fpage;
  568. } else {
  569. $new_url = $fpage . "?" . $new_params;
  570. }
  571. return $new_url;
  572. }
  573. function print_submenu_url($display_name, $page, $current_page, $last)
  574. {
  575. $url_arr = explode('?', $page);
  576. $fpage = $url_arr[0];
  577. $new_url = reencodeurl($page);
  578. if ($fpage === $current_page) {
  579. print "<b>$display_name</b>";
  580. } else {
  581. print "<a href='" . $new_url . "'> $display_name </a>";
  582. }
  583. if (!isset($last) or $last == 0) {
  584. print " | ";
  585. }
  586. }
  587. function print_submenu_nw($display_name, $page, $current_page, $last)
  588. {
  589. $url_arr = explode('?', $page);
  590. $fpage = $url_arr[0];
  591. $new_url = reencodeurl($page);
  592. if ($fpage === $current_page) {
  593. print "<b>$display_name</b>";
  594. } else {
  595. print '<a href="" onclick="window.open(\''.$new_url."', '_tab').focus(); return false;\">". $display_name ."</a>";
  596. }
  597. if (!isset($last) or $last == 0) {
  598. print " | ";
  599. }
  600. }
  601. function print_url($display_name, $page)
  602. {
  603. print "<a href='" . reencodeurl($page) . "'> $display_name </a>";
  604. }
  605. function print_log_submenu($current_page)
  606. {
  607. print "<div id='submenu'>\n";
  608. print_submenu_url(WEB_submenu_dhcp_log, '/admin/logs/dhcp.php', $current_page, 0);
  609. print_submenu_url(WEB_submenu_work_log, '/admin/logs/index.php', $current_page, 0);
  610. print_submenu_url(WEB_submenu_mac_history, '/admin/logs/mac.php', $current_page, 0);
  611. print_submenu_url(WEB_submenu_ip_history, '/admin/logs/ip.php', $current_page, 0);
  612. print_submenu_url(WEB_submenu_mac_unknown, '/admin/logs/unknown.php', $current_page, 0);
  613. print_submenu_url(WEB_submenu_traffic, '/admin/logs/detaillog.php', $current_page, 0);
  614. print_submenu_url(WEB_submenu_syslog, '/admin/logs/syslog.php', $current_page, 1);
  615. print "</div>\n";
  616. }
  617. function print_control_submenu($current_page)
  618. {
  619. print "<div id='submenu'>\n";
  620. print_submenu_url(WEB_submenu_control, '/admin/customers/control.php', $current_page, 0);
  621. print_submenu_url(WEB_submenu_network, '/admin/customers/index-subnets.php', $current_page, 0);
  622. print_submenu_url(WEB_submenu_network_stats, '/admin/customers/control-subnets-usage.php', $current_page, 0);
  623. print_submenu_url(WEB_submenu_options, '/admin/customers/control-options.php', $current_page, 0);
  624. print_submenu_url(WEB_submenu_customers, '/admin/customers/index.php', $current_page, 0);
  625. print_submenu_url(WEB_submenu_buildings, '/admin/customers/building.php', $current_page, 0);
  626. print_submenu_url(WEB_submenu_device_models, '/admin/customers/devmodels.php', $current_page, 0);
  627. print_submenu_url(WEB_submenu_vendors, '/admin/customers/devvendors.php', $current_page, 1);
  628. print "</div>\n";
  629. }
  630. function print_filters_submenu($current_page)
  631. {
  632. print "<div id='submenu'>\n";
  633. print_submenu_url(WEB_submenu_filter_list, '/admin/filters/index.php', $current_page, 0);
  634. print_submenu_url(WEB_submenu_filter_group, '/admin/filters/groups.php', $current_page, 1);
  635. print "</div>\n";
  636. }
  637. function print_reports_submenu($current_page)
  638. {
  639. print "<div id='submenu'>\n";
  640. print_submenu_url(WEB_submenu_traffic_ip_report, '/admin/reports/index-full.php', $current_page, 0);
  641. print_submenu_url(WEB_submenu_traffic_login_report, '/admin/reports/index.php', $current_page, 1);
  642. print "</div>\n";
  643. }
  644. function print_trafdetail_submenu($current_page, $params, $description)
  645. {
  646. print "<div id='subsubmenu'>\n";
  647. print "$description\n";
  648. print_submenu_url(WEB_submenu_traffic_top10, '/admin/reports/userdaydetail.php' . "?$params", $current_page, 0);
  649. print_submenu_url(WEB_submenu_detail_log, '/admin/reports/userdaydetaillog.php' . "?$params", $current_page, 1);
  650. print "</div>\n";
  651. }
  652. function print_device_submenu($current_page)
  653. {
  654. print "<div id='submenu'>\n";
  655. print_submenu_url(WEB_submenu_net_devices, '/admin/devices/index.php', $current_page, 0);
  656. print_submenu_url(WEB_submenu_passive_net_devices, '/admin/devices/index-passive.php', $current_page, 0);
  657. print_submenu_url(WEB_submenu_hierarchy, '/admin/devices/index-tree.php', $current_page, 0);
  658. print_submenu_url(WEB_submenu_ports_vlan, '/admin/devices/portsbyvlan.php', $current_page, 1);
  659. print "</div>\n";
  660. }
  661. function open_window_url ($url)
  662. {
  663. return "window.open('".$url."', '_blank');";
  664. }
  665. function print_editdevice_submenu($current_page, $id, $dev_type, $dev_name = NULL)
  666. {
  667. print "<div id='subsubmenu'>\n";
  668. $dev_id = '';
  669. if (isset($id)) {
  670. $dev_id = '?id=' . $id;
  671. }
  672. if (!empty($dev_name)) {
  673. print "<b>" . $dev_name . "::</b>";
  674. }
  675. print_submenu_url(WEB_submenu_options, '/admin/devices/editdevice.php' . $dev_id, $current_page, 0);
  676. if ($dev_type <= 2) {
  677. print_submenu_url(WEB_submenu_ports, '/admin/devices/switchport.php' . $dev_id, $current_page, 0);
  678. print_submenu_url(WEB_submenu_state, '/admin/devices/switchstatus.php' . $dev_id, $current_page, 0);
  679. print_submenu_url(WEB_submenu_connections, '/admin/devices/switchport-conn.php' . $dev_id, $current_page, 1);
  680. }
  681. print "</div>\n";
  682. }
  683. function print_ip_submenu($current_page)
  684. {
  685. print "<div id='submenu'>\n";
  686. print_submenu_url(WEB_submenu_ip_list, '/admin/iplist/index.php', $current_page, 0);
  687. print_submenu_url(WEB_submenu_nagios, '/admin/iplist/nagios.php', $current_page, 0);
  688. print_submenu_url(WEB_submenu_doubles, '/admin/iplist/doubles.php', $current_page, 0);
  689. print_submenu_url(WEB_submenu_deleted, '/admin/iplist/deleted.php', $current_page, 0);
  690. print_submenu_url(WEB_submenu_auto_rules, '/admin/iplist/auto_rules.php', $current_page, 1);
  691. print "</div>\n";
  692. }
  693. function get_nagios_name($auth)
  694. {
  695. if (!empty($auth['dns_name'])) {
  696. return $auth['dns_name'];
  697. }
  698. if (!empty($auth['dhcp_hostname'])) {
  699. return $auth['dhcp_hostname'];
  700. }
  701. if (!empty($auth['comments'])) {
  702. $result = transliterate($auth['comments']);
  703. $result = preg_replace('/\(/', '-', $result);
  704. $result = preg_replace('/\)/', '-', $result);
  705. $result = preg_replace('/--/', '-', $result);
  706. return $result;
  707. }
  708. if (empty($auth['login'])) {
  709. $auth['login'] = 'host';
  710. }
  711. return $auth['login'] . "_" . $auth['id'];
  712. }
  713. function get_ou($db, $ou_value)
  714. {
  715. if (!isset($ou_value)) {
  716. return;
  717. }
  718. $ou_name = get_record_sql($db, "SELECT ou_name FROM OU WHERE id=$ou_value");
  719. if (empty($ou_name)) {
  720. return;
  721. }
  722. return $ou_name['ou_name'];
  723. }
  724. function get_device_model($db, $model_value)
  725. {
  726. if (!isset($model_value)) {
  727. return;
  728. }
  729. $model_name = get_record_sql($db, "SELECT model_name FROM device_models WHERE id=$model_value");
  730. if (empty($model_name)) {
  731. return;
  732. }
  733. return $model_name['model_name'];
  734. }
  735. function get_device_model_name($db, $model_value)
  736. {
  737. if (!isset($model_value)) {
  738. return '';
  739. }
  740. $model_name = get_record_sql($db, "SELECT M.id,M.model_name,V.name FROM device_models M,vendors V WHERE M.vendor_id = V.id AND M.id=$model_value");
  741. if (empty($model_name)) {
  742. return '';
  743. }
  744. return $model_name['name'] . ' ' . $model_name['model_name'];
  745. }
  746. function get_device_model_vendor($db, $model_value)
  747. {
  748. if (!isset($model_value)) {
  749. return '';
  750. }
  751. $model_name = get_record_sql($db, "SELECT vendor_id FROM device_models WHERE id=$model_value");
  752. if (empty($model_name)) {
  753. return '';
  754. }
  755. return $model_name['vendor_id'];
  756. }
  757. function get_building($db, $building_value)
  758. {
  759. if (!isset($building_value)) {
  760. return;
  761. }
  762. $building_name = get_record_sql($db, "SELECT name FROM building WHERE id=$building_value");
  763. if (empty($building_name)) {
  764. return;
  765. }
  766. return $building_name['name'];
  767. }
  768. function print_device_model_select($db, $device_model_name, $device_model_value)
  769. {
  770. print "<select name=\"$device_model_name\" class=\"js-select-single\">\n";
  771. $t_device_model = mysqli_query($db, "SELECT M.id,M.model_name,V.name FROM device_models M,vendors V WHERE M.vendor_id = V.id ORDER BY V.name,M.model_name");
  772. while (list($f_device_model_id, $f_device_model_name, $f_vendor_name) = mysqli_fetch_array($t_device_model)) {
  773. print_select_item($f_vendor_name . " " . $f_device_model_name, $f_device_model_id, $device_model_value);
  774. }
  775. print "</select>\n";
  776. }
  777. function print_group_select($db, $group_name, $group_value)
  778. {
  779. print "<select name=\"$group_name\">\n";
  780. $t_group = mysqli_query($db, "SELECT id,group_name FROM Group_list Order by group_name");
  781. while (list($f_group_id, $f_group_name) = mysqli_fetch_array($t_group)) {
  782. print_select_item($f_group_name, $f_group_id, $group_value);
  783. }
  784. print "</select>\n";
  785. }
  786. function print_building_select($db, $building_name, $building_value)
  787. {
  788. print "<select name=\"$building_name\">\n";
  789. print_select_item(WEB_select_item_all, 0, $building_value);
  790. $t_building = mysqli_query($db, "SELECT id,name FROM building Order by name");
  791. while (list($f_building_id, $f_building_name) = mysqli_fetch_array($t_building)) {
  792. print_select_item($f_building_name, $f_building_id, $building_value);
  793. }
  794. print "</select>\n";
  795. }
  796. function print_devmodels_select($db, $devmodel_name, $devmodel_value, $dev_filter = 'device_type<=2')
  797. {
  798. print "<select name=\"$devmodel_name\">\n";
  799. print_select_item(WEB_select_item_all, -1, $devmodel_value);
  800. $t_devmodel = mysqli_query($db, "SELECT M.id,V.name,M.model_name FROM device_models M,vendors V WHERE M.vendor_id = V.id and M.id in (SELECT device_model_id FROM devices WHERE $dev_filter) ORDER BY V.name,M.model_name");
  801. while (list($f_devmodel_id, $f_devmodel_vendor, $f_devmodel_name) = mysqli_fetch_array($t_devmodel)) {
  802. print_select_item($f_devmodel_vendor." ".$f_devmodel_name, $f_devmodel_id, $devmodel_value);
  803. }
  804. print "</select>\n";
  805. }
  806. function print_devtypes_select($db, $devtype_name, $devtype_value, $mode)
  807. {
  808. print "<select name=\"$devtype_name\">\n";
  809. print_select_item(WEB_select_item_all, -1, $devtype_value);
  810. $filter = '';
  811. if (!empty($mode)) {
  812. $filter = "WHERE $mode";
  813. }
  814. $t_devtype = mysqli_query($db, "SELECT id,`name.".HTML_LANG."` FROM device_types $filter ORDER BY `name.".HTML_LANG."`");
  815. while (list($f_devtype_id, $f_devtype_name) = mysqli_fetch_array($t_devtype)) {
  816. print_select_item($f_devtype_name, $f_devtype_id, $devtype_value);
  817. }
  818. print "</select>\n";
  819. }
  820. function print_devtype_select($db, $devtype_name, $devtype_value)
  821. {
  822. print "<select name=\"$devtype_name\">\n";
  823. $t_devtype = mysqli_query($db, "SELECT id,`name.".HTML_LANG."` FROM device_types ORDER BY `name.".HTML_LANG."`");
  824. while (list($f_devtype_id, $f_devtype_name) = mysqli_fetch_array($t_devtype)) {
  825. print_select_item($f_devtype_name, $f_devtype_id, $devtype_value);
  826. }
  827. print "</select>\n";
  828. }
  829. function get_group($db, $group_value)
  830. {
  831. list($group_name) = mysqli_fetch_array(mysqli_query($db, "SELECT group_name FROM Group_list WHERE id=$group_value"));
  832. return $group_name;
  833. }
  834. function get_devtype_name($db, $device_type_id)
  835. {
  836. list($type_name) = mysqli_fetch_array(mysqli_query($db, "SELECT `name.".HTML_LANG."` FROM device_types WHERE id=$device_type_id"));
  837. return $type_name;
  838. }
  839. function get_l3_interfaces($db, $device_id)
  840. {
  841. $wan = '';
  842. $lan = '';
  843. $t_l3int = mysqli_query($db, "SELECT name,interface_type FROM device_l3_interfaces WHERE device_id=$device_id ORDER BY name");
  844. while (list($f_name, $f_type) = mysqli_fetch_array($t_l3int)) {
  845. if ($f_type == 0) {
  846. $lan = $lan . " " . $f_name;
  847. }
  848. if ($f_type == 1) {
  849. $wan = $wan . " " . $f_name;
  850. }
  851. }
  852. $wan = trim($wan);
  853. $lan = trim($lan);
  854. $result = '';
  855. if (!empty($wan)) {
  856. $result .= ' WAN: ' . $wan . '<br>';
  857. }
  858. if (!empty($lan)) {
  859. $result .= ' LAN: ' . $lan;
  860. }
  861. return trim($result);
  862. }
  863. function get_gw_subnets($db, $device_id)
  864. {
  865. $gw_subnets_sql='SELECT gateway_subnets.*,subnets.subnet,subnets.comment FROM gateway_subnets LEFT JOIN subnets ON gateway_subnets.subnet_id = subnets.id WHERE gateway_subnets.device_id='.$device_id.' ORDER BY subnets.subnet ASC';
  866. $gw_subnets=get_records_sql($db,$gw_subnets_sql);
  867. $result='';
  868. foreach ($gw_subnets as $row) {
  869. if (!empty($row)) {
  870. $result.=' '.$row['subnet'].'<br>';
  871. }
  872. }
  873. return trim($result);
  874. }
  875. function print_queue_select($db, $queue_name, $queue_value)
  876. {
  877. print "<select name=\"$queue_name\">\n";
  878. $t_queue = mysqli_query($db, "SELECT id,queue_name FROM Queue_list Order by queue_name");
  879. while (list($f_queue_id, $f_queue_name) = mysqli_fetch_array($t_queue)) {
  880. print_select_item($f_queue_name, $f_queue_id, $queue_value);
  881. }
  882. print "</select>\n";
  883. }
  884. function get_queue($db, $queue_value)
  885. {
  886. list($queue_name) = mysqli_fetch_array(mysqli_query($db, "SELECT queue_name FROM Queue_list WHERE id=$queue_value"));
  887. return $queue_name;
  888. }
  889. function print_qa_l3int_select($qa_name, $qa_value = 0)
  890. {
  891. print "<select name=\"$qa_name\">\n";
  892. print_select_item(WEB_select_item_lan, 0, $qa_value);
  893. print_select_item(WEB_select_item_wan, 1, $qa_value);
  894. print "</select>\n";
  895. }
  896. function print_qa_rule_select($qa_name, $qa_value = 1)
  897. {
  898. print "<select name=\"$qa_name\">\n";
  899. print_select_item('Subnet', 1, $qa_value);
  900. print_select_item('Mac', 2, $qa_value);
  901. print_select_item('Hostname', 3, $qa_value);
  902. print "</select>\n";
  903. }
  904. function get_int($qa_value = 0)
  905. {
  906. if (empty($qa_value)) { $qa_value = 0; } else { $qa_value = (int)$qa_value * 1; }
  907. return $qa_value;
  908. }
  909. function print_qa_select($qa_name, $qa_value = 0)
  910. {
  911. print "<select name=\"$qa_name\">\n";
  912. if (empty($qa_value)) { $qa_value = 0; } else { $qa_value = $qa_value*1; }
  913. print_select_item(WEB_select_item_yes, 1, $qa_value);
  914. print_select_item(WEB_select_item_no, 0, $qa_value);
  915. print "</select>\n";
  916. }
  917. function print_list_select($qa_name, $qa_value, $list)
  918. {
  919. print "<select name=\"$qa_name\">\n";
  920. if (empty($qa_value)) { $qa_value = ''; }
  921. for($i = 0; $i < count($list); ++$i) {
  922. print_select_item($list[$i], $list[$i], $qa_value);
  923. }
  924. print "</select>\n";
  925. }
  926. function print_qa_select_ext($qa_name, $qa_value = 0, $readonly = 1)
  927. {
  928. $state = '';
  929. if ($readonly) {
  930. $state = 'disabled=true';
  931. }
  932. print "<select name=\"$qa_name\">\n";
  933. print_select_item_ext(WEB_select_item_yes, 1, $qa_value, $readonly);
  934. print_select_item_ext(WEB_select_item_no, 0, $qa_value, $readonly);
  935. print "</select>\n";
  936. }
  937. function print_td_yes_no($qa_value = 0)
  938. {
  939. $cl = 'down';
  940. if ($qa_value==1) { $cl='up'; }
  941. print "<td class=\"$cl\">";
  942. if ($qa_value==1) { print WEB_select_item_yes; } else { print WEB_select_item_no; }
  943. print "</td>\n";
  944. }
  945. function print_control_proto_select($qa_name, $qa_value = -1)
  946. {
  947. print "<select name=\"$qa_name\">\n";
  948. print_select_item('Disabled', -1, $qa_value);
  949. print_select_item('Ssh', 0, $qa_value);
  950. print_select_item('Telnet', 1, $qa_value);
  951. // print_select_item('Mikrotik rest api', 2, $qa_value);
  952. print "</select>\n";
  953. }
  954. function print_snmp_select($qa_name, $qa_value = 0)
  955. {
  956. print "<select name=\"$qa_name\">\n";
  957. print_select_item('Disabled', 0, $qa_value);
  958. print_select_item('v1', 1, $qa_value);
  959. print_select_item('v2', 2, $qa_value);
  960. print_select_item('v3', 3, $qa_value);
  961. print "</select>\n";
  962. }
  963. function print_dhcp_select($qa_name, $qa_value = 0)
  964. {
  965. print "<select name=\"$qa_name\">\n";
  966. if (!isset($qa_value) or strlen($qa_value) == 0) {
  967. $qa_value = 'all';
  968. }
  969. print_select_item(WEB_select_item_events, 'all', $qa_value);
  970. print_select_item(WEB_select_item_lease, 'add', $qa_value);
  971. print_select_item(WEB_select_item_lease_refresh, 'old', $qa_value);
  972. print_select_item(WEB_select_item_lease_free, 'del', $qa_value);
  973. print "</select>\n";
  974. }
  975. function print_nagios_handler_select($qa_name)
  976. {
  977. print "<select name=\"$qa_name\">\n";
  978. print_select_simple(WEB_select_item_no, '');
  979. print_select_simple('restart-port', 'restart-port');
  980. print "</select>\n";
  981. }
  982. function print_dhcp_acl_select($qa_name)
  983. {
  984. print "<select name=\"$qa_name\">\n";
  985. print_select_simple(WEB_select_item_no, '');
  986. print_select_simple('hotspot-free', 'hotspot-free');
  987. print "</select>\n";
  988. }
  989. function print_enabled_select($qa_name, $qa_value)
  990. {
  991. print "<select name=\"$qa_name\">\n";
  992. if (!isset($qa_value) or strlen($qa_value) == 0) {
  993. $qa_value = 0;
  994. }
  995. print_select_item(WEB_select_item_every, 0, $qa_value);
  996. print_select_item(WEB_select_item_disabled, 1, $qa_value);
  997. print_select_item(WEB_select_item_enabled, 2, $qa_value);
  998. print "</select>\n";
  999. }
  1000. function print_ip_type_select($qa_name, $qa_value)
  1001. {
  1002. print "<select name=\"$qa_name\">\n";
  1003. if (!isset($qa_value) or strlen($qa_value) == 0) {
  1004. $qa_value = 0;
  1005. }
  1006. print_select_item(WEB_select_item_every, 0, $qa_value);
  1007. print_select_item(WEB_select_item_static, 1, $qa_value);
  1008. print_select_item(WEB_select_item_dhcp, 2, $qa_value);
  1009. print_select_item(WEB_select_item_suspicious, 3, $qa_value);
  1010. print "</select>\n";
  1011. }
  1012. function print_vendor_select($db, $qa_name, $qa_value)
  1013. {
  1014. print "<select name=\"$qa_name\" class=\"js-select-single\">\n";
  1015. $sSQL = "SELECT id,`name` FROM `vendors` order by `name`";
  1016. $vendors = mysqli_query($db, $sSQL);
  1017. print_select_item(WEB_select_item_all, 0, $qa_value);
  1018. while (list($v_id, $v_name) = mysqli_fetch_array($vendors)) {
  1019. print_select_item($v_name, $v_id, $qa_value);
  1020. }
  1021. print "</select>\n";
  1022. }
  1023. function print_vendor_set($db, $qa_name, $qa_value)
  1024. {
  1025. print "<select name=\"$qa_name\" class=\"js-select-single\" style=\"width: 100%\">\n";
  1026. $sSQL = "SELECT id,`name` FROM `vendors` order by `name`";
  1027. $vendors = mysqli_query($db, $sSQL);
  1028. while (list($v_id, $v_name) = mysqli_fetch_array($vendors)) {
  1029. print_select_item($v_name, $v_id, $qa_value);
  1030. }
  1031. print "</select>\n";
  1032. }
  1033. function get_vendor_name($db, $v_id)
  1034. {
  1035. $vendor = get_record_sql($db, "SELECT * FROM `vendors` WHERE id=" . $v_id);
  1036. if (empty($vendor)) {
  1037. return NULL;
  1038. }
  1039. return $vendor['name'];
  1040. }
  1041. function get_qa($qa_value)
  1042. {
  1043. if ($qa_value == 1) {
  1044. return "Да";
  1045. }
  1046. return "Нет";
  1047. }
  1048. function print_action_select($action_name, $action_value)
  1049. {
  1050. print "<select name=\"$action_name\">\n";
  1051. print_select_item(WEB_select_item_allow, 1, $action_value);
  1052. print_select_item(WEB_select_item_forbidden, 0, $action_value);
  1053. print "</select>\n";
  1054. }
  1055. function get_action($action_value)
  1056. {
  1057. if ($action_value == 1) {
  1058. return "Разрешить";
  1059. }
  1060. return "Запретить";
  1061. }
  1062. function print_filter_select($db, $filter_name, $group_id)
  1063. {
  1064. print "<select name=\"$filter_name\" class=\"js-select-single\">\n";
  1065. if (isset($group_id)) {
  1066. $sSQL = "SELECT id,name FROM Filter_list WHERE Filter_list.id not in (Select filter_id FROM Group_filters WHERE group_id=$group_id)";
  1067. } else {
  1068. $sSQL = "SELECT id,name FROM Filter_list Order by name";
  1069. }
  1070. $t_filters = mysqli_query($db, $sSQL);
  1071. while (list($filter_id, $filter_name) = mysqli_fetch_array($t_filters)) {
  1072. print_select_item($filter_name, $filter_id, 0);
  1073. }
  1074. print "</select>\n";
  1075. }
  1076. function get_filter($db, $filter_value)
  1077. {
  1078. list($filter) = mysqli_fetch_array(mysqli_query($db, "SELECT name FROM Filter_list WHERE id=" . $filter_value));
  1079. return $filter;
  1080. }
  1081. function get_login($db, $user_id)
  1082. {
  1083. list($login) = mysqli_fetch_array(mysqli_query($db, "SELECT login FROM User_list WHERE id=$user_id"));
  1084. return $login;
  1085. }
  1086. function get_auth_count($db, $user_id)
  1087. {
  1088. list($count) = mysqli_fetch_array(mysqli_query($db, "SELECT count(id) FROM User_auth WHERE user_id=$user_id and deleted=0"));
  1089. return $count;
  1090. }
  1091. function print_login_select($db, $login_name, $current_login)
  1092. {
  1093. print "<select name=\"$login_name\" class=\"js-select-single\">\n";
  1094. $t_login = mysqli_query($db, "SELECT id,login FROM User_list Order by Login");
  1095. print_select_item('None', 0, $current_login);
  1096. while (list($f_user_id, $f_login) = mysqli_fetch_array($t_login)) {
  1097. print_select_item($f_login, $f_user_id, $current_login);
  1098. }
  1099. print "</select>\n";
  1100. }
  1101. function print_auth_select($db, $login_name, $current_auth)
  1102. {
  1103. print "<select name=\"$login_name\" class=\"js-select-single\">\n";
  1104. $t_login = mysqli_query($db, "SELECT U.login,U.fio,A.ip,A.id FROM User_list as U, User_auth as A WHERE A.user_id=U.id and A.deleted=0 and (A.id not in (select device_ports.auth_id FROM device_ports) or A.id=$current_auth) order by U.login,U.fio,A.ip");
  1105. print_select_item('Empty', 0, $current_auth);
  1106. while (list($f_login, $f_fio, $f_ip, $f_auth_id) = mysqli_fetch_array($t_login)) {
  1107. print_select_item($f_login . "[" . $f_fio . "] - " . $f_ip, $f_auth_id, $current_auth);
  1108. }
  1109. print "</select>\n";
  1110. }
  1111. function print_auth_select_mac($db, $login_name, $current_auth)
  1112. {
  1113. print "<select name=\"$login_name\" class=\"js-select-single\">\n";
  1114. $t_login = mysqli_query($db, "SELECT U.login,U.fio,A.ip,A.mac,A.id FROM User_list as U, User_auth as A WHERE A.user_id=U.id and A.deleted=0 and (A.id not in (select device_ports.auth_id FROM device_ports) or A.id=$current_auth) order by U.login,U.fio,A.ip");
  1115. print_select_item('Empty', 0, $current_auth);
  1116. while (list($f_login, $f_fio, $f_ip, $f_mac, $f_auth_id) = mysqli_fetch_array($t_login)) {
  1117. print_select_item($f_login . "[" . $f_mac . "] - " . $f_ip, $f_auth_id, $current_auth);
  1118. }
  1119. print "</select>\n";
  1120. }
  1121. function compact_port_name($port)
  1122. {
  1123. $result = $port;
  1124. $result = preg_replace('/XGigabitEthernet/', 'X', $result);
  1125. $result = preg_replace('/TenGigabitEthernet/', 'Te', $result);
  1126. $result = preg_replace('/GigabitEthernet/', 'Gi', $result);
  1127. return $result;
  1128. }
  1129. function print_device_port_select($db, $field_name, $device_id, $target_id)
  1130. {
  1131. print "<select name=\"$field_name\" class=\"js-select-single\">\n";
  1132. if (empty($target_id)) {
  1133. $target_id = 0;
  1134. }
  1135. if (empty($device_id)) {
  1136. $device_id = 0;
  1137. }
  1138. $d_sql = "SELECT D.device_name, DP.port, DP.device_id, DP.id, DP.ifName FROM devices AS D, device_ports AS DP WHERE D.deleted=0 and D.id = DP.device_id AND (DP.device_id<>$device_id or DP.id=$target_id) and (DP.id not in (select target_port_id FROM device_ports WHERE target_port_id>0 and target_port_id<>$target_id)) ORDER BY D.device_name,DP.port";
  1139. $t_device = mysqli_query($db, $d_sql);
  1140. print_select_item('Empty', 0, $target_id);
  1141. while (list($f_name, $f_port, $f_device_id, $f_target_id, $f_ifname) = mysqli_fetch_array($t_device)) {
  1142. if (empty($f_ifname)) {
  1143. $f_ifname = $f_port;
  1144. }
  1145. print_select_item($f_name . "[" . $f_port."] - " . compact_port_name($f_ifname), $f_target_id, $target_id);
  1146. }
  1147. print "</select>\n";
  1148. }
  1149. function print_device_select($db, $field_name, $device_id)
  1150. {
  1151. print "<select name=\"$field_name\" class=\"js-select-single\" >\n";
  1152. $d_sql = "SELECT D.device_name, D.id FROM devices AS D Where D.deleted=0 order by D.device_name ASC";
  1153. $t_device = mysqli_query($db, $d_sql);
  1154. print_select_item(WEB_select_item_every, 0, $device_id);
  1155. while (list($f_name, $f_device_id) = mysqli_fetch_array($t_device)) {
  1156. print_select_item($f_name, $f_device_id, $device_id);
  1157. }
  1158. print "</select>\n";
  1159. }
  1160. function print_netdevice_select($db, $field_name, $device_id)
  1161. {
  1162. print "<select name=\"$field_name\" class=\"js-select-single\" >\n";
  1163. $d_sql = "SELECT D.device_name, D.id FROM devices AS D Where D.deleted=0 and D.device_type<=2 order by D.device_name ASC";
  1164. $t_device = mysqli_query($db, $d_sql);
  1165. print_select_item(WEB_select_item_every, 0, $device_id);
  1166. while (list($f_name, $f_device_id) = mysqli_fetch_array($t_device)) {
  1167. print_select_item($f_name, $f_device_id, $device_id);
  1168. }
  1169. print "</select>\n";
  1170. }
  1171. function print_vlan_select($db, $field_name, $vlan)
  1172. {
  1173. print "<select name=\"$field_name\" class=\"js-select-single\">\n";
  1174. $d_sql = "SELECT DISTINCT vlan FROM device_ports ORDER BY vlan DESC";
  1175. $v_device = mysqli_query($db, $d_sql);
  1176. if (!isset($vlan) or empty($vlan)) {
  1177. $vlan = 1;
  1178. };
  1179. print_select_item('1', 1, $vlan);
  1180. while (list($f_vlan) = mysqli_fetch_array($v_device)) {
  1181. if ($f_vlan === '1') {
  1182. continue;
  1183. }
  1184. print_select_item($f_vlan, $f_vlan, $vlan);
  1185. }
  1186. print "</select>\n";
  1187. }
  1188. function print_device_select_ip($db, $field_name, $device_ip)
  1189. {
  1190. print "<select name=\"$field_name\" class=\"js-select-single\" >\n";
  1191. $d_sql = "SELECT D.device_name, D.ip FROM devices AS D Where D.deleted=0 order by D.device_name ASC";
  1192. $t_device = mysqli_query($db, $d_sql);
  1193. print_select_item(WEB_select_item_every, '', $device_ip);
  1194. while (list($f_name, $f_device_ip) = mysqli_fetch_array($t_device)) {
  1195. print_select_item($f_name, $f_device_ip, $device_ip);
  1196. }
  1197. print "</select>\n";
  1198. }
  1199. function print_syslog_device_select($db, $field_name, $syslog_filter, $device_ip)
  1200. {
  1201. print "<select name=\"$field_name\" class=\"js-select-single\" >\n";
  1202. $d_sql = "SELECT R.ip, D.device_name FROM (SELECT DISTINCT ip FROM remote_syslog WHERE $syslog_filter) AS R LEFT JOIN (SELECT ip, device_name FROM devices WHERE deleted=0) AS D ON R.ip=D.ip ORDER BY R.ip ASC";
  1203. $t_device = mysqli_query($db, $d_sql);
  1204. print_select_item(WEB_select_item_every, '', $device_ip);
  1205. while (list($f_ip, $f_name) = mysqli_fetch_array($t_device)) {
  1206. if (!isset($f_name) or empty($f_name)) {
  1207. $f_name = $f_ip;
  1208. }
  1209. print_select_item($f_name, $f_ip, $device_ip);
  1210. }
  1211. print "</select>\n";
  1212. }
  1213. function print_gateway_select($db, $field_name, $device_id)
  1214. {
  1215. print "<select name=\"$field_name\" >\n";
  1216. $d_sql = "SELECT D.device_name, D.id FROM devices AS D Where D.deleted=0 and D.device_type=2 order by D.device_name ASC";
  1217. $t_device = mysqli_query($db, $d_sql);
  1218. print_select_item(WEB_select_item_every, 0, $device_id);
  1219. while (list($f_name, $f_device_id) = mysqli_fetch_array($t_device)) {
  1220. print_select_item($f_name, $f_device_id, $device_id);
  1221. }
  1222. print "</select>\n";
  1223. }
  1224. function get_gateways($db)
  1225. {
  1226. $d_sql = "SELECT D.device_name, D.id FROM devices AS D Where D.deleted=0 and D.device_type=2 order by D.device_name ASC";
  1227. $t_device = mysqli_query($db, $d_sql);
  1228. unset($result);
  1229. while (list($f_name, $f_device_id) = mysqli_fetch_array($t_device)) {
  1230. $result[$f_device_id] = $f_name;
  1231. }
  1232. return $result;
  1233. }
  1234. function print_device_port($db, $target_id)
  1235. {
  1236. $d_sql = "SELECT D.device_name, DP.port, DP.device_id FROM devices AS D, device_ports AS DP WHERE D.id = DP.device_id AND DP.id=$target_id and D.deleted=0";
  1237. $t_device = mysqli_query($db, $d_sql);
  1238. while (list($f_name, $f_port, $f_device_id) = mysqli_fetch_array($t_device)) {
  1239. print "<a href=\"/admin/devices/switchport.php?id=$f_device_id\">" . $f_name . "[" . $f_port . "]</a>\n";
  1240. }
  1241. }
  1242. function get_device_ips($db, $device_id)
  1243. {
  1244. $switch = get_record($db, 'devices', 'id=' . $device_id);
  1245. $index = 0;
  1246. if (!empty($switch['user_id'])) {
  1247. $auth_ips = get_records($db, 'User_auth', 'deleted=0 and user_id=' . $switch['user_id']);
  1248. foreach ($auth_ips as $key => $value) {
  1249. if (isset($value['ip'])) {
  1250. $result[$index] = $value['ip'];
  1251. $index++;
  1252. }
  1253. }
  1254. } else {
  1255. if (isset($switch['ip'])) {
  1256. $result[$index] = $switch['ip'];
  1257. $index++;
  1258. }
  1259. }
  1260. return $result;
  1261. }
  1262. function get_device_id($db, $device_name)
  1263. {
  1264. $d_sql = "SELECT id FROM devices WHERE device_name='$device_name' and deleted=0";
  1265. $dev = get_record_sql($db, $d_sql);
  1266. if (empty($dev)) {
  1267. return NULL;
  1268. }
  1269. return $dev["id"];
  1270. }
  1271. function get_device_name($db, $device_id)
  1272. {
  1273. $d_sql = "SELECT device_name FROM devices WHERE id='$device_id'";
  1274. $dev = get_record_sql($db, $d_sql);
  1275. if (empty($dev)) {
  1276. return NULL;
  1277. }
  1278. return $dev["device_name"];
  1279. }
  1280. function get_auth_by_ip($db, $ip)
  1281. {
  1282. $d_sql = "SELECT id FROM User_auth WHERE ip='$ip' and deleted=0";
  1283. $auth = get_record_sql($db, $d_sql);
  1284. if (empty($auth)) {
  1285. return NULL;
  1286. }
  1287. return $auth["id"];
  1288. }
  1289. function get_user_by_ip($db, $ip)
  1290. {
  1291. $d_sql = "SELECT user_id FROM User_auth WHERE ip='$ip' and deleted=0";
  1292. $auth = get_record_sql($db, $d_sql);
  1293. if (empty($auth)) {
  1294. return NULL;
  1295. }
  1296. return $auth["user_id"];
  1297. }
  1298. function get_device_by_auth($db, $id)
  1299. {
  1300. $d_sql = "SELECT id FROM devices WHERE user_id=$id and deleted=0";
  1301. $f_dev = get_record_sql($db, $d_sql);
  1302. if (empty($f_dev)) {
  1303. return NULL;
  1304. }
  1305. return $f_dev['id'];
  1306. }
  1307. function print_auth_port($db, $port_id, $new_window = FALSE)
  1308. {
  1309. $d_sql = "SELECT A.ip, A.ip_int, A.mac, A.id, A.dns_name, A.user_id FROM User_auth as A, connections as C WHERE C.port_id=$port_id and A.id=C.auth_id and A.deleted=0 order by A.ip_int";
  1310. $t_auth = mysqli_query($db, $d_sql);
  1311. while (list($f_ip, $f_int, $f_mac, $f_auth_id, $f_dns, $f_user_id) = mysqli_fetch_array($t_auth)) {
  1312. $name = $f_ip;
  1313. if (!empty($f_dns)) { $name = $f_dns; }
  1314. $title=get_login($db,$f_user_id)." =>".$f_ip."[".$f_mac."]";
  1315. if (!empty($f_dns)) { $title.=" | ".$f_dns; }
  1316. if ($new_window) {
  1317. print "<a href=\"\" title=\"" . $title . "\" onclick=\"".open_window_url("/admin/users/editauth.php?id=".$f_auth_id)." return false;\">" . $name . " [" . $f_ip . "]</a><br>";
  1318. } else {
  1319. print "<a href=/admin/users/editauth.php?id=".$f_auth_id." title=\"" . $title . "\" >" . $name . " [" . $f_ip . "]</a><br>";
  1320. }
  1321. }
  1322. }
  1323. function get_port_comment($db, $port_id, $port_comment = '')
  1324. {
  1325. $d_sql = "SELECT A.ip_int, A.comments FROM User_auth as A, connections as C WHERE C.port_id=$port_id and A.id=C.auth_id and A.deleted=0 order by A.ip_int";
  1326. $t_auth = mysqli_query($db, $d_sql);
  1327. $comment_found = 0;
  1328. $result = '';
  1329. while (list($f_int, $f_comment) = mysqli_fetch_array($t_auth)) {
  1330. if (!empty($f_comment)) { $comment_found = 1; } else { $f_comment = ''; }
  1331. $result .=$f_comment.'<br>';
  1332. }
  1333. if (!$comment_found) { return $port_comment; }
  1334. if (!empty($port_comment)) { $result .='('.$port_comment.')'; }
  1335. return $result;
  1336. }
  1337. function print_auth_simple($db, $auth_id)
  1338. {
  1339. $auth = get_record($db, "User_auth", "id=$auth_id");
  1340. $name = $auth['dns_name'];
  1341. if (empty($name)) {
  1342. $name = $auth['comments'];
  1343. }
  1344. if (empty($name)) {
  1345. $name = $auth['ip'];
  1346. }
  1347. print "<a href=\"/admin/users/editauth.php?id=$auth_id\">" . $name . "</a><br>";
  1348. }
  1349. function print_auth($db, $auth_id)
  1350. {
  1351. $auth = get_record($db, "User_auth", "id=$auth_id");
  1352. $name = $auth['dns_name'];
  1353. if (empty($name)) {
  1354. $name = $auth['comments'];
  1355. } else {
  1356. $name .= " (" . $auth['comments'] . ")";
  1357. }
  1358. if (empty($name)) {
  1359. $name = $auth['ip'];
  1360. } else {
  1361. $name .= " [" . $auth['ip'] . "]";
  1362. }
  1363. print "<a href=\"/admin/users/editauth.php?id=$auth_id\">" . $name . "</a><br>";
  1364. }
  1365. function print_auth_detail($db, $auth_id)
  1366. {
  1367. $auth = get_record($db, "User_auth", "id=$auth_id");
  1368. $name = $auth['dns_name'];
  1369. if (empty($name)) {
  1370. $name = $auth['comments'];
  1371. } else {
  1372. $name .= " (" . $auth['comments'] . ")";
  1373. }
  1374. if (empty($name)) {
  1375. $name = $auth['ip'];
  1376. } else {
  1377. $name .= " [" . $auth['ip'] . "]";
  1378. }
  1379. $name .= " last: [" . $auth['last_found'] . "] ";
  1380. if ($auth['deleted'] == 1) {
  1381. $name .= " <font color='red'>DELETED!!!</font>";
  1382. }
  1383. print "<a href=\"/admin/users/editauth.php?id=$auth_id\">" . $name . "</a><br>";
  1384. }
  1385. function get_auth_port_count($db, $port_id)
  1386. {
  1387. $d_sql = "SELECT count(A.id) FROM User_auth as A, connections as C WHERE C.port_id=$port_id and A.id=C.auth_id and A.deleted=0";
  1388. $t_device = mysqli_query($db, $d_sql);
  1389. list($f_count) = mysqli_fetch_array($t_device);
  1390. if (!isset($f_count)) {
  1391. $f_count = 0;
  1392. }
  1393. return $f_count;
  1394. }
  1395. function get_connection($db, $auth_id)
  1396. {
  1397. $d_sql = "SELECT D.device_name, DP.port FROM devices AS D, device_ports AS DP, connections AS C WHERE D.deleted=0 and D.id = DP.device_id AND DP.id = C.port_id AND C.auth_id=$auth_id";
  1398. $t_device = mysqli_query($db, $d_sql);
  1399. list($f_name, $f_port) = mysqli_fetch_array($t_device);
  1400. if (isset($f_name)) {
  1401. $result = expand_device_name($db, $f_name) . "[" . $f_port . "]";
  1402. } else {
  1403. $result = '';
  1404. }
  1405. return $result;
  1406. }
  1407. function get_connection_string($db, $auth_id)
  1408. {
  1409. $d_sql = "SELECT D.device_name, DP.port FROM devices AS D, device_ports AS DP, connections AS C WHERE D.deleted=0 and D.id = DP.device_id AND DP.id = C.port_id AND C.auth_id=$auth_id";
  1410. $t_device = mysqli_query($db, $d_sql);
  1411. list($f_name, $f_port) = mysqli_fetch_array($t_device);
  1412. if (isset($f_name)) {
  1413. $result = $f_name . "[" . $f_port . "]";
  1414. } else {
  1415. $result = '';
  1416. }
  1417. return $result;
  1418. }
  1419. function get_port($db, $port_id)
  1420. {
  1421. $d_sql = "SELECT D.device_name, DP.port FROM devices AS D, device_ports AS DP WHERE D.deleted=0 and D.id = DP.device_id AND DP.id = $port_id";
  1422. $t_device = mysqli_query($db, $d_sql);
  1423. list($f_name, $f_port) = mysqli_fetch_array($t_device);
  1424. if (isset($f_name)) {
  1425. $result = expand_device_name($db, $f_name) . "[" . $f_port . "]";
  1426. } else {
  1427. $result = '';
  1428. }
  1429. return $result;
  1430. }
  1431. function print_option_select($db, $option_name)
  1432. {
  1433. print "<select name=\"$option_name\">\n";
  1434. $t_option = mysqli_query($db, "SELECT id,option_name FROM config_options WHERE uniq=0 order by option_name");
  1435. while (list($f_id, $f_name) = mysqli_fetch_array($t_option)) {
  1436. print "<option value=$f_id>$f_name</option>";
  1437. }
  1438. $t_option = mysqli_query($db, "SELECT id,option_name FROM config_options WHERE uniq=1 and id not in (select option_id FROM config) order by option_name");
  1439. while (list($f_id, $f_name) = mysqli_fetch_array($t_option)) {
  1440. print "<option value=$f_id>$f_name</option>";
  1441. }
  1442. print "</select>\n";
  1443. }
  1444. function run_sql($db, $query)
  1445. {
  1446. if (preg_match('/^\s*(UPDATE|DELETE)/i', $query)) {
  1447. unset($matches);
  1448. preg_match('/FROM\s+(.*)\s+/i', $query, $matches);
  1449. if (!empty($matches[1])) {
  1450. if (!allow_update($matches[1],'del')) {
  1451. LOG_DEBUG($db, "Access denied: $query ");
  1452. return;
  1453. }
  1454. }
  1455. unset($matches);
  1456. preg_match('/INSERT\s+INTO\s+(.*)\s+/i', $query, $matches);
  1457. if (!empty($matches[1])) {
  1458. if (!allow_update($matches[1],'add')) {
  1459. LOG_DEBUG($db, "Access denied: $query ");
  1460. return;
  1461. }
  1462. }
  1463. unset($matches);
  1464. preg_match('/UPDATE\s+(.*)\s+/i', $query, $matches);
  1465. if (!empty($matches[1])) {
  1466. if (!allow_update($matches[1],'update')) {
  1467. LOG_DEBUG($db, "Access denied: $query ");
  1468. return;
  1469. }
  1470. }
  1471. unset($matches);
  1472. }
  1473. $sql_result = mysqli_query($db, $query);
  1474. if (!$sql_result) {
  1475. LOG_ERROR($db, "At simple SQL: $query :" . mysqli_error($db));
  1476. return;
  1477. }
  1478. return $sql_result;
  1479. }
  1480. function get_count_records($db, $table, $filter)
  1481. {
  1482. if (!empty($filter)) {
  1483. $filter = 'where ' . $filter;
  1484. }
  1485. $t_count = mysqli_query($db, "SELECT count(*) FROM $table $filter");
  1486. list($count) = mysqli_fetch_array($t_count);
  1487. if (!isset($count)) {
  1488. $count = 0;
  1489. }
  1490. return $count;
  1491. }
  1492. function get_id_record($db, $table, $filter)
  1493. {
  1494. if (isset($filter)) {
  1495. $filter = 'WHERE ' . $filter;
  1496. }
  1497. $t_record = mysqli_query($db, "SELECT id FROM $table $filter limit 1");
  1498. list($id) = mysqli_fetch_array($t_record);
  1499. return $id;
  1500. }
  1501. function set_changed($db, $id)
  1502. {
  1503. $auth['changed'] = 1;
  1504. update_record($db, "User_auth", "id=" . $id, $auth);
  1505. }
  1506. function ResolveIP($db, $ip_int)
  1507. {
  1508. $ip_name = "-";
  1509. if (empty($ip_int)) {
  1510. return $ip_name;
  1511. }
  1512. $dns_cache = get_record_sql($db, "SELECT * FROM dns_cache WHERE ip=$ip_int");
  1513. if (empty($dns_cache) or empty($dns_cache['dns'])) {
  1514. $ip_name = gethostbyaddr(long2ip($ip_int));
  1515. if (empty($ip_name) or $ip_name == long2ip($ip_int)) {
  1516. $ip_name = "-";
  1517. }
  1518. run_sql($db, "INSERT INTO dns_cache(dns,ip) VALUES('" . $ip_name . "'," . $ip_int . ")");
  1519. } else {
  1520. $ip_name = $dns_cache['dns'];
  1521. }
  1522. return $ip_name;
  1523. }
  1524. function clean_dns_cache($db)
  1525. {
  1526. $date = time();
  1527. $date = $date - 86400;
  1528. $date_clean = DateTimeImmutable::createFromFormat('U', $date);
  1529. $clean_date = $date_clean->format('Y-m-d H:i:s');
  1530. run_sql($db, "DELETE FROM dns_cache WHERE `timestamp`<='" . $clean_date . "'");
  1531. }
  1532. function clean_unreferensed_rules($db)
  1533. {
  1534. run_sql($db, "DELETE FROM `auth_rules` WHERE user_id NOT IN (SELECT id FROM User_list)");
  1535. }
  1536. function FormatDateStr($format = 'Y-m-d H:i:s', $date_str)
  1537. {
  1538. $date1 = GetDateTimeFromString($date_str);
  1539. $result = $date1->format($format);
  1540. return $result;
  1541. }
  1542. function GetDateTimeFromString($date_str)
  1543. {
  1544. if (!is_a($date_str, 'DateTime')) {
  1545. $t_date_str = urldecode($date_str);
  1546. $t_date_str = preg_replace('/(\'|\")/', '', $t_date_str);
  1547. $t_date_str = preg_replace('/T/', ' ', $t_date_str);
  1548. $date1 = DateTime::createFromFormat('Y-m-d H:i:s', $t_date_str);
  1549. if (!$date1) {
  1550. $date1 = DateTime::createFromFormat('Y.m.d H:i:s', $t_date_str);
  1551. }
  1552. if (!$date1) {
  1553. $date1 = DateTime::createFromFormat('Y/m/d H:i:s', $t_date_str);
  1554. }
  1555. if (!$date1) {
  1556. $date1 = DateTime::createFromFormat('Y-m-d H:i', $t_date_str);
  1557. }
  1558. if (!$date1) {
  1559. $date1 = DateTime::createFromFormat('Y.m.d H:i', $t_date_str);
  1560. }
  1561. if (!$date1) {
  1562. $date1 = DateTime::createFromFormat('Y/m/d H:i', $t_date_str);
  1563. }
  1564. if (!$date1) {
  1565. $date1 = DateTime::createFromFormat('Y-m-d|', $t_date_str);
  1566. }
  1567. if (!$date1) {
  1568. $date1 = DateTime::createFromFormat('Y.m.d|', $t_date_str);
  1569. }
  1570. if (!$date1) {
  1571. $date1 = DateTime::createFromFormat('Y/m/d|', $t_date_str);
  1572. }
  1573. if (!$date1) {
  1574. $date1 = new DateTime;
  1575. $date1->setTime(0, 0, 0, 1);
  1576. }
  1577. } else {
  1578. return $date_str;
  1579. }
  1580. return $date1;
  1581. }
  1582. function GetNowTimeString()
  1583. {
  1584. $now = new DateTimeImmutable('now');
  1585. $result = $now->format('Y-m-d H:i:s');
  1586. return $result;
  1587. }
  1588. function GetNowDayString()
  1589. {
  1590. $now = new DateTimeImmutable('now');
  1591. $result = $now->format('Y-m-d');
  1592. return $result;
  1593. }
  1594. function get_ip_subnet($db, $ip)
  1595. {
  1596. if (empty($ip)) {
  1597. return;
  1598. }
  1599. $ip_aton = ip2long($ip);
  1600. $user_subnet = get_record_sql($db, "SELECT * FROM `subnets` WHERE hotspot=1 or office=1 and ( $ip_aton >= ip_int_start and $ip_aton <= ip_int_stop)");
  1601. if (empty($user_subnet)) {
  1602. return;
  1603. }
  1604. return $user_subnet;
  1605. }
  1606. function find_mac_in_subnet($db, $ip, $mac)
  1607. {
  1608. if (empty($ip)) {
  1609. return;
  1610. }
  1611. if (empty($mac)) {
  1612. return;
  1613. }
  1614. $ip_subnet = get_ip_subnet($db, $ip);
  1615. if (empty($ip_subnet)) {
  1616. return;
  1617. }
  1618. $t_auth = get_records_sql($db, "SELECT id,mac,user_id FROM User_auth WHERE ip_int>=" . $ip_subnet['ip_int_start'] . " and ip_int<=" . $ip_subnet['ip_int_stop'] . " and mac='" . $mac . "' and deleted=0 ORDER BY id");
  1619. $auth_count = 0;
  1620. $result['count'] = 0;
  1621. $result['users_id'] = [];
  1622. foreach ($t_auth as $row) {
  1623. if (!empty($row['id'])) {
  1624. $auth_count++;
  1625. $result['count'] = $auth_count;
  1626. $result[$auth_count] = $row['id'];
  1627. array_push($result['users_id'], $row['user_id']);
  1628. }
  1629. }
  1630. return $result;
  1631. }
  1632. function apply_auth_rule($db, $auth_record, $user_id)
  1633. {
  1634. if (empty($auth_record)) { return; }
  1635. if (empty($user_id)) { return $auth_record; }
  1636. $user_rec = get_record($db, 'User_list', "id=" . $user_id);
  1637. if (empty($user_rec)) { return $auth_record; }
  1638. //set filter and status by user
  1639. $auth_record['ou_id'] = $user_rec['ou_id'];
  1640. $auth_record['user_id'] = $user_rec['id'];
  1641. $auth_record['filter_group_id'] = $user_rec['filter_group_id'];
  1642. $auth_record['queue_id'] = $user_rec['queue_id'];
  1643. $auth_record['enabled'] = $user_rec['enabled'];
  1644. $auth_record['changed'] = 1;
  1645. //maybe fill comments?
  1646. if (!empty($user_rec['fio']) and empty($auth_record['comments'])) { $auth_record['comments'] = $user_rec['fio']; }
  1647. return $auth_record;
  1648. }
  1649. function fix_auth_rules($db)
  1650. {
  1651. //cleanup hotspot subnet rules
  1652. delete_record($db, "auth_rules", "ou_id=" . get_const('default_user_ou_id'));
  1653. delete_record($db, "auth_rules", "ou_id=" . get_const('default_hotspot_ou_id'));
  1654. $t_hotspot = get_records_sql($db, "SELECT * FROM subnets WHERE hotspot=1");
  1655. if (!empty($t_hotspot)) {
  1656. foreach ($t_hotspot as $row) {
  1657. delete_record($db, "auth_rules", "rule='" . $row['subnet'] . "'");
  1658. }
  1659. }
  1660. }
  1661. #---------------------------------------------------------------------------------------------------------------
  1662. function new_user($db, $user_info)
  1663. {
  1664. if (!empty($user_info['mac'])) {
  1665. $user['login'] = mac_dotted($user_info['mac']);
  1666. } else {
  1667. $user['login'] = $user_info['ip'];
  1668. }
  1669. if (!empty($user_info['dhcp_hostname'])) {
  1670. $user['fio'] = $user_info['ip'] . '[' . $user_info['dhcp_hostname'] . ']';
  1671. } else {
  1672. $user['fio'] = $user_info['ip'];
  1673. }
  1674. $login_count = get_count_records($db, "User_list", "(login LIKE '" . $user['login'] . "(%)') OR (login='" . $user['login'] . "')");
  1675. if (!empty($login_count) and $login_count > 0) {
  1676. $login_count++;
  1677. $user['login'] = $user['login'] . "(" . $login_count . ")";
  1678. }
  1679. $user['ou_id'] = $user_info['ou_id'];
  1680. $ou_info = get_record_sql($db, "SELECT * FROM OU WHERE id=" . $user_info['ou_id']);
  1681. if (!empty($ou_info)) {
  1682. $user['enabled'] = $ou_info['enabled'];
  1683. if (empty($user['enabled'])) {
  1684. $user['enabled'] = 0;
  1685. }
  1686. $user['queue_id'] = $ou_info['queue_id'];
  1687. if (empty($user['queue_id'])) {
  1688. $user['queue_id'] = 0;
  1689. }
  1690. $user['filter_group_id'] = $ou_info['filter_group_id'];
  1691. if (empty($user['filter_group_id'])) {
  1692. $user['filter_group_id'] = 0;
  1693. }
  1694. }
  1695. $result = insert_record($db, "User_list", $user);
  1696. $auto_mac_rule = get_option($db, 64);
  1697. if (!empty($result) and $auto_mac_rule and $user_info['mac']) {
  1698. $auth_rule['user_id'] = $result;
  1699. $auth_rule['type'] = 2;
  1700. $auth_rule['rule'] = mac_dotted($user_info['mac']);
  1701. insert_record($db, "auth_rules", $auth_rule);
  1702. }
  1703. return $result;
  1704. }
  1705. function new_auth($db, $ip, $mac, $user_id)
  1706. {
  1707. $ip_aton = ip2long($ip);
  1708. $msg = '';
  1709. if (!empty($mac)) {
  1710. $auth_record = get_record_sql($db, "SELECT * FROM User_auth WHERE ip_int=$ip_aton AND mac='" . mac_dotted($mac) . "' AND deleted=0");
  1711. if (!empty($auth_record)) {
  1712. LOG_WARNING($db, "Pair ip-mac already exists! Skip creating $ip [$mac] auth_id: " . $auth_record["id"]);
  1713. return $auth_record['id'];
  1714. }
  1715. }
  1716. // save traffic detailization
  1717. $save_traf = get_option($db, 23);
  1718. $resurrection_id = NULL;
  1719. // seek old auth with same ip and mac
  1720. $resurrection_id = get_id_record($db, 'User_auth', " deleted=1 AND ip_int=" . $ip_aton . " AND mac='" . $mac . "'");
  1721. if (!empty($resurrection_id)) {
  1722. $msg .= "Recovered auth_id: $resurrection_id with ip: $ip and mac: $mac ";
  1723. $auth['user_id'] = $user_id;
  1724. $auth['deleted'] = 0;
  1725. $auth['save_traf'] = $save_traf * 1;
  1726. update_record($db, "User_auth", "id=$resurrection_id", $auth);
  1727. } else {
  1728. // not found ->create new record
  1729. $msg .= "Create new ip record \r\nip: $ip\r\nmac: $mac\r\n";
  1730. $auth['deleted'] = 0;
  1731. $auth['user_id'] = $user_id;
  1732. $auth['ip'] = $ip;
  1733. $auth['ip_int'] = $ip_aton;
  1734. $auth['mac'] = $mac;
  1735. $auth['save_traf'] = $save_traf * 1;
  1736. $resurrection_id = insert_record($db, "User_auth", $auth);
  1737. }
  1738. //check rules, update filter and state for new record
  1739. if (!empty($resurrection_id)) {
  1740. $auth=apply_auth_rule($db, $auth, $user_id);
  1741. update_record($db, "User_auth", "id=$resurrection_id", $auth);
  1742. if (!is_hotspot($db, $ip) and !empty($msg)) {
  1743. LOG_WARNING($db, $msg);
  1744. }
  1745. if (is_hotspot($db, $ip) and !empty($msg)) {
  1746. LOG_INFO($db, $msg);
  1747. }
  1748. }
  1749. return $resurrection_id;
  1750. }
  1751. function resurrection_auth($db, $ip_record)
  1752. {
  1753. $ip = $ip_record['ip'];
  1754. $mac = $ip_record['mac'];
  1755. $action = $ip_record['type'];
  1756. $dhcp_hostname = $ip_record['hostname'];
  1757. $hotspot_found = $ip_record['hotspot'];
  1758. $ip_aton = ip2long($ip);
  1759. $auth_record = get_record_sql($db, "SELECT * FROM User_auth WHERE ip_int=$ip_aton AND mac='" . $mac . "' AND deleted=0");
  1760. if (!empty($auth_record)) {
  1761. $user_info = get_record_sql($db, "SELECT * FROM User_list WHERE id=" . $auth_record['user_id']);
  1762. LOG_DEBUG($db, "external dhcp user " . $user_info['login'] . " [" . $ip . "] auth_id: " . $auth_record['id']);
  1763. if (isset($dhcp_hostname) and !empty($dhcp_hostname)) {
  1764. $auth['dhcp_hostname'] = $dhcp_hostname;
  1765. }
  1766. $auth['dhcp_action'] = $action;
  1767. $auth['dhcp_time'] = GetNowTimeString();
  1768. if ($action === 'add') {
  1769. $auth['last_found'] = GetNowTimeString();
  1770. }
  1771. update_record($db, "User_auth", "id=" . $auth_record['id'], $auth);
  1772. return $auth_record['id'];
  1773. }
  1774. $ip_subnet = get_ip_subnet($db, $ip);
  1775. if ($ip_subnet['static']) {
  1776. LOG_WARNING($db, "Unknown pair ip+mac in static subnet! ip: $ip mac: [" . mac_dotted($mac) . "]. Skip");
  1777. return;
  1778. }
  1779. $msg = '';
  1780. // search changed mac
  1781. $auth_record = get_record_sql($db, "SELECT * FROM User_auth WHERE ip_int=$ip_aton AND deleted=0");
  1782. if (!empty($auth_record)) {
  1783. if (empty($auth_record['mac'])) {
  1784. $auth['mac'] = mac_dotted($mac);
  1785. $auth['dhcp_action'] = $action;
  1786. $auth['dhcp_time'] = GetNowTimeString();
  1787. if (!empty($dhcp_hostname)) {
  1788. $auth['dhcp_hostname'] = $dhcp_hostname;
  1789. }
  1790. if ($action === 'add') {
  1791. $auth['last_found'] = GetNowTimeString();
  1792. }
  1793. LOG_INFO($db, "for ip: $ip mac not found! Use empty record...");
  1794. update_record($db, "User_auth", "id=" . $auth_record['id'], $auth);
  1795. return $auth_record['id'];
  1796. } else {
  1797. if (!$hotspot_found) {
  1798. LOG_WARNING($db, "for ip: $ip mac change detected! Old mac: [" . $auth_record['mac'] . "] New mac: [" . mac_dotted($mac) . "]. Disable old auth_id: " . $auth_record['id']);
  1799. }
  1800. run_sql($db, "UPDATE User_auth SET changed=1, deleted=1 WHERE id=" . $auth_record['id']);
  1801. }
  1802. }
  1803. // default id
  1804. $new_user_info = get_new_user_id($db, $ip, $mac, $dhcp_hostname);
  1805. if (!empty($new_user_info['user_id'])) {
  1806. $new_user_id = $new_user_info['user_id'];
  1807. }
  1808. if (empty($new_user_id)) {
  1809. $new_user_id = new_user($db, $new_user_info);
  1810. }
  1811. $resurrection_id = NULL;
  1812. $save_traf = get_option($db, 23);
  1813. $auth_record = get_record_sql($db, "SELECT * FROM User_auth WHERE ip_int=" . $ip_aton . " and mac='" . $mac . "'");
  1814. // seek old auth with same ip and mac
  1815. if (!empty($auth_record)) {
  1816. // found ->Resurrection old record
  1817. $resurrection_id = $auth_record['id'];
  1818. $msg .= "Recovered auth_id: $resurrection_id with ip: $ip and mac: $mac ";
  1819. $auth['dhcp_action'] = $action;
  1820. $auth['user_id'] = $new_user_id;
  1821. $auth['deleted'] = 0;
  1822. $auth['dhcp_time'] = GetNowTimeString();
  1823. $auth['save_traf'] = $save_traf * 1;
  1824. if (!empty($dhcp_hostname)) {
  1825. $auth['dhcp_hostname'] = $dhcp_hostname;
  1826. }
  1827. if ($action === 'add') {
  1828. $auth['last_found'] = GetNowTimeString();
  1829. }
  1830. update_record($db, "User_auth", "id=$resurrection_id", $auth);
  1831. } else {
  1832. // not found ->create new record
  1833. $msg .= "Создаём новый ip-адрес \r\nip: $ip\r\nmac: $mac\r\n";
  1834. $auth['deleted'] = 0;
  1835. $auth['user_id'] = $new_user_id;
  1836. $auth['ip'] = $ip;
  1837. $auth['ip_int'] = $ip_aton;
  1838. $auth['mac'] = $mac;
  1839. $auth['dhcp_action'] = $action;
  1840. $auth['dhcp_time'] = GetNowTimeString();
  1841. $auth['save_traf'] = $save_traf * 1;
  1842. if (!empty($dhcp_hostname)) {
  1843. $auth['dhcp_hostname'] = $dhcp_hostname;
  1844. }
  1845. if ($action == 'add') {
  1846. $auth['last_found'] = GetNowTimeString();
  1847. }
  1848. $resurrection_id = insert_record($db, "User_auth", $auth);
  1849. }
  1850. //check rules, update filter and state for new record
  1851. if (!empty($resurrection_id)) {
  1852. $auth = apply_auth_rule($db, $auth, $new_user_id);
  1853. update_record($db, "User_auth", "id=$resurrection_id", $auth);
  1854. $msg .= "filter: " . $auth['filter_group_id'] . "\r\n queue_id: " . $auth['queue_id'] . "\r\n enabled: " . $auth['enabled'] . "\r\nid: $resurrection_id";
  1855. if (!$hotspot_found and !empty($msg)) {
  1856. LOG_WARNING($db, $msg);
  1857. }
  1858. if ($hotspot_found and !empty($msg)) {
  1859. LOG_INFO($db, $msg);
  1860. }
  1861. }
  1862. return $resurrection_id;
  1863. }
  1864. function get_auth($db, $current_auth)
  1865. {
  1866. if (!isset($current_auth)) {
  1867. return;
  1868. }
  1869. if ($current_auth == 0) {
  1870. return;
  1871. }
  1872. $t_login = mysqli_query($db, "SELECT U.login,A.ip FROM User_list as U, User_auth as A WHERE A.user_id=U.id and A.id=$current_auth");
  1873. list($f_login, $f_ip) = mysqli_fetch_array($t_login);
  1874. $result = $f_login . "[" . $f_ip . "]";
  1875. return $result;
  1876. }
  1877. function get_auth_by_mac($db, $mac)
  1878. {
  1879. if (!isset($mac)) {
  1880. return;
  1881. }
  1882. $mac = mac_dotted($mac);
  1883. $t_login = mysqli_query($db, "SELECT U.id,U.login,A.id,A.ip FROM User_list as U, User_auth as A WHERE A.user_id=U.id and A.mac='" . $mac . "' and A.deleted=0 ORDER BY A.last_found DESC limit 1");
  1884. list($f_id, $f_login, $f_auth_id, $f_ip) = mysqli_fetch_array($t_login);
  1885. if (isset($f_id)) {
  1886. $result['auth'] = '<a href=/admin/users/edituser.php?id=' . $f_id . '>' . $f_login . '</a> / ip: <a href=/admin/users/editauth.php?id=' . $f_auth_id . '>' . $f_ip . '</a>';
  1887. } else {
  1888. $result['auth'] = 'Unknown';
  1889. }
  1890. $result['mac'] = expand_mac($db, $mac);
  1891. return $result;
  1892. }
  1893. function get_auth_mac($db, $current_auth)
  1894. {
  1895. if (!isset($current_auth)) {
  1896. return;
  1897. }
  1898. if ($current_auth == 0) {
  1899. return;
  1900. }
  1901. $t_login = mysqli_query($db, "SELECT U.login,A.mac FROM User_list as U, User_auth as A WHERE A.user_id=U.id and A.id=$current_auth");
  1902. list($f_login, $f_mac) = mysqli_fetch_array($t_login);
  1903. $result = $f_login . "[" . $f_mac . "]";
  1904. return $result;
  1905. }
  1906. function add_auth_rule($db,$rule,$type,$user_id)
  1907. {
  1908. $new['user_id']=$user_id;
  1909. $new['type']=$type;
  1910. $new['rule']=$rule;
  1911. $rule_id=0;
  1912. $auth_rules = get_record_sql($db,"SELECT * FROM auth_rules WHERE rule='".$rule."' AND type=".$type);
  1913. if (empty($auth_rules)) {
  1914. $rule_id = insert_record($db,"auth_rules",$new);
  1915. LOG_INFO($db,"Create auto rule for user_id: ".$user_id." rule: ".$rule." type: ".$type);
  1916. } else {
  1917. if ($auth_rules['user_id'] === $user_id) {
  1918. $rule_id = update_record($db, "auth_rules", "id=".$auth_rules['id'], $new);
  1919. LOG_INFO($db,"Replaced auto rule for user_id: ".$user_id." rule: ".$rule." type: ".$type);
  1920. } else {
  1921. LOG_WARNING($db,"Create auto rule for user_id: ".$user_id." rule: ".$rule." type: ".$type." failed! Already exists at user_id: ".$auth_rules['user_id']);
  1922. $rule_id=0;
  1923. }
  1924. }
  1925. return $rule_id;
  1926. }
  1927. function update_auth_rule($db,$rule,$type,$rule_id)
  1928. {
  1929. $new['type']=$type;
  1930. $new['rule']=$rule;
  1931. $rule_id=0;
  1932. $auth_rules = get_record_sql($db,"SELECT * FROM auth_rules WHERE rule='".$rule."' AND type=".$type." AND id<>".$rule_id);
  1933. if (empty($auth_rules)) {
  1934. $rule_id = update_record($db, "auth_rules", "id=".$rule_id, $new);
  1935. } else {
  1936. LOG_WARNING($db,"Create auto rule id: ".$rule_id." rule: ".$rule." type: ".$type." failed! Already exists at user_id: ".$auth_rules['user_id']);
  1937. $rule_id=0;
  1938. }
  1939. return $rule_id;
  1940. }
  1941. //action: add,update,del
  1942. function allow_update($table, $action = 'update', $field = '')
  1943. {
  1944. //always allow modification for tables
  1945. if (preg_match('/(variables|dns_cache|worklog|sessions)/i', $table)) { return 1; }
  1946. if (isset($_SESSION['login'])) {
  1947. $work_user = $_SESSION['login'];
  1948. }
  1949. if (isset($_SESSION['user_id'])) {
  1950. $work_id = $_SESSION['user_id'];
  1951. }
  1952. if (isset($_SESSION['acl'])) {
  1953. $user_level = $_SESSION['acl'];
  1954. }
  1955. if (!isset($work_user) or !isset($work_id) or empty($user_level)) { return 0; }
  1956. //always allow Administrator
  1957. if ($user_level == 1) { return 1; }
  1958. //always forbid ViewOnly
  1959. if ($user_level == 3) { return 0; }
  1960. //allow tables for Operator
  1961. if (preg_match('/(dns_queue|User_auth_alias)/i', $table)) { return 1; }
  1962. if ($action == 'update') {
  1963. $operator_acl = [
  1964. 'User_auth'=> [
  1965. 'comments'=>'1',
  1966. 'dns_name'=>'1',
  1967. 'firmware'=>'1',
  1968. 'link_check'=>'1',
  1969. 'nagios'=>'1',
  1970. 'nagios_handler'=>'1',
  1971. 'Wikiname'=>'1'
  1972. ],
  1973. 'User_list'=> [
  1974. 'fio'=>'1',
  1975. 'login'=>'1',
  1976. ],
  1977. ];
  1978. if (!isset($operator_acl[$table])) { return 0; }
  1979. if (isset($operator_acl[$table]) and empty($field)) { return 1; }
  1980. if (!isset($operator_acl[$table][$field])) { return 0; }
  1981. if (empty($operator_acl[$table][$field]) or $operator_acl[$table][$field]=='0') { return 0; }
  1982. return 1;
  1983. }
  1984. return 0;
  1985. }
  1986. function LOG_INFO($db, $msg, $auth_id = 0)
  1987. {
  1988. if (get_const('log_level') < L_INFO) {
  1989. return;
  1990. }
  1991. write_log($db, $msg, L_INFO, $auth_id);
  1992. }
  1993. function LOG_ERROR($db, $msg, $auth_id = 0)
  1994. {
  1995. if (get_const('log_level') < L_ERROR) {
  1996. return;
  1997. }
  1998. email(L_ERROR, $msg);
  1999. write_log($db, $msg, L_ERROR, $auth_id);
  2000. }
  2001. function LOG_VERBOSE($db, $msg, $auth_id = 0)
  2002. {
  2003. if (get_const('log_level') < L_VERBOSE) {
  2004. return;
  2005. }
  2006. write_log($db, $msg, L_VERBOSE, $auth_id);
  2007. }
  2008. function LOG_WARNING($db, $msg, $auth_id = 0)
  2009. {
  2010. if (get_const('log_level') < L_WARNING) {
  2011. return;
  2012. }
  2013. email(L_WARNING, $msg);
  2014. write_log($db, $msg, L_WARNING, $auth_id);
  2015. }
  2016. function LOG_DEBUG($db, $msg, $auth_id = 0)
  2017. {
  2018. if (!empty(get_const('debug')) and get_const('debug')) {
  2019. write_log($db, $msg, L_DEBUG, $auth_id);
  2020. }
  2021. }
  2022. function get_first_line($msg)
  2023. {
  2024. if (empty($msg)) {
  2025. return;
  2026. }
  2027. preg_match('/(.*)(\n|\<br\>)/', $msg, $matches);
  2028. if (!empty($matches[1])) {
  2029. return $matches[1];
  2030. }
  2031. return;
  2032. }
  2033. function email($level, $msg)
  2034. {
  2035. if (!get_const('send_email')) {
  2036. return;
  2037. }
  2038. if (!($level === L_WARNING or $level === L_ERROR)) {
  2039. return;
  2040. }
  2041. $message = '<html>';
  2042. $subject = get_first_line($msg);
  2043. if ($level === L_WARNING) {
  2044. $subject = "WARN: " . $subject . "...";
  2045. $message .= 'WARNING! Manager: ' . $_SESSION['login'] . ' </br>';
  2046. }
  2047. if ($level === L_ERROR) {
  2048. $subject = "ERROR: " . $subject . "...";
  2049. $message .= 'ERROR! Manager: ' . $_SESSION['login'] . ' </br>';
  2050. }
  2051. $msg_lines = preg_replace("/\r\n/", "</br>", $msg);
  2052. $msg_lines = preg_replace("/\n/", "</br>", $msg_lines);
  2053. $message .= $msg_lines.'</html>';
  2054. $to = get_const('admin_email');
  2055. $headers = 'From: '. get_const('sender_email') . "\r\n" .
  2056. 'X-Mailer: PHP'."\r\n".
  2057. 'Content-Type: text/html; charset="utf-8"'."\r\n";
  2058. mail($to, $subject, $message, $headers);
  2059. }
  2060. function write_log($db, $msg, $level, $auth_id = 0)
  2061. {
  2062. $work_user = 'http';
  2063. if (isset($_SESSION['login'])) {
  2064. $work_user = $_SESSION['login'];
  2065. }
  2066. if (!isset($msg)) {
  2067. $msg = 'ERROR! Empty log string!';
  2068. }
  2069. if (!isset($level)) {
  2070. $level = L_INFO;
  2071. }
  2072. $msg = str_replace("'", '', $msg);
  2073. $sSQL = "insert into worklog(customer,message,level,auth_id) values('$work_user','$msg',$level,$auth_id)";
  2074. mysqli_query($db, $sSQL);
  2075. }
  2076. function print_year_select($year_name, $year)
  2077. {
  2078. print "<select name=\"$year_name\" >\n";
  2079. for ($i = $year - 10; $i <= $year + 10; $i++) {
  2080. print_select_item($i, $i, $year);
  2081. }
  2082. print "</select>\n";
  2083. }
  2084. function print_date_select($dd, $mm, $yy)
  2085. {
  2086. if ($dd >= 1) {
  2087. print "<b>День</b>\n";
  2088. print "<select name=\"day\" >\n";
  2089. for ($i = 1; $i <= 31; $i++) {
  2090. print_select_item($i, $i, $dd);
  2091. }
  2092. print "</select>\n";
  2093. }
  2094. if ($mm >= 1) {
  2095. print "<b>Месяц</b>\n";
  2096. print "<select name=\"month\" >\n";
  2097. for ($i = 1; $i <= 12; $i++) {
  2098. $tmp_date = DateTimeImmutable::createFromFormat('U', strtotime("$i/01/$yy"));
  2099. $month_name = $tmp_date->format('F');
  2100. print_select_item($month_name, $i, $mm);
  2101. }
  2102. print "</select>\n";
  2103. }
  2104. print "<b>Год</b>\n";
  2105. print_year_select('year', $yy);
  2106. }
  2107. function print_date_select2($dd, $mm, $yy)
  2108. {
  2109. if ($dd >= 1) {
  2110. print "<b>День</b>\n";
  2111. print "<select name=\"day2\" >\n";
  2112. for ($i = 1; $i <= 31; $i++) {
  2113. print_select_item($i, $i, $dd);
  2114. }
  2115. print "</select>\n";
  2116. }
  2117. if ($mm >= 1) {
  2118. print "<b>Месяц</b>\n";
  2119. print "<select name=\"month2\" >\n";
  2120. for ($i = 1; $i <= 12; $i++) {
  2121. $tmp_date = DateTimeImmutable::createFromFormat('U', strtotime("$i/01/$yy"));
  2122. $month_name = $tmp_date->format('F');
  2123. print_select_item($month_name, $i, $mm);
  2124. }
  2125. print "</select>\n";
  2126. }
  2127. print "<b>Год</b>\n";
  2128. print_year_select('year2', $yy);
  2129. }
  2130. function is_up($ip)
  2131. {
  2132. if (!isset($ip) or strlen($ip) == 0) {
  2133. return false;
  2134. }
  2135. exec(sprintf('ping -i .3 -c 1 -W 5 %s', escapeshellarg($ip)), $res, $rval);
  2136. return $rval == 0;
  2137. }
  2138. function apply_device_lock ($db, $device_id, $iteration =0) {
  2139. $iteration++;
  2140. if ($iteration>2) { return false; }
  2141. $dev = get_record_sql($db,'SELECT discovery_locked,UNIX_TIMESTAMP(locked_timestamp) as u_locked_timestamp FROM devices WHERE id='.$device_id.' AND discovery_locked > 0');
  2142. if (empty($dev) or empty($dev['u_locked_timestamp'])) {
  2143. LOG_DEBUG($db,"Snmp discovery lock not found. Set and discovery.");
  2144. return set_lock_discovery($db,$device_id);
  2145. }
  2146. //wait for discovery
  2147. $now = time();
  2148. $wait_time = ($dev['u_locked_timestamp'] + SNMP_LOCK_TIMEOUT) - $now;
  2149. LOG_DEBUG($db,"Check snmp lock for device id: " . $device_id . ". Lock timestamp: ".$dev['u_locked_timestamp'].", now: ".$now);
  2150. if ($wait_time<0) {
  2151. LOG_DEBUG($db,"The lock is already expired. Set new lock.");
  2152. return set_lock_discovery($db,$device_id);
  2153. }
  2154. LOG_VERBOSE($db,"Snmp discovery lock for device id: $device_id found! Need wait ".$wait_time." sec.");
  2155. sleep($wait_time);
  2156. LOG_VERBOSE($db,"Try set new lock and continue discovery for device id:".$device_id);
  2157. return apply_device_lock($db,$device_id,$iteration);
  2158. }
  2159. function set_lock_discovery($db,$device_id) {
  2160. $new['discovery_locked'] = 1;
  2161. $new['locked_timestamp'] = GetNowTimeString();
  2162. if (update_record($db,'devices','id='.$device_id,$new)) { return true; }
  2163. return false;
  2164. }
  2165. function unset_lock_discovery($db,$device_id) {
  2166. $new['discovery_locked'] = 0;
  2167. $new['locked_timestamp'] = GetNowTimeString();
  2168. if (update_record($db,'devices','id='.$device_id,$new)) { return true; }
  2169. return false;
  2170. }
  2171. function get_ifmib_index_table($ip, $community, $version)
  2172. {
  2173. $ifmib_map = NULL;
  2174. $is_mikrotik = walk_snmp($ip, $community, $version, MIKROTIK_DHCP_SERVER);
  2175. $mk_ros_version = 0;
  2176. if ($is_mikrotik) {
  2177. $mikrotik_version = walk_snmp($ip, $community, $version, MIKROTIK_ROS_VERSION);
  2178. $mk_ros_version = 6491;
  2179. $result = preg_match('/RouterOS\s+(\d)\.(\d{1,3})\.(\d{1,3})\s+/', $mikrotik_version[MIKROTIK_ROS_VERSION], $matches);
  2180. if ($result) {
  2181. $mk_ros_version = $matches[1] * 1000 + $matches[2] * 10 + $matches[3];
  2182. }
  2183. }
  2184. if ($mk_ros_version == 0 or $mk_ros_version > 6468) {
  2185. #fdb_index => snmp_index
  2186. $index_map_table = walk_snmp($ip, $community, $version, IFMIB_IFINDEX_MAP);
  2187. #get map snmp interfaces to fdb table
  2188. if (isset($index_map_table) and count($index_map_table) > 0) {
  2189. foreach ($index_map_table as $key => $value) {
  2190. $key = trim($key);
  2191. $value = intval(trim(str_replace('INTEGER:', '', $value)));
  2192. $result = preg_match('/\.(\d{1,10})$/', $key, $matches);
  2193. if ($result) {
  2194. $fdb_index = preg_replace('/^\./', '', $matches[0]);
  2195. $ifmib_map[$fdb_index] = $value;
  2196. }
  2197. }
  2198. }
  2199. }
  2200. #return simple map snmp_port_index = snmp_port_index
  2201. if (empty($ifmib_map)) {
  2202. #ifindex
  2203. $index_table = walk_snmp($ip, $community, $version, IFMIB_IFINDEX);
  2204. if (isset($index_table) and count($index_table) > 0) {
  2205. foreach ($index_table as $key => $value) {
  2206. $key = trim($key);
  2207. $value = intval(trim(str_replace('INTEGER:', '', $value)));
  2208. $result = preg_match('/\.(\d{1,10})$/', $key, $matches);
  2209. if ($result) {
  2210. $fdb_index = preg_replace('/^\./', '', $matches[0]);
  2211. $ifmib_map[$fdb_index] = $value;
  2212. }
  2213. }
  2214. }
  2215. }
  2216. return $ifmib_map;
  2217. }
  2218. #get mac table by selected snmp oid
  2219. function get_mac_table($ip, $community, $version, $oid, $index_map)
  2220. {
  2221. if (!isset($ip)) {
  2222. return;
  2223. }
  2224. if (!isset($oid)) {
  2225. return;
  2226. }
  2227. if (!isset($community)) {
  2228. $community = 'public';
  2229. }
  2230. if (!isset($version)) {
  2231. $version = '2';
  2232. }
  2233. $mac_table = walk_snmp($ip, $community, $version, $oid);
  2234. if (isset($mac_table) and gettype($mac_table) == 'array' and count($mac_table) > 0) {
  2235. foreach ($mac_table as $key => $value) {
  2236. if (empty($value)) {
  2237. continue;
  2238. }
  2239. if (empty($key)) {
  2240. continue;
  2241. }
  2242. $key = trim($key);
  2243. $value_raw = intval(trim(str_replace('INTEGER:', '', $value)));
  2244. if (empty($value_raw)) {
  2245. continue;
  2246. }
  2247. if (!empty($index_map)) {
  2248. if (empty($index_map[$value_raw])) {
  2249. $value = $value_raw;
  2250. } else {
  2251. $value = $index_map[$value_raw];
  2252. }
  2253. } else {
  2254. $value = $value_raw;
  2255. }
  2256. $pattern = '/\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})$/';
  2257. $result = preg_match($pattern, $key, $matches);
  2258. if (!empty($result)) {
  2259. $mac_key = preg_replace('/^\./', '', $matches[0]);
  2260. $fdb_table[$mac_key] = $value;
  2261. }
  2262. }
  2263. }
  2264. return $fdb_table;
  2265. }
  2266. #get ip interfaces
  2267. function getIpAdEntIfIndex($db, $ip, $community = 'public', $version = '2')
  2268. {
  2269. if (!isset($ip)) { return; }
  2270. #oid+ip = index
  2271. $ip_table = walk_snmp($ip, $community, $version, ipAdEntIfIndex);
  2272. #oid+index=name
  2273. $int_table = walk_snmp($ip, $community, $version, ifDescr);
  2274. $result = [];
  2275. if (isset($ip_table) and gettype($ip_table) == 'array' and count($ip_table) > 0) {
  2276. foreach ($ip_table as $key => $value) {
  2277. if (empty($value)) { continue; }
  2278. if (empty($key)) { continue; }
  2279. $key = trim($key);
  2280. $interface_index = intval(trim(str_replace('INTEGER:', '', $value)));
  2281. if (empty($value)) { continue; }
  2282. $interface_name = $int_table[ifDescr . '.' .$interface_index];
  2283. $interface_name = trim(str_replace('STRING:', '', $interface_name));
  2284. $interface_ip = trim(str_replace(ipAdEntIfIndex.'.','',$key));
  2285. if (empty($interface_name)) { continue; }
  2286. $result[$interface_index]['ip']=$interface_ip;
  2287. $result[$interface_index]['index']=$interface_index;
  2288. $result[$interface_index]['name']=$interface_name;
  2289. //type: 0 - local, 1 - WAN
  2290. $result[$interface_index]['type'] = 1;
  2291. if (is_our_network($db,$interface_ip)) { $result[$interface_index]['type']=0; }
  2292. }
  2293. }
  2294. return $result;
  2295. }
  2296. #get mac table by analyze all available tables
  2297. function get_fdb_table($ip, $community, $version)
  2298. {
  2299. if (!isset($ip)) {
  2300. return;
  2301. }
  2302. if (!isset($community)) {
  2303. $community = 'public';
  2304. }
  2305. if (!isset($version)) {
  2306. $version = '2';
  2307. }
  2308. $ifindex_map = get_ifmib_index_table($ip, $community, $version);
  2309. $fdb1_table = get_mac_table($ip, $community, $version, MAC_TABLE_OID, $ifindex_map);
  2310. if (!empty($fdb1_table)) {
  2311. $fdb_table = $fdb1_table;
  2312. } else {
  2313. $fdb2_table = get_mac_table($ip, $community, $version, MAC_TABLE_OID2, $ifindex_map);
  2314. if (!empty($fdb2_table)) {
  2315. $fdb_table = $fdb2_table;
  2316. }
  2317. }
  2318. // maybe cisco?!
  2319. if (!isset($fdb_table) or empty($fdb_table) or count($fdb_table) == 0) {
  2320. $vlan_table = walk_snmp($ip, $community, $version, CISCO_VLAN_OID);
  2321. if (empty($vlan_table)) {
  2322. return;
  2323. }
  2324. foreach ($vlan_table as $vlan_oid => $value) {
  2325. if (empty($vlan_oid)) {
  2326. continue;
  2327. }
  2328. $pattern = '/\.(\d{1,4})$/';
  2329. $result = preg_match($pattern, $vlan_oid, $matches);
  2330. if (!empty($result)) {
  2331. $vlan_id = preg_replace('/^\./', '', $matches[0]);
  2332. if ($vlan_id > 1000 and $vlan_id < 1009) {
  2333. continue;
  2334. }
  2335. $fdb_vlan_table = get_mac_table($ip, $community . '@' . $vlan_id, $version, MAC_TABLE_OID, $ifindex_map);
  2336. if (!isset($fdb_vlan_table) or !$fdb_vlan_table or count($fdb_vlan_table) == 0) {
  2337. $fdb_vlan_table = get_mac_table($ip, $community, $version, MAC_TABLE_OID2, $ifindex_map);
  2338. }
  2339. foreach ($fdb_vlan_table as $mac => $port) {
  2340. if (!isset($mac)) {
  2341. continue;
  2342. }
  2343. $fdb_table[$mac] = $port;
  2344. }
  2345. }
  2346. }
  2347. }
  2348. return $fdb_table;
  2349. }
  2350. function check_snmp_access($ip, $community, $version)
  2351. {
  2352. if (!isset($ip)) {
  2353. return;
  2354. }
  2355. if (!isset($community)) {
  2356. $community = 'public';
  2357. }
  2358. if (!isset($version)) {
  2359. $version = '2';
  2360. }
  2361. #check host up
  2362. $status = exec(escapeshellcmd("ping -W 1 -i 1 -c 3 " . $ip));
  2363. if (empty($status)) {
  2364. return;
  2365. }
  2366. #check snmp
  2367. $result = get_snmp($ip, $community, $version, SYS_DESCR_MIB);
  2368. if (empty($result)) {
  2369. return;
  2370. }
  2371. return 1;
  2372. }
  2373. function get_port_state($port, $ip, $community, $version)
  2374. {
  2375. if (!isset($port)) {
  2376. return;
  2377. }
  2378. if (!isset($ip)) {
  2379. return;
  2380. }
  2381. if (!isset($community)) {
  2382. $community = 'public';
  2383. }
  2384. if (!isset($version)) {
  2385. $version = '2';
  2386. }
  2387. $port_oid = PORT_STATUS_OID .'.'. $port;
  2388. $port_state = get_snmp($ip, $community, $version, $port_oid);
  2389. return $port_state;
  2390. }
  2391. function get_last_digit($oid)
  2392. {
  2393. if (!isset($oid)) {
  2394. return;
  2395. }
  2396. $pattern = '/\.(\d{1,})$/';
  2397. preg_match($pattern, $oid, $matches);
  2398. return $matches[1];
  2399. }
  2400. function get_cisco_sensors($ip, $community, $version, $mkey)
  2401. {
  2402. $index = get_last_digit($mkey);
  2403. $result = parse_snmp_value(get_snmp($ip, $community, $version, CISCO_SFP_SENSORS . "." . $index));
  2404. $prec = parse_snmp_value(get_snmp($ip, $community, $version, CISCO_SFP_PRECISION . "." . $index));
  2405. if (!isset($prec)) {
  2406. $prec = 1;
  2407. }
  2408. $result = round(trim($result) / (10 * $prec), 2);
  2409. return $result;
  2410. }
  2411. function get_snmp_ifname($ip, $community, $version, $port)
  2412. {
  2413. $port_name = parse_snmp_value(get_snmp($ip, $community, $version, IFMIB_IFNAME . "." . $port));
  2414. if (empty($port_name)) {
  2415. $port_name = parse_snmp_value(get_snmp($ip, $community, $version, IFMIB_IFDESCR . "." . $port));
  2416. }
  2417. if (empty($port_name)) {
  2418. $port_name = parse_snmp_value(get_snmp($ip, $community, $version, IFMIB_IFALIAS . "." . $port));
  2419. }
  2420. return $port_name;
  2421. }
  2422. function get_snmp_interfaces($ip, $community, $version)
  2423. {
  2424. $result=[];
  2425. $ifmib_list = walk_snmp($ip, $community, $version, IFMIB_IFNAME);
  2426. if (empty($ifmib_list)) {
  2427. $ifmib_list = walk_snmp($ip, $community, $version, IFMIB_IFDESCR);
  2428. }
  2429. if (empty($ifmib_list)) {
  2430. $ifmib_list = walk_snmp($ip, $community, $version, IFMIB_IFALIAS);
  2431. }
  2432. if (!empty($ifmib_list)) {
  2433. foreach ($ifmib_list as $key => $value) {
  2434. $key = trim($key);
  2435. $value = parse_snmp_value($value);
  2436. if (preg_match('/\.(\d{1,10})$/', $key, $matches)) {
  2437. $int_index = preg_replace('/^\./', '', $matches[0]);
  2438. $result[$int_index]=$value;
  2439. }
  2440. }
  2441. }
  2442. return $result;
  2443. }
  2444. function walk_snmp($ip, $community, $version, $oid)
  2445. {
  2446. snmp_set_oid_output_format(SNMP_OID_OUTPUT_NUMERIC);
  2447. $result = NULL;
  2448. if ($version == 2) {
  2449. $result = snmp2_real_walk($ip, $community, $oid, SNMP_timeout, SNMP_retry);
  2450. }
  2451. if ($version == 1) {
  2452. $result = snmprealwalk($ip, $community, $oid, SNMP_timeout, SNMP_retry);
  2453. }
  2454. return $result;
  2455. }
  2456. function get_snmp_module_id($modules_oids, $port_name)
  2457. {
  2458. $port_name = preg_quote(trim($port_name), '/');
  2459. foreach ($modules_oids as $key => $value) {
  2460. $pattern = '/' . $port_name . '/i';
  2461. preg_match($pattern, $value, $matches);
  2462. if (isset($matches[0])) {
  2463. $module_id = get_last_digit($key);
  2464. return $module_id;
  2465. }
  2466. }
  2467. return;
  2468. }
  2469. function get_sfp_status($vendor_id, $port, $ip, $community='public', $version='2', $modules_oids)
  2470. {
  2471. if (!isset($vendor_id)) {
  2472. return;
  2473. }
  2474. if (!isset($port)) {
  2475. return;
  2476. }
  2477. if (!isset($ip)) {
  2478. return;
  2479. }
  2480. try {
  2481. $status = '';
  2482. // eltex
  2483. if ($vendor_id == 2) {
  2484. $sfp_vendor = parse_snmp_value(get_snmp($ip, $community, $version, ELTEX_SFP_VENDOR . "." . $port));
  2485. if (!empty($sfp_vendor)) {
  2486. $sfp_status_temp = ELTEX_SFP_STATUS . "." . $port . ".5";
  2487. $sfp_status_volt = ELTEX_SFP_STATUS . "." . $port . ".6";
  2488. $sfp_status_circut = ELTEX_SFP_STATUS . "." . $port . ".7";
  2489. $sfp_status_tx = ELTEX_SFP_STATUS . "." . $port . ".8";
  2490. $sfp_status_rx = ELTEX_SFP_STATUS . "." . $port . ".9";
  2491. $temp = parse_snmp_value(get_snmp($ip, $community, $version, $sfp_status_temp));
  2492. $volt = parse_snmp_value(get_snmp($ip, $community, $version, $sfp_status_volt));
  2493. $circut = parse_snmp_value(get_snmp($ip, $community, $version, $sfp_status_circut));
  2494. $tx = parse_snmp_value(get_snmp($ip, $community, $version, $sfp_status_tx));
  2495. $rx = parse_snmp_value(get_snmp($ip, $community, $version, $sfp_status_rx));
  2496. $sfp_sn = parse_snmp_value(get_snmp($ip, $community, $version, ELTEX_SFP_SN . "." . $port));
  2497. $sfp_freq = parse_snmp_value(get_snmp($ip, $community, $version, ELTEX_SFP_FREQ . "." . $port));
  2498. if (!isset($sfp_freq) or $sfp_freq == 65535) {
  2499. $sfp_freq = 'unspecified';
  2500. }
  2501. $sfp_length = parse_snmp_value(get_snmp($ip, $community, $version, ELTEX_SFP_LENGTH . "." . $port));
  2502. $status = 'Vendor: ' . $sfp_vendor . ' Serial: ' . $sfp_sn . ' Laser: ' . $sfp_freq . ' Distance: ' . $sfp_length . '<br>';
  2503. if (!empty($sfp_status_temp) and $temp > 0.1) {
  2504. $status .= 'Temp: ' . $temp . " C";
  2505. }
  2506. if (!empty($sfp_status_volt) and $volt > 0.1) {
  2507. $status .= ' Volt: ' . round($volt / 1000000, 2) . ' V';
  2508. }
  2509. if (!empty($sfp_status_circut) and $circut > 0.1) {
  2510. $status .= ' Circut: ' . round($circut / 1000, 2) . ' mA';
  2511. }
  2512. if (!empty($sfp_status_tx) and $tx > 0.1) {
  2513. $status .= ' Tx: ' . round($tx / 1000, 2) . ' dBm';
  2514. }
  2515. if (!empty($sfp_status_rx) and $rx > 0.1) {
  2516. $status .= ' Rx: ' . round($rx / 1000, 2) . ' dBm';
  2517. }
  2518. $status .= '<br>';
  2519. return $status;
  2520. }
  2521. return;
  2522. }
  2523. // snr
  2524. if ($vendor_id == 6) {
  2525. $sfp_vendor = parse_snmp_value(get_snmp($ip, $community, $version, SNR_SFP_VendorName . "." . $port));
  2526. if (!empty($sfp_vendor) and $sfp_vendor != 'NULL') {
  2527. $oid_sfp_model_name = SNR_SFP_ModelName . "." . $port;
  2528. $oid_sfp_type_name = SNR_SFP_TypeName . "." . $port;
  2529. $oid_sfp_bitrate = SNR_SFP_BitRate . "." . $port;
  2530. $oid_sfp_status_volt = SNR_SFP_VOLT . "." . $port;
  2531. $oid_sfp_status_circut = SNR_SFP_BIAS . "." . $port;
  2532. $oid_sfp_status_tx = SNR_SFP_TX . "." . $port;
  2533. $oid_sfp_status_rx = SNR_SFP_RX . "." . $port;
  2534. $oid_sfp_length = SNR_SFP_WaveLength . "." . $port;
  2535. $volt = parse_snmp_value(get_snmp($ip, $community, $version, $oid_sfp_status_volt));
  2536. $circut = parse_snmp_value(get_snmp($ip, $community, $version, $oid_sfp_status_circut));
  2537. $tx = parse_snmp_value(get_snmp($ip, $community, $version, $oid_sfp_status_tx));
  2538. $rx = parse_snmp_value(get_snmp($ip, $community, $version, $oid_sfp_status_rx));
  2539. $sfp_model_name = parse_snmp_value(get_snmp($ip, $community, $version, $oid_sfp_model_name));
  2540. $sfp_type_name = parse_snmp_value(get_snmp($ip, $community, $version, $oid_sfp_type_name));
  2541. $sfp_freq = parse_snmp_value(get_snmp($ip, $community, $version, $oid_sfp_bitrate));
  2542. $sfp_length = parse_snmp_value(get_snmp($ip, $community, $version, $oid_sfp_length));
  2543. $status = 'Vendor: ' . $sfp_vendor . ' '. $sfp_model_name. ' '.$sfp_type_name. ' Speed: ' . $sfp_freq . ' Freq: ' . $sfp_length . '<br>';
  2544. if (!empty($sfp_status_volt) and $volt > 0.1) {
  2545. $status .= ' Volt: ' . round($volt / 1000000, 2) . ' V';
  2546. }
  2547. if (!empty($sfp_status_circut) and $circut > 0.1) {
  2548. $status .= ' Circut: ' . round($circut / 1000, 2) . ' mA';
  2549. }
  2550. if (!empty($sfp_status_tx) and $tx > 0.1) {
  2551. $status .= ' Tx: ' . round($tx / 1000, 2) . ' dBm';
  2552. }
  2553. if (!empty($sfp_status_rx) and $rx > 0.1) {
  2554. $status .= ' Rx: ' . round($rx / 1000, 2) . ' dBm';
  2555. }
  2556. $status .= '<br>';
  2557. return $status;
  2558. }
  2559. return;
  2560. }
  2561. // cisco
  2562. if ($vendor_id == 16) {
  2563. // get interface names
  2564. $port_name = parse_snmp_value(get_snmp($ip, $community, $version, IFMIB_IFNAME . "." . $port));
  2565. if (empty($port_name)) {
  2566. $port_name = parse_snmp_value(get_snmp($ip, $community, $version, IFMIB_IFDESCR . "." . $port));
  2567. }
  2568. // search module indexes
  2569. $port_name = preg_quote(trim($port_name), '/');
  2570. foreach ($modules_oids as $key => $value) {
  2571. $pattern = '/(' . $port_name . ' Module Temperature Sensor)/i';
  2572. preg_match($pattern, $value, $matches);
  2573. if (isset($matches[0])) {
  2574. $temp = get_cisco_sensors($ip, $community, $version, $key);
  2575. continue;
  2576. }
  2577. $pattern = '/(' . $port_name . ' Supply Voltage Sensor)/i';
  2578. preg_match($pattern, $value, $matches);
  2579. if (isset($matches[0])) {
  2580. $volt = get_cisco_sensors($ip, $community, $version, $key);
  2581. continue;
  2582. }
  2583. $pattern = '/(' . $port_name . ' Bias Current Sensor)/i';
  2584. preg_match($pattern, $value, $matches);
  2585. if (isset($matches[0])) {
  2586. $circut = get_cisco_sensors($ip, $community, $version, $key);
  2587. continue;
  2588. }
  2589. $pattern = '/(' . $port_name . ' Transmit Power Sensor)/i';
  2590. preg_match($pattern, $value, $matches);
  2591. if (isset($matches[0])) {
  2592. $tx = get_cisco_sensors($ip, $community, $version, $key);
  2593. continue;
  2594. }
  2595. $pattern = '/(' . $port_name . ' Receive Power Sensor)/i';
  2596. preg_match($pattern, $value, $matches);
  2597. if (isset($matches[0])) {
  2598. $rx = get_cisco_sensors($ip, $community, $version, $key);
  2599. continue;
  2600. }
  2601. }
  2602. if (!empty($temp) and $temp > 0) {
  2603. $status .= 'Temp: ' . $temp . " C";
  2604. }
  2605. if (!empty($volt) and $volt > 0) {
  2606. $status .= ' Volt: ' . $volt . ' V';
  2607. }
  2608. if (!empty($circut) and $circut > 0) {
  2609. $status .= ' Circut: ' . $circut . ' mA';
  2610. }
  2611. if (!empty($tx) and abs($tx) > 0.1) {
  2612. $status .= ' Tx: ' . $tx . ' dBm';
  2613. }
  2614. if (!empty($rx) and abs($rx) > 0.1) {
  2615. $status .= ' Rx: ' . $rx . ' dBm';
  2616. }
  2617. if (!empty($status)) {
  2618. $status = preg_replace('/"/', '', $status);
  2619. $status .= '<br>';
  2620. }
  2621. return $status;
  2622. }
  2623. // huawei
  2624. if ($vendor_id == 3) {
  2625. // get interface names
  2626. $port_name = parse_snmp_value(get_snmp($ip, $community, $version, IFMIB_IFNAME . "." . $port));
  2627. if (empty($port_name)) {
  2628. $port_name = parse_snmp_value(get_snmp($ip, $community, $version, IFMIB_IFDESCR . "." . $port));
  2629. }
  2630. // search module indexes
  2631. $port_name = preg_quote(trim($port_name), '/');
  2632. foreach ($modules_oids as $key => $value) {
  2633. $pattern = '/' . $port_name . '/i';
  2634. preg_match($pattern, $value, $matches);
  2635. if (isset($matches[0])) {
  2636. $module_id = get_last_digit($key);
  2637. unset($result);
  2638. $result = parse_snmp_value(get_snmp($ip, $community, $version, HUAWEI_SFP_VENDOR . "." . $module_id));
  2639. if (!empty($result)) {
  2640. $sfp_vendor = $result;
  2641. }
  2642. unset($result);
  2643. $result = parse_snmp_value(get_snmp($ip, $community, $version, HUAWEI_SFP_SPEED . "." . $module_id));
  2644. if (!empty($result)) {
  2645. list($sfp_speed, $spf_lenght, $sfp_type) = explode('-', $result);
  2646. if ($sfp_type == 0) {
  2647. $sfp_type = 'MultiMode';
  2648. }
  2649. if ($sfp_type == 1) {
  2650. $sfp_type = 'SingleMode';
  2651. }
  2652. }
  2653. $volt = parse_snmp_value(get_snmp($ip, $community, $version, HUAWEI_SFP_VOLT . "." . $module_id));
  2654. $circut = parse_snmp_value(get_snmp($ip, $community, $version, HUAWEI_SFP_BIASCURRENT . "." . $module_id));
  2655. $tx = parse_snmp_value(get_snmp($ip, $community, $version, HUAWEI_SFP_OPTTX . "." . $module_id));
  2656. $rx = parse_snmp_value(get_snmp($ip, $community, $version, HUAWEI_SFP_OPTRX . "." . $module_id));
  2657. if (!isset($tx)) {
  2658. $tx = parse_snmp_value(get_snmp($ip, $community, $version, HUAWEI_SFP_TX . "." . $module_id));
  2659. }
  2660. if (!isset($rx)) {
  2661. $rx = parse_snmp_value(get_snmp($ip, $community, $version, HUAWEI_SFP_RX . "." . $module_id));
  2662. }
  2663. if (!empty($sfp_vendor)) {
  2664. $status .= ' Name:' . $sfp_vendor . '<br>';
  2665. }
  2666. // if (isset($sfp_speed)) { $status .= ' ' . $sfp_speed; }
  2667. // if (isset($spf_lenght)) { $status .= ' ' . $spf_lenght; }
  2668. if ($volt > 0) {
  2669. $status .= ' Volt: ' . round($volt / 1000, 2) . ' V';
  2670. }
  2671. if (!empty($circut) and $circut > 0) {
  2672. $status .= ' Circut: ' . $circut . ' mA <br>';
  2673. }
  2674. if (!empty($tx)) {
  2675. $tx = preg_replace('/"/', '', $tx);
  2676. try {
  2677. list($tx_dbm, $pattern) = explode('.', $tx);
  2678. $tx_dbm = round(floatval(trim($tx_dbm)) / 100, 2);
  2679. } catch (Exception $e) { $tx_dbm = 0; }
  2680. if (abs($tx_dbm) > 0.1) {
  2681. $status .= ' Tx: ' . $tx_dbm . ' dBm';
  2682. }
  2683. }
  2684. if (!empty($rx)) {
  2685. $rx = preg_replace('/"/', '', $rx);
  2686. try {
  2687. list($rx_dbm, $pattern) = explode('.', $rx);
  2688. $rx_dbm = round(floatval(trim($rx_dbm)) / 100, 2);
  2689. } catch (Exception $e) { $rx_dbm=0; }
  2690. if (abs($rx_dbm) > 0.1) {
  2691. $status .= ' Rx: ' . $rx_dbm . ' dBm';
  2692. }
  2693. }
  2694. break;
  2695. }
  2696. }
  2697. if (isset($status)) {
  2698. $status = preg_replace('/"/', '', $status);
  2699. $status .= '<br>';
  2700. }
  2701. return $status;
  2702. }
  2703. } catch (Exception $e) {
  2704. return;
  2705. }
  2706. }
  2707. function get_switch_vlans($vendor,$ip,$community='public',$version='2') {
  2708. $switch_vlans = [];
  2709. $port_status = [];
  2710. $vlan_status = [];
  2711. //cisco...
  2712. if ($vendor == 16) {
  2713. //all vlan at switch
  2714. $vlan_list = walk_snmp($ip, $community, $version, vtpVlanName);
  2715. if (empty($vlan_list)) { return; }
  2716. foreach ($vlan_list as $key => $value) {
  2717. if (empty($value)) { $value = ''; }
  2718. $key = trim($key);
  2719. $value = parse_snmp_value($value);
  2720. $vlan_id = NULL;
  2721. if (preg_match('/\.(\d{1,10})$/', $key, $matches)) { $vlan_id = preg_replace('/^\./', '', $matches[0]); }
  2722. //skip service vlan
  2723. if (preg_match('/(1002|1003|1004|1005)/',$vlan_id)) { continue; }
  2724. if (isset($vlan_id) and !empty($vlan_id)) { $switch_vlans[$vlan_id]=$value; }
  2725. }
  2726. //native vlan for port - get list of all ports
  2727. $pvid_list = walk_snmp($ip, $community, $version, vlanTrunkPortNativeVlan);
  2728. if (!empty($pvid_list)) {
  2729. foreach ($pvid_list as $key => $value) {
  2730. if (empty($value)) { $value = ''; }
  2731. $key = trim($key);
  2732. $value = parse_snmp_value($value);
  2733. $port = NULL;
  2734. if (preg_match('/\.(\d{1,10})$/', $key, $matches)) { $port = preg_replace('/^\./', '', $matches[0]); }
  2735. if (isset($port) and !empty($port)) { $port_status[$port]['native']=$value; }
  2736. }
  2737. }
  2738. //pvid
  2739. $pvid_list = walk_snmp($ip, $community, $version, vmVlanPvid);
  2740. if (!empty($pvid_list)) {
  2741. foreach ($pvid_list as $key => $value) {
  2742. if (empty($value)) { $value = ''; }
  2743. $key = trim($key);
  2744. $value = parse_snmp_value($value);
  2745. $port = NULL;
  2746. if (preg_match('/\.(\d{1,10})$/', $key, $matches)) { $port = preg_replace('/^\./', '', $matches[0]); }
  2747. if (isset($port) and !empty($port)) { $port_status[$port]['pvid']=$value; }
  2748. }
  2749. }
  2750. //init port config
  2751. foreach ($port_status as &$port) {
  2752. if (!is_array($port)) { continue; }
  2753. if (!isset($port['pvid'])) { $port['pvid']=$port['native']; }
  2754. $port['untagged']='';
  2755. $port['tagged']='';
  2756. }
  2757. unset($port);
  2758. //get vlan list at ports
  2759. $egress_vlan = walk_snmp($ip,$community,$version,vlanTrunkPortVlansEnabled);
  2760. if (!empty($egress_vlan)) {
  2761. $j = 0;
  2762. foreach ($egress_vlan as $key => $value) {
  2763. $j++;
  2764. if (empty($value)) { $value = ''; }
  2765. $key = trim($key);
  2766. $value = parse_snmp_value($value);
  2767. if (preg_match('/\.(\d{1,10})$/', $key, $matches)) {
  2768. $port = preg_replace('/^\./', '', $matches[0]);
  2769. }
  2770. if (isset($port) and !empty($port)) {
  2771. //skip access ports
  2772. if (!is_array($port_status[$port]) or !isset($port_status[$port]['pvid']) or !isset($port_status[$port]['native'])) { continue; }
  2773. if ($port_status[$port]['pvid'] != $port_status[$port]['native']) { continue; }
  2774. //get vlan at port in hex
  2775. $hex_value = preg_replace('/\s+/','',$value);
  2776. $bin_value = strHexToBin($hex_value);
  2777. //analyze switch vlans
  2778. foreach ($switch_vlans as $vlan_id => $vlan_name) {
  2779. if (isset($bin_value[$vlan_id]) and $bin_value[$vlan_id]=='1') {
  2780. $port_status[$port]['tagged']=$port_status[$port]['tagged'].','.$vlan_id;
  2781. }
  2782. }
  2783. }
  2784. }
  2785. }
  2786. //remove lliding ,
  2787. foreach ($port_status as &$port) {
  2788. if (!is_array($port)) { continue; }
  2789. $port['untagged']=preg_replace('/^,/', '',$port['untagged']);
  2790. $port['tagged']=preg_replace('/^,/', '',$port['tagged']);
  2791. }
  2792. unset($port);
  2793. return $port_status;
  2794. }
  2795. //standart switches
  2796. //tplink
  2797. if ($vendor == 69) {
  2798. //pvid for port
  2799. $pvid_list = walk_snmp($ip, $community, $version, TPLINK_dot1qPortVlanEntry);
  2800. if (!empty($pvid_list)) {
  2801. foreach ($pvid_list as $key => $value) {
  2802. if (empty($value)) { $value = ''; }
  2803. $key = trim($key);
  2804. $value = parse_snmp_value($value);
  2805. $port = NULL;
  2806. if (preg_match('/\.(\d{1,10})$/', $key, $matches)) { $port = preg_replace('/^\./', '', $matches[0]); }
  2807. if (isset($port) and !empty($port)) { $port_status[$port]['pvid']=$value; }
  2808. }
  2809. }
  2810. return $port_status;
  2811. }
  2812. //default
  2813. //pvid for port
  2814. $pvid_list = walk_snmp($ip, $community, $version, dot1qPortVlanEntry);
  2815. if (!empty($pvid_list)) {
  2816. foreach ($pvid_list as $key => $value) {
  2817. if (empty($value)) { $value = ''; }
  2818. $key = trim($key);
  2819. $value = parse_snmp_value($value);
  2820. $port = NULL;
  2821. if (preg_match('/\.(\d{1,10})$/', $key, $matches)) { $port = preg_replace('/^\./', '', $matches[0]); }
  2822. if (isset($port) and !empty($port)) { $port_status[$port]['pvid']=$value; }
  2823. }
  2824. }
  2825. //init port config
  2826. foreach ($port_status as &$port) {
  2827. if (!is_array($port)) { continue; }
  2828. $port['native']=$port['pvid'];
  2829. $port['untagged']='';
  2830. $port['tagged']='';
  2831. }
  2832. unset($port);
  2833. //all vlan at switch
  2834. $vlan_list = walk_snmp($ip, $community, $version, dot1qVlanStaticName);
  2835. if (empty($vlan_list)) { return $port_status; }
  2836. foreach ($vlan_list as $key => $value) {
  2837. if (empty($value)) { $value = ''; }
  2838. $key = trim($key);
  2839. $value = parse_snmp_value($value);
  2840. $vlan_id = NULL;
  2841. if (preg_match('/\.(\d{1,10})$/', $key, $matches)) { $vlan_id = preg_replace('/^\./', '', $matches[0]); }
  2842. if (isset($vlan_id) and !empty($vlan_id)) { $switch_vlans[$vlan_id]=$value; }
  2843. }
  2844. $forbidden_vlan = walk_snmp($ip,$community,$version,dot1qVlanForbiddenEgressPorts);
  2845. if (!empty($forbidden_vlan)) {
  2846. foreach ($forbidden_vlan as $key => $value) {
  2847. if (empty($value)) { $value = ''; }
  2848. $key = trim($key);
  2849. $value = parse_snmp_value($value);
  2850. if (preg_match('/\.(\d{1,10})$/', $key, $matches)) { $vlan_id = preg_replace('/^\./', '', $matches[0]); }
  2851. if (isset($vlan_id) and !empty($vlan_id)) {
  2852. $hex_value = preg_replace('/\s+/','',$value);
  2853. $hex_value = preg_replace('/0*$/','',$hex_value);
  2854. $bin_value = strHexToBin($hex_value);
  2855. for ($i=0; $i<strlen($bin_value); $i++) {
  2856. $port = $i+1;
  2857. $vlan_status['forbidden_vlan'][$vlan_id][$port] = $bin_value[$i];
  2858. if ($bin_value[$i]=='1') {
  2859. $port_status[$port]['forbidden'].=','.$vlan_id;
  2860. }
  2861. }
  2862. }
  2863. }
  2864. }
  2865. $untagged_vlan = walk_snmp($ip,$community,$version,dot1qVlanStaticUntaggedPorts);
  2866. if (!empty($untagged_vlan)) {
  2867. foreach ($untagged_vlan as $key => $value) {
  2868. if (empty($value)) { $value = ''; }
  2869. $key = trim($key);
  2870. $value = parse_snmp_value($value);
  2871. if (preg_match('/\.(\d{1,10})$/', $key, $matches)) { $vlan_id = preg_replace('/^\./', '', $matches[0]); }
  2872. if (isset($vlan_id) and !empty($vlan_id)) {
  2873. $hex_value = preg_replace('/\s+/','',$value);
  2874. $hex_value = preg_replace('/0*$/','',$hex_value);
  2875. $bin_value = strHexToBin($hex_value);
  2876. for ($i=0; $i<strlen($bin_value); $i++) {
  2877. $port = $i+1;
  2878. $vlan_status['untagged_vlan'][$vlan_id][$port] = $bin_value[$i];
  2879. if ($bin_value[$i]=='1') {
  2880. if (isset($vlan_status['forbidden_vlan']) and $vlan_status['forbidden_vlan'][$vlan_id][$port]=='0') {
  2881. $port_status[$port]['untagged'].=','.$vlan_id;
  2882. } else {
  2883. $vlan_status['untagged_vlan'][$vlan_id][$port]='0';
  2884. }
  2885. }
  2886. }
  2887. }
  2888. }
  2889. }
  2890. $egress_vlan = walk_snmp($ip,$community,$version,dot1qVlanStaticEgressPorts);
  2891. if (!empty($egress_vlan)) {
  2892. foreach ($egress_vlan as $key => $value) {
  2893. if (empty($value)) { $value = ''; }
  2894. $key = trim($key);
  2895. $value = parse_snmp_value($value);
  2896. if (preg_match('/\.(\d{1,10})$/', $key, $matches)) { $vlan_id = preg_replace('/^\./', '', $matches[0]); }
  2897. //exclude vlan 1 from tagged vlan
  2898. if ($vlan_id =='1') { continue; }
  2899. if (isset($vlan_id) and !empty($vlan_id)) {
  2900. $hex_value = preg_replace('/\s+/','',$value);
  2901. $hex_value = preg_replace('/0*$/','',$hex_value);
  2902. $bin_value = strHexToBin($hex_value);
  2903. for ($i=0; $i<strlen($bin_value); $i++) {
  2904. $port = $i+1;
  2905. $vlan_status['egress_vlan'][$vlan_id][$port] = $bin_value[$i];
  2906. //analyze egress & untagged vlans
  2907. if ($bin_value[$i]=='1') {
  2908. if ((!isset($vlan_status['untagged_vlan'][$vlan_id][$port]) or $vlan_status['untagged_vlan'][$vlan_id][$port]=='0' ) and
  2909. (!isset($vlan_status['forbidden_vlan'][$vlan_id][$port]) or $vlan_status['forbidden_vlan'][$vlan_id][$port]=='0') and
  2910. (!isset($port_status[$port]['pvid']) or $port_status[$port]['pvid']!=$vlan_id )) {
  2911. $vlan_status['tagged_vlan'][$vlan_id][$port]='1';
  2912. $port_status[$port]['tagged'].=','.$vlan_id;
  2913. } else {
  2914. $vlan_status['tagged_vlan'][$vlan_id][$port]='0';
  2915. }
  2916. }
  2917. }
  2918. }
  2919. }
  2920. }
  2921. foreach ($port_status as &$port) {
  2922. if (!is_array($port)) { continue; }
  2923. $port['untagged']=preg_replace('/^,/', '',$port['untagged']);
  2924. $port['tagged']=preg_replace('/^,/', '',$port['tagged']);
  2925. }
  2926. unset($port);
  2927. return $port_status;
  2928. }
  2929. function get_port_vlan($vendor, $port, $port_index, $ip, $community='public', $version='2')
  2930. {
  2931. if (!isset($port_index)) {
  2932. return;
  2933. }
  2934. if (!isset($ip)) {
  2935. return;
  2936. }
  2937. //default - default port index
  2938. $port_oid = dot1qPortVlanEntry . "." . $port_index;
  2939. //tplink
  2940. if ($vendor == 69) {
  2941. $port_oid = TPLINK_dot1qPortVlanEntry . "." . $port_index;
  2942. }
  2943. //huawei
  2944. if ($vendor == 3) {
  2945. $port_oid = dot1qPortVlanEntry . "." . $port;
  2946. }
  2947. //allied telesys
  2948. if ($vendor == 8) {
  2949. $port_oid = dot1qPortVlanEntry . "." . $port;
  2950. }
  2951. $port_vlan = get_snmp($ip, $community, $version, $port_oid);
  2952. $port_vlan = preg_replace('/.*\:/', '', $port_vlan);
  2953. $port_vlan = intval(trim($port_vlan));
  2954. return $port_vlan;
  2955. }
  2956. function get_ports_poe_state($vendor_id, $ip, $community = 'public', $version = '2')
  2957. {
  2958. if (!isset($vendor_id)) {
  2959. return;
  2960. }
  2961. if (!isset($ip)) {
  2962. return;
  2963. }
  2964. // default poe oid
  2965. $poe_status = PETH_PSE_PORT_ADMIN_ENABLE;
  2966. if ($vendor_id == 3) { $poe_status = HUAWEI_POE_OID; }
  2967. if ($vendor_id == 6) { $poe_status = SNR_POE_OID; }
  2968. if ($vendor_id == 8) { $poe_status = ALLIED_POE_OID; }
  2969. if ($vendor_id == 9) { $poe_status = MIKROTIK_POE_OID; }
  2970. if ($vendor_id == 10) { $poe_status = NETGEAR_POE_OID; }
  2971. if ($vendor_id == 15) { $poe_status = HP_POE_OID; }
  2972. if ($vendor_id == 69) { $poe_status = TPLINK_POE_OID; }
  2973. $result = [];
  2974. $c_state = walk_snmp($ip, $community, $version, $poe_status);
  2975. if (isset($c_state) and !empty($c_state)) {
  2976. foreach ($c_state as $key => $value) {
  2977. if (empty($value)) { $value = ''; }
  2978. $key = trim($key);
  2979. $value = parse_snmp_value($value);
  2980. $port = NULL;
  2981. if (preg_match('/\.(\d{1,10})$/', $key, $matches)) {
  2982. $port = preg_replace('/^\./', '', $matches[0]);
  2983. $result[$port] = $value;
  2984. // patch for mikrotik
  2985. if ($vendor_id == 9) {
  2986. if ($value == 1) { $result[$port]=2; }
  2987. if ($value > 1) { $result[$port]=1; }
  2988. }
  2989. //patch for tplink
  2990. if ($vendor_id == 69) {
  2991. if ($value == 0) { $result[$port]=2; }
  2992. if ($value >= 1) { $result[$port]=1; }
  2993. }
  2994. }
  2995. }
  2996. }
  2997. return $result;
  2998. }
  2999. function get_port_poe_state($vendor_id, $port, $port_snmp_index, $ip, $community = 'public', $version = '2')
  3000. {
  3001. if (!isset($port)) {
  3002. return;
  3003. }
  3004. if (!isset($ip)) {
  3005. return;
  3006. }
  3007. // default poe oid
  3008. $poe_status = PETH_PSE_PORT_ADMIN_ENABLE . "." . $port_snmp_index;
  3009. if ($vendor_id == 3) {
  3010. $poe_status = HUAWEI_POE_OID . "." . $port_snmp_index;
  3011. }
  3012. if ($vendor_id == 6) {
  3013. $poe_status = SNR_POE_OID . "." . $port_snmp_index;
  3014. }
  3015. if ($vendor_id == 8) {
  3016. $poe_status = ALLIED_POE_OID . "." . $port_snmp_index;
  3017. }
  3018. if ($vendor_id == 15) {
  3019. $poe_status = HP_POE_OID . "." . $port_snmp_index;
  3020. }
  3021. if ($vendor_id == 9) {
  3022. $poe_status = MIKROTIK_POE_OID . "." . $port_snmp_index;
  3023. }
  3024. if ($vendor_id == 10) {
  3025. $poe_status = NETGEAR_POE_OID . "." . $port_snmp_index;
  3026. }
  3027. if ($vendor_id == 69) {
  3028. $poe_status = TPLINK_POE_OID . "." . $port;
  3029. }
  3030. $result = '';
  3031. $c_state = get_snmp($ip, $community, $version, $poe_status);
  3032. if (isset($c_state) and !empty($c_state)) {
  3033. $p_state = parse_snmp_value($c_state);
  3034. if (empty($p_state)) { return NULL; }
  3035. // patch for mikrotik
  3036. if ($vendor_id == 9) {
  3037. if ($p_state == 1) {
  3038. return 2;
  3039. }
  3040. if ($p_state > 1) {
  3041. return 1;
  3042. }
  3043. }
  3044. //patch for tplink
  3045. if ($vendor_id == 69) {
  3046. if ($p_state == 0) {
  3047. return 2;
  3048. }
  3049. if ($p_state >= 1) {
  3050. return 1;
  3051. }
  3052. }
  3053. return $p_state;
  3054. }
  3055. return NULL;
  3056. }
  3057. function set_port_poe_state($vendor_id, $port, $port_snmp_index, $ip, $community='public', $version='2', $state)
  3058. {
  3059. if (!isset($ip)) {
  3060. return;
  3061. }
  3062. //default poe status
  3063. $poe_enable = 1;
  3064. $poe_disable = 2;
  3065. // default poe oid
  3066. $poe_status = PETH_PSE_PORT_ADMIN_ENABLE . "." . $port_snmp_index;
  3067. if ($vendor_id == 3) {
  3068. $poe_status = HUAWEI_POE_OID . "." . $port_snmp_index;
  3069. }
  3070. if ($vendor_id == 8) {
  3071. $poe_status = ALLIED_POE_OID . "." . $port_snmp_index;
  3072. }
  3073. if ($vendor_id == 15) {
  3074. $poe_status = HP_POE_OID . "." . $port_snmp_index;
  3075. }
  3076. if ($vendor_id == 10) {
  3077. $poe_status = NETGEAR_POE_OID . "." . $port_snmp_index;
  3078. }
  3079. if ($vendor_id == 69) {
  3080. $poe_status = TPLINK_POE_OID . "." . $port;
  3081. $poe_enable = 1;
  3082. $poe_disable = 0;
  3083. }
  3084. if ($state) {
  3085. // enable port
  3086. $c_state = set_snmp($ip, $community, $version, $poe_status, 'i', $poe_enable);
  3087. return $c_state;
  3088. } else {
  3089. // disable port
  3090. $c_state = set_snmp($ip, $community, $version, $poe_status, 'i', $poe_disable);
  3091. return $c_state;
  3092. }
  3093. }
  3094. function get_ports_poe_detail($vendor_id, $ip, $community='public', $version='2')
  3095. {
  3096. if (!isset($vendor_id)) {
  3097. return;
  3098. }
  3099. if (!isset($ip)) {
  3100. return;
  3101. }
  3102. $result = [];
  3103. $poe_class = PETH_PSE_PORT_POE_CLASS;
  3104. // eltex
  3105. if ($vendor_id == 2) {
  3106. $poe_power = ELTEX_POE_USAGE;
  3107. $poe_current = ELTEX_POE_CURRENT;
  3108. $poe_volt = ELTEX_POE_VOLT;
  3109. }
  3110. // huawei
  3111. if ($vendor_id == 3) {
  3112. $poe_power = HUAWEI_POE_USAGE;
  3113. $poe_current = HUAWEI_POE_CURRENT;
  3114. $poe_volt = HUAWEI_POE_VOLT;
  3115. }
  3116. // snr
  3117. if ($vendor_id == 6) {
  3118. $poe_class = SNR_POE_CLASS;
  3119. $poe_power = SNR_POE_USAGE;
  3120. $poe_current = SNR_POE_CURRENT;
  3121. $poe_volt = SNR_POE_VOLT;
  3122. }
  3123. // AT
  3124. if ($vendor_id == 8) {
  3125. $poe_power = ALLIED_POE_USAGE;
  3126. $poe_current = ALLIED_POE_CURRENT;
  3127. $poe_volt = ALLIED_POE_VOLT;
  3128. }
  3129. // mikrotik
  3130. if ($vendor_id == 9) {
  3131. $poe_power = MIKROTIK_POE_USAGE;
  3132. $poe_current = MIKROTIK_POE_CURRENT;
  3133. $poe_volt = MIKROTIK_POE_VOLT;
  3134. }
  3135. // netgear
  3136. if ($vendor_id == 10) {
  3137. $poe_power = NETGEAR_POE_USAGE;
  3138. $poe_current = NETGEAR_POE_CURRENT;
  3139. $poe_volt = NETGEAR_POE_VOLT;
  3140. }
  3141. // HP
  3142. if ($vendor_id == 15) {
  3143. $poe_power = HP_POE_USAGE;
  3144. $poe_volt = HP_POE_VOLT;
  3145. }
  3146. // TP-Link
  3147. if ($vendor_id == 69) {
  3148. $poe_power = TPLINK_POE_USAGE;
  3149. $poe_current = TPLINK_POE_CURRENT;
  3150. $poe_volt = TPLINK_POE_VOLT;
  3151. $poe_class = TPLINK_POE_CLASS;
  3152. }
  3153. if (isset($poe_power)) {
  3154. $c_power = walk_snmp($ip, $community, $version, $poe_power);
  3155. if (isset($c_power)) {
  3156. foreach ($c_power as $key => $value) {
  3157. if (empty($value)) { $value = 'INT:0'; }
  3158. $key = trim($key);
  3159. $p_power = parse_snmp_value($value);
  3160. $port = NULL;
  3161. if (preg_match('/\.(\d{1,10})$/', $key, $matches)) {
  3162. $port = preg_replace('/^\./', '', $matches[0]);
  3163. $result[$port]['power']=0;
  3164. $result[$port]['power_display']='';
  3165. switch ($vendor_id) {
  3166. case 9://mikrotik
  3167. $p_power = round($p_power / 10, 2);
  3168. break;
  3169. case 69://tplink
  3170. $p_power = round($p_power / 10, 2);
  3171. break;
  3172. default:
  3173. $p_power = round($p_power / 1000, 2);
  3174. break;
  3175. }
  3176. if ($p_power > 0) {
  3177. $result[$port]['power'] = $p_power;
  3178. $result[$port]['power_display'] = 'P: ' . $p_power . ' W';
  3179. }
  3180. }
  3181. }
  3182. }
  3183. }
  3184. if (isset($poe_current)) {
  3185. $c_current = walk_snmp($ip, $community, $version, $poe_current);
  3186. if (isset($c_current)) {
  3187. foreach ($c_current as $key => $value) {
  3188. if (empty($value)) { $value = 'INT:0'; }
  3189. $key = trim($key);
  3190. $p_current = parse_snmp_value($value);
  3191. $port = NULL;
  3192. if (preg_match('/\.(\d{1,10})$/', $key, $matches)) {
  3193. $port = preg_replace('/^\./', '', $matches[0]);
  3194. $result[$port]['current']=0;
  3195. $result[$port]['current_display']='';
  3196. if ($p_current > 0) {
  3197. $result[$port]['current'] = $p_current;
  3198. $result[$port]['current_display'] = 'C: ' . $p_current . ' mA';
  3199. }
  3200. }
  3201. }
  3202. }
  3203. }
  3204. if (isset($poe_volt)) {
  3205. $c_volt = walk_snmp($ip, $community, $version, $poe_volt);
  3206. if (isset($c_volt)) {
  3207. foreach ($c_volt as $key => $value) {
  3208. if (empty($value)) { $value = 'INT:0'; }
  3209. $key = trim($key);
  3210. $p_volt = parse_snmp_value($value);
  3211. $port = NULL;
  3212. if (preg_match('/\.(\d{1,10})$/', $key, $matches)) {
  3213. $port = preg_replace('/^\./', '', $matches[0]);
  3214. $result[$port]['volt'] = 0;
  3215. $result[$port]['volt_display'] ='';
  3216. switch ($vendor_id) {
  3217. case 2:
  3218. case 8:
  3219. $p_volt = round($p_volt / 1000, 2);
  3220. break;
  3221. case 9:
  3222. case 69:
  3223. $p_volt = round($p_volt / 10, 2);
  3224. break;
  3225. case 15:
  3226. $p_volt = round($p_volt / 100, 2);
  3227. break;
  3228. }
  3229. if ($p_volt > 0 and $result[$port]['power'] > 0) {
  3230. $result[$port]['volt'] = $p_volt;
  3231. $result[$port]['volt_display'] = ' V: ' . $p_volt . " V";
  3232. }
  3233. }
  3234. }
  3235. }
  3236. }
  3237. if (isset($poe_class)) {
  3238. $c_class = walk_snmp($ip, $community, $version, $poe_class);
  3239. if (isset($c_class)) {
  3240. foreach ($c_class as $key => $value) {
  3241. if (empty($value)) { $value = 'INT:0'; }
  3242. $key = trim($key);
  3243. $p_class = parse_snmp_value($value);
  3244. $port = NULL;
  3245. if (preg_match('/\.(\d{1,10})$/', $key, $matches)) {
  3246. $port = preg_replace('/^\./', '', $matches[0]);
  3247. $result[$port]['class'] = 0;
  3248. $result[$port]['class_display']='';
  3249. switch ($vendor_id) {
  3250. case 69:
  3251. if ($p_class > 0 and $result[$port]['power'] > 0) {
  3252. if ($p_class == 7) { $p_class = 'class-not-defined'; }
  3253. $result[$port]['class_display'] = 'Class: ' . $p_class;
  3254. $result[$port]['class'] = $p_class;
  3255. }
  3256. break;
  3257. default:
  3258. if ($p_class > 0 and $result[$port]['power'] > 0) {
  3259. $result[$port]['class_display'] = 'Class: ' . ($p_class - 1);
  3260. $result[$port]['class'] = $p_class-1;
  3261. }
  3262. break;
  3263. }
  3264. }
  3265. }
  3266. }
  3267. }
  3268. foreach ($result as &$port) {
  3269. if (!isset($port['power'])) { $port['power'] = 0; }
  3270. if (!isset($port['current'])) { $port['current'] = 0; }
  3271. if (!isset($port['volt'])) { $port['volt'] = 0; }
  3272. if (!isset($port['class'])) { $port['class'] = 0; }
  3273. }
  3274. unset($port);
  3275. return $result;
  3276. }
  3277. function get_port_poe_detail($vendor_id, $port, $port_snmp_index, $ip, $community, $version)
  3278. {
  3279. if (!isset($port) or !isset($port_snmp_index)) {
  3280. return;
  3281. }
  3282. if (!isset($ip)) {
  3283. return;
  3284. }
  3285. if (!isset($community)) {
  3286. $community = 'public';
  3287. }
  3288. if (!isset($version)) {
  3289. $version = '2';
  3290. }
  3291. $result = '';
  3292. $poe_class = PETH_PSE_PORT_POE_CLASS . $port_snmp_index;
  3293. // eltex
  3294. if ($vendor_id == 2) {
  3295. $poe_power = ELTEX_POE_USAGE . '.' . $port_snmp_index;
  3296. $poe_current = ELTEX_POE_CURRENT . '.' . $port_snmp_index;
  3297. $poe_volt = ELTEX_POE_VOLT . '.' . $port_snmp_index;
  3298. }
  3299. // huawei
  3300. if ($vendor_id == 3) {
  3301. $poe_power = HUAWEI_POE_USAGE . '.' . $port_snmp_index;
  3302. $poe_current = HUAWEI_POE_CURRENT . '.' . $port_snmp_index;
  3303. $poe_volt = HUAWEI_POE_VOLT . '.' . $port_snmp_index;
  3304. }
  3305. // snr
  3306. if ($vendor_id == 6) {
  3307. $poe_class = SNR_POE_CLASS . '.' . $port_snmp_index;
  3308. $poe_power = SNR_POE_USAGE . '.' . $port_snmp_index;
  3309. $poe_current = SNR_POE_CURRENT . '.' . $port_snmp_index;
  3310. $poe_volt = SNR_POE_VOLT . '.' . $port_snmp_index;
  3311. }
  3312. // AT
  3313. if ($vendor_id == 8) {
  3314. $poe_power = ALLIED_POE_USAGE . '.' . $port_snmp_index;
  3315. $poe_current = ALLIED_POE_CURRENT . '.' . $port_snmp_index;
  3316. $poe_volt = ALLIED_POE_VOLT . '.' . $port_snmp_index;
  3317. }
  3318. // mikrotik
  3319. if ($vendor_id == 9) {
  3320. $poe_power = MIKROTIK_POE_USAGE . '.' . $port_snmp_index;
  3321. $poe_current = MIKROTIK_POE_CURRENT . '.' . $port_snmp_index;
  3322. $poe_volt = MIKROTIK_POE_VOLT . '.' . $port_snmp_index;
  3323. }
  3324. // netgear
  3325. if ($vendor_id == 10) {
  3326. $poe_power = NETGEAR_POE_USAGE . '.' . $port_snmp_index;
  3327. $poe_current = NETGEAR_POE_CURRENT . '.' . $port_snmp_index;
  3328. $poe_volt = NETGEAR_POE_VOLT . '.' . $port_snmp_index;
  3329. }
  3330. // HP
  3331. if ($vendor_id == 15) {
  3332. $poe_power = HP_POE_USAGE . '.' . $port_snmp_index;
  3333. $poe_volt = HP_POE_VOLT . '.' . $port_snmp_index;
  3334. }
  3335. // TP-Link
  3336. if ($vendor_id == 69) {
  3337. $poe_power = TPLINK_POE_USAGE . '.' . $port;
  3338. $poe_current = TPLINK_POE_CURRENT . '.' . $port;
  3339. $poe_volt = TPLINK_POE_VOLT . '.' . $port;
  3340. $poe_class = TPLINK_POE_CLASS . "." . $port;
  3341. }
  3342. if (isset($poe_power)) {
  3343. $c_power = get_snmp($ip, $community, $version, $poe_power);
  3344. if (isset($c_power)) {
  3345. $p_power = parse_snmp_value($c_power);
  3346. switch ($vendor_id) {
  3347. case 9:
  3348. $p_power = round($p_power / 10, 2);
  3349. break;
  3350. case 69:
  3351. $p_power = round($p_power / 10, 2);
  3352. break;
  3353. default:
  3354. $p_power = round($p_power / 1000, 2);
  3355. break;
  3356. }
  3357. if ($p_power > 0) {
  3358. $result .= ' P: ' . $p_power . ' W';
  3359. }
  3360. }
  3361. }
  3362. if (isset($poe_current)) {
  3363. $c_current = get_snmp($ip, $community, $version, $poe_current);
  3364. if (isset($c_current)) {
  3365. $p_current = parse_snmp_value($c_current);
  3366. if ($p_current > 0) {
  3367. $result .= ' C: ' . $p_current . ' mA';
  3368. }
  3369. }
  3370. }
  3371. if (isset($poe_volt)) {
  3372. $c_volt = get_snmp($ip, $community, $version, $poe_volt);
  3373. if (isset($c_volt)) {
  3374. $p_volt = parse_snmp_value($c_volt);
  3375. switch ($vendor_id) {
  3376. case 2:
  3377. case 8:
  3378. $p_volt = round($p_volt / 1000, 2);
  3379. break;
  3380. case 9:
  3381. case 69:
  3382. $p_volt = round($p_volt / 10, 2);
  3383. break;
  3384. case 15:
  3385. $p_volt = round($p_volt / 100, 2);
  3386. break;
  3387. }
  3388. if ($p_volt > 0 and $p_power > 0) {
  3389. $result .= ' V: ' . $p_volt . " V";
  3390. }
  3391. }
  3392. }
  3393. if (isset($poe_class)) {
  3394. $c_class = get_snmp($ip, $community, $version, $poe_class);
  3395. if (isset($c_class)) {
  3396. $p_class = parse_snmp_value($c_class);
  3397. switch ($vendor_id) {
  3398. case 69:
  3399. if ($p_class > 0 and $p_power > 0) {
  3400. if ($p_class == 7) {
  3401. $p_class = 'class-not-defined';
  3402. }
  3403. $result .= ' Class: ' . $p_class;
  3404. }
  3405. break;
  3406. default:
  3407. if ($p_class > 0 and $p_power > 0) {
  3408. $result .= ' Class: ' . ($p_class - 1);
  3409. }
  3410. break;
  3411. }
  3412. }
  3413. }
  3414. return $result;
  3415. }
  3416. function get_snmp($ip, $community, $version, $oid)
  3417. {
  3418. snmp_set_oid_output_format(SNMP_OID_OUTPUT_NUMERIC);
  3419. $result = NULL;
  3420. try {
  3421. if ($version == 2) {
  3422. $result = snmp2_get($ip, $community, $oid, SNMP_timeout, SNMP_retry);
  3423. }
  3424. if ($version == 1) {
  3425. $result = snmpget($ip, $community, $oid, SNMP_timeout, SNMP_retry);
  3426. }
  3427. if (empty($result)) { $result = NULL; }
  3428. } catch (Exception $e) {
  3429. # echo 'Caught exception: ', $e->getMessage(), "\n";
  3430. $result = NULL;
  3431. }
  3432. return $result;
  3433. }
  3434. function set_snmp($ip, $community, $version, $oid, $field, $value)
  3435. {
  3436. $result = false;
  3437. try {
  3438. if ($version == 2) {
  3439. $result = snmp2_set($ip, $community, $oid, $field, $value, SNMP_timeout, SNMP_retry);
  3440. }
  3441. if ($version == 1) {
  3442. $result = snmpset($ip, $community, $oid, $field, $value, SNMP_timeout, SNMP_retry);
  3443. }
  3444. } catch (Exception $e) {
  3445. # echo 'Caught exception: ', $e->getMessage(), "\n";
  3446. $result = false;
  3447. }
  3448. return $result;
  3449. }
  3450. function set_port_state($vendor_id, $port, $ip, $community, $version, $state)
  3451. {
  3452. // port -> snmp_index!!!
  3453. if (!isset($port)) {
  3454. return;
  3455. }
  3456. if (!isset($ip)) {
  3457. return;
  3458. }
  3459. if (!isset($community)) {
  3460. $community = 'public';
  3461. }
  3462. if (!isset($version)) {
  3463. $version = '2';
  3464. }
  3465. $port_status = PORT_ADMIN_STATUS_OID .'.' . $port;
  3466. if ($state == 1) {
  3467. // enable port
  3468. $c_state = set_snmp($ip, $community, $version, $port_status, 'i', 1);
  3469. return $c_state;
  3470. } else {
  3471. // disable port
  3472. $c_state = set_snmp($ip, $community, $version, $port_status, 'i', 2);
  3473. return $c_state;
  3474. }
  3475. }
  3476. function set_port_for_group($db, $group_id, $place_id, $state)
  3477. {
  3478. $authSQL = 'SELECT User_auth.id,User_auth.dns_name,User_auth.ip FROM User_auth, User_list WHERE User_auth.user_id = User_list.id AND User_auth.deleted=0 and User_list.ou_id=' . $group_id;
  3479. $auth_list = mysqli_query($db, $authSQL);
  3480. LOG_VERBOSE($db, 'Mass port state change started!');
  3481. // get auth list for group
  3482. while (list($a_id, $a_name, $a_ip) = mysqli_fetch_array($auth_list)) {
  3483. // get device and port for auth
  3484. if ($place_id == 0) {
  3485. $place_filter = '';
  3486. } else {
  3487. $place_filter = 'D.building_id=' . $place_id . ' and ';
  3488. }
  3489. $devSQL = 'SELECT D.id, D.device_name, D.vendor_id, D.device_model, D.ip, D.snmp_version, D.rw_community, DP.port, DP.snmp_index FROM devices AS D, device_ports AS DP, connections AS C WHERE ' . $place_filter . ' D.id = DP.device_id AND DP.id = C.port_id AND C.auth_id=' . $a_id . ' LIMIT 1';
  3490. $dev_info = mysqli_query($db, $devSQL);
  3491. list($d_id, $d_name, $d_vendor_id, $d_model, $d_ip, $d_snmp, $d_community, $d_port, $d_snmp_index) = mysqli_fetch_array($dev_info);
  3492. if (!isset($d_id)) {
  3493. continue;
  3494. }
  3495. if ($state == 1) {
  3496. $mode = 'enable';
  3497. run_sql($db, "Update User_auth set nagios_handler='restart-port' WHERE id=$a_id and nagios_handler='manual-mode'");
  3498. } else {
  3499. $mode = 'disable';
  3500. run_sql($db, "Update User_auth set nagios_handler='manual-mode' WHERE id=$a_id and nagios_handler='restart-port'");
  3501. }
  3502. LOG_INFO($db, "At device $d_name [$d_ip] $mode port $d_port for auth_id: $a_id ($a_ip [$a_name])");
  3503. set_port_state($d_vendor_id, $d_snmp_index, $d_ip, $d_community, $d_snmp, $state);
  3504. set_port_poe_state($d_vendor_id, $d_port, $d_snmp_index, $d_ip, $d_community, $d_snmp, $state);
  3505. }
  3506. LOG_VERBOSE($db, 'Mass port state change stopped.');
  3507. }
  3508. function get_vendor($db, $mac)
  3509. {
  3510. $mac = mac_dotted($mac);
  3511. $mac5 = substr($mac, 0, 14);
  3512. $mac4 = substr($mac, 0, 11);
  3513. $mac3 = substr($mac, 0, 8);
  3514. $vendor = get_record_sql($db, 'SELECT companyName,companyAddress FROM mac_vendors WHERE oui="' . $mac . '"');
  3515. if (empty($vendor)) {
  3516. $vendor = get_record_sql($db, 'SELECT companyName,companyAddress FROM mac_vendors WHERE oui="' . $mac5 . '"');
  3517. }
  3518. if (empty($vendor)) {
  3519. $vendor = get_record_sql($db, 'SELECT companyName,companyAddress FROM mac_vendors WHERE oui="' . $mac4 . '"');
  3520. }
  3521. if (empty($vendor)) {
  3522. $vendor = get_record_sql($db, 'SELECT companyName,companyAddress FROM mac_vendors WHERE oui="' . $mac3 . '"');
  3523. }
  3524. $result = '';
  3525. if (!empty($vendor)) {
  3526. $result = $vendor['companyName'];
  3527. if (!empty($vendor['companyAddress'])) {
  3528. $result = $vendor['companyAddress'];
  3529. }
  3530. }
  3531. return $result;
  3532. }
  3533. function get_ports_state_detail($ip, $community='public', $version='2') {
  3534. if (!isset($ip)) {
  3535. return;
  3536. }
  3537. $result = [];
  3538. //post status
  3539. $p_state = walk_snmp($ip, $community, $version, PORT_STATUS_OID);
  3540. if (!empty($p_state)) {
  3541. foreach ($p_state as $key => $value) {
  3542. if (empty($value)) { $value = ''; }
  3543. $key = trim($key);
  3544. $value = parse_snmp_value($value);
  3545. $port = NULL;
  3546. if (preg_match('/\.(\d{1,10})$/', $key, $matches)) {
  3547. $port = preg_replace('/^\./', '', $matches[0]);
  3548. $result[$port]['status']=$value;
  3549. $result[$port]['admin_status']=0;
  3550. $result[$port]['speed']=0;
  3551. $result[$port]['errors']=0;
  3552. }
  3553. }
  3554. }
  3555. //admin state
  3556. $p_admin = walk_snmp($ip, $community, $version, PORT_ADMIN_STATUS_OID);
  3557. if (!empty($p_admin)) {
  3558. foreach ($p_admin as $key => $value) {
  3559. if (empty($value)) { $value = ''; }
  3560. $key = trim($key);
  3561. $value = parse_snmp_value($value);
  3562. $port = NULL;
  3563. if (preg_match('/\.(\d{1,10})$/', $key, $matches)) {
  3564. $port = preg_replace('/^\./', '', $matches[0]);
  3565. $result[$port]['admin_status']=$value;
  3566. }
  3567. }
  3568. }
  3569. //port speed
  3570. $p_speed = walk_snmp($ip,$community,$version,PORT_SPEED_OID);
  3571. if (!empty($p_speed)) {
  3572. foreach ($p_speed as $key => $value) {
  3573. if (empty($value)) { $value = 'INT:0'; }
  3574. $key = trim($key);
  3575. $value = parse_snmp_value($value);
  3576. $port = NULL;
  3577. if (preg_match('/\.(\d{1,10})$/', $key, $matches)) {
  3578. $port = preg_replace('/^\./', '', $matches[0]);
  3579. $result[$port]['speed']=$value;
  3580. }
  3581. }
  3582. }
  3583. //errors at
  3584. $p_errors = walk_snmp($ip,$community,$version,PORT_ERRORS_OID);
  3585. if (!empty($p_errors)) {
  3586. foreach ($p_errors as $key => $value) {
  3587. if (empty($value)) { $value = 'INT:0'; }
  3588. $key = trim($key);
  3589. $value = parse_snmp_value($value);
  3590. $port = NULL;
  3591. if (preg_match('/\.(\d{1,10})$/', $key, $matches)) {
  3592. $port = preg_replace('/^\./', '', $matches[0]);
  3593. $result[$port]['errors']=$value;
  3594. }
  3595. }
  3596. }
  3597. return $result;
  3598. }
  3599. function get_port_state_detail($port, $ip, $community, $version)
  3600. {
  3601. if (!isset($port)) {
  3602. return;
  3603. }
  3604. if (!isset($ip)) {
  3605. return;
  3606. }
  3607. if (!isset($community)) {
  3608. $community = 'public';
  3609. }
  3610. if (!isset($version)) {
  3611. $version = '2';
  3612. }
  3613. // if (!is_up($ip)) { return; }
  3614. $oper = PORT_STATUS_OID .'.'. $port;
  3615. $admin = PORT_ADMIN_STATUS_OID .'.' . $port;
  3616. $speed = PORT_SPEED_OID .'.'. $port;
  3617. $errors = PORT_ERRORS_OID .'.'. $port;
  3618. $result = '';
  3619. $c_state = get_snmp($ip, $community, $version, $oper);
  3620. $p_state = parse_snmp_value($c_state);
  3621. $c_admin = get_snmp($ip, $community, $version, $admin);
  3622. $p_admin = parse_snmp_value($c_admin);
  3623. if ($p_state == 1) {
  3624. $c_speed = get_snmp($ip, $community, $version, $speed);
  3625. } else {
  3626. $c_speed = 'INT:0';
  3627. }
  3628. $p_speed = parse_snmp_value($c_speed);
  3629. $c_errors = get_snmp($ip, $community, $version, $errors);
  3630. $p_errors = parse_snmp_value($c_errors);
  3631. $result = $p_state . ";" . $p_admin . ";" . $p_speed . ";" . $p_errors;
  3632. return $result;
  3633. }
  3634. function parse_snmp_value($value)
  3635. {
  3636. if (empty($value)) {
  3637. return NULL;
  3638. }
  3639. if (!preg_match('/:/',$value)) { return NULL; }
  3640. list($p_type, $p_value) = explode(':', $value);
  3641. if (empty($p_value)) { return NULL; }
  3642. $p_value = trim($p_value);
  3643. $p_value = preg_replace('/^\"/', '', $p_value);
  3644. $p_value = preg_replace('/\"$/', '', $p_value);
  3645. $p_value = trim($p_value);
  3646. return $p_value;
  3647. }
  3648. function strHexToBin ( $number ) {
  3649. $result = '';
  3650. for ( $i = 0; $i < strlen($number); $i++ ){
  3651. $conv = base_convert($number[$i], 16, 2);
  3652. $result .= str_pad($conv, 4, '0', STR_PAD_LEFT);
  3653. }
  3654. return $result;
  3655. }
  3656. function dec_to_hex($mac)
  3657. {
  3658. if (!isset($mac)) {
  3659. return;
  3660. }
  3661. $mac_array = explode('.', $mac);
  3662. for ($i = 0; $i < count($mac_array); $i++) {
  3663. $hex_i = dechex($mac_array[$i]);
  3664. if (strlen($hex_i) == 1) {
  3665. $hex_i = "0" . $hex_i;
  3666. }
  3667. $mac_array[$i] = $hex_i;
  3668. }
  3669. $hex_mac = implode(':', $mac_array);
  3670. return $hex_mac;
  3671. }
  3672. function mac_simplify($mac)
  3673. {
  3674. if (!isset($mac)) {
  3675. return;
  3676. }
  3677. $mac = strtolower(trim($mac));
  3678. $mac = preg_replace('/(\.|:|-)/', '', $mac);
  3679. return $mac;
  3680. }
  3681. function mac_dotted($mac)
  3682. {
  3683. if (!isset($mac)) {
  3684. return;
  3685. }
  3686. $mac = mac_simplify($mac);
  3687. $mac = preg_replace('/(\S{2})(\S{2})?(\S{2})?(\S{2})?(\S{2})?(\S{2})?/', '$1:$2:$3:$4:$5:$6', $mac);
  3688. $mac = preg_replace('/\:+$/','',$mac,5);
  3689. return $mac;
  3690. }
  3691. function unbind_ports($db, $device_id)
  3692. {
  3693. $target = mysqli_query($db, "SELECT U.target_port_id,U.id FROM device_ports U WHERE U.device_id=$device_id");
  3694. while (list($target_id, $id) = mysqli_fetch_array($target)) {
  3695. run_sql($db, "UPDATE device_ports SET target_port_id=0 WHERE target_port_id=" . $id);
  3696. run_sql($db, "UPDATE device_ports SET target_port_id=0 WHERE id=" . $id);
  3697. }
  3698. }
  3699. function bind_ports($db, $port_id, $target_id)
  3700. {
  3701. $old_target = mysqli_query($db, "SELECT U.target_port_id FROM device_ports U WHERE U.id=$port_id");
  3702. list($old_target_id) = mysqli_fetch_array($old_target);
  3703. // unbind current connection
  3704. $new['target_port_id'] = 0;
  3705. update_record($db, "device_ports", "id='$port_id'", $new);
  3706. if (isset($old_target_id)) {
  3707. update_record($db, "device_ports", "id='$old_target_id'", $new);
  3708. }
  3709. // new link
  3710. if (isset($target_id) and $target_id > 0) {
  3711. $new['target_port_id'] = $target_id;
  3712. update_record($db, "device_ports", "id='$port_id'", $new);
  3713. $new['target_port_id'] = $port_id;
  3714. update_record($db, "device_ports", "id='$target_id'", $new);
  3715. }
  3716. }
  3717. function expand_device_name($db, $name)
  3718. {
  3719. $device_id = get_device_id($db, $name);
  3720. $result = $name;
  3721. if (isset($device_id) and $device_id > 0) {
  3722. $result = '<a href=/admin/devices/editdevice.php?id=' . $device_id . '>' . $name . '</a>';
  3723. }
  3724. return $result;
  3725. }
  3726. function expand_mac($db, $msg)
  3727. {
  3728. if (!isset($msg)) {
  3729. return;
  3730. }
  3731. $mac = mac_dotted($msg);
  3732. $vendor_info = get_vendor($db, $mac);
  3733. $result = ' <p title="' . $vendor_info . '"><a href=/admin/logs/mac.php?mac=' . $mac . '>' . $mac . '</a></p>';
  3734. return $result;
  3735. }
  3736. function expand_log_str($db, $msg)
  3737. {
  3738. if (!isset($msg)) {
  3739. return;
  3740. }
  3741. $auth_pattern = '/(auth_id:|auth|auth id:|auth id)\s+(\d+)\s+/i';
  3742. $auth_replace = '<a href=/admin/users/editauth.php?id=${2}>auth_id:${2}</a> ';
  3743. $result = preg_replace($auth_pattern, $auth_replace, $msg);
  3744. $user_pattern = '/(user_id:|user|user id:|user id)\s+(\d+)\s+/i';
  3745. $user_replace = '<a href=/admin/users/edituser.php?id=${2}>user_id:${2}</a> ';
  3746. $result = preg_replace($user_pattern, $user_replace, $result);
  3747. $mac_pattern = '/\s+\[(\w{12})\]\s+/i';
  3748. preg_match($mac_pattern, $result, $matches);
  3749. if (isset($matches[1])) {
  3750. $mac = $matches[1];
  3751. $mac = mac_dotted($mac);
  3752. # $vendor_info = get_vendor($db,$mac);
  3753. # $mac_replace = ' <p title="'.$vendor_info.'"><a href=/admin/logs/mac.php?mac='.$mac.'>'.$mac.'</a></p>';
  3754. $mac_replace = ' <a href=/admin/logs/mac.php?mac=' . $mac . '>' . $mac . '</a> ';
  3755. $result = preg_replace($mac_pattern, $mac_replace, $result);
  3756. }
  3757. $mac_pattern = '/\s+mac:\s+([\w\:]{17})$/i';
  3758. preg_match($mac_pattern, $result, $matches);
  3759. if (isset($matches[1])) {
  3760. $mac = $matches[1];
  3761. $mac = mac_dotted($mac);
  3762. # $vendor_info = get_vendor($db,$mac);
  3763. # $mac_replace = ' mac: <p title="'.$vendor_info.'"><a href=/admin/logs/mac.php?mac='.$mac.'>'.$mac.'</a></p>';
  3764. $mac_replace = ' mac: <a href=/admin/logs/mac.php?mac=' . $mac . '>' . $mac . '</a> ';
  3765. $result = preg_replace($mac_pattern, $mac_replace, $result);
  3766. }
  3767. $device_pattern = '/at device\s+([\w\.\-]+)/i';
  3768. preg_match($device_pattern, $result, $matches);
  3769. if (isset($matches[1])) {
  3770. $device_name = $matches[1];
  3771. $device_id = get_device_id($db, $device_name);
  3772. if (isset($device_id) and $device_id > 0) {
  3773. $device_replace = 'at device <a href=/admin/devices/editdevice.php?id=' . $device_id . '>${1}</a> ';
  3774. $result = preg_replace($device_pattern, $device_replace, $result);
  3775. }
  3776. }
  3777. $device_pattern = '/(device_id:|device id:|device id|device_id)\s+(\d+)\s+/i';
  3778. $device_replace = 'device_id: <a href=/admin/devices/editdevice.php?id=${2}>${2}</a> ';
  3779. $result = preg_replace($device_pattern, $device_replace, $result);
  3780. $ip_pattern = '/\s+ip\:\s+(\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})\s+/i';
  3781. preg_match($ip_pattern, $result, $matches);
  3782. if (isset($matches[1])) {
  3783. $ip = $matches[1];
  3784. $auth_id = get_auth_by_ip($db, $ip);
  3785. if (isset($auth_id) and $auth_id > 0) {
  3786. $auth_replace = ' ip: <a href=/admin/users/editauth.php?id=' . $auth_id . '>${1}</a> ';
  3787. $result = preg_replace($ip_pattern, $auth_replace, $result);
  3788. }
  3789. }
  3790. return $result;
  3791. }
  3792. function get_record_field($db, $table, $field, $filter)
  3793. {
  3794. if (!isset($table)) {
  3795. LOG_ERROR($db, "Search in unknown table! Skip command.");
  3796. return;
  3797. }
  3798. if (!isset($filter)) {
  3799. LOG_ERROR($db, "Search filter is empty! Skip command.");
  3800. return;
  3801. }
  3802. if (!isset($field)) {
  3803. LOG_ERROR($db, "Search field is empty! Skip command.");
  3804. return;
  3805. }
  3806. if (preg_match('/=$/', $filter)) {
  3807. LOG_ERROR($db, "Search record ($table) with illegal filter $filter! Skip command.");
  3808. return;
  3809. }
  3810. $old_sql = "SELECT $field FROM $table WHERE $filter LIMIT 1";
  3811. $old_record = mysqli_query($db, $old_sql) or LOG_ERROR($db, "SQL: $old_sql :" . mysqli_error($db));
  3812. $old = mysqli_fetch_array($old_record, MYSQLI_ASSOC);
  3813. foreach ($old as $key => $value) {
  3814. if (!isset($value) or $value === 'NULL') {
  3815. $value = '';
  3816. }
  3817. $result[$key] = $value;
  3818. }
  3819. return $result[$field];
  3820. }
  3821. function get_record($db, $table, $filter)
  3822. {
  3823. if (!isset($table)) {
  3824. LOG_ERROR($db, "Search in unknown table! Skip command.");
  3825. return;
  3826. }
  3827. if (!isset($filter)) {
  3828. LOG_ERROR($db, "Search filter is empty! Skip command.");
  3829. return;
  3830. }
  3831. if (preg_match('/=$/', $filter)) {
  3832. LOG_ERROR($db, "Search record ($table) with illegal filter $filter! Skip command.");
  3833. return;
  3834. }
  3835. $get_sql = "SELECT * FROM $table WHERE $filter LIMIT 1";
  3836. $get_record = mysqli_query($db, $get_sql);
  3837. if (!$get_record) {
  3838. LOG_ERROR($db, "SQL: $get_sql :" . mysqli_error($db));
  3839. return;
  3840. }
  3841. $fields = [];
  3842. while ($field = mysqli_fetch_field($get_record)) {
  3843. $f_table = $field->table;
  3844. $f_name = $field->name;
  3845. $fields[$f_table][$f_name]=$field;
  3846. }
  3847. $record = mysqli_fetch_array($get_record, MYSQLI_ASSOC);
  3848. $result = NULL;
  3849. if (!empty($record)) {
  3850. foreach ($record as $key => $value) {
  3851. if (!isset($value) or $value === 'NULL' or $value == NULL) {
  3852. if (!empty($key) and !empty($fields[$table]) and !empty($fields[$table][$key])) {
  3853. if (in_array($fields[$table][$key]->type,MYSQL_FIELD_DIGIT)) { $value = 0; }
  3854. if (in_array($fields[$table][$key]->type,MYSQL_FIELD_STRING)) { $value = ''; }
  3855. }
  3856. }
  3857. if (!empty($key)) { $result[$key] = $value; }
  3858. }
  3859. }
  3860. return $result;
  3861. }
  3862. function get_records($db, $table, $filter)
  3863. {
  3864. if (!isset($table)) {
  3865. LOG_ERROR($db, "Search in unknown table! Skip command.");
  3866. return;
  3867. }
  3868. if (isset($filter) and preg_match('/=$/', $filter)) {
  3869. LOG_ERROR($db, "Search record ($table) with illegal filter $filter! Skip command.");
  3870. return;
  3871. }
  3872. $s_filter = '';
  3873. if (isset($filter)) {
  3874. $s_filter = 'WHERE ' . $filter;
  3875. }
  3876. $get_sql = "SELECT * FROM $table $s_filter";
  3877. $get_record = mysqli_query($db, $get_sql);
  3878. if (!$get_record) {
  3879. LOG_ERROR($db, "SQL: $get_sql :" . mysqli_error($db));
  3880. return;
  3881. }
  3882. $fields = [];
  3883. while ($field = mysqli_fetch_field($get_record)) {
  3884. $f_table = $field->table;
  3885. $f_name = $field->name;
  3886. $fields[$f_table][$f_name]=$field;
  3887. }
  3888. $result = NULL;
  3889. $index = 0;
  3890. while ($rec = mysqli_fetch_array($get_record, MYSQLI_ASSOC)) {
  3891. foreach ($rec as $key => $value) {
  3892. if (!isset($value) or $value === 'NULL' or $value == NULL) {
  3893. if (!empty($key) and !empty($fields[$table]) and !empty($fields[$table][$key])) {
  3894. if (in_array($fields[$table][$key]->type,MYSQL_FIELD_DIGIT)) { $value = 0; }
  3895. if (in_array($fields[$table][$key]->type,MYSQL_FIELD_STRING)) { $value = ''; }
  3896. }
  3897. }
  3898. $result[$index][$key] = $value;
  3899. }
  3900. $index++;
  3901. }
  3902. return $result;
  3903. }
  3904. function get_records_sql($db, $sql)
  3905. {
  3906. $result = NULL;
  3907. if (empty($sql)) {
  3908. LOG_ERROR($db, "Empty query! Skip command.");
  3909. return $result;
  3910. }
  3911. $records = mysqli_query($db, $sql);
  3912. if (!$records) {
  3913. LOG_ERROR($db, "SQL: $sql :" . mysqli_error($db));
  3914. return $result;
  3915. }
  3916. $fields = [];
  3917. //we assume that fields with the same name have the same type
  3918. while ($field = mysqli_fetch_field($records)) {
  3919. $f_name = $field->name;
  3920. $fields[$f_name]=$field;
  3921. }
  3922. $index = 0;
  3923. while ($rec = mysqli_fetch_array($records, MYSQLI_ASSOC)) {
  3924. foreach ($rec as $key => $value) {
  3925. if (!isset($value) or $value === 'NULL' or $value == NULL) {
  3926. if (!empty($key) and !empty($fields[$key])) {
  3927. if (in_array($fields[$key]->type,MYSQL_FIELD_DIGIT)) { $value = 0; }
  3928. if (in_array($fields[$key]->type,MYSQL_FIELD_STRING)) { $value = ''; }
  3929. }
  3930. }
  3931. if (!empty($key)) { $result[$index][$key] = $value; }
  3932. }
  3933. $index++;
  3934. }
  3935. return $result;
  3936. }
  3937. function get_record_sql($db, $sql)
  3938. {
  3939. $result = NULL;
  3940. if (!isset($sql)) {
  3941. LOG_ERROR($db, "Empty query! Skip command.");
  3942. return $result;
  3943. }
  3944. $record = mysqli_query($db, $sql . " LIMIT 1");
  3945. if (!isset($record)) {
  3946. LOG_ERROR($db, "SQL: $sql LIMIT 1: " . mysqli_error($db));
  3947. return $result;
  3948. }
  3949. $fields = [];
  3950. //we assume that fields with the same name have the same type
  3951. while ($field = mysqli_fetch_field($record)) {
  3952. $f_name = $field->name;
  3953. $fields[$f_name]=$field;
  3954. }
  3955. $rec = mysqli_fetch_array($record, MYSQLI_ASSOC);
  3956. if (!empty($rec)) {
  3957. foreach ($rec as $key => $value) {
  3958. if (!isset($value) or $value === 'NULL' or $value == NULL) {
  3959. if (!empty($key) and !empty($fields[$key])) {
  3960. if (in_array($fields[$key]->type,MYSQL_FIELD_DIGIT)) { $value = 0; }
  3961. if (in_array($fields[$key]->type,MYSQL_FIELD_STRING)) { $value = ''; }
  3962. }
  3963. }
  3964. if (!empty($key)) { $result[$key] = $value; }
  3965. }
  3966. }
  3967. return $result;
  3968. }
  3969. function is_auth_bind_changed($db, $id, $ip, $mac)
  3970. {
  3971. $old_sql = "SELECT ip,mac FROM User_auth WHERE id=$id";
  3972. $old_record = get_record_sql($db, $old_sql);
  3973. if (empty($old_record["ip"]) or empty($old_record["mac"])) {
  3974. return 0;
  3975. }
  3976. if ($old_record["ip"] !== $ip or $old_record["mac"] !== $mac) {
  3977. LOG_VERBOSE($db, "Changed ip or mac for auth record!");
  3978. return 1;
  3979. }
  3980. return 0;
  3981. }
  3982. function copy_auth($db, $id, $new_auth)
  3983. {
  3984. $old_record = get_record_sql($db, "SELECT * FROM User_auth WHERE id=$id");
  3985. delete_record($db, "User_auth", "id=" . $id);
  3986. $new_auth["user_id"] = $old_record["user_id"];
  3987. $new_auth["changed"] = 1;
  3988. $changed_time = GetNowTimeString();
  3989. $new_auth["changed_time"] = $changed_time;
  3990. $new_id = insert_record($db, "User_auth", $new_auth);
  3991. LOG_VERBOSE($db, "Old record with id: $id deleted. Created new auth record for new ip+mac id: $new_id!");
  3992. return $new_id;
  3993. }
  3994. function get_dns_name($db,$id)
  3995. {
  3996. $auth_record = get_record_sql($db,"SELECT dns_name FROM User_auth WHERE id=".$id);
  3997. if (!empty($auth_record) and !empty($auth_record['dns_name'])) { return $auth_record['dns_name']; }
  3998. return '';
  3999. }
  4000. function update_record($db, $table, $filter, $newvalue)
  4001. {
  4002. if (!isset($table)) {
  4003. LOG_WARNING($db, "Change record for unknown table! Skip command.");
  4004. return;
  4005. }
  4006. if (!isset($filter)) {
  4007. LOG_WARNING($db, "Change record ($table) with empty filter! Skip command.");
  4008. return;
  4009. }
  4010. if (preg_match('/=$/', $filter)) {
  4011. LOG_WARNING($db, "Change record ($table) with illegal filter $filter! Skip command.");
  4012. return;
  4013. }
  4014. if (!isset($newvalue)) {
  4015. LOG_WARNING($db, "Change record ($table [ $filter ]) with empty data! Skip command.");
  4016. return;
  4017. }
  4018. if (!allow_update($table,'update')) {
  4019. LOG_INFO($db,"Access denied: $table [ $filter ]");
  4020. return 1;
  4021. }
  4022. $old_sql = "SELECT * FROM $table WHERE $filter";
  4023. $old_record = mysqli_query($db, $old_sql) or LOG_ERROR($db, "SQL: $old_sql :" . mysqli_error($db));
  4024. $old = mysqli_fetch_array($old_record, MYSQLI_ASSOC);
  4025. $rec_id = NULL;
  4026. if (!empty($old['id'])) { $rec_id = $old['id']; }
  4027. $changed_log = '';
  4028. $run_sql = '';
  4029. $network_changed = 0;
  4030. $dhcp_changed = 0;
  4031. $dns_changed = 0;
  4032. $acl_fields = [
  4033. 'ip' => '1',
  4034. 'ip_int' => '1',
  4035. 'enabled' => '1',
  4036. 'dhcp' => '1',
  4037. 'filter_group_id' => '1',
  4038. 'deleted' => '1',
  4039. 'dhcp_acl' => '1',
  4040. 'queue_id' => '1',
  4041. 'mac' => '1',
  4042. 'blocked' => '1',
  4043. ];
  4044. $dhcp_fields = [
  4045. 'ip' => '1',
  4046. 'dhcp' => '1',
  4047. 'deleted' => '1',
  4048. 'mac' => '1',
  4049. ];
  4050. $dns_fields = [
  4051. 'ip' => '1',
  4052. 'dns_name' => '1',
  4053. 'alias' => '1',
  4054. ];
  4055. foreach ($newvalue as $key => $value) {
  4056. if (!allow_update($table,'update',$key)) { continue; }
  4057. if (!isset($value)) {
  4058. $value = '';
  4059. }
  4060. $value = trim($value);
  4061. if (strcmp($old[$key], $value) == 0) {
  4062. continue;
  4063. }
  4064. if ($table === "User_auth") {
  4065. if (!empty($acl_fields["$key"])) {
  4066. $network_changed = 1;
  4067. }
  4068. if (!empty($dhcp_fields["$key"])) {
  4069. $dhcp_changed = 1;
  4070. }
  4071. if (!empty($dns_fields["$key"])) {
  4072. $dns_changed = 1;
  4073. }
  4074. }
  4075. if ($table === "User_auth_alias") {
  4076. if (!empty($dns_fields["$key"])) {
  4077. $dns_changed = 1;
  4078. }
  4079. }
  4080. if (!preg_match('/password/i',$key)) {
  4081. $changed_log = $changed_log . " $key => $value (old: $old[$key]),";
  4082. }
  4083. $run_sql = $run_sql . " `" . $key . "`='" . mysqli_real_escape_string($db, $value) . "',";
  4084. }
  4085. if ($table === "User_auth" and $dns_changed) {
  4086. if (!empty($old['dns_name']) and !empty($old['ip'])) {
  4087. $del_dns['name_type']='A';
  4088. $del_dns['name']=$old['dns_name'];
  4089. $del_dns['value']=$old['ip'];
  4090. $del_dns['type']='del';
  4091. if (!empty($rec_id)) { $del_dns['auth_id']=$rec_id; }
  4092. insert_record($db,'dns_queue',$del_dns);
  4093. }
  4094. if (!empty($newvalue['dns_name']) and !empty($newvalue['ip'])) {
  4095. $new_dns['name_type']='A';
  4096. $new_dns['name']=$newvalue['dns_name'];
  4097. $new_dns['value']=$newvalue['ip'];
  4098. $new_dns['type']='add';
  4099. if (!empty($rec_id)) { $new_dns['auth_id']=$rec_id; }
  4100. insert_record($db,'dns_queue',$new_dns);
  4101. }
  4102. }
  4103. if ($table === "User_auth_alias" and $dns_changed) {
  4104. $auth_id = NULL;
  4105. if ($old['auth_id']) { $auth_id = $old['auth_id']; }
  4106. if (!empty($old['alias'])) {
  4107. $del_dns['name_type']='CNAME';
  4108. $del_dns['name']=$old['alias'];
  4109. $del_dns['type']='del';
  4110. if (!empty($auth_id)) {
  4111. $del_dns['auth_id']=$auth_id;
  4112. $del_dns['value']=get_dns_name($db,$auth_id);
  4113. }
  4114. insert_record($db,'dns_queue',$del_dns);
  4115. }
  4116. if (!empty($newvalue['alias'])) {
  4117. $new_dns['name_type']='CNAME';
  4118. $new_dns['name']=$newvalue['alias'];
  4119. $new_dns['type']='add';
  4120. if (!empty($auth_id)) {
  4121. $new_dns['auth_id']=$auth_id;
  4122. $new_dns['value']=get_dns_name($db,$auth_id);
  4123. }
  4124. insert_record($db,'dns_queue',$new_dns);
  4125. }
  4126. }
  4127. if (empty($run_sql)) {
  4128. return 1;
  4129. }
  4130. if ($network_changed) {
  4131. $run_sql = $run_sql . " `changed`='1',";
  4132. }
  4133. if ($dhcp_changed) {
  4134. $run_sql = $run_sql . " `dhcp_changed`='1',";
  4135. }
  4136. $changed_log = substr_replace($changed_log, "", -1);
  4137. $run_sql = substr_replace($run_sql, "", -1);
  4138. if ($table === 'User_auth') {
  4139. $changed_time = GetNowTimeString();
  4140. $run_sql = $run_sql . ", `changed_time`='" . $changed_time . "'";
  4141. }
  4142. $new_sql = "UPDATE $table SET $run_sql WHERE $filter";
  4143. LOG_DEBUG($db, "Run sql: $new_sql");
  4144. $sql_result = mysqli_query($db, $new_sql) or LOG_ERROR($db, "SQL: $new_sql :" . mysqli_error($db));
  4145. if (!$sql_result) {
  4146. LOG_ERROR($db, "UPDATE Request: $new_sql :" . mysqli_error($db));
  4147. return;
  4148. }
  4149. if ($table !== "sessions") {
  4150. LOG_VERBOSE($db, "Change table $table WHERE $filter set $changed_log");
  4151. }
  4152. return $sql_result;
  4153. }
  4154. function delete_record($db, $table, $filter)
  4155. {
  4156. if (!allow_update($table,'del')) {
  4157. LOG_WARNING($db, "User does not have write permission");
  4158. return;
  4159. }
  4160. if (!isset($table)) {
  4161. LOG_WARNING($db, "Delete FROM unknown table! Skip command.");
  4162. return;
  4163. }
  4164. if (!isset($filter)) {
  4165. LOG_WARNING($db, "Delete FROM table $table with empty filter! Skip command.");
  4166. return;
  4167. }
  4168. if (preg_match('/=$/', $filter)) {
  4169. LOG_WARNING($db, "Change record ($table) with illegal filter $filter! Skip command.");
  4170. return;
  4171. }
  4172. $old_sql = "SELECT * FROM $table WHERE $filter";
  4173. $old_record = mysqli_query($db, $old_sql) or LOG_ERROR($db, "SQL: $old_sql :" . mysqli_error($db));
  4174. $old = mysqli_fetch_array($old_record, MYSQLI_ASSOC);
  4175. $rec_id = NULL;
  4176. if (!empty($old['id'])) { $rec_id = $old['id']; }
  4177. $changed_log = 'record: ';
  4178. if (!empty($old)) {
  4179. foreach ($old as $key => $value) {
  4180. if (!isset($value)) {
  4181. $value = '';
  4182. }
  4183. $changed_log = $changed_log . " $key => $value,";
  4184. }
  4185. }
  4186. $delete_it = 1;
  4187. //never delete user ip record or dns alias record
  4188. if ($table === 'User_auth') {
  4189. $delete_it = 0;
  4190. $changed_time = GetNowTimeString();
  4191. $new_sql = "UPDATE $table SET deleted=1, changed=1, `changed_time`='" . $changed_time . "' WHERE $filter";
  4192. LOG_DEBUG($db, "Run sql: $new_sql");
  4193. $sql_result = mysqli_query($db, $new_sql) or LOG_ERROR($db, "SQL: $new_sql :" . mysqli_error($db));
  4194. if (!$sql_result) {
  4195. LOG_ERROR($db, "UPDATE Request (from delete): " . mysqli_error($db));
  4196. return;
  4197. }
  4198. //dns
  4199. if (!empty($old['dns_name']) and !empty($old['ip'])) {
  4200. $del_dns['name_type']='A';
  4201. $del_dns['name']=$old['dns_name'];
  4202. $del_dns['value']=$old['ip'];
  4203. $del_dns['type']='del';
  4204. if (!empty($rec_id)) { $del_dns['auth_id']=$rec_id; }
  4205. insert_record($db,'dns_queue',$del_dns);
  4206. }
  4207. }
  4208. if ($table === 'User_auth_alias') {
  4209. //dns
  4210. if (!empty($old['alias'])) {
  4211. $del_dns['name_type']='CNAME';
  4212. $del_dns['name']=$old['alias'];
  4213. $del_dns['value']='';
  4214. $del_dns['type']='del';
  4215. if (!empty($old['auth_id'])) {
  4216. $del_dns['auth_id']=$old['auth_id'];
  4217. $del_dns['value']=get_dns_name($db,$old['auth_id']);
  4218. }
  4219. insert_record($db,'dns_queue',$del_dns);
  4220. }
  4221. }
  4222. if ($delete_it) {
  4223. $new_sql = "DELETE FROM $table WHERE $filter";
  4224. LOG_DEBUG($db, "Run sql: $new_sql");
  4225. $sql_result = mysqli_query($db, $new_sql) or LOG_ERROR($db, "SQL: $new_sql :" . mysqli_error($db));
  4226. if (!$sql_result) {
  4227. LOG_ERROR($db, "DELETE Request: $new_sql : " . mysqli_error($db));
  4228. return;
  4229. }
  4230. }
  4231. if ($table !== "sessions") {
  4232. LOG_VERBOSE($db, "Delete FROM table $table WHERE $filter $changed_log");
  4233. }
  4234. return $changed_log;
  4235. }
  4236. function insert_record($db, $table, $newvalue)
  4237. {
  4238. if (!allow_update($table,'add')) {
  4239. LOG_WARNING($db, "User does not have write permission");
  4240. return;
  4241. }
  4242. if (!isset($table)) {
  4243. LOG_WARNING($db, "Create record for unknown table! Skip command.");
  4244. return;
  4245. }
  4246. if (empty($newvalue)) {
  4247. LOG_WARNING($db, "Create record ($table) with empty data! Skip command.");
  4248. return;
  4249. }
  4250. $changed_log = '';
  4251. $field_list = '';
  4252. $value_list = '';
  4253. foreach ($newvalue as $key => $value) {
  4254. if (empty($value) and $value != '0') {
  4255. $value = '';
  4256. }
  4257. if (!preg_match('/password/i',$key)) {
  4258. $changed_log = $changed_log . " $key => $value,";
  4259. }
  4260. $field_list = $field_list . "`" . $key . "`,";
  4261. $value = trim($value);
  4262. $value_list = $value_list . "'" . mysqli_real_escape_string($db, $value) . "',";
  4263. }
  4264. if (empty($value_list)) {
  4265. return;
  4266. }
  4267. $changed_log = substr_replace($changed_log, "", -1);
  4268. $field_list = substr_replace($field_list, "", -1);
  4269. $value_list = substr_replace($value_list, "", -1);
  4270. $new_sql = "insert into $table(" . $field_list . ") values(" . $value_list . ")";
  4271. LOG_DEBUG($db, "Run sql: $new_sql");
  4272. $sql_result = mysqli_query($db, $new_sql) or LOG_ERROR($db, "SQL: $new_sql :" . mysqli_error($db));
  4273. if (!$sql_result) {
  4274. LOG_ERROR($db, "INSERT Request:" . mysqli_error($db));
  4275. return;
  4276. }
  4277. $last_id = mysqli_insert_id($db);
  4278. if ($table !== "sessions") {
  4279. LOG_VERBOSE($db, "Create record in table $table: $changed_log with id: $last_id");
  4280. }
  4281. if ($table === 'User_auth') {
  4282. run_sql($db, "UPDATE User_auth SET changed=1, dhcp_changed=1 WHERE id=" . $last_id);
  4283. }
  4284. if ($table === 'User_auth_alias') {
  4285. //dns
  4286. if (!empty($newvalue['alias'])) {
  4287. $add_dns['name_type']='CNAME';
  4288. $add_dns['name']=$newvalue['alias'];
  4289. $add_dns['value']=get_dns_name($db,$newvalue['auth_id']);
  4290. $add_dns['type']='add';
  4291. $add_dns['auth_id']=$newvalue['auth_id'];
  4292. insert_record($db,'dns_queue',$add_dns);
  4293. }
  4294. }
  4295. if ($table === 'User_auth') {
  4296. //dns
  4297. if (!empty($newvalue['dns_name']) and !empty($newvalue['ip'])) {
  4298. $add_dns['name_type']='A';
  4299. $add_dns['name']=$newvalue['dns_name'];
  4300. $add_dns['value']=$newvalue['ip'];
  4301. $add_dns['type']='add';
  4302. $add_dns['auth_id']=$last_id;
  4303. insert_record($db,'dns_queue',$add_dns);
  4304. }
  4305. }
  4306. return $last_id;
  4307. }
  4308. function dump_record($db,$table,$filter)
  4309. {
  4310. $result = '';
  4311. $old = get_record($db,$table,$filter);
  4312. if (empty($old)) { return $result; }
  4313. $result = 'record: '. get_rec_str($old);
  4314. return $result;
  4315. }
  4316. function get_rec_str($array)
  4317. {
  4318. $result = '';
  4319. foreach ($array as $key => $value) {
  4320. $result .= "[" . $key . "]=" . $value . ", ";
  4321. }
  4322. $result = preg_replace('/,\s+$/', '', $result);
  4323. return $result;
  4324. }
  4325. function get_diff_rec($db, $table, $filter, $newvalue, $only_changed)
  4326. {
  4327. if (!isset($table)) {
  4328. return;
  4329. }
  4330. if (!isset($filter)) {
  4331. return;
  4332. }
  4333. if (!isset($newvalue)) {
  4334. return;
  4335. }
  4336. if (!isset($only_changed)) {
  4337. $only_changed = 0;
  4338. }
  4339. $old_sql = "SELECT * FROM $table WHERE $filter";
  4340. $old_record = mysqli_query($db, $old_sql) or LOG_ERROR($db, "SQL: $old_sql :" . mysqli_error($db));
  4341. $old = mysqli_fetch_array($old_record, MYSQLI_ASSOC);
  4342. $changed_log = "\r\n";
  4343. foreach ($newvalue as $key => $value) {
  4344. if (strcmp($old[$key], $value) !== 0) {
  4345. $changed_log = $changed_log . " $key => cur: $value old: $old[$key],\r\n";
  4346. }
  4347. }
  4348. $old_record = '';
  4349. if (!$only_changed) {
  4350. $old_record = "\r\n Has not changed:\r\n";
  4351. foreach ($old as $key => $value) {
  4352. if (!empty($newvalue[$key])) {
  4353. $old_record = $old_record . " $key = $value,\r\n";
  4354. }
  4355. }
  4356. $old_record = substr_replace($old_record, "", -3);
  4357. }
  4358. // print $changed_log;
  4359. return $changed_log . $old_record;
  4360. }
  4361. function get_cacti_graph($host_ip, $port_index)
  4362. {
  4363. if (empty(get_const('cacti_url'))) {
  4364. return;
  4365. }
  4366. if (CACTI_DB_HOST == null or CACTI_DB_USER == null or CACTI_DB_PASS == null or CACTI_DB_NAME == null) {
  4367. return;
  4368. }
  4369. if (empty(CACTI_DB_HOST) or empty(CACTI_DB_USER) or empty(CACTI_DB_PASS) or empty(CACTI_DB_NAME)) {
  4370. return;
  4371. }
  4372. $cacti_db_link = new_connection(CACTI_DB_HOST, CACTI_DB_USER, CACTI_DB_PASS, CACTI_DB_NAME);
  4373. if (!$cacti_db_link) {
  4374. return FALSE;
  4375. }
  4376. $host_sql = 'SELECT * FROM host WHERE hostname="' . $host_ip . '"';
  4377. $cacti_host = get_record_sql($cacti_db_link, $host_sql);
  4378. $host_id = $cacti_host["id"];
  4379. if (empty($host_id)) {
  4380. return;
  4381. }
  4382. $graph_sql = 'SELECT * FROM graph_local WHERE host_id="' . $host_id . '" and snmp_index="' . $port_index . '" and graph_template_id IN (SELECT id FROM graph_templates WHERE name LIKE "Interface - Traffic%") ORDER BY id ASC';
  4383. $cacti_graph = get_record_sql($cacti_db_link, $graph_sql);
  4384. $graph_id = $cacti_graph["id"];
  4385. if (empty($graph_id)) {
  4386. return;
  4387. }
  4388. $result = get_const('cacti_url') . "/graph_image.php?local_graph_id=" . $graph_id;
  4389. return $result;
  4390. }
  4391. function print_select_item($description, $value, $current)
  4392. {
  4393. if ((string)$value === (string)$current) {
  4394. print "<option value=$value selected>$description</option>";
  4395. } else {
  4396. print "<option value=$value>$description</option>";
  4397. }
  4398. }
  4399. function print_select_simple($description, $value)
  4400. {
  4401. print "<option value=$value>$description</option>";
  4402. }
  4403. function print_select_item_ext($description, $value, $current, $disabled)
  4404. {
  4405. if ((string)$value === (string)$current) {
  4406. print "<option value=$value selected>$description</option>";
  4407. } else {
  4408. if (!$disabled) {
  4409. print "<option value=$value>$description</option>";
  4410. } else {
  4411. print "<option disabled value=$value>$description</option>";
  4412. }
  4413. }
  4414. }
  4415. function print_row_at_pages($name, $value)
  4416. {
  4417. print "<select name='" . $name . "'>\n";
  4418. print_select_item(WEB_select_item_more, pow(10, 10), $value);
  4419. print_select_item('25', 25, $value);
  4420. print_select_item('50', 50, $value);
  4421. print_select_item('100', 100, $value);
  4422. print_select_item('200', 200, $value);
  4423. print_select_item('500', 500, $value);
  4424. print_select_item('1000', 1000, $value);
  4425. print_select_item('2000', 2000, $value);
  4426. print "</select>\n";
  4427. }
  4428. function print_navigation($url, $page, $displayed, $count_records, $total)
  4429. {
  4430. if ($total <= 1) {
  4431. print "<div align=left class=records >";
  4432. print "| Total records: $count_records";
  4433. print "</div>";
  4434. return;
  4435. }
  4436. $v_char = "?";
  4437. if (preg_match('/\.php\?/', $url)) {
  4438. $v_char = "&";
  4439. }
  4440. #две назад
  4441. print "<div align=left class=records >";
  4442. if (($page - 2) > 0) :
  4443. $pagetwoleft = "<a class='first_page_link' href=" . $url . $v_char . "page=" . ($page - 2) . ">" . ($page - 2) . "</a> ";
  4444. else :
  4445. $pagetwoleft = null;
  4446. endif;
  4447. #одна назад
  4448. if (($page - 1) > 0) :
  4449. $pageoneleft = "<a class='first_page_link' href=" . $url . $v_char . "page=" . ($page - 1) . ">" . ($page - 1) . "</a> ";
  4450. $pagetemp = ($page - 1);
  4451. else :
  4452. $pageoneleft = null;
  4453. $pagetemp = null;
  4454. endif;
  4455. #две вперед
  4456. if (($page + 2) <= $total) :
  4457. $pagetworight = " <a class='first_page_link' href=" . $url . $v_char . "page=" . ($page + 2) . ">" . ($page + 2) . "</a>";
  4458. else :
  4459. $pagetworight = null;
  4460. endif;
  4461. #одна вперед
  4462. if (($page + 1) <= $total) :
  4463. $pageoneright = " <a class='first_page_link' href=" . $url . $v_char . "page=" . ($page + 1) . ">" . ($page + 1) . "</a>";
  4464. $pagetemp2 = ($page + 1);
  4465. else :
  4466. $pageoneright = null;
  4467. $pagetemp2 = null;
  4468. endif;
  4469. # в начало
  4470. if ($page != 1 && $pagetemp != 1 && $pagetemp != 2) :
  4471. $pagerevp = "<a href=" . $url . $v_char . "page=1 class='first_page_link' title='В начало'><<</a> ";
  4472. else :
  4473. $pagerevp = null;
  4474. endif;
  4475. #в конец (последняя)
  4476. if ($page != $total && $pagetemp2 != ($total - 1) && $pagetemp2 != $total) :
  4477. $nextp = " ... <a href=" . $url . $v_char . "page=" . $total . " class='first_page_link'>$total</a>";
  4478. else :
  4479. $nextp = null;
  4480. endif;
  4481. print $pagerevp . $pagetwoleft . $pageoneleft . '<span class="num_page_not_link"><b>' . $page . '</b></span>' . $pageoneright . $pagetworight . $nextp;
  4482. print " | Total records: $count_records";
  4483. print "</div>";
  4484. }
  4485. function get_option($db, $option_id)
  4486. {
  4487. $option = get_record($db, "config", "option_id=" . $option_id);
  4488. if (empty($option) or empty($option['value'])) {
  4489. $default = get_record($db, "config_options", "id=$option_id");
  4490. return $default['default_value'];
  4491. }
  4492. return $option['value'];
  4493. }
  4494. function is_option($db, $option_id)
  4495. {
  4496. $option = get_record($db, "config", "option_id=" . $option_id);
  4497. if (empty($option) or empty($option['value'])) {
  4498. return;
  4499. }
  4500. return 1;
  4501. }
  4502. function set_option($db, $option_id, $value)
  4503. {
  4504. $option['value'] = $value;
  4505. update_record($db, 'config', "option_id=$option_id", $option);
  4506. }
  4507. function is_subnet_aton($subnet, $ip)
  4508. {
  4509. if (!isset($subnet)) {
  4510. return 0;
  4511. }
  4512. if (!isset($ip)) {
  4513. return 0;
  4514. }
  4515. $range = cidrToRange($subnet);
  4516. if ($ip >= ip2long($range[0]) and $ip <= ip2long($range[1])) {
  4517. return 1;
  4518. }
  4519. return 0;
  4520. }
  4521. function get_new_user_id($db, $ip, $mac, $hostname)
  4522. {
  4523. $result['ip'] = $ip;
  4524. $result['mac'] = mac_dotted($mac);
  4525. $result['hostname'] = $hostname;
  4526. $result['user_id'] = NULL;
  4527. $result['ou_id'] = NULL;
  4528. $ip_aton = ip2long($ip);
  4529. //personal user rules
  4530. //ip
  4531. if (!empty($ip)) {
  4532. $t_rules = get_records_sql($db, "SELECT * FROM auth_rules WHERE type=1 and LENGTH(rule)>0 AND user_id IS NOT NULL");
  4533. foreach ($t_rules as $row) {
  4534. if (!empty($row['rule']) and is_subnet_aton($row['rule'], $ip_aton)) {
  4535. $result['user_id'] = $row['user_id'];
  4536. }
  4537. }
  4538. }
  4539. //mac
  4540. if (!empty($mac)) {
  4541. $mac_rules = get_records_sql($db, "SELECT * FROM auth_rules WHERE type=2 AND LENGTH(rule)>0 AND user_id IS NOT NULL");
  4542. foreach ($mac_rules as $row) {
  4543. $pattern = '/' . mac_simplify($row['rule']) . '/';
  4544. if (!empty($row['rule']) and preg_match($pattern, mac_simplify($mac))) {
  4545. $result['user_id'] = $row['user_id'];
  4546. }
  4547. }
  4548. }
  4549. //hostname
  4550. if (!empty($hostname)) {
  4551. $mac_rules = get_records_sql($db, "SELECT * FROM auth_rules WHERE type=3 AND LENGTH(rule)>0 AND user_id IS NOT NULL");
  4552. foreach ($mac_rules as $row) {
  4553. if (!empty($row['rule']) and preg_match($row['rule'], $hostname)) {
  4554. $result['user_id'] = $row['user_id'];
  4555. }
  4556. }
  4557. }
  4558. if (!empty($result['user_id'])) {
  4559. return $result;
  4560. }
  4561. //ou rules
  4562. //ip
  4563. if (!empty($ip)) {
  4564. if (is_hotspot($db, $ip)) {
  4565. $result['ou_id'] = get_const('default_hotspot_ou_id');
  4566. }
  4567. $t_rules = get_records_sql($db, "SELECT * FROM auth_rules WHERE type=1 and LENGTH(rule)>0 AND ou_id IS NOT NULL");
  4568. foreach ($t_rules as $row) {
  4569. if (!empty($row['rule']) and is_subnet_aton($row['rule'], $ip_aton)) {
  4570. $result['ou_id'] = $row['ou_id'];
  4571. }
  4572. }
  4573. }
  4574. //mac
  4575. if (!empty($mac)) {
  4576. $mac_rules = get_records_sql($db, "SELECT * FROM auth_rules WHERE type=2 AND LENGTH(rule)>0 AND ou_id IS NOT NULL");
  4577. foreach ($mac_rules as $row) {
  4578. $pattern = '/' . mac_simplify($row['rule']) . '/';
  4579. if (!empty($row['rule']) and preg_match($pattern, mac_simplify($mac))) {
  4580. $result['ou_id'] = $row['ou_id'];
  4581. }
  4582. }
  4583. }
  4584. //hostname
  4585. if (!empty($hostname)) {
  4586. $mac_rules = get_records_sql($db, "SELECT * FROM auth_rules WHERE type=3 AND LENGTH(rule)>0 AND ou_id IS NOT NULL");
  4587. foreach ($mac_rules as $row) {
  4588. if (!empty($row['rule']) and preg_match($row['rule'], $hostname)) {
  4589. $result['ou_id'] = $row['ou_id'];
  4590. }
  4591. }
  4592. }
  4593. if (empty($result['ou_id'])) {
  4594. $result['ou_id'] = get_const('default_user_ou_id');
  4595. }
  4596. return $result;
  4597. }
  4598. function get_subnet_range($db, $subnet_id)
  4599. {
  4600. if (empty($subnet_id)) {
  4601. return;
  4602. }
  4603. $t_option = get_record_sql($db, "SELECT ip_int_start,ip_int_stop FROM `subnets` WHERE id=$subnet_id");
  4604. if (!isset($t_option['ip_int_start'])) {
  4605. $t_option['ip_int_start'] = 0;
  4606. }
  4607. if (!isset($t_option['ip_int_stop'])) {
  4608. $t_option['ip_int_stop'] = 0;
  4609. }
  4610. $subnet['start'] = $t_option['ip_int_start'];
  4611. $subnet['stop'] = $t_option['ip_int_stop'];
  4612. return $subnet;
  4613. }
  4614. function int_between($value, $start, $end) {
  4615. return in_array($value, range($start, $end));
  4616. }
  4617. function is_gray_network($ip)
  4618. {
  4619. if (empty($ip)) { return 0; }
  4620. $ip_aton = ip2long($ip);
  4621. $gray_nets = array('10.0.0.0/8','192.168.0.0/16','172.16.0.0/12','100.64.0.0/10');
  4622. foreach ($gray_nets as &$net) {
  4623. $net_cidr = cidrToRange($net);
  4624. if (int_between($ip_aton,ip2long($net_cidr[0]),ip2long($net_cidr[1]))) { return $net; }
  4625. }
  4626. return 0;
  4627. }
  4628. function is_hotspot($db, $ip)
  4629. {
  4630. if (!isset($ip)) {
  4631. return 0;
  4632. }
  4633. LOG_DEBUG($db, "Check hotspot network for ip: $ip");
  4634. $ip_aton = ip2long($ip);
  4635. $t_option = mysqli_query($db, "SELECT subnet,ip_int_start,ip_int_stop FROM `subnets` WHERE hotspot=1");
  4636. while (list($f_net, $f_start, $f_stop) = mysqli_fetch_array($t_option)) {
  4637. if ($ip_aton >= $f_start and $ip_aton <= $f_stop) {
  4638. LOG_DEBUG($db, "ip: $ip [$ip_aton] found in network $f_net: [" . $f_start . ".." . $f_stop . "]");
  4639. return 1;
  4640. }
  4641. }
  4642. LOG_DEBUG($db, "ip $ip not found in hotspot network!");
  4643. return 0;
  4644. }
  4645. function is_office($db, $ip)
  4646. {
  4647. if (!isset($ip)) {
  4648. return 0;
  4649. }
  4650. LOG_DEBUG($db, "Check office network for ip: $ip");
  4651. $ip_aton = ip2long($ip);
  4652. $t_option = mysqli_query($db, "SELECT subnet,ip_int_start,ip_int_stop FROM `subnets` WHERE office=1");
  4653. while (list($f_net, $f_start, $f_stop) = mysqli_fetch_array($t_option)) {
  4654. if ($ip_aton >= $f_start and $ip_aton <= $f_stop) {
  4655. LOG_DEBUG($db, "ip: $ip [$ip_aton] found in office $f_net: [" . $f_start . ".." . $f_stop . "]");
  4656. return 1;
  4657. }
  4658. }
  4659. LOG_DEBUG($db, "ip $ip not found in office network!");
  4660. return 0;
  4661. }
  4662. function is_our_network($db, $ip)
  4663. {
  4664. if (!isset($ip)) {
  4665. return 0;
  4666. }
  4667. if (is_hotspot($db, $ip)) {
  4668. return 1;
  4669. }
  4670. if (is_office($db, $ip)) {
  4671. return 1;
  4672. }
  4673. return 0;
  4674. }
  4675. function get_const($const_name)
  4676. {
  4677. global $config;
  4678. if (isset($config[$const_name])) {
  4679. return $config[$const_name];
  4680. }
  4681. return NULL;
  4682. }
  4683. function get_eye_version ($db)
  4684. {
  4685. $v_table=get_record_sql($db,"SELECT version FROM version");
  4686. if (!empty($v_table)) { return $v_table['version']; }
  4687. return NULL;
  4688. }
  4689. $config["org_name"] = get_option($db_link, 32);
  4690. $config["version"] = get_eye_version($db_link);
  4691. $config["KB"] = get_option($db_link, 1);
  4692. if ($config["KB"] == 0) {
  4693. $config["KB"] = 1000;
  4694. }
  4695. if ($config["KB"] == 1) {
  4696. $config["KB"] = 1024;
  4697. }
  4698. $config["debug"] = get_option($db_link, 34);
  4699. $config["log_level"] = get_option($db_link, 53);
  4700. if ($config["debug"]) {
  4701. $config["log_level"] = 255;
  4702. }
  4703. $config["send_email"] = get_option($db_link, 51);
  4704. $config["admin_email"] = get_option($db_link, 21);
  4705. $config["sender_email"] = get_option($db_link, 52);
  4706. $config["snmp_default_version"] = get_option($db_link, 9);
  4707. $config["snmp_default_community"] = get_option($db_link, 11);
  4708. $config["auto_mac_rule"] = get_option($db_link, 64);
  4709. $config["cacti_url"] = rtrim(get_option($db_link, 58), '/');
  4710. if (preg_match('/127.0.0.1/', $config["cacti_url"])) {
  4711. $config["cacti_url"] = NULL;
  4712. }
  4713. $config["nagios_url"] = rtrim(get_option($db_link, 57), '/') . '/cgi-bin/';
  4714. if (preg_match('/127.0.0.1/', $config["nagios_url"])) {
  4715. $config["nagios_url"] = NULL;
  4716. }
  4717. $config["torrus_url"] = rtrim(get_option($db_link, 59), '/') . '?nodeid=if//HOST_IP//IF_NAME////inoutbps';
  4718. if (preg_match('/127.0.0.1/', $config["torrus_url"])) {
  4719. $config["torrus_url"] = NULL;
  4720. }
  4721. $config["dns_server"]=get_option($db_link,3);
  4722. $config["dns_server_type"]=get_option($db_link,70);
  4723. $ou = get_record_sql($db_link, "SELECT id FROM OU WHERE default_users = 1");
  4724. if (empty($ou)) {
  4725. $config["default_user_ou_id"] = 0;
  4726. } else {
  4727. $config["default_user_ou_id"] = $ou['id'];
  4728. }
  4729. $ou = get_record_sql($db_link, "SELECT id FROM OU WHERE default_hotspot=1");
  4730. if (empty($ou)) {
  4731. $config["default_hotspot_ou_id"] = $config["default_user_ou_id"];
  4732. } else {
  4733. $config["default_hotspot_ou_id"] = $ou['id'];
  4734. }
  4735. $config["init"] = 1;
  4736. clean_dns_cache($db_link);
  4737. //clean_unreferensed_rules($db_link);
  4738. snmp_set_valueretrieval(SNMP_VALUE_LIBRARY);
  4739. snmp_set_enum_print(1);