common.php 173 KB

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