common.php 119 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491
  1. <?php
  2. if (! defined("CONFIG")) { die("Not defined"); }
  3. require_once ($_SERVER['DOCUMENT_ROOT']."/inc/class.simple.mail.php");
  4. $config["init"]=0;
  5. #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])$";
  6. #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])$";
  7. #$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}$";
  8. $port_status_oid = '.1.3.6.1.2.1.2.2.1.8.';
  9. $port_admin_status_oid = '.1.3.6.1.2.1.2.2.1.7.';
  10. $port_speed_oid = '.1.3.6.1.2.1.2.2.1.5.';
  11. $port_errors_oid = '.1.3.6.1.2.1.2.2.1.14.';
  12. $port_vlan_oid = '.1.3.6.1.2.1.17.7.1.4.5.1.1.';
  13. $mac_table_oid = '.1.3.6.1.2.1.17.7.1.2.2.1.2';
  14. $mac_table_oid2 = '.1.3.6.1.2.1.17.4.3.1.2';
  15. $eltex_sfp_status = '.1.3.6.1.4.1.89.90.1.2.1.3';
  16. $eltex_sfp_vendor = '.1.3.6.1.4.1.35265.1.23.53.1.1.1.5';
  17. $eltex_sfp_sn = '.1.3.6.1.4.1.35265.1.23.53.1.1.1.6';
  18. $eltex_sfp_freq = '.1.3.6.1.4.1.35265.1.23.53.1.1.1.4';
  19. $eltex_sfp_length = '.1.3.6.1.4.1.35265.1.23.53.1.1.1.8';
  20. $cisco_descr = '.1.3.6.1.2.1.1.1.0';
  21. $cisco_modules = '.1.3.6.1.2.1.47.1.1.1.1.7';
  22. $cisco_sfp_sensors = '.1.3.6.1.4.1.9.9.91.1.1.1.1.4';
  23. $cisco_sfp_precision = '.1.3.6.1.4.1.9.9.91.1.1.1.1.3';
  24. $cisco_vlan_oid = '.1.3.6.1.4.1.9.9.9.46.1.3.1.1.2';
  25. $ifmib_ifindex = '.1.3.6.1.2.1.2.2.1.1';
  26. $ifmib_ifindex_map = '.1.3.6.1.2.1.17.1.4.1.2';
  27. $ifmib_ifdescr = '.1.3.6.1.2.1.2.2.1.2';
  28. $ifmib_ifname = '.1.3.6.1.2.1.31.1.1.1.1';
  29. $huawei_sfp_vendor = '.1.3.6.1.4.1.2011.5.25.31.1.1.2.1.11';
  30. $huawei_sfp_speed = '.1.3.6.1.4.1.2011.5.25.31.1.1.2.1.2';
  31. $huawei_sfp_volt = '.1.3.6.1.4.1.2011.5.25.31.1.1.3.1.6';
  32. $huawei_sfp_optrx = '.1.3.6.1.4.1.2011.5.25.31.1.1.3.1.32';
  33. $huawei_sfp_opttx = '.1.3.6.1.4.1.2011.5.25.31.1.1.3.1.33';
  34. $huawei_sfp_biascurrent = '.1.3.6.1.4.1.2011.5.25.31.1.1.3.1.31';
  35. $huawei_sfp_rx = '.1.3.6.1.4.1.2011.5.25.31.1.1.3.1.8';
  36. $huawei_sfp_tx = '.1.3.6.1.4.1.2011.5.25.31.1.1.3.1.9';
  37. $pethPsePortAdminEnable = '.1.3.6.1.2.1.105.1.1.1.3.1';
  38. $huawei_poe_oid = '.1.3.6.1.4.1.2011.5.25.195.3.1.3';
  39. $allied_poe_oid = '.1.3.6.1.2.1.105.1.1.1.3.1';
  40. $hp_poe_oid = '.1.3.6.1.2.1.105.1.1.1.3.1';
  41. $netgear_poe_oid = '.1.3.6.1.4.1.4526.11.15.1.1.1.6.1';
  42. $mikrotik_poe_oid = '.1.3.6.1.4.1.14988.1.1.15.1.1.3';
  43. // interface id
  44. $mikrotik_poe_int = '.1.3.6.1.4.1.14988.1.1.15.1.1.1';
  45. // interface names
  46. $mikrotik_poe_int_names = '.1.3.6.1.4.1.14988.1.1.15.1.1.2';
  47. // voltage in dV (decivolt)
  48. $mikrotik_poe_volt = '.1.3.6.1.4.1.14988.1.1.15.1.1.4';
  49. // current in mA
  50. $mikrotik_poe_current = '.1.3.6.1.4.1.14988.1.1.15.1.1.5';
  51. // power usage in dW (deviwatt)
  52. $mikrotik_poe_usage = '.1.3.6.1.4.1.14988.1.1.15.1.1.6';
  53. $sysinfo_mib = '.1.3.6.1.2.1.1';
  54. $L_ERROR = 0;
  55. $L_WARNING = 1;
  56. $L_INFO = 2;
  57. $L_VERBOSE = 3;
  58. $L_DEBUG = 255;
  59. $log_level = 2;
  60. // #### vendor id
  61. // 1, Unknown
  62. // 2, 'Eltex'
  63. // 3, 'Huawei'
  64. // 4, 'Zyxel'
  65. // 5, 'Raisecom'
  66. // 6, 'SNR'
  67. // 7, 'Dlink'
  68. // 8, 'Allied Telesis'
  69. // 9, 'Mikrotik'
  70. // 10, 'NetGear'
  71. // 11, 'Ubiquiti'
  72. // 15, 'HP'
  73. // 16, 'Cisco'
  74. // 17, 'Maipu'
  75. // 18, 'Asus'
  76. $admin_email = "admin";
  77. $sender_email = "root";
  78. $send_email = 0;
  79. $mac_table_str_oid = '.1.3.6.1.2.1.17.4.3.1.2';
  80. $mac_table_str_oid2 = '1.3.6.1.2.1.17.7.1.2.2.1.2';
  81. function get_user_ip()
  82. {
  83. $auth_ip = getenv("HTTP_CLIENT_IP");
  84. if (empty($auth_ip)) {
  85. $auth_ip = getenv("HTTP_X_FORWARDED_FOR");
  86. if (empty($auth_ip)) {
  87. $auth_ip = getenv("REMOTE_ADDR");
  88. if (empty($auth_ip)) {
  89. $auth_ip = $_SERVER['REMOTE_ADDR'];
  90. }
  91. }
  92. }
  93. return $auth_ip;
  94. }
  95. function fbytes($traff)
  96. {
  97. $units = array(
  98. "",
  99. "k",
  100. "M",
  101. "G",
  102. "T"
  103. );
  104. $KB = 1024;
  105. if (!empty($traff) and $traff > 0) {
  106. $index = min(((int) log($traff, $KB)), count($units) - 1);
  107. $result = round($traff / pow($KB, $index), 3) . ' ' . $units[$index] . 'b';
  108. } else {
  109. $result = '0 b';
  110. }
  111. return $result;
  112. }
  113. function fpkts($packets)
  114. {
  115. $units = array(
  116. "",
  117. "k",
  118. "M",
  119. "G",
  120. "T"
  121. );
  122. $KB = 1000;
  123. if (!empty($packets) and $packets > 0) {
  124. $index = min(((int) log($packets, $KB)), count($units) - 1);
  125. $result = round($packets / pow($KB, $index), 3) . ' ' . $units[$index] . 'pkt/s';
  126. } else {
  127. $result = '0 pkt/s';
  128. }
  129. return $result;
  130. }
  131. function checkValidIp($cidr)
  132. {
  133. // Checks for a valid IP address or optionally a cidr notation range
  134. // e.g. 1.2.3.4 or 1.2.3.0/24
  135. // 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)) {
  136. $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}/';
  137. if (! preg_match($ip_pattern, $cidr)) {
  138. $return = FALSE;
  139. } else {
  140. $return = TRUE;
  141. }
  142. if ($return == TRUE) {
  143. $parts = explode("/", $cidr);
  144. $ip = $parts[0];
  145. if (empty($parts[1])) { $parts[1]="32"; }
  146. $netmask = $parts[1];
  147. $octets = explode(".", $ip);
  148. foreach ($octets as $octet) {
  149. if ($octet > 255) {
  150. $return = FALSE;
  151. }
  152. }
  153. if (($netmask != "") && ($netmask > 32)) {
  154. $return = FALSE;
  155. }
  156. }
  157. return $return;
  158. }
  159. function checkValidMac($mac) {
  160. $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}$/";
  161. if (! preg_match($ValidMacAddressRegex, $mac)) { $return = FALSE; } else { $return = TRUE; }
  162. return $return;
  163. }
  164. function checkValidHostname($dnsname)
  165. {
  166. $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])$/";
  167. if (! preg_match($host_pattern, $dnsname)) {
  168. $result = FALSE;
  169. } else {
  170. $result = TRUE;
  171. }
  172. return $result;
  173. }
  174. function checkUniqHostname($db,$id,$hostname)
  175. {
  176. $count=get_count_records($db,'User_auth','deleted=0 and id !="'.$id.'" and dns_name ="'.mysqli_real_escape_string($db,$hostname).'"');
  177. if ($count >0 ) { return FALSE; }
  178. return TRUE;
  179. }
  180. // Транслитерация строк.
  181. function transliterate($string, $gost = false)
  182. {
  183. if ($gost) {
  184. $replace = array(
  185. "А" => "A",
  186. "а" => "a",
  187. "Б" => "B",
  188. "б" => "b",
  189. "В" => "V",
  190. "в" => "v",
  191. "Г" => "G",
  192. "г" => "g",
  193. "Д" => "D",
  194. "д" => "d",
  195. "Е" => "E",
  196. "е" => "e",
  197. "Ё" => "E",
  198. "ё" => "e",
  199. "Ж" => "Zh",
  200. "ж" => "zh",
  201. "З" => "Z",
  202. "з" => "z",
  203. "И" => "I",
  204. "и" => "i",
  205. "Й" => "I",
  206. "й" => "i",
  207. "К" => "K",
  208. "к" => "k",
  209. "Л" => "L",
  210. "л" => "l",
  211. "М" => "M",
  212. "м" => "m",
  213. "Н" => "N",
  214. "н" => "n",
  215. "О" => "O",
  216. "о" => "o",
  217. "П" => "P",
  218. "п" => "p",
  219. "Р" => "R",
  220. "р" => "r",
  221. "С" => "S",
  222. "с" => "s",
  223. "Т" => "T",
  224. "т" => "t",
  225. "У" => "U",
  226. "у" => "u",
  227. "Ф" => "F",
  228. "ф" => "f",
  229. "Х" => "Kh",
  230. "х" => "kh",
  231. "Ц" => "Tc",
  232. "ц" => "tc",
  233. "Ч" => "Ch",
  234. "ч" => "ch",
  235. "Ш" => "Sh",
  236. "ш" => "sh",
  237. "Щ" => "Shch",
  238. "щ" => "shch",
  239. "Ы" => "Y",
  240. "ы" => "y",
  241. "Э" => "E",
  242. "э" => "e",
  243. "Ю" => "Iu",
  244. "ю" => "iu",
  245. "Я" => "Ia",
  246. "я" => "ia",
  247. "ъ" => "",
  248. "ь" => ""
  249. );
  250. } else {
  251. $arStrES = array(
  252. "ае",
  253. "уе",
  254. "ое",
  255. "ые",
  256. "ие",
  257. "эе",
  258. "яе",
  259. "юе",
  260. "ёе",
  261. "ее",
  262. "ье",
  263. "ъе",
  264. "ый",
  265. "ий"
  266. );
  267. $arStrOS = array(
  268. "аё",
  269. "уё",
  270. "оё",
  271. "ыё",
  272. "иё",
  273. "эё",
  274. "яё",
  275. "юё",
  276. "ёё",
  277. "её",
  278. "ьё",
  279. "ъё",
  280. "ый",
  281. "ий"
  282. );
  283. $arStrRS = array(
  284. "а$",
  285. "у$",
  286. "о$",
  287. "ы$",
  288. "и$",
  289. "э$",
  290. "я$",
  291. "ю$",
  292. "ё$",
  293. "е$",
  294. "ь$",
  295. "ъ$",
  296. "@",
  297. "@"
  298. );
  299. $replace = array(
  300. "А" => "A",
  301. "а" => "a",
  302. "Б" => "B",
  303. "б" => "b",
  304. "В" => "V",
  305. "в" => "v",
  306. "Г" => "G",
  307. "г" => "g",
  308. "Д" => "D",
  309. "д" => "d",
  310. "Е" => "Ye",
  311. "е" => "e",
  312. "Ё" => "Ye",
  313. "ё" => "e",
  314. "Ж" => "Zh",
  315. "ж" => "zh",
  316. "З" => "Z",
  317. "з" => "z",
  318. "И" => "I",
  319. "и" => "i",
  320. "Й" => "Y",
  321. "й" => "y",
  322. "К" => "K",
  323. "к" => "k",
  324. "Л" => "L",
  325. "л" => "l",
  326. "М" => "M",
  327. "м" => "m",
  328. "Н" => "N",
  329. "н" => "n",
  330. "О" => "O",
  331. "о" => "o",
  332. "П" => "P",
  333. "п" => "p",
  334. "Р" => "R",
  335. "р" => "r",
  336. "С" => "S",
  337. "с" => "s",
  338. "Т" => "T",
  339. "т" => "t",
  340. "У" => "U",
  341. "у" => "u",
  342. "Ф" => "F",
  343. "ф" => "f",
  344. "Х" => "Kh",
  345. "х" => "kh",
  346. "Ц" => "Ts",
  347. "ц" => "ts",
  348. "Ч" => "Ch",
  349. "ч" => "ch",
  350. "Ш" => "Sh",
  351. "ш" => "sh",
  352. "Щ" => "Shch",
  353. "щ" => "shch",
  354. "Ъ" => "",
  355. "ъ" => "",
  356. "Ы" => "Y",
  357. "ы" => "y",
  358. "Ь" => "",
  359. "ь" => "",
  360. "Э" => "E",
  361. "э" => "e",
  362. "Ю" => "Yu",
  363. "ю" => "yu",
  364. "Я" => "Ya",
  365. "я" => "ya",
  366. "@" => "y",
  367. "$" => "ye"
  368. );
  369. $string = str_replace($arStrES, $arStrRS, $string);
  370. $string = str_replace($arStrOS, $arStrRS, $string);
  371. }
  372. return strtr($string, $replace);
  373. }
  374. function cidrToRange($cidr)
  375. {
  376. $range = array();
  377. $cidr = explode('/', $cidr);
  378. if (! isset($cidr[1])) { $cidr[1] = 32; }
  379. $start=(ip2long($cidr[0])) & ((- 1 << (32 - (int) $cidr[1])));
  380. $stop=$start + pow(2, (32 - (int) $cidr[1])) - 1;
  381. $range[0] = long2ip($start);
  382. $range[1] = long2ip($stop);
  383. $range[2] = $cidr;
  384. #dhcp
  385. $dhcp_size=round(($stop-$start)/2,PHP_ROUND_HALF_UP);
  386. $dhcp_start=$start+round($dhcp_size/2,PHP_ROUND_HALF_UP);
  387. $range[3]=long2ip($dhcp_start);
  388. $range[4]=long2ip($dhcp_start+$dhcp_size);
  389. $range[5]=long2ip($start+1);
  390. return $range;
  391. }
  392. function print_ou_select($db, $ou_name, $ou_value)
  393. {
  394. print "<select name=\"$ou_name\" class=\"js-select-single\">\n";
  395. $t_ou = mysqli_query($db, "SELECT id,ou_name FROM OU ORDER BY ou_name");
  396. while (list ($f_ou_id, $f_ou_name) = mysqli_fetch_array($t_ou)) {
  397. print_select_item($f_ou_name,$f_ou_id,$ou_value);
  398. }
  399. print "</select>\n";
  400. }
  401. function print_ou_set($db, $ou_name, $ou_value)
  402. {
  403. print "<select name=\"$ou_name\" class=\"js-select-single\">\n";
  404. $t_ou = mysqli_query($db, "SELECT id,ou_name FROM OU WHERE id>=1 ORDER BY ou_name");
  405. while (list ($f_ou_id, $f_ou_name) = mysqli_fetch_array($t_ou)) {
  406. print_select_item($f_ou_name,$f_ou_id,$ou_value);
  407. }
  408. print "</select>\n";
  409. }
  410. function print_subnet_select($db, $subnet_name, $subnet_value)
  411. {
  412. print "<select name=\"$subnet_name\" >\n";
  413. $t_subnet = mysqli_query($db, "SELECT id,subnet FROM subnets ORDER BY ip_int_start");
  414. print_select_item('Всe ip',0,$subnet_value);
  415. while (list ($f_subnet_id, $f_subnet_name) = mysqli_fetch_array($t_subnet)) {
  416. print_select_item($f_subnet_name,$f_subnet_id,$subnet_value);
  417. }
  418. print "</select>\n";
  419. }
  420. function print_device_ip_select($db, $ip_name, $ip, $user_id)
  421. {
  422. print "<select name=\"$ip_name\" >\n";
  423. $auth_list = get_records_sql($db, "SELECT ip FROM User_auth WHERE user_id=$user_id AND deleted=0 ORDER BY ip_int");
  424. foreach ($auth_list as $row) {
  425. print_select_item($row['ip'],$row['ip'],$ip);
  426. }
  427. print "</select>\n";
  428. }
  429. function print_subnet_select_office($db, $subnet_name, $subnet_value)
  430. {
  431. print "<select name=\"$subnet_name\" >\n";
  432. $t_subnet = mysqli_query($db, "SELECT id,subnet FROM subnets WHERE office=1 ORDER BY ip_int_start");
  433. print_select_item('Всe ip',0,$subnet_value);
  434. while (list ($f_subnet_id, $f_subnet_name) = mysqli_fetch_array($t_subnet)) {
  435. print_select_item($f_subnet_name,$f_subnet_id,$subnet_value);
  436. }
  437. print "</select>\n";
  438. }
  439. function print_loglevel_select($item_name, $value)
  440. {
  441. print "<select name=\"$item_name\">\n";
  442. global $L_INFO;
  443. global $L_WARNING;
  444. global $L_ERROR;
  445. global $L_VERBOSE;
  446. global $L_DEBUG;
  447. print_select_item('Error',$L_ERROR,$value);
  448. print_select_item('Warning',$L_WARNING,$value);
  449. print_select_item('Info',$L_INFO,$value);
  450. print_select_item('Verbose',$L_VERBOSE,$value);
  451. print_select_item('Debug',$L_DEBUG,$value);
  452. print "</select>\n";
  453. }
  454. function reencodeurl($url) {
  455. $url_arr = explode('?', $url);
  456. $fpage = $url_arr[0];
  457. if (isset($url_arr[1])) {
  458. $params = $url_arr[1];
  459. $params_arr = explode('&', $params);
  460. $new_params = '';
  461. foreach ($params_arr as $row) {
  462. $param = explode ('=',$row);
  463. $key = $param[0];
  464. $value = urlencode(urldecode($param[1]));
  465. $new_params.="&".$key."=".$value;
  466. }
  467. $new_params = preg_replace('/^&/','',$new_params);
  468. } else { $new_params='='; }
  469. if ($new_params === '=') { $new_url = $fpage; } else { $new_url = $fpage."?".$new_params; }
  470. return $new_url;
  471. }
  472. function print_submenu_url($display_name,$page,$current_page,$last) {
  473. $url_arr = explode('?', $page);
  474. $fpage = $url_arr[0];
  475. $new_url = reencodeurl($page);
  476. if ($fpage === $current_page) { print "<b>$display_name</b>"; } else { print "<a href='".$new_url."'> $display_name </a>"; }
  477. if (!isset($last) or $last==0) { print " | "; }
  478. }
  479. function print_url($display_name,$page) {
  480. print "<a href='".reencodeurl($page)."'> $display_name </a>";
  481. }
  482. function print_log_submenu ($current_page) {
  483. print "<div id='submenu'>\n";
  484. print_submenu_url('Журнал dhcp','/admin/logs/dhcp.php',$current_page,0);
  485. print_submenu_url('Журнал работы ','/admin/logs/index.php',$current_page,0);
  486. print_submenu_url('Приключения маков','/admin/logs/mac.php',$current_page,0);
  487. print_submenu_url('История ip-адресов','/admin/logs/ip.php',$current_page,0);
  488. print_submenu_url('Неизвестные','/admin/logs/unknown.php',$current_page,0);
  489. print_submenu_url('Трафик','/admin/logs/detaillog.php',$current_page,0);
  490. print_submenu_url('syslog','/admin/logs/syslog.php',$current_page,1);
  491. print "</div>\n";
  492. }
  493. function print_control_submenu ($current_page) {
  494. print "<div id='submenu'>\n";
  495. print_submenu_url('Управление','/admin/customers/control.php',$current_page,0);
  496. print_submenu_url('Сети','/admin/customers/control-subnets.php',$current_page,0);
  497. print_submenu_url('Сети (Статистика)','/admin/customers/control-subnets-usage.php',$current_page,0);
  498. print_submenu_url('Параметры','/admin/customers/control-options.php',$current_page,0);
  499. print_submenu_url('Пользователи','/admin/customers/index.php',$current_page,1);
  500. print "</div>\n";
  501. }
  502. function print_filters_submenu ($current_page) {
  503. print "<div id='submenu'>\n";
  504. print_submenu_url('Список фильтров','/admin/filters/index.php',$current_page,0);
  505. print_submenu_url('Группы фильтров','/admin/filters/groups.php',$current_page,1);
  506. print "</div>\n";
  507. }
  508. function print_reports_submenu ($current_page) {
  509. print "<div id='submenu'>\n";
  510. print_submenu_url('Отчёт по трафику (ip)','/admin/reports/index-full.php',$current_page,0);
  511. print_submenu_url('Отчёт по трафику (login)','/admin/reports/index.php',$current_page,1);
  512. print "</div>\n";
  513. }
  514. function print_trafdetail_submenu ($current_page,$params,$description) {
  515. print "<div id='submenu'>\n";
  516. print "$description\n";
  517. print_submenu_url('TOP 10 по трафику','/admin/reports/userdaydetail.php'."?$params",$current_page,0);
  518. print_submenu_url('Подробный лог','/admin/reports/userdaydetaillog.php'."?$params",$current_page,1);
  519. print "</div>\n";
  520. }
  521. function print_device_submenu ($current_page) {
  522. print "<div id='submenu'>\n";
  523. print_submenu_url('Активное сетевое оборудование','/admin/devices/index.php',$current_page,0);
  524. print_submenu_url('Пассивное оборудование','/admin/devices/index-passive.php',$current_page,0);
  525. print_submenu_url('Расположение','/admin/devices/building.php',$current_page,0);
  526. print_submenu_url('Структура','/admin/devices/index-tree.php',$current_page,0);
  527. print_submenu_url('Модели устройств','/admin/devices/devmodels.php',$current_page,0);
  528. print_submenu_url('Vendors','/admin/devices/devvendors.php',$current_page,0);
  529. print_submenu_url('Порты по вланам','/admin/devices/portsbyvlan.php',$current_page,1);
  530. print "</div>\n";
  531. }
  532. function print_editdevice_submenu ($current_page,$id,$dev_type) {
  533. print "<div id='submenu'>\n";
  534. $dev_id='';
  535. if (isset($id)) { $dev_id='?id='.$id; }
  536. print_submenu_url('Параметры','/admin/devices/editdevice.php'.$dev_id,$current_page,0);
  537. if ($dev_type<=2) {
  538. print_submenu_url('Порты','/admin/devices/switchport.php'.$dev_id,$current_page,0);
  539. print_submenu_url('Состояние','/admin/devices/switchstatus.php'.$dev_id,$current_page,0);
  540. print_submenu_url('Соединения','/admin/devices/switchport-conn.php'.$dev_id,$current_page,1);
  541. }
  542. print "</div>\n";
  543. }
  544. function print_ip_submenu ($current_page) {
  545. print "<div id='submenu'>\n";
  546. print_submenu_url('Список адресов','/admin/iplist/index.php',$current_page,0);
  547. print_submenu_url('Информация для nagios','/admin/iplist/nagios.php',$current_page,0);
  548. print_submenu_url('Дубли','/admin/iplist/doubles.php',$current_page,0);
  549. print_submenu_url('Удалённые адреса','/admin/iplist/deleted.php',$current_page,0);
  550. print_submenu_url('Правила автоназначения','/admin/iplist/auto_rules.php',$current_page,1);
  551. print "</div>\n";
  552. }
  553. function get_nagios_name ($auth)
  554. {
  555. if (!empty($auth['dns_name'])) { return $auth['dns_name']; }
  556. if (!empty($auth['dhcp_hostname'])) { return $auth['dhcp_hostname']; }
  557. if (!empty($auth['comments'])) {
  558. $result = transliterate($auth['comments']);
  559. $result = preg_replace('/\(/', '-', $result);
  560. $result = preg_replace('/\)/', '-', $result);
  561. $result = preg_replace('/--/', '-', $result);
  562. return $result;
  563. }
  564. if (empty($auth['login'])) { $auth['login']='host'; }
  565. return $auth['login']."_".$auth['id'];
  566. }
  567. function get_ou($db, $ou_value)
  568. {
  569. if (!isset($ou_value)) { return; }
  570. $ou_name = get_record_sql($db, "SELECT ou_name FROM OU WHERE id=$ou_value");
  571. if (empty($ou_name)) { return; }
  572. return $ou_name['ou_name'];
  573. }
  574. function get_device_model($db, $model_value)
  575. {
  576. if (!isset($model_value)) { return; }
  577. $model_name = get_record_sql($db, "SELECT model_name FROM device_models WHERE id=$model_value");
  578. if (empty($model_name)) { return; }
  579. return $model_name['model_name'];
  580. }
  581. function get_device_model_name($db, $model_value)
  582. {
  583. if (!isset($model_value)) { return ''; }
  584. $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");
  585. if (empty($model_name)) { return ''; }
  586. return $model_name['name'].' '.$model_name['model_name'];
  587. }
  588. function get_device_model_vendor($db, $model_value)
  589. {
  590. if (!isset($model_value)) { return ''; }
  591. $model_name = get_record_sql($db, "SELECT vendor_id FROM device_models WHERE id=$model_value");
  592. if (empty($model_name)) { return ''; }
  593. return $model_name['vendor_id'];
  594. }
  595. function get_building($db, $building_value)
  596. {
  597. if (!isset($building_value)) { return; }
  598. $building_name = get_record_sql($db, "SELECT name FROM building WHERE id=$building_value");
  599. if (empty($building_name)) { return; }
  600. return $building_name['name'];
  601. }
  602. function print_device_model_select($db, $device_model_name, $device_model_value)
  603. {
  604. print "<select name=\"$device_model_name\" class=\"js-select-single\" style=\"width: 100%\">\n";
  605. $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");
  606. while (list ($f_device_model_id, $f_device_model_name,$f_vendor_name) = mysqli_fetch_array($t_device_model)) {
  607. print_select_item($f_vendor_name." ".$f_device_model_name,$f_device_model_id,$device_model_value);
  608. }
  609. print "</select>\n";
  610. }
  611. function print_group_select($db, $group_name, $group_value)
  612. {
  613. print "<select name=\"$group_name\">\n";
  614. $t_group = mysqli_query($db, "SELECT id,group_name FROM Group_list Order by group_name");
  615. while (list ($f_group_id, $f_group_name) = mysqli_fetch_array($t_group)) {
  616. print_select_item($f_group_name,$f_group_id,$group_value);
  617. }
  618. print "</select>\n";
  619. }
  620. function print_building_select($db, $building_name, $building_value)
  621. {
  622. print "<select name=\"$building_name\">\n";
  623. print_select_item('Всё',0,$building_value);
  624. $t_building = mysqli_query($db, "SELECT id,name FROM building Order by name");
  625. while (list ($f_building_id, $f_building_name) = mysqli_fetch_array($t_building)) {
  626. print_select_item($f_building_name,$f_building_id,$building_value);
  627. }
  628. print "</select>\n";
  629. }
  630. function print_devtypes_select($db, $devtype_name, $devtype_value, $mode)
  631. {
  632. print "<select name=\"$devtype_name\">\n";
  633. print_select_item('Всё',0,$devtype_value);
  634. $filter='';
  635. if (!empty($mode)) { $filter = "WHERE $mode"; }
  636. $t_devtype = mysqli_query($db, "SELECT id,name FROM device_types $filter ORDER BY name");
  637. while (list ($f_devtype_id, $f_devtype_name) = mysqli_fetch_array($t_devtype)) {
  638. print_select_item($f_devtype_name,$f_devtype_id,$devtype_value);
  639. }
  640. print "</select>\n";
  641. }
  642. function print_devtype_select($db, $devtype_name, $devtype_value)
  643. {
  644. print "<select name=\"$devtype_name\">\n";
  645. $t_devtype = mysqli_query($db, "SELECT id,name FROM device_types Order by name");
  646. while (list ($f_devtype_id, $f_devtype_name) = mysqli_fetch_array($t_devtype)) {
  647. print_select_item($f_devtype_name,$f_devtype_id,$devtype_value);
  648. }
  649. print "</select>\n";
  650. }
  651. function get_group($db, $group_value)
  652. {
  653. list ($group_name) = mysqli_fetch_array(mysqli_query($db, "SELECT group_name FROM Group_list WHERE id=$group_value"));
  654. return $group_name;
  655. }
  656. function get_devtype_name($db, $device_type_id)
  657. {
  658. list ($type_name) = mysqli_fetch_array(mysqli_query($db, "SELECT name FROM device_types WHERE id=$device_type_id"));
  659. return $type_name;
  660. }
  661. function get_l3_interfaces($db, $device_id)
  662. {
  663. $wan='';
  664. $lan='';
  665. $t_l3int = mysqli_query($db, "SELECT name,interface_type FROM device_l3_interfaces WHERE device_id=$device_id ORDER BY name");
  666. while (list ($f_name,$f_type) = mysqli_fetch_array($t_l3int)) {
  667. if ($f_type==0) { $lan=$lan." ".$f_name; }
  668. if ($f_type==1) { $wan=$wan." ".$f_name; }
  669. }
  670. $wan=trim($wan);
  671. $lan=trim($lan);
  672. $result='';
  673. if (!empty($wan)) { $result.=' WAN: '.$wan.'<br>'; }
  674. if (!empty($lan)) { $result.=' LAN: '.$lan; }
  675. return trim($result);
  676. }
  677. function print_queue_select($db, $queue_name, $queue_value)
  678. {
  679. print "<select name=\"$queue_name\">\n";
  680. $t_queue = mysqli_query($db, "SELECT id,queue_name FROM Queue_list Order by queue_name");
  681. while (list ($f_queue_id, $f_queue_name) = mysqli_fetch_array($t_queue)) {
  682. print_select_item($f_queue_name,$f_queue_id,$queue_value);
  683. }
  684. print "</select>\n";
  685. }
  686. function get_queue($db, $queue_value)
  687. {
  688. list ($queue_name) = mysqli_fetch_array(mysqli_query($db, "SELECT queue_name FROM Queue_list WHERE id=$queue_value"));
  689. return $queue_name;
  690. }
  691. function print_qa_l3int_select($qa_name, $qa_value)
  692. {
  693. print "<select name=\"$qa_name\">\n";
  694. print_select_item('Внутренний',0,$qa_value);
  695. print_select_item('Внешний',1,$qa_value);
  696. print "</select>\n";
  697. }
  698. function print_qa_rule_select($qa_name, $qa_value)
  699. {
  700. print "<select name=\"$qa_name\">\n";
  701. print_select_item('Subnet',1,$qa_value);
  702. print_select_item('Mac',2,$qa_value);
  703. print_select_item('Hostname',3,$qa_value);
  704. print "</select>\n";
  705. }
  706. function print_qa_select($qa_name, $qa_value)
  707. {
  708. print "<select name=\"$qa_name\">\n";
  709. print_select_item('Да',1,$qa_value);
  710. print_select_item('Нет',0,$qa_value);
  711. print "</select>\n";
  712. }
  713. function print_qa_select_ext($qa_name, $qa_value, $readonly)
  714. {
  715. $state = '';
  716. if ($readonly) { $state='disabled=true'; }
  717. print "<select name=\"$qa_name\">\n";
  718. print_select_item_ext('Да',1,$qa_value, $readonly);
  719. print_select_item_ext('Нет',0,$qa_value, $readonly);
  720. print "</select>\n";
  721. }
  722. function print_snmp_select($qa_name, $qa_value)
  723. {
  724. print "<select name=\"$qa_name\">\n";
  725. print_select_item('Disabled',0,$qa_value);
  726. print_select_item('v1',1,$qa_value);
  727. print_select_item('v2',2,$qa_value);
  728. print_select_item('v3',3,$qa_value);
  729. print "</select>\n";
  730. }
  731. function print_dhcp_select($qa_name, $qa_value)
  732. {
  733. print "<select name=\"$qa_name\">\n";
  734. if (! isset($qa_value) or strlen($qa_value) == 0) {
  735. $qa_value = 'all';
  736. }
  737. print_select_item('Все события','all',$qa_value);
  738. print_select_item('Аренда адреса','add',$qa_value);
  739. print_select_item('Обновление аренды','old',$qa_value);
  740. print_select_item('Освобождение адреса','del',$qa_value);
  741. print "</select>\n";
  742. }
  743. function print_nagios_handler_select($qa_name)
  744. {
  745. print "<select name=\"$qa_name\">\n";
  746. print_select_simple('Нет','');
  747. print_select_simple('restart-port','restart-port');
  748. print "</select>\n";
  749. }
  750. function print_dhcp_acl_select($qa_name)
  751. {
  752. print "<select name=\"$qa_name\">\n";
  753. print_select_simple('Нет','');
  754. print_select_simple('hotspot-free','hotspot-free');
  755. print "</select>\n";
  756. }
  757. function print_enabled_select($qa_name, $qa_value)
  758. {
  759. print "<select name=\"$qa_name\">\n";
  760. if (! isset($qa_value) or strlen($qa_value) == 0) { $qa_value = 0; }
  761. print_select_item('Все',0,$qa_value);
  762. print_select_item('Выключенные',1,$qa_value);
  763. print_select_item('Включенные',2,$qa_value);
  764. print "</select>\n";
  765. }
  766. function print_vendor_select($db, $qa_name, $qa_value)
  767. {
  768. print "<select name=\"$qa_name\" class=\"js-select-single\">\n";
  769. $sSQL = "SELECT id,`name` FROM `vendors` order by `name`";
  770. $vendors = mysqli_query($db, $sSQL);
  771. print_select_item('Всё',0,$qa_value);
  772. while (list ($v_id, $v_name) = mysqli_fetch_array($vendors)) {
  773. print_select_item($v_name,$v_id,$qa_value);
  774. }
  775. print "</select>\n";
  776. }
  777. function print_vendor_set($db, $qa_name, $qa_value)
  778. {
  779. print "<select name=\"$qa_name\" class=\"js-select-single\" style=\"width: 100%\">\n";
  780. $sSQL = "SELECT id,`name` FROM `vendors` order by `name`";
  781. $vendors = mysqli_query($db, $sSQL);
  782. while (list ($v_id, $v_name) = mysqli_fetch_array($vendors)) {
  783. print_select_item($v_name,$v_id,$qa_value);
  784. }
  785. print "</select>\n";
  786. }
  787. function get_vendor_name($db, $v_id)
  788. {
  789. $sSQL = "SELECT `name` FROM `vendors` WHERE id=$v_id";
  790. $vendors = mysqli_query($db, $sSQL);
  791. (list ($v_name) = mysqli_fetch_array($vendors));
  792. return $v_name;
  793. }
  794. function get_qa($qa_value)
  795. {
  796. if ($qa_value == 1) { return "Да"; }
  797. return "Нет";
  798. }
  799. function print_action_select($action_name, $action_value)
  800. {
  801. print "<select name=\"$action_name\">\n";
  802. print_select_item('Разрешить',1,$action_value);
  803. print_select_item('Запретить',0,$action_value);
  804. print "</select>\n";
  805. }
  806. function get_action($action_value)
  807. {
  808. if ($action_value == 1) { return "Разрешить"; }
  809. return "Запретить";
  810. }
  811. function print_filter_select($db, $filter_name, $group_id)
  812. {
  813. print "<select name=\"$filter_name\" class=\"js-select-single\">\n";
  814. if (isset($group_id)) {
  815. $sSQL = "SELECT id,name FROM Filter_list WHERE Filter_list.id not in (Select filter_id FROM Group_filters WHERE group_id=$group_id)";
  816. } else {
  817. $sSQL = "SELECT id,name FROM Filter_list Order by name";
  818. }
  819. $t_filters = mysqli_query($db, $sSQL);
  820. while (list ($filter_id, $filter_name) = mysqli_fetch_array($t_filters)) {
  821. print_select_item($filter_name,$filter_id,0);
  822. }
  823. print "</select>\n";
  824. }
  825. function get_filter($db, $filter_value)
  826. {
  827. list ($filter) = mysqli_fetch_array(mysqli_query($db, "SELECT name FROM Filter_list WHERE id=".$filter_value));
  828. return $filter;
  829. }
  830. function get_login($db, $user_id)
  831. {
  832. list ($login) = mysqli_fetch_array(mysqli_query($db, "SELECT login FROM User_list WHERE id=$user_id"));
  833. return $login;
  834. }
  835. function get_auth_count($db, $user_id)
  836. {
  837. list ($count) = mysqli_fetch_array(mysqli_query($db, "SELECT count(id) FROM User_auth WHERE user_id=$user_id and deleted=0"));
  838. return $count;
  839. }
  840. function print_login_select($db, $login_name, $current_login)
  841. {
  842. print "<select name=\"$login_name\" class=\"js-select-single\">\n";
  843. $t_login = mysqli_query($db, "SELECT id,login FROM User_list Order by Login");
  844. print_select_item('None',0,$current_login);
  845. while (list ($f_user_id, $f_login) = mysqli_fetch_array($t_login)) {
  846. print_select_item($f_login,$f_user_id,$current_login);
  847. }
  848. print "</select>\n";
  849. }
  850. function print_auth_select($db, $login_name, $current_auth)
  851. {
  852. print "<select name=\"$login_name\" class=\"js-select-single\">\n";
  853. $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");
  854. print_select_item('Empty',0,$current_auth);
  855. while (list ($f_login, $f_fio, $f_ip, $f_auth_id) = mysqli_fetch_array($t_login)) {
  856. print_select_item($f_login."[" . $f_fio . "] - ".$f_ip,$f_auth_id,$current_auth);
  857. }
  858. print "</select>\n";
  859. }
  860. function print_auth_select_mac($db, $login_name, $current_auth)
  861. {
  862. print "<select name=\"$login_name\" class=\"js-select-single\">\n";
  863. $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");
  864. print_select_item('Empty',0,$current_auth);
  865. while (list ($f_login, $f_fio, $f_ip, $f_mac, $f_auth_id) = mysqli_fetch_array($t_login)) {
  866. print_select_item($f_login."[" . $f_mac . "] - ".$f_ip,$f_auth_id,$current_auth);
  867. }
  868. print "</select>\n";
  869. }
  870. function compact_port_name($port)
  871. {
  872. $result = $port;
  873. $result = preg_replace('/XGigabitEthernet/','X',$result);
  874. $result = preg_replace('/TenGigabitEthernet/','Te',$result);
  875. $result = preg_replace('/GigabitEthernet/','Gi',$result);
  876. return $result;
  877. }
  878. function print_device_port_select($db, $field_name, $device_id, $target_id)
  879. {
  880. print "<select name=\"$field_name\" class=\"js-select-single\">\n";
  881. if (! isset($target_id)) {
  882. $target_id = 0;
  883. }
  884. if (! isset($device_id)) {
  885. $device_id = 0;
  886. }
  887. $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";
  888. $t_device = mysqli_query($db, $d_sql);
  889. print_select_item('Empty',0,$target_id);
  890. while (list ($f_name, $f_port, $f_device_id, $f_target_id, $f_ifname) = mysqli_fetch_array($t_device)) {
  891. if (empty($f_ifname)) { $f_ifname=$f_port; }
  892. print_select_item($f_name."[" . compact_port_name($f_ifname) . "]",$f_target_id,$target_id);
  893. }
  894. print "</select>\n";
  895. }
  896. function print_device_select($db, $field_name, $device_id)
  897. {
  898. print "<select name=\"$field_name\" class=\"js-select-single\" >\n";
  899. $d_sql = "SELECT D.device_name, D.id FROM devices AS D Where D.deleted=0 order by D.device_name ASC";
  900. $t_device = mysqli_query($db, $d_sql);
  901. print_select_item('Все',0,$device_id);
  902. while (list ($f_name, $f_device_id) = mysqli_fetch_array($t_device)) {
  903. print_select_item($f_name,$f_device_id,$device_id);
  904. }
  905. print "</select>\n";
  906. }
  907. function print_netdevice_select($db, $field_name, $device_id)
  908. {
  909. print "<select name=\"$field_name\" class=\"js-select-single\" >\n";
  910. $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";
  911. $t_device = mysqli_query($db, $d_sql);
  912. print_select_item('Все',0,$device_id);
  913. while (list ($f_name, $f_device_id) = mysqli_fetch_array($t_device)) {
  914. print_select_item($f_name,$f_device_id,$device_id);
  915. }
  916. print "</select>\n";
  917. }
  918. function print_vlan_select($db, $field_name, $vlan)
  919. {
  920. print "<select name=\"$field_name\" class=\"js-select-single\">\n";
  921. $d_sql = "SELECT DISTINCT vlan FROM device_ports ORDER BY vlan DESC";
  922. $v_device = mysqli_query($db, $d_sql);
  923. if (!isset($vlan) or $vlan ==='') { $vlan=1; };
  924. print_select_item('1',1,$vlan);
  925. while (list ($f_vlan) = mysqli_fetch_array($v_device)) {
  926. if ($f_vlan === '1') { continue; }
  927. print_select_item($f_vlan,$f_vlan,$vlan);
  928. }
  929. print "</select>\n";
  930. }
  931. function print_device_select_ip($db, $field_name, $device_ip)
  932. {
  933. print "<select name=\"$field_name\" class=\"js-select-single\" >\n";
  934. $d_sql = "SELECT D.device_name, D.ip FROM devices AS D Where D.deleted=0 order by D.device_name ASC";
  935. $t_device = mysqli_query($db, $d_sql);
  936. print_select_item('Все','',$device_ip);
  937. while (list ($f_name, $f_device_ip) = mysqli_fetch_array($t_device)) {
  938. print_select_item($f_name,$f_device_ip,$device_ip);
  939. }
  940. print "</select>\n";
  941. }
  942. function print_syslog_device_select($db, $field_name, $syslog_filter, $device_ip)
  943. {
  944. print "<select name=\"$field_name\" class=\"js-select-single\" >\n";
  945. $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";
  946. $t_device = mysqli_query($db, $d_sql);
  947. print_select_item('Все','',$device_ip);
  948. while (list ($f_ip, $f_name) = mysqli_fetch_array($t_device)) {
  949. if (!isset($f_name) or $f_name === '') { $f_name=$f_ip; }
  950. print_select_item($f_name,$f_ip,$device_ip);
  951. }
  952. print "</select>\n";
  953. }
  954. function print_gateway_select($db, $field_name, $device_id)
  955. {
  956. print "<select name=\"$field_name\" >\n";
  957. $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";
  958. $t_device = mysqli_query($db, $d_sql);
  959. print_select_item('Все',0,$device_id);
  960. while (list ($f_name, $f_device_id) = mysqli_fetch_array($t_device)) {
  961. print_select_item($f_name,$f_device_id,$device_id);
  962. }
  963. print "</select>\n";
  964. }
  965. function get_gateways($db)
  966. {
  967. $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";
  968. $t_device = mysqli_query($db, $d_sql);
  969. unset($result);
  970. while (list ($f_name, $f_device_id) = mysqli_fetch_array($t_device)) {
  971. $result[$f_device_id] = $f_name;
  972. }
  973. return $result;
  974. }
  975. function print_device_port($db, $target_id)
  976. {
  977. $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";
  978. $t_device = mysqli_query($db, $d_sql);
  979. while (list ($f_name, $f_port, $f_device_id) = mysqli_fetch_array($t_device)) {
  980. print "<a href=\"/admin/devices/switchport.php?id=$f_device_id\">" . $f_name . "[" . $f_port . "]</a>\n";
  981. }
  982. }
  983. function get_device_ips($db, $device_id)
  984. {
  985. $switch=get_record($db,'devices','id='.$device_id);
  986. $index=0;
  987. if (!empty($switch['user_id'])) {
  988. $auth_ips=get_records($db,'User_auth','deleted=0 and user_id='.$switch['user_id']);
  989. foreach ( $auth_ips as $key => $value ) {
  990. if (isset($value['ip'])) { $result[$index]=$value['ip']; $index++; }
  991. }
  992. } else {
  993. if (isset($switch['ip'])) { $result[$index]=$switch['ip']; $index++; }
  994. }
  995. return $result;
  996. }
  997. function get_device_id($db, $device_name)
  998. {
  999. $d_sql = "SELECT id FROM devices WHERE device_name='$device_name' and deleted=0";
  1000. $t_device = mysqli_query($db, $d_sql);
  1001. list ($f_device_id) = mysqli_fetch_array($t_device);
  1002. return $f_device_id;
  1003. }
  1004. function get_device_name($db, $device_id)
  1005. {
  1006. $d_sql = "SELECT device_name FROM devices WHERE id='$device_id'";
  1007. $t_device = mysqli_query($db, $d_sql);
  1008. list ($f_device_name) = mysqli_fetch_array($t_device);
  1009. return $f_device_name;
  1010. }
  1011. function get_auth_by_ip($db, $ip)
  1012. {
  1013. $d_sql = "SELECT id FROM User_auth WHERE ip='$ip' and deleted=0";
  1014. $auth_q = mysqli_query($db, $d_sql);
  1015. list ($f_auth_id) = mysqli_fetch_array($auth_q);
  1016. return $f_auth_id;
  1017. }
  1018. function get_user_by_ip($db, $ip)
  1019. {
  1020. $d_sql = "SELECT user_id FROM User_auth WHERE ip='$ip' and deleted=0";
  1021. $auth_q = mysqli_query($db, $d_sql);
  1022. list ($f_auth_id) = mysqli_fetch_array($auth_q);
  1023. return $f_auth_id;
  1024. }
  1025. function get_device_by_auth($db, $id)
  1026. {
  1027. $d_sql = "SELECT id FROM devices WHERE user_id=$id and deleted=0";
  1028. $f_dev = get_record_sql($db,$d_sql);
  1029. return $f_dev['id'];
  1030. }
  1031. function print_auth_port($db, $port_id)
  1032. {
  1033. $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";
  1034. $t_device = mysqli_query($db, $d_sql);
  1035. while (list ($f_ip, $f_int, $f_mac, $f_auth_id, $f_dns) = mysqli_fetch_array($t_device)) {
  1036. $name = $f_ip;
  1037. if (isset($f_dns) and $f_dns != '') {
  1038. $name = $f_dns;
  1039. }
  1040. print "<a href=\"/admin/users/editauth.php?id=$f_auth_id\">" . $name . " [" . $f_ip . "]</a><br>";
  1041. }
  1042. }
  1043. function print_auth_simple($db, $auth_id)
  1044. {
  1045. $auth = get_record($db,"User_auth","id=$auth_id");
  1046. $name = $auth['dns_name'];
  1047. if (empty($name)) { $name = $auth['comments']; }
  1048. if (empty($name)) { $name = $auth['ip']; }
  1049. print "<a href=\"/admin/users/editauth.php?id=$auth_id\">" . $name . "</a><br>";
  1050. }
  1051. function print_auth($db, $auth_id)
  1052. {
  1053. $auth = get_record($db,"User_auth","id=$auth_id");
  1054. $name = $auth['dns_name'];
  1055. if (empty($name)) { $name = $auth['comments']; } else { $name.=" (".$auth['comments'].")"; }
  1056. if (empty($name)) { $name = $auth['ip']; } else { $name.=" [".$auth['ip']."]"; }
  1057. print "<a href=\"/admin/users/editauth.php?id=$auth_id\">" . $name . "</a><br>";
  1058. }
  1059. function print_auth_detail($db, $auth_id)
  1060. {
  1061. $auth = get_record($db,"User_auth","id=$auth_id");
  1062. $name = $auth['dns_name'];
  1063. if (empty($name)) { $name = $auth['comments']; } else { $name.=" (".$auth['comments'].")"; }
  1064. if (empty($name)) { $name = $auth['ip']; } else { $name.=" [".$auth['ip']."]"; }
  1065. $name.=" last: [".$auth['last_found']."] ";
  1066. if ($auth['deleted'] ==1) { $name.=" <font color='red'>DELETED!!!</font>"; }
  1067. print "<a href=\"/admin/users/editauth.php?id=$auth_id\">" . $name . "</a><br>";
  1068. }
  1069. function get_auth_port_count($db, $port_id)
  1070. {
  1071. $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";
  1072. $t_device = mysqli_query($db, $d_sql);
  1073. list ($f_count) = mysqli_fetch_array($t_device);
  1074. if (! isset($f_count)) {
  1075. $f_count = 0;
  1076. }
  1077. return $f_count;
  1078. }
  1079. function get_connection($db, $auth_id)
  1080. {
  1081. $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";
  1082. $t_device = mysqli_query($db, $d_sql);
  1083. list ($f_name, $f_port) = mysqli_fetch_array($t_device);
  1084. if (isset($f_name)) {
  1085. $result = expand_device_name($db, $f_name) . "[" . $f_port . "]";
  1086. } else {
  1087. $result = '';
  1088. }
  1089. return $result;
  1090. }
  1091. function get_port($db, $port_id)
  1092. {
  1093. $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";
  1094. $t_device = mysqli_query($db, $d_sql);
  1095. list ($f_name, $f_port) = mysqli_fetch_array($t_device);
  1096. if (isset($f_name)) {
  1097. $result = expand_device_name($db, $f_name) . "[" . $f_port . "]";
  1098. } else {
  1099. $result = '';
  1100. }
  1101. return $result;
  1102. }
  1103. function print_option_select($db, $option_name)
  1104. {
  1105. print "<select name=\"$option_name\">\n";
  1106. $t_option = mysqli_query($db, "SELECT id,option_name FROM config_options WHERE uniq=0 order by option_name");
  1107. while (list ($f_id, $f_name) = mysqli_fetch_array($t_option)) {
  1108. print "<option value=$f_id>$f_name</option>";
  1109. }
  1110. $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");
  1111. while (list ($f_id, $f_name) = mysqli_fetch_array($t_option)) {
  1112. print "<option value=$f_id>$f_name</option>";
  1113. }
  1114. print "</select>\n";
  1115. }
  1116. function run_sql($db, $query)
  1117. {
  1118. $sql_result = mysqli_query($db, $query);
  1119. if ($sql_result === false) { LOG_ERROR($db, "At simple SQL: $query :".mysqli_error($db)); return; }
  1120. return $sql_result;
  1121. }
  1122. function get_count_records($db, $table, $filter)
  1123. {
  1124. if (!empty($filter)) { $filter = 'where ' . $filter; }
  1125. $t_count = mysqli_query($db, "SELECT count(*) FROM $table $filter");
  1126. list ($count) = mysqli_fetch_array($t_count);
  1127. if (! isset($count)) { $count = 0; }
  1128. return $count;
  1129. }
  1130. function get_id_record($db, $table, $filter)
  1131. {
  1132. if (isset($filter)) {
  1133. $filter = 'WHERE ' . $filter;
  1134. }
  1135. $t_record = mysqli_query($db, "SELECT id FROM $table $filter limit 1");
  1136. list ($id) = mysqli_fetch_array($t_record);
  1137. return $id;
  1138. }
  1139. function set_changed($db,$id)
  1140. {
  1141. $auth['changed'] = 1;
  1142. update_record($db, "User_auth", "id=" . $id, $auth);
  1143. }
  1144. function ResolveIP($db,$ip_int) {
  1145. $ip_name="-";
  1146. if (empty($ip_int)) { return $ip_name; }
  1147. $dns_cache=get_record_sql($db,"SELECT * FROM dns_cache WHERE ip=$ip_int");
  1148. if (empty($dns_cache) or empty($dns_cache['dns'])) {
  1149. $ip_name = gethostbyaddr(long2ip($ip_int));
  1150. if (empty($ip_name) or $ip_name === long2ip($ip_int)) { $ip_name="-"; }
  1151. run_sql($db,"INSERT INTO dns_cache(dns,ip) VALUES('".$ip_name."',".$ip_int.")");
  1152. } else { $ip_name=$dns_cache['dns']; }
  1153. return $ip_name;
  1154. }
  1155. function clean_dns_cache($db) {
  1156. $date = time();
  1157. $date = $date - 86400;
  1158. $now = strftime('%Y-%m-%d %H:%M:%S',time());
  1159. $clean_date=strftime('%Y-%m-%d %H:%M:%S',$date);
  1160. #LOG_DEBUG($db,"Clean dns cache before $clean_date at $now");
  1161. run_sql($db,"DELETE FROM dns_cache WHERE `timestamp`<='".$clean_date."'");
  1162. }
  1163. function FormatDateStr($format = 'Y-m-d H:i:s', $date_str) {
  1164. $date1 = GetDateTimeFromString($date_str);
  1165. $result = $date1->format($format);
  1166. return $result;
  1167. }
  1168. function GetDateTimeFromString($date_str) {
  1169. if (!is_a($date_str,'DateTime')) {
  1170. $t_date_str = urldecode($date_str);
  1171. $t_date_str = preg_replace('/(\'|\")/','',$t_date_str);
  1172. $t_date_str = preg_replace('/T/',' ',$t_date_str);
  1173. $date1 = DateTime::createFromFormat('Y-m-d H:i:s',$t_date_str);
  1174. if (!$date1) { $date1 = DateTime::createFromFormat('Y.m.d H:i:s',$t_date_str); }
  1175. if (!$date1) { $date1 = DateTime::createFromFormat('Y/m/d H:i:s',$t_date_str); }
  1176. if (!$date1) { $date1 = DateTime::createFromFormat('Y-m-d H:i',$t_date_str); }
  1177. if (!$date1) { $date1 = DateTime::createFromFormat('Y.m.d H:i',$t_date_str); }
  1178. if (!$date1) { $date1 = DateTime::createFromFormat('Y/m/d H:i',$t_date_str); }
  1179. if (!$date1) { $date1 = DateTime::createFromFormat('Y-m-d|',$t_date_str); }
  1180. if (!$date1) { $date1 = DateTime::createFromFormat('Y.m.d|',$t_date_str); }
  1181. if (!$date1) { $date1 = DateTime::createFromFormat('Y/m/d|',$t_date_str); }
  1182. if (!$date1) { $date1 = new DateTime; $date1->setTime(0,0,0,1); }
  1183. } else { return $date_str; }
  1184. return $date1;
  1185. }
  1186. function GetNowTimeString() {
  1187. $now = strftime('%Y-%m-%d %H:%M:%S',time());
  1188. return $now;
  1189. }
  1190. function GetNowDayString() {
  1191. $now = strftime('%Y-%m-%d',time());
  1192. return $now;
  1193. }
  1194. function get_ip_subnet($db,$ip)
  1195. {
  1196. if (empty($ip)) { return; }
  1197. $ip_aton = ip2long($ip);
  1198. $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)");
  1199. if (empty($user_subnet)) { return; }
  1200. return $user_subnet;
  1201. }
  1202. function find_mac_in_subnet($db,$ip,$mac)
  1203. {
  1204. if (empty($ip)) { return; }
  1205. if (empty($mac)) { return; }
  1206. $ip_subnet=get_ip_subnet($db,$ip);
  1207. if (empty($ip_subnet)) { return; }
  1208. $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");
  1209. $auth_count=0;
  1210. $result['count']=0;
  1211. $result['users_id']=[];
  1212. foreach ($t_auth as $row) {
  1213. if (!empty($row['id'])) {
  1214. $auth_count++;
  1215. $result['count']=$auth_count;
  1216. $result[$auth_count]=$row['id'];
  1217. array_push($result['users_id'],$row['user_id']);
  1218. }
  1219. }
  1220. return $result;
  1221. }
  1222. function apply_auth_rule($db,$auth_id,$user_id) {
  1223. if (empty($auth_id)) { return; }
  1224. if (empty($user_id)) { return; }
  1225. $user_rec = get_record($db, 'User_list', "id=".$user_id);
  1226. if (empty($user_rec)) { return; }
  1227. //set filter and status by user
  1228. $set_auth['ou_id']=$user_rec['ou_id'];
  1229. $set_auth['user_id']=$user_rec['id'];
  1230. $set_auth['filter_group_id']=$user_rec['filter_group_id'];
  1231. $set_auth['queue_id']= $user_rec['queue_id'];
  1232. $set_auth['enabled'] = $user_rec['enabled'];
  1233. $set_auth['changed'] = 1;
  1234. update_record($db, "User_auth", "id=$auth_id", $set_auth);
  1235. return $set_auth;
  1236. }
  1237. function fix_auth_rules($db) {
  1238. global $default_user_ou_id;
  1239. global $default_hotspot_ou_id;
  1240. //cleanup hotspot subnet rules
  1241. delete_record($db,"auth_rules","ou_id=".$default_user_ou_id);
  1242. delete_record($db,"auth_rules","ou_id=".$default_hotspot_ou_id);
  1243. $t_hotspot = get_records_sql($db,"SELECT * FROM subnets WHERE hotspot=1");
  1244. if (!empty($t_hotspot)) {
  1245. foreach ($t_hotspot as $row) { delete_record($db,"auth_rules","rule='".$row['subnet']."'"); }
  1246. }
  1247. }
  1248. #---------------------------------------------------------------------------------------------------------------
  1249. function new_user($db,$user_info) {
  1250. global $auto_mac_rule;
  1251. if (!empty($user_info['mac'])) {
  1252. $user['login']=mac_dotted($user_info['mac']);
  1253. } else {
  1254. $user['login']=$user_info['ip'];
  1255. }
  1256. if (!empty($user_info['dhcp_hostname'])) {
  1257. $user['fio']=$user_info['ip']. '['.$user_info['dhcp_hostname'] .']';
  1258. } else {
  1259. $user['fio']=$user_info['ip'];
  1260. }
  1261. $login_count = get_count_records($db,"User_list","(login LIKE '".$user['login']."(%)') OR (login='".$user['login']."')");
  1262. if (!empty($login_count) and $login_count>0) { $login_count++; $user['login'] = $user['login']."(".$login_count.")"; }
  1263. $user['ou_id'] = $user_info['ou_id'];
  1264. $ou_info = get_record_sql($db,"SELECT * FROM OU WHERE id=".$user_info['ou_id']);
  1265. if (!empty($ou_info)) {
  1266. $user['enabled'] = $ou_info['enabled'];
  1267. $user['queue_id'] = $ou_info['queue_id'];
  1268. $user['filter_group_id'] = $ou_info['filter_group_id'];
  1269. }
  1270. $result = insert_record($db,"User_list",$user);
  1271. if (!empty($result) and $auto_mac_rule and $user_info['mac']) {
  1272. $auth_rule['user_id'] = $result;
  1273. $auth_rule['type'] = 2;
  1274. $auth_rule['rule'] = mac_dotted($user_info['mac']);
  1275. insert_record($db,"auth_rules",$auth_rule);
  1276. }
  1277. return $result;
  1278. }
  1279. function new_auth($db, $ip, $mac, $user_id)
  1280. {
  1281. $ip_aton = ip2long($ip);
  1282. $msg = '';
  1283. if (!empty($mac)) {
  1284. $auth_record = get_record_sql($db, "SELECT * FROM User_auth WHERE ip_int=$ip_aton AND mac='" . mac_dotted($mac) . "' AND deleted=0");
  1285. if (!empty($auth_record)) {
  1286. LOG_WARNING($db, "Pair ip-mac already exists! Skip creating $ip [$mac] auth_id: $aid");
  1287. return $auth_record['id'];
  1288. }
  1289. }
  1290. // default id
  1291. $save_traf = get_option($db, 23);
  1292. $resurrection_id = NULL;
  1293. // seek old auth with same ip and mac
  1294. $resurrection_id = get_id_record($db, 'User_auth', " deleted=1 AND ip_int=" . $ip_aton . " AND mac='" . $mac . "'");
  1295. if (!empty($resurrection_id)) {
  1296. $msg.="Восстанавливаем доступ для auth_id: $resurrection_id with ip: $ip and mac: $mac ";
  1297. $auth['user_id'] = $user_id;
  1298. $auth['deleted'] = 0;
  1299. $auth['save_traf'] = $save_traf *1;
  1300. update_record($db, "User_auth", "id=$resurrection_id", $auth);
  1301. } else {
  1302. // not found ->create new record
  1303. $msg.="Создаём новый ip-адрес \r\nip: $ip\r\nmac: $mac\r\n";
  1304. $auth['deleted'] = 0;
  1305. $auth['user_id'] = $user_id;
  1306. $auth['ip'] = $ip;
  1307. $auth['ip_int'] = $ip_aton;
  1308. $auth['mac'] = $mac;
  1309. $auth['save_traf'] = $save_traf *1;
  1310. $resurrection_id=insert_record($db, "User_auth", $auth);
  1311. }
  1312. //check rules, update filter and state for new record
  1313. if (!empty($resurrection_id)) {
  1314. apply_auth_rule($db,$resurrection_id,$user_id);
  1315. if (!is_hotspot($db,$ip) and !empty($msg)) { LOG_WARNING($db, $msg); }
  1316. if (is_hotspot($db,$ip) and !empty($msg)) { LOG_INFO($db, $msg); }
  1317. }
  1318. return $resurrection_id;
  1319. }
  1320. function resurrection_auth($db, $ip, $mac, $action, $dhcp_hostname)
  1321. {
  1322. $ip_aton = ip2long($ip);
  1323. $auth_record= get_record_sql($db, "SELECT * FROM User_auth WHERE ip_int=$ip_aton AND mac='" . $mac . "' AND deleted=0");
  1324. if (!empty($auth_record)) {
  1325. $user_info = get_record_sql($db, "SELECT * FROM User_list WHERE id=".$auth_record['user_id']);
  1326. LOG_DEBUG($db, "external dhcp user ".$user_info['login']." [".$ip."] auth_id: ".$auth_record['id']);
  1327. if (isset($dhcp_hostname) and !empty($dhcp_hostname)) { $auth['dhcp_hostname'] = $dhcp_hostname; }
  1328. $auth['dhcp_action'] = $action;
  1329. $auth['dhcp_time'] = GetNowTimeString();
  1330. if ($action == 'add') { $auth['last_found'] = GetNowTimeString(); }
  1331. update_record($db, "User_auth", "id=" . $auth_record['id'], $auth);
  1332. return $auth_record['id'];
  1333. }
  1334. $ip_subnet=get_ip_subnet($db,$ip);
  1335. if ($ip_subnet['static']) {
  1336. LOG_WARNING($db, "Unknown pair ip+mac in static subnet! ip: $ip mac: [".mac_dotted($mac)."]. Skip");
  1337. return;
  1338. }
  1339. $msg = '';
  1340. // search changed mac
  1341. $auth_record = get_record_sql($db, "SELECT * FROM User_auth WHERE ip_int=$ip_aton AND deleted=0");
  1342. if (!empty($auth_record)) {
  1343. if (empty($auth_record['mac'])) {
  1344. $auth['mac'] = mac_dotted($mac);
  1345. $auth['dhcp_action'] = $action;
  1346. $auth['dhcp_time'] = GetNowTimeString();
  1347. if (!empty($dhcp_hostname)) { $auth['dhcp_hostname'] = $dhcp_hostname; }
  1348. if ($action == 'add') { $auth['last_found'] = GetNowTimeString(); }
  1349. LOG_INFO($db, "for ip: $ip mac not found! Use empty record...");
  1350. update_record($db, "User_auth", "id=" . $auth_record['id'], $auth);
  1351. return $auth_record['id'];
  1352. } else {
  1353. 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']);
  1354. run_sql($db, "UPDATE User_auth SET changed=1, deleted=1 WHERE id=" . $auth_record['id']);
  1355. }
  1356. }
  1357. // default id
  1358. $new_user_info = get_new_user_id($db, $ip, $mac, $dhcp_hostname);
  1359. if (!empty($new_user_info['user_id'])) { $new_user_id = $new_user_info['user_id']; }
  1360. if (empty($new_user_id)) { $new_user_id = new_user($db,$new_user_info); }
  1361. $resurrection_id = NULL;
  1362. $save_traf = get_option($db, 23);
  1363. $auth_record =get_record_sql($db, 'User_auth', "ip_int=" . $ip_aton . " and mac='" . $mac . "'");
  1364. // seek old auth with same ip and mac
  1365. if (!empty($auth_record)) {
  1366. // found ->Resurrection old record
  1367. $resurrection_id = $auth_record['id'];
  1368. $msg .="Восстанавливаем доступ для auth_id: $resurrection_id with ip: $ip and mac: $mac ";
  1369. $auth['dhcp_action'] = $action;
  1370. $auth['user_id'] = $new_user_id;
  1371. $auth['deleted'] = 0;
  1372. $auth['dhcp_time'] = GetNowTimeString();
  1373. $auth['save_traf'] = $save_traf *1;
  1374. if (!empty($dhcp_hostname)) { $auth['dhcp_hostname'] = $dhcp_hostname; }
  1375. if ($action == 'add') { $auth['last_found'] = GetNowTimeString(); }
  1376. update_record($db, "User_auth", "id=$resurrection_id", $auth);
  1377. } else {
  1378. // not found ->create new record
  1379. $msg.="Создаём новый ip-адрес \r\nip: $ip\r\nmac: $mac\r\n";
  1380. $auth['deleted'] = 0;
  1381. $auth['user_id'] = $new_user_id;
  1382. $auth['ip'] = $ip;
  1383. $auth['ip_int'] = $ip_aton;
  1384. $auth['mac'] = $mac;
  1385. $auth['dhcp_action'] = $action;
  1386. $auth['dhcp_time'] = GetNowTimeString();
  1387. $auth['save_traf'] = $save_traf *1;
  1388. if (!empty($dhcp_hostname)) { $auth['dhcp_hostname'] = $dhcp_hostname; }
  1389. if ($action == 'add') { $auth['last_found'] = GetNowTimeString(); }
  1390. $resurrection_id=insert_record($db, "User_auth", $auth);
  1391. }
  1392. //check rules, update filter and state for new record
  1393. if (!empty($resurrection_id)) {
  1394. $user_rec=apply_auth_rule($db,$resurrection_id,$new_user_id);
  1395. $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";
  1396. if (!is_hotspot($db,$ip) and !empty($msg)) { LOG_WARNING($db, $msg); }
  1397. if (is_hotspot($db,$ip) and !empty($msg)) { LOG_INFO($db, $msg); }
  1398. }
  1399. return $resurrection_id;
  1400. }
  1401. function get_auth($db, $current_auth)
  1402. {
  1403. if (! isset($current_auth)) {
  1404. return;
  1405. }
  1406. if ($current_auth == 0) {
  1407. return;
  1408. }
  1409. $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");
  1410. list ($f_login, $f_ip) = mysqli_fetch_array($t_login);
  1411. $result = $f_login . "[" . $f_ip . "]";
  1412. return $result;
  1413. }
  1414. function get_auth_by_mac($db, $mac)
  1415. {
  1416. if (! isset($mac)) { return; }
  1417. $mac = mac_dotted($mac);
  1418. $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");
  1419. list ($f_id, $f_login, $f_auth_id, $f_ip) = mysqli_fetch_array($t_login);
  1420. if (isset($f_id)) {
  1421. $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>';
  1422. } else { $result['auth']='Unknown'; }
  1423. $result['mac'] = expand_mac($db,$mac);
  1424. return $result;
  1425. }
  1426. function get_auth_mac($db, $current_auth)
  1427. {
  1428. if (! isset($current_auth)) {
  1429. return;
  1430. }
  1431. if ($current_auth == 0) {
  1432. return;
  1433. }
  1434. $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");
  1435. list ($f_login, $f_mac) = mysqli_fetch_array($t_login);
  1436. $result = $f_login . "[" . $f_mac . "]";
  1437. return $result;
  1438. }
  1439. function isRO($db,$table)
  1440. {
  1441. $result = 1;
  1442. if (isset($_SESSION['login'])) {
  1443. $work_user = $_SESSION['login'];
  1444. }
  1445. if (isset($_SESSION['user_id'])) {
  1446. $work_id = $_SESSION['user_id'];
  1447. }
  1448. if (! isset($work_user) or ! isset($work_id)) {
  1449. return $result;
  1450. }
  1451. if (preg_match('/^(variables|dns_cache|syslog)$/',$table)) { return $result; }
  1452. $t_login = mysqli_query($db, "SELECT readonly FROM Customers WHERE Login='" . $work_user . "' and id='" . $work_id . "'");
  1453. list ($f_ro) = mysqli_fetch_array($t_login);
  1454. if (! isset($f_ro)) {
  1455. return $result;
  1456. }
  1457. return $f_ro;
  1458. }
  1459. function LOG_INFO($db,$msg,$auth_id = 0) {
  1460. global $L_INFO;
  1461. global $log_level;
  1462. if ($log_level < $L_INFO) { return; }
  1463. write_log($db,$msg,$L_INFO,$auth_id);
  1464. }
  1465. function LOG_ERROR($db,$msg,$auth_id = 0) {
  1466. global $L_ERROR;
  1467. global $log_level;
  1468. if ($log_level < $L_ERROR) { return; }
  1469. email($L_ERROR,$msg);
  1470. write_log($db,$msg,$L_ERROR,$auth_id);
  1471. }
  1472. function LOG_VERBOSE($db,$msg,$auth_id=0) {
  1473. global $L_VERBOSE;
  1474. global $log_level;
  1475. if ($log_level < $L_VERBOSE) { return; }
  1476. write_log($db,$msg,$L_VERBOSE,$auth_id);
  1477. }
  1478. function LOG_WARNING($db,$msg,$auth_id=0) {
  1479. global $L_WARNING;
  1480. global $log_level;
  1481. if ($log_level < $L_WARNING) { return; }
  1482. email($L_WARNING,$msg);
  1483. write_log($db,$msg,$L_WARNING,$auth_id);
  1484. }
  1485. function LOG_DEBUG($db,$msg,$auth_id=0) {
  1486. global $debug;
  1487. global $L_DEBUG;
  1488. if (isset($debug) and $debug) { write_log($db,$msg,$L_DEBUG,$auth_id); }
  1489. }
  1490. function get_first_line($msg) {
  1491. if (empty($msg)) { return; }
  1492. preg_match('/(.*)(\n|\<br\>)/', $msg, $matches);
  1493. if (!empty($matches[1])) { return $matches[1]; }
  1494. return;
  1495. }
  1496. function email ($level,$msg) {
  1497. global $send_email;
  1498. global $admin_email;
  1499. global $sender_email;
  1500. global $L_WARNING;
  1501. global $L_ERROR;
  1502. if (!$send_email) { return; }
  1503. if (!($level === $L_WARNING or $level === $L_ERROR)) { return; }
  1504. $subject = get_first_line($msg);
  1505. if ($level === $L_WARNING) { $subject = "WARN: ".$subject."..."; $message = 'WARNING! Manager: '.$_SESSION['login'].' </br>'; }
  1506. if ($level === $L_ERROR) { $subject = "ERROR: ".$subject."..."; $message = 'ERROR! Manager: '.$_SESSION['login'].' </br>'; }
  1507. $msg_lines = preg_replace("/\r\n/","</br>",$msg);
  1508. $message .= $msg_lines;
  1509. $send = SimpleMail::make()
  1510. ->setTo($admin_email, 'Administrator')
  1511. ->setFrom($sender_email, 'Stat')
  1512. ->setSubject($subject)
  1513. ->setMessage($message)
  1514. ->setHtml()
  1515. ->setWrap(80)
  1516. ->send();
  1517. }
  1518. function write_log($db, $msg, $level, $auth_id = 0)
  1519. {
  1520. $work_user = 'http';
  1521. if (isset($_SESSION['login'])) {
  1522. $work_user = $_SESSION['login'];
  1523. }
  1524. if (! isset($msg)) {
  1525. $msg = 'ERROR! Empty log string!';
  1526. }
  1527. global $L_INFO;
  1528. if (!isset($level)) { $level = $L_INFO; }
  1529. $msg = str_replace("'", '', $msg);
  1530. $sSQL = "insert into syslog(customer,message,level,auth_id) values('$work_user','$msg',$level,$auth_id)";
  1531. mysqli_query($db, $sSQL);
  1532. }
  1533. function print_year_select($year_name, $year)
  1534. {
  1535. print "<select name=\"$year_name\" class=\"js-select-single\" >\n";
  1536. for ($i = $year - 10; $i <= $year + 10; $i ++) {
  1537. print_select_item($i,$i,$year);
  1538. }
  1539. print "</select>\n";
  1540. }
  1541. function print_date_select($dd, $mm, $yy)
  1542. {
  1543. if ($dd >= 1) {
  1544. print "<b>День</b>\n";
  1545. print "<select name=\"day\" class=\"js-select-single\" >\n";
  1546. for ($i = 1; $i <= 31; $i ++) {
  1547. print_select_item($i,$i,$dd);
  1548. }
  1549. print "</select>\n";
  1550. }
  1551. if ($mm >= 1) {
  1552. print "<b>Месяц</b>\n";
  1553. print "<select name=\"month\" class=\"js-select-single\" >\n";
  1554. for ($i = 1; $i <= 12; $i ++) {
  1555. $month_name = strftime("%B", strtotime("$i/01/$yy"));
  1556. print_select_item($month_name,$i,$mm);
  1557. }
  1558. print "</select>\n";
  1559. }
  1560. print "<b>Год</b>\n";
  1561. print_year_select('year', $yy);
  1562. }
  1563. function print_date_select2($dd, $mm, $yy)
  1564. {
  1565. if ($dd >= 1) {
  1566. print "<b>День</b>\n";
  1567. print "<select name=\"day2\" class=\"js-select-single\" >\n";
  1568. for ($i = 1; $i <= 31; $i ++) {
  1569. print_select_item($i,$i,$dd);
  1570. }
  1571. print "</select>\n";
  1572. }
  1573. if ($mm >= 1) {
  1574. print "<b>Месяц</b>\n";
  1575. print "<select name=\"month2\" class=\"js-select-single\" >\n";
  1576. for ($i = 1; $i <= 12; $i ++) {
  1577. $month_name = strftime("%B", strtotime("$i/01/$yy"));
  1578. print_select_item($month_name,$i,$mm);
  1579. }
  1580. print "</select>\n";
  1581. }
  1582. print "<b>Год</b>\n";
  1583. print_year_select('year2', $yy);
  1584. }
  1585. function is_up($ip)
  1586. {
  1587. if (! isset($ip) or strlen($ip) == 0) {
  1588. return false;
  1589. }
  1590. exec(sprintf('ping -i .3 -c 1 -W 5 %s', escapeshellarg($ip)), $res, $rval);
  1591. return $rval === 0;
  1592. }
  1593. function get_mac_port_table($ip, $port_index, $community, $version, $oid, $index_map)
  1594. {
  1595. if (! isset($ip)) {
  1596. return;
  1597. }
  1598. if (! isset($port_index)) {
  1599. return;
  1600. }
  1601. if (! isset($oid)) {
  1602. return;
  1603. }
  1604. if (! isset($community)) {
  1605. $community = 'public';
  1606. }
  1607. if (! isset($version)) {
  1608. $version = '2';
  1609. }
  1610. $mac_table = walk_snmp($ip, $community, $version, $oid);
  1611. if (isset($mac_table) and count($mac_table) > 0) {
  1612. foreach ($mac_table as $key => $value) {
  1613. $key = trim($key);
  1614. $value = intval(trim(str_replace('INTEGER:', '', $value)));
  1615. $value = $index_map[$value];
  1616. if ($value == $port_index) {
  1617. $pattern = '/\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})$/';
  1618. $result = preg_match($pattern, $key, $matches);
  1619. if ($result) {
  1620. $mac_key = preg_replace('/^\./', '', $matches[0]);
  1621. $fdb_port_table[$mac_key] = $value;
  1622. }
  1623. }
  1624. }
  1625. }
  1626. return $fdb_port_table;
  1627. }
  1628. function get_ifmib_index_table ($ip, $community, $version)
  1629. {
  1630. global $ifmib_ifindex_map;
  1631. global $ifmib_ifindex;
  1632. $ifmib_map = NULL;
  1633. $index_table = walk_snmp($ip, $community, $version, $ifmib_ifindex_map);
  1634. $is_mikrotik = walk_snmp($ip, $community, '.1.3.6.1.2.1.9999.1.1.1.1.0', $version);
  1635. if (isset($index_table) and count($index_table) > 0 and isset($is_mikrotik)) {
  1636. foreach ($index_table as $key => $value) {
  1637. $key = trim($key);
  1638. $value = intval(trim(str_replace('INTEGER:', '', $value)));
  1639. $result = preg_match('/\.(\d{1,10})$/',$key,$matches);
  1640. if ($result) {
  1641. $int_index = preg_replace('/^\./', '', $matches[0]);
  1642. $ifmib_map[$int_index]=$value;
  1643. }
  1644. }
  1645. } else {
  1646. $index_table = walk_snmp($ip, $community, $version, $ifmib_ifindex);
  1647. if (isset($index_table) and count($index_table) > 0) {
  1648. foreach ($index_table as $key => $value) {
  1649. $value = intval(trim(str_replace('INTEGER:', '', $value)));
  1650. $ifmib_map[$value]=$value;
  1651. }
  1652. }
  1653. }
  1654. return $ifmib_map;
  1655. }
  1656. function get_fdb_port_table($ip, $port_index, $community, $version)
  1657. {
  1658. global $mac_table_str_oid;
  1659. global $mac_table_str_oid2;
  1660. global $mac_table_oid;
  1661. global $mac_table_oid2;
  1662. if (! isset($ip)) {
  1663. return;
  1664. }
  1665. if (! isset($port_index)) {
  1666. return;
  1667. }
  1668. if (! isset($community)) {
  1669. $community = 'public';
  1670. }
  1671. if (! isset($version)) {
  1672. $version = '2';
  1673. }
  1674. $ifindex_map = get_ifmib_index_table($ip,$community, $version);
  1675. $fdb1_port_table = get_mac_port_table($ip, $port_index, $community, $version, $mac_table_oid2, $ifindex_map);
  1676. $fdb2_port_table = get_mac_port_table($ip, $port_index, $community, $version, $mac_table_oid, $ifindex_map);
  1677. if (!empty($fdb1_port_table)) { $fdb_port_table = $fdb1_port_table; }
  1678. if (!empty($fdb2_port_table)) {
  1679. if (empty($fdb_port_table)) {
  1680. $fdb_port_table = $fdb2_port_table;
  1681. } else {
  1682. foreach ($fdb2_port_table as $mac => $port) {
  1683. if (empty($fdb_port_table[$mac])) { $fdb_port_table[$mac]=$port; }
  1684. }
  1685. }
  1686. }
  1687. // maybe cisco?!
  1688. if (! isset($fdb_port_table) or ! $fdb_port_table or count($fdb_port_table) == 0) {
  1689. global $cisco_vlan_oid;
  1690. $vlan_table = walk_snmp($ip, $community, $version, $cisco_vlan_oid);
  1691. if (! $vlan_table) {
  1692. return;
  1693. }
  1694. //fucking cisco!!!
  1695. foreach ($vlan_table as $vlan_oid => $value) {
  1696. if (! $vlan_oid) { continue; }
  1697. $pattern = '/\.(\d{1,4})$/';
  1698. $result = preg_match($pattern, $vlan_oid, $matches);
  1699. if (!empty($result)) {
  1700. $vlan_id = preg_replace('/^\./', '', $matches[0]);
  1701. if ($vlan_id > 1000 and $vlan_id < 1009) { continue; }
  1702. $fdb_vlan_table = get_mac_port_table($ip, $port_index, $community . '@' . $vlan_id, $version, $mac_table_oid2,$ifindex_map);
  1703. if (! isset($fdb_vlan_table) or ! $fdb_vlan_table or count($fdb_vlan_table) == 0) {
  1704. $fdb_vlan_table = get_mac_port_table($ip, $port_index, $community, $version, $mac_table_oid,$ifindex_map);
  1705. }
  1706. foreach ($fdb_vlan_table as $mac => $port) {
  1707. if (! isset($mac)) { continue; }
  1708. $fdb_port_table[$mac] = $port;
  1709. }
  1710. }
  1711. }
  1712. }
  1713. return $fdb_port_table;
  1714. }
  1715. function get_mac_table($ip, $community, $version, $oid, $index_map)
  1716. {
  1717. if (! isset($ip)) {
  1718. return;
  1719. }
  1720. if (! isset($oid)) {
  1721. return;
  1722. }
  1723. if (! isset($community)) {
  1724. $community = 'public';
  1725. }
  1726. if (! isset($version)) {
  1727. $version = '2';
  1728. }
  1729. $mac_table = walk_snmp($ip, $community, $version, $oid);
  1730. if (isset($mac_table) and count($mac_table) > 0) {
  1731. foreach ($mac_table as $key => $value) {
  1732. $key = trim($key);
  1733. $value = intval(trim(str_replace('INTEGER:', '', $value)));
  1734. $value = $index_map[$value];
  1735. $pattern = '/\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})$/';
  1736. $result = preg_match($pattern, $key, $matches);
  1737. if (!empty($result)) {
  1738. $mac_key = preg_replace('/^\./', '', $matches[0]);
  1739. $fdb_port_table[$mac_key] = $value;
  1740. }
  1741. }
  1742. }
  1743. return $fdb_port_table;
  1744. }
  1745. function get_fdb_table($ip, $community, $version)
  1746. {
  1747. global $mac_table_str_oid;
  1748. global $mac_table_str_oid2;
  1749. global $mac_table_oid;
  1750. global $mac_table_oid2;
  1751. if (! isset($ip)) {
  1752. return;
  1753. }
  1754. if (! isset($community)) {
  1755. $community = 'public';
  1756. }
  1757. if (! isset($version)) {
  1758. $version = '2';
  1759. }
  1760. $ifindex_map = get_ifmib_index_table($ip,$community, $version);
  1761. $fdb1_port_table = get_mac_table($ip, $community, $version, $mac_table_oid2, $ifindex_map);
  1762. $fdb2_port_table = get_mac_table($ip, $community, $version, $mac_table_oid, $ifindex_map);
  1763. if (!empty($fdb1_port_table)) { $fdb_port_table = $fdb1_port_table; }
  1764. if (!empty($fdb2_port_table)) {
  1765. if (empty($fdb_port_table)) {
  1766. $fdb_port_table = $fdb2_port_table;
  1767. } else {
  1768. foreach ($fdb2_port_table as $mac => $port) {
  1769. if (empty($fdb_port_table[$mac])) { $fdb_port_table[$mac]=$port; }
  1770. }
  1771. }
  1772. }
  1773. // maybe cisco?!
  1774. if (! isset($fdb_port_table) or ! $fdb_port_table or count($fdb_port_table) == 0) {
  1775. global $cisco_vlan_oid;
  1776. $vlan_table = walk_snmp($ip, $community, $version, $cisco_vlan_oid);
  1777. if (! $vlan_table) {
  1778. return;
  1779. }
  1780. foreach ($vlan_table as $vlan_oid => $value) {
  1781. if (! $vlan_oid) { continue; }
  1782. $pattern = '/\.(\d{1,4})$/';
  1783. $result = preg_match($pattern, $vlan_oid, $matches);
  1784. if (!empty($result)) {
  1785. $vlan_id = preg_replace('/^\./', '', $matches[0]);
  1786. if ($vlan_id > 1000 and $vlan_id < 1009) { continue; }
  1787. $fdb_vlan_table = get_mac_table($ip, $community . '@' . $vlan_id, $version, $mac_table_oid2);
  1788. if (! isset($fdb_vlan_table) or ! $fdb_vlan_table or count($fdb_vlan_table) == 0) {
  1789. $fdb_vlan_table = get_mac_table($ip, $community, $version, $mac_table_oid);
  1790. }
  1791. foreach ($fdb_vlan_table as $mac => $port) {
  1792. if (! isset($mac)) { continue; }
  1793. $fdb_port_table[$mac] = $port;
  1794. }
  1795. }
  1796. }
  1797. }
  1798. return $fdb_port_table;
  1799. }
  1800. function check_snmp_access($ip, $community, $version)
  1801. {
  1802. if (! isset($ip)) {
  1803. return;
  1804. }
  1805. if (! isset($community)) {
  1806. $community = 'public';
  1807. }
  1808. if (! isset($version)) {
  1809. $version = '2';
  1810. }
  1811. $result = get_snmp($ip, $community, $version, '.1.3.6.1.2.1.1.1.0');
  1812. if (!isset($result)) { return; }
  1813. return 1;
  1814. }
  1815. function get_port_state($port, $ip, $community, $version)
  1816. {
  1817. if (! isset($port)) {
  1818. return;
  1819. }
  1820. if (! isset($ip)) {
  1821. return;
  1822. }
  1823. if (! isset($community)) {
  1824. $community = 'public';
  1825. }
  1826. if (! isset($version)) {
  1827. $version = '2';
  1828. }
  1829. global $port_status_oid;
  1830. $port_oid = $port_status_oid . $port;
  1831. $port_state = get_snmp($ip, $community, $version, $port_oid);
  1832. return $port_state;
  1833. }
  1834. function get_last_digit($oid)
  1835. {
  1836. if (! isset($oid)) {
  1837. return;
  1838. }
  1839. $pattern = '/\.(\d{1,})$/';
  1840. preg_match($pattern, $oid, $matches);
  1841. return $matches[1];
  1842. }
  1843. function get_cisco_sensors($ip, $community, $version, $mkey)
  1844. {
  1845. $index = get_last_digit($mkey);
  1846. global $cisco_sfp_sensors;
  1847. global $cisco_sfp_precision;
  1848. $result = parse_snmp_value(get_snmp($ip, $community, $version, $cisco_sfp_sensors . "." . $index));
  1849. $prec = parse_snmp_value(get_snmp($ip, $community, $version, $cisco_sfp_precision . "." . $index));
  1850. if (! isset($prec)) { $prec = 1; }
  1851. $result = round(trim($result) / (10 * $prec), 2);
  1852. return $result;
  1853. }
  1854. function get_snmp_ifname1($ip, $community, $version, $port)
  1855. {
  1856. global $ifmib_ifname;
  1857. $port_name = parse_snmp_value(get_snmp($ip, $community, $version, $ifmib_ifname . "." . $port));
  1858. return $port_name;
  1859. }
  1860. function get_snmp_ifname2($ip, $community, $version, $port)
  1861. {
  1862. global $ifmib_ifdescr;
  1863. $port_name = parse_snmp_value(get_snmp($ip, $community, $version, $ifmib_ifdescr . "." . $port));
  1864. return $port_name;
  1865. }
  1866. function get_snmp_interfaces($ip, $community, $version)
  1867. {
  1868. global $ifmib_ifname;
  1869. global $ifmib_ifdescr;
  1870. $result = walk_snmp($ip, $community, $version, $ifmib_ifname);
  1871. if (empty($result)) { $result = walk_snmp($ip, $community, $version, $ifmib_ifdescr); }
  1872. return $result;
  1873. }
  1874. function walk_snmp($ip, $community, $version, $oid)
  1875. {
  1876. if ($version == 2) {
  1877. $result = snmp2_real_walk($ip, $community, $oid);
  1878. }
  1879. if ($version == 1) {
  1880. $result = snmprealwalk($ip, $community, $oid);
  1881. }
  1882. return $result;
  1883. }
  1884. function get_snmp_module_id($modules_oids, $port_name)
  1885. {
  1886. $port_name = preg_quote(trim($port_name), '/');
  1887. foreach ($modules_oids as $key => $value) {
  1888. $pattern = '/' . $port_name . '/i';
  1889. preg_match($pattern, $value, $matches);
  1890. if (isset($matches[0])) {
  1891. $module_id = get_last_digit($key);
  1892. return $module_id;
  1893. }
  1894. }
  1895. return;
  1896. }
  1897. function get_sfp_status($vendor_id, $port, $ip, $community, $version, $modules_oids)
  1898. {
  1899. if (! isset($vendor_id)) {
  1900. return;
  1901. }
  1902. if (! isset($port)) {
  1903. return;
  1904. }
  1905. if (! isset($ip)) {
  1906. return;
  1907. }
  1908. if (! isset($community)) {
  1909. $community = 'public';
  1910. }
  1911. if (! isset($version)) {
  1912. $version = '2';
  1913. }
  1914. // if (!is_up($ip)) { return; }
  1915. global $ifmib_ifname;
  1916. global $ifmib_ifdescr;
  1917. $status = '';
  1918. // eltex
  1919. if ($vendor_id == 2) {
  1920. global $eltex_sfp_status;
  1921. global $eltex_sfp_vendor;
  1922. global $eltex_sfp_sn;
  1923. global $eltex_sfp_freq;
  1924. global $eltex_sfp_length;
  1925. $sfp_vendor = parse_snmp_value(get_snmp($ip, $community, $version, $eltex_sfp_vendor . "." . $port));
  1926. if (isset($sfp_vendor)) {
  1927. $sfp_status_temp = $eltex_sfp_status . "." . $port . ".5";
  1928. $sfp_status_volt = $eltex_sfp_status . "." . $port . ".6";
  1929. $sfp_status_circut = $eltex_sfp_status . "." . $port . ".7";
  1930. $sfp_status_tx = $eltex_sfp_status . "." . $port . ".8";
  1931. $sfp_status_rx = $eltex_sfp_status . "." . $port . ".9";
  1932. $temp = parse_snmp_value(get_snmp($ip, $community, $version, $sfp_status_temp));
  1933. $volt = parse_snmp_value(get_snmp($ip, $community, $version, $sfp_status_volt));
  1934. $circut = parse_snmp_value(get_snmp($ip, $community, $version, $sfp_status_circut));
  1935. $tx = parse_snmp_value(get_snmp($ip, $community, $version, $sfp_status_tx));
  1936. $rx = parse_snmp_value(get_snmp($ip, $community, $version, $sfp_status_rx));
  1937. $sfp_sn = parse_snmp_value(get_snmp($ip, $community, $version, $eltex_sfp_sn . "." . $port));
  1938. $sfp_freq = parse_snmp_value(get_snmp($ip, $community, $version, $eltex_sfp_freq . "." . $port));
  1939. if (! isset($sfp_freq) or $sfp_freq == 65535) { $sfp_freq = 'unspecified'; }
  1940. $sfp_length = parse_snmp_value(get_snmp($ip, $community, $version, $eltex_sfp_length . "." . $port));
  1941. $status = 'Vendor: ' . $sfp_vendor . ' Serial: ' . $sfp_sn . ' Laser: ' . $sfp_freq . ' Distance: ' . $sfp_length . '<br>';
  1942. if (isset($sfp_status_temp) and $temp > 0.1) { $status .= 'Temp: ' . $temp . " C"; }
  1943. if (isset($sfp_status_volt) and $volt > 0.1) { $status .= ' Volt: ' . round($volt / 1000000, 2) . ' V'; }
  1944. if (isset($sfp_status_circut) and $circut > 0.1) { $status .= ' Circut: ' . round($circut / 1000, 2) . ' mA'; }
  1945. if (isset($sfp_status_tx) and $tx > 0.1) { $status .= ' Tx: ' . round($tx / 1000, 2) . ' dBm'; }
  1946. if (isset($sfp_status_rx) and $rx > 0.1) { $status .= ' Rx: ' . round($rx / 1000, 2) . ' dBm'; }
  1947. $status .= '<br>';
  1948. return $status;
  1949. }
  1950. return;
  1951. }
  1952. // cisco
  1953. if ($vendor_id == 16) {
  1954. global $cisco_descr;
  1955. global $cisco_modules;
  1956. // get interface names
  1957. $port_name = parse_snmp_value(get_snmp($ip, $community, $version, $ifmib_ifname . "." . $port));
  1958. if (empty($port_name)) {
  1959. $port_name = parse_snmp_value(get_snmp($ip, $community, $version, $ifmib_ifdescr . "." . $port));
  1960. }
  1961. // search module indexes
  1962. $port_name = preg_quote(trim($port_name), '/');
  1963. foreach ($modules_oids as $key => $value) {
  1964. $pattern = '/(' . $port_name . ' Module Temperature Sensor)/i';
  1965. preg_match($pattern, $value, $matches);
  1966. if (isset($matches[0])) {
  1967. $temp = get_cisco_sensors($ip, $community, $version, $key);
  1968. continue;
  1969. }
  1970. $pattern = '/(' . $port_name . ' Supply Voltage Sensor)/i';
  1971. preg_match($pattern, $value, $matches);
  1972. if (isset($matches[0])) {
  1973. $volt = get_cisco_sensors($ip, $community, $version, $key);
  1974. continue;
  1975. }
  1976. $pattern = '/(' . $port_name . ' Bias Current Sensor)/i';
  1977. preg_match($pattern, $value, $matches);
  1978. if (isset($matches[0])) {
  1979. $circut = get_cisco_sensors($ip, $community, $version, $key);
  1980. continue;
  1981. }
  1982. $pattern = '/(' . $port_name . ' Transmit Power Sensor)/i';
  1983. preg_match($pattern, $value, $matches);
  1984. if (isset($matches[0])) {
  1985. $tx = get_cisco_sensors($ip, $community, $version, $key);
  1986. continue;
  1987. }
  1988. $pattern = '/(' . $port_name . ' Receive Power Sensor)/i';
  1989. preg_match($pattern, $value, $matches);
  1990. if (isset($matches[0])) {
  1991. $rx = get_cisco_sensors($ip, $community, $version, $key);
  1992. continue;
  1993. }
  1994. }
  1995. if (isset($temp) and $temp > 0) {
  1996. $status .= 'Temp: ' . $temp . " C";
  1997. }
  1998. if (isset($volt) and $volt > 0) {
  1999. $status .= ' Volt: ' . $volt . ' V';
  2000. }
  2001. if (isset($circut) and $circut > 0) {
  2002. $status .= ' Circut: ' . $circut . ' mA';
  2003. }
  2004. if (isset($tx) and abs($tx)>0.1) {
  2005. $status .= ' Tx: ' . $tx . ' dBm';
  2006. }
  2007. if (isset($rx) and abs($rx)>0.1) {
  2008. $status .= ' Rx: ' . $rx . ' dBm';
  2009. }
  2010. if (isset($status)) {
  2011. $status = preg_replace('/"/', '', $status);
  2012. $status .= '<br>';
  2013. }
  2014. return $status;
  2015. }
  2016. // huawei
  2017. if ($vendor_id == 3) {
  2018. global $huawei_sfp_vendor;
  2019. global $huawei_sfp_speed;
  2020. global $huawei_sfp_db;
  2021. global $huawei_sfp_volt;
  2022. global $huawei_sfp_optrx;
  2023. global $huawei_sfp_opttx;
  2024. global $huawei_sfp_rx;
  2025. global $huawei_sfp_tx;
  2026. global $huawei_sfp_biascurrent;
  2027. // get interface names
  2028. $port_name = parse_snmp_value(get_snmp($ip, $community, $version, $ifmib_ifname . "." . $port));
  2029. if (empty($port_name)) {
  2030. $port_name = parse_snmp_value(get_snmp($ip, $community, $version, $ifmib_ifdescr . "." . $port));
  2031. }
  2032. // search module indexes
  2033. $port_name = preg_quote(trim($port_name), '/');
  2034. foreach ($modules_oids as $key => $value) {
  2035. $pattern = '/' . $port_name . '/i';
  2036. preg_match($pattern, $value, $matches);
  2037. if (isset($matches[0])) {
  2038. $module_id = get_last_digit($key);
  2039. unset($result);
  2040. $result = parse_snmp_value(get_snmp($ip, $community, $version, $huawei_sfp_vendor . "." . $module_id));
  2041. if (isset($result)) { $sfp_vendor = $result; }
  2042. unset($result);
  2043. $result = parse_snmp_value(get_snmp($ip, $community, $version, $huawei_sfp_speed . "." . $module_id));
  2044. if (isset($result)) {
  2045. list ($sfp_speed, $spf_lenght, $sfp_type) = explode('-', $result);
  2046. if ($sfp_type == 0) { $sfp_type = 'MultiMode'; }
  2047. if ($sfp_type == 1) { $sfp_type = 'SingleMode'; }
  2048. }
  2049. $volt = parse_snmp_value(get_snmp($ip, $community, $version, $huawei_sfp_volt . "." . $module_id));
  2050. $circut = parse_snmp_value(get_snmp($ip, $community, $version, $huawei_sfp_biascurrent . "." . $module_id));
  2051. $tx = parse_snmp_value(get_snmp($ip, $community, $version, $huawei_sfp_opttx . "." . $module_id));
  2052. $rx = parse_snmp_value(get_snmp($ip, $community, $version, $huawei_sfp_optrx . "." . $module_id));
  2053. if (!isset($tx)) { $tx = parse_snmp_value(get_snmp($ip, $community, $version, $huawei_sfp_tx . "." . $module_id)); }
  2054. if (!isset($rx)) { $rx = parse_snmp_value(get_snmp($ip, $community, $version, $huawei_sfp_rx . "." . $module_id)); }
  2055. if (isset($sfp_vendor)) { $status .= ' Name:' . $sfp_vendor.'<br>'; }
  2056. // if (isset($sfp_speed)) { $status .= ' ' . $sfp_speed; }
  2057. // if (isset($spf_lenght)) { $status .= ' ' . $spf_lenght; }
  2058. if ($volt > 0) { $status .= ' Volt: ' . round($volt / 1000, 2) . ' V'; }
  2059. if (isset($circut)) { $status .= ' Circut: ' . $circut . ' mA <br>'; }
  2060. if (isset($tx)) {
  2061. $tx = preg_replace('/"/', '', $tx);
  2062. list($tx_dbm,$pattern) = explode('.', $tx);
  2063. $tx_dbm=round(trim($tx_dbm) / 100,2);
  2064. if (abs($tx_dbm)>0.1) { $status .= ' Tx: '.$tx_dbm.' dBm'; }
  2065. }
  2066. if (isset($rx)) {
  2067. $rx = preg_replace('/"/', '', $rx);
  2068. list($rx_dbm,$pattern) = explode('.', $rx);
  2069. $rx_dbm=round(trim($rx_dbm) / 100,2);
  2070. if (abs($rx_dbm)>0.1) { $status .= ' Rx: '.$rx_dbm.' dBm'; }
  2071. }
  2072. break;
  2073. }
  2074. }
  2075. if (isset($status)) {
  2076. $status = preg_replace('/"/', '', $status);
  2077. $status .= '<br>';
  2078. }
  2079. return $status;
  2080. }
  2081. return;
  2082. }
  2083. function get_port_vlan($port, $port_index, $ip, $community, $version, $fdb_by_snmp)
  2084. {
  2085. if (! isset($port)) {
  2086. return;
  2087. }
  2088. if (! isset($ip)) {
  2089. return;
  2090. }
  2091. if (! isset($community)) {
  2092. $community = 'public';
  2093. }
  2094. if (! isset($version)) {
  2095. $version = '2';
  2096. }
  2097. // if (!is_up($ip)) { return; }
  2098. if ($fdb_by_snmp == 1) { $port = $port_index; }
  2099. global $port_vlan_oid;
  2100. $port_oid = $port_vlan_oid . $port;
  2101. $port_vlan = get_snmp($ip, $community, $version, $port_oid);
  2102. $port_vlan = preg_replace('/.*\:/','',$port_vlan);
  2103. $port_vlan = intval(trim($port_vlan));
  2104. return $port_vlan;
  2105. }
  2106. function get_port_poe_state($vendor_id, $port, $ip, $community, $version)
  2107. {
  2108. // port = snmp_index!!!!
  2109. if (! isset($port)) {
  2110. return;
  2111. }
  2112. if (! isset($ip)) {
  2113. return;
  2114. }
  2115. if (! isset($community)) {
  2116. $community = 'public';
  2117. }
  2118. if (! isset($version)) {
  2119. $version = '2';
  2120. }
  2121. // if (!is_up($ip)) { return; }
  2122. // default poe oid
  2123. global $pethPsePortAdminEnable;
  2124. $poe_status = $pethPsePortAdminEnable . "." . $port;
  2125. if ($vendor_id == 3) {
  2126. global $huawei_poe_oid;
  2127. $poe_status = $huawei_poe_oid . "." . $port;
  2128. }
  2129. if ($vendor_id == 8) {
  2130. global $allied_poe_oid;
  2131. $poe_status = $allied_poe_oid . "." . $port;
  2132. }
  2133. if ($vendor_id == 15) {
  2134. global $hp_poe_oid;
  2135. $poe_status = $hp_poe_oid . "." . $port;
  2136. }
  2137. if ($vendor_id == 9) {
  2138. global $mikrotik_poe_oid;
  2139. $poe_status = $mikrotik_poe_oid . "." . $port;
  2140. }
  2141. if ($vendor_id == 10) {
  2142. global $netgear_poe_oid;
  2143. $poe_status = $netgear_poe_oid . "." . $port;
  2144. }
  2145. $result = '';
  2146. $c_state = get_snmp($ip, $community, $version, $poe_status);
  2147. if (!empty($c_state)) {
  2148. $p_state = parse_snmp_value($c_state);
  2149. if ($vendor_id == 9) {
  2150. if ($p_state == 1) { return 2; }
  2151. if ($p_state > 1) { return 1; }
  2152. }
  2153. return $p_state;
  2154. }
  2155. return;
  2156. }
  2157. function set_port_poe_state($vendor_id, $port, $ip, $community, $version, $state)
  2158. {
  2159. // port -> snmp_index!!!
  2160. if (! isset($port)) {
  2161. return;
  2162. }
  2163. if (! isset($ip)) {
  2164. return;
  2165. }
  2166. if (! isset($community)) {
  2167. $community = 'public';
  2168. }
  2169. if (! isset($version)) {
  2170. $version = '2';
  2171. }
  2172. // if (!is_up($ip)) { return; }
  2173. // default poe oid
  2174. global $pethPsePortAdminEnable;
  2175. $poe_status = $pethPsePortAdminEnable . "." . $port;
  2176. if ($vendor_id == 3) {
  2177. global $huawei_poe_oid;
  2178. $poe_status = $huawei_poe_oid . "." . $port;
  2179. }
  2180. if ($vendor_id == 8) {
  2181. global $allied_poe_oid;
  2182. $poe_status = $allied_poe_oid . "." . $port;
  2183. }
  2184. if ($vendor_id == 15) {
  2185. global $hp_poe_oid;
  2186. $poe_status = $hp_poe_oid . "." . $port;
  2187. }
  2188. if ($vendor_id == 10) {
  2189. global $netgear_poe_oid;
  2190. $poe_status = $netgear_poe_oid . "." . $port;
  2191. }
  2192. if ($state) {
  2193. // enable port
  2194. $c_state = set_snmp($ip, $community, $version, $poe_status, 'i', 1);
  2195. return $c_state;
  2196. } else {
  2197. // disable port
  2198. $c_state = set_snmp($ip, $community, $version, $poe_status, 'i', 2);
  2199. return $c_state;
  2200. }
  2201. }
  2202. function get_port_poe_detail($vendor_id, $port, $ip, $community, $version)
  2203. {
  2204. // port = snmp_index!!!!
  2205. if (! isset($port)) {
  2206. return;
  2207. }
  2208. if (! isset($ip)) {
  2209. return;
  2210. }
  2211. if (! isset($community)) {
  2212. $community = 'public';
  2213. }
  2214. if (! isset($version)) {
  2215. $version = '2';
  2216. }
  2217. // if (!is_up($ip)) { return; }
  2218. $result = '';
  2219. $poe_class = '.1.3.6.1.2.1.105.1.1.1.10.1.' . $port;
  2220. // eltex
  2221. if ($vendor_id == 2) {
  2222. $poe_power = '.1.3.6.1.4.1.89.108.1.1.5.1.' . $port;
  2223. $poe_current = '.1.3.6.1.4.1.89.108.1.1.4.1.' . $port;
  2224. $poe_volt = '.1.3.6.1.4.1.89.108.1.1.3.1.' . $port;
  2225. }
  2226. // huawei
  2227. if ($vendor_id == 3) {
  2228. $poe_power = '.1.3.6.1.4.1.2011.5.25.195.3.1.10.' . $port;
  2229. $poe_current = '.1.3.6.1.4.1.4526.11.15.1.1.1.3.1.' . $port;
  2230. $poe_volt = '.1.3.6.1.4.1.2011.5.25.195.3.1.14.' . $port;
  2231. }
  2232. // AT
  2233. if ($vendor_id == 8) {
  2234. $poe_power = '.1.3.6.1.4.1.89.108.1.1.5.1.' . $port;
  2235. $poe_current = '.1.3.6.1.4.1.89.108.1.1.4.1.' . $port;
  2236. $poe_volt = '.1.3.6.1.4.1.89.108.1.1.3.1.' . $port;
  2237. }
  2238. // mikrotik
  2239. if ($vendor_id == 9) {
  2240. global $mikrotik_poe_volt;
  2241. global $mikrotik_poe_current;
  2242. global $mikrotik_poe_usage;
  2243. $poe_power = $mikrotik_poe_usage . '.' . $port;
  2244. $poe_current = $mikrotik_poe_current . '.' . $port;
  2245. $poe_volt = $mikrotik_poe_volt . '.' . $port;
  2246. }
  2247. // netgear
  2248. if ($vendor_id == 10) {
  2249. $poe_power = '.1.3.6.1.4.1.4526.11.15.1.1.1.2.1.' . $port;
  2250. $poe_current = '.1.3.6.1.4.1.4526.11.15.1.1.1.3.1.' . $port;
  2251. $poe_volt = '.1.3.6.1.4.1.4526.11.15.1.1.1.4.1.' . $port;
  2252. }
  2253. // HP
  2254. if ($vendor_id == 15) {
  2255. $poe_power = '.1.3.6.1.4.1.25506.2.14.1.1.4.1.' . $port;
  2256. $poe_volt = '.1.3.6.1.4.1.25506.2.14.1.1.3.1.' . $port;
  2257. }
  2258. if (isset($poe_power)) {
  2259. $c_power = get_snmp($ip, $community, $version, $poe_power);
  2260. if (isset($c_power)) {
  2261. $p_power = parse_snmp_value($c_power);
  2262. if ($vendor_id == 9) {
  2263. $p_power = round($p_power / 10, 2);
  2264. } else {
  2265. $p_power = round($p_power / 1000, 2);
  2266. }
  2267. if ($p_power > 0) {
  2268. $result .= ' P: ' . $p_power . ' W';
  2269. }
  2270. }
  2271. }
  2272. if (isset($poe_current)) {
  2273. $c_current = get_snmp($ip, $community, $version, $poe_current);
  2274. if (isset($c_current)) {
  2275. $p_current = parse_snmp_value($c_current);
  2276. if ($p_current > 0) {
  2277. $result .= ' C: ' . $p_current . ' mA';
  2278. }
  2279. }
  2280. }
  2281. if (isset($poe_volt)) {
  2282. $c_volt = get_snmp($ip, $community, $version, $poe_volt);
  2283. if (isset($c_volt)) {
  2284. $p_volt = parse_snmp_value($c_volt);
  2285. if ($vendor_id == 2 or $vendor_id == 8) {
  2286. $p_volt = round($p_volt / 1000, 2);
  2287. }
  2288. if ($vendor_id == 9) {
  2289. $p_volt = round($p_volt / 10, 2);
  2290. }
  2291. if ($vendor_id == 15) {
  2292. $p_volt = round($p_volt / 100, 2);
  2293. }
  2294. if ($p_volt > 0 and $p_power > 0) {
  2295. $result .= ' V: ' . $p_volt . " V";
  2296. }
  2297. }
  2298. }
  2299. if (isset($poe_class)) {
  2300. $c_class = get_snmp($ip, $community, $version, $poe_class);
  2301. if (isset($c_class)) {
  2302. $p_class = parse_snmp_value($c_class);
  2303. if ($p_class > 0 and $p_power > 0) {
  2304. $result .= ' Class: ' . ($p_class - 1);
  2305. }
  2306. }
  2307. }
  2308. return $result;
  2309. }
  2310. function get_snmp($ip, $community, $version, $oid)
  2311. {
  2312. if ($version == 2) {
  2313. $result = snmp2_get($ip, $community, $oid);
  2314. }
  2315. if ($version == 1) {
  2316. $result = snmpget($ip, $community, $oid);
  2317. }
  2318. return $result;
  2319. }
  2320. function set_snmp($ip, $community, $version, $oid, $field, $value)
  2321. {
  2322. if ($version == 2) {
  2323. $result = snmp2_set($ip, $community, $oid, $field, $value);
  2324. }
  2325. if ($version == 1) {
  2326. $result = snmpset($ip, $community, $oid, $field, $value);
  2327. }
  2328. return $result;
  2329. }
  2330. function set_port_state($vendor_id, $port, $ip, $community, $version, $state)
  2331. {
  2332. // port -> snmp_index!!!
  2333. if (! isset($port)) {
  2334. return;
  2335. }
  2336. if (! isset($ip)) {
  2337. return;
  2338. }
  2339. if (! isset($community)) {
  2340. $community = 'public';
  2341. }
  2342. if (! isset($version)) {
  2343. $version = '2';
  2344. }
  2345. global $port_admin_status_oid;
  2346. $port_status = $port_admin_status_oid . $port;
  2347. if ($state == 1) {
  2348. // enable port
  2349. $c_state = set_snmp($ip, $community, $version, $port_status, 'i', 1);
  2350. return $c_state;
  2351. } else {
  2352. // disable port
  2353. $c_state = set_snmp($ip, $community, $version, $port_status, 'i', 2);
  2354. return $c_state;
  2355. }
  2356. }
  2357. function set_port_for_group($db, $group_id, $place_id, $state)
  2358. {
  2359. $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;
  2360. $auth_list = mysqli_query($db, $authSQL);
  2361. LOG_INFO($db, 'Mass port state change started!');
  2362. // get auth list for group
  2363. while (list ($a_id, $a_name, $a_ip) = mysqli_fetch_array($auth_list)) {
  2364. // get device and port for auth
  2365. if ($place_id == 0) {
  2366. $place_filter = '';
  2367. } else {
  2368. $place_filter = 'D.building_id=' . $place_id . ' and ';
  2369. }
  2370. $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';
  2371. $dev_info = mysqli_query($db, $devSQL);
  2372. 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);
  2373. if (! isset($d_id)) {
  2374. continue;
  2375. }
  2376. if ($state == 1) {
  2377. $mode = 'enable';
  2378. run_sql($db, "Update User_auth set nagios_handler='restart-port' WHERE id=$a_id and nagios_handler='manual-mode'");
  2379. } else {
  2380. $mode = 'disable';
  2381. run_sql($db, "Update User_auth set nagios_handler='manual-mode' WHERE id=$a_id and nagios_handler='restart-port'");
  2382. }
  2383. LOG_INFO($db, "At device $d_name [$d_ip] $mode port $d_port for auth_id: $a_id ($a_ip [$a_name])");
  2384. set_port_state($d_vendor_id, $d_snmp_index, $d_ip, $d_community, $d_snmp, $state);
  2385. set_port_poe_state($d_vendor_id, $d_snmp_index, $d_ip, $d_community, $d_snmp, $state);
  2386. }
  2387. LOG_INFO($db, 'Mass port state change stopped.');
  2388. }
  2389. function get_vendor($db,$mac)
  2390. {
  2391. $mac = mac_dotted($mac);
  2392. $vendor = get_record_sql($db,'SELECT companyName,companyAddress FROM mac_vendors WHERE oui="'.substr($mac,0,8).'" or oui="'.substr($mac,0,11).'"');
  2393. $result='';
  2394. if (!empty($vendor)) {
  2395. $result = $vendor['companyName'];
  2396. if (!empty($vendor['companyAddress'])) { $result = $vendor['companyAddress']; }
  2397. }
  2398. return $result;
  2399. }
  2400. function get_port_state_detail($port, $ip, $community, $version)
  2401. {
  2402. if (! isset($port)) {
  2403. return;
  2404. }
  2405. if (! isset($ip)) {
  2406. return;
  2407. }
  2408. if (! isset($community)) {
  2409. $community = 'public';
  2410. }
  2411. if (! isset($version)) {
  2412. $version = '2';
  2413. }
  2414. // if (!is_up($ip)) { return; }
  2415. global $port_status_oid;
  2416. global $port_admin_status_oid;
  2417. global $port_speed_oid;
  2418. global $port_errors_oid;
  2419. $oper = $port_status_oid . $port;
  2420. $admin = $port_admin_status_oid . $port;
  2421. $speed = $port_speed_oid . $port;
  2422. $errors = $port_errors_oid . $port;
  2423. $result = '';
  2424. $c_state = get_snmp($ip, $community, $version, $oper);
  2425. $p_state = parse_snmp_value($c_state);
  2426. $c_admin = get_snmp($ip, $community, $version, $admin);
  2427. $p_admin = parse_snmp_value ($c_admin);
  2428. if ($p_state == 1) { $c_speed = get_snmp($ip, $community, $version, $speed); } else { $c_speed = 'INT:0'; }
  2429. $p_speed = parse_snmp_value($c_speed);
  2430. $c_errors = get_snmp($ip, $community, $version, $errors);
  2431. $p_errors = parse_snmp_value($c_errors);
  2432. $result = $p_state . ";" . $p_admin . ";" . $p_speed . ";" . $p_errors;
  2433. return $result;
  2434. }
  2435. function parse_snmp_value($value) {
  2436. if (empty($value)) { return NULL; }
  2437. list ($p_type, $p_value) = explode(':', $value);
  2438. $p_value = trim($p_value);
  2439. $p_value= preg_replace('/^\"/','',$p_value);
  2440. $p_value= preg_replace('/\"$/','',$p_value);
  2441. $p_value = trim($p_value);
  2442. return $p_value;
  2443. }
  2444. function dec_to_hex($mac)
  2445. {
  2446. if (! isset($mac)) {
  2447. return;
  2448. }
  2449. $mac_array = explode('.', $mac);
  2450. for ($i = 0; $i < count($mac_array); $i ++) {
  2451. $hex_i = dechex($mac_array[$i]);
  2452. if (strlen($hex_i) == 1) {
  2453. $hex_i = "0" . $hex_i;
  2454. }
  2455. $mac_array[$i] = $hex_i;
  2456. }
  2457. $hex_mac = implode(':', $mac_array);
  2458. return $hex_mac;
  2459. }
  2460. function mac_simplify($mac)
  2461. {
  2462. if (! isset($mac)) {
  2463. return;
  2464. }
  2465. $mac = strtolower(trim($mac));
  2466. $mac = preg_replace('/(\.|:|-)/', '', $mac);
  2467. return $mac;
  2468. }
  2469. function mac_dotted($mac)
  2470. {
  2471. if (! isset($mac)) {
  2472. return;
  2473. }
  2474. $mac = mac_simplify($mac);
  2475. $mac = preg_replace('/(\S{2})(\S{2})(\S{2})(\S{2})(\S{2})(\S{2})/', '$1:$2:$3:$4:$5:$6', $mac);
  2476. return $mac;
  2477. }
  2478. function unbind_ports($db, $device_id)
  2479. {
  2480. $target = mysqli_query($db, "SELECT U.target_port_id,U.id FROM device_ports U WHERE U.device_id=$device_id");
  2481. while (list ($target_id, $id) = mysqli_fetch_array($target)) {
  2482. run_sql($db, "UPDATE device_ports SET target_port_id=0 WHERE target_port_id=".$id);
  2483. run_sql($db, "UPDATE device_ports SET target_port_id=0 WHERE id=".$id);
  2484. }
  2485. }
  2486. function bind_ports($db, $port_id, $target_id)
  2487. {
  2488. $old_target = mysqli_query($db, "SELECT U.target_port_id FROM device_ports U WHERE U.id=$port_id");
  2489. list ($old_target_id) = mysqli_fetch_array($old_target);
  2490. // unbind current connection
  2491. $new['target_port_id'] = 0;
  2492. update_record($db, "device_ports", "id='$port_id'", $new);
  2493. if (isset($old_target_id)) {
  2494. update_record($db, "device_ports", "id='$old_target_id'", $new);
  2495. }
  2496. // new link
  2497. if (isset($target_id) and $target_id > 0) {
  2498. $new['target_port_id'] = $target_id;
  2499. update_record($db, "device_ports", "id='$port_id'", $new);
  2500. $new['target_port_id'] = $port_id;
  2501. update_record($db, "device_ports", "id='$target_id'", $new);
  2502. }
  2503. }
  2504. function expand_device_name($db, $name)
  2505. {
  2506. $device_id = get_device_id($db, $name);
  2507. $result = $name;
  2508. if (isset($device_id) and $device_id > 0) {
  2509. $result = '<a href=/admin/devices/editdevice.php?id=' . $device_id . '>' . $name . '</a>';
  2510. }
  2511. return $result;
  2512. }
  2513. function expand_mac($db,$msg)
  2514. {
  2515. if (! isset($msg)) { return; }
  2516. $mac = mac_dotted($msg);
  2517. $vendor_info = get_vendor($db,$mac);
  2518. $result = ' <p title="'.$vendor_info.'"><a href=/admin/logs/mac.php?mac='.$mac.'>'.$mac.'</a></p>';
  2519. return $result;
  2520. }
  2521. function expand_log_str($db, $msg)
  2522. {
  2523. if (! isset($msg)) {
  2524. return;
  2525. }
  2526. $auth_pattern = '/(auth_id:|auth|auth id:|auth id)\s+(\d+)\s+/i';
  2527. $auth_replace = '<a href=/admin/users/editauth.php?id=${2}>auth_id:${2}</a> ';
  2528. $result = preg_replace($auth_pattern, $auth_replace, $msg);
  2529. $user_pattern = '/(user_id:|user|user id:|user id)\s+(\d+)\s+/i';
  2530. $user_replace = '<a href=/admin/users/edituser.php?id=${2}>user_id:${2}</a>';
  2531. $result = preg_replace($user_pattern, $user_replace, $result);
  2532. $mac_pattern = '/\s+\[(\w{12})\]\s+/i';
  2533. preg_match($mac_pattern, $result, $matches);
  2534. if (isset($matches[1])) {
  2535. $mac = $matches[1];
  2536. $mac = mac_dotted($mac);
  2537. # $vendor_info = get_vendor($db,$mac);
  2538. # $mac_replace = ' <p title="'.$vendor_info.'"><a href=/admin/logs/mac.php?mac='.$mac.'>'.$mac.'</a></p>';
  2539. $mac_replace = ' <a href=/admin/logs/mac.php?mac='.$mac.'>'.$mac.'</a> ';
  2540. $result = preg_replace($mac_pattern, $mac_replace, $result);
  2541. }
  2542. $mac_pattern = '/\s+mac:\s+([\w\:]{17})$/i';
  2543. preg_match($mac_pattern, $result, $matches);
  2544. if (isset($matches[1])) {
  2545. $mac = $matches[1];
  2546. $mac = mac_dotted($mac);
  2547. # $vendor_info = get_vendor($db,$mac);
  2548. # $mac_replace = ' mac: <p title="'.$vendor_info.'"><a href=/admin/logs/mac.php?mac='.$mac.'>'.$mac.'</a></p>';
  2549. $mac_replace = ' mac: <a href=/admin/logs/mac.php?mac='.$mac.'>'.$mac.'</a> ';
  2550. $result = preg_replace($mac_pattern, $mac_replace, $result);
  2551. }
  2552. $device_pattern = '/at device\s+([\w\.\-]+)/i';
  2553. preg_match($device_pattern, $result, $matches);
  2554. if (isset($matches[1])) {
  2555. $device_name = $matches[1];
  2556. $device_id = get_device_id($db, $device_name);
  2557. if (isset($device_id) and $device_id > 0) {
  2558. $device_replace = 'at device <a href=/admin/devices/editdevice.php?id=' . $device_id . '>${1}</a>';
  2559. $result = preg_replace($device_pattern, $device_replace, $result);
  2560. }
  2561. }
  2562. $device_pattern = '/(device_id:|device id:|device id|device_id)\s+(\d+)\s+/i';
  2563. $device_replace = 'device_id: <a href=/admin/devices/editdevice.php?id=${2}>${2}</a> ';
  2564. $result = preg_replace($device_pattern, $device_replace, $result);
  2565. $ip_pattern = '/\s+ip\:\s+(\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})\s+/i';
  2566. preg_match($ip_pattern, $result, $matches);
  2567. if (isset($matches[1])) {
  2568. $ip = $matches[1];
  2569. $auth_id = get_auth_by_ip($db, $ip);
  2570. if (isset($auth_id) and $auth_id > 0) {
  2571. $auth_replace = ' ip: <a href=/admin/users/editauth.php?id=' . $auth_id . '>${1}</a> ';
  2572. $result = preg_replace($ip_pattern, $auth_replace, $result);
  2573. }
  2574. }
  2575. return $result;
  2576. }
  2577. function get_record_field($db, $table, $field, $filter)
  2578. {
  2579. if (! isset($table)) {
  2580. LOG_ERROR($db, "Search in unknown table! Skip command.");
  2581. return;
  2582. }
  2583. if (! isset($filter)) {
  2584. LOG_ERROR($db, "Search filter is empty! Skip command.");
  2585. return;
  2586. }
  2587. if (! isset($field)) {
  2588. LOG_ERROR($db, "Search field is empty! Skip command.");
  2589. return;
  2590. }
  2591. if (preg_match('/=$/', $filter)) {
  2592. LOG_ERROR($db, "Search record ($table) with illegal filter $filter! Skip command.");
  2593. return;
  2594. }
  2595. $old_sql = "SELECT $field FROM $table WHERE $filter LIMIT 1";
  2596. $old_record = mysqli_query($db, $old_sql) or LOG_ERROR($db, "Select SQL: $old_sql :".mysqli_error($db));
  2597. $old = mysqli_fetch_array($old_record, MYSQLI_ASSOC);
  2598. foreach ($old as $key => $value) {
  2599. if (! isset($value) or $value==='NULL') { $value = ''; }
  2600. $result[$key] = $value;
  2601. }
  2602. return $result[$field];
  2603. }
  2604. function get_record($db, $table, $filter)
  2605. {
  2606. if (! isset($table)) {
  2607. LOG_ERROR($db, "Search in unknown table! Skip command.");
  2608. return;
  2609. }
  2610. if (! isset($filter)) {
  2611. LOG_ERROR($db, "Search filter is empty! Skip command.");
  2612. return;
  2613. }
  2614. if (preg_match('/=$/', $filter)) {
  2615. LOG_ERROR($db, "Search record ($table) with illegal filter $filter! Skip command.");
  2616. return;
  2617. }
  2618. $old_sql = "SELECT * FROM $table WHERE $filter LIMIT 1";
  2619. $old_record = mysqli_query($db, $old_sql) or LOG_ERROR($db, "Select SQL: $old_sql :".mysqli_error($db));
  2620. $old = mysqli_fetch_array($old_record, MYSQLI_ASSOC);
  2621. $result = NULL;
  2622. if (!empty($old)) {
  2623. foreach ($old as $key => $value) {
  2624. if (!isset($value) or $value==='NULL') { $value = ''; }
  2625. if (!empty($key)) { $result[$key] = $value; }
  2626. }
  2627. }
  2628. return $result;
  2629. }
  2630. function get_records($db, $table, $filter)
  2631. {
  2632. if (! isset($table)) {
  2633. LOG_ERROR($db, "Search in unknown table! Skip command.");
  2634. return;
  2635. }
  2636. if (isset($filter) and preg_match('/=$/', $filter)) {
  2637. LOG_ERROR($db, "Search record ($table) with illegal filter $filter! Skip command.");
  2638. return;
  2639. }
  2640. $s_filter='';
  2641. if (isset($filter)) { $s_filter = 'WHERE '.$filter; }
  2642. $old_sql = "SELECT * FROM $table $s_filter";
  2643. $old_record = mysqli_query($db, $old_sql) or LOG_ERROR($db, "Select SQL: $old_sql :".mysqli_error($db));
  2644. $result = NULL;
  2645. $index = 0;
  2646. while ($old = mysqli_fetch_array($old_record, MYSQLI_ASSOC)) {
  2647. foreach ($old as $key => $value) {
  2648. if (! isset($value) or $value==='NULL') { $value = ''; }
  2649. $result[$index][$key] = $value;
  2650. }
  2651. $index++;
  2652. }
  2653. return $result;
  2654. }
  2655. function get_records_sql($db, $sql)
  2656. {
  2657. if (empty($sql)) {
  2658. LOG_ERROR($db, "Empty query! Skip command.");
  2659. return;
  2660. }
  2661. $record = mysqli_query($db, $sql) or LOG_ERROR($db, "Select SQL: $sql :".mysqli_error($db));
  2662. $index = 0;
  2663. $result = NULL;
  2664. while ($rec = mysqli_fetch_array($record, MYSQLI_ASSOC)) {
  2665. foreach ($rec as $key => $value) {
  2666. if (! isset($value) or $value==='NULL') { $value = ''; }
  2667. if (!empty($key)) { $result[$index][$key] = $value; }
  2668. }
  2669. $index++;
  2670. }
  2671. return $result;
  2672. }
  2673. function get_record_sql($db, $sql)
  2674. {
  2675. if (! isset($sql)) {
  2676. LOG_ERROR($db, "Empty query! Skip command.");
  2677. return;
  2678. }
  2679. $record = mysqli_query($db, $sql." LIMIT 1") or LOG_ERROR($db, "Select SQL: $sql LIMIT 1: ".mysqli_error($db));
  2680. $result = NULL;
  2681. $rec = mysqli_fetch_array($record, MYSQLI_ASSOC);
  2682. if (!empty($rec)) {
  2683. foreach ($rec as $key => $value) {
  2684. if (! isset($value) or $value==='NULL') { $value = ''; }
  2685. $result[$key] = $value;
  2686. }
  2687. }
  2688. return $result;
  2689. }
  2690. function is_auth_bind_changed ($db, $id, $ip,$mac) {
  2691. $old_sql = "SELECT ip,mac FROM User_auth WHERE id=$id";
  2692. $old_record = get_record_sql($db, $old_sql);
  2693. if (empty($old_record["ip"]) or empty($old_record["mac"])) { return 0; }
  2694. if ($old_record["ip"] !== $ip or $old_record["mac"] !== $mac) {
  2695. LOG_VERBOSE($db, "Changed ip or mac for auth record!");
  2696. return 1;
  2697. }
  2698. return 0;
  2699. }
  2700. function copy_auth($db, $id, $new_auth) {
  2701. $old_record = get_record_sql($db, "SELECT * FROM User_auth WHERE id=$id");
  2702. delete_record($db,"User_auth","id=".$id);
  2703. $new_auth["user_id"] = $old_record["user_id"];
  2704. $new_auth["changed"] = 1;
  2705. $changed_time = GetNowTimeString();
  2706. $new_auth["changed_time"]=$changed_time;
  2707. $new_id = insert_record($db,"User_auth",$new_auth);
  2708. LOG_VERBOSE($db, "Old record with id: $id deleted. Created new auth record for new ip+mac id: $new_id!");
  2709. return $new_id;
  2710. }
  2711. function update_record($db, $table, $filter, $newvalue)
  2712. {
  2713. if (isRO($db,$table)) {
  2714. LOG_ERROR($db, "User does not have write permission");
  2715. return;
  2716. }
  2717. if (! isset($table)) {
  2718. LOG_ERROR($db, "Change record for unknown table! Skip command.");
  2719. return;
  2720. }
  2721. if (! isset($filter)) {
  2722. LOG_ERROR($db, "Change record ($table) with empty filter! Skip command.");
  2723. return;
  2724. }
  2725. if (preg_match('/=$/', $filter)) {
  2726. LOG_ERROR($db, "Change record ($table) with illegal filter $filter! Skip command.");
  2727. return;
  2728. }
  2729. if (! isset($newvalue)) {
  2730. LOG_ERROR($db, "Change record ($table [ $filter ]) with empty data! Skip command.");
  2731. return;
  2732. }
  2733. $old_sql = "SELECT * FROM $table WHERE $filter";
  2734. $old_record = mysqli_query($db, $old_sql) or LOG_ERROR($db, "Select SQL: $old_sql :".mysqli_error($db));
  2735. $old = mysqli_fetch_array($old_record, MYSQLI_ASSOC);
  2736. $changed_log = '';
  2737. $run_sql = '';
  2738. $network_changed = 0;
  2739. $acl_fields = [
  2740. 'ip' => '1',
  2741. 'ip_int' => '1',
  2742. 'enabled'=>'1',
  2743. 'dhcp'=>'1',
  2744. 'filter_group_id'=>'1',
  2745. 'deleted'=>'1',
  2746. 'dhcp_acl'=>'1',
  2747. 'queue_id'=>'1',
  2748. 'mac'=>'1',
  2749. 'blocked'=>'1',
  2750. ];
  2751. foreach ($newvalue as $key => $value) {
  2752. if (! isset($value)) {
  2753. $value = '';
  2754. }
  2755. $value = trim($value);
  2756. if (strcmp($old[$key], $value) == 0) {
  2757. continue;
  2758. }
  2759. if ($table==="User_auth") {
  2760. if (!empty($acl_fields["$key"])) { $network_changed = 1; }
  2761. }
  2762. $changed_log = $changed_log . " $key => $value (old: $old[$key]),";
  2763. $run_sql = $run_sql . " `" . $key . "`='" . mysqli_real_escape_string($db, $value) . "',";
  2764. }
  2765. if ($run_sql == '') { return; }
  2766. if ($network_changed) { $run_sql = $run_sql . " `changed`='1',"; }
  2767. $changed_log = substr_replace($changed_log, "", - 1);
  2768. $run_sql = substr_replace($run_sql, "", - 1);
  2769. if ($table === 'User_auth') {
  2770. $changed_time = GetNowTimeString();
  2771. $run_sql = $run_sql . ", `changed_time`='".$changed_time."'";
  2772. }
  2773. $new_sql = "UPDATE $table SET $run_sql WHERE $filter";
  2774. LOG_DEBUG($db, "Run sql: $new_sql");
  2775. $sql_result = mysqli_query($db, $new_sql) or LOG_ERROR($db, "Select SQL: $new_sql :".mysqli_error($db));
  2776. if ($sql_result === false) { LOG_ERROR($db, "UPDATE Request: $new_sql :".mysqli_error($db)); return; }
  2777. LOG_VERBOSE($db, "Change table $table WHERE $filter set $changed_log");
  2778. }
  2779. function delete_record($db, $table, $filter)
  2780. {
  2781. if (isRO($db,$table)) {
  2782. LOG_ERROR($db, "User does not have write permission");
  2783. return;
  2784. }
  2785. if (! isset($table)) {
  2786. LOG_ERROR($db, "Delete FROM unknown table! Skip command.");
  2787. return;
  2788. }
  2789. if (! isset($filter)) {
  2790. LOG_ERROR($db, "Delete FROM table $table with empty filter! Skip command.");
  2791. return;
  2792. }
  2793. if (preg_match('/=$/', $filter)) {
  2794. LOG_ERROR($db, "Change record ($table) with illegal filter $filter! Skip command.");
  2795. return;
  2796. }
  2797. $old_sql = "SELECT * FROM $table WHERE $filter";
  2798. $old_record = mysqli_query($db, $old_sql) or LOG_ERROR($db, "Select SQL: $old_sql :".mysqli_error($db));
  2799. $old = mysqli_fetch_array($old_record, MYSQLI_ASSOC);
  2800. $changed_log = 'record: ';
  2801. foreach ($old as $key => $value) {
  2802. if (! isset($value)) { $value = ''; }
  2803. $changed_log = $changed_log . " $key => $value,";
  2804. }
  2805. //never delete user ip record
  2806. if ($table === 'User_auth') {
  2807. $changed_time = GetNowTimeString();
  2808. $new_sql = "UPDATE $table SET deleted=1, changed=1, `changed_time`='".$changed_time."' WHERE $filter";
  2809. LOG_DEBUG($db, "Run sql: $new_sql");
  2810. $sql_result = mysqli_query($db, $new_sql) or LOG_ERROR($db, "Select SQL: $new_sql :".mysqli_error($db));
  2811. if ($sql_result === false) { LOG_ERROR($db, "UPDATE Request (from delete): ".mysqli_error($db)); return; }
  2812. } else {
  2813. $new_sql = "DELETE FROM $table WHERE $filter";
  2814. LOG_DEBUG($db, "Run sql: $new_sql");
  2815. $sql_result = mysqli_query($db, $new_sql) or LOG_ERROR($db, "Select SQL: $new_sql :".mysqli_error($db));
  2816. if ($sql_result === false) { LOG_ERROR($db, "DELETE Request: $new_sql : ".mysqli_error($db)); return; }
  2817. }
  2818. LOG_VERBOSE($db, "Delete FROM table $table WHERE $filter $changed_log");
  2819. }
  2820. function insert_record($db, $table, $newvalue)
  2821. {
  2822. if (isRO($db,$table)) {
  2823. LOG_ERROR($db, "User does not have write permission");
  2824. return;
  2825. }
  2826. if (! isset($table)) {
  2827. LOG_ERROR($db, "Create record for unknown table! Skip command.");
  2828. return;
  2829. }
  2830. if (empty($newvalue)) {
  2831. LOG_ERROR($db, "Create record ($table) with empty data! Skip command.");
  2832. return;
  2833. }
  2834. $changed_log = '';
  2835. $field_list = '';
  2836. $value_list = '';
  2837. foreach ($newvalue as $key => $value) {
  2838. if (empty($value)) { $value = ''; }
  2839. $changed_log = $changed_log . " $key => $value,";
  2840. $field_list = $field_list . "`" . $key . "`,";
  2841. $value = trim($value);
  2842. $value_list = $value_list . "'" . mysqli_real_escape_string($db, $value) . "',";
  2843. }
  2844. if ($value_list == '') {
  2845. return;
  2846. }
  2847. $changed_log = substr_replace($changed_log, "", - 1);
  2848. $field_list = substr_replace($field_list, "", - 1);
  2849. $value_list = substr_replace($value_list, "", - 1);
  2850. $new_sql = "insert into $table(" . $field_list . ") values(" . $value_list . ")";
  2851. LOG_DEBUG($db, "Run sql: $new_sql");
  2852. $sql_result = mysqli_query($db, $new_sql) or LOG_ERROR($db, "Select SQL: $new_sql :".mysqli_error($db));
  2853. if ($sql_result === false) { LOG_ERROR($db, "INSERT Request:".mysqli_error($db)); return; }
  2854. $last_id = mysqli_insert_id($db);
  2855. LOG_VERBOSE($db, "Create record in table $table: $changed_log with id: $last_id");
  2856. if ($table === 'User_auth') { run_sql($db,"UPDATE User_auth SET changed=1 WHERE id=".$last_id); }
  2857. return $last_id;
  2858. }
  2859. function get_diff_rec($db, $table, $filter, $newvalue, $only_changed)
  2860. {
  2861. if (! isset($table)) {
  2862. return;
  2863. }
  2864. if (! isset($filter)) {
  2865. return;
  2866. }
  2867. if (! isset($newvalue)) {
  2868. return;
  2869. }
  2870. if (!isset($only_changed)) { $only_changed=0; }
  2871. $old_sql = "SELECT * FROM $table WHERE $filter";
  2872. $old_record = mysqli_query($db, $old_sql) or LOG_ERROR($db, "Select SQL: $old_sql :".mysqli_error($db));
  2873. $old = mysqli_fetch_array($old_record, MYSQLI_ASSOC);
  2874. $changed_log = "\r\n";
  2875. foreach ($newvalue as $key => $value) {
  2876. if (strcmp($old[$key], $value) !== 0) {
  2877. $changed_log = $changed_log . " $key => cur: $value old: $old[$key],\r\n";
  2878. }
  2879. }
  2880. $old_record = '';
  2881. if (!$only_changed) {
  2882. $old_record = "\r\n Не изменялось:\r\n";
  2883. foreach ($old as $key => $value) {
  2884. if (!$newvalue[$key]) { $old_record = $old_record . " $key = $value,\r\n"; }
  2885. }
  2886. $old_record = substr_replace($old_record, "", -3);
  2887. }
  2888. // print $changed_log;
  2889. return $changed_log.$old_record;
  2890. }
  2891. function get_cacti_graph($host_ip, $port_index)
  2892. {
  2893. global $cacti_dbname;
  2894. global $cacti_dbhost;
  2895. global $cacti_url;
  2896. global $dbuser;
  2897. global $dbpass;
  2898. if (! isset($cacti_url)) { return; }
  2899. $cacti_db_link = mysqli_connect($cacti_dbhost, $dbuser, $dbpass, $cacti_dbname);
  2900. if (! $cacti_db_link) {
  2901. echo "Ошибка: Невозможно установить соединение с MySQL with $cacti_dbhost [$cacti_dbname] for $dbuser." . PHP_EOL;
  2902. echo "Код ошибки errno: " . mysqli_connect_errno() . PHP_EOL;
  2903. echo "Текст ошибки error: " . mysqli_connect_error() . PHP_EOL;
  2904. return FALSE;
  2905. }
  2906. $host_sql = 'SELECT * FROM host WHERE hostname="' . $host_ip . '"';
  2907. $cacti_host = get_record_sql($cacti_db_link,$host_sql);
  2908. $host_id = $cacti_host["id"];
  2909. if (empty($host_id)) { return; }
  2910. $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';
  2911. $cacti_graph = get_record_sql($cacti_db_link,$graph_sql);
  2912. $graph_id = $cacti_graph["id"];
  2913. if (empty($graph_id)) { return; }
  2914. $result = $cacti_url . "/graph_image.php?local_graph_id=" . $graph_id;
  2915. return $result;
  2916. }
  2917. function print_select_item ($description,$value,$current) {
  2918. if ($value == $current) { print "<option value=$value selected>$description</option>"; } else { print "<option value=$value>$description</option>"; }
  2919. }
  2920. function print_select_simple ($description,$value) {
  2921. print "<option value=$value>$description</option>";
  2922. }
  2923. function print_select_item_ext ($description,$value,$current,$disabled) {
  2924. if ($value == $current) { print "<option value=$value selected>$description</option>"; }
  2925. else {
  2926. if (!$disabled) { print "<option value=$value>$description</option>"; } else { print "<option disabled value=$value>$description</option>"; }
  2927. }
  2928. }
  2929. function print_row_at_pages ($name,$value) {
  2930. print "<select name='".$name."'>\n";
  2931. print_select_item('Много',pow(10,10),$value);
  2932. print_select_item('25',25,$value);
  2933. print_select_item('50',50,$value);
  2934. print_select_item('100',100,$value);
  2935. print_select_item('200',200,$value);
  2936. print_select_item('500',500,$value);
  2937. print_select_item('1000',1000,$value);
  2938. print_select_item('2000',2000,$value);
  2939. print "</select>\n";
  2940. }
  2941. function print_navigation($url,$page,$displayed,$count_records,$total) {
  2942. if ($total<=1) { return; }
  2943. #две назад
  2944. print "<br><div align=left>";
  2945. if(($page-2)>0):
  2946. $pagetwoleft="<a class='first_page_link' href=".$url."?page=".($page-2).">".($page-2)."</a> ";
  2947. else:
  2948. $pagetwoleft=null;
  2949. endif;
  2950. #одна назад
  2951. if(($page-1)>0):
  2952. $pageoneleft="<a class='first_page_link' href=".$url."?page=".($page-1).">".($page-1)."</a> ";
  2953. $pagetemp=($page-1);
  2954. else:
  2955. $pageoneleft=null;
  2956. $pagetemp=null;
  2957. endif;
  2958. #две вперед
  2959. if(($page+2)<=$total):
  2960. $pagetworight=" <a class='first_page_link' href=".$url."?page=".($page+2).">".($page+2)."</a>";
  2961. else:
  2962. $pagetworight=null;
  2963. endif;
  2964. #одна вперед
  2965. if(($page+1)<=$total):
  2966. $pageoneright=" <a class='first_page_link' href=".$url."?page=".($page+1).">".($page+1)."</a>";
  2967. $pagetemp2=($page+1);
  2968. else:
  2969. $pageoneright=null;
  2970. $pagetemp2=null;
  2971. endif;
  2972. # в начало
  2973. if($page!=1 && $pagetemp!=1 && $pagetemp!=2):
  2974. $pagerevp="<a href=".$url."?page=1 class='first_page_link' title='В начало'><<</a> ";
  2975. else:
  2976. $pagerevp=null;
  2977. endif;
  2978. #в конец (последняя)
  2979. if($page!=$total && $pagetemp2!=($total-1) && $pagetemp2!=$total):
  2980. $nextp=" ... <a href=".$url."?page=".$total." class='first_page_link'>$total</a>";
  2981. else:
  2982. $nextp=null;
  2983. endif;
  2984. print "<br>".$pagerevp.$pagetwoleft.$pageoneleft.'<span class="num_page_not_link"><b>'.$page.'</b></span>'.$pageoneright.$pagetworight.$nextp;
  2985. print " | Total records: $count_records";
  2986. print "</div>";
  2987. }
  2988. function get_option($db, $option_id)
  2989. {
  2990. $option = get_record($db, "config", "option_id=".$option_id);
  2991. if (empty($option) or empty($option['value'])) {
  2992. $default = get_record($db, "config_options","id=$option_id");
  2993. return $default['default_value'];
  2994. }
  2995. return $option['value'];
  2996. }
  2997. function is_option($db, $option_id)
  2998. {
  2999. $option = get_record($db, "config", "option_id=".$option_id);
  3000. if (empty($option) or empty($option['value'])) { return; }
  3001. return 1;
  3002. }
  3003. function set_option($db, $option_id, $value)
  3004. {
  3005. $option['value'] = $value;
  3006. update_record($db, 'config', "option_id=$option_id", $option);
  3007. }
  3008. function is_subnet_aton($subnet,$ip) {
  3009. if (!isset($subnet)) { return 0; }
  3010. if (!isset($ip)) { return 0; }
  3011. $range = cidrToRange($subnet);
  3012. if ($ip>=ip2long($range[0]) and $ip <=ip2long($range[1])) { return 1; }
  3013. return 0;
  3014. }
  3015. function get_new_user_id($db, $ip, $mac, $hostname)
  3016. {
  3017. global $default_hotspot_ou_id;
  3018. global $default_user_ou_id;
  3019. $result['ip']=$ip;
  3020. $result['mac']=mac_dotted($mac);
  3021. $result['hostname']=$hostname;
  3022. $result['user_id']=NULL;
  3023. $result['ou_id']=NULL;
  3024. $ip_aton = ip2long($ip);
  3025. //personal user rules
  3026. //ip
  3027. if (!empty($ip)) {
  3028. $t_rules = get_records_sql($db, "SELECT * FROM auth_rules WHERE type=1 and LENGTH(rule)>0 AND user_id IS NOT NULL");
  3029. foreach ($t_rules as $row) {
  3030. if (!empty($row['rule']) and is_subnet_aton($row['rule'],$ip_aton)) { $result['user_id']=$row['user_id']; }
  3031. }
  3032. }
  3033. //mac
  3034. if (!empty($mac)) {
  3035. $mac_rules=get_records_sql($db,"SELECT * FROM auth_rules WHERE type=2 AND LENGTH(rule)>0 AND user_id IS NOT NULL");
  3036. foreach ($mac_rules as $row) {
  3037. if (!empty($row['rule']) and preg_match(mac_simplify($row['rule']), mac_simplify($mac))) { $result['user_id']=$row['user_id']; }
  3038. }
  3039. }
  3040. //hostname
  3041. if (!empty($hostname)) {
  3042. $mac_rules=get_records_sql($db,"SELECT * FROM auth_rules WHERE type=3 AND LENGTH(rule)>0 AND user_id IS NOT NULL");
  3043. foreach ($mac_rules as $row) {
  3044. if (!empty($row['rule']) and preg_match($row['rule'], $mac)) { $result['user_id']=$row['user_id']; }
  3045. }
  3046. }
  3047. if (!empty($result['user_id'])) { return $result; }
  3048. //ou rules
  3049. //ip
  3050. if (!empty($ip)) {
  3051. if (is_hotspot($db, $ip)) { $result['ou_id']=$default_hotspot_ou_id; }
  3052. $t_rules = get_records_sql($db, "SELECT * FROM auth_rules WHERE type=1 and LENGTH(rule)>0 AND ou_id IS NOT NULL");
  3053. foreach ($t_rules as $row) {
  3054. if (!empty($row['rule']) and is_subnet_aton($row['rule'],$ip_aton)) { $result['ou_id']=$row['ou_id']; }
  3055. }
  3056. }
  3057. //mac
  3058. if (!empty($mac)) {
  3059. $mac_rules=get_records_sql($db,"SELECT * FROM auth_rules WHERE type=2 AND LENGTH(rule)>0 AND ou_id IS NOT NULL");
  3060. foreach ($mac_rules as $row) {
  3061. if (!empty($row['rule']) and preg_match(mac_simplify($row['rule']), mac_simplify($mac))) { $result['ou_id']=$row['ou_id']; }
  3062. }
  3063. }
  3064. //hostname
  3065. if (!empty($hostname)) {
  3066. $mac_rules=get_records_sql($db,"SELECT * FROM auth_rules WHERE type=3 AND LENGTH(rule)>0 AND ou_id IS NOT NULL");
  3067. foreach ($mac_rules as $row) {
  3068. if (!empty($row['rule']) and preg_match($row['rule'], $mac)) { $result['ou_id']=$row['ou_id']; }
  3069. }
  3070. }
  3071. if (empty($result['ou_id'])) { $result['ou_id']=$default_user_ou_id; }
  3072. return $result;
  3073. }
  3074. function get_subnet_range($db,$subnet_id) {
  3075. if (empty($subnet_id)) { return; }
  3076. $t_option = get_record_sql($db, "SELECT ip_int_start,ip_int_stop FROM `subnets` WHERE id=$subnet_id");
  3077. if (!isset($t_option['ip_int_start'])) { $t_option['ip_int_start']=0; }
  3078. if (!isset($t_option['ip_int_stop'])) { $t_option['ip_int_stop']=0; }
  3079. $subnet['start']=$t_option['ip_int_start'];
  3080. $subnet['stop']=$t_option['ip_int_stop'];
  3081. return $subnet;
  3082. }
  3083. function is_hotspot($db, $ip)
  3084. {
  3085. if (! isset($ip)) { return 0; }
  3086. LOG_DEBUG($db,"Check hotspot network for ip: $ip");
  3087. $ip_aton = ip2long($ip);
  3088. $t_option = mysqli_query($db, "SELECT subnet,ip_int_start,ip_int_stop FROM `subnets` WHERE hotspot=1");
  3089. while (list ($f_net,$f_start,$f_stop) = mysqli_fetch_array($t_option)) {
  3090. if ($ip_aton >= $f_start and $ip_aton <= $f_stop) {
  3091. LOG_DEBUG($db,"ip: $ip [$ip_aton] found in network $f_net: [".$f_start."..".$f_stop."]");
  3092. return 1;
  3093. }
  3094. }
  3095. LOG_DEBUG($db,"ip $ip not found in hotspot network!");
  3096. return 0;
  3097. }
  3098. function is_office($db, $ip)
  3099. {
  3100. if (! isset($ip)) { return 0; }
  3101. LOG_DEBUG($db,"Check office network for ip: $ip");
  3102. $ip_aton = ip2long($ip);
  3103. $t_option = mysqli_query($db, "SELECT subnet,ip_int_start,ip_int_stop FROM `subnets` WHERE office=1");
  3104. while (list ($f_net,$f_start,$f_stop) = mysqli_fetch_array($t_option)) {
  3105. if ($ip_aton >= $f_start and $ip_aton <= $f_stop) {
  3106. LOG_DEBUG($db,"ip: $ip [$ip_aton] found in office $f_net: [".$f_start."..".$f_stop."]");
  3107. return 1;
  3108. }
  3109. }
  3110. LOG_DEBUG($db,"ip $ip not found in office network!");
  3111. return 0;
  3112. }
  3113. function is_our_network($db, $ip)
  3114. {
  3115. if (! isset($ip)) { return 0; }
  3116. if (is_hotspot($db, $ip)) { return 1; }
  3117. if (is_office($db, $ip)) { return 1; }
  3118. return 0;
  3119. }
  3120. function init_option($db)
  3121. {
  3122. global $org_name;
  3123. $org_name = get_option($db, 32);
  3124. global $KB;
  3125. $KB = get_option($db, 1);
  3126. global $debug;
  3127. $debug = get_option($db, 34);
  3128. global $log_level;
  3129. $log_level = get_option($db, 53);
  3130. if ($debug) { $log_level = 255; }
  3131. global $send_email;
  3132. $send_email = get_option($db, 51);
  3133. global $admin_email;
  3134. $admin_email = get_option($db, 21);
  3135. global $sender_email;
  3136. $sender_email = get_option($db, 52);
  3137. global $mac_discovery;
  3138. $mac_discovery = get_option($db, 17);
  3139. global $snmp_default_version;
  3140. $snmp_default_version = get_option($db, 9);
  3141. global $snmp_default_community;
  3142. $snmp_default_community = get_option($db, 11);
  3143. global $cacti_url;
  3144. $cacti_url = rtrim(get_option($db, 58),'/');
  3145. if (preg_match('/127.0.0.1/', $cacti_url)) { $cacti_url=NULL; }
  3146. global $nagios_url;
  3147. $nagios_url = rtrim(get_option($db, 57),'/').'/cgi-bin/';
  3148. if (preg_match('/127.0.0.1/', $nagios_url)) { $nagios_url=NULL; }
  3149. global $torrus_url;
  3150. $torrus_url = rtrim(get_option($db, 59),'/').'?nodeid=if//HOST_IP//IF_NAME////inoutbps';
  3151. if (preg_match('/127.0.0.1/', $torrus_url)) { $torrus_url=NULL; }
  3152. global $auto_mac_rule;
  3153. $auto_mac_rule = get_option($db, 64);
  3154. global $default_user_ou_id;
  3155. $ou = get_record_sql($db,"SELECT id FROM OU WHERE default_users = 1");
  3156. if (empty($ou)) { $default_user_ou_id = 0; } else { $default_user_ou_id = $ou['id']; }
  3157. global $default_hotspot_ou_id;
  3158. $ou = get_record_sql($db,"SELECT id FROM OU WHERE default_hotspot = 1");
  3159. if (empty($ou)) { $default_hotspot_ou_id = $default_user_ou_id; } else { $default_hotspot_ou_id = $ou['id']; }
  3160. }
  3161. init_option($db_link);
  3162. clean_dns_cache($db_link);
  3163. snmp_set_valueretrieval(SNMP_VALUE_LIBRARY);
  3164. snmp_set_enum_print(1);
  3165. ?>