common.php 154 KB

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