common.php 148 KB

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