common.php 123 KB

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