common.php 126 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919
  1. <?php
  2. if (!defined("CONFIG")) {
  3. die("Not defined");
  4. }
  5. require_once($_SERVER['DOCUMENT_ROOT'] . "/inc/consts.php");
  6. require_once($_SERVER['DOCUMENT_ROOT'] . "/inc/snmp.php");
  7. //ValidIpAddressRegex = "^(([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])\.){3}([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])$";
  8. //ValidHostnameRegex = "^(([a-zA-Z0-9]|[a-zA-Z0-9][a-zA-Z0-9\-]*[a-zA-Z0-9])\.)*([A-Za-z0-9]|[A-Za-z0-9][A-Za-z0-9\-]*[A-Za-z0-9])$";
  9. //$ValidMacAddressRegex="^([0-9A-Fa-f]{2}[:-]){5}[0-9A-Fa-f]{2}|([0-9a-fA-F]{4}[\\.-][0-9a-fA-F]{4}[\\.-][0-9a-fA-F]{4})|[0-9A-Fa-f]{12}$";
  10. $config["init"] = 0;
  11. // #### vendor id
  12. // 1, Unknown
  13. // 2, 'Eltex'
  14. // 3, 'Huawei'
  15. // 4, 'Zyxel'
  16. // 5, 'Raisecom'
  17. // 6, 'SNR'
  18. // 7, 'Dlink'
  19. // 8, 'Allied Telesis'
  20. // 9, 'Mikrotik'
  21. // 10, 'NetGear'
  22. // 11, 'Ubiquiti'
  23. // 15, 'HP'
  24. // 16, 'Cisco'
  25. // 17, 'Maipu'
  26. // 18, 'Asus'
  27. function randomPassword($length = 8)
  28. {
  29. $alphabet = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890';
  30. $pass = array(); //remember to declare $pass as an array
  31. $alphaLength = strlen($alphabet) - 1; //put the length -1 in cache
  32. for ($i = 0; $i < $length; $i++) {
  33. $n = rand(0, $alphaLength);
  34. $pass[] = $alphabet[$n];
  35. }
  36. return implode($pass); //turn the array into a string
  37. }
  38. function mb_ucfirst($str)
  39. {
  40. $str = mb_strtolower($str);
  41. $fc = mb_strtoupper(mb_substr($str, 0, 1));
  42. return $fc . mb_substr($str, 1);
  43. }
  44. function get_user_ip()
  45. {
  46. $auth_ip = getenv("HTTP_CLIENT_IP");
  47. if (empty($auth_ip)) {
  48. $auth_ip = getenv("HTTP_X_FORWARDED_FOR");
  49. if (empty($auth_ip)) {
  50. $auth_ip = getenv("REMOTE_ADDR");
  51. if (empty($auth_ip)) {
  52. $auth_ip = $_SERVER['REMOTE_ADDR'];
  53. }
  54. }
  55. }
  56. return $auth_ip;
  57. }
  58. function fbytes($traff)
  59. {
  60. $units_IEC = array(
  61. "",
  62. "Ki",
  63. "Mi",
  64. "Gi",
  65. "Ti"
  66. );
  67. $units_metric = array(
  68. "",
  69. "k",
  70. "M",
  71. "G",
  72. "T"
  73. );
  74. if (!empty($traff) and $traff > 0) {
  75. $KB = get_const('KB');
  76. if ($KB) {
  77. $KB = 1024;
  78. } else {
  79. $KB = 1000;
  80. }
  81. //IEC
  82. if ($KB == 1024) {
  83. $index = min(((int) log($traff, $KB)), count($units_IEC) - 1);
  84. $result = round($traff / pow($KB, $index), 3) . ' ' . $units_IEC[$index] . 'B';
  85. } else {
  86. $index = min(((int) log($traff, $KB)), count($units_metric) - 1);
  87. $result = round($traff / pow($KB, $index), 3) . ' ' . $units_metric[$index] . 'B';
  88. }
  89. } else {
  90. $result = '0 B';
  91. }
  92. return $result;
  93. }
  94. function fpkts($packets)
  95. {
  96. $units = array(
  97. "",
  98. "k",
  99. "M",
  100. "G",
  101. "T"
  102. );
  103. if (!empty($packets) and $packets > 0) {
  104. $index = min(((int) log($packets, 1000)), count($units) - 1);
  105. $result = round($packets / pow(1000, $index), 3) . ' ' . $units[$index] . 'pkt/s';
  106. } else {
  107. $result = '0 pkt/s';
  108. }
  109. return $result;
  110. }
  111. function checkValidIp($cidr)
  112. {
  113. // Checks for a valid IP address or optionally a cidr notation range
  114. // e.g. 1.2.3.4 or 1.2.3.0/24
  115. // if(!preg_match("^[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}(/[0-9]{1,2}){0,1}$", $cidr)) {
  116. $ip_pattern = '/^(?:25[0-5]|2[0-4]\d|1\d\d|[1-9]\d|\d)(?:[.](?:25[0-5]|2[0-4]\d|1\d\d|[1-9]\d|\d)){3}/';
  117. if (!preg_match($ip_pattern, $cidr)) {
  118. $return = FALSE;
  119. } else {
  120. $return = TRUE;
  121. }
  122. if ($return == TRUE) {
  123. $parts = explode("/", $cidr);
  124. $ip = $parts[0];
  125. if (empty($parts[1])) {
  126. $parts[1] = "32";
  127. }
  128. $netmask = $parts[1];
  129. $octets = explode(".", $ip);
  130. foreach ($octets as $octet) {
  131. if ($octet > 255) {
  132. $return = FALSE;
  133. }
  134. }
  135. if (($netmask != "") && ($netmask > 32)) {
  136. $return = FALSE;
  137. }
  138. }
  139. return $return;
  140. }
  141. function checkValidMac($mac)
  142. {
  143. $ValidMacAddressRegex = "/^([0-9A-Fa-f]{2}[:-]){5}[0-9A-Fa-f]{2}|([0-9a-fA-F]{4}[\\.-][0-9a-fA-F]{4}[\\.-][0-9a-fA-F]{4})|[0-9A-Fa-f]{12}$/";
  144. if (!preg_match($ValidMacAddressRegex, $mac)) {
  145. $return = FALSE;
  146. } else {
  147. $return = TRUE;
  148. }
  149. return $return;
  150. }
  151. function checkValidHostname($dnsname)
  152. {
  153. if (empty($dnsname)) {
  154. return TRUE;
  155. }
  156. $host_pattern = "/^(([a-zA-Z0-9]|[a-zA-Z0-9][a-zA-Z0-9\-]*[a-zA-Z0-9])\.)*([A-Za-z0-9]|[A-Za-z0-9][A-Za-z0-9\-]*[A-Za-z0-9])$/";
  157. if (!preg_match($host_pattern, $dnsname)) {
  158. $result = FALSE;
  159. } else {
  160. $result = TRUE;
  161. }
  162. return $result;
  163. }
  164. function searchHostname($db, $id, $hostname)
  165. {
  166. if (empty($hostname)) {
  167. return NULL;
  168. }
  169. $result = '';
  170. $domain_zone = get_option($db, 33);
  171. $a_search_filter = 'SELECT * FROM User_auth WHERE deleted=0 and id !="' . $id . '" and (dns_name ="' . mysqli_real_escape_string($db, $hostname) . '" or dns_name ="' . mysqli_real_escape_string($db, $hostname . '.' . $domain_zone) . '")';
  172. // LOG_DEBUG($db, "A search-filter: ".$a_search_filter);
  173. $a_records = get_records_sql($db, $a_search_filter);
  174. foreach ($a_records as $a_rec) {
  175. $result .= 'auth_id:' . $a_rec['id'] . ' ip: ' . $a_rec['ip'] . '; ';
  176. }
  177. if (!empty($result)) {
  178. $result = 'A-record: ' . $result;
  179. }
  180. $result_cname = '';
  181. $cname_search_filter = 'SELECT * FROM User_auth_alias WHERE auth_id !="' . $id . '" and (alias ="' . mysqli_real_escape_string($db, $hostname) . '" or alias ="' . mysqli_real_escape_string($db, $hostname . '.' . $domain_zone) . '")';
  182. // LOG_DEBUG($db, "CNAME search-filter: ".$cname_search_filter);
  183. $a_records = get_records_sql($db, $cname_search_filter);
  184. foreach ($a_records as $a_rec) {
  185. $result_cname .= 'auth_id:' . $a_rec['auth_id'] . ';';
  186. }
  187. if (!empty($result_cname)) {
  188. $result_cname = 'CNAME-record: ' . $result_cname;
  189. }
  190. $result = trim($result . ' ' . $result_cname);
  191. return $result;
  192. }
  193. function checkUniqHostname($db, $id, $hostname)
  194. {
  195. if (empty($hostname)) {
  196. return TRUE;
  197. }
  198. $domain_zone = get_option($db, 33);
  199. $check_A_filter = 'deleted=0 and id !="' . $id . '" and (dns_name ="' . mysqli_real_escape_string($db, $hostname) . '" or dns_name ="' . mysqli_real_escape_string($db, $hostname . '.' . $domain_zone) . '")';
  200. // LOG_DEBUG($db, "CNAME filter: ".$check_A_filter);
  201. $count = get_count_records($db, 'User_auth', $check_A_filter);
  202. if ($count > 0) {
  203. return FALSE;
  204. }
  205. $check_CNAME_filter = 'auth_id !="' . $id . '" and (alias ="' . mysqli_real_escape_string($db, $hostname) . '" or alias ="' . mysqli_real_escape_string($db, $hostname . '.' . $domain_zone) . '")';
  206. // LOG_DEBUG($db, "CNAME filter: ".$check_CNAME_filter);
  207. $count = get_count_records($db, 'User_auth_alias', $check_CNAME_filter);
  208. if ($count > 0) {
  209. return FALSE;
  210. }
  211. return TRUE;
  212. }
  213. // Транслитерация строк.
  214. function transliterate($string, $gost = false)
  215. {
  216. if ($gost) {
  217. $replace = array(
  218. "А" => "A",
  219. "а" => "a",
  220. "Б" => "B",
  221. "б" => "b",
  222. "В" => "V",
  223. "в" => "v",
  224. "Г" => "G",
  225. "г" => "g",
  226. "Д" => "D",
  227. "д" => "d",
  228. "Е" => "E",
  229. "е" => "e",
  230. "Ё" => "E",
  231. "ё" => "e",
  232. "Ж" => "Zh",
  233. "ж" => "zh",
  234. "З" => "Z",
  235. "з" => "z",
  236. "И" => "I",
  237. "и" => "i",
  238. "Й" => "I",
  239. "й" => "i",
  240. "К" => "K",
  241. "к" => "k",
  242. "Л" => "L",
  243. "л" => "l",
  244. "М" => "M",
  245. "м" => "m",
  246. "Н" => "N",
  247. "н" => "n",
  248. "О" => "O",
  249. "о" => "o",
  250. "П" => "P",
  251. "п" => "p",
  252. "Р" => "R",
  253. "р" => "r",
  254. "С" => "S",
  255. "с" => "s",
  256. "Т" => "T",
  257. "т" => "t",
  258. "У" => "U",
  259. "у" => "u",
  260. "Ф" => "F",
  261. "ф" => "f",
  262. "Х" => "Kh",
  263. "х" => "kh",
  264. "Ц" => "Tc",
  265. "ц" => "tc",
  266. "Ч" => "Ch",
  267. "ч" => "ch",
  268. "Ш" => "Sh",
  269. "ш" => "sh",
  270. "Щ" => "Shch",
  271. "щ" => "shch",
  272. "Ы" => "Y",
  273. "ы" => "y",
  274. "Э" => "E",
  275. "э" => "e",
  276. "Ю" => "Iu",
  277. "ю" => "iu",
  278. "Я" => "Ia",
  279. "я" => "ia",
  280. "ъ" => "",
  281. "ь" => ""
  282. );
  283. } else {
  284. $arStrES = array(
  285. "ае",
  286. "уе",
  287. "ое",
  288. "ые",
  289. "ие",
  290. "эе",
  291. "яе",
  292. "юе",
  293. "ёе",
  294. "ее",
  295. "ье",
  296. "ъе",
  297. "ый",
  298. "ий"
  299. );
  300. $arStrOS = array(
  301. "аё",
  302. "уё",
  303. "оё",
  304. "ыё",
  305. "иё",
  306. "эё",
  307. "яё",
  308. "юё",
  309. "ёё",
  310. "её",
  311. "ьё",
  312. "ъё",
  313. "ый",
  314. "ий"
  315. );
  316. $arStrRS = array(
  317. "а$",
  318. "у$",
  319. "о$",
  320. "ы$",
  321. "и$",
  322. "э$",
  323. "я$",
  324. "ю$",
  325. "ё$",
  326. "е$",
  327. "ь$",
  328. "ъ$",
  329. "@",
  330. "@"
  331. );
  332. $replace = array(
  333. "А" => "A",
  334. "а" => "a",
  335. "Б" => "B",
  336. "б" => "b",
  337. "В" => "V",
  338. "в" => "v",
  339. "Г" => "G",
  340. "г" => "g",
  341. "Д" => "D",
  342. "д" => "d",
  343. "Е" => "Ye",
  344. "е" => "e",
  345. "Ё" => "Ye",
  346. "ё" => "e",
  347. "Ж" => "Zh",
  348. "ж" => "zh",
  349. "З" => "Z",
  350. "з" => "z",
  351. "И" => "I",
  352. "и" => "i",
  353. "Й" => "Y",
  354. "й" => "y",
  355. "К" => "K",
  356. "к" => "k",
  357. "Л" => "L",
  358. "л" => "l",
  359. "М" => "M",
  360. "м" => "m",
  361. "Н" => "N",
  362. "н" => "n",
  363. "О" => "O",
  364. "о" => "o",
  365. "П" => "P",
  366. "п" => "p",
  367. "Р" => "R",
  368. "р" => "r",
  369. "С" => "S",
  370. "с" => "s",
  371. "Т" => "T",
  372. "т" => "t",
  373. "У" => "U",
  374. "у" => "u",
  375. "Ф" => "F",
  376. "ф" => "f",
  377. "Х" => "Kh",
  378. "х" => "kh",
  379. "Ц" => "Ts",
  380. "ц" => "ts",
  381. "Ч" => "Ch",
  382. "ч" => "ch",
  383. "Ш" => "Sh",
  384. "ш" => "sh",
  385. "Щ" => "Shch",
  386. "щ" => "shch",
  387. "Ъ" => "",
  388. "ъ" => "",
  389. "Ы" => "Y",
  390. "ы" => "y",
  391. "Ь" => "",
  392. "ь" => "",
  393. "Э" => "E",
  394. "э" => "e",
  395. "Ю" => "Yu",
  396. "ю" => "yu",
  397. "Я" => "Ya",
  398. "я" => "ya",
  399. "@" => "y",
  400. "$" => "ye"
  401. );
  402. $string = str_replace($arStrES, $arStrRS, $string);
  403. $string = str_replace($arStrOS, $arStrRS, $string);
  404. }
  405. return strtr($string, $replace);
  406. }
  407. function cidrToRange($cidr)
  408. {
  409. $range = array();
  410. $cidr = explode('/', $cidr);
  411. if (!isset($cidr[1])) {
  412. $cidr[1] = 32;
  413. }
  414. $start = (ip2long($cidr[0])) & ((-1 << (32 - (int) $cidr[1])));
  415. $stop = $start + pow(2, (32 - (int) $cidr[1])) - 1;
  416. $range[0] = long2ip($start);
  417. $range[1] = long2ip($stop);
  418. $range[2] = $cidr;
  419. //dhcp
  420. $dhcp_size = round(($stop - $start) / 2, PHP_ROUND_HALF_UP);
  421. $dhcp_start = $start + round($dhcp_size / 2, PHP_ROUND_HALF_UP);
  422. $range[3] = long2ip($dhcp_start);
  423. $range[4] = long2ip($dhcp_start + $dhcp_size);
  424. $range[5] = long2ip($start + 1);
  425. return $range;
  426. }
  427. function crypt_string($simple_string)
  428. {
  429. // Storin gthe cipher method
  430. $ciphering = "aes-128-cbc";
  431. // Using OpenSSl Encryption method
  432. $iv_length = openssl_cipher_iv_length($ciphering);
  433. $options = 0;
  434. // Using openssl_encrypt() function to encrypt the data
  435. return openssl_encrypt($simple_string, $ciphering, ENCRYPTION_KEY, $options, ENCRYPTION_IV);
  436. }
  437. function decrypt_string($crypted_string)
  438. {
  439. // Storin gthe cipher method
  440. $ciphering = "aes-128-cbc";
  441. // Using OpenSSl Encryption method
  442. $iv_length = openssl_cipher_iv_length($ciphering);
  443. $options = 0;
  444. // Using openssl_decrypt() function to decrypt the data
  445. return openssl_decrypt($crypted_string, $ciphering, ENCRYPTION_KEY, $options, ENCRYPTION_IV);
  446. }
  447. function print_ou_select($db, $ou_name, $ou_value)
  448. {
  449. print "<select id=\"$ou_name\" name=\"$ou_name\" >\n";
  450. $t_ou = mysqli_query($db, "SELECT id,ou_name FROM OU ORDER BY ou_name");
  451. while (list($f_ou_id, $f_ou_name) = mysqli_fetch_array($t_ou)) {
  452. print_select_item($f_ou_name, $f_ou_id, $ou_value);
  453. }
  454. print "</select>\n";
  455. }
  456. function 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 id=\"$gs_name\" 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 id=\"$int_name\" 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 id=\"$ou_name\" 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 id=\"$subnet_name\" 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 id=\"$acl_name\" 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 id=\"$ip_name\" 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 id=\"$subnet_name\" 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 id=\"$subnet_name\" 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 id=\"$item_name\" 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 id=\"$device_model_name\" 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 id=\"$group_name\" 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 id=\"$building_name\" 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 id=\"$devmodel_name\" 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 id=\"$devtype_name\" 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 id=\"$devtype_name\" 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 id=\"$queue_name\" 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 id=\"$qa_name\" 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 id=\"$qa_name\" 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 id=\"$qa_name\" 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 id=\"$qa_name\" 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\" id=\"$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 id=\"$qa_name\" 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 id=\"$qa_name\" 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, $text = FALSE)
  1010. {
  1011. $cl = 'down';
  1012. if ($qa_value == 1) { $cl = 'up'; }
  1013. // $cl = 'data';
  1014. if ($text) {
  1015. print "<td class=\"$cl\">";
  1016. if ($qa_value == 1) { print WEB_select_item_yes; } else { print WEB_select_item_no; }
  1017. print "</td>\n";
  1018. } else { print_td_qa($qa_value,FALSE,$cl); }
  1019. }
  1020. function print_yes_no($qa_value = 0, $yes_style = 'data', $no_style='data')
  1021. {
  1022. if ($qa_value) { $cl = $yes_style; } else { $cl = $no_style; }
  1023. print "<td class=\"$cl\">";
  1024. if ($qa_value == 1) {
  1025. print WEB_select_item_yes;
  1026. } else {
  1027. print WEB_select_item_no;
  1028. }
  1029. print "</td>\n";
  1030. }
  1031. function print_control_proto_select($qa_name, $qa_value = -1)
  1032. {
  1033. print "<select name=\"$qa_name\">\n";
  1034. print_select_item('Disabled', -1, $qa_value);
  1035. print_select_item('Ssh', 0, $qa_value);
  1036. print_select_item('Telnet', 1, $qa_value);
  1037. // print_select_item('Mikrotik rest api', 2, $qa_value);
  1038. print "</select>\n";
  1039. }
  1040. function print_snmp_select($qa_name, $qa_value = 0)
  1041. {
  1042. print "<select id=\"$qa_name\" name=\"$qa_name\">\n";
  1043. print_select_item('Disabled', 0, $qa_value);
  1044. print_select_item('v1', 1, $qa_value);
  1045. print_select_item('v2', 2, $qa_value);
  1046. print_select_item('v3', 3, $qa_value);
  1047. print "</select>\n";
  1048. }
  1049. function print_dhcp_select($qa_name, $qa_value = 0)
  1050. {
  1051. print "<select id=\"$qa_name\" name=\"$qa_name\">\n";
  1052. if (!isset($qa_value) or strlen($qa_value) == 0) {
  1053. $qa_value = 'all';
  1054. }
  1055. print_select_item(WEB_select_item_events, 'all', $qa_value);
  1056. print_select_item(WEB_select_item_lease, 'add', $qa_value);
  1057. print_select_item(WEB_select_item_lease_refresh, 'old', $qa_value);
  1058. print_select_item(WEB_select_item_lease_free, 'del', $qa_value);
  1059. print "</select>\n";
  1060. }
  1061. function print_nagios_handler_select($qa_name)
  1062. {
  1063. print "<select name=\"$qa_name\">\n";
  1064. print_select_simple(WEB_select_item_no, '');
  1065. print_select_simple('restart-port', 'restart-port');
  1066. print "</select>\n";
  1067. }
  1068. function print_dhcp_acl_select($qa_name)
  1069. {
  1070. print "<select name=\"$qa_name\">\n";
  1071. print_select_simple(WEB_select_item_no, '');
  1072. print_select_simple('hotspot-free', 'hotspot-free');
  1073. print "</select>\n";
  1074. }
  1075. function print_enabled_select($qa_name, $qa_value)
  1076. {
  1077. print "<select id=\"$qa_name\" name=\"$qa_name\">\n";
  1078. if (!isset($qa_value) or strlen($qa_value) == 0) {
  1079. $qa_value = 0;
  1080. }
  1081. print_select_item('-', 0, $qa_value);
  1082. print_select_item(WEB_select_item_disabled, 1, $qa_value);
  1083. print_select_item(WEB_select_item_enabled, 2, $qa_value);
  1084. print "</select>\n";
  1085. }
  1086. function print_rule_target_select($qa_name, $qa_value)
  1087. {
  1088. print "<select id=\"$qa_name\" name=\"$qa_name\">\n";
  1089. if (!isset($qa_value) or strlen($qa_value) == 0) {
  1090. $qa_value = 0;
  1091. }
  1092. print_select_item('-', 0, $qa_value);
  1093. print_select_item(WEB_msg_IP, 1, $qa_value);
  1094. print_select_item(WEB_cell_ou, 2, $qa_value);
  1095. print "</select>\n";
  1096. }
  1097. function print_rule_type_select($qa_name, $qa_value)
  1098. {
  1099. print "<select id=\"$qa_name\" name=\"$qa_name\">\n";
  1100. if (!isset($qa_value) or strlen($qa_value) == 0) {
  1101. $qa_value = 0;
  1102. }
  1103. print_select_item('-', 0, $qa_value);
  1104. print_select_item(WEB_rules_type_subnet, 1, $qa_value);
  1105. print_select_item(WEB_rules_type_mac, 2, $qa_value);
  1106. print_select_item(WEB_rules_type_hostname, 3, $qa_value);
  1107. print "</select>\n";
  1108. }
  1109. function print_yn_select($qa_name, $qa_value)
  1110. {
  1111. print "<select id=\"$qa_name\" name=\"$qa_name\">\n";
  1112. if (!isset($qa_value) or strlen($qa_value) == 0) {
  1113. $qa_value = 0;
  1114. }
  1115. print_select_item('-', 0, $qa_value);
  1116. print_select_item(WEB_select_item_yes, 1, $qa_value);
  1117. print_select_item(WEB_select_item_no, 2, $qa_value);
  1118. print "</select>\n";
  1119. }
  1120. function print_ip_type_select($qa_name, $qa_value)
  1121. {
  1122. print "<select id=\"$qa_name\" name=\"$qa_name\">\n";
  1123. if (!isset($qa_value) or strlen($qa_value) == 0) {
  1124. $qa_value = 0;
  1125. }
  1126. print_select_item(WEB_select_item_every, 0, $qa_value);
  1127. print_select_item(WEB_select_item_static, 1, $qa_value);
  1128. print_select_item(WEB_select_item_dhcp, 2, $qa_value);
  1129. print_select_item(WEB_select_item_suspicious, 3, $qa_value);
  1130. print "</select>\n";
  1131. }
  1132. function print_vendor_select($db, $qa_name, $qa_value)
  1133. {
  1134. print "<select id=\"$qa_name\" name=\"$qa_name\" style=\"width: 100%\">\n";
  1135. $sSQL = "SELECT id,`name` FROM `vendors` order by `name`";
  1136. $vendors = mysqli_query($db, $sSQL);
  1137. print_select_item(WEB_select_item_all, 0, $qa_value);
  1138. while (list($v_id, $v_name) = mysqli_fetch_array($vendors)) {
  1139. print_select_item($v_name, $v_id, $qa_value);
  1140. }
  1141. print "</select>\n";
  1142. }
  1143. function print_vendor_set($db, $qa_name, $qa_value)
  1144. {
  1145. print "<select id=\"$qa_name\" name=\"$qa_name\" style=\"width: 100%\">\n";
  1146. $sSQL = "SELECT id,`name` FROM `vendors` order by `name`";
  1147. $vendors = mysqli_query($db, $sSQL);
  1148. while (list($v_id, $v_name) = mysqli_fetch_array($vendors)) {
  1149. print_select_item($v_name, $v_id, $qa_value);
  1150. }
  1151. print "</select>\n";
  1152. }
  1153. function get_vendor_name($db, $v_id)
  1154. {
  1155. $vendor = get_record_sql($db, "SELECT * FROM `vendors` WHERE id=" . $v_id);
  1156. if (empty($vendor)) {
  1157. return NULL;
  1158. }
  1159. return $vendor['name'];
  1160. }
  1161. function get_qa($qa_value, $text = FALSE)
  1162. {
  1163. if ($text) {
  1164. if ($qa_value == 1) { return "Да"; }
  1165. return "Нет";
  1166. } else {
  1167. if ($qa_value == 1) { return '<span style="font-size: 16px;">✓</span>'; }
  1168. return '<span style="font-size: 16px;">✗</span>';
  1169. }
  1170. }
  1171. function print_td_qa ($qa_value, $text = FALSE, $parent_class = '')
  1172. {
  1173. $cl = "data_green";
  1174. if (!$qa_value) { $cl = "data_red"; }
  1175. print "<td class=\"$parent_class $cl\" >" . get_qa($qa_value,$text) . "</td>";
  1176. }
  1177. function print_action_select($action_name, $action_value)
  1178. {
  1179. print "<select id=\"$action_name\" name=\"$action_name\">\n";
  1180. print_select_item(WEB_select_item_allow, 1, $action_value);
  1181. print_select_item(WEB_select_item_forbidden, 0, $action_value);
  1182. print "</select>\n";
  1183. }
  1184. function get_action($action_value)
  1185. {
  1186. if ($action_value == 1) {
  1187. return "Разрешить";
  1188. }
  1189. return "Запретить";
  1190. }
  1191. function print_filter_select($db, $filter_name, $group_id)
  1192. {
  1193. print "<select id=\"$filter_name\" name=\"$filter_name\" class=\"js-select-single\">\n";
  1194. if (isset($group_id)) {
  1195. $sSQL = "SELECT id,name FROM Filter_list WHERE Filter_list.id not in (Select filter_id FROM Group_filters WHERE group_id=$group_id)";
  1196. } else {
  1197. $sSQL = "SELECT id,name FROM Filter_list Order by name";
  1198. }
  1199. $t_filters = mysqli_query($db, $sSQL);
  1200. while (list($filter_id, $filter_name) = mysqli_fetch_array($t_filters)) {
  1201. print_select_item($filter_name, $filter_id, 0);
  1202. }
  1203. print "</select>\n";
  1204. }
  1205. function get_filter($db, $filter_value)
  1206. {
  1207. list($filter) = mysqli_fetch_array(mysqli_query($db, "SELECT name FROM Filter_list WHERE id=" . $filter_value));
  1208. return $filter;
  1209. }
  1210. function get_login($db, $user_id)
  1211. {
  1212. list($login) = mysqli_fetch_array(mysqli_query($db, "SELECT login FROM User_list WHERE id=$user_id"));
  1213. return $login;
  1214. }
  1215. function get_auth_count($db, $user_id)
  1216. {
  1217. list($count) = mysqli_fetch_array(mysqli_query($db, "SELECT count(id) FROM User_auth WHERE user_id=$user_id and deleted=0"));
  1218. return $count;
  1219. }
  1220. function print_login_select($db, $login_name, $current_login)
  1221. {
  1222. print "<select id=\"$login_name\" name=\"$login_name\" class=\"js-select-single\">\n";
  1223. $t_login = mysqli_query($db, "SELECT id,login FROM User_list Order by Login");
  1224. print_select_item('None', 0, $current_login);
  1225. while (list($f_user_id, $f_login) = mysqli_fetch_array($t_login)) {
  1226. print_select_item($f_login, $f_user_id, $current_login);
  1227. }
  1228. print "</select>\n";
  1229. }
  1230. function print_auth_select($db, $login_name, $current_auth)
  1231. {
  1232. print "<select id=\"$login_name\" name=\"$login_name\" class=\"js-select-single\">\n";
  1233. $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");
  1234. print_select_item('Empty', 0, $current_auth);
  1235. while (list($f_login, $f_fio, $f_ip, $f_auth_id) = mysqli_fetch_array($t_login)) {
  1236. print_select_item($f_login . "[" . $f_fio . "] - " . $f_ip, $f_auth_id, $current_auth);
  1237. }
  1238. print "</select>\n";
  1239. }
  1240. function print_auth_select_mac($db, $login_name, $current_auth)
  1241. {
  1242. print "<select id=\"$login_name\" name=\"$login_name\" class=\"js-select-single\">\n";
  1243. $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");
  1244. print_select_item('Empty', 0, $current_auth);
  1245. while (list($f_login, $f_fio, $f_ip, $f_mac, $f_auth_id) = mysqli_fetch_array($t_login)) {
  1246. print_select_item($f_login . "[" . $f_mac . "] - " . $f_ip, $f_auth_id, $current_auth);
  1247. }
  1248. print "</select>\n";
  1249. }
  1250. function compact_port_name($port)
  1251. {
  1252. $result = $port;
  1253. $result = preg_replace('/XGigabitEthernet/', 'X', $result);
  1254. $result = preg_replace('/TenGigabitEthernet/', 'Te', $result);
  1255. $result = preg_replace('/GigabitEthernet/', 'Gi', $result);
  1256. return $result;
  1257. }
  1258. function print_device_port_select($db, $field_name, $device_id, $target_id)
  1259. {
  1260. print "<select id=\"$field_name\" name=\"$field_name\" class=\"js-select-single\">\n";
  1261. if (empty($target_id)) {
  1262. $target_id = 0;
  1263. }
  1264. if (empty($device_id)) {
  1265. $device_id = 0;
  1266. }
  1267. $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";
  1268. $t_device = mysqli_query($db, $d_sql);
  1269. print_select_item('Empty', 0, $target_id);
  1270. while (list($f_name, $f_port, $f_device_id, $f_target_id, $f_ifname) = mysqli_fetch_array($t_device)) {
  1271. if (empty($f_ifname)) {
  1272. $f_ifname = $f_port;
  1273. }
  1274. print_select_item($f_name . "[" . $f_port . "] - " . compact_port_name($f_ifname), $f_target_id, $target_id);
  1275. }
  1276. print "</select>\n";
  1277. }
  1278. function print_device_select($db, $field_name, $device_id)
  1279. {
  1280. print "<select id=\"$field_name\" name=\"$field_name\" class=\"js-select-single\" >\n";
  1281. $d_sql = "SELECT D.device_name, D.id FROM devices AS D Where D.deleted=0 order by D.device_name ASC";
  1282. $t_device = mysqli_query($db, $d_sql);
  1283. print_select_item(WEB_select_item_every, 0, $device_id);
  1284. while (list($f_name, $f_device_id) = mysqli_fetch_array($t_device)) {
  1285. print_select_item($f_name, $f_device_id, $device_id);
  1286. }
  1287. print "</select>\n";
  1288. }
  1289. function print_netdevice_select($db, $field_name, $device_id)
  1290. {
  1291. print "<select id=\"$field_name\" name=\"$field_name\" class=\"js-select-single\" >\n";
  1292. $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";
  1293. $t_device = mysqli_query($db, $d_sql);
  1294. print_select_item(WEB_select_item_every, 0, $device_id);
  1295. while (list($f_name, $f_device_id) = mysqli_fetch_array($t_device)) {
  1296. print_select_item($f_name, $f_device_id, $device_id);
  1297. }
  1298. print "</select>\n";
  1299. }
  1300. function print_vlan_select($db, $field_name, $vlan)
  1301. {
  1302. print "<select id=\"$field_name\" name=\"$field_name\" class=\"js-select-single\">\n";
  1303. $d_sql = "SELECT DISTINCT vlan FROM device_ports ORDER BY vlan DESC";
  1304. $v_device = mysqli_query($db, $d_sql);
  1305. if (!isset($vlan) or empty($vlan)) {
  1306. $vlan = 1;
  1307. };
  1308. print_select_item('1', 1, $vlan);
  1309. while (list($f_vlan) = mysqli_fetch_array($v_device)) {
  1310. if ($f_vlan === '1') {
  1311. continue;
  1312. }
  1313. print_select_item($f_vlan, $f_vlan, $vlan);
  1314. }
  1315. print "</select>\n";
  1316. }
  1317. function print_device_select_ip($db, $field_name, $device_ip)
  1318. {
  1319. print "<select id=\"$field_name\" name=\"$field_name\" class=\"js-select-single\" >\n";
  1320. $d_sql = "SELECT D.device_name, D.ip FROM devices AS D Where D.deleted=0 order by D.device_name ASC";
  1321. $t_device = mysqli_query($db, $d_sql);
  1322. print_select_item(WEB_select_item_every, '', $device_ip);
  1323. while (list($f_name, $f_device_ip) = mysqli_fetch_array($t_device)) {
  1324. print_select_item($f_name, $f_device_ip, $device_ip);
  1325. }
  1326. print "</select>\n";
  1327. }
  1328. function print_syslog_device_select($db, $field_name, $syslog_filter, $device_ip)
  1329. {
  1330. print "<select id=\"$field_name\" name=\"$field_name\" class=\"js-select-single\" >\n";
  1331. $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";
  1332. $t_device = mysqli_query($db, $d_sql);
  1333. print_select_item(WEB_select_item_every, '', $device_ip);
  1334. while (list($f_ip, $f_name) = mysqli_fetch_array($t_device)) {
  1335. if (!isset($f_name) or empty($f_name)) {
  1336. $f_name = $f_ip;
  1337. }
  1338. print_select_item($f_name, $f_ip, $device_ip);
  1339. }
  1340. print "</select>\n";
  1341. }
  1342. function print_gateway_select($db, $field_name, $device_id)
  1343. {
  1344. print "<select id=\"$field_name\" name=\"$field_name\" >\n";
  1345. $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";
  1346. $t_device = mysqli_query($db, $d_sql);
  1347. print_select_item(WEB_select_item_every, 0, $device_id);
  1348. while (list($f_name, $f_device_id) = mysqli_fetch_array($t_device)) {
  1349. print_select_item($f_name, $f_device_id, $device_id);
  1350. }
  1351. print "</select>\n";
  1352. }
  1353. function get_gateways($db)
  1354. {
  1355. $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";
  1356. $t_device = mysqli_query($db, $d_sql);
  1357. unset($result);
  1358. while (list($f_name, $f_device_id) = mysqli_fetch_array($t_device)) {
  1359. $result[$f_device_id] = $f_name;
  1360. }
  1361. return $result;
  1362. }
  1363. function print_device_port($db, $target_id)
  1364. {
  1365. $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";
  1366. $t_device = mysqli_query($db, $d_sql);
  1367. while (list($f_name, $f_port, $f_device_id) = mysqli_fetch_array($t_device)) {
  1368. print "<a href=\"/admin/devices/switchport.php?id=$f_device_id\">" . $f_name . "[" . $f_port . "]</a>\n";
  1369. }
  1370. }
  1371. function get_device_ips($db, $device_id)
  1372. {
  1373. $switch = get_record($db, 'devices', 'id=' . $device_id);
  1374. $index = 0;
  1375. if (!empty($switch['user_id'])) {
  1376. $auth_ips = get_records($db, 'User_auth', 'deleted=0 and user_id=' . $switch['user_id']);
  1377. foreach ($auth_ips as $key => $value) {
  1378. if (isset($value['ip'])) {
  1379. $result[$index] = $value['ip'];
  1380. $index++;
  1381. }
  1382. }
  1383. } else {
  1384. if (isset($switch['ip'])) {
  1385. $result[$index] = $switch['ip'];
  1386. $index++;
  1387. }
  1388. }
  1389. return $result;
  1390. }
  1391. function get_device_id($db, $device_name)
  1392. {
  1393. $d_sql = "SELECT id FROM devices WHERE device_name='$device_name' and deleted=0";
  1394. $dev = get_record_sql($db, $d_sql);
  1395. if (empty($dev)) {
  1396. return NULL;
  1397. }
  1398. return $dev["id"];
  1399. }
  1400. function get_device_name($db, $device_id)
  1401. {
  1402. $d_sql = "SELECT device_name FROM devices WHERE id='$device_id'";
  1403. $dev = get_record_sql($db, $d_sql);
  1404. if (empty($dev)) {
  1405. return NULL;
  1406. }
  1407. return $dev["device_name"];
  1408. }
  1409. function get_auth_by_ip($db, $ip)
  1410. {
  1411. $d_sql = "SELECT id FROM User_auth WHERE ip='$ip' and deleted=0";
  1412. $auth = get_record_sql($db, $d_sql);
  1413. if (empty($auth)) {
  1414. return NULL;
  1415. }
  1416. return $auth["id"];
  1417. }
  1418. function get_user_by_ip($db, $ip)
  1419. {
  1420. $d_sql = "SELECT user_id FROM User_auth WHERE ip='$ip' and deleted=0";
  1421. $auth = get_record_sql($db, $d_sql);
  1422. if (empty($auth)) {
  1423. return NULL;
  1424. }
  1425. return $auth["user_id"];
  1426. }
  1427. function get_device_by_auth($db, $id)
  1428. {
  1429. $d_sql = "SELECT id FROM devices WHERE user_id=$id and deleted=0";
  1430. $f_dev = get_record_sql($db, $d_sql);
  1431. if (empty($f_dev)) {
  1432. return NULL;
  1433. }
  1434. return $f_dev['id'];
  1435. }
  1436. function print_auth_port($db, $port_id, $new_window = FALSE)
  1437. {
  1438. $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";
  1439. $t_auth = mysqli_query($db, $d_sql);
  1440. while (list($f_ip, $f_int, $f_mac, $f_auth_id, $f_dns, $f_user_id) = mysqli_fetch_array($t_auth)) {
  1441. $name = $f_ip;
  1442. if (!empty($f_dns)) {
  1443. $name = $f_dns;
  1444. }
  1445. $title = get_login($db, $f_user_id) . " =>" . $f_ip . "[" . $f_mac . "]";
  1446. if (!empty($f_dns)) {
  1447. $title .= " | " . $f_dns;
  1448. }
  1449. if ($new_window) {
  1450. print "<a href=\"\" title=\"" . $title . "\" onclick=\"" . open_window_url("/admin/users/editauth.php?id=" . $f_auth_id) . " return false;\">" . $name . " [" . $f_ip . "]</a><br>";
  1451. } else {
  1452. print "<a href=/admin/users/editauth.php?id=" . $f_auth_id . " title=\"" . $title . "\" >" . $name . " [" . $f_ip . "]</a><br>";
  1453. }
  1454. }
  1455. }
  1456. function get_port_comment($db, $port_id, $port_comment = '')
  1457. {
  1458. $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";
  1459. $t_auth = mysqli_query($db, $d_sql);
  1460. $comment_found = 0;
  1461. $result = '';
  1462. while (list($f_int, $f_comment) = mysqli_fetch_array($t_auth)) {
  1463. if (!empty($f_comment)) {
  1464. $comment_found = 1;
  1465. } else {
  1466. $f_comment = '';
  1467. }
  1468. $result .= $f_comment . '<br>';
  1469. }
  1470. if (!$comment_found) {
  1471. return $port_comment;
  1472. }
  1473. if (!empty($port_comment)) {
  1474. $result .= '(' . $port_comment . ')';
  1475. }
  1476. return $result;
  1477. }
  1478. function print_auth_simple($db, $auth_id)
  1479. {
  1480. $auth = get_record($db, "User_auth", "id=$auth_id");
  1481. $name = $auth['dns_name'];
  1482. if (empty($name)) {
  1483. $name = $auth['comments'];
  1484. }
  1485. if (empty($name)) {
  1486. $name = $auth['ip'];
  1487. }
  1488. print "<a href=\"/admin/users/editauth.php?id=$auth_id\">" . $name . "</a><br>";
  1489. }
  1490. function print_auth($db, $auth_id)
  1491. {
  1492. $auth = get_record($db, "User_auth", "id=$auth_id");
  1493. $name = $auth['dns_name'];
  1494. if (empty($name)) {
  1495. $name = $auth['comments'];
  1496. } else {
  1497. $name .= " (" . $auth['comments'] . ")";
  1498. }
  1499. if (empty($name)) {
  1500. $name = $auth['ip'];
  1501. } else {
  1502. $name .= " [" . $auth['ip'] . "]";
  1503. }
  1504. print "<a href=\"/admin/users/editauth.php?id=$auth_id\">" . $name . "</a><br>";
  1505. }
  1506. function print_auth_detail($db, $auth_id)
  1507. {
  1508. $auth = get_record($db, "User_auth", "id=$auth_id");
  1509. $name = $auth['dns_name'];
  1510. if (empty($name)) {
  1511. $name = $auth['comments'];
  1512. } else {
  1513. $name .= " (" . $auth['comments'] . ")";
  1514. }
  1515. if (empty($name)) {
  1516. $name = $auth['ip'];
  1517. } else {
  1518. $name .= " [" . $auth['ip'] . "]";
  1519. }
  1520. $name .= " last: [" . $auth['last_found'] . "] ";
  1521. if ($auth['deleted'] == 1) {
  1522. $name .= " <font color='red'>DELETED!!!</font>";
  1523. }
  1524. print "<a href=\"/admin/users/editauth.php?id=$auth_id\">" . $name . "</a><br>";
  1525. }
  1526. function get_auth_port_count($db, $port_id)
  1527. {
  1528. $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";
  1529. $t_device = mysqli_query($db, $d_sql);
  1530. list($f_count) = mysqli_fetch_array($t_device);
  1531. if (!isset($f_count)) {
  1532. $f_count = 0;
  1533. }
  1534. return $f_count;
  1535. }
  1536. function get_connection($db, $auth_id)
  1537. {
  1538. $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";
  1539. $t_device = mysqli_query($db, $d_sql);
  1540. list($f_name, $f_port) = mysqli_fetch_array($t_device);
  1541. if (isset($f_name)) {
  1542. $result = expand_device_name($db, $f_name) . "[" . $f_port . "]";
  1543. } else {
  1544. $result = '';
  1545. }
  1546. return $result;
  1547. }
  1548. function get_connection_string($db, $auth_id)
  1549. {
  1550. $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";
  1551. $t_device = mysqli_query($db, $d_sql);
  1552. list($f_name, $f_port) = mysqli_fetch_array($t_device);
  1553. if (isset($f_name)) {
  1554. $result = $f_name . "[" . $f_port . "]";
  1555. } else {
  1556. $result = '';
  1557. }
  1558. return $result;
  1559. }
  1560. function get_port($db, $port_id)
  1561. {
  1562. $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";
  1563. $t_device = mysqli_query($db, $d_sql);
  1564. list($f_name, $f_port) = mysqli_fetch_array($t_device);
  1565. if (isset($f_name)) {
  1566. $result = expand_device_name($db, $f_name) . "[" . $f_port . "]";
  1567. } else {
  1568. $result = '';
  1569. }
  1570. return $result;
  1571. }
  1572. function print_option_select($db, $option_name)
  1573. {
  1574. print "<select id=\"$option_name\" name=\"$option_name\">\n";
  1575. $t_option = mysqli_query($db, "SELECT id,option_name FROM config_options WHERE uniq=0 AND draft=0 order by option_name");
  1576. while (list($f_id, $f_name) = mysqli_fetch_array($t_option)) {
  1577. print "<option value=$f_id>$f_name</option>";
  1578. }
  1579. $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");
  1580. while (list($f_id, $f_name) = mysqli_fetch_array($t_option)) {
  1581. print "<option value=$f_id>$f_name</option>";
  1582. }
  1583. print "</select>\n";
  1584. }
  1585. function run_sql($db, $query)
  1586. {
  1587. if (preg_match('/^\s*(UPDATE|DELETE)/i', $query)) {
  1588. unset($matches);
  1589. preg_match('/FROM\s+(.*)\s+/i', $query, $matches);
  1590. if (!empty($matches[1])) {
  1591. if (!allow_update($matches[1], 'del')) {
  1592. LOG_DEBUG($db, "Access denied: $query ");
  1593. return;
  1594. }
  1595. }
  1596. unset($matches);
  1597. preg_match('/INSERT\s+INTO\s+(.*)\s+/i', $query, $matches);
  1598. if (!empty($matches[1])) {
  1599. if (!allow_update($matches[1], 'add')) {
  1600. LOG_DEBUG($db, "Access denied: $query ");
  1601. return;
  1602. }
  1603. }
  1604. unset($matches);
  1605. preg_match('/UPDATE\s+(.*)\s+/i', $query, $matches);
  1606. if (!empty($matches[1])) {
  1607. if (!allow_update($matches[1], 'update')) {
  1608. LOG_DEBUG($db, "Access denied: $query ");
  1609. return;
  1610. }
  1611. }
  1612. unset($matches);
  1613. }
  1614. $sql_result = mysqli_query($db, $query);
  1615. if (!$sql_result) {
  1616. LOG_ERROR($db, "At simple SQL: $query :" . mysqli_error($db));
  1617. return;
  1618. }
  1619. return $sql_result;
  1620. }
  1621. function get_count_records($db, $table, $filter)
  1622. {
  1623. if (!empty($filter)) {
  1624. $filter = 'where ' . $filter;
  1625. }
  1626. $t_count = mysqli_query($db, "SELECT count(*) FROM $table $filter");
  1627. list($count) = mysqli_fetch_array($t_count);
  1628. if (!isset($count)) {
  1629. $count = 0;
  1630. }
  1631. return $count;
  1632. }
  1633. function get_id_record($db, $table, $filter)
  1634. {
  1635. if (isset($filter)) {
  1636. $filter = 'WHERE ' . $filter;
  1637. }
  1638. $t_record = mysqli_query($db, "SELECT id FROM $table $filter limit 1");
  1639. list($id) = mysqli_fetch_array($t_record);
  1640. return $id;
  1641. }
  1642. function set_changed($db, $id)
  1643. {
  1644. $auth['changed'] = 1;
  1645. update_record($db, "User_auth", "id=" . $id, $auth);
  1646. }
  1647. function ResolveIP($db, $ip_int)
  1648. {
  1649. $ip_name = "-";
  1650. if (empty($ip_int)) {
  1651. return $ip_name;
  1652. }
  1653. $dns_cache = get_record_sql($db, "SELECT * FROM dns_cache WHERE ip=$ip_int");
  1654. if (empty($dns_cache) or empty($dns_cache['dns'])) {
  1655. $ip_name = gethostbyaddr(long2ip($ip_int));
  1656. if (empty($ip_name) or $ip_name == long2ip($ip_int)) {
  1657. $ip_name = "-";
  1658. }
  1659. run_sql($db, "INSERT INTO dns_cache(dns,ip) VALUES('" . $ip_name . "'," . $ip_int . ")");
  1660. } else {
  1661. $ip_name = $dns_cache['dns'];
  1662. }
  1663. return $ip_name;
  1664. }
  1665. function clean_dns_cache($db)
  1666. {
  1667. $date = time();
  1668. $date = $date - 86400;
  1669. $date_clean = DateTimeImmutable::createFromFormat('U', $date);
  1670. $clean_date = $date_clean->format('Y-m-d H:i:s');
  1671. run_sql($db, "DELETE FROM dns_cache WHERE `timestamp`<='" . $clean_date . "'");
  1672. }
  1673. function clean_unreferensed_rules($db)
  1674. {
  1675. run_sql($db, "DELETE FROM `auth_rules` WHERE user_id NOT IN (SELECT id FROM User_list)");
  1676. }
  1677. function FormatDateStr($format = 'Y-m-d H:i:s', $date_str)
  1678. {
  1679. $date1 = GetDateTimeFromString($date_str);
  1680. $result = $date1->format($format);
  1681. return $result;
  1682. }
  1683. function GetDateTimeFromString($date_str)
  1684. {
  1685. if (!is_a($date_str, 'DateTime')) {
  1686. $t_date_str = urldecode($date_str);
  1687. $t_date_str = preg_replace('/(\'|\")/', '', $t_date_str);
  1688. $t_date_str = preg_replace('/T/', ' ', $t_date_str);
  1689. $date1 = DateTime::createFromFormat('Y-m-d H:i:s', $t_date_str);
  1690. if (!$date1) {
  1691. $date1 = DateTime::createFromFormat('Y.m.d H:i:s', $t_date_str);
  1692. }
  1693. if (!$date1) {
  1694. $date1 = DateTime::createFromFormat('Y/m/d H:i:s', $t_date_str);
  1695. }
  1696. if (!$date1) {
  1697. $date1 = DateTime::createFromFormat('Y-m-d H:i', $t_date_str);
  1698. }
  1699. if (!$date1) {
  1700. $date1 = DateTime::createFromFormat('Y.m.d H:i', $t_date_str);
  1701. }
  1702. if (!$date1) {
  1703. $date1 = DateTime::createFromFormat('Y/m/d H:i', $t_date_str);
  1704. }
  1705. if (!$date1) {
  1706. $date1 = DateTime::createFromFormat('Y-m-d|', $t_date_str);
  1707. }
  1708. if (!$date1) {
  1709. $date1 = DateTime::createFromFormat('Y.m.d|', $t_date_str);
  1710. }
  1711. if (!$date1) {
  1712. $date1 = DateTime::createFromFormat('Y/m/d|', $t_date_str);
  1713. }
  1714. if (!$date1) {
  1715. $date1 = new DateTime;
  1716. $date1->setTime(0, 0, 0, 1);
  1717. }
  1718. } else {
  1719. return $date_str;
  1720. }
  1721. return $date1;
  1722. }
  1723. function GetNowTimeString()
  1724. {
  1725. $now = new DateTimeImmutable('now');
  1726. $result = $now->format('Y-m-d H:i:s');
  1727. return $result;
  1728. }
  1729. function GetNowDayString()
  1730. {
  1731. $now = new DateTimeImmutable('now');
  1732. $result = $now->format('Y-m-d');
  1733. return $result;
  1734. }
  1735. function get_ip_subnet($db, $ip)
  1736. {
  1737. if (empty($ip)) {
  1738. return;
  1739. }
  1740. $ip_aton = ip2long($ip);
  1741. $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)");
  1742. if (empty($user_subnet)) {
  1743. return;
  1744. }
  1745. return $user_subnet;
  1746. }
  1747. function find_mac_in_subnet($db, $ip, $mac)
  1748. {
  1749. if (empty($ip)) {
  1750. return;
  1751. }
  1752. if (empty($mac)) {
  1753. return;
  1754. }
  1755. $ip_subnet = get_ip_subnet($db, $ip);
  1756. if (empty($ip_subnet)) {
  1757. return;
  1758. }
  1759. $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");
  1760. $auth_count = 0;
  1761. $result['count'] = 0;
  1762. $result['users_id'] = [];
  1763. foreach ($t_auth as $row) {
  1764. if (!empty($row['id'])) {
  1765. $auth_count++;
  1766. $result['count'] = $auth_count;
  1767. $result[$auth_count] = $row['id'];
  1768. array_push($result['users_id'], $row['user_id']);
  1769. }
  1770. }
  1771. return $result;
  1772. }
  1773. function apply_auth_rule($db, $auth_record, $user_id)
  1774. {
  1775. if (empty($auth_record)) {
  1776. return;
  1777. }
  1778. if (empty($user_id)) {
  1779. return $auth_record;
  1780. }
  1781. $user_rec = get_record($db, 'User_list', "id=" . $user_id);
  1782. if (empty($user_rec)) {
  1783. return $auth_record;
  1784. }
  1785. //set filter and status by user
  1786. $auth_record['ou_id'] = $user_rec['ou_id'];
  1787. $auth_record['user_id'] = $user_rec['id'];
  1788. $auth_record['filter_group_id'] = $user_rec['filter_group_id'];
  1789. $auth_record['queue_id'] = $user_rec['queue_id'];
  1790. $auth_record['enabled'] = $user_rec['enabled'];
  1791. $auth_record['changed'] = 1;
  1792. //maybe fill comments?
  1793. if (!empty($user_rec['fio']) and empty($auth_record['comments'])) {
  1794. $auth_record['comments'] = $user_rec['fio'];
  1795. }
  1796. return $auth_record;
  1797. }
  1798. function fix_auth_rules($db)
  1799. {
  1800. //cleanup hotspot subnet rules
  1801. delete_record($db, "auth_rules", "ou_id=" . get_const('default_user_ou_id'));
  1802. delete_record($db, "auth_rules", "ou_id=" . get_const('default_hotspot_ou_id'));
  1803. $t_hotspot = get_records_sql($db, "SELECT * FROM subnets WHERE hotspot=1");
  1804. if (!empty($t_hotspot)) {
  1805. foreach ($t_hotspot as $row) {
  1806. delete_record($db, "auth_rules", "rule='" . $row['subnet'] . "'");
  1807. }
  1808. }
  1809. }
  1810. #---------------------------------------------------------------------------------------------------------------
  1811. function new_user($db, $user_info)
  1812. {
  1813. if (!empty($user_info['mac'])) {
  1814. $user['login'] = mac_dotted($user_info['mac']);
  1815. } else {
  1816. $user['login'] = $user_info['ip'];
  1817. }
  1818. if (!empty($user_info['dhcp_hostname'])) {
  1819. $user['fio'] = $user_info['ip'] . '[' . $user_info['dhcp_hostname'] . ']';
  1820. } else {
  1821. $user['fio'] = $user_info['ip'];
  1822. }
  1823. $login_count = get_count_records($db, "User_list", "(login LIKE '" . $user['login'] . "(%)') OR (login='" . $user['login'] . "')");
  1824. if (!empty($login_count) and $login_count > 0) {
  1825. $login_count++;
  1826. $user['login'] = $user['login'] . "(" . $login_count . ")";
  1827. }
  1828. $user['ou_id'] = $user_info['ou_id'];
  1829. $ou_info = get_record_sql($db, "SELECT * FROM OU WHERE id=" . $user_info['ou_id']);
  1830. if (!empty($ou_info)) {
  1831. $user['enabled'] = $ou_info['enabled'];
  1832. if (empty($user['enabled'])) {
  1833. $user['enabled'] = 0;
  1834. }
  1835. $user['queue_id'] = $ou_info['queue_id'];
  1836. if (empty($user['queue_id'])) {
  1837. $user['queue_id'] = 0;
  1838. }
  1839. $user['filter_group_id'] = $ou_info['filter_group_id'];
  1840. if (empty($user['filter_group_id'])) {
  1841. $user['filter_group_id'] = 0;
  1842. }
  1843. }
  1844. $result = insert_record($db, "User_list", $user);
  1845. $auto_mac_rule = get_option($db, 64);
  1846. if (!empty($result) and $auto_mac_rule and $user_info['mac']) {
  1847. $auth_rule['user_id'] = $result;
  1848. $auth_rule['type'] = 2;
  1849. $auth_rule['rule'] = mac_dotted($user_info['mac']);
  1850. insert_record($db, "auth_rules", $auth_rule);
  1851. }
  1852. return $result;
  1853. }
  1854. function new_auth($db, $ip, $mac, $user_id)
  1855. {
  1856. $ip_aton = ip2long($ip);
  1857. $msg = '';
  1858. if (!empty($mac)) {
  1859. $auth_record = get_record_sql($db, "SELECT * FROM User_auth WHERE ip_int=$ip_aton AND mac='" . mac_dotted($mac) . "' AND deleted=0");
  1860. if (!empty($auth_record)) {
  1861. LOG_WARNING($db, "Pair ip-mac already exists! Skip creating $ip [$mac] auth_id: " . $auth_record["id"]);
  1862. return $auth_record['id'];
  1863. }
  1864. }
  1865. // save traffic detailization
  1866. $save_traf = get_option($db, 23);
  1867. $resurrection_id = NULL;
  1868. // seek old auth with same ip and mac
  1869. $resurrection_id = get_id_record($db, 'User_auth', " deleted=1 AND ip_int=" . $ip_aton . " AND mac='" . $mac . "'");
  1870. if (!empty($resurrection_id)) {
  1871. $msg .= "Recovered auth_id: $resurrection_id with ip: $ip and mac: $mac ";
  1872. $auth['user_id'] = $user_id;
  1873. $auth['deleted'] = 0;
  1874. $auth['save_traf'] = $save_traf * 1;
  1875. update_record($db, "User_auth", "id=$resurrection_id", $auth);
  1876. } else {
  1877. // not found ->create new record
  1878. $msg .= "Create new ip record \r\nip: $ip\r\nmac: $mac\r\n";
  1879. $auth['deleted'] = 0;
  1880. $auth['user_id'] = $user_id;
  1881. $auth['ip'] = $ip;
  1882. $auth['ip_int'] = $ip_aton;
  1883. $auth['mac'] = $mac;
  1884. $auth['save_traf'] = $save_traf * 1;
  1885. $resurrection_id = insert_record($db, "User_auth", $auth);
  1886. }
  1887. //check rules, update filter and state for new record
  1888. if (!empty($resurrection_id)) {
  1889. $auth = apply_auth_rule($db, $auth, $user_id);
  1890. update_record($db, "User_auth", "id=$resurrection_id", $auth);
  1891. if (!is_hotspot($db, $ip) and !empty($msg)) {
  1892. LOG_WARNING($db, $msg);
  1893. }
  1894. if (is_hotspot($db, $ip) and !empty($msg)) {
  1895. LOG_INFO($db, $msg);
  1896. }
  1897. }
  1898. return $resurrection_id;
  1899. }
  1900. function resurrection_auth($db, $ip_record)
  1901. {
  1902. $ip = $ip_record['ip'];
  1903. $mac = $ip_record['mac'];
  1904. $action = $ip_record['type'];
  1905. $dhcp_hostname = $ip_record['hostname'];
  1906. $hotspot_found = $ip_record['hotspot'];
  1907. $ip_aton = ip2long($ip);
  1908. $auth_record = get_record_sql($db, "SELECT * FROM User_auth WHERE ip_int=$ip_aton AND mac='" . $mac . "' AND deleted=0");
  1909. if (!empty($auth_record)) {
  1910. $user_info = get_record_sql($db, "SELECT * FROM User_list WHERE id=" . $auth_record['user_id']);
  1911. LOG_DEBUG($db, "external dhcp user " . $user_info['login'] . " [" . $ip . "] auth_id: " . $auth_record['id']);
  1912. if (isset($dhcp_hostname) and !empty($dhcp_hostname)) {
  1913. $auth['dhcp_hostname'] = $dhcp_hostname;
  1914. }
  1915. $auth['dhcp_action'] = $action;
  1916. $auth['dhcp_time'] = GetNowTimeString();
  1917. if ($action === 'add') {
  1918. $auth['last_found'] = GetNowTimeString();
  1919. }
  1920. update_record($db, "User_auth", "id=" . $auth_record['id'], $auth);
  1921. return $auth_record['id'];
  1922. }
  1923. $ip_subnet = get_ip_subnet($db, $ip);
  1924. if ($ip_subnet['static']) {
  1925. LOG_WARNING($db, "Unknown pair ip+mac in static subnet! ip: $ip mac: [" . mac_dotted($mac) . "]. Skip");
  1926. return;
  1927. }
  1928. $msg = '';
  1929. // search changed mac
  1930. $auth_record = get_record_sql($db, "SELECT * FROM User_auth WHERE ip_int=$ip_aton AND deleted=0");
  1931. if (!empty($auth_record)) {
  1932. if (empty($auth_record['mac'])) {
  1933. $auth['mac'] = mac_dotted($mac);
  1934. $auth['dhcp_action'] = $action;
  1935. $auth['dhcp_time'] = GetNowTimeString();
  1936. if (!empty($dhcp_hostname)) {
  1937. $auth['dhcp_hostname'] = $dhcp_hostname;
  1938. }
  1939. if ($action === 'add') {
  1940. $auth['last_found'] = GetNowTimeString();
  1941. }
  1942. LOG_INFO($db, "for ip: $ip mac not found! Use empty record...");
  1943. update_record($db, "User_auth", "id=" . $auth_record['id'], $auth);
  1944. return $auth_record['id'];
  1945. } else {
  1946. if (!$hotspot_found) {
  1947. 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']);
  1948. }
  1949. run_sql($db, "UPDATE User_auth SET changed=1, deleted=1 WHERE id=" . $auth_record['id']);
  1950. }
  1951. }
  1952. // default id
  1953. $new_user_info = get_new_user_id($db, $ip, $mac, $dhcp_hostname);
  1954. if (!empty($new_user_info['user_id'])) {
  1955. $new_user_id = $new_user_info['user_id'];
  1956. }
  1957. if (empty($new_user_id)) {
  1958. $new_user_id = new_user($db, $new_user_info);
  1959. }
  1960. $resurrection_id = NULL;
  1961. $save_traf = get_option($db, 23);
  1962. $auth_record = get_record_sql($db, "SELECT * FROM User_auth WHERE ip_int=" . $ip_aton . " and mac='" . $mac . "'");
  1963. // seek old auth with same ip and mac
  1964. if (!empty($auth_record)) {
  1965. // found ->Resurrection old record
  1966. $resurrection_id = $auth_record['id'];
  1967. $msg .= "Recovered auth_id: $resurrection_id with ip: $ip and mac: $mac ";
  1968. $auth['dhcp_action'] = $action;
  1969. $auth['user_id'] = $new_user_id;
  1970. $auth['deleted'] = 0;
  1971. $auth['dhcp_time'] = GetNowTimeString();
  1972. $auth['save_traf'] = $save_traf * 1;
  1973. if (!empty($dhcp_hostname)) {
  1974. $auth['dhcp_hostname'] = $dhcp_hostname;
  1975. }
  1976. if ($action === 'add') {
  1977. $auth['last_found'] = GetNowTimeString();
  1978. }
  1979. update_record($db, "User_auth", "id=$resurrection_id", $auth);
  1980. } else {
  1981. // not found ->create new record
  1982. $msg .= "Создаём новый ip-адрес \r\nip: $ip\r\nmac: $mac\r\n";
  1983. $auth['deleted'] = 0;
  1984. $auth['user_id'] = $new_user_id;
  1985. $auth['ip'] = $ip;
  1986. $auth['ip_int'] = $ip_aton;
  1987. $auth['mac'] = $mac;
  1988. $auth['dhcp_action'] = $action;
  1989. $auth['dhcp_time'] = GetNowTimeString();
  1990. $auth['save_traf'] = $save_traf * 1;
  1991. if (!empty($dhcp_hostname)) {
  1992. $auth['dhcp_hostname'] = $dhcp_hostname;
  1993. }
  1994. if ($action == 'add') {
  1995. $auth['last_found'] = GetNowTimeString();
  1996. }
  1997. $resurrection_id = insert_record($db, "User_auth", $auth);
  1998. }
  1999. //check rules, update filter and state for new record
  2000. if (!empty($resurrection_id)) {
  2001. $auth = apply_auth_rule($db, $auth, $new_user_id);
  2002. update_record($db, "User_auth", "id=$resurrection_id", $auth);
  2003. $msg .= "filter: " . $auth['filter_group_id'] . "\r\n queue_id: " . $auth['queue_id'] . "\r\n enabled: " . $auth['enabled'] . "\r\nid: $resurrection_id";
  2004. if (!$hotspot_found and !empty($msg)) {
  2005. LOG_WARNING($db, $msg);
  2006. }
  2007. if ($hotspot_found and !empty($msg)) {
  2008. LOG_INFO($db, $msg);
  2009. }
  2010. }
  2011. return $resurrection_id;
  2012. }
  2013. function get_auth($db, $current_auth)
  2014. {
  2015. if (!isset($current_auth)) {
  2016. return;
  2017. }
  2018. if ($current_auth == 0) {
  2019. return;
  2020. }
  2021. $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");
  2022. list($f_login, $f_ip) = mysqli_fetch_array($t_login);
  2023. $result = $f_login . "[" . $f_ip . "]";
  2024. return $result;
  2025. }
  2026. function get_auth_by_mac($db, $mac)
  2027. {
  2028. if (!isset($mac)) {
  2029. return;
  2030. }
  2031. $mac = mac_dotted($mac);
  2032. $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");
  2033. list($f_id, $f_login, $f_auth_id, $f_ip) = mysqli_fetch_array($t_login);
  2034. if (isset($f_id)) {
  2035. $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>';
  2036. } else {
  2037. $result['auth'] = 'Unknown';
  2038. }
  2039. $result['mac'] = expand_mac($db, $mac);
  2040. return $result;
  2041. }
  2042. function get_auth_mac($db, $current_auth)
  2043. {
  2044. if (!isset($current_auth)) {
  2045. return;
  2046. }
  2047. if ($current_auth == 0) {
  2048. return;
  2049. }
  2050. $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");
  2051. list($f_login, $f_mac) = mysqli_fetch_array($t_login);
  2052. $result = $f_login . "[" . $f_mac . "]";
  2053. return $result;
  2054. }
  2055. function add_auth_rule($db, $rule, $type, $user_id)
  2056. {
  2057. $new['user_id'] = $user_id;
  2058. $new['type'] = $type;
  2059. $new['rule'] = $rule;
  2060. $rule_id = 0;
  2061. $auth_rules = get_record_sql($db, "SELECT * FROM auth_rules WHERE rule='" . $rule . "' AND type=" . $type);
  2062. if (empty($auth_rules)) {
  2063. $rule_id = insert_record($db, "auth_rules", $new);
  2064. LOG_INFO($db, "Create auto rule for user_id: " . $user_id . " rule: " . $rule . " type: " . $type);
  2065. } else {
  2066. if ($auth_rules['user_id'] === $user_id) {
  2067. $rule_id = update_record($db, "auth_rules", "id=" . $auth_rules['id'], $new);
  2068. LOG_INFO($db, "Replaced auto rule for user_id: " . $user_id . " rule: " . $rule . " type: " . $type);
  2069. } else {
  2070. 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']);
  2071. $rule_id = 0;
  2072. }
  2073. }
  2074. return $rule_id;
  2075. }
  2076. function update_auth_rule($db, $rule, $type, $rule_id = 0)
  2077. {
  2078. $new['type'] = $type;
  2079. $new['rule'] = $rule;
  2080. $auth_rules = get_record_sql($db, "SELECT * FROM auth_rules WHERE rule='" . $rule . "' AND type=" . $type . " AND id<>" . $rule_id);
  2081. if (empty($auth_rules)) {
  2082. $rule_id = update_record($db, "auth_rules", "id=" . $rule_id, $new);
  2083. } else {
  2084. LOG_WARNING($db, "Create auto rule id: " . $rule_id . " rule: " . $rule . " type: " . $type . " failed! Already exists at user_id: " . $auth_rules['user_id']);
  2085. $rule_id = 0;
  2086. }
  2087. return $rule_id;
  2088. }
  2089. //action: add,update,del
  2090. function allow_update($table, $action = 'update', $field = '')
  2091. {
  2092. //always allow modification for tables
  2093. if (preg_match('/(variables|dns_cache|worklog|sessions)/i', $table)) {
  2094. return 1;
  2095. }
  2096. if (isset($_SESSION['login'])) {
  2097. $work_user = $_SESSION['login'];
  2098. }
  2099. if (isset($_SESSION['user_id'])) {
  2100. $work_id = $_SESSION['user_id'];
  2101. }
  2102. if (isset($_SESSION['acl'])) {
  2103. $user_level = $_SESSION['acl'];
  2104. }
  2105. if (!isset($work_user) or !isset($work_id) or empty($user_level)) {
  2106. return 0;
  2107. }
  2108. //always allow Administrator
  2109. if ($user_level == 1) {
  2110. return 1;
  2111. }
  2112. //always forbid ViewOnly
  2113. if ($user_level == 3) {
  2114. return 0;
  2115. }
  2116. //allow tables for Operator
  2117. if (preg_match('/(dns_queue|User_auth_alias)/i', $table)) {
  2118. return 1;
  2119. }
  2120. if ($action == 'update') {
  2121. $operator_acl = [
  2122. 'User_auth' => [
  2123. 'comments' => '1',
  2124. 'dns_name' => '1',
  2125. 'firmware' => '1',
  2126. 'link_check' => '1',
  2127. 'nagios' => '1',
  2128. 'nagios_handler' => '1',
  2129. 'Wikiname' => '1'
  2130. ],
  2131. 'User_list' => [
  2132. 'fio' => '1',
  2133. 'login' => '1',
  2134. ],
  2135. ];
  2136. if (!isset($operator_acl[$table])) {
  2137. return 0;
  2138. }
  2139. if (isset($operator_acl[$table]) and empty($field)) {
  2140. return 1;
  2141. }
  2142. if (!isset($operator_acl[$table][$field])) {
  2143. return 0;
  2144. }
  2145. if (empty($operator_acl[$table][$field]) or $operator_acl[$table][$field] == '0') {
  2146. return 0;
  2147. }
  2148. return 1;
  2149. }
  2150. return 0;
  2151. }
  2152. function LOG_INFO($db, $msg, $auth_id = 0)
  2153. {
  2154. if (get_const('log_level') < L_INFO) {
  2155. return;
  2156. }
  2157. write_log($db, $msg, L_INFO, $auth_id);
  2158. }
  2159. function LOG_ERROR($db, $msg, $auth_id = 0)
  2160. {
  2161. if (get_const('log_level') < L_ERROR) {
  2162. return;
  2163. }
  2164. email(L_ERROR, $msg);
  2165. write_log($db, $msg, L_ERROR, $auth_id);
  2166. }
  2167. function LOG_VERBOSE($db, $msg, $auth_id = 0)
  2168. {
  2169. if (get_const('log_level') < L_VERBOSE) {
  2170. return;
  2171. }
  2172. write_log($db, $msg, L_VERBOSE, $auth_id);
  2173. }
  2174. function LOG_WARNING($db, $msg, $auth_id = 0)
  2175. {
  2176. if (get_const('log_level') < L_WARNING) {
  2177. return;
  2178. }
  2179. email(L_WARNING, $msg);
  2180. write_log($db, $msg, L_WARNING, $auth_id);
  2181. }
  2182. function LOG_DEBUG($db, $msg, $auth_id = 0)
  2183. {
  2184. if (!empty(get_const('debug')) and get_const('debug')) {
  2185. write_log($db, $msg, L_DEBUG, $auth_id);
  2186. }
  2187. }
  2188. function truncateByWords($string, $length = 100)
  2189. {
  2190. if (strlen($string) <= $length) {
  2191. return $string;
  2192. }
  2193. $wrapped = wordwrap($string, $length);
  2194. $shortened = substr($wrapped, 0, strpos($wrapped, "\n"));
  2195. return $shortened;
  2196. }
  2197. function get_first_line($msg)
  2198. {
  2199. if (empty($msg)) {
  2200. return;
  2201. }
  2202. preg_match('/(.*)(\n|\<br\>)/', $msg, $matches);
  2203. if (!empty($matches[1])) {
  2204. return $matches[1];
  2205. }
  2206. return truncateByWords($msg, 80);
  2207. }
  2208. function email($level, $msg)
  2209. {
  2210. if (!get_const('send_email')) {
  2211. return;
  2212. }
  2213. if (!($level === L_WARNING or $level === L_ERROR)) {
  2214. return;
  2215. }
  2216. // Generate a boundary string
  2217. $boundary = md5(time());
  2218. // Headers
  2219. $headers = array(
  2220. 'From' => get_const('sender_email'),
  2221. 'Reply-To' => get_const('sender_email'),
  2222. 'X-Mailer' => 'PHP',
  2223. 'MIME-Version' => '1.0',
  2224. 'Content-Type' => 'multipart/mixed; boundary=' . $boundary
  2225. );
  2226. $subject = get_first_line($msg);
  2227. if ($level === L_WARNING) {
  2228. $subject = "WARN: " . $subject . "...";
  2229. $message = 'WARNING! Manager: ' . $_SESSION['login'] . ' <br>' . $msg . '<br>';
  2230. }
  2231. if ($level === L_ERROR) {
  2232. $subject = "ERROR: " . $subject . "...";
  2233. $message = 'ERROR! Manager: ' . $_SESSION['login'] . ' <br>' . $msg . '<br>';
  2234. }
  2235. // HTML part
  2236. $html_message = "<html><body><h1>$message</h1></body></html>";
  2237. $html_encoded = chunk_split(base64_encode($html_message));
  2238. // Create the message body
  2239. $message = "";
  2240. $message .= "--" . $boundary . "\r\n";
  2241. $message .= "Content-Type: text/html; charset=UTF-8\r\n";
  2242. $message .= "Content-Transfer-Encoding: base64\r\n\r\n";
  2243. $message .= $html_encoded . "\r\n";
  2244. $message .= "--" . $boundary . "--";
  2245. // Send email
  2246. mail(get_const('admin_email'), $subject, $message, $headers);
  2247. }
  2248. function write_log($db, $msg, $level, $auth_id = 0)
  2249. {
  2250. $work_user = 'http';
  2251. if (isset($_SESSION['login'])) {
  2252. $work_user = $_SESSION['login'];
  2253. }
  2254. if (!isset($msg)) {
  2255. $msg = 'ERROR! Empty log string!';
  2256. }
  2257. if (!isset($level)) {
  2258. $level = L_INFO;
  2259. }
  2260. $msg = str_replace("'", '', $msg);
  2261. $sSQL = "insert into worklog(customer,message,level,auth_id) values('$work_user','$msg',$level,$auth_id)";
  2262. mysqli_query($db, $sSQL);
  2263. }
  2264. function print_year_select($year_name, $year)
  2265. {
  2266. print "<select id=\"$year_name\" name=\"$year_name\" >\n";
  2267. for ($i = $year - 10; $i <= $year + 10; $i++) {
  2268. print_select_item($i, $i, $year);
  2269. }
  2270. print "</select>\n";
  2271. }
  2272. function print_date_select($dd, $mm, $yy)
  2273. {
  2274. if ($dd >= 1) {
  2275. print "<b>День</b>\n";
  2276. print "<select id=\"day\" name=\"day\" >\n";
  2277. for ($i = 1; $i <= 31; $i++) {
  2278. print_select_item($i, $i, $dd);
  2279. }
  2280. print "</select>\n";
  2281. }
  2282. if ($mm >= 1) {
  2283. print "<b>Месяц</b>\n";
  2284. print "<select id=\"month\" name=\"month\" >\n";
  2285. for ($i = 1; $i <= 12; $i++) {
  2286. $tmp_date = DateTimeImmutable::createFromFormat('U', strtotime("$i/01/$yy"));
  2287. $month_name = $tmp_date->format('F');
  2288. print_select_item($month_name, $i, $mm);
  2289. }
  2290. print "</select>\n";
  2291. }
  2292. print "<b>Год</b>\n";
  2293. print_year_select('year', $yy);
  2294. }
  2295. function print_date_select2($dd, $mm, $yy)
  2296. {
  2297. if ($dd >= 1) {
  2298. print "<b>День</b>\n";
  2299. print "<select id=\"day2\" name=\"day2\" >\n";
  2300. for ($i = 1; $i <= 31; $i++) {
  2301. print_select_item($i, $i, $dd);
  2302. }
  2303. print "</select>\n";
  2304. }
  2305. if ($mm >= 1) {
  2306. print "<b>Месяц</b>\n";
  2307. print "<select id=\"month2\" name=\"month2\" >\n";
  2308. for ($i = 1; $i <= 12; $i++) {
  2309. $tmp_date = DateTimeImmutable::createFromFormat('U', strtotime("$i/01/$yy"));
  2310. $month_name = $tmp_date->format('F');
  2311. print_select_item($month_name, $i, $mm);
  2312. }
  2313. print "</select>\n";
  2314. }
  2315. print "<b>Год</b>\n";
  2316. print_year_select('year2', $yy);
  2317. }
  2318. function is_up($ip)
  2319. {
  2320. if (!isset($ip) or strlen($ip) == 0) {
  2321. return false;
  2322. }
  2323. exec(sprintf('ping -i .3 -c 1 -W 5 %s', escapeshellarg($ip)), $res, $rval);
  2324. return $rval == 0;
  2325. }
  2326. function apply_device_lock($db, $device_id, $iteration = 0)
  2327. {
  2328. $iteration++;
  2329. if ($iteration > 2) {
  2330. return false;
  2331. }
  2332. $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');
  2333. if (empty($dev) or empty($dev['u_locked_timestamp'])) {
  2334. LOG_DEBUG($db, "Snmp discovery lock not found. Set and discovery.");
  2335. return set_lock_discovery($db, $device_id);
  2336. }
  2337. //wait for discovery
  2338. $now = time();
  2339. $wait_time = ($dev['u_locked_timestamp'] + SNMP_LOCK_TIMEOUT) - $now;
  2340. LOG_DEBUG($db, "Check snmp lock for device id: " . $device_id . ". Lock timestamp: " . $dev['u_locked_timestamp'] . ", now: " . $now);
  2341. if ($wait_time < 0) {
  2342. LOG_DEBUG($db, "The lock is already expired. Set new lock.");
  2343. return set_lock_discovery($db, $device_id);
  2344. }
  2345. LOG_VERBOSE($db, "Snmp discovery lock for device id: $device_id found! Need wait " . $wait_time . " sec.");
  2346. sleep($wait_time);
  2347. LOG_VERBOSE($db, "Try set new lock and continue discovery for device id:" . $device_id);
  2348. return apply_device_lock($db, $device_id, $iteration);
  2349. }
  2350. function set_lock_discovery($db, $device_id)
  2351. {
  2352. $new['discovery_locked'] = 1;
  2353. $new['locked_timestamp'] = GetNowTimeString();
  2354. if (update_record($db, 'devices', 'id=' . $device_id, $new)) {
  2355. return true;
  2356. }
  2357. return false;
  2358. }
  2359. function unset_lock_discovery($db, $device_id)
  2360. {
  2361. $new['discovery_locked'] = 0;
  2362. $new['locked_timestamp'] = GetNowTimeString();
  2363. if (update_record($db, 'devices', 'id=' . $device_id, $new)) {
  2364. return true;
  2365. }
  2366. return false;
  2367. }
  2368. function set_port_for_group($db, $group_id, $place_id, $state)
  2369. {
  2370. $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;
  2371. $auth_list = mysqli_query($db, $authSQL);
  2372. LOG_VERBOSE($db, 'Mass port state change started!');
  2373. // get auth list for group
  2374. while (list($a_id, $a_name, $a_ip) = mysqli_fetch_array($auth_list)) {
  2375. // get device and port for auth
  2376. if ($place_id == 0) {
  2377. $place_filter = '';
  2378. } else {
  2379. $place_filter = 'D.building_id=' . $place_id . ' and ';
  2380. }
  2381. $devSQL = 'SELECT D.id, D.device_name, D.vendor_id, D.device_model, D.ip, DP.port, DP.snmp_index FROM devices AS D, device_ports AS DP, connections AS C WHERE ' . $place_filter . ' D.id = DP.device_id AND DP.id = C.port_id AND C.auth_id=' . $a_id . ' LIMIT 1';
  2382. $dev_info = mysqli_query($db, $devSQL);
  2383. list($d_id, $d_name, $d_vendor_id, $d_model, $d_ip, $d_port, $d_snmp_index) = mysqli_fetch_array($dev_info);
  2384. if (!isset($d_id)) {
  2385. continue;
  2386. }
  2387. $device=get_record($db,'devices',"id=".$d_id);
  2388. $snmp = getSnmpAccess($device);
  2389. if ($state == 1) {
  2390. $mode = 'enable';
  2391. run_sql($db, "Update User_auth set nagios_handler='restart-port' WHERE id=$a_id and nagios_handler='manual-mode'");
  2392. } else {
  2393. $mode = 'disable';
  2394. run_sql($db, "Update User_auth set nagios_handler='manual-mode' WHERE id=$a_id and nagios_handler='restart-port'");
  2395. }
  2396. LOG_INFO($db, "At device $d_name [$d_ip] $mode port $d_port for auth_id: $a_id ($a_ip [$a_name])");
  2397. set_port_state($d_vendor_id, $d_snmp_index, $d_ip, $snmp, $state);
  2398. set_port_poe_state($d_vendor_id, $d_port, $d_snmp_index, $d_ip, $snmp, $state);
  2399. }
  2400. LOG_VERBOSE($db, 'Mass port state change stopped.');
  2401. }
  2402. function get_vendor($db, $mac)
  2403. {
  2404. $mac = mac_dotted($mac);
  2405. $mac5 = substr($mac, 0, 14);
  2406. $mac4 = substr($mac, 0, 11);
  2407. $mac3 = substr($mac, 0, 8);
  2408. $vendor = get_record_sql($db, 'SELECT companyName,companyAddress FROM mac_vendors WHERE oui="' . $mac . '"');
  2409. if (empty($vendor)) {
  2410. $vendor = get_record_sql($db, 'SELECT companyName,companyAddress FROM mac_vendors WHERE oui="' . $mac5 . '"');
  2411. }
  2412. if (empty($vendor)) {
  2413. $vendor = get_record_sql($db, 'SELECT companyName,companyAddress FROM mac_vendors WHERE oui="' . $mac4 . '"');
  2414. }
  2415. if (empty($vendor)) {
  2416. $vendor = get_record_sql($db, 'SELECT companyName,companyAddress FROM mac_vendors WHERE oui="' . $mac3 . '"');
  2417. }
  2418. $result = '';
  2419. if (!empty($vendor)) {
  2420. $result = $vendor['companyName'];
  2421. if (!empty($vendor['companyAddress'])) {
  2422. $result = $vendor['companyAddress'];
  2423. }
  2424. }
  2425. return $result;
  2426. }
  2427. function strHexToBin($number)
  2428. {
  2429. $result = '';
  2430. for ($i = 0; $i < strlen($number); $i++) {
  2431. $conv = base_convert($number[$i], 16, 2);
  2432. $result .= str_pad($conv, 4, '0', STR_PAD_LEFT);
  2433. }
  2434. return $result;
  2435. }
  2436. function dec_to_hex($mac)
  2437. {
  2438. if (!isset($mac)) {
  2439. return;
  2440. }
  2441. $mac_array = explode('.', $mac);
  2442. for ($i = 0; $i < count($mac_array); $i++) {
  2443. $hex_i = dechex($mac_array[$i]);
  2444. if (strlen($hex_i) == 1) {
  2445. $hex_i = "0" . $hex_i;
  2446. }
  2447. $mac_array[$i] = $hex_i;
  2448. }
  2449. $hex_mac = implode(':', $mac_array);
  2450. return $hex_mac;
  2451. }
  2452. function mac_simplify($mac)
  2453. {
  2454. if (!isset($mac)) {
  2455. return;
  2456. }
  2457. $mac = strtolower(trim($mac));
  2458. $mac = preg_replace('/(\.|:|-)/', '', $mac);
  2459. return $mac;
  2460. }
  2461. function mac_dotted($mac)
  2462. {
  2463. if (!isset($mac)) {
  2464. return;
  2465. }
  2466. $mac = mac_simplify($mac);
  2467. $mac = preg_replace('/(\S{2})(\S{2})?(\S{2})?(\S{2})?(\S{2})?(\S{2})?/', '$1:$2:$3:$4:$5:$6', $mac);
  2468. $mac = preg_replace('/\:+$/', '', $mac, 5);
  2469. return $mac;
  2470. }
  2471. function unbind_ports($db, $device_id)
  2472. {
  2473. $target = mysqli_query($db, "SELECT U.target_port_id,U.id FROM device_ports U WHERE U.device_id=$device_id");
  2474. while (list($target_id, $id) = mysqli_fetch_array($target)) {
  2475. run_sql($db, "UPDATE device_ports SET target_port_id=0 WHERE target_port_id=" . $id);
  2476. run_sql($db, "UPDATE device_ports SET target_port_id=0 WHERE id=" . $id);
  2477. }
  2478. }
  2479. function bind_ports($db, $port_id, $target_id)
  2480. {
  2481. $old_target = mysqli_query($db, "SELECT U.target_port_id FROM device_ports U WHERE U.id=$port_id");
  2482. list($old_target_id) = mysqli_fetch_array($old_target);
  2483. // unbind current connection
  2484. $new['target_port_id'] = 0;
  2485. update_record($db, "device_ports", "id='$port_id'", $new);
  2486. if (isset($old_target_id)) {
  2487. update_record($db, "device_ports", "id='$old_target_id'", $new);
  2488. }
  2489. // new link
  2490. if (isset($target_id) and $target_id > 0) {
  2491. $new['target_port_id'] = $target_id;
  2492. update_record($db, "device_ports", "id='$port_id'", $new);
  2493. $new['target_port_id'] = $port_id;
  2494. update_record($db, "device_ports", "id='$target_id'", $new);
  2495. }
  2496. }
  2497. function expand_device_name($db, $name)
  2498. {
  2499. $device_id = get_device_id($db, $name);
  2500. $result = $name;
  2501. if (isset($device_id) and $device_id > 0) {
  2502. $result = '<a href=/admin/devices/editdevice.php?id=' . $device_id . '>' . $name . '</a>';
  2503. }
  2504. return $result;
  2505. }
  2506. function expand_mac($db, $msg)
  2507. {
  2508. if (!isset($msg)) {
  2509. return;
  2510. }
  2511. $mac = mac_dotted($msg);
  2512. $vendor_info = get_vendor($db, $mac);
  2513. if (!empty($vendor_info)) {
  2514. // $result = '<p title="' . $vendor_info . '"><a href=/admin/logs/mac.php?mac=' . $mac . '>' . $mac . '</a></p>';
  2515. $result = '<a href=/admin/logs/mac.php?mac=' . $mac . '><p title="' . $vendor_info . '">'. $mac . '</p></a>';
  2516. } else {
  2517. $result = '<a href=/admin/logs/mac.php?mac=' . $mac . '>' . $mac . '</a>';
  2518. }
  2519. return $result;
  2520. }
  2521. function expand_log_str($db, $msg)
  2522. {
  2523. if (!isset($msg)) {
  2524. return;
  2525. }
  2526. $auth_pattern = '/(auth_id:|auth|auth id:|auth id)\s+(\d+)\s+/i';
  2527. $auth_replace = '<a href=/admin/users/editauth.php?id=${2}>auth_id:${2}</a> ';
  2528. $result = preg_replace($auth_pattern, $auth_replace, $msg);
  2529. $user_pattern = '/(user_id:|user|user id:|user id)\s+(\d+)\s+/i';
  2530. $user_replace = '<a href=/admin/users/edituser.php?id=${2}>user_id:${2}</a> ';
  2531. $result = preg_replace($user_pattern, $user_replace, $result);
  2532. $mac_pattern = '/\s+\[(\w{12})\]\s+/i';
  2533. preg_match($mac_pattern, $result, $matches);
  2534. if (isset($matches[1])) {
  2535. $mac = $matches[1];
  2536. $mac = mac_dotted($mac);
  2537. // $vendor_info = get_vendor($db,$mac);
  2538. // $mac_replace = ' <p title="'.$vendor_info.'"><a href=/admin/logs/mac.php?mac='.$mac.'>'.$mac.'</a></p>';
  2539. $mac_replace = ' <a href=/admin/logs/mac.php?mac=' . $mac . '>' . $mac . '</a> ';
  2540. $result = preg_replace($mac_pattern, $mac_replace, $result);
  2541. }
  2542. $mac_pattern = '/\s+mac:\s+([\w\:]{17})$/i';
  2543. preg_match($mac_pattern, $result, $matches);
  2544. if (isset($matches[1])) {
  2545. $mac = $matches[1];
  2546. $mac = mac_dotted($mac);
  2547. // $vendor_info = get_vendor($db,$mac);
  2548. // $mac_replace = ' mac: <p title="'.$vendor_info.'"><a href=/admin/logs/mac.php?mac='.$mac.'>'.$mac.'</a></p>';
  2549. $mac_replace = ' mac: <a href=/admin/logs/mac.php?mac=' . $mac . '>' . $mac . '</a> ';
  2550. $result = preg_replace($mac_pattern, $mac_replace, $result);
  2551. }
  2552. $device_pattern = '/at device\s+([\w\.\-]+)/i';
  2553. preg_match($device_pattern, $result, $matches);
  2554. if (isset($matches[1])) {
  2555. $device_name = $matches[1];
  2556. $device_id = get_device_id($db, $device_name);
  2557. if (isset($device_id) and $device_id > 0) {
  2558. $device_replace = 'at device <a href=/admin/devices/editdevice.php?id=' . $device_id . '>${1}</a> ';
  2559. $result = preg_replace($device_pattern, $device_replace, $result);
  2560. }
  2561. }
  2562. $device_pattern = '/(device_id:|device id:|device id|device_id)\s+(\d+)\s+/i';
  2563. $device_replace = 'device_id: <a href=/admin/devices/editdevice.php?id=${2}>${2}</a> ';
  2564. $result = preg_replace($device_pattern, $device_replace, $result);
  2565. $ip_pattern = '/\s+ip\:\s+(\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})\s+/i';
  2566. preg_match($ip_pattern, $result, $matches);
  2567. if (isset($matches[1])) {
  2568. $ip = $matches[1];
  2569. $auth_id = get_auth_by_ip($db, $ip);
  2570. if (isset($auth_id) and $auth_id > 0) {
  2571. $auth_replace = ' ip: <a href=/admin/users/editauth.php?id=' . $auth_id . '>${1}</a> ';
  2572. $result = preg_replace($ip_pattern, $auth_replace, $result);
  2573. }
  2574. }
  2575. return $result;
  2576. }
  2577. function get_record_field($db, $table, $field, $filter)
  2578. {
  2579. if (!isset($table)) {
  2580. LOG_ERROR($db, "Search in unknown table! Skip command.");
  2581. return;
  2582. }
  2583. if (!isset($filter)) {
  2584. LOG_ERROR($db, "Search filter is empty! Skip command.");
  2585. return;
  2586. }
  2587. if (!isset($field)) {
  2588. LOG_ERROR($db, "Search field is empty! Skip command.");
  2589. return;
  2590. }
  2591. if (preg_match('/=$/', $filter)) {
  2592. LOG_ERROR($db, "Search record ($table) with illegal filter $filter! Skip command.");
  2593. return;
  2594. }
  2595. $old_sql = "SELECT $field FROM $table WHERE $filter LIMIT 1";
  2596. $old_record = mysqli_query($db, $old_sql) or LOG_ERROR($db, "SQL: $old_sql :" . mysqli_error($db));
  2597. $old = mysqli_fetch_array($old_record, MYSQLI_ASSOC);
  2598. foreach ($old as $key => $value) {
  2599. if (!isset($value) or $value === 'NULL') {
  2600. $value = '';
  2601. }
  2602. $result[$key] = $value;
  2603. }
  2604. return $result[$field];
  2605. }
  2606. function get_record($db, $table, $filter)
  2607. {
  2608. if (!isset($table)) {
  2609. LOG_ERROR($db, "Search in unknown table! Skip command.");
  2610. return;
  2611. }
  2612. if (!isset($filter)) {
  2613. LOG_ERROR($db, "Search filter is empty! Skip command.");
  2614. return;
  2615. }
  2616. if (preg_match('/=$/', $filter)) {
  2617. LOG_ERROR($db, "Search record ($table) with illegal filter $filter! Skip command.");
  2618. return;
  2619. }
  2620. $get_sql = "SELECT * FROM $table WHERE $filter LIMIT 1";
  2621. $get_record = mysqli_query($db, $get_sql);
  2622. if (!$get_record) {
  2623. LOG_ERROR($db, "SQL: $get_sql :" . mysqli_error($db));
  2624. return;
  2625. }
  2626. $fields = [];
  2627. while ($field = mysqli_fetch_field($get_record)) {
  2628. $f_table = $field->table;
  2629. $f_name = $field->name;
  2630. $fields[$f_table][$f_name] = $field;
  2631. }
  2632. $record = mysqli_fetch_array($get_record, MYSQLI_ASSOC);
  2633. $result = NULL;
  2634. if (!empty($record)) {
  2635. foreach ($record as $key => $value) {
  2636. if (!isset($value) or $value === 'NULL' or $value == NULL) {
  2637. if (!empty($key) and !empty($fields[$table]) and !empty($fields[$table][$key])) {
  2638. if (in_array($fields[$table][$key]->type, MYSQL_FIELD_DIGIT)) {
  2639. $value = 0;
  2640. }
  2641. if (in_array($fields[$table][$key]->type, MYSQL_FIELD_STRING)) {
  2642. $value = '';
  2643. }
  2644. }
  2645. }
  2646. if (!empty($key)) {
  2647. $result[$key] = $value;
  2648. }
  2649. }
  2650. }
  2651. return $result;
  2652. }
  2653. function get_records($db, $table, $filter)
  2654. {
  2655. if (!isset($table)) {
  2656. LOG_ERROR($db, "Search in unknown table! Skip command.");
  2657. return;
  2658. }
  2659. if (isset($filter) and preg_match('/=$/', $filter)) {
  2660. LOG_ERROR($db, "Search record ($table) with illegal filter $filter! Skip command.");
  2661. return;
  2662. }
  2663. $s_filter = '';
  2664. if (isset($filter)) {
  2665. $s_filter = 'WHERE ' . $filter;
  2666. }
  2667. $get_sql = "SELECT * FROM $table $s_filter";
  2668. $get_record = mysqli_query($db, $get_sql);
  2669. if (!$get_record) {
  2670. LOG_ERROR($db, "SQL: $get_sql :" . mysqli_error($db));
  2671. return;
  2672. }
  2673. $fields = [];
  2674. while ($field = mysqli_fetch_field($get_record)) {
  2675. $f_table = $field->table;
  2676. $f_name = $field->name;
  2677. $fields[$f_table][$f_name] = $field;
  2678. }
  2679. $result = NULL;
  2680. $index = 0;
  2681. while ($rec = mysqli_fetch_array($get_record, MYSQLI_ASSOC)) {
  2682. foreach ($rec as $key => $value) {
  2683. if (!isset($value) or $value === 'NULL' or $value == NULL) {
  2684. if (!empty($key) and !empty($fields[$table]) and !empty($fields[$table][$key])) {
  2685. if (in_array($fields[$table][$key]->type, MYSQL_FIELD_DIGIT)) {
  2686. $value = 0;
  2687. }
  2688. if (in_array($fields[$table][$key]->type, MYSQL_FIELD_STRING)) {
  2689. $value = '';
  2690. }
  2691. }
  2692. }
  2693. $result[$index][$key] = $value;
  2694. }
  2695. $index++;
  2696. }
  2697. return $result;
  2698. }
  2699. function get_records_sql($db, $sql)
  2700. {
  2701. $result = NULL;
  2702. if (empty($sql)) {
  2703. LOG_ERROR($db, "Empty query! Skip command.");
  2704. return $result;
  2705. }
  2706. $records = mysqli_query($db, $sql);
  2707. if (!$records) {
  2708. LOG_ERROR($db, "SQL: $sql :" . mysqli_error($db));
  2709. return $result;
  2710. }
  2711. $fields = [];
  2712. //we assume that fields with the same name have the same type
  2713. while ($field = mysqli_fetch_field($records)) {
  2714. $f_name = $field->name;
  2715. $fields[$f_name] = $field;
  2716. }
  2717. $index = 0;
  2718. while ($rec = mysqli_fetch_array($records, MYSQLI_ASSOC)) {
  2719. foreach ($rec as $key => $value) {
  2720. if (!isset($value) or $value === 'NULL' or $value == NULL) {
  2721. if (!empty($key) and !empty($fields[$key])) {
  2722. if (in_array($fields[$key]->type, MYSQL_FIELD_DIGIT)) {
  2723. $value = 0;
  2724. }
  2725. if (in_array($fields[$key]->type, MYSQL_FIELD_STRING)) {
  2726. $value = '';
  2727. }
  2728. }
  2729. }
  2730. if (!empty($key)) {
  2731. $result[$index][$key] = $value;
  2732. }
  2733. }
  2734. $index++;
  2735. }
  2736. return $result;
  2737. }
  2738. function get_record_sql($db, $sql)
  2739. {
  2740. $result = NULL;
  2741. if (!isset($sql)) {
  2742. LOG_ERROR($db, "Empty query! Skip command.");
  2743. return $result;
  2744. }
  2745. $record = mysqli_query($db, $sql . " LIMIT 1");
  2746. if (!isset($record)) {
  2747. LOG_ERROR($db, "SQL: $sql LIMIT 1: " . mysqli_error($db));
  2748. return $result;
  2749. }
  2750. $fields = [];
  2751. //we assume that fields with the same name have the same type
  2752. while ($field = mysqli_fetch_field($record)) {
  2753. $f_name = $field->name;
  2754. $fields[$f_name] = $field;
  2755. }
  2756. $rec = mysqli_fetch_array($record, MYSQLI_ASSOC);
  2757. if (!empty($rec)) {
  2758. foreach ($rec as $key => $value) {
  2759. if (!isset($value) or $value === 'NULL' or $value == NULL) {
  2760. if (!empty($key) and !empty($fields[$key])) {
  2761. if (in_array($fields[$key]->type, MYSQL_FIELD_DIGIT)) {
  2762. $value = 0;
  2763. }
  2764. if (in_array($fields[$key]->type, MYSQL_FIELD_STRING)) {
  2765. $value = '';
  2766. }
  2767. }
  2768. }
  2769. if (!empty($key)) {
  2770. $result[$key] = $value;
  2771. }
  2772. }
  2773. }
  2774. return $result;
  2775. }
  2776. function is_auth_bind_changed($db, $id, $ip, $mac)
  2777. {
  2778. $old_sql = "SELECT ip,mac FROM User_auth WHERE id=$id";
  2779. $old_record = get_record_sql($db, $old_sql);
  2780. if (empty($old_record["ip"]) or empty($old_record["mac"])) {
  2781. return 0;
  2782. }
  2783. if ($old_record["ip"] !== $ip or $old_record["mac"] !== $mac) {
  2784. LOG_VERBOSE($db, "Changed ip or mac for auth record!");
  2785. return 1;
  2786. }
  2787. return 0;
  2788. }
  2789. function copy_auth($db, $id, $new_auth)
  2790. {
  2791. $old_record = get_record_sql($db, "SELECT * FROM User_auth WHERE id=$id");
  2792. delete_record($db, "User_auth", "id=" . $id);
  2793. $new_auth["user_id"] = $old_record["user_id"];
  2794. $new_auth["changed"] = 1;
  2795. $changed_time = GetNowTimeString();
  2796. $new_auth["changed_time"] = $changed_time;
  2797. $new_id = insert_record($db, "User_auth", $new_auth);
  2798. LOG_VERBOSE($db, "Old record with id: $id deleted. Created new auth record for new ip+mac id: $new_id!");
  2799. return $new_id;
  2800. }
  2801. function get_dns_name($db, $id)
  2802. {
  2803. $auth_record = get_record_sql($db, "SELECT dns_name FROM User_auth WHERE id=" . $id);
  2804. if (!empty($auth_record) and !empty($auth_record['dns_name'])) {
  2805. return $auth_record['dns_name'];
  2806. }
  2807. return '';
  2808. }
  2809. function update_record($db, $table, $filter, $newvalue)
  2810. {
  2811. if (!isset($table)) {
  2812. LOG_WARNING($db, "Change record for unknown table! Skip command.");
  2813. return;
  2814. }
  2815. if (!isset($filter)) {
  2816. LOG_WARNING($db, "Change record ($table) with empty filter! Skip command.");
  2817. return;
  2818. }
  2819. if (preg_match('/=$/', $filter)) {
  2820. LOG_WARNING($db, "Change record ($table) with illegal filter $filter! Skip command.");
  2821. return;
  2822. }
  2823. if (!isset($newvalue)) {
  2824. LOG_WARNING($db, "Change record ($table [ $filter ]) with empty data! Skip command.");
  2825. return;
  2826. }
  2827. if (!allow_update($table, 'update')) {
  2828. LOG_INFO($db, "Access denied: $table [ $filter ]");
  2829. return 1;
  2830. }
  2831. $old_sql = "SELECT * FROM $table WHERE $filter";
  2832. $old_record = mysqli_query($db, $old_sql) or LOG_ERROR($db, "SQL: $old_sql :" . mysqli_error($db));
  2833. $old = mysqli_fetch_array($old_record, MYSQLI_ASSOC);
  2834. $rec_id = NULL;
  2835. if (!empty($old['id'])) {
  2836. $rec_id = $old['id'];
  2837. }
  2838. $changed_log = '';
  2839. $run_sql = '';
  2840. $network_changed = 0;
  2841. $dhcp_changed = 0;
  2842. $dns_changed = 0;
  2843. $acl_fields = [
  2844. 'ip' => '1',
  2845. 'ip_int' => '1',
  2846. 'enabled' => '1',
  2847. 'dhcp' => '1',
  2848. 'filter_group_id' => '1',
  2849. 'deleted' => '1',
  2850. 'dhcp_acl' => '1',
  2851. 'queue_id' => '1',
  2852. 'mac' => '1',
  2853. 'blocked' => '1',
  2854. ];
  2855. $dhcp_fields = [
  2856. 'ip' => '1',
  2857. 'dhcp' => '1',
  2858. 'deleted' => '1',
  2859. 'mac' => '1',
  2860. ];
  2861. $dns_fields = [
  2862. 'ip' => '1',
  2863. 'dns_name' => '1',
  2864. 'alias' => '1',
  2865. ];
  2866. foreach ($newvalue as $key => $value) {
  2867. if (!allow_update($table, 'update', $key)) {
  2868. continue;
  2869. }
  2870. if (!isset($value)) {
  2871. $value = '';
  2872. }
  2873. $value = trim($value);
  2874. if (strcmp($old[$key], $value) == 0) {
  2875. continue;
  2876. }
  2877. if ($table === "User_auth") {
  2878. if (!empty($acl_fields["$key"])) {
  2879. $network_changed = 1;
  2880. }
  2881. if (!empty($dhcp_fields["$key"])) {
  2882. $dhcp_changed = 1;
  2883. }
  2884. if (!empty($dns_fields["$key"])) {
  2885. $dns_changed = 1;
  2886. }
  2887. }
  2888. if ($table === "User_auth_alias") {
  2889. if (!empty($dns_fields["$key"])) {
  2890. $dns_changed = 1;
  2891. }
  2892. }
  2893. if (!preg_match('/password/i', $key)) {
  2894. $changed_log = $changed_log . " $key => $value (old: $old[$key]),";
  2895. }
  2896. $run_sql = $run_sql . " `" . $key . "`='" . mysqli_real_escape_string($db, $value) . "',";
  2897. }
  2898. if ($table === "User_auth" and $dns_changed) {
  2899. if (!empty($old['dns_name']) and !empty($old['ip'])) {
  2900. $del_dns['name_type'] = 'A';
  2901. $del_dns['name'] = $old['dns_name'];
  2902. $del_dns['value'] = $old['ip'];
  2903. $del_dns['type'] = 'del';
  2904. if (!empty($rec_id)) {
  2905. $del_dns['auth_id'] = $rec_id;
  2906. }
  2907. insert_record($db, 'dns_queue', $del_dns);
  2908. }
  2909. if (!empty($newvalue['dns_name']) and !empty($newvalue['ip'])) {
  2910. $new_dns['name_type'] = 'A';
  2911. $new_dns['name'] = $newvalue['dns_name'];
  2912. $new_dns['value'] = $newvalue['ip'];
  2913. $new_dns['type'] = 'add';
  2914. if (!empty($rec_id)) {
  2915. $new_dns['auth_id'] = $rec_id;
  2916. }
  2917. insert_record($db, 'dns_queue', $new_dns);
  2918. }
  2919. }
  2920. if ($table === "User_auth_alias" and $dns_changed) {
  2921. $auth_id = NULL;
  2922. if ($old['auth_id']) {
  2923. $auth_id = $old['auth_id'];
  2924. }
  2925. if (!empty($old['alias'])) {
  2926. $del_dns['name_type'] = 'CNAME';
  2927. $del_dns['name'] = $old['alias'];
  2928. $del_dns['type'] = 'del';
  2929. if (!empty($auth_id)) {
  2930. $del_dns['auth_id'] = $auth_id;
  2931. $del_dns['value'] = get_dns_name($db, $auth_id);
  2932. }
  2933. insert_record($db, 'dns_queue', $del_dns);
  2934. }
  2935. if (!empty($newvalue['alias'])) {
  2936. $new_dns['name_type'] = 'CNAME';
  2937. $new_dns['name'] = $newvalue['alias'];
  2938. $new_dns['type'] = 'add';
  2939. if (!empty($auth_id)) {
  2940. $new_dns['auth_id'] = $auth_id;
  2941. $new_dns['value'] = get_dns_name($db, $auth_id);
  2942. }
  2943. insert_record($db, 'dns_queue', $new_dns);
  2944. }
  2945. }
  2946. if (empty($run_sql)) {
  2947. return 1;
  2948. }
  2949. if ($network_changed) {
  2950. $run_sql = $run_sql . " `changed`='1',";
  2951. }
  2952. if ($dhcp_changed) {
  2953. $run_sql = $run_sql . " `dhcp_changed`='1',";
  2954. }
  2955. $changed_log = substr_replace($changed_log, "", -1);
  2956. $run_sql = substr_replace($run_sql, "", -1);
  2957. if ($table === 'User_auth') {
  2958. $changed_time = GetNowTimeString();
  2959. $run_sql = $run_sql . ", `changed_time`='" . $changed_time . "'";
  2960. }
  2961. $new_sql = "UPDATE $table SET $run_sql WHERE $filter";
  2962. LOG_DEBUG($db, "Run sql: $new_sql");
  2963. $sql_result = mysqli_query($db, $new_sql) or LOG_ERROR($db, "SQL: $new_sql :" . mysqli_error($db));
  2964. if (!$sql_result) {
  2965. LOG_ERROR($db, "UPDATE Request: $new_sql :" . mysqli_error($db));
  2966. return;
  2967. }
  2968. if ($table !== "sessions") {
  2969. LOG_VERBOSE($db, "Change table $table WHERE $filter set $changed_log");
  2970. }
  2971. return $sql_result;
  2972. }
  2973. function delete_record($db, $table, $filter)
  2974. {
  2975. if (!allow_update($table, 'del')) {
  2976. LOG_WARNING($db, "User does not have write permission");
  2977. return;
  2978. }
  2979. if (!isset($table)) {
  2980. LOG_WARNING($db, "Delete FROM unknown table! Skip command.");
  2981. return;
  2982. }
  2983. if (!isset($filter)) {
  2984. LOG_WARNING($db, "Delete FROM table $table with empty filter! Skip command.");
  2985. return;
  2986. }
  2987. if (preg_match('/=$/', $filter)) {
  2988. LOG_WARNING($db, "Change record ($table) with illegal filter $filter! Skip command.");
  2989. return;
  2990. }
  2991. $old_sql = "SELECT * FROM $table WHERE $filter";
  2992. $old_record = mysqli_query($db, $old_sql) or LOG_ERROR($db, "SQL: $old_sql :" . mysqli_error($db));
  2993. $old = mysqli_fetch_array($old_record, MYSQLI_ASSOC);
  2994. $rec_id = NULL;
  2995. if (!empty($old['id'])) {
  2996. $rec_id = $old['id'];
  2997. }
  2998. $changed_log = 'record: ';
  2999. if (!empty($old)) {
  3000. asort($old, SORT_STRING);
  3001. $old = array_reverse($old, 1);
  3002. foreach ($old as $key => $value) {
  3003. if (empty($value)) {
  3004. continue;
  3005. }
  3006. if (preg_match('/action/', $key)) {
  3007. continue;
  3008. }
  3009. if (preg_match('/status/', $key)) {
  3010. continue;
  3011. }
  3012. if (preg_match('/time/', $key)) {
  3013. continue;
  3014. }
  3015. if (preg_match('/found/', $key)) {
  3016. continue;
  3017. }
  3018. $changed_log = $changed_log . " $key => $value,";
  3019. }
  3020. }
  3021. $delete_it = 1;
  3022. //never delete user ip record or dns alias record
  3023. if ($table === 'User_auth') {
  3024. $delete_it = 0;
  3025. $changed_time = GetNowTimeString();
  3026. $new_sql = "UPDATE $table SET deleted=1, changed=1, `changed_time`='" . $changed_time . "' WHERE $filter";
  3027. LOG_DEBUG($db, "Run sql: $new_sql");
  3028. $sql_result = mysqli_query($db, $new_sql) or LOG_ERROR($db, "SQL: $new_sql :" . mysqli_error($db));
  3029. if (!$sql_result) {
  3030. LOG_ERROR($db, "UPDATE Request (from delete): " . mysqli_error($db));
  3031. return;
  3032. }
  3033. //dns
  3034. if (!empty($old['dns_name']) and !empty($old['ip'])) {
  3035. $del_dns['name_type'] = 'A';
  3036. $del_dns['name'] = $old['dns_name'];
  3037. $del_dns['value'] = $old['ip'];
  3038. $del_dns['type'] = 'del';
  3039. if (!empty($rec_id)) {
  3040. $del_dns['auth_id'] = $rec_id;
  3041. }
  3042. insert_record($db, 'dns_queue', $del_dns);
  3043. }
  3044. }
  3045. if ($table === 'User_auth_alias') {
  3046. //dns
  3047. if (!empty($old['alias'])) {
  3048. $del_dns['name_type'] = 'CNAME';
  3049. $del_dns['name'] = $old['alias'];
  3050. $del_dns['value'] = '';
  3051. $del_dns['type'] = 'del';
  3052. if (!empty($old['auth_id'])) {
  3053. $del_dns['auth_id'] = $old['auth_id'];
  3054. $del_dns['value'] = get_dns_name($db, $old['auth_id']);
  3055. }
  3056. insert_record($db, 'dns_queue', $del_dns);
  3057. }
  3058. }
  3059. if ($delete_it) {
  3060. $new_sql = "DELETE FROM $table WHERE $filter";
  3061. LOG_DEBUG($db, "Run sql: $new_sql");
  3062. $sql_result = mysqli_query($db, $new_sql) or LOG_ERROR($db, "SQL: $new_sql :" . mysqli_error($db));
  3063. if (!$sql_result) {
  3064. LOG_ERROR($db, "DELETE Request: $new_sql : " . mysqli_error($db));
  3065. return;
  3066. }
  3067. }
  3068. if ($table !== "sessions") {
  3069. LOG_VERBOSE($db, "Delete FROM table $table WHERE $filter $changed_log");
  3070. }
  3071. return $changed_log;
  3072. }
  3073. function insert_record($db, $table, $newvalue)
  3074. {
  3075. if (!allow_update($table, 'add')) {
  3076. LOG_WARNING($db, "User does not have write permission");
  3077. return;
  3078. }
  3079. if (!isset($table)) {
  3080. LOG_WARNING($db, "Create record for unknown table! Skip command.");
  3081. return;
  3082. }
  3083. if (empty($newvalue)) {
  3084. LOG_WARNING($db, "Create record ($table) with empty data! Skip command.");
  3085. return;
  3086. }
  3087. $changed_log = '';
  3088. $field_list = '';
  3089. $value_list = '';
  3090. foreach ($newvalue as $key => $value) {
  3091. if (empty($value) and $value != '0') {
  3092. $value = '';
  3093. }
  3094. if (!preg_match('/password/i', $key)) {
  3095. $changed_log = $changed_log . " $key => $value,";
  3096. }
  3097. $field_list = $field_list . "`" . $key . "`,";
  3098. $value = trim($value);
  3099. $value_list = $value_list . "'" . mysqli_real_escape_string($db, $value) . "',";
  3100. }
  3101. if (empty($value_list)) {
  3102. return;
  3103. }
  3104. $changed_log = substr_replace($changed_log, "", -1);
  3105. $field_list = substr_replace($field_list, "", -1);
  3106. $value_list = substr_replace($value_list, "", -1);
  3107. $new_sql = "insert into $table(" . $field_list . ") values(" . $value_list . ")";
  3108. LOG_DEBUG($db, "Run sql: $new_sql");
  3109. $sql_result = mysqli_query($db, $new_sql) or LOG_ERROR($db, "SQL: $new_sql :" . mysqli_error($db));
  3110. if (!$sql_result) {
  3111. LOG_ERROR($db, "INSERT Request:" . mysqli_error($db));
  3112. return;
  3113. }
  3114. $last_id = mysqli_insert_id($db);
  3115. if ($table !== "sessions") {
  3116. LOG_VERBOSE($db, "Create record in table $table: $changed_log with id: $last_id");
  3117. }
  3118. if ($table === 'User_auth') {
  3119. run_sql($db, "UPDATE User_auth SET changed=1, dhcp_changed=1 WHERE id=" . $last_id);
  3120. }
  3121. if ($table === 'User_auth_alias') {
  3122. //dns
  3123. if (!empty($newvalue['alias'])) {
  3124. $add_dns['name_type'] = 'CNAME';
  3125. $add_dns['name'] = $newvalue['alias'];
  3126. $add_dns['value'] = get_dns_name($db, $newvalue['auth_id']);
  3127. $add_dns['type'] = 'add';
  3128. $add_dns['auth_id'] = $newvalue['auth_id'];
  3129. insert_record($db, 'dns_queue', $add_dns);
  3130. }
  3131. }
  3132. if ($table === 'User_auth') {
  3133. //dns
  3134. if (!empty($newvalue['dns_name']) and !empty($newvalue['ip'])) {
  3135. $add_dns['name_type'] = 'A';
  3136. $add_dns['name'] = $newvalue['dns_name'];
  3137. $add_dns['value'] = $newvalue['ip'];
  3138. $add_dns['type'] = 'add';
  3139. $add_dns['auth_id'] = $last_id;
  3140. insert_record($db, 'dns_queue', $add_dns);
  3141. }
  3142. }
  3143. return $last_id;
  3144. }
  3145. function dump_record($db, $table, $filter)
  3146. {
  3147. $result = '';
  3148. $old = get_record($db, $table, $filter);
  3149. if (empty($old)) {
  3150. return $result;
  3151. }
  3152. $result = 'record: ' . get_rec_str($old);
  3153. return $result;
  3154. }
  3155. function get_rec_str($array)
  3156. {
  3157. $result = '';
  3158. foreach ($array as $key => $value) {
  3159. $result .= "[" . $key . "]=" . $value . ", ";
  3160. }
  3161. $result = preg_replace('/,\s+$/', '', $result);
  3162. return $result;
  3163. }
  3164. function get_diff_rec($db, $table, $filter, $newvalue, $only_changed)
  3165. {
  3166. if (!isset($table)) {
  3167. return;
  3168. }
  3169. if (!isset($filter)) {
  3170. return;
  3171. }
  3172. if (!isset($newvalue)) {
  3173. return;
  3174. }
  3175. if (!isset($only_changed)) {
  3176. $only_changed = 0;
  3177. }
  3178. $old_sql = "SELECT * FROM $table WHERE $filter";
  3179. $old_record = mysqli_query($db, $old_sql) or LOG_ERROR($db, "SQL: $old_sql :" . mysqli_error($db));
  3180. $old = mysqli_fetch_array($old_record, MYSQLI_ASSOC);
  3181. $changed_log = "\r\n";
  3182. foreach ($newvalue as $key => $value) {
  3183. if (strcmp($old[$key], $value) !== 0) {
  3184. $changed_log = $changed_log . " $key => cur: $value old: $old[$key],\r\n";
  3185. }
  3186. }
  3187. $old_record = '';
  3188. if (!$only_changed) {
  3189. $old_record = "\r\n Has not changed:\r\n";
  3190. foreach ($old as $key => $value) {
  3191. if (!empty($newvalue[$key])) {
  3192. $old_record = $old_record . " $key = $value,\r\n";
  3193. }
  3194. }
  3195. $old_record = substr_replace($old_record, "", -3);
  3196. }
  3197. // print $changed_log;
  3198. return $changed_log . $old_record;
  3199. }
  3200. function get_cacti_graph($host_ip, $port_index)
  3201. {
  3202. if (empty(get_const('cacti_url'))) {
  3203. return;
  3204. }
  3205. if (CACTI_DB_HOST == null or CACTI_DB_USER == null or CACTI_DB_PASS == null or CACTI_DB_NAME == null) {
  3206. return;
  3207. }
  3208. if (empty(CACTI_DB_HOST) or empty(CACTI_DB_USER) or empty(CACTI_DB_PASS) or empty(CACTI_DB_NAME)) {
  3209. return;
  3210. }
  3211. $cacti_db_link = new_connection(CACTI_DB_HOST, CACTI_DB_USER, CACTI_DB_PASS, CACTI_DB_NAME);
  3212. if (!$cacti_db_link) {
  3213. return FALSE;
  3214. }
  3215. $host_sql = 'SELECT * FROM host WHERE hostname="' . $host_ip . '"';
  3216. $cacti_host = get_record_sql($cacti_db_link, $host_sql);
  3217. $host_id = $cacti_host["id"];
  3218. if (empty($host_id)) {
  3219. return;
  3220. }
  3221. $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';
  3222. $cacti_graph = get_record_sql($cacti_db_link, $graph_sql);
  3223. $graph_id = $cacti_graph["id"];
  3224. if (empty($graph_id)) {
  3225. return;
  3226. }
  3227. $result = get_const('cacti_url') . "/graph_image.php?local_graph_id=" . $graph_id;
  3228. return $result;
  3229. }
  3230. function print_select_item($description, $value, $current)
  3231. {
  3232. if ((string)$value === (string)$current) {
  3233. print "<option value='" . $value . "' selected>$description</option>";
  3234. } else {
  3235. print "<option value='" . $value . "'>$description</option>";
  3236. }
  3237. }
  3238. function print_select_simple($description, $value)
  3239. {
  3240. print "<option value=$value>$description</option>";
  3241. }
  3242. function print_select_item_ext($description, $value, $current, $disabled)
  3243. {
  3244. if ((string)$value === (string)$current) {
  3245. print "<option value=$value selected>$description</option>";
  3246. } else {
  3247. if (!$disabled) {
  3248. print "<option value=$value>$description</option>";
  3249. } else {
  3250. print "<option disabled value=$value>$description</option>";
  3251. }
  3252. }
  3253. }
  3254. function print_row_at_pages($name, $value)
  3255. {
  3256. print "<select id='" . $name . "' name='" . $name . "'>\n";
  3257. print_select_item(WEB_select_item_more, pow(10, 10), $value);
  3258. print_select_item('25', 25, $value);
  3259. print_select_item('50', 50, $value);
  3260. print_select_item('100', 100, $value);
  3261. print_select_item('200', 200, $value);
  3262. print_select_item('500', 500, $value);
  3263. print_select_item('1000', 1000, $value);
  3264. print_select_item('2000', 2000, $value);
  3265. print "</select>\n";
  3266. }
  3267. function print_navigation($url, $page, $displayed, $count_records, $total)
  3268. {
  3269. if ($total <= 1) {
  3270. print "<div align=left class=records >";
  3271. print "| Total records: $count_records";
  3272. print "</div>";
  3273. return;
  3274. }
  3275. $v_char = "?";
  3276. if (preg_match('/\.php\?/', $url)) {
  3277. $v_char = "&";
  3278. }
  3279. //две назад
  3280. print "<div align=left class=records >";
  3281. if (($page - 2) > 0) :
  3282. $pagetwoleft = "<a class='first_page_link' href=" . $url . $v_char . "page=" . ($page - 2) . ">" . ($page - 2) . "</a> ";
  3283. else :
  3284. $pagetwoleft = null;
  3285. endif;
  3286. //одна назад
  3287. if (($page - 1) > 0) :
  3288. $pageoneleft = "<a class='first_page_link' href=" . $url . $v_char . "page=" . ($page - 1) . ">" . ($page - 1) . "</a> ";
  3289. $pagetemp = ($page - 1);
  3290. else :
  3291. $pageoneleft = null;
  3292. $pagetemp = null;
  3293. endif;
  3294. //две вперед
  3295. if (($page + 2) <= $total) :
  3296. $pagetworight = " <a class='first_page_link' href=" . $url . $v_char . "page=" . ($page + 2) . ">" . ($page + 2) . "</a>";
  3297. else :
  3298. $pagetworight = null;
  3299. endif;
  3300. //одна вперед
  3301. if (($page + 1) <= $total) :
  3302. $pageoneright = " <a class='first_page_link' href=" . $url . $v_char . "page=" . ($page + 1) . ">" . ($page + 1) . "</a>";
  3303. $pagetemp2 = ($page + 1);
  3304. else :
  3305. $pageoneright = null;
  3306. $pagetemp2 = null;
  3307. endif;
  3308. // в начало
  3309. if ($page != 1 && $pagetemp != 1 && $pagetemp != 2) :
  3310. $pagerevp = "<a href=" . $url . $v_char . "page=1 class='first_page_link' title='В начало'><<</a> ";
  3311. else :
  3312. $pagerevp = null;
  3313. endif;
  3314. //в конец (последняя)
  3315. if ($page != $total && $pagetemp2 != ($total - 1) && $pagetemp2 != $total) :
  3316. $nextp = " ... <a href=" . $url . $v_char . "page=" . $total . " class='first_page_link'>$total</a>";
  3317. else :
  3318. $nextp = null;
  3319. endif;
  3320. print $pagerevp . $pagetwoleft . $pageoneleft . '<span class="num_page_not_link"><b>' . $page . '</b></span>' . $pageoneright . $pagetworight . $nextp;
  3321. print " | Total records: $count_records";
  3322. print "</div>";
  3323. }
  3324. function get_option($db, $option_id)
  3325. {
  3326. $option = get_record($db, "config", "option_id=" . $option_id);
  3327. if (empty($option) or empty($option['value'])) {
  3328. $default = get_record($db, "config_options", "id=$option_id");
  3329. return $default['default_value'];
  3330. }
  3331. return $option['value'];
  3332. }
  3333. function is_option($db, $option_id)
  3334. {
  3335. $option = get_record($db, "config", "option_id=" . $option_id);
  3336. if (empty($option) or empty($option['value'])) {
  3337. return;
  3338. }
  3339. return 1;
  3340. }
  3341. function set_option($db, $option_id, $value)
  3342. {
  3343. $option['value'] = $value;
  3344. update_record($db, 'config', "option_id=$option_id", $option);
  3345. }
  3346. function is_subnet_aton($subnet, $ip)
  3347. {
  3348. if (!isset($subnet)) {
  3349. return 0;
  3350. }
  3351. if (!isset($ip)) {
  3352. return 0;
  3353. }
  3354. $range = cidrToRange($subnet);
  3355. if ($ip >= ip2long($range[0]) and $ip <= ip2long($range[1])) {
  3356. return 1;
  3357. }
  3358. return 0;
  3359. }
  3360. function get_new_user_id($db, $ip, $mac, $hostname)
  3361. {
  3362. $result['ip'] = $ip;
  3363. $result['mac'] = mac_dotted($mac);
  3364. $result['hostname'] = $hostname;
  3365. $result['user_id'] = NULL;
  3366. $result['ou_id'] = NULL;
  3367. $ip_aton = ip2long($ip);
  3368. //personal user rules
  3369. //ip
  3370. if (!empty($ip)) {
  3371. $t_rules = get_records_sql($db, "SELECT * FROM auth_rules WHERE type=1 and LENGTH(rule)>0 AND user_id IS NOT NULL");
  3372. foreach ($t_rules as $row) {
  3373. if (!empty($row['rule']) and is_subnet_aton($row['rule'], $ip_aton)) {
  3374. $result['user_id'] = $row['user_id'];
  3375. }
  3376. }
  3377. }
  3378. //mac
  3379. if (!empty($mac)) {
  3380. $mac_rules = get_records_sql($db, "SELECT * FROM auth_rules WHERE type=2 AND LENGTH(rule)>0 AND user_id IS NOT NULL");
  3381. foreach ($mac_rules as $row) {
  3382. $pattern = '/' . mac_simplify($row['rule']) . '/';
  3383. if (!empty($row['rule']) and preg_match($pattern, mac_simplify($mac))) {
  3384. $result['user_id'] = $row['user_id'];
  3385. }
  3386. }
  3387. }
  3388. //hostname
  3389. if (!empty($hostname)) {
  3390. $mac_rules = get_records_sql($db, "SELECT * FROM auth_rules WHERE type=3 AND LENGTH(rule)>0 AND user_id IS NOT NULL");
  3391. foreach ($mac_rules as $row) {
  3392. if (!empty($row['rule']) and preg_match($row['rule'], $hostname)) {
  3393. $result['user_id'] = $row['user_id'];
  3394. }
  3395. }
  3396. }
  3397. if (!empty($result['user_id'])) {
  3398. return $result;
  3399. }
  3400. //ou rules
  3401. //ip
  3402. if (!empty($ip)) {
  3403. if (is_hotspot($db, $ip)) {
  3404. $result['ou_id'] = get_const('default_hotspot_ou_id');
  3405. }
  3406. $t_rules = get_records_sql($db, "SELECT * FROM auth_rules WHERE type=1 and LENGTH(rule)>0 AND ou_id IS NOT NULL");
  3407. foreach ($t_rules as $row) {
  3408. if (!empty($row['rule']) and is_subnet_aton($row['rule'], $ip_aton)) {
  3409. $result['ou_id'] = $row['ou_id'];
  3410. }
  3411. }
  3412. }
  3413. //mac
  3414. if (!empty($mac)) {
  3415. $mac_rules = get_records_sql($db, "SELECT * FROM auth_rules WHERE type=2 AND LENGTH(rule)>0 AND ou_id IS NOT NULL");
  3416. foreach ($mac_rules as $row) {
  3417. $pattern = '/' . mac_simplify($row['rule']) . '/';
  3418. if (!empty($row['rule']) and preg_match($pattern, mac_simplify($mac))) {
  3419. $result['ou_id'] = $row['ou_id'];
  3420. }
  3421. }
  3422. }
  3423. //hostname
  3424. if (!empty($hostname)) {
  3425. $mac_rules = get_records_sql($db, "SELECT * FROM auth_rules WHERE type=3 AND LENGTH(rule)>0 AND ou_id IS NOT NULL");
  3426. foreach ($mac_rules as $row) {
  3427. if (!empty($row['rule']) and preg_match($row['rule'], $hostname)) {
  3428. $result['ou_id'] = $row['ou_id'];
  3429. }
  3430. }
  3431. }
  3432. if (empty($result['ou_id'])) {
  3433. $result['ou_id'] = get_const('default_user_ou_id');
  3434. }
  3435. return $result;
  3436. }
  3437. function get_subnet_range($db, $subnet_id)
  3438. {
  3439. if (empty($subnet_id)) {
  3440. return;
  3441. }
  3442. $t_option = get_record_sql($db, "SELECT ip_int_start,ip_int_stop FROM `subnets` WHERE id=$subnet_id");
  3443. if (!isset($t_option['ip_int_start'])) {
  3444. $t_option['ip_int_start'] = 0;
  3445. }
  3446. if (!isset($t_option['ip_int_stop'])) {
  3447. $t_option['ip_int_stop'] = 0;
  3448. }
  3449. $subnet['start'] = $t_option['ip_int_start'];
  3450. $subnet['stop'] = $t_option['ip_int_stop'];
  3451. return $subnet;
  3452. }
  3453. function int_between($value, $start, $end)
  3454. {
  3455. return in_array($value, range($start, $end));
  3456. }
  3457. function is_gray_network($ip)
  3458. {
  3459. if (empty($ip)) {
  3460. return 0;
  3461. }
  3462. $ip_aton = ip2long($ip);
  3463. $gray_nets = array('10.0.0.0/8', '192.168.0.0/16', '172.16.0.0/12', '100.64.0.0/10');
  3464. foreach ($gray_nets as &$net) {
  3465. $net_cidr = cidrToRange($net);
  3466. if (int_between($ip_aton, ip2long($net_cidr[0]), ip2long($net_cidr[1]))) {
  3467. return $net;
  3468. }
  3469. }
  3470. return 0;
  3471. }
  3472. function is_hotspot($db, $ip)
  3473. {
  3474. if (!isset($ip)) {
  3475. return 0;
  3476. }
  3477. LOG_DEBUG($db, "Check hotspot network for ip: $ip");
  3478. $ip_aton = ip2long($ip);
  3479. $t_option = mysqli_query($db, "SELECT subnet,ip_int_start,ip_int_stop FROM `subnets` WHERE hotspot=1");
  3480. while (list($f_net, $f_start, $f_stop) = mysqli_fetch_array($t_option)) {
  3481. if ($ip_aton >= $f_start and $ip_aton <= $f_stop) {
  3482. LOG_DEBUG($db, "ip: $ip [$ip_aton] found in network $f_net: [" . $f_start . ".." . $f_stop . "]");
  3483. return 1;
  3484. }
  3485. }
  3486. LOG_DEBUG($db, "ip $ip not found in hotspot network!");
  3487. return 0;
  3488. }
  3489. function is_office($db, $ip)
  3490. {
  3491. if (!isset($ip)) {
  3492. return 0;
  3493. }
  3494. LOG_DEBUG($db, "Check office network for ip: $ip");
  3495. $ip_aton = ip2long($ip);
  3496. $t_option = mysqli_query($db, "SELECT subnet,ip_int_start,ip_int_stop FROM `subnets` WHERE office=1");
  3497. while (list($f_net, $f_start, $f_stop) = mysqli_fetch_array($t_option)) {
  3498. if ($ip_aton >= $f_start and $ip_aton <= $f_stop) {
  3499. LOG_DEBUG($db, "ip: $ip [$ip_aton] found in office $f_net: [" . $f_start . ".." . $f_stop . "]");
  3500. return 1;
  3501. }
  3502. }
  3503. LOG_DEBUG($db, "ip $ip not found in office network!");
  3504. return 0;
  3505. }
  3506. function is_our_network($db, $ip)
  3507. {
  3508. if (!isset($ip)) {
  3509. return 0;
  3510. }
  3511. if (is_hotspot($db, $ip)) {
  3512. return 1;
  3513. }
  3514. if (is_office($db, $ip)) {
  3515. return 1;
  3516. }
  3517. return 0;
  3518. }
  3519. function get_const($const_name)
  3520. {
  3521. global $config;
  3522. if (isset($config[$const_name])) {
  3523. return $config[$const_name];
  3524. }
  3525. return NULL;
  3526. }
  3527. function get_eye_version($db)
  3528. {
  3529. $v_table = get_record_sql($db, "SELECT version FROM version");
  3530. if (!empty($v_table)) {
  3531. return $v_table['version'];
  3532. }
  3533. return NULL;
  3534. }
  3535. $config["org_name"] = get_option($db_link, 32);
  3536. $config["version"] = get_eye_version($db_link);
  3537. $config["KB"] = get_option($db_link, 1);
  3538. if ($config["KB"] == 0) {
  3539. $config["KB"] = 1000;
  3540. }
  3541. if ($config["KB"] == 1) {
  3542. $config["KB"] = 1024;
  3543. }
  3544. $config["debug"] = get_option($db_link, 34);
  3545. $config["log_level"] = get_option($db_link, 53);
  3546. if ($config["debug"]) {
  3547. $config["log_level"] = 255;
  3548. }
  3549. $config["send_email"] = get_option($db_link, 51);
  3550. $config["admin_email"] = get_option($db_link, 21);
  3551. $config["sender_email"] = get_option($db_link, 52);
  3552. $config["snmp_default_version"] = get_option($db_link, 9);
  3553. $config["snmp_default_community"] = get_option($db_link, 11);
  3554. $config["auto_mac_rule"] = get_option($db_link, 64);
  3555. $config["cacti_url"] = rtrim(get_option($db_link, 58), '/');
  3556. if (preg_match('/127.0.0.1/', $config["cacti_url"])) {
  3557. $config["cacti_url"] = NULL;
  3558. }
  3559. $config["nagios_url"] = rtrim(get_option($db_link, 57), '/') . '/cgi-bin/';
  3560. if (preg_match('/127.0.0.1/', $config["nagios_url"])) {
  3561. $config["nagios_url"] = NULL;
  3562. }
  3563. $config["torrus_url"] = rtrim(get_option($db_link, 59), '/') . '?nodeid=if//HOST_IP//IF_NAME////inoutbps';
  3564. if (preg_match('/127.0.0.1/', $config["torrus_url"])) {
  3565. $config["torrus_url"] = NULL;
  3566. }
  3567. $config["dns_server"] = get_option($db_link, 3);
  3568. $config["dns_server_type"] = get_option($db_link, 70);
  3569. $ou = get_record_sql($db_link, "SELECT id FROM OU WHERE default_users = 1");
  3570. if (empty($ou)) {
  3571. $config["default_user_ou_id"] = 0;
  3572. } else {
  3573. $config["default_user_ou_id"] = $ou['id'];
  3574. }
  3575. $ou = get_record_sql($db_link, "SELECT id FROM OU WHERE default_hotspot=1");
  3576. if (empty($ou)) {
  3577. $config["default_hotspot_ou_id"] = $config["default_user_ou_id"];
  3578. } else {
  3579. $config["default_hotspot_ou_id"] = $ou['id'];
  3580. }
  3581. $config["init"] = 1;
  3582. clean_dns_cache($db_link);
  3583. //clean_unreferensed_rules($db_link);