common.php 114 KB

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