common.php 127 KB

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