common.php 126 KB

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