common.php 123 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853
  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 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 get_subnet_description($db, $subnet_id)
  457. {
  458. if (empty($subnet_id)) {
  459. return '';
  460. }
  461. $subnet = get_record_sql($db, 'SELECT * FROM subnets WHERE id=' . $subnet_id);
  462. if (empty($subnet)) {
  463. return '';
  464. }
  465. $result = $subnet['subnet'] . '&nbsp(' . $subnet['comment'] . ')';
  466. return $result;
  467. }
  468. function print_add_gw_subnets($db, $device_id, $gs_name)
  469. {
  470. print "<select name=\"$gs_name\" >\n";
  471. $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");
  472. while (list($f_gs_id, $f_gs_name, $f_gs_comment) = mysqli_fetch_array($t_gs)) {
  473. print_select_item($f_gs_name . '(' . $f_gs_comment . ')', $f_gs_id, 0);
  474. }
  475. print "</select>\n";
  476. }
  477. function print_add_dev_interface($db, $device_id, $int_list, $int_name)
  478. {
  479. print "&nbsp<select name=\"$int_name\" >\n";
  480. $t_int = get_records_sql($db, "SELECT * FROM device_l3_interfaces WHERE device_id=" . $device_id);
  481. $int_exists = [];
  482. foreach ($t_int as $interface) {
  483. $int_exists[$interface['snmpin']] = $interface;
  484. }
  485. foreach ($int_list as $interface) {
  486. if (!empty($int_exists[$interface['index']])) {
  487. continue;
  488. }
  489. $value = $interface['name'] . ';' . $interface['index'] . ';' . $interface['type'];
  490. if ($interface['type'] == 1) {
  491. $interface['type'] = WEB_select_item_wan;
  492. }
  493. if ($interface['type'] == 0) {
  494. $interface['type'] = WEB_select_item_lan;
  495. }
  496. $display_str = $interface['name'] . '&nbsp|' . $interface['ip'] . '|' . $interface['type'];
  497. print_select_item($display_str, $value, 0);
  498. }
  499. print "</select>\n";
  500. }
  501. function print_ou_set($db, $ou_name, $ou_value)
  502. {
  503. print "<select name=\"$ou_name\">\n";
  504. $t_ou = mysqli_query($db, "SELECT id,ou_name FROM OU WHERE id>=1 ORDER BY ou_name");
  505. while (list($f_ou_id, $f_ou_name) = mysqli_fetch_array($t_ou)) {
  506. print_select_item($f_ou_name, $f_ou_id, $ou_value);
  507. }
  508. print "</select>\n";
  509. }
  510. function print_subnet_select($db, $subnet_name, $subnet_value)
  511. {
  512. print "<select name=\"$subnet_name\" >\n";
  513. $t_subnet = mysqli_query($db, "SELECT id,subnet FROM subnets ORDER BY ip_int_start");
  514. print_select_item(WEB_select_item_all_ips, 0, $subnet_value);
  515. while (list($f_subnet_id, $f_subnet_name) = mysqli_fetch_array($t_subnet)) {
  516. print_select_item($f_subnet_name, $f_subnet_id, $subnet_value);
  517. }
  518. print "</select>\n";
  519. }
  520. function print_acl_select($db, $acl_name, $acl_value)
  521. {
  522. print "<select name=\"$acl_name\" >\n";
  523. $t_acl = mysqli_query($db, "SELECT id,name FROM acl ORDER BY id");
  524. while (list($f_acl_id, $f_acl_name) = mysqli_fetch_array($t_acl)) {
  525. print_select_item($f_acl_name, $f_acl_id, $acl_value);
  526. }
  527. print "</select>\n";
  528. }
  529. function print_device_ip_select($db, $ip_name, $ip, $user_id)
  530. {
  531. print "<select name=\"$ip_name\">\n";
  532. $auth_list = get_records_sql($db, "SELECT ip FROM User_auth WHERE user_id=$user_id AND deleted=0 ORDER BY ip_int");
  533. foreach ($auth_list as $row) {
  534. print_select_item($row['ip'], $row['ip'], $ip);
  535. }
  536. print "</select>\n";
  537. }
  538. function print_subnet_select_office($db, $subnet_name, $subnet_value)
  539. {
  540. print "<select name=\"$subnet_name\" >\n";
  541. $t_subnet = mysqli_query($db, "SELECT id,subnet FROM subnets WHERE office=1 ORDER BY ip_int_start");
  542. print_select_item(WEB_select_item_all_ips, 0, $subnet_value);
  543. while (list($f_subnet_id, $f_subnet_name) = mysqli_fetch_array($t_subnet)) {
  544. print_select_item($f_subnet_name, $f_subnet_id, $subnet_value);
  545. }
  546. print "</select>\n";
  547. }
  548. function print_subnet_select_office_splitted($db, $subnet_name, $subnet_value)
  549. {
  550. print "<select name=\"$subnet_name\" >\n";
  551. $t_subnet = mysqli_query($db, "SELECT id,subnet,ip_int_start,ip_int_stop FROM subnets WHERE office=1 ORDER BY ip_int_start");
  552. print_select_item(WEB_select_item_all_ips, 0, $subnet_value);
  553. while (list($f_subnet_id, $f_subnet_name, $f_start_ip, $f_stop_ip) = mysqli_fetch_array($t_subnet)) {
  554. print_select_item($f_subnet_name, $f_subnet_name, $subnet_value);
  555. $cidr = cidrToRange($f_subnet_name);
  556. if ($cidr[2][1] < 24) {
  557. while ($f_start_ip <= $f_stop_ip) {
  558. print_select_item("&nbsp&nbsp-&nbsp" . long2ip($f_start_ip) . "/24", long2ip($f_start_ip) . "/24", $subnet_value);
  559. $f_start_ip += 256;
  560. }
  561. }
  562. }
  563. print "</select>\n";
  564. }
  565. function print_loglevel_select($item_name, $value)
  566. {
  567. print "<select name=\"$item_name\">\n";
  568. print_select_item('Error', L_ERROR, $value);
  569. print_select_item('Warning', L_WARNING, $value);
  570. print_select_item('Info', L_INFO, $value);
  571. print_select_item('Verbose', L_VERBOSE, $value);
  572. print_select_item('Debug', L_DEBUG, $value);
  573. print "</select>\n";
  574. }
  575. function reencodeurl($url)
  576. {
  577. $url_arr = explode('?', $url);
  578. $fpage = $url_arr[0];
  579. if (isset($url_arr[1])) {
  580. $params = $url_arr[1];
  581. $params_arr = explode('&', $params);
  582. $new_params = '';
  583. foreach ($params_arr as $row) {
  584. $param = explode('=', $row);
  585. $key = $param[0];
  586. $value = urlencode(urldecode($param[1]));
  587. $new_params .= "&" . $key . "=" . $value;
  588. }
  589. $new_params = preg_replace('/^&/', '', $new_params);
  590. } else {
  591. $new_params = '=';
  592. }
  593. if ($new_params === '=') {
  594. $new_url = $fpage;
  595. } else {
  596. $new_url = $fpage . "?" . $new_params;
  597. }
  598. return $new_url;
  599. }
  600. function print_submenu_url($display_name, $page, $current_page, $last)
  601. {
  602. $url_arr = explode('?', $page);
  603. $fpage = $url_arr[0];
  604. $new_url = reencodeurl($page);
  605. if ($fpage === $current_page) {
  606. print "<b>$display_name</b>";
  607. } else {
  608. print "<a href='" . $new_url . "'> $display_name </a>";
  609. }
  610. if (!isset($last) or $last == 0) {
  611. print " | ";
  612. }
  613. }
  614. function print_submenu_nw($display_name, $page, $current_page, $last)
  615. {
  616. $url_arr = explode('?', $page);
  617. $fpage = $url_arr[0];
  618. $new_url = reencodeurl($page);
  619. if ($fpage === $current_page) {
  620. print "<b>$display_name</b>";
  621. } else {
  622. print '<a href="" onclick="window.open(\'' . $new_url . "', '_tab').focus(); return false;\">" . $display_name . "</a>";
  623. }
  624. if (!isset($last) or $last == 0) {
  625. print " | ";
  626. }
  627. }
  628. function print_url($display_name, $page)
  629. {
  630. print "<a href='" . reencodeurl($page) . "'> $display_name </a>";
  631. }
  632. function print_log_submenu($current_page)
  633. {
  634. print "<div id='submenu'>\n";
  635. print_submenu_url(WEB_submenu_dhcp_log, '/admin/logs/dhcp.php', $current_page, 0);
  636. print_submenu_url(WEB_submenu_work_log, '/admin/logs/index.php', $current_page, 0);
  637. print_submenu_url(WEB_submenu_mac_history, '/admin/logs/mac.php', $current_page, 0);
  638. print_submenu_url(WEB_submenu_ip_history, '/admin/logs/ip.php', $current_page, 0);
  639. print_submenu_url(WEB_submenu_mac_unknown, '/admin/logs/unknown.php', $current_page, 0);
  640. print_submenu_url(WEB_submenu_traffic, '/admin/logs/detaillog.php', $current_page, 0);
  641. print_submenu_url(WEB_submenu_syslog, '/admin/logs/syslog.php', $current_page, 1);
  642. print "</div>\n";
  643. }
  644. function print_control_submenu($current_page)
  645. {
  646. print "<div id='submenu'>\n";
  647. print_submenu_url(WEB_submenu_control, '/admin/customers/control.php', $current_page, 0);
  648. print_submenu_url(WEB_submenu_network, '/admin/customers/index-subnets.php', $current_page, 0);
  649. print_submenu_url(WEB_submenu_network_stats, '/admin/customers/control-subnets-usage.php', $current_page, 0);
  650. print_submenu_url(WEB_submenu_options, '/admin/customers/control-options.php', $current_page, 0);
  651. print_submenu_url(WEB_submenu_customers, '/admin/customers/index.php', $current_page, 0);
  652. print_submenu_url(WEB_submenu_buildings, '/admin/customers/building.php', $current_page, 0);
  653. print_submenu_url(WEB_submenu_device_models, '/admin/customers/devmodels.php', $current_page, 0);
  654. print_submenu_url(WEB_submenu_vendors, '/admin/customers/devvendors.php', $current_page, 1);
  655. print "</div>\n";
  656. }
  657. function print_filters_submenu($current_page)
  658. {
  659. print "<div id='submenu'>\n";
  660. print_submenu_url(WEB_submenu_filter_list, '/admin/filters/index.php', $current_page, 0);
  661. print_submenu_url(WEB_submenu_filter_group, '/admin/filters/groups.php', $current_page, 1);
  662. print "</div>\n";
  663. }
  664. function print_reports_submenu($current_page)
  665. {
  666. print "<div id='submenu'>\n";
  667. print_submenu_url(WEB_submenu_traffic_ip_report, '/admin/reports/index-full.php', $current_page, 0);
  668. print_submenu_url(WEB_submenu_traffic_login_report, '/admin/reports/index.php', $current_page, 0);
  669. print_submenu_url(WEB_submenu_traffic_wan_report, '/admin/reports/wan.php', $current_page, 1);
  670. print "</div>\n";
  671. }
  672. function print_trafdetail_submenu($current_page, $params, $description)
  673. {
  674. print "<div id='subsubmenu'>\n";
  675. print "$description\n";
  676. print_submenu_url(WEB_submenu_traffic_top10, '/admin/reports/userdaydetail.php' . "?$params", $current_page, 0);
  677. print_submenu_url(WEB_submenu_detail_log, '/admin/reports/userdaydetaillog.php' . "?$params", $current_page, 1);
  678. print "</div>\n";
  679. }
  680. function print_device_submenu($current_page)
  681. {
  682. print "<div id='submenu'>\n";
  683. print_submenu_url(WEB_submenu_net_devices, '/admin/devices/index.php', $current_page, 0);
  684. print_submenu_url(WEB_submenu_passive_net_devices, '/admin/devices/index-passive.php', $current_page, 0);
  685. print_submenu_url(WEB_submenu_hierarchy, '/admin/devices/index-tree.php', $current_page, 0);
  686. print_submenu_url(WEB_submenu_ports_vlan, '/admin/devices/portsbyvlan.php', $current_page, 1);
  687. print "</div>\n";
  688. }
  689. function open_window_url($url)
  690. {
  691. return "window.open('" . $url . "', '_blank');";
  692. }
  693. function print_editdevice_submenu($current_page, $id, $dev_type, $dev_name = NULL)
  694. {
  695. print "<div id='subsubmenu'>\n";
  696. $dev_id = '';
  697. if (isset($id)) {
  698. $dev_id = '?id=' . $id;
  699. }
  700. if (!empty($dev_name)) {
  701. print "<b>" . $dev_name . "::</b>";
  702. }
  703. print_submenu_url(WEB_submenu_options, '/admin/devices/editdevice.php' . $dev_id, $current_page, 0);
  704. if ($dev_type <= 2) {
  705. print_submenu_url(WEB_submenu_ports, '/admin/devices/switchport.php' . $dev_id, $current_page, 0);
  706. print_submenu_url(WEB_submenu_state, '/admin/devices/switchstatus.php' . $dev_id, $current_page, 0);
  707. print_submenu_url(WEB_submenu_connections, '/admin/devices/switchport-conn.php' . $dev_id, $current_page, 1);
  708. }
  709. print "</div>\n";
  710. }
  711. function print_ip_submenu($current_page)
  712. {
  713. print "<div id='submenu'>\n";
  714. print_submenu_url(WEB_submenu_ip_list, '/admin/iplist/index.php', $current_page, 0);
  715. print_submenu_url(WEB_submenu_nagios, '/admin/iplist/nagios.php', $current_page, 0);
  716. print_submenu_url(WEB_submenu_doubles, '/admin/iplist/doubles.php', $current_page, 0);
  717. print_submenu_url(WEB_submenu_deleted, '/admin/iplist/deleted.php', $current_page, 0);
  718. print_submenu_url(WEB_submenu_auto_rules, '/admin/iplist/auto_rules.php', $current_page, 1);
  719. print "</div>\n";
  720. }
  721. function get_nagios_name($auth)
  722. {
  723. if (!empty($auth['dns_name'])) {
  724. return $auth['dns_name'];
  725. }
  726. if (!empty($auth['dhcp_hostname'])) {
  727. return $auth['dhcp_hostname'];
  728. }
  729. if (!empty($auth['comments'])) {
  730. $result = transliterate($auth['comments']);
  731. $result = preg_replace('/\(/', '-', $result);
  732. $result = preg_replace('/\)/', '-', $result);
  733. $result = preg_replace('/--/', '-', $result);
  734. return $result;
  735. }
  736. if (empty($auth['login'])) {
  737. $auth['login'] = 'host';
  738. }
  739. return $auth['login'] . "_" . $auth['id'];
  740. }
  741. function get_ou($db, $ou_value)
  742. {
  743. if (!isset($ou_value)) {
  744. return;
  745. }
  746. $ou_name = get_record_sql($db, "SELECT ou_name FROM OU WHERE id=$ou_value");
  747. if (empty($ou_name)) {
  748. return;
  749. }
  750. return $ou_name['ou_name'];
  751. }
  752. function get_device_model($db, $model_value)
  753. {
  754. if (!isset($model_value)) {
  755. return;
  756. }
  757. $model_name = get_record_sql($db, "SELECT model_name FROM device_models WHERE id=$model_value");
  758. if (empty($model_name)) {
  759. return;
  760. }
  761. return $model_name['model_name'];
  762. }
  763. function get_device_model_name($db, $model_value)
  764. {
  765. if (!isset($model_value)) {
  766. return '';
  767. }
  768. $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");
  769. if (empty($model_name)) {
  770. return '';
  771. }
  772. return $model_name['name'] . ' ' . $model_name['model_name'];
  773. }
  774. function get_device_model_vendor($db, $model_value)
  775. {
  776. if (!isset($model_value)) {
  777. return '';
  778. }
  779. $model_name = get_record_sql($db, "SELECT vendor_id FROM device_models WHERE id=$model_value");
  780. if (empty($model_name)) {
  781. return '';
  782. }
  783. return $model_name['vendor_id'];
  784. }
  785. function get_building($db, $building_value)
  786. {
  787. if (!isset($building_value)) {
  788. return;
  789. }
  790. $building_name = get_record_sql($db, "SELECT name FROM building WHERE id=$building_value");
  791. if (empty($building_name)) {
  792. return;
  793. }
  794. return $building_name['name'];
  795. }
  796. function print_device_model_select($db, $device_model_name, $device_model_value)
  797. {
  798. print "<select name=\"$device_model_name\" class=\"js-select-single\">\n";
  799. $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");
  800. while (list($f_device_model_id, $f_device_model_name, $f_vendor_name) = mysqli_fetch_array($t_device_model)) {
  801. print_select_item($f_vendor_name . " " . $f_device_model_name, $f_device_model_id, $device_model_value);
  802. }
  803. print "</select>\n";
  804. }
  805. function print_group_select($db, $group_name, $group_value)
  806. {
  807. print "<select name=\"$group_name\">\n";
  808. $t_group = mysqli_query($db, "SELECT id,group_name FROM Group_list Order by group_name");
  809. while (list($f_group_id, $f_group_name) = mysqli_fetch_array($t_group)) {
  810. print_select_item($f_group_name, $f_group_id, $group_value);
  811. }
  812. print "</select>\n";
  813. }
  814. function print_building_select($db, $building_name, $building_value)
  815. {
  816. print "<select name=\"$building_name\">\n";
  817. print_select_item(WEB_select_item_all, 0, $building_value);
  818. $t_building = mysqli_query($db, "SELECT id,name FROM building Order by name");
  819. while (list($f_building_id, $f_building_name) = mysqli_fetch_array($t_building)) {
  820. print_select_item($f_building_name, $f_building_id, $building_value);
  821. }
  822. print "</select>\n";
  823. }
  824. function print_devmodels_select($db, $devmodel_name, $devmodel_value, $dev_filter = 'device_type<=2')
  825. {
  826. print "<select name=\"$devmodel_name\">\n";
  827. print_select_item(WEB_select_item_all, -1, $devmodel_value);
  828. $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");
  829. while (list($f_devmodel_id, $f_devmodel_vendor, $f_devmodel_name) = mysqli_fetch_array($t_devmodel)) {
  830. print_select_item($f_devmodel_vendor . " " . $f_devmodel_name, $f_devmodel_id, $devmodel_value);
  831. }
  832. print "</select>\n";
  833. }
  834. function print_devtypes_select($db, $devtype_name, $devtype_value, $mode)
  835. {
  836. print "<select name=\"$devtype_name\">\n";
  837. print_select_item(WEB_select_item_all, -1, $devtype_value);
  838. $filter = '';
  839. if (!empty($mode)) {
  840. $filter = "WHERE $mode";
  841. }
  842. $t_devtype = mysqli_query($db, "SELECT id,`name." . HTML_LANG . "` FROM device_types $filter ORDER BY `name." . HTML_LANG . "`");
  843. while (list($f_devtype_id, $f_devtype_name) = mysqli_fetch_array($t_devtype)) {
  844. print_select_item($f_devtype_name, $f_devtype_id, $devtype_value);
  845. }
  846. print "</select>\n";
  847. }
  848. function print_devtype_select($db, $devtype_name, $devtype_value)
  849. {
  850. print "<select name=\"$devtype_name\">\n";
  851. $t_devtype = mysqli_query($db, "SELECT id,`name." . HTML_LANG . "` FROM device_types ORDER BY `name." . HTML_LANG . "`");
  852. while (list($f_devtype_id, $f_devtype_name) = mysqli_fetch_array($t_devtype)) {
  853. print_select_item($f_devtype_name, $f_devtype_id, $devtype_value);
  854. }
  855. print "</select>\n";
  856. }
  857. function get_group($db, $group_value)
  858. {
  859. list($group_name) = mysqli_fetch_array(mysqli_query($db, "SELECT group_name FROM Group_list WHERE id=$group_value"));
  860. return $group_name;
  861. }
  862. function get_devtype_name($db, $device_type_id)
  863. {
  864. list($type_name) = mysqli_fetch_array(mysqli_query($db, "SELECT `name." . HTML_LANG . "` FROM device_types WHERE id=$device_type_id"));
  865. return $type_name;
  866. }
  867. function get_l3_interfaces($db, $device_id)
  868. {
  869. $wan = '';
  870. $lan = '';
  871. $t_l3int = mysqli_query($db, "SELECT name,interface_type FROM device_l3_interfaces WHERE device_id=$device_id ORDER BY name");
  872. while (list($f_name, $f_type) = mysqli_fetch_array($t_l3int)) {
  873. if ($f_type == 0) {
  874. $lan = $lan . " " . $f_name;
  875. }
  876. if ($f_type == 1) {
  877. $wan = $wan . " " . $f_name;
  878. }
  879. }
  880. $wan = trim($wan);
  881. $lan = trim($lan);
  882. $result = '';
  883. if (!empty($wan)) {
  884. $result .= ' WAN: ' . $wan . '<br>';
  885. }
  886. if (!empty($lan)) {
  887. $result .= ' LAN: ' . $lan;
  888. }
  889. return trim($result);
  890. }
  891. function get_wan_interfaces($db, $device_id)
  892. {
  893. $l3_wan_sql = "SELECT id,name,snmpin FROM device_l3_interfaces WHERE device_id='" . $device_id . "' and interface_type=1 ORDER BY name";
  894. $t_l3int = get_records_sql($db, $l3_wan_sql);
  895. for ($i = 0; $i < count($t_l3int); ++$i) {
  896. $t_l3int[$i]['comment'] = '';
  897. if (empty($t_l3int[$i]['snmpin'])) {
  898. continue;
  899. }
  900. $con_sql = "SELECT * FROM `device_ports` WHERE device_id='" . $device_id . "' AND snmp_index='" . $t_l3int[$i]['snmpin'] . "'";
  901. $conn = get_record_sql($db, $con_sql);
  902. if (isset($conn) and !empty($conn['comment'])) {
  903. $t_l3int[$i]['comment'] = $conn['comment'];
  904. }
  905. }
  906. return $t_l3int;
  907. }
  908. function get_gw_subnets($db, $device_id)
  909. {
  910. $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';
  911. $gw_subnets = get_records_sql($db, $gw_subnets_sql);
  912. $result = '';
  913. foreach ($gw_subnets as $row) {
  914. if (!empty($row)) {
  915. $result .= ' ' . $row['subnet'] . '<br>';
  916. }
  917. }
  918. return trim($result);
  919. }
  920. function print_queue_select($db, $queue_name, $queue_value)
  921. {
  922. print "<select name=\"$queue_name\">\n";
  923. $t_queue = mysqli_query($db, "SELECT id,queue_name FROM Queue_list Order by queue_name");
  924. while (list($f_queue_id, $f_queue_name) = mysqli_fetch_array($t_queue)) {
  925. print_select_item($f_queue_name, $f_queue_id, $queue_value);
  926. }
  927. print "</select>\n";
  928. }
  929. function get_queue($db, $queue_value)
  930. {
  931. list($queue_name) = mysqli_fetch_array(mysqli_query($db, "SELECT queue_name FROM Queue_list WHERE id=$queue_value"));
  932. return $queue_name;
  933. }
  934. function print_qa_l3int_select($qa_name, $qa_value = 0)
  935. {
  936. print "<select name=\"$qa_name\">\n";
  937. print_select_item(WEB_select_item_lan, 0, $qa_value);
  938. print_select_item(WEB_select_item_wan, 1, $qa_value);
  939. print "</select>\n";
  940. }
  941. function print_qa_rule_select($qa_name, $qa_value = 1)
  942. {
  943. print "<select name=\"$qa_name\">\n";
  944. print_select_item('Subnet', 1, $qa_value);
  945. print_select_item('Mac', 2, $qa_value);
  946. print_select_item('Hostname', 3, $qa_value);
  947. print "</select>\n";
  948. }
  949. function print_snmp_auth_proto_select($qa_name, $qa_value = 'sha512')
  950. {
  951. print "<select name=\"$qa_name\">\n";
  952. print_select_item('sha512', 'sha512', $qa_value);
  953. print_select_item('sha256', 'sha256', $qa_value);
  954. print_select_item('sha', 'sha', $qa_value);
  955. print_select_item('md5', 'md5', $qa_value);
  956. print "</select>\n";
  957. }
  958. function print_snmp_priv_proto_select($qa_name, $qa_value = 'aes128')
  959. {
  960. print "<select name=\"$qa_name\">\n";
  961. print_select_item('aes128', 'aes128', $qa_value);
  962. print_select_item('aes', 'aes', $qa_value);
  963. print_select_item('des', 'des', $qa_value);
  964. print "</select>\n";
  965. }
  966. function get_int($qa_value = 0)
  967. {
  968. if (empty($qa_value)) {
  969. $qa_value = 0;
  970. } else {
  971. $qa_value = (int)$qa_value * 1;
  972. }
  973. return $qa_value;
  974. }
  975. function print_qa_select($qa_name, $qa_value = 0)
  976. {
  977. print "<select name=\"$qa_name\">\n";
  978. if (empty($qa_value)) {
  979. $qa_value = 0;
  980. } else {
  981. $qa_value = $qa_value * 1;
  982. }
  983. print_select_item(WEB_select_item_yes, 1, $qa_value);
  984. print_select_item(WEB_select_item_no, 0, $qa_value);
  985. print "</select>\n";
  986. }
  987. function print_list_select($qa_name, $qa_value, $list)
  988. {
  989. print "<select name=\"$qa_name\">\n";
  990. if (empty($qa_value)) {
  991. $qa_value = '';
  992. }
  993. for ($i = 0; $i < count($list); ++$i) {
  994. print_select_item($list[$i], $list[$i], $qa_value);
  995. }
  996. print "</select>\n";
  997. }
  998. function print_qa_select_ext($qa_name, $qa_value = 0, $readonly = 1)
  999. {
  1000. $state = '';
  1001. if ($readonly) {
  1002. $state = 'disabled=true';
  1003. }
  1004. print "<select name=\"$qa_name\">\n";
  1005. print_select_item_ext(WEB_select_item_yes, 1, $qa_value, $readonly);
  1006. print_select_item_ext(WEB_select_item_no, 0, $qa_value, $readonly);
  1007. print "</select>\n";
  1008. }
  1009. function print_td_yes_no($qa_value = 0)
  1010. {
  1011. $cl = 'down';
  1012. if ($qa_value == 1) {
  1013. $cl = 'up';
  1014. }
  1015. print "<td class=\"$cl\">";
  1016. if ($qa_value == 1) {
  1017. print WEB_select_item_yes;
  1018. } else {
  1019. print WEB_select_item_no;
  1020. }
  1021. print "</td>\n";
  1022. }
  1023. function print_control_proto_select($qa_name, $qa_value = -1)
  1024. {
  1025. print "<select name=\"$qa_name\">\n";
  1026. print_select_item('Disabled', -1, $qa_value);
  1027. print_select_item('Ssh', 0, $qa_value);
  1028. print_select_item('Telnet', 1, $qa_value);
  1029. // print_select_item('Mikrotik rest api', 2, $qa_value);
  1030. print "</select>\n";
  1031. }
  1032. function print_snmp_select($qa_name, $qa_value = 0)
  1033. {
  1034. print "<select name=\"$qa_name\">\n";
  1035. print_select_item('Disabled', 0, $qa_value);
  1036. print_select_item('v1', 1, $qa_value);
  1037. print_select_item('v2', 2, $qa_value);
  1038. print_select_item('v3', 3, $qa_value);
  1039. print "</select>\n";
  1040. }
  1041. function print_dhcp_select($qa_name, $qa_value = 0)
  1042. {
  1043. print "<select name=\"$qa_name\">\n";
  1044. if (!isset($qa_value) or strlen($qa_value) == 0) {
  1045. $qa_value = 'all';
  1046. }
  1047. print_select_item(WEB_select_item_events, 'all', $qa_value);
  1048. print_select_item(WEB_select_item_lease, 'add', $qa_value);
  1049. print_select_item(WEB_select_item_lease_refresh, 'old', $qa_value);
  1050. print_select_item(WEB_select_item_lease_free, 'del', $qa_value);
  1051. print "</select>\n";
  1052. }
  1053. function print_nagios_handler_select($qa_name)
  1054. {
  1055. print "<select name=\"$qa_name\">\n";
  1056. print_select_simple(WEB_select_item_no, '');
  1057. print_select_simple('restart-port', 'restart-port');
  1058. print "</select>\n";
  1059. }
  1060. function print_dhcp_acl_select($qa_name)
  1061. {
  1062. print "<select name=\"$qa_name\">\n";
  1063. print_select_simple(WEB_select_item_no, '');
  1064. print_select_simple('hotspot-free', 'hotspot-free');
  1065. print "</select>\n";
  1066. }
  1067. function print_enabled_select($qa_name, $qa_value)
  1068. {
  1069. print "<select name=\"$qa_name\">\n";
  1070. if (!isset($qa_value) or strlen($qa_value) == 0) {
  1071. $qa_value = 0;
  1072. }
  1073. print_select_item(WEB_select_item_every, 0, $qa_value);
  1074. print_select_item(WEB_select_item_disabled, 1, $qa_value);
  1075. print_select_item(WEB_select_item_enabled, 2, $qa_value);
  1076. print "</select>\n";
  1077. }
  1078. function print_ip_type_select($qa_name, $qa_value)
  1079. {
  1080. print "<select name=\"$qa_name\">\n";
  1081. if (!isset($qa_value) or strlen($qa_value) == 0) {
  1082. $qa_value = 0;
  1083. }
  1084. print_select_item(WEB_select_item_every, 0, $qa_value);
  1085. print_select_item(WEB_select_item_static, 1, $qa_value);
  1086. print_select_item(WEB_select_item_dhcp, 2, $qa_value);
  1087. print_select_item(WEB_select_item_suspicious, 3, $qa_value);
  1088. print "</select>\n";
  1089. }
  1090. function print_vendor_select($db, $qa_name, $qa_value)
  1091. {
  1092. print "<select name=\"$qa_name\" class=\"js-select-single\">\n";
  1093. $sSQL = "SELECT id,`name` FROM `vendors` order by `name`";
  1094. $vendors = mysqli_query($db, $sSQL);
  1095. print_select_item(WEB_select_item_all, 0, $qa_value);
  1096. while (list($v_id, $v_name) = mysqli_fetch_array($vendors)) {
  1097. print_select_item($v_name, $v_id, $qa_value);
  1098. }
  1099. print "</select>\n";
  1100. }
  1101. function print_vendor_set($db, $qa_name, $qa_value)
  1102. {
  1103. print "<select name=\"$qa_name\" class=\"js-select-single\" style=\"width: 100%\">\n";
  1104. $sSQL = "SELECT id,`name` FROM `vendors` order by `name`";
  1105. $vendors = mysqli_query($db, $sSQL);
  1106. while (list($v_id, $v_name) = mysqli_fetch_array($vendors)) {
  1107. print_select_item($v_name, $v_id, $qa_value);
  1108. }
  1109. print "</select>\n";
  1110. }
  1111. function get_vendor_name($db, $v_id)
  1112. {
  1113. $vendor = get_record_sql($db, "SELECT * FROM `vendors` WHERE id=" . $v_id);
  1114. if (empty($vendor)) {
  1115. return NULL;
  1116. }
  1117. return $vendor['name'];
  1118. }
  1119. function get_qa($qa_value)
  1120. {
  1121. if ($qa_value == 1) {
  1122. return "Да";
  1123. }
  1124. return "Нет";
  1125. }
  1126. function print_action_select($action_name, $action_value)
  1127. {
  1128. print "<select name=\"$action_name\">\n";
  1129. print_select_item(WEB_select_item_allow, 1, $action_value);
  1130. print_select_item(WEB_select_item_forbidden, 0, $action_value);
  1131. print "</select>\n";
  1132. }
  1133. function get_action($action_value)
  1134. {
  1135. if ($action_value == 1) {
  1136. return "Разрешить";
  1137. }
  1138. return "Запретить";
  1139. }
  1140. function print_filter_select($db, $filter_name, $group_id)
  1141. {
  1142. print "<select name=\"$filter_name\" class=\"js-select-single\">\n";
  1143. if (isset($group_id)) {
  1144. $sSQL = "SELECT id,name FROM Filter_list WHERE Filter_list.id not in (Select filter_id FROM Group_filters WHERE group_id=$group_id)";
  1145. } else {
  1146. $sSQL = "SELECT id,name FROM Filter_list Order by name";
  1147. }
  1148. $t_filters = mysqli_query($db, $sSQL);
  1149. while (list($filter_id, $filter_name) = mysqli_fetch_array($t_filters)) {
  1150. print_select_item($filter_name, $filter_id, 0);
  1151. }
  1152. print "</select>\n";
  1153. }
  1154. function get_filter($db, $filter_value)
  1155. {
  1156. list($filter) = mysqli_fetch_array(mysqli_query($db, "SELECT name FROM Filter_list WHERE id=" . $filter_value));
  1157. return $filter;
  1158. }
  1159. function get_login($db, $user_id)
  1160. {
  1161. list($login) = mysqli_fetch_array(mysqli_query($db, "SELECT login FROM User_list WHERE id=$user_id"));
  1162. return $login;
  1163. }
  1164. function get_auth_count($db, $user_id)
  1165. {
  1166. list($count) = mysqli_fetch_array(mysqli_query($db, "SELECT count(id) FROM User_auth WHERE user_id=$user_id and deleted=0"));
  1167. return $count;
  1168. }
  1169. function print_login_select($db, $login_name, $current_login)
  1170. {
  1171. print "<select name=\"$login_name\" class=\"js-select-single\">\n";
  1172. $t_login = mysqli_query($db, "SELECT id,login FROM User_list Order by Login");
  1173. print_select_item('None', 0, $current_login);
  1174. while (list($f_user_id, $f_login) = mysqli_fetch_array($t_login)) {
  1175. print_select_item($f_login, $f_user_id, $current_login);
  1176. }
  1177. print "</select>\n";
  1178. }
  1179. function print_auth_select($db, $login_name, $current_auth)
  1180. {
  1181. print "<select name=\"$login_name\" class=\"js-select-single\">\n";
  1182. $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");
  1183. print_select_item('Empty', 0, $current_auth);
  1184. while (list($f_login, $f_fio, $f_ip, $f_auth_id) = mysqli_fetch_array($t_login)) {
  1185. print_select_item($f_login . "[" . $f_fio . "] - " . $f_ip, $f_auth_id, $current_auth);
  1186. }
  1187. print "</select>\n";
  1188. }
  1189. function print_auth_select_mac($db, $login_name, $current_auth)
  1190. {
  1191. print "<select name=\"$login_name\" class=\"js-select-single\">\n";
  1192. $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");
  1193. print_select_item('Empty', 0, $current_auth);
  1194. while (list($f_login, $f_fio, $f_ip, $f_mac, $f_auth_id) = mysqli_fetch_array($t_login)) {
  1195. print_select_item($f_login . "[" . $f_mac . "] - " . $f_ip, $f_auth_id, $current_auth);
  1196. }
  1197. print "</select>\n";
  1198. }
  1199. function compact_port_name($port)
  1200. {
  1201. $result = $port;
  1202. $result = preg_replace('/XGigabitEthernet/', 'X', $result);
  1203. $result = preg_replace('/TenGigabitEthernet/', 'Te', $result);
  1204. $result = preg_replace('/GigabitEthernet/', 'Gi', $result);
  1205. return $result;
  1206. }
  1207. function print_device_port_select($db, $field_name, $device_id, $target_id)
  1208. {
  1209. print "<select name=\"$field_name\" class=\"js-select-single\">\n";
  1210. if (empty($target_id)) {
  1211. $target_id = 0;
  1212. }
  1213. if (empty($device_id)) {
  1214. $device_id = 0;
  1215. }
  1216. $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";
  1217. $t_device = mysqli_query($db, $d_sql);
  1218. print_select_item('Empty', 0, $target_id);
  1219. while (list($f_name, $f_port, $f_device_id, $f_target_id, $f_ifname) = mysqli_fetch_array($t_device)) {
  1220. if (empty($f_ifname)) {
  1221. $f_ifname = $f_port;
  1222. }
  1223. print_select_item($f_name . "[" . $f_port . "] - " . compact_port_name($f_ifname), $f_target_id, $target_id);
  1224. }
  1225. print "</select>\n";
  1226. }
  1227. function print_device_select($db, $field_name, $device_id)
  1228. {
  1229. print "<select name=\"$field_name\" class=\"js-select-single\" >\n";
  1230. $d_sql = "SELECT D.device_name, D.id FROM devices AS D Where D.deleted=0 order by D.device_name ASC";
  1231. $t_device = mysqli_query($db, $d_sql);
  1232. print_select_item(WEB_select_item_every, 0, $device_id);
  1233. while (list($f_name, $f_device_id) = mysqli_fetch_array($t_device)) {
  1234. print_select_item($f_name, $f_device_id, $device_id);
  1235. }
  1236. print "</select>\n";
  1237. }
  1238. function print_netdevice_select($db, $field_name, $device_id)
  1239. {
  1240. print "<select name=\"$field_name\" class=\"js-select-single\" >\n";
  1241. $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";
  1242. $t_device = mysqli_query($db, $d_sql);
  1243. print_select_item(WEB_select_item_every, 0, $device_id);
  1244. while (list($f_name, $f_device_id) = mysqli_fetch_array($t_device)) {
  1245. print_select_item($f_name, $f_device_id, $device_id);
  1246. }
  1247. print "</select>\n";
  1248. }
  1249. function print_vlan_select($db, $field_name, $vlan)
  1250. {
  1251. print "<select name=\"$field_name\" class=\"js-select-single\">\n";
  1252. $d_sql = "SELECT DISTINCT vlan FROM device_ports ORDER BY vlan DESC";
  1253. $v_device = mysqli_query($db, $d_sql);
  1254. if (!isset($vlan) or empty($vlan)) {
  1255. $vlan = 1;
  1256. };
  1257. print_select_item('1', 1, $vlan);
  1258. while (list($f_vlan) = mysqli_fetch_array($v_device)) {
  1259. if ($f_vlan === '1') {
  1260. continue;
  1261. }
  1262. print_select_item($f_vlan, $f_vlan, $vlan);
  1263. }
  1264. print "</select>\n";
  1265. }
  1266. function print_device_select_ip($db, $field_name, $device_ip)
  1267. {
  1268. print "<select name=\"$field_name\" class=\"js-select-single\" >\n";
  1269. $d_sql = "SELECT D.device_name, D.ip FROM devices AS D Where D.deleted=0 order by D.device_name ASC";
  1270. $t_device = mysqli_query($db, $d_sql);
  1271. print_select_item(WEB_select_item_every, '', $device_ip);
  1272. while (list($f_name, $f_device_ip) = mysqli_fetch_array($t_device)) {
  1273. print_select_item($f_name, $f_device_ip, $device_ip);
  1274. }
  1275. print "</select>\n";
  1276. }
  1277. function print_syslog_device_select($db, $field_name, $syslog_filter, $device_ip)
  1278. {
  1279. print "<select name=\"$field_name\" class=\"js-select-single\" >\n";
  1280. $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";
  1281. $t_device = mysqli_query($db, $d_sql);
  1282. print_select_item(WEB_select_item_every, '', $device_ip);
  1283. while (list($f_ip, $f_name) = mysqli_fetch_array($t_device)) {
  1284. if (!isset($f_name) or empty($f_name)) {
  1285. $f_name = $f_ip;
  1286. }
  1287. print_select_item($f_name, $f_ip, $device_ip);
  1288. }
  1289. print "</select>\n";
  1290. }
  1291. function print_gateway_select($db, $field_name, $device_id)
  1292. {
  1293. print "<select name=\"$field_name\" >\n";
  1294. $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";
  1295. $t_device = mysqli_query($db, $d_sql);
  1296. print_select_item(WEB_select_item_every, 0, $device_id);
  1297. while (list($f_name, $f_device_id) = mysqli_fetch_array($t_device)) {
  1298. print_select_item($f_name, $f_device_id, $device_id);
  1299. }
  1300. print "</select>\n";
  1301. }
  1302. function get_gateways($db)
  1303. {
  1304. $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";
  1305. $t_device = mysqli_query($db, $d_sql);
  1306. unset($result);
  1307. while (list($f_name, $f_device_id) = mysqli_fetch_array($t_device)) {
  1308. $result[$f_device_id] = $f_name;
  1309. }
  1310. return $result;
  1311. }
  1312. function print_device_port($db, $target_id)
  1313. {
  1314. $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";
  1315. $t_device = mysqli_query($db, $d_sql);
  1316. while (list($f_name, $f_port, $f_device_id) = mysqli_fetch_array($t_device)) {
  1317. print "<a href=\"/admin/devices/switchport.php?id=$f_device_id\">" . $f_name . "[" . $f_port . "]</a>\n";
  1318. }
  1319. }
  1320. function get_device_ips($db, $device_id)
  1321. {
  1322. $switch = get_record($db, 'devices', 'id=' . $device_id);
  1323. $index = 0;
  1324. if (!empty($switch['user_id'])) {
  1325. $auth_ips = get_records($db, 'User_auth', 'deleted=0 and user_id=' . $switch['user_id']);
  1326. foreach ($auth_ips as $key => $value) {
  1327. if (isset($value['ip'])) {
  1328. $result[$index] = $value['ip'];
  1329. $index++;
  1330. }
  1331. }
  1332. } else {
  1333. if (isset($switch['ip'])) {
  1334. $result[$index] = $switch['ip'];
  1335. $index++;
  1336. }
  1337. }
  1338. return $result;
  1339. }
  1340. function get_device_id($db, $device_name)
  1341. {
  1342. $d_sql = "SELECT id FROM devices WHERE device_name='$device_name' and deleted=0";
  1343. $dev = get_record_sql($db, $d_sql);
  1344. if (empty($dev)) {
  1345. return NULL;
  1346. }
  1347. return $dev["id"];
  1348. }
  1349. function get_device_name($db, $device_id)
  1350. {
  1351. $d_sql = "SELECT device_name FROM devices WHERE id='$device_id'";
  1352. $dev = get_record_sql($db, $d_sql);
  1353. if (empty($dev)) {
  1354. return NULL;
  1355. }
  1356. return $dev["device_name"];
  1357. }
  1358. function get_auth_by_ip($db, $ip)
  1359. {
  1360. $d_sql = "SELECT id FROM User_auth WHERE ip='$ip' and deleted=0";
  1361. $auth = get_record_sql($db, $d_sql);
  1362. if (empty($auth)) {
  1363. return NULL;
  1364. }
  1365. return $auth["id"];
  1366. }
  1367. function get_user_by_ip($db, $ip)
  1368. {
  1369. $d_sql = "SELECT user_id FROM User_auth WHERE ip='$ip' and deleted=0";
  1370. $auth = get_record_sql($db, $d_sql);
  1371. if (empty($auth)) {
  1372. return NULL;
  1373. }
  1374. return $auth["user_id"];
  1375. }
  1376. function get_device_by_auth($db, $id)
  1377. {
  1378. $d_sql = "SELECT id FROM devices WHERE user_id=$id and deleted=0";
  1379. $f_dev = get_record_sql($db, $d_sql);
  1380. if (empty($f_dev)) {
  1381. return NULL;
  1382. }
  1383. return $f_dev['id'];
  1384. }
  1385. function print_auth_port($db, $port_id, $new_window = FALSE)
  1386. {
  1387. $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";
  1388. $t_auth = mysqli_query($db, $d_sql);
  1389. while (list($f_ip, $f_int, $f_mac, $f_auth_id, $f_dns, $f_user_id) = mysqli_fetch_array($t_auth)) {
  1390. $name = $f_ip;
  1391. if (!empty($f_dns)) {
  1392. $name = $f_dns;
  1393. }
  1394. $title = get_login($db, $f_user_id) . " =>" . $f_ip . "[" . $f_mac . "]";
  1395. if (!empty($f_dns)) {
  1396. $title .= " | " . $f_dns;
  1397. }
  1398. if ($new_window) {
  1399. print "<a href=\"\" title=\"" . $title . "\" onclick=\"" . open_window_url("/admin/users/editauth.php?id=" . $f_auth_id) . " return false;\">" . $name . " [" . $f_ip . "]</a><br>";
  1400. } else {
  1401. print "<a href=/admin/users/editauth.php?id=" . $f_auth_id . " title=\"" . $title . "\" >" . $name . " [" . $f_ip . "]</a><br>";
  1402. }
  1403. }
  1404. }
  1405. function get_port_comment($db, $port_id, $port_comment = '')
  1406. {
  1407. $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";
  1408. $t_auth = mysqli_query($db, $d_sql);
  1409. $comment_found = 0;
  1410. $result = '';
  1411. while (list($f_int, $f_comment) = mysqli_fetch_array($t_auth)) {
  1412. if (!empty($f_comment)) {
  1413. $comment_found = 1;
  1414. } else {
  1415. $f_comment = '';
  1416. }
  1417. $result .= $f_comment . '<br>';
  1418. }
  1419. if (!$comment_found) {
  1420. return $port_comment;
  1421. }
  1422. if (!empty($port_comment)) {
  1423. $result .= '(' . $port_comment . ')';
  1424. }
  1425. return $result;
  1426. }
  1427. function print_auth_simple($db, $auth_id)
  1428. {
  1429. $auth = get_record($db, "User_auth", "id=$auth_id");
  1430. $name = $auth['dns_name'];
  1431. if (empty($name)) {
  1432. $name = $auth['comments'];
  1433. }
  1434. if (empty($name)) {
  1435. $name = $auth['ip'];
  1436. }
  1437. print "<a href=\"/admin/users/editauth.php?id=$auth_id\">" . $name . "</a><br>";
  1438. }
  1439. function print_auth($db, $auth_id)
  1440. {
  1441. $auth = get_record($db, "User_auth", "id=$auth_id");
  1442. $name = $auth['dns_name'];
  1443. if (empty($name)) {
  1444. $name = $auth['comments'];
  1445. } else {
  1446. $name .= " (" . $auth['comments'] . ")";
  1447. }
  1448. if (empty($name)) {
  1449. $name = $auth['ip'];
  1450. } else {
  1451. $name .= " [" . $auth['ip'] . "]";
  1452. }
  1453. print "<a href=\"/admin/users/editauth.php?id=$auth_id\">" . $name . "</a><br>";
  1454. }
  1455. function print_auth_detail($db, $auth_id)
  1456. {
  1457. $auth = get_record($db, "User_auth", "id=$auth_id");
  1458. $name = $auth['dns_name'];
  1459. if (empty($name)) {
  1460. $name = $auth['comments'];
  1461. } else {
  1462. $name .= " (" . $auth['comments'] . ")";
  1463. }
  1464. if (empty($name)) {
  1465. $name = $auth['ip'];
  1466. } else {
  1467. $name .= " [" . $auth['ip'] . "]";
  1468. }
  1469. $name .= " last: [" . $auth['last_found'] . "] ";
  1470. if ($auth['deleted'] == 1) {
  1471. $name .= " <font color='red'>DELETED!!!</font>";
  1472. }
  1473. print "<a href=\"/admin/users/editauth.php?id=$auth_id\">" . $name . "</a><br>";
  1474. }
  1475. function get_auth_port_count($db, $port_id)
  1476. {
  1477. $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";
  1478. $t_device = mysqli_query($db, $d_sql);
  1479. list($f_count) = mysqli_fetch_array($t_device);
  1480. if (!isset($f_count)) {
  1481. $f_count = 0;
  1482. }
  1483. return $f_count;
  1484. }
  1485. function get_connection($db, $auth_id)
  1486. {
  1487. $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";
  1488. $t_device = mysqli_query($db, $d_sql);
  1489. list($f_name, $f_port) = mysqli_fetch_array($t_device);
  1490. if (isset($f_name)) {
  1491. $result = expand_device_name($db, $f_name) . "[" . $f_port . "]";
  1492. } else {
  1493. $result = '';
  1494. }
  1495. return $result;
  1496. }
  1497. function get_connection_string($db, $auth_id)
  1498. {
  1499. $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";
  1500. $t_device = mysqli_query($db, $d_sql);
  1501. list($f_name, $f_port) = mysqli_fetch_array($t_device);
  1502. if (isset($f_name)) {
  1503. $result = $f_name . "[" . $f_port . "]";
  1504. } else {
  1505. $result = '';
  1506. }
  1507. return $result;
  1508. }
  1509. function get_port($db, $port_id)
  1510. {
  1511. $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";
  1512. $t_device = mysqli_query($db, $d_sql);
  1513. list($f_name, $f_port) = mysqli_fetch_array($t_device);
  1514. if (isset($f_name)) {
  1515. $result = expand_device_name($db, $f_name) . "[" . $f_port . "]";
  1516. } else {
  1517. $result = '';
  1518. }
  1519. return $result;
  1520. }
  1521. function print_option_select($db, $option_name)
  1522. {
  1523. print "<select name=\"$option_name\">\n";
  1524. $t_option = mysqli_query($db, "SELECT id,option_name FROM config_options WHERE uniq=0 AND draft=0 order by option_name");
  1525. while (list($f_id, $f_name) = mysqli_fetch_array($t_option)) {
  1526. print "<option value=$f_id>$f_name</option>";
  1527. }
  1528. $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");
  1529. while (list($f_id, $f_name) = mysqli_fetch_array($t_option)) {
  1530. print "<option value=$f_id>$f_name</option>";
  1531. }
  1532. print "</select>\n";
  1533. }
  1534. function run_sql($db, $query)
  1535. {
  1536. if (preg_match('/^\s*(UPDATE|DELETE)/i', $query)) {
  1537. unset($matches);
  1538. preg_match('/FROM\s+(.*)\s+/i', $query, $matches);
  1539. if (!empty($matches[1])) {
  1540. if (!allow_update($matches[1], 'del')) {
  1541. LOG_DEBUG($db, "Access denied: $query ");
  1542. return;
  1543. }
  1544. }
  1545. unset($matches);
  1546. preg_match('/INSERT\s+INTO\s+(.*)\s+/i', $query, $matches);
  1547. if (!empty($matches[1])) {
  1548. if (!allow_update($matches[1], 'add')) {
  1549. LOG_DEBUG($db, "Access denied: $query ");
  1550. return;
  1551. }
  1552. }
  1553. unset($matches);
  1554. preg_match('/UPDATE\s+(.*)\s+/i', $query, $matches);
  1555. if (!empty($matches[1])) {
  1556. if (!allow_update($matches[1], 'update')) {
  1557. LOG_DEBUG($db, "Access denied: $query ");
  1558. return;
  1559. }
  1560. }
  1561. unset($matches);
  1562. }
  1563. $sql_result = mysqli_query($db, $query);
  1564. if (!$sql_result) {
  1565. LOG_ERROR($db, "At simple SQL: $query :" . mysqli_error($db));
  1566. return;
  1567. }
  1568. return $sql_result;
  1569. }
  1570. function get_count_records($db, $table, $filter)
  1571. {
  1572. if (!empty($filter)) {
  1573. $filter = 'where ' . $filter;
  1574. }
  1575. $t_count = mysqli_query($db, "SELECT count(*) FROM $table $filter");
  1576. list($count) = mysqli_fetch_array($t_count);
  1577. if (!isset($count)) {
  1578. $count = 0;
  1579. }
  1580. return $count;
  1581. }
  1582. function get_id_record($db, $table, $filter)
  1583. {
  1584. if (isset($filter)) {
  1585. $filter = 'WHERE ' . $filter;
  1586. }
  1587. $t_record = mysqli_query($db, "SELECT id FROM $table $filter limit 1");
  1588. list($id) = mysqli_fetch_array($t_record);
  1589. return $id;
  1590. }
  1591. function set_changed($db, $id)
  1592. {
  1593. $auth['changed'] = 1;
  1594. update_record($db, "User_auth", "id=" . $id, $auth);
  1595. }
  1596. function ResolveIP($db, $ip_int)
  1597. {
  1598. $ip_name = "-";
  1599. if (empty($ip_int)) {
  1600. return $ip_name;
  1601. }
  1602. $dns_cache = get_record_sql($db, "SELECT * FROM dns_cache WHERE ip=$ip_int");
  1603. if (empty($dns_cache) or empty($dns_cache['dns'])) {
  1604. $ip_name = gethostbyaddr(long2ip($ip_int));
  1605. if (empty($ip_name) or $ip_name == long2ip($ip_int)) {
  1606. $ip_name = "-";
  1607. }
  1608. run_sql($db, "INSERT INTO dns_cache(dns,ip) VALUES('" . $ip_name . "'," . $ip_int . ")");
  1609. } else {
  1610. $ip_name = $dns_cache['dns'];
  1611. }
  1612. return $ip_name;
  1613. }
  1614. function clean_dns_cache($db)
  1615. {
  1616. $date = time();
  1617. $date = $date - 86400;
  1618. $date_clean = DateTimeImmutable::createFromFormat('U', $date);
  1619. $clean_date = $date_clean->format('Y-m-d H:i:s');
  1620. run_sql($db, "DELETE FROM dns_cache WHERE `timestamp`<='" . $clean_date . "'");
  1621. }
  1622. function clean_unreferensed_rules($db)
  1623. {
  1624. run_sql($db, "DELETE FROM `auth_rules` WHERE user_id NOT IN (SELECT id FROM User_list)");
  1625. }
  1626. function FormatDateStr($format = 'Y-m-d H:i:s', $date_str)
  1627. {
  1628. $date1 = GetDateTimeFromString($date_str);
  1629. $result = $date1->format($format);
  1630. return $result;
  1631. }
  1632. function GetDateTimeFromString($date_str)
  1633. {
  1634. if (!is_a($date_str, 'DateTime')) {
  1635. $t_date_str = urldecode($date_str);
  1636. $t_date_str = preg_replace('/(\'|\")/', '', $t_date_str);
  1637. $t_date_str = preg_replace('/T/', ' ', $t_date_str);
  1638. $date1 = DateTime::createFromFormat('Y-m-d H:i:s', $t_date_str);
  1639. if (!$date1) {
  1640. $date1 = DateTime::createFromFormat('Y.m.d H:i:s', $t_date_str);
  1641. }
  1642. if (!$date1) {
  1643. $date1 = DateTime::createFromFormat('Y/m/d H:i:s', $t_date_str);
  1644. }
  1645. if (!$date1) {
  1646. $date1 = DateTime::createFromFormat('Y-m-d H:i', $t_date_str);
  1647. }
  1648. if (!$date1) {
  1649. $date1 = DateTime::createFromFormat('Y.m.d H:i', $t_date_str);
  1650. }
  1651. if (!$date1) {
  1652. $date1 = DateTime::createFromFormat('Y/m/d H:i', $t_date_str);
  1653. }
  1654. if (!$date1) {
  1655. $date1 = DateTime::createFromFormat('Y-m-d|', $t_date_str);
  1656. }
  1657. if (!$date1) {
  1658. $date1 = DateTime::createFromFormat('Y.m.d|', $t_date_str);
  1659. }
  1660. if (!$date1) {
  1661. $date1 = DateTime::createFromFormat('Y/m/d|', $t_date_str);
  1662. }
  1663. if (!$date1) {
  1664. $date1 = new DateTime;
  1665. $date1->setTime(0, 0, 0, 1);
  1666. }
  1667. } else {
  1668. return $date_str;
  1669. }
  1670. return $date1;
  1671. }
  1672. function GetNowTimeString()
  1673. {
  1674. $now = new DateTimeImmutable('now');
  1675. $result = $now->format('Y-m-d H:i:s');
  1676. return $result;
  1677. }
  1678. function GetNowDayString()
  1679. {
  1680. $now = new DateTimeImmutable('now');
  1681. $result = $now->format('Y-m-d');
  1682. return $result;
  1683. }
  1684. function get_ip_subnet($db, $ip)
  1685. {
  1686. if (empty($ip)) {
  1687. return;
  1688. }
  1689. $ip_aton = ip2long($ip);
  1690. $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)");
  1691. if (empty($user_subnet)) {
  1692. return;
  1693. }
  1694. return $user_subnet;
  1695. }
  1696. function find_mac_in_subnet($db, $ip, $mac)
  1697. {
  1698. if (empty($ip)) {
  1699. return;
  1700. }
  1701. if (empty($mac)) {
  1702. return;
  1703. }
  1704. $ip_subnet = get_ip_subnet($db, $ip);
  1705. if (empty($ip_subnet)) {
  1706. return;
  1707. }
  1708. $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");
  1709. $auth_count = 0;
  1710. $result['count'] = 0;
  1711. $result['users_id'] = [];
  1712. foreach ($t_auth as $row) {
  1713. if (!empty($row['id'])) {
  1714. $auth_count++;
  1715. $result['count'] = $auth_count;
  1716. $result[$auth_count] = $row['id'];
  1717. array_push($result['users_id'], $row['user_id']);
  1718. }
  1719. }
  1720. return $result;
  1721. }
  1722. function apply_auth_rule($db, $auth_record, $user_id)
  1723. {
  1724. if (empty($auth_record)) {
  1725. return;
  1726. }
  1727. if (empty($user_id)) {
  1728. return $auth_record;
  1729. }
  1730. $user_rec = get_record($db, 'User_list', "id=" . $user_id);
  1731. if (empty($user_rec)) {
  1732. return $auth_record;
  1733. }
  1734. //set filter and status by user
  1735. $auth_record['ou_id'] = $user_rec['ou_id'];
  1736. $auth_record['user_id'] = $user_rec['id'];
  1737. $auth_record['filter_group_id'] = $user_rec['filter_group_id'];
  1738. $auth_record['queue_id'] = $user_rec['queue_id'];
  1739. $auth_record['enabled'] = $user_rec['enabled'];
  1740. $auth_record['changed'] = 1;
  1741. //maybe fill comments?
  1742. if (!empty($user_rec['fio']) and empty($auth_record['comments'])) {
  1743. $auth_record['comments'] = $user_rec['fio'];
  1744. }
  1745. return $auth_record;
  1746. }
  1747. function fix_auth_rules($db)
  1748. {
  1749. //cleanup hotspot subnet rules
  1750. delete_record($db, "auth_rules", "ou_id=" . get_const('default_user_ou_id'));
  1751. delete_record($db, "auth_rules", "ou_id=" . get_const('default_hotspot_ou_id'));
  1752. $t_hotspot = get_records_sql($db, "SELECT * FROM subnets WHERE hotspot=1");
  1753. if (!empty($t_hotspot)) {
  1754. foreach ($t_hotspot as $row) {
  1755. delete_record($db, "auth_rules", "rule='" . $row['subnet'] . "'");
  1756. }
  1757. }
  1758. }
  1759. #---------------------------------------------------------------------------------------------------------------
  1760. function new_user($db, $user_info)
  1761. {
  1762. if (!empty($user_info['mac'])) {
  1763. $user['login'] = mac_dotted($user_info['mac']);
  1764. } else {
  1765. $user['login'] = $user_info['ip'];
  1766. }
  1767. if (!empty($user_info['dhcp_hostname'])) {
  1768. $user['fio'] = $user_info['ip'] . '[' . $user_info['dhcp_hostname'] . ']';
  1769. } else {
  1770. $user['fio'] = $user_info['ip'];
  1771. }
  1772. $login_count = get_count_records($db, "User_list", "(login LIKE '" . $user['login'] . "(%)') OR (login='" . $user['login'] . "')");
  1773. if (!empty($login_count) and $login_count > 0) {
  1774. $login_count++;
  1775. $user['login'] = $user['login'] . "(" . $login_count . ")";
  1776. }
  1777. $user['ou_id'] = $user_info['ou_id'];
  1778. $ou_info = get_record_sql($db, "SELECT * FROM OU WHERE id=" . $user_info['ou_id']);
  1779. if (!empty($ou_info)) {
  1780. $user['enabled'] = $ou_info['enabled'];
  1781. if (empty($user['enabled'])) {
  1782. $user['enabled'] = 0;
  1783. }
  1784. $user['queue_id'] = $ou_info['queue_id'];
  1785. if (empty($user['queue_id'])) {
  1786. $user['queue_id'] = 0;
  1787. }
  1788. $user['filter_group_id'] = $ou_info['filter_group_id'];
  1789. if (empty($user['filter_group_id'])) {
  1790. $user['filter_group_id'] = 0;
  1791. }
  1792. }
  1793. $result = insert_record($db, "User_list", $user);
  1794. $auto_mac_rule = get_option($db, 64);
  1795. if (!empty($result) and $auto_mac_rule and $user_info['mac']) {
  1796. $auth_rule['user_id'] = $result;
  1797. $auth_rule['type'] = 2;
  1798. $auth_rule['rule'] = mac_dotted($user_info['mac']);
  1799. insert_record($db, "auth_rules", $auth_rule);
  1800. }
  1801. return $result;
  1802. }
  1803. function new_auth($db, $ip, $mac, $user_id)
  1804. {
  1805. $ip_aton = ip2long($ip);
  1806. $msg = '';
  1807. if (!empty($mac)) {
  1808. $auth_record = get_record_sql($db, "SELECT * FROM User_auth WHERE ip_int=$ip_aton AND mac='" . mac_dotted($mac) . "' AND deleted=0");
  1809. if (!empty($auth_record)) {
  1810. LOG_WARNING($db, "Pair ip-mac already exists! Skip creating $ip [$mac] auth_id: " . $auth_record["id"]);
  1811. return $auth_record['id'];
  1812. }
  1813. }
  1814. // save traffic detailization
  1815. $save_traf = get_option($db, 23);
  1816. $resurrection_id = NULL;
  1817. // seek old auth with same ip and mac
  1818. $resurrection_id = get_id_record($db, 'User_auth', " deleted=1 AND ip_int=" . $ip_aton . " AND mac='" . $mac . "'");
  1819. if (!empty($resurrection_id)) {
  1820. $msg .= "Recovered auth_id: $resurrection_id with ip: $ip and mac: $mac ";
  1821. $auth['user_id'] = $user_id;
  1822. $auth['deleted'] = 0;
  1823. $auth['save_traf'] = $save_traf * 1;
  1824. update_record($db, "User_auth", "id=$resurrection_id", $auth);
  1825. } else {
  1826. // not found ->create new record
  1827. $msg .= "Create new ip record \r\nip: $ip\r\nmac: $mac\r\n";
  1828. $auth['deleted'] = 0;
  1829. $auth['user_id'] = $user_id;
  1830. $auth['ip'] = $ip;
  1831. $auth['ip_int'] = $ip_aton;
  1832. $auth['mac'] = $mac;
  1833. $auth['save_traf'] = $save_traf * 1;
  1834. $resurrection_id = insert_record($db, "User_auth", $auth);
  1835. }
  1836. //check rules, update filter and state for new record
  1837. if (!empty($resurrection_id)) {
  1838. $auth = apply_auth_rule($db, $auth, $user_id);
  1839. update_record($db, "User_auth", "id=$resurrection_id", $auth);
  1840. if (!is_hotspot($db, $ip) and !empty($msg)) {
  1841. LOG_WARNING($db, $msg);
  1842. }
  1843. if (is_hotspot($db, $ip) and !empty($msg)) {
  1844. LOG_INFO($db, $msg);
  1845. }
  1846. }
  1847. return $resurrection_id;
  1848. }
  1849. function resurrection_auth($db, $ip_record)
  1850. {
  1851. $ip = $ip_record['ip'];
  1852. $mac = $ip_record['mac'];
  1853. $action = $ip_record['type'];
  1854. $dhcp_hostname = $ip_record['hostname'];
  1855. $hotspot_found = $ip_record['hotspot'];
  1856. $ip_aton = ip2long($ip);
  1857. $auth_record = get_record_sql($db, "SELECT * FROM User_auth WHERE ip_int=$ip_aton AND mac='" . $mac . "' AND deleted=0");
  1858. if (!empty($auth_record)) {
  1859. $user_info = get_record_sql($db, "SELECT * FROM User_list WHERE id=" . $auth_record['user_id']);
  1860. LOG_DEBUG($db, "external dhcp user " . $user_info['login'] . " [" . $ip . "] auth_id: " . $auth_record['id']);
  1861. if (isset($dhcp_hostname) and !empty($dhcp_hostname)) {
  1862. $auth['dhcp_hostname'] = $dhcp_hostname;
  1863. }
  1864. $auth['dhcp_action'] = $action;
  1865. $auth['dhcp_time'] = GetNowTimeString();
  1866. if ($action === 'add') {
  1867. $auth['last_found'] = GetNowTimeString();
  1868. }
  1869. update_record($db, "User_auth", "id=" . $auth_record['id'], $auth);
  1870. return $auth_record['id'];
  1871. }
  1872. $ip_subnet = get_ip_subnet($db, $ip);
  1873. if ($ip_subnet['static']) {
  1874. LOG_WARNING($db, "Unknown pair ip+mac in static subnet! ip: $ip mac: [" . mac_dotted($mac) . "]. Skip");
  1875. return;
  1876. }
  1877. $msg = '';
  1878. // search changed mac
  1879. $auth_record = get_record_sql($db, "SELECT * FROM User_auth WHERE ip_int=$ip_aton AND deleted=0");
  1880. if (!empty($auth_record)) {
  1881. if (empty($auth_record['mac'])) {
  1882. $auth['mac'] = mac_dotted($mac);
  1883. $auth['dhcp_action'] = $action;
  1884. $auth['dhcp_time'] = GetNowTimeString();
  1885. if (!empty($dhcp_hostname)) {
  1886. $auth['dhcp_hostname'] = $dhcp_hostname;
  1887. }
  1888. if ($action === 'add') {
  1889. $auth['last_found'] = GetNowTimeString();
  1890. }
  1891. LOG_INFO($db, "for ip: $ip mac not found! Use empty record...");
  1892. update_record($db, "User_auth", "id=" . $auth_record['id'], $auth);
  1893. return $auth_record['id'];
  1894. } else {
  1895. if (!$hotspot_found) {
  1896. 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']);
  1897. }
  1898. run_sql($db, "UPDATE User_auth SET changed=1, deleted=1 WHERE id=" . $auth_record['id']);
  1899. }
  1900. }
  1901. // default id
  1902. $new_user_info = get_new_user_id($db, $ip, $mac, $dhcp_hostname);
  1903. if (!empty($new_user_info['user_id'])) {
  1904. $new_user_id = $new_user_info['user_id'];
  1905. }
  1906. if (empty($new_user_id)) {
  1907. $new_user_id = new_user($db, $new_user_info);
  1908. }
  1909. $resurrection_id = NULL;
  1910. $save_traf = get_option($db, 23);
  1911. $auth_record = get_record_sql($db, "SELECT * FROM User_auth WHERE ip_int=" . $ip_aton . " and mac='" . $mac . "'");
  1912. // seek old auth with same ip and mac
  1913. if (!empty($auth_record)) {
  1914. // found ->Resurrection old record
  1915. $resurrection_id = $auth_record['id'];
  1916. $msg .= "Recovered auth_id: $resurrection_id with ip: $ip and mac: $mac ";
  1917. $auth['dhcp_action'] = $action;
  1918. $auth['user_id'] = $new_user_id;
  1919. $auth['deleted'] = 0;
  1920. $auth['dhcp_time'] = GetNowTimeString();
  1921. $auth['save_traf'] = $save_traf * 1;
  1922. if (!empty($dhcp_hostname)) {
  1923. $auth['dhcp_hostname'] = $dhcp_hostname;
  1924. }
  1925. if ($action === 'add') {
  1926. $auth['last_found'] = GetNowTimeString();
  1927. }
  1928. update_record($db, "User_auth", "id=$resurrection_id", $auth);
  1929. } else {
  1930. // not found ->create new record
  1931. $msg .= "Создаём новый ip-адрес \r\nip: $ip\r\nmac: $mac\r\n";
  1932. $auth['deleted'] = 0;
  1933. $auth['user_id'] = $new_user_id;
  1934. $auth['ip'] = $ip;
  1935. $auth['ip_int'] = $ip_aton;
  1936. $auth['mac'] = $mac;
  1937. $auth['dhcp_action'] = $action;
  1938. $auth['dhcp_time'] = GetNowTimeString();
  1939. $auth['save_traf'] = $save_traf * 1;
  1940. if (!empty($dhcp_hostname)) {
  1941. $auth['dhcp_hostname'] = $dhcp_hostname;
  1942. }
  1943. if ($action == 'add') {
  1944. $auth['last_found'] = GetNowTimeString();
  1945. }
  1946. $resurrection_id = insert_record($db, "User_auth", $auth);
  1947. }
  1948. //check rules, update filter and state for new record
  1949. if (!empty($resurrection_id)) {
  1950. $auth = apply_auth_rule($db, $auth, $new_user_id);
  1951. update_record($db, "User_auth", "id=$resurrection_id", $auth);
  1952. $msg .= "filter: " . $auth['filter_group_id'] . "\r\n queue_id: " . $auth['queue_id'] . "\r\n enabled: " . $auth['enabled'] . "\r\nid: $resurrection_id";
  1953. if (!$hotspot_found and !empty($msg)) {
  1954. LOG_WARNING($db, $msg);
  1955. }
  1956. if ($hotspot_found and !empty($msg)) {
  1957. LOG_INFO($db, $msg);
  1958. }
  1959. }
  1960. return $resurrection_id;
  1961. }
  1962. function get_auth($db, $current_auth)
  1963. {
  1964. if (!isset($current_auth)) {
  1965. return;
  1966. }
  1967. if ($current_auth == 0) {
  1968. return;
  1969. }
  1970. $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");
  1971. list($f_login, $f_ip) = mysqli_fetch_array($t_login);
  1972. $result = $f_login . "[" . $f_ip . "]";
  1973. return $result;
  1974. }
  1975. function get_auth_by_mac($db, $mac)
  1976. {
  1977. if (!isset($mac)) {
  1978. return;
  1979. }
  1980. $mac = mac_dotted($mac);
  1981. $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");
  1982. list($f_id, $f_login, $f_auth_id, $f_ip) = mysqli_fetch_array($t_login);
  1983. if (isset($f_id)) {
  1984. $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>';
  1985. } else {
  1986. $result['auth'] = 'Unknown';
  1987. }
  1988. $result['mac'] = expand_mac($db, $mac);
  1989. return $result;
  1990. }
  1991. function get_auth_mac($db, $current_auth)
  1992. {
  1993. if (!isset($current_auth)) {
  1994. return;
  1995. }
  1996. if ($current_auth == 0) {
  1997. return;
  1998. }
  1999. $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");
  2000. list($f_login, $f_mac) = mysqli_fetch_array($t_login);
  2001. $result = $f_login . "[" . $f_mac . "]";
  2002. return $result;
  2003. }
  2004. function add_auth_rule($db, $rule, $type, $user_id)
  2005. {
  2006. $new['user_id'] = $user_id;
  2007. $new['type'] = $type;
  2008. $new['rule'] = $rule;
  2009. $rule_id = 0;
  2010. $auth_rules = get_record_sql($db, "SELECT * FROM auth_rules WHERE rule='" . $rule . "' AND type=" . $type);
  2011. if (empty($auth_rules)) {
  2012. $rule_id = insert_record($db, "auth_rules", $new);
  2013. LOG_INFO($db, "Create auto rule for user_id: " . $user_id . " rule: " . $rule . " type: " . $type);
  2014. } else {
  2015. if ($auth_rules['user_id'] === $user_id) {
  2016. $rule_id = update_record($db, "auth_rules", "id=" . $auth_rules['id'], $new);
  2017. LOG_INFO($db, "Replaced auto rule for user_id: " . $user_id . " rule: " . $rule . " type: " . $type);
  2018. } else {
  2019. 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']);
  2020. $rule_id = 0;
  2021. }
  2022. }
  2023. return $rule_id;
  2024. }
  2025. function update_auth_rule($db, $rule, $type, $rule_id = 0)
  2026. {
  2027. $new['type'] = $type;
  2028. $new['rule'] = $rule;
  2029. $auth_rules = get_record_sql($db, "SELECT * FROM auth_rules WHERE rule='" . $rule . "' AND type=" . $type . " AND id<>" . $rule_id);
  2030. if (empty($auth_rules)) {
  2031. $rule_id = update_record($db, "auth_rules", "id=" . $rule_id, $new);
  2032. } else {
  2033. LOG_WARNING($db, "Create auto rule id: " . $rule_id . " rule: " . $rule . " type: " . $type . " failed! Already exists at user_id: " . $auth_rules['user_id']);
  2034. $rule_id = 0;
  2035. }
  2036. return $rule_id;
  2037. }
  2038. //action: add,update,del
  2039. function allow_update($table, $action = 'update', $field = '')
  2040. {
  2041. //always allow modification for tables
  2042. if (preg_match('/(variables|dns_cache|worklog|sessions)/i', $table)) {
  2043. return 1;
  2044. }
  2045. if (isset($_SESSION['login'])) {
  2046. $work_user = $_SESSION['login'];
  2047. }
  2048. if (isset($_SESSION['user_id'])) {
  2049. $work_id = $_SESSION['user_id'];
  2050. }
  2051. if (isset($_SESSION['acl'])) {
  2052. $user_level = $_SESSION['acl'];
  2053. }
  2054. if (!isset($work_user) or !isset($work_id) or empty($user_level)) {
  2055. return 0;
  2056. }
  2057. //always allow Administrator
  2058. if ($user_level == 1) {
  2059. return 1;
  2060. }
  2061. //always forbid ViewOnly
  2062. if ($user_level == 3) {
  2063. return 0;
  2064. }
  2065. //allow tables for Operator
  2066. if (preg_match('/(dns_queue|User_auth_alias)/i', $table)) {
  2067. return 1;
  2068. }
  2069. if ($action == 'update') {
  2070. $operator_acl = [
  2071. 'User_auth' => [
  2072. 'comments' => '1',
  2073. 'dns_name' => '1',
  2074. 'firmware' => '1',
  2075. 'link_check' => '1',
  2076. 'nagios' => '1',
  2077. 'nagios_handler' => '1',
  2078. 'Wikiname' => '1'
  2079. ],
  2080. 'User_list' => [
  2081. 'fio' => '1',
  2082. 'login' => '1',
  2083. ],
  2084. ];
  2085. if (!isset($operator_acl[$table])) {
  2086. return 0;
  2087. }
  2088. if (isset($operator_acl[$table]) and empty($field)) {
  2089. return 1;
  2090. }
  2091. if (!isset($operator_acl[$table][$field])) {
  2092. return 0;
  2093. }
  2094. if (empty($operator_acl[$table][$field]) or $operator_acl[$table][$field] == '0') {
  2095. return 0;
  2096. }
  2097. return 1;
  2098. }
  2099. return 0;
  2100. }
  2101. function LOG_INFO($db, $msg, $auth_id = 0)
  2102. {
  2103. if (get_const('log_level') < L_INFO) {
  2104. return;
  2105. }
  2106. write_log($db, $msg, L_INFO, $auth_id);
  2107. }
  2108. function LOG_ERROR($db, $msg, $auth_id = 0)
  2109. {
  2110. if (get_const('log_level') < L_ERROR) {
  2111. return;
  2112. }
  2113. email(L_ERROR, $msg);
  2114. write_log($db, $msg, L_ERROR, $auth_id);
  2115. }
  2116. function LOG_VERBOSE($db, $msg, $auth_id = 0)
  2117. {
  2118. if (get_const('log_level') < L_VERBOSE) {
  2119. return;
  2120. }
  2121. write_log($db, $msg, L_VERBOSE, $auth_id);
  2122. }
  2123. function LOG_WARNING($db, $msg, $auth_id = 0)
  2124. {
  2125. if (get_const('log_level') < L_WARNING) {
  2126. return;
  2127. }
  2128. email(L_WARNING, $msg);
  2129. write_log($db, $msg, L_WARNING, $auth_id);
  2130. }
  2131. function LOG_DEBUG($db, $msg, $auth_id = 0)
  2132. {
  2133. if (!empty(get_const('debug')) and get_const('debug')) {
  2134. write_log($db, $msg, L_DEBUG, $auth_id);
  2135. }
  2136. }
  2137. function truncateByWords($string, $length = 100)
  2138. {
  2139. if (strlen($string) <= $length) {
  2140. return $string;
  2141. }
  2142. $wrapped = wordwrap($string, $length);
  2143. $shortened = substr($wrapped, 0, strpos($wrapped, "\n"));
  2144. return $shortened;
  2145. }
  2146. function get_first_line($msg)
  2147. {
  2148. if (empty($msg)) {
  2149. return;
  2150. }
  2151. preg_match('/(.*)(\n|\<br\>)/', $msg, $matches);
  2152. if (!empty($matches[1])) {
  2153. return $matches[1];
  2154. }
  2155. return truncateByWords($msg, 80);
  2156. }
  2157. function email($level, $msg)
  2158. {
  2159. if (!get_const('send_email')) {
  2160. return;
  2161. }
  2162. if (!($level === L_WARNING or $level === L_ERROR)) {
  2163. return;
  2164. }
  2165. // Generate a boundary string
  2166. $boundary = md5(time());
  2167. // Headers
  2168. $headers = array(
  2169. 'From' => get_const('sender_email'),
  2170. 'Reply-To' => get_const('sender_email'),
  2171. 'X-Mailer' => 'PHP',
  2172. 'MIME-Version' => '1.0',
  2173. 'Content-Type' => 'multipart/mixed; boundary=' . $boundary
  2174. );
  2175. $subject = get_first_line($msg);
  2176. if ($level === L_WARNING) {
  2177. $subject = "WARN: " . $subject . "...";
  2178. $message = 'WARNING! Manager: ' . $_SESSION['login'] . ' <br>' . $msg . '<br>';
  2179. }
  2180. if ($level === L_ERROR) {
  2181. $subject = "ERROR: " . $subject . "...";
  2182. $message = 'ERROR! Manager: ' . $_SESSION['login'] . ' <br>' . $msg . '<br>';
  2183. }
  2184. // HTML part
  2185. $html_message = "<html><body><h1>$message</h1></body></html>";
  2186. $html_encoded = chunk_split(base64_encode($html_message));
  2187. // Create the message body
  2188. $message = "";
  2189. $message .= "--" . $boundary . "\r\n";
  2190. $message .= "Content-Type: text/html; charset=UTF-8\r\n";
  2191. $message .= "Content-Transfer-Encoding: base64\r\n\r\n";
  2192. $message .= $html_encoded . "\r\n";
  2193. $message .= "--" . $boundary . "--";
  2194. // Send email
  2195. mail(get_const('admin_email'), $subject, $message, $headers);
  2196. }
  2197. function write_log($db, $msg, $level, $auth_id = 0)
  2198. {
  2199. $work_user = 'http';
  2200. if (isset($_SESSION['login'])) {
  2201. $work_user = $_SESSION['login'];
  2202. }
  2203. if (!isset($msg)) {
  2204. $msg = 'ERROR! Empty log string!';
  2205. }
  2206. if (!isset($level)) {
  2207. $level = L_INFO;
  2208. }
  2209. $msg = str_replace("'", '', $msg);
  2210. $sSQL = "insert into worklog(customer,message,level,auth_id) values('$work_user','$msg',$level,$auth_id)";
  2211. mysqli_query($db, $sSQL);
  2212. }
  2213. function print_year_select($year_name, $year)
  2214. {
  2215. print "<select name=\"$year_name\" >\n";
  2216. for ($i = $year - 10; $i <= $year + 10; $i++) {
  2217. print_select_item($i, $i, $year);
  2218. }
  2219. print "</select>\n";
  2220. }
  2221. function print_date_select($dd, $mm, $yy)
  2222. {
  2223. if ($dd >= 1) {
  2224. print "<b>День</b>\n";
  2225. print "<select name=\"day\" >\n";
  2226. for ($i = 1; $i <= 31; $i++) {
  2227. print_select_item($i, $i, $dd);
  2228. }
  2229. print "</select>\n";
  2230. }
  2231. if ($mm >= 1) {
  2232. print "<b>Месяц</b>\n";
  2233. print "<select name=\"month\" >\n";
  2234. for ($i = 1; $i <= 12; $i++) {
  2235. $tmp_date = DateTimeImmutable::createFromFormat('U', strtotime("$i/01/$yy"));
  2236. $month_name = $tmp_date->format('F');
  2237. print_select_item($month_name, $i, $mm);
  2238. }
  2239. print "</select>\n";
  2240. }
  2241. print "<b>Год</b>\n";
  2242. print_year_select('year', $yy);
  2243. }
  2244. function print_date_select2($dd, $mm, $yy)
  2245. {
  2246. if ($dd >= 1) {
  2247. print "<b>День</b>\n";
  2248. print "<select name=\"day2\" >\n";
  2249. for ($i = 1; $i <= 31; $i++) {
  2250. print_select_item($i, $i, $dd);
  2251. }
  2252. print "</select>\n";
  2253. }
  2254. if ($mm >= 1) {
  2255. print "<b>Месяц</b>\n";
  2256. print "<select name=\"month2\" >\n";
  2257. for ($i = 1; $i <= 12; $i++) {
  2258. $tmp_date = DateTimeImmutable::createFromFormat('U', strtotime("$i/01/$yy"));
  2259. $month_name = $tmp_date->format('F');
  2260. print_select_item($month_name, $i, $mm);
  2261. }
  2262. print "</select>\n";
  2263. }
  2264. print "<b>Год</b>\n";
  2265. print_year_select('year2', $yy);
  2266. }
  2267. function is_up($ip)
  2268. {
  2269. if (!isset($ip) or strlen($ip) == 0) {
  2270. return false;
  2271. }
  2272. exec(sprintf('ping -i .3 -c 1 -W 5 %s', escapeshellarg($ip)), $res, $rval);
  2273. return $rval == 0;
  2274. }
  2275. function apply_device_lock($db, $device_id, $iteration = 0)
  2276. {
  2277. $iteration++;
  2278. if ($iteration > 2) {
  2279. return false;
  2280. }
  2281. $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');
  2282. if (empty($dev) or empty($dev['u_locked_timestamp'])) {
  2283. LOG_DEBUG($db, "Snmp discovery lock not found. Set and discovery.");
  2284. return set_lock_discovery($db, $device_id);
  2285. }
  2286. //wait for discovery
  2287. $now = time();
  2288. $wait_time = ($dev['u_locked_timestamp'] + SNMP_LOCK_TIMEOUT) - $now;
  2289. LOG_DEBUG($db, "Check snmp lock for device id: " . $device_id . ". Lock timestamp: " . $dev['u_locked_timestamp'] . ", now: " . $now);
  2290. if ($wait_time < 0) {
  2291. LOG_DEBUG($db, "The lock is already expired. Set new lock.");
  2292. return set_lock_discovery($db, $device_id);
  2293. }
  2294. LOG_VERBOSE($db, "Snmp discovery lock for device id: $device_id found! Need wait " . $wait_time . " sec.");
  2295. sleep($wait_time);
  2296. LOG_VERBOSE($db, "Try set new lock and continue discovery for device id:" . $device_id);
  2297. return apply_device_lock($db, $device_id, $iteration);
  2298. }
  2299. function set_lock_discovery($db, $device_id)
  2300. {
  2301. $new['discovery_locked'] = 1;
  2302. $new['locked_timestamp'] = GetNowTimeString();
  2303. if (update_record($db, 'devices', 'id=' . $device_id, $new)) {
  2304. return true;
  2305. }
  2306. return false;
  2307. }
  2308. function unset_lock_discovery($db, $device_id)
  2309. {
  2310. $new['discovery_locked'] = 0;
  2311. $new['locked_timestamp'] = GetNowTimeString();
  2312. if (update_record($db, 'devices', 'id=' . $device_id, $new)) {
  2313. return true;
  2314. }
  2315. return false;
  2316. }
  2317. function set_port_for_group($db, $group_id, $place_id, $state)
  2318. {
  2319. $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;
  2320. $auth_list = mysqli_query($db, $authSQL);
  2321. LOG_VERBOSE($db, 'Mass port state change started!');
  2322. // get auth list for group
  2323. while (list($a_id, $a_name, $a_ip) = mysqli_fetch_array($auth_list)) {
  2324. // get device and port for auth
  2325. if ($place_id == 0) {
  2326. $place_filter = '';
  2327. } else {
  2328. $place_filter = 'D.building_id=' . $place_id . ' and ';
  2329. }
  2330. $devSQL = 'SELECT D.id, D.device_name, D.vendor_id, D.device_model, D.ip, D.snmp_version, D.rw_community, DP.port, DP.snmp_index FROM devices AS D, device_ports AS DP, connections AS C WHERE ' . $place_filter . ' D.id = DP.device_id AND DP.id = C.port_id AND C.auth_id=' . $a_id . ' LIMIT 1';
  2331. $dev_info = mysqli_query($db, $devSQL);
  2332. list($d_id, $d_name, $d_vendor_id, $d_model, $d_ip, $d_snmp, $d_community, $d_port, $d_snmp_index) = mysqli_fetch_array($dev_info);
  2333. if (!isset($d_id)) {
  2334. continue;
  2335. }
  2336. if ($state == 1) {
  2337. $mode = 'enable';
  2338. run_sql($db, "Update User_auth set nagios_handler='restart-port' WHERE id=$a_id and nagios_handler='manual-mode'");
  2339. } else {
  2340. $mode = 'disable';
  2341. run_sql($db, "Update User_auth set nagios_handler='manual-mode' WHERE id=$a_id and nagios_handler='restart-port'");
  2342. }
  2343. LOG_INFO($db, "At device $d_name [$d_ip] $mode port $d_port for auth_id: $a_id ($a_ip [$a_name])");
  2344. set_port_state($d_vendor_id, $d_snmp_index, $d_ip, $d_community, $d_snmp, $state);
  2345. set_port_poe_state($d_vendor_id, $d_port, $d_snmp_index, $d_ip, $d_community, $d_snmp, $state);
  2346. }
  2347. LOG_VERBOSE($db, 'Mass port state change stopped.');
  2348. }
  2349. function get_vendor($db, $mac)
  2350. {
  2351. $mac = mac_dotted($mac);
  2352. $mac5 = substr($mac, 0, 14);
  2353. $mac4 = substr($mac, 0, 11);
  2354. $mac3 = substr($mac, 0, 8);
  2355. $vendor = get_record_sql($db, 'SELECT companyName,companyAddress FROM mac_vendors WHERE oui="' . $mac . '"');
  2356. if (empty($vendor)) {
  2357. $vendor = get_record_sql($db, 'SELECT companyName,companyAddress FROM mac_vendors WHERE oui="' . $mac5 . '"');
  2358. }
  2359. if (empty($vendor)) {
  2360. $vendor = get_record_sql($db, 'SELECT companyName,companyAddress FROM mac_vendors WHERE oui="' . $mac4 . '"');
  2361. }
  2362. if (empty($vendor)) {
  2363. $vendor = get_record_sql($db, 'SELECT companyName,companyAddress FROM mac_vendors WHERE oui="' . $mac3 . '"');
  2364. }
  2365. $result = '';
  2366. if (!empty($vendor)) {
  2367. $result = $vendor['companyName'];
  2368. if (!empty($vendor['companyAddress'])) {
  2369. $result = $vendor['companyAddress'];
  2370. }
  2371. }
  2372. return $result;
  2373. }
  2374. function strHexToBin($number)
  2375. {
  2376. $result = '';
  2377. for ($i = 0; $i < strlen($number); $i++) {
  2378. $conv = base_convert($number[$i], 16, 2);
  2379. $result .= str_pad($conv, 4, '0', STR_PAD_LEFT);
  2380. }
  2381. return $result;
  2382. }
  2383. function dec_to_hex($mac)
  2384. {
  2385. if (!isset($mac)) {
  2386. return;
  2387. }
  2388. $mac_array = explode('.', $mac);
  2389. for ($i = 0; $i < count($mac_array); $i++) {
  2390. $hex_i = dechex($mac_array[$i]);
  2391. if (strlen($hex_i) == 1) {
  2392. $hex_i = "0" . $hex_i;
  2393. }
  2394. $mac_array[$i] = $hex_i;
  2395. }
  2396. $hex_mac = implode(':', $mac_array);
  2397. return $hex_mac;
  2398. }
  2399. function mac_simplify($mac)
  2400. {
  2401. if (!isset($mac)) {
  2402. return;
  2403. }
  2404. $mac = strtolower(trim($mac));
  2405. $mac = preg_replace('/(\.|:|-)/', '', $mac);
  2406. return $mac;
  2407. }
  2408. function mac_dotted($mac)
  2409. {
  2410. if (!isset($mac)) {
  2411. return;
  2412. }
  2413. $mac = mac_simplify($mac);
  2414. $mac = preg_replace('/(\S{2})(\S{2})?(\S{2})?(\S{2})?(\S{2})?(\S{2})?/', '$1:$2:$3:$4:$5:$6', $mac);
  2415. $mac = preg_replace('/\:+$/', '', $mac, 5);
  2416. return $mac;
  2417. }
  2418. function unbind_ports($db, $device_id)
  2419. {
  2420. $target = mysqli_query($db, "SELECT U.target_port_id,U.id FROM device_ports U WHERE U.device_id=$device_id");
  2421. while (list($target_id, $id) = mysqli_fetch_array($target)) {
  2422. run_sql($db, "UPDATE device_ports SET target_port_id=0 WHERE target_port_id=" . $id);
  2423. run_sql($db, "UPDATE device_ports SET target_port_id=0 WHERE id=" . $id);
  2424. }
  2425. }
  2426. function bind_ports($db, $port_id, $target_id)
  2427. {
  2428. $old_target = mysqli_query($db, "SELECT U.target_port_id FROM device_ports U WHERE U.id=$port_id");
  2429. list($old_target_id) = mysqli_fetch_array($old_target);
  2430. // unbind current connection
  2431. $new['target_port_id'] = 0;
  2432. update_record($db, "device_ports", "id='$port_id'", $new);
  2433. if (isset($old_target_id)) {
  2434. update_record($db, "device_ports", "id='$old_target_id'", $new);
  2435. }
  2436. // new link
  2437. if (isset($target_id) and $target_id > 0) {
  2438. $new['target_port_id'] = $target_id;
  2439. update_record($db, "device_ports", "id='$port_id'", $new);
  2440. $new['target_port_id'] = $port_id;
  2441. update_record($db, "device_ports", "id='$target_id'", $new);
  2442. }
  2443. }
  2444. function expand_device_name($db, $name)
  2445. {
  2446. $device_id = get_device_id($db, $name);
  2447. $result = $name;
  2448. if (isset($device_id) and $device_id > 0) {
  2449. $result = '<a href=/admin/devices/editdevice.php?id=' . $device_id . '>' . $name . '</a>';
  2450. }
  2451. return $result;
  2452. }
  2453. function expand_mac($db, $msg)
  2454. {
  2455. if (!isset($msg)) {
  2456. return;
  2457. }
  2458. $mac = mac_dotted($msg);
  2459. $vendor_info = get_vendor($db, $mac);
  2460. $result = ' <p title="' . $vendor_info . '"><a href=/admin/logs/mac.php?mac=' . $mac . '>' . $mac . '</a></p>';
  2461. return $result;
  2462. }
  2463. function expand_log_str($db, $msg)
  2464. {
  2465. if (!isset($msg)) {
  2466. return;
  2467. }
  2468. $auth_pattern = '/(auth_id:|auth|auth id:|auth id)\s+(\d+)\s+/i';
  2469. $auth_replace = '<a href=/admin/users/editauth.php?id=${2}>auth_id:${2}</a> ';
  2470. $result = preg_replace($auth_pattern, $auth_replace, $msg);
  2471. $user_pattern = '/(user_id:|user|user id:|user id)\s+(\d+)\s+/i';
  2472. $user_replace = '<a href=/admin/users/edituser.php?id=${2}>user_id:${2}</a> ';
  2473. $result = preg_replace($user_pattern, $user_replace, $result);
  2474. $mac_pattern = '/\s+\[(\w{12})\]\s+/i';
  2475. preg_match($mac_pattern, $result, $matches);
  2476. if (isset($matches[1])) {
  2477. $mac = $matches[1];
  2478. $mac = mac_dotted($mac);
  2479. # $vendor_info = get_vendor($db,$mac);
  2480. # $mac_replace = ' <p title="'.$vendor_info.'"><a href=/admin/logs/mac.php?mac='.$mac.'>'.$mac.'</a></p>';
  2481. $mac_replace = ' <a href=/admin/logs/mac.php?mac=' . $mac . '>' . $mac . '</a> ';
  2482. $result = preg_replace($mac_pattern, $mac_replace, $result);
  2483. }
  2484. $mac_pattern = '/\s+mac:\s+([\w\:]{17})$/i';
  2485. preg_match($mac_pattern, $result, $matches);
  2486. if (isset($matches[1])) {
  2487. $mac = $matches[1];
  2488. $mac = mac_dotted($mac);
  2489. # $vendor_info = get_vendor($db,$mac);
  2490. # $mac_replace = ' mac: <p title="'.$vendor_info.'"><a href=/admin/logs/mac.php?mac='.$mac.'>'.$mac.'</a></p>';
  2491. $mac_replace = ' mac: <a href=/admin/logs/mac.php?mac=' . $mac . '>' . $mac . '</a> ';
  2492. $result = preg_replace($mac_pattern, $mac_replace, $result);
  2493. }
  2494. $device_pattern = '/at device\s+([\w\.\-]+)/i';
  2495. preg_match($device_pattern, $result, $matches);
  2496. if (isset($matches[1])) {
  2497. $device_name = $matches[1];
  2498. $device_id = get_device_id($db, $device_name);
  2499. if (isset($device_id) and $device_id > 0) {
  2500. $device_replace = 'at device <a href=/admin/devices/editdevice.php?id=' . $device_id . '>${1}</a> ';
  2501. $result = preg_replace($device_pattern, $device_replace, $result);
  2502. }
  2503. }
  2504. $device_pattern = '/(device_id:|device id:|device id|device_id)\s+(\d+)\s+/i';
  2505. $device_replace = 'device_id: <a href=/admin/devices/editdevice.php?id=${2}>${2}</a> ';
  2506. $result = preg_replace($device_pattern, $device_replace, $result);
  2507. $ip_pattern = '/\s+ip\:\s+(\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})\s+/i';
  2508. preg_match($ip_pattern, $result, $matches);
  2509. if (isset($matches[1])) {
  2510. $ip = $matches[1];
  2511. $auth_id = get_auth_by_ip($db, $ip);
  2512. if (isset($auth_id) and $auth_id > 0) {
  2513. $auth_replace = ' ip: <a href=/admin/users/editauth.php?id=' . $auth_id . '>${1}</a> ';
  2514. $result = preg_replace($ip_pattern, $auth_replace, $result);
  2515. }
  2516. }
  2517. return $result;
  2518. }
  2519. function get_record_field($db, $table, $field, $filter)
  2520. {
  2521. if (!isset($table)) {
  2522. LOG_ERROR($db, "Search in unknown table! Skip command.");
  2523. return;
  2524. }
  2525. if (!isset($filter)) {
  2526. LOG_ERROR($db, "Search filter is empty! Skip command.");
  2527. return;
  2528. }
  2529. if (!isset($field)) {
  2530. LOG_ERROR($db, "Search field is empty! Skip command.");
  2531. return;
  2532. }
  2533. if (preg_match('/=$/', $filter)) {
  2534. LOG_ERROR($db, "Search record ($table) with illegal filter $filter! Skip command.");
  2535. return;
  2536. }
  2537. $old_sql = "SELECT $field FROM $table WHERE $filter LIMIT 1";
  2538. $old_record = mysqli_query($db, $old_sql) or LOG_ERROR($db, "SQL: $old_sql :" . mysqli_error($db));
  2539. $old = mysqli_fetch_array($old_record, MYSQLI_ASSOC);
  2540. foreach ($old as $key => $value) {
  2541. if (!isset($value) or $value === 'NULL') {
  2542. $value = '';
  2543. }
  2544. $result[$key] = $value;
  2545. }
  2546. return $result[$field];
  2547. }
  2548. function get_record($db, $table, $filter)
  2549. {
  2550. if (!isset($table)) {
  2551. LOG_ERROR($db, "Search in unknown table! Skip command.");
  2552. return;
  2553. }
  2554. if (!isset($filter)) {
  2555. LOG_ERROR($db, "Search filter is empty! Skip command.");
  2556. return;
  2557. }
  2558. if (preg_match('/=$/', $filter)) {
  2559. LOG_ERROR($db, "Search record ($table) with illegal filter $filter! Skip command.");
  2560. return;
  2561. }
  2562. $get_sql = "SELECT * FROM $table WHERE $filter LIMIT 1";
  2563. $get_record = mysqli_query($db, $get_sql);
  2564. if (!$get_record) {
  2565. LOG_ERROR($db, "SQL: $get_sql :" . mysqli_error($db));
  2566. return;
  2567. }
  2568. $fields = [];
  2569. while ($field = mysqli_fetch_field($get_record)) {
  2570. $f_table = $field->table;
  2571. $f_name = $field->name;
  2572. $fields[$f_table][$f_name] = $field;
  2573. }
  2574. $record = mysqli_fetch_array($get_record, MYSQLI_ASSOC);
  2575. $result = NULL;
  2576. if (!empty($record)) {
  2577. foreach ($record as $key => $value) {
  2578. if (!isset($value) or $value === 'NULL' or $value == NULL) {
  2579. if (!empty($key) and !empty($fields[$table]) and !empty($fields[$table][$key])) {
  2580. if (in_array($fields[$table][$key]->type, MYSQL_FIELD_DIGIT)) {
  2581. $value = 0;
  2582. }
  2583. if (in_array($fields[$table][$key]->type, MYSQL_FIELD_STRING)) {
  2584. $value = '';
  2585. }
  2586. }
  2587. }
  2588. if (!empty($key)) {
  2589. $result[$key] = $value;
  2590. }
  2591. }
  2592. }
  2593. return $result;
  2594. }
  2595. function get_records($db, $table, $filter)
  2596. {
  2597. if (!isset($table)) {
  2598. LOG_ERROR($db, "Search in unknown table! Skip command.");
  2599. return;
  2600. }
  2601. if (isset($filter) and preg_match('/=$/', $filter)) {
  2602. LOG_ERROR($db, "Search record ($table) with illegal filter $filter! Skip command.");
  2603. return;
  2604. }
  2605. $s_filter = '';
  2606. if (isset($filter)) {
  2607. $s_filter = 'WHERE ' . $filter;
  2608. }
  2609. $get_sql = "SELECT * FROM $table $s_filter";
  2610. $get_record = mysqli_query($db, $get_sql);
  2611. if (!$get_record) {
  2612. LOG_ERROR($db, "SQL: $get_sql :" . mysqli_error($db));
  2613. return;
  2614. }
  2615. $fields = [];
  2616. while ($field = mysqli_fetch_field($get_record)) {
  2617. $f_table = $field->table;
  2618. $f_name = $field->name;
  2619. $fields[$f_table][$f_name] = $field;
  2620. }
  2621. $result = NULL;
  2622. $index = 0;
  2623. while ($rec = mysqli_fetch_array($get_record, MYSQLI_ASSOC)) {
  2624. foreach ($rec as $key => $value) {
  2625. if (!isset($value) or $value === 'NULL' or $value == NULL) {
  2626. if (!empty($key) and !empty($fields[$table]) and !empty($fields[$table][$key])) {
  2627. if (in_array($fields[$table][$key]->type, MYSQL_FIELD_DIGIT)) {
  2628. $value = 0;
  2629. }
  2630. if (in_array($fields[$table][$key]->type, MYSQL_FIELD_STRING)) {
  2631. $value = '';
  2632. }
  2633. }
  2634. }
  2635. $result[$index][$key] = $value;
  2636. }
  2637. $index++;
  2638. }
  2639. return $result;
  2640. }
  2641. function get_records_sql($db, $sql)
  2642. {
  2643. $result = NULL;
  2644. if (empty($sql)) {
  2645. LOG_ERROR($db, "Empty query! Skip command.");
  2646. return $result;
  2647. }
  2648. $records = mysqli_query($db, $sql);
  2649. if (!$records) {
  2650. LOG_ERROR($db, "SQL: $sql :" . mysqli_error($db));
  2651. return $result;
  2652. }
  2653. $fields = [];
  2654. //we assume that fields with the same name have the same type
  2655. while ($field = mysqli_fetch_field($records)) {
  2656. $f_name = $field->name;
  2657. $fields[$f_name] = $field;
  2658. }
  2659. $index = 0;
  2660. while ($rec = mysqli_fetch_array($records, MYSQLI_ASSOC)) {
  2661. foreach ($rec as $key => $value) {
  2662. if (!isset($value) or $value === 'NULL' or $value == NULL) {
  2663. if (!empty($key) and !empty($fields[$key])) {
  2664. if (in_array($fields[$key]->type, MYSQL_FIELD_DIGIT)) {
  2665. $value = 0;
  2666. }
  2667. if (in_array($fields[$key]->type, MYSQL_FIELD_STRING)) {
  2668. $value = '';
  2669. }
  2670. }
  2671. }
  2672. if (!empty($key)) {
  2673. $result[$index][$key] = $value;
  2674. }
  2675. }
  2676. $index++;
  2677. }
  2678. return $result;
  2679. }
  2680. function get_record_sql($db, $sql)
  2681. {
  2682. $result = NULL;
  2683. if (!isset($sql)) {
  2684. LOG_ERROR($db, "Empty query! Skip command.");
  2685. return $result;
  2686. }
  2687. $record = mysqli_query($db, $sql . " LIMIT 1");
  2688. if (!isset($record)) {
  2689. LOG_ERROR($db, "SQL: $sql LIMIT 1: " . mysqli_error($db));
  2690. return $result;
  2691. }
  2692. $fields = [];
  2693. //we assume that fields with the same name have the same type
  2694. while ($field = mysqli_fetch_field($record)) {
  2695. $f_name = $field->name;
  2696. $fields[$f_name] = $field;
  2697. }
  2698. $rec = mysqli_fetch_array($record, MYSQLI_ASSOC);
  2699. if (!empty($rec)) {
  2700. foreach ($rec as $key => $value) {
  2701. if (!isset($value) or $value === 'NULL' or $value == NULL) {
  2702. if (!empty($key) and !empty($fields[$key])) {
  2703. if (in_array($fields[$key]->type, MYSQL_FIELD_DIGIT)) {
  2704. $value = 0;
  2705. }
  2706. if (in_array($fields[$key]->type, MYSQL_FIELD_STRING)) {
  2707. $value = '';
  2708. }
  2709. }
  2710. }
  2711. if (!empty($key)) {
  2712. $result[$key] = $value;
  2713. }
  2714. }
  2715. }
  2716. return $result;
  2717. }
  2718. function is_auth_bind_changed($db, $id, $ip, $mac)
  2719. {
  2720. $old_sql = "SELECT ip,mac FROM User_auth WHERE id=$id";
  2721. $old_record = get_record_sql($db, $old_sql);
  2722. if (empty($old_record["ip"]) or empty($old_record["mac"])) {
  2723. return 0;
  2724. }
  2725. if ($old_record["ip"] !== $ip or $old_record["mac"] !== $mac) {
  2726. LOG_VERBOSE($db, "Changed ip or mac for auth record!");
  2727. return 1;
  2728. }
  2729. return 0;
  2730. }
  2731. function copy_auth($db, $id, $new_auth)
  2732. {
  2733. $old_record = get_record_sql($db, "SELECT * FROM User_auth WHERE id=$id");
  2734. delete_record($db, "User_auth", "id=" . $id);
  2735. $new_auth["user_id"] = $old_record["user_id"];
  2736. $new_auth["changed"] = 1;
  2737. $changed_time = GetNowTimeString();
  2738. $new_auth["changed_time"] = $changed_time;
  2739. $new_id = insert_record($db, "User_auth", $new_auth);
  2740. LOG_VERBOSE($db, "Old record with id: $id deleted. Created new auth record for new ip+mac id: $new_id!");
  2741. return $new_id;
  2742. }
  2743. function get_dns_name($db, $id)
  2744. {
  2745. $auth_record = get_record_sql($db, "SELECT dns_name FROM User_auth WHERE id=" . $id);
  2746. if (!empty($auth_record) and !empty($auth_record['dns_name'])) {
  2747. return $auth_record['dns_name'];
  2748. }
  2749. return '';
  2750. }
  2751. function update_record($db, $table, $filter, $newvalue)
  2752. {
  2753. if (!isset($table)) {
  2754. LOG_WARNING($db, "Change record for unknown table! Skip command.");
  2755. return;
  2756. }
  2757. if (!isset($filter)) {
  2758. LOG_WARNING($db, "Change record ($table) with empty filter! Skip command.");
  2759. return;
  2760. }
  2761. if (preg_match('/=$/', $filter)) {
  2762. LOG_WARNING($db, "Change record ($table) with illegal filter $filter! Skip command.");
  2763. return;
  2764. }
  2765. if (!isset($newvalue)) {
  2766. LOG_WARNING($db, "Change record ($table [ $filter ]) with empty data! Skip command.");
  2767. return;
  2768. }
  2769. if (!allow_update($table, 'update')) {
  2770. LOG_INFO($db, "Access denied: $table [ $filter ]");
  2771. return 1;
  2772. }
  2773. $old_sql = "SELECT * FROM $table WHERE $filter";
  2774. $old_record = mysqli_query($db, $old_sql) or LOG_ERROR($db, "SQL: $old_sql :" . mysqli_error($db));
  2775. $old = mysqli_fetch_array($old_record, MYSQLI_ASSOC);
  2776. $rec_id = NULL;
  2777. if (!empty($old['id'])) {
  2778. $rec_id = $old['id'];
  2779. }
  2780. $changed_log = '';
  2781. $run_sql = '';
  2782. $network_changed = 0;
  2783. $dhcp_changed = 0;
  2784. $dns_changed = 0;
  2785. $acl_fields = [
  2786. 'ip' => '1',
  2787. 'ip_int' => '1',
  2788. 'enabled' => '1',
  2789. 'dhcp' => '1',
  2790. 'filter_group_id' => '1',
  2791. 'deleted' => '1',
  2792. 'dhcp_acl' => '1',
  2793. 'queue_id' => '1',
  2794. 'mac' => '1',
  2795. 'blocked' => '1',
  2796. ];
  2797. $dhcp_fields = [
  2798. 'ip' => '1',
  2799. 'dhcp' => '1',
  2800. 'deleted' => '1',
  2801. 'mac' => '1',
  2802. ];
  2803. $dns_fields = [
  2804. 'ip' => '1',
  2805. 'dns_name' => '1',
  2806. 'alias' => '1',
  2807. ];
  2808. foreach ($newvalue as $key => $value) {
  2809. if (!allow_update($table, 'update', $key)) {
  2810. continue;
  2811. }
  2812. if (!isset($value)) {
  2813. $value = '';
  2814. }
  2815. $value = trim($value);
  2816. if (strcmp($old[$key], $value) == 0) {
  2817. continue;
  2818. }
  2819. if ($table === "User_auth") {
  2820. if (!empty($acl_fields["$key"])) {
  2821. $network_changed = 1;
  2822. }
  2823. if (!empty($dhcp_fields["$key"])) {
  2824. $dhcp_changed = 1;
  2825. }
  2826. if (!empty($dns_fields["$key"])) {
  2827. $dns_changed = 1;
  2828. }
  2829. }
  2830. if ($table === "User_auth_alias") {
  2831. if (!empty($dns_fields["$key"])) {
  2832. $dns_changed = 1;
  2833. }
  2834. }
  2835. if (!preg_match('/password/i', $key)) {
  2836. $changed_log = $changed_log . " $key => $value (old: $old[$key]),";
  2837. }
  2838. $run_sql = $run_sql . " `" . $key . "`='" . mysqli_real_escape_string($db, $value) . "',";
  2839. }
  2840. if ($table === "User_auth" and $dns_changed) {
  2841. if (!empty($old['dns_name']) and !empty($old['ip'])) {
  2842. $del_dns['name_type'] = 'A';
  2843. $del_dns['name'] = $old['dns_name'];
  2844. $del_dns['value'] = $old['ip'];
  2845. $del_dns['type'] = 'del';
  2846. if (!empty($rec_id)) {
  2847. $del_dns['auth_id'] = $rec_id;
  2848. }
  2849. insert_record($db, 'dns_queue', $del_dns);
  2850. }
  2851. if (!empty($newvalue['dns_name']) and !empty($newvalue['ip'])) {
  2852. $new_dns['name_type'] = 'A';
  2853. $new_dns['name'] = $newvalue['dns_name'];
  2854. $new_dns['value'] = $newvalue['ip'];
  2855. $new_dns['type'] = 'add';
  2856. if (!empty($rec_id)) {
  2857. $new_dns['auth_id'] = $rec_id;
  2858. }
  2859. insert_record($db, 'dns_queue', $new_dns);
  2860. }
  2861. }
  2862. if ($table === "User_auth_alias" and $dns_changed) {
  2863. $auth_id = NULL;
  2864. if ($old['auth_id']) {
  2865. $auth_id = $old['auth_id'];
  2866. }
  2867. if (!empty($old['alias'])) {
  2868. $del_dns['name_type'] = 'CNAME';
  2869. $del_dns['name'] = $old['alias'];
  2870. $del_dns['type'] = 'del';
  2871. if (!empty($auth_id)) {
  2872. $del_dns['auth_id'] = $auth_id;
  2873. $del_dns['value'] = get_dns_name($db, $auth_id);
  2874. }
  2875. insert_record($db, 'dns_queue', $del_dns);
  2876. }
  2877. if (!empty($newvalue['alias'])) {
  2878. $new_dns['name_type'] = 'CNAME';
  2879. $new_dns['name'] = $newvalue['alias'];
  2880. $new_dns['type'] = 'add';
  2881. if (!empty($auth_id)) {
  2882. $new_dns['auth_id'] = $auth_id;
  2883. $new_dns['value'] = get_dns_name($db, $auth_id);
  2884. }
  2885. insert_record($db, 'dns_queue', $new_dns);
  2886. }
  2887. }
  2888. if (empty($run_sql)) {
  2889. return 1;
  2890. }
  2891. if ($network_changed) {
  2892. $run_sql = $run_sql . " `changed`='1',";
  2893. }
  2894. if ($dhcp_changed) {
  2895. $run_sql = $run_sql . " `dhcp_changed`='1',";
  2896. }
  2897. $changed_log = substr_replace($changed_log, "", -1);
  2898. $run_sql = substr_replace($run_sql, "", -1);
  2899. if ($table === 'User_auth') {
  2900. $changed_time = GetNowTimeString();
  2901. $run_sql = $run_sql . ", `changed_time`='" . $changed_time . "'";
  2902. }
  2903. $new_sql = "UPDATE $table SET $run_sql WHERE $filter";
  2904. LOG_DEBUG($db, "Run sql: $new_sql");
  2905. $sql_result = mysqli_query($db, $new_sql) or LOG_ERROR($db, "SQL: $new_sql :" . mysqli_error($db));
  2906. if (!$sql_result) {
  2907. LOG_ERROR($db, "UPDATE Request: $new_sql :" . mysqli_error($db));
  2908. return;
  2909. }
  2910. if ($table !== "sessions") {
  2911. LOG_VERBOSE($db, "Change table $table WHERE $filter set $changed_log");
  2912. }
  2913. return $sql_result;
  2914. }
  2915. function delete_record($db, $table, $filter)
  2916. {
  2917. if (!allow_update($table, 'del')) {
  2918. LOG_WARNING($db, "User does not have write permission");
  2919. return;
  2920. }
  2921. if (!isset($table)) {
  2922. LOG_WARNING($db, "Delete FROM unknown table! Skip command.");
  2923. return;
  2924. }
  2925. if (!isset($filter)) {
  2926. LOG_WARNING($db, "Delete FROM table $table with empty filter! Skip command.");
  2927. return;
  2928. }
  2929. if (preg_match('/=$/', $filter)) {
  2930. LOG_WARNING($db, "Change record ($table) with illegal filter $filter! Skip command.");
  2931. return;
  2932. }
  2933. $old_sql = "SELECT * FROM $table WHERE $filter";
  2934. $old_record = mysqli_query($db, $old_sql) or LOG_ERROR($db, "SQL: $old_sql :" . mysqli_error($db));
  2935. $old = mysqli_fetch_array($old_record, MYSQLI_ASSOC);
  2936. $rec_id = NULL;
  2937. if (!empty($old['id'])) {
  2938. $rec_id = $old['id'];
  2939. }
  2940. $changed_log = 'record: ';
  2941. if (!empty($old)) {
  2942. asort($old, SORT_STRING);
  2943. $old = array_reverse($old, 1);
  2944. foreach ($old as $key => $value) {
  2945. if (empty($value)) {
  2946. continue;
  2947. }
  2948. if (preg_match('/action/', $key)) {
  2949. continue;
  2950. }
  2951. if (preg_match('/status/', $key)) {
  2952. continue;
  2953. }
  2954. if (preg_match('/time/', $key)) {
  2955. continue;
  2956. }
  2957. if (preg_match('/found/', $key)) {
  2958. continue;
  2959. }
  2960. $changed_log = $changed_log . " $key => $value,";
  2961. }
  2962. }
  2963. $delete_it = 1;
  2964. //never delete user ip record or dns alias record
  2965. if ($table === 'User_auth') {
  2966. $delete_it = 0;
  2967. $changed_time = GetNowTimeString();
  2968. $new_sql = "UPDATE $table SET deleted=1, changed=1, `changed_time`='" . $changed_time . "' WHERE $filter";
  2969. LOG_DEBUG($db, "Run sql: $new_sql");
  2970. $sql_result = mysqli_query($db, $new_sql) or LOG_ERROR($db, "SQL: $new_sql :" . mysqli_error($db));
  2971. if (!$sql_result) {
  2972. LOG_ERROR($db, "UPDATE Request (from delete): " . mysqli_error($db));
  2973. return;
  2974. }
  2975. //dns
  2976. if (!empty($old['dns_name']) and !empty($old['ip'])) {
  2977. $del_dns['name_type'] = 'A';
  2978. $del_dns['name'] = $old['dns_name'];
  2979. $del_dns['value'] = $old['ip'];
  2980. $del_dns['type'] = 'del';
  2981. if (!empty($rec_id)) {
  2982. $del_dns['auth_id'] = $rec_id;
  2983. }
  2984. insert_record($db, 'dns_queue', $del_dns);
  2985. }
  2986. }
  2987. if ($table === 'User_auth_alias') {
  2988. //dns
  2989. if (!empty($old['alias'])) {
  2990. $del_dns['name_type'] = 'CNAME';
  2991. $del_dns['name'] = $old['alias'];
  2992. $del_dns['value'] = '';
  2993. $del_dns['type'] = 'del';
  2994. if (!empty($old['auth_id'])) {
  2995. $del_dns['auth_id'] = $old['auth_id'];
  2996. $del_dns['value'] = get_dns_name($db, $old['auth_id']);
  2997. }
  2998. insert_record($db, 'dns_queue', $del_dns);
  2999. }
  3000. }
  3001. if ($delete_it) {
  3002. $new_sql = "DELETE FROM $table WHERE $filter";
  3003. LOG_DEBUG($db, "Run sql: $new_sql");
  3004. $sql_result = mysqli_query($db, $new_sql) or LOG_ERROR($db, "SQL: $new_sql :" . mysqli_error($db));
  3005. if (!$sql_result) {
  3006. LOG_ERROR($db, "DELETE Request: $new_sql : " . mysqli_error($db));
  3007. return;
  3008. }
  3009. }
  3010. if ($table !== "sessions") {
  3011. LOG_VERBOSE($db, "Delete FROM table $table WHERE $filter $changed_log");
  3012. }
  3013. return $changed_log;
  3014. }
  3015. function insert_record($db, $table, $newvalue)
  3016. {
  3017. if (!allow_update($table, 'add')) {
  3018. LOG_WARNING($db, "User does not have write permission");
  3019. return;
  3020. }
  3021. if (!isset($table)) {
  3022. LOG_WARNING($db, "Create record for unknown table! Skip command.");
  3023. return;
  3024. }
  3025. if (empty($newvalue)) {
  3026. LOG_WARNING($db, "Create record ($table) with empty data! Skip command.");
  3027. return;
  3028. }
  3029. $changed_log = '';
  3030. $field_list = '';
  3031. $value_list = '';
  3032. foreach ($newvalue as $key => $value) {
  3033. if (empty($value) and $value != '0') {
  3034. $value = '';
  3035. }
  3036. if (!preg_match('/password/i', $key)) {
  3037. $changed_log = $changed_log . " $key => $value,";
  3038. }
  3039. $field_list = $field_list . "`" . $key . "`,";
  3040. $value = trim($value);
  3041. $value_list = $value_list . "'" . mysqli_real_escape_string($db, $value) . "',";
  3042. }
  3043. if (empty($value_list)) {
  3044. return;
  3045. }
  3046. $changed_log = substr_replace($changed_log, "", -1);
  3047. $field_list = substr_replace($field_list, "", -1);
  3048. $value_list = substr_replace($value_list, "", -1);
  3049. $new_sql = "insert into $table(" . $field_list . ") values(" . $value_list . ")";
  3050. LOG_DEBUG($db, "Run sql: $new_sql");
  3051. $sql_result = mysqli_query($db, $new_sql) or LOG_ERROR($db, "SQL: $new_sql :" . mysqli_error($db));
  3052. if (!$sql_result) {
  3053. LOG_ERROR($db, "INSERT Request:" . mysqli_error($db));
  3054. return;
  3055. }
  3056. $last_id = mysqli_insert_id($db);
  3057. if ($table !== "sessions") {
  3058. LOG_VERBOSE($db, "Create record in table $table: $changed_log with id: $last_id");
  3059. }
  3060. if ($table === 'User_auth') {
  3061. run_sql($db, "UPDATE User_auth SET changed=1, dhcp_changed=1 WHERE id=" . $last_id);
  3062. }
  3063. if ($table === 'User_auth_alias') {
  3064. //dns
  3065. if (!empty($newvalue['alias'])) {
  3066. $add_dns['name_type'] = 'CNAME';
  3067. $add_dns['name'] = $newvalue['alias'];
  3068. $add_dns['value'] = get_dns_name($db, $newvalue['auth_id']);
  3069. $add_dns['type'] = 'add';
  3070. $add_dns['auth_id'] = $newvalue['auth_id'];
  3071. insert_record($db, 'dns_queue', $add_dns);
  3072. }
  3073. }
  3074. if ($table === 'User_auth') {
  3075. //dns
  3076. if (!empty($newvalue['dns_name']) and !empty($newvalue['ip'])) {
  3077. $add_dns['name_type'] = 'A';
  3078. $add_dns['name'] = $newvalue['dns_name'];
  3079. $add_dns['value'] = $newvalue['ip'];
  3080. $add_dns['type'] = 'add';
  3081. $add_dns['auth_id'] = $last_id;
  3082. insert_record($db, 'dns_queue', $add_dns);
  3083. }
  3084. }
  3085. return $last_id;
  3086. }
  3087. function dump_record($db, $table, $filter)
  3088. {
  3089. $result = '';
  3090. $old = get_record($db, $table, $filter);
  3091. if (empty($old)) {
  3092. return $result;
  3093. }
  3094. $result = 'record: ' . get_rec_str($old);
  3095. return $result;
  3096. }
  3097. function get_rec_str($array)
  3098. {
  3099. $result = '';
  3100. foreach ($array as $key => $value) {
  3101. $result .= "[" . $key . "]=" . $value . ", ";
  3102. }
  3103. $result = preg_replace('/,\s+$/', '', $result);
  3104. return $result;
  3105. }
  3106. function get_diff_rec($db, $table, $filter, $newvalue, $only_changed)
  3107. {
  3108. if (!isset($table)) {
  3109. return;
  3110. }
  3111. if (!isset($filter)) {
  3112. return;
  3113. }
  3114. if (!isset($newvalue)) {
  3115. return;
  3116. }
  3117. if (!isset($only_changed)) {
  3118. $only_changed = 0;
  3119. }
  3120. $old_sql = "SELECT * FROM $table WHERE $filter";
  3121. $old_record = mysqli_query($db, $old_sql) or LOG_ERROR($db, "SQL: $old_sql :" . mysqli_error($db));
  3122. $old = mysqli_fetch_array($old_record, MYSQLI_ASSOC);
  3123. $changed_log = "\r\n";
  3124. foreach ($newvalue as $key => $value) {
  3125. if (strcmp($old[$key], $value) !== 0) {
  3126. $changed_log = $changed_log . " $key => cur: $value old: $old[$key],\r\n";
  3127. }
  3128. }
  3129. $old_record = '';
  3130. if (!$only_changed) {
  3131. $old_record = "\r\n Has not changed:\r\n";
  3132. foreach ($old as $key => $value) {
  3133. if (!empty($newvalue[$key])) {
  3134. $old_record = $old_record . " $key = $value,\r\n";
  3135. }
  3136. }
  3137. $old_record = substr_replace($old_record, "", -3);
  3138. }
  3139. // print $changed_log;
  3140. return $changed_log . $old_record;
  3141. }
  3142. function get_cacti_graph($host_ip, $port_index)
  3143. {
  3144. if (empty(get_const('cacti_url'))) {
  3145. return;
  3146. }
  3147. if (CACTI_DB_HOST == null or CACTI_DB_USER == null or CACTI_DB_PASS == null or CACTI_DB_NAME == null) {
  3148. return;
  3149. }
  3150. if (empty(CACTI_DB_HOST) or empty(CACTI_DB_USER) or empty(CACTI_DB_PASS) or empty(CACTI_DB_NAME)) {
  3151. return;
  3152. }
  3153. $cacti_db_link = new_connection(CACTI_DB_HOST, CACTI_DB_USER, CACTI_DB_PASS, CACTI_DB_NAME);
  3154. if (!$cacti_db_link) {
  3155. return FALSE;
  3156. }
  3157. $host_sql = 'SELECT * FROM host WHERE hostname="' . $host_ip . '"';
  3158. $cacti_host = get_record_sql($cacti_db_link, $host_sql);
  3159. $host_id = $cacti_host["id"];
  3160. if (empty($host_id)) {
  3161. return;
  3162. }
  3163. $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';
  3164. $cacti_graph = get_record_sql($cacti_db_link, $graph_sql);
  3165. $graph_id = $cacti_graph["id"];
  3166. if (empty($graph_id)) {
  3167. return;
  3168. }
  3169. $result = get_const('cacti_url') . "/graph_image.php?local_graph_id=" . $graph_id;
  3170. return $result;
  3171. }
  3172. function print_select_item($description, $value, $current)
  3173. {
  3174. if ((string)$value === (string)$current) {
  3175. print "<option value='" . $value . "' selected>$description</option>";
  3176. } else {
  3177. print "<option value='" . $value . "'>$description</option>";
  3178. }
  3179. }
  3180. function print_select_simple($description, $value)
  3181. {
  3182. print "<option value=$value>$description</option>";
  3183. }
  3184. function print_select_item_ext($description, $value, $current, $disabled)
  3185. {
  3186. if ((string)$value === (string)$current) {
  3187. print "<option value=$value selected>$description</option>";
  3188. } else {
  3189. if (!$disabled) {
  3190. print "<option value=$value>$description</option>";
  3191. } else {
  3192. print "<option disabled value=$value>$description</option>";
  3193. }
  3194. }
  3195. }
  3196. function print_row_at_pages($name, $value)
  3197. {
  3198. print "<select name='" . $name . "'>\n";
  3199. print_select_item(WEB_select_item_more, pow(10, 10), $value);
  3200. print_select_item('25', 25, $value);
  3201. print_select_item('50', 50, $value);
  3202. print_select_item('100', 100, $value);
  3203. print_select_item('200', 200, $value);
  3204. print_select_item('500', 500, $value);
  3205. print_select_item('1000', 1000, $value);
  3206. print_select_item('2000', 2000, $value);
  3207. print "</select>\n";
  3208. }
  3209. function print_navigation($url, $page, $displayed, $count_records, $total)
  3210. {
  3211. if ($total <= 1) {
  3212. print "<div align=left class=records >";
  3213. print "| Total records: $count_records";
  3214. print "</div>";
  3215. return;
  3216. }
  3217. $v_char = "?";
  3218. if (preg_match('/\.php\?/', $url)) {
  3219. $v_char = "&";
  3220. }
  3221. #две назад
  3222. print "<div align=left class=records >";
  3223. if (($page - 2) > 0) :
  3224. $pagetwoleft = "<a class='first_page_link' href=" . $url . $v_char . "page=" . ($page - 2) . ">" . ($page - 2) . "</a> ";
  3225. else :
  3226. $pagetwoleft = null;
  3227. endif;
  3228. #одна назад
  3229. if (($page - 1) > 0) :
  3230. $pageoneleft = "<a class='first_page_link' href=" . $url . $v_char . "page=" . ($page - 1) . ">" . ($page - 1) . "</a> ";
  3231. $pagetemp = ($page - 1);
  3232. else :
  3233. $pageoneleft = null;
  3234. $pagetemp = null;
  3235. endif;
  3236. #две вперед
  3237. if (($page + 2) <= $total) :
  3238. $pagetworight = " <a class='first_page_link' href=" . $url . $v_char . "page=" . ($page + 2) . ">" . ($page + 2) . "</a>";
  3239. else :
  3240. $pagetworight = null;
  3241. endif;
  3242. #одна вперед
  3243. if (($page + 1) <= $total) :
  3244. $pageoneright = " <a class='first_page_link' href=" . $url . $v_char . "page=" . ($page + 1) . ">" . ($page + 1) . "</a>";
  3245. $pagetemp2 = ($page + 1);
  3246. else :
  3247. $pageoneright = null;
  3248. $pagetemp2 = null;
  3249. endif;
  3250. # в начало
  3251. if ($page != 1 && $pagetemp != 1 && $pagetemp != 2) :
  3252. $pagerevp = "<a href=" . $url . $v_char . "page=1 class='first_page_link' title='В начало'><<</a> ";
  3253. else :
  3254. $pagerevp = null;
  3255. endif;
  3256. #в конец (последняя)
  3257. if ($page != $total && $pagetemp2 != ($total - 1) && $pagetemp2 != $total) :
  3258. $nextp = " ... <a href=" . $url . $v_char . "page=" . $total . " class='first_page_link'>$total</a>";
  3259. else :
  3260. $nextp = null;
  3261. endif;
  3262. print $pagerevp . $pagetwoleft . $pageoneleft . '<span class="num_page_not_link"><b>' . $page . '</b></span>' . $pageoneright . $pagetworight . $nextp;
  3263. print " | Total records: $count_records";
  3264. print "</div>";
  3265. }
  3266. function get_option($db, $option_id)
  3267. {
  3268. $option = get_record($db, "config", "option_id=" . $option_id);
  3269. if (empty($option) or empty($option['value'])) {
  3270. $default = get_record($db, "config_options", "id=$option_id");
  3271. return $default['default_value'];
  3272. }
  3273. return $option['value'];
  3274. }
  3275. function is_option($db, $option_id)
  3276. {
  3277. $option = get_record($db, "config", "option_id=" . $option_id);
  3278. if (empty($option) or empty($option['value'])) {
  3279. return;
  3280. }
  3281. return 1;
  3282. }
  3283. function set_option($db, $option_id, $value)
  3284. {
  3285. $option['value'] = $value;
  3286. update_record($db, 'config', "option_id=$option_id", $option);
  3287. }
  3288. function is_subnet_aton($subnet, $ip)
  3289. {
  3290. if (!isset($subnet)) {
  3291. return 0;
  3292. }
  3293. if (!isset($ip)) {
  3294. return 0;
  3295. }
  3296. $range = cidrToRange($subnet);
  3297. if ($ip >= ip2long($range[0]) and $ip <= ip2long($range[1])) {
  3298. return 1;
  3299. }
  3300. return 0;
  3301. }
  3302. function get_new_user_id($db, $ip, $mac, $hostname)
  3303. {
  3304. $result['ip'] = $ip;
  3305. $result['mac'] = mac_dotted($mac);
  3306. $result['hostname'] = $hostname;
  3307. $result['user_id'] = NULL;
  3308. $result['ou_id'] = NULL;
  3309. $ip_aton = ip2long($ip);
  3310. //personal user rules
  3311. //ip
  3312. if (!empty($ip)) {
  3313. $t_rules = get_records_sql($db, "SELECT * FROM auth_rules WHERE type=1 and LENGTH(rule)>0 AND user_id IS NOT NULL");
  3314. foreach ($t_rules as $row) {
  3315. if (!empty($row['rule']) and is_subnet_aton($row['rule'], $ip_aton)) {
  3316. $result['user_id'] = $row['user_id'];
  3317. }
  3318. }
  3319. }
  3320. //mac
  3321. if (!empty($mac)) {
  3322. $mac_rules = get_records_sql($db, "SELECT * FROM auth_rules WHERE type=2 AND LENGTH(rule)>0 AND user_id IS NOT NULL");
  3323. foreach ($mac_rules as $row) {
  3324. $pattern = '/' . mac_simplify($row['rule']) . '/';
  3325. if (!empty($row['rule']) and preg_match($pattern, mac_simplify($mac))) {
  3326. $result['user_id'] = $row['user_id'];
  3327. }
  3328. }
  3329. }
  3330. //hostname
  3331. if (!empty($hostname)) {
  3332. $mac_rules = get_records_sql($db, "SELECT * FROM auth_rules WHERE type=3 AND LENGTH(rule)>0 AND user_id IS NOT NULL");
  3333. foreach ($mac_rules as $row) {
  3334. if (!empty($row['rule']) and preg_match($row['rule'], $hostname)) {
  3335. $result['user_id'] = $row['user_id'];
  3336. }
  3337. }
  3338. }
  3339. if (!empty($result['user_id'])) {
  3340. return $result;
  3341. }
  3342. //ou rules
  3343. //ip
  3344. if (!empty($ip)) {
  3345. if (is_hotspot($db, $ip)) {
  3346. $result['ou_id'] = get_const('default_hotspot_ou_id');
  3347. }
  3348. $t_rules = get_records_sql($db, "SELECT * FROM auth_rules WHERE type=1 and LENGTH(rule)>0 AND ou_id IS NOT NULL");
  3349. foreach ($t_rules as $row) {
  3350. if (!empty($row['rule']) and is_subnet_aton($row['rule'], $ip_aton)) {
  3351. $result['ou_id'] = $row['ou_id'];
  3352. }
  3353. }
  3354. }
  3355. //mac
  3356. if (!empty($mac)) {
  3357. $mac_rules = get_records_sql($db, "SELECT * FROM auth_rules WHERE type=2 AND LENGTH(rule)>0 AND ou_id IS NOT NULL");
  3358. foreach ($mac_rules as $row) {
  3359. $pattern = '/' . mac_simplify($row['rule']) . '/';
  3360. if (!empty($row['rule']) and preg_match($pattern, mac_simplify($mac))) {
  3361. $result['ou_id'] = $row['ou_id'];
  3362. }
  3363. }
  3364. }
  3365. //hostname
  3366. if (!empty($hostname)) {
  3367. $mac_rules = get_records_sql($db, "SELECT * FROM auth_rules WHERE type=3 AND LENGTH(rule)>0 AND ou_id IS NOT NULL");
  3368. foreach ($mac_rules as $row) {
  3369. if (!empty($row['rule']) and preg_match($row['rule'], $hostname)) {
  3370. $result['ou_id'] = $row['ou_id'];
  3371. }
  3372. }
  3373. }
  3374. if (empty($result['ou_id'])) {
  3375. $result['ou_id'] = get_const('default_user_ou_id');
  3376. }
  3377. return $result;
  3378. }
  3379. function get_subnet_range($db, $subnet_id)
  3380. {
  3381. if (empty($subnet_id)) {
  3382. return;
  3383. }
  3384. $t_option = get_record_sql($db, "SELECT ip_int_start,ip_int_stop FROM `subnets` WHERE id=$subnet_id");
  3385. if (!isset($t_option['ip_int_start'])) {
  3386. $t_option['ip_int_start'] = 0;
  3387. }
  3388. if (!isset($t_option['ip_int_stop'])) {
  3389. $t_option['ip_int_stop'] = 0;
  3390. }
  3391. $subnet['start'] = $t_option['ip_int_start'];
  3392. $subnet['stop'] = $t_option['ip_int_stop'];
  3393. return $subnet;
  3394. }
  3395. function int_between($value, $start, $end)
  3396. {
  3397. return in_array($value, range($start, $end));
  3398. }
  3399. function is_gray_network($ip)
  3400. {
  3401. if (empty($ip)) {
  3402. return 0;
  3403. }
  3404. $ip_aton = ip2long($ip);
  3405. $gray_nets = array('10.0.0.0/8', '192.168.0.0/16', '172.16.0.0/12', '100.64.0.0/10');
  3406. foreach ($gray_nets as &$net) {
  3407. $net_cidr = cidrToRange($net);
  3408. if (int_between($ip_aton, ip2long($net_cidr[0]), ip2long($net_cidr[1]))) {
  3409. return $net;
  3410. }
  3411. }
  3412. return 0;
  3413. }
  3414. function is_hotspot($db, $ip)
  3415. {
  3416. if (!isset($ip)) {
  3417. return 0;
  3418. }
  3419. LOG_DEBUG($db, "Check hotspot network for ip: $ip");
  3420. $ip_aton = ip2long($ip);
  3421. $t_option = mysqli_query($db, "SELECT subnet,ip_int_start,ip_int_stop FROM `subnets` WHERE hotspot=1");
  3422. while (list($f_net, $f_start, $f_stop) = mysqli_fetch_array($t_option)) {
  3423. if ($ip_aton >= $f_start and $ip_aton <= $f_stop) {
  3424. LOG_DEBUG($db, "ip: $ip [$ip_aton] found in network $f_net: [" . $f_start . ".." . $f_stop . "]");
  3425. return 1;
  3426. }
  3427. }
  3428. LOG_DEBUG($db, "ip $ip not found in hotspot network!");
  3429. return 0;
  3430. }
  3431. function is_office($db, $ip)
  3432. {
  3433. if (!isset($ip)) {
  3434. return 0;
  3435. }
  3436. LOG_DEBUG($db, "Check office network for ip: $ip");
  3437. $ip_aton = ip2long($ip);
  3438. $t_option = mysqli_query($db, "SELECT subnet,ip_int_start,ip_int_stop FROM `subnets` WHERE office=1");
  3439. while (list($f_net, $f_start, $f_stop) = mysqli_fetch_array($t_option)) {
  3440. if ($ip_aton >= $f_start and $ip_aton <= $f_stop) {
  3441. LOG_DEBUG($db, "ip: $ip [$ip_aton] found in office $f_net: [" . $f_start . ".." . $f_stop . "]");
  3442. return 1;
  3443. }
  3444. }
  3445. LOG_DEBUG($db, "ip $ip not found in office network!");
  3446. return 0;
  3447. }
  3448. function is_our_network($db, $ip)
  3449. {
  3450. if (!isset($ip)) {
  3451. return 0;
  3452. }
  3453. if (is_hotspot($db, $ip)) {
  3454. return 1;
  3455. }
  3456. if (is_office($db, $ip)) {
  3457. return 1;
  3458. }
  3459. return 0;
  3460. }
  3461. function get_const($const_name)
  3462. {
  3463. global $config;
  3464. if (isset($config[$const_name])) {
  3465. return $config[$const_name];
  3466. }
  3467. return NULL;
  3468. }
  3469. function get_eye_version($db)
  3470. {
  3471. $v_table = get_record_sql($db, "SELECT version FROM version");
  3472. if (!empty($v_table)) {
  3473. return $v_table['version'];
  3474. }
  3475. return NULL;
  3476. }
  3477. $config["org_name"] = get_option($db_link, 32);
  3478. $config["version"] = get_eye_version($db_link);
  3479. $config["KB"] = get_option($db_link, 1);
  3480. if ($config["KB"] == 0) {
  3481. $config["KB"] = 1000;
  3482. }
  3483. if ($config["KB"] == 1) {
  3484. $config["KB"] = 1024;
  3485. }
  3486. $config["debug"] = get_option($db_link, 34);
  3487. $config["log_level"] = get_option($db_link, 53);
  3488. if ($config["debug"]) {
  3489. $config["log_level"] = 255;
  3490. }
  3491. $config["send_email"] = get_option($db_link, 51);
  3492. $config["admin_email"] = get_option($db_link, 21);
  3493. $config["sender_email"] = get_option($db_link, 52);
  3494. $config["snmp_default_version"] = get_option($db_link, 9);
  3495. $config["snmp_default_community"] = get_option($db_link, 11);
  3496. $config["auto_mac_rule"] = get_option($db_link, 64);
  3497. $config["cacti_url"] = rtrim(get_option($db_link, 58), '/');
  3498. if (preg_match('/127.0.0.1/', $config["cacti_url"])) {
  3499. $config["cacti_url"] = NULL;
  3500. }
  3501. $config["nagios_url"] = rtrim(get_option($db_link, 57), '/') . '/cgi-bin/';
  3502. if (preg_match('/127.0.0.1/', $config["nagios_url"])) {
  3503. $config["nagios_url"] = NULL;
  3504. }
  3505. $config["torrus_url"] = rtrim(get_option($db_link, 59), '/') . '?nodeid=if//HOST_IP//IF_NAME////inoutbps';
  3506. if (preg_match('/127.0.0.1/', $config["torrus_url"])) {
  3507. $config["torrus_url"] = NULL;
  3508. }
  3509. $config["dns_server"] = get_option($db_link, 3);
  3510. $config["dns_server_type"] = get_option($db_link, 70);
  3511. $ou = get_record_sql($db_link, "SELECT id FROM OU WHERE default_users = 1");
  3512. if (empty($ou)) {
  3513. $config["default_user_ou_id"] = 0;
  3514. } else {
  3515. $config["default_user_ou_id"] = $ou['id'];
  3516. }
  3517. $ou = get_record_sql($db_link, "SELECT id FROM OU WHERE default_hotspot=1");
  3518. if (empty($ou)) {
  3519. $config["default_hotspot_ou_id"] = $config["default_user_ou_id"];
  3520. } else {
  3521. $config["default_hotspot_ou_id"] = $ou['id'];
  3522. }
  3523. $config["init"] = 1;
  3524. clean_dns_cache($db_link);
  3525. //clean_unreferensed_rules($db_link);