common.php 105 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201
  1. <?php
  2. if (!defined("CONFIG")) {
  3. die("Not defined");
  4. }
  5. require_once($_SERVER['DOCUMENT_ROOT'] . "/inc/consts.php");
  6. require_once($_SERVER['DOCUMENT_ROOT'] . "/inc/snmp.php");
  7. //ValidIpAddressRegex = "^(([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])\.){3}([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])$";
  8. //ValidHostnameRegex = "^(([a-zA-Z0-9]|[a-zA-Z0-9][a-zA-Z0-9\-]*[a-zA-Z0-9])\.)*([A-Za-z0-9]|[A-Za-z0-9][A-Za-z0-9\-]*[A-Za-z0-9])$";
  9. //$ValidMacAddressRegex="^([0-9A-Fa-f]{2}[:-]){5}[0-9A-Fa-f]{2}|([0-9a-fA-F]{4}[\\.-][0-9a-fA-F]{4}[\\.-][0-9a-fA-F]{4})|[0-9A-Fa-f]{12}$";
  10. $config["init"] = 0;
  11. // #### vendor id
  12. // 1, Unknown
  13. // 2, 'Eltex'
  14. // 3, 'Huawei'
  15. // 4, 'Zyxel'
  16. // 5, 'Raisecom'
  17. // 6, 'SNR'
  18. // 7, 'Dlink'
  19. // 8, 'Allied Telesis'
  20. // 9, 'Mikrotik'
  21. // 10, 'NetGear'
  22. // 11, 'Ubiquiti'
  23. // 15, 'HP'
  24. // 16, 'Cisco'
  25. // 17, 'Maipu'
  26. // 18, 'Asus'
  27. function randomPassword($length = 8)
  28. {
  29. $alphabet = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890';
  30. $pass = array(); //remember to declare $pass as an array
  31. $alphaLength = strlen($alphabet) - 1; //put the length -1 in cache
  32. for ($i = 0; $i < $length; $i++) {
  33. $n = rand(0, $alphaLength);
  34. $pass[] = $alphabet[$n];
  35. }
  36. return implode($pass); //turn the array into a string
  37. }
  38. function mb_ucfirst($str)
  39. {
  40. $str = mb_strtolower($str);
  41. $fc = mb_strtoupper(mb_substr($str, 0, 1));
  42. return $fc . mb_substr($str, 1);
  43. }
  44. function get_user_ip()
  45. {
  46. $auth_ip = getenv("HTTP_CLIENT_IP");
  47. if (empty($auth_ip)) {
  48. $auth_ip = getenv("HTTP_X_FORWARDED_FOR");
  49. if (empty($auth_ip)) {
  50. $auth_ip = getenv("REMOTE_ADDR");
  51. if (empty($auth_ip)) {
  52. $auth_ip = $_SERVER['REMOTE_ADDR'];
  53. }
  54. }
  55. }
  56. return $auth_ip;
  57. }
  58. function fbytes($traff)
  59. {
  60. $units_IEC = array(
  61. "",
  62. "Ki",
  63. "Mi",
  64. "Gi",
  65. "Ti"
  66. );
  67. $units_metric = array(
  68. "",
  69. "k",
  70. "M",
  71. "G",
  72. "T"
  73. );
  74. if (!empty($traff) and $traff > 0) {
  75. $KB = get_const('KB');
  76. if ($KB) {
  77. $KB = 1024;
  78. } else {
  79. $KB = 1000;
  80. }
  81. //IEC
  82. if ($KB == 1024) {
  83. $index = min(((int) log($traff, $KB)), count($units_IEC) - 1);
  84. $result = round($traff / pow($KB, $index), 3) . ' ' . $units_IEC[$index] . 'B';
  85. } else {
  86. $index = min(((int) log($traff, $KB)), count($units_metric) - 1);
  87. $result = round($traff / pow($KB, $index), 3) . ' ' . $units_metric[$index] . 'B';
  88. }
  89. } else {
  90. $result = '0 B';
  91. }
  92. return $result;
  93. }
  94. function fpkts($packets)
  95. {
  96. $units = array(
  97. "",
  98. "k",
  99. "M",
  100. "G",
  101. "T"
  102. );
  103. if (!empty($packets) and $packets > 0) {
  104. $index = min(((int) log($packets, 1000)), count($units) - 1);
  105. $result = round($packets / pow(1000, $index), 3) . ' ' . $units[$index] . 'pkt/s';
  106. } else {
  107. $result = '0 pkt/s';
  108. }
  109. return $result;
  110. }
  111. function checkValidIp($cidr)
  112. {
  113. // Checks for a valid IP address or optionally a cidr notation range
  114. // e.g. 1.2.3.4 or 1.2.3.0/24
  115. // if(!preg_match("^[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}(/[0-9]{1,2}){0,1}$", $cidr)) {
  116. $ip_pattern = '/^(?:25[0-5]|2[0-4]\d|1\d\d|[1-9]\d|\d)(?:[.](?:25[0-5]|2[0-4]\d|1\d\d|[1-9]\d|\d)){3}/';
  117. if (!preg_match($ip_pattern, $cidr)) {
  118. $return = FALSE;
  119. } else {
  120. $return = TRUE;
  121. }
  122. if ($return == TRUE) {
  123. $parts = explode("/", $cidr);
  124. $ip = $parts[0];
  125. if (empty($parts[1])) {
  126. $parts[1] = "32";
  127. }
  128. $netmask = $parts[1];
  129. $octets = explode(".", $ip);
  130. foreach ($octets as $octet) {
  131. if ($octet > 255) {
  132. $return = FALSE;
  133. }
  134. }
  135. if (($netmask != "") && ($netmask > 32)) {
  136. $return = FALSE;
  137. }
  138. }
  139. return $return;
  140. }
  141. function checkValidMac($mac)
  142. {
  143. $ValidMacAddressRegex = "/^([0-9A-Fa-f]{2}[:-]){5}[0-9A-Fa-f]{2}|([0-9a-fA-F]{4}[\\.-][0-9a-fA-F]{4}[\\.-][0-9a-fA-F]{4})|[0-9A-Fa-f]{12}$/";
  144. if (!preg_match($ValidMacAddressRegex, $mac)) {
  145. $return = FALSE;
  146. } else {
  147. $return = TRUE;
  148. }
  149. return $return;
  150. }
  151. function checkValidHostname($dnsname)
  152. {
  153. if (empty($dnsname)) {
  154. return TRUE;
  155. }
  156. $host_pattern = "/^(([a-zA-Z0-9]|[a-zA-Z0-9][a-zA-Z0-9\-]*[a-zA-Z0-9])\.)*([A-Za-z0-9]|[A-Za-z0-9][A-Za-z0-9\-]*[A-Za-z0-9])$/";
  157. if (!preg_match($host_pattern, $dnsname)) {
  158. $result = FALSE;
  159. } else {
  160. $result = TRUE;
  161. }
  162. return $result;
  163. }
  164. function searchHostname($db, $id, $hostname)
  165. {
  166. if (empty($hostname)) {
  167. return NULL;
  168. }
  169. $result = '';
  170. $domain_zone = get_option($db, 33);
  171. $a_search_filter = 'SELECT * FROM User_auth WHERE deleted=0 and id !="' . $id . '" and (dns_name ="' . mysqli_real_escape_string($db, $hostname) . '" or dns_name ="' . mysqli_real_escape_string($db, $hostname . '.' . $domain_zone) . '")';
  172. // LOG_DEBUG($db, "A search-filter: ".$a_search_filter);
  173. $a_records = get_records_sql($db, $a_search_filter);
  174. foreach ($a_records as $a_rec) {
  175. $result .= 'auth_id:' . $a_rec['id'] . ' ip: ' . $a_rec['ip'] . '; ';
  176. }
  177. if (!empty($result)) {
  178. $result = 'A-record: ' . $result;
  179. }
  180. $result_cname = '';
  181. $cname_search_filter = 'SELECT * FROM User_auth_alias WHERE auth_id !="' . $id . '" and (alias ="' . mysqli_real_escape_string($db, $hostname) . '" or alias ="' . mysqli_real_escape_string($db, $hostname . '.' . $domain_zone) . '")';
  182. // LOG_DEBUG($db, "CNAME search-filter: ".$cname_search_filter);
  183. $a_records = get_records_sql($db, $cname_search_filter);
  184. foreach ($a_records as $a_rec) {
  185. $result_cname .= 'auth_id:' . $a_rec['auth_id'] . ';';
  186. }
  187. if (!empty($result_cname)) {
  188. $result_cname = 'CNAME-record: ' . $result_cname;
  189. }
  190. $result = trim($result . ' ' . $result_cname);
  191. return $result;
  192. }
  193. function checkUniqHostname($db, $id, $hostname)
  194. {
  195. if (empty($hostname)) {
  196. return TRUE;
  197. }
  198. $domain_zone = get_option($db, 33);
  199. $check_A_filter = 'deleted=0 and id !="' . $id . '" and (dns_name ="' . mysqli_real_escape_string($db, $hostname) . '" or dns_name ="' . mysqli_real_escape_string($db, $hostname . '.' . $domain_zone) . '")';
  200. // LOG_DEBUG($db, "CNAME filter: ".$check_A_filter);
  201. $count = get_count_records($db, 'User_auth', $check_A_filter);
  202. if ($count > 0) {
  203. return FALSE;
  204. }
  205. $check_CNAME_filter = 'auth_id !="' . $id . '" and (alias ="' . mysqli_real_escape_string($db, $hostname) . '" or alias ="' . mysqli_real_escape_string($db, $hostname . '.' . $domain_zone) . '")';
  206. // LOG_DEBUG($db, "CNAME filter: ".$check_CNAME_filter);
  207. $count = get_count_records($db, 'User_auth_alias', $check_CNAME_filter);
  208. if ($count > 0) {
  209. return FALSE;
  210. }
  211. return TRUE;
  212. }
  213. // Транслитерация строк.
  214. function transliterate($string, $gost = false)
  215. {
  216. if ($gost) {
  217. $replace = array(
  218. "А" => "A",
  219. "а" => "a",
  220. "Б" => "B",
  221. "б" => "b",
  222. "В" => "V",
  223. "в" => "v",
  224. "Г" => "G",
  225. "г" => "g",
  226. "Д" => "D",
  227. "д" => "d",
  228. "Е" => "E",
  229. "е" => "e",
  230. "Ё" => "E",
  231. "ё" => "e",
  232. "Ж" => "Zh",
  233. "ж" => "zh",
  234. "З" => "Z",
  235. "з" => "z",
  236. "И" => "I",
  237. "и" => "i",
  238. "Й" => "I",
  239. "й" => "i",
  240. "К" => "K",
  241. "к" => "k",
  242. "Л" => "L",
  243. "л" => "l",
  244. "М" => "M",
  245. "м" => "m",
  246. "Н" => "N",
  247. "н" => "n",
  248. "О" => "O",
  249. "о" => "o",
  250. "П" => "P",
  251. "п" => "p",
  252. "Р" => "R",
  253. "р" => "r",
  254. "С" => "S",
  255. "с" => "s",
  256. "Т" => "T",
  257. "т" => "t",
  258. "У" => "U",
  259. "у" => "u",
  260. "Ф" => "F",
  261. "ф" => "f",
  262. "Х" => "Kh",
  263. "х" => "kh",
  264. "Ц" => "Tc",
  265. "ц" => "tc",
  266. "Ч" => "Ch",
  267. "ч" => "ch",
  268. "Ш" => "Sh",
  269. "ш" => "sh",
  270. "Щ" => "Shch",
  271. "щ" => "shch",
  272. "Ы" => "Y",
  273. "ы" => "y",
  274. "Э" => "E",
  275. "э" => "e",
  276. "Ю" => "Iu",
  277. "ю" => "iu",
  278. "Я" => "Ia",
  279. "я" => "ia",
  280. "ъ" => "",
  281. "ь" => ""
  282. );
  283. } else {
  284. $arStrES = array(
  285. "ае",
  286. "уе",
  287. "ое",
  288. "ые",
  289. "ие",
  290. "эе",
  291. "яе",
  292. "юе",
  293. "ёе",
  294. "ее",
  295. "ье",
  296. "ъе",
  297. "ый",
  298. "ий"
  299. );
  300. $arStrOS = array(
  301. "аё",
  302. "уё",
  303. "оё",
  304. "ыё",
  305. "иё",
  306. "эё",
  307. "яё",
  308. "юё",
  309. "ёё",
  310. "её",
  311. "ьё",
  312. "ъё",
  313. "ый",
  314. "ий"
  315. );
  316. $arStrRS = array(
  317. "а$",
  318. "у$",
  319. "о$",
  320. "ы$",
  321. "и$",
  322. "э$",
  323. "я$",
  324. "ю$",
  325. "ё$",
  326. "е$",
  327. "ь$",
  328. "ъ$",
  329. "@",
  330. "@"
  331. );
  332. $replace = array(
  333. "А" => "A",
  334. "а" => "a",
  335. "Б" => "B",
  336. "б" => "b",
  337. "В" => "V",
  338. "в" => "v",
  339. "Г" => "G",
  340. "г" => "g",
  341. "Д" => "D",
  342. "д" => "d",
  343. "Е" => "Ye",
  344. "е" => "e",
  345. "Ё" => "Ye",
  346. "ё" => "e",
  347. "Ж" => "Zh",
  348. "ж" => "zh",
  349. "З" => "Z",
  350. "з" => "z",
  351. "И" => "I",
  352. "и" => "i",
  353. "Й" => "Y",
  354. "й" => "y",
  355. "К" => "K",
  356. "к" => "k",
  357. "Л" => "L",
  358. "л" => "l",
  359. "М" => "M",
  360. "м" => "m",
  361. "Н" => "N",
  362. "н" => "n",
  363. "О" => "O",
  364. "о" => "o",
  365. "П" => "P",
  366. "п" => "p",
  367. "Р" => "R",
  368. "р" => "r",
  369. "С" => "S",
  370. "с" => "s",
  371. "Т" => "T",
  372. "т" => "t",
  373. "У" => "U",
  374. "у" => "u",
  375. "Ф" => "F",
  376. "ф" => "f",
  377. "Х" => "Kh",
  378. "х" => "kh",
  379. "Ц" => "Ts",
  380. "ц" => "ts",
  381. "Ч" => "Ch",
  382. "ч" => "ch",
  383. "Ш" => "Sh",
  384. "ш" => "sh",
  385. "Щ" => "Shch",
  386. "щ" => "shch",
  387. "Ъ" => "",
  388. "ъ" => "",
  389. "Ы" => "Y",
  390. "ы" => "y",
  391. "Ь" => "",
  392. "ь" => "",
  393. "Э" => "E",
  394. "э" => "e",
  395. "Ю" => "Yu",
  396. "ю" => "yu",
  397. "Я" => "Ya",
  398. "я" => "ya",
  399. "@" => "y",
  400. "$" => "ye"
  401. );
  402. $string = str_replace($arStrES, $arStrRS, $string);
  403. $string = str_replace($arStrOS, $arStrRS, $string);
  404. }
  405. return strtr($string, $replace);
  406. }
  407. function cidrToRange($cidr)
  408. {
  409. $range = array();
  410. $cidr = explode('/', $cidr);
  411. if (!isset($cidr[1])) {
  412. $cidr[1] = 32;
  413. }
  414. $start = (ip2long($cidr[0])) & ((-1 << (32 - (int) $cidr[1])));
  415. $stop = $start + pow(2, (32 - (int) $cidr[1])) - 1;
  416. $range[0] = long2ip($start);
  417. $range[1] = long2ip($stop);
  418. $range[2] = $cidr;
  419. //dhcp
  420. $dhcp_size = round(($stop - $start) / 2, PHP_ROUND_HALF_UP);
  421. $dhcp_start = $start + round($dhcp_size / 2, PHP_ROUND_HALF_UP);
  422. $range[3] = long2ip($dhcp_start);
  423. $range[4] = long2ip($dhcp_start + $dhcp_size);
  424. $range[5] = long2ip($start + 1);
  425. return $range;
  426. }
  427. function crypt_string($simple_string)
  428. {
  429. // Storin gthe cipher method
  430. $ciphering = "aes-128-cbc";
  431. // Using OpenSSl Encryption method
  432. $iv_length = openssl_cipher_iv_length($ciphering);
  433. $options = 0;
  434. // Using openssl_encrypt() function to encrypt the data
  435. return openssl_encrypt($simple_string, $ciphering, ENCRYPTION_KEY, $options, ENCRYPTION_IV);
  436. }
  437. function decrypt_string($crypted_string)
  438. {
  439. // Storin gthe cipher method
  440. $ciphering = "aes-128-cbc";
  441. // Using OpenSSl Encryption method
  442. $iv_length = openssl_cipher_iv_length($ciphering);
  443. $options = 0;
  444. // Using openssl_decrypt() function to decrypt the data
  445. return openssl_decrypt($crypted_string, $ciphering, ENCRYPTION_KEY, $options, ENCRYPTION_IV);
  446. }
  447. function print_ou_select($db, $ou_name, $ou_value)
  448. {
  449. print "<select id=\"$ou_name\" name=\"$ou_name\" >\n";
  450. $t_ou = mysqli_query($db, "SELECT id,ou_name FROM OU ORDER BY ou_name");
  451. while (list($f_ou_id, $f_ou_name) = mysqli_fetch_array($t_ou)) {
  452. print_select_item($f_ou_name, $f_ou_id, $ou_value);
  453. }
  454. print "</select>\n";
  455. }
  456. function print_instance_select($db, $instance_name, $instance_value)
  457. {
  458. print "<select id=\"$instance_name\" name=\"$instance_name\" >\n";
  459. $t_instance = mysqli_query($db, "SELECT * FROM filter_instances ORDER BY id");
  460. while (list($f_instance_id, $f_instance_name, $f_instance_comment) = mysqli_fetch_array($t_instance)) {
  461. print_select_item($f_instance_name, $f_instance_id, $instance_value);
  462. }
  463. print "</select>\n";
  464. }
  465. function get_subnet_description($db, $subnet_id)
  466. {
  467. if (empty($subnet_id)) {
  468. return '';
  469. }
  470. $subnet = get_record_sql($db, 'SELECT * FROM subnets WHERE id=' . $subnet_id);
  471. if (empty($subnet)) {
  472. return '';
  473. }
  474. $result = $subnet['subnet'] . '&nbsp(' . $subnet['comment'] . ')';
  475. return $result;
  476. }
  477. function get_filter_instance_description($db, $instance_id)
  478. {
  479. if (empty($instance_id)) {
  480. return '';
  481. }
  482. $instance = get_record_sql($db, 'SELECT * FROM filter_instances WHERE id=' . $instance_id);
  483. if (empty($instance)) {
  484. return '';
  485. }
  486. $result = $instance['name'] . '&nbsp(' . $instance['comment'] . ')';
  487. return $result;
  488. }
  489. function print_add_gw_subnets($db, $device_id, $gs_name)
  490. {
  491. print "<select id=\"$gs_name\" name=\"$gs_name\" >\n";
  492. $t_gs = mysqli_query($db, "SELECT id,subnet,comment FROM subnets WHERE subnets.free=0 AND subnets.id NOT IN (SELECT subnet_id FROM gateway_subnets WHERE gateway_subnets.device_id=" . $device_id . ") ORDER BY subnet");
  493. while (list($f_gs_id, $f_gs_name, $f_gs_comment) = mysqli_fetch_array($t_gs)) {
  494. print_select_item($f_gs_name . '(' . $f_gs_comment . ')', $f_gs_id, 0);
  495. }
  496. print "</select>\n";
  497. }
  498. function print_add_gw_instances($db, $device_id, $gs_name)
  499. {
  500. print "<select id=\"$gs_name\" name=\"$gs_name\" >\n";
  501. $t_gs = mysqli_query($db, "SELECT id,name,comment FROM filter_instances WHERE filter_instances.id NOT IN (SELECT instance_id FROM device_filter_instances WHERE device_filter_instances.device_id=" . $device_id . ") ORDER BY name");
  502. while (list($f_gs_id, $f_gs_name, $f_gs_comment) = mysqli_fetch_array($t_gs)) {
  503. print_select_item($f_gs_name . '(' . $f_gs_comment . ')', $f_gs_id, 0);
  504. }
  505. print "</select>\n";
  506. }
  507. function print_add_dev_interface($db, $device_id, $int_list, $int_name)
  508. {
  509. print "&nbsp<select id=\"$int_name\" name=\"$int_name\" >\n";
  510. $t_int = get_records_sql($db, "SELECT * FROM device_l3_interfaces WHERE device_id=" . $device_id);
  511. $int_exists = [];
  512. foreach ($t_int as $interface) {
  513. $int_exists[$interface['snmpin']] = $interface;
  514. }
  515. foreach ($int_list as $interface) {
  516. if (!empty($int_exists[$interface['index']])) {
  517. continue;
  518. }
  519. $value = $interface['name'] . ';' . $interface['index'] . ';' . $interface['type'];
  520. if ($interface['type'] == 1) {
  521. $interface['type'] = WEB_select_item_wan;
  522. }
  523. if ($interface['type'] == 0) {
  524. $interface['type'] = WEB_select_item_lan;
  525. }
  526. $display_str = $interface['name'] . '&nbsp|' . $interface['ip'] . '|' . $interface['type'];
  527. print_select_item($display_str, $value, 0);
  528. }
  529. print "</select>\n";
  530. }
  531. function print_ou_set($db, $ou_name, $ou_value)
  532. {
  533. print "<select id=\"$ou_name\" name=\"$ou_name\">\n";
  534. $t_ou = mysqli_query($db, "SELECT id,ou_name FROM OU WHERE id>=1 ORDER BY ou_name");
  535. while (list($f_ou_id, $f_ou_name) = mysqli_fetch_array($t_ou)) {
  536. print_select_item($f_ou_name, $f_ou_id, $ou_value);
  537. }
  538. print "</select>\n";
  539. }
  540. function print_subnet_select($db, $subnet_name, $subnet_value)
  541. {
  542. print "<select id=\"$subnet_name\" name=\"$subnet_name\" >\n";
  543. $t_subnet = mysqli_query($db, "SELECT id,subnet FROM subnets ORDER BY ip_int_start");
  544. print_select_item(WEB_select_item_all_ips, 0, $subnet_value);
  545. while (list($f_subnet_id, $f_subnet_name) = mysqli_fetch_array($t_subnet)) {
  546. print_select_item($f_subnet_name, $f_subnet_id, $subnet_value);
  547. }
  548. print "</select>\n";
  549. }
  550. function print_acl_select($db, $acl_name, $acl_value)
  551. {
  552. print "<select id=\"$acl_name\" name=\"$acl_name\" >\n";
  553. $t_acl = mysqli_query($db, "SELECT id,name FROM acl ORDER BY id");
  554. while (list($f_acl_id, $f_acl_name) = mysqli_fetch_array($t_acl)) {
  555. print_select_item($f_acl_name, $f_acl_id, $acl_value);
  556. }
  557. print "</select>\n";
  558. }
  559. function print_device_ip_select($db, $ip_name, $ip, $user_id)
  560. {
  561. print "<select id=\"$ip_name\" name=\"$ip_name\">\n";
  562. $auth_list = get_records_sql($db, "SELECT ip FROM User_auth WHERE user_id=$user_id AND deleted=0 ORDER BY ip_int");
  563. foreach ($auth_list as $row) {
  564. print_select_item($row['ip'], $row['ip'], $ip);
  565. }
  566. print "</select>\n";
  567. }
  568. function print_subnet_select_office($db, $subnet_name, $subnet_value)
  569. {
  570. print "<select id=\"$subnet_name\" name=\"$subnet_name\" >\n";
  571. $t_subnet = mysqli_query($db, "SELECT id,subnet FROM subnets WHERE office=1 ORDER BY ip_int_start");
  572. print_select_item(WEB_select_item_all_ips, 0, $subnet_value);
  573. while (list($f_subnet_id, $f_subnet_name) = mysqli_fetch_array($t_subnet)) {
  574. print_select_item($f_subnet_name, $f_subnet_id, $subnet_value);
  575. }
  576. print "</select>\n";
  577. }
  578. function print_subnet_select_office_splitted($db, $subnet_name, $subnet_value)
  579. {
  580. print "<select id=\"$subnet_name\" name=\"$subnet_name\" >\n";
  581. $t_subnet = mysqli_query($db, "SELECT id,subnet,ip_int_start,ip_int_stop FROM subnets WHERE office=1 ORDER BY ip_int_start");
  582. print_select_item(WEB_select_item_all_ips, 0, $subnet_value);
  583. while (list($f_subnet_id, $f_subnet_name, $f_start_ip, $f_stop_ip) = mysqli_fetch_array($t_subnet)) {
  584. print_select_item($f_subnet_name, $f_subnet_name, $subnet_value);
  585. $cidr = cidrToRange($f_subnet_name);
  586. if ($cidr[2][1] < 24) {
  587. while ($f_start_ip <= $f_stop_ip) {
  588. print_select_item("&nbsp&nbsp-&nbsp" . long2ip($f_start_ip) . "/24", long2ip($f_start_ip) . "/24", $subnet_value);
  589. $f_start_ip += 256;
  590. }
  591. }
  592. }
  593. print "</select>\n";
  594. }
  595. function print_loglevel_select($item_name, $value)
  596. {
  597. print "<select id=\"$item_name\" name=\"$item_name\">\n";
  598. print_select_item('Error', L_ERROR, $value);
  599. print_select_item('Warning', L_WARNING, $value);
  600. print_select_item('Info', L_INFO, $value);
  601. print_select_item('Verbose', L_VERBOSE, $value);
  602. print_select_item('Debug', L_DEBUG, $value);
  603. print "</select>\n";
  604. }
  605. function reencodeurl($url)
  606. {
  607. $url_arr = explode('?', $url);
  608. $fpage = $url_arr[0];
  609. if (isset($url_arr[1])) {
  610. $params = $url_arr[1];
  611. $params_arr = explode('&', $params);
  612. $new_params = '';
  613. foreach ($params_arr as $row) {
  614. $param = explode('=', $row);
  615. $key = $param[0];
  616. $value = urlencode(urldecode($param[1]));
  617. $new_params .= "&" . $key . "=" . $value;
  618. }
  619. $new_params = preg_replace('/^&/', '', $new_params);
  620. } else {
  621. $new_params = '=';
  622. }
  623. if ($new_params === '=') {
  624. $new_url = $fpage;
  625. } else {
  626. $new_url = $fpage . "?" . $new_params;
  627. }
  628. return $new_url;
  629. }
  630. function print_submenu_url($display_name, $page, $current_page, $last)
  631. {
  632. $url_arr = explode('?', $page);
  633. $fpage = $url_arr[0];
  634. $new_url = reencodeurl($page);
  635. if ($fpage === $current_page) {
  636. print "<b>$display_name</b>";
  637. } else {
  638. print "<a href='" . $new_url . "'> $display_name </a>";
  639. }
  640. if (!isset($last) or $last == 0) {
  641. print " | ";
  642. }
  643. }
  644. function print_submenu_nw($display_name, $page, $current_page, $last)
  645. {
  646. $url_arr = explode('?', $page);
  647. $fpage = $url_arr[0];
  648. $new_url = reencodeurl($page);
  649. if ($fpage === $current_page) {
  650. print "<b>$display_name</b>";
  651. } else {
  652. print '<a href="" onclick="window.open(\'' . $new_url . "', '_tab').focus(); return false;\">" . $display_name . "</a>";
  653. }
  654. if (!isset($last) or $last == 0) {
  655. print " | ";
  656. }
  657. }
  658. function print_url($display_name, $page)
  659. {
  660. print "<a href='" . reencodeurl($page) . "'> $display_name </a>";
  661. }
  662. function print_log_submenu($current_page)
  663. {
  664. print "<div id='submenu'>\n";
  665. print_submenu_url(WEB_submenu_dhcp_log, '/admin/logs/dhcp.php', $current_page, 0);
  666. print_submenu_url(WEB_submenu_work_log, '/admin/logs/index.php', $current_page, 0);
  667. print_submenu_url(WEB_submenu_mac_history, '/admin/logs/mac.php', $current_page, 0);
  668. print_submenu_url(WEB_submenu_ip_history, '/admin/logs/ip.php', $current_page, 0);
  669. print_submenu_url(WEB_submenu_mac_unknown, '/admin/logs/unknown.php', $current_page, 0);
  670. print_submenu_url(WEB_submenu_traffic, '/admin/logs/detaillog.php', $current_page, 0);
  671. print_submenu_url(WEB_submenu_syslog, '/admin/logs/syslog.php', $current_page, 1);
  672. print "</div>\n";
  673. }
  674. function print_control_submenu($current_page)
  675. {
  676. print "<div id='submenu'>\n";
  677. print_submenu_url(WEB_submenu_control, '/admin/customers/control.php', $current_page, 0);
  678. print_submenu_url(WEB_submenu_network, '/admin/customers/index-subnets.php', $current_page, 0);
  679. print_submenu_url(WEB_submenu_network_stats, '/admin/customers/control-subnets-usage.php', $current_page, 0);
  680. print_submenu_url(WEB_submenu_options, '/admin/customers/control-options.php', $current_page, 0);
  681. print_submenu_url(WEB_submenu_customers, '/admin/customers/index.php', $current_page, 0);
  682. print_submenu_url(WEB_submenu_buildings, '/admin/customers/building.php', $current_page, 0);
  683. print_submenu_url(WEB_submenu_device_models, '/admin/customers/devmodels.php', $current_page, 0);
  684. print_submenu_url(WEB_submenu_vendors, '/admin/customers/devvendors.php', $current_page, 1);
  685. print "</div>\n";
  686. }
  687. function print_filters_submenu($current_page)
  688. {
  689. print "<div id='submenu'>\n";
  690. print_submenu_url(WEB_submenu_filter_list, '/admin/filters/index.php', $current_page, 0);
  691. print_submenu_url(WEB_submenu_filter_group, '/admin/filters/groups.php', $current_page, 0);
  692. print_submenu_url(WEB_submenu_filter_instances, '/admin/filters/instances.php', $current_page, 1);
  693. print "</div>\n";
  694. }
  695. function print_reports_submenu($current_page)
  696. {
  697. print "<div id='submenu'>\n";
  698. print_submenu_url(WEB_submenu_traffic_ip_report, '/admin/reports/index-full.php', $current_page, 0);
  699. print_submenu_url(WEB_submenu_traffic_login_report, '/admin/reports/index.php', $current_page, 0);
  700. print_submenu_url(WEB_submenu_traffic_wan_report, '/admin/reports/wan.php', $current_page, 1);
  701. print "</div>\n";
  702. }
  703. function print_trafdetail_submenu($current_page, $params, $description)
  704. {
  705. print "<div id='subsubmenu'>\n";
  706. print "$description\n";
  707. print_submenu_url(WEB_submenu_traffic_top10, '/admin/reports/userdaydetail.php' . "?$params", $current_page, 0);
  708. print_submenu_url(WEB_submenu_detail_log, '/admin/reports/userdaydetaillog.php' . "?$params", $current_page, 1);
  709. print "</div>\n";
  710. }
  711. function print_device_submenu($current_page)
  712. {
  713. print "<div id='submenu'>\n";
  714. print_submenu_url(WEB_submenu_net_devices, '/admin/devices/index.php', $current_page, 0);
  715. print_submenu_url(WEB_submenu_passive_net_devices, '/admin/devices/index-passive.php', $current_page, 0);
  716. print_submenu_url(WEB_submenu_hierarchy, '/admin/devices/index-tree.php', $current_page, 0);
  717. print_submenu_url(WEB_submenu_ports_vlan, '/admin/devices/portsbyvlan.php', $current_page, 1);
  718. print "</div>\n";
  719. }
  720. function open_window_url($url)
  721. {
  722. return "window.open('" . $url . "', '_blank');";
  723. }
  724. function print_editdevice_submenu($current_page, $id, $dev_type, $dev_name = NULL)
  725. {
  726. print "<div id='subsubmenu'>\n";
  727. $dev_id = '';
  728. if (isset($id)) {
  729. $dev_id = '?id=' . $id;
  730. }
  731. if (!empty($dev_name)) {
  732. print "<b>" . $dev_name . "::</b>";
  733. }
  734. print_submenu_url(WEB_submenu_options, '/admin/devices/editdevice.php' . $dev_id, $current_page, 0);
  735. if ($dev_type <= 2) {
  736. print_submenu_url(WEB_submenu_ports, '/admin/devices/switchport.php' . $dev_id, $current_page, 0);
  737. print_submenu_url(WEB_submenu_state, '/admin/devices/switchstatus.php' . $dev_id, $current_page, 0);
  738. print_submenu_url(WEB_submenu_connections, '/admin/devices/switchport-conn.php' . $dev_id, $current_page, 1);
  739. }
  740. print "</div>\n";
  741. }
  742. function print_ip_submenu($current_page)
  743. {
  744. print "<div id='submenu'>\n";
  745. print_submenu_url(WEB_submenu_ip_list, '/admin/iplist/index.php', $current_page, 0);
  746. print_submenu_url(WEB_submenu_nagios, '/admin/iplist/nagios.php', $current_page, 0);
  747. print_submenu_url(WEB_submenu_doubles, '/admin/iplist/doubles.php', $current_page, 0);
  748. print_submenu_url(WEB_submenu_deleted, '/admin/iplist/deleted.php', $current_page, 0);
  749. print_submenu_url(WEB_submenu_auto_rules, '/admin/iplist/auto_rules.php', $current_page, 1);
  750. print "</div>\n";
  751. }
  752. function get_nagios_name($auth)
  753. {
  754. if (!empty($auth['dns_name'])) {
  755. return $auth['dns_name'];
  756. }
  757. if (!empty($auth['dhcp_hostname'])) {
  758. return $auth['dhcp_hostname'];
  759. }
  760. if (!empty($auth['comments'])) {
  761. $result = transliterate($auth['comments']);
  762. $result = preg_replace('/\(/', '-', $result);
  763. $result = preg_replace('/\)/', '-', $result);
  764. $result = preg_replace('/--/', '-', $result);
  765. return $result;
  766. }
  767. if (empty($auth['login'])) {
  768. $auth['login'] = 'host';
  769. }
  770. return $auth['login'] . "_" . $auth['id'];
  771. }
  772. function get_ou($db, $ou_value)
  773. {
  774. if (!isset($ou_value)) {
  775. return;
  776. }
  777. $ou_name = get_record_sql($db, "SELECT ou_name FROM OU WHERE id=$ou_value");
  778. if (empty($ou_name)) {
  779. return;
  780. }
  781. return $ou_name['ou_name'];
  782. }
  783. function get_device_model($db, $model_value)
  784. {
  785. if (!isset($model_value)) {
  786. return;
  787. }
  788. $model_name = get_record_sql($db, "SELECT model_name FROM device_models WHERE id=$model_value");
  789. if (empty($model_name)) {
  790. return;
  791. }
  792. return $model_name['model_name'];
  793. }
  794. function get_device_model_name($db, $model_value)
  795. {
  796. if (!isset($model_value)) {
  797. return '';
  798. }
  799. $model_name = get_record_sql($db, "SELECT M.id,M.model_name,V.name FROM device_models M,vendors V WHERE M.vendor_id = V.id AND M.id=$model_value");
  800. if (empty($model_name)) {
  801. return '';
  802. }
  803. return $model_name['name'] . ' ' . $model_name['model_name'];
  804. }
  805. function get_device_model_vendor($db, $model_value)
  806. {
  807. if (!isset($model_value)) {
  808. return '';
  809. }
  810. $model_name = get_record_sql($db, "SELECT vendor_id FROM device_models WHERE id=$model_value");
  811. if (empty($model_name)) {
  812. return '';
  813. }
  814. return $model_name['vendor_id'];
  815. }
  816. function get_building($db, $building_value)
  817. {
  818. if (!isset($building_value)) {
  819. return;
  820. }
  821. $building_name = get_record_sql($db, "SELECT name FROM building WHERE id=$building_value");
  822. if (empty($building_name)) {
  823. return;
  824. }
  825. return $building_name['name'];
  826. }
  827. function print_device_model_select($db, $device_model_name, $device_model_value)
  828. {
  829. print "<select id=\"$device_model_name\" name=\"$device_model_name\" class=\"js-select-single\">\n";
  830. $t_device_model = mysqli_query($db, "SELECT M.id,M.model_name,V.name FROM device_models M,vendors V WHERE M.vendor_id = V.id ORDER BY V.name,M.model_name");
  831. while (list($f_device_model_id, $f_device_model_name, $f_vendor_name) = mysqli_fetch_array($t_device_model)) {
  832. print_select_item($f_vendor_name . " " . $f_device_model_name, $f_device_model_id, $device_model_value);
  833. }
  834. print "</select>\n";
  835. }
  836. function print_group_select($db, $group_name, $group_value)
  837. {
  838. print "<select id=\"$group_name\" name=\"$group_name\">\n";
  839. $t_group = mysqli_query($db, "SELECT id,group_name FROM Group_list Order by group_name");
  840. while (list($f_group_id, $f_group_name) = mysqli_fetch_array($t_group)) {
  841. print_select_item($f_group_name, $f_group_id, $group_value);
  842. }
  843. print "</select>\n";
  844. }
  845. function print_building_select($db, $building_name, $building_value)
  846. {
  847. print "<select id=\"$building_name\" name=\"$building_name\">\n";
  848. print_select_item(WEB_select_item_all, 0, $building_value);
  849. $t_building = mysqli_query($db, "SELECT id,name FROM building Order by name");
  850. while (list($f_building_id, $f_building_name) = mysqli_fetch_array($t_building)) {
  851. print_select_item($f_building_name, $f_building_id, $building_value);
  852. }
  853. print "</select>\n";
  854. }
  855. function print_devmodels_select($db, $devmodel_name, $devmodel_value, $dev_filter = 'device_type<=2')
  856. {
  857. print "<select id=\"$devmodel_name\" name=\"$devmodel_name\">\n";
  858. print_select_item(WEB_select_item_all, -1, $devmodel_value);
  859. $t_devmodel = mysqli_query($db, "SELECT M.id,V.name,M.model_name FROM device_models M,vendors V WHERE M.vendor_id = V.id and M.id in (SELECT device_model_id FROM devices WHERE $dev_filter) ORDER BY V.name,M.model_name");
  860. while (list($f_devmodel_id, $f_devmodel_vendor, $f_devmodel_name) = mysqli_fetch_array($t_devmodel)) {
  861. print_select_item($f_devmodel_vendor . " " . $f_devmodel_name, $f_devmodel_id, $devmodel_value);
  862. }
  863. print "</select>\n";
  864. }
  865. function print_devtypes_select($db, $devtype_name, $devtype_value, $mode)
  866. {
  867. print "<select id=\"$devtype_name\" name=\"$devtype_name\">\n";
  868. print_select_item(WEB_select_item_all, -1, $devtype_value);
  869. $filter = '';
  870. if (!empty($mode)) {
  871. $filter = "WHERE $mode";
  872. }
  873. $t_devtype = mysqli_query($db, "SELECT id,`name." . HTML_LANG . "` FROM device_types $filter ORDER BY `name." . HTML_LANG . "`");
  874. while (list($f_devtype_id, $f_devtype_name) = mysqli_fetch_array($t_devtype)) {
  875. print_select_item($f_devtype_name, $f_devtype_id, $devtype_value);
  876. }
  877. print "</select>\n";
  878. }
  879. function print_devtype_select($db, $devtype_name, $devtype_value)
  880. {
  881. print "<select id=\"$devtype_name\" name=\"$devtype_name\">\n";
  882. $t_devtype = mysqli_query($db, "SELECT id,`name." . HTML_LANG . "` FROM device_types ORDER BY `name." . HTML_LANG . "`");
  883. while (list($f_devtype_id, $f_devtype_name) = mysqli_fetch_array($t_devtype)) {
  884. print_select_item($f_devtype_name, $f_devtype_id, $devtype_value);
  885. }
  886. print "</select>\n";
  887. }
  888. function get_group($db, $group_value)
  889. {
  890. list($group_name) = mysqli_fetch_array(mysqli_query($db, "SELECT group_name FROM Group_list WHERE id=$group_value"));
  891. return $group_name;
  892. }
  893. function get_devtype_name($db, $device_type_id)
  894. {
  895. list($type_name) = mysqli_fetch_array(mysqli_query($db, "SELECT `name." . HTML_LANG . "` FROM device_types WHERE id=$device_type_id"));
  896. return $type_name;
  897. }
  898. function get_l3_interfaces($db, $device_id)
  899. {
  900. $wan = '';
  901. $lan = '';
  902. $t_l3int = mysqli_query($db, "SELECT name,interface_type FROM device_l3_interfaces WHERE device_id=$device_id ORDER BY name");
  903. while (list($f_name, $f_type) = mysqli_fetch_array($t_l3int)) {
  904. if ($f_type == 0) {
  905. $lan = $lan . " " . $f_name;
  906. }
  907. if ($f_type == 1) {
  908. $wan = $wan . " " . $f_name;
  909. }
  910. }
  911. $wan = trim($wan);
  912. $lan = trim($lan);
  913. $result = '';
  914. if (!empty($wan)) {
  915. $result .= ' WAN: ' . $wan . '<br>';
  916. }
  917. if (!empty($lan)) {
  918. $result .= ' LAN: ' . $lan;
  919. }
  920. return trim($result);
  921. }
  922. function get_wan_interfaces($db, $device_id)
  923. {
  924. $l3_wan_sql = "SELECT id,name,snmpin FROM device_l3_interfaces WHERE device_id='" . $device_id . "' and interface_type=1 ORDER BY name";
  925. $t_l3int = get_records_sql($db, $l3_wan_sql);
  926. for ($i = 0; $i < count($t_l3int); ++$i) {
  927. $t_l3int[$i]['comment'] = '';
  928. if (empty($t_l3int[$i]['snmpin'])) {
  929. continue;
  930. }
  931. $con_sql = "SELECT * FROM `device_ports` WHERE device_id='" . $device_id . "' AND snmp_index='" . $t_l3int[$i]['snmpin'] . "'";
  932. $conn = get_record_sql($db, $con_sql);
  933. if (isset($conn) and !empty($conn['comment'])) {
  934. $t_l3int[$i]['comment'] = $conn['comment'];
  935. }
  936. }
  937. return $t_l3int;
  938. }
  939. function get_gw_subnets($db, $device_id)
  940. {
  941. $gw_subnets_sql = 'SELECT gateway_subnets.*,subnets.subnet,subnets.comment FROM gateway_subnets LEFT JOIN subnets ON gateway_subnets.subnet_id = subnets.id WHERE gateway_subnets.device_id=' . $device_id . ' ORDER BY subnets.subnet ASC';
  942. $gw_subnets = get_records_sql($db, $gw_subnets_sql);
  943. $result = '';
  944. foreach ($gw_subnets as $row) {
  945. if (!empty($row)) {
  946. $result .= ' ' . $row['subnet'] . '<br>';
  947. }
  948. }
  949. return trim($result);
  950. }
  951. function print_queue_select($db, $queue_name, $queue_value)
  952. {
  953. print "<select id=\"$queue_name\" name=\"$queue_name\">\n";
  954. $t_queue = mysqli_query($db, "SELECT id,queue_name FROM Queue_list Order by queue_name");
  955. while (list($f_queue_id, $f_queue_name) = mysqli_fetch_array($t_queue)) {
  956. print_select_item($f_queue_name, $f_queue_id, $queue_value);
  957. }
  958. print "</select>\n";
  959. }
  960. function get_queue($db, $queue_value)
  961. {
  962. list($queue_name) = mysqli_fetch_array(mysqli_query($db, "SELECT queue_name FROM Queue_list WHERE id=$queue_value"));
  963. return $queue_name;
  964. }
  965. function print_qa_l3int_select($qa_name, $qa_value = 0)
  966. {
  967. print "<select id=\"$qa_name\" name=\"$qa_name\">\n";
  968. print_select_item(WEB_select_item_lan, 0, $qa_value);
  969. print_select_item(WEB_select_item_wan, 1, $qa_value);
  970. print "</select>\n";
  971. }
  972. function print_qa_rule_select($qa_name, $qa_value = 1)
  973. {
  974. print "<select id=\"$qa_name\" name=\"$qa_name\">\n";
  975. print_select_item('Subnet', 1, $qa_value);
  976. print_select_item('Mac', 2, $qa_value);
  977. print_select_item('Hostname', 3, $qa_value);
  978. print "</select>\n";
  979. }
  980. function print_snmp_auth_proto_select($qa_name, $qa_value = 'sha512')
  981. {
  982. print "<select id=\"$qa_name\" name=\"$qa_name\">\n";
  983. print_select_item('sha512', 'sha512', $qa_value);
  984. print_select_item('sha256', 'sha256', $qa_value);
  985. print_select_item('sha', 'sha', $qa_value);
  986. print_select_item('md5', 'md5', $qa_value);
  987. print "</select>\n";
  988. }
  989. function print_snmp_priv_proto_select($qa_name, $qa_value = 'aes128')
  990. {
  991. print "<select id=\"$qa_name\" name=\"$qa_name\">\n";
  992. print_select_item('aes128', 'aes128', $qa_value);
  993. print_select_item('aes', 'aes', $qa_value);
  994. print_select_item('des', 'des', $qa_value);
  995. print "</select>\n";
  996. }
  997. function get_int($qa_value = 0)
  998. {
  999. if (empty($qa_value)) {
  1000. $qa_value = 0;
  1001. } else {
  1002. $qa_value = (int)$qa_value * 1;
  1003. }
  1004. return $qa_value;
  1005. }
  1006. function print_qa_select($qa_name, $qa_value = 0)
  1007. {
  1008. print "<select name=\"$qa_name\" id=\"$qa_name\">\n";
  1009. if (empty($qa_value)) {
  1010. $qa_value = 0;
  1011. } else {
  1012. $qa_value = $qa_value * 1;
  1013. }
  1014. print_select_item(WEB_select_item_yes, 1, $qa_value);
  1015. print_select_item(WEB_select_item_no, 0, $qa_value);
  1016. print "</select>\n";
  1017. }
  1018. function print_list_select($qa_name, $qa_value, $list)
  1019. {
  1020. print "<select id=\"$qa_name\" name=\"$qa_name\">\n";
  1021. if (empty($qa_value)) {
  1022. $qa_value = '';
  1023. }
  1024. for ($i = 0; $i < count($list); ++$i) {
  1025. print_select_item($list[$i], $list[$i], $qa_value);
  1026. }
  1027. print "</select>\n";
  1028. }
  1029. function print_qa_select_ext($qa_name, $qa_value = 0, $readonly = 1)
  1030. {
  1031. $state = '';
  1032. if ($readonly) {
  1033. $state = 'disabled=true';
  1034. }
  1035. print "<select id=\"$qa_name\" name=\"$qa_name\">\n";
  1036. print_select_item_ext(WEB_select_item_yes, 1, $qa_value, $readonly);
  1037. print_select_item_ext(WEB_select_item_no, 0, $qa_value, $readonly);
  1038. print "</select>\n";
  1039. }
  1040. function print_td_yes_no($qa_value = 0, $text = FALSE)
  1041. {
  1042. $cl = 'down';
  1043. if ($qa_value == 1) { $cl = 'up'; }
  1044. // $cl = 'data';
  1045. if ($text) {
  1046. print "<td class=\"$cl\">";
  1047. if ($qa_value == 1) { print WEB_select_item_yes; } else { print WEB_select_item_no; }
  1048. print "</td>\n";
  1049. } else { print_td_qa($qa_value,FALSE,$cl); }
  1050. }
  1051. function print_yes_no($qa_value = 0, $yes_style = 'data', $no_style='data')
  1052. {
  1053. if ($qa_value) { $cl = $yes_style; } else { $cl = $no_style; }
  1054. print "<td class=\"$cl\">";
  1055. if ($qa_value == 1) {
  1056. print WEB_select_item_yes;
  1057. } else {
  1058. print WEB_select_item_no;
  1059. }
  1060. print "</td>\n";
  1061. }
  1062. function print_control_proto_select($qa_name, $qa_value = -1)
  1063. {
  1064. print "<select name=\"$qa_name\">\n";
  1065. print_select_item('Disabled', -1, $qa_value);
  1066. print_select_item('Ssh', 0, $qa_value);
  1067. print_select_item('Telnet', 1, $qa_value);
  1068. // print_select_item('Mikrotik rest api', 2, $qa_value);
  1069. print "</select>\n";
  1070. }
  1071. function print_snmp_select($qa_name, $qa_value = 0)
  1072. {
  1073. print "<select id=\"$qa_name\" name=\"$qa_name\">\n";
  1074. print_select_item('Disabled', 0, $qa_value);
  1075. print_select_item('v1', 1, $qa_value);
  1076. print_select_item('v2', 2, $qa_value);
  1077. print_select_item('v3', 3, $qa_value);
  1078. print "</select>\n";
  1079. }
  1080. function print_dhcp_select($qa_name, $qa_value = 0)
  1081. {
  1082. print "<select id=\"$qa_name\" name=\"$qa_name\">\n";
  1083. if (!isset($qa_value) or strlen($qa_value) == 0) {
  1084. $qa_value = 'all';
  1085. }
  1086. print_select_item(WEB_select_item_events, 'all', $qa_value);
  1087. print_select_item(WEB_select_item_lease, 'add', $qa_value);
  1088. print_select_item(WEB_select_item_lease_refresh, 'old', $qa_value);
  1089. print_select_item(WEB_select_item_lease_free, 'del', $qa_value);
  1090. print "</select>\n";
  1091. }
  1092. function print_nagios_handler_select($qa_name)
  1093. {
  1094. print "<select name=\"$qa_name\">\n";
  1095. print_select_simple(WEB_select_item_no, '');
  1096. print_select_simple('restart-port', 'restart-port');
  1097. print "</select>\n";
  1098. }
  1099. function print_dhcp_acl_select($qa_name)
  1100. {
  1101. print "<select name=\"$qa_name\">\n";
  1102. print_select_simple(WEB_select_item_no, '');
  1103. print_select_simple('hotspot-free', 'hotspot-free');
  1104. print "</select>\n";
  1105. }
  1106. function print_enabled_select($qa_name, $qa_value)
  1107. {
  1108. print "<select id=\"$qa_name\" name=\"$qa_name\">\n";
  1109. if (!isset($qa_value) or strlen($qa_value) == 0) {
  1110. $qa_value = 0;
  1111. }
  1112. print_select_item('-', 0, $qa_value);
  1113. print_select_item(WEB_select_item_disabled, 1, $qa_value);
  1114. print_select_item(WEB_select_item_enabled, 2, $qa_value);
  1115. print "</select>\n";
  1116. }
  1117. function print_rule_target_select($qa_name, $qa_value)
  1118. {
  1119. print "<select id=\"$qa_name\" name=\"$qa_name\">\n";
  1120. if (!isset($qa_value) or strlen($qa_value) == 0) {
  1121. $qa_value = 0;
  1122. }
  1123. print_select_item('-', 0, $qa_value);
  1124. print_select_item(WEB_rules_target_user, 1, $qa_value);
  1125. print_select_item(WEB_rules_target_group, 2, $qa_value);
  1126. print "</select>\n";
  1127. }
  1128. function print_rule_type_select($qa_name, $qa_value)
  1129. {
  1130. print "<select id=\"$qa_name\" name=\"$qa_name\">\n";
  1131. if (!isset($qa_value) or strlen($qa_value) == 0) {
  1132. $qa_value = 0;
  1133. }
  1134. print_select_item('-', 0, $qa_value);
  1135. print_select_item(WEB_rules_type_subnet, 1, $qa_value);
  1136. print_select_item(WEB_rules_type_mac, 2, $qa_value);
  1137. print_select_item(WEB_rules_type_hostname, 3, $qa_value);
  1138. print "</select>\n";
  1139. }
  1140. function print_yn_select($qa_name, $qa_value)
  1141. {
  1142. print "<select id=\"$qa_name\" name=\"$qa_name\">\n";
  1143. if (!isset($qa_value) or strlen($qa_value) == 0) {
  1144. $qa_value = 0;
  1145. }
  1146. print_select_item('-', 0, $qa_value);
  1147. print_select_item(WEB_select_item_yes, 1, $qa_value);
  1148. print_select_item(WEB_select_item_no, 2, $qa_value);
  1149. print "</select>\n";
  1150. }
  1151. function print_ip_type_select($qa_name, $qa_value)
  1152. {
  1153. print "<select id=\"$qa_name\" name=\"$qa_name\">\n";
  1154. if (!isset($qa_value) or strlen($qa_value) == 0) {
  1155. $qa_value = 0;
  1156. }
  1157. print_select_item(WEB_select_item_every, 0, $qa_value);
  1158. print_select_item(WEB_select_item_static, 1, $qa_value);
  1159. print_select_item(WEB_select_item_dhcp, 2, $qa_value);
  1160. print_select_item(WEB_select_item_suspicious, 3, $qa_value);
  1161. print "</select>\n";
  1162. }
  1163. function print_vendor_select($db, $qa_name, $qa_value)
  1164. {
  1165. print "<select id=\"$qa_name\" name=\"$qa_name\" style=\"width: 100%\">\n";
  1166. $sSQL = "SELECT id,`name` FROM `vendors` order by `name`";
  1167. $vendors = mysqli_query($db, $sSQL);
  1168. print_select_item(WEB_select_item_all, 0, $qa_value);
  1169. while (list($v_id, $v_name) = mysqli_fetch_array($vendors)) {
  1170. print_select_item($v_name, $v_id, $qa_value);
  1171. }
  1172. print "</select>\n";
  1173. }
  1174. function print_vendor_set($db, $qa_name, $qa_value)
  1175. {
  1176. print "<select id=\"$qa_name\" name=\"$qa_name\" style=\"width: 100%\">\n";
  1177. $sSQL = "SELECT id,`name` FROM `vendors` order by `name`";
  1178. $vendors = mysqli_query($db, $sSQL);
  1179. while (list($v_id, $v_name) = mysqli_fetch_array($vendors)) {
  1180. print_select_item($v_name, $v_id, $qa_value);
  1181. }
  1182. print "</select>\n";
  1183. }
  1184. function get_vendor_name($db, $v_id)
  1185. {
  1186. $vendor = get_record_sql($db, "SELECT * FROM `vendors` WHERE id=" . $v_id);
  1187. if (empty($vendor)) {
  1188. return NULL;
  1189. }
  1190. return $vendor['name'];
  1191. }
  1192. function get_qa($qa_value, $text = FALSE)
  1193. {
  1194. if ($text) {
  1195. if ($qa_value == 1) { return "Да"; }
  1196. return "Нет";
  1197. } else {
  1198. if ($qa_value == 1) { return '<span style="font-size: 16px; font-weight: bold;">✓</span>'; }
  1199. return '<span style="font-size: 16px;">✗</span>';
  1200. }
  1201. }
  1202. function get_yes($qa_value, $text = FALSE)
  1203. {
  1204. if ($text) {
  1205. if ($qa_value == 1) { return "Да"; }
  1206. return "";
  1207. } else {
  1208. if ($qa_value == 1) { return '<span style="font-size: 16px; font-weight: bold;">✓</span>'; }
  1209. return "";
  1210. }
  1211. }
  1212. function print_td_qa ($qa_value, $text = FALSE, $parent_class = '')
  1213. {
  1214. $cl = "data_green";
  1215. if (!$qa_value) { $cl = "data_red"; }
  1216. print "<td class=\"$parent_class $cl\" >" . get_qa($qa_value,$text) . "</td>";
  1217. }
  1218. function print_td_yes ($qa_value, $text = FALSE, $parent_class = '')
  1219. {
  1220. $cl = "data_green";
  1221. if (!$qa_value) { $cl = "data_red"; }
  1222. print "<td class=\"$parent_class $cl\" >" . get_yes($qa_value,$text) . "</td>";
  1223. }
  1224. function print_action_select($action_name, $action_value)
  1225. {
  1226. print "<select id=\"$action_name\" name=\"$action_name\">\n";
  1227. print_select_item(WEB_select_item_allow, 1, $action_value);
  1228. print_select_item(WEB_select_item_forbidden, 0, $action_value);
  1229. print "</select>\n";
  1230. }
  1231. function get_action($action_value)
  1232. {
  1233. if ($action_value == 1) {
  1234. return "Разрешить";
  1235. }
  1236. return "Запретить";
  1237. }
  1238. function print_filter_select($db, $filter_name, $group_id)
  1239. {
  1240. print "<select id=\"$filter_name\" name=\"$filter_name\" class=\"js-select-single\">\n";
  1241. if (isset($group_id)) {
  1242. $sSQL = "SELECT id,name FROM Filter_list WHERE Filter_list.id not in (Select filter_id FROM Group_filters WHERE group_id=$group_id)";
  1243. } else {
  1244. $sSQL = "SELECT id,name FROM Filter_list Order by name";
  1245. }
  1246. $t_filters = mysqli_query($db, $sSQL);
  1247. while (list($filter_id, $filter_name) = mysqli_fetch_array($t_filters)) {
  1248. print_select_item($filter_name, $filter_id, 0);
  1249. }
  1250. print "</select>\n";
  1251. }
  1252. function get_filter($db, $filter_value)
  1253. {
  1254. list($filter) = mysqli_fetch_array(mysqli_query($db, "SELECT name FROM Filter_list WHERE id=" . $filter_value));
  1255. return $filter;
  1256. }
  1257. function get_login($db, $user_id)
  1258. {
  1259. list($login) = mysqli_fetch_array(mysqli_query($db, "SELECT login FROM User_list WHERE id=$user_id"));
  1260. return $login;
  1261. }
  1262. function get_auth_count($db, $user_id)
  1263. {
  1264. list($count) = mysqli_fetch_array(mysqli_query($db, "SELECT count(id) FROM User_auth WHERE user_id=$user_id and deleted=0"));
  1265. return $count;
  1266. }
  1267. function print_login_select($db, $login_name, $current_login)
  1268. {
  1269. print "<select id=\"$login_name\" name=\"$login_name\" class=\"js-select-single\">\n";
  1270. $t_login = mysqli_query($db, "SELECT id,login FROM User_list Order by Login");
  1271. print_select_item('None', 0, $current_login);
  1272. while (list($f_user_id, $f_login) = mysqli_fetch_array($t_login)) {
  1273. print_select_item($f_login, $f_user_id, $current_login);
  1274. }
  1275. print "</select>\n";
  1276. }
  1277. function print_auth_select($db, $login_name, $current_auth)
  1278. {
  1279. print "<select id=\"$login_name\" name=\"$login_name\" class=\"js-select-single\">\n";
  1280. $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");
  1281. print_select_item('Empty', 0, $current_auth);
  1282. while (list($f_login, $f_fio, $f_ip, $f_auth_id) = mysqli_fetch_array($t_login)) {
  1283. print_select_item($f_login . "[" . $f_fio . "] - " . $f_ip, $f_auth_id, $current_auth);
  1284. }
  1285. print "</select>\n";
  1286. }
  1287. function print_auth_select_mac($db, $login_name, $current_auth)
  1288. {
  1289. print "<select id=\"$login_name\" name=\"$login_name\" class=\"js-select-single\">\n";
  1290. $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");
  1291. print_select_item('Empty', 0, $current_auth);
  1292. while (list($f_login, $f_fio, $f_ip, $f_mac, $f_auth_id) = mysqli_fetch_array($t_login)) {
  1293. print_select_item($f_login . "[" . $f_mac . "] - " . $f_ip, $f_auth_id, $current_auth);
  1294. }
  1295. print "</select>\n";
  1296. }
  1297. function compact_port_name($port)
  1298. {
  1299. $result = $port;
  1300. $result = preg_replace('/XGigabitEthernet/', 'X', $result);
  1301. $result = preg_replace('/TenGigabitEthernet/', 'Te', $result);
  1302. $result = preg_replace('/GigabitEthernet/', 'Gi', $result);
  1303. return $result;
  1304. }
  1305. function print_device_port_select($db, $field_name, $device_id, $target_id)
  1306. {
  1307. print "<select id=\"$field_name\" name=\"$field_name\" class=\"js-select-single\" >\n";
  1308. if (empty($target_id)) {
  1309. $target_id = 0;
  1310. }
  1311. if (empty($device_id)) {
  1312. $device_id = 0;
  1313. }
  1314. $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";
  1315. $t_device = mysqli_query($db, $d_sql);
  1316. print_select_item('Empty', 0, $target_id);
  1317. while (list($f_name, $f_port, $f_device_id, $f_target_id, $f_ifname) = mysqli_fetch_array($t_device)) {
  1318. if (empty($f_ifname)) {
  1319. $f_ifname = $f_port;
  1320. }
  1321. print_select_item($f_name . "[" . $f_port . "] - " . compact_port_name($f_ifname), $f_target_id, $target_id);
  1322. }
  1323. print "</select>\n";
  1324. }
  1325. function print_device_select($db, $field_name, $device_id)
  1326. {
  1327. print "<select id=\"$field_name\" name=\"$field_name\" class=\"js-select-single\" >\n";
  1328. $d_sql = "SELECT D.device_name, D.id FROM devices AS D Where D.deleted=0 order by D.device_name ASC";
  1329. $t_device = mysqli_query($db, $d_sql);
  1330. print_select_item(WEB_select_item_every, 0, $device_id);
  1331. while (list($f_name, $f_device_id) = mysqli_fetch_array($t_device)) {
  1332. print_select_item($f_name, $f_device_id, $device_id);
  1333. }
  1334. print "</select>\n";
  1335. }
  1336. function print_netdevice_select($db, $field_name, $device_id)
  1337. {
  1338. print "<select id=\"$field_name\" name=\"$field_name\" class=\"js-select-single\" >\n";
  1339. $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";
  1340. $t_device = mysqli_query($db, $d_sql);
  1341. print_select_item(WEB_select_item_every, 0, $device_id);
  1342. while (list($f_name, $f_device_id) = mysqli_fetch_array($t_device)) {
  1343. print_select_item($f_name, $f_device_id, $device_id);
  1344. }
  1345. print "</select>\n";
  1346. }
  1347. function print_vlan_select($db, $field_name, $vlan)
  1348. {
  1349. print "<select id=\"$field_name\" name=\"$field_name\" class=\"js-select-single\" style=\"width: 100px;\" >\n";
  1350. $d_sql = "SELECT DISTINCT vlan FROM device_ports ORDER BY vlan DESC";
  1351. $v_device = mysqli_query($db, $d_sql);
  1352. if (!isset($vlan) or empty($vlan)) {
  1353. $vlan = 1;
  1354. };
  1355. print_select_item('1', 1, $vlan);
  1356. while (list($f_vlan) = mysqli_fetch_array($v_device)) {
  1357. if ($f_vlan === '1') {
  1358. continue;
  1359. }
  1360. print_select_item($f_vlan, $f_vlan, $vlan);
  1361. }
  1362. print "</select>\n";
  1363. }
  1364. function print_device_select_ip($db, $field_name, $device_ip)
  1365. {
  1366. print "<select id=\"$field_name\" name=\"$field_name\" class=\"js-select-single\" >\n";
  1367. $d_sql = "SELECT D.device_name, D.ip FROM devices AS D Where D.deleted=0 order by D.device_name ASC";
  1368. $t_device = mysqli_query($db, $d_sql);
  1369. print_select_item(WEB_select_item_every, '', $device_ip);
  1370. while (list($f_name, $f_device_ip) = mysqli_fetch_array($t_device)) {
  1371. print_select_item($f_name, $f_device_ip, $device_ip);
  1372. }
  1373. print "</select>\n";
  1374. }
  1375. function print_syslog_device_select($db, $field_name, $syslog_filter, $device_ip)
  1376. {
  1377. print "<select id=\"$field_name\" name=\"$field_name\" class=\"js-select-single\" >\n";
  1378. $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";
  1379. $t_device = mysqli_query($db, $d_sql);
  1380. print_select_item(WEB_select_item_every, '', $device_ip);
  1381. while (list($f_ip, $f_name) = mysqli_fetch_array($t_device)) {
  1382. if (!isset($f_name) or empty($f_name)) {
  1383. $f_name = $f_ip;
  1384. }
  1385. print_select_item($f_name, $f_ip, $device_ip);
  1386. }
  1387. print "</select>\n";
  1388. }
  1389. function print_gateway_select($db, $field_name, $device_id)
  1390. {
  1391. print "<select id=\"$field_name\" name=\"$field_name\" >\n";
  1392. $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";
  1393. $t_device = mysqli_query($db, $d_sql);
  1394. print_select_item(WEB_select_item_every, 0, $device_id);
  1395. while (list($f_name, $f_device_id) = mysqli_fetch_array($t_device)) {
  1396. print_select_item($f_name, $f_device_id, $device_id);
  1397. }
  1398. print "</select>\n";
  1399. }
  1400. function get_gateways($db)
  1401. {
  1402. $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";
  1403. $t_device = mysqli_query($db, $d_sql);
  1404. unset($result);
  1405. while (list($f_name, $f_device_id) = mysqli_fetch_array($t_device)) {
  1406. $result[$f_device_id] = $f_name;
  1407. }
  1408. return $result;
  1409. }
  1410. function print_device_port($db, $target_id)
  1411. {
  1412. $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";
  1413. $t_device = mysqli_query($db, $d_sql);
  1414. while (list($f_name, $f_port, $f_device_id) = mysqli_fetch_array($t_device)) {
  1415. print "<a href=\"/admin/devices/switchport.php?id=$f_device_id\">" . $f_name . "[" . $f_port . "]</a>\n";
  1416. }
  1417. }
  1418. function get_device_ips($db, $device_id)
  1419. {
  1420. $switch = get_record($db, 'devices', 'id=' . $device_id);
  1421. $index = 0;
  1422. if (!empty($switch['user_id'])) {
  1423. $auth_ips = get_records($db, 'User_auth', 'deleted=0 and user_id=' . $switch['user_id']);
  1424. foreach ($auth_ips as $key => $value) {
  1425. if (isset($value['ip'])) {
  1426. $result[$index] = $value['ip'];
  1427. $index++;
  1428. }
  1429. }
  1430. } else {
  1431. if (isset($switch['ip'])) {
  1432. $result[$index] = $switch['ip'];
  1433. $index++;
  1434. }
  1435. }
  1436. return $result;
  1437. }
  1438. function get_device_id($db, $device_name)
  1439. {
  1440. $d_sql = "SELECT id FROM devices WHERE device_name='$device_name' and deleted=0";
  1441. $dev = get_record_sql($db, $d_sql);
  1442. if (empty($dev)) {
  1443. return NULL;
  1444. }
  1445. return $dev["id"];
  1446. }
  1447. function get_device_name($db, $device_id)
  1448. {
  1449. $d_sql = "SELECT device_name FROM devices WHERE id='$device_id'";
  1450. $dev = get_record_sql($db, $d_sql);
  1451. if (empty($dev)) {
  1452. return NULL;
  1453. }
  1454. return $dev["device_name"];
  1455. }
  1456. function get_auth_by_ip($db, $ip)
  1457. {
  1458. $d_sql = "SELECT id FROM User_auth WHERE ip='$ip' and deleted=0";
  1459. $auth = get_record_sql($db, $d_sql);
  1460. if (empty($auth)) {
  1461. return NULL;
  1462. }
  1463. return $auth["id"];
  1464. }
  1465. function get_user_by_ip($db, $ip)
  1466. {
  1467. $d_sql = "SELECT user_id FROM User_auth WHERE ip='$ip' and deleted=0";
  1468. $auth = get_record_sql($db, $d_sql);
  1469. if (empty($auth)) {
  1470. return NULL;
  1471. }
  1472. return $auth["user_id"];
  1473. }
  1474. function get_device_by_auth($db, $id)
  1475. {
  1476. $d_sql = "SELECT id FROM devices WHERE user_id=$id and deleted=0";
  1477. $f_dev = get_record_sql($db, $d_sql);
  1478. if (empty($f_dev)) {
  1479. return NULL;
  1480. }
  1481. return $f_dev['id'];
  1482. }
  1483. function print_auth_port($db, $port_id, $new_window = FALSE)
  1484. {
  1485. $d_sql = "SELECT A.ip, A.ip_int, A.mac, A.id, A.dns_name, A.user_id FROM User_auth as A, connections as C WHERE C.port_id=$port_id and A.id=C.auth_id and A.deleted=0 order by A.ip_int";
  1486. $t_auth = mysqli_query($db, $d_sql);
  1487. while (list($f_ip, $f_int, $f_mac, $f_auth_id, $f_dns, $f_user_id) = mysqli_fetch_array($t_auth)) {
  1488. $name = $f_ip;
  1489. if (!empty($f_dns)) {
  1490. $name = $f_dns;
  1491. }
  1492. $title = get_login($db, $f_user_id) . " =>" . $f_ip . "[" . $f_mac . "]";
  1493. if (!empty($f_dns)) {
  1494. $title .= " | " . $f_dns;
  1495. }
  1496. if ($new_window) {
  1497. print "<a href=\"\" title=\"" . $title . "\" onclick=\"" . open_window_url("/admin/users/editauth.php?id=" . $f_auth_id) . " return false;\">" . $name . " [" . $f_ip . "]</a><br>";
  1498. } else {
  1499. print "<a href=/admin/users/editauth.php?id=" . $f_auth_id . " title=\"" . $title . "\" >" . $name . " [" . $f_ip . "]</a><br>";
  1500. }
  1501. }
  1502. }
  1503. function get_port_comment($db, $port_id, $port_comment = '')
  1504. {
  1505. $d_sql = "SELECT A.ip_int, A.comments FROM User_auth as A, connections as C WHERE C.port_id=$port_id and A.id=C.auth_id and A.deleted=0 order by A.ip_int";
  1506. $t_auth = mysqli_query($db, $d_sql);
  1507. $comment_found = 0;
  1508. $result = '';
  1509. while (list($f_int, $f_comment) = mysqli_fetch_array($t_auth)) {
  1510. if (!empty($f_comment)) {
  1511. $comment_found = 1;
  1512. } else {
  1513. $f_comment = '';
  1514. }
  1515. $result .= $f_comment . '<br>';
  1516. }
  1517. if (!$comment_found) {
  1518. return $port_comment;
  1519. }
  1520. if (!empty($port_comment)) {
  1521. $result .= '(' . $port_comment . ')';
  1522. }
  1523. return $result;
  1524. }
  1525. function print_auth_simple($db, $auth_id)
  1526. {
  1527. $auth = get_record($db, "User_auth", "id=$auth_id");
  1528. $name = $auth['dns_name'];
  1529. if (empty($name)) {
  1530. $name = $auth['comments'];
  1531. }
  1532. if (empty($name)) {
  1533. $name = $auth['ip'];
  1534. }
  1535. print "<a href=\"/admin/users/editauth.php?id=$auth_id\">" . $name . "</a><br>";
  1536. }
  1537. function print_auth($db, $auth_id)
  1538. {
  1539. $auth = get_record($db, "User_auth", "id=$auth_id");
  1540. $name = $auth['dns_name'];
  1541. if (empty($name)) {
  1542. $name = $auth['comments'];
  1543. } else {
  1544. $name .= " (" . $auth['comments'] . ")";
  1545. }
  1546. if (empty($name)) {
  1547. $name = $auth['ip'];
  1548. } else {
  1549. $name .= " [" . $auth['ip'] . "]";
  1550. }
  1551. print "<a href=\"/admin/users/editauth.php?id=$auth_id\">" . $name . "</a><br>";
  1552. }
  1553. function print_auth_detail($db, $auth_id)
  1554. {
  1555. $auth = get_record($db, "User_auth", "id=$auth_id");
  1556. $name = $auth['dns_name'];
  1557. if (empty($name)) {
  1558. $name = $auth['comments'];
  1559. } else {
  1560. $name .= " (" . $auth['comments'] . ")";
  1561. }
  1562. if (empty($name)) {
  1563. $name = $auth['ip'];
  1564. } else {
  1565. $name .= " [" . $auth['ip'] . "]";
  1566. }
  1567. $name .= " last: [" . $auth['last_found'] . "] ";
  1568. if ($auth['deleted'] == 1) {
  1569. $name .= " <font color='red'>DELETED!!!</font>";
  1570. }
  1571. print "<a href=\"/admin/users/editauth.php?id=$auth_id\">" . $name . "</a><br>";
  1572. }
  1573. function get_auth_port_count($db, $port_id)
  1574. {
  1575. $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";
  1576. $t_device = mysqli_query($db, $d_sql);
  1577. list($f_count) = mysqli_fetch_array($t_device);
  1578. if (!isset($f_count)) {
  1579. $f_count = 0;
  1580. }
  1581. return $f_count;
  1582. }
  1583. function get_connection($db, $auth_id)
  1584. {
  1585. $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";
  1586. $t_device = mysqli_query($db, $d_sql);
  1587. list($f_name, $f_port) = mysqli_fetch_array($t_device);
  1588. if (isset($f_name)) {
  1589. $result = expand_device_name($db, $f_name) . "[" . $f_port . "]";
  1590. } else {
  1591. $result = '';
  1592. }
  1593. return $result;
  1594. }
  1595. function get_connection_string($db, $auth_id)
  1596. {
  1597. $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";
  1598. $t_device = mysqli_query($db, $d_sql);
  1599. list($f_name, $f_port) = mysqli_fetch_array($t_device);
  1600. if (isset($f_name)) {
  1601. $result = $f_name . "[" . $f_port . "]";
  1602. } else {
  1603. $result = '';
  1604. }
  1605. return $result;
  1606. }
  1607. function get_port($db, $port_id)
  1608. {
  1609. $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";
  1610. $t_device = mysqli_query($db, $d_sql);
  1611. list($f_name, $f_port) = mysqli_fetch_array($t_device);
  1612. if (isset($f_name)) {
  1613. $result = expand_device_name($db, $f_name) . "[" . $f_port . "]";
  1614. } else {
  1615. $result = '';
  1616. }
  1617. return $result;
  1618. }
  1619. function print_option_select($db, $option_name)
  1620. {
  1621. print "<select id=\"$option_name\" name=\"$option_name\">\n";
  1622. $t_option = mysqli_query($db, "SELECT id,option_name FROM config_options WHERE uniq=0 AND draft=0 order by option_name");
  1623. while (list($f_id, $f_name) = mysqli_fetch_array($t_option)) {
  1624. print "<option value=$f_id>$f_name</option>";
  1625. }
  1626. $t_option = mysqli_query($db, "SELECT id,option_name FROM config_options WHERE draft=0 AND uniq=1 AND id NOT IN (select option_id FROM config where draft=0) order by option_name");
  1627. while (list($f_id, $f_name) = mysqli_fetch_array($t_option)) {
  1628. print "<option value=$f_id>$f_name</option>";
  1629. }
  1630. print "</select>\n";
  1631. }
  1632. function ResolveIP($db, $ip_int)
  1633. {
  1634. $ip_name = "-";
  1635. if (empty($ip_int)) {
  1636. return $ip_name;
  1637. }
  1638. $dns_cache = get_record_sql($db, "SELECT * FROM dns_cache WHERE ip=$ip_int");
  1639. if (empty($dns_cache) or empty($dns_cache['dns'])) {
  1640. $ip_name = gethostbyaddr(long2ip($ip_int));
  1641. if (empty($ip_name) or $ip_name == long2ip($ip_int)) {
  1642. $ip_name = "-";
  1643. }
  1644. run_sql($db, "INSERT INTO dns_cache(dns,ip) VALUES('" . $ip_name . "'," . $ip_int . ")");
  1645. } else {
  1646. $ip_name = $dns_cache['dns'];
  1647. }
  1648. return $ip_name;
  1649. }
  1650. function clean_dns_cache($db)
  1651. {
  1652. $date = time();
  1653. $date = $date - 86400;
  1654. $date_clean = DateTimeImmutable::createFromFormat('U', $date);
  1655. $clean_date = $date_clean->format('Y-m-d H:i:s');
  1656. run_sql($db, "DELETE FROM dns_cache WHERE `timestamp`<='" . $clean_date . "'");
  1657. }
  1658. function clean_unreferensed_rules($db)
  1659. {
  1660. run_sql($db, "DELETE FROM `auth_rules` WHERE user_id NOT IN (SELECT id FROM User_list)");
  1661. }
  1662. function FormatDateStr($format = 'Y-m-d H:i:s', $date_str)
  1663. {
  1664. $date1 = GetDateTimeFromString($date_str);
  1665. $result = $date1->format($format);
  1666. return $result;
  1667. }
  1668. function GetDateTimeFromString($date_str)
  1669. {
  1670. if (!is_a($date_str, 'DateTime')) {
  1671. $t_date_str = urldecode($date_str);
  1672. $t_date_str = preg_replace('/(\'|\")/', '', $t_date_str);
  1673. $t_date_str = preg_replace('/T/', ' ', $t_date_str);
  1674. $date1 = DateTime::createFromFormat('Y-m-d H:i:s', $t_date_str);
  1675. if (!$date1) {
  1676. $date1 = DateTime::createFromFormat('Y.m.d H:i:s', $t_date_str);
  1677. }
  1678. if (!$date1) {
  1679. $date1 = DateTime::createFromFormat('Y/m/d H:i:s', $t_date_str);
  1680. }
  1681. if (!$date1) {
  1682. $date1 = DateTime::createFromFormat('Y-m-d H:i', $t_date_str);
  1683. }
  1684. if (!$date1) {
  1685. $date1 = DateTime::createFromFormat('Y.m.d H:i', $t_date_str);
  1686. }
  1687. if (!$date1) {
  1688. $date1 = DateTime::createFromFormat('Y/m/d H:i', $t_date_str);
  1689. }
  1690. if (!$date1) {
  1691. $date1 = DateTime::createFromFormat('Y-m-d|', $t_date_str);
  1692. }
  1693. if (!$date1) {
  1694. $date1 = DateTime::createFromFormat('Y.m.d|', $t_date_str);
  1695. }
  1696. if (!$date1) {
  1697. $date1 = DateTime::createFromFormat('Y/m/d|', $t_date_str);
  1698. }
  1699. if (!$date1) {
  1700. $date1 = new DateTime;
  1701. $date1->setTime(0, 0, 0, 1);
  1702. }
  1703. } else {
  1704. return $date_str;
  1705. }
  1706. return $date1;
  1707. }
  1708. function GetNowTimeString()
  1709. {
  1710. $now = new DateTimeImmutable('now');
  1711. $result = $now->format('Y-m-d H:i:s');
  1712. return $result;
  1713. }
  1714. function GetNowDayString()
  1715. {
  1716. $now = new DateTimeImmutable('now');
  1717. $result = $now->format('Y-m-d');
  1718. return $result;
  1719. }
  1720. function get_ip_subnet($db, $ip)
  1721. {
  1722. if (empty($ip)) {
  1723. return;
  1724. }
  1725. $ip_aton = ip2long($ip);
  1726. $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)");
  1727. if (empty($user_subnet)) {
  1728. return;
  1729. }
  1730. return $user_subnet;
  1731. }
  1732. function find_mac_in_subnet($db, $ip, $mac)
  1733. {
  1734. if (empty($ip)) {
  1735. return;
  1736. }
  1737. if (empty($mac)) {
  1738. return;
  1739. }
  1740. $ip_subnet = get_ip_subnet($db, $ip);
  1741. if (empty($ip_subnet)) {
  1742. return;
  1743. }
  1744. $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");
  1745. $auth_count = 0;
  1746. $result['count'] = 0;
  1747. $result['users_id'] = [];
  1748. foreach ($t_auth as $row) {
  1749. if (!empty($row['id'])) {
  1750. $auth_count++;
  1751. $result['count'] = $auth_count;
  1752. $result[$auth_count] = $row['id'];
  1753. array_push($result['users_id'], $row['user_id']);
  1754. }
  1755. }
  1756. return $result;
  1757. }
  1758. function apply_auth_rule($db, $auth_record, $user_id)
  1759. {
  1760. if (empty($auth_record)) {
  1761. return;
  1762. }
  1763. if (empty($user_id)) {
  1764. return $auth_record;
  1765. }
  1766. $user_rec = get_record($db, 'User_list', "id=" . $user_id);
  1767. if (empty($user_rec)) {
  1768. return $auth_record;
  1769. }
  1770. //set filter and status by user
  1771. $auth_record['ou_id'] = $user_rec['ou_id'];
  1772. $auth_record['user_id'] = $user_rec['id'];
  1773. $auth_record['filter_group_id'] = $user_rec['filter_group_id'];
  1774. $auth_record['queue_id'] = $user_rec['queue_id'];
  1775. $auth_record['enabled'] = $user_rec['enabled'];
  1776. $auth_record['changed'] = 1;
  1777. //maybe fill comments?
  1778. if (!empty($user_rec['fio']) and empty($auth_record['comments'])) {
  1779. $auth_record['comments'] = $user_rec['fio'];
  1780. }
  1781. return $auth_record;
  1782. }
  1783. function fix_auth_rules($db)
  1784. {
  1785. //cleanup hotspot subnet rules
  1786. delete_record($db, "auth_rules", "ou_id=" . get_const('default_user_ou_id'));
  1787. delete_record($db, "auth_rules", "ou_id=" . get_const('default_hotspot_ou_id'));
  1788. $t_hotspot = get_records_sql($db, "SELECT * FROM subnets WHERE hotspot=1");
  1789. if (!empty($t_hotspot)) {
  1790. foreach ($t_hotspot as $row) {
  1791. delete_record($db, "auth_rules", "rule='" . $row['subnet'] . "'");
  1792. }
  1793. }
  1794. }
  1795. #---------------------------------------------------------------------------------------------------------------
  1796. function new_user($db, $user_info)
  1797. {
  1798. if (!empty($user_info['mac'])) {
  1799. $user['login'] = mac_dotted($user_info['mac']);
  1800. } else {
  1801. $user['login'] = $user_info['ip'];
  1802. }
  1803. if (!empty($user_info['dhcp_hostname'])) {
  1804. $user['fio'] = $user_info['ip'] . '[' . $user_info['dhcp_hostname'] . ']';
  1805. } else {
  1806. $user['fio'] = $user_info['ip'];
  1807. }
  1808. $login_count = get_count_records($db, "User_list", "(login LIKE '" . $user['login'] . "(%)') OR (login='" . $user['login'] . "')");
  1809. if (!empty($login_count) and $login_count > 0) {
  1810. $login_count++;
  1811. $user['login'] = $user['login'] . "(" . $login_count . ")";
  1812. }
  1813. $user['ou_id'] = $user_info['ou_id'];
  1814. $ou_info = get_record_sql($db, "SELECT * FROM OU WHERE id=" . $user_info['ou_id']);
  1815. if (!empty($ou_info)) {
  1816. $user['enabled'] = $ou_info['enabled'];
  1817. if (empty($user['enabled'])) {
  1818. $user['enabled'] = 0;
  1819. }
  1820. $user['queue_id'] = $ou_info['queue_id'];
  1821. if (empty($user['queue_id'])) {
  1822. $user['queue_id'] = 0;
  1823. }
  1824. $user['filter_group_id'] = $ou_info['filter_group_id'];
  1825. if (empty($user['filter_group_id'])) {
  1826. $user['filter_group_id'] = 0;
  1827. }
  1828. }
  1829. $result = insert_record($db, "User_list", $user);
  1830. $auto_mac_rule = get_option($db, 64);
  1831. if (!empty($result) and $auto_mac_rule and $user_info['mac']) {
  1832. $auth_rule['user_id'] = $result;
  1833. $auth_rule['type'] = 2;
  1834. $auth_rule['rule'] = mac_dotted($user_info['mac']);
  1835. insert_record($db, "auth_rules", $auth_rule);
  1836. }
  1837. return $result;
  1838. }
  1839. function new_auth($db, $ip, $mac, $user_id)
  1840. {
  1841. $ip_aton = ip2long($ip);
  1842. $msg = '';
  1843. if (!empty($mac)) {
  1844. $auth_record = get_record_sql($db, "SELECT * FROM User_auth WHERE ip_int=$ip_aton AND mac='" . mac_dotted($mac) . "' AND deleted=0");
  1845. if (!empty($auth_record)) {
  1846. LOG_WARNING($db, "Pair ip-mac already exists! Skip creating $ip [$mac] auth_id: " . $auth_record["id"]);
  1847. return $auth_record['id'];
  1848. }
  1849. }
  1850. // save traffic detailization
  1851. $save_traf = get_option($db, 23);
  1852. $resurrection_id = NULL;
  1853. // seek old auth with same ip and mac
  1854. $resurrection_id = get_id_record($db, 'User_auth', " deleted=1 AND ip_int=" . $ip_aton . " AND mac='" . $mac . "'");
  1855. if (!empty($resurrection_id)) {
  1856. $msg .= "Recovered auth_id: $resurrection_id with ip: $ip and mac: $mac ";
  1857. $auth['user_id'] = $user_id;
  1858. $auth['deleted'] = 0;
  1859. $auth['save_traf'] = $save_traf * 1;
  1860. update_record($db, "User_auth", "id=$resurrection_id", $auth);
  1861. } else {
  1862. // not found ->create new record
  1863. $msg .= "Create new ip record \r\nip: $ip\r\nmac: $mac\r\n";
  1864. $auth['deleted'] = 0;
  1865. $auth['user_id'] = $user_id;
  1866. $auth['ip'] = $ip;
  1867. $auth['ip_int'] = $ip_aton;
  1868. $auth['mac'] = $mac;
  1869. $auth['save_traf'] = $save_traf * 1;
  1870. $resurrection_id = insert_record($db, "User_auth", $auth);
  1871. }
  1872. //check rules, update filter and state for new record
  1873. if (!empty($resurrection_id)) {
  1874. $auth = apply_auth_rule($db, $auth, $user_id);
  1875. update_record($db, "User_auth", "id=$resurrection_id", $auth);
  1876. if (!is_hotspot($db, $ip) and !empty($msg)) {
  1877. LOG_WARNING($db, $msg);
  1878. }
  1879. if (is_hotspot($db, $ip) and !empty($msg)) {
  1880. LOG_INFO($db, $msg);
  1881. }
  1882. }
  1883. return $resurrection_id;
  1884. }
  1885. function resurrection_auth($db, $ip_record)
  1886. {
  1887. $ip = $ip_record['ip'];
  1888. $mac = $ip_record['mac'];
  1889. $action = $ip_record['type'];
  1890. $dhcp_hostname = $ip_record['hostname'];
  1891. $hotspot_found = $ip_record['hotspot'];
  1892. $ip_aton = ip2long($ip);
  1893. $auth_record = get_record_sql($db, "SELECT * FROM User_auth WHERE ip_int=$ip_aton AND mac='" . $mac . "' AND deleted=0");
  1894. if (!empty($auth_record)) {
  1895. $user_info = get_record_sql($db, "SELECT * FROM User_list WHERE id=" . $auth_record['user_id']);
  1896. LOG_DEBUG($db, "external dhcp user " . $user_info['login'] . " [" . $ip . "] auth_id: " . $auth_record['id']);
  1897. if (isset($dhcp_hostname) and !empty($dhcp_hostname)) {
  1898. $auth['dhcp_hostname'] = $dhcp_hostname;
  1899. }
  1900. $auth['dhcp_action'] = $action;
  1901. $auth['dhcp_time'] = GetNowTimeString();
  1902. if ($action === 'add') {
  1903. $auth['last_found'] = GetNowTimeString();
  1904. }
  1905. update_record($db, "User_auth", "id=" . $auth_record['id'], $auth);
  1906. return $auth_record['id'];
  1907. }
  1908. $ip_subnet = get_ip_subnet($db, $ip);
  1909. if ($ip_subnet['static']) {
  1910. LOG_WARNING($db, "Unknown pair ip+mac in static subnet! ip: $ip mac: [" . mac_dotted($mac) . "]. Skip");
  1911. return;
  1912. }
  1913. $msg = '';
  1914. // search changed mac
  1915. $auth_record = get_record_sql($db, "SELECT * FROM User_auth WHERE ip_int=$ip_aton AND deleted=0");
  1916. if (!empty($auth_record)) {
  1917. if (empty($auth_record['mac'])) {
  1918. $auth['mac'] = mac_dotted($mac);
  1919. $auth['dhcp_action'] = $action;
  1920. $auth['dhcp_time'] = GetNowTimeString();
  1921. if (!empty($dhcp_hostname)) {
  1922. $auth['dhcp_hostname'] = $dhcp_hostname;
  1923. }
  1924. if ($action === 'add') {
  1925. $auth['last_found'] = GetNowTimeString();
  1926. }
  1927. LOG_INFO($db, "for ip: $ip mac not found! Use empty record...");
  1928. update_record($db, "User_auth", "id=" . $auth_record['id'], $auth);
  1929. return $auth_record['id'];
  1930. } else {
  1931. if (!$hotspot_found) {
  1932. 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']);
  1933. }
  1934. run_sql($db, "UPDATE User_auth SET changed=1, deleted=1 WHERE id=" . $auth_record['id']);
  1935. }
  1936. }
  1937. // default id
  1938. $new_user_info = get_new_user_id($db, $ip, $mac, $dhcp_hostname);
  1939. if (!empty($new_user_info['user_id'])) {
  1940. $new_user_id = $new_user_info['user_id'];
  1941. }
  1942. if (empty($new_user_id)) {
  1943. $new_user_id = new_user($db, $new_user_info);
  1944. }
  1945. $resurrection_id = NULL;
  1946. $save_traf = get_option($db, 23);
  1947. $auth_record = get_record_sql($db, "SELECT * FROM User_auth WHERE ip_int=" . $ip_aton . " and mac='" . $mac . "'");
  1948. // seek old auth with same ip and mac
  1949. if (!empty($auth_record)) {
  1950. // found ->Resurrection old record
  1951. $resurrection_id = $auth_record['id'];
  1952. $msg .= "Recovered auth_id: $resurrection_id with ip: $ip and mac: $mac ";
  1953. $auth['dhcp_action'] = $action;
  1954. $auth['user_id'] = $new_user_id;
  1955. $auth['deleted'] = 0;
  1956. $auth['dhcp_time'] = GetNowTimeString();
  1957. $auth['save_traf'] = $save_traf * 1;
  1958. if (!empty($dhcp_hostname)) {
  1959. $auth['dhcp_hostname'] = $dhcp_hostname;
  1960. }
  1961. if ($action === 'add') {
  1962. $auth['last_found'] = GetNowTimeString();
  1963. }
  1964. update_record($db, "User_auth", "id=$resurrection_id", $auth);
  1965. } else {
  1966. // not found ->create new record
  1967. $msg .= "Создаём новый ip-адрес \r\nip: $ip\r\nmac: $mac\r\n";
  1968. $auth['deleted'] = 0;
  1969. $auth['user_id'] = $new_user_id;
  1970. $auth['ip'] = $ip;
  1971. $auth['ip_int'] = $ip_aton;
  1972. $auth['mac'] = $mac;
  1973. $auth['dhcp_action'] = $action;
  1974. $auth['dhcp_time'] = GetNowTimeString();
  1975. $auth['save_traf'] = $save_traf * 1;
  1976. if (!empty($dhcp_hostname)) {
  1977. $auth['dhcp_hostname'] = $dhcp_hostname;
  1978. }
  1979. if ($action == 'add') {
  1980. $auth['last_found'] = GetNowTimeString();
  1981. }
  1982. $resurrection_id = insert_record($db, "User_auth", $auth);
  1983. }
  1984. //check rules, update filter and state for new record
  1985. if (!empty($resurrection_id)) {
  1986. $auth = apply_auth_rule($db, $auth, $new_user_id);
  1987. update_record($db, "User_auth", "id=$resurrection_id", $auth);
  1988. $msg .= "filter: " . $auth['filter_group_id'] . "\r\n queue_id: " . $auth['queue_id'] . "\r\n enabled: " . $auth['enabled'] . "\r\nid: $resurrection_id";
  1989. if (!$hotspot_found and !empty($msg)) {
  1990. LOG_WARNING($db, $msg);
  1991. }
  1992. if ($hotspot_found and !empty($msg)) {
  1993. LOG_INFO($db, $msg);
  1994. }
  1995. }
  1996. return $resurrection_id;
  1997. }
  1998. function get_auth($db, $current_auth)
  1999. {
  2000. if (!isset($current_auth)) {
  2001. return;
  2002. }
  2003. if ($current_auth == 0) {
  2004. return;
  2005. }
  2006. $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");
  2007. list($f_login, $f_ip) = mysqli_fetch_array($t_login);
  2008. $result = $f_login . "[" . $f_ip . "]";
  2009. return $result;
  2010. }
  2011. function get_auth_by_mac($db, $mac)
  2012. {
  2013. if (!isset($mac)) {
  2014. return;
  2015. }
  2016. $mac = mac_dotted($mac);
  2017. $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");
  2018. list($f_id, $f_login, $f_auth_id, $f_ip) = mysqli_fetch_array($t_login);
  2019. if (isset($f_id)) {
  2020. $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>';
  2021. } else {
  2022. $result['auth'] = 'Unknown';
  2023. }
  2024. $result['mac'] = expand_mac($db, $mac);
  2025. return $result;
  2026. }
  2027. function get_auth_mac($db, $current_auth)
  2028. {
  2029. if (!isset($current_auth)) {
  2030. return;
  2031. }
  2032. if ($current_auth == 0) {
  2033. return;
  2034. }
  2035. $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");
  2036. list($f_login, $f_mac) = mysqli_fetch_array($t_login);
  2037. $result = $f_login . "[" . $f_mac . "]";
  2038. return $result;
  2039. }
  2040. function add_auth_rule($db, $rule, $type, $user_id)
  2041. {
  2042. $new['user_id'] = $user_id;
  2043. $new['type'] = $type;
  2044. $new['rule'] = $rule;
  2045. $rule_id = 0;
  2046. $auth_rules = get_record_sql($db, "SELECT * FROM auth_rules WHERE rule='" . $rule . "' AND type=" . $type);
  2047. if (empty($auth_rules)) {
  2048. $rule_id = insert_record($db, "auth_rules", $new);
  2049. LOG_INFO($db, "Create auto rule for user_id: " . $user_id . " rule: " . $rule . " type: " . $type);
  2050. } else {
  2051. if ($auth_rules['user_id'] === $user_id) {
  2052. $rule_id = update_record($db, "auth_rules", "id=" . $auth_rules['id'], $new);
  2053. LOG_INFO($db, "Replaced auto rule for user_id: " . $user_id . " rule: " . $rule . " type: " . $type);
  2054. } else {
  2055. LOG_WARNING($db, "Create auto rule for user_id: " . $user_id . " rule: " . $rule . " type: " . $type . " failed! Already exists at user_id: " . $auth_rules['user_id']);
  2056. $rule_id = 0;
  2057. }
  2058. }
  2059. return $rule_id;
  2060. }
  2061. function update_auth_rule($db, $new, $rule_id = 0)
  2062. {
  2063. $type = $new['type'];
  2064. $rule = $new['rule'];
  2065. $auth_rules = get_record_sql($db, "SELECT * FROM auth_rules WHERE rule='" . $rule . "' AND type=" . $type . " AND id<>" . $rule_id);
  2066. if (empty($auth_rules)) {
  2067. $rule_id = update_record($db, "auth_rules", "id=" . $rule_id, $new);
  2068. } else {
  2069. LOG_WARNING($db, "Create auto rule id: " . $rule_id . " rule: " . $rule . " type: " . $type . " failed! Already exists at user_id: " . $auth_rules['user_id']);
  2070. $rule_id = 0;
  2071. }
  2072. return $rule_id;
  2073. }
  2074. function LOG_INFO($db, $msg, $auth_id = 0)
  2075. {
  2076. if (get_const('log_level') < L_INFO) {
  2077. return;
  2078. }
  2079. write_log($db, $msg, L_INFO, $auth_id);
  2080. }
  2081. function LOG_ERROR($db, $msg, $auth_id = 0)
  2082. {
  2083. if (get_const('log_level') < L_ERROR) {
  2084. return;
  2085. }
  2086. email(L_ERROR, $msg);
  2087. write_log($db, $msg, L_ERROR, $auth_id);
  2088. }
  2089. function LOG_VERBOSE($db, $msg, $auth_id = 0)
  2090. {
  2091. if (get_const('log_level') < L_VERBOSE) {
  2092. return;
  2093. }
  2094. write_log($db, $msg, L_VERBOSE, $auth_id);
  2095. }
  2096. function LOG_WARNING($db, $msg, $auth_id = 0)
  2097. {
  2098. if (get_const('log_level') < L_WARNING) {
  2099. return;
  2100. }
  2101. email(L_WARNING, $msg);
  2102. write_log($db, $msg, L_WARNING, $auth_id);
  2103. }
  2104. function LOG_DEBUG($db, $msg, $auth_id = 0)
  2105. {
  2106. if (!empty(get_const('debug')) and get_const('debug')) {
  2107. write_log($db, $msg, L_DEBUG, $auth_id);
  2108. }
  2109. }
  2110. function truncateByWords($string, $length = 100)
  2111. {
  2112. if (strlen($string) <= $length) {
  2113. return $string;
  2114. }
  2115. $wrapped = wordwrap($string, $length);
  2116. $shortened = substr($wrapped, 0, strpos($wrapped, "\n"));
  2117. return $shortened;
  2118. }
  2119. function get_first_line($msg)
  2120. {
  2121. if (empty($msg)) {
  2122. return;
  2123. }
  2124. preg_match('/(.*)(\n|\<br\>)/', $msg, $matches);
  2125. if (!empty($matches[1])) {
  2126. return $matches[1];
  2127. }
  2128. return truncateByWords($msg, 80);
  2129. }
  2130. function email($level, $msg)
  2131. {
  2132. if (!get_const('send_email')) {
  2133. return;
  2134. }
  2135. if (!($level === L_WARNING or $level === L_ERROR)) {
  2136. return;
  2137. }
  2138. // Generate a boundary string
  2139. $boundary = md5(time());
  2140. // Headers
  2141. $headers = array(
  2142. 'From' => get_const('sender_email'),
  2143. 'Reply-To' => get_const('sender_email'),
  2144. 'X-Mailer' => 'PHP',
  2145. 'MIME-Version' => '1.0',
  2146. 'Content-Type' => 'multipart/mixed; boundary=' . $boundary
  2147. );
  2148. $subject = get_first_line($msg);
  2149. if ($level === L_WARNING) {
  2150. $subject = "WARN: " . $subject . "...";
  2151. $message = 'WARNING! Manager: ' . $_SESSION['login'] . ' <br>' . $msg . '<br>';
  2152. }
  2153. if ($level === L_ERROR) {
  2154. $subject = "ERROR: " . $subject . "...";
  2155. $message = 'ERROR! Manager: ' . $_SESSION['login'] . ' <br>' . $msg . '<br>';
  2156. }
  2157. // HTML part
  2158. $html_message = "<html><body><h1>$message</h1></body></html>";
  2159. $html_encoded = chunk_split(base64_encode($html_message));
  2160. // Create the message body
  2161. $message = "";
  2162. $message .= "--" . $boundary . "\r\n";
  2163. $message .= "Content-Type: text/html; charset=UTF-8\r\n";
  2164. $message .= "Content-Transfer-Encoding: base64\r\n\r\n";
  2165. $message .= $html_encoded . "\r\n";
  2166. $message .= "--" . $boundary . "--";
  2167. // Send email
  2168. mail(get_const('admin_email'), $subject, $message, $headers);
  2169. }
  2170. function write_log($db, $msg, $level, $auth_id = 0)
  2171. {
  2172. $work_user = 'http';
  2173. if (isset($_SESSION['login'])) {
  2174. $work_user = $_SESSION['login'];
  2175. }
  2176. if (!isset($msg)) {
  2177. $msg = 'ERROR! Empty log string!';
  2178. }
  2179. if (!isset($level)) {
  2180. $level = L_INFO;
  2181. }
  2182. $msg = str_replace("'", '', $msg);
  2183. $sSQL = "insert into worklog(customer,message,level,auth_id) values('$work_user','$msg',$level,$auth_id)";
  2184. mysqli_query($db, $sSQL);
  2185. }
  2186. function print_year_select($year_name, $year)
  2187. {
  2188. print "<select id=\"$year_name\" name=\"$year_name\" >\n";
  2189. for ($i = $year - 10; $i <= $year + 10; $i++) {
  2190. print_select_item($i, $i, $year);
  2191. }
  2192. print "</select>\n";
  2193. }
  2194. function print_date_select($dd, $mm, $yy)
  2195. {
  2196. if ($dd >= 1) {
  2197. print "<b>День</b>\n";
  2198. print "<select id=\"day\" name=\"day\" >\n";
  2199. for ($i = 1; $i <= 31; $i++) {
  2200. print_select_item($i, $i, $dd);
  2201. }
  2202. print "</select>\n";
  2203. }
  2204. if ($mm >= 1) {
  2205. print "<b>Месяц</b>\n";
  2206. print "<select id=\"month\" name=\"month\" >\n";
  2207. for ($i = 1; $i <= 12; $i++) {
  2208. $tmp_date = DateTimeImmutable::createFromFormat('U', strtotime("$i/01/$yy"));
  2209. $month_name = $tmp_date->format('F');
  2210. print_select_item($month_name, $i, $mm);
  2211. }
  2212. print "</select>\n";
  2213. }
  2214. print "<b>Год</b>\n";
  2215. print_year_select('year', $yy);
  2216. }
  2217. function print_date_select2($dd, $mm, $yy)
  2218. {
  2219. if ($dd >= 1) {
  2220. print "<b>День</b>\n";
  2221. print "<select id=\"day2\" name=\"day2\" >\n";
  2222. for ($i = 1; $i <= 31; $i++) {
  2223. print_select_item($i, $i, $dd);
  2224. }
  2225. print "</select>\n";
  2226. }
  2227. if ($mm >= 1) {
  2228. print "<b>Месяц</b>\n";
  2229. print "<select id=\"month2\" name=\"month2\" >\n";
  2230. for ($i = 1; $i <= 12; $i++) {
  2231. $tmp_date = DateTimeImmutable::createFromFormat('U', strtotime("$i/01/$yy"));
  2232. $month_name = $tmp_date->format('F');
  2233. print_select_item($month_name, $i, $mm);
  2234. }
  2235. print "</select>\n";
  2236. }
  2237. print "<b>Год</b>\n";
  2238. print_year_select('year2', $yy);
  2239. }
  2240. function is_up($ip)
  2241. {
  2242. if (!isset($ip) or strlen($ip) == 0) {
  2243. return false;
  2244. }
  2245. exec(sprintf('ping -i .3 -c 1 -W 5 %s', escapeshellarg($ip)), $res, $rval);
  2246. return $rval == 0;
  2247. }
  2248. function apply_device_lock($db, $device_id, $iteration = 0)
  2249. {
  2250. $iteration++;
  2251. if ($iteration > 2) {
  2252. return false;
  2253. }
  2254. $dev = get_record_sql($db, 'SELECT discovery_locked,UNIX_TIMESTAMP(locked_timestamp) as u_locked_timestamp FROM devices WHERE id=' . $device_id . ' AND discovery_locked > 0');
  2255. if (empty($dev) or empty($dev['u_locked_timestamp'])) {
  2256. LOG_DEBUG($db, "Snmp discovery lock not found. Set and discovery.");
  2257. return set_lock_discovery($db, $device_id);
  2258. }
  2259. //wait for discovery
  2260. $now = time();
  2261. $wait_time = ($dev['u_locked_timestamp'] + SNMP_LOCK_TIMEOUT) - $now;
  2262. LOG_DEBUG($db, "Check snmp lock for device id: " . $device_id . ". Lock timestamp: " . $dev['u_locked_timestamp'] . ", now: " . $now);
  2263. if ($wait_time < 0) {
  2264. LOG_DEBUG($db, "The lock is already expired. Set new lock.");
  2265. return set_lock_discovery($db, $device_id);
  2266. }
  2267. LOG_VERBOSE($db, "Snmp discovery lock for device id: $device_id found! Need wait " . $wait_time . " sec.");
  2268. sleep($wait_time);
  2269. LOG_VERBOSE($db, "Try set new lock and continue discovery for device id:" . $device_id);
  2270. return apply_device_lock($db, $device_id, $iteration);
  2271. }
  2272. function set_lock_discovery($db, $device_id)
  2273. {
  2274. $new['discovery_locked'] = 1;
  2275. $new['locked_timestamp'] = GetNowTimeString();
  2276. if (update_record($db, 'devices', 'id=' . $device_id, $new)) {
  2277. return true;
  2278. }
  2279. return false;
  2280. }
  2281. function unset_lock_discovery($db, $device_id)
  2282. {
  2283. $new['discovery_locked'] = 0;
  2284. $new['locked_timestamp'] = GetNowTimeString();
  2285. if (update_record($db, 'devices', 'id=' . $device_id, $new)) {
  2286. return true;
  2287. }
  2288. return false;
  2289. }
  2290. function set_port_for_group($db, $group_id, $place_id, $state)
  2291. {
  2292. $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;
  2293. $auth_list = mysqli_query($db, $authSQL);
  2294. LOG_VERBOSE($db, 'Mass port state change started!');
  2295. // get auth list for group
  2296. while (list($a_id, $a_name, $a_ip) = mysqli_fetch_array($auth_list)) {
  2297. // get device and port for auth
  2298. if ($place_id == 0) {
  2299. $place_filter = '';
  2300. } else {
  2301. $place_filter = 'D.building_id=' . $place_id . ' and ';
  2302. }
  2303. $devSQL = 'SELECT D.id, D.device_name, D.vendor_id, D.device_model, D.ip, DP.port, DP.snmp_index FROM devices AS D, device_ports AS DP, connections AS C WHERE ' . $place_filter . ' D.id = DP.device_id AND DP.id = C.port_id AND C.auth_id=' . $a_id . ' LIMIT 1';
  2304. $dev_info = mysqli_query($db, $devSQL);
  2305. list($d_id, $d_name, $d_vendor_id, $d_model, $d_ip, $d_port, $d_snmp_index) = mysqli_fetch_array($dev_info);
  2306. if (!isset($d_id)) {
  2307. continue;
  2308. }
  2309. $device=get_record($db,'devices',"id=".$d_id);
  2310. $snmp = getSnmpAccess($device);
  2311. if ($state == 1) {
  2312. $mode = 'enable';
  2313. run_sql($db, "Update User_auth set nagios_handler='restart-port' WHERE id=$a_id and nagios_handler='manual-mode'");
  2314. } else {
  2315. $mode = 'disable';
  2316. run_sql($db, "Update User_auth set nagios_handler='manual-mode' WHERE id=$a_id and nagios_handler='restart-port'");
  2317. }
  2318. LOG_INFO($db, "At device $d_name [$d_ip] $mode port $d_port for auth_id: $a_id ($a_ip [$a_name])");
  2319. set_port_state($d_vendor_id, $d_snmp_index, $d_ip, $snmp, $state);
  2320. set_port_poe_state($d_vendor_id, $d_port, $d_snmp_index, $d_ip, $snmp, $state);
  2321. }
  2322. LOG_VERBOSE($db, 'Mass port state change stopped.');
  2323. }
  2324. function get_vendor($db, $mac)
  2325. {
  2326. $mac = mac_dotted($mac);
  2327. $mac5 = substr($mac, 0, 14);
  2328. $mac4 = substr($mac, 0, 11);
  2329. $mac3 = substr($mac, 0, 8);
  2330. $vendor = get_record_sql($db, 'SELECT companyName,companyAddress FROM mac_vendors WHERE oui="' . $mac . '"');
  2331. if (empty($vendor)) {
  2332. $vendor = get_record_sql($db, 'SELECT companyName,companyAddress FROM mac_vendors WHERE oui="' . $mac5 . '"');
  2333. }
  2334. if (empty($vendor)) {
  2335. $vendor = get_record_sql($db, 'SELECT companyName,companyAddress FROM mac_vendors WHERE oui="' . $mac4 . '"');
  2336. }
  2337. if (empty($vendor)) {
  2338. $vendor = get_record_sql($db, 'SELECT companyName,companyAddress FROM mac_vendors WHERE oui="' . $mac3 . '"');
  2339. }
  2340. $result = '';
  2341. if (!empty($vendor)) {
  2342. $result = $vendor['companyName'];
  2343. if (!empty($vendor['companyAddress'])) {
  2344. $result = $vendor['companyAddress'];
  2345. }
  2346. }
  2347. return $result;
  2348. }
  2349. function strHexToBin($number)
  2350. {
  2351. $result = '';
  2352. for ($i = 0; $i < strlen($number); $i++) {
  2353. $conv = base_convert($number[$i], 16, 2);
  2354. $result .= str_pad($conv, 4, '0', STR_PAD_LEFT);
  2355. }
  2356. return $result;
  2357. }
  2358. function dec_to_hex($mac)
  2359. {
  2360. if (!isset($mac)) {
  2361. return;
  2362. }
  2363. $mac_array = explode('.', $mac);
  2364. for ($i = 0; $i < count($mac_array); $i++) {
  2365. $hex_i = dechex($mac_array[$i]);
  2366. if (strlen($hex_i) == 1) {
  2367. $hex_i = "0" . $hex_i;
  2368. }
  2369. $mac_array[$i] = $hex_i;
  2370. }
  2371. $hex_mac = implode(':', $mac_array);
  2372. return $hex_mac;
  2373. }
  2374. function mac_simplify($mac)
  2375. {
  2376. if (!isset($mac)) {
  2377. return;
  2378. }
  2379. $mac = strtolower(trim($mac));
  2380. $mac = preg_replace('/(\.|:|-)/', '', $mac);
  2381. return $mac;
  2382. }
  2383. function mac_dotted($mac)
  2384. {
  2385. if (!isset($mac)) {
  2386. return;
  2387. }
  2388. $mac = mac_simplify($mac);
  2389. $mac = preg_replace('/(\S{2})(\S{2})?(\S{2})?(\S{2})?(\S{2})?(\S{2})?/', '$1:$2:$3:$4:$5:$6', $mac);
  2390. $mac = preg_replace('/\:+$/', '', $mac, 5);
  2391. return $mac;
  2392. }
  2393. function unbind_ports($db, $device_id)
  2394. {
  2395. $target = mysqli_query($db, "SELECT U.target_port_id,U.id FROM device_ports U WHERE U.device_id=$device_id");
  2396. while (list($target_id, $id) = mysqli_fetch_array($target)) {
  2397. run_sql($db, "UPDATE device_ports SET target_port_id=0 WHERE target_port_id=" . $id);
  2398. run_sql($db, "UPDATE device_ports SET target_port_id=0 WHERE id=" . $id);
  2399. }
  2400. }
  2401. function bind_ports($db, $port_id, $target_id)
  2402. {
  2403. $old_target = mysqli_query($db, "SELECT U.target_port_id FROM device_ports U WHERE U.id=$port_id");
  2404. list($old_target_id) = mysqli_fetch_array($old_target);
  2405. // unbind current connection
  2406. $new['target_port_id'] = 0;
  2407. update_record($db, "device_ports", "id='$port_id'", $new);
  2408. if (isset($old_target_id)) {
  2409. update_record($db, "device_ports", "id='$old_target_id'", $new);
  2410. }
  2411. // new link
  2412. if (isset($target_id) and $target_id > 0) {
  2413. $new['target_port_id'] = $target_id;
  2414. update_record($db, "device_ports", "id='$port_id'", $new);
  2415. $new['target_port_id'] = $port_id;
  2416. update_record($db, "device_ports", "id='$target_id'", $new);
  2417. }
  2418. }
  2419. function expand_device_name($db, $name)
  2420. {
  2421. $device_id = get_device_id($db, $name);
  2422. $result = $name;
  2423. if (isset($device_id) and $device_id > 0) {
  2424. $result = '<a href=/admin/devices/editdevice.php?id=' . $device_id . '>' . $name . '</a>';
  2425. }
  2426. return $result;
  2427. }
  2428. function expand_mac($db, $msg)
  2429. {
  2430. if (!isset($msg)) {
  2431. return;
  2432. }
  2433. $mac = mac_dotted($msg);
  2434. $vendor_info = get_vendor($db, $mac);
  2435. if (!empty($vendor_info)) {
  2436. // $result = '<p title="' . $vendor_info . '"><a href=/admin/logs/mac.php?mac=' . $mac . '>' . $mac . '</a></p>';
  2437. $result = '<a href=/admin/logs/mac.php?mac=' . $mac . '><p title="' . $vendor_info . '">'. $mac . '</p></a>';
  2438. } else {
  2439. $result = '<a href=/admin/logs/mac.php?mac=' . $mac . '>' . $mac . '</a>';
  2440. }
  2441. return $result;
  2442. }
  2443. function expand_log_str($db, $msg)
  2444. {
  2445. if (!isset($msg)) {
  2446. return;
  2447. }
  2448. $auth_pattern = '/(auth_id:|auth|auth id:|auth id)\s+(\d+)\s+/i';
  2449. $auth_replace = '<a href=/admin/users/editauth.php?id=${2}>auth_id:${2}</a> ';
  2450. $result = preg_replace($auth_pattern, $auth_replace, $msg);
  2451. $user_pattern = '/(user_id:|user|user id:|user id)\s+(\d+)\s+/i';
  2452. $user_replace = '<a href=/admin/users/edituser.php?id=${2}>user_id:${2}</a> ';
  2453. $result = preg_replace($user_pattern, $user_replace, $result);
  2454. $mac_pattern = '/\s+\[(\w{12})\]\s+/i';
  2455. preg_match($mac_pattern, $result, $matches);
  2456. if (isset($matches[1])) {
  2457. $mac = $matches[1];
  2458. $mac = mac_dotted($mac);
  2459. // $vendor_info = get_vendor($db,$mac);
  2460. // $mac_replace = ' <p title="'.$vendor_info.'"><a href=/admin/logs/mac.php?mac='.$mac.'>'.$mac.'</a></p>';
  2461. $mac_replace = ' <a href=/admin/logs/mac.php?mac=' . $mac . '>' . $mac . '</a> ';
  2462. $result = preg_replace($mac_pattern, $mac_replace, $result);
  2463. }
  2464. $mac_pattern = '/\s+mac:\s+([\w\:]{17})$/i';
  2465. preg_match($mac_pattern, $result, $matches);
  2466. if (isset($matches[1])) {
  2467. $mac = $matches[1];
  2468. $mac = mac_dotted($mac);
  2469. // $vendor_info = get_vendor($db,$mac);
  2470. // $mac_replace = ' mac: <p title="'.$vendor_info.'"><a href=/admin/logs/mac.php?mac='.$mac.'>'.$mac.'</a></p>';
  2471. $mac_replace = ' mac: <a href=/admin/logs/mac.php?mac=' . $mac . '>' . $mac . '</a> ';
  2472. $result = preg_replace($mac_pattern, $mac_replace, $result);
  2473. }
  2474. $device_pattern = '/at device\s+([\w\.\-]+)/i';
  2475. preg_match($device_pattern, $result, $matches);
  2476. if (isset($matches[1])) {
  2477. $device_name = $matches[1];
  2478. $device_id = get_device_id($db, $device_name);
  2479. if (isset($device_id) and $device_id > 0) {
  2480. $device_replace = 'at device <a href=/admin/devices/editdevice.php?id=' . $device_id . '>${1}</a> ';
  2481. $result = preg_replace($device_pattern, $device_replace, $result);
  2482. }
  2483. }
  2484. $device_pattern = '/(device_id:|device id:|device id|device_id)\s+(\d+)\s+/i';
  2485. $device_replace = 'device_id: <a href=/admin/devices/editdevice.php?id=${2}>${2}</a> ';
  2486. $result = preg_replace($device_pattern, $device_replace, $result);
  2487. $ip_pattern = '/\s+ip\:\s+(\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})\s+/i';
  2488. preg_match($ip_pattern, $result, $matches);
  2489. if (isset($matches[1])) {
  2490. $ip = $matches[1];
  2491. $auth_id = get_auth_by_ip($db, $ip);
  2492. if (isset($auth_id) and $auth_id > 0) {
  2493. $auth_replace = ' ip: <a href=/admin/users/editauth.php?id=' . $auth_id . '>${1}</a> ';
  2494. $result = preg_replace($ip_pattern, $auth_replace, $result);
  2495. }
  2496. }
  2497. return $result;
  2498. }
  2499. function is_auth_bind_changed($db, $id, $ip, $mac)
  2500. {
  2501. $old_sql = "SELECT ip,mac FROM User_auth WHERE id=$id";
  2502. $old_record = get_record_sql($db, $old_sql);
  2503. if (empty($old_record["ip"]) or empty($old_record["mac"])) {
  2504. return 0;
  2505. }
  2506. if ($old_record["ip"] !== $ip or $old_record["mac"] !== $mac) {
  2507. LOG_VERBOSE($db, "Changed ip or mac for auth record!");
  2508. return 1;
  2509. }
  2510. return 0;
  2511. }
  2512. function copy_auth($db, $id, $new_auth)
  2513. {
  2514. $old_record = get_record_sql($db, "SELECT * FROM User_auth WHERE id=$id");
  2515. delete_record($db, "User_auth", "id=" . $id);
  2516. $new_auth["user_id"] = $old_record["user_id"];
  2517. $new_auth["changed"] = 1;
  2518. $changed_time = GetNowTimeString();
  2519. $new_auth["changed_time"] = $changed_time;
  2520. $new_id = insert_record($db, "User_auth", $new_auth);
  2521. LOG_VERBOSE($db, "Old record with id: $id deleted. Created new auth record for new ip+mac id: $new_id!");
  2522. return $new_id;
  2523. }
  2524. function get_dns_name($db, $id)
  2525. {
  2526. $auth_record = get_record_sql($db, "SELECT dns_name FROM User_auth WHERE id=" . $id);
  2527. if (!empty($auth_record) and !empty($auth_record['dns_name'])) {
  2528. return $auth_record['dns_name'];
  2529. }
  2530. return '';
  2531. }
  2532. function get_cacti_graph($host_ip, $port_index)
  2533. {
  2534. if (empty(get_const('cacti_url'))) {
  2535. return;
  2536. }
  2537. if (CACTI_DB_HOST == null or CACTI_DB_USER == null or CACTI_DB_PASS == null or CACTI_DB_NAME == null) {
  2538. return;
  2539. }
  2540. if (empty(CACTI_DB_HOST) or empty(CACTI_DB_USER) or empty(CACTI_DB_PASS) or empty(CACTI_DB_NAME)) {
  2541. return;
  2542. }
  2543. $cacti_db_link = new_connection(CACTI_DB_HOST, CACTI_DB_USER, CACTI_DB_PASS, CACTI_DB_NAME);
  2544. if (!$cacti_db_link) {
  2545. return FALSE;
  2546. }
  2547. $host_sql = 'SELECT * FROM host WHERE hostname="' . $host_ip . '"';
  2548. $cacti_host = get_record_sql($cacti_db_link, $host_sql);
  2549. $host_id = $cacti_host["id"];
  2550. if (empty($host_id)) {
  2551. return;
  2552. }
  2553. $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';
  2554. $cacti_graph = get_record_sql($cacti_db_link, $graph_sql);
  2555. $graph_id = $cacti_graph["id"];
  2556. if (empty($graph_id)) {
  2557. return;
  2558. }
  2559. $result = get_const('cacti_url') . "/graph_image.php?local_graph_id=" . $graph_id;
  2560. return $result;
  2561. }
  2562. function print_select_item($description, $value, $current)
  2563. {
  2564. if ((string)$value === (string)$current) {
  2565. print "<option value='" . $value . "' selected>$description</option>";
  2566. } else {
  2567. print "<option value='" . $value . "'>$description</option>";
  2568. }
  2569. }
  2570. function print_select_simple($description, $value)
  2571. {
  2572. print "<option value=$value>$description</option>";
  2573. }
  2574. function print_select_item_ext($description, $value, $current, $disabled)
  2575. {
  2576. if ((string)$value === (string)$current) {
  2577. print "<option value=$value selected>$description</option>";
  2578. } else {
  2579. if (!$disabled) {
  2580. print "<option value=$value>$description</option>";
  2581. } else {
  2582. print "<option disabled value=$value>$description</option>";
  2583. }
  2584. }
  2585. }
  2586. function print_row_at_pages($name, $value)
  2587. {
  2588. print "<select id='" . $name . "' name='" . $name . "'>\n";
  2589. print_select_item(WEB_select_item_more, pow(10, 10), $value);
  2590. print_select_item('25', 25, $value);
  2591. print_select_item('50', 50, $value);
  2592. print_select_item('100', 100, $value);
  2593. print_select_item('200', 200, $value);
  2594. print_select_item('500', 500, $value);
  2595. print_select_item('1000', 1000, $value);
  2596. print_select_item('2000', 2000, $value);
  2597. print "</select>\n";
  2598. }
  2599. function print_navigation($url, $page, $displayed, $count_records, $total)
  2600. {
  2601. if ($total <= 1) {
  2602. print "<div align=left class=records >";
  2603. print "| Total records: $count_records";
  2604. print "</div>";
  2605. return;
  2606. }
  2607. $v_char = "?";
  2608. if (preg_match('/\.php\?/', $url)) {
  2609. $v_char = "&";
  2610. }
  2611. //две назад
  2612. print "<div align=left class=records >";
  2613. if (($page - 2) > 0) :
  2614. $pagetwoleft = "<a class='first_page_link' href=" . $url . $v_char . "page=" . ($page - 2) . ">" . ($page - 2) . "</a> ";
  2615. else :
  2616. $pagetwoleft = null;
  2617. endif;
  2618. //одна назад
  2619. if (($page - 1) > 0) :
  2620. $pageoneleft = "<a class='first_page_link' href=" . $url . $v_char . "page=" . ($page - 1) . ">" . ($page - 1) . "</a> ";
  2621. $pagetemp = ($page - 1);
  2622. else :
  2623. $pageoneleft = null;
  2624. $pagetemp = null;
  2625. endif;
  2626. //две вперед
  2627. if (($page + 2) <= $total) :
  2628. $pagetworight = " <a class='first_page_link' href=" . $url . $v_char . "page=" . ($page + 2) . ">" . ($page + 2) . "</a>";
  2629. else :
  2630. $pagetworight = null;
  2631. endif;
  2632. //одна вперед
  2633. if (($page + 1) <= $total) :
  2634. $pageoneright = " <a class='first_page_link' href=" . $url . $v_char . "page=" . ($page + 1) . ">" . ($page + 1) . "</a>";
  2635. $pagetemp2 = ($page + 1);
  2636. else :
  2637. $pageoneright = null;
  2638. $pagetemp2 = null;
  2639. endif;
  2640. // в начало
  2641. if ($page != 1 && $pagetemp != 1 && $pagetemp != 2) :
  2642. $pagerevp = "<a href=" . $url . $v_char . "page=1 class='first_page_link' title='В начало'><<</a> ";
  2643. else :
  2644. $pagerevp = null;
  2645. endif;
  2646. //в конец (последняя)
  2647. if ($page != $total && $pagetemp2 != ($total - 1) && $pagetemp2 != $total) :
  2648. $nextp = " ... <a href=" . $url . $v_char . "page=" . $total . " class='first_page_link'>$total</a>";
  2649. else :
  2650. $nextp = null;
  2651. endif;
  2652. print $pagerevp . $pagetwoleft . $pageoneleft . '<span class="num_page_not_link"><b>' . $page . '</b></span>' . $pageoneright . $pagetworight . $nextp;
  2653. print " | Total records: $count_records";
  2654. print "</div>";
  2655. }
  2656. function get_option($db, $option_id)
  2657. {
  2658. $option = get_record($db, "config", "option_id=" . $option_id);
  2659. if (empty($option) or empty($option['value'])) {
  2660. $default = get_record($db, "config_options", "id=$option_id");
  2661. return $default['default_value'];
  2662. }
  2663. return $option['value'];
  2664. }
  2665. function is_option($db, $option_id)
  2666. {
  2667. $option = get_record($db, "config", "option_id=" . $option_id);
  2668. if (empty($option) or empty($option['value'])) {
  2669. return;
  2670. }
  2671. return 1;
  2672. }
  2673. function set_option($db, $option_id, $value)
  2674. {
  2675. $option['value'] = $value;
  2676. update_record($db, 'config', "option_id=$option_id", $option);
  2677. }
  2678. function is_subnet_aton($subnet, $ip)
  2679. {
  2680. if (!isset($subnet)) {
  2681. return 0;
  2682. }
  2683. if (!isset($ip)) {
  2684. return 0;
  2685. }
  2686. $range = cidrToRange($subnet);
  2687. if ($ip >= ip2long($range[0]) and $ip <= ip2long($range[1])) {
  2688. return 1;
  2689. }
  2690. return 0;
  2691. }
  2692. function get_new_user_id($db, $ip, $mac, $hostname)
  2693. {
  2694. $result['ip'] = $ip;
  2695. $result['mac'] = mac_dotted($mac);
  2696. $result['hostname'] = $hostname;
  2697. $result['user_id'] = NULL;
  2698. $result['ou_id'] = NULL;
  2699. $ip_aton = ip2long($ip);
  2700. //personal user rules
  2701. //ip
  2702. if (!empty($ip)) {
  2703. $t_rules = get_records_sql($db, "SELECT * FROM auth_rules WHERE type=1 and LENGTH(rule)>0 AND user_id IS NOT NULL");
  2704. foreach ($t_rules as $row) {
  2705. if (!empty($row['rule']) and is_subnet_aton($row['rule'], $ip_aton)) {
  2706. $result['user_id'] = $row['user_id'];
  2707. }
  2708. }
  2709. }
  2710. //mac
  2711. if (!empty($mac)) {
  2712. $mac_rules = get_records_sql($db, "SELECT * FROM auth_rules WHERE type=2 AND LENGTH(rule)>0 AND user_id IS NOT NULL");
  2713. foreach ($mac_rules as $row) {
  2714. $pattern = '/' . mac_simplify($row['rule']) . '/';
  2715. if (!empty($row['rule']) and preg_match($pattern, mac_simplify($mac))) {
  2716. $result['user_id'] = $row['user_id'];
  2717. }
  2718. }
  2719. }
  2720. //hostname
  2721. if (!empty($hostname)) {
  2722. $mac_rules = get_records_sql($db, "SELECT * FROM auth_rules WHERE type=3 AND LENGTH(rule)>0 AND user_id IS NOT NULL");
  2723. foreach ($mac_rules as $row) {
  2724. if (!empty($row['rule']) and preg_match($row['rule'], $hostname)) {
  2725. $result['user_id'] = $row['user_id'];
  2726. }
  2727. }
  2728. }
  2729. if (!empty($result['user_id'])) {
  2730. return $result;
  2731. }
  2732. //ou rules
  2733. //ip
  2734. if (!empty($ip)) {
  2735. if (is_hotspot($db, $ip)) {
  2736. $result['ou_id'] = get_const('default_hotspot_ou_id');
  2737. }
  2738. $t_rules = get_records_sql($db, "SELECT * FROM auth_rules WHERE type=1 and LENGTH(rule)>0 AND ou_id IS NOT NULL");
  2739. foreach ($t_rules as $row) {
  2740. if (!empty($row['rule']) and is_subnet_aton($row['rule'], $ip_aton)) {
  2741. $result['ou_id'] = $row['ou_id'];
  2742. }
  2743. }
  2744. }
  2745. //mac
  2746. if (!empty($mac)) {
  2747. $mac_rules = get_records_sql($db, "SELECT * FROM auth_rules WHERE type=2 AND LENGTH(rule)>0 AND ou_id IS NOT NULL");
  2748. foreach ($mac_rules as $row) {
  2749. $pattern = '/' . mac_simplify($row['rule']) . '/';
  2750. if (!empty($row['rule']) and preg_match($pattern, mac_simplify($mac))) {
  2751. $result['ou_id'] = $row['ou_id'];
  2752. }
  2753. }
  2754. }
  2755. //hostname
  2756. if (!empty($hostname)) {
  2757. $mac_rules = get_records_sql($db, "SELECT * FROM auth_rules WHERE type=3 AND LENGTH(rule)>0 AND ou_id IS NOT NULL");
  2758. foreach ($mac_rules as $row) {
  2759. if (!empty($row['rule']) and preg_match($row['rule'], $hostname)) {
  2760. $result['ou_id'] = $row['ou_id'];
  2761. }
  2762. }
  2763. }
  2764. if (empty($result['ou_id'])) {
  2765. $result['ou_id'] = get_const('default_user_ou_id');
  2766. }
  2767. return $result;
  2768. }
  2769. function get_subnet_range($db, $subnet_id)
  2770. {
  2771. if (empty($subnet_id)) {
  2772. return;
  2773. }
  2774. $t_option = get_record_sql($db, "SELECT ip_int_start,ip_int_stop FROM `subnets` WHERE id=$subnet_id");
  2775. if (!isset($t_option['ip_int_start'])) {
  2776. $t_option['ip_int_start'] = 0;
  2777. }
  2778. if (!isset($t_option['ip_int_stop'])) {
  2779. $t_option['ip_int_stop'] = 0;
  2780. }
  2781. $subnet['start'] = $t_option['ip_int_start'];
  2782. $subnet['stop'] = $t_option['ip_int_stop'];
  2783. return $subnet;
  2784. }
  2785. function int_between($value, $start, $end)
  2786. {
  2787. return in_array($value, range($start, $end));
  2788. }
  2789. function is_gray_network($ip)
  2790. {
  2791. if (empty($ip)) {
  2792. return 0;
  2793. }
  2794. $ip_aton = ip2long($ip);
  2795. $gray_nets = array('10.0.0.0/8', '192.168.0.0/16', '172.16.0.0/12', '100.64.0.0/10');
  2796. foreach ($gray_nets as &$net) {
  2797. $net_cidr = cidrToRange($net);
  2798. if (int_between($ip_aton, ip2long($net_cidr[0]), ip2long($net_cidr[1]))) {
  2799. return $net;
  2800. }
  2801. }
  2802. return 0;
  2803. }
  2804. function is_hotspot($db, $ip)
  2805. {
  2806. if (!isset($ip)) {
  2807. return 0;
  2808. }
  2809. LOG_DEBUG($db, "Check hotspot network for ip: $ip");
  2810. $ip_aton = ip2long($ip);
  2811. $t_option = mysqli_query($db, "SELECT subnet,ip_int_start,ip_int_stop FROM `subnets` WHERE hotspot=1");
  2812. while (list($f_net, $f_start, $f_stop) = mysqli_fetch_array($t_option)) {
  2813. if ($ip_aton >= $f_start and $ip_aton <= $f_stop) {
  2814. LOG_DEBUG($db, "ip: $ip [$ip_aton] found in network $f_net: [" . $f_start . ".." . $f_stop . "]");
  2815. return 1;
  2816. }
  2817. }
  2818. LOG_DEBUG($db, "ip $ip not found in hotspot network!");
  2819. return 0;
  2820. }
  2821. function is_office($db, $ip)
  2822. {
  2823. if (!isset($ip)) {
  2824. return 0;
  2825. }
  2826. LOG_DEBUG($db, "Check office network for ip: $ip");
  2827. $ip_aton = ip2long($ip);
  2828. $t_option = mysqli_query($db, "SELECT subnet,ip_int_start,ip_int_stop FROM `subnets` WHERE office=1");
  2829. while (list($f_net, $f_start, $f_stop) = mysqli_fetch_array($t_option)) {
  2830. if ($ip_aton >= $f_start and $ip_aton <= $f_stop) {
  2831. LOG_DEBUG($db, "ip: $ip [$ip_aton] found in office $f_net: [" . $f_start . ".." . $f_stop . "]");
  2832. return 1;
  2833. }
  2834. }
  2835. LOG_DEBUG($db, "ip $ip not found in office network!");
  2836. return 0;
  2837. }
  2838. function is_our_network($db, $ip)
  2839. {
  2840. if (!isset($ip)) {
  2841. return 0;
  2842. }
  2843. if (is_hotspot($db, $ip)) {
  2844. return 1;
  2845. }
  2846. if (is_office($db, $ip)) {
  2847. return 1;
  2848. }
  2849. return 0;
  2850. }
  2851. function get_const($const_name)
  2852. {
  2853. global $config;
  2854. if (isset($config[$const_name])) {
  2855. return $config[$const_name];
  2856. }
  2857. return NULL;
  2858. }
  2859. function get_eye_version($db)
  2860. {
  2861. $v_table = get_record_sql($db, "SELECT version FROM version");
  2862. if (!empty($v_table)) {
  2863. return $v_table['version'];
  2864. }
  2865. return NULL;
  2866. }
  2867. $config["org_name"] = get_option($db_link, 32);
  2868. $config["version"] = get_eye_version($db_link);
  2869. $config["KB"] = get_option($db_link, 1);
  2870. if ($config["KB"] == 0) {
  2871. $config["KB"] = 1000;
  2872. }
  2873. if ($config["KB"] == 1) {
  2874. $config["KB"] = 1024;
  2875. }
  2876. $config["debug"] = get_option($db_link, 34);
  2877. $config["log_level"] = get_option($db_link, 53);
  2878. if ($config["debug"]) {
  2879. $config["log_level"] = 255;
  2880. }
  2881. $config["send_email"] = get_option($db_link, 51);
  2882. $config["admin_email"] = get_option($db_link, 21);
  2883. $config["sender_email"] = get_option($db_link, 52);
  2884. $config["snmp_default_version"] = get_option($db_link, 9);
  2885. $config["snmp_default_community"] = get_option($db_link, 11);
  2886. $config["auto_mac_rule"] = get_option($db_link, 64);
  2887. $config["cacti_url"] = rtrim(get_option($db_link, 58), '/');
  2888. if (preg_match('/127.0.0.1/', $config["cacti_url"])) {
  2889. $config["cacti_url"] = NULL;
  2890. }
  2891. $config["nagios_url"] = rtrim(get_option($db_link, 57), '/') . '/cgi-bin/';
  2892. if (preg_match('/127.0.0.1/', $config["nagios_url"])) {
  2893. $config["nagios_url"] = NULL;
  2894. }
  2895. $config["torrus_url"] = rtrim(get_option($db_link, 59), '/') . '?nodeid=if//HOST_IP//IF_NAME////inoutbps';
  2896. if (preg_match('/127.0.0.1/', $config["torrus_url"])) {
  2897. $config["torrus_url"] = NULL;
  2898. }
  2899. $config["dns_server"] = get_option($db_link, 3);
  2900. $config["dns_server_type"] = get_option($db_link, 70);
  2901. $ou = get_record_sql($db_link, "SELECT id FROM OU WHERE default_users = 1");
  2902. if (empty($ou)) {
  2903. $config["default_user_ou_id"] = 0;
  2904. } else {
  2905. $config["default_user_ou_id"] = $ou['id'];
  2906. }
  2907. $ou = get_record_sql($db_link, "SELECT id FROM OU WHERE default_hotspot=1");
  2908. if (empty($ou)) {
  2909. $config["default_hotspot_ou_id"] = $config["default_user_ou_id"];
  2910. } else {
  2911. $config["default_hotspot_ou_id"] = $ou['id'];
  2912. }
  2913. $config["init"] = 1;
  2914. clean_dns_cache($db_link);
  2915. //clean_unreferensed_rules($db_link);