common.php 109 KB

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