common.php 110 KB

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