common.php 124 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933
  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_devmodels_select($db, $devmodel_name, $devmodel_value)
  691. {
  692. print "<select name=\"$devmodel_name\">\n";
  693. print_select_item(WEB_select_item_all, -1, $devmodel_value);
  694. $t_devmodel = mysqli_query($db, "SELECT M.id,V.name,M.model_name FROM device_models M,vendors V WHERE M.vendor_id = V.id and M.id in (SELECT device_model_id FROM devices WHERE device_type<=2) ORDER BY V.name,M.model_name");
  695. while (list($f_devmodel_id, $f_devmodel_vendor, $f_devmodel_name) = mysqli_fetch_array($t_devmodel)) {
  696. print_select_item($f_devmodel_vendor." ".$f_devmodel_name, $f_devmodel_id, $devmodel_value);
  697. }
  698. print "</select>\n";
  699. }
  700. function print_devtypes_select($db, $devtype_name, $devtype_value, $mode)
  701. {
  702. print "<select name=\"$devtype_name\">\n";
  703. print_select_item(WEB_select_item_all, -1, $devtype_value);
  704. $filter = '';
  705. if (!empty($mode)) {
  706. $filter = "WHERE $mode";
  707. }
  708. $t_devtype = mysqli_query($db, "SELECT id,`name.".HTML_LANG."` FROM device_types $filter ORDER BY `name.".HTML_LANG."`");
  709. while (list($f_devtype_id, $f_devtype_name) = mysqli_fetch_array($t_devtype)) {
  710. print_select_item($f_devtype_name, $f_devtype_id, $devtype_value);
  711. }
  712. print "</select>\n";
  713. }
  714. function print_devtype_select($db, $devtype_name, $devtype_value)
  715. {
  716. print "<select name=\"$devtype_name\">\n";
  717. $t_devtype = mysqli_query($db, "SELECT id,`name.".HTML_LANG."` FROM device_types ORDER BY `name.".HTML_LANG."`");
  718. while (list($f_devtype_id, $f_devtype_name) = mysqli_fetch_array($t_devtype)) {
  719. print_select_item($f_devtype_name, $f_devtype_id, $devtype_value);
  720. }
  721. print "</select>\n";
  722. }
  723. function get_group($db, $group_value)
  724. {
  725. list($group_name) = mysqli_fetch_array(mysqli_query($db, "SELECT group_name FROM Group_list WHERE id=$group_value"));
  726. return $group_name;
  727. }
  728. function get_devtype_name($db, $device_type_id)
  729. {
  730. list($type_name) = mysqli_fetch_array(mysqli_query($db, "SELECT `name.".HTML_LANG."` FROM device_types WHERE id=$device_type_id"));
  731. return $type_name;
  732. }
  733. function get_l3_interfaces($db, $device_id)
  734. {
  735. $wan = '';
  736. $lan = '';
  737. $t_l3int = mysqli_query($db, "SELECT name,interface_type FROM device_l3_interfaces WHERE device_id=$device_id ORDER BY name");
  738. while (list($f_name, $f_type) = mysqli_fetch_array($t_l3int)) {
  739. if ($f_type == 0) {
  740. $lan = $lan . " " . $f_name;
  741. }
  742. if ($f_type == 1) {
  743. $wan = $wan . " " . $f_name;
  744. }
  745. }
  746. $wan = trim($wan);
  747. $lan = trim($lan);
  748. $result = '';
  749. if (!empty($wan)) {
  750. $result .= ' WAN: ' . $wan . '<br>';
  751. }
  752. if (!empty($lan)) {
  753. $result .= ' LAN: ' . $lan;
  754. }
  755. return trim($result);
  756. }
  757. function print_queue_select($db, $queue_name, $queue_value)
  758. {
  759. print "<select name=\"$queue_name\">\n";
  760. $t_queue = mysqli_query($db, "SELECT id,queue_name FROM Queue_list Order by queue_name");
  761. while (list($f_queue_id, $f_queue_name) = mysqli_fetch_array($t_queue)) {
  762. print_select_item($f_queue_name, $f_queue_id, $queue_value);
  763. }
  764. print "</select>\n";
  765. }
  766. function get_queue($db, $queue_value)
  767. {
  768. list($queue_name) = mysqli_fetch_array(mysqli_query($db, "SELECT queue_name FROM Queue_list WHERE id=$queue_value"));
  769. return $queue_name;
  770. }
  771. function print_qa_l3int_select($qa_name, $qa_value)
  772. {
  773. print "<select name=\"$qa_name\">\n";
  774. print_select_item(WEB_select_item_lan, 0, $qa_value);
  775. print_select_item(WEB_select_item_wan, 1, $qa_value);
  776. print "</select>\n";
  777. }
  778. function print_qa_rule_select($qa_name, $qa_value)
  779. {
  780. print "<select name=\"$qa_name\">\n";
  781. print_select_item('Subnet', 1, $qa_value);
  782. print_select_item('Mac', 2, $qa_value);
  783. print_select_item('Hostname', 3, $qa_value);
  784. print "</select>\n";
  785. }
  786. function print_qa_select($qa_name, $qa_value)
  787. {
  788. print "<select name=\"$qa_name\">\n";
  789. print_select_item(WEB_select_item_yes, 1, $qa_value);
  790. print_select_item(WEB_select_item_no, 0, $qa_value);
  791. print "</select>\n";
  792. }
  793. function print_qa_select_ext($qa_name, $qa_value, $readonly)
  794. {
  795. $state = '';
  796. if ($readonly) {
  797. $state = 'disabled=true';
  798. }
  799. print "<select name=\"$qa_name\">\n";
  800. print_select_item_ext(WEB_select_item_yes, 1, $qa_value, $readonly);
  801. print_select_item_ext(WEB_select_item_no, 0, $qa_value, $readonly);
  802. print "</select>\n";
  803. }
  804. function print_control_proto_select($qa_name, $qa_value)
  805. {
  806. print "<select name=\"$qa_name\">\n";
  807. print_select_item('Disabled', -1, $qa_value);
  808. print_select_item('Ssh', 0, $qa_value);
  809. print_select_item('Telnet', 1, $qa_value);
  810. // print_select_item('Mikrotik rest api', 2, $qa_value);
  811. print "</select>\n";
  812. }
  813. function print_snmp_select($qa_name, $qa_value)
  814. {
  815. print "<select name=\"$qa_name\">\n";
  816. print_select_item('Disabled', 0, $qa_value);
  817. print_select_item('v1', 1, $qa_value);
  818. print_select_item('v2', 2, $qa_value);
  819. print_select_item('v3', 3, $qa_value);
  820. print "</select>\n";
  821. }
  822. function print_dhcp_select($qa_name, $qa_value)
  823. {
  824. print "<select name=\"$qa_name\">\n";
  825. if (!isset($qa_value) or strlen($qa_value) == 0) {
  826. $qa_value = 'all';
  827. }
  828. print_select_item(WEB_select_item_events, 'all', $qa_value);
  829. print_select_item(WEB_select_item_lease, 'add', $qa_value);
  830. print_select_item(WEB_select_item_lease_refresh, 'old', $qa_value);
  831. print_select_item(WEB_select_item_lease_free, 'del', $qa_value);
  832. print "</select>\n";
  833. }
  834. function print_nagios_handler_select($qa_name)
  835. {
  836. print "<select name=\"$qa_name\">\n";
  837. print_select_simple(WEB_select_item_no, '');
  838. print_select_simple('restart-port', 'restart-port');
  839. print "</select>\n";
  840. }
  841. function print_dhcp_acl_select($qa_name)
  842. {
  843. print "<select name=\"$qa_name\">\n";
  844. print_select_simple(WEB_select_item_no, '');
  845. print_select_simple('hotspot-free', 'hotspot-free');
  846. print "</select>\n";
  847. }
  848. function print_enabled_select($qa_name, $qa_value)
  849. {
  850. print "<select name=\"$qa_name\">\n";
  851. if (!isset($qa_value) or strlen($qa_value) == 0) {
  852. $qa_value = 0;
  853. }
  854. print_select_item(WEB_select_item_every, 0, $qa_value);
  855. print_select_item(WEB_select_item_disabled, 1, $qa_value);
  856. print_select_item(WEB_select_item_enabled, 2, $qa_value);
  857. print "</select>\n";
  858. }
  859. function print_vendor_select($db, $qa_name, $qa_value)
  860. {
  861. print "<select name=\"$qa_name\" class=\"js-select-single\">\n";
  862. $sSQL = "SELECT id,`name` FROM `vendors` order by `name`";
  863. $vendors = mysqli_query($db, $sSQL);
  864. print_select_item(WEB_select_item_all, 0, $qa_value);
  865. while (list($v_id, $v_name) = mysqli_fetch_array($vendors)) {
  866. print_select_item($v_name, $v_id, $qa_value);
  867. }
  868. print "</select>\n";
  869. }
  870. function print_vendor_set($db, $qa_name, $qa_value)
  871. {
  872. print "<select name=\"$qa_name\" class=\"js-select-single\" style=\"width: 100%\">\n";
  873. $sSQL = "SELECT id,`name` FROM `vendors` order by `name`";
  874. $vendors = mysqli_query($db, $sSQL);
  875. while (list($v_id, $v_name) = mysqli_fetch_array($vendors)) {
  876. print_select_item($v_name, $v_id, $qa_value);
  877. }
  878. print "</select>\n";
  879. }
  880. function get_vendor_name($db, $v_id)
  881. {
  882. $vendor = get_record_sql($db, "SELECT * FROM `vendors` WHERE id=" . $v_id);
  883. if (empty($vendor)) {
  884. return NULL;
  885. }
  886. return $vendor['name'];
  887. }
  888. function get_qa($qa_value)
  889. {
  890. if ($qa_value == 1) {
  891. return "Да";
  892. }
  893. return "Нет";
  894. }
  895. function print_action_select($action_name, $action_value)
  896. {
  897. print "<select name=\"$action_name\">\n";
  898. print_select_item(WEB_select_item_allow, 1, $action_value);
  899. print_select_item(WEB_select_item_forbidden, 0, $action_value);
  900. print "</select>\n";
  901. }
  902. function get_action($action_value)
  903. {
  904. if ($action_value == 1) {
  905. return "Разрешить";
  906. }
  907. return "Запретить";
  908. }
  909. function print_filter_select($db, $filter_name, $group_id)
  910. {
  911. print "<select name=\"$filter_name\" class=\"js-select-single\">\n";
  912. if (isset($group_id)) {
  913. $sSQL = "SELECT id,name FROM Filter_list WHERE Filter_list.id not in (Select filter_id FROM Group_filters WHERE group_id=$group_id)";
  914. } else {
  915. $sSQL = "SELECT id,name FROM Filter_list Order by name";
  916. }
  917. $t_filters = mysqli_query($db, $sSQL);
  918. while (list($filter_id, $filter_name) = mysqli_fetch_array($t_filters)) {
  919. print_select_item($filter_name, $filter_id, 0);
  920. }
  921. print "</select>\n";
  922. }
  923. function get_filter($db, $filter_value)
  924. {
  925. list($filter) = mysqli_fetch_array(mysqli_query($db, "SELECT name FROM Filter_list WHERE id=" . $filter_value));
  926. return $filter;
  927. }
  928. function get_login($db, $user_id)
  929. {
  930. list($login) = mysqli_fetch_array(mysqli_query($db, "SELECT login FROM User_list WHERE id=$user_id"));
  931. return $login;
  932. }
  933. function get_auth_count($db, $user_id)
  934. {
  935. list($count) = mysqli_fetch_array(mysqli_query($db, "SELECT count(id) FROM User_auth WHERE user_id=$user_id and deleted=0"));
  936. return $count;
  937. }
  938. function print_login_select($db, $login_name, $current_login)
  939. {
  940. print "<select name=\"$login_name\" class=\"js-select-single\">\n";
  941. $t_login = mysqli_query($db, "SELECT id,login FROM User_list Order by Login");
  942. print_select_item('None', 0, $current_login);
  943. while (list($f_user_id, $f_login) = mysqli_fetch_array($t_login)) {
  944. print_select_item($f_login, $f_user_id, $current_login);
  945. }
  946. print "</select>\n";
  947. }
  948. function print_auth_select($db, $login_name, $current_auth)
  949. {
  950. print "<select name=\"$login_name\" class=\"js-select-single\">\n";
  951. $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");
  952. print_select_item('Empty', 0, $current_auth);
  953. while (list($f_login, $f_fio, $f_ip, $f_auth_id) = mysqli_fetch_array($t_login)) {
  954. print_select_item($f_login . "[" . $f_fio . "] - " . $f_ip, $f_auth_id, $current_auth);
  955. }
  956. print "</select>\n";
  957. }
  958. function print_auth_select_mac($db, $login_name, $current_auth)
  959. {
  960. print "<select name=\"$login_name\" class=\"js-select-single\">\n";
  961. $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");
  962. print_select_item('Empty', 0, $current_auth);
  963. while (list($f_login, $f_fio, $f_ip, $f_mac, $f_auth_id) = mysqli_fetch_array($t_login)) {
  964. print_select_item($f_login . "[" . $f_mac . "] - " . $f_ip, $f_auth_id, $current_auth);
  965. }
  966. print "</select>\n";
  967. }
  968. function compact_port_name($port)
  969. {
  970. $result = $port;
  971. $result = preg_replace('/XGigabitEthernet/', 'X', $result);
  972. $result = preg_replace('/TenGigabitEthernet/', 'Te', $result);
  973. $result = preg_replace('/GigabitEthernet/', 'Gi', $result);
  974. return $result;
  975. }
  976. function print_device_port_select($db, $field_name, $device_id, $target_id)
  977. {
  978. print "<select name=\"$field_name\" class=\"js-select-single\">\n";
  979. if (empty($target_id)) {
  980. $target_id = 0;
  981. }
  982. if (empty($device_id)) {
  983. $device_id = 0;
  984. }
  985. $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";
  986. $t_device = mysqli_query($db, $d_sql);
  987. print_select_item('Empty', 0, $target_id);
  988. while (list($f_name, $f_port, $f_device_id, $f_target_id, $f_ifname) = mysqli_fetch_array($t_device)) {
  989. if (empty($f_ifname)) {
  990. $f_ifname = $f_port;
  991. }
  992. print_select_item($f_name . "[" . $f_port."] - " . compact_port_name($f_ifname), $f_target_id, $target_id);
  993. }
  994. print "</select>\n";
  995. }
  996. function print_device_select($db, $field_name, $device_id)
  997. {
  998. print "<select name=\"$field_name\" class=\"js-select-single\" >\n";
  999. $d_sql = "SELECT D.device_name, D.id FROM devices AS D Where D.deleted=0 order by D.device_name ASC";
  1000. $t_device = mysqli_query($db, $d_sql);
  1001. print_select_item(WEB_select_item_every, 0, $device_id);
  1002. while (list($f_name, $f_device_id) = mysqli_fetch_array($t_device)) {
  1003. print_select_item($f_name, $f_device_id, $device_id);
  1004. }
  1005. print "</select>\n";
  1006. }
  1007. function print_netdevice_select($db, $field_name, $device_id)
  1008. {
  1009. print "<select name=\"$field_name\" class=\"js-select-single\" >\n";
  1010. $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";
  1011. $t_device = mysqli_query($db, $d_sql);
  1012. print_select_item(WEB_select_item_every, 0, $device_id);
  1013. while (list($f_name, $f_device_id) = mysqli_fetch_array($t_device)) {
  1014. print_select_item($f_name, $f_device_id, $device_id);
  1015. }
  1016. print "</select>\n";
  1017. }
  1018. function print_vlan_select($db, $field_name, $vlan)
  1019. {
  1020. print "<select name=\"$field_name\" class=\"js-select-single\">\n";
  1021. $d_sql = "SELECT DISTINCT vlan FROM device_ports ORDER BY vlan DESC";
  1022. $v_device = mysqli_query($db, $d_sql);
  1023. if (!isset($vlan) or empty($vlan)) {
  1024. $vlan = 1;
  1025. };
  1026. print_select_item('1', 1, $vlan);
  1027. while (list($f_vlan) = mysqli_fetch_array($v_device)) {
  1028. if ($f_vlan === '1') {
  1029. continue;
  1030. }
  1031. print_select_item($f_vlan, $f_vlan, $vlan);
  1032. }
  1033. print "</select>\n";
  1034. }
  1035. function print_device_select_ip($db, $field_name, $device_ip)
  1036. {
  1037. print "<select name=\"$field_name\" class=\"js-select-single\" >\n";
  1038. $d_sql = "SELECT D.device_name, D.ip FROM devices AS D Where D.deleted=0 order by D.device_name ASC";
  1039. $t_device = mysqli_query($db, $d_sql);
  1040. print_select_item(WEB_select_item_every, '', $device_ip);
  1041. while (list($f_name, $f_device_ip) = mysqli_fetch_array($t_device)) {
  1042. print_select_item($f_name, $f_device_ip, $device_ip);
  1043. }
  1044. print "</select>\n";
  1045. }
  1046. function print_syslog_device_select($db, $field_name, $syslog_filter, $device_ip)
  1047. {
  1048. print "<select name=\"$field_name\" class=\"js-select-single\" >\n";
  1049. $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";
  1050. $t_device = mysqli_query($db, $d_sql);
  1051. print_select_item(WEB_select_item_every, '', $device_ip);
  1052. while (list($f_ip, $f_name) = mysqli_fetch_array($t_device)) {
  1053. if (!isset($f_name) or empty($f_name)) {
  1054. $f_name = $f_ip;
  1055. }
  1056. print_select_item($f_name, $f_ip, $device_ip);
  1057. }
  1058. print "</select>\n";
  1059. }
  1060. function print_gateway_select($db, $field_name, $device_id)
  1061. {
  1062. print "<select name=\"$field_name\" >\n";
  1063. $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";
  1064. $t_device = mysqli_query($db, $d_sql);
  1065. print_select_item(WEB_select_item_every, 0, $device_id);
  1066. while (list($f_name, $f_device_id) = mysqli_fetch_array($t_device)) {
  1067. print_select_item($f_name, $f_device_id, $device_id);
  1068. }
  1069. print "</select>\n";
  1070. }
  1071. function get_gateways($db)
  1072. {
  1073. $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";
  1074. $t_device = mysqli_query($db, $d_sql);
  1075. unset($result);
  1076. while (list($f_name, $f_device_id) = mysqli_fetch_array($t_device)) {
  1077. $result[$f_device_id] = $f_name;
  1078. }
  1079. return $result;
  1080. }
  1081. function print_device_port($db, $target_id)
  1082. {
  1083. $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";
  1084. $t_device = mysqli_query($db, $d_sql);
  1085. while (list($f_name, $f_port, $f_device_id) = mysqli_fetch_array($t_device)) {
  1086. print "<a href=\"/admin/devices/switchport.php?id=$f_device_id\">" . $f_name . "[" . $f_port . "]</a>\n";
  1087. }
  1088. }
  1089. function get_device_ips($db, $device_id)
  1090. {
  1091. $switch = get_record($db, 'devices', 'id=' . $device_id);
  1092. $index = 0;
  1093. if (!empty($switch['user_id'])) {
  1094. $auth_ips = get_records($db, 'User_auth', 'deleted=0 and user_id=' . $switch['user_id']);
  1095. foreach ($auth_ips as $key => $value) {
  1096. if (isset($value['ip'])) {
  1097. $result[$index] = $value['ip'];
  1098. $index++;
  1099. }
  1100. }
  1101. } else {
  1102. if (isset($switch['ip'])) {
  1103. $result[$index] = $switch['ip'];
  1104. $index++;
  1105. }
  1106. }
  1107. return $result;
  1108. }
  1109. function get_device_id($db, $device_name)
  1110. {
  1111. $d_sql = "SELECT id FROM devices WHERE device_name='$device_name' and deleted=0";
  1112. $dev = get_record_sql($db, $d_sql);
  1113. if (empty($dev)) {
  1114. return NULL;
  1115. }
  1116. return $dev["id"];
  1117. }
  1118. function get_device_name($db, $device_id)
  1119. {
  1120. $d_sql = "SELECT device_name FROM devices WHERE id='$device_id'";
  1121. $dev = get_record_sql($db, $d_sql);
  1122. if (empty($dev)) {
  1123. return NULL;
  1124. }
  1125. return $dev["device_name"];
  1126. }
  1127. function get_auth_by_ip($db, $ip)
  1128. {
  1129. $d_sql = "SELECT id FROM User_auth WHERE ip='$ip' and deleted=0";
  1130. $auth = get_record_sql($db, $d_sql);
  1131. if (empty($auth)) {
  1132. return NULL;
  1133. }
  1134. return $auth["id"];
  1135. }
  1136. function get_user_by_ip($db, $ip)
  1137. {
  1138. $d_sql = "SELECT user_id FROM User_auth WHERE ip='$ip' and deleted=0";
  1139. $auth = get_record_sql($db, $d_sql);
  1140. if (empty($auth)) {
  1141. return NULL;
  1142. }
  1143. return $auth["user_id"];
  1144. }
  1145. function get_device_by_auth($db, $id)
  1146. {
  1147. $d_sql = "SELECT id FROM devices WHERE user_id=$id and deleted=0";
  1148. $f_dev = get_record_sql($db, $d_sql);
  1149. if (empty($f_dev)) {
  1150. return NULL;
  1151. }
  1152. return $f_dev['id'];
  1153. }
  1154. function print_auth_port($db, $port_id)
  1155. {
  1156. $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";
  1157. $t_device = mysqli_query($db, $d_sql);
  1158. while (list($f_ip, $f_int, $f_mac, $f_auth_id, $f_dns) = mysqli_fetch_array($t_device)) {
  1159. $name = $f_ip;
  1160. if (isset($f_dns) and $f_dns != '') {
  1161. $name = $f_dns;
  1162. }
  1163. print "<a href=\"/admin/users/editauth.php?id=$f_auth_id\">" . $name . " [" . $f_ip . "]</a><br>";
  1164. }
  1165. }
  1166. function print_auth_simple($db, $auth_id)
  1167. {
  1168. $auth = get_record($db, "User_auth", "id=$auth_id");
  1169. $name = $auth['dns_name'];
  1170. if (empty($name)) {
  1171. $name = $auth['comments'];
  1172. }
  1173. if (empty($name)) {
  1174. $name = $auth['ip'];
  1175. }
  1176. print "<a href=\"/admin/users/editauth.php?id=$auth_id\">" . $name . "</a><br>";
  1177. }
  1178. function print_auth($db, $auth_id)
  1179. {
  1180. $auth = get_record($db, "User_auth", "id=$auth_id");
  1181. $name = $auth['dns_name'];
  1182. if (empty($name)) {
  1183. $name = $auth['comments'];
  1184. } else {
  1185. $name .= " (" . $auth['comments'] . ")";
  1186. }
  1187. if (empty($name)) {
  1188. $name = $auth['ip'];
  1189. } else {
  1190. $name .= " [" . $auth['ip'] . "]";
  1191. }
  1192. print "<a href=\"/admin/users/editauth.php?id=$auth_id\">" . $name . "</a><br>";
  1193. }
  1194. function print_auth_detail($db, $auth_id)
  1195. {
  1196. $auth = get_record($db, "User_auth", "id=$auth_id");
  1197. $name = $auth['dns_name'];
  1198. if (empty($name)) {
  1199. $name = $auth['comments'];
  1200. } else {
  1201. $name .= " (" . $auth['comments'] . ")";
  1202. }
  1203. if (empty($name)) {
  1204. $name = $auth['ip'];
  1205. } else {
  1206. $name .= " [" . $auth['ip'] . "]";
  1207. }
  1208. $name .= " last: [" . $auth['last_found'] . "] ";
  1209. if ($auth['deleted'] == 1) {
  1210. $name .= " <font color='red'>DELETED!!!</font>";
  1211. }
  1212. print "<a href=\"/admin/users/editauth.php?id=$auth_id\">" . $name . "</a><br>";
  1213. }
  1214. function get_auth_port_count($db, $port_id)
  1215. {
  1216. $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";
  1217. $t_device = mysqli_query($db, $d_sql);
  1218. list($f_count) = mysqli_fetch_array($t_device);
  1219. if (!isset($f_count)) {
  1220. $f_count = 0;
  1221. }
  1222. return $f_count;
  1223. }
  1224. function get_connection($db, $auth_id)
  1225. {
  1226. $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";
  1227. $t_device = mysqli_query($db, $d_sql);
  1228. list($f_name, $f_port) = mysqli_fetch_array($t_device);
  1229. if (isset($f_name)) {
  1230. $result = expand_device_name($db, $f_name) . "[" . $f_port . "]";
  1231. } else {
  1232. $result = '';
  1233. }
  1234. return $result;
  1235. }
  1236. function get_port($db, $port_id)
  1237. {
  1238. $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";
  1239. $t_device = mysqli_query($db, $d_sql);
  1240. list($f_name, $f_port) = mysqli_fetch_array($t_device);
  1241. if (isset($f_name)) {
  1242. $result = expand_device_name($db, $f_name) . "[" . $f_port . "]";
  1243. } else {
  1244. $result = '';
  1245. }
  1246. return $result;
  1247. }
  1248. function print_option_select($db, $option_name)
  1249. {
  1250. print "<select name=\"$option_name\">\n";
  1251. $t_option = mysqli_query($db, "SELECT id,option_name FROM config_options WHERE uniq=0 order by option_name");
  1252. while (list($f_id, $f_name) = mysqli_fetch_array($t_option)) {
  1253. print "<option value=$f_id>$f_name</option>";
  1254. }
  1255. $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");
  1256. while (list($f_id, $f_name) = mysqli_fetch_array($t_option)) {
  1257. print "<option value=$f_id>$f_name</option>";
  1258. }
  1259. print "</select>\n";
  1260. }
  1261. function run_sql($db, $query)
  1262. {
  1263. $sql_result = mysqli_query($db, $query);
  1264. if (!$sql_result) {
  1265. LOG_ERROR($db, "At simple SQL: $query :" . mysqli_error($db));
  1266. return;
  1267. }
  1268. return $sql_result;
  1269. }
  1270. function get_count_records($db, $table, $filter)
  1271. {
  1272. if (!empty($filter)) {
  1273. $filter = 'where ' . $filter;
  1274. }
  1275. $t_count = mysqli_query($db, "SELECT count(*) FROM $table $filter");
  1276. list($count) = mysqli_fetch_array($t_count);
  1277. if (!isset($count)) {
  1278. $count = 0;
  1279. }
  1280. return $count;
  1281. }
  1282. function get_id_record($db, $table, $filter)
  1283. {
  1284. if (isset($filter)) {
  1285. $filter = 'WHERE ' . $filter;
  1286. }
  1287. $t_record = mysqli_query($db, "SELECT id FROM $table $filter limit 1");
  1288. list($id) = mysqli_fetch_array($t_record);
  1289. return $id;
  1290. }
  1291. function set_changed($db, $id)
  1292. {
  1293. $auth['changed'] = 1;
  1294. update_record($db, "User_auth", "id=" . $id, $auth);
  1295. }
  1296. function ResolveIP($db, $ip_int)
  1297. {
  1298. $ip_name = "-";
  1299. if (empty($ip_int)) {
  1300. return $ip_name;
  1301. }
  1302. $dns_cache = get_record_sql($db, "SELECT * FROM dns_cache WHERE ip=$ip_int");
  1303. if (empty($dns_cache) or empty($dns_cache['dns'])) {
  1304. $ip_name = gethostbyaddr(long2ip($ip_int));
  1305. if (empty($ip_name) or $ip_name == long2ip($ip_int)) {
  1306. $ip_name = "-";
  1307. }
  1308. run_sql($db, "INSERT INTO dns_cache(dns,ip) VALUES('" . $ip_name . "'," . $ip_int . ")");
  1309. } else {
  1310. $ip_name = $dns_cache['dns'];
  1311. }
  1312. return $ip_name;
  1313. }
  1314. function clean_dns_cache($db)
  1315. {
  1316. $date = time();
  1317. $date = $date - 86400;
  1318. $date_clean = DateTimeImmutable::createFromFormat('U', $date);
  1319. $clean_date = $date_clean->format('Y-m-d H:i:s');
  1320. run_sql($db, "DELETE FROM dns_cache WHERE `timestamp`<='" . $clean_date . "'");
  1321. }
  1322. function FormatDateStr($format = 'Y-m-d H:i:s', $date_str)
  1323. {
  1324. $date1 = GetDateTimeFromString($date_str);
  1325. $result = $date1->format($format);
  1326. return $result;
  1327. }
  1328. function GetDateTimeFromString($date_str)
  1329. {
  1330. if (!is_a($date_str, 'DateTime')) {
  1331. $t_date_str = urldecode($date_str);
  1332. $t_date_str = preg_replace('/(\'|\")/', '', $t_date_str);
  1333. $t_date_str = preg_replace('/T/', ' ', $t_date_str);
  1334. $date1 = DateTime::createFromFormat('Y-m-d H:i:s', $t_date_str);
  1335. if (!$date1) {
  1336. $date1 = DateTime::createFromFormat('Y.m.d H:i:s', $t_date_str);
  1337. }
  1338. if (!$date1) {
  1339. $date1 = DateTime::createFromFormat('Y/m/d H:i:s', $t_date_str);
  1340. }
  1341. if (!$date1) {
  1342. $date1 = DateTime::createFromFormat('Y-m-d H:i', $t_date_str);
  1343. }
  1344. if (!$date1) {
  1345. $date1 = DateTime::createFromFormat('Y.m.d H:i', $t_date_str);
  1346. }
  1347. if (!$date1) {
  1348. $date1 = DateTime::createFromFormat('Y/m/d H:i', $t_date_str);
  1349. }
  1350. if (!$date1) {
  1351. $date1 = DateTime::createFromFormat('Y-m-d|', $t_date_str);
  1352. }
  1353. if (!$date1) {
  1354. $date1 = DateTime::createFromFormat('Y.m.d|', $t_date_str);
  1355. }
  1356. if (!$date1) {
  1357. $date1 = DateTime::createFromFormat('Y/m/d|', $t_date_str);
  1358. }
  1359. if (!$date1) {
  1360. $date1 = new DateTime;
  1361. $date1->setTime(0, 0, 0, 1);
  1362. }
  1363. } else {
  1364. return $date_str;
  1365. }
  1366. return $date1;
  1367. }
  1368. function GetNowTimeString()
  1369. {
  1370. $now = new DateTimeImmutable('now');
  1371. $result = $now->format('Y-m-d H:i:s');
  1372. return $result;
  1373. }
  1374. function GetNowDayString()
  1375. {
  1376. $now = new DateTimeImmutable('now');
  1377. $result = $now->format('Y-m-d');
  1378. return $result;
  1379. }
  1380. function get_ip_subnet($db, $ip)
  1381. {
  1382. if (empty($ip)) {
  1383. return;
  1384. }
  1385. $ip_aton = ip2long($ip);
  1386. $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)");
  1387. if (empty($user_subnet)) {
  1388. return;
  1389. }
  1390. return $user_subnet;
  1391. }
  1392. function find_mac_in_subnet($db, $ip, $mac)
  1393. {
  1394. if (empty($ip)) {
  1395. return;
  1396. }
  1397. if (empty($mac)) {
  1398. return;
  1399. }
  1400. $ip_subnet = get_ip_subnet($db, $ip);
  1401. if (empty($ip_subnet)) {
  1402. return;
  1403. }
  1404. $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");
  1405. $auth_count = 0;
  1406. $result['count'] = 0;
  1407. $result['users_id'] = [];
  1408. foreach ($t_auth as $row) {
  1409. if (!empty($row['id'])) {
  1410. $auth_count++;
  1411. $result['count'] = $auth_count;
  1412. $result[$auth_count] = $row['id'];
  1413. array_push($result['users_id'], $row['user_id']);
  1414. }
  1415. }
  1416. return $result;
  1417. }
  1418. function apply_auth_rule($db, $auth_id, $user_id)
  1419. {
  1420. if (empty($auth_id)) {
  1421. return;
  1422. }
  1423. if (empty($user_id)) {
  1424. return;
  1425. }
  1426. $user_rec = get_record($db, 'User_list', "id=" . $user_id);
  1427. if (empty($user_rec)) {
  1428. return;
  1429. }
  1430. //set filter and status by user
  1431. $set_auth['ou_id'] = $user_rec['ou_id'];
  1432. $set_auth['user_id'] = $user_rec['id'];
  1433. $set_auth['filter_group_id'] = $user_rec['filter_group_id'];
  1434. $set_auth['queue_id'] = $user_rec['queue_id'];
  1435. $set_auth['enabled'] = $user_rec['enabled'];
  1436. $set_auth['changed'] = 1;
  1437. update_record($db, "User_auth", "id=$auth_id", $set_auth);
  1438. return $set_auth;
  1439. }
  1440. function fix_auth_rules($db)
  1441. {
  1442. //cleanup hotspot subnet rules
  1443. delete_record($db, "auth_rules", "ou_id=" . get_const('default_user_ou_id'));
  1444. delete_record($db, "auth_rules", "ou_id=" . get_const('default_hotspot_ou_id'));
  1445. $t_hotspot = get_records_sql($db, "SELECT * FROM subnets WHERE hotspot=1");
  1446. if (!empty($t_hotspot)) {
  1447. foreach ($t_hotspot as $row) {
  1448. delete_record($db, "auth_rules", "rule='" . $row['subnet'] . "'");
  1449. }
  1450. }
  1451. }
  1452. #---------------------------------------------------------------------------------------------------------------
  1453. function new_user($db, $user_info)
  1454. {
  1455. if (!empty($user_info['mac'])) {
  1456. $user['login'] = mac_dotted($user_info['mac']);
  1457. } else {
  1458. $user['login'] = $user_info['ip'];
  1459. }
  1460. if (!empty($user_info['dhcp_hostname'])) {
  1461. $user['fio'] = $user_info['ip'] . '[' . $user_info['dhcp_hostname'] . ']';
  1462. } else {
  1463. $user['fio'] = $user_info['ip'];
  1464. }
  1465. $login_count = get_count_records($db, "User_list", "(login LIKE '" . $user['login'] . "(%)') OR (login='" . $user['login'] . "')");
  1466. if (!empty($login_count) and $login_count > 0) {
  1467. $login_count++;
  1468. $user['login'] = $user['login'] . "(" . $login_count . ")";
  1469. }
  1470. $user['ou_id'] = $user_info['ou_id'];
  1471. $ou_info = get_record_sql($db, "SELECT * FROM OU WHERE id=" . $user_info['ou_id']);
  1472. if (!empty($ou_info)) {
  1473. $user['enabled'] = $ou_info['enabled'];
  1474. if (empty($user['enabled'])) {
  1475. $user['enabled'] = 0;
  1476. }
  1477. $user['queue_id'] = $ou_info['queue_id'];
  1478. if (empty($user['queue_id'])) {
  1479. $user['queue_id'] = 0;
  1480. }
  1481. $user['filter_group_id'] = $ou_info['filter_group_id'];
  1482. if (empty($user['filter_group_id'])) {
  1483. $user['filter_group_id'] = 0;
  1484. }
  1485. }
  1486. $result = insert_record($db, "User_list", $user);
  1487. $auto_mac_rule = get_option($db, 64);
  1488. if (!empty($result) and $auto_mac_rule and $user_info['mac']) {
  1489. $auth_rule['user_id'] = $result;
  1490. $auth_rule['type'] = 2;
  1491. $auth_rule['rule'] = mac_dotted($user_info['mac']);
  1492. insert_record($db, "auth_rules", $auth_rule);
  1493. }
  1494. return $result;
  1495. }
  1496. function new_auth($db, $ip, $mac, $user_id)
  1497. {
  1498. $ip_aton = ip2long($ip);
  1499. $msg = '';
  1500. if (!empty($mac)) {
  1501. $auth_record = get_record_sql($db, "SELECT * FROM User_auth WHERE ip_int=$ip_aton AND mac='" . mac_dotted($mac) . "' AND deleted=0");
  1502. if (!empty($auth_record)) {
  1503. LOG_WARNING($db, "Pair ip-mac already exists! Skip creating $ip [$mac] auth_id: " . $auth_record["id"]);
  1504. return $auth_record['id'];
  1505. }
  1506. }
  1507. // default id
  1508. $save_traf = get_option($db, 23);
  1509. $resurrection_id = NULL;
  1510. // seek old auth with same ip and mac
  1511. $resurrection_id = get_id_record($db, 'User_auth', " deleted=1 AND ip_int=" . $ip_aton . " AND mac='" . $mac . "'");
  1512. if (!empty($resurrection_id)) {
  1513. $msg .= "Recovered auth_id: $resurrection_id with ip: $ip and mac: $mac ";
  1514. $auth['user_id'] = $user_id;
  1515. $auth['deleted'] = 0;
  1516. $auth['save_traf'] = $save_traf * 1;
  1517. update_record($db, "User_auth", "id=$resurrection_id", $auth);
  1518. } else {
  1519. // not found ->create new record
  1520. $msg .= "Create new ip record \r\nip: $ip\r\nmac: $mac\r\n";
  1521. $auth['deleted'] = 0;
  1522. $auth['user_id'] = $user_id;
  1523. $auth['ip'] = $ip;
  1524. $auth['ip_int'] = $ip_aton;
  1525. $auth['mac'] = $mac;
  1526. $auth['save_traf'] = $save_traf * 1;
  1527. $resurrection_id = insert_record($db, "User_auth", $auth);
  1528. }
  1529. //check rules, update filter and state for new record
  1530. if (!empty($resurrection_id)) {
  1531. apply_auth_rule($db, $resurrection_id, $user_id);
  1532. if (!is_hotspot($db, $ip) and !empty($msg)) {
  1533. LOG_WARNING($db, $msg);
  1534. }
  1535. if (is_hotspot($db, $ip) and !empty($msg)) {
  1536. LOG_INFO($db, $msg);
  1537. }
  1538. }
  1539. return $resurrection_id;
  1540. }
  1541. function resurrection_auth($db, $ip_record)
  1542. {
  1543. $ip = $ip_record['ip'];
  1544. $mac = $ip_record['mac'];
  1545. $action = $ip_record['type'];
  1546. $dhcp_hostname = $ip_record['hostname'];
  1547. $hotspot_found = $ip_record['hotspot'];
  1548. $ip_aton = ip2long($ip);
  1549. $auth_record = get_record_sql($db, "SELECT * FROM User_auth WHERE ip_int=$ip_aton AND mac='" . $mac . "' AND deleted=0");
  1550. if (!empty($auth_record)) {
  1551. $user_info = get_record_sql($db, "SELECT * FROM User_list WHERE id=" . $auth_record['user_id']);
  1552. LOG_DEBUG($db, "external dhcp user " . $user_info['login'] . " [" . $ip . "] auth_id: " . $auth_record['id']);
  1553. if (isset($dhcp_hostname) and !empty($dhcp_hostname)) {
  1554. $auth['dhcp_hostname'] = $dhcp_hostname;
  1555. }
  1556. $auth['dhcp_action'] = $action;
  1557. $auth['dhcp_time'] = GetNowTimeString();
  1558. if ($action === 'add') {
  1559. $auth['last_found'] = GetNowTimeString();
  1560. }
  1561. update_record($db, "User_auth", "id=" . $auth_record['id'], $auth);
  1562. return $auth_record['id'];
  1563. }
  1564. $ip_subnet = get_ip_subnet($db, $ip);
  1565. if ($ip_subnet['static']) {
  1566. LOG_WARNING($db, "Unknown pair ip+mac in static subnet! ip: $ip mac: [" . mac_dotted($mac) . "]. Skip");
  1567. return;
  1568. }
  1569. $msg = '';
  1570. // search changed mac
  1571. $auth_record = get_record_sql($db, "SELECT * FROM User_auth WHERE ip_int=$ip_aton AND deleted=0");
  1572. if (!empty($auth_record)) {
  1573. if (empty($auth_record['mac'])) {
  1574. $auth['mac'] = mac_dotted($mac);
  1575. $auth['dhcp_action'] = $action;
  1576. $auth['dhcp_time'] = GetNowTimeString();
  1577. if (!empty($dhcp_hostname)) {
  1578. $auth['dhcp_hostname'] = $dhcp_hostname;
  1579. }
  1580. if ($action === 'add') {
  1581. $auth['last_found'] = GetNowTimeString();
  1582. }
  1583. LOG_INFO($db, "for ip: $ip mac not found! Use empty record...");
  1584. update_record($db, "User_auth", "id=" . $auth_record['id'], $auth);
  1585. return $auth_record['id'];
  1586. } else {
  1587. if (!$hotspot_found) {
  1588. 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']);
  1589. }
  1590. run_sql($db, "UPDATE User_auth SET changed=1, deleted=1 WHERE id=" . $auth_record['id']);
  1591. }
  1592. }
  1593. // default id
  1594. $new_user_info = get_new_user_id($db, $ip, $mac, $dhcp_hostname);
  1595. if (!empty($new_user_info['user_id'])) {
  1596. $new_user_id = $new_user_info['user_id'];
  1597. }
  1598. if (empty($new_user_id)) {
  1599. $new_user_id = new_user($db, $new_user_info);
  1600. }
  1601. $resurrection_id = NULL;
  1602. $save_traf = get_option($db, 23);
  1603. $auth_record = get_record_sql($db, "SELECT * FROM User_auth WHERE ip_int=" . $ip_aton . " and mac='" . $mac . "'");
  1604. // seek old auth with same ip and mac
  1605. if (!empty($auth_record)) {
  1606. // found ->Resurrection old record
  1607. $resurrection_id = $auth_record['id'];
  1608. $msg .= "Recovered auth_id: $resurrection_id with ip: $ip and mac: $mac ";
  1609. $auth['dhcp_action'] = $action;
  1610. $auth['user_id'] = $new_user_id;
  1611. $auth['deleted'] = 0;
  1612. $auth['dhcp_time'] = GetNowTimeString();
  1613. $auth['save_traf'] = $save_traf * 1;
  1614. if (!empty($dhcp_hostname)) {
  1615. $auth['dhcp_hostname'] = $dhcp_hostname;
  1616. }
  1617. if ($action === 'add') {
  1618. $auth['last_found'] = GetNowTimeString();
  1619. }
  1620. update_record($db, "User_auth", "id=$resurrection_id", $auth);
  1621. } else {
  1622. // not found ->create new record
  1623. $msg .= "Создаём новый ip-адрес \r\nip: $ip\r\nmac: $mac\r\n";
  1624. $auth['deleted'] = 0;
  1625. $auth['user_id'] = $new_user_id;
  1626. $auth['ip'] = $ip;
  1627. $auth['ip_int'] = $ip_aton;
  1628. $auth['mac'] = $mac;
  1629. $auth['dhcp_action'] = $action;
  1630. $auth['dhcp_time'] = GetNowTimeString();
  1631. $auth['save_traf'] = $save_traf * 1;
  1632. if (!empty($dhcp_hostname)) {
  1633. $auth['dhcp_hostname'] = $dhcp_hostname;
  1634. }
  1635. if ($action == 'add') {
  1636. $auth['last_found'] = GetNowTimeString();
  1637. }
  1638. $resurrection_id = insert_record($db, "User_auth", $auth);
  1639. }
  1640. //check rules, update filter and state for new record
  1641. if (!empty($resurrection_id)) {
  1642. $user_rec = apply_auth_rule($db, $resurrection_id, $new_user_id);
  1643. $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";
  1644. if (!$hotspot_found and !empty($msg)) {
  1645. LOG_WARNING($db, $msg);
  1646. }
  1647. if ($hotspot_found and !empty($msg)) {
  1648. LOG_INFO($db, $msg);
  1649. }
  1650. }
  1651. return $resurrection_id;
  1652. }
  1653. function get_auth($db, $current_auth)
  1654. {
  1655. if (!isset($current_auth)) {
  1656. return;
  1657. }
  1658. if ($current_auth == 0) {
  1659. return;
  1660. }
  1661. $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");
  1662. list($f_login, $f_ip) = mysqli_fetch_array($t_login);
  1663. $result = $f_login . "[" . $f_ip . "]";
  1664. return $result;
  1665. }
  1666. function get_auth_by_mac($db, $mac)
  1667. {
  1668. if (!isset($mac)) {
  1669. return;
  1670. }
  1671. $mac = mac_dotted($mac);
  1672. $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");
  1673. list($f_id, $f_login, $f_auth_id, $f_ip) = mysqli_fetch_array($t_login);
  1674. if (isset($f_id)) {
  1675. $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>';
  1676. } else {
  1677. $result['auth'] = 'Unknown';
  1678. }
  1679. $result['mac'] = expand_mac($db, $mac);
  1680. return $result;
  1681. }
  1682. function get_auth_mac($db, $current_auth)
  1683. {
  1684. if (!isset($current_auth)) {
  1685. return;
  1686. }
  1687. if ($current_auth == 0) {
  1688. return;
  1689. }
  1690. $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");
  1691. list($f_login, $f_mac) = mysqli_fetch_array($t_login);
  1692. $result = $f_login . "[" . $f_mac . "]";
  1693. return $result;
  1694. }
  1695. function isRO($db, $table)
  1696. {
  1697. $result = 1;
  1698. if (isset($_SESSION['login'])) {
  1699. $work_user = $_SESSION['login'];
  1700. }
  1701. if (isset($_SESSION['user_id'])) {
  1702. $work_id = $_SESSION['user_id'];
  1703. }
  1704. if (!isset($work_user) or !isset($work_id)) {
  1705. return $result;
  1706. }
  1707. if (preg_match('/^(variables|dns_cache|syslog)$/', $table)) {
  1708. return $result;
  1709. }
  1710. $t_login = mysqli_query($db, "SELECT readonly FROM Customers WHERE Login='" . $work_user . "' and id='" . $work_id . "'");
  1711. list($f_ro) = mysqli_fetch_array($t_login);
  1712. if (!isset($f_ro)) {
  1713. return $result;
  1714. }
  1715. return $f_ro;
  1716. }
  1717. function LOG_INFO($db, $msg, $auth_id = 0)
  1718. {
  1719. if (get_const('log_level') < L_INFO) {
  1720. return;
  1721. }
  1722. write_log($db, $msg, L_INFO, $auth_id);
  1723. }
  1724. function LOG_ERROR($db, $msg, $auth_id = 0)
  1725. {
  1726. if (get_const('log_level') < L_ERROR) {
  1727. return;
  1728. }
  1729. email(L_ERROR, $msg);
  1730. write_log($db, $msg, L_ERROR, $auth_id);
  1731. }
  1732. function LOG_VERBOSE($db, $msg, $auth_id = 0)
  1733. {
  1734. if (get_const('log_level') < L_VERBOSE) {
  1735. return;
  1736. }
  1737. write_log($db, $msg, L_VERBOSE, $auth_id);
  1738. }
  1739. function LOG_WARNING($db, $msg, $auth_id = 0)
  1740. {
  1741. if (get_const('log_level') < L_WARNING) {
  1742. return;
  1743. }
  1744. email(L_WARNING, $msg);
  1745. write_log($db, $msg, L_WARNING, $auth_id);
  1746. }
  1747. function LOG_DEBUG($db, $msg, $auth_id = 0)
  1748. {
  1749. if (!empty(get_const('debug')) and get_const('debug')) {
  1750. write_log($db, $msg, L_DEBUG, $auth_id);
  1751. }
  1752. }
  1753. function get_first_line($msg)
  1754. {
  1755. if (empty($msg)) {
  1756. return;
  1757. }
  1758. preg_match('/(.*)(\n|\<br\>)/', $msg, $matches);
  1759. if (!empty($matches[1])) {
  1760. return $matches[1];
  1761. }
  1762. return;
  1763. }
  1764. function email($level, $msg)
  1765. {
  1766. if (!get_const('send_email')) {
  1767. return;
  1768. }
  1769. if (!($level === L_WARNING or $level === L_ERROR)) {
  1770. return;
  1771. }
  1772. $subject = get_first_line($msg);
  1773. if ($level === L_WARNING) {
  1774. $subject = "WARN: " . $subject . "...";
  1775. $message = 'WARNING! Manager: ' . $_SESSION['login'] . ' </br>';
  1776. }
  1777. if ($level === L_ERROR) {
  1778. $subject = "ERROR: " . $subject . "...";
  1779. $message = 'ERROR! Manager: ' . $_SESSION['login'] . ' </br>';
  1780. }
  1781. $msg_lines = preg_replace("/\r\n/", "</br>", $msg);
  1782. $message .= $msg_lines;
  1783. $send = SimpleMail::make()
  1784. ->setTo(get_const('admin_email'), 'Administrator')
  1785. ->setFrom(get_const('sender_email'), 'Stat')
  1786. ->setSubject($subject)
  1787. ->setMessage($message)
  1788. ->setHtml()
  1789. ->setWrap(80)
  1790. ->send();
  1791. }
  1792. function write_log($db, $msg, $level, $auth_id = 0)
  1793. {
  1794. $work_user = 'http';
  1795. if (isset($_SESSION['login'])) {
  1796. $work_user = $_SESSION['login'];
  1797. }
  1798. if (!isset($msg)) {
  1799. $msg = 'ERROR! Empty log string!';
  1800. }
  1801. if (!isset($level)) {
  1802. $level = L_INFO;
  1803. }
  1804. $msg = str_replace("'", '', $msg);
  1805. $sSQL = "insert into syslog(customer,message,level,auth_id) values('$work_user','$msg',$level,$auth_id)";
  1806. mysqli_query($db, $sSQL);
  1807. }
  1808. function print_year_select($year_name, $year)
  1809. {
  1810. print "<select name=\"$year_name\" >\n";
  1811. for ($i = $year - 10; $i <= $year + 10; $i++) {
  1812. print_select_item($i, $i, $year);
  1813. }
  1814. print "</select>\n";
  1815. }
  1816. function print_date_select($dd, $mm, $yy)
  1817. {
  1818. if ($dd >= 1) {
  1819. print "<b>День</b>\n";
  1820. print "<select name=\"day\" >\n";
  1821. for ($i = 1; $i <= 31; $i++) {
  1822. print_select_item($i, $i, $dd);
  1823. }
  1824. print "</select>\n";
  1825. }
  1826. if ($mm >= 1) {
  1827. print "<b>Месяц</b>\n";
  1828. print "<select name=\"month\" >\n";
  1829. for ($i = 1; $i <= 12; $i++) {
  1830. $tmp_date = DateTimeImmutable::createFromFormat('U', strtotime("$i/01/$yy"));
  1831. $month_name = $tmp_date->format('F');
  1832. print_select_item($month_name, $i, $mm);
  1833. }
  1834. print "</select>\n";
  1835. }
  1836. print "<b>Год</b>\n";
  1837. print_year_select('year', $yy);
  1838. }
  1839. function print_date_select2($dd, $mm, $yy)
  1840. {
  1841. if ($dd >= 1) {
  1842. print "<b>День</b>\n";
  1843. print "<select name=\"day2\" >\n";
  1844. for ($i = 1; $i <= 31; $i++) {
  1845. print_select_item($i, $i, $dd);
  1846. }
  1847. print "</select>\n";
  1848. }
  1849. if ($mm >= 1) {
  1850. print "<b>Месяц</b>\n";
  1851. print "<select name=\"month2\" >\n";
  1852. for ($i = 1; $i <= 12; $i++) {
  1853. $tmp_date = DateTimeImmutable::createFromFormat('U', strtotime("$i/01/$yy"));
  1854. $month_name = $tmp_date->format('F');
  1855. print_select_item($month_name, $i, $mm);
  1856. }
  1857. print "</select>\n";
  1858. }
  1859. print "<b>Год</b>\n";
  1860. print_year_select('year2', $yy);
  1861. }
  1862. function is_up($ip)
  1863. {
  1864. if (!isset($ip) or strlen($ip) == 0) {
  1865. return false;
  1866. }
  1867. exec(sprintf('ping -i .3 -c 1 -W 5 %s', escapeshellarg($ip)), $res, $rval);
  1868. return $rval == 0;
  1869. }
  1870. function get_ifmib_index_table($ip, $community, $version)
  1871. {
  1872. $ifmib_map = NULL;
  1873. $is_mikrotik = walk_snmp($ip, $community, $version, MIKROTIK_DHCP_SERVER);
  1874. $mk_ros_version = 0;
  1875. if ($is_mikrotik) {
  1876. $mikrotik_version = walk_snmp($ip, $community, $version, MIKROTIK_ROS_VERSION);
  1877. $mk_ros_version = 6491;
  1878. $result = preg_match('/RouterOS\s+(\d)\.(\d{1,3})\.(\d{1,3})\s+/', $mikrotik_version[MIKROTIK_ROS_VERSION], $matches);
  1879. if ($result) {
  1880. $mk_ros_version = $matches[1] * 1000 + $matches[2] * 10 + $matches[3];
  1881. }
  1882. }
  1883. if ($mk_ros_version == 0 or $mk_ros_version > 6468) {
  1884. #fdb_index => snmp_index
  1885. $index_map_table = walk_snmp($ip, $community, $version, IFMIB_IFINDEX_MAP);
  1886. #get map snmp interfaces to fdb table
  1887. if (isset($index_map_table) and count($index_map_table) > 0) {
  1888. foreach ($index_map_table as $key => $value) {
  1889. $key = trim($key);
  1890. $value = intval(trim(str_replace('INTEGER:', '', $value)));
  1891. $result = preg_match('/\.(\d{1,10})$/', $key, $matches);
  1892. if ($result) {
  1893. $fdb_index = preg_replace('/^\./', '', $matches[0]);
  1894. $ifmib_map[$fdb_index] = $value;
  1895. }
  1896. }
  1897. }
  1898. }
  1899. #return simple map snmp_port_index = snmp_port_index
  1900. if (empty($ifmib_map)) {
  1901. #ifindex
  1902. $index_table = walk_snmp($ip, $community, $version, IFMIB_IFINDEX);
  1903. if (isset($index_table) and count($index_table) > 0) {
  1904. foreach ($index_table as $key => $value) {
  1905. $key = trim($key);
  1906. $value = intval(trim(str_replace('INTEGER:', '', $value)));
  1907. $result = preg_match('/\.(\d{1,10})$/', $key, $matches);
  1908. if ($result) {
  1909. $fdb_index = preg_replace('/^\./', '', $matches[0]);
  1910. $ifmib_map[$fdb_index] = $value;
  1911. }
  1912. }
  1913. }
  1914. }
  1915. return $ifmib_map;
  1916. }
  1917. #get mac table by selected snmp oid
  1918. function get_mac_table($ip, $community, $version, $oid, $index_map)
  1919. {
  1920. if (!isset($ip)) {
  1921. return;
  1922. }
  1923. if (!isset($oid)) {
  1924. return;
  1925. }
  1926. if (!isset($community)) {
  1927. $community = 'public';
  1928. }
  1929. if (!isset($version)) {
  1930. $version = '2';
  1931. }
  1932. $mac_table = walk_snmp($ip, $community, $version, $oid);
  1933. if (isset($mac_table) and count($mac_table) > 0) {
  1934. foreach ($mac_table as $key => $value) {
  1935. if (empty($value)) {
  1936. continue;
  1937. }
  1938. if (empty($key)) {
  1939. continue;
  1940. }
  1941. $key = trim($key);
  1942. $value_raw = intval(trim(str_replace('INTEGER:', '', $value)));
  1943. if (empty($value_raw)) {
  1944. continue;
  1945. }
  1946. if (!empty($index_map)) {
  1947. if (empty($index_map[$value_raw])) {
  1948. $value = $value_raw;
  1949. } else {
  1950. $value = $index_map[$value_raw];
  1951. }
  1952. } else {
  1953. $value = $value_raw;
  1954. }
  1955. $pattern = '/\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})$/';
  1956. $result = preg_match($pattern, $key, $matches);
  1957. if (!empty($result)) {
  1958. $mac_key = preg_replace('/^\./', '', $matches[0]);
  1959. $fdb_table[$mac_key] = $value;
  1960. }
  1961. }
  1962. }
  1963. return $fdb_table;
  1964. }
  1965. #get mac table by analyze all available tables
  1966. function get_fdb_table($ip, $community, $version)
  1967. {
  1968. if (!isset($ip)) {
  1969. return;
  1970. }
  1971. if (!isset($community)) {
  1972. $community = 'public';
  1973. }
  1974. if (!isset($version)) {
  1975. $version = '2';
  1976. }
  1977. $ifindex_map = get_ifmib_index_table($ip, $community, $version);
  1978. $fdb1_table = get_mac_table($ip, $community, $version, MAC_TABLE_OID, $ifindex_map);
  1979. if (!empty($fdb1_table)) {
  1980. $fdb_table = $fdb1_table;
  1981. } else {
  1982. $fdb2_table = get_mac_table($ip, $community, $version, MAC_TABLE_OID2, $ifindex_map);
  1983. if (!empty($fdb2_table)) {
  1984. $fdb_table = $fdb2_table;
  1985. }
  1986. }
  1987. // maybe cisco?!
  1988. if (!isset($fdb_table) or empty($fdb_table) or count($fdb_table) == 0) {
  1989. $vlan_table = walk_snmp($ip, $community, $version, CISCO_VLAN_OID);
  1990. if (empty($vlan_table)) {
  1991. return;
  1992. }
  1993. foreach ($vlan_table as $vlan_oid => $value) {
  1994. if (empty($vlan_oid)) {
  1995. continue;
  1996. }
  1997. $pattern = '/\.(\d{1,4})$/';
  1998. $result = preg_match($pattern, $vlan_oid, $matches);
  1999. if (!empty($result)) {
  2000. $vlan_id = preg_replace('/^\./', '', $matches[0]);
  2001. if ($vlan_id > 1000 and $vlan_id < 1009) {
  2002. continue;
  2003. }
  2004. $fdb_vlan_table = get_mac_table($ip, $community . '@' . $vlan_id, $version, MAC_TABLE_OID, $ifindex_map);
  2005. if (!isset($fdb_vlan_table) or !$fdb_vlan_table or count($fdb_vlan_table) == 0) {
  2006. $fdb_vlan_table = get_mac_table($ip, $community, $version, MAC_TABLE_OID2, $ifindex_map);
  2007. }
  2008. foreach ($fdb_vlan_table as $mac => $port) {
  2009. if (!isset($mac)) {
  2010. continue;
  2011. }
  2012. $fdb_table[$mac] = $port;
  2013. }
  2014. }
  2015. }
  2016. }
  2017. return $fdb_table;
  2018. }
  2019. function check_snmp_access($ip, $community, $version)
  2020. {
  2021. if (!isset($ip)) {
  2022. return;
  2023. }
  2024. if (!isset($community)) {
  2025. $community = 'public';
  2026. }
  2027. if (!isset($version)) {
  2028. $version = '2';
  2029. }
  2030. #check host up
  2031. $status = exec(escapeshellcmd("ping -W 1 -i 1 -c 3 " . $ip));
  2032. if (empty($status)) {
  2033. return;
  2034. }
  2035. #check snmp
  2036. $result = get_snmp($ip, $community, $version, SYS_DESCR_MIB);
  2037. if (empty($result)) {
  2038. return;
  2039. }
  2040. return 1;
  2041. }
  2042. function get_port_state($port, $ip, $community, $version)
  2043. {
  2044. if (!isset($port)) {
  2045. return;
  2046. }
  2047. if (!isset($ip)) {
  2048. return;
  2049. }
  2050. if (!isset($community)) {
  2051. $community = 'public';
  2052. }
  2053. if (!isset($version)) {
  2054. $version = '2';
  2055. }
  2056. $port_oid = PORT_STATUS_OID . $port;
  2057. $port_state = get_snmp($ip, $community, $version, $port_oid);
  2058. return $port_state;
  2059. }
  2060. function get_last_digit($oid)
  2061. {
  2062. if (!isset($oid)) {
  2063. return;
  2064. }
  2065. $pattern = '/\.(\d{1,})$/';
  2066. preg_match($pattern, $oid, $matches);
  2067. return $matches[1];
  2068. }
  2069. function get_cisco_sensors($ip, $community, $version, $mkey)
  2070. {
  2071. $index = get_last_digit($mkey);
  2072. $result = parse_snmp_value(get_snmp($ip, $community, $version, CISCO_SFP_SENSORS . "." . $index));
  2073. $prec = parse_snmp_value(get_snmp($ip, $community, $version, CISCO_SFP_PRECISION . "." . $index));
  2074. if (!isset($prec)) {
  2075. $prec = 1;
  2076. }
  2077. $result = round(trim($result) / (10 * $prec), 2);
  2078. return $result;
  2079. }
  2080. function get_snmp_ifname1($ip, $community, $version, $port)
  2081. {
  2082. $port_name = parse_snmp_value(get_snmp($ip, $community, $version, IFMIB_IFNAME . "." . $port));
  2083. return $port_name;
  2084. }
  2085. function get_snmp_ifname2($ip, $community, $version, $port)
  2086. {
  2087. $port_name = parse_snmp_value(get_snmp($ip, $community, $version, IFMIB_IFDESCR . "." . $port));
  2088. return $port_name;
  2089. }
  2090. function get_snmp_interfaces($ip, $community, $version)
  2091. {
  2092. $result = walk_snmp($ip, $community, $version, IFMIB_IFNAME);
  2093. if (empty($result)) {
  2094. $result = walk_snmp($ip, $community, $version, IFMIB_IFDESCR);
  2095. }
  2096. return $result;
  2097. }
  2098. function walk_snmp($ip, $community, $version, $oid)
  2099. {
  2100. snmp_set_oid_output_format(SNMP_OID_OUTPUT_NUMERIC);
  2101. if ($version == 2) {
  2102. $result = snmp2_real_walk($ip, $community, $oid, SNMP_timeout, SNMP_retry);
  2103. }
  2104. if ($version == 1) {
  2105. $result = snmprealwalk($ip, $community, $oid, SNMP_timeout, SNMP_retry);
  2106. }
  2107. return $result;
  2108. }
  2109. function get_snmp_module_id($modules_oids, $port_name)
  2110. {
  2111. $port_name = preg_quote(trim($port_name), '/');
  2112. foreach ($modules_oids as $key => $value) {
  2113. $pattern = '/' . $port_name . '/i';
  2114. preg_match($pattern, $value, $matches);
  2115. if (isset($matches[0])) {
  2116. $module_id = get_last_digit($key);
  2117. return $module_id;
  2118. }
  2119. }
  2120. return;
  2121. }
  2122. function get_sfp_status($vendor_id, $port, $ip, $community, $version, $modules_oids)
  2123. {
  2124. if (!isset($vendor_id)) {
  2125. return;
  2126. }
  2127. if (!isset($port)) {
  2128. return;
  2129. }
  2130. if (!isset($ip)) {
  2131. return;
  2132. }
  2133. if (!isset($community)) {
  2134. $community = 'public';
  2135. }
  2136. if (!isset($version)) {
  2137. $version = '2';
  2138. }
  2139. $status = '';
  2140. // eltex
  2141. if ($vendor_id == 2) {
  2142. $sfp_vendor = parse_snmp_value(get_snmp($ip, $community, $version, ELTEX_SFP_VENDOR . "." . $port));
  2143. if (isset($sfp_vendor)) {
  2144. $sfp_status_temp = ELTEX_SFP_STATUS . "." . $port . ".5";
  2145. $sfp_status_volt = ELTEX_SFP_STATUS . "." . $port . ".6";
  2146. $sfp_status_circut = ELTEX_SFP_STATUS . "." . $port . ".7";
  2147. $sfp_status_tx = ELTEX_SFP_STATUS . "." . $port . ".8";
  2148. $sfp_status_rx = ELTEX_SFP_STATUS . "." . $port . ".9";
  2149. $temp = parse_snmp_value(get_snmp($ip, $community, $version, $sfp_status_temp));
  2150. $volt = parse_snmp_value(get_snmp($ip, $community, $version, $sfp_status_volt));
  2151. $circut = parse_snmp_value(get_snmp($ip, $community, $version, $sfp_status_circut));
  2152. $tx = parse_snmp_value(get_snmp($ip, $community, $version, $sfp_status_tx));
  2153. $rx = parse_snmp_value(get_snmp($ip, $community, $version, $sfp_status_rx));
  2154. $sfp_sn = parse_snmp_value(get_snmp($ip, $community, $version, ELTEX_SFP_SN . "." . $port));
  2155. $sfp_freq = parse_snmp_value(get_snmp($ip, $community, $version, ELTEX_SFP_FREQ . "." . $port));
  2156. if (!isset($sfp_freq) or $sfp_freq == 65535) {
  2157. $sfp_freq = 'unspecified';
  2158. }
  2159. $sfp_length = parse_snmp_value(get_snmp($ip, $community, $version, ELTEX_SFP_LENGTH . "." . $port));
  2160. $status = 'Vendor: ' . $sfp_vendor . ' Serial: ' . $sfp_sn . ' Laser: ' . $sfp_freq . ' Distance: ' . $sfp_length . '<br>';
  2161. if (!empty($sfp_status_temp) and $temp > 0.1) {
  2162. $status .= 'Temp: ' . $temp . " C";
  2163. }
  2164. if (!empty($sfp_status_volt) and $volt > 0.1) {
  2165. $status .= ' Volt: ' . round($volt / 1000000, 2) . ' V';
  2166. }
  2167. if (!empty($sfp_status_circut) and $circut > 0.1) {
  2168. $status .= ' Circut: ' . round($circut / 1000, 2) . ' mA';
  2169. }
  2170. if (!empty($sfp_status_tx) and $tx > 0.1) {
  2171. $status .= ' Tx: ' . round($tx / 1000, 2) . ' dBm';
  2172. }
  2173. if (!empty($sfp_status_rx) and $rx > 0.1) {
  2174. $status .= ' Rx: ' . round($rx / 1000, 2) . ' dBm';
  2175. }
  2176. $status .= '<br>';
  2177. return $status;
  2178. }
  2179. return;
  2180. }
  2181. // cisco
  2182. if ($vendor_id == 16) {
  2183. // get interface names
  2184. $port_name = parse_snmp_value(get_snmp($ip, $community, $version, IFMIB_IFNAME . "." . $port));
  2185. if (empty($port_name)) {
  2186. $port_name = parse_snmp_value(get_snmp($ip, $community, $version, IFMIB_IFDESCR . "." . $port));
  2187. }
  2188. // search module indexes
  2189. $port_name = preg_quote(trim($port_name), '/');
  2190. foreach ($modules_oids as $key => $value) {
  2191. $pattern = '/(' . $port_name . ' Module Temperature Sensor)/i';
  2192. preg_match($pattern, $value, $matches);
  2193. if (isset($matches[0])) {
  2194. $temp = get_cisco_sensors($ip, $community, $version, $key);
  2195. continue;
  2196. }
  2197. $pattern = '/(' . $port_name . ' Supply Voltage Sensor)/i';
  2198. preg_match($pattern, $value, $matches);
  2199. if (isset($matches[0])) {
  2200. $volt = get_cisco_sensors($ip, $community, $version, $key);
  2201. continue;
  2202. }
  2203. $pattern = '/(' . $port_name . ' Bias Current Sensor)/i';
  2204. preg_match($pattern, $value, $matches);
  2205. if (isset($matches[0])) {
  2206. $circut = get_cisco_sensors($ip, $community, $version, $key);
  2207. continue;
  2208. }
  2209. $pattern = '/(' . $port_name . ' Transmit Power Sensor)/i';
  2210. preg_match($pattern, $value, $matches);
  2211. if (isset($matches[0])) {
  2212. $tx = get_cisco_sensors($ip, $community, $version, $key);
  2213. continue;
  2214. }
  2215. $pattern = '/(' . $port_name . ' Receive Power Sensor)/i';
  2216. preg_match($pattern, $value, $matches);
  2217. if (isset($matches[0])) {
  2218. $rx = get_cisco_sensors($ip, $community, $version, $key);
  2219. continue;
  2220. }
  2221. }
  2222. if (!empty($temp) and $temp > 0) {
  2223. $status .= 'Temp: ' . $temp . " C";
  2224. }
  2225. if (!empty($volt) and $volt > 0) {
  2226. $status .= ' Volt: ' . $volt . ' V';
  2227. }
  2228. if (!empty($circut) and $circut > 0) {
  2229. $status .= ' Circut: ' . $circut . ' mA';
  2230. }
  2231. if (!empty($tx) and abs($tx) > 0.1) {
  2232. $status .= ' Tx: ' . $tx . ' dBm';
  2233. }
  2234. if (!empty($rx) and abs($rx) > 0.1) {
  2235. $status .= ' Rx: ' . $rx . ' dBm';
  2236. }
  2237. if (!empty($status)) {
  2238. $status = preg_replace('/"/', '', $status);
  2239. $status .= '<br>';
  2240. }
  2241. return $status;
  2242. }
  2243. // huawei
  2244. if ($vendor_id == 3) {
  2245. // get interface names
  2246. $port_name = parse_snmp_value(get_snmp($ip, $community, $version, IFMIB_IFNAME . "." . $port));
  2247. if (empty($port_name)) {
  2248. $port_name = parse_snmp_value(get_snmp($ip, $community, $version, IFMIB_IFDESCR . "." . $port));
  2249. }
  2250. // search module indexes
  2251. $port_name = preg_quote(trim($port_name), '/');
  2252. foreach ($modules_oids as $key => $value) {
  2253. $pattern = '/' . $port_name . '/i';
  2254. preg_match($pattern, $value, $matches);
  2255. if (isset($matches[0])) {
  2256. $module_id = get_last_digit($key);
  2257. unset($result);
  2258. $result = parse_snmp_value(get_snmp($ip, $community, $version, HUAWEI_SFP_VENDOR . "." . $module_id));
  2259. if (!empty($result)) {
  2260. $sfp_vendor = $result;
  2261. }
  2262. unset($result);
  2263. $result = parse_snmp_value(get_snmp($ip, $community, $version, HUAWEI_SFP_SPEED . "." . $module_id));
  2264. if (!empty($result)) {
  2265. list($sfp_speed, $spf_lenght, $sfp_type) = explode('-', $result);
  2266. if ($sfp_type == 0) {
  2267. $sfp_type = 'MultiMode';
  2268. }
  2269. if ($sfp_type == 1) {
  2270. $sfp_type = 'SingleMode';
  2271. }
  2272. }
  2273. $volt = parse_snmp_value(get_snmp($ip, $community, $version, HUAWEI_SFP_VOLT . "." . $module_id));
  2274. $circut = parse_snmp_value(get_snmp($ip, $community, $version, HUAWEI_SFP_BIASCURRENT . "." . $module_id));
  2275. $tx = parse_snmp_value(get_snmp($ip, $community, $version, HUAWEI_SFP_OPTTX . "." . $module_id));
  2276. $rx = parse_snmp_value(get_snmp($ip, $community, $version, HUAWEI_SFP_OPTRX . "." . $module_id));
  2277. if (!isset($tx)) {
  2278. $tx = parse_snmp_value(get_snmp($ip, $community, $version, HUAWEI_SFP_TX . "." . $module_id));
  2279. }
  2280. if (!isset($rx)) {
  2281. $rx = parse_snmp_value(get_snmp($ip, $community, $version, HUAWEI_SFP_RX . "." . $module_id));
  2282. }
  2283. if (!empty($sfp_vendor)) {
  2284. $status .= ' Name:' . $sfp_vendor . '<br>';
  2285. }
  2286. // if (isset($sfp_speed)) { $status .= ' ' . $sfp_speed; }
  2287. // if (isset($spf_lenght)) { $status .= ' ' . $spf_lenght; }
  2288. if ($volt > 0) {
  2289. $status .= ' Volt: ' . round($volt / 1000, 2) . ' V';
  2290. }
  2291. if (!empty($circut) and $circut > 0) {
  2292. $status .= ' Circut: ' . $circut . ' mA <br>';
  2293. }
  2294. if (!empty($tx)) {
  2295. $tx = preg_replace('/"/', '', $tx);
  2296. list($tx_dbm, $pattern) = explode('.', $tx);
  2297. $tx_dbm = round(trim($tx_dbm) / 100, 2);
  2298. if (abs($tx_dbm) > 0.1) {
  2299. $status .= ' Tx: ' . $tx_dbm . ' dBm';
  2300. }
  2301. }
  2302. if (!empty($rx)) {
  2303. $rx = preg_replace('/"/', '', $rx);
  2304. list($rx_dbm, $pattern) = explode('.', $rx);
  2305. $rx_dbm = round(trim($rx_dbm) / 100, 2);
  2306. if (abs($rx_dbm) > 0.1) {
  2307. $status .= ' Rx: ' . $rx_dbm . ' dBm';
  2308. }
  2309. }
  2310. break;
  2311. }
  2312. }
  2313. if (isset($status)) {
  2314. $status = preg_replace('/"/', '', $status);
  2315. $status .= '<br>';
  2316. }
  2317. return $status;
  2318. }
  2319. return;
  2320. }
  2321. function get_port_vlan($vendor, $port_index, $ip, $community, $version)
  2322. {
  2323. if (!isset($port_index)) {
  2324. return;
  2325. }
  2326. if (!isset($ip)) {
  2327. return;
  2328. }
  2329. if (!isset($community)) {
  2330. $community = 'public';
  2331. }
  2332. if (!isset($version)) {
  2333. $version = '2';
  2334. }
  2335. if ($vendor == 69) {
  2336. $port_oid = TPLINK_VLAN_PVID . "." . $port_index;
  2337. } else {
  2338. $port_oid = PORT_VLAN_OID . "." . $port_index;
  2339. }
  2340. $port_vlan = get_snmp($ip, $community, $version, $port_oid);
  2341. $port_vlan = preg_replace('/.*\:/', '', $port_vlan);
  2342. $port_vlan = intval(trim($port_vlan));
  2343. return $port_vlan;
  2344. }
  2345. function get_port_poe_state($vendor_id, $port, $port_snmp_index, $ip, $community = 'public', $version = '2')
  2346. {
  2347. if (!isset($port)) {
  2348. return;
  2349. }
  2350. if (!isset($ip)) {
  2351. return;
  2352. }
  2353. // default poe oid
  2354. $poe_status = PETH_PSE_PORT_ADMIN_ENABLE . "." . $port_snmp_index;
  2355. if ($vendor_id == 3) {
  2356. $poe_status = HUAWEI_POE_OID . "." . $port_snmp_index;
  2357. }
  2358. if ($vendor_id == 6) {
  2359. $poe_status = SNR_POE_OID . "." . $port_snmp_index;
  2360. }
  2361. if ($vendor_id == 8) {
  2362. $poe_status = ALLIED_POE_OID . "." . $port_snmp_index;
  2363. }
  2364. if ($vendor_id == 15) {
  2365. $poe_status = HP_POE_OID . "." . $port_snmp_index;
  2366. }
  2367. if ($vendor_id == 9) {
  2368. $poe_status = MIKROTIK_POE_OID . "." . $port_snmp_index;
  2369. }
  2370. if ($vendor_id == 10) {
  2371. $poe_status = NETGEAR_POE_OID . "." . $port_snmp_index;
  2372. }
  2373. if ($vendor_id == 69) {
  2374. $poe_status = TPLINK_POE_OID . "." . $port;
  2375. }
  2376. $result = '';
  2377. $c_state = get_snmp($ip, $community, $version, $poe_status);
  2378. if (!empty($c_state)) {
  2379. $p_state = parse_snmp_value($c_state);
  2380. // patch for mikrotik
  2381. if ($vendor_id == 9) {
  2382. if ($p_state == 1) {
  2383. return 2;
  2384. }
  2385. if ($p_state > 1) {
  2386. return 1;
  2387. }
  2388. }
  2389. //patch for tplink
  2390. if ($vendor_id == 69) {
  2391. if ($p_state == 0) {
  2392. return 2;
  2393. }
  2394. if ($p_state >= 1) {
  2395. return 1;
  2396. }
  2397. }
  2398. return $p_state;
  2399. }
  2400. return;
  2401. }
  2402. function set_port_poe_state($vendor_id, $port, $port_snmp_index, $ip, $community, $version, $state)
  2403. {
  2404. if (!isset($ip)) {
  2405. return;
  2406. }
  2407. if (!isset($community)) {
  2408. $community = 'public';
  2409. }
  2410. if (!isset($version)) {
  2411. $version = '2';
  2412. }
  2413. //default poe status
  2414. $poe_enable = 1;
  2415. $poe_disable = 2;
  2416. // default poe oid
  2417. $poe_status = PETH_PSE_PORT_ADMIN_ENABLE . "." . $port_snmp_index;
  2418. if ($vendor_id == 3) {
  2419. $poe_status = HUAWEI_POE_OID . "." . $port_snmp_index;
  2420. }
  2421. if ($vendor_id == 8) {
  2422. $poe_status = ALLIED_POE_OID . "." . $port_snmp_index;
  2423. }
  2424. if ($vendor_id == 15) {
  2425. $poe_status = HP_POE_OID . "." . $port_snmp_index;
  2426. }
  2427. if ($vendor_id == 10) {
  2428. $poe_status = NETGEAR_POE_OID . "." . $port_snmp_index;
  2429. }
  2430. if ($vendor_id == 69) {
  2431. $poe_status = TPLINK_POE_OID . "." . $port;
  2432. $poe_enable = 1;
  2433. $poe_disable = 0;
  2434. }
  2435. if ($state) {
  2436. // enable port
  2437. $c_state = set_snmp($ip, $community, $version, $poe_status, 'i', $poe_enable);
  2438. return $c_state;
  2439. } else {
  2440. // disable port
  2441. $c_state = set_snmp($ip, $community, $version, $poe_status, 'i', $poe_disable);
  2442. return $c_state;
  2443. }
  2444. }
  2445. function get_port_poe_detail($vendor_id, $port, $port_snmp_index, $ip, $community, $version)
  2446. {
  2447. if (!isset($port) or !isset($port_snmp_index)) {
  2448. return;
  2449. }
  2450. if (!isset($ip)) {
  2451. return;
  2452. }
  2453. if (!isset($community)) {
  2454. $community = 'public';
  2455. }
  2456. if (!isset($version)) {
  2457. $version = '2';
  2458. }
  2459. $result = '';
  2460. $poe_class = PETH_PSE_PORT_POE_CLASS . $port_snmp_index;
  2461. // eltex
  2462. if ($vendor_id == 2) {
  2463. $poe_power = ELTEX_POE_USAGE . '.' . $port_snmp_index;
  2464. $poe_current = ELTEX_POE_CURRENT . '.' . $port_snmp_index;
  2465. $poe_volt = ELTEX_POE_VOLT . '.' . $port_snmp_index;
  2466. }
  2467. // huawei
  2468. if ($vendor_id == 3) {
  2469. $poe_power = HUAWEI_POE_USAGE . '.' . $port_snmp_index;
  2470. $poe_current = HUAWEI_POE_CURRENT . '.' . $port_snmp_index;
  2471. $poe_volt = HUAWEI_POE_VOLT . '.' . $port_snmp_index;
  2472. }
  2473. // snr
  2474. if ($vendor_id == 6) {
  2475. $poe_class = SNR_POE_CLASS . '.' . $port_snmp_index;
  2476. $poe_power = SNR_POE_USAGE . '.' . $port_snmp_index;
  2477. $poe_current = SNR_POE_CURRENT . '.' . $port_snmp_index;
  2478. $poe_volt = SNR_POE_VOLT . '.' . $port_snmp_index;
  2479. }
  2480. // AT
  2481. if ($vendor_id == 8) {
  2482. $poe_power = ALLIED_POE_USAGE . '.' . $port_snmp_index;
  2483. $poe_current = ALLIED_POE_CURRENT . '.' . $port_snmp_index;
  2484. $poe_volt = ALLIED_POE_VOLT . '.' . $port_snmp_index;
  2485. }
  2486. // mikrotik
  2487. if ($vendor_id == 9) {
  2488. $poe_power = MIKROTIK_POE_USAGE . '.' . $port_snmp_index;
  2489. $poe_current = MIKROTIK_POE_CURRENT . '.' . $port_snmp_index;
  2490. $poe_volt = MIKROTIK_POE_VOLT . '.' . $port_snmp_index;
  2491. }
  2492. // netgear
  2493. if ($vendor_id == 10) {
  2494. $poe_power = NETGEAR_POE_USAGE . '.' . $port_snmp_index;
  2495. $poe_current = NETGEAR_POE_CURRENT . '.' . $port_snmp_index;
  2496. $poe_volt = NETGEAR_POE_VOLT . '.' . $port_snmp_index;
  2497. }
  2498. // HP
  2499. if ($vendor_id == 15) {
  2500. $poe_power = HP_POE_USAGE . '.' . $port_snmp_index;
  2501. $poe_volt = HP_POE_VOLT . '.' . $port_snmp_index;
  2502. }
  2503. // TP-Link
  2504. if ($vendor_id == 69) {
  2505. $poe_power = TPLINK_POE_USAGE . '.' . $port;
  2506. $poe_current = TPLINK_POE_CURRENT . '.' . $port;
  2507. $poe_volt = TPLINK_POE_VOLT . '.' . $port;
  2508. $poe_class = TPLINK_POE_CLASS . "." . $port;
  2509. }
  2510. if (isset($poe_power)) {
  2511. $c_power = get_snmp($ip, $community, $version, $poe_power);
  2512. if (isset($c_power)) {
  2513. $p_power = parse_snmp_value($c_power);
  2514. switch ($vendor_id) {
  2515. case 9:
  2516. $p_power = round($p_power / 10, 2);
  2517. break;
  2518. case 69:
  2519. $p_power = round($p_power / 10, 2);
  2520. break;
  2521. default:
  2522. $p_power = round($p_power / 1000, 2);
  2523. break;
  2524. }
  2525. if ($p_power > 0) {
  2526. $result .= ' P: ' . $p_power . ' W';
  2527. }
  2528. }
  2529. }
  2530. if (isset($poe_current)) {
  2531. $c_current = get_snmp($ip, $community, $version, $poe_current);
  2532. if (isset($c_current)) {
  2533. $p_current = parse_snmp_value($c_current);
  2534. if ($p_current > 0) {
  2535. $result .= ' C: ' . $p_current . ' mA';
  2536. }
  2537. }
  2538. }
  2539. if (isset($poe_volt)) {
  2540. $c_volt = get_snmp($ip, $community, $version, $poe_volt);
  2541. if (isset($c_volt)) {
  2542. $p_volt = parse_snmp_value($c_volt);
  2543. switch ($vendor_id) {
  2544. case 2:
  2545. case 8:
  2546. $p_volt = round($p_volt / 1000, 2);
  2547. break;
  2548. case 9:
  2549. case 69:
  2550. $p_volt = round($p_volt / 10, 2);
  2551. break;
  2552. case 15:
  2553. $p_volt = round($p_volt / 100, 2);
  2554. break;
  2555. }
  2556. if ($p_volt > 0 and $p_power > 0) {
  2557. $result .= ' V: ' . $p_volt . " V";
  2558. }
  2559. }
  2560. }
  2561. if (isset($poe_class)) {
  2562. $c_class = get_snmp($ip, $community, $version, $poe_class);
  2563. if (isset($c_class)) {
  2564. $p_class = parse_snmp_value($c_class);
  2565. switch ($vendor_id) {
  2566. case 69:
  2567. if ($p_class > 0 and $p_power > 0) {
  2568. if ($p_class == 7) {
  2569. $p_class = 'class-not-defined';
  2570. }
  2571. $result .= ' Class: ' . $p_class;
  2572. }
  2573. break;
  2574. default:
  2575. if ($p_class > 0 and $p_power > 0) {
  2576. $result .= ' Class: ' . ($p_class - 1);
  2577. }
  2578. break;
  2579. }
  2580. }
  2581. }
  2582. return $result;
  2583. }
  2584. function get_snmp($ip, $community, $version, $oid)
  2585. {
  2586. snmp_set_oid_output_format(SNMP_OID_OUTPUT_NUMERIC);
  2587. if ($version == 2) {
  2588. $result = snmp2_get($ip, $community, $oid, SNMP_timeout, SNMP_retry);
  2589. }
  2590. if ($version == 1) {
  2591. $result = snmpget($ip, $community, $oid, SNMP_timeout, SNMP_retry);
  2592. }
  2593. return $result;
  2594. }
  2595. function set_snmp($ip, $community, $version, $oid, $field, $value)
  2596. {
  2597. if ($version == 2) {
  2598. $result = snmp2_set($ip, $community, $oid, $field, $value, SNMP_timeout, SNMP_retry);
  2599. }
  2600. if ($version == 1) {
  2601. $result = snmpset($ip, $community, $oid, $field, $value, SNMP_timeout, SNMP_retry);
  2602. }
  2603. return $result;
  2604. }
  2605. function set_port_state($vendor_id, $port, $ip, $community, $version, $state)
  2606. {
  2607. // port -> snmp_index!!!
  2608. if (!isset($port)) {
  2609. return;
  2610. }
  2611. if (!isset($ip)) {
  2612. return;
  2613. }
  2614. if (!isset($community)) {
  2615. $community = 'public';
  2616. }
  2617. if (!isset($version)) {
  2618. $version = '2';
  2619. }
  2620. $port_status = PORT_ADMIN_STATUS_OID . $port;
  2621. if ($state == 1) {
  2622. // enable port
  2623. $c_state = set_snmp($ip, $community, $version, $port_status, 'i', 1);
  2624. return $c_state;
  2625. } else {
  2626. // disable port
  2627. $c_state = set_snmp($ip, $community, $version, $port_status, 'i', 2);
  2628. return $c_state;
  2629. }
  2630. }
  2631. function set_port_for_group($db, $group_id, $place_id, $state)
  2632. {
  2633. $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;
  2634. $auth_list = mysqli_query($db, $authSQL);
  2635. LOG_INFO($db, 'Mass port state change started!');
  2636. // get auth list for group
  2637. while (list($a_id, $a_name, $a_ip) = mysqli_fetch_array($auth_list)) {
  2638. // get device and port for auth
  2639. if ($place_id == 0) {
  2640. $place_filter = '';
  2641. } else {
  2642. $place_filter = 'D.building_id=' . $place_id . ' and ';
  2643. }
  2644. $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';
  2645. $dev_info = mysqli_query($db, $devSQL);
  2646. 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);
  2647. if (!isset($d_id)) {
  2648. continue;
  2649. }
  2650. if ($state == 1) {
  2651. $mode = 'enable';
  2652. run_sql($db, "Update User_auth set nagios_handler='restart-port' WHERE id=$a_id and nagios_handler='manual-mode'");
  2653. } else {
  2654. $mode = 'disable';
  2655. run_sql($db, "Update User_auth set nagios_handler='manual-mode' WHERE id=$a_id and nagios_handler='restart-port'");
  2656. }
  2657. LOG_INFO($db, "At device $d_name [$d_ip] $mode port $d_port for auth_id: $a_id ($a_ip [$a_name])");
  2658. set_port_state($d_vendor_id, $d_snmp_index, $d_ip, $d_community, $d_snmp, $state);
  2659. set_port_poe_state($d_vendor_id, $d_port, $d_snmp_index, $d_ip, $d_community, $d_snmp, $state);
  2660. }
  2661. LOG_INFO($db, 'Mass port state change stopped.');
  2662. }
  2663. function get_vendor($db, $mac)
  2664. {
  2665. $mac = mac_dotted($mac);
  2666. $vendor = get_record_sql($db, 'SELECT companyName,companyAddress FROM mac_vendors WHERE oui="' . substr($mac, 0, 8) . '" or oui="' . substr($mac, 0, 11) . '"');
  2667. $result = '';
  2668. if (!empty($vendor)) {
  2669. $result = $vendor['companyName'];
  2670. if (!empty($vendor['companyAddress'])) {
  2671. $result = $vendor['companyAddress'];
  2672. }
  2673. }
  2674. return $result;
  2675. }
  2676. function get_port_state_detail($port, $ip, $community, $version)
  2677. {
  2678. if (!isset($port)) {
  2679. return;
  2680. }
  2681. if (!isset($ip)) {
  2682. return;
  2683. }
  2684. if (!isset($community)) {
  2685. $community = 'public';
  2686. }
  2687. if (!isset($version)) {
  2688. $version = '2';
  2689. }
  2690. // if (!is_up($ip)) { return; }
  2691. $oper = PORT_STATUS_OID . $port;
  2692. $admin = PORT_ADMIN_STATUS_OID . $port;
  2693. $speed = PORT_SPEED_OID . $port;
  2694. $errors = PORT_ERRORS_OID . $port;
  2695. $result = '';
  2696. $c_state = get_snmp($ip, $community, $version, $oper);
  2697. $p_state = parse_snmp_value($c_state);
  2698. $c_admin = get_snmp($ip, $community, $version, $admin);
  2699. $p_admin = parse_snmp_value($c_admin);
  2700. if ($p_state == 1) {
  2701. $c_speed = get_snmp($ip, $community, $version, $speed);
  2702. } else {
  2703. $c_speed = 'INT:0';
  2704. }
  2705. $p_speed = parse_snmp_value($c_speed);
  2706. $c_errors = get_snmp($ip, $community, $version, $errors);
  2707. $p_errors = parse_snmp_value($c_errors);
  2708. $result = $p_state . ";" . $p_admin . ";" . $p_speed . ";" . $p_errors;
  2709. return $result;
  2710. }
  2711. function parse_snmp_value($value)
  2712. {
  2713. if (empty($value)) {
  2714. return NULL;
  2715. }
  2716. list($p_type, $p_value) = explode(':', $value);
  2717. $p_value = trim($p_value);
  2718. $p_value = preg_replace('/^\"/', '', $p_value);
  2719. $p_value = preg_replace('/\"$/', '', $p_value);
  2720. $p_value = trim($p_value);
  2721. return $p_value;
  2722. }
  2723. function dec_to_hex($mac)
  2724. {
  2725. if (!isset($mac)) {
  2726. return;
  2727. }
  2728. $mac_array = explode('.', $mac);
  2729. for ($i = 0; $i < count($mac_array); $i++) {
  2730. $hex_i = dechex($mac_array[$i]);
  2731. if (strlen($hex_i) == 1) {
  2732. $hex_i = "0" . $hex_i;
  2733. }
  2734. $mac_array[$i] = $hex_i;
  2735. }
  2736. $hex_mac = implode(':', $mac_array);
  2737. return $hex_mac;
  2738. }
  2739. function mac_simplify($mac)
  2740. {
  2741. if (!isset($mac)) {
  2742. return;
  2743. }
  2744. $mac = strtolower(trim($mac));
  2745. $mac = preg_replace('/(\.|:|-)/', '', $mac);
  2746. return $mac;
  2747. }
  2748. function mac_dotted($mac)
  2749. {
  2750. if (!isset($mac)) {
  2751. return;
  2752. }
  2753. $mac = mac_simplify($mac);
  2754. $mac = preg_replace('/(\S{2})(\S{2})(\S{2})(\S{2})(\S{2})(\S{2})/', '$1:$2:$3:$4:$5:$6', $mac);
  2755. return $mac;
  2756. }
  2757. function unbind_ports($db, $device_id)
  2758. {
  2759. $target = mysqli_query($db, "SELECT U.target_port_id,U.id FROM device_ports U WHERE U.device_id=$device_id");
  2760. while (list($target_id, $id) = mysqli_fetch_array($target)) {
  2761. run_sql($db, "UPDATE device_ports SET target_port_id=0 WHERE target_port_id=" . $id);
  2762. run_sql($db, "UPDATE device_ports SET target_port_id=0 WHERE id=" . $id);
  2763. }
  2764. }
  2765. function bind_ports($db, $port_id, $target_id)
  2766. {
  2767. $old_target = mysqli_query($db, "SELECT U.target_port_id FROM device_ports U WHERE U.id=$port_id");
  2768. list($old_target_id) = mysqli_fetch_array($old_target);
  2769. // unbind current connection
  2770. $new['target_port_id'] = 0;
  2771. update_record($db, "device_ports", "id='$port_id'", $new);
  2772. if (isset($old_target_id)) {
  2773. update_record($db, "device_ports", "id='$old_target_id'", $new);
  2774. }
  2775. // new link
  2776. if (isset($target_id) and $target_id > 0) {
  2777. $new['target_port_id'] = $target_id;
  2778. update_record($db, "device_ports", "id='$port_id'", $new);
  2779. $new['target_port_id'] = $port_id;
  2780. update_record($db, "device_ports", "id='$target_id'", $new);
  2781. }
  2782. }
  2783. function expand_device_name($db, $name)
  2784. {
  2785. $device_id = get_device_id($db, $name);
  2786. $result = $name;
  2787. if (isset($device_id) and $device_id > 0) {
  2788. $result = '<a href=/admin/devices/editdevice.php?id=' . $device_id . '>' . $name . '</a>';
  2789. }
  2790. return $result;
  2791. }
  2792. function expand_mac($db, $msg)
  2793. {
  2794. if (!isset($msg)) {
  2795. return;
  2796. }
  2797. $mac = mac_dotted($msg);
  2798. $vendor_info = get_vendor($db, $mac);
  2799. $result = ' <p title="' . $vendor_info . '"><a href=/admin/logs/mac.php?mac=' . $mac . '>' . $mac . '</a></p>';
  2800. return $result;
  2801. }
  2802. function expand_log_str($db, $msg)
  2803. {
  2804. if (!isset($msg)) {
  2805. return;
  2806. }
  2807. $auth_pattern = '/(auth_id:|auth|auth id:|auth id)\s+(\d+)\s+/i';
  2808. $auth_replace = '<a href=/admin/users/editauth.php?id=${2}>auth_id:${2}</a> ';
  2809. $result = preg_replace($auth_pattern, $auth_replace, $msg);
  2810. $user_pattern = '/(user_id:|user|user id:|user id)\s+(\d+)\s+/i';
  2811. $user_replace = '<a href=/admin/users/edituser.php?id=${2}>user_id:${2}</a> ';
  2812. $result = preg_replace($user_pattern, $user_replace, $result);
  2813. $mac_pattern = '/\s+\[(\w{12})\]\s+/i';
  2814. preg_match($mac_pattern, $result, $matches);
  2815. if (isset($matches[1])) {
  2816. $mac = $matches[1];
  2817. $mac = mac_dotted($mac);
  2818. # $vendor_info = get_vendor($db,$mac);
  2819. # $mac_replace = ' <p title="'.$vendor_info.'"><a href=/admin/logs/mac.php?mac='.$mac.'>'.$mac.'</a></p>';
  2820. $mac_replace = ' <a href=/admin/logs/mac.php?mac=' . $mac . '>' . $mac . '</a> ';
  2821. $result = preg_replace($mac_pattern, $mac_replace, $result);
  2822. }
  2823. $mac_pattern = '/\s+mac:\s+([\w\:]{17})$/i';
  2824. preg_match($mac_pattern, $result, $matches);
  2825. if (isset($matches[1])) {
  2826. $mac = $matches[1];
  2827. $mac = mac_dotted($mac);
  2828. # $vendor_info = get_vendor($db,$mac);
  2829. # $mac_replace = ' mac: <p title="'.$vendor_info.'"><a href=/admin/logs/mac.php?mac='.$mac.'>'.$mac.'</a></p>';
  2830. $mac_replace = ' mac: <a href=/admin/logs/mac.php?mac=' . $mac . '>' . $mac . '</a> ';
  2831. $result = preg_replace($mac_pattern, $mac_replace, $result);
  2832. }
  2833. $device_pattern = '/at device\s+([\w\.\-]+)/i';
  2834. preg_match($device_pattern, $result, $matches);
  2835. if (isset($matches[1])) {
  2836. $device_name = $matches[1];
  2837. $device_id = get_device_id($db, $device_name);
  2838. if (isset($device_id) and $device_id > 0) {
  2839. $device_replace = 'at device <a href=/admin/devices/editdevice.php?id=' . $device_id . '>${1}</a> ';
  2840. $result = preg_replace($device_pattern, $device_replace, $result);
  2841. }
  2842. }
  2843. $device_pattern = '/(device_id:|device id:|device id|device_id)\s+(\d+)\s+/i';
  2844. $device_replace = 'device_id: <a href=/admin/devices/editdevice.php?id=${2}>${2}</a> ';
  2845. $result = preg_replace($device_pattern, $device_replace, $result);
  2846. $ip_pattern = '/\s+ip\:\s+(\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})\s+/i';
  2847. preg_match($ip_pattern, $result, $matches);
  2848. if (isset($matches[1])) {
  2849. $ip = $matches[1];
  2850. $auth_id = get_auth_by_ip($db, $ip);
  2851. if (isset($auth_id) and $auth_id > 0) {
  2852. $auth_replace = ' ip: <a href=/admin/users/editauth.php?id=' . $auth_id . '>${1}</a> ';
  2853. $result = preg_replace($ip_pattern, $auth_replace, $result);
  2854. }
  2855. }
  2856. return $result;
  2857. }
  2858. function get_record_field($db, $table, $field, $filter)
  2859. {
  2860. if (!isset($table)) {
  2861. LOG_ERROR($db, "Search in unknown table! Skip command.");
  2862. return;
  2863. }
  2864. if (!isset($filter)) {
  2865. LOG_ERROR($db, "Search filter is empty! Skip command.");
  2866. return;
  2867. }
  2868. if (!isset($field)) {
  2869. LOG_ERROR($db, "Search field is empty! Skip command.");
  2870. return;
  2871. }
  2872. if (preg_match('/=$/', $filter)) {
  2873. LOG_ERROR($db, "Search record ($table) with illegal filter $filter! Skip command.");
  2874. return;
  2875. }
  2876. $old_sql = "SELECT $field FROM $table WHERE $filter LIMIT 1";
  2877. $old_record = mysqli_query($db, $old_sql) or LOG_ERROR($db, "SQL: $old_sql :" . mysqli_error($db));
  2878. $old = mysqli_fetch_array($old_record, MYSQLI_ASSOC);
  2879. foreach ($old as $key => $value) {
  2880. if (!isset($value) or $value === 'NULL') {
  2881. $value = '';
  2882. }
  2883. $result[$key] = $value;
  2884. }
  2885. return $result[$field];
  2886. }
  2887. function get_record($db, $table, $filter)
  2888. {
  2889. if (!isset($table)) {
  2890. LOG_ERROR($db, "Search in unknown table! Skip command.");
  2891. return;
  2892. }
  2893. if (!isset($filter)) {
  2894. LOG_ERROR($db, "Search filter is empty! Skip command.");
  2895. return;
  2896. }
  2897. if (preg_match('/=$/', $filter)) {
  2898. LOG_ERROR($db, "Search record ($table) with illegal filter $filter! Skip command.");
  2899. return;
  2900. }
  2901. $old_sql = "SELECT * FROM $table WHERE $filter LIMIT 1";
  2902. $old_record = mysqli_query($db, $old_sql) or LOG_ERROR($db, "SQL: $old_sql :" . mysqli_error($db));
  2903. $old = mysqli_fetch_array($old_record, MYSQLI_ASSOC);
  2904. $result = NULL;
  2905. if (!empty($old)) {
  2906. foreach ($old as $key => $value) {
  2907. if (!isset($value) or $value === 'NULL') {
  2908. $value = '';
  2909. }
  2910. if (!empty($key)) {
  2911. $result[$key] = $value;
  2912. }
  2913. }
  2914. }
  2915. return $result;
  2916. }
  2917. function get_records($db, $table, $filter)
  2918. {
  2919. if (!isset($table)) {
  2920. LOG_ERROR($db, "Search in unknown table! Skip command.");
  2921. return;
  2922. }
  2923. if (isset($filter) and preg_match('/=$/', $filter)) {
  2924. LOG_ERROR($db, "Search record ($table) with illegal filter $filter! Skip command.");
  2925. return;
  2926. }
  2927. $s_filter = '';
  2928. if (isset($filter)) {
  2929. $s_filter = 'WHERE ' . $filter;
  2930. }
  2931. $old_sql = "SELECT * FROM $table $s_filter";
  2932. $old_record = mysqli_query($db, $old_sql) or LOG_ERROR($db, "SQL: $old_sql :" . mysqli_error($db));
  2933. $result = NULL;
  2934. $index = 0;
  2935. while ($old = mysqli_fetch_array($old_record, MYSQLI_ASSOC)) {
  2936. foreach ($old as $key => $value) {
  2937. if (!isset($value) or $value === 'NULL') {
  2938. $value = '';
  2939. }
  2940. $result[$index][$key] = $value;
  2941. }
  2942. $index++;
  2943. }
  2944. return $result;
  2945. }
  2946. function get_records_sql($db, $sql)
  2947. {
  2948. if (empty($sql)) {
  2949. LOG_ERROR($db, "Empty query! Skip command.");
  2950. return;
  2951. }
  2952. $record = mysqli_query($db, $sql) or LOG_ERROR($db, "SQL: $sql :" . mysqli_error($db));
  2953. $index = 0;
  2954. $result = NULL;
  2955. while ($rec = mysqli_fetch_array($record, MYSQLI_ASSOC)) {
  2956. foreach ($rec as $key => $value) {
  2957. if (!isset($value) or $value === 'NULL') {
  2958. $value = '';
  2959. }
  2960. if (!empty($key)) {
  2961. $result[$index][$key] = $value;
  2962. }
  2963. }
  2964. $index++;
  2965. }
  2966. return $result;
  2967. }
  2968. function get_record_sql($db, $sql)
  2969. {
  2970. $result = NULL;
  2971. if (!isset($sql)) {
  2972. LOG_ERROR($db, "Empty query! Skip command.");
  2973. return $result;
  2974. }
  2975. $record = mysqli_query($db, $sql . " LIMIT 1") or LOG_ERROR($db, "SQL: $sql LIMIT 1: " . mysqli_error($db));
  2976. if (!isset($record)) {
  2977. return $result;
  2978. }
  2979. $rec = mysqli_fetch_array($record, MYSQLI_ASSOC);
  2980. if (!empty($rec)) {
  2981. foreach ($rec as $key => $value) {
  2982. if (!isset($value) or $value === 'NULL') {
  2983. $value = '';
  2984. }
  2985. $result[$key] = $value;
  2986. }
  2987. }
  2988. return $result;
  2989. }
  2990. function is_auth_bind_changed($db, $id, $ip, $mac)
  2991. {
  2992. $old_sql = "SELECT ip,mac FROM User_auth WHERE id=$id";
  2993. $old_record = get_record_sql($db, $old_sql);
  2994. if (empty($old_record["ip"]) or empty($old_record["mac"])) {
  2995. return 0;
  2996. }
  2997. if ($old_record["ip"] !== $ip or $old_record["mac"] !== $mac) {
  2998. LOG_VERBOSE($db, "Changed ip or mac for auth record!");
  2999. return 1;
  3000. }
  3001. return 0;
  3002. }
  3003. function copy_auth($db, $id, $new_auth)
  3004. {
  3005. $old_record = get_record_sql($db, "SELECT * FROM User_auth WHERE id=$id");
  3006. delete_record($db, "User_auth", "id=" . $id);
  3007. $new_auth["user_id"] = $old_record["user_id"];
  3008. $new_auth["changed"] = 1;
  3009. $changed_time = GetNowTimeString();
  3010. $new_auth["changed_time"] = $changed_time;
  3011. $new_id = insert_record($db, "User_auth", $new_auth);
  3012. LOG_VERBOSE($db, "Old record with id: $id deleted. Created new auth record for new ip+mac id: $new_id!");
  3013. return $new_id;
  3014. }
  3015. function update_record($db, $table, $filter, $newvalue)
  3016. {
  3017. if (isRO($db, $table)) {
  3018. LOG_ERROR($db, "User does not have write permission");
  3019. return;
  3020. }
  3021. if (!isset($table)) {
  3022. LOG_ERROR($db, "Change record for unknown table! Skip command.");
  3023. return;
  3024. }
  3025. if (!isset($filter)) {
  3026. LOG_ERROR($db, "Change record ($table) with empty filter! Skip command.");
  3027. return;
  3028. }
  3029. if (preg_match('/=$/', $filter)) {
  3030. LOG_ERROR($db, "Change record ($table) with illegal filter $filter! Skip command.");
  3031. return;
  3032. }
  3033. if (!isset($newvalue)) {
  3034. LOG_ERROR($db, "Change record ($table [ $filter ]) with empty data! Skip command.");
  3035. return;
  3036. }
  3037. $old_sql = "SELECT * FROM $table WHERE $filter";
  3038. $old_record = mysqli_query($db, $old_sql) or LOG_ERROR($db, "SQL: $old_sql :" . mysqli_error($db));
  3039. $old = mysqli_fetch_array($old_record, MYSQLI_ASSOC);
  3040. $changed_log = '';
  3041. $run_sql = '';
  3042. $network_changed = 0;
  3043. $dhcp_changed = 0;
  3044. $acl_fields = [
  3045. 'ip' => '1',
  3046. 'ip_int' => '1',
  3047. 'enabled' => '1',
  3048. 'dhcp' => '1',
  3049. 'filter_group_id' => '1',
  3050. 'deleted' => '1',
  3051. 'dhcp_acl' => '1',
  3052. 'queue_id' => '1',
  3053. 'mac' => '1',
  3054. 'blocked' => '1',
  3055. ];
  3056. $dhcp_fields = [
  3057. 'ip' => '1',
  3058. 'dhcp' => '1',
  3059. 'deleted' => '1',
  3060. 'mac' => '1',
  3061. ];
  3062. foreach ($newvalue as $key => $value) {
  3063. if (!isset($value)) {
  3064. $value = '';
  3065. }
  3066. $value = trim($value);
  3067. if (strcmp($old[$key], $value) == 0) {
  3068. continue;
  3069. }
  3070. if ($table === "User_auth") {
  3071. if (!empty($acl_fields["$key"])) {
  3072. $network_changed = 1;
  3073. }
  3074. if (!empty($dhcp_fields["$key"])) {
  3075. $dhcp_changed = 1;
  3076. }
  3077. }
  3078. if (!preg_match('/password/i',$key)) {
  3079. $changed_log = $changed_log . " $key => $value (old: $old[$key]),";
  3080. }
  3081. $run_sql = $run_sql . " `" . $key . "`='" . mysqli_real_escape_string($db, $value) . "',";
  3082. }
  3083. if (empty($run_sql)) {
  3084. return;
  3085. }
  3086. if ($network_changed) {
  3087. $run_sql = $run_sql . " `changed`='1',";
  3088. }
  3089. if ($dhcp_changed) {
  3090. $run_sql = $run_sql . " `dhcp_changed`='1',";
  3091. }
  3092. $changed_log = substr_replace($changed_log, "", -1);
  3093. $run_sql = substr_replace($run_sql, "", -1);
  3094. if ($table === 'User_auth') {
  3095. $changed_time = GetNowTimeString();
  3096. $run_sql = $run_sql . ", `changed_time`='" . $changed_time . "'";
  3097. }
  3098. $new_sql = "UPDATE $table SET $run_sql WHERE $filter";
  3099. LOG_DEBUG($db, "Run sql: $new_sql");
  3100. $sql_result = mysqli_query($db, $new_sql) or LOG_ERROR($db, "SQL: $new_sql :" . mysqli_error($db));
  3101. if (!$sql_result) {
  3102. LOG_ERROR($db, "UPDATE Request: $new_sql :" . mysqli_error($db));
  3103. return;
  3104. }
  3105. if ($table !== "sessions") {
  3106. LOG_VERBOSE($db, "Change table $table WHERE $filter set $changed_log");
  3107. }
  3108. }
  3109. function delete_record($db, $table, $filter)
  3110. {
  3111. if (isRO($db, $table)) {
  3112. LOG_ERROR($db, "User does not have write permission");
  3113. return;
  3114. }
  3115. if (!isset($table)) {
  3116. LOG_ERROR($db, "Delete FROM unknown table! Skip command.");
  3117. return;
  3118. }
  3119. if (!isset($filter)) {
  3120. LOG_ERROR($db, "Delete FROM table $table with empty filter! Skip command.");
  3121. return;
  3122. }
  3123. if (preg_match('/=$/', $filter)) {
  3124. LOG_ERROR($db, "Change record ($table) with illegal filter $filter! Skip command.");
  3125. return;
  3126. }
  3127. $old_sql = "SELECT * FROM $table WHERE $filter";
  3128. $old_record = mysqli_query($db, $old_sql) or LOG_ERROR($db, "SQL: $old_sql :" . mysqli_error($db));
  3129. $old = mysqli_fetch_array($old_record, MYSQLI_ASSOC);
  3130. $changed_log = 'record: ';
  3131. if (!empty($old)) {
  3132. foreach ($old as $key => $value) {
  3133. if (!isset($value)) {
  3134. $value = '';
  3135. }
  3136. $changed_log = $changed_log . " $key => $value,";
  3137. }
  3138. }
  3139. //never delete user ip record
  3140. if ($table === 'User_auth') {
  3141. $changed_time = GetNowTimeString();
  3142. $new_sql = "UPDATE $table SET deleted=1, changed=1, `changed_time`='" . $changed_time . "' WHERE $filter";
  3143. LOG_DEBUG($db, "Run sql: $new_sql");
  3144. $sql_result = mysqli_query($db, $new_sql) or LOG_ERROR($db, "SQL: $new_sql :" . mysqli_error($db));
  3145. if (!$sql_result) {
  3146. LOG_ERROR($db, "UPDATE Request (from delete): " . mysqli_error($db));
  3147. return;
  3148. }
  3149. } else {
  3150. $new_sql = "DELETE FROM $table WHERE $filter";
  3151. LOG_DEBUG($db, "Run sql: $new_sql");
  3152. $sql_result = mysqli_query($db, $new_sql) or LOG_ERROR($db, "SQL: $new_sql :" . mysqli_error($db));
  3153. if (!$sql_result) {
  3154. LOG_ERROR($db, "DELETE Request: $new_sql : " . mysqli_error($db));
  3155. return;
  3156. }
  3157. }
  3158. if ($table !== "sessions") {
  3159. LOG_VERBOSE($db, "Delete FROM table $table WHERE $filter $changed_log");
  3160. }
  3161. return $changed_log;
  3162. }
  3163. function insert_record($db, $table, $newvalue)
  3164. {
  3165. if (isRO($db, $table)) {
  3166. LOG_ERROR($db, "User does not have write permission");
  3167. return;
  3168. }
  3169. if (!isset($table)) {
  3170. LOG_ERROR($db, "Create record for unknown table! Skip command.");
  3171. return;
  3172. }
  3173. if (empty($newvalue)) {
  3174. LOG_ERROR($db, "Create record ($table) with empty data! Skip command.");
  3175. return;
  3176. }
  3177. $changed_log = '';
  3178. $field_list = '';
  3179. $value_list = '';
  3180. foreach ($newvalue as $key => $value) {
  3181. if (empty($value) and $value !== 0) {
  3182. $value = '';
  3183. }
  3184. if (!preg_match('/password/i',$key)) {
  3185. $changed_log = $changed_log . " $key => $value,";
  3186. }
  3187. $field_list = $field_list . "`" . $key . "`,";
  3188. $value = trim($value);
  3189. $value_list = $value_list . "'" . mysqli_real_escape_string($db, $value) . "',";
  3190. }
  3191. if (empty($value_list)) {
  3192. return;
  3193. }
  3194. $changed_log = substr_replace($changed_log, "", -1);
  3195. $field_list = substr_replace($field_list, "", -1);
  3196. $value_list = substr_replace($value_list, "", -1);
  3197. $new_sql = "insert into $table(" . $field_list . ") values(" . $value_list . ")";
  3198. LOG_DEBUG($db, "Run sql: $new_sql");
  3199. $sql_result = mysqli_query($db, $new_sql) or LOG_ERROR($db, "SQL: $new_sql :" . mysqli_error($db));
  3200. if (!$sql_result) {
  3201. LOG_ERROR($db, "INSERT Request:" . mysqli_error($db));
  3202. return;
  3203. }
  3204. $last_id = mysqli_insert_id($db);
  3205. if ($table !== "sessions") {
  3206. LOG_VERBOSE($db, "Create record in table $table: $changed_log with id: $last_id");
  3207. }
  3208. if ($table === 'User_auth') {
  3209. run_sql($db, "UPDATE User_auth SET changed=1, dhcp_changed=1 WHERE id=" . $last_id);
  3210. }
  3211. return $last_id;
  3212. }
  3213. function get_rec_str($array)
  3214. {
  3215. $result = '';
  3216. foreach ($array as $key => $value) {
  3217. $result .= "[" . $key . "]=" . $value . ", ";
  3218. }
  3219. $result = preg_replace('/,\s+$/', '', $result);
  3220. return $result;
  3221. }
  3222. function get_diff_rec($db, $table, $filter, $newvalue, $only_changed)
  3223. {
  3224. if (!isset($table)) {
  3225. return;
  3226. }
  3227. if (!isset($filter)) {
  3228. return;
  3229. }
  3230. if (!isset($newvalue)) {
  3231. return;
  3232. }
  3233. if (!isset($only_changed)) {
  3234. $only_changed = 0;
  3235. }
  3236. $old_sql = "SELECT * FROM $table WHERE $filter";
  3237. $old_record = mysqli_query($db, $old_sql) or LOG_ERROR($db, "SQL: $old_sql :" . mysqli_error($db));
  3238. $old = mysqli_fetch_array($old_record, MYSQLI_ASSOC);
  3239. $changed_log = "\r\n";
  3240. foreach ($newvalue as $key => $value) {
  3241. if (strcmp($old[$key], $value) !== 0) {
  3242. $changed_log = $changed_log . " $key => cur: $value old: $old[$key],\r\n";
  3243. }
  3244. }
  3245. $old_record = '';
  3246. if (!$only_changed) {
  3247. $old_record = "\r\n Has not changed:\r\n";
  3248. foreach ($old as $key => $value) {
  3249. if (!empty($newvalue[$key])) {
  3250. $old_record = $old_record . " $key = $value,\r\n";
  3251. }
  3252. }
  3253. $old_record = substr_replace($old_record, "", -3);
  3254. }
  3255. // print $changed_log;
  3256. return $changed_log . $old_record;
  3257. }
  3258. function get_cacti_graph($host_ip, $port_index)
  3259. {
  3260. if (empty(get_const('cacti_url'))) {
  3261. return;
  3262. }
  3263. if (CACTI_DB_HOST == null or CACTI_DB_USER == null or CACTI_DB_PASS == null or CACTI_DB_NAME == null) {
  3264. return;
  3265. }
  3266. if (empty(CACTI_DB_HOST) or empty(CACTI_DB_USER) or empty(CACTI_DB_PASS) or empty(CACTI_DB_NAME)) {
  3267. return;
  3268. }
  3269. $cacti_db_link = new_connection(CACTI_DB_HOST, CACTI_DB_USER, CACTI_DB_PASS, CACTI_DB_NAME);
  3270. if (!$cacti_db_link) {
  3271. return FALSE;
  3272. }
  3273. $host_sql = 'SELECT * FROM host WHERE hostname="' . $host_ip . '"';
  3274. $cacti_host = get_record_sql($cacti_db_link, $host_sql);
  3275. $host_id = $cacti_host["id"];
  3276. if (empty($host_id)) {
  3277. return;
  3278. }
  3279. $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';
  3280. $cacti_graph = get_record_sql($cacti_db_link, $graph_sql);
  3281. $graph_id = $cacti_graph["id"];
  3282. if (empty($graph_id)) {
  3283. return;
  3284. }
  3285. $result = get_const('cacti_url') . "/graph_image.php?local_graph_id=" . $graph_id;
  3286. return $result;
  3287. }
  3288. function print_select_item($description, $value, $current)
  3289. {
  3290. if ((string)$value === (string)$current) {
  3291. print "<option value=$value selected>$description</option>";
  3292. } else {
  3293. print "<option value=$value>$description</option>";
  3294. }
  3295. }
  3296. function print_select_simple($description, $value)
  3297. {
  3298. print "<option value=$value>$description</option>";
  3299. }
  3300. function print_select_item_ext($description, $value, $current, $disabled)
  3301. {
  3302. if ((string)$value === (string)$current) {
  3303. print "<option value=$value selected>$description</option>";
  3304. } else {
  3305. if (!$disabled) {
  3306. print "<option value=$value>$description</option>";
  3307. } else {
  3308. print "<option disabled value=$value>$description</option>";
  3309. }
  3310. }
  3311. }
  3312. function print_row_at_pages($name, $value)
  3313. {
  3314. print "<select name='" . $name . "'>\n";
  3315. print_select_item(WEB_select_item_more, pow(10, 10), $value);
  3316. print_select_item('25', 25, $value);
  3317. print_select_item('50', 50, $value);
  3318. print_select_item('100', 100, $value);
  3319. print_select_item('200', 200, $value);
  3320. print_select_item('500', 500, $value);
  3321. print_select_item('1000', 1000, $value);
  3322. print_select_item('2000', 2000, $value);
  3323. print "</select>\n";
  3324. }
  3325. function print_navigation($url, $page, $displayed, $count_records, $total)
  3326. {
  3327. if ($total <= 1) {
  3328. return;
  3329. }
  3330. $v_char = "?";
  3331. if (preg_match('/\.php\?/', $url)) {
  3332. $v_char = "&";
  3333. }
  3334. #две назад
  3335. print "<div align=left class=records >";
  3336. if (($page - 2) > 0) :
  3337. $pagetwoleft = "<a class='first_page_link' href=" . $url . $v_char . "page=" . ($page - 2) . ">" . ($page - 2) . "</a> ";
  3338. else :
  3339. $pagetwoleft = null;
  3340. endif;
  3341. #одна назад
  3342. if (($page - 1) > 0) :
  3343. $pageoneleft = "<a class='first_page_link' href=" . $url . $v_char . "page=" . ($page - 1) . ">" . ($page - 1) . "</a> ";
  3344. $pagetemp = ($page - 1);
  3345. else :
  3346. $pageoneleft = null;
  3347. $pagetemp = null;
  3348. endif;
  3349. #две вперед
  3350. if (($page + 2) <= $total) :
  3351. $pagetworight = " <a class='first_page_link' href=" . $url . $v_char . "page=" . ($page + 2) . ">" . ($page + 2) . "</a>";
  3352. else :
  3353. $pagetworight = null;
  3354. endif;
  3355. #одна вперед
  3356. if (($page + 1) <= $total) :
  3357. $pageoneright = " <a class='first_page_link' href=" . $url . $v_char . "page=" . ($page + 1) . ">" . ($page + 1) . "</a>";
  3358. $pagetemp2 = ($page + 1);
  3359. else :
  3360. $pageoneright = null;
  3361. $pagetemp2 = null;
  3362. endif;
  3363. # в начало
  3364. if ($page != 1 && $pagetemp != 1 && $pagetemp != 2) :
  3365. $pagerevp = "<a href=" . $url . $v_char . "page=1 class='first_page_link' title='В начало'><<</a> ";
  3366. else :
  3367. $pagerevp = null;
  3368. endif;
  3369. #в конец (последняя)
  3370. if ($page != $total && $pagetemp2 != ($total - 1) && $pagetemp2 != $total) :
  3371. $nextp = " ... <a href=" . $url . $v_char . "page=" . $total . " class='first_page_link'>$total</a>";
  3372. else :
  3373. $nextp = null;
  3374. endif;
  3375. print $pagerevp . $pagetwoleft . $pageoneleft . '<span class="num_page_not_link"><b>' . $page . '</b></span>' . $pageoneright . $pagetworight . $nextp;
  3376. print " | Total records: $count_records";
  3377. print "</div>";
  3378. }
  3379. function get_option($db, $option_id)
  3380. {
  3381. $option = get_record($db, "config", "option_id=" . $option_id);
  3382. if (empty($option) or empty($option['value'])) {
  3383. $default = get_record($db, "config_options", "id=$option_id");
  3384. return $default['default_value'];
  3385. }
  3386. return $option['value'];
  3387. }
  3388. function is_option($db, $option_id)
  3389. {
  3390. $option = get_record($db, "config", "option_id=" . $option_id);
  3391. if (empty($option) or empty($option['value'])) {
  3392. return;
  3393. }
  3394. return 1;
  3395. }
  3396. function set_option($db, $option_id, $value)
  3397. {
  3398. $option['value'] = $value;
  3399. update_record($db, 'config', "option_id=$option_id", $option);
  3400. }
  3401. function is_subnet_aton($subnet, $ip)
  3402. {
  3403. if (!isset($subnet)) {
  3404. return 0;
  3405. }
  3406. if (!isset($ip)) {
  3407. return 0;
  3408. }
  3409. $range = cidrToRange($subnet);
  3410. if ($ip >= ip2long($range[0]) and $ip <= ip2long($range[1])) {
  3411. return 1;
  3412. }
  3413. return 0;
  3414. }
  3415. function get_new_user_id($db, $ip, $mac, $hostname)
  3416. {
  3417. $result['ip'] = $ip;
  3418. $result['mac'] = mac_dotted($mac);
  3419. $result['hostname'] = $hostname;
  3420. $result['user_id'] = NULL;
  3421. $result['ou_id'] = NULL;
  3422. $ip_aton = ip2long($ip);
  3423. //personal user rules
  3424. //ip
  3425. if (!empty($ip)) {
  3426. $t_rules = get_records_sql($db, "SELECT * FROM auth_rules WHERE type=1 and LENGTH(rule)>0 AND user_id IS NOT NULL");
  3427. foreach ($t_rules as $row) {
  3428. if (!empty($row['rule']) and is_subnet_aton($row['rule'], $ip_aton)) {
  3429. $result['user_id'] = $row['user_id'];
  3430. }
  3431. }
  3432. }
  3433. //mac
  3434. if (!empty($mac)) {
  3435. $mac_rules = get_records_sql($db, "SELECT * FROM auth_rules WHERE type=2 AND LENGTH(rule)>0 AND user_id IS NOT NULL");
  3436. foreach ($mac_rules as $row) {
  3437. $pattern = '/' . mac_simplify($row['rule']) . '/';
  3438. if (!empty($row['rule']) and preg_match($pattern, mac_simplify($mac))) {
  3439. $result['user_id'] = $row['user_id'];
  3440. }
  3441. }
  3442. }
  3443. //hostname
  3444. if (!empty($hostname)) {
  3445. $mac_rules = get_records_sql($db, "SELECT * FROM auth_rules WHERE type=3 AND LENGTH(rule)>0 AND user_id IS NOT NULL");
  3446. foreach ($mac_rules as $row) {
  3447. if (!empty($row['rule']) and preg_match($row['rule'], $hostname)) {
  3448. $result['user_id'] = $row['user_id'];
  3449. }
  3450. }
  3451. }
  3452. if (!empty($result['user_id'])) {
  3453. return $result;
  3454. }
  3455. //ou rules
  3456. //ip
  3457. if (!empty($ip)) {
  3458. if (is_hotspot($db, $ip)) {
  3459. $result['ou_id'] = get_const('default_hotspot_ou_id');
  3460. }
  3461. $t_rules = get_records_sql($db, "SELECT * FROM auth_rules WHERE type=1 and LENGTH(rule)>0 AND ou_id IS NOT NULL");
  3462. foreach ($t_rules as $row) {
  3463. if (!empty($row['rule']) and is_subnet_aton($row['rule'], $ip_aton)) {
  3464. $result['ou_id'] = $row['ou_id'];
  3465. }
  3466. }
  3467. }
  3468. //mac
  3469. if (!empty($mac)) {
  3470. $mac_rules = get_records_sql($db, "SELECT * FROM auth_rules WHERE type=2 AND LENGTH(rule)>0 AND ou_id IS NOT NULL");
  3471. foreach ($mac_rules as $row) {
  3472. $pattern = '/' . mac_simplify($row['rule']) . '/';
  3473. if (!empty($row['rule']) and preg_match($pattern, mac_simplify($mac))) {
  3474. $result['ou_id'] = $row['ou_id'];
  3475. }
  3476. }
  3477. }
  3478. //hostname
  3479. if (!empty($hostname)) {
  3480. $mac_rules = get_records_sql($db, "SELECT * FROM auth_rules WHERE type=3 AND LENGTH(rule)>0 AND ou_id IS NOT NULL");
  3481. foreach ($mac_rules as $row) {
  3482. if (!empty($row['rule']) and preg_match($row['rule'], $hostname)) {
  3483. $result['ou_id'] = $row['ou_id'];
  3484. }
  3485. }
  3486. }
  3487. if (empty($result['ou_id'])) {
  3488. $result['ou_id'] = get_const('default_user_ou_id');
  3489. }
  3490. return $result;
  3491. }
  3492. function get_subnet_range($db, $subnet_id)
  3493. {
  3494. if (empty($subnet_id)) {
  3495. return;
  3496. }
  3497. $t_option = get_record_sql($db, "SELECT ip_int_start,ip_int_stop FROM `subnets` WHERE id=$subnet_id");
  3498. if (!isset($t_option['ip_int_start'])) {
  3499. $t_option['ip_int_start'] = 0;
  3500. }
  3501. if (!isset($t_option['ip_int_stop'])) {
  3502. $t_option['ip_int_stop'] = 0;
  3503. }
  3504. $subnet['start'] = $t_option['ip_int_start'];
  3505. $subnet['stop'] = $t_option['ip_int_stop'];
  3506. return $subnet;
  3507. }
  3508. function is_hotspot($db, $ip)
  3509. {
  3510. if (!isset($ip)) {
  3511. return 0;
  3512. }
  3513. LOG_DEBUG($db, "Check hotspot network for ip: $ip");
  3514. $ip_aton = ip2long($ip);
  3515. $t_option = mysqli_query($db, "SELECT subnet,ip_int_start,ip_int_stop FROM `subnets` WHERE hotspot=1");
  3516. while (list($f_net, $f_start, $f_stop) = mysqli_fetch_array($t_option)) {
  3517. if ($ip_aton >= $f_start and $ip_aton <= $f_stop) {
  3518. LOG_DEBUG($db, "ip: $ip [$ip_aton] found in network $f_net: [" . $f_start . ".." . $f_stop . "]");
  3519. return 1;
  3520. }
  3521. }
  3522. LOG_DEBUG($db, "ip $ip not found in hotspot network!");
  3523. return 0;
  3524. }
  3525. function is_office($db, $ip)
  3526. {
  3527. if (!isset($ip)) {
  3528. return 0;
  3529. }
  3530. LOG_DEBUG($db, "Check office network for ip: $ip");
  3531. $ip_aton = ip2long($ip);
  3532. $t_option = mysqli_query($db, "SELECT subnet,ip_int_start,ip_int_stop FROM `subnets` WHERE office=1");
  3533. while (list($f_net, $f_start, $f_stop) = mysqli_fetch_array($t_option)) {
  3534. if ($ip_aton >= $f_start and $ip_aton <= $f_stop) {
  3535. LOG_DEBUG($db, "ip: $ip [$ip_aton] found in office $f_net: [" . $f_start . ".." . $f_stop . "]");
  3536. return 1;
  3537. }
  3538. }
  3539. LOG_DEBUG($db, "ip $ip not found in office network!");
  3540. return 0;
  3541. }
  3542. function is_our_network($db, $ip)
  3543. {
  3544. if (!isset($ip)) {
  3545. return 0;
  3546. }
  3547. if (is_hotspot($db, $ip)) {
  3548. return 1;
  3549. }
  3550. if (is_office($db, $ip)) {
  3551. return 1;
  3552. }
  3553. return 0;
  3554. }
  3555. function get_const($const_name)
  3556. {
  3557. global $config;
  3558. if (isset($config[$const_name])) {
  3559. return $config[$const_name];
  3560. }
  3561. return NULL;
  3562. }
  3563. $config["org_name"] = get_option($db_link, 32);
  3564. $config["KB"] = get_option($db_link, 1);
  3565. if ($config["KB"] == 0) {
  3566. $config["KB"] = 1000;
  3567. }
  3568. if ($config["KB"] == 1) {
  3569. $config["KB"] = 1024;
  3570. }
  3571. $config["debug"] = get_option($db_link, 34);
  3572. $config["log_level"] = get_option($db_link, 53);
  3573. if ($config["debug"]) {
  3574. $config["log_level"] = 255;
  3575. }
  3576. $config["send_email"] = get_option($db_link, 51);
  3577. $config["admin_email"] = get_option($db_link, 21);
  3578. $config["sender_email"] = get_option($db_link, 52);
  3579. $config["snmp_default_version"] = get_option($db_link, 9);
  3580. $config["snmp_default_community"] = get_option($db_link, 11);
  3581. $config["auto_mac_rule"] = get_option($db_link, 64);
  3582. $config["cacti_url"] = rtrim(get_option($db_link, 58), '/');
  3583. if (preg_match('/127.0.0.1/', $config["cacti_url"])) {
  3584. $config["cacti_url"] = NULL;
  3585. }
  3586. $config["nagios_url"] = rtrim(get_option($db_link, 57), '/') . '/cgi-bin/';
  3587. if (preg_match('/127.0.0.1/', $config["nagios_url"])) {
  3588. $config["nagios_url"] = NULL;
  3589. }
  3590. $config["torrus_url"] = rtrim(get_option($db_link, 59), '/') . '?nodeid=if//HOST_IP//IF_NAME////inoutbps';
  3591. if (preg_match('/127.0.0.1/', $config["torrus_url"])) {
  3592. $config["torrus_url"] = NULL;
  3593. }
  3594. $ou = get_record_sql($db_link, "SELECT id FROM OU WHERE default_users = 1");
  3595. if (empty($ou)) {
  3596. $config["default_user_ou_id"] = 0;
  3597. } else {
  3598. $config["default_user_ou_id"] = $ou['id'];
  3599. }
  3600. $ou = get_record_sql($db_link, "SELECT id FROM OU WHERE default_hotspot=1");
  3601. if (empty($ou)) {
  3602. $config["default_hotspot_ou_id"] = $config["default_user_ou_id"];
  3603. } else {
  3604. $config["default_hotspot_ou_id"] = $ou['id'];
  3605. }
  3606. $config["init"] = 1;
  3607. clean_dns_cache($db_link);
  3608. snmp_set_valueretrieval(SNMP_VALUE_LIBRARY);
  3609. snmp_set_enum_print(1);