common.php 154 KB

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