1
0

common.php 109 KB

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