common.php 126 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937
  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_rules_target_user, 1, $qa_value);
  1094. print_select_item(WEB_rules_target_group, 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; font-weight: bold;">✓</span>'; }
  1168. return '<span style="font-size: 16px;">✗</span>';
  1169. }
  1170. }
  1171. function get_yes($qa_value, $text = FALSE)
  1172. {
  1173. if ($text) {
  1174. if ($qa_value == 1) { return "Да"; }
  1175. return "";
  1176. } else {
  1177. if ($qa_value == 1) { return '<span style="font-size: 16px; font-weight: bold;">✓</span>'; }
  1178. return "";
  1179. }
  1180. }
  1181. function print_td_qa ($qa_value, $text = FALSE, $parent_class = '')
  1182. {
  1183. $cl = "data_green";
  1184. if (!$qa_value) { $cl = "data_red"; }
  1185. print "<td class=\"$parent_class $cl\" >" . get_qa($qa_value,$text) . "</td>";
  1186. }
  1187. function print_td_yes ($qa_value, $text = FALSE, $parent_class = '')
  1188. {
  1189. $cl = "data_green";
  1190. if (!$qa_value) { $cl = "data_red"; }
  1191. print "<td class=\"$parent_class $cl\" >" . get_yes($qa_value,$text) . "</td>";
  1192. }
  1193. function print_action_select($action_name, $action_value)
  1194. {
  1195. print "<select id=\"$action_name\" name=\"$action_name\">\n";
  1196. print_select_item(WEB_select_item_allow, 1, $action_value);
  1197. print_select_item(WEB_select_item_forbidden, 0, $action_value);
  1198. print "</select>\n";
  1199. }
  1200. function get_action($action_value)
  1201. {
  1202. if ($action_value == 1) {
  1203. return "Разрешить";
  1204. }
  1205. return "Запретить";
  1206. }
  1207. function print_filter_select($db, $filter_name, $group_id)
  1208. {
  1209. print "<select id=\"$filter_name\" name=\"$filter_name\" class=\"js-select-single\">\n";
  1210. if (isset($group_id)) {
  1211. $sSQL = "SELECT id,name FROM Filter_list WHERE Filter_list.id not in (Select filter_id FROM Group_filters WHERE group_id=$group_id)";
  1212. } else {
  1213. $sSQL = "SELECT id,name FROM Filter_list Order by name";
  1214. }
  1215. $t_filters = mysqli_query($db, $sSQL);
  1216. while (list($filter_id, $filter_name) = mysqli_fetch_array($t_filters)) {
  1217. print_select_item($filter_name, $filter_id, 0);
  1218. }
  1219. print "</select>\n";
  1220. }
  1221. function get_filter($db, $filter_value)
  1222. {
  1223. list($filter) = mysqli_fetch_array(mysqli_query($db, "SELECT name FROM Filter_list WHERE id=" . $filter_value));
  1224. return $filter;
  1225. }
  1226. function get_login($db, $user_id)
  1227. {
  1228. list($login) = mysqli_fetch_array(mysqli_query($db, "SELECT login FROM User_list WHERE id=$user_id"));
  1229. return $login;
  1230. }
  1231. function get_auth_count($db, $user_id)
  1232. {
  1233. list($count) = mysqli_fetch_array(mysqli_query($db, "SELECT count(id) FROM User_auth WHERE user_id=$user_id and deleted=0"));
  1234. return $count;
  1235. }
  1236. function print_login_select($db, $login_name, $current_login)
  1237. {
  1238. print "<select id=\"$login_name\" name=\"$login_name\" class=\"js-select-single\">\n";
  1239. $t_login = mysqli_query($db, "SELECT id,login FROM User_list Order by Login");
  1240. print_select_item('None', 0, $current_login);
  1241. while (list($f_user_id, $f_login) = mysqli_fetch_array($t_login)) {
  1242. print_select_item($f_login, $f_user_id, $current_login);
  1243. }
  1244. print "</select>\n";
  1245. }
  1246. function print_auth_select($db, $login_name, $current_auth)
  1247. {
  1248. print "<select id=\"$login_name\" name=\"$login_name\" class=\"js-select-single\">\n";
  1249. $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");
  1250. print_select_item('Empty', 0, $current_auth);
  1251. while (list($f_login, $f_fio, $f_ip, $f_auth_id) = mysqli_fetch_array($t_login)) {
  1252. print_select_item($f_login . "[" . $f_fio . "] - " . $f_ip, $f_auth_id, $current_auth);
  1253. }
  1254. print "</select>\n";
  1255. }
  1256. function print_auth_select_mac($db, $login_name, $current_auth)
  1257. {
  1258. print "<select id=\"$login_name\" name=\"$login_name\" class=\"js-select-single\">\n";
  1259. $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");
  1260. print_select_item('Empty', 0, $current_auth);
  1261. while (list($f_login, $f_fio, $f_ip, $f_mac, $f_auth_id) = mysqli_fetch_array($t_login)) {
  1262. print_select_item($f_login . "[" . $f_mac . "] - " . $f_ip, $f_auth_id, $current_auth);
  1263. }
  1264. print "</select>\n";
  1265. }
  1266. function compact_port_name($port)
  1267. {
  1268. $result = $port;
  1269. $result = preg_replace('/XGigabitEthernet/', 'X', $result);
  1270. $result = preg_replace('/TenGigabitEthernet/', 'Te', $result);
  1271. $result = preg_replace('/GigabitEthernet/', 'Gi', $result);
  1272. return $result;
  1273. }
  1274. function print_device_port_select($db, $field_name, $device_id, $target_id)
  1275. {
  1276. print "<select id=\"$field_name\" name=\"$field_name\" class=\"js-select-single\">\n";
  1277. if (empty($target_id)) {
  1278. $target_id = 0;
  1279. }
  1280. if (empty($device_id)) {
  1281. $device_id = 0;
  1282. }
  1283. $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";
  1284. $t_device = mysqli_query($db, $d_sql);
  1285. print_select_item('Empty', 0, $target_id);
  1286. while (list($f_name, $f_port, $f_device_id, $f_target_id, $f_ifname) = mysqli_fetch_array($t_device)) {
  1287. if (empty($f_ifname)) {
  1288. $f_ifname = $f_port;
  1289. }
  1290. print_select_item($f_name . "[" . $f_port . "] - " . compact_port_name($f_ifname), $f_target_id, $target_id);
  1291. }
  1292. print "</select>\n";
  1293. }
  1294. function print_device_select($db, $field_name, $device_id)
  1295. {
  1296. print "<select id=\"$field_name\" name=\"$field_name\" class=\"js-select-single\" >\n";
  1297. $d_sql = "SELECT D.device_name, D.id FROM devices AS D Where D.deleted=0 order by D.device_name ASC";
  1298. $t_device = mysqli_query($db, $d_sql);
  1299. print_select_item(WEB_select_item_every, 0, $device_id);
  1300. while (list($f_name, $f_device_id) = mysqli_fetch_array($t_device)) {
  1301. print_select_item($f_name, $f_device_id, $device_id);
  1302. }
  1303. print "</select>\n";
  1304. }
  1305. function print_netdevice_select($db, $field_name, $device_id)
  1306. {
  1307. print "<select id=\"$field_name\" name=\"$field_name\" class=\"js-select-single\" >\n";
  1308. $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";
  1309. $t_device = mysqli_query($db, $d_sql);
  1310. print_select_item(WEB_select_item_every, 0, $device_id);
  1311. while (list($f_name, $f_device_id) = mysqli_fetch_array($t_device)) {
  1312. print_select_item($f_name, $f_device_id, $device_id);
  1313. }
  1314. print "</select>\n";
  1315. }
  1316. function print_vlan_select($db, $field_name, $vlan)
  1317. {
  1318. print "<select id=\"$field_name\" name=\"$field_name\" class=\"js-select-single\">\n";
  1319. $d_sql = "SELECT DISTINCT vlan FROM device_ports ORDER BY vlan DESC";
  1320. $v_device = mysqli_query($db, $d_sql);
  1321. if (!isset($vlan) or empty($vlan)) {
  1322. $vlan = 1;
  1323. };
  1324. print_select_item('1', 1, $vlan);
  1325. while (list($f_vlan) = mysqli_fetch_array($v_device)) {
  1326. if ($f_vlan === '1') {
  1327. continue;
  1328. }
  1329. print_select_item($f_vlan, $f_vlan, $vlan);
  1330. }
  1331. print "</select>\n";
  1332. }
  1333. function print_device_select_ip($db, $field_name, $device_ip)
  1334. {
  1335. print "<select id=\"$field_name\" name=\"$field_name\" class=\"js-select-single\" >\n";
  1336. $d_sql = "SELECT D.device_name, D.ip FROM devices AS D Where D.deleted=0 order by D.device_name ASC";
  1337. $t_device = mysqli_query($db, $d_sql);
  1338. print_select_item(WEB_select_item_every, '', $device_ip);
  1339. while (list($f_name, $f_device_ip) = mysqli_fetch_array($t_device)) {
  1340. print_select_item($f_name, $f_device_ip, $device_ip);
  1341. }
  1342. print "</select>\n";
  1343. }
  1344. function print_syslog_device_select($db, $field_name, $syslog_filter, $device_ip)
  1345. {
  1346. print "<select id=\"$field_name\" name=\"$field_name\" class=\"js-select-single\" >\n";
  1347. $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";
  1348. $t_device = mysqli_query($db, $d_sql);
  1349. print_select_item(WEB_select_item_every, '', $device_ip);
  1350. while (list($f_ip, $f_name) = mysqli_fetch_array($t_device)) {
  1351. if (!isset($f_name) or empty($f_name)) {
  1352. $f_name = $f_ip;
  1353. }
  1354. print_select_item($f_name, $f_ip, $device_ip);
  1355. }
  1356. print "</select>\n";
  1357. }
  1358. function print_gateway_select($db, $field_name, $device_id)
  1359. {
  1360. print "<select id=\"$field_name\" name=\"$field_name\" >\n";
  1361. $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";
  1362. $t_device = mysqli_query($db, $d_sql);
  1363. print_select_item(WEB_select_item_every, 0, $device_id);
  1364. while (list($f_name, $f_device_id) = mysqli_fetch_array($t_device)) {
  1365. print_select_item($f_name, $f_device_id, $device_id);
  1366. }
  1367. print "</select>\n";
  1368. }
  1369. function get_gateways($db)
  1370. {
  1371. $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";
  1372. $t_device = mysqli_query($db, $d_sql);
  1373. unset($result);
  1374. while (list($f_name, $f_device_id) = mysqli_fetch_array($t_device)) {
  1375. $result[$f_device_id] = $f_name;
  1376. }
  1377. return $result;
  1378. }
  1379. function print_device_port($db, $target_id)
  1380. {
  1381. $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";
  1382. $t_device = mysqli_query($db, $d_sql);
  1383. while (list($f_name, $f_port, $f_device_id) = mysqli_fetch_array($t_device)) {
  1384. print "<a href=\"/admin/devices/switchport.php?id=$f_device_id\">" . $f_name . "[" . $f_port . "]</a>\n";
  1385. }
  1386. }
  1387. function get_device_ips($db, $device_id)
  1388. {
  1389. $switch = get_record($db, 'devices', 'id=' . $device_id);
  1390. $index = 0;
  1391. if (!empty($switch['user_id'])) {
  1392. $auth_ips = get_records($db, 'User_auth', 'deleted=0 and user_id=' . $switch['user_id']);
  1393. foreach ($auth_ips as $key => $value) {
  1394. if (isset($value['ip'])) {
  1395. $result[$index] = $value['ip'];
  1396. $index++;
  1397. }
  1398. }
  1399. } else {
  1400. if (isset($switch['ip'])) {
  1401. $result[$index] = $switch['ip'];
  1402. $index++;
  1403. }
  1404. }
  1405. return $result;
  1406. }
  1407. function get_device_id($db, $device_name)
  1408. {
  1409. $d_sql = "SELECT id FROM devices WHERE device_name='$device_name' and deleted=0";
  1410. $dev = get_record_sql($db, $d_sql);
  1411. if (empty($dev)) {
  1412. return NULL;
  1413. }
  1414. return $dev["id"];
  1415. }
  1416. function get_device_name($db, $device_id)
  1417. {
  1418. $d_sql = "SELECT device_name FROM devices WHERE id='$device_id'";
  1419. $dev = get_record_sql($db, $d_sql);
  1420. if (empty($dev)) {
  1421. return NULL;
  1422. }
  1423. return $dev["device_name"];
  1424. }
  1425. function get_auth_by_ip($db, $ip)
  1426. {
  1427. $d_sql = "SELECT id FROM User_auth WHERE ip='$ip' and deleted=0";
  1428. $auth = get_record_sql($db, $d_sql);
  1429. if (empty($auth)) {
  1430. return NULL;
  1431. }
  1432. return $auth["id"];
  1433. }
  1434. function get_user_by_ip($db, $ip)
  1435. {
  1436. $d_sql = "SELECT user_id FROM User_auth WHERE ip='$ip' and deleted=0";
  1437. $auth = get_record_sql($db, $d_sql);
  1438. if (empty($auth)) {
  1439. return NULL;
  1440. }
  1441. return $auth["user_id"];
  1442. }
  1443. function get_device_by_auth($db, $id)
  1444. {
  1445. $d_sql = "SELECT id FROM devices WHERE user_id=$id and deleted=0";
  1446. $f_dev = get_record_sql($db, $d_sql);
  1447. if (empty($f_dev)) {
  1448. return NULL;
  1449. }
  1450. return $f_dev['id'];
  1451. }
  1452. function print_auth_port($db, $port_id, $new_window = FALSE)
  1453. {
  1454. $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";
  1455. $t_auth = mysqli_query($db, $d_sql);
  1456. while (list($f_ip, $f_int, $f_mac, $f_auth_id, $f_dns, $f_user_id) = mysqli_fetch_array($t_auth)) {
  1457. $name = $f_ip;
  1458. if (!empty($f_dns)) {
  1459. $name = $f_dns;
  1460. }
  1461. $title = get_login($db, $f_user_id) . " =>" . $f_ip . "[" . $f_mac . "]";
  1462. if (!empty($f_dns)) {
  1463. $title .= " | " . $f_dns;
  1464. }
  1465. if ($new_window) {
  1466. print "<a href=\"\" title=\"" . $title . "\" onclick=\"" . open_window_url("/admin/users/editauth.php?id=" . $f_auth_id) . " return false;\">" . $name . " [" . $f_ip . "]</a><br>";
  1467. } else {
  1468. print "<a href=/admin/users/editauth.php?id=" . $f_auth_id . " title=\"" . $title . "\" >" . $name . " [" . $f_ip . "]</a><br>";
  1469. }
  1470. }
  1471. }
  1472. function get_port_comment($db, $port_id, $port_comment = '')
  1473. {
  1474. $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";
  1475. $t_auth = mysqli_query($db, $d_sql);
  1476. $comment_found = 0;
  1477. $result = '';
  1478. while (list($f_int, $f_comment) = mysqli_fetch_array($t_auth)) {
  1479. if (!empty($f_comment)) {
  1480. $comment_found = 1;
  1481. } else {
  1482. $f_comment = '';
  1483. }
  1484. $result .= $f_comment . '<br>';
  1485. }
  1486. if (!$comment_found) {
  1487. return $port_comment;
  1488. }
  1489. if (!empty($port_comment)) {
  1490. $result .= '(' . $port_comment . ')';
  1491. }
  1492. return $result;
  1493. }
  1494. function print_auth_simple($db, $auth_id)
  1495. {
  1496. $auth = get_record($db, "User_auth", "id=$auth_id");
  1497. $name = $auth['dns_name'];
  1498. if (empty($name)) {
  1499. $name = $auth['comments'];
  1500. }
  1501. if (empty($name)) {
  1502. $name = $auth['ip'];
  1503. }
  1504. print "<a href=\"/admin/users/editauth.php?id=$auth_id\">" . $name . "</a><br>";
  1505. }
  1506. function print_auth($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. print "<a href=\"/admin/users/editauth.php?id=$auth_id\">" . $name . "</a><br>";
  1521. }
  1522. function print_auth_detail($db, $auth_id)
  1523. {
  1524. $auth = get_record($db, "User_auth", "id=$auth_id");
  1525. $name = $auth['dns_name'];
  1526. if (empty($name)) {
  1527. $name = $auth['comments'];
  1528. } else {
  1529. $name .= " (" . $auth['comments'] . ")";
  1530. }
  1531. if (empty($name)) {
  1532. $name = $auth['ip'];
  1533. } else {
  1534. $name .= " [" . $auth['ip'] . "]";
  1535. }
  1536. $name .= " last: [" . $auth['last_found'] . "] ";
  1537. if ($auth['deleted'] == 1) {
  1538. $name .= " <font color='red'>DELETED!!!</font>";
  1539. }
  1540. print "<a href=\"/admin/users/editauth.php?id=$auth_id\">" . $name . "</a><br>";
  1541. }
  1542. function get_auth_port_count($db, $port_id)
  1543. {
  1544. $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";
  1545. $t_device = mysqli_query($db, $d_sql);
  1546. list($f_count) = mysqli_fetch_array($t_device);
  1547. if (!isset($f_count)) {
  1548. $f_count = 0;
  1549. }
  1550. return $f_count;
  1551. }
  1552. function get_connection($db, $auth_id)
  1553. {
  1554. $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";
  1555. $t_device = mysqli_query($db, $d_sql);
  1556. list($f_name, $f_port) = mysqli_fetch_array($t_device);
  1557. if (isset($f_name)) {
  1558. $result = expand_device_name($db, $f_name) . "[" . $f_port . "]";
  1559. } else {
  1560. $result = '';
  1561. }
  1562. return $result;
  1563. }
  1564. function get_connection_string($db, $auth_id)
  1565. {
  1566. $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";
  1567. $t_device = mysqli_query($db, $d_sql);
  1568. list($f_name, $f_port) = mysqli_fetch_array($t_device);
  1569. if (isset($f_name)) {
  1570. $result = $f_name . "[" . $f_port . "]";
  1571. } else {
  1572. $result = '';
  1573. }
  1574. return $result;
  1575. }
  1576. function get_port($db, $port_id)
  1577. {
  1578. $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";
  1579. $t_device = mysqli_query($db, $d_sql);
  1580. list($f_name, $f_port) = mysqli_fetch_array($t_device);
  1581. if (isset($f_name)) {
  1582. $result = expand_device_name($db, $f_name) . "[" . $f_port . "]";
  1583. } else {
  1584. $result = '';
  1585. }
  1586. return $result;
  1587. }
  1588. function print_option_select($db, $option_name)
  1589. {
  1590. print "<select id=\"$option_name\" name=\"$option_name\">\n";
  1591. $t_option = mysqli_query($db, "SELECT id,option_name FROM config_options WHERE uniq=0 AND draft=0 order by option_name");
  1592. while (list($f_id, $f_name) = mysqli_fetch_array($t_option)) {
  1593. print "<option value=$f_id>$f_name</option>";
  1594. }
  1595. $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");
  1596. while (list($f_id, $f_name) = mysqli_fetch_array($t_option)) {
  1597. print "<option value=$f_id>$f_name</option>";
  1598. }
  1599. print "</select>\n";
  1600. }
  1601. function run_sql($db, $query)
  1602. {
  1603. if (preg_match('/^\s*(UPDATE|DELETE)/i', $query)) {
  1604. unset($matches);
  1605. preg_match('/FROM\s+(.*)\s+/i', $query, $matches);
  1606. if (!empty($matches[1])) {
  1607. if (!allow_update($matches[1], 'del')) {
  1608. LOG_DEBUG($db, "Access denied: $query ");
  1609. return;
  1610. }
  1611. }
  1612. unset($matches);
  1613. preg_match('/INSERT\s+INTO\s+(.*)\s+/i', $query, $matches);
  1614. if (!empty($matches[1])) {
  1615. if (!allow_update($matches[1], 'add')) {
  1616. LOG_DEBUG($db, "Access denied: $query ");
  1617. return;
  1618. }
  1619. }
  1620. unset($matches);
  1621. preg_match('/UPDATE\s+(.*)\s+/i', $query, $matches);
  1622. if (!empty($matches[1])) {
  1623. if (!allow_update($matches[1], 'update')) {
  1624. LOG_DEBUG($db, "Access denied: $query ");
  1625. return;
  1626. }
  1627. }
  1628. unset($matches);
  1629. }
  1630. $sql_result = mysqli_query($db, $query);
  1631. if (!$sql_result) {
  1632. LOG_ERROR($db, "At simple SQL: $query :" . mysqli_error($db));
  1633. return;
  1634. }
  1635. return $sql_result;
  1636. }
  1637. function get_count_records($db, $table, $filter)
  1638. {
  1639. if (!empty($filter)) {
  1640. $filter = 'where ' . $filter;
  1641. }
  1642. $t_count = mysqli_query($db, "SELECT count(*) FROM $table $filter");
  1643. list($count) = mysqli_fetch_array($t_count);
  1644. if (!isset($count)) {
  1645. $count = 0;
  1646. }
  1647. return $count;
  1648. }
  1649. function get_id_record($db, $table, $filter)
  1650. {
  1651. if (isset($filter)) {
  1652. $filter = 'WHERE ' . $filter;
  1653. }
  1654. $t_record = mysqli_query($db, "SELECT id FROM $table $filter limit 1");
  1655. list($id) = mysqli_fetch_array($t_record);
  1656. return $id;
  1657. }
  1658. function set_changed($db, $id)
  1659. {
  1660. $auth['changed'] = 1;
  1661. update_record($db, "User_auth", "id=" . $id, $auth);
  1662. }
  1663. function ResolveIP($db, $ip_int)
  1664. {
  1665. $ip_name = "-";
  1666. if (empty($ip_int)) {
  1667. return $ip_name;
  1668. }
  1669. $dns_cache = get_record_sql($db, "SELECT * FROM dns_cache WHERE ip=$ip_int");
  1670. if (empty($dns_cache) or empty($dns_cache['dns'])) {
  1671. $ip_name = gethostbyaddr(long2ip($ip_int));
  1672. if (empty($ip_name) or $ip_name == long2ip($ip_int)) {
  1673. $ip_name = "-";
  1674. }
  1675. run_sql($db, "INSERT INTO dns_cache(dns,ip) VALUES('" . $ip_name . "'," . $ip_int . ")");
  1676. } else {
  1677. $ip_name = $dns_cache['dns'];
  1678. }
  1679. return $ip_name;
  1680. }
  1681. function clean_dns_cache($db)
  1682. {
  1683. $date = time();
  1684. $date = $date - 86400;
  1685. $date_clean = DateTimeImmutable::createFromFormat('U', $date);
  1686. $clean_date = $date_clean->format('Y-m-d H:i:s');
  1687. run_sql($db, "DELETE FROM dns_cache WHERE `timestamp`<='" . $clean_date . "'");
  1688. }
  1689. function clean_unreferensed_rules($db)
  1690. {
  1691. run_sql($db, "DELETE FROM `auth_rules` WHERE user_id NOT IN (SELECT id FROM User_list)");
  1692. }
  1693. function FormatDateStr($format = 'Y-m-d H:i:s', $date_str)
  1694. {
  1695. $date1 = GetDateTimeFromString($date_str);
  1696. $result = $date1->format($format);
  1697. return $result;
  1698. }
  1699. function GetDateTimeFromString($date_str)
  1700. {
  1701. if (!is_a($date_str, 'DateTime')) {
  1702. $t_date_str = urldecode($date_str);
  1703. $t_date_str = preg_replace('/(\'|\")/', '', $t_date_str);
  1704. $t_date_str = preg_replace('/T/', ' ', $t_date_str);
  1705. $date1 = DateTime::createFromFormat('Y-m-d H:i:s', $t_date_str);
  1706. if (!$date1) {
  1707. $date1 = DateTime::createFromFormat('Y.m.d H:i:s', $t_date_str);
  1708. }
  1709. if (!$date1) {
  1710. $date1 = DateTime::createFromFormat('Y/m/d H:i:s', $t_date_str);
  1711. }
  1712. if (!$date1) {
  1713. $date1 = DateTime::createFromFormat('Y-m-d H:i', $t_date_str);
  1714. }
  1715. if (!$date1) {
  1716. $date1 = DateTime::createFromFormat('Y.m.d H:i', $t_date_str);
  1717. }
  1718. if (!$date1) {
  1719. $date1 = DateTime::createFromFormat('Y/m/d H:i', $t_date_str);
  1720. }
  1721. if (!$date1) {
  1722. $date1 = DateTime::createFromFormat('Y-m-d|', $t_date_str);
  1723. }
  1724. if (!$date1) {
  1725. $date1 = DateTime::createFromFormat('Y.m.d|', $t_date_str);
  1726. }
  1727. if (!$date1) {
  1728. $date1 = DateTime::createFromFormat('Y/m/d|', $t_date_str);
  1729. }
  1730. if (!$date1) {
  1731. $date1 = new DateTime;
  1732. $date1->setTime(0, 0, 0, 1);
  1733. }
  1734. } else {
  1735. return $date_str;
  1736. }
  1737. return $date1;
  1738. }
  1739. function GetNowTimeString()
  1740. {
  1741. $now = new DateTimeImmutable('now');
  1742. $result = $now->format('Y-m-d H:i:s');
  1743. return $result;
  1744. }
  1745. function GetNowDayString()
  1746. {
  1747. $now = new DateTimeImmutable('now');
  1748. $result = $now->format('Y-m-d');
  1749. return $result;
  1750. }
  1751. function get_ip_subnet($db, $ip)
  1752. {
  1753. if (empty($ip)) {
  1754. return;
  1755. }
  1756. $ip_aton = ip2long($ip);
  1757. $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)");
  1758. if (empty($user_subnet)) {
  1759. return;
  1760. }
  1761. return $user_subnet;
  1762. }
  1763. function find_mac_in_subnet($db, $ip, $mac)
  1764. {
  1765. if (empty($ip)) {
  1766. return;
  1767. }
  1768. if (empty($mac)) {
  1769. return;
  1770. }
  1771. $ip_subnet = get_ip_subnet($db, $ip);
  1772. if (empty($ip_subnet)) {
  1773. return;
  1774. }
  1775. $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");
  1776. $auth_count = 0;
  1777. $result['count'] = 0;
  1778. $result['users_id'] = [];
  1779. foreach ($t_auth as $row) {
  1780. if (!empty($row['id'])) {
  1781. $auth_count++;
  1782. $result['count'] = $auth_count;
  1783. $result[$auth_count] = $row['id'];
  1784. array_push($result['users_id'], $row['user_id']);
  1785. }
  1786. }
  1787. return $result;
  1788. }
  1789. function apply_auth_rule($db, $auth_record, $user_id)
  1790. {
  1791. if (empty($auth_record)) {
  1792. return;
  1793. }
  1794. if (empty($user_id)) {
  1795. return $auth_record;
  1796. }
  1797. $user_rec = get_record($db, 'User_list', "id=" . $user_id);
  1798. if (empty($user_rec)) {
  1799. return $auth_record;
  1800. }
  1801. //set filter and status by user
  1802. $auth_record['ou_id'] = $user_rec['ou_id'];
  1803. $auth_record['user_id'] = $user_rec['id'];
  1804. $auth_record['filter_group_id'] = $user_rec['filter_group_id'];
  1805. $auth_record['queue_id'] = $user_rec['queue_id'];
  1806. $auth_record['enabled'] = $user_rec['enabled'];
  1807. $auth_record['changed'] = 1;
  1808. //maybe fill comments?
  1809. if (!empty($user_rec['fio']) and empty($auth_record['comments'])) {
  1810. $auth_record['comments'] = $user_rec['fio'];
  1811. }
  1812. return $auth_record;
  1813. }
  1814. function fix_auth_rules($db)
  1815. {
  1816. //cleanup hotspot subnet rules
  1817. delete_record($db, "auth_rules", "ou_id=" . get_const('default_user_ou_id'));
  1818. delete_record($db, "auth_rules", "ou_id=" . get_const('default_hotspot_ou_id'));
  1819. $t_hotspot = get_records_sql($db, "SELECT * FROM subnets WHERE hotspot=1");
  1820. if (!empty($t_hotspot)) {
  1821. foreach ($t_hotspot as $row) {
  1822. delete_record($db, "auth_rules", "rule='" . $row['subnet'] . "'");
  1823. }
  1824. }
  1825. }
  1826. #---------------------------------------------------------------------------------------------------------------
  1827. function new_user($db, $user_info)
  1828. {
  1829. if (!empty($user_info['mac'])) {
  1830. $user['login'] = mac_dotted($user_info['mac']);
  1831. } else {
  1832. $user['login'] = $user_info['ip'];
  1833. }
  1834. if (!empty($user_info['dhcp_hostname'])) {
  1835. $user['fio'] = $user_info['ip'] . '[' . $user_info['dhcp_hostname'] . ']';
  1836. } else {
  1837. $user['fio'] = $user_info['ip'];
  1838. }
  1839. $login_count = get_count_records($db, "User_list", "(login LIKE '" . $user['login'] . "(%)') OR (login='" . $user['login'] . "')");
  1840. if (!empty($login_count) and $login_count > 0) {
  1841. $login_count++;
  1842. $user['login'] = $user['login'] . "(" . $login_count . ")";
  1843. }
  1844. $user['ou_id'] = $user_info['ou_id'];
  1845. $ou_info = get_record_sql($db, "SELECT * FROM OU WHERE id=" . $user_info['ou_id']);
  1846. if (!empty($ou_info)) {
  1847. $user['enabled'] = $ou_info['enabled'];
  1848. if (empty($user['enabled'])) {
  1849. $user['enabled'] = 0;
  1850. }
  1851. $user['queue_id'] = $ou_info['queue_id'];
  1852. if (empty($user['queue_id'])) {
  1853. $user['queue_id'] = 0;
  1854. }
  1855. $user['filter_group_id'] = $ou_info['filter_group_id'];
  1856. if (empty($user['filter_group_id'])) {
  1857. $user['filter_group_id'] = 0;
  1858. }
  1859. }
  1860. $result = insert_record($db, "User_list", $user);
  1861. $auto_mac_rule = get_option($db, 64);
  1862. if (!empty($result) and $auto_mac_rule and $user_info['mac']) {
  1863. $auth_rule['user_id'] = $result;
  1864. $auth_rule['type'] = 2;
  1865. $auth_rule['rule'] = mac_dotted($user_info['mac']);
  1866. insert_record($db, "auth_rules", $auth_rule);
  1867. }
  1868. return $result;
  1869. }
  1870. function new_auth($db, $ip, $mac, $user_id)
  1871. {
  1872. $ip_aton = ip2long($ip);
  1873. $msg = '';
  1874. if (!empty($mac)) {
  1875. $auth_record = get_record_sql($db, "SELECT * FROM User_auth WHERE ip_int=$ip_aton AND mac='" . mac_dotted($mac) . "' AND deleted=0");
  1876. if (!empty($auth_record)) {
  1877. LOG_WARNING($db, "Pair ip-mac already exists! Skip creating $ip [$mac] auth_id: " . $auth_record["id"]);
  1878. return $auth_record['id'];
  1879. }
  1880. }
  1881. // save traffic detailization
  1882. $save_traf = get_option($db, 23);
  1883. $resurrection_id = NULL;
  1884. // seek old auth with same ip and mac
  1885. $resurrection_id = get_id_record($db, 'User_auth', " deleted=1 AND ip_int=" . $ip_aton . " AND mac='" . $mac . "'");
  1886. if (!empty($resurrection_id)) {
  1887. $msg .= "Recovered auth_id: $resurrection_id with ip: $ip and mac: $mac ";
  1888. $auth['user_id'] = $user_id;
  1889. $auth['deleted'] = 0;
  1890. $auth['save_traf'] = $save_traf * 1;
  1891. update_record($db, "User_auth", "id=$resurrection_id", $auth);
  1892. } else {
  1893. // not found ->create new record
  1894. $msg .= "Create new ip record \r\nip: $ip\r\nmac: $mac\r\n";
  1895. $auth['deleted'] = 0;
  1896. $auth['user_id'] = $user_id;
  1897. $auth['ip'] = $ip;
  1898. $auth['ip_int'] = $ip_aton;
  1899. $auth['mac'] = $mac;
  1900. $auth['save_traf'] = $save_traf * 1;
  1901. $resurrection_id = insert_record($db, "User_auth", $auth);
  1902. }
  1903. //check rules, update filter and state for new record
  1904. if (!empty($resurrection_id)) {
  1905. $auth = apply_auth_rule($db, $auth, $user_id);
  1906. update_record($db, "User_auth", "id=$resurrection_id", $auth);
  1907. if (!is_hotspot($db, $ip) and !empty($msg)) {
  1908. LOG_WARNING($db, $msg);
  1909. }
  1910. if (is_hotspot($db, $ip) and !empty($msg)) {
  1911. LOG_INFO($db, $msg);
  1912. }
  1913. }
  1914. return $resurrection_id;
  1915. }
  1916. function resurrection_auth($db, $ip_record)
  1917. {
  1918. $ip = $ip_record['ip'];
  1919. $mac = $ip_record['mac'];
  1920. $action = $ip_record['type'];
  1921. $dhcp_hostname = $ip_record['hostname'];
  1922. $hotspot_found = $ip_record['hotspot'];
  1923. $ip_aton = ip2long($ip);
  1924. $auth_record = get_record_sql($db, "SELECT * FROM User_auth WHERE ip_int=$ip_aton AND mac='" . $mac . "' AND deleted=0");
  1925. if (!empty($auth_record)) {
  1926. $user_info = get_record_sql($db, "SELECT * FROM User_list WHERE id=" . $auth_record['user_id']);
  1927. LOG_DEBUG($db, "external dhcp user " . $user_info['login'] . " [" . $ip . "] auth_id: " . $auth_record['id']);
  1928. if (isset($dhcp_hostname) and !empty($dhcp_hostname)) {
  1929. $auth['dhcp_hostname'] = $dhcp_hostname;
  1930. }
  1931. $auth['dhcp_action'] = $action;
  1932. $auth['dhcp_time'] = GetNowTimeString();
  1933. if ($action === 'add') {
  1934. $auth['last_found'] = GetNowTimeString();
  1935. }
  1936. update_record($db, "User_auth", "id=" . $auth_record['id'], $auth);
  1937. return $auth_record['id'];
  1938. }
  1939. $ip_subnet = get_ip_subnet($db, $ip);
  1940. if ($ip_subnet['static']) {
  1941. LOG_WARNING($db, "Unknown pair ip+mac in static subnet! ip: $ip mac: [" . mac_dotted($mac) . "]. Skip");
  1942. return;
  1943. }
  1944. $msg = '';
  1945. // search changed mac
  1946. $auth_record = get_record_sql($db, "SELECT * FROM User_auth WHERE ip_int=$ip_aton AND deleted=0");
  1947. if (!empty($auth_record)) {
  1948. if (empty($auth_record['mac'])) {
  1949. $auth['mac'] = mac_dotted($mac);
  1950. $auth['dhcp_action'] = $action;
  1951. $auth['dhcp_time'] = GetNowTimeString();
  1952. if (!empty($dhcp_hostname)) {
  1953. $auth['dhcp_hostname'] = $dhcp_hostname;
  1954. }
  1955. if ($action === 'add') {
  1956. $auth['last_found'] = GetNowTimeString();
  1957. }
  1958. LOG_INFO($db, "for ip: $ip mac not found! Use empty record...");
  1959. update_record($db, "User_auth", "id=" . $auth_record['id'], $auth);
  1960. return $auth_record['id'];
  1961. } else {
  1962. if (!$hotspot_found) {
  1963. 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']);
  1964. }
  1965. run_sql($db, "UPDATE User_auth SET changed=1, deleted=1 WHERE id=" . $auth_record['id']);
  1966. }
  1967. }
  1968. // default id
  1969. $new_user_info = get_new_user_id($db, $ip, $mac, $dhcp_hostname);
  1970. if (!empty($new_user_info['user_id'])) {
  1971. $new_user_id = $new_user_info['user_id'];
  1972. }
  1973. if (empty($new_user_id)) {
  1974. $new_user_id = new_user($db, $new_user_info);
  1975. }
  1976. $resurrection_id = NULL;
  1977. $save_traf = get_option($db, 23);
  1978. $auth_record = get_record_sql($db, "SELECT * FROM User_auth WHERE ip_int=" . $ip_aton . " and mac='" . $mac . "'");
  1979. // seek old auth with same ip and mac
  1980. if (!empty($auth_record)) {
  1981. // found ->Resurrection old record
  1982. $resurrection_id = $auth_record['id'];
  1983. $msg .= "Recovered auth_id: $resurrection_id with ip: $ip and mac: $mac ";
  1984. $auth['dhcp_action'] = $action;
  1985. $auth['user_id'] = $new_user_id;
  1986. $auth['deleted'] = 0;
  1987. $auth['dhcp_time'] = GetNowTimeString();
  1988. $auth['save_traf'] = $save_traf * 1;
  1989. if (!empty($dhcp_hostname)) {
  1990. $auth['dhcp_hostname'] = $dhcp_hostname;
  1991. }
  1992. if ($action === 'add') {
  1993. $auth['last_found'] = GetNowTimeString();
  1994. }
  1995. update_record($db, "User_auth", "id=$resurrection_id", $auth);
  1996. } else {
  1997. // not found ->create new record
  1998. $msg .= "Создаём новый ip-адрес \r\nip: $ip\r\nmac: $mac\r\n";
  1999. $auth['deleted'] = 0;
  2000. $auth['user_id'] = $new_user_id;
  2001. $auth['ip'] = $ip;
  2002. $auth['ip_int'] = $ip_aton;
  2003. $auth['mac'] = $mac;
  2004. $auth['dhcp_action'] = $action;
  2005. $auth['dhcp_time'] = GetNowTimeString();
  2006. $auth['save_traf'] = $save_traf * 1;
  2007. if (!empty($dhcp_hostname)) {
  2008. $auth['dhcp_hostname'] = $dhcp_hostname;
  2009. }
  2010. if ($action == 'add') {
  2011. $auth['last_found'] = GetNowTimeString();
  2012. }
  2013. $resurrection_id = insert_record($db, "User_auth", $auth);
  2014. }
  2015. //check rules, update filter and state for new record
  2016. if (!empty($resurrection_id)) {
  2017. $auth = apply_auth_rule($db, $auth, $new_user_id);
  2018. update_record($db, "User_auth", "id=$resurrection_id", $auth);
  2019. $msg .= "filter: " . $auth['filter_group_id'] . "\r\n queue_id: " . $auth['queue_id'] . "\r\n enabled: " . $auth['enabled'] . "\r\nid: $resurrection_id";
  2020. if (!$hotspot_found and !empty($msg)) {
  2021. LOG_WARNING($db, $msg);
  2022. }
  2023. if ($hotspot_found and !empty($msg)) {
  2024. LOG_INFO($db, $msg);
  2025. }
  2026. }
  2027. return $resurrection_id;
  2028. }
  2029. function get_auth($db, $current_auth)
  2030. {
  2031. if (!isset($current_auth)) {
  2032. return;
  2033. }
  2034. if ($current_auth == 0) {
  2035. return;
  2036. }
  2037. $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");
  2038. list($f_login, $f_ip) = mysqli_fetch_array($t_login);
  2039. $result = $f_login . "[" . $f_ip . "]";
  2040. return $result;
  2041. }
  2042. function get_auth_by_mac($db, $mac)
  2043. {
  2044. if (!isset($mac)) {
  2045. return;
  2046. }
  2047. $mac = mac_dotted($mac);
  2048. $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");
  2049. list($f_id, $f_login, $f_auth_id, $f_ip) = mysqli_fetch_array($t_login);
  2050. if (isset($f_id)) {
  2051. $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>';
  2052. } else {
  2053. $result['auth'] = 'Unknown';
  2054. }
  2055. $result['mac'] = expand_mac($db, $mac);
  2056. return $result;
  2057. }
  2058. function get_auth_mac($db, $current_auth)
  2059. {
  2060. if (!isset($current_auth)) {
  2061. return;
  2062. }
  2063. if ($current_auth == 0) {
  2064. return;
  2065. }
  2066. $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");
  2067. list($f_login, $f_mac) = mysqli_fetch_array($t_login);
  2068. $result = $f_login . "[" . $f_mac . "]";
  2069. return $result;
  2070. }
  2071. function add_auth_rule($db, $rule, $type, $user_id)
  2072. {
  2073. $new['user_id'] = $user_id;
  2074. $new['type'] = $type;
  2075. $new['rule'] = $rule;
  2076. $rule_id = 0;
  2077. $auth_rules = get_record_sql($db, "SELECT * FROM auth_rules WHERE rule='" . $rule . "' AND type=" . $type);
  2078. if (empty($auth_rules)) {
  2079. $rule_id = insert_record($db, "auth_rules", $new);
  2080. LOG_INFO($db, "Create auto rule for user_id: " . $user_id . " rule: " . $rule . " type: " . $type);
  2081. } else {
  2082. if ($auth_rules['user_id'] === $user_id) {
  2083. $rule_id = update_record($db, "auth_rules", "id=" . $auth_rules['id'], $new);
  2084. LOG_INFO($db, "Replaced auto rule for user_id: " . $user_id . " rule: " . $rule . " type: " . $type);
  2085. } else {
  2086. 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']);
  2087. $rule_id = 0;
  2088. }
  2089. }
  2090. return $rule_id;
  2091. }
  2092. function update_auth_rule($db, $rule, $type, $rule_id = 0)
  2093. {
  2094. $new['type'] = $type;
  2095. $new['rule'] = $rule;
  2096. $auth_rules = get_record_sql($db, "SELECT * FROM auth_rules WHERE rule='" . $rule . "' AND type=" . $type . " AND id<>" . $rule_id);
  2097. if (empty($auth_rules)) {
  2098. $rule_id = update_record($db, "auth_rules", "id=" . $rule_id, $new);
  2099. } else {
  2100. LOG_WARNING($db, "Create auto rule id: " . $rule_id . " rule: " . $rule . " type: " . $type . " failed! Already exists at user_id: " . $auth_rules['user_id']);
  2101. $rule_id = 0;
  2102. }
  2103. return $rule_id;
  2104. }
  2105. //action: add,update,del
  2106. function allow_update($table, $action = 'update', $field = '')
  2107. {
  2108. //always allow modification for tables
  2109. if (preg_match('/(variables|dns_cache|worklog|sessions)/i', $table)) {
  2110. return 1;
  2111. }
  2112. if (isset($_SESSION['login'])) {
  2113. $work_user = $_SESSION['login'];
  2114. }
  2115. if (isset($_SESSION['user_id'])) {
  2116. $work_id = $_SESSION['user_id'];
  2117. }
  2118. if (isset($_SESSION['acl'])) {
  2119. $user_level = $_SESSION['acl'];
  2120. }
  2121. if (!isset($work_user) or !isset($work_id) or empty($user_level)) {
  2122. return 0;
  2123. }
  2124. //always allow Administrator
  2125. if ($user_level == 1) {
  2126. return 1;
  2127. }
  2128. //always forbid ViewOnly
  2129. if ($user_level == 3) {
  2130. return 0;
  2131. }
  2132. //allow tables for Operator
  2133. if (preg_match('/(dns_queue|User_auth_alias)/i', $table)) {
  2134. return 1;
  2135. }
  2136. if ($action == 'update') {
  2137. $operator_acl = [
  2138. 'User_auth' => [
  2139. 'comments' => '1',
  2140. 'dns_name' => '1',
  2141. 'firmware' => '1',
  2142. 'link_check' => '1',
  2143. 'nagios' => '1',
  2144. 'nagios_handler' => '1',
  2145. 'Wikiname' => '1'
  2146. ],
  2147. 'User_list' => [
  2148. 'fio' => '1',
  2149. 'login' => '1',
  2150. ],
  2151. ];
  2152. if (!isset($operator_acl[$table])) {
  2153. return 0;
  2154. }
  2155. if (isset($operator_acl[$table]) and empty($field)) {
  2156. return 1;
  2157. }
  2158. if (!isset($operator_acl[$table][$field])) {
  2159. return 0;
  2160. }
  2161. if (empty($operator_acl[$table][$field]) or $operator_acl[$table][$field] == '0') {
  2162. return 0;
  2163. }
  2164. return 1;
  2165. }
  2166. return 0;
  2167. }
  2168. function LOG_INFO($db, $msg, $auth_id = 0)
  2169. {
  2170. if (get_const('log_level') < L_INFO) {
  2171. return;
  2172. }
  2173. write_log($db, $msg, L_INFO, $auth_id);
  2174. }
  2175. function LOG_ERROR($db, $msg, $auth_id = 0)
  2176. {
  2177. if (get_const('log_level') < L_ERROR) {
  2178. return;
  2179. }
  2180. email(L_ERROR, $msg);
  2181. write_log($db, $msg, L_ERROR, $auth_id);
  2182. }
  2183. function LOG_VERBOSE($db, $msg, $auth_id = 0)
  2184. {
  2185. if (get_const('log_level') < L_VERBOSE) {
  2186. return;
  2187. }
  2188. write_log($db, $msg, L_VERBOSE, $auth_id);
  2189. }
  2190. function LOG_WARNING($db, $msg, $auth_id = 0)
  2191. {
  2192. if (get_const('log_level') < L_WARNING) {
  2193. return;
  2194. }
  2195. email(L_WARNING, $msg);
  2196. write_log($db, $msg, L_WARNING, $auth_id);
  2197. }
  2198. function LOG_DEBUG($db, $msg, $auth_id = 0)
  2199. {
  2200. if (!empty(get_const('debug')) and get_const('debug')) {
  2201. write_log($db, $msg, L_DEBUG, $auth_id);
  2202. }
  2203. }
  2204. function truncateByWords($string, $length = 100)
  2205. {
  2206. if (strlen($string) <= $length) {
  2207. return $string;
  2208. }
  2209. $wrapped = wordwrap($string, $length);
  2210. $shortened = substr($wrapped, 0, strpos($wrapped, "\n"));
  2211. return $shortened;
  2212. }
  2213. function get_first_line($msg)
  2214. {
  2215. if (empty($msg)) {
  2216. return;
  2217. }
  2218. preg_match('/(.*)(\n|\<br\>)/', $msg, $matches);
  2219. if (!empty($matches[1])) {
  2220. return $matches[1];
  2221. }
  2222. return truncateByWords($msg, 80);
  2223. }
  2224. function email($level, $msg)
  2225. {
  2226. if (!get_const('send_email')) {
  2227. return;
  2228. }
  2229. if (!($level === L_WARNING or $level === L_ERROR)) {
  2230. return;
  2231. }
  2232. // Generate a boundary string
  2233. $boundary = md5(time());
  2234. // Headers
  2235. $headers = array(
  2236. 'From' => get_const('sender_email'),
  2237. 'Reply-To' => get_const('sender_email'),
  2238. 'X-Mailer' => 'PHP',
  2239. 'MIME-Version' => '1.0',
  2240. 'Content-Type' => 'multipart/mixed; boundary=' . $boundary
  2241. );
  2242. $subject = get_first_line($msg);
  2243. if ($level === L_WARNING) {
  2244. $subject = "WARN: " . $subject . "...";
  2245. $message = 'WARNING! Manager: ' . $_SESSION['login'] . ' <br>' . $msg . '<br>';
  2246. }
  2247. if ($level === L_ERROR) {
  2248. $subject = "ERROR: " . $subject . "...";
  2249. $message = 'ERROR! Manager: ' . $_SESSION['login'] . ' <br>' . $msg . '<br>';
  2250. }
  2251. // HTML part
  2252. $html_message = "<html><body><h1>$message</h1></body></html>";
  2253. $html_encoded = chunk_split(base64_encode($html_message));
  2254. // Create the message body
  2255. $message = "";
  2256. $message .= "--" . $boundary . "\r\n";
  2257. $message .= "Content-Type: text/html; charset=UTF-8\r\n";
  2258. $message .= "Content-Transfer-Encoding: base64\r\n\r\n";
  2259. $message .= $html_encoded . "\r\n";
  2260. $message .= "--" . $boundary . "--";
  2261. // Send email
  2262. mail(get_const('admin_email'), $subject, $message, $headers);
  2263. }
  2264. function write_log($db, $msg, $level, $auth_id = 0)
  2265. {
  2266. $work_user = 'http';
  2267. if (isset($_SESSION['login'])) {
  2268. $work_user = $_SESSION['login'];
  2269. }
  2270. if (!isset($msg)) {
  2271. $msg = 'ERROR! Empty log string!';
  2272. }
  2273. if (!isset($level)) {
  2274. $level = L_INFO;
  2275. }
  2276. $msg = str_replace("'", '', $msg);
  2277. $sSQL = "insert into worklog(customer,message,level,auth_id) values('$work_user','$msg',$level,$auth_id)";
  2278. mysqli_query($db, $sSQL);
  2279. }
  2280. function print_year_select($year_name, $year)
  2281. {
  2282. print "<select id=\"$year_name\" name=\"$year_name\" >\n";
  2283. for ($i = $year - 10; $i <= $year + 10; $i++) {
  2284. print_select_item($i, $i, $year);
  2285. }
  2286. print "</select>\n";
  2287. }
  2288. function print_date_select($dd, $mm, $yy)
  2289. {
  2290. if ($dd >= 1) {
  2291. print "<b>День</b>\n";
  2292. print "<select id=\"day\" name=\"day\" >\n";
  2293. for ($i = 1; $i <= 31; $i++) {
  2294. print_select_item($i, $i, $dd);
  2295. }
  2296. print "</select>\n";
  2297. }
  2298. if ($mm >= 1) {
  2299. print "<b>Месяц</b>\n";
  2300. print "<select id=\"month\" name=\"month\" >\n";
  2301. for ($i = 1; $i <= 12; $i++) {
  2302. $tmp_date = DateTimeImmutable::createFromFormat('U', strtotime("$i/01/$yy"));
  2303. $month_name = $tmp_date->format('F');
  2304. print_select_item($month_name, $i, $mm);
  2305. }
  2306. print "</select>\n";
  2307. }
  2308. print "<b>Год</b>\n";
  2309. print_year_select('year', $yy);
  2310. }
  2311. function print_date_select2($dd, $mm, $yy)
  2312. {
  2313. if ($dd >= 1) {
  2314. print "<b>День</b>\n";
  2315. print "<select id=\"day2\" name=\"day2\" >\n";
  2316. for ($i = 1; $i <= 31; $i++) {
  2317. print_select_item($i, $i, $dd);
  2318. }
  2319. print "</select>\n";
  2320. }
  2321. if ($mm >= 1) {
  2322. print "<b>Месяц</b>\n";
  2323. print "<select id=\"month2\" name=\"month2\" >\n";
  2324. for ($i = 1; $i <= 12; $i++) {
  2325. $tmp_date = DateTimeImmutable::createFromFormat('U', strtotime("$i/01/$yy"));
  2326. $month_name = $tmp_date->format('F');
  2327. print_select_item($month_name, $i, $mm);
  2328. }
  2329. print "</select>\n";
  2330. }
  2331. print "<b>Год</b>\n";
  2332. print_year_select('year2', $yy);
  2333. }
  2334. function is_up($ip)
  2335. {
  2336. if (!isset($ip) or strlen($ip) == 0) {
  2337. return false;
  2338. }
  2339. exec(sprintf('ping -i .3 -c 1 -W 5 %s', escapeshellarg($ip)), $res, $rval);
  2340. return $rval == 0;
  2341. }
  2342. function apply_device_lock($db, $device_id, $iteration = 0)
  2343. {
  2344. $iteration++;
  2345. if ($iteration > 2) {
  2346. return false;
  2347. }
  2348. $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');
  2349. if (empty($dev) or empty($dev['u_locked_timestamp'])) {
  2350. LOG_DEBUG($db, "Snmp discovery lock not found. Set and discovery.");
  2351. return set_lock_discovery($db, $device_id);
  2352. }
  2353. //wait for discovery
  2354. $now = time();
  2355. $wait_time = ($dev['u_locked_timestamp'] + SNMP_LOCK_TIMEOUT) - $now;
  2356. LOG_DEBUG($db, "Check snmp lock for device id: " . $device_id . ". Lock timestamp: " . $dev['u_locked_timestamp'] . ", now: " . $now);
  2357. if ($wait_time < 0) {
  2358. LOG_DEBUG($db, "The lock is already expired. Set new lock.");
  2359. return set_lock_discovery($db, $device_id);
  2360. }
  2361. LOG_VERBOSE($db, "Snmp discovery lock for device id: $device_id found! Need wait " . $wait_time . " sec.");
  2362. sleep($wait_time);
  2363. LOG_VERBOSE($db, "Try set new lock and continue discovery for device id:" . $device_id);
  2364. return apply_device_lock($db, $device_id, $iteration);
  2365. }
  2366. function set_lock_discovery($db, $device_id)
  2367. {
  2368. $new['discovery_locked'] = 1;
  2369. $new['locked_timestamp'] = GetNowTimeString();
  2370. if (update_record($db, 'devices', 'id=' . $device_id, $new)) {
  2371. return true;
  2372. }
  2373. return false;
  2374. }
  2375. function unset_lock_discovery($db, $device_id)
  2376. {
  2377. $new['discovery_locked'] = 0;
  2378. $new['locked_timestamp'] = GetNowTimeString();
  2379. if (update_record($db, 'devices', 'id=' . $device_id, $new)) {
  2380. return true;
  2381. }
  2382. return false;
  2383. }
  2384. function set_port_for_group($db, $group_id, $place_id, $state)
  2385. {
  2386. $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;
  2387. $auth_list = mysqli_query($db, $authSQL);
  2388. LOG_VERBOSE($db, 'Mass port state change started!');
  2389. // get auth list for group
  2390. while (list($a_id, $a_name, $a_ip) = mysqli_fetch_array($auth_list)) {
  2391. // get device and port for auth
  2392. if ($place_id == 0) {
  2393. $place_filter = '';
  2394. } else {
  2395. $place_filter = 'D.building_id=' . $place_id . ' and ';
  2396. }
  2397. $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';
  2398. $dev_info = mysqli_query($db, $devSQL);
  2399. list($d_id, $d_name, $d_vendor_id, $d_model, $d_ip, $d_port, $d_snmp_index) = mysqli_fetch_array($dev_info);
  2400. if (!isset($d_id)) {
  2401. continue;
  2402. }
  2403. $device=get_record($db,'devices',"id=".$d_id);
  2404. $snmp = getSnmpAccess($device);
  2405. if ($state == 1) {
  2406. $mode = 'enable';
  2407. run_sql($db, "Update User_auth set nagios_handler='restart-port' WHERE id=$a_id and nagios_handler='manual-mode'");
  2408. } else {
  2409. $mode = 'disable';
  2410. run_sql($db, "Update User_auth set nagios_handler='manual-mode' WHERE id=$a_id and nagios_handler='restart-port'");
  2411. }
  2412. LOG_INFO($db, "At device $d_name [$d_ip] $mode port $d_port for auth_id: $a_id ($a_ip [$a_name])");
  2413. set_port_state($d_vendor_id, $d_snmp_index, $d_ip, $snmp, $state);
  2414. set_port_poe_state($d_vendor_id, $d_port, $d_snmp_index, $d_ip, $snmp, $state);
  2415. }
  2416. LOG_VERBOSE($db, 'Mass port state change stopped.');
  2417. }
  2418. function get_vendor($db, $mac)
  2419. {
  2420. $mac = mac_dotted($mac);
  2421. $mac5 = substr($mac, 0, 14);
  2422. $mac4 = substr($mac, 0, 11);
  2423. $mac3 = substr($mac, 0, 8);
  2424. $vendor = get_record_sql($db, 'SELECT companyName,companyAddress FROM mac_vendors WHERE oui="' . $mac . '"');
  2425. if (empty($vendor)) {
  2426. $vendor = get_record_sql($db, 'SELECT companyName,companyAddress FROM mac_vendors WHERE oui="' . $mac5 . '"');
  2427. }
  2428. if (empty($vendor)) {
  2429. $vendor = get_record_sql($db, 'SELECT companyName,companyAddress FROM mac_vendors WHERE oui="' . $mac4 . '"');
  2430. }
  2431. if (empty($vendor)) {
  2432. $vendor = get_record_sql($db, 'SELECT companyName,companyAddress FROM mac_vendors WHERE oui="' . $mac3 . '"');
  2433. }
  2434. $result = '';
  2435. if (!empty($vendor)) {
  2436. $result = $vendor['companyName'];
  2437. if (!empty($vendor['companyAddress'])) {
  2438. $result = $vendor['companyAddress'];
  2439. }
  2440. }
  2441. return $result;
  2442. }
  2443. function strHexToBin($number)
  2444. {
  2445. $result = '';
  2446. for ($i = 0; $i < strlen($number); $i++) {
  2447. $conv = base_convert($number[$i], 16, 2);
  2448. $result .= str_pad($conv, 4, '0', STR_PAD_LEFT);
  2449. }
  2450. return $result;
  2451. }
  2452. function dec_to_hex($mac)
  2453. {
  2454. if (!isset($mac)) {
  2455. return;
  2456. }
  2457. $mac_array = explode('.', $mac);
  2458. for ($i = 0; $i < count($mac_array); $i++) {
  2459. $hex_i = dechex($mac_array[$i]);
  2460. if (strlen($hex_i) == 1) {
  2461. $hex_i = "0" . $hex_i;
  2462. }
  2463. $mac_array[$i] = $hex_i;
  2464. }
  2465. $hex_mac = implode(':', $mac_array);
  2466. return $hex_mac;
  2467. }
  2468. function mac_simplify($mac)
  2469. {
  2470. if (!isset($mac)) {
  2471. return;
  2472. }
  2473. $mac = strtolower(trim($mac));
  2474. $mac = preg_replace('/(\.|:|-)/', '', $mac);
  2475. return $mac;
  2476. }
  2477. function mac_dotted($mac)
  2478. {
  2479. if (!isset($mac)) {
  2480. return;
  2481. }
  2482. $mac = mac_simplify($mac);
  2483. $mac = preg_replace('/(\S{2})(\S{2})?(\S{2})?(\S{2})?(\S{2})?(\S{2})?/', '$1:$2:$3:$4:$5:$6', $mac);
  2484. $mac = preg_replace('/\:+$/', '', $mac, 5);
  2485. return $mac;
  2486. }
  2487. function unbind_ports($db, $device_id)
  2488. {
  2489. $target = mysqli_query($db, "SELECT U.target_port_id,U.id FROM device_ports U WHERE U.device_id=$device_id");
  2490. while (list($target_id, $id) = mysqli_fetch_array($target)) {
  2491. run_sql($db, "UPDATE device_ports SET target_port_id=0 WHERE target_port_id=" . $id);
  2492. run_sql($db, "UPDATE device_ports SET target_port_id=0 WHERE id=" . $id);
  2493. }
  2494. }
  2495. function bind_ports($db, $port_id, $target_id)
  2496. {
  2497. $old_target = mysqli_query($db, "SELECT U.target_port_id FROM device_ports U WHERE U.id=$port_id");
  2498. list($old_target_id) = mysqli_fetch_array($old_target);
  2499. // unbind current connection
  2500. $new['target_port_id'] = 0;
  2501. update_record($db, "device_ports", "id='$port_id'", $new);
  2502. if (isset($old_target_id)) {
  2503. update_record($db, "device_ports", "id='$old_target_id'", $new);
  2504. }
  2505. // new link
  2506. if (isset($target_id) and $target_id > 0) {
  2507. $new['target_port_id'] = $target_id;
  2508. update_record($db, "device_ports", "id='$port_id'", $new);
  2509. $new['target_port_id'] = $port_id;
  2510. update_record($db, "device_ports", "id='$target_id'", $new);
  2511. }
  2512. }
  2513. function expand_device_name($db, $name)
  2514. {
  2515. $device_id = get_device_id($db, $name);
  2516. $result = $name;
  2517. if (isset($device_id) and $device_id > 0) {
  2518. $result = '<a href=/admin/devices/editdevice.php?id=' . $device_id . '>' . $name . '</a>';
  2519. }
  2520. return $result;
  2521. }
  2522. function expand_mac($db, $msg)
  2523. {
  2524. if (!isset($msg)) {
  2525. return;
  2526. }
  2527. $mac = mac_dotted($msg);
  2528. $vendor_info = get_vendor($db, $mac);
  2529. if (!empty($vendor_info)) {
  2530. // $result = '<p title="' . $vendor_info . '"><a href=/admin/logs/mac.php?mac=' . $mac . '>' . $mac . '</a></p>';
  2531. $result = '<a href=/admin/logs/mac.php?mac=' . $mac . '><p title="' . $vendor_info . '">'. $mac . '</p></a>';
  2532. } else {
  2533. $result = '<a href=/admin/logs/mac.php?mac=' . $mac . '>' . $mac . '</a>';
  2534. }
  2535. return $result;
  2536. }
  2537. function expand_log_str($db, $msg)
  2538. {
  2539. if (!isset($msg)) {
  2540. return;
  2541. }
  2542. $auth_pattern = '/(auth_id:|auth|auth id:|auth id)\s+(\d+)\s+/i';
  2543. $auth_replace = '<a href=/admin/users/editauth.php?id=${2}>auth_id:${2}</a> ';
  2544. $result = preg_replace($auth_pattern, $auth_replace, $msg);
  2545. $user_pattern = '/(user_id:|user|user id:|user id)\s+(\d+)\s+/i';
  2546. $user_replace = '<a href=/admin/users/edituser.php?id=${2}>user_id:${2}</a> ';
  2547. $result = preg_replace($user_pattern, $user_replace, $result);
  2548. $mac_pattern = '/\s+\[(\w{12})\]\s+/i';
  2549. preg_match($mac_pattern, $result, $matches);
  2550. if (isset($matches[1])) {
  2551. $mac = $matches[1];
  2552. $mac = mac_dotted($mac);
  2553. // $vendor_info = get_vendor($db,$mac);
  2554. // $mac_replace = ' <p title="'.$vendor_info.'"><a href=/admin/logs/mac.php?mac='.$mac.'>'.$mac.'</a></p>';
  2555. $mac_replace = ' <a href=/admin/logs/mac.php?mac=' . $mac . '>' . $mac . '</a> ';
  2556. $result = preg_replace($mac_pattern, $mac_replace, $result);
  2557. }
  2558. $mac_pattern = '/\s+mac:\s+([\w\:]{17})$/i';
  2559. preg_match($mac_pattern, $result, $matches);
  2560. if (isset($matches[1])) {
  2561. $mac = $matches[1];
  2562. $mac = mac_dotted($mac);
  2563. // $vendor_info = get_vendor($db,$mac);
  2564. // $mac_replace = ' mac: <p title="'.$vendor_info.'"><a href=/admin/logs/mac.php?mac='.$mac.'>'.$mac.'</a></p>';
  2565. $mac_replace = ' mac: <a href=/admin/logs/mac.php?mac=' . $mac . '>' . $mac . '</a> ';
  2566. $result = preg_replace($mac_pattern, $mac_replace, $result);
  2567. }
  2568. $device_pattern = '/at device\s+([\w\.\-]+)/i';
  2569. preg_match($device_pattern, $result, $matches);
  2570. if (isset($matches[1])) {
  2571. $device_name = $matches[1];
  2572. $device_id = get_device_id($db, $device_name);
  2573. if (isset($device_id) and $device_id > 0) {
  2574. $device_replace = 'at device <a href=/admin/devices/editdevice.php?id=' . $device_id . '>${1}</a> ';
  2575. $result = preg_replace($device_pattern, $device_replace, $result);
  2576. }
  2577. }
  2578. $device_pattern = '/(device_id:|device id:|device id|device_id)\s+(\d+)\s+/i';
  2579. $device_replace = 'device_id: <a href=/admin/devices/editdevice.php?id=${2}>${2}</a> ';
  2580. $result = preg_replace($device_pattern, $device_replace, $result);
  2581. $ip_pattern = '/\s+ip\:\s+(\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})\s+/i';
  2582. preg_match($ip_pattern, $result, $matches);
  2583. if (isset($matches[1])) {
  2584. $ip = $matches[1];
  2585. $auth_id = get_auth_by_ip($db, $ip);
  2586. if (isset($auth_id) and $auth_id > 0) {
  2587. $auth_replace = ' ip: <a href=/admin/users/editauth.php?id=' . $auth_id . '>${1}</a> ';
  2588. $result = preg_replace($ip_pattern, $auth_replace, $result);
  2589. }
  2590. }
  2591. return $result;
  2592. }
  2593. function get_record_field($db, $table, $field, $filter)
  2594. {
  2595. if (!isset($table)) {
  2596. LOG_ERROR($db, "Search in unknown table! Skip command.");
  2597. return;
  2598. }
  2599. if (!isset($filter)) {
  2600. LOG_ERROR($db, "Search filter is empty! Skip command.");
  2601. return;
  2602. }
  2603. if (!isset($field)) {
  2604. LOG_ERROR($db, "Search field is empty! Skip command.");
  2605. return;
  2606. }
  2607. if (preg_match('/=$/', $filter)) {
  2608. LOG_ERROR($db, "Search record ($table) with illegal filter $filter! Skip command.");
  2609. return;
  2610. }
  2611. $old_sql = "SELECT $field FROM $table WHERE $filter LIMIT 1";
  2612. $old_record = mysqli_query($db, $old_sql) or LOG_ERROR($db, "SQL: $old_sql :" . mysqli_error($db));
  2613. $old = mysqli_fetch_array($old_record, MYSQLI_ASSOC);
  2614. foreach ($old as $key => $value) {
  2615. if (!isset($value) or $value === 'NULL') {
  2616. $value = '';
  2617. }
  2618. $result[$key] = $value;
  2619. }
  2620. return $result[$field];
  2621. }
  2622. function get_record($db, $table, $filter)
  2623. {
  2624. if (!isset($table)) {
  2625. LOG_ERROR($db, "Search in unknown table! Skip command.");
  2626. return;
  2627. }
  2628. if (!isset($filter)) {
  2629. LOG_ERROR($db, "Search filter is empty! Skip command.");
  2630. return;
  2631. }
  2632. if (preg_match('/=$/', $filter)) {
  2633. LOG_ERROR($db, "Search record ($table) with illegal filter $filter! Skip command.");
  2634. return;
  2635. }
  2636. $get_sql = "SELECT * FROM $table WHERE $filter LIMIT 1";
  2637. $get_record = mysqli_query($db, $get_sql);
  2638. if (!$get_record) {
  2639. LOG_ERROR($db, "SQL: $get_sql :" . mysqli_error($db));
  2640. return;
  2641. }
  2642. $fields = [];
  2643. while ($field = mysqli_fetch_field($get_record)) {
  2644. $f_table = $field->table;
  2645. $f_name = $field->name;
  2646. $fields[$f_table][$f_name] = $field;
  2647. }
  2648. $record = mysqli_fetch_array($get_record, MYSQLI_ASSOC);
  2649. $result = NULL;
  2650. if (!empty($record)) {
  2651. foreach ($record as $key => $value) {
  2652. if (!isset($value) or $value === 'NULL' or $value == NULL) {
  2653. if (!empty($key) and !empty($fields[$table]) and !empty($fields[$table][$key])) {
  2654. if (in_array($fields[$table][$key]->type, MYSQL_FIELD_DIGIT)) {
  2655. $value = 0;
  2656. }
  2657. if (in_array($fields[$table][$key]->type, MYSQL_FIELD_STRING)) {
  2658. $value = '';
  2659. }
  2660. }
  2661. }
  2662. if (!empty($key)) {
  2663. $result[$key] = $value;
  2664. }
  2665. }
  2666. }
  2667. return $result;
  2668. }
  2669. function get_records($db, $table, $filter)
  2670. {
  2671. if (!isset($table)) {
  2672. LOG_ERROR($db, "Search in unknown table! Skip command.");
  2673. return;
  2674. }
  2675. if (isset($filter) and preg_match('/=$/', $filter)) {
  2676. LOG_ERROR($db, "Search record ($table) with illegal filter $filter! Skip command.");
  2677. return;
  2678. }
  2679. $s_filter = '';
  2680. if (isset($filter)) {
  2681. $s_filter = 'WHERE ' . $filter;
  2682. }
  2683. $get_sql = "SELECT * FROM $table $s_filter";
  2684. $get_record = mysqli_query($db, $get_sql);
  2685. if (!$get_record) {
  2686. LOG_ERROR($db, "SQL: $get_sql :" . mysqli_error($db));
  2687. return;
  2688. }
  2689. $fields = [];
  2690. while ($field = mysqli_fetch_field($get_record)) {
  2691. $f_table = $field->table;
  2692. $f_name = $field->name;
  2693. $fields[$f_table][$f_name] = $field;
  2694. }
  2695. $result = NULL;
  2696. $index = 0;
  2697. while ($rec = mysqli_fetch_array($get_record, MYSQLI_ASSOC)) {
  2698. foreach ($rec as $key => $value) {
  2699. if (!isset($value) or $value === 'NULL' or $value == NULL) {
  2700. if (!empty($key) and !empty($fields[$table]) and !empty($fields[$table][$key])) {
  2701. if (in_array($fields[$table][$key]->type, MYSQL_FIELD_DIGIT)) {
  2702. $value = 0;
  2703. }
  2704. if (in_array($fields[$table][$key]->type, MYSQL_FIELD_STRING)) {
  2705. $value = '';
  2706. }
  2707. }
  2708. }
  2709. $result[$index][$key] = $value;
  2710. }
  2711. $index++;
  2712. }
  2713. return $result;
  2714. }
  2715. function get_records_sql($db, $sql)
  2716. {
  2717. $result = NULL;
  2718. if (empty($sql)) {
  2719. LOG_ERROR($db, "Empty query! Skip command.");
  2720. return $result;
  2721. }
  2722. $records = mysqli_query($db, $sql);
  2723. if (!$records) {
  2724. LOG_ERROR($db, "SQL: $sql :" . mysqli_error($db));
  2725. return $result;
  2726. }
  2727. $fields = [];
  2728. //we assume that fields with the same name have the same type
  2729. while ($field = mysqli_fetch_field($records)) {
  2730. $f_name = $field->name;
  2731. $fields[$f_name] = $field;
  2732. }
  2733. $index = 0;
  2734. while ($rec = mysqli_fetch_array($records, MYSQLI_ASSOC)) {
  2735. foreach ($rec as $key => $value) {
  2736. if (!isset($value) or $value === 'NULL' or $value == NULL) {
  2737. if (!empty($key) and !empty($fields[$key])) {
  2738. if (in_array($fields[$key]->type, MYSQL_FIELD_DIGIT)) {
  2739. $value = 0;
  2740. }
  2741. if (in_array($fields[$key]->type, MYSQL_FIELD_STRING)) {
  2742. $value = '';
  2743. }
  2744. }
  2745. }
  2746. if (!empty($key)) {
  2747. $result[$index][$key] = $value;
  2748. }
  2749. }
  2750. $index++;
  2751. }
  2752. return $result;
  2753. }
  2754. function get_record_sql($db, $sql)
  2755. {
  2756. $result = NULL;
  2757. if (!isset($sql)) {
  2758. LOG_ERROR($db, "Empty query! Skip command.");
  2759. return $result;
  2760. }
  2761. $record = mysqli_query($db, $sql . " LIMIT 1");
  2762. if (!isset($record)) {
  2763. LOG_ERROR($db, "SQL: $sql LIMIT 1: " . mysqli_error($db));
  2764. return $result;
  2765. }
  2766. $fields = [];
  2767. //we assume that fields with the same name have the same type
  2768. while ($field = mysqli_fetch_field($record)) {
  2769. $f_name = $field->name;
  2770. $fields[$f_name] = $field;
  2771. }
  2772. $rec = mysqli_fetch_array($record, MYSQLI_ASSOC);
  2773. if (!empty($rec)) {
  2774. foreach ($rec as $key => $value) {
  2775. if (!isset($value) or $value === 'NULL' or $value == NULL) {
  2776. if (!empty($key) and !empty($fields[$key])) {
  2777. if (in_array($fields[$key]->type, MYSQL_FIELD_DIGIT)) {
  2778. $value = 0;
  2779. }
  2780. if (in_array($fields[$key]->type, MYSQL_FIELD_STRING)) {
  2781. $value = '';
  2782. }
  2783. }
  2784. }
  2785. if (!empty($key)) {
  2786. $result[$key] = $value;
  2787. }
  2788. }
  2789. }
  2790. return $result;
  2791. }
  2792. function is_auth_bind_changed($db, $id, $ip, $mac)
  2793. {
  2794. $old_sql = "SELECT ip,mac FROM User_auth WHERE id=$id";
  2795. $old_record = get_record_sql($db, $old_sql);
  2796. if (empty($old_record["ip"]) or empty($old_record["mac"])) {
  2797. return 0;
  2798. }
  2799. if ($old_record["ip"] !== $ip or $old_record["mac"] !== $mac) {
  2800. LOG_VERBOSE($db, "Changed ip or mac for auth record!");
  2801. return 1;
  2802. }
  2803. return 0;
  2804. }
  2805. function copy_auth($db, $id, $new_auth)
  2806. {
  2807. $old_record = get_record_sql($db, "SELECT * FROM User_auth WHERE id=$id");
  2808. delete_record($db, "User_auth", "id=" . $id);
  2809. $new_auth["user_id"] = $old_record["user_id"];
  2810. $new_auth["changed"] = 1;
  2811. $changed_time = GetNowTimeString();
  2812. $new_auth["changed_time"] = $changed_time;
  2813. $new_id = insert_record($db, "User_auth", $new_auth);
  2814. LOG_VERBOSE($db, "Old record with id: $id deleted. Created new auth record for new ip+mac id: $new_id!");
  2815. return $new_id;
  2816. }
  2817. function get_dns_name($db, $id)
  2818. {
  2819. $auth_record = get_record_sql($db, "SELECT dns_name FROM User_auth WHERE id=" . $id);
  2820. if (!empty($auth_record) and !empty($auth_record['dns_name'])) {
  2821. return $auth_record['dns_name'];
  2822. }
  2823. return '';
  2824. }
  2825. function update_record($db, $table, $filter, $newvalue)
  2826. {
  2827. if (!isset($table)) {
  2828. LOG_WARNING($db, "Change record for unknown table! Skip command.");
  2829. return;
  2830. }
  2831. if (!isset($filter)) {
  2832. LOG_WARNING($db, "Change record ($table) with empty filter! Skip command.");
  2833. return;
  2834. }
  2835. if (preg_match('/=$/', $filter)) {
  2836. LOG_WARNING($db, "Change record ($table) with illegal filter $filter! Skip command.");
  2837. return;
  2838. }
  2839. if (!isset($newvalue)) {
  2840. LOG_WARNING($db, "Change record ($table [ $filter ]) with empty data! Skip command.");
  2841. return;
  2842. }
  2843. if (!allow_update($table, 'update')) {
  2844. LOG_INFO($db, "Access denied: $table [ $filter ]");
  2845. return 1;
  2846. }
  2847. $old_sql = "SELECT * FROM $table WHERE $filter";
  2848. $old_record = mysqli_query($db, $old_sql) or LOG_ERROR($db, "SQL: $old_sql :" . mysqli_error($db));
  2849. $old = mysqli_fetch_array($old_record, MYSQLI_ASSOC);
  2850. $rec_id = NULL;
  2851. if (!empty($old['id'])) {
  2852. $rec_id = $old['id'];
  2853. }
  2854. $changed_log = '';
  2855. $run_sql = '';
  2856. $network_changed = 0;
  2857. $dhcp_changed = 0;
  2858. $dns_changed = 0;
  2859. $acl_fields = [
  2860. 'ip' => '1',
  2861. 'ip_int' => '1',
  2862. 'enabled' => '1',
  2863. 'dhcp' => '1',
  2864. 'filter_group_id' => '1',
  2865. 'deleted' => '1',
  2866. 'dhcp_acl' => '1',
  2867. 'queue_id' => '1',
  2868. 'mac' => '1',
  2869. 'blocked' => '1',
  2870. ];
  2871. $dhcp_fields = [
  2872. 'ip' => '1',
  2873. 'dhcp' => '1',
  2874. 'deleted' => '1',
  2875. 'mac' => '1',
  2876. ];
  2877. $dns_fields = [
  2878. 'ip' => '1',
  2879. 'dns_name' => '1',
  2880. 'alias' => '1',
  2881. ];
  2882. foreach ($newvalue as $key => $value) {
  2883. if (!allow_update($table, 'update', $key)) {
  2884. continue;
  2885. }
  2886. if (!isset($value)) {
  2887. $value = '';
  2888. }
  2889. $value = trim($value);
  2890. if (strcmp($old[$key], $value) == 0) {
  2891. continue;
  2892. }
  2893. if ($table === "User_auth") {
  2894. if (!empty($acl_fields["$key"])) {
  2895. $network_changed = 1;
  2896. }
  2897. if (!empty($dhcp_fields["$key"])) {
  2898. $dhcp_changed = 1;
  2899. }
  2900. if (!empty($dns_fields["$key"])) {
  2901. $dns_changed = 1;
  2902. }
  2903. }
  2904. if ($table === "User_auth_alias") {
  2905. if (!empty($dns_fields["$key"])) {
  2906. $dns_changed = 1;
  2907. }
  2908. }
  2909. if (!preg_match('/password/i', $key)) {
  2910. $changed_log = $changed_log . " $key => $value (old: $old[$key]),";
  2911. }
  2912. $run_sql = $run_sql . " `" . $key . "`='" . mysqli_real_escape_string($db, $value) . "',";
  2913. }
  2914. if ($table === "User_auth" and $dns_changed) {
  2915. if (!empty($old['dns_name']) and !empty($old['ip'])) {
  2916. $del_dns['name_type'] = 'A';
  2917. $del_dns['name'] = $old['dns_name'];
  2918. $del_dns['value'] = $old['ip'];
  2919. $del_dns['type'] = 'del';
  2920. if (!empty($rec_id)) {
  2921. $del_dns['auth_id'] = $rec_id;
  2922. }
  2923. insert_record($db, 'dns_queue', $del_dns);
  2924. }
  2925. if (!empty($newvalue['dns_name']) and !empty($newvalue['ip'])) {
  2926. $new_dns['name_type'] = 'A';
  2927. $new_dns['name'] = $newvalue['dns_name'];
  2928. $new_dns['value'] = $newvalue['ip'];
  2929. $new_dns['type'] = 'add';
  2930. if (!empty($rec_id)) {
  2931. $new_dns['auth_id'] = $rec_id;
  2932. }
  2933. insert_record($db, 'dns_queue', $new_dns);
  2934. }
  2935. }
  2936. if ($table === "User_auth_alias" and $dns_changed) {
  2937. $auth_id = NULL;
  2938. if ($old['auth_id']) {
  2939. $auth_id = $old['auth_id'];
  2940. }
  2941. if (!empty($old['alias'])) {
  2942. $del_dns['name_type'] = 'CNAME';
  2943. $del_dns['name'] = $old['alias'];
  2944. $del_dns['type'] = 'del';
  2945. if (!empty($auth_id)) {
  2946. $del_dns['auth_id'] = $auth_id;
  2947. $del_dns['value'] = get_dns_name($db, $auth_id);
  2948. }
  2949. insert_record($db, 'dns_queue', $del_dns);
  2950. }
  2951. if (!empty($newvalue['alias'])) {
  2952. $new_dns['name_type'] = 'CNAME';
  2953. $new_dns['name'] = $newvalue['alias'];
  2954. $new_dns['type'] = 'add';
  2955. if (!empty($auth_id)) {
  2956. $new_dns['auth_id'] = $auth_id;
  2957. $new_dns['value'] = get_dns_name($db, $auth_id);
  2958. }
  2959. insert_record($db, 'dns_queue', $new_dns);
  2960. }
  2961. }
  2962. if (empty($run_sql)) {
  2963. return 1;
  2964. }
  2965. if ($network_changed) {
  2966. $run_sql = $run_sql . " `changed`='1',";
  2967. }
  2968. if ($dhcp_changed) {
  2969. $run_sql = $run_sql . " `dhcp_changed`='1',";
  2970. }
  2971. $changed_log = substr_replace($changed_log, "", -1);
  2972. $run_sql = substr_replace($run_sql, "", -1);
  2973. if ($table === 'User_auth') {
  2974. $changed_time = GetNowTimeString();
  2975. $run_sql = $run_sql . ", `changed_time`='" . $changed_time . "'";
  2976. }
  2977. $new_sql = "UPDATE $table SET $run_sql WHERE $filter";
  2978. LOG_DEBUG($db, "Run sql: $new_sql");
  2979. $sql_result = mysqli_query($db, $new_sql) or LOG_ERROR($db, "SQL: $new_sql :" . mysqli_error($db));
  2980. if (!$sql_result) {
  2981. LOG_ERROR($db, "UPDATE Request: $new_sql :" . mysqli_error($db));
  2982. return;
  2983. }
  2984. if ($table !== "sessions") {
  2985. LOG_VERBOSE($db, "Change table $table WHERE $filter set $changed_log");
  2986. }
  2987. return $sql_result;
  2988. }
  2989. function delete_record($db, $table, $filter)
  2990. {
  2991. if (!allow_update($table, 'del')) {
  2992. LOG_WARNING($db, "User does not have write permission");
  2993. return;
  2994. }
  2995. if (!isset($table)) {
  2996. LOG_WARNING($db, "Delete FROM unknown table! Skip command.");
  2997. return;
  2998. }
  2999. if (!isset($filter)) {
  3000. LOG_WARNING($db, "Delete FROM table $table with empty filter! Skip command.");
  3001. return;
  3002. }
  3003. if (preg_match('/=$/', $filter)) {
  3004. LOG_WARNING($db, "Change record ($table) with illegal filter $filter! Skip command.");
  3005. return;
  3006. }
  3007. $old_sql = "SELECT * FROM $table WHERE $filter";
  3008. $old_record = mysqli_query($db, $old_sql) or LOG_ERROR($db, "SQL: $old_sql :" . mysqli_error($db));
  3009. $old = mysqli_fetch_array($old_record, MYSQLI_ASSOC);
  3010. $rec_id = NULL;
  3011. if (!empty($old['id'])) {
  3012. $rec_id = $old['id'];
  3013. }
  3014. $changed_log = 'record: ';
  3015. if (!empty($old)) {
  3016. asort($old, SORT_STRING);
  3017. $old = array_reverse($old, 1);
  3018. foreach ($old as $key => $value) {
  3019. if (empty($value)) {
  3020. continue;
  3021. }
  3022. if (preg_match('/action/', $key)) {
  3023. continue;
  3024. }
  3025. if (preg_match('/status/', $key)) {
  3026. continue;
  3027. }
  3028. if (preg_match('/time/', $key)) {
  3029. continue;
  3030. }
  3031. if (preg_match('/found/', $key)) {
  3032. continue;
  3033. }
  3034. $changed_log = $changed_log . " $key => $value,";
  3035. }
  3036. }
  3037. $delete_it = 1;
  3038. //never delete user ip record or dns alias record
  3039. if ($table === 'User_auth') {
  3040. $delete_it = 0;
  3041. $changed_time = GetNowTimeString();
  3042. $new_sql = "UPDATE $table SET deleted=1, changed=1, `changed_time`='" . $changed_time . "' WHERE $filter";
  3043. LOG_DEBUG($db, "Run sql: $new_sql");
  3044. $sql_result = mysqli_query($db, $new_sql) or LOG_ERROR($db, "SQL: $new_sql :" . mysqli_error($db));
  3045. if (!$sql_result) {
  3046. LOG_ERROR($db, "UPDATE Request (from delete): " . mysqli_error($db));
  3047. return;
  3048. }
  3049. //dns
  3050. if (!empty($old['dns_name']) and !empty($old['ip'])) {
  3051. $del_dns['name_type'] = 'A';
  3052. $del_dns['name'] = $old['dns_name'];
  3053. $del_dns['value'] = $old['ip'];
  3054. $del_dns['type'] = 'del';
  3055. if (!empty($rec_id)) {
  3056. $del_dns['auth_id'] = $rec_id;
  3057. }
  3058. insert_record($db, 'dns_queue', $del_dns);
  3059. }
  3060. }
  3061. if ($table === 'User_auth_alias') {
  3062. //dns
  3063. if (!empty($old['alias'])) {
  3064. $del_dns['name_type'] = 'CNAME';
  3065. $del_dns['name'] = $old['alias'];
  3066. $del_dns['value'] = '';
  3067. $del_dns['type'] = 'del';
  3068. if (!empty($old['auth_id'])) {
  3069. $del_dns['auth_id'] = $old['auth_id'];
  3070. $del_dns['value'] = get_dns_name($db, $old['auth_id']);
  3071. }
  3072. insert_record($db, 'dns_queue', $del_dns);
  3073. }
  3074. }
  3075. if ($delete_it) {
  3076. $new_sql = "DELETE FROM $table WHERE $filter";
  3077. LOG_DEBUG($db, "Run sql: $new_sql");
  3078. $sql_result = mysqli_query($db, $new_sql) or LOG_ERROR($db, "SQL: $new_sql :" . mysqli_error($db));
  3079. if (!$sql_result) {
  3080. LOG_ERROR($db, "DELETE Request: $new_sql : " . mysqli_error($db));
  3081. return;
  3082. }
  3083. }
  3084. if ($table !== "sessions") {
  3085. LOG_VERBOSE($db, "Delete FROM table $table WHERE $filter $changed_log");
  3086. }
  3087. return $changed_log;
  3088. }
  3089. function insert_record($db, $table, $newvalue)
  3090. {
  3091. if (!allow_update($table, 'add')) {
  3092. LOG_WARNING($db, "User does not have write permission");
  3093. return;
  3094. }
  3095. if (!isset($table)) {
  3096. LOG_WARNING($db, "Create record for unknown table! Skip command.");
  3097. return;
  3098. }
  3099. if (empty($newvalue)) {
  3100. LOG_WARNING($db, "Create record ($table) with empty data! Skip command.");
  3101. return;
  3102. }
  3103. $changed_log = '';
  3104. $field_list = '';
  3105. $value_list = '';
  3106. foreach ($newvalue as $key => $value) {
  3107. if (empty($value) and $value != '0') {
  3108. $value = '';
  3109. }
  3110. if (!preg_match('/password/i', $key)) {
  3111. $changed_log = $changed_log . " $key => $value,";
  3112. }
  3113. $field_list = $field_list . "`" . $key . "`,";
  3114. $value = trim($value);
  3115. $value_list = $value_list . "'" . mysqli_real_escape_string($db, $value) . "',";
  3116. }
  3117. if (empty($value_list)) {
  3118. return;
  3119. }
  3120. $changed_log = substr_replace($changed_log, "", -1);
  3121. $field_list = substr_replace($field_list, "", -1);
  3122. $value_list = substr_replace($value_list, "", -1);
  3123. $new_sql = "insert into $table(" . $field_list . ") values(" . $value_list . ")";
  3124. LOG_DEBUG($db, "Run sql: $new_sql");
  3125. $sql_result = mysqli_query($db, $new_sql) or LOG_ERROR($db, "SQL: $new_sql :" . mysqli_error($db));
  3126. if (!$sql_result) {
  3127. LOG_ERROR($db, "INSERT Request:" . mysqli_error($db));
  3128. return;
  3129. }
  3130. $last_id = mysqli_insert_id($db);
  3131. if ($table !== "sessions") {
  3132. LOG_VERBOSE($db, "Create record in table $table: $changed_log with id: $last_id");
  3133. }
  3134. if ($table === 'User_auth') {
  3135. run_sql($db, "UPDATE User_auth SET changed=1, dhcp_changed=1 WHERE id=" . $last_id);
  3136. }
  3137. if ($table === 'User_auth_alias') {
  3138. //dns
  3139. if (!empty($newvalue['alias'])) {
  3140. $add_dns['name_type'] = 'CNAME';
  3141. $add_dns['name'] = $newvalue['alias'];
  3142. $add_dns['value'] = get_dns_name($db, $newvalue['auth_id']);
  3143. $add_dns['type'] = 'add';
  3144. $add_dns['auth_id'] = $newvalue['auth_id'];
  3145. insert_record($db, 'dns_queue', $add_dns);
  3146. }
  3147. }
  3148. if ($table === 'User_auth') {
  3149. //dns
  3150. if (!empty($newvalue['dns_name']) and !empty($newvalue['ip'])) {
  3151. $add_dns['name_type'] = 'A';
  3152. $add_dns['name'] = $newvalue['dns_name'];
  3153. $add_dns['value'] = $newvalue['ip'];
  3154. $add_dns['type'] = 'add';
  3155. $add_dns['auth_id'] = $last_id;
  3156. insert_record($db, 'dns_queue', $add_dns);
  3157. }
  3158. }
  3159. return $last_id;
  3160. }
  3161. function dump_record($db, $table, $filter)
  3162. {
  3163. $result = '';
  3164. $old = get_record($db, $table, $filter);
  3165. if (empty($old)) {
  3166. return $result;
  3167. }
  3168. $result = 'record: ' . get_rec_str($old);
  3169. return $result;
  3170. }
  3171. function get_rec_str($array)
  3172. {
  3173. $result = '';
  3174. foreach ($array as $key => $value) {
  3175. $result .= "[" . $key . "]=" . $value . ", ";
  3176. }
  3177. $result = preg_replace('/,\s+$/', '', $result);
  3178. return $result;
  3179. }
  3180. function get_diff_rec($db, $table, $filter, $newvalue, $only_changed)
  3181. {
  3182. if (!isset($table)) {
  3183. return;
  3184. }
  3185. if (!isset($filter)) {
  3186. return;
  3187. }
  3188. if (!isset($newvalue)) {
  3189. return;
  3190. }
  3191. if (!isset($only_changed)) {
  3192. $only_changed = 0;
  3193. }
  3194. $old_sql = "SELECT * FROM $table WHERE $filter";
  3195. $old_record = mysqli_query($db, $old_sql) or LOG_ERROR($db, "SQL: $old_sql :" . mysqli_error($db));
  3196. $old = mysqli_fetch_array($old_record, MYSQLI_ASSOC);
  3197. $changed_log = "\r\n";
  3198. foreach ($newvalue as $key => $value) {
  3199. if (strcmp($old[$key], $value) !== 0) {
  3200. $changed_log = $changed_log . " $key => cur: $value old: $old[$key],\r\n";
  3201. }
  3202. }
  3203. $old_record = '';
  3204. if (!$only_changed) {
  3205. $old_record = "\r\n Has not changed:\r\n";
  3206. foreach ($old as $key => $value) {
  3207. if (!empty($newvalue[$key])) {
  3208. $old_record = $old_record . " $key = $value,\r\n";
  3209. }
  3210. }
  3211. $old_record = substr_replace($old_record, "", -3);
  3212. }
  3213. // print $changed_log;
  3214. return $changed_log . $old_record;
  3215. }
  3216. function get_cacti_graph($host_ip, $port_index)
  3217. {
  3218. if (empty(get_const('cacti_url'))) {
  3219. return;
  3220. }
  3221. if (CACTI_DB_HOST == null or CACTI_DB_USER == null or CACTI_DB_PASS == null or CACTI_DB_NAME == null) {
  3222. return;
  3223. }
  3224. if (empty(CACTI_DB_HOST) or empty(CACTI_DB_USER) or empty(CACTI_DB_PASS) or empty(CACTI_DB_NAME)) {
  3225. return;
  3226. }
  3227. $cacti_db_link = new_connection(CACTI_DB_HOST, CACTI_DB_USER, CACTI_DB_PASS, CACTI_DB_NAME);
  3228. if (!$cacti_db_link) {
  3229. return FALSE;
  3230. }
  3231. $host_sql = 'SELECT * FROM host WHERE hostname="' . $host_ip . '"';
  3232. $cacti_host = get_record_sql($cacti_db_link, $host_sql);
  3233. $host_id = $cacti_host["id"];
  3234. if (empty($host_id)) {
  3235. return;
  3236. }
  3237. $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';
  3238. $cacti_graph = get_record_sql($cacti_db_link, $graph_sql);
  3239. $graph_id = $cacti_graph["id"];
  3240. if (empty($graph_id)) {
  3241. return;
  3242. }
  3243. $result = get_const('cacti_url') . "/graph_image.php?local_graph_id=" . $graph_id;
  3244. return $result;
  3245. }
  3246. function print_select_item($description, $value, $current)
  3247. {
  3248. if ((string)$value === (string)$current) {
  3249. print "<option value='" . $value . "' selected>$description</option>";
  3250. } else {
  3251. print "<option value='" . $value . "'>$description</option>";
  3252. }
  3253. }
  3254. function print_select_simple($description, $value)
  3255. {
  3256. print "<option value=$value>$description</option>";
  3257. }
  3258. function print_select_item_ext($description, $value, $current, $disabled)
  3259. {
  3260. if ((string)$value === (string)$current) {
  3261. print "<option value=$value selected>$description</option>";
  3262. } else {
  3263. if (!$disabled) {
  3264. print "<option value=$value>$description</option>";
  3265. } else {
  3266. print "<option disabled value=$value>$description</option>";
  3267. }
  3268. }
  3269. }
  3270. function print_row_at_pages($name, $value)
  3271. {
  3272. print "<select id='" . $name . "' name='" . $name . "'>\n";
  3273. print_select_item(WEB_select_item_more, pow(10, 10), $value);
  3274. print_select_item('25', 25, $value);
  3275. print_select_item('50', 50, $value);
  3276. print_select_item('100', 100, $value);
  3277. print_select_item('200', 200, $value);
  3278. print_select_item('500', 500, $value);
  3279. print_select_item('1000', 1000, $value);
  3280. print_select_item('2000', 2000, $value);
  3281. print "</select>\n";
  3282. }
  3283. function print_navigation($url, $page, $displayed, $count_records, $total)
  3284. {
  3285. if ($total <= 1) {
  3286. print "<div align=left class=records >";
  3287. print "| Total records: $count_records";
  3288. print "</div>";
  3289. return;
  3290. }
  3291. $v_char = "?";
  3292. if (preg_match('/\.php\?/', $url)) {
  3293. $v_char = "&";
  3294. }
  3295. //две назад
  3296. print "<div align=left class=records >";
  3297. if (($page - 2) > 0) :
  3298. $pagetwoleft = "<a class='first_page_link' href=" . $url . $v_char . "page=" . ($page - 2) . ">" . ($page - 2) . "</a> ";
  3299. else :
  3300. $pagetwoleft = null;
  3301. endif;
  3302. //одна назад
  3303. if (($page - 1) > 0) :
  3304. $pageoneleft = "<a class='first_page_link' href=" . $url . $v_char . "page=" . ($page - 1) . ">" . ($page - 1) . "</a> ";
  3305. $pagetemp = ($page - 1);
  3306. else :
  3307. $pageoneleft = null;
  3308. $pagetemp = null;
  3309. endif;
  3310. //две вперед
  3311. if (($page + 2) <= $total) :
  3312. $pagetworight = " <a class='first_page_link' href=" . $url . $v_char . "page=" . ($page + 2) . ">" . ($page + 2) . "</a>";
  3313. else :
  3314. $pagetworight = null;
  3315. endif;
  3316. //одна вперед
  3317. if (($page + 1) <= $total) :
  3318. $pageoneright = " <a class='first_page_link' href=" . $url . $v_char . "page=" . ($page + 1) . ">" . ($page + 1) . "</a>";
  3319. $pagetemp2 = ($page + 1);
  3320. else :
  3321. $pageoneright = null;
  3322. $pagetemp2 = null;
  3323. endif;
  3324. // в начало
  3325. if ($page != 1 && $pagetemp != 1 && $pagetemp != 2) :
  3326. $pagerevp = "<a href=" . $url . $v_char . "page=1 class='first_page_link' title='В начало'><<</a> ";
  3327. else :
  3328. $pagerevp = null;
  3329. endif;
  3330. //в конец (последняя)
  3331. if ($page != $total && $pagetemp2 != ($total - 1) && $pagetemp2 != $total) :
  3332. $nextp = " ... <a href=" . $url . $v_char . "page=" . $total . " class='first_page_link'>$total</a>";
  3333. else :
  3334. $nextp = null;
  3335. endif;
  3336. print $pagerevp . $pagetwoleft . $pageoneleft . '<span class="num_page_not_link"><b>' . $page . '</b></span>' . $pageoneright . $pagetworight . $nextp;
  3337. print " | Total records: $count_records";
  3338. print "</div>";
  3339. }
  3340. function get_option($db, $option_id)
  3341. {
  3342. $option = get_record($db, "config", "option_id=" . $option_id);
  3343. if (empty($option) or empty($option['value'])) {
  3344. $default = get_record($db, "config_options", "id=$option_id");
  3345. return $default['default_value'];
  3346. }
  3347. return $option['value'];
  3348. }
  3349. function is_option($db, $option_id)
  3350. {
  3351. $option = get_record($db, "config", "option_id=" . $option_id);
  3352. if (empty($option) or empty($option['value'])) {
  3353. return;
  3354. }
  3355. return 1;
  3356. }
  3357. function set_option($db, $option_id, $value)
  3358. {
  3359. $option['value'] = $value;
  3360. update_record($db, 'config', "option_id=$option_id", $option);
  3361. }
  3362. function is_subnet_aton($subnet, $ip)
  3363. {
  3364. if (!isset($subnet)) {
  3365. return 0;
  3366. }
  3367. if (!isset($ip)) {
  3368. return 0;
  3369. }
  3370. $range = cidrToRange($subnet);
  3371. if ($ip >= ip2long($range[0]) and $ip <= ip2long($range[1])) {
  3372. return 1;
  3373. }
  3374. return 0;
  3375. }
  3376. function get_new_user_id($db, $ip, $mac, $hostname)
  3377. {
  3378. $result['ip'] = $ip;
  3379. $result['mac'] = mac_dotted($mac);
  3380. $result['hostname'] = $hostname;
  3381. $result['user_id'] = NULL;
  3382. $result['ou_id'] = NULL;
  3383. $ip_aton = ip2long($ip);
  3384. //personal user rules
  3385. //ip
  3386. if (!empty($ip)) {
  3387. $t_rules = get_records_sql($db, "SELECT * FROM auth_rules WHERE type=1 and LENGTH(rule)>0 AND user_id IS NOT NULL");
  3388. foreach ($t_rules as $row) {
  3389. if (!empty($row['rule']) and is_subnet_aton($row['rule'], $ip_aton)) {
  3390. $result['user_id'] = $row['user_id'];
  3391. }
  3392. }
  3393. }
  3394. //mac
  3395. if (!empty($mac)) {
  3396. $mac_rules = get_records_sql($db, "SELECT * FROM auth_rules WHERE type=2 AND LENGTH(rule)>0 AND user_id IS NOT NULL");
  3397. foreach ($mac_rules as $row) {
  3398. $pattern = '/' . mac_simplify($row['rule']) . '/';
  3399. if (!empty($row['rule']) and preg_match($pattern, mac_simplify($mac))) {
  3400. $result['user_id'] = $row['user_id'];
  3401. }
  3402. }
  3403. }
  3404. //hostname
  3405. if (!empty($hostname)) {
  3406. $mac_rules = get_records_sql($db, "SELECT * FROM auth_rules WHERE type=3 AND LENGTH(rule)>0 AND user_id IS NOT NULL");
  3407. foreach ($mac_rules as $row) {
  3408. if (!empty($row['rule']) and preg_match($row['rule'], $hostname)) {
  3409. $result['user_id'] = $row['user_id'];
  3410. }
  3411. }
  3412. }
  3413. if (!empty($result['user_id'])) {
  3414. return $result;
  3415. }
  3416. //ou rules
  3417. //ip
  3418. if (!empty($ip)) {
  3419. if (is_hotspot($db, $ip)) {
  3420. $result['ou_id'] = get_const('default_hotspot_ou_id');
  3421. }
  3422. $t_rules = get_records_sql($db, "SELECT * FROM auth_rules WHERE type=1 and LENGTH(rule)>0 AND ou_id IS NOT NULL");
  3423. foreach ($t_rules as $row) {
  3424. if (!empty($row['rule']) and is_subnet_aton($row['rule'], $ip_aton)) {
  3425. $result['ou_id'] = $row['ou_id'];
  3426. }
  3427. }
  3428. }
  3429. //mac
  3430. if (!empty($mac)) {
  3431. $mac_rules = get_records_sql($db, "SELECT * FROM auth_rules WHERE type=2 AND LENGTH(rule)>0 AND ou_id IS NOT NULL");
  3432. foreach ($mac_rules as $row) {
  3433. $pattern = '/' . mac_simplify($row['rule']) . '/';
  3434. if (!empty($row['rule']) and preg_match($pattern, mac_simplify($mac))) {
  3435. $result['ou_id'] = $row['ou_id'];
  3436. }
  3437. }
  3438. }
  3439. //hostname
  3440. if (!empty($hostname)) {
  3441. $mac_rules = get_records_sql($db, "SELECT * FROM auth_rules WHERE type=3 AND LENGTH(rule)>0 AND ou_id IS NOT NULL");
  3442. foreach ($mac_rules as $row) {
  3443. if (!empty($row['rule']) and preg_match($row['rule'], $hostname)) {
  3444. $result['ou_id'] = $row['ou_id'];
  3445. }
  3446. }
  3447. }
  3448. if (empty($result['ou_id'])) {
  3449. $result['ou_id'] = get_const('default_user_ou_id');
  3450. }
  3451. return $result;
  3452. }
  3453. function get_subnet_range($db, $subnet_id)
  3454. {
  3455. if (empty($subnet_id)) {
  3456. return;
  3457. }
  3458. $t_option = get_record_sql($db, "SELECT ip_int_start,ip_int_stop FROM `subnets` WHERE id=$subnet_id");
  3459. if (!isset($t_option['ip_int_start'])) {
  3460. $t_option['ip_int_start'] = 0;
  3461. }
  3462. if (!isset($t_option['ip_int_stop'])) {
  3463. $t_option['ip_int_stop'] = 0;
  3464. }
  3465. $subnet['start'] = $t_option['ip_int_start'];
  3466. $subnet['stop'] = $t_option['ip_int_stop'];
  3467. return $subnet;
  3468. }
  3469. function int_between($value, $start, $end)
  3470. {
  3471. return in_array($value, range($start, $end));
  3472. }
  3473. function is_gray_network($ip)
  3474. {
  3475. if (empty($ip)) {
  3476. return 0;
  3477. }
  3478. $ip_aton = ip2long($ip);
  3479. $gray_nets = array('10.0.0.0/8', '192.168.0.0/16', '172.16.0.0/12', '100.64.0.0/10');
  3480. foreach ($gray_nets as &$net) {
  3481. $net_cidr = cidrToRange($net);
  3482. if (int_between($ip_aton, ip2long($net_cidr[0]), ip2long($net_cidr[1]))) {
  3483. return $net;
  3484. }
  3485. }
  3486. return 0;
  3487. }
  3488. function is_hotspot($db, $ip)
  3489. {
  3490. if (!isset($ip)) {
  3491. return 0;
  3492. }
  3493. LOG_DEBUG($db, "Check hotspot network for ip: $ip");
  3494. $ip_aton = ip2long($ip);
  3495. $t_option = mysqli_query($db, "SELECT subnet,ip_int_start,ip_int_stop FROM `subnets` WHERE hotspot=1");
  3496. while (list($f_net, $f_start, $f_stop) = mysqli_fetch_array($t_option)) {
  3497. if ($ip_aton >= $f_start and $ip_aton <= $f_stop) {
  3498. LOG_DEBUG($db, "ip: $ip [$ip_aton] found in network $f_net: [" . $f_start . ".." . $f_stop . "]");
  3499. return 1;
  3500. }
  3501. }
  3502. LOG_DEBUG($db, "ip $ip not found in hotspot network!");
  3503. return 0;
  3504. }
  3505. function is_office($db, $ip)
  3506. {
  3507. if (!isset($ip)) {
  3508. return 0;
  3509. }
  3510. LOG_DEBUG($db, "Check office network for ip: $ip");
  3511. $ip_aton = ip2long($ip);
  3512. $t_option = mysqli_query($db, "SELECT subnet,ip_int_start,ip_int_stop FROM `subnets` WHERE office=1");
  3513. while (list($f_net, $f_start, $f_stop) = mysqli_fetch_array($t_option)) {
  3514. if ($ip_aton >= $f_start and $ip_aton <= $f_stop) {
  3515. LOG_DEBUG($db, "ip: $ip [$ip_aton] found in office $f_net: [" . $f_start . ".." . $f_stop . "]");
  3516. return 1;
  3517. }
  3518. }
  3519. LOG_DEBUG($db, "ip $ip not found in office network!");
  3520. return 0;
  3521. }
  3522. function is_our_network($db, $ip)
  3523. {
  3524. if (!isset($ip)) {
  3525. return 0;
  3526. }
  3527. if (is_hotspot($db, $ip)) {
  3528. return 1;
  3529. }
  3530. if (is_office($db, $ip)) {
  3531. return 1;
  3532. }
  3533. return 0;
  3534. }
  3535. function get_const($const_name)
  3536. {
  3537. global $config;
  3538. if (isset($config[$const_name])) {
  3539. return $config[$const_name];
  3540. }
  3541. return NULL;
  3542. }
  3543. function get_eye_version($db)
  3544. {
  3545. $v_table = get_record_sql($db, "SELECT version FROM version");
  3546. if (!empty($v_table)) {
  3547. return $v_table['version'];
  3548. }
  3549. return NULL;
  3550. }
  3551. $config["org_name"] = get_option($db_link, 32);
  3552. $config["version"] = get_eye_version($db_link);
  3553. $config["KB"] = get_option($db_link, 1);
  3554. if ($config["KB"] == 0) {
  3555. $config["KB"] = 1000;
  3556. }
  3557. if ($config["KB"] == 1) {
  3558. $config["KB"] = 1024;
  3559. }
  3560. $config["debug"] = get_option($db_link, 34);
  3561. $config["log_level"] = get_option($db_link, 53);
  3562. if ($config["debug"]) {
  3563. $config["log_level"] = 255;
  3564. }
  3565. $config["send_email"] = get_option($db_link, 51);
  3566. $config["admin_email"] = get_option($db_link, 21);
  3567. $config["sender_email"] = get_option($db_link, 52);
  3568. $config["snmp_default_version"] = get_option($db_link, 9);
  3569. $config["snmp_default_community"] = get_option($db_link, 11);
  3570. $config["auto_mac_rule"] = get_option($db_link, 64);
  3571. $config["cacti_url"] = rtrim(get_option($db_link, 58), '/');
  3572. if (preg_match('/127.0.0.1/', $config["cacti_url"])) {
  3573. $config["cacti_url"] = NULL;
  3574. }
  3575. $config["nagios_url"] = rtrim(get_option($db_link, 57), '/') . '/cgi-bin/';
  3576. if (preg_match('/127.0.0.1/', $config["nagios_url"])) {
  3577. $config["nagios_url"] = NULL;
  3578. }
  3579. $config["torrus_url"] = rtrim(get_option($db_link, 59), '/') . '?nodeid=if//HOST_IP//IF_NAME////inoutbps';
  3580. if (preg_match('/127.0.0.1/', $config["torrus_url"])) {
  3581. $config["torrus_url"] = NULL;
  3582. }
  3583. $config["dns_server"] = get_option($db_link, 3);
  3584. $config["dns_server_type"] = get_option($db_link, 70);
  3585. $ou = get_record_sql($db_link, "SELECT id FROM OU WHERE default_users = 1");
  3586. if (empty($ou)) {
  3587. $config["default_user_ou_id"] = 0;
  3588. } else {
  3589. $config["default_user_ou_id"] = $ou['id'];
  3590. }
  3591. $ou = get_record_sql($db_link, "SELECT id FROM OU WHERE default_hotspot=1");
  3592. if (empty($ou)) {
  3593. $config["default_hotspot_ou_id"] = $config["default_user_ou_id"];
  3594. } else {
  3595. $config["default_hotspot_ou_id"] = $ou['id'];
  3596. }
  3597. $config["init"] = 1;
  3598. clean_dns_cache($db_link);
  3599. //clean_unreferensed_rules($db_link);