common.php 110 KB

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