common.php 111 KB

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