common.php 123 KB

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