common.php 107 KB

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