common.php 106 KB

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