common.php 165 KB

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