common.php 115 KB

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