1
0

common.php 167 KB

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