common.php 108 KB

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