common.php 163 KB

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