common.php 123 KB

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