common.php 116 KB

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