common.php 118 KB

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