common.php 114 KB

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