common.php 106 KB

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