1
0

common.php 175 KB

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