common.php 115 KB

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