common.php 171 KB

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