common.php 163 KB

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