common.php 109 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268
  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_dhcp_acl_list($db,$qa_name,$value='')
  1135. {
  1136. $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");
  1137. if (!empty($dhcp_acl) and count($dhcp_acl)>0) {
  1138. print "<input list=\"dhcp_acl\" id=\"$qa_name\" name=\"$qa_name\" value=\"$value\"/>";
  1139. print "<datalist id=\"dhcp_acl\">";
  1140. print "<option value=\"\">";
  1141. foreach ($dhcp_acl as $acl) {
  1142. print "<option value=\"{$acl['dhcp_acl']}\">";
  1143. }
  1144. print "</datalist>";
  1145. } else {
  1146. print "<input type=\"text\" name=\"$qa_name\" value=\"\" size=10/>";
  1147. }
  1148. }
  1149. function print_dhcp_option_set_list($db,$qa_name,$value='')
  1150. {
  1151. $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");
  1152. if (!empty($dhcp_option_sets) and count($dhcp_option_sets)>0) {
  1153. print "<input list=\"dhcp_option_set\" id=\"$qa_name\" name=\"$qa_name\" value=\"$value\"/>";
  1154. print "<datalist id=\"dhcp_option_set\">";
  1155. print "<option value=\"\">";
  1156. foreach ($dhcp_option_sets as $dhcp_option_set) {
  1157. print "<option value=\"{$dhcp_option_set['dhcp_option_set']}\">";
  1158. }
  1159. print "</datalist>";
  1160. } else {
  1161. print "<input type=\"text\" name=\"$qa_name\" value=\"\" size=10/>";
  1162. }
  1163. }
  1164. function print_enabled_select($qa_name, $qa_value)
  1165. {
  1166. print "<select id=\"$qa_name\" name=\"$qa_name\">\n";
  1167. if (!isset($qa_value) or strlen($qa_value) == 0) {
  1168. $qa_value = 0;
  1169. }
  1170. print_select_item('-', 0, $qa_value);
  1171. print_select_item(WEB_select_item_disabled, 1, $qa_value);
  1172. print_select_item(WEB_select_item_enabled, 2, $qa_value);
  1173. print "</select>\n";
  1174. }
  1175. function print_rule_target_select($qa_name, $qa_value)
  1176. {
  1177. print "<select id=\"$qa_name\" name=\"$qa_name\">\n";
  1178. if (!isset($qa_value) or strlen($qa_value) == 0) {
  1179. $qa_value = 0;
  1180. }
  1181. print_select_item('-', 0, $qa_value);
  1182. print_select_item(WEB_rules_target_user, 1, $qa_value);
  1183. print_select_item(WEB_rules_target_group, 2, $qa_value);
  1184. print "</select>\n";
  1185. }
  1186. function print_rule_type_select($qa_name, $qa_value)
  1187. {
  1188. print "<select id=\"$qa_name\" name=\"$qa_name\">\n";
  1189. if (!isset($qa_value) or strlen($qa_value) == 0) {
  1190. $qa_value = 0;
  1191. }
  1192. print_select_item('-', 0, $qa_value);
  1193. print_select_item(WEB_rules_type_subnet, 1, $qa_value);
  1194. print_select_item(WEB_rules_type_mac, 2, $qa_value);
  1195. print_select_item(WEB_rules_type_hostname, 3, $qa_value);
  1196. print "</select>\n";
  1197. }
  1198. function print_yn_select($qa_name, $qa_value)
  1199. {
  1200. print "<select id=\"$qa_name\" name=\"$qa_name\">\n";
  1201. if (!isset($qa_value) or strlen($qa_value) == 0) {
  1202. $qa_value = 0;
  1203. }
  1204. print_select_item('-', 0, $qa_value);
  1205. print_select_item(WEB_select_item_yes, 1, $qa_value);
  1206. print_select_item(WEB_select_item_no, 2, $qa_value);
  1207. print "</select>\n";
  1208. }
  1209. function print_ip_type_select($qa_name, $qa_value)
  1210. {
  1211. print "<select id=\"$qa_name\" name=\"$qa_name\">\n";
  1212. if (!isset($qa_value) or strlen($qa_value) == 0) {
  1213. $qa_value = 0;
  1214. }
  1215. print_select_item(WEB_select_item_every, 0, $qa_value);
  1216. print_select_item(WEB_select_item_static, 1, $qa_value);
  1217. print_select_item(WEB_select_item_dhcp, 2, $qa_value);
  1218. print_select_item(WEB_select_item_suspicious, 3, $qa_value);
  1219. print "</select>\n";
  1220. }
  1221. function print_vendor_select($db, $qa_name, $qa_value)
  1222. {
  1223. print "<select id=\"$qa_name\" name=\"$qa_name\" style=\"width: 100%\">\n";
  1224. $sSQL = "SELECT id,`name` FROM `vendors` order by `name`";
  1225. $vendors = mysqli_query($db, $sSQL);
  1226. print_select_item(WEB_select_item_all, 0, $qa_value);
  1227. while (list($v_id, $v_name) = mysqli_fetch_array($vendors)) {
  1228. print_select_item($v_name, $v_id, $qa_value);
  1229. }
  1230. print "</select>\n";
  1231. }
  1232. function print_vendor_set($db, $qa_name, $qa_value)
  1233. {
  1234. print "<select id=\"$qa_name\" name=\"$qa_name\" style=\"width: 100%\">\n";
  1235. $sSQL = "SELECT id,`name` FROM `vendors` order by `name`";
  1236. $vendors = mysqli_query($db, $sSQL);
  1237. while (list($v_id, $v_name) = mysqli_fetch_array($vendors)) {
  1238. print_select_item($v_name, $v_id, $qa_value);
  1239. }
  1240. print "</select>\n";
  1241. }
  1242. function get_vendor_name($db, $v_id)
  1243. {
  1244. $vendor = get_record_sql($db, "SELECT * FROM `vendors` WHERE id=" . $v_id);
  1245. if (empty($vendor)) {
  1246. return NULL;
  1247. }
  1248. return $vendor['name'];
  1249. }
  1250. function get_qa($qa_value, $text = FALSE)
  1251. {
  1252. if ($text) {
  1253. if ($qa_value == 1) { return "Да"; }
  1254. return "Нет";
  1255. } else {
  1256. if ($qa_value == 1) { return '<span style="font-size: 16px; font-weight: bold;">✓</span>'; }
  1257. return '<span style="font-size: 16px;">✗</span>';
  1258. }
  1259. }
  1260. function get_yes($qa_value, $text = FALSE)
  1261. {
  1262. if ($text) {
  1263. if ($qa_value == 1) { return "Да"; }
  1264. return "";
  1265. } else {
  1266. if ($qa_value == 1) { return '<span style="font-size: 16px; font-weight: bold;">✓</span>'; }
  1267. return "";
  1268. }
  1269. }
  1270. function print_td_qa ($qa_value, $text = FALSE, $parent_class = '')
  1271. {
  1272. $cl = "data_green";
  1273. if (!$qa_value) { $cl = "data_red"; }
  1274. print "<td class=\"$parent_class $cl\" >" . get_qa($qa_value,$text) . "</td>";
  1275. }
  1276. function print_td_yes ($qa_value, $text = FALSE, $parent_class = '')
  1277. {
  1278. $cl = "data_green";
  1279. if (!$qa_value) { $cl = "data_red"; }
  1280. print "<td class=\"$parent_class $cl\" >" . get_yes($qa_value,$text) . "</td>";
  1281. }
  1282. function print_action_select($action_name, $action_value)
  1283. {
  1284. print "<select id=\"$action_name\" name=\"$action_name\">\n";
  1285. print_select_item(WEB_select_item_allow, 1, $action_value);
  1286. print_select_item(WEB_select_item_forbidden, 0, $action_value);
  1287. print "</select>\n";
  1288. }
  1289. function get_action($action_value)
  1290. {
  1291. if ($action_value == 1) {
  1292. return "Разрешить";
  1293. }
  1294. return "Запретить";
  1295. }
  1296. function print_filter_select($db, $filter_name, $group_id)
  1297. {
  1298. print "<select id=\"$filter_name\" name=\"$filter_name\" class=\"js-select-single\">\n";
  1299. if (isset($group_id)) {
  1300. $sSQL = "SELECT id,name FROM Filter_list WHERE Filter_list.id not in (Select filter_id FROM Group_filters WHERE group_id=$group_id)";
  1301. } else {
  1302. $sSQL = "SELECT id,name FROM Filter_list Order by name";
  1303. }
  1304. $t_filters = mysqli_query($db, $sSQL);
  1305. while (list($filter_id, $filter_name) = mysqli_fetch_array($t_filters)) {
  1306. print_select_item($filter_name, $filter_id, 0);
  1307. }
  1308. print "</select>\n";
  1309. }
  1310. function get_filter($db, $filter_value)
  1311. {
  1312. list($filter) = mysqli_fetch_array(mysqli_query($db, "SELECT name FROM Filter_list WHERE id=" . $filter_value));
  1313. return $filter;
  1314. }
  1315. function get_login($db, $user_id)
  1316. {
  1317. list($login) = mysqli_fetch_array(mysqli_query($db, "SELECT login FROM User_list WHERE id=$user_id"));
  1318. return $login;
  1319. }
  1320. function get_auth_count($db, $user_id)
  1321. {
  1322. list($count) = mysqli_fetch_array(mysqli_query($db, "SELECT count(id) FROM User_auth WHERE user_id=$user_id and deleted=0"));
  1323. return $count;
  1324. }
  1325. function print_login_select($db, $login_name, $current_login)
  1326. {
  1327. print "<select id=\"$login_name\" name=\"$login_name\" class=\"js-select-single\">\n";
  1328. $t_login = mysqli_query($db, "SELECT id,login FROM User_list Order by Login");
  1329. print_select_item('None', 0, $current_login);
  1330. while (list($f_user_id, $f_login) = mysqli_fetch_array($t_login)) {
  1331. print_select_item($f_login, $f_user_id, $current_login);
  1332. }
  1333. print "</select>\n";
  1334. }
  1335. function print_auth_select($db, $login_name, $current_auth)
  1336. {
  1337. print "<select id=\"$login_name\" name=\"$login_name\" class=\"js-select-single\">\n";
  1338. $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");
  1339. print_select_item('Empty', 0, $current_auth);
  1340. while (list($f_login, $f_fio, $f_ip, $f_auth_id) = mysqli_fetch_array($t_login)) {
  1341. print_select_item($f_login . "[" . $f_fio . "] - " . $f_ip, $f_auth_id, $current_auth);
  1342. }
  1343. print "</select>\n";
  1344. }
  1345. function print_auth_select_mac($db, $login_name, $current_auth)
  1346. {
  1347. print "<select id=\"$login_name\" name=\"$login_name\" class=\"js-select-single\">\n";
  1348. $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");
  1349. print_select_item('Empty', 0, $current_auth);
  1350. while (list($f_login, $f_fio, $f_ip, $f_mac, $f_auth_id) = mysqli_fetch_array($t_login)) {
  1351. print_select_item($f_login . "[" . $f_mac . "] - " . $f_ip, $f_auth_id, $current_auth);
  1352. }
  1353. print "</select>\n";
  1354. }
  1355. function compact_port_name($port)
  1356. {
  1357. $result = $port;
  1358. $result = preg_replace('/XGigabitEthernet/', 'X', $result);
  1359. $result = preg_replace('/TenGigabitEthernet/', 'Te', $result);
  1360. $result = preg_replace('/GigabitEthernet/', 'Gi', $result);
  1361. return $result;
  1362. }
  1363. function print_device_port_select($db, $field_name, $device_id, $target_id)
  1364. {
  1365. print "<select id=\"$field_name\" name=\"$field_name\" class=\"js-select-single\" >\n";
  1366. if (empty($target_id)) {
  1367. $target_id = 0;
  1368. }
  1369. if (empty($device_id)) {
  1370. $device_id = 0;
  1371. }
  1372. $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";
  1373. $t_device = mysqli_query($db, $d_sql);
  1374. print_select_item('Empty', 0, $target_id);
  1375. while (list($f_name, $f_port, $f_device_id, $f_target_id, $f_ifname) = mysqli_fetch_array($t_device)) {
  1376. if (empty($f_ifname)) {
  1377. $f_ifname = $f_port;
  1378. }
  1379. print_select_item($f_name . "[" . $f_port . "] - " . compact_port_name($f_ifname), $f_target_id, $target_id);
  1380. }
  1381. print "</select>\n";
  1382. }
  1383. function print_device_select($db, $field_name, $device_id)
  1384. {
  1385. print "<select id=\"$field_name\" name=\"$field_name\" class=\"js-select-single\" >\n";
  1386. $d_sql = "SELECT D.device_name, D.id FROM devices AS D Where D.deleted=0 order by D.device_name ASC";
  1387. $t_device = mysqli_query($db, $d_sql);
  1388. print_select_item(WEB_select_item_every, 0, $device_id);
  1389. while (list($f_name, $f_device_id) = mysqli_fetch_array($t_device)) {
  1390. print_select_item($f_name, $f_device_id, $device_id);
  1391. }
  1392. print "</select>\n";
  1393. }
  1394. function print_netdevice_select($db, $field_name, $device_id)
  1395. {
  1396. print "<select id=\"$field_name\" name=\"$field_name\" class=\"js-select-single\" >\n";
  1397. $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";
  1398. $t_device = mysqli_query($db, $d_sql);
  1399. print_select_item(WEB_select_item_every, 0, $device_id);
  1400. while (list($f_name, $f_device_id) = mysqli_fetch_array($t_device)) {
  1401. print_select_item($f_name, $f_device_id, $device_id);
  1402. }
  1403. print "</select>\n";
  1404. }
  1405. function print_vlan_select($db, $field_name, $vlan)
  1406. {
  1407. print "<select id=\"$field_name\" name=\"$field_name\" class=\"js-select-single\" style=\"width: 100px;\" >\n";
  1408. $d_sql = "SELECT DISTINCT vlan FROM device_ports ORDER BY vlan DESC";
  1409. $v_device = mysqli_query($db, $d_sql);
  1410. if (!isset($vlan) or empty($vlan)) {
  1411. $vlan = 1;
  1412. };
  1413. print_select_item('1', 1, $vlan);
  1414. while (list($f_vlan) = mysqli_fetch_array($v_device)) {
  1415. if ($f_vlan === '1') {
  1416. continue;
  1417. }
  1418. print_select_item($f_vlan, $f_vlan, $vlan);
  1419. }
  1420. print "</select>\n";
  1421. }
  1422. function print_device_select_ip($db, $field_name, $device_ip)
  1423. {
  1424. print "<select id=\"$field_name\" name=\"$field_name\" class=\"js-select-single\" >\n";
  1425. $d_sql = "SELECT D.device_name, D.ip FROM devices AS D Where D.deleted=0 order by D.device_name ASC";
  1426. $t_device = mysqli_query($db, $d_sql);
  1427. print_select_item(WEB_select_item_every, '', $device_ip);
  1428. while (list($f_name, $f_device_ip) = mysqli_fetch_array($t_device)) {
  1429. print_select_item($f_name, $f_device_ip, $device_ip);
  1430. }
  1431. print "</select>\n";
  1432. }
  1433. function print_syslog_device_select($db, $field_name, $syslog_filter, $device_ip)
  1434. {
  1435. print "<select id=\"$field_name\" name=\"$field_name\" class=\"js-select-single\" >\n";
  1436. $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";
  1437. $t_device = mysqli_query($db, $d_sql);
  1438. print_select_item(WEB_select_item_every, '', $device_ip);
  1439. while (list($f_ip, $f_name) = mysqli_fetch_array($t_device)) {
  1440. if (!isset($f_name) or empty($f_name)) {
  1441. $f_name = $f_ip;
  1442. }
  1443. print_select_item($f_name, $f_ip, $device_ip);
  1444. }
  1445. print "</select>\n";
  1446. }
  1447. function print_gateway_select($db, $field_name, $device_id)
  1448. {
  1449. print "<select id=\"$field_name\" name=\"$field_name\" >\n";
  1450. $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";
  1451. $t_device = mysqli_query($db, $d_sql);
  1452. print_select_item(WEB_select_item_every, 0, $device_id);
  1453. while (list($f_name, $f_device_id) = mysqli_fetch_array($t_device)) {
  1454. print_select_item($f_name, $f_device_id, $device_id);
  1455. }
  1456. print "</select>\n";
  1457. }
  1458. function get_gateways($db)
  1459. {
  1460. $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";
  1461. $t_device = mysqli_query($db, $d_sql);
  1462. unset($result);
  1463. while (list($f_name, $f_device_id) = mysqli_fetch_array($t_device)) {
  1464. $result[$f_device_id] = $f_name;
  1465. }
  1466. return $result;
  1467. }
  1468. function print_device_port($db, $target_id)
  1469. {
  1470. $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";
  1471. $t_device = mysqli_query($db, $d_sql);
  1472. while (list($f_name, $f_port, $f_device_id) = mysqli_fetch_array($t_device)) {
  1473. print "<a href=\"/admin/devices/switchport.php?id=$f_device_id\">" . $f_name . "[" . $f_port . "]</a>\n";
  1474. }
  1475. }
  1476. function get_device_ips($db, $device_id)
  1477. {
  1478. $switch = get_record($db, 'devices', 'id=' . $device_id);
  1479. $index = 0;
  1480. if (!empty($switch['user_id'])) {
  1481. $auth_ips = get_records($db, 'User_auth', 'deleted=0 and user_id=' . $switch['user_id']);
  1482. foreach ($auth_ips as $key => $value) {
  1483. if (isset($value['ip'])) {
  1484. $result[$index] = $value['ip'];
  1485. $index++;
  1486. }
  1487. }
  1488. } else {
  1489. if (isset($switch['ip'])) {
  1490. $result[$index] = $switch['ip'];
  1491. $index++;
  1492. }
  1493. }
  1494. return $result;
  1495. }
  1496. function get_device_id($db, $device_name)
  1497. {
  1498. $d_sql = "SELECT id FROM devices WHERE device_name='$device_name' and deleted=0";
  1499. $dev = get_record_sql($db, $d_sql);
  1500. if (empty($dev)) {
  1501. return NULL;
  1502. }
  1503. return $dev["id"];
  1504. }
  1505. function get_device_name($db, $device_id)
  1506. {
  1507. $d_sql = "SELECT device_name FROM devices WHERE id='$device_id'";
  1508. $dev = get_record_sql($db, $d_sql);
  1509. if (empty($dev)) {
  1510. return NULL;
  1511. }
  1512. return $dev["device_name"];
  1513. }
  1514. function get_auth_by_ip($db, $ip)
  1515. {
  1516. $d_sql = "SELECT id FROM User_auth WHERE ip='$ip' and deleted=0";
  1517. $auth = get_record_sql($db, $d_sql);
  1518. if (empty($auth)) {
  1519. return NULL;
  1520. }
  1521. return $auth["id"];
  1522. }
  1523. function get_user_by_ip($db, $ip)
  1524. {
  1525. $d_sql = "SELECT user_id FROM User_auth WHERE ip='$ip' and deleted=0";
  1526. $auth = get_record_sql($db, $d_sql);
  1527. if (empty($auth)) {
  1528. return NULL;
  1529. }
  1530. return $auth["user_id"];
  1531. }
  1532. function get_device_by_auth($db, $id)
  1533. {
  1534. $d_sql = "SELECT id FROM devices WHERE user_id=$id and deleted=0";
  1535. $f_dev = get_record_sql($db, $d_sql);
  1536. if (empty($f_dev)) {
  1537. return NULL;
  1538. }
  1539. return $f_dev['id'];
  1540. }
  1541. function print_auth_port($db, $port_id, $new_window = FALSE)
  1542. {
  1543. $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";
  1544. $t_auth = mysqli_query($db, $d_sql);
  1545. while (list($f_ip, $f_int, $f_mac, $f_auth_id, $f_dns, $f_user_id) = mysqli_fetch_array($t_auth)) {
  1546. $name = $f_ip;
  1547. if (!empty($f_dns)) {
  1548. $name = $f_dns;
  1549. }
  1550. $title = get_login($db, $f_user_id) . " =>" . $f_ip . "[" . $f_mac . "]";
  1551. if (!empty($f_dns)) {
  1552. $title .= " | " . $f_dns;
  1553. }
  1554. if ($new_window) {
  1555. print "<a href=\"\" title=\"" . $title . "\" onclick=\"" . open_window_url("/admin/users/editauth.php?id=" . $f_auth_id) . " return false;\">" . $name . " [" . $f_ip . "]</a><br>";
  1556. } else {
  1557. print "<a href=/admin/users/editauth.php?id=" . $f_auth_id . " title=\"" . $title . "\" >" . $name . " [" . $f_ip . "]</a><br>";
  1558. }
  1559. }
  1560. }
  1561. function get_port_comment($db, $port_id, $port_comment = '')
  1562. {
  1563. $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";
  1564. $t_auth = mysqli_query($db, $d_sql);
  1565. $comment_found = 0;
  1566. $result = '';
  1567. while (list($f_int, $f_comment) = mysqli_fetch_array($t_auth)) {
  1568. if (!empty($f_comment)) {
  1569. $comment_found = 1;
  1570. } else {
  1571. $f_comment = '';
  1572. }
  1573. $result .= $f_comment . '<br>';
  1574. }
  1575. if (!$comment_found) {
  1576. return $port_comment;
  1577. }
  1578. if (!empty($port_comment)) {
  1579. $result .= '(' . $port_comment . ')';
  1580. }
  1581. return $result;
  1582. }
  1583. function print_auth_simple($db, $auth_id)
  1584. {
  1585. $auth = get_record($db, "User_auth", "id=$auth_id");
  1586. $name = $auth['dns_name'];
  1587. if (empty($name)) {
  1588. $name = $auth['comments'];
  1589. }
  1590. if (empty($name)) {
  1591. $name = $auth['ip'];
  1592. }
  1593. print "<a href=\"/admin/users/editauth.php?id=$auth_id\">" . $name . "</a><br>";
  1594. }
  1595. function print_auth($db, $auth_id)
  1596. {
  1597. $auth = get_record($db, "User_auth", "id=$auth_id");
  1598. $name = $auth['dns_name'];
  1599. if (empty($name)) {
  1600. $name = $auth['comments'];
  1601. } else {
  1602. $name .= " (" . $auth['comments'] . ")";
  1603. }
  1604. if (empty($name)) {
  1605. $name = $auth['ip'];
  1606. } else {
  1607. $name .= " [" . $auth['ip'] . "]";
  1608. }
  1609. print "<a href=\"/admin/users/editauth.php?id=$auth_id\">" . $name . "</a><br>";
  1610. }
  1611. function print_auth_detail($db, $auth_id)
  1612. {
  1613. $auth = get_record($db, "User_auth", "id=$auth_id");
  1614. $name = $auth['dns_name'];
  1615. if (empty($name)) {
  1616. $name = $auth['comments'];
  1617. } else {
  1618. $name .= " (" . $auth['comments'] . ")";
  1619. }
  1620. if (empty($name)) {
  1621. $name = $auth['ip'];
  1622. } else {
  1623. $name .= " [" . $auth['ip'] . "]";
  1624. }
  1625. $name .= " last: [" . $auth['last_found'] . "] ";
  1626. if ($auth['deleted'] == 1) {
  1627. $name .= " <font color='red'>DELETED!!!</font>";
  1628. }
  1629. print "<a href=\"/admin/users/editauth.php?id=$auth_id\">" . $name . "</a><br>";
  1630. }
  1631. function get_auth_port_count($db, $port_id)
  1632. {
  1633. $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";
  1634. $t_device = mysqli_query($db, $d_sql);
  1635. list($f_count) = mysqli_fetch_array($t_device);
  1636. if (!isset($f_count)) {
  1637. $f_count = 0;
  1638. }
  1639. return $f_count;
  1640. }
  1641. function get_connection($db, $auth_id)
  1642. {
  1643. $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";
  1644. $t_device = mysqli_query($db, $d_sql);
  1645. list($f_name, $f_port) = mysqli_fetch_array($t_device);
  1646. if (isset($f_name)) {
  1647. $result = expand_device_name($db, $f_name) . "[" . $f_port . "]";
  1648. } else {
  1649. $result = '';
  1650. }
  1651. return $result;
  1652. }
  1653. function get_connection_string($db, $auth_id)
  1654. {
  1655. $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";
  1656. $t_device = mysqli_query($db, $d_sql);
  1657. list($f_name, $f_port) = mysqli_fetch_array($t_device);
  1658. if (isset($f_name)) {
  1659. $result = $f_name . "[" . $f_port . "]";
  1660. } else {
  1661. $result = '';
  1662. }
  1663. return $result;
  1664. }
  1665. function get_port($db, $port_id)
  1666. {
  1667. $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";
  1668. $t_device = mysqli_query($db, $d_sql);
  1669. list($f_name, $f_port) = mysqli_fetch_array($t_device);
  1670. if (isset($f_name)) {
  1671. $result = expand_device_name($db, $f_name) . "[" . $f_port . "]";
  1672. } else {
  1673. $result = '';
  1674. }
  1675. return $result;
  1676. }
  1677. function print_option_select($db, $option_name)
  1678. {
  1679. print "<select id=\"$option_name\" name=\"$option_name\">\n";
  1680. $t_option = mysqli_query($db, "SELECT id,option_name FROM config_options WHERE uniq=0 AND draft=0 order by option_name");
  1681. while (list($f_id, $f_name) = mysqli_fetch_array($t_option)) {
  1682. print "<option value=$f_id>$f_name</option>";
  1683. }
  1684. $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");
  1685. while (list($f_id, $f_name) = mysqli_fetch_array($t_option)) {
  1686. print "<option value=$f_id>$f_name</option>";
  1687. }
  1688. print "</select>\n";
  1689. }
  1690. function ResolveIP($db, $ip_int)
  1691. {
  1692. $ip_name = "-";
  1693. if (empty($ip_int)) {
  1694. return $ip_name;
  1695. }
  1696. $dns_cache = get_record_sql($db, "SELECT * FROM dns_cache WHERE ip=$ip_int");
  1697. if (empty($dns_cache) or empty($dns_cache['dns'])) {
  1698. $ip_name = gethostbyaddr(long2ip($ip_int));
  1699. if (empty($ip_name) or $ip_name == long2ip($ip_int)) {
  1700. $ip_name = "-";
  1701. }
  1702. run_sql($db, "INSERT INTO dns_cache(dns,ip) VALUES('" . $ip_name . "'," . $ip_int . ")");
  1703. } else {
  1704. $ip_name = $dns_cache['dns'];
  1705. }
  1706. return $ip_name;
  1707. }
  1708. function clean_dns_cache($db)
  1709. {
  1710. $date = time();
  1711. $date = $date - 86400;
  1712. $date_clean = DateTimeImmutable::createFromFormat('U', $date);
  1713. $clean_date = $date_clean->format('Y-m-d H:i:s');
  1714. run_sql($db, "DELETE FROM dns_cache WHERE `timestamp`<='" . $clean_date . "'");
  1715. }
  1716. function clean_unreferensed_rules($db)
  1717. {
  1718. run_sql($db, "DELETE FROM `auth_rules` WHERE user_id NOT IN (SELECT id FROM User_list)");
  1719. }
  1720. function FormatDateStr($format = 'Y-m-d H:i:s', $date_str)
  1721. {
  1722. $date1 = GetDateTimeFromString($date_str);
  1723. $result = $date1->format($format);
  1724. return $result;
  1725. }
  1726. function GetDateTimeFromString($date_str)
  1727. {
  1728. if (!is_a($date_str, 'DateTime')) {
  1729. $t_date_str = urldecode($date_str);
  1730. $t_date_str = preg_replace('/(\'|\")/', '', $t_date_str);
  1731. $t_date_str = preg_replace('/T/', ' ', $t_date_str);
  1732. $date1 = DateTime::createFromFormat('Y-m-d H:i:s', $t_date_str);
  1733. if (!$date1) {
  1734. $date1 = DateTime::createFromFormat('Y.m.d H:i:s', $t_date_str);
  1735. }
  1736. if (!$date1) {
  1737. $date1 = DateTime::createFromFormat('Y/m/d H:i:s', $t_date_str);
  1738. }
  1739. if (!$date1) {
  1740. $date1 = DateTime::createFromFormat('Y-m-d H:i', $t_date_str);
  1741. }
  1742. if (!$date1) {
  1743. $date1 = DateTime::createFromFormat('Y.m.d H:i', $t_date_str);
  1744. }
  1745. if (!$date1) {
  1746. $date1 = DateTime::createFromFormat('Y/m/d H:i', $t_date_str);
  1747. }
  1748. if (!$date1) {
  1749. $date1 = DateTime::createFromFormat('Y-m-d|', $t_date_str);
  1750. }
  1751. if (!$date1) {
  1752. $date1 = DateTime::createFromFormat('Y.m.d|', $t_date_str);
  1753. }
  1754. if (!$date1) {
  1755. $date1 = DateTime::createFromFormat('Y/m/d|', $t_date_str);
  1756. }
  1757. if (!$date1) {
  1758. $date1 = new DateTime;
  1759. $date1->setTime(0, 0, 0, 1);
  1760. }
  1761. } else {
  1762. return $date_str;
  1763. }
  1764. return $date1;
  1765. }
  1766. function GetNowTimeString()
  1767. {
  1768. $now = new DateTimeImmutable('now');
  1769. $result = $now->format('Y-m-d H:i:s');
  1770. return $result;
  1771. }
  1772. function GetNowDayString()
  1773. {
  1774. $now = new DateTimeImmutable('now');
  1775. $result = $now->format('Y-m-d');
  1776. return $result;
  1777. }
  1778. function get_ip_subnet($db, $ip)
  1779. {
  1780. if (empty($ip)) {
  1781. return;
  1782. }
  1783. $ip_aton = ip2long($ip);
  1784. $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)");
  1785. if (empty($user_subnet)) {
  1786. return;
  1787. }
  1788. return $user_subnet;
  1789. }
  1790. function find_mac_in_subnet($db, $ip, $mac)
  1791. {
  1792. if (empty($ip)) {
  1793. return;
  1794. }
  1795. if (empty($mac)) {
  1796. return;
  1797. }
  1798. $ip_subnet = get_ip_subnet($db, $ip);
  1799. if (empty($ip_subnet)) {
  1800. return;
  1801. }
  1802. $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");
  1803. $auth_count = 0;
  1804. $result['count'] = 0;
  1805. $result['users_id'] = [];
  1806. foreach ($t_auth as $row) {
  1807. if (!empty($row['id'])) {
  1808. $auth_count++;
  1809. $result['count'] = $auth_count;
  1810. $result[$auth_count] = $row['id'];
  1811. array_push($result['users_id'], $row['user_id']);
  1812. }
  1813. }
  1814. return $result;
  1815. }
  1816. function apply_auth_rule($db, $auth_record, $user_id)
  1817. {
  1818. if (empty($auth_record)) {
  1819. return;
  1820. }
  1821. if (empty($user_id)) {
  1822. return $auth_record;
  1823. }
  1824. $user_rec = get_record($db, 'User_list', "id=" . $user_id);
  1825. if (empty($user_rec)) {
  1826. return $auth_record;
  1827. }
  1828. //set filter and status by user
  1829. $auth_record['ou_id'] = $user_rec['ou_id'];
  1830. $auth_record['user_id'] = $user_rec['id'];
  1831. $auth_record['filter_group_id'] = $user_rec['filter_group_id'];
  1832. $auth_record['queue_id'] = $user_rec['queue_id'];
  1833. $auth_record['enabled'] = $user_rec['enabled'];
  1834. $auth_record['changed'] = 1;
  1835. //maybe fill comments?
  1836. if (!empty($user_rec['fio']) and empty($auth_record['comments'])) {
  1837. $auth_record['comments'] = $user_rec['fio'];
  1838. }
  1839. return $auth_record;
  1840. }
  1841. function fix_auth_rules($db)
  1842. {
  1843. //cleanup hotspot subnet rules
  1844. $t_hotspot = get_records_sql($db, "SELECT * FROM `OU` WHERE default_users=1 or default_hotspot=1");
  1845. if (!empty($t_hotspot)) {
  1846. foreach ($t_hotspot as $row) {
  1847. delete_record($db, "auth_rules", "ou_id='" . $row['id'] . "'");
  1848. }
  1849. }
  1850. $t_hotspot = get_records_sql($db, "SELECT * FROM subnets WHERE hotspot=1");
  1851. if (!empty($t_hotspot)) {
  1852. foreach ($t_hotspot as $row) {
  1853. delete_record($db, "auth_rules", "rule='" . $row['subnet'] . "'");
  1854. }
  1855. }
  1856. }
  1857. #---------------------------------------------------------------------------------------------------------------
  1858. function new_user($db, $user_info)
  1859. {
  1860. if (!empty($user_info['mac'])) {
  1861. $user['login'] = mac_dotted($user_info['mac']);
  1862. } else {
  1863. $user['login'] = $user_info['ip'];
  1864. }
  1865. if (!empty($user_info['dhcp_hostname'])) {
  1866. $user['fio'] = $user_info['ip'] . '[' . $user_info['dhcp_hostname'] . ']';
  1867. } else {
  1868. $user['fio'] = $user_info['ip'];
  1869. }
  1870. $login_count = get_count_records($db, "User_list", "(login LIKE '" . $user['login'] . "(%)') OR (login='" . $user['login'] . "')");
  1871. if (!empty($login_count) and $login_count > 0) {
  1872. $login_count++;
  1873. $user['login'] = $user['login'] . "(" . $login_count . ")";
  1874. }
  1875. $user['ou_id'] = $user_info['ou_id'];
  1876. $ou_info = get_record_sql($db, "SELECT * FROM OU WHERE id=" . $user_info['ou_id']);
  1877. if (!empty($ou_info)) {
  1878. $user['enabled'] = $ou_info['enabled'];
  1879. if (empty($user['enabled'])) {
  1880. $user['enabled'] = 0;
  1881. }
  1882. $user['queue_id'] = $ou_info['queue_id'];
  1883. if (empty($user['queue_id'])) {
  1884. $user['queue_id'] = 0;
  1885. }
  1886. $user['filter_group_id'] = $ou_info['filter_group_id'];
  1887. if (empty($user['filter_group_id'])) {
  1888. $user['filter_group_id'] = 0;
  1889. }
  1890. }
  1891. $result = insert_record($db, "User_list", $user);
  1892. $auto_mac_rule = get_option($db, 64);
  1893. if (!empty($result) and $auto_mac_rule and $user_info['mac']) {
  1894. $auth_rule['user_id'] = $result;
  1895. $auth_rule['type'] = 2;
  1896. $auth_rule['rule'] = mac_dotted($user_info['mac']);
  1897. insert_record($db, "auth_rules", $auth_rule);
  1898. }
  1899. return $result;
  1900. }
  1901. function new_auth($db, $ip, $mac, $user_id)
  1902. {
  1903. $ip_aton = ip2long($ip);
  1904. $msg = '';
  1905. if (!empty($mac)) {
  1906. $auth_record = get_record_sql($db, "SELECT * FROM User_auth WHERE ip_int=$ip_aton AND mac='" . mac_dotted($mac) . "' AND deleted=0");
  1907. if (!empty($auth_record)) {
  1908. LOG_WARNING($db, "Pair ip-mac already exists! Skip creating $ip [$mac] auth_id: " . $auth_record["id"]);
  1909. return $auth_record['id'];
  1910. }
  1911. }
  1912. // save traffic detailization
  1913. $save_traf = get_option($db, 23);
  1914. $resurrection_id = NULL;
  1915. // seek old auth with same ip and mac
  1916. $resurrection_id = get_id_record($db, 'User_auth', " deleted=1 AND ip_int=" . $ip_aton . " AND mac='" . $mac . "'");
  1917. if (!empty($resurrection_id)) {
  1918. $msg .= "Recovered auth_id: $resurrection_id with ip: $ip and mac: $mac ";
  1919. $auth['user_id'] = $user_id;
  1920. $auth['deleted'] = 0;
  1921. $auth['save_traf'] = $save_traf * 1;
  1922. update_record($db, "User_auth", "id=$resurrection_id", $auth);
  1923. } else {
  1924. // not found ->create new record
  1925. $msg .= "Create new ip record \r\nip: $ip\r\nmac: $mac\r\n";
  1926. $auth['deleted'] = 0;
  1927. $auth['user_id'] = $user_id;
  1928. $auth['ip'] = $ip;
  1929. $auth['ip_int'] = $ip_aton;
  1930. $auth['mac'] = $mac;
  1931. $auth['save_traf'] = $save_traf * 1;
  1932. $resurrection_id = insert_record($db, "User_auth", $auth);
  1933. }
  1934. //check rules, update filter and state for new record
  1935. if (!empty($resurrection_id)) {
  1936. $auth = apply_auth_rule($db, $auth, $user_id);
  1937. update_record($db, "User_auth", "id=$resurrection_id", $auth);
  1938. if (!is_hotspot($db, $ip) and !empty($msg)) {
  1939. LOG_WARNING($db, $msg);
  1940. }
  1941. if (is_hotspot($db, $ip) and !empty($msg)) {
  1942. LOG_INFO($db, $msg);
  1943. }
  1944. }
  1945. return $resurrection_id;
  1946. }
  1947. function resurrection_auth($db, $ip_record)
  1948. {
  1949. $ip = $ip_record['ip'];
  1950. $mac = $ip_record['mac'];
  1951. $action = $ip_record['type'];
  1952. $dhcp_hostname = $ip_record['hostname'];
  1953. $hotspot_found = $ip_record['hotspot'];
  1954. $ip_aton = ip2long($ip);
  1955. $auth_record = get_record_sql($db, "SELECT * FROM User_auth WHERE ip_int=$ip_aton AND mac='" . $mac . "' AND deleted=0");
  1956. if (!empty($auth_record)) {
  1957. $user_info = get_record_sql($db, "SELECT * FROM User_list WHERE id=" . $auth_record['user_id']);
  1958. LOG_DEBUG($db, "external dhcp user " . $user_info['login'] . " [" . $ip . "] auth_id: " . $auth_record['id']);
  1959. if (isset($dhcp_hostname) and !empty($dhcp_hostname)) {
  1960. $auth['dhcp_hostname'] = $dhcp_hostname;
  1961. }
  1962. $auth['dhcp_action'] = $action;
  1963. $auth['dhcp_time'] = GetNowTimeString();
  1964. if ($action === 'add') {
  1965. $auth['last_found'] = GetNowTimeString();
  1966. }
  1967. update_record($db, "User_auth", "id=" . $auth_record['id'], $auth);
  1968. return $auth_record['id'];
  1969. }
  1970. $ip_subnet = get_ip_subnet($db, $ip);
  1971. if ($ip_subnet['static']) {
  1972. LOG_WARNING($db, "Unknown pair ip+mac in static subnet! ip: $ip mac: [" . mac_dotted($mac) . "]. Skip");
  1973. return;
  1974. }
  1975. $msg = '';
  1976. // search changed mac
  1977. $auth_record = get_record_sql($db, "SELECT * FROM User_auth WHERE ip_int=$ip_aton AND deleted=0");
  1978. if (!empty($auth_record)) {
  1979. if (empty($auth_record['mac'])) {
  1980. $auth['mac'] = mac_dotted($mac);
  1981. $auth['dhcp_action'] = $action;
  1982. $auth['dhcp_time'] = GetNowTimeString();
  1983. if (!empty($dhcp_hostname)) {
  1984. $auth['dhcp_hostname'] = $dhcp_hostname;
  1985. }
  1986. if ($action === 'add') {
  1987. $auth['last_found'] = GetNowTimeString();
  1988. }
  1989. LOG_INFO($db, "for ip: $ip mac not found! Use empty record...");
  1990. update_record($db, "User_auth", "id=" . $auth_record['id'], $auth);
  1991. return $auth_record['id'];
  1992. } else {
  1993. if (!$hotspot_found) {
  1994. 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']);
  1995. }
  1996. run_sql($db, "UPDATE User_auth SET changed=1, deleted=1 WHERE id=" . $auth_record['id']);
  1997. }
  1998. }
  1999. // default id
  2000. $new_user_info = get_new_user_id($db, $ip, $mac, $dhcp_hostname);
  2001. if (!empty($new_user_info['user_id'])) {
  2002. $new_user_id = $new_user_info['user_id'];
  2003. }
  2004. if (empty($new_user_id)) {
  2005. $new_user_id = new_user($db, $new_user_info);
  2006. }
  2007. $resurrection_id = NULL;
  2008. $save_traf = get_option($db, 23);
  2009. $auth_record = get_record_sql($db, "SELECT * FROM User_auth WHERE ip_int=" . $ip_aton . " and mac='" . $mac . "'");
  2010. // seek old auth with same ip and mac
  2011. if (!empty($auth_record)) {
  2012. // found ->Resurrection old record
  2013. $resurrection_id = $auth_record['id'];
  2014. $msg .= "Recovered auth_id: $resurrection_id with ip: $ip and mac: $mac ";
  2015. $auth['dhcp_action'] = $action;
  2016. $auth['user_id'] = $new_user_id;
  2017. $auth['deleted'] = 0;
  2018. $auth['dhcp_time'] = GetNowTimeString();
  2019. $auth['save_traf'] = $save_traf * 1;
  2020. if (!empty($dhcp_hostname)) {
  2021. $auth['dhcp_hostname'] = $dhcp_hostname;
  2022. }
  2023. if ($action === 'add') {
  2024. $auth['last_found'] = GetNowTimeString();
  2025. }
  2026. update_record($db, "User_auth", "id=$resurrection_id", $auth);
  2027. } else {
  2028. // not found ->create new record
  2029. $msg .= "Создаём новый ip-адрес \r\nip: $ip\r\nmac: $mac\r\n";
  2030. $auth['deleted'] = 0;
  2031. $auth['user_id'] = $new_user_id;
  2032. $auth['ip'] = $ip;
  2033. $auth['ip_int'] = $ip_aton;
  2034. $auth['mac'] = $mac;
  2035. $auth['dhcp_action'] = $action;
  2036. $auth['dhcp_time'] = GetNowTimeString();
  2037. $auth['save_traf'] = $save_traf * 1;
  2038. if (!empty($dhcp_hostname)) {
  2039. $auth['dhcp_hostname'] = $dhcp_hostname;
  2040. }
  2041. if ($action == 'add') {
  2042. $auth['last_found'] = GetNowTimeString();
  2043. }
  2044. $resurrection_id = insert_record($db, "User_auth", $auth);
  2045. }
  2046. //check rules, update filter and state for new record
  2047. if (!empty($resurrection_id)) {
  2048. $auth = apply_auth_rule($db, $auth, $new_user_id);
  2049. update_record($db, "User_auth", "id=$resurrection_id", $auth);
  2050. $msg .= "filter: " . $auth['filter_group_id'] . "\r\n queue_id: " . $auth['queue_id'] . "\r\n enabled: " . $auth['enabled'] . "\r\nid: $resurrection_id";
  2051. if (!$hotspot_found and !empty($msg)) {
  2052. LOG_WARNING($db, $msg);
  2053. }
  2054. if ($hotspot_found and !empty($msg)) {
  2055. LOG_INFO($db, $msg);
  2056. }
  2057. }
  2058. return $resurrection_id;
  2059. }
  2060. function get_auth($db, $current_auth)
  2061. {
  2062. if (!isset($current_auth)) {
  2063. return;
  2064. }
  2065. if ($current_auth == 0) {
  2066. return;
  2067. }
  2068. $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");
  2069. list($f_login, $f_ip) = mysqli_fetch_array($t_login);
  2070. $result = $f_login . "[" . $f_ip . "]";
  2071. return $result;
  2072. }
  2073. function get_auth_by_mac($db, $mac)
  2074. {
  2075. if (!isset($mac)) {
  2076. return;
  2077. }
  2078. $mac = mac_dotted($mac);
  2079. $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");
  2080. list($f_id, $f_login, $f_auth_id, $f_ip) = mysqli_fetch_array($t_login);
  2081. if (isset($f_id)) {
  2082. $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>';
  2083. } else {
  2084. $result['auth'] = 'Unknown';
  2085. }
  2086. $result['mac'] = expand_mac($db, $mac);
  2087. return $result;
  2088. }
  2089. function get_auth_mac($db, $current_auth)
  2090. {
  2091. if (!isset($current_auth)) {
  2092. return;
  2093. }
  2094. if ($current_auth == 0) {
  2095. return;
  2096. }
  2097. $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");
  2098. list($f_login, $f_mac) = mysqli_fetch_array($t_login);
  2099. $result = $f_login . "[" . $f_mac . "]";
  2100. return $result;
  2101. }
  2102. function add_auth_rule($db, $rule, $type, $user_id)
  2103. {
  2104. $new['user_id'] = $user_id;
  2105. $new['type'] = $type;
  2106. $new['rule'] = $rule;
  2107. $rule_id = 0;
  2108. $auth_rules = get_record_sql($db, "SELECT * FROM auth_rules WHERE rule='" . $rule . "' AND type=" . $type);
  2109. if (empty($auth_rules)) {
  2110. $rule_id = insert_record($db, "auth_rules", $new);
  2111. LOG_INFO($db, "Create auto rule for user_id: " . $user_id . " rule: " . $rule . " type: " . $type);
  2112. } else {
  2113. if ($auth_rules['user_id'] !== $user_id) {
  2114. 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']);
  2115. $rule_id = 0;
  2116. } else { $rule_id = $auth_rules['id']; }
  2117. }
  2118. return $rule_id;
  2119. }
  2120. function update_auth_rule($db, $new, $rule_id = 0)
  2121. {
  2122. $type = $new['type'];
  2123. $rule = $new['rule'];
  2124. $auth_rules = get_record_sql($db, "SELECT * FROM auth_rules WHERE rule='" . $rule . "' AND type=" . $type . " AND id<>" . $rule_id);
  2125. if (empty($auth_rules)) {
  2126. $rule_id = update_record($db, "auth_rules", "id=" . $rule_id, $new);
  2127. } else {
  2128. LOG_WARNING($db, "Create auto rule id: " . $rule_id . " rule: " . $rule . " type: " . $type . " failed! Already exists at user_id: " . $auth_rules['user_id']);
  2129. $rule_id = 0;
  2130. }
  2131. return $rule_id;
  2132. }
  2133. function LOG_INFO($db, $msg, $auth_id = 0)
  2134. {
  2135. if (get_const('log_level') < L_INFO) {
  2136. return;
  2137. }
  2138. write_log($db, $msg, L_INFO, $auth_id);
  2139. }
  2140. function LOG_ERROR($db, $msg, $auth_id = 0)
  2141. {
  2142. if (get_const('log_level') < L_ERROR) {
  2143. return;
  2144. }
  2145. email(L_ERROR, $msg);
  2146. write_log($db, $msg, L_ERROR, $auth_id);
  2147. }
  2148. function LOG_VERBOSE($db, $msg, $auth_id = 0)
  2149. {
  2150. if (get_const('log_level') < L_VERBOSE) {
  2151. return;
  2152. }
  2153. write_log($db, $msg, L_VERBOSE, $auth_id);
  2154. }
  2155. function LOG_WARNING($db, $msg, $auth_id = 0)
  2156. {
  2157. if (get_const('log_level') < L_WARNING) {
  2158. return;
  2159. }
  2160. email(L_WARNING, $msg);
  2161. write_log($db, $msg, L_WARNING, $auth_id);
  2162. }
  2163. function LOG_DEBUG($db, $msg, $auth_id = 0)
  2164. {
  2165. if (!empty(get_const('debug')) and get_const('debug')) {
  2166. write_log($db, $msg, L_DEBUG, $auth_id);
  2167. }
  2168. }
  2169. function truncateByWords($string, $length = 100)
  2170. {
  2171. if (strlen($string) <= $length) {
  2172. return $string;
  2173. }
  2174. $wrapped = wordwrap($string, $length);
  2175. $shortened = substr($wrapped, 0, strpos($wrapped, "\n"));
  2176. return $shortened;
  2177. }
  2178. function get_first_line($msg)
  2179. {
  2180. if (empty($msg)) {
  2181. return;
  2182. }
  2183. preg_match('/(.*)(\n|\<br\>)/', $msg, $matches);
  2184. if (!empty($matches[1])) {
  2185. return $matches[1];
  2186. }
  2187. return truncateByWords($msg, 80);
  2188. }
  2189. function email($level = L_INFO, $msg = '') {
  2190. // Проверка констант и уровня
  2191. if (!get_const('send_email') || !in_array($level, [L_WARNING, L_ERROR], true)) {
  2192. return;
  2193. }
  2194. // Безопасное получение данных сессии
  2195. $currentIp = filter_var($_SESSION['ip'] ?? '127.0.0.1', FILTER_VALIDATE_IP) ?: '127.0.0.1';
  2196. $currentLogin = htmlspecialchars($_SESSION['login'] ?? 'http', ENT_QUOTES, 'UTF-8');
  2197. // Обработка сообщения
  2198. $subjectPrefix = ($level === L_WARNING) ? "WARN: " : "ERROR: ";
  2199. $subject = $subjectPrefix . htmlspecialchars(get_first_line($msg), ENT_QUOTES, 'UTF-8') . "...";
  2200. $messageType = ($level === L_WARNING) ? 'WARNING' : 'ERROR';
  2201. // Формирование HTML-сообщения с экранированием
  2202. $safeMsg = nl2br(htmlspecialchars($msg, ENT_QUOTES, 'UTF-8'));
  2203. $htmlMessage = "<html>
  2204. <body>
  2205. <h1>$messageType!</h1>
  2206. <p>Manager: $currentLogin</p>
  2207. <p>From: $currentIp</p>
  2208. <div>$safeMsg</div>
  2209. </body>
  2210. </html>";
  2211. // Заголовки письма
  2212. $senderEmail = filter_var(get_const('sender_email'), FILTER_VALIDATE_EMAIL);
  2213. if (!$senderEmail) {
  2214. error_log("Invalid sender email address");
  2215. return;
  2216. }
  2217. $boundary = md5(uniqid(time(), true));
  2218. $headers = [
  2219. 'From' => $senderEmail,
  2220. 'Reply-To' => $senderEmail,
  2221. 'X-Mailer' => 'PHP',
  2222. 'MIME-Version' => '1.0',
  2223. 'Content-Type' => 'multipart/mixed; boundary=' . $boundary,
  2224. 'Content-Transfer-Encoding' => 'base64'
  2225. ];
  2226. // Формирование тела письма
  2227. $message = "--$boundary\r\n" .
  2228. "Content-Type: text/html; charset=UTF-8\r\n" .
  2229. "Content-Transfer-Encoding: base64\r\n\r\n" .
  2230. chunk_split(base64_encode($htmlMessage)) . "\r\n" .
  2231. "--$boundary--";
  2232. // Отправка письма
  2233. $adminEmail = filter_var(get_const('admin_email'), FILTER_VALIDATE_EMAIL);
  2234. if ($adminEmail) {
  2235. if (!mail($adminEmail, $subject, $message, $headers)) {
  2236. error_log("Failed to send email to $adminEmail");
  2237. }
  2238. } else {
  2239. error_log("Invalid admin email address");
  2240. }
  2241. }
  2242. function write_log($db, $msg, $level = L_INFO, $auth_id = 0)
  2243. {
  2244. // Безопасное получение данных сессии
  2245. $currentIp = filter_var($_SESSION['ip'] ?? '127.0.0.1', FILTER_VALIDATE_IP) ?: '127.0.0.1';
  2246. $currentLogin = htmlspecialchars($_SESSION['login'] ?? 'http', ENT_QUOTES, 'UTF-8');
  2247. if (!isset($msg)) { return; }
  2248. $stmt = mysqli_prepare($db, "INSERT INTO worklog(customer, message, level, auth_id, ip) VALUES (?, ?, ?, ?, ?)");
  2249. mysqli_stmt_bind_param($stmt, 'ssiis', $currentLogin, $msg, $level, $auth_id, $currentIp);
  2250. mysqli_stmt_execute($stmt);
  2251. mysqli_stmt_close($stmt);
  2252. }
  2253. function print_year_select($year_name, $year)
  2254. {
  2255. print "<select id=\"$year_name\" name=\"$year_name\" >\n";
  2256. for ($i = $year - 10; $i <= $year + 10; $i++) {
  2257. print_select_item($i, $i, $year);
  2258. }
  2259. print "</select>\n";
  2260. }
  2261. function print_date_select($dd, $mm, $yy)
  2262. {
  2263. if ($dd >= 1) {
  2264. print "<b>День</b>\n";
  2265. print "<select id=\"day\" name=\"day\" >\n";
  2266. for ($i = 1; $i <= 31; $i++) {
  2267. print_select_item($i, $i, $dd);
  2268. }
  2269. print "</select>\n";
  2270. }
  2271. if ($mm >= 1) {
  2272. print "<b>Месяц</b>\n";
  2273. print "<select id=\"month\" name=\"month\" >\n";
  2274. for ($i = 1; $i <= 12; $i++) {
  2275. $tmp_date = DateTimeImmutable::createFromFormat('U', strtotime("$i/01/$yy"));
  2276. $month_name = $tmp_date->format('F');
  2277. print_select_item($month_name, $i, $mm);
  2278. }
  2279. print "</select>\n";
  2280. }
  2281. print "<b>Год</b>\n";
  2282. print_year_select('year', $yy);
  2283. }
  2284. function print_date_select2($dd, $mm, $yy)
  2285. {
  2286. if ($dd >= 1) {
  2287. print "<b>День</b>\n";
  2288. print "<select id=\"day2\" name=\"day2\" >\n";
  2289. for ($i = 1; $i <= 31; $i++) {
  2290. print_select_item($i, $i, $dd);
  2291. }
  2292. print "</select>\n";
  2293. }
  2294. if ($mm >= 1) {
  2295. print "<b>Месяц</b>\n";
  2296. print "<select id=\"month2\" name=\"month2\" >\n";
  2297. for ($i = 1; $i <= 12; $i++) {
  2298. $tmp_date = DateTimeImmutable::createFromFormat('U', strtotime("$i/01/$yy"));
  2299. $month_name = $tmp_date->format('F');
  2300. print_select_item($month_name, $i, $mm);
  2301. }
  2302. print "</select>\n";
  2303. }
  2304. print "<b>Год</b>\n";
  2305. print_year_select('year2', $yy);
  2306. }
  2307. function is_up($ip)
  2308. {
  2309. if (!isset($ip) or strlen($ip) == 0) {
  2310. return false;
  2311. }
  2312. exec(sprintf('ping -i .3 -c 1 -W 5 %s', escapeshellarg($ip)), $res, $rval);
  2313. return $rval == 0;
  2314. }
  2315. function apply_device_lock($db, $device_id, $iteration = 0)
  2316. {
  2317. $iteration++;
  2318. if ($iteration > 2) {
  2319. return false;
  2320. }
  2321. $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');
  2322. if (empty($dev) or empty($dev['u_locked_timestamp'])) {
  2323. LOG_DEBUG($db, "Snmp discovery lock not found. Set and discovery.");
  2324. return set_lock_discovery($db, $device_id);
  2325. }
  2326. //wait for discovery
  2327. $now = time();
  2328. $wait_time = ($dev['u_locked_timestamp'] + SNMP_LOCK_TIMEOUT) - $now;
  2329. LOG_DEBUG($db, "Check snmp lock for device id: " . $device_id . ". Lock timestamp: " . $dev['u_locked_timestamp'] . ", now: " . $now);
  2330. if ($wait_time < 0) {
  2331. LOG_DEBUG($db, "The lock is already expired. Set new lock.");
  2332. return set_lock_discovery($db, $device_id);
  2333. }
  2334. LOG_VERBOSE($db, "Snmp discovery lock for device id: $device_id found! Need wait " . $wait_time . " sec.");
  2335. sleep($wait_time);
  2336. LOG_VERBOSE($db, "Try set new lock and continue discovery for device id:" . $device_id);
  2337. return apply_device_lock($db, $device_id, $iteration);
  2338. }
  2339. function set_lock_discovery($db, $device_id)
  2340. {
  2341. $new['discovery_locked'] = 1;
  2342. $new['locked_timestamp'] = GetNowTimeString();
  2343. if (update_record($db, 'devices', 'id=' . $device_id, $new)) {
  2344. return true;
  2345. }
  2346. return false;
  2347. }
  2348. function unset_lock_discovery($db, $device_id)
  2349. {
  2350. $new['discovery_locked'] = 0;
  2351. $new['locked_timestamp'] = GetNowTimeString();
  2352. if (update_record($db, 'devices', 'id=' . $device_id, $new)) {
  2353. return true;
  2354. }
  2355. return false;
  2356. }
  2357. function set_port_for_group($db, $group_id, $place_id, $state)
  2358. {
  2359. $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;
  2360. $auth_list = mysqli_query($db, $authSQL);
  2361. LOG_VERBOSE($db, 'Mass port state change started!');
  2362. // get auth list for group
  2363. while (list($a_id, $a_name, $a_ip) = mysqli_fetch_array($auth_list)) {
  2364. // get device and port for auth
  2365. if ($place_id == 0) {
  2366. $place_filter = '';
  2367. } else {
  2368. $place_filter = 'D.building_id=' . $place_id . ' and ';
  2369. }
  2370. $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';
  2371. $dev_info = mysqli_query($db, $devSQL);
  2372. list($d_id, $d_name, $d_vendor_id, $d_model, $d_ip, $d_port, $d_snmp_index) = mysqli_fetch_array($dev_info);
  2373. if (!isset($d_id)) {
  2374. continue;
  2375. }
  2376. $device=get_record($db,'devices',"id=".$d_id);
  2377. $snmp = getSnmpAccess($device);
  2378. if ($state == 1) {
  2379. $mode = 'enable';
  2380. run_sql($db, "Update User_auth set nagios_handler='restart-port' WHERE id=$a_id and nagios_handler='manual-mode'");
  2381. } else {
  2382. $mode = 'disable';
  2383. run_sql($db, "Update User_auth set nagios_handler='manual-mode' WHERE id=$a_id and nagios_handler='restart-port'");
  2384. }
  2385. LOG_INFO($db, "At device $d_name [$d_ip] $mode port $d_port for auth_id: $a_id ($a_ip [$a_name])");
  2386. set_port_state($d_vendor_id, $d_snmp_index, $d_ip, $snmp, $state);
  2387. set_port_poe_state($d_vendor_id, $d_port, $d_snmp_index, $d_ip, $snmp, $state);
  2388. }
  2389. LOG_VERBOSE($db, 'Mass port state change stopped.');
  2390. }
  2391. function get_vendor($db, $mac)
  2392. {
  2393. $mac = mac_dotted($mac);
  2394. $mac5 = substr($mac, 0, 14);
  2395. $mac4 = substr($mac, 0, 11);
  2396. $mac3 = substr($mac, 0, 8);
  2397. $vendor = get_record_sql($db, 'SELECT companyName,companyAddress FROM mac_vendors WHERE oui="' . $mac . '"');
  2398. if (empty($vendor)) {
  2399. $vendor = get_record_sql($db, 'SELECT companyName,companyAddress FROM mac_vendors WHERE oui="' . $mac5 . '"');
  2400. }
  2401. if (empty($vendor)) {
  2402. $vendor = get_record_sql($db, 'SELECT companyName,companyAddress FROM mac_vendors WHERE oui="' . $mac4 . '"');
  2403. }
  2404. if (empty($vendor)) {
  2405. $vendor = get_record_sql($db, 'SELECT companyName,companyAddress FROM mac_vendors WHERE oui="' . $mac3 . '"');
  2406. }
  2407. $result = '';
  2408. if (!empty($vendor)) {
  2409. $result = $vendor['companyName'];
  2410. if (!empty($vendor['companyAddress'])) {
  2411. $result = $vendor['companyAddress'];
  2412. }
  2413. }
  2414. return $result;
  2415. }
  2416. function strHexToBin($number)
  2417. {
  2418. $result = '';
  2419. for ($i = 0; $i < strlen($number); $i++) {
  2420. $conv = base_convert($number[$i], 16, 2);
  2421. $result .= str_pad($conv, 4, '0', STR_PAD_LEFT);
  2422. }
  2423. return $result;
  2424. }
  2425. function dec_to_hex($mac)
  2426. {
  2427. if (!isset($mac)) {
  2428. return;
  2429. }
  2430. $mac_array = explode('.', $mac);
  2431. for ($i = 0; $i < count($mac_array); $i++) {
  2432. $hex_i = dechex($mac_array[$i]);
  2433. if (strlen($hex_i) == 1) {
  2434. $hex_i = "0" . $hex_i;
  2435. }
  2436. $mac_array[$i] = $hex_i;
  2437. }
  2438. $hex_mac = implode(':', $mac_array);
  2439. return $hex_mac;
  2440. }
  2441. function mac_simplify($mac)
  2442. {
  2443. if (!isset($mac)) {
  2444. return;
  2445. }
  2446. $mac = strtolower(trim($mac));
  2447. $mac = preg_replace('/(\.|:|-)/', '', $mac);
  2448. return $mac;
  2449. }
  2450. function mac_dotted($mac)
  2451. {
  2452. if (!isset($mac)) {
  2453. return;
  2454. }
  2455. $mac = mac_simplify($mac);
  2456. $mac = preg_replace('/(\S{2})(\S{2})?(\S{2})?(\S{2})?(\S{2})?(\S{2})?/', '$1:$2:$3:$4:$5:$6', $mac);
  2457. $mac = preg_replace('/\:+$/', '', $mac, 5);
  2458. return $mac;
  2459. }
  2460. function unbind_ports($db, $device_id)
  2461. {
  2462. $target = mysqli_query($db, "SELECT U.target_port_id,U.id FROM device_ports U WHERE U.device_id=$device_id");
  2463. while (list($target_id, $id) = mysqli_fetch_array($target)) {
  2464. run_sql($db, "UPDATE device_ports SET target_port_id=0 WHERE target_port_id=" . $id);
  2465. run_sql($db, "UPDATE device_ports SET target_port_id=0 WHERE id=" . $id);
  2466. }
  2467. }
  2468. function bind_ports($db, $port_id, $target_id)
  2469. {
  2470. $old_target = mysqli_query($db, "SELECT U.target_port_id FROM device_ports U WHERE U.id=$port_id");
  2471. list($old_target_id) = mysqli_fetch_array($old_target);
  2472. // unbind current connection
  2473. $new['target_port_id'] = 0;
  2474. update_record($db, "device_ports", "id='$port_id'", $new);
  2475. if (isset($old_target_id)) {
  2476. update_record($db, "device_ports", "id='$old_target_id'", $new);
  2477. }
  2478. // new link
  2479. if (isset($target_id) and $target_id > 0) {
  2480. $new['target_port_id'] = $target_id;
  2481. update_record($db, "device_ports", "id='$port_id'", $new);
  2482. $new['target_port_id'] = $port_id;
  2483. update_record($db, "device_ports", "id='$target_id'", $new);
  2484. }
  2485. }
  2486. function expand_device_name($db, $name)
  2487. {
  2488. $device_id = get_device_id($db, $name);
  2489. $result = $name;
  2490. if (isset($device_id) and $device_id > 0) {
  2491. $result = '<a href=/admin/devices/editdevice.php?id=' . $device_id . '>' . $name . '</a>';
  2492. }
  2493. return $result;
  2494. }
  2495. function expand_mac($db, $msg)
  2496. {
  2497. if (!isset($msg)) {
  2498. return;
  2499. }
  2500. $mac = mac_dotted($msg);
  2501. $vendor_info = get_vendor($db, $mac);
  2502. if (!empty($vendor_info)) {
  2503. // $result = '<p title="' . $vendor_info . '"><a href=/admin/logs/mac.php?mac=' . $mac . '>' . $mac . '</a></p>';
  2504. $result = '<a href=/admin/logs/mac.php?mac=' . $mac . '><p title="' . $vendor_info . '">'. $mac . '</p></a>';
  2505. } else {
  2506. $result = '<a href=/admin/logs/mac.php?mac=' . $mac . '>' . $mac . '</a>';
  2507. }
  2508. return $result;
  2509. }
  2510. function expand_log_str($db, $msg)
  2511. {
  2512. if (!isset($msg)) {
  2513. return;
  2514. }
  2515. $auth_pattern = '/(auth_id:|auth|auth id:|auth id)\s+(\d+)\s+/i';
  2516. $auth_replace = '<a href=/admin/users/editauth.php?id=${2}>auth_id:${2}</a> ';
  2517. $result = preg_replace($auth_pattern, $auth_replace, $msg);
  2518. $user_pattern = '/(user_id:|user|user id:|user id)\s+(\d+)\s+/i';
  2519. $user_replace = '<a href=/admin/users/edituser.php?id=${2}>user_id:${2}</a> ';
  2520. $result = preg_replace($user_pattern, $user_replace, $result);
  2521. $mac_pattern = '/\s+\[(\w{12})\]\s+/i';
  2522. preg_match($mac_pattern, $result, $matches);
  2523. if (isset($matches[1])) {
  2524. $mac = $matches[1];
  2525. $mac = mac_dotted($mac);
  2526. // $vendor_info = get_vendor($db,$mac);
  2527. // $mac_replace = ' <p title="'.$vendor_info.'"><a href=/admin/logs/mac.php?mac='.$mac.'>'.$mac.'</a></p>';
  2528. $mac_replace = ' <a href=/admin/logs/mac.php?mac=' . $mac . '>' . $mac . '</a> ';
  2529. $result = preg_replace($mac_pattern, $mac_replace, $result);
  2530. }
  2531. $mac_pattern = '/\s+mac:\s+([\w\:]{17})$/i';
  2532. preg_match($mac_pattern, $result, $matches);
  2533. if (isset($matches[1])) {
  2534. $mac = $matches[1];
  2535. $mac = mac_dotted($mac);
  2536. // $vendor_info = get_vendor($db,$mac);
  2537. // $mac_replace = ' mac: <p title="'.$vendor_info.'"><a href=/admin/logs/mac.php?mac='.$mac.'>'.$mac.'</a></p>';
  2538. $mac_replace = ' mac: <a href=/admin/logs/mac.php?mac=' . $mac . '>' . $mac . '</a> ';
  2539. $result = preg_replace($mac_pattern, $mac_replace, $result);
  2540. }
  2541. $device_pattern = '/at device\s+([\w\.\-]+)/i';
  2542. preg_match($device_pattern, $result, $matches);
  2543. if (isset($matches[1])) {
  2544. $device_name = $matches[1];
  2545. $device_id = get_device_id($db, $device_name);
  2546. if (isset($device_id) and $device_id > 0) {
  2547. $device_replace = 'at device <a href=/admin/devices/editdevice.php?id=' . $device_id . '>${1}</a> ';
  2548. $result = preg_replace($device_pattern, $device_replace, $result);
  2549. }
  2550. }
  2551. $device_pattern = '/(device_id:|device id:|device id|device_id)\s+(\d+)\s+/i';
  2552. $device_replace = 'device_id: <a href=/admin/devices/editdevice.php?id=${2}>${2}</a> ';
  2553. $result = preg_replace($device_pattern, $device_replace, $result);
  2554. $ip_pattern = '/\s+ip\:\s+(\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})\s+/i';
  2555. preg_match($ip_pattern, $result, $matches);
  2556. if (isset($matches[1])) {
  2557. $ip = $matches[1];
  2558. $auth_id = get_auth_by_ip($db, $ip);
  2559. if (isset($auth_id) and $auth_id > 0) {
  2560. $auth_replace = ' ip: <a href=/admin/users/editauth.php?id=' . $auth_id . '>${1}</a> ';
  2561. $result = preg_replace($ip_pattern, $auth_replace, $result);
  2562. }
  2563. }
  2564. return $result;
  2565. }
  2566. function is_auth_bind_changed($db, $id, $ip, $mac)
  2567. {
  2568. $old_sql = "SELECT ip,mac FROM User_auth WHERE id=$id";
  2569. $old_record = get_record_sql($db, $old_sql);
  2570. if (empty($old_record["ip"]) or empty($old_record["mac"])) {
  2571. return 0;
  2572. }
  2573. if ($old_record["ip"] !== $ip or $old_record["mac"] !== $mac) {
  2574. LOG_VERBOSE($db, "Changed ip or mac for auth record!");
  2575. return 1;
  2576. }
  2577. return 0;
  2578. }
  2579. function copy_auth($db, $id, $new_auth)
  2580. {
  2581. $old_record = get_record_sql($db, "SELECT * FROM User_auth WHERE id=$id");
  2582. delete_record($db, "User_auth", "id=" . $id);
  2583. $new_auth["user_id"] = $old_record["user_id"];
  2584. $new_auth["changed"] = 1;
  2585. $changed_time = GetNowTimeString();
  2586. $new_auth["changed_time"] = $changed_time;
  2587. $new_id = insert_record($db, "User_auth", $new_auth);
  2588. LOG_VERBOSE($db, "Old record with id: $id deleted. Created new auth record for new ip+mac id: $new_id!");
  2589. return $new_id;
  2590. }
  2591. function get_dns_name($db, $id)
  2592. {
  2593. $auth_record = get_record_sql($db, "SELECT dns_name FROM User_auth WHERE id=" . $id);
  2594. if (!empty($auth_record) and !empty($auth_record['dns_name'])) {
  2595. return $auth_record['dns_name'];
  2596. }
  2597. return '';
  2598. }
  2599. function get_cacti_graph($host_ip, $port_index)
  2600. {
  2601. if (empty(get_const('cacti_url'))) {
  2602. return;
  2603. }
  2604. if (CACTI_DB_HOST == null or CACTI_DB_USER == null or CACTI_DB_PASS == null or CACTI_DB_NAME == null) {
  2605. return;
  2606. }
  2607. if (empty(CACTI_DB_HOST) or empty(CACTI_DB_USER) or empty(CACTI_DB_PASS) or empty(CACTI_DB_NAME)) {
  2608. return;
  2609. }
  2610. $cacti_db_link = new_connection(CACTI_DB_HOST, CACTI_DB_USER, CACTI_DB_PASS, CACTI_DB_NAME);
  2611. if (!$cacti_db_link) {
  2612. return FALSE;
  2613. }
  2614. $host_sql = 'SELECT * FROM host WHERE hostname="' . $host_ip . '"';
  2615. $cacti_host = get_record_sql($cacti_db_link, $host_sql);
  2616. $host_id = $cacti_host["id"];
  2617. if (empty($host_id)) {
  2618. return;
  2619. }
  2620. $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';
  2621. $cacti_graph = get_record_sql($cacti_db_link, $graph_sql);
  2622. $graph_id = $cacti_graph["id"];
  2623. if (empty($graph_id)) {
  2624. return;
  2625. }
  2626. $result = get_const('cacti_url') . "/graph_image.php?local_graph_id=" . $graph_id;
  2627. return $result;
  2628. }
  2629. function print_select_item($description, $value, $current)
  2630. {
  2631. if ((string)$value === (string)$current) {
  2632. print "<option value='" . $value . "' selected>$description</option>";
  2633. } else {
  2634. print "<option value='" . $value . "'>$description</option>";
  2635. }
  2636. }
  2637. function print_select_simple($description, $value)
  2638. {
  2639. print "<option value=$value>$description</option>";
  2640. }
  2641. function print_select_item_ext($description, $value, $current, $disabled)
  2642. {
  2643. if ((string)$value === (string)$current) {
  2644. print "<option value=$value selected>$description</option>";
  2645. } else {
  2646. if (!$disabled) {
  2647. print "<option value=$value>$description</option>";
  2648. } else {
  2649. print "<option disabled value=$value>$description</option>";
  2650. }
  2651. }
  2652. }
  2653. function print_row_at_pages($name, $value)
  2654. {
  2655. print "<select id='" . $name . "' name='" . $name . "'>\n";
  2656. print_select_item(WEB_select_item_more, pow(10, 10), $value);
  2657. print_select_item('25', 25, $value);
  2658. print_select_item('50', 50, $value);
  2659. print_select_item('100', 100, $value);
  2660. print_select_item('200', 200, $value);
  2661. print_select_item('500', 500, $value);
  2662. print_select_item('1000', 1000, $value);
  2663. print_select_item('2000', 2000, $value);
  2664. print "</select>\n";
  2665. }
  2666. function print_navigation($url, $page, $displayed, $count_records, $total)
  2667. {
  2668. if ($total <= 1) {
  2669. print "<div align=left class=records >";
  2670. print "| Total records: $count_records";
  2671. print "</div>";
  2672. return;
  2673. }
  2674. $v_char = "?";
  2675. if (preg_match('/\.php\?/', $url)) {
  2676. $v_char = "&";
  2677. }
  2678. //две назад
  2679. print "<div align=left class=records >";
  2680. if (($page - 2) > 0) :
  2681. $pagetwoleft = "<a class='first_page_link' href=" . $url . $v_char . "page=" . ($page - 2) . ">" . ($page - 2) . "</a> ";
  2682. else :
  2683. $pagetwoleft = null;
  2684. endif;
  2685. //одна назад
  2686. if (($page - 1) > 0) :
  2687. $pageoneleft = "<a class='first_page_link' href=" . $url . $v_char . "page=" . ($page - 1) . ">" . ($page - 1) . "</a> ";
  2688. $pagetemp = ($page - 1);
  2689. else :
  2690. $pageoneleft = null;
  2691. $pagetemp = null;
  2692. endif;
  2693. //две вперед
  2694. if (($page + 2) <= $total) :
  2695. $pagetworight = " <a class='first_page_link' href=" . $url . $v_char . "page=" . ($page + 2) . ">" . ($page + 2) . "</a>";
  2696. else :
  2697. $pagetworight = null;
  2698. endif;
  2699. //одна вперед
  2700. if (($page + 1) <= $total) :
  2701. $pageoneright = " <a class='first_page_link' href=" . $url . $v_char . "page=" . ($page + 1) . ">" . ($page + 1) . "</a>";
  2702. $pagetemp2 = ($page + 1);
  2703. else :
  2704. $pageoneright = null;
  2705. $pagetemp2 = null;
  2706. endif;
  2707. // в начало
  2708. if ($page != 1 && $pagetemp != 1 && $pagetemp != 2) :
  2709. $pagerevp = "<a href=" . $url . $v_char . "page=1 class='first_page_link' title='В начало'><<</a> ";
  2710. else :
  2711. $pagerevp = null;
  2712. endif;
  2713. //в конец (последняя)
  2714. if ($page != $total && $pagetemp2 != ($total - 1) && $pagetemp2 != $total) :
  2715. $nextp = " ... <a href=" . $url . $v_char . "page=" . $total . " class='first_page_link'>$total</a>";
  2716. else :
  2717. $nextp = null;
  2718. endif;
  2719. print $pagerevp . $pagetwoleft . $pageoneleft . '<span class="num_page_not_link"><b>' . $page . '</b></span>' . $pageoneright . $pagetworight . $nextp;
  2720. print " | Total records: $count_records";
  2721. print "</div>";
  2722. }
  2723. function get_option($db, $option_id)
  2724. {
  2725. $option = get_record($db, "config", "option_id=" . $option_id);
  2726. if (empty($option) or empty($option['value'])) {
  2727. $default = get_record($db, "config_options", "id=$option_id");
  2728. return $default['default_value'];
  2729. }
  2730. return $option['value'];
  2731. }
  2732. function is_option($db, $option_id)
  2733. {
  2734. $option = get_record($db, "config", "option_id=" . $option_id);
  2735. if (empty($option) or empty($option['value'])) {
  2736. return;
  2737. }
  2738. return 1;
  2739. }
  2740. function set_option($db, $option_id, $value)
  2741. {
  2742. $option['value'] = $value;
  2743. update_record($db, 'config', "option_id=$option_id", $option);
  2744. }
  2745. function is_subnet_aton($subnet, $ip)
  2746. {
  2747. if (!isset($subnet)) {
  2748. return 0;
  2749. }
  2750. if (!isset($ip)) {
  2751. return 0;
  2752. }
  2753. $range = cidrToRange($subnet);
  2754. if ($ip >= ip2long($range[0]) and $ip <= ip2long($range[1])) {
  2755. return 1;
  2756. }
  2757. return 0;
  2758. }
  2759. function get_new_user_id($db, $ip, $mac, $hostname)
  2760. {
  2761. $result['ip'] = $ip;
  2762. $result['mac'] = mac_dotted($mac);
  2763. $result['hostname'] = $hostname;
  2764. $result['user_id'] = NULL;
  2765. $result['ou_id'] = NULL;
  2766. $ip_aton = ip2long($ip);
  2767. if (is_hotspot($db, $ip)) {
  2768. $result['ou_id'] = get_const('default_hotspot_ou_id');
  2769. return $result;
  2770. }
  2771. //personal user rules
  2772. //ip
  2773. if (!empty($ip)) {
  2774. $t_rules = get_records_sql($db, "SELECT * FROM auth_rules WHERE type=1 and LENGTH(rule)>0 AND user_id IS NOT NULL");
  2775. foreach ($t_rules as $row) {
  2776. if (!empty($row['rule']) and is_subnet_aton($row['rule'], $ip_aton)) {
  2777. $result['user_id'] = $row['user_id'];
  2778. return $result;
  2779. }
  2780. }
  2781. }
  2782. //mac
  2783. if (!empty($mac)) {
  2784. $mac_rules = get_records_sql($db, "SELECT * FROM auth_rules WHERE type=2 AND LENGTH(rule)>0 AND user_id IS NOT NULL");
  2785. foreach ($mac_rules as $row) {
  2786. $pattern = '/' . mac_simplify($row['rule']) . '/';
  2787. if (!empty($row['rule']) and preg_match($pattern, mac_simplify($mac))) {
  2788. $result['user_id'] = $row['user_id'];
  2789. return $result;
  2790. }
  2791. }
  2792. }
  2793. //hostname
  2794. if (!empty($hostname)) {
  2795. $mac_rules = get_records_sql($db, "SELECT * FROM auth_rules WHERE type=3 AND LENGTH(rule)>0 AND user_id IS NOT NULL");
  2796. foreach ($mac_rules as $row) {
  2797. if (!empty($row['rule']) and preg_match($row['rule'], $hostname)) {
  2798. $result['user_id'] = $row['user_id'];
  2799. return $result;
  2800. }
  2801. }
  2802. }
  2803. //ou rules
  2804. //ip
  2805. if (!empty($ip)) {
  2806. $t_rules = get_records_sql($db, "SELECT * FROM auth_rules WHERE type=1 and LENGTH(rule)>0 AND ou_id IS NOT NULL");
  2807. foreach ($t_rules as $row) {
  2808. if (!empty($row['rule']) and is_subnet_aton($row['rule'], $ip_aton)) {
  2809. $result['ou_id'] = $row['ou_id'];
  2810. return $result;
  2811. }
  2812. }
  2813. }
  2814. //mac
  2815. if (!empty($mac)) {
  2816. $mac_rules = get_records_sql($db, "SELECT * FROM auth_rules WHERE type=2 AND LENGTH(rule)>0 AND ou_id IS NOT NULL");
  2817. foreach ($mac_rules as $row) {
  2818. $pattern = '/' . mac_simplify($row['rule']) . '/';
  2819. if (!empty($row['rule']) and preg_match($pattern, mac_simplify($mac))) {
  2820. $result['ou_id'] = $row['ou_id'];
  2821. return $result;
  2822. }
  2823. }
  2824. }
  2825. //hostname
  2826. if (!empty($hostname)) {
  2827. $mac_rules = get_records_sql($db, "SELECT * FROM auth_rules WHERE type=3 AND LENGTH(rule)>0 AND ou_id IS NOT NULL");
  2828. foreach ($mac_rules as $row) {
  2829. if (!empty($row['rule']) and preg_match($row['rule'], $hostname)) {
  2830. $result['ou_id'] = $row['ou_id'];
  2831. return $result;
  2832. }
  2833. }
  2834. }
  2835. if (empty($result['ou_id'])) {
  2836. $result['ou_id'] = get_const('default_user_ou_id');
  2837. }
  2838. return $result;
  2839. }
  2840. function get_subnet_range($db, $subnet_id)
  2841. {
  2842. if (empty($subnet_id)) {
  2843. return;
  2844. }
  2845. $t_option = get_record_sql($db, "SELECT ip_int_start,ip_int_stop FROM `subnets` WHERE id=$subnet_id");
  2846. if (!isset($t_option['ip_int_start'])) {
  2847. $t_option['ip_int_start'] = 0;
  2848. }
  2849. if (!isset($t_option['ip_int_stop'])) {
  2850. $t_option['ip_int_stop'] = 0;
  2851. }
  2852. $subnet['start'] = $t_option['ip_int_start'];
  2853. $subnet['stop'] = $t_option['ip_int_stop'];
  2854. return $subnet;
  2855. }
  2856. function int_between($value, $start, $end)
  2857. {
  2858. return in_array($value, range($start, $end));
  2859. }
  2860. function is_gray_network($ip)
  2861. {
  2862. if (empty($ip)) {
  2863. return 0;
  2864. }
  2865. $ip_aton = ip2long($ip);
  2866. $gray_nets = array('10.0.0.0/8', '192.168.0.0/16', '172.16.0.0/12', '100.64.0.0/10');
  2867. foreach ($gray_nets as &$net) {
  2868. $net_cidr = cidrToRange($net);
  2869. if (int_between($ip_aton, ip2long($net_cidr[0]), ip2long($net_cidr[1]))) {
  2870. return $net;
  2871. }
  2872. }
  2873. return 0;
  2874. }
  2875. function is_hotspot($db, $ip)
  2876. {
  2877. if (!isset($ip)) {
  2878. return 0;
  2879. }
  2880. LOG_DEBUG($db, "Check hotspot network for ip: $ip");
  2881. $ip_aton = ip2long($ip);
  2882. $t_option = mysqli_query($db, "SELECT subnet,ip_int_start,ip_int_stop FROM `subnets` WHERE hotspot=1");
  2883. while (list($f_net, $f_start, $f_stop) = mysqli_fetch_array($t_option)) {
  2884. if ($ip_aton >= $f_start and $ip_aton <= $f_stop) {
  2885. LOG_DEBUG($db, "ip: $ip [$ip_aton] found in network $f_net: [" . $f_start . ".." . $f_stop . "]");
  2886. return 1;
  2887. }
  2888. }
  2889. LOG_DEBUG($db, "ip $ip not found in hotspot network!");
  2890. return 0;
  2891. }
  2892. function is_office($db, $ip)
  2893. {
  2894. if (!isset($ip)) {
  2895. return 0;
  2896. }
  2897. LOG_DEBUG($db, "Check office network for ip: $ip");
  2898. $ip_aton = ip2long($ip);
  2899. $t_option = mysqli_query($db, "SELECT subnet,ip_int_start,ip_int_stop FROM `subnets` WHERE office=1");
  2900. while (list($f_net, $f_start, $f_stop) = mysqli_fetch_array($t_option)) {
  2901. if ($ip_aton >= $f_start and $ip_aton <= $f_stop) {
  2902. LOG_DEBUG($db, "ip: $ip [$ip_aton] found in office $f_net: [" . $f_start . ".." . $f_stop . "]");
  2903. return 1;
  2904. }
  2905. }
  2906. LOG_DEBUG($db, "ip $ip not found in office network!");
  2907. return 0;
  2908. }
  2909. function is_our_network($db, $ip)
  2910. {
  2911. if (!isset($ip)) {
  2912. return 0;
  2913. }
  2914. if (is_hotspot($db, $ip)) {
  2915. return 1;
  2916. }
  2917. if (is_office($db, $ip)) {
  2918. return 1;
  2919. }
  2920. return 0;
  2921. }
  2922. function get_const($const_name)
  2923. {
  2924. global $config;
  2925. if (isset($config[$const_name])) {
  2926. return $config[$const_name];
  2927. }
  2928. return NULL;
  2929. }
  2930. function get_eye_version($db)
  2931. {
  2932. $v_table = get_record_sql($db, "SELECT version FROM version");
  2933. if (!empty($v_table)) {
  2934. return $v_table['version'];
  2935. }
  2936. return NULL;
  2937. }
  2938. $config["org_name"] = get_option($db_link, 32);
  2939. $config["version"] = get_eye_version($db_link);
  2940. $config["KB"] = get_option($db_link, 1);
  2941. if ($config["KB"] == 0) {
  2942. $config["KB"] = 1000;
  2943. }
  2944. if ($config["KB"] == 1) {
  2945. $config["KB"] = 1024;
  2946. }
  2947. $config["debug"] = get_option($db_link, 34);
  2948. $config["log_level"] = get_option($db_link, 53);
  2949. if ($config["debug"]) {
  2950. $config["log_level"] = 255;
  2951. }
  2952. $config["send_email"] = get_option($db_link, 51);
  2953. $config["admin_email"] = get_option($db_link, 21);
  2954. $config["sender_email"] = get_option($db_link, 52);
  2955. $config["snmp_default_version"] = get_option($db_link, 9);
  2956. $config["snmp_default_community"] = get_option($db_link, 11);
  2957. $config["auto_mac_rule"] = get_option($db_link, 64);
  2958. $config["cacti_url"] = rtrim(get_option($db_link, 58), '/');
  2959. if (preg_match('/127.0.0.1/', $config["cacti_url"])) {
  2960. $config["cacti_url"] = NULL;
  2961. }
  2962. $config["nagios_url"] = rtrim(get_option($db_link, 57), '/') . '/cgi-bin/';
  2963. if (preg_match('/127.0.0.1/', $config["nagios_url"])) {
  2964. $config["nagios_url"] = NULL;
  2965. }
  2966. $config["torrus_url"] = rtrim(get_option($db_link, 59), '/') . '?nodeid=if//HOST_IP//IF_NAME////inoutbps';
  2967. if (preg_match('/127.0.0.1/', $config["torrus_url"])) {
  2968. $config["torrus_url"] = NULL;
  2969. }
  2970. $config["dns_server"] = get_option($db_link, 3);
  2971. $config["dns_server_type"] = get_option($db_link, 70);
  2972. $ou = get_record_sql($db_link, "SELECT id FROM OU WHERE default_users = 1");
  2973. if (empty($ou)) {
  2974. $config["default_user_ou_id"] = 0;
  2975. } else {
  2976. $config["default_user_ou_id"] = $ou['id'];
  2977. }
  2978. $ou = get_record_sql($db_link, "SELECT id FROM OU WHERE default_hotspot=1");
  2979. if (empty($ou)) {
  2980. $config["default_hotspot_ou_id"] = $config["default_user_ou_id"];
  2981. } else {
  2982. $config["default_hotspot_ou_id"] = $ou['id'];
  2983. }
  2984. $config["init"] = 1;
  2985. clean_dns_cache($db_link);
  2986. //clean_unreferensed_rules($db_link);