1
0

common.php 155 KB

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