common.php 175 KB

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