common.php 161 KB

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