common.php 107 KB

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