common.php 167 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064
  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 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_device = mysqli_query($db, $d_sql);
  1296. while (list($f_ip, $f_int, $f_mac, $f_auth_id, $f_dns) = mysqli_fetch_array($t_device)) {
  1297. $name = $f_ip;
  1298. if (isset($f_dns) and $f_dns != '') {
  1299. $name = $f_dns;
  1300. }
  1301. if ($new_window) {
  1302. print "<a href=\"#\" onclick=\"".open_window_url("/admin/users/editauth.php?id=".$f_auth_id)." return false;\">" . $name . " [" . $f_ip . "]</a><br>";
  1303. } else {
  1304. print "<a href=/admin/users/editauth.php?id=".$f_auth_id.">" . $name . " [" . $f_ip . "]</a><br>";
  1305. }
  1306. }
  1307. }
  1308. function print_auth_simple($db, $auth_id)
  1309. {
  1310. $auth = get_record($db, "User_auth", "id=$auth_id");
  1311. $name = $auth['dns_name'];
  1312. if (empty($name)) {
  1313. $name = $auth['comments'];
  1314. }
  1315. if (empty($name)) {
  1316. $name = $auth['ip'];
  1317. }
  1318. print "<a href=\"/admin/users/editauth.php?id=$auth_id\">" . $name . "</a><br>";
  1319. }
  1320. function print_auth($db, $auth_id)
  1321. {
  1322. $auth = get_record($db, "User_auth", "id=$auth_id");
  1323. $name = $auth['dns_name'];
  1324. if (empty($name)) {
  1325. $name = $auth['comments'];
  1326. } else {
  1327. $name .= " (" . $auth['comments'] . ")";
  1328. }
  1329. if (empty($name)) {
  1330. $name = $auth['ip'];
  1331. } else {
  1332. $name .= " [" . $auth['ip'] . "]";
  1333. }
  1334. print "<a href=\"/admin/users/editauth.php?id=$auth_id\">" . $name . "</a><br>";
  1335. }
  1336. function print_auth_detail($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. $name .= " last: [" . $auth['last_found'] . "] ";
  1351. if ($auth['deleted'] == 1) {
  1352. $name .= " <font color='red'>DELETED!!!</font>";
  1353. }
  1354. print "<a href=\"/admin/users/editauth.php?id=$auth_id\">" . $name . "</a><br>";
  1355. }
  1356. function get_auth_port_count($db, $port_id)
  1357. {
  1358. $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";
  1359. $t_device = mysqli_query($db, $d_sql);
  1360. list($f_count) = mysqli_fetch_array($t_device);
  1361. if (!isset($f_count)) {
  1362. $f_count = 0;
  1363. }
  1364. return $f_count;
  1365. }
  1366. function get_connection($db, $auth_id)
  1367. {
  1368. $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";
  1369. $t_device = mysqli_query($db, $d_sql);
  1370. list($f_name, $f_port) = mysqli_fetch_array($t_device);
  1371. if (isset($f_name)) {
  1372. $result = expand_device_name($db, $f_name) . "[" . $f_port . "]";
  1373. } else {
  1374. $result = '';
  1375. }
  1376. return $result;
  1377. }
  1378. function get_connection_string($db, $auth_id)
  1379. {
  1380. $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";
  1381. $t_device = mysqli_query($db, $d_sql);
  1382. list($f_name, $f_port) = mysqli_fetch_array($t_device);
  1383. if (isset($f_name)) {
  1384. $result = $f_name . "[" . $f_port . "]";
  1385. } else {
  1386. $result = '';
  1387. }
  1388. return $result;
  1389. }
  1390. function get_port($db, $port_id)
  1391. {
  1392. $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";
  1393. $t_device = mysqli_query($db, $d_sql);
  1394. list($f_name, $f_port) = mysqli_fetch_array($t_device);
  1395. if (isset($f_name)) {
  1396. $result = expand_device_name($db, $f_name) . "[" . $f_port . "]";
  1397. } else {
  1398. $result = '';
  1399. }
  1400. return $result;
  1401. }
  1402. function print_option_select($db, $option_name)
  1403. {
  1404. print "<select name=\"$option_name\">\n";
  1405. $t_option = mysqli_query($db, "SELECT id,option_name FROM config_options WHERE uniq=0 order by option_name");
  1406. while (list($f_id, $f_name) = mysqli_fetch_array($t_option)) {
  1407. print "<option value=$f_id>$f_name</option>";
  1408. }
  1409. $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");
  1410. while (list($f_id, $f_name) = mysqli_fetch_array($t_option)) {
  1411. print "<option value=$f_id>$f_name</option>";
  1412. }
  1413. print "</select>\n";
  1414. }
  1415. function run_sql($db, $query)
  1416. {
  1417. if (preg_match('/^\s*(UPDATE|DELETE)/i', $query)) {
  1418. unset($matches);
  1419. preg_match('/FROM\s+(.*)\s+/i', $query, $matches);
  1420. if (!empty($matches[1])) {
  1421. if (!allow_update($matches[1],'del')) {
  1422. LOG_DEBUG($db, "Access denied: $query ");
  1423. return;
  1424. }
  1425. }
  1426. unset($matches);
  1427. preg_match('/INSERT\s+INTO\s+(.*)\s+/i', $query, $matches);
  1428. if (!empty($matches[1])) {
  1429. if (!allow_update($matches[1],'add')) {
  1430. LOG_DEBUG($db, "Access denied: $query ");
  1431. return;
  1432. }
  1433. }
  1434. unset($matches);
  1435. preg_match('/UPDATE\s+(.*)\s+/i', $query, $matches);
  1436. if (!empty($matches[1])) {
  1437. if (!allow_update($matches[1],'update')) {
  1438. LOG_DEBUG($db, "Access denied: $query ");
  1439. return;
  1440. }
  1441. }
  1442. unset($matches);
  1443. }
  1444. $sql_result = mysqli_query($db, $query);
  1445. if (!$sql_result) {
  1446. LOG_ERROR($db, "At simple SQL: $query :" . mysqli_error($db));
  1447. return;
  1448. }
  1449. return $sql_result;
  1450. }
  1451. function get_count_records($db, $table, $filter)
  1452. {
  1453. if (!empty($filter)) {
  1454. $filter = 'where ' . $filter;
  1455. }
  1456. $t_count = mysqli_query($db, "SELECT count(*) FROM $table $filter");
  1457. list($count) = mysqli_fetch_array($t_count);
  1458. if (!isset($count)) {
  1459. $count = 0;
  1460. }
  1461. return $count;
  1462. }
  1463. function get_id_record($db, $table, $filter)
  1464. {
  1465. if (isset($filter)) {
  1466. $filter = 'WHERE ' . $filter;
  1467. }
  1468. $t_record = mysqli_query($db, "SELECT id FROM $table $filter limit 1");
  1469. list($id) = mysqli_fetch_array($t_record);
  1470. return $id;
  1471. }
  1472. function set_changed($db, $id)
  1473. {
  1474. $auth['changed'] = 1;
  1475. update_record($db, "User_auth", "id=" . $id, $auth);
  1476. }
  1477. function ResolveIP($db, $ip_int)
  1478. {
  1479. $ip_name = "-";
  1480. if (empty($ip_int)) {
  1481. return $ip_name;
  1482. }
  1483. $dns_cache = get_record_sql($db, "SELECT * FROM dns_cache WHERE ip=$ip_int");
  1484. if (empty($dns_cache) or empty($dns_cache['dns'])) {
  1485. $ip_name = gethostbyaddr(long2ip($ip_int));
  1486. if (empty($ip_name) or $ip_name == long2ip($ip_int)) {
  1487. $ip_name = "-";
  1488. }
  1489. run_sql($db, "INSERT INTO dns_cache(dns,ip) VALUES('" . $ip_name . "'," . $ip_int . ")");
  1490. } else {
  1491. $ip_name = $dns_cache['dns'];
  1492. }
  1493. return $ip_name;
  1494. }
  1495. function clean_dns_cache($db)
  1496. {
  1497. $date = time();
  1498. $date = $date - 86400;
  1499. $date_clean = DateTimeImmutable::createFromFormat('U', $date);
  1500. $clean_date = $date_clean->format('Y-m-d H:i:s');
  1501. run_sql($db, "DELETE FROM dns_cache WHERE `timestamp`<='" . $clean_date . "'");
  1502. }
  1503. function clean_unreferensed_rules($db)
  1504. {
  1505. run_sql($db, "DELETE FROM `auth_rules` WHERE user_id NOT IN (SELECT id FROM User_list)");
  1506. }
  1507. function FormatDateStr($format = 'Y-m-d H:i:s', $date_str)
  1508. {
  1509. $date1 = GetDateTimeFromString($date_str);
  1510. $result = $date1->format($format);
  1511. return $result;
  1512. }
  1513. function GetDateTimeFromString($date_str)
  1514. {
  1515. if (!is_a($date_str, 'DateTime')) {
  1516. $t_date_str = urldecode($date_str);
  1517. $t_date_str = preg_replace('/(\'|\")/', '', $t_date_str);
  1518. $t_date_str = preg_replace('/T/', ' ', $t_date_str);
  1519. $date1 = DateTime::createFromFormat('Y-m-d H:i:s', $t_date_str);
  1520. if (!$date1) {
  1521. $date1 = DateTime::createFromFormat('Y.m.d H:i:s', $t_date_str);
  1522. }
  1523. if (!$date1) {
  1524. $date1 = DateTime::createFromFormat('Y/m/d H:i:s', $t_date_str);
  1525. }
  1526. if (!$date1) {
  1527. $date1 = DateTime::createFromFormat('Y-m-d H:i', $t_date_str);
  1528. }
  1529. if (!$date1) {
  1530. $date1 = DateTime::createFromFormat('Y.m.d H:i', $t_date_str);
  1531. }
  1532. if (!$date1) {
  1533. $date1 = DateTime::createFromFormat('Y/m/d H:i', $t_date_str);
  1534. }
  1535. if (!$date1) {
  1536. $date1 = DateTime::createFromFormat('Y-m-d|', $t_date_str);
  1537. }
  1538. if (!$date1) {
  1539. $date1 = DateTime::createFromFormat('Y.m.d|', $t_date_str);
  1540. }
  1541. if (!$date1) {
  1542. $date1 = DateTime::createFromFormat('Y/m/d|', $t_date_str);
  1543. }
  1544. if (!$date1) {
  1545. $date1 = new DateTime;
  1546. $date1->setTime(0, 0, 0, 1);
  1547. }
  1548. } else {
  1549. return $date_str;
  1550. }
  1551. return $date1;
  1552. }
  1553. function GetNowTimeString()
  1554. {
  1555. $now = new DateTimeImmutable('now');
  1556. $result = $now->format('Y-m-d H:i:s');
  1557. return $result;
  1558. }
  1559. function GetNowDayString()
  1560. {
  1561. $now = new DateTimeImmutable('now');
  1562. $result = $now->format('Y-m-d');
  1563. return $result;
  1564. }
  1565. function get_ip_subnet($db, $ip)
  1566. {
  1567. if (empty($ip)) {
  1568. return;
  1569. }
  1570. $ip_aton = ip2long($ip);
  1571. $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)");
  1572. if (empty($user_subnet)) {
  1573. return;
  1574. }
  1575. return $user_subnet;
  1576. }
  1577. function find_mac_in_subnet($db, $ip, $mac)
  1578. {
  1579. if (empty($ip)) {
  1580. return;
  1581. }
  1582. if (empty($mac)) {
  1583. return;
  1584. }
  1585. $ip_subnet = get_ip_subnet($db, $ip);
  1586. if (empty($ip_subnet)) {
  1587. return;
  1588. }
  1589. $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");
  1590. $auth_count = 0;
  1591. $result['count'] = 0;
  1592. $result['users_id'] = [];
  1593. foreach ($t_auth as $row) {
  1594. if (!empty($row['id'])) {
  1595. $auth_count++;
  1596. $result['count'] = $auth_count;
  1597. $result[$auth_count] = $row['id'];
  1598. array_push($result['users_id'], $row['user_id']);
  1599. }
  1600. }
  1601. return $result;
  1602. }
  1603. function apply_auth_rule($db, $auth_record, $user_id)
  1604. {
  1605. if (empty($auth_record)) { return; }
  1606. if (empty($user_id)) { return $auth_record; }
  1607. $user_rec = get_record($db, 'User_list', "id=" . $user_id);
  1608. if (empty($user_rec)) { return $auth_record; }
  1609. //set filter and status by user
  1610. $auth_record['ou_id'] = $user_rec['ou_id'];
  1611. $auth_record['user_id'] = $user_rec['id'];
  1612. $auth_record['filter_group_id'] = $user_rec['filter_group_id'];
  1613. $auth_record['queue_id'] = $user_rec['queue_id'];
  1614. $auth_record['enabled'] = $user_rec['enabled'];
  1615. $auth_record['changed'] = 1;
  1616. //maybe fill comments?
  1617. if (!empty($user_rec['fio']) and empty($auth_record['comments'])) { $auth_record['comments'] = $user_rec['fio']; }
  1618. return $auth_record;
  1619. }
  1620. function fix_auth_rules($db)
  1621. {
  1622. //cleanup hotspot subnet rules
  1623. delete_record($db, "auth_rules", "ou_id=" . get_const('default_user_ou_id'));
  1624. delete_record($db, "auth_rules", "ou_id=" . get_const('default_hotspot_ou_id'));
  1625. $t_hotspot = get_records_sql($db, "SELECT * FROM subnets WHERE hotspot=1");
  1626. if (!empty($t_hotspot)) {
  1627. foreach ($t_hotspot as $row) {
  1628. delete_record($db, "auth_rules", "rule='" . $row['subnet'] . "'");
  1629. }
  1630. }
  1631. }
  1632. #---------------------------------------------------------------------------------------------------------------
  1633. function new_user($db, $user_info)
  1634. {
  1635. if (!empty($user_info['mac'])) {
  1636. $user['login'] = mac_dotted($user_info['mac']);
  1637. } else {
  1638. $user['login'] = $user_info['ip'];
  1639. }
  1640. if (!empty($user_info['dhcp_hostname'])) {
  1641. $user['fio'] = $user_info['ip'] . '[' . $user_info['dhcp_hostname'] . ']';
  1642. } else {
  1643. $user['fio'] = $user_info['ip'];
  1644. }
  1645. $login_count = get_count_records($db, "User_list", "(login LIKE '" . $user['login'] . "(%)') OR (login='" . $user['login'] . "')");
  1646. if (!empty($login_count) and $login_count > 0) {
  1647. $login_count++;
  1648. $user['login'] = $user['login'] . "(" . $login_count . ")";
  1649. }
  1650. $user['ou_id'] = $user_info['ou_id'];
  1651. $ou_info = get_record_sql($db, "SELECT * FROM OU WHERE id=" . $user_info['ou_id']);
  1652. if (!empty($ou_info)) {
  1653. $user['enabled'] = $ou_info['enabled'];
  1654. if (empty($user['enabled'])) {
  1655. $user['enabled'] = 0;
  1656. }
  1657. $user['queue_id'] = $ou_info['queue_id'];
  1658. if (empty($user['queue_id'])) {
  1659. $user['queue_id'] = 0;
  1660. }
  1661. $user['filter_group_id'] = $ou_info['filter_group_id'];
  1662. if (empty($user['filter_group_id'])) {
  1663. $user['filter_group_id'] = 0;
  1664. }
  1665. }
  1666. $result = insert_record($db, "User_list", $user);
  1667. $auto_mac_rule = get_option($db, 64);
  1668. if (!empty($result) and $auto_mac_rule and $user_info['mac']) {
  1669. $auth_rule['user_id'] = $result;
  1670. $auth_rule['type'] = 2;
  1671. $auth_rule['rule'] = mac_dotted($user_info['mac']);
  1672. insert_record($db, "auth_rules", $auth_rule);
  1673. }
  1674. return $result;
  1675. }
  1676. function new_auth($db, $ip, $mac, $user_id)
  1677. {
  1678. $ip_aton = ip2long($ip);
  1679. $msg = '';
  1680. if (!empty($mac)) {
  1681. $auth_record = get_record_sql($db, "SELECT * FROM User_auth WHERE ip_int=$ip_aton AND mac='" . mac_dotted($mac) . "' AND deleted=0");
  1682. if (!empty($auth_record)) {
  1683. LOG_WARNING($db, "Pair ip-mac already exists! Skip creating $ip [$mac] auth_id: " . $auth_record["id"]);
  1684. return $auth_record['id'];
  1685. }
  1686. }
  1687. // save traffic detailization
  1688. $save_traf = get_option($db, 23);
  1689. $resurrection_id = NULL;
  1690. // seek old auth with same ip and mac
  1691. $resurrection_id = get_id_record($db, 'User_auth', " deleted=1 AND ip_int=" . $ip_aton . " AND mac='" . $mac . "'");
  1692. if (!empty($resurrection_id)) {
  1693. $msg .= "Recovered auth_id: $resurrection_id with ip: $ip and mac: $mac ";
  1694. $auth['user_id'] = $user_id;
  1695. $auth['deleted'] = 0;
  1696. $auth['save_traf'] = $save_traf * 1;
  1697. update_record($db, "User_auth", "id=$resurrection_id", $auth);
  1698. } else {
  1699. // not found ->create new record
  1700. $msg .= "Create new ip record \r\nip: $ip\r\nmac: $mac\r\n";
  1701. $auth['deleted'] = 0;
  1702. $auth['user_id'] = $user_id;
  1703. $auth['ip'] = $ip;
  1704. $auth['ip_int'] = $ip_aton;
  1705. $auth['mac'] = $mac;
  1706. $auth['save_traf'] = $save_traf * 1;
  1707. $resurrection_id = insert_record($db, "User_auth", $auth);
  1708. }
  1709. //check rules, update filter and state for new record
  1710. if (!empty($resurrection_id)) {
  1711. $auth=apply_auth_rule($db, $auth, $user_id);
  1712. update_record($db, "User_auth", "id=$resurrection_id", $auth);
  1713. if (!is_hotspot($db, $ip) and !empty($msg)) {
  1714. LOG_WARNING($db, $msg);
  1715. }
  1716. if (is_hotspot($db, $ip) and !empty($msg)) {
  1717. LOG_INFO($db, $msg);
  1718. }
  1719. }
  1720. return $resurrection_id;
  1721. }
  1722. function resurrection_auth($db, $ip_record)
  1723. {
  1724. $ip = $ip_record['ip'];
  1725. $mac = $ip_record['mac'];
  1726. $action = $ip_record['type'];
  1727. $dhcp_hostname = $ip_record['hostname'];
  1728. $hotspot_found = $ip_record['hotspot'];
  1729. $ip_aton = ip2long($ip);
  1730. $auth_record = get_record_sql($db, "SELECT * FROM User_auth WHERE ip_int=$ip_aton AND mac='" . $mac . "' AND deleted=0");
  1731. if (!empty($auth_record)) {
  1732. $user_info = get_record_sql($db, "SELECT * FROM User_list WHERE id=" . $auth_record['user_id']);
  1733. LOG_DEBUG($db, "external dhcp user " . $user_info['login'] . " [" . $ip . "] auth_id: " . $auth_record['id']);
  1734. if (isset($dhcp_hostname) and !empty($dhcp_hostname)) {
  1735. $auth['dhcp_hostname'] = $dhcp_hostname;
  1736. }
  1737. $auth['dhcp_action'] = $action;
  1738. $auth['dhcp_time'] = GetNowTimeString();
  1739. if ($action === 'add') {
  1740. $auth['last_found'] = GetNowTimeString();
  1741. }
  1742. update_record($db, "User_auth", "id=" . $auth_record['id'], $auth);
  1743. return $auth_record['id'];
  1744. }
  1745. $ip_subnet = get_ip_subnet($db, $ip);
  1746. if ($ip_subnet['static']) {
  1747. LOG_WARNING($db, "Unknown pair ip+mac in static subnet! ip: $ip mac: [" . mac_dotted($mac) . "]. Skip");
  1748. return;
  1749. }
  1750. $msg = '';
  1751. // search changed mac
  1752. $auth_record = get_record_sql($db, "SELECT * FROM User_auth WHERE ip_int=$ip_aton AND deleted=0");
  1753. if (!empty($auth_record)) {
  1754. if (empty($auth_record['mac'])) {
  1755. $auth['mac'] = mac_dotted($mac);
  1756. $auth['dhcp_action'] = $action;
  1757. $auth['dhcp_time'] = GetNowTimeString();
  1758. if (!empty($dhcp_hostname)) {
  1759. $auth['dhcp_hostname'] = $dhcp_hostname;
  1760. }
  1761. if ($action === 'add') {
  1762. $auth['last_found'] = GetNowTimeString();
  1763. }
  1764. LOG_INFO($db, "for ip: $ip mac not found! Use empty record...");
  1765. update_record($db, "User_auth", "id=" . $auth_record['id'], $auth);
  1766. return $auth_record['id'];
  1767. } else {
  1768. if (!$hotspot_found) {
  1769. 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']);
  1770. }
  1771. run_sql($db, "UPDATE User_auth SET changed=1, deleted=1 WHERE id=" . $auth_record['id']);
  1772. }
  1773. }
  1774. // default id
  1775. $new_user_info = get_new_user_id($db, $ip, $mac, $dhcp_hostname);
  1776. if (!empty($new_user_info['user_id'])) {
  1777. $new_user_id = $new_user_info['user_id'];
  1778. }
  1779. if (empty($new_user_id)) {
  1780. $new_user_id = new_user($db, $new_user_info);
  1781. }
  1782. $resurrection_id = NULL;
  1783. $save_traf = get_option($db, 23);
  1784. $auth_record = get_record_sql($db, "SELECT * FROM User_auth WHERE ip_int=" . $ip_aton . " and mac='" . $mac . "'");
  1785. // seek old auth with same ip and mac
  1786. if (!empty($auth_record)) {
  1787. // found ->Resurrection old record
  1788. $resurrection_id = $auth_record['id'];
  1789. $msg .= "Recovered auth_id: $resurrection_id with ip: $ip and mac: $mac ";
  1790. $auth['dhcp_action'] = $action;
  1791. $auth['user_id'] = $new_user_id;
  1792. $auth['deleted'] = 0;
  1793. $auth['dhcp_time'] = GetNowTimeString();
  1794. $auth['save_traf'] = $save_traf * 1;
  1795. if (!empty($dhcp_hostname)) {
  1796. $auth['dhcp_hostname'] = $dhcp_hostname;
  1797. }
  1798. if ($action === 'add') {
  1799. $auth['last_found'] = GetNowTimeString();
  1800. }
  1801. update_record($db, "User_auth", "id=$resurrection_id", $auth);
  1802. } else {
  1803. // not found ->create new record
  1804. $msg .= "Создаём новый ip-адрес \r\nip: $ip\r\nmac: $mac\r\n";
  1805. $auth['deleted'] = 0;
  1806. $auth['user_id'] = $new_user_id;
  1807. $auth['ip'] = $ip;
  1808. $auth['ip_int'] = $ip_aton;
  1809. $auth['mac'] = $mac;
  1810. $auth['dhcp_action'] = $action;
  1811. $auth['dhcp_time'] = GetNowTimeString();
  1812. $auth['save_traf'] = $save_traf * 1;
  1813. if (!empty($dhcp_hostname)) {
  1814. $auth['dhcp_hostname'] = $dhcp_hostname;
  1815. }
  1816. if ($action == 'add') {
  1817. $auth['last_found'] = GetNowTimeString();
  1818. }
  1819. $resurrection_id = insert_record($db, "User_auth", $auth);
  1820. }
  1821. //check rules, update filter and state for new record
  1822. if (!empty($resurrection_id)) {
  1823. $auth = apply_auth_rule($db, $auth, $new_user_id);
  1824. update_record($db, "User_auth", "id=$resurrection_id", $auth);
  1825. $msg .= "filter: " . $auth['filter_group_id'] . "\r\n queue_id: " . $auth['queue_id'] . "\r\n enabled: " . $auth['enabled'] . "\r\nid: $resurrection_id";
  1826. if (!$hotspot_found and !empty($msg)) {
  1827. LOG_WARNING($db, $msg);
  1828. }
  1829. if ($hotspot_found and !empty($msg)) {
  1830. LOG_INFO($db, $msg);
  1831. }
  1832. }
  1833. return $resurrection_id;
  1834. }
  1835. function get_auth($db, $current_auth)
  1836. {
  1837. if (!isset($current_auth)) {
  1838. return;
  1839. }
  1840. if ($current_auth == 0) {
  1841. return;
  1842. }
  1843. $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");
  1844. list($f_login, $f_ip) = mysqli_fetch_array($t_login);
  1845. $result = $f_login . "[" . $f_ip . "]";
  1846. return $result;
  1847. }
  1848. function get_auth_by_mac($db, $mac)
  1849. {
  1850. if (!isset($mac)) {
  1851. return;
  1852. }
  1853. $mac = mac_dotted($mac);
  1854. $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");
  1855. list($f_id, $f_login, $f_auth_id, $f_ip) = mysqli_fetch_array($t_login);
  1856. if (isset($f_id)) {
  1857. $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>';
  1858. } else {
  1859. $result['auth'] = 'Unknown';
  1860. }
  1861. $result['mac'] = expand_mac($db, $mac);
  1862. return $result;
  1863. }
  1864. function get_auth_mac($db, $current_auth)
  1865. {
  1866. if (!isset($current_auth)) {
  1867. return;
  1868. }
  1869. if ($current_auth == 0) {
  1870. return;
  1871. }
  1872. $t_login = mysqli_query($db, "SELECT U.login,A.mac FROM User_list as U, User_auth as A WHERE A.user_id=U.id and A.id=$current_auth");
  1873. list($f_login, $f_mac) = mysqli_fetch_array($t_login);
  1874. $result = $f_login . "[" . $f_mac . "]";
  1875. return $result;
  1876. }
  1877. function add_auth_rule($db,$rule,$type,$user_id)
  1878. {
  1879. $new['user_id']=$user_id;
  1880. $new['type']=$type;
  1881. $new['rule']=$rule;
  1882. $rule_id=0;
  1883. $auth_rules = get_record_sql($db,"SELECT * FROM auth_rules WHERE rule='".$rule."' AND type=".$type);
  1884. if (empty($auth_rules)) {
  1885. $rule_id = insert_record($db,"auth_rules",$new);
  1886. LOG_INFO($db,"Create auto rule for user_id: ".$user_id." rule: ".$rule." type: ".$type);
  1887. } else {
  1888. if ($auth_rules['user_id'] === $user_id) {
  1889. $rule_id = update_record($db, "auth_rules", "id=".$auth_rules['id'], $new);
  1890. LOG_INFO($db,"Replaced auto rule for user_id: ".$user_id." rule: ".$rule." type: ".$type);
  1891. } else {
  1892. 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']);
  1893. $rule_id=0;
  1894. }
  1895. }
  1896. return $rule_id;
  1897. }
  1898. function update_auth_rule($db,$rule,$type,$rule_id)
  1899. {
  1900. $new['type']=$type;
  1901. $new['rule']=$rule;
  1902. $rule_id=0;
  1903. $auth_rules = get_record_sql($db,"SELECT * FROM auth_rules WHERE rule='".$rule."' AND type=".$type." AND id<>".$rule_id);
  1904. if (empty($auth_rules)) {
  1905. $rule_id = update_record($db, "auth_rules", "id=".$rule_id, $new);
  1906. } else {
  1907. LOG_WARNING($db,"Create auto rule id: ".$rule_id." rule: ".$rule." type: ".$type." failed! Already exists at user_id: ".$auth_rules['user_id']);
  1908. $rule_id=0;
  1909. }
  1910. return $rule_id;
  1911. }
  1912. //action: add,update,del
  1913. function allow_update($table, $action = 'update', $field = '')
  1914. {
  1915. //always allow modification for tables
  1916. if (preg_match('/(variables|dns_cache|syslog|sessions|dns_queue|User_auth_alias)/i', $table)) { return 1; }
  1917. if (isset($_SESSION['login'])) {
  1918. $work_user = $_SESSION['login'];
  1919. }
  1920. if (isset($_SESSION['user_id'])) {
  1921. $work_id = $_SESSION['user_id'];
  1922. }
  1923. if (isset($_SESSION['acl'])) {
  1924. $user_level = $_SESSION['acl'];
  1925. }
  1926. if (!isset($work_user) or !isset($work_id) or empty($user_level)) { return 0; }
  1927. //always allow Administrator
  1928. if ($user_level == 1) { return 1; }
  1929. //always forbid ViewOnly
  1930. if ($user_level == 3) { return 0; }
  1931. //allow tables for Operator
  1932. if ($action == 'update') {
  1933. $operator_acl = [
  1934. 'User_auth'=> [
  1935. 'comments'=>'1',
  1936. 'dns_name'=>'1',
  1937. 'firmware'=>'1',
  1938. 'link_check'=>'1',
  1939. 'nagios'=>'1',
  1940. 'nagios_handler'=>'1',
  1941. 'Wikiname'=>'1'
  1942. ],
  1943. 'User_list'=> [
  1944. 'fio'=>'1',
  1945. 'login'=>'1',
  1946. ],
  1947. ];
  1948. if (!isset($operator_acl[$table])) { return 0; }
  1949. if (isset($operator_acl[$table]) and empty($field)) { return 1; }
  1950. if (!isset($operator_acl[$table][$field])) { return 0; }
  1951. if (empty($operator_acl[$table][$field]) or $operator_acl[$table][$field]=='0') { return 0; }
  1952. return 1;
  1953. }
  1954. return 0;
  1955. }
  1956. function LOG_INFO($db, $msg, $auth_id = 0)
  1957. {
  1958. if (get_const('log_level') < L_INFO) {
  1959. return;
  1960. }
  1961. write_log($db, $msg, L_INFO, $auth_id);
  1962. }
  1963. function LOG_ERROR($db, $msg, $auth_id = 0)
  1964. {
  1965. if (get_const('log_level') < L_ERROR) {
  1966. return;
  1967. }
  1968. email(L_ERROR, $msg);
  1969. write_log($db, $msg, L_ERROR, $auth_id);
  1970. }
  1971. function LOG_VERBOSE($db, $msg, $auth_id = 0)
  1972. {
  1973. if (get_const('log_level') < L_VERBOSE) {
  1974. return;
  1975. }
  1976. write_log($db, $msg, L_VERBOSE, $auth_id);
  1977. }
  1978. function LOG_WARNING($db, $msg, $auth_id = 0)
  1979. {
  1980. if (get_const('log_level') < L_WARNING) {
  1981. return;
  1982. }
  1983. email(L_WARNING, $msg);
  1984. write_log($db, $msg, L_WARNING, $auth_id);
  1985. }
  1986. function LOG_DEBUG($db, $msg, $auth_id = 0)
  1987. {
  1988. if (!empty(get_const('debug')) and get_const('debug')) {
  1989. write_log($db, $msg, L_DEBUG, $auth_id);
  1990. }
  1991. }
  1992. function get_first_line($msg)
  1993. {
  1994. if (empty($msg)) {
  1995. return;
  1996. }
  1997. preg_match('/(.*)(\n|\<br\>)/', $msg, $matches);
  1998. if (!empty($matches[1])) {
  1999. return $matches[1];
  2000. }
  2001. return;
  2002. }
  2003. function email($level, $msg)
  2004. {
  2005. if (!get_const('send_email')) {
  2006. return;
  2007. }
  2008. if (!($level === L_WARNING or $level === L_ERROR)) {
  2009. return;
  2010. }
  2011. $subject = get_first_line($msg);
  2012. if ($level === L_WARNING) {
  2013. $subject = "WARN: " . $subject . "...";
  2014. $message = 'WARNING! Manager: ' . $_SESSION['login'] . ' </br>';
  2015. }
  2016. if ($level === L_ERROR) {
  2017. $subject = "ERROR: " . $subject . "...";
  2018. $message = 'ERROR! Manager: ' . $_SESSION['login'] . ' </br>';
  2019. }
  2020. $msg_lines = preg_replace("/\r\n/", "</br>", $msg);
  2021. $message .= $msg_lines;
  2022. $send = SimpleMail::make()
  2023. ->setTo(get_const('admin_email'), 'Administrator')
  2024. ->setFrom(get_const('sender_email'), 'Stat')
  2025. ->setSubject($subject)
  2026. ->setMessage($message)
  2027. ->setHtml()
  2028. ->setWrap(80)
  2029. ->send();
  2030. }
  2031. function write_log($db, $msg, $level, $auth_id = 0)
  2032. {
  2033. $work_user = 'http';
  2034. if (isset($_SESSION['login'])) {
  2035. $work_user = $_SESSION['login'];
  2036. }
  2037. if (!isset($msg)) {
  2038. $msg = 'ERROR! Empty log string!';
  2039. }
  2040. if (!isset($level)) {
  2041. $level = L_INFO;
  2042. }
  2043. $msg = str_replace("'", '', $msg);
  2044. $sSQL = "insert into syslog(customer,message,level,auth_id) values('$work_user','$msg',$level,$auth_id)";
  2045. mysqli_query($db, $sSQL);
  2046. }
  2047. function print_year_select($year_name, $year)
  2048. {
  2049. print "<select name=\"$year_name\" >\n";
  2050. for ($i = $year - 10; $i <= $year + 10; $i++) {
  2051. print_select_item($i, $i, $year);
  2052. }
  2053. print "</select>\n";
  2054. }
  2055. function print_date_select($dd, $mm, $yy)
  2056. {
  2057. if ($dd >= 1) {
  2058. print "<b>День</b>\n";
  2059. print "<select name=\"day\" >\n";
  2060. for ($i = 1; $i <= 31; $i++) {
  2061. print_select_item($i, $i, $dd);
  2062. }
  2063. print "</select>\n";
  2064. }
  2065. if ($mm >= 1) {
  2066. print "<b>Месяц</b>\n";
  2067. print "<select name=\"month\" >\n";
  2068. for ($i = 1; $i <= 12; $i++) {
  2069. $tmp_date = DateTimeImmutable::createFromFormat('U', strtotime("$i/01/$yy"));
  2070. $month_name = $tmp_date->format('F');
  2071. print_select_item($month_name, $i, $mm);
  2072. }
  2073. print "</select>\n";
  2074. }
  2075. print "<b>Год</b>\n";
  2076. print_year_select('year', $yy);
  2077. }
  2078. function print_date_select2($dd, $mm, $yy)
  2079. {
  2080. if ($dd >= 1) {
  2081. print "<b>День</b>\n";
  2082. print "<select name=\"day2\" >\n";
  2083. for ($i = 1; $i <= 31; $i++) {
  2084. print_select_item($i, $i, $dd);
  2085. }
  2086. print "</select>\n";
  2087. }
  2088. if ($mm >= 1) {
  2089. print "<b>Месяц</b>\n";
  2090. print "<select name=\"month2\" >\n";
  2091. for ($i = 1; $i <= 12; $i++) {
  2092. $tmp_date = DateTimeImmutable::createFromFormat('U', strtotime("$i/01/$yy"));
  2093. $month_name = $tmp_date->format('F');
  2094. print_select_item($month_name, $i, $mm);
  2095. }
  2096. print "</select>\n";
  2097. }
  2098. print "<b>Год</b>\n";
  2099. print_year_select('year2', $yy);
  2100. }
  2101. function is_up($ip)
  2102. {
  2103. if (!isset($ip) or strlen($ip) == 0) {
  2104. return false;
  2105. }
  2106. exec(sprintf('ping -i .3 -c 1 -W 5 %s', escapeshellarg($ip)), $res, $rval);
  2107. return $rval == 0;
  2108. }
  2109. function apply_device_lock ($db, $device_id, $iteration =0) {
  2110. $iteration++;
  2111. if ($iteration>2) { return false; }
  2112. $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');
  2113. if (empty($dev) or empty($dev['u_locked_timestamp'])) {
  2114. LOG_DEBUG($db,"Snmp discovery lock not found. Set and discovery.");
  2115. return set_lock_discovery($db,$device_id);
  2116. }
  2117. //wait for discovery
  2118. $now = time();
  2119. $wait_time = ($dev['u_locked_timestamp'] + SNMP_LOCK_TIMEOUT) - $now;
  2120. LOG_DEBUG($db,"Check snmp lock for device id: " . $device_id . ". Lock timestamp: ".$dev['u_locked_timestamp'].", now: ".$now);
  2121. if ($wait_time<0) {
  2122. LOG_DEBUG($db,"The lock is already expired. Set new lock.");
  2123. return set_lock_discovery($db,$device_id);
  2124. }
  2125. LOG_INFO($db,"Snmp discovery lock for device id: $device_id found! Need wait ".$wait_time." sec.");
  2126. sleep($wait_time);
  2127. LOG_INFO($db,"Try set new lock and continue discovery for device id:".$device_id);
  2128. return apply_device_lock($db,$device_id,$iteration);
  2129. }
  2130. function set_lock_discovery($db,$device_id) {
  2131. $new['discovery_locked'] = 1;
  2132. $new['locked_timestamp'] = GetNowTimeString();
  2133. if (update_record($db,'devices','id='.$device_id,$new)) { return true; }
  2134. return false;
  2135. }
  2136. function unset_lock_discovery($db,$device_id) {
  2137. $new['discovery_locked'] = 0;
  2138. $new['locked_timestamp'] = GetNowTimeString();
  2139. if (update_record($db,'devices','id='.$device_id,$new)) { return true; }
  2140. return false;
  2141. }
  2142. function get_ifmib_index_table($ip, $community, $version)
  2143. {
  2144. $ifmib_map = NULL;
  2145. $is_mikrotik = walk_snmp($ip, $community, $version, MIKROTIK_DHCP_SERVER);
  2146. $mk_ros_version = 0;
  2147. if ($is_mikrotik) {
  2148. $mikrotik_version = walk_snmp($ip, $community, $version, MIKROTIK_ROS_VERSION);
  2149. $mk_ros_version = 6491;
  2150. $result = preg_match('/RouterOS\s+(\d)\.(\d{1,3})\.(\d{1,3})\s+/', $mikrotik_version[MIKROTIK_ROS_VERSION], $matches);
  2151. if ($result) {
  2152. $mk_ros_version = $matches[1] * 1000 + $matches[2] * 10 + $matches[3];
  2153. }
  2154. }
  2155. if ($mk_ros_version == 0 or $mk_ros_version > 6468) {
  2156. #fdb_index => snmp_index
  2157. $index_map_table = walk_snmp($ip, $community, $version, IFMIB_IFINDEX_MAP);
  2158. #get map snmp interfaces to fdb table
  2159. if (isset($index_map_table) and count($index_map_table) > 0) {
  2160. foreach ($index_map_table as $key => $value) {
  2161. $key = trim($key);
  2162. $value = intval(trim(str_replace('INTEGER:', '', $value)));
  2163. $result = preg_match('/\.(\d{1,10})$/', $key, $matches);
  2164. if ($result) {
  2165. $fdb_index = preg_replace('/^\./', '', $matches[0]);
  2166. $ifmib_map[$fdb_index] = $value;
  2167. }
  2168. }
  2169. }
  2170. }
  2171. #return simple map snmp_port_index = snmp_port_index
  2172. if (empty($ifmib_map)) {
  2173. #ifindex
  2174. $index_table = walk_snmp($ip, $community, $version, IFMIB_IFINDEX);
  2175. if (isset($index_table) and count($index_table) > 0) {
  2176. foreach ($index_table as $key => $value) {
  2177. $key = trim($key);
  2178. $value = intval(trim(str_replace('INTEGER:', '', $value)));
  2179. $result = preg_match('/\.(\d{1,10})$/', $key, $matches);
  2180. if ($result) {
  2181. $fdb_index = preg_replace('/^\./', '', $matches[0]);
  2182. $ifmib_map[$fdb_index] = $value;
  2183. }
  2184. }
  2185. }
  2186. }
  2187. return $ifmib_map;
  2188. }
  2189. #get mac table by selected snmp oid
  2190. function get_mac_table($ip, $community, $version, $oid, $index_map)
  2191. {
  2192. if (!isset($ip)) {
  2193. return;
  2194. }
  2195. if (!isset($oid)) {
  2196. return;
  2197. }
  2198. if (!isset($community)) {
  2199. $community = 'public';
  2200. }
  2201. if (!isset($version)) {
  2202. $version = '2';
  2203. }
  2204. $mac_table = walk_snmp($ip, $community, $version, $oid);
  2205. if (isset($mac_table) and gettype($mac_table) == 'array' and count($mac_table) > 0) {
  2206. foreach ($mac_table as $key => $value) {
  2207. if (empty($value)) {
  2208. continue;
  2209. }
  2210. if (empty($key)) {
  2211. continue;
  2212. }
  2213. $key = trim($key);
  2214. $value_raw = intval(trim(str_replace('INTEGER:', '', $value)));
  2215. if (empty($value_raw)) {
  2216. continue;
  2217. }
  2218. if (!empty($index_map)) {
  2219. if (empty($index_map[$value_raw])) {
  2220. $value = $value_raw;
  2221. } else {
  2222. $value = $index_map[$value_raw];
  2223. }
  2224. } else {
  2225. $value = $value_raw;
  2226. }
  2227. $pattern = '/\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})$/';
  2228. $result = preg_match($pattern, $key, $matches);
  2229. if (!empty($result)) {
  2230. $mac_key = preg_replace('/^\./', '', $matches[0]);
  2231. $fdb_table[$mac_key] = $value;
  2232. }
  2233. }
  2234. }
  2235. return $fdb_table;
  2236. }
  2237. #get mac table by analyze all available tables
  2238. function get_fdb_table($ip, $community, $version)
  2239. {
  2240. if (!isset($ip)) {
  2241. return;
  2242. }
  2243. if (!isset($community)) {
  2244. $community = 'public';
  2245. }
  2246. if (!isset($version)) {
  2247. $version = '2';
  2248. }
  2249. $ifindex_map = get_ifmib_index_table($ip, $community, $version);
  2250. $fdb1_table = get_mac_table($ip, $community, $version, MAC_TABLE_OID, $ifindex_map);
  2251. if (!empty($fdb1_table)) {
  2252. $fdb_table = $fdb1_table;
  2253. } else {
  2254. $fdb2_table = get_mac_table($ip, $community, $version, MAC_TABLE_OID2, $ifindex_map);
  2255. if (!empty($fdb2_table)) {
  2256. $fdb_table = $fdb2_table;
  2257. }
  2258. }
  2259. // maybe cisco?!
  2260. if (!isset($fdb_table) or empty($fdb_table) or count($fdb_table) == 0) {
  2261. $vlan_table = walk_snmp($ip, $community, $version, CISCO_VLAN_OID);
  2262. if (empty($vlan_table)) {
  2263. return;
  2264. }
  2265. foreach ($vlan_table as $vlan_oid => $value) {
  2266. if (empty($vlan_oid)) {
  2267. continue;
  2268. }
  2269. $pattern = '/\.(\d{1,4})$/';
  2270. $result = preg_match($pattern, $vlan_oid, $matches);
  2271. if (!empty($result)) {
  2272. $vlan_id = preg_replace('/^\./', '', $matches[0]);
  2273. if ($vlan_id > 1000 and $vlan_id < 1009) {
  2274. continue;
  2275. }
  2276. $fdb_vlan_table = get_mac_table($ip, $community . '@' . $vlan_id, $version, MAC_TABLE_OID, $ifindex_map);
  2277. if (!isset($fdb_vlan_table) or !$fdb_vlan_table or count($fdb_vlan_table) == 0) {
  2278. $fdb_vlan_table = get_mac_table($ip, $community, $version, MAC_TABLE_OID2, $ifindex_map);
  2279. }
  2280. foreach ($fdb_vlan_table as $mac => $port) {
  2281. if (!isset($mac)) {
  2282. continue;
  2283. }
  2284. $fdb_table[$mac] = $port;
  2285. }
  2286. }
  2287. }
  2288. }
  2289. return $fdb_table;
  2290. }
  2291. function check_snmp_access($ip, $community, $version)
  2292. {
  2293. if (!isset($ip)) {
  2294. return;
  2295. }
  2296. if (!isset($community)) {
  2297. $community = 'public';
  2298. }
  2299. if (!isset($version)) {
  2300. $version = '2';
  2301. }
  2302. #check host up
  2303. $status = exec(escapeshellcmd("ping -W 1 -i 1 -c 3 " . $ip));
  2304. if (empty($status)) {
  2305. return;
  2306. }
  2307. #check snmp
  2308. $result = get_snmp($ip, $community, $version, SYS_DESCR_MIB);
  2309. if (empty($result)) {
  2310. return;
  2311. }
  2312. return 1;
  2313. }
  2314. function get_port_state($port, $ip, $community, $version)
  2315. {
  2316. if (!isset($port)) {
  2317. return;
  2318. }
  2319. if (!isset($ip)) {
  2320. return;
  2321. }
  2322. if (!isset($community)) {
  2323. $community = 'public';
  2324. }
  2325. if (!isset($version)) {
  2326. $version = '2';
  2327. }
  2328. $port_oid = PORT_STATUS_OID .'.'. $port;
  2329. $port_state = get_snmp($ip, $community, $version, $port_oid);
  2330. return $port_state;
  2331. }
  2332. function get_last_digit($oid)
  2333. {
  2334. if (!isset($oid)) {
  2335. return;
  2336. }
  2337. $pattern = '/\.(\d{1,})$/';
  2338. preg_match($pattern, $oid, $matches);
  2339. return $matches[1];
  2340. }
  2341. function get_cisco_sensors($ip, $community, $version, $mkey)
  2342. {
  2343. $index = get_last_digit($mkey);
  2344. $result = parse_snmp_value(get_snmp($ip, $community, $version, CISCO_SFP_SENSORS . "." . $index));
  2345. $prec = parse_snmp_value(get_snmp($ip, $community, $version, CISCO_SFP_PRECISION . "." . $index));
  2346. if (!isset($prec)) {
  2347. $prec = 1;
  2348. }
  2349. $result = round(trim($result) / (10 * $prec), 2);
  2350. return $result;
  2351. }
  2352. function get_snmp_ifname($ip, $community, $version, $port)
  2353. {
  2354. $port_name = parse_snmp_value(get_snmp($ip, $community, $version, IFMIB_IFNAME . "." . $port));
  2355. if (empty($port_name)) {
  2356. $port_name = parse_snmp_value(get_snmp($ip, $community, $version, IFMIB_IFDESCR . "." . $port));
  2357. }
  2358. if (empty($port_name)) {
  2359. $port_name = parse_snmp_value(get_snmp($ip, $community, $version, IFMIB_IFALIAS . "." . $port));
  2360. }
  2361. return $port_name;
  2362. }
  2363. function get_snmp_interfaces($ip, $community, $version)
  2364. {
  2365. $result=[];
  2366. $ifmib_list = walk_snmp($ip, $community, $version, IFMIB_IFNAME);
  2367. if (empty($ifmib_list)) {
  2368. $ifmib_list = walk_snmp($ip, $community, $version, IFMIB_IFDESCR);
  2369. }
  2370. if (empty($ifmib_list)) {
  2371. $ifmib_list = walk_snmp($ip, $community, $version, IFMIB_IFALIAS);
  2372. }
  2373. if (!empty($ifmib_list)) {
  2374. foreach ($ifmib_list as $key => $value) {
  2375. $key = trim($key);
  2376. $value = parse_snmp_value($value);
  2377. if (preg_match('/\.(\d{1,10})$/', $key, $matches)) {
  2378. $int_index = preg_replace('/^\./', '', $matches[0]);
  2379. $result[$int_index]=$value;
  2380. }
  2381. }
  2382. }
  2383. return $result;
  2384. }
  2385. function walk_snmp($ip, $community, $version, $oid)
  2386. {
  2387. snmp_set_oid_output_format(SNMP_OID_OUTPUT_NUMERIC);
  2388. $result = NULL;
  2389. if ($version == 2) {
  2390. $result = snmp2_real_walk($ip, $community, $oid, SNMP_timeout, SNMP_retry);
  2391. }
  2392. if ($version == 1) {
  2393. $result = snmprealwalk($ip, $community, $oid, SNMP_timeout, SNMP_retry);
  2394. }
  2395. return $result;
  2396. }
  2397. function get_snmp_module_id($modules_oids, $port_name)
  2398. {
  2399. $port_name = preg_quote(trim($port_name), '/');
  2400. foreach ($modules_oids as $key => $value) {
  2401. $pattern = '/' . $port_name . '/i';
  2402. preg_match($pattern, $value, $matches);
  2403. if (isset($matches[0])) {
  2404. $module_id = get_last_digit($key);
  2405. return $module_id;
  2406. }
  2407. }
  2408. return;
  2409. }
  2410. function get_sfp_status($vendor_id, $port, $ip, $community='public', $version='2', $modules_oids)
  2411. {
  2412. if (!isset($vendor_id)) {
  2413. return;
  2414. }
  2415. if (!isset($port)) {
  2416. return;
  2417. }
  2418. if (!isset($ip)) {
  2419. return;
  2420. }
  2421. try {
  2422. $status = '';
  2423. // eltex
  2424. if ($vendor_id == 2) {
  2425. $sfp_vendor = parse_snmp_value(get_snmp($ip, $community, $version, ELTEX_SFP_VENDOR . "." . $port));
  2426. if (isset($sfp_vendor)) {
  2427. $sfp_status_temp = ELTEX_SFP_STATUS . "." . $port . ".5";
  2428. $sfp_status_volt = ELTEX_SFP_STATUS . "." . $port . ".6";
  2429. $sfp_status_circut = ELTEX_SFP_STATUS . "." . $port . ".7";
  2430. $sfp_status_tx = ELTEX_SFP_STATUS . "." . $port . ".8";
  2431. $sfp_status_rx = ELTEX_SFP_STATUS . "." . $port . ".9";
  2432. $temp = parse_snmp_value(get_snmp($ip, $community, $version, $sfp_status_temp));
  2433. $volt = parse_snmp_value(get_snmp($ip, $community, $version, $sfp_status_volt));
  2434. $circut = parse_snmp_value(get_snmp($ip, $community, $version, $sfp_status_circut));
  2435. $tx = parse_snmp_value(get_snmp($ip, $community, $version, $sfp_status_tx));
  2436. $rx = parse_snmp_value(get_snmp($ip, $community, $version, $sfp_status_rx));
  2437. $sfp_sn = parse_snmp_value(get_snmp($ip, $community, $version, ELTEX_SFP_SN . "." . $port));
  2438. $sfp_freq = parse_snmp_value(get_snmp($ip, $community, $version, ELTEX_SFP_FREQ . "." . $port));
  2439. if (!isset($sfp_freq) or $sfp_freq == 65535) {
  2440. $sfp_freq = 'unspecified';
  2441. }
  2442. $sfp_length = parse_snmp_value(get_snmp($ip, $community, $version, ELTEX_SFP_LENGTH . "." . $port));
  2443. $status = 'Vendor: ' . $sfp_vendor . ' Serial: ' . $sfp_sn . ' Laser: ' . $sfp_freq . ' Distance: ' . $sfp_length . '<br>';
  2444. if (!empty($sfp_status_temp) and $temp > 0.1) {
  2445. $status .= 'Temp: ' . $temp . " C";
  2446. }
  2447. if (!empty($sfp_status_volt) and $volt > 0.1) {
  2448. $status .= ' Volt: ' . round($volt / 1000000, 2) . ' V';
  2449. }
  2450. if (!empty($sfp_status_circut) and $circut > 0.1) {
  2451. $status .= ' Circut: ' . round($circut / 1000, 2) . ' mA';
  2452. }
  2453. if (!empty($sfp_status_tx) and $tx > 0.1) {
  2454. $status .= ' Tx: ' . round($tx / 1000, 2) . ' dBm';
  2455. }
  2456. if (!empty($sfp_status_rx) and $rx > 0.1) {
  2457. $status .= ' Rx: ' . round($rx / 1000, 2) . ' dBm';
  2458. }
  2459. $status .= '<br>';
  2460. return $status;
  2461. }
  2462. return;
  2463. }
  2464. // cisco
  2465. if ($vendor_id == 16) {
  2466. // get interface names
  2467. $port_name = parse_snmp_value(get_snmp($ip, $community, $version, IFMIB_IFNAME . "." . $port));
  2468. if (empty($port_name)) {
  2469. $port_name = parse_snmp_value(get_snmp($ip, $community, $version, IFMIB_IFDESCR . "." . $port));
  2470. }
  2471. // search module indexes
  2472. $port_name = preg_quote(trim($port_name), '/');
  2473. foreach ($modules_oids as $key => $value) {
  2474. $pattern = '/(' . $port_name . ' Module Temperature Sensor)/i';
  2475. preg_match($pattern, $value, $matches);
  2476. if (isset($matches[0])) {
  2477. $temp = get_cisco_sensors($ip, $community, $version, $key);
  2478. continue;
  2479. }
  2480. $pattern = '/(' . $port_name . ' Supply Voltage Sensor)/i';
  2481. preg_match($pattern, $value, $matches);
  2482. if (isset($matches[0])) {
  2483. $volt = get_cisco_sensors($ip, $community, $version, $key);
  2484. continue;
  2485. }
  2486. $pattern = '/(' . $port_name . ' Bias Current Sensor)/i';
  2487. preg_match($pattern, $value, $matches);
  2488. if (isset($matches[0])) {
  2489. $circut = get_cisco_sensors($ip, $community, $version, $key);
  2490. continue;
  2491. }
  2492. $pattern = '/(' . $port_name . ' Transmit Power Sensor)/i';
  2493. preg_match($pattern, $value, $matches);
  2494. if (isset($matches[0])) {
  2495. $tx = get_cisco_sensors($ip, $community, $version, $key);
  2496. continue;
  2497. }
  2498. $pattern = '/(' . $port_name . ' Receive Power Sensor)/i';
  2499. preg_match($pattern, $value, $matches);
  2500. if (isset($matches[0])) {
  2501. $rx = get_cisco_sensors($ip, $community, $version, $key);
  2502. continue;
  2503. }
  2504. }
  2505. if (!empty($temp) and $temp > 0) {
  2506. $status .= 'Temp: ' . $temp . " C";
  2507. }
  2508. if (!empty($volt) and $volt > 0) {
  2509. $status .= ' Volt: ' . $volt . ' V';
  2510. }
  2511. if (!empty($circut) and $circut > 0) {
  2512. $status .= ' Circut: ' . $circut . ' mA';
  2513. }
  2514. if (!empty($tx) and abs($tx) > 0.1) {
  2515. $status .= ' Tx: ' . $tx . ' dBm';
  2516. }
  2517. if (!empty($rx) and abs($rx) > 0.1) {
  2518. $status .= ' Rx: ' . $rx . ' dBm';
  2519. }
  2520. if (!empty($status)) {
  2521. $status = preg_replace('/"/', '', $status);
  2522. $status .= '<br>';
  2523. }
  2524. return $status;
  2525. }
  2526. // huawei
  2527. if ($vendor_id == 3) {
  2528. // get interface names
  2529. $port_name = parse_snmp_value(get_snmp($ip, $community, $version, IFMIB_IFNAME . "." . $port));
  2530. if (empty($port_name)) {
  2531. $port_name = parse_snmp_value(get_snmp($ip, $community, $version, IFMIB_IFDESCR . "." . $port));
  2532. }
  2533. // search module indexes
  2534. $port_name = preg_quote(trim($port_name), '/');
  2535. foreach ($modules_oids as $key => $value) {
  2536. $pattern = '/' . $port_name . '/i';
  2537. preg_match($pattern, $value, $matches);
  2538. if (isset($matches[0])) {
  2539. $module_id = get_last_digit($key);
  2540. unset($result);
  2541. $result = parse_snmp_value(get_snmp($ip, $community, $version, HUAWEI_SFP_VENDOR . "." . $module_id));
  2542. if (!empty($result)) {
  2543. $sfp_vendor = $result;
  2544. }
  2545. unset($result);
  2546. $result = parse_snmp_value(get_snmp($ip, $community, $version, HUAWEI_SFP_SPEED . "." . $module_id));
  2547. if (!empty($result)) {
  2548. list($sfp_speed, $spf_lenght, $sfp_type) = explode('-', $result);
  2549. if ($sfp_type == 0) {
  2550. $sfp_type = 'MultiMode';
  2551. }
  2552. if ($sfp_type == 1) {
  2553. $sfp_type = 'SingleMode';
  2554. }
  2555. }
  2556. $volt = parse_snmp_value(get_snmp($ip, $community, $version, HUAWEI_SFP_VOLT . "." . $module_id));
  2557. $circut = parse_snmp_value(get_snmp($ip, $community, $version, HUAWEI_SFP_BIASCURRENT . "." . $module_id));
  2558. $tx = parse_snmp_value(get_snmp($ip, $community, $version, HUAWEI_SFP_OPTTX . "." . $module_id));
  2559. $rx = parse_snmp_value(get_snmp($ip, $community, $version, HUAWEI_SFP_OPTRX . "." . $module_id));
  2560. if (!isset($tx)) {
  2561. $tx = parse_snmp_value(get_snmp($ip, $community, $version, HUAWEI_SFP_TX . "." . $module_id));
  2562. }
  2563. if (!isset($rx)) {
  2564. $rx = parse_snmp_value(get_snmp($ip, $community, $version, HUAWEI_SFP_RX . "." . $module_id));
  2565. }
  2566. if (!empty($sfp_vendor)) {
  2567. $status .= ' Name:' . $sfp_vendor . '<br>';
  2568. }
  2569. // if (isset($sfp_speed)) { $status .= ' ' . $sfp_speed; }
  2570. // if (isset($spf_lenght)) { $status .= ' ' . $spf_lenght; }
  2571. if ($volt > 0) {
  2572. $status .= ' Volt: ' . round($volt / 1000, 2) . ' V';
  2573. }
  2574. if (!empty($circut) and $circut > 0) {
  2575. $status .= ' Circut: ' . $circut . ' mA <br>';
  2576. }
  2577. if (!empty($tx)) {
  2578. $tx = preg_replace('/"/', '', $tx);
  2579. try {
  2580. list($tx_dbm, $pattern) = explode('.', $tx);
  2581. $tx_dbm = round(floatval(trim($tx_dbm)) / 100, 2);
  2582. } catch (Exception $e) { $tx_dbm = 0; }
  2583. if (abs($tx_dbm) > 0.1) {
  2584. $status .= ' Tx: ' . $tx_dbm . ' dBm';
  2585. }
  2586. }
  2587. if (!empty($rx)) {
  2588. $rx = preg_replace('/"/', '', $rx);
  2589. try {
  2590. list($rx_dbm, $pattern) = explode('.', $rx);
  2591. $rx_dbm = round(floatval(trim($rx_dbm)) / 100, 2);
  2592. } catch (Exception $e) { $rx_dbm=0; }
  2593. if (abs($rx_dbm) > 0.1) {
  2594. $status .= ' Rx: ' . $rx_dbm . ' dBm';
  2595. }
  2596. }
  2597. break;
  2598. }
  2599. }
  2600. if (isset($status)) {
  2601. $status = preg_replace('/"/', '', $status);
  2602. $status .= '<br>';
  2603. }
  2604. return $status;
  2605. }
  2606. } catch (Exception $e) {
  2607. return;
  2608. }
  2609. }
  2610. function get_switch_vlans($vendor,$ip,$community='public',$version='2') {
  2611. $switch_vlans = [];
  2612. $port_status = [];
  2613. $vlan_status = [];
  2614. //cisco...
  2615. if ($vendor == 16) {
  2616. //all vlan at switch
  2617. $vlan_list = walk_snmp($ip, $community, $version, vtpVlanName);
  2618. if (empty($vlan_list)) { return; }
  2619. foreach ($vlan_list as $key => $value) {
  2620. if (empty($value)) { $value = ''; }
  2621. $key = trim($key);
  2622. $value = parse_snmp_value($value);
  2623. $vlan_id = NULL;
  2624. if (preg_match('/\.(\d{1,10})$/', $key, $matches)) { $vlan_id = preg_replace('/^\./', '', $matches[0]); }
  2625. //skip service vlan
  2626. if (preg_match('/(1002|1003|1004|1005)/',$vlan_id)) { continue; }
  2627. if (isset($vlan_id) and !empty($vlan_id)) { $switch_vlans[$vlan_id]=$value; }
  2628. }
  2629. //native vlan for port - get list of all ports
  2630. $pvid_list = walk_snmp($ip, $community, $version, vlanTrunkPortNativeVlan);
  2631. if (!empty($pvid_list)) {
  2632. foreach ($pvid_list as $key => $value) {
  2633. if (empty($value)) { $value = ''; }
  2634. $key = trim($key);
  2635. $value = parse_snmp_value($value);
  2636. $port = NULL;
  2637. if (preg_match('/\.(\d{1,10})$/', $key, $matches)) { $port = preg_replace('/^\./', '', $matches[0]); }
  2638. if (isset($port) and !empty($port)) { $port_status[$port]['native']=$value; }
  2639. }
  2640. }
  2641. //pvid
  2642. $pvid_list = walk_snmp($ip, $community, $version, vmVlanPvid);
  2643. if (!empty($pvid_list)) {
  2644. foreach ($pvid_list as $key => $value) {
  2645. if (empty($value)) { $value = ''; }
  2646. $key = trim($key);
  2647. $value = parse_snmp_value($value);
  2648. $port = NULL;
  2649. if (preg_match('/\.(\d{1,10})$/', $key, $matches)) { $port = preg_replace('/^\./', '', $matches[0]); }
  2650. if (isset($port) and !empty($port)) { $port_status[$port]['pvid']=$value; }
  2651. }
  2652. }
  2653. //init port config
  2654. foreach ($port_status as &$port) {
  2655. if (!is_array($port)) { continue; }
  2656. if (!isset($port['pvid'])) { $port['pvid']=$port['native']; }
  2657. $port['untagged']='';
  2658. $port['tagged']='';
  2659. }
  2660. unset($port);
  2661. //get vlan list at ports
  2662. $egress_vlan = walk_snmp($ip,$community,$version,vlanTrunkPortVlansEnabled);
  2663. if (!empty($egress_vlan)) {
  2664. $j = 0;
  2665. foreach ($egress_vlan as $key => $value) {
  2666. $j++;
  2667. if (empty($value)) { $value = ''; }
  2668. $key = trim($key);
  2669. $value = parse_snmp_value($value);
  2670. if (preg_match('/\.(\d{1,10})$/', $key, $matches)) {
  2671. $port = preg_replace('/^\./', '', $matches[0]);
  2672. }
  2673. if (isset($port) and !empty($port)) {
  2674. //skip access ports
  2675. if (!is_array($port_status[$port]) or !isset($port_status[$port]['pvid']) or !isset($port_status[$port]['native'])) { continue; }
  2676. if ($port_status[$port]['pvid'] != $port_status[$port]['native']) { continue; }
  2677. //get vlan at port in hex
  2678. $hex_value = preg_replace('/\s+/','',$value);
  2679. $bin_value = strHexToBin($hex_value);
  2680. //analyze switch vlans
  2681. foreach ($switch_vlans as $vlan_id => $vlan_name) {
  2682. if (isset($bin_value[$vlan_id]) and $bin_value[$vlan_id]=='1') {
  2683. $port_status[$port]['tagged']=$port_status[$port]['tagged'].','.$vlan_id;
  2684. }
  2685. }
  2686. }
  2687. }
  2688. }
  2689. //remove lliding ,
  2690. foreach ($port_status as &$port) {
  2691. if (!is_array($port)) { continue; }
  2692. $port['untagged']=preg_replace('/^,/', '',$port['untagged']);
  2693. $port['tagged']=preg_replace('/^,/', '',$port['tagged']);
  2694. }
  2695. unset($port);
  2696. return $port_status;
  2697. }
  2698. //standart switches
  2699. //tplink
  2700. if ($vendor == 69) {
  2701. //pvid for port
  2702. $pvid_list = walk_snmp($ip, $community, $version, TPLINK_dot1qPortVlanEntry);
  2703. if (!empty($pvid_list)) {
  2704. foreach ($pvid_list as $key => $value) {
  2705. if (empty($value)) { $value = ''; }
  2706. $key = trim($key);
  2707. $value = parse_snmp_value($value);
  2708. $port = NULL;
  2709. if (preg_match('/\.(\d{1,10})$/', $key, $matches)) { $port = preg_replace('/^\./', '', $matches[0]); }
  2710. if (isset($port) and !empty($port)) { $port_status[$port]['pvid']=$value; }
  2711. }
  2712. }
  2713. return $port_status;
  2714. }
  2715. //default
  2716. //pvid for port
  2717. $pvid_list = walk_snmp($ip, $community, $version, dot1qPortVlanEntry);
  2718. if (!empty($pvid_list)) {
  2719. foreach ($pvid_list as $key => $value) {
  2720. if (empty($value)) { $value = ''; }
  2721. $key = trim($key);
  2722. $value = parse_snmp_value($value);
  2723. $port = NULL;
  2724. if (preg_match('/\.(\d{1,10})$/', $key, $matches)) { $port = preg_replace('/^\./', '', $matches[0]); }
  2725. if (isset($port) and !empty($port)) { $port_status[$port]['pvid']=$value; }
  2726. }
  2727. }
  2728. //init port config
  2729. foreach ($port_status as &$port) {
  2730. if (!is_array($port)) { continue; }
  2731. $port['native']=$port['pvid'];
  2732. $port['untagged']='';
  2733. $port['tagged']='';
  2734. }
  2735. unset($port);
  2736. //all vlan at switch
  2737. $vlan_list = walk_snmp($ip, $community, $version, dot1qVlanStaticName);
  2738. if (empty($vlan_list)) { return $port_status; }
  2739. foreach ($vlan_list as $key => $value) {
  2740. if (empty($value)) { $value = ''; }
  2741. $key = trim($key);
  2742. $value = parse_snmp_value($value);
  2743. $vlan_id = NULL;
  2744. if (preg_match('/\.(\d{1,10})$/', $key, $matches)) { $vlan_id = preg_replace('/^\./', '', $matches[0]); }
  2745. if (isset($vlan_id) and !empty($vlan_id)) { $switch_vlans[$vlan_id]=$value; }
  2746. }
  2747. $forbidden_vlan = walk_snmp($ip,$community,$version,dot1qVlanForbiddenEgressPorts);
  2748. if (!empty($forbidden_vlan)) {
  2749. foreach ($forbidden_vlan as $key => $value) {
  2750. if (empty($value)) { $value = ''; }
  2751. $key = trim($key);
  2752. $value = parse_snmp_value($value);
  2753. if (preg_match('/\.(\d{1,10})$/', $key, $matches)) { $vlan_id = preg_replace('/^\./', '', $matches[0]); }
  2754. if (isset($vlan_id) and !empty($vlan_id)) {
  2755. $hex_value = preg_replace('/\s+/','',$value);
  2756. $hex_value = preg_replace('/0*$/','',$hex_value);
  2757. $bin_value = strHexToBin($hex_value);
  2758. for ($i=0; $i<strlen($bin_value); $i++) {
  2759. $port = $i+1;
  2760. $vlan_status['forbidden_vlan'][$vlan_id][$port] = $bin_value[$i];
  2761. if ($bin_value[$i]=='1') {
  2762. $port_status[$port]['forbidden'].=','.$vlan_id;
  2763. }
  2764. }
  2765. }
  2766. }
  2767. }
  2768. $untagged_vlan = walk_snmp($ip,$community,$version,dot1qVlanStaticUntaggedPorts);
  2769. if (!empty($untagged_vlan)) {
  2770. foreach ($untagged_vlan as $key => $value) {
  2771. if (empty($value)) { $value = ''; }
  2772. $key = trim($key);
  2773. $value = parse_snmp_value($value);
  2774. if (preg_match('/\.(\d{1,10})$/', $key, $matches)) { $vlan_id = preg_replace('/^\./', '', $matches[0]); }
  2775. if (isset($vlan_id) and !empty($vlan_id)) {
  2776. $hex_value = preg_replace('/\s+/','',$value);
  2777. $hex_value = preg_replace('/0*$/','',$hex_value);
  2778. $bin_value = strHexToBin($hex_value);
  2779. for ($i=0; $i<strlen($bin_value); $i++) {
  2780. $port = $i+1;
  2781. $vlan_status['untagged_vlan'][$vlan_id][$port] = $bin_value[$i];
  2782. if ($bin_value[$i]=='1') {
  2783. if (isset($vlan_status['forbidden_vlan']) and $vlan_status['forbidden_vlan'][$vlan_id][$port]=='0') {
  2784. $port_status[$port]['untagged'].=','.$vlan_id;
  2785. } else {
  2786. $vlan_status['untagged_vlan'][$vlan_id][$port]='0';
  2787. }
  2788. }
  2789. }
  2790. }
  2791. }
  2792. }
  2793. $egress_vlan = walk_snmp($ip,$community,$version,dot1qVlanStaticEgressPorts);
  2794. if (!empty($egress_vlan)) {
  2795. foreach ($egress_vlan as $key => $value) {
  2796. if (empty($value)) { $value = ''; }
  2797. $key = trim($key);
  2798. $value = parse_snmp_value($value);
  2799. if (preg_match('/\.(\d{1,10})$/', $key, $matches)) { $vlan_id = preg_replace('/^\./', '', $matches[0]); }
  2800. //exclude vlan 1 from tagged vlan
  2801. if ($vlan_id =='1') { continue; }
  2802. if (isset($vlan_id) and !empty($vlan_id)) {
  2803. $hex_value = preg_replace('/\s+/','',$value);
  2804. $hex_value = preg_replace('/0*$/','',$hex_value);
  2805. $bin_value = strHexToBin($hex_value);
  2806. for ($i=0; $i<strlen($bin_value); $i++) {
  2807. $port = $i+1;
  2808. $vlan_status['egress_vlan'][$vlan_id][$port] = $bin_value[$i];
  2809. //analyze egress & untagged vlans
  2810. if ($bin_value[$i]=='1') {
  2811. if ((!isset($vlan_status['untagged_vlan'][$vlan_id][$port]) or $vlan_status['untagged_vlan'][$vlan_id][$port]=='0' ) and
  2812. (!isset($vlan_status['forbidden_vlan'][$vlan_id][$port]) or $vlan_status['forbidden_vlan'][$vlan_id][$port]=='0') and
  2813. (!isset($port_status[$port]['pvid']) or $port_status[$port]['pvid']!=$vlan_id )) {
  2814. $vlan_status['tagged_vlan'][$vlan_id][$port]='1';
  2815. $port_status[$port]['tagged'].=','.$vlan_id;
  2816. } else {
  2817. $vlan_status['tagged_vlan'][$vlan_id][$port]='0';
  2818. }
  2819. }
  2820. }
  2821. }
  2822. }
  2823. }
  2824. foreach ($port_status as &$port) {
  2825. if (!is_array($port)) { continue; }
  2826. $port['untagged']=preg_replace('/^,/', '',$port['untagged']);
  2827. $port['tagged']=preg_replace('/^,/', '',$port['tagged']);
  2828. }
  2829. unset($port);
  2830. return $port_status;
  2831. }
  2832. function get_port_vlan($vendor, $port, $port_index, $ip, $community='public', $version='2')
  2833. {
  2834. if (!isset($port_index)) {
  2835. return;
  2836. }
  2837. if (!isset($ip)) {
  2838. return;
  2839. }
  2840. //default - default port index
  2841. $port_oid = dot1qPortVlanEntry . "." . $port_index;
  2842. //tplink
  2843. if ($vendor == 69) {
  2844. $port_oid = TPLINK_dot1qPortVlanEntry . "." . $port_index;
  2845. }
  2846. //huawei
  2847. if ($vendor == 3) {
  2848. $port_oid = dot1qPortVlanEntry . "." . $port;
  2849. }
  2850. //allied telesys
  2851. if ($vendor == 8) {
  2852. $port_oid = dot1qPortVlanEntry . "." . $port;
  2853. }
  2854. $port_vlan = get_snmp($ip, $community, $version, $port_oid);
  2855. $port_vlan = preg_replace('/.*\:/', '', $port_vlan);
  2856. $port_vlan = intval(trim($port_vlan));
  2857. return $port_vlan;
  2858. }
  2859. function get_ports_poe_state($vendor_id, $ip, $community = 'public', $version = '2')
  2860. {
  2861. if (!isset($vendor_id)) {
  2862. return;
  2863. }
  2864. if (!isset($ip)) {
  2865. return;
  2866. }
  2867. // default poe oid
  2868. $poe_status = PETH_PSE_PORT_ADMIN_ENABLE;
  2869. if ($vendor_id == 3) { $poe_status = HUAWEI_POE_OID; }
  2870. if ($vendor_id == 6) { $poe_status = SNR_POE_OID; }
  2871. if ($vendor_id == 8) { $poe_status = ALLIED_POE_OID; }
  2872. if ($vendor_id == 9) { $poe_status = MIKROTIK_POE_OID; }
  2873. if ($vendor_id == 10) { $poe_status = NETGEAR_POE_OID; }
  2874. if ($vendor_id == 15) { $poe_status = HP_POE_OID; }
  2875. if ($vendor_id == 69) { $poe_status = TPLINK_POE_OID; }
  2876. $result = [];
  2877. $c_state = walk_snmp($ip, $community, $version, $poe_status);
  2878. if (!empty($c_state)) {
  2879. foreach ($c_state as $key => $value) {
  2880. if (empty($value)) { $value = ''; }
  2881. $key = trim($key);
  2882. $value = parse_snmp_value($value);
  2883. $port = NULL;
  2884. if (preg_match('/\.(\d{1,10})$/', $key, $matches)) {
  2885. $port = preg_replace('/^\./', '', $matches[0]);
  2886. $result[$port] = $value;
  2887. // patch for mikrotik
  2888. if ($vendor_id == 9) {
  2889. if ($value == 1) { $result[$port]=2; }
  2890. if ($value > 1) { $result[$port]=1; }
  2891. }
  2892. //patch for tplink
  2893. if ($vendor_id == 69) {
  2894. if ($value == 0) { $result[$port]=2; }
  2895. if ($value >= 1) { $result[$port]=1; }
  2896. }
  2897. }
  2898. }
  2899. }
  2900. return $result;
  2901. }
  2902. function get_port_poe_state($vendor_id, $port, $port_snmp_index, $ip, $community = 'public', $version = '2')
  2903. {
  2904. if (!isset($port)) {
  2905. return;
  2906. }
  2907. if (!isset($ip)) {
  2908. return;
  2909. }
  2910. // default poe oid
  2911. $poe_status = PETH_PSE_PORT_ADMIN_ENABLE . "." . $port_snmp_index;
  2912. if ($vendor_id == 3) {
  2913. $poe_status = HUAWEI_POE_OID . "." . $port_snmp_index;
  2914. }
  2915. if ($vendor_id == 6) {
  2916. $poe_status = SNR_POE_OID . "." . $port_snmp_index;
  2917. }
  2918. if ($vendor_id == 8) {
  2919. $poe_status = ALLIED_POE_OID . "." . $port_snmp_index;
  2920. }
  2921. if ($vendor_id == 15) {
  2922. $poe_status = HP_POE_OID . "." . $port_snmp_index;
  2923. }
  2924. if ($vendor_id == 9) {
  2925. $poe_status = MIKROTIK_POE_OID . "." . $port_snmp_index;
  2926. }
  2927. if ($vendor_id == 10) {
  2928. $poe_status = NETGEAR_POE_OID . "." . $port_snmp_index;
  2929. }
  2930. if ($vendor_id == 69) {
  2931. $poe_status = TPLINK_POE_OID . "." . $port;
  2932. }
  2933. $result = '';
  2934. $c_state = get_snmp($ip, $community, $version, $poe_status);
  2935. if (!empty($c_state)) {
  2936. $p_state = parse_snmp_value($c_state);
  2937. // patch for mikrotik
  2938. if ($vendor_id == 9) {
  2939. if ($p_state == 1) {
  2940. return 2;
  2941. }
  2942. if ($p_state > 1) {
  2943. return 1;
  2944. }
  2945. }
  2946. //patch for tplink
  2947. if ($vendor_id == 69) {
  2948. if ($p_state == 0) {
  2949. return 2;
  2950. }
  2951. if ($p_state >= 1) {
  2952. return 1;
  2953. }
  2954. }
  2955. return $p_state;
  2956. }
  2957. return;
  2958. }
  2959. function set_port_poe_state($vendor_id, $port, $port_snmp_index, $ip, $community='public', $version='2', $state)
  2960. {
  2961. if (!isset($ip)) {
  2962. return;
  2963. }
  2964. //default poe status
  2965. $poe_enable = 1;
  2966. $poe_disable = 2;
  2967. // default poe oid
  2968. $poe_status = PETH_PSE_PORT_ADMIN_ENABLE . "." . $port_snmp_index;
  2969. if ($vendor_id == 3) {
  2970. $poe_status = HUAWEI_POE_OID . "." . $port_snmp_index;
  2971. }
  2972. if ($vendor_id == 8) {
  2973. $poe_status = ALLIED_POE_OID . "." . $port_snmp_index;
  2974. }
  2975. if ($vendor_id == 15) {
  2976. $poe_status = HP_POE_OID . "." . $port_snmp_index;
  2977. }
  2978. if ($vendor_id == 10) {
  2979. $poe_status = NETGEAR_POE_OID . "." . $port_snmp_index;
  2980. }
  2981. if ($vendor_id == 69) {
  2982. $poe_status = TPLINK_POE_OID . "." . $port;
  2983. $poe_enable = 1;
  2984. $poe_disable = 0;
  2985. }
  2986. if ($state) {
  2987. // enable port
  2988. $c_state = set_snmp($ip, $community, $version, $poe_status, 'i', $poe_enable);
  2989. return $c_state;
  2990. } else {
  2991. // disable port
  2992. $c_state = set_snmp($ip, $community, $version, $poe_status, 'i', $poe_disable);
  2993. return $c_state;
  2994. }
  2995. }
  2996. function get_ports_poe_detail($vendor_id, $ip, $community='public', $version='2')
  2997. {
  2998. if (!isset($vendor_id)) {
  2999. return;
  3000. }
  3001. if (!isset($ip)) {
  3002. return;
  3003. }
  3004. $result = [];
  3005. $poe_class = PETH_PSE_PORT_POE_CLASS;
  3006. // eltex
  3007. if ($vendor_id == 2) {
  3008. $poe_power = ELTEX_POE_USAGE;
  3009. $poe_current = ELTEX_POE_CURRENT;
  3010. $poe_volt = ELTEX_POE_VOLT;
  3011. }
  3012. // huawei
  3013. if ($vendor_id == 3) {
  3014. $poe_power = HUAWEI_POE_USAGE;
  3015. $poe_current = HUAWEI_POE_CURRENT;
  3016. $poe_volt = HUAWEI_POE_VOLT;
  3017. }
  3018. // snr
  3019. if ($vendor_id == 6) {
  3020. $poe_class = SNR_POE_CLASS;
  3021. $poe_power = SNR_POE_USAGE;
  3022. $poe_current = SNR_POE_CURRENT;
  3023. $poe_volt = SNR_POE_VOLT;
  3024. }
  3025. // AT
  3026. if ($vendor_id == 8) {
  3027. $poe_power = ALLIED_POE_USAGE;
  3028. $poe_current = ALLIED_POE_CURRENT;
  3029. $poe_volt = ALLIED_POE_VOLT;
  3030. }
  3031. // mikrotik
  3032. if ($vendor_id == 9) {
  3033. $poe_power = MIKROTIK_POE_USAGE;
  3034. $poe_current = MIKROTIK_POE_CURRENT;
  3035. $poe_volt = MIKROTIK_POE_VOLT;
  3036. }
  3037. // netgear
  3038. if ($vendor_id == 10) {
  3039. $poe_power = NETGEAR_POE_USAGE;
  3040. $poe_current = NETGEAR_POE_CURRENT;
  3041. $poe_volt = NETGEAR_POE_VOLT;
  3042. }
  3043. // HP
  3044. if ($vendor_id == 15) {
  3045. $poe_power = HP_POE_USAGE;
  3046. $poe_volt = HP_POE_VOLT;
  3047. }
  3048. // TP-Link
  3049. if ($vendor_id == 69) {
  3050. $poe_power = TPLINK_POE_USAGE;
  3051. $poe_current = TPLINK_POE_CURRENT;
  3052. $poe_volt = TPLINK_POE_VOLT;
  3053. $poe_class = TPLINK_POE_CLASS;
  3054. }
  3055. if (isset($poe_power)) {
  3056. $c_power = walk_snmp($ip, $community, $version, $poe_power);
  3057. if (isset($c_power)) {
  3058. foreach ($c_power as $key => $value) {
  3059. if (empty($value)) { $value = 'INT:0'; }
  3060. $key = trim($key);
  3061. $p_power = parse_snmp_value($value);
  3062. $port = NULL;
  3063. if (preg_match('/\.(\d{1,10})$/', $key, $matches)) {
  3064. $port = preg_replace('/^\./', '', $matches[0]);
  3065. $result[$port]['power']=0;
  3066. $result[$port]['power_display']='';
  3067. switch ($vendor_id) {
  3068. case 9://mikrotik
  3069. $p_power = round($p_power / 10, 2);
  3070. break;
  3071. case 69://tplink
  3072. $p_power = round($p_power / 10, 2);
  3073. break;
  3074. default:
  3075. $p_power = round($p_power / 1000, 2);
  3076. break;
  3077. }
  3078. if ($p_power > 0) {
  3079. $result[$port]['power'] = $p_power;
  3080. $result[$port]['power_display'] = 'P: ' . $p_power . ' W';
  3081. }
  3082. }
  3083. }
  3084. }
  3085. }
  3086. if (isset($poe_current)) {
  3087. $c_current = walk_snmp($ip, $community, $version, $poe_current);
  3088. if (isset($c_current)) {
  3089. foreach ($c_current as $key => $value) {
  3090. if (empty($value)) { $value = 'INT:0'; }
  3091. $key = trim($key);
  3092. $p_current = parse_snmp_value($value);
  3093. $port = NULL;
  3094. if (preg_match('/\.(\d{1,10})$/', $key, $matches)) {
  3095. $port = preg_replace('/^\./', '', $matches[0]);
  3096. $result[$port]['current']=0;
  3097. $result[$port]['current_display']='';
  3098. if ($p_current > 0) {
  3099. $result[$port]['current'] = $p_current;
  3100. $result[$port]['current_display'] = 'C: ' . $p_current . ' mA';
  3101. }
  3102. }
  3103. }
  3104. }
  3105. }
  3106. if (isset($poe_volt)) {
  3107. $c_volt = walk_snmp($ip, $community, $version, $poe_volt);
  3108. if (isset($c_volt)) {
  3109. foreach ($c_volt as $key => $value) {
  3110. if (empty($value)) { $value = 'INT:0'; }
  3111. $key = trim($key);
  3112. $p_volt = parse_snmp_value($value);
  3113. $port = NULL;
  3114. if (preg_match('/\.(\d{1,10})$/', $key, $matches)) {
  3115. $port = preg_replace('/^\./', '', $matches[0]);
  3116. $result[$port]['volt'] = 0;
  3117. $result[$port]['volt_display'] ='';
  3118. switch ($vendor_id) {
  3119. case 2:
  3120. case 8:
  3121. $p_volt = round($p_volt / 1000, 2);
  3122. break;
  3123. case 9:
  3124. case 69:
  3125. $p_volt = round($p_volt / 10, 2);
  3126. break;
  3127. case 15:
  3128. $p_volt = round($p_volt / 100, 2);
  3129. break;
  3130. }
  3131. if ($p_volt > 0 and $result[$port]['power'] > 0) {
  3132. $result[$port]['volt'] = $p_volt;
  3133. $result[$port]['volt_display'] = ' V: ' . $p_volt . " V";
  3134. }
  3135. }
  3136. }
  3137. }
  3138. }
  3139. if (isset($poe_class)) {
  3140. $c_class = walk_snmp($ip, $community, $version, $poe_class);
  3141. if (isset($c_class)) {
  3142. foreach ($c_class as $key => $value) {
  3143. if (empty($value)) { $value = 'INT:0'; }
  3144. $key = trim($key);
  3145. $p_class = parse_snmp_value($value);
  3146. $port = NULL;
  3147. if (preg_match('/\.(\d{1,10})$/', $key, $matches)) {
  3148. $port = preg_replace('/^\./', '', $matches[0]);
  3149. $result[$port]['class'] = 0;
  3150. $result[$port]['class_display']='';
  3151. switch ($vendor_id) {
  3152. case 69:
  3153. if ($p_class > 0 and $result[$port]['power'] > 0) {
  3154. if ($p_class == 7) { $p_class = 'class-not-defined'; }
  3155. $result[$port]['class_display'] = 'Class: ' . $p_class;
  3156. $result[$port]['class'] = $p_class;
  3157. }
  3158. break;
  3159. default:
  3160. if ($p_class > 0 and $result[$port]['power'] > 0) {
  3161. $result[$port]['class_display'] = 'Class: ' . ($p_class - 1);
  3162. $result[$port]['class'] = $p_class-1;
  3163. }
  3164. break;
  3165. }
  3166. }
  3167. }
  3168. }
  3169. }
  3170. foreach ($result as &$port) {
  3171. if (!isset($port['power'])) { $port['power'] = 0; }
  3172. if (!isset($port['current'])) { $port['current'] = 0; }
  3173. if (!isset($port['volt'])) { $port['volt'] = 0; }
  3174. if (!isset($port['class'])) { $port['class'] = 0; }
  3175. }
  3176. unset($port);
  3177. return $result;
  3178. }
  3179. function get_port_poe_detail($vendor_id, $port, $port_snmp_index, $ip, $community, $version)
  3180. {
  3181. if (!isset($port) or !isset($port_snmp_index)) {
  3182. return;
  3183. }
  3184. if (!isset($ip)) {
  3185. return;
  3186. }
  3187. if (!isset($community)) {
  3188. $community = 'public';
  3189. }
  3190. if (!isset($version)) {
  3191. $version = '2';
  3192. }
  3193. $result = '';
  3194. $poe_class = PETH_PSE_PORT_POE_CLASS . $port_snmp_index;
  3195. // eltex
  3196. if ($vendor_id == 2) {
  3197. $poe_power = ELTEX_POE_USAGE . '.' . $port_snmp_index;
  3198. $poe_current = ELTEX_POE_CURRENT . '.' . $port_snmp_index;
  3199. $poe_volt = ELTEX_POE_VOLT . '.' . $port_snmp_index;
  3200. }
  3201. // huawei
  3202. if ($vendor_id == 3) {
  3203. $poe_power = HUAWEI_POE_USAGE . '.' . $port_snmp_index;
  3204. $poe_current = HUAWEI_POE_CURRENT . '.' . $port_snmp_index;
  3205. $poe_volt = HUAWEI_POE_VOLT . '.' . $port_snmp_index;
  3206. }
  3207. // snr
  3208. if ($vendor_id == 6) {
  3209. $poe_class = SNR_POE_CLASS . '.' . $port_snmp_index;
  3210. $poe_power = SNR_POE_USAGE . '.' . $port_snmp_index;
  3211. $poe_current = SNR_POE_CURRENT . '.' . $port_snmp_index;
  3212. $poe_volt = SNR_POE_VOLT . '.' . $port_snmp_index;
  3213. }
  3214. // AT
  3215. if ($vendor_id == 8) {
  3216. $poe_power = ALLIED_POE_USAGE . '.' . $port_snmp_index;
  3217. $poe_current = ALLIED_POE_CURRENT . '.' . $port_snmp_index;
  3218. $poe_volt = ALLIED_POE_VOLT . '.' . $port_snmp_index;
  3219. }
  3220. // mikrotik
  3221. if ($vendor_id == 9) {
  3222. $poe_power = MIKROTIK_POE_USAGE . '.' . $port_snmp_index;
  3223. $poe_current = MIKROTIK_POE_CURRENT . '.' . $port_snmp_index;
  3224. $poe_volt = MIKROTIK_POE_VOLT . '.' . $port_snmp_index;
  3225. }
  3226. // netgear
  3227. if ($vendor_id == 10) {
  3228. $poe_power = NETGEAR_POE_USAGE . '.' . $port_snmp_index;
  3229. $poe_current = NETGEAR_POE_CURRENT . '.' . $port_snmp_index;
  3230. $poe_volt = NETGEAR_POE_VOLT . '.' . $port_snmp_index;
  3231. }
  3232. // HP
  3233. if ($vendor_id == 15) {
  3234. $poe_power = HP_POE_USAGE . '.' . $port_snmp_index;
  3235. $poe_volt = HP_POE_VOLT . '.' . $port_snmp_index;
  3236. }
  3237. // TP-Link
  3238. if ($vendor_id == 69) {
  3239. $poe_power = TPLINK_POE_USAGE . '.' . $port;
  3240. $poe_current = TPLINK_POE_CURRENT . '.' . $port;
  3241. $poe_volt = TPLINK_POE_VOLT . '.' . $port;
  3242. $poe_class = TPLINK_POE_CLASS . "." . $port;
  3243. }
  3244. if (isset($poe_power)) {
  3245. $c_power = get_snmp($ip, $community, $version, $poe_power);
  3246. if (isset($c_power)) {
  3247. $p_power = parse_snmp_value($c_power);
  3248. switch ($vendor_id) {
  3249. case 9:
  3250. $p_power = round($p_power / 10, 2);
  3251. break;
  3252. case 69:
  3253. $p_power = round($p_power / 10, 2);
  3254. break;
  3255. default:
  3256. $p_power = round($p_power / 1000, 2);
  3257. break;
  3258. }
  3259. if ($p_power > 0) {
  3260. $result .= ' P: ' . $p_power . ' W';
  3261. }
  3262. }
  3263. }
  3264. if (isset($poe_current)) {
  3265. $c_current = get_snmp($ip, $community, $version, $poe_current);
  3266. if (isset($c_current)) {
  3267. $p_current = parse_snmp_value($c_current);
  3268. if ($p_current > 0) {
  3269. $result .= ' C: ' . $p_current . ' mA';
  3270. }
  3271. }
  3272. }
  3273. if (isset($poe_volt)) {
  3274. $c_volt = get_snmp($ip, $community, $version, $poe_volt);
  3275. if (isset($c_volt)) {
  3276. $p_volt = parse_snmp_value($c_volt);
  3277. switch ($vendor_id) {
  3278. case 2:
  3279. case 8:
  3280. $p_volt = round($p_volt / 1000, 2);
  3281. break;
  3282. case 9:
  3283. case 69:
  3284. $p_volt = round($p_volt / 10, 2);
  3285. break;
  3286. case 15:
  3287. $p_volt = round($p_volt / 100, 2);
  3288. break;
  3289. }
  3290. if ($p_volt > 0 and $p_power > 0) {
  3291. $result .= ' V: ' . $p_volt . " V";
  3292. }
  3293. }
  3294. }
  3295. if (isset($poe_class)) {
  3296. $c_class = get_snmp($ip, $community, $version, $poe_class);
  3297. if (isset($c_class)) {
  3298. $p_class = parse_snmp_value($c_class);
  3299. switch ($vendor_id) {
  3300. case 69:
  3301. if ($p_class > 0 and $p_power > 0) {
  3302. if ($p_class == 7) {
  3303. $p_class = 'class-not-defined';
  3304. }
  3305. $result .= ' Class: ' . $p_class;
  3306. }
  3307. break;
  3308. default:
  3309. if ($p_class > 0 and $p_power > 0) {
  3310. $result .= ' Class: ' . ($p_class - 1);
  3311. }
  3312. break;
  3313. }
  3314. }
  3315. }
  3316. return $result;
  3317. }
  3318. function get_snmp($ip, $community, $version, $oid)
  3319. {
  3320. snmp_set_oid_output_format(SNMP_OID_OUTPUT_NUMERIC);
  3321. $result = NULL;
  3322. try {
  3323. if ($version == 2) {
  3324. $result = snmp2_get($ip, $community, $oid, SNMP_timeout, SNMP_retry);
  3325. }
  3326. if ($version == 1) {
  3327. $result = snmpget($ip, $community, $oid, SNMP_timeout, SNMP_retry);
  3328. }
  3329. if (!$result) { $result = ''; }
  3330. } catch (Exception $e) {
  3331. # echo 'Caught exception: ', $e->getMessage(), "\n";
  3332. $result = NULL;
  3333. }
  3334. return $result;
  3335. }
  3336. function set_snmp($ip, $community, $version, $oid, $field, $value)
  3337. {
  3338. $result = false;
  3339. try {
  3340. if ($version == 2) {
  3341. $result = snmp2_set($ip, $community, $oid, $field, $value, SNMP_timeout, SNMP_retry);
  3342. }
  3343. if ($version == 1) {
  3344. $result = snmpset($ip, $community, $oid, $field, $value, SNMP_timeout, SNMP_retry);
  3345. }
  3346. } catch (Exception $e) {
  3347. # echo 'Caught exception: ', $e->getMessage(), "\n";
  3348. $result = false;
  3349. }
  3350. return $result;
  3351. }
  3352. function set_port_state($vendor_id, $port, $ip, $community, $version, $state)
  3353. {
  3354. // port -> snmp_index!!!
  3355. if (!isset($port)) {
  3356. return;
  3357. }
  3358. if (!isset($ip)) {
  3359. return;
  3360. }
  3361. if (!isset($community)) {
  3362. $community = 'public';
  3363. }
  3364. if (!isset($version)) {
  3365. $version = '2';
  3366. }
  3367. $port_status = PORT_ADMIN_STATUS_OID .'.' . $port;
  3368. if ($state == 1) {
  3369. // enable port
  3370. $c_state = set_snmp($ip, $community, $version, $port_status, 'i', 1);
  3371. return $c_state;
  3372. } else {
  3373. // disable port
  3374. $c_state = set_snmp($ip, $community, $version, $port_status, 'i', 2);
  3375. return $c_state;
  3376. }
  3377. }
  3378. function set_port_for_group($db, $group_id, $place_id, $state)
  3379. {
  3380. $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;
  3381. $auth_list = mysqli_query($db, $authSQL);
  3382. LOG_INFO($db, 'Mass port state change started!');
  3383. // get auth list for group
  3384. while (list($a_id, $a_name, $a_ip) = mysqli_fetch_array($auth_list)) {
  3385. // get device and port for auth
  3386. if ($place_id == 0) {
  3387. $place_filter = '';
  3388. } else {
  3389. $place_filter = 'D.building_id=' . $place_id . ' and ';
  3390. }
  3391. $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';
  3392. $dev_info = mysqli_query($db, $devSQL);
  3393. 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);
  3394. if (!isset($d_id)) {
  3395. continue;
  3396. }
  3397. if ($state == 1) {
  3398. $mode = 'enable';
  3399. run_sql($db, "Update User_auth set nagios_handler='restart-port' WHERE id=$a_id and nagios_handler='manual-mode'");
  3400. } else {
  3401. $mode = 'disable';
  3402. run_sql($db, "Update User_auth set nagios_handler='manual-mode' WHERE id=$a_id and nagios_handler='restart-port'");
  3403. }
  3404. LOG_INFO($db, "At device $d_name [$d_ip] $mode port $d_port for auth_id: $a_id ($a_ip [$a_name])");
  3405. set_port_state($d_vendor_id, $d_snmp_index, $d_ip, $d_community, $d_snmp, $state);
  3406. set_port_poe_state($d_vendor_id, $d_port, $d_snmp_index, $d_ip, $d_community, $d_snmp, $state);
  3407. }
  3408. LOG_INFO($db, 'Mass port state change stopped.');
  3409. }
  3410. function get_vendor($db, $mac)
  3411. {
  3412. $mac = mac_dotted($mac);
  3413. $mac5 = substr($mac, 0, 14);
  3414. $mac4 = substr($mac, 0, 11);
  3415. $mac3 = substr($mac, 0, 8);
  3416. $vendor = get_record_sql($db, 'SELECT companyName,companyAddress FROM mac_vendors WHERE oui="' . $mac . '"');
  3417. if (empty($vendor)) {
  3418. $vendor = get_record_sql($db, 'SELECT companyName,companyAddress FROM mac_vendors WHERE oui="' . $mac5 . '"');
  3419. }
  3420. if (empty($vendor)) {
  3421. $vendor = get_record_sql($db, 'SELECT companyName,companyAddress FROM mac_vendors WHERE oui="' . $mac4 . '"');
  3422. }
  3423. if (empty($vendor)) {
  3424. $vendor = get_record_sql($db, 'SELECT companyName,companyAddress FROM mac_vendors WHERE oui="' . $mac3 . '"');
  3425. }
  3426. $result = '';
  3427. if (!empty($vendor)) {
  3428. $result = $vendor['companyName'];
  3429. if (!empty($vendor['companyAddress'])) {
  3430. $result = $vendor['companyAddress'];
  3431. }
  3432. }
  3433. return $result;
  3434. }
  3435. function get_ports_state_detail($ip, $community='public', $version='2') {
  3436. if (!isset($ip)) {
  3437. return;
  3438. }
  3439. $result = [];
  3440. //post status
  3441. $p_state = walk_snmp($ip, $community, $version, PORT_STATUS_OID);
  3442. if (!empty($p_state)) {
  3443. foreach ($p_state as $key => $value) {
  3444. if (empty($value)) { $value = ''; }
  3445. $key = trim($key);
  3446. $value = parse_snmp_value($value);
  3447. $port = NULL;
  3448. if (preg_match('/\.(\d{1,10})$/', $key, $matches)) {
  3449. $port = preg_replace('/^\./', '', $matches[0]);
  3450. $result[$port]['status']=$value;
  3451. $result[$port]['admin_status']=0;
  3452. $result[$port]['speed']=0;
  3453. $result[$port]['errors']=0;
  3454. }
  3455. }
  3456. }
  3457. //admin state
  3458. $p_admin = walk_snmp($ip, $community, $version, PORT_ADMIN_STATUS_OID);
  3459. if (!empty($p_admin)) {
  3460. foreach ($p_admin as $key => $value) {
  3461. if (empty($value)) { $value = ''; }
  3462. $key = trim($key);
  3463. $value = parse_snmp_value($value);
  3464. $port = NULL;
  3465. if (preg_match('/\.(\d{1,10})$/', $key, $matches)) {
  3466. $port = preg_replace('/^\./', '', $matches[0]);
  3467. $result[$port]['admin_status']=$value;
  3468. }
  3469. }
  3470. }
  3471. //port speed
  3472. $p_speed = walk_snmp($ip,$community,$version,PORT_SPEED_OID);
  3473. if (!empty($p_speed)) {
  3474. foreach ($p_speed as $key => $value) {
  3475. if (empty($value)) { $value = 'INT:0'; }
  3476. $key = trim($key);
  3477. $value = parse_snmp_value($value);
  3478. $port = NULL;
  3479. if (preg_match('/\.(\d{1,10})$/', $key, $matches)) {
  3480. $port = preg_replace('/^\./', '', $matches[0]);
  3481. $result[$port]['speed']=$value;
  3482. }
  3483. }
  3484. }
  3485. //errors at
  3486. $p_errors = walk_snmp($ip,$community,$version,PORT_ERRORS_OID);
  3487. if (!empty($p_errors)) {
  3488. foreach ($p_errors as $key => $value) {
  3489. if (empty($value)) { $value = 'INT:0'; }
  3490. $key = trim($key);
  3491. $value = parse_snmp_value($value);
  3492. $port = NULL;
  3493. if (preg_match('/\.(\d{1,10})$/', $key, $matches)) {
  3494. $port = preg_replace('/^\./', '', $matches[0]);
  3495. $result[$port]['errors']=$value;
  3496. }
  3497. }
  3498. }
  3499. return $result;
  3500. }
  3501. function get_port_state_detail($port, $ip, $community, $version)
  3502. {
  3503. if (!isset($port)) {
  3504. return;
  3505. }
  3506. if (!isset($ip)) {
  3507. return;
  3508. }
  3509. if (!isset($community)) {
  3510. $community = 'public';
  3511. }
  3512. if (!isset($version)) {
  3513. $version = '2';
  3514. }
  3515. // if (!is_up($ip)) { return; }
  3516. $oper = PORT_STATUS_OID .'.'. $port;
  3517. $admin = PORT_ADMIN_STATUS_OID .'.' . $port;
  3518. $speed = PORT_SPEED_OID .'.'. $port;
  3519. $errors = PORT_ERRORS_OID .'.'. $port;
  3520. $result = '';
  3521. $c_state = get_snmp($ip, $community, $version, $oper);
  3522. $p_state = parse_snmp_value($c_state);
  3523. $c_admin = get_snmp($ip, $community, $version, $admin);
  3524. $p_admin = parse_snmp_value($c_admin);
  3525. if ($p_state == 1) {
  3526. $c_speed = get_snmp($ip, $community, $version, $speed);
  3527. } else {
  3528. $c_speed = 'INT:0';
  3529. }
  3530. $p_speed = parse_snmp_value($c_speed);
  3531. $c_errors = get_snmp($ip, $community, $version, $errors);
  3532. $p_errors = parse_snmp_value($c_errors);
  3533. $result = $p_state . ";" . $p_admin . ";" . $p_speed . ";" . $p_errors;
  3534. return $result;
  3535. }
  3536. function parse_snmp_value($value)
  3537. {
  3538. if (empty($value)) {
  3539. return NULL;
  3540. }
  3541. if (!preg_match('/:/',$value)) { return NULL; }
  3542. list($p_type, $p_value) = explode(':', $value);
  3543. if (empty($p_value)) { return NULL; }
  3544. $p_value = trim($p_value);
  3545. $p_value = preg_replace('/^\"/', '', $p_value);
  3546. $p_value = preg_replace('/\"$/', '', $p_value);
  3547. $p_value = trim($p_value);
  3548. return $p_value;
  3549. }
  3550. function strHexToBin ( $number ) {
  3551. $result = '';
  3552. for ( $i = 0; $i < strlen($number); $i++ ){
  3553. $conv = base_convert($number[$i], 16, 2);
  3554. $result .= str_pad($conv, 4, '0', STR_PAD_LEFT);
  3555. }
  3556. return $result;
  3557. }
  3558. function dec_to_hex($mac)
  3559. {
  3560. if (!isset($mac)) {
  3561. return;
  3562. }
  3563. $mac_array = explode('.', $mac);
  3564. for ($i = 0; $i < count($mac_array); $i++) {
  3565. $hex_i = dechex($mac_array[$i]);
  3566. if (strlen($hex_i) == 1) {
  3567. $hex_i = "0" . $hex_i;
  3568. }
  3569. $mac_array[$i] = $hex_i;
  3570. }
  3571. $hex_mac = implode(':', $mac_array);
  3572. return $hex_mac;
  3573. }
  3574. function mac_simplify($mac)
  3575. {
  3576. if (!isset($mac)) {
  3577. return;
  3578. }
  3579. $mac = strtolower(trim($mac));
  3580. $mac = preg_replace('/(\.|:|-)/', '', $mac);
  3581. return $mac;
  3582. }
  3583. function mac_dotted($mac)
  3584. {
  3585. if (!isset($mac)) {
  3586. return;
  3587. }
  3588. $mac = mac_simplify($mac);
  3589. $mac = preg_replace('/(\S{2})(\S{2})?(\S{2})?(\S{2})?(\S{2})?(\S{2})?/', '$1:$2:$3:$4:$5:$6', $mac);
  3590. $mac = preg_replace('/\:+$/','',$mac,5);
  3591. return $mac;
  3592. }
  3593. function unbind_ports($db, $device_id)
  3594. {
  3595. $target = mysqli_query($db, "SELECT U.target_port_id,U.id FROM device_ports U WHERE U.device_id=$device_id");
  3596. while (list($target_id, $id) = mysqli_fetch_array($target)) {
  3597. run_sql($db, "UPDATE device_ports SET target_port_id=0 WHERE target_port_id=" . $id);
  3598. run_sql($db, "UPDATE device_ports SET target_port_id=0 WHERE id=" . $id);
  3599. }
  3600. }
  3601. function bind_ports($db, $port_id, $target_id)
  3602. {
  3603. $old_target = mysqli_query($db, "SELECT U.target_port_id FROM device_ports U WHERE U.id=$port_id");
  3604. list($old_target_id) = mysqli_fetch_array($old_target);
  3605. // unbind current connection
  3606. $new['target_port_id'] = 0;
  3607. update_record($db, "device_ports", "id='$port_id'", $new);
  3608. if (isset($old_target_id)) {
  3609. update_record($db, "device_ports", "id='$old_target_id'", $new);
  3610. }
  3611. // new link
  3612. if (isset($target_id) and $target_id > 0) {
  3613. $new['target_port_id'] = $target_id;
  3614. update_record($db, "device_ports", "id='$port_id'", $new);
  3615. $new['target_port_id'] = $port_id;
  3616. update_record($db, "device_ports", "id='$target_id'", $new);
  3617. }
  3618. }
  3619. function expand_device_name($db, $name)
  3620. {
  3621. $device_id = get_device_id($db, $name);
  3622. $result = $name;
  3623. if (isset($device_id) and $device_id > 0) {
  3624. $result = '<a href=/admin/devices/editdevice.php?id=' . $device_id . '>' . $name . '</a>';
  3625. }
  3626. return $result;
  3627. }
  3628. function expand_mac($db, $msg)
  3629. {
  3630. if (!isset($msg)) {
  3631. return;
  3632. }
  3633. $mac = mac_dotted($msg);
  3634. $vendor_info = get_vendor($db, $mac);
  3635. $result = ' <p title="' . $vendor_info . '"><a href=/admin/logs/mac.php?mac=' . $mac . '>' . $mac . '</a></p>';
  3636. return $result;
  3637. }
  3638. function expand_log_str($db, $msg)
  3639. {
  3640. if (!isset($msg)) {
  3641. return;
  3642. }
  3643. $auth_pattern = '/(auth_id:|auth|auth id:|auth id)\s+(\d+)\s+/i';
  3644. $auth_replace = '<a href=/admin/users/editauth.php?id=${2}>auth_id:${2}</a> ';
  3645. $result = preg_replace($auth_pattern, $auth_replace, $msg);
  3646. $user_pattern = '/(user_id:|user|user id:|user id)\s+(\d+)\s+/i';
  3647. $user_replace = '<a href=/admin/users/edituser.php?id=${2}>user_id:${2}</a> ';
  3648. $result = preg_replace($user_pattern, $user_replace, $result);
  3649. $mac_pattern = '/\s+\[(\w{12})\]\s+/i';
  3650. preg_match($mac_pattern, $result, $matches);
  3651. if (isset($matches[1])) {
  3652. $mac = $matches[1];
  3653. $mac = mac_dotted($mac);
  3654. # $vendor_info = get_vendor($db,$mac);
  3655. # $mac_replace = ' <p title="'.$vendor_info.'"><a href=/admin/logs/mac.php?mac='.$mac.'>'.$mac.'</a></p>';
  3656. $mac_replace = ' <a href=/admin/logs/mac.php?mac=' . $mac . '>' . $mac . '</a> ';
  3657. $result = preg_replace($mac_pattern, $mac_replace, $result);
  3658. }
  3659. $mac_pattern = '/\s+mac:\s+([\w\:]{17})$/i';
  3660. preg_match($mac_pattern, $result, $matches);
  3661. if (isset($matches[1])) {
  3662. $mac = $matches[1];
  3663. $mac = mac_dotted($mac);
  3664. # $vendor_info = get_vendor($db,$mac);
  3665. # $mac_replace = ' mac: <p title="'.$vendor_info.'"><a href=/admin/logs/mac.php?mac='.$mac.'>'.$mac.'</a></p>';
  3666. $mac_replace = ' mac: <a href=/admin/logs/mac.php?mac=' . $mac . '>' . $mac . '</a> ';
  3667. $result = preg_replace($mac_pattern, $mac_replace, $result);
  3668. }
  3669. $device_pattern = '/at device\s+([\w\.\-]+)/i';
  3670. preg_match($device_pattern, $result, $matches);
  3671. if (isset($matches[1])) {
  3672. $device_name = $matches[1];
  3673. $device_id = get_device_id($db, $device_name);
  3674. if (isset($device_id) and $device_id > 0) {
  3675. $device_replace = 'at device <a href=/admin/devices/editdevice.php?id=' . $device_id . '>${1}</a> ';
  3676. $result = preg_replace($device_pattern, $device_replace, $result);
  3677. }
  3678. }
  3679. $device_pattern = '/(device_id:|device id:|device id|device_id)\s+(\d+)\s+/i';
  3680. $device_replace = 'device_id: <a href=/admin/devices/editdevice.php?id=${2}>${2}</a> ';
  3681. $result = preg_replace($device_pattern, $device_replace, $result);
  3682. $ip_pattern = '/\s+ip\:\s+(\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})\s+/i';
  3683. preg_match($ip_pattern, $result, $matches);
  3684. if (isset($matches[1])) {
  3685. $ip = $matches[1];
  3686. $auth_id = get_auth_by_ip($db, $ip);
  3687. if (isset($auth_id) and $auth_id > 0) {
  3688. $auth_replace = ' ip: <a href=/admin/users/editauth.php?id=' . $auth_id . '>${1}</a> ';
  3689. $result = preg_replace($ip_pattern, $auth_replace, $result);
  3690. }
  3691. }
  3692. return $result;
  3693. }
  3694. function get_record_field($db, $table, $field, $filter)
  3695. {
  3696. if (!isset($table)) {
  3697. LOG_ERROR($db, "Search in unknown table! Skip command.");
  3698. return;
  3699. }
  3700. if (!isset($filter)) {
  3701. LOG_ERROR($db, "Search filter is empty! Skip command.");
  3702. return;
  3703. }
  3704. if (!isset($field)) {
  3705. LOG_ERROR($db, "Search field is empty! Skip command.");
  3706. return;
  3707. }
  3708. if (preg_match('/=$/', $filter)) {
  3709. LOG_ERROR($db, "Search record ($table) with illegal filter $filter! Skip command.");
  3710. return;
  3711. }
  3712. $old_sql = "SELECT $field FROM $table WHERE $filter LIMIT 1";
  3713. $old_record = mysqli_query($db, $old_sql) or LOG_ERROR($db, "SQL: $old_sql :" . mysqli_error($db));
  3714. $old = mysqli_fetch_array($old_record, MYSQLI_ASSOC);
  3715. foreach ($old as $key => $value) {
  3716. if (!isset($value) or $value === 'NULL') {
  3717. $value = '';
  3718. }
  3719. $result[$key] = $value;
  3720. }
  3721. return $result[$field];
  3722. }
  3723. function get_record($db, $table, $filter)
  3724. {
  3725. if (!isset($table)) {
  3726. LOG_ERROR($db, "Search in unknown table! Skip command.");
  3727. return;
  3728. }
  3729. if (!isset($filter)) {
  3730. LOG_ERROR($db, "Search filter is empty! Skip command.");
  3731. return;
  3732. }
  3733. if (preg_match('/=$/', $filter)) {
  3734. LOG_ERROR($db, "Search record ($table) with illegal filter $filter! Skip command.");
  3735. return;
  3736. }
  3737. $get_sql = "SELECT * FROM $table WHERE $filter LIMIT 1";
  3738. $get_record = mysqli_query($db, $get_sql);
  3739. if (!$get_record) {
  3740. LOG_ERROR($db, "SQL: $get_sql :" . mysqli_error($db));
  3741. return;
  3742. }
  3743. $fields = [];
  3744. while ($field = mysqli_fetch_field($get_record)) {
  3745. $f_table = $field->table;
  3746. $f_name = $field->name;
  3747. $fields[$f_table][$f_name]=$field;
  3748. }
  3749. $record = mysqli_fetch_array($get_record, MYSQLI_ASSOC);
  3750. $result = NULL;
  3751. if (!empty($record)) {
  3752. foreach ($record as $key => $value) {
  3753. if (!isset($value) or $value === 'NULL' or $value == NULL) {
  3754. if (!empty($key) and !empty($fields[$table]) and !empty($fields[$table][$key])) {
  3755. if (in_array($fields[$table][$key]->type,MYSQL_FIELD_DIGIT)) { $value = 0; }
  3756. if (in_array($fields[$table][$key]->type,MYSQL_FIELD_STRING)) { $value = ''; }
  3757. }
  3758. }
  3759. if (!empty($key)) { $result[$key] = $value; }
  3760. }
  3761. }
  3762. return $result;
  3763. }
  3764. function get_records($db, $table, $filter)
  3765. {
  3766. if (!isset($table)) {
  3767. LOG_ERROR($db, "Search in unknown table! Skip command.");
  3768. return;
  3769. }
  3770. if (isset($filter) and preg_match('/=$/', $filter)) {
  3771. LOG_ERROR($db, "Search record ($table) with illegal filter $filter! Skip command.");
  3772. return;
  3773. }
  3774. $s_filter = '';
  3775. if (isset($filter)) {
  3776. $s_filter = 'WHERE ' . $filter;
  3777. }
  3778. $get_sql = "SELECT * FROM $table $s_filter";
  3779. $get_record = mysqli_query($db, $get_sql);
  3780. if (!$get_record) {
  3781. LOG_ERROR($db, "SQL: $get_sql :" . mysqli_error($db));
  3782. return;
  3783. }
  3784. $fields = [];
  3785. while ($field = mysqli_fetch_field($get_record)) {
  3786. $f_table = $field->table;
  3787. $f_name = $field->name;
  3788. $fields[$f_table][$f_name]=$field;
  3789. }
  3790. $result = NULL;
  3791. $index = 0;
  3792. while ($rec = mysqli_fetch_array($get_record, MYSQLI_ASSOC)) {
  3793. foreach ($rec as $key => $value) {
  3794. if (!isset($value) or $value === 'NULL' or $value == NULL) {
  3795. if (!empty($key) and !empty($fields[$table]) and !empty($fields[$table][$key])) {
  3796. if (in_array($fields[$table][$key]->type,MYSQL_FIELD_DIGIT)) { $value = 0; }
  3797. if (in_array($fields[$table][$key]->type,MYSQL_FIELD_STRING)) { $value = ''; }
  3798. }
  3799. }
  3800. $result[$index][$key] = $value;
  3801. }
  3802. $index++;
  3803. }
  3804. return $result;
  3805. }
  3806. function get_records_sql($db, $sql)
  3807. {
  3808. $result = NULL;
  3809. if (empty($sql)) {
  3810. LOG_ERROR($db, "Empty query! Skip command.");
  3811. return $result;
  3812. }
  3813. $records = mysqli_query($db, $sql);
  3814. if (!$records) {
  3815. LOG_ERROR($db, "SQL: $sql :" . mysqli_error($db));
  3816. return $result;
  3817. }
  3818. $fields = [];
  3819. //we assume that fields with the same name have the same type
  3820. while ($field = mysqli_fetch_field($records)) {
  3821. $f_name = $field->name;
  3822. $fields[$f_name]=$field;
  3823. }
  3824. $index = 0;
  3825. while ($rec = mysqli_fetch_array($records, MYSQLI_ASSOC)) {
  3826. foreach ($rec as $key => $value) {
  3827. if (!isset($value) or $value === 'NULL' or $value == NULL) {
  3828. if (!empty($key) and !empty($fields[$key])) {
  3829. if (in_array($fields[$key]->type,MYSQL_FIELD_DIGIT)) { $value = 0; }
  3830. if (in_array($fields[$key]->type,MYSQL_FIELD_STRING)) { $value = ''; }
  3831. }
  3832. }
  3833. if (!empty($key)) { $result[$index][$key] = $value; }
  3834. }
  3835. $index++;
  3836. }
  3837. return $result;
  3838. }
  3839. function get_record_sql($db, $sql)
  3840. {
  3841. $result = NULL;
  3842. if (!isset($sql)) {
  3843. LOG_ERROR($db, "Empty query! Skip command.");
  3844. return $result;
  3845. }
  3846. $record = mysqli_query($db, $sql . " LIMIT 1");
  3847. if (!isset($record)) {
  3848. LOG_ERROR($db, "SQL: $sql LIMIT 1: " . mysqli_error($db));
  3849. return $result;
  3850. }
  3851. $fields = [];
  3852. //we assume that fields with the same name have the same type
  3853. while ($field = mysqli_fetch_field($record)) {
  3854. $f_name = $field->name;
  3855. $fields[$f_name]=$field;
  3856. }
  3857. $rec = mysqli_fetch_array($record, MYSQLI_ASSOC);
  3858. if (!empty($rec)) {
  3859. foreach ($rec as $key => $value) {
  3860. if (!isset($value) or $value === 'NULL' or $value == NULL) {
  3861. if (!empty($key) and !empty($fields[$key])) {
  3862. if (in_array($fields[$key]->type,MYSQL_FIELD_DIGIT)) { $value = 0; }
  3863. if (in_array($fields[$key]->type,MYSQL_FIELD_STRING)) { $value = ''; }
  3864. }
  3865. }
  3866. if (!empty($key)) { $result[$key] = $value; }
  3867. }
  3868. }
  3869. return $result;
  3870. }
  3871. function is_auth_bind_changed($db, $id, $ip, $mac)
  3872. {
  3873. $old_sql = "SELECT ip,mac FROM User_auth WHERE id=$id";
  3874. $old_record = get_record_sql($db, $old_sql);
  3875. if (empty($old_record["ip"]) or empty($old_record["mac"])) {
  3876. return 0;
  3877. }
  3878. if ($old_record["ip"] !== $ip or $old_record["mac"] !== $mac) {
  3879. LOG_VERBOSE($db, "Changed ip or mac for auth record!");
  3880. return 1;
  3881. }
  3882. return 0;
  3883. }
  3884. function copy_auth($db, $id, $new_auth)
  3885. {
  3886. $old_record = get_record_sql($db, "SELECT * FROM User_auth WHERE id=$id");
  3887. delete_record($db, "User_auth", "id=" . $id);
  3888. $new_auth["user_id"] = $old_record["user_id"];
  3889. $new_auth["changed"] = 1;
  3890. $changed_time = GetNowTimeString();
  3891. $new_auth["changed_time"] = $changed_time;
  3892. $new_id = insert_record($db, "User_auth", $new_auth);
  3893. LOG_VERBOSE($db, "Old record with id: $id deleted. Created new auth record for new ip+mac id: $new_id!");
  3894. return $new_id;
  3895. }
  3896. function get_dns_name($db,$id)
  3897. {
  3898. $auth_record = get_record_sql($db,"SELECT dns_name FROM User_auth WHERE id=".$id);
  3899. if (!empty($auth_record) and !empty($auth_record['dns_name'])) { return $auth_record['dns_name']; }
  3900. return '';
  3901. }
  3902. function update_record($db, $table, $filter, $newvalue)
  3903. {
  3904. if (!isset($table)) {
  3905. LOG_WARNING($db, "Change record for unknown table! Skip command.");
  3906. return;
  3907. }
  3908. if (!isset($filter)) {
  3909. LOG_WARNING($db, "Change record ($table) with empty filter! Skip command.");
  3910. return;
  3911. }
  3912. if (preg_match('/=$/', $filter)) {
  3913. LOG_WARNING($db, "Change record ($table) with illegal filter $filter! Skip command.");
  3914. return;
  3915. }
  3916. if (!isset($newvalue)) {
  3917. LOG_WARNING($db, "Change record ($table [ $filter ]) with empty data! Skip command.");
  3918. return;
  3919. }
  3920. if (!allow_update($table,'update')) {
  3921. LOG_WARNING($db,"Access denied: $table [ $filter ]");
  3922. return 1;
  3923. }
  3924. $old_sql = "SELECT * FROM $table WHERE $filter";
  3925. $old_record = mysqli_query($db, $old_sql) or LOG_ERROR($db, "SQL: $old_sql :" . mysqli_error($db));
  3926. $old = mysqli_fetch_array($old_record, MYSQLI_ASSOC);
  3927. $rec_id = NULL;
  3928. if (!empty($old['id'])) { $rec_id = $old['id']; }
  3929. $changed_log = '';
  3930. $run_sql = '';
  3931. $network_changed = 0;
  3932. $dhcp_changed = 0;
  3933. $dns_changed = 0;
  3934. $acl_fields = [
  3935. 'ip' => '1',
  3936. 'ip_int' => '1',
  3937. 'enabled' => '1',
  3938. 'dhcp' => '1',
  3939. 'filter_group_id' => '1',
  3940. 'deleted' => '1',
  3941. 'dhcp_acl' => '1',
  3942. 'queue_id' => '1',
  3943. 'mac' => '1',
  3944. 'blocked' => '1',
  3945. ];
  3946. $dhcp_fields = [
  3947. 'ip' => '1',
  3948. 'dhcp' => '1',
  3949. 'deleted' => '1',
  3950. 'mac' => '1',
  3951. ];
  3952. $dns_fields = [
  3953. 'ip' => '1',
  3954. 'dns_name' => '1',
  3955. 'alias' => '1',
  3956. ];
  3957. foreach ($newvalue as $key => $value) {
  3958. if (!allow_update($table,'update',$key)) { continue; }
  3959. if (!isset($value)) {
  3960. $value = '';
  3961. }
  3962. $value = trim($value);
  3963. if (strcmp($old[$key], $value) == 0) {
  3964. continue;
  3965. }
  3966. if ($table === "User_auth") {
  3967. if (!empty($acl_fields["$key"])) {
  3968. $network_changed = 1;
  3969. }
  3970. if (!empty($dhcp_fields["$key"])) {
  3971. $dhcp_changed = 1;
  3972. }
  3973. if (!empty($dns_fields["$key"])) {
  3974. $dns_changed = 1;
  3975. }
  3976. }
  3977. if ($table === "User_auth_alias") {
  3978. if (!empty($dns_fields["$key"])) {
  3979. $dns_changed = 1;
  3980. }
  3981. }
  3982. if (!preg_match('/password/i',$key)) {
  3983. $changed_log = $changed_log . " $key => $value (old: $old[$key]),";
  3984. }
  3985. $run_sql = $run_sql . " `" . $key . "`='" . mysqli_real_escape_string($db, $value) . "',";
  3986. }
  3987. if ($table === "User_auth" and $dns_changed) {
  3988. if (!empty($old['dns_name']) and !empty($old['ip'])) {
  3989. $del_dns['name_type']='A';
  3990. $del_dns['name']=$old['dns_name'];
  3991. $del_dns['value']=$old['ip'];
  3992. $del_dns['type']='del';
  3993. if (!empty($rec_id)) { $del_dns['auth_id']=$rec_id; }
  3994. insert_record($db,'dns_queue',$del_dns);
  3995. }
  3996. if (!empty($newvalue['dns_name']) and !empty($newvalue['ip'])) {
  3997. $new_dns['name_type']='A';
  3998. $new_dns['name']=$newvalue['dns_name'];
  3999. $new_dns['value']=$newvalue['ip'];
  4000. $new_dns['type']='add';
  4001. if (!empty($rec_id)) { $new_dns['auth_id']=$rec_id; }
  4002. insert_record($db,'dns_queue',$new_dns);
  4003. }
  4004. }
  4005. if ($table === "User_auth_alias" and $dns_changed) {
  4006. $auth_id = NULL;
  4007. if ($old['auth_id']) { $auth_id = $old['auth_id']; }
  4008. if (!empty($old['alias'])) {
  4009. $del_dns['name_type']='CNAME';
  4010. $del_dns['name']=$old['alias'];
  4011. $del_dns['type']='del';
  4012. if (!empty($auth_id)) {
  4013. $del_dns['auth_id']=$auth_id;
  4014. $del_dns['value']=get_dns_name($db,$auth_id);
  4015. }
  4016. insert_record($db,'dns_queue',$del_dns);
  4017. }
  4018. if (!empty($newvalue['alias'])) {
  4019. $new_dns['name_type']='CNAME';
  4020. $new_dns['name']=$newvalue['alias'];
  4021. $new_dns['type']='add';
  4022. if (!empty($auth_id)) {
  4023. $new_dns['auth_id']=$auth_id;
  4024. $new_dns['value']=get_dns_name($db,$auth_id);
  4025. }
  4026. insert_record($db,'dns_queue',$new_dns);
  4027. }
  4028. }
  4029. if (empty($run_sql)) {
  4030. return 1;
  4031. }
  4032. if ($network_changed) {
  4033. $run_sql = $run_sql . " `changed`='1',";
  4034. }
  4035. if ($dhcp_changed) {
  4036. $run_sql = $run_sql . " `dhcp_changed`='1',";
  4037. }
  4038. $changed_log = substr_replace($changed_log, "", -1);
  4039. $run_sql = substr_replace($run_sql, "", -1);
  4040. if ($table === 'User_auth') {
  4041. $changed_time = GetNowTimeString();
  4042. $run_sql = $run_sql . ", `changed_time`='" . $changed_time . "'";
  4043. }
  4044. $new_sql = "UPDATE $table SET $run_sql WHERE $filter";
  4045. LOG_DEBUG($db, "Run sql: $new_sql");
  4046. $sql_result = mysqli_query($db, $new_sql) or LOG_ERROR($db, "SQL: $new_sql :" . mysqli_error($db));
  4047. if (!$sql_result) {
  4048. LOG_ERROR($db, "UPDATE Request: $new_sql :" . mysqli_error($db));
  4049. return;
  4050. }
  4051. if ($table !== "sessions") {
  4052. LOG_VERBOSE($db, "Change table $table WHERE $filter set $changed_log");
  4053. }
  4054. return $sql_result;
  4055. }
  4056. function delete_record($db, $table, $filter)
  4057. {
  4058. if (!allow_update($table,'del')) {
  4059. LOG_WARNING($db, "User does not have write permission");
  4060. return;
  4061. }
  4062. if (!isset($table)) {
  4063. LOG_WARNING($db, "Delete FROM unknown table! Skip command.");
  4064. return;
  4065. }
  4066. if (!isset($filter)) {
  4067. LOG_WARNING($db, "Delete FROM table $table with empty filter! Skip command.");
  4068. return;
  4069. }
  4070. if (preg_match('/=$/', $filter)) {
  4071. LOG_WARNING($db, "Change record ($table) with illegal filter $filter! Skip command.");
  4072. return;
  4073. }
  4074. $old_sql = "SELECT * FROM $table WHERE $filter";
  4075. $old_record = mysqli_query($db, $old_sql) or LOG_ERROR($db, "SQL: $old_sql :" . mysqli_error($db));
  4076. $old = mysqli_fetch_array($old_record, MYSQLI_ASSOC);
  4077. $rec_id = NULL;
  4078. if (!empty($old['id'])) { $rec_id = $old['id']; }
  4079. $changed_log = 'record: ';
  4080. if (!empty($old)) {
  4081. foreach ($old as $key => $value) {
  4082. if (!isset($value)) {
  4083. $value = '';
  4084. }
  4085. $changed_log = $changed_log . " $key => $value,";
  4086. }
  4087. }
  4088. $delete_it = 1;
  4089. //never delete user ip record or dns alias record
  4090. if ($table === 'User_auth') {
  4091. $delete_it = 0;
  4092. $changed_time = GetNowTimeString();
  4093. $new_sql = "UPDATE $table SET deleted=1, changed=1, `changed_time`='" . $changed_time . "' WHERE $filter";
  4094. LOG_DEBUG($db, "Run sql: $new_sql");
  4095. $sql_result = mysqli_query($db, $new_sql) or LOG_ERROR($db, "SQL: $new_sql :" . mysqli_error($db));
  4096. if (!$sql_result) {
  4097. LOG_ERROR($db, "UPDATE Request (from delete): " . mysqli_error($db));
  4098. return;
  4099. }
  4100. //dns
  4101. if (!empty($old['dns_name']) and !empty($old['ip'])) {
  4102. $del_dns['name_type']='A';
  4103. $del_dns['name']=$old['dns_name'];
  4104. $del_dns['value']=$old['ip'];
  4105. $del_dns['type']='del';
  4106. if (!empty($rec_id)) { $del_dns['auth_id']=$rec_id; }
  4107. insert_record($db,'dns_queue',$del_dns);
  4108. }
  4109. }
  4110. if ($table === 'User_auth_alias') {
  4111. //dns
  4112. if (!empty($old['alias'])) {
  4113. $del_dns['name_type']='CNAME';
  4114. $del_dns['name']=$old['alias'];
  4115. $del_dns['value']='';
  4116. $del_dns['type']='del';
  4117. if (!empty($old['auth_id'])) {
  4118. $del_dns['auth_id']=$old['auth_id'];
  4119. $del_dns['value']=get_dns_name($db,$old['auth_id']);
  4120. }
  4121. insert_record($db,'dns_queue',$del_dns);
  4122. }
  4123. }
  4124. if ($delete_it) {
  4125. $new_sql = "DELETE FROM $table WHERE $filter";
  4126. LOG_DEBUG($db, "Run sql: $new_sql");
  4127. $sql_result = mysqli_query($db, $new_sql) or LOG_ERROR($db, "SQL: $new_sql :" . mysqli_error($db));
  4128. if (!$sql_result) {
  4129. LOG_ERROR($db, "DELETE Request: $new_sql : " . mysqli_error($db));
  4130. return;
  4131. }
  4132. }
  4133. if ($table !== "sessions") {
  4134. LOG_VERBOSE($db, "Delete FROM table $table WHERE $filter $changed_log");
  4135. }
  4136. return $changed_log;
  4137. }
  4138. function insert_record($db, $table, $newvalue)
  4139. {
  4140. if (!allow_update($table,'add')) {
  4141. LOG_WARNING($db, "User does not have write permission");
  4142. return;
  4143. }
  4144. if (!isset($table)) {
  4145. LOG_WARNING($db, "Create record for unknown table! Skip command.");
  4146. return;
  4147. }
  4148. if (empty($newvalue)) {
  4149. LOG_WARNING($db, "Create record ($table) with empty data! Skip command.");
  4150. return;
  4151. }
  4152. $changed_log = '';
  4153. $field_list = '';
  4154. $value_list = '';
  4155. foreach ($newvalue as $key => $value) {
  4156. if (empty($value) and $value !== 0) {
  4157. $value = '';
  4158. }
  4159. if (!preg_match('/password/i',$key)) {
  4160. $changed_log = $changed_log . " $key => $value,";
  4161. }
  4162. $field_list = $field_list . "`" . $key . "`,";
  4163. $value = trim($value);
  4164. $value_list = $value_list . "'" . mysqli_real_escape_string($db, $value) . "',";
  4165. }
  4166. if (empty($value_list)) {
  4167. return;
  4168. }
  4169. $changed_log = substr_replace($changed_log, "", -1);
  4170. $field_list = substr_replace($field_list, "", -1);
  4171. $value_list = substr_replace($value_list, "", -1);
  4172. $new_sql = "insert into $table(" . $field_list . ") values(" . $value_list . ")";
  4173. LOG_DEBUG($db, "Run sql: $new_sql");
  4174. $sql_result = mysqli_query($db, $new_sql) or LOG_ERROR($db, "SQL: $new_sql :" . mysqli_error($db));
  4175. if (!$sql_result) {
  4176. LOG_ERROR($db, "INSERT Request:" . mysqli_error($db));
  4177. return;
  4178. }
  4179. $last_id = mysqli_insert_id($db);
  4180. if ($table !== "sessions") {
  4181. LOG_VERBOSE($db, "Create record in table $table: $changed_log with id: $last_id");
  4182. }
  4183. if ($table === 'User_auth') {
  4184. run_sql($db, "UPDATE User_auth SET changed=1, dhcp_changed=1 WHERE id=" . $last_id);
  4185. }
  4186. if ($table === 'User_auth_alias') {
  4187. //dns
  4188. if (!empty($newvalue['alias'])) {
  4189. $add_dns['name_type']='CNAME';
  4190. $add_dns['name']=$newvalue['alias'];
  4191. $add_dns['value']=get_dns_name($db,$newvalue['auth_id']);
  4192. $add_dns['type']='add';
  4193. $add_dns['auth_id']=$newvalue['auth_id'];
  4194. insert_record($db,'dns_queue',$add_dns);
  4195. }
  4196. }
  4197. if ($table === 'User_auth') {
  4198. //dns
  4199. if (!empty($newvalue['dns_name']) and !empty($newvalue['ip'])) {
  4200. $add_dns['name_type']='A';
  4201. $add_dns['name']=$newvalue['dns_name'];
  4202. $add_dns['value']=$newvalue['ip'];
  4203. $add_dns['type']='add';
  4204. $add_dns['auth_id']=$last_id;
  4205. insert_record($db,'dns_queue',$add_dns);
  4206. }
  4207. }
  4208. return $last_id;
  4209. }
  4210. function get_rec_str($array)
  4211. {
  4212. $result = '';
  4213. foreach ($array as $key => $value) {
  4214. $result .= "[" . $key . "]=" . $value . ", ";
  4215. }
  4216. $result = preg_replace('/,\s+$/', '', $result);
  4217. return $result;
  4218. }
  4219. function get_diff_rec($db, $table, $filter, $newvalue, $only_changed)
  4220. {
  4221. if (!isset($table)) {
  4222. return;
  4223. }
  4224. if (!isset($filter)) {
  4225. return;
  4226. }
  4227. if (!isset($newvalue)) {
  4228. return;
  4229. }
  4230. if (!isset($only_changed)) {
  4231. $only_changed = 0;
  4232. }
  4233. $old_sql = "SELECT * FROM $table WHERE $filter";
  4234. $old_record = mysqli_query($db, $old_sql) or LOG_ERROR($db, "SQL: $old_sql :" . mysqli_error($db));
  4235. $old = mysqli_fetch_array($old_record, MYSQLI_ASSOC);
  4236. $changed_log = "\r\n";
  4237. foreach ($newvalue as $key => $value) {
  4238. if (strcmp($old[$key], $value) !== 0) {
  4239. $changed_log = $changed_log . " $key => cur: $value old: $old[$key],\r\n";
  4240. }
  4241. }
  4242. $old_record = '';
  4243. if (!$only_changed) {
  4244. $old_record = "\r\n Has not changed:\r\n";
  4245. foreach ($old as $key => $value) {
  4246. if (!empty($newvalue[$key])) {
  4247. $old_record = $old_record . " $key = $value,\r\n";
  4248. }
  4249. }
  4250. $old_record = substr_replace($old_record, "", -3);
  4251. }
  4252. // print $changed_log;
  4253. return $changed_log . $old_record;
  4254. }
  4255. function get_cacti_graph($host_ip, $port_index)
  4256. {
  4257. if (empty(get_const('cacti_url'))) {
  4258. return;
  4259. }
  4260. if (CACTI_DB_HOST == null or CACTI_DB_USER == null or CACTI_DB_PASS == null or CACTI_DB_NAME == null) {
  4261. return;
  4262. }
  4263. if (empty(CACTI_DB_HOST) or empty(CACTI_DB_USER) or empty(CACTI_DB_PASS) or empty(CACTI_DB_NAME)) {
  4264. return;
  4265. }
  4266. $cacti_db_link = new_connection(CACTI_DB_HOST, CACTI_DB_USER, CACTI_DB_PASS, CACTI_DB_NAME);
  4267. if (!$cacti_db_link) {
  4268. return FALSE;
  4269. }
  4270. $host_sql = 'SELECT * FROM host WHERE hostname="' . $host_ip . '"';
  4271. $cacti_host = get_record_sql($cacti_db_link, $host_sql);
  4272. $host_id = $cacti_host["id"];
  4273. if (empty($host_id)) {
  4274. return;
  4275. }
  4276. $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';
  4277. $cacti_graph = get_record_sql($cacti_db_link, $graph_sql);
  4278. $graph_id = $cacti_graph["id"];
  4279. if (empty($graph_id)) {
  4280. return;
  4281. }
  4282. $result = get_const('cacti_url') . "/graph_image.php?local_graph_id=" . $graph_id;
  4283. return $result;
  4284. }
  4285. function print_select_item($description, $value, $current)
  4286. {
  4287. if ((string)$value === (string)$current) {
  4288. print "<option value=$value selected>$description</option>";
  4289. } else {
  4290. print "<option value=$value>$description</option>";
  4291. }
  4292. }
  4293. function print_select_simple($description, $value)
  4294. {
  4295. print "<option value=$value>$description</option>";
  4296. }
  4297. function print_select_item_ext($description, $value, $current, $disabled)
  4298. {
  4299. if ((string)$value === (string)$current) {
  4300. print "<option value=$value selected>$description</option>";
  4301. } else {
  4302. if (!$disabled) {
  4303. print "<option value=$value>$description</option>";
  4304. } else {
  4305. print "<option disabled value=$value>$description</option>";
  4306. }
  4307. }
  4308. }
  4309. function print_row_at_pages($name, $value)
  4310. {
  4311. print "<select name='" . $name . "'>\n";
  4312. print_select_item(WEB_select_item_more, pow(10, 10), $value);
  4313. print_select_item('25', 25, $value);
  4314. print_select_item('50', 50, $value);
  4315. print_select_item('100', 100, $value);
  4316. print_select_item('200', 200, $value);
  4317. print_select_item('500', 500, $value);
  4318. print_select_item('1000', 1000, $value);
  4319. print_select_item('2000', 2000, $value);
  4320. print "</select>\n";
  4321. }
  4322. function print_navigation($url, $page, $displayed, $count_records, $total)
  4323. {
  4324. if ($total <= 1) {
  4325. print "<div align=left class=records >";
  4326. print "| Total records: $count_records";
  4327. print "</div>";
  4328. return;
  4329. }
  4330. $v_char = "?";
  4331. if (preg_match('/\.php\?/', $url)) {
  4332. $v_char = "&";
  4333. }
  4334. #две назад
  4335. print "<div align=left class=records >";
  4336. if (($page - 2) > 0) :
  4337. $pagetwoleft = "<a class='first_page_link' href=" . $url . $v_char . "page=" . ($page - 2) . ">" . ($page - 2) . "</a> ";
  4338. else :
  4339. $pagetwoleft = null;
  4340. endif;
  4341. #одна назад
  4342. if (($page - 1) > 0) :
  4343. $pageoneleft = "<a class='first_page_link' href=" . $url . $v_char . "page=" . ($page - 1) . ">" . ($page - 1) . "</a> ";
  4344. $pagetemp = ($page - 1);
  4345. else :
  4346. $pageoneleft = null;
  4347. $pagetemp = null;
  4348. endif;
  4349. #две вперед
  4350. if (($page + 2) <= $total) :
  4351. $pagetworight = " <a class='first_page_link' href=" . $url . $v_char . "page=" . ($page + 2) . ">" . ($page + 2) . "</a>";
  4352. else :
  4353. $pagetworight = null;
  4354. endif;
  4355. #одна вперед
  4356. if (($page + 1) <= $total) :
  4357. $pageoneright = " <a class='first_page_link' href=" . $url . $v_char . "page=" . ($page + 1) . ">" . ($page + 1) . "</a>";
  4358. $pagetemp2 = ($page + 1);
  4359. else :
  4360. $pageoneright = null;
  4361. $pagetemp2 = null;
  4362. endif;
  4363. # в начало
  4364. if ($page != 1 && $pagetemp != 1 && $pagetemp != 2) :
  4365. $pagerevp = "<a href=" . $url . $v_char . "page=1 class='first_page_link' title='В начало'><<</a> ";
  4366. else :
  4367. $pagerevp = null;
  4368. endif;
  4369. #в конец (последняя)
  4370. if ($page != $total && $pagetemp2 != ($total - 1) && $pagetemp2 != $total) :
  4371. $nextp = " ... <a href=" . $url . $v_char . "page=" . $total . " class='first_page_link'>$total</a>";
  4372. else :
  4373. $nextp = null;
  4374. endif;
  4375. print $pagerevp . $pagetwoleft . $pageoneleft . '<span class="num_page_not_link"><b>' . $page . '</b></span>' . $pageoneright . $pagetworight . $nextp;
  4376. print " | Total records: $count_records";
  4377. print "</div>";
  4378. }
  4379. function get_option($db, $option_id)
  4380. {
  4381. $option = get_record($db, "config", "option_id=" . $option_id);
  4382. if (empty($option) or empty($option['value'])) {
  4383. $default = get_record($db, "config_options", "id=$option_id");
  4384. return $default['default_value'];
  4385. }
  4386. return $option['value'];
  4387. }
  4388. function is_option($db, $option_id)
  4389. {
  4390. $option = get_record($db, "config", "option_id=" . $option_id);
  4391. if (empty($option) or empty($option['value'])) {
  4392. return;
  4393. }
  4394. return 1;
  4395. }
  4396. function set_option($db, $option_id, $value)
  4397. {
  4398. $option['value'] = $value;
  4399. update_record($db, 'config', "option_id=$option_id", $option);
  4400. }
  4401. function is_subnet_aton($subnet, $ip)
  4402. {
  4403. if (!isset($subnet)) {
  4404. return 0;
  4405. }
  4406. if (!isset($ip)) {
  4407. return 0;
  4408. }
  4409. $range = cidrToRange($subnet);
  4410. if ($ip >= ip2long($range[0]) and $ip <= ip2long($range[1])) {
  4411. return 1;
  4412. }
  4413. return 0;
  4414. }
  4415. function get_new_user_id($db, $ip, $mac, $hostname)
  4416. {
  4417. $result['ip'] = $ip;
  4418. $result['mac'] = mac_dotted($mac);
  4419. $result['hostname'] = $hostname;
  4420. $result['user_id'] = NULL;
  4421. $result['ou_id'] = NULL;
  4422. $ip_aton = ip2long($ip);
  4423. //personal user rules
  4424. //ip
  4425. if (!empty($ip)) {
  4426. $t_rules = get_records_sql($db, "SELECT * FROM auth_rules WHERE type=1 and LENGTH(rule)>0 AND user_id IS NOT NULL");
  4427. foreach ($t_rules as $row) {
  4428. if (!empty($row['rule']) and is_subnet_aton($row['rule'], $ip_aton)) {
  4429. $result['user_id'] = $row['user_id'];
  4430. }
  4431. }
  4432. }
  4433. //mac
  4434. if (!empty($mac)) {
  4435. $mac_rules = get_records_sql($db, "SELECT * FROM auth_rules WHERE type=2 AND LENGTH(rule)>0 AND user_id IS NOT NULL");
  4436. foreach ($mac_rules as $row) {
  4437. $pattern = '/' . mac_simplify($row['rule']) . '/';
  4438. if (!empty($row['rule']) and preg_match($pattern, mac_simplify($mac))) {
  4439. $result['user_id'] = $row['user_id'];
  4440. }
  4441. }
  4442. }
  4443. //hostname
  4444. if (!empty($hostname)) {
  4445. $mac_rules = get_records_sql($db, "SELECT * FROM auth_rules WHERE type=3 AND LENGTH(rule)>0 AND user_id IS NOT NULL");
  4446. foreach ($mac_rules as $row) {
  4447. if (!empty($row['rule']) and preg_match($row['rule'], $hostname)) {
  4448. $result['user_id'] = $row['user_id'];
  4449. }
  4450. }
  4451. }
  4452. if (!empty($result['user_id'])) {
  4453. return $result;
  4454. }
  4455. //ou rules
  4456. //ip
  4457. if (!empty($ip)) {
  4458. if (is_hotspot($db, $ip)) {
  4459. $result['ou_id'] = get_const('default_hotspot_ou_id');
  4460. }
  4461. $t_rules = get_records_sql($db, "SELECT * FROM auth_rules WHERE type=1 and LENGTH(rule)>0 AND ou_id IS NOT NULL");
  4462. foreach ($t_rules as $row) {
  4463. if (!empty($row['rule']) and is_subnet_aton($row['rule'], $ip_aton)) {
  4464. $result['ou_id'] = $row['ou_id'];
  4465. }
  4466. }
  4467. }
  4468. //mac
  4469. if (!empty($mac)) {
  4470. $mac_rules = get_records_sql($db, "SELECT * FROM auth_rules WHERE type=2 AND LENGTH(rule)>0 AND ou_id IS NOT NULL");
  4471. foreach ($mac_rules as $row) {
  4472. $pattern = '/' . mac_simplify($row['rule']) . '/';
  4473. if (!empty($row['rule']) and preg_match($pattern, mac_simplify($mac))) {
  4474. $result['ou_id'] = $row['ou_id'];
  4475. }
  4476. }
  4477. }
  4478. //hostname
  4479. if (!empty($hostname)) {
  4480. $mac_rules = get_records_sql($db, "SELECT * FROM auth_rules WHERE type=3 AND LENGTH(rule)>0 AND ou_id IS NOT NULL");
  4481. foreach ($mac_rules as $row) {
  4482. if (!empty($row['rule']) and preg_match($row['rule'], $hostname)) {
  4483. $result['ou_id'] = $row['ou_id'];
  4484. }
  4485. }
  4486. }
  4487. if (empty($result['ou_id'])) {
  4488. $result['ou_id'] = get_const('default_user_ou_id');
  4489. }
  4490. return $result;
  4491. }
  4492. function get_subnet_range($db, $subnet_id)
  4493. {
  4494. if (empty($subnet_id)) {
  4495. return;
  4496. }
  4497. $t_option = get_record_sql($db, "SELECT ip_int_start,ip_int_stop FROM `subnets` WHERE id=$subnet_id");
  4498. if (!isset($t_option['ip_int_start'])) {
  4499. $t_option['ip_int_start'] = 0;
  4500. }
  4501. if (!isset($t_option['ip_int_stop'])) {
  4502. $t_option['ip_int_stop'] = 0;
  4503. }
  4504. $subnet['start'] = $t_option['ip_int_start'];
  4505. $subnet['stop'] = $t_option['ip_int_stop'];
  4506. return $subnet;
  4507. }
  4508. function is_hotspot($db, $ip)
  4509. {
  4510. if (!isset($ip)) {
  4511. return 0;
  4512. }
  4513. LOG_DEBUG($db, "Check hotspot network for ip: $ip");
  4514. $ip_aton = ip2long($ip);
  4515. $t_option = mysqli_query($db, "SELECT subnet,ip_int_start,ip_int_stop FROM `subnets` WHERE hotspot=1");
  4516. while (list($f_net, $f_start, $f_stop) = mysqli_fetch_array($t_option)) {
  4517. if ($ip_aton >= $f_start and $ip_aton <= $f_stop) {
  4518. LOG_DEBUG($db, "ip: $ip [$ip_aton] found in network $f_net: [" . $f_start . ".." . $f_stop . "]");
  4519. return 1;
  4520. }
  4521. }
  4522. LOG_DEBUG($db, "ip $ip not found in hotspot network!");
  4523. return 0;
  4524. }
  4525. function is_office($db, $ip)
  4526. {
  4527. if (!isset($ip)) {
  4528. return 0;
  4529. }
  4530. LOG_DEBUG($db, "Check office network for ip: $ip");
  4531. $ip_aton = ip2long($ip);
  4532. $t_option = mysqli_query($db, "SELECT subnet,ip_int_start,ip_int_stop FROM `subnets` WHERE office=1");
  4533. while (list($f_net, $f_start, $f_stop) = mysqli_fetch_array($t_option)) {
  4534. if ($ip_aton >= $f_start and $ip_aton <= $f_stop) {
  4535. LOG_DEBUG($db, "ip: $ip [$ip_aton] found in office $f_net: [" . $f_start . ".." . $f_stop . "]");
  4536. return 1;
  4537. }
  4538. }
  4539. LOG_DEBUG($db, "ip $ip not found in office network!");
  4540. return 0;
  4541. }
  4542. function is_our_network($db, $ip)
  4543. {
  4544. if (!isset($ip)) {
  4545. return 0;
  4546. }
  4547. if (is_hotspot($db, $ip)) {
  4548. return 1;
  4549. }
  4550. if (is_office($db, $ip)) {
  4551. return 1;
  4552. }
  4553. return 0;
  4554. }
  4555. function get_const($const_name)
  4556. {
  4557. global $config;
  4558. if (isset($config[$const_name])) {
  4559. return $config[$const_name];
  4560. }
  4561. return NULL;
  4562. }
  4563. function get_eye_version ($db)
  4564. {
  4565. $v_table=get_record_sql($db,"SELECT version FROM version");
  4566. if (!empty($v_table)) { return $v_table['version']; }
  4567. return NULL;
  4568. }
  4569. $config["org_name"] = get_option($db_link, 32);
  4570. $config["version"] = get_eye_version($db_link);
  4571. $config["KB"] = get_option($db_link, 1);
  4572. if ($config["KB"] == 0) {
  4573. $config["KB"] = 1000;
  4574. }
  4575. if ($config["KB"] == 1) {
  4576. $config["KB"] = 1024;
  4577. }
  4578. $config["debug"] = get_option($db_link, 34);
  4579. $config["log_level"] = get_option($db_link, 53);
  4580. if ($config["debug"]) {
  4581. $config["log_level"] = 255;
  4582. }
  4583. $config["send_email"] = get_option($db_link, 51);
  4584. $config["admin_email"] = get_option($db_link, 21);
  4585. $config["sender_email"] = get_option($db_link, 52);
  4586. $config["snmp_default_version"] = get_option($db_link, 9);
  4587. $config["snmp_default_community"] = get_option($db_link, 11);
  4588. $config["auto_mac_rule"] = get_option($db_link, 64);
  4589. $config["cacti_url"] = rtrim(get_option($db_link, 58), '/');
  4590. if (preg_match('/127.0.0.1/', $config["cacti_url"])) {
  4591. $config["cacti_url"] = NULL;
  4592. }
  4593. $config["nagios_url"] = rtrim(get_option($db_link, 57), '/') . '/cgi-bin/';
  4594. if (preg_match('/127.0.0.1/', $config["nagios_url"])) {
  4595. $config["nagios_url"] = NULL;
  4596. }
  4597. $config["torrus_url"] = rtrim(get_option($db_link, 59), '/') . '?nodeid=if//HOST_IP//IF_NAME////inoutbps';
  4598. if (preg_match('/127.0.0.1/', $config["torrus_url"])) {
  4599. $config["torrus_url"] = NULL;
  4600. }
  4601. $config["dns_server"]=get_option($db_link,3);
  4602. $config["dns_server_type"]=get_option($db_link,70);
  4603. $ou = get_record_sql($db_link, "SELECT id FROM OU WHERE default_users = 1");
  4604. if (empty($ou)) {
  4605. $config["default_user_ou_id"] = 0;
  4606. } else {
  4607. $config["default_user_ou_id"] = $ou['id'];
  4608. }
  4609. $ou = get_record_sql($db_link, "SELECT id FROM OU WHERE default_hotspot=1");
  4610. if (empty($ou)) {
  4611. $config["default_hotspot_ou_id"] = $config["default_user_ou_id"];
  4612. } else {
  4613. $config["default_hotspot_ou_id"] = $ou['id'];
  4614. }
  4615. $config["init"] = 1;
  4616. clean_dns_cache($db_link);
  4617. //clean_unreferensed_rules($db_link);
  4618. snmp_set_valueretrieval(SNMP_VALUE_LIBRARY);
  4619. snmp_set_enum_print(1);