common.php 151 KB

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