common.php 170 KB

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