common.php 148 KB

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