common.php 116 KB

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