common.php 153 KB

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