common.php 111 KB

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