common.php 174 KB

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