common.php 106 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219
  1. <?php
  2. if (! defined("CONFIG")) { die("Not defined"); }
  3. require_once ($_SERVER['DOCUMENT_ROOT']."/inc/class.simple.mail.php");
  4. $config["init"]=0;
  5. #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])$";
  6. #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])$";
  7. $port_status_oid = '.1.3.6.1.2.1.2.2.1.8.';
  8. $port_admin_status_oid = '.1.3.6.1.2.1.2.2.1.7.';
  9. $port_speed_oid = '.1.3.6.1.2.1.2.2.1.5.';
  10. $port_errors_oid = '.1.3.6.1.2.1.2.2.1.14.';
  11. $port_vlan_oid = '.1.3.6.1.2.1.17.7.1.4.5.1.1.';
  12. $mac_table_oid = '.1.3.6.1.2.1.17.7.1.2.2.1.2';
  13. $mac_table_oid2 = '.1.3.6.1.2.1.17.4.3.1.2';
  14. $eltex_sfp_status = '.1.3.6.1.4.1.89.90.1.2.1.3';
  15. $eltex_sfp_vendor = '.1.3.6.1.4.1.35265.1.23.53.1.1.1.5';
  16. $eltex_sfp_sn = '.1.3.6.1.4.1.35265.1.23.53.1.1.1.6';
  17. $eltex_sfp_freq = '.1.3.6.1.4.1.35265.1.23.53.1.1.1.4';
  18. $eltex_sfp_length = '.1.3.6.1.4.1.35265.1.23.53.1.1.1.8';
  19. $cisco_descr = '.1.3.6.1.2.1.1.1.0';
  20. $cisco_modules = '.1.3.6.1.2.1.47.1.1.1.1.7';
  21. $cisco_sfp_sensors = '.1.3.6.1.4.1.9.9.91.1.1.1.1.4';
  22. $cisco_sfp_precision = '.1.3.6.1.4.1.9.9.91.1.1.1.1.3';
  23. $cisco_vlan_oid = 'SNMPv2-SMI::enterprises.9.9.46.1.3.1.1.2';
  24. $ifmib_ifindex = '.1.3.6.1.2.1.31.1.1.1.1';
  25. $huawei_sfp_vendor = '.1.3.6.1.4.1.2011.5.25.31.1.1.2.1.11';
  26. $huawei_sfp_speed = '.1.3.6.1.4.1.2011.5.25.31.1.1.2.1.2';
  27. $huawei_sfp_volt = '.1.3.6.1.4.1.2011.5.25.31.1.1.3.1.6';
  28. $huawei_sfp_optrx = '.1.3.6.1.4.1.2011.5.25.31.1.1.3.1.32';
  29. $huawei_sfp_opttx = '.1.3.6.1.4.1.2011.5.25.31.1.1.3.1.33';
  30. $huawei_sfp_biascurrent = '.1.3.6.1.4.1.2011.5.25.31.1.1.3.1.31';
  31. $huawei_sfp_rx = '.1.3.6.1.4.1.2011.5.25.31.1.1.3.1.8';
  32. $huawei_sfp_tx = '.1.3.6.1.4.1.2011.5.25.31.1.1.3.1.9';
  33. $pethPsePortAdminEnable = '.1.3.6.1.2.1.105.1.1.1.3.1';
  34. $huawei_poe_oid = '.1.3.6.1.4.1.2011.5.25.195.3.1.3';
  35. $allied_poe_oid = '.1.3.6.1.2.1.105.1.1.1.3.1';
  36. $hp_poe_oid = '.1.3.6.1.2.1.105.1.1.1.3.1';
  37. $netgear_poe_oid = '.1.3.6.1.4.1.4526.11.15.1.1.1.6.1';
  38. $mikrotik_poe_oid = '.1.3.6.1.4.1.14988.1.1.15.1.1.3';
  39. // interface id
  40. $mikrotik_poe_int = '.1.3.6.1.4.1.14988.1.1.15.1.1.1';
  41. // interface names
  42. $mikrotik_poe_int_names = '.1.3.6.1.4.1.14988.1.1.15.1.1.2';
  43. // voltage in dV (decivolt)
  44. $mikrotik_poe_volt = '.1.3.6.1.4.1.14988.1.1.15.1.1.4';
  45. // current in mA
  46. $mikrotik_poe_current = '.1.3.6.1.4.1.14988.1.1.15.1.1.5';
  47. // power usage in dW (deviwatt)
  48. $mikrotik_poe_usage = '.1.3.6.1.4.1.14988.1.1.15.1.1.6';
  49. $L_ERROR = 0;
  50. $L_WARNING = 1;
  51. $L_INFO = 2;
  52. $L_VERBOSE = 3;
  53. $L_DEBUG = 255;
  54. $log_level = 2;
  55. // #### vendor id
  56. // 1, Unknown
  57. // 2, 'Eltex'
  58. // 3, 'Huawei'
  59. // 4, 'Zyxel'
  60. // 5, 'Raisecom'
  61. // 6, 'SNR'
  62. // 7, 'Dlink'
  63. // 8, 'Allied Telesis'
  64. // 9, 'Mikrotik'
  65. // 10, 'NetGear'
  66. // 11, 'Ubiquiti'
  67. // 15, 'HP'
  68. // 16, 'Cisco'
  69. // 17, 'Maipu'
  70. // 18, 'Asus'
  71. $admin_email = "admin";
  72. $sender_email = "root";
  73. $send_email = 0;
  74. $mac_table_str_oid = 'SNMPv2-SMI::mib-2.17.7.1.2.2.1.2';
  75. $mac_table_str_oid2 = 'SNMPv2-SMI::mib-2.17.4.3.1.2';
  76. function get_user_ip()
  77. {
  78. $auth_ip = getenv("HTTP_CLIENT_IP");
  79. if (empty($auth_ip)) {
  80. $auth_ip = getenv("HTTP_X_FORWARDED_FOR");
  81. if (empty($auth_ip)) {
  82. $auth_ip = getenv("REMOTE_ADDR");
  83. if (empty($auth_ip)) {
  84. $auth_ip = $_SERVER['REMOTE_ADDR'];
  85. }
  86. }
  87. }
  88. return $auth_ip;
  89. }
  90. function fbytes($traff)
  91. {
  92. $units = array(
  93. "",
  94. "k",
  95. "M",
  96. "G",
  97. "T"
  98. );
  99. $KB = 1024;
  100. if ($traff) {
  101. $index = min(((int) log($traff, $KB)), count($units) - 1);
  102. $result = round($traff / pow($KB, $index), 3) . ' ' . $units[$index] . 'b';
  103. } else {
  104. $result = '0 b';
  105. }
  106. return $result;
  107. }
  108. function fpkts($packets)
  109. {
  110. $units = array(
  111. "",
  112. "k",
  113. "M",
  114. "G",
  115. "T"
  116. );
  117. $KB = 1000;
  118. if ($packets) {
  119. $index = min(((int) log($packets, $KB)), count($units) - 1);
  120. $result = round($packets / pow($KB, $index), 3) . ' ' . $units[$index] . 'pkt/s';
  121. } else {
  122. $result = '0 pkt/s';
  123. }
  124. return $result;
  125. }
  126. function checkValidIp($cidr)
  127. {
  128. // Checks for a valid IP address or optionally a cidr notation range
  129. // e.g. 1.2.3.4 or 1.2.3.0/24
  130. // 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)) {
  131. $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}/';
  132. if (! preg_match($ip_pattern, $cidr)) {
  133. $return = FALSE;
  134. } else {
  135. $return = TRUE;
  136. }
  137. if ($return == TRUE) {
  138. $parts = explode("/", $cidr);
  139. $ip = $parts[0];
  140. if (empty($parts[1])) { $parts[1]="32"; }
  141. $netmask = $parts[1];
  142. $octets = explode(".", $ip);
  143. foreach ($octets as $octet) {
  144. if ($octet > 255) {
  145. $return = FALSE;
  146. }
  147. }
  148. if (($netmask != "") && ($netmask > 32)) {
  149. $return = FALSE;
  150. }
  151. }
  152. return $return;
  153. }
  154. function checkValidHostname($dnsname)
  155. {
  156. $host_pattern="/^(([a-zA-Z0-9]|[a-zA-Z0-9][a-zA-Z0-9\-]*[a-zA-Z0-9])\.)*([A-Za-z0-9]|[A-Za-z0-9][A-Za-z0-9\-]*[A-Za-z0-9])$/";
  157. if (! preg_match($host_pattern, $dnsname)) {
  158. $result = FALSE;
  159. } else {
  160. $result = TRUE;
  161. }
  162. return $result;
  163. }
  164. function checkUniqHostname($db,$id,$hostname)
  165. {
  166. $count=get_count_records($db,'User_auth','deleted=0 and id !="'.$id.'" and dns_name ="'.mysqli_real_escape_string($db,$hostname).'"');
  167. if ($count >0 ) { return FALSE; }
  168. return TRUE;
  169. }
  170. // Транслитерация строк.
  171. function transliterate($string, $gost = false)
  172. {
  173. if ($gost) {
  174. $replace = array(
  175. "А" => "A",
  176. "а" => "a",
  177. "Б" => "B",
  178. "б" => "b",
  179. "В" => "V",
  180. "в" => "v",
  181. "Г" => "G",
  182. "г" => "g",
  183. "Д" => "D",
  184. "д" => "d",
  185. "Е" => "E",
  186. "е" => "e",
  187. "Ё" => "E",
  188. "ё" => "e",
  189. "Ж" => "Zh",
  190. "ж" => "zh",
  191. "З" => "Z",
  192. "з" => "z",
  193. "И" => "I",
  194. "и" => "i",
  195. "Й" => "I",
  196. "й" => "i",
  197. "К" => "K",
  198. "к" => "k",
  199. "Л" => "L",
  200. "л" => "l",
  201. "М" => "M",
  202. "м" => "m",
  203. "Н" => "N",
  204. "н" => "n",
  205. "О" => "O",
  206. "о" => "o",
  207. "П" => "P",
  208. "п" => "p",
  209. "Р" => "R",
  210. "р" => "r",
  211. "С" => "S",
  212. "с" => "s",
  213. "Т" => "T",
  214. "т" => "t",
  215. "У" => "U",
  216. "у" => "u",
  217. "Ф" => "F",
  218. "ф" => "f",
  219. "Х" => "Kh",
  220. "х" => "kh",
  221. "Ц" => "Tc",
  222. "ц" => "tc",
  223. "Ч" => "Ch",
  224. "ч" => "ch",
  225. "Ш" => "Sh",
  226. "ш" => "sh",
  227. "Щ" => "Shch",
  228. "щ" => "shch",
  229. "Ы" => "Y",
  230. "ы" => "y",
  231. "Э" => "E",
  232. "э" => "e",
  233. "Ю" => "Iu",
  234. "ю" => "iu",
  235. "Я" => "Ia",
  236. "я" => "ia",
  237. "ъ" => "",
  238. "ь" => ""
  239. );
  240. } else {
  241. $arStrES = array(
  242. "ае",
  243. "уе",
  244. "ое",
  245. "ые",
  246. "ие",
  247. "эе",
  248. "яе",
  249. "юе",
  250. "ёе",
  251. "ее",
  252. "ье",
  253. "ъе",
  254. "ый",
  255. "ий"
  256. );
  257. $arStrOS = array(
  258. "аё",
  259. "уё",
  260. "оё",
  261. "ыё",
  262. "иё",
  263. "эё",
  264. "яё",
  265. "юё",
  266. "ёё",
  267. "её",
  268. "ьё",
  269. "ъё",
  270. "ый",
  271. "ий"
  272. );
  273. $arStrRS = array(
  274. "а$",
  275. "у$",
  276. "о$",
  277. "ы$",
  278. "и$",
  279. "э$",
  280. "я$",
  281. "ю$",
  282. "ё$",
  283. "е$",
  284. "ь$",
  285. "ъ$",
  286. "@",
  287. "@"
  288. );
  289. $replace = array(
  290. "А" => "A",
  291. "а" => "a",
  292. "Б" => "B",
  293. "б" => "b",
  294. "В" => "V",
  295. "в" => "v",
  296. "Г" => "G",
  297. "г" => "g",
  298. "Д" => "D",
  299. "д" => "d",
  300. "Е" => "Ye",
  301. "е" => "e",
  302. "Ё" => "Ye",
  303. "ё" => "e",
  304. "Ж" => "Zh",
  305. "ж" => "zh",
  306. "З" => "Z",
  307. "з" => "z",
  308. "И" => "I",
  309. "и" => "i",
  310. "Й" => "Y",
  311. "й" => "y",
  312. "К" => "K",
  313. "к" => "k",
  314. "Л" => "L",
  315. "л" => "l",
  316. "М" => "M",
  317. "м" => "m",
  318. "Н" => "N",
  319. "н" => "n",
  320. "О" => "O",
  321. "о" => "o",
  322. "П" => "P",
  323. "п" => "p",
  324. "Р" => "R",
  325. "р" => "r",
  326. "С" => "S",
  327. "с" => "s",
  328. "Т" => "T",
  329. "т" => "t",
  330. "У" => "U",
  331. "у" => "u",
  332. "Ф" => "F",
  333. "ф" => "f",
  334. "Х" => "Kh",
  335. "х" => "kh",
  336. "Ц" => "Ts",
  337. "ц" => "ts",
  338. "Ч" => "Ch",
  339. "ч" => "ch",
  340. "Ш" => "Sh",
  341. "ш" => "sh",
  342. "Щ" => "Shch",
  343. "щ" => "shch",
  344. "Ъ" => "",
  345. "ъ" => "",
  346. "Ы" => "Y",
  347. "ы" => "y",
  348. "Ь" => "",
  349. "ь" => "",
  350. "Э" => "E",
  351. "э" => "e",
  352. "Ю" => "Yu",
  353. "ю" => "yu",
  354. "Я" => "Ya",
  355. "я" => "ya",
  356. "@" => "y",
  357. "$" => "ye"
  358. );
  359. $string = str_replace($arStrES, $arStrRS, $string);
  360. $string = str_replace($arStrOS, $arStrRS, $string);
  361. }
  362. return strtr($string, $replace);
  363. }
  364. function cidrToRange($cidr)
  365. {
  366. $range = array();
  367. $cidr = explode('/', $cidr);
  368. if (! isset($cidr[1])) { $cidr[1] = 32; }
  369. $start=(ip2long($cidr[0])) & ((- 1 << (32 - (int) $cidr[1])));
  370. $stop=$start + pow(2, (32 - (int) $cidr[1])) - 1;
  371. $range[0] = long2ip($start);
  372. $range[1] = long2ip($stop);
  373. $range[2] = $cidr;
  374. #dhcp
  375. $dhcp_size=round(($stop-$start)/2,PHP_ROUND_HALF_UP);
  376. $dhcp_start=$start+round($dhcp_size/2,PHP_ROUND_HALF_UP);
  377. $range[3]=long2ip($dhcp_start);
  378. $range[4]=long2ip($dhcp_start+$dhcp_size);
  379. $range[5]=long2ip($start+1);
  380. return $range;
  381. }
  382. function print_ou_select($db, $ou_name, $ou_value)
  383. {
  384. print "<select name=\"$ou_name\" class=\"js-select-single\">\n";
  385. $t_ou = mysqli_query($db, "SELECT id,ou_name FROM OU ORDER BY ou_name");
  386. while (list ($f_ou_id, $f_ou_name) = mysqli_fetch_array($t_ou)) {
  387. print_select_item($f_ou_name,$f_ou_id,$ou_value);
  388. }
  389. print "</select>\n";
  390. }
  391. function print_subnet_select($db, $subnet_name, $subnet_value)
  392. {
  393. print "<select name=\"$subnet_name\" >\n";
  394. $t_subnet = mysqli_query($db, "SELECT id,subnet FROM subnets ORDER BY ip_int_start");
  395. print_select_item('Всe ip',0,$subnet_value);
  396. while (list ($f_subnet_id, $f_subnet_name) = mysqli_fetch_array($t_subnet)) {
  397. print_select_item($f_subnet_name,$f_subnet_id,$subnet_value);
  398. }
  399. print "</select>\n";
  400. }
  401. function print_subnet_select_office($db, $subnet_name, $subnet_value)
  402. {
  403. print "<select name=\"$subnet_name\" >\n";
  404. $t_subnet = mysqli_query($db, "SELECT id,subnet FROM subnets WHERE office=1 ORDER BY ip_int_start");
  405. print_select_item('Всe ip',0,$subnet_value);
  406. while (list ($f_subnet_id, $f_subnet_name) = mysqli_fetch_array($t_subnet)) {
  407. print_select_item($f_subnet_name,$f_subnet_id,$subnet_value);
  408. }
  409. print "</select>\n";
  410. }
  411. function print_loglevel_select($item_name, $value)
  412. {
  413. print "<select name=\"$item_name\">\n";
  414. global $L_INFO;
  415. global $L_WARNING;
  416. global $L_ERROR;
  417. global $L_VERBOSE;
  418. global $L_DEBUG;
  419. print_select_item('Error',$L_ERROR,$value);
  420. print_select_item('Warning',$L_WARNING,$value);
  421. print_select_item('Info',$L_INFO,$value);
  422. print_select_item('Verbose',$L_VERBOSE,$value);
  423. print_select_item('Debug',$L_DEBUG,$value);
  424. print "</select>\n";
  425. }
  426. function reencodeurl($url) {
  427. $url_arr = explode('?', $url);
  428. $fpage = $url_arr[0];
  429. $params = $url_arr[1];
  430. $params_arr = explode('&', $params);
  431. $new_params = '';
  432. foreach ($params_arr as $row) {
  433. $param = explode ('=',$row);
  434. $key = $param[0];
  435. $value = urlencode(urldecode($param[1]));
  436. $new_params.="&".$key."=".$value;
  437. }
  438. $new_params = preg_replace('/^&/','',$new_params);
  439. if ($new_params === '=') { $new_url = $fpage; } else { $new_url = $fpage."?".$new_params; }
  440. return $new_url;
  441. }
  442. function print_submenu_url($display_name,$page,$current_page,$last) {
  443. $url_arr = explode('?', $page);
  444. $fpage = $url_arr[0];
  445. $new_url = reencodeurl($page);
  446. if ($fpage === $current_page) { print "<b>$display_name</b>"; } else { print "<a href='".$new_url."'> $display_name </a>"; }
  447. if (!isset($last) or $last==0) { print " | "; }
  448. }
  449. function print_url($display_name,$page) {
  450. print "<a href='".reencodeurl($page)."'> $display_name </a>";
  451. }
  452. function print_log_submenu ($current_page) {
  453. print "<div id='submenu'>\n";
  454. print_submenu_url('Журнал dhcp','/admin/logs/dhcp.php',$current_page,0);
  455. print_submenu_url('Журнал работы ','/admin/logs/index.php',$current_page,0);
  456. print_submenu_url('Приключения маков','/admin/logs/mac.php',$current_page,0);
  457. print_submenu_url('История ip-адресов','/admin/logs/ip.php',$current_page,0);
  458. print_submenu_url('Неизвестные','/admin/logs/unknown.php',$current_page,0);
  459. print_submenu_url('syslog','/admin/logs/syslog.php',$current_page,1);
  460. print "</div>\n";
  461. }
  462. function print_control_submenu ($current_page) {
  463. print "<div id='submenu'>\n";
  464. print_submenu_url('Управление','/admin/customers/control.php',$current_page,0);
  465. print_submenu_url('Сети','/admin/customers/control-subnets.php',$current_page,0);
  466. print_submenu_url('Сети (Статистика)','/admin/customers/control-subnets-usage.php',$current_page,0);
  467. print_submenu_url('Параметры','/admin/customers/control-options.php',$current_page,0);
  468. print_submenu_url('Пользователи','/admin/customers/index.php',$current_page,1);
  469. print "</div>\n";
  470. }
  471. function print_filters_submenu ($current_page) {
  472. print "<div id='submenu'>\n";
  473. print_submenu_url('Список фильтров','/admin/filters/index.php',$current_page,0);
  474. print_submenu_url('Группы фильтров','/admin/filters/groups.php',$current_page,1);
  475. print "</div>\n";
  476. }
  477. function print_reports_submenu ($current_page) {
  478. print "<div id='submenu'>\n";
  479. print_submenu_url('Отчёт по трафику (ip)','/admin/reports/index-full.php',$current_page,0);
  480. print_submenu_url('Отчёт по трафику (login)','/admin/reports/index.php',$current_page,1);
  481. print "</div>\n";
  482. }
  483. function print_trafdetail_submenu ($current_page,$params,$description) {
  484. print "<div id='submenu'>\n";
  485. print "$description\n";
  486. print_submenu_url('TOP 10 по трафику','/admin/reports/userdaydetail.php'."?$params",$current_page,0);
  487. print_submenu_url('Подробный лог','/admin/reports/userdaydetaillog.php'."?$params",$current_page,1);
  488. print "</div>\n";
  489. }
  490. function print_device_submenu ($current_page) {
  491. print "<div id='submenu'>\n";
  492. print_submenu_url('Активное сетевое оборудование','/admin/devices/index.php',$current_page,0);
  493. print_submenu_url('Расположение','/admin/devices/building.php',$current_page,0);
  494. print_submenu_url('Удалённые','/admin/devices/deleted.php',$current_page,0);
  495. print_submenu_url('Модели устройств','/admin/devices/devmodels.php',$current_page,0);
  496. print_submenu_url('Порты по вланам','/admin/devices/portsbyvlan.php',$current_page,1);
  497. print "</div>\n";
  498. }
  499. function print_editdevice_submenu ($current_page,$id) {
  500. print "<div id='submenu'>\n";
  501. $dev_id='';
  502. if (isset($id)) { $dev_id='?id='.$id; }
  503. print_submenu_url('Параметры','/admin/devices/editdevice.php'.$dev_id,$current_page,0);
  504. print_submenu_url('Порты','/admin/devices/switchport.php'.$dev_id,$current_page,0);
  505. print_submenu_url('Состояние','/admin/devices/switchstatus.php'.$dev_id,$current_page,0);
  506. print_submenu_url('Соединения','/admin/devices/switchport-conn.php'.$dev_id,$current_page,1);
  507. print "</div>\n";
  508. }
  509. function print_ip_submenu ($current_page) {
  510. print "<div id='submenu'>\n";
  511. print_submenu_url('Активные адреса','/admin/iplist/index.php',$current_page,0);
  512. print_submenu_url('Удалённые адреса','/admin/iplist/deleted.php',$current_page,0);
  513. print_submenu_url('Дубли','/admin/iplist/doubles.php',$current_page,1);
  514. print "</div>\n";
  515. }
  516. function get_ou($db, $ou_value)
  517. {
  518. if (!isset($ou_value)) { return; }
  519. $ou_name = get_record_sql($db, "SELECT ou_name FROM OU WHERE id=$ou_value");
  520. if (empty($ou_name)) { return; }
  521. return $ou_name['ou_name'];
  522. }
  523. function get_building($db, $building_value)
  524. {
  525. if (!isset($building_value)) { return; }
  526. $building_name = get_record_sql($db, "SELECT name FROM building WHERE id=$building_value");
  527. if (empty($building_name)) { return; }
  528. return $building_name['name'];
  529. }
  530. function print_group_select($db, $group_name, $group_value)
  531. {
  532. print "<select name=\"$group_name\">\n";
  533. $t_group = mysqli_query($db, "SELECT id,group_name FROM Group_list Order by group_name");
  534. while (list ($f_group_id, $f_group_name) = mysqli_fetch_array($t_group)) {
  535. print_select_item($f_group_name,$f_group_id,$group_value);
  536. }
  537. print "</select>\n";
  538. }
  539. function print_building_select($db, $building_name, $building_value)
  540. {
  541. print "<select name=\"$building_name\">\n";
  542. print_select_item('Всё',0,$building_value);
  543. $t_building = mysqli_query($db, "SELECT id,name FROM building Order by name");
  544. while (list ($f_building_id, $f_building_name) = mysqli_fetch_array($t_building)) {
  545. print_select_item($f_building_name,$f_building_id,$building_value);
  546. }
  547. print "</select>\n";
  548. }
  549. function print_devtypes_select($db, $devtype_name, $devtype_value)
  550. {
  551. print "<select name=\"$devtype_name\">\n";
  552. print_select_item('Всё',0,$devtype_value);
  553. $t_devtype = mysqli_query($db, "SELECT id,name FROM device_types Order by name");
  554. while (list ($f_devtype_id, $f_devtype_name) = mysqli_fetch_array($t_devtype)) {
  555. print_select_item($f_devtype_name,$f_devtype_id,$devtype_value);
  556. }
  557. print "</select>\n";
  558. }
  559. function print_devtype_select($db, $devtype_name, $devtype_value)
  560. {
  561. print "<select name=\"$devtype_name\">\n";
  562. $t_devtype = mysqli_query($db, "SELECT id,name FROM device_types Order by name");
  563. while (list ($f_devtype_id, $f_devtype_name) = mysqli_fetch_array($t_devtype)) {
  564. print_select_item($f_devtype_name,$f_devtype_id,$devtype_value);
  565. }
  566. print "</select>\n";
  567. }
  568. function get_group($db, $group_value)
  569. {
  570. list ($group_name) = mysqli_fetch_array(mysqli_query($db, "SELECT group_name FROM Group_list WHERE id=$group_value"));
  571. return $group_name;
  572. }
  573. function get_devtype_name($db, $device_type_id)
  574. {
  575. list ($type_name) = mysqli_fetch_array(mysqli_query($db, "SELECT name FROM device_types WHERE id=$device_type_id"));
  576. return $type_name;
  577. }
  578. function get_l3_interfaces($db, $device_id)
  579. {
  580. $wan='';
  581. $lan='';
  582. $t_l3int = mysqli_query($db, "SELECT name,interface_type FROM device_l3_interfaces WHERE device_id=$device_id ORDER BY name");
  583. while (list ($f_name,$f_type) = mysqli_fetch_array($t_l3int)) {
  584. if ($f_type==0) { $lan=$lan." ".$f_name; }
  585. if ($f_type==1) { $wan=$wan." ".$f_name; }
  586. }
  587. $wan=trim($wan);
  588. $lan=trim($lan);
  589. $result='';
  590. if (!empty($wan)) { $result.=' WAN: '.$wan.'<br>'; }
  591. if (!empty($lan)) { $result.=' LAN: '.$lan; }
  592. return trim($result);
  593. }
  594. function print_queue_select($db, $queue_name, $queue_value)
  595. {
  596. print "<select name=\"$queue_name\">\n";
  597. $t_queue = mysqli_query($db, "SELECT id,queue_name FROM Queue_list Order by queue_name");
  598. while (list ($f_queue_id, $f_queue_name) = mysqli_fetch_array($t_queue)) {
  599. print_select_item($f_queue_name,$f_queue_id,$queue_value);
  600. }
  601. print "</select>\n";
  602. }
  603. function get_queue($db, $queue_value)
  604. {
  605. list ($queue_name) = mysqli_fetch_array(mysqli_query($db, "SELECT queue_name FROM Queue_list WHERE id=$queue_value"));
  606. return $queue_name;
  607. }
  608. function print_qa_l3int_select($qa_name, $qa_value)
  609. {
  610. print "<select name=\"$qa_name\">\n";
  611. print_select_item('Внутренний',0,$qa_value);
  612. print_select_item('Внешний',1,$qa_value);
  613. print "</select>\n";
  614. }
  615. function print_qa_rule_select($qa_name, $qa_value)
  616. {
  617. print "<select name=\"$qa_name\">\n";
  618. print_select_item('Subnet',1,$qa_value);
  619. print_select_item('Mac',2,$qa_value);
  620. print_select_item('Hostname',3,$qa_value);
  621. print "</select>\n";
  622. }
  623. function print_qa_select($qa_name, $qa_value)
  624. {
  625. print "<select name=\"$qa_name\">\n";
  626. print_select_item('Да',1,$qa_value);
  627. print_select_item('Нет',0,$qa_value);
  628. print "</select>\n";
  629. }
  630. function print_qa_select_ext($qa_name, $qa_value, $readonly)
  631. {
  632. $state = '';
  633. if ($readonly) { $state='disabled=true'; }
  634. print "<select name=\"$qa_name\">\n";
  635. print_select_item_ext('Да',1,$qa_value, $readonly);
  636. print_select_item_ext('Нет',0,$qa_value, $readonly);
  637. print "</select>\n";
  638. }
  639. function print_snmp_select($qa_name, $qa_value)
  640. {
  641. print "<select name=\"$qa_name\">\n";
  642. print_select_item('Disabled',0,$qa_value);
  643. print_select_item('v1',1,$qa_value);
  644. print_select_item('v2',2,$qa_value);
  645. print_select_item('v3',3,$qa_value);
  646. print "</select>\n";
  647. }
  648. function print_dhcp_select($qa_name, $qa_value)
  649. {
  650. print "<select name=\"$qa_name\">\n";
  651. if (! isset($qa_value) or strlen($qa_value) == 0) {
  652. $qa_value = 'all';
  653. }
  654. print_select_item('Все события','all',$qa_value);
  655. print_select_item('Аренда адреса','add',$qa_value);
  656. print_select_item('Обновление аренды','old',$qa_value);
  657. print_select_item('Освобождение адреса','del',$qa_value);
  658. print "</select>\n";
  659. }
  660. function print_enabled_select($qa_name, $qa_value)
  661. {
  662. print "<select name=\"$qa_name\">\n";
  663. if (! isset($qa_value) or strlen($qa_value) == 0) { $qa_value = 0; }
  664. print_select_item('Все',0,$qa_value);
  665. print_select_item('Выключенные',1,$qa_value);
  666. print_select_item('Включенные',2,$qa_value);
  667. print "</select>\n";
  668. }
  669. function print_vendor_select($db, $qa_name, $qa_value)
  670. {
  671. print "<select name=\"$qa_name\" class=\"js-select-single\" style=\"width: 100%\">\n";
  672. $sSQL = "SELECT id,`name` FROM `vendors` order by `name`";
  673. $vendors = mysqli_query($db, $sSQL);
  674. print_select_item('Всё',0,$qa_value);
  675. while (list ($v_id, $v_name) = mysqli_fetch_array($vendors)) {
  676. print_select_item($v_name,$v_id,$qa_value);
  677. }
  678. print "</select>\n";
  679. }
  680. function print_vendor_set($db, $qa_name, $qa_value)
  681. {
  682. print "<select name=\"$qa_name\" class=\"js-select-single\" style=\"width: 100%\">\n";
  683. $sSQL = "SELECT id,`name` FROM `vendors` order by `name`";
  684. $vendors = mysqli_query($db, $sSQL);
  685. while (list ($v_id, $v_name) = mysqli_fetch_array($vendors)) {
  686. print_select_item($v_name,$v_id,$qa_value);
  687. }
  688. print "</select>\n";
  689. }
  690. function get_vendor_name($db, $v_id)
  691. {
  692. $sSQL = "SELECT `name` FROM `vendors` WHERE id=$v_id";
  693. $vendors = mysqli_query($db, $sSQL);
  694. (list ($v_name) = mysqli_fetch_array($vendors));
  695. return $v_name;
  696. }
  697. function get_qa($qa_value)
  698. {
  699. if ($qa_value) { return "Да"; }
  700. return "Нет";
  701. }
  702. function print_action_select($action_name, $action_value)
  703. {
  704. print "<select name=\"$action_name\">\n";
  705. print_select_item('Разрешить',1,$action_value);
  706. print_select_item('Запретить',0,$action_value);
  707. print "</select>\n";
  708. }
  709. function get_action($action_value)
  710. {
  711. if ($action_value) { return "Разрешить"; }
  712. return "Запретить";
  713. }
  714. function print_filter_select($db, $filter_name, $group_id)
  715. {
  716. print "<select name=\"$filter_name\" class=\"js-select-single\">\n";
  717. if (isset($group_id)) {
  718. $sSQL = "SELECT id,name FROM Filter_list WHERE Filter_list.id not in (Select filter_id FROM Group_filters WHERE group_id=$group_id)";
  719. } else {
  720. $sSQL = "SELECT id,name FROM Filter_list Order by name";
  721. }
  722. $t_filters = mysqli_query($db, $sSQL);
  723. while (list ($filter_id, $filter_name) = mysqli_fetch_array($t_filters)) {
  724. print_select_item($filter_name,$filter_id,0);
  725. }
  726. print "</select>\n";
  727. }
  728. function get_filter($db, $filter_value)
  729. {
  730. list ($filter) = mysqli_fetch_array(mysqli_query($db, "SELECT name FROM Filter_list WHERE id=".$filter_value));
  731. return $filter;
  732. }
  733. function get_login($db, $user_id)
  734. {
  735. list ($login) = mysqli_fetch_array(mysqli_query($db, "SELECT login FROM User_list WHERE id=$user_id"));
  736. return $login;
  737. }
  738. function get_auth_count($db, $user_id)
  739. {
  740. list ($count) = mysqli_fetch_array(mysqli_query($db, "SELECT count(id) FROM User_auth WHERE user_id=$user_id and deleted=0"));
  741. return $count;
  742. }
  743. function print_login_select($db, $login_name, $current_login)
  744. {
  745. print "<select name=\"$login_name\" class=\"js-select-single\">\n";
  746. $t_login = mysqli_query($db, "SELECT id,login FROM User_list Order by Login");
  747. print_select_item('None',0,$current_login);
  748. while (list ($f_user_id, $f_login) = mysqli_fetch_array($t_login)) {
  749. print_select_item($f_login,$f_user_id,$current_login);
  750. }
  751. print "</select>\n";
  752. }
  753. function print_auth_select($db, $login_name, $current_auth)
  754. {
  755. print "<select name=\"$login_name\" class=\"js-select-single\">\n";
  756. $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");
  757. print_select_item('Empty',0,$current_auth);
  758. while (list ($f_login, $f_fio, $f_ip, $f_auth_id) = mysqli_fetch_array($t_login)) {
  759. print_select_item($f_login."[" . $f_fio . "] - ".$f_ip,$f_auth_id,$current_auth);
  760. }
  761. print "</select>\n";
  762. }
  763. function print_auth_select_mac($db, $login_name, $current_auth)
  764. {
  765. print "<select name=\"$login_name\" class=\"js-select-single\">\n";
  766. $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");
  767. print_select_item('Empty',0,$current_auth);
  768. while (list ($f_login, $f_fio, $f_ip, $f_mac, $f_auth_id) = mysqli_fetch_array($t_login)) {
  769. print_select_item($f_login."[" . $f_mac . "] - ".$f_ip,$f_auth_id,$current_auth);
  770. }
  771. print "</select>\n";
  772. }
  773. function print_device_port_select($db, $field_name, $device_id, $target_id)
  774. {
  775. print "<select name=\"$field_name\" class=\"js-select-single\">\n";
  776. if (! isset($target_id)) {
  777. $target_id = 0;
  778. }
  779. if (! isset($device_id)) {
  780. $device_id = 0;
  781. }
  782. $d_sql = "SELECT D.device_name, DP.port, DP.device_id, DP.id 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";
  783. $t_device = mysqli_query($db, $d_sql);
  784. print_select_item('Empty',0,$target_id);
  785. while (list ($f_name, $f_port, $f_device_id, $f_target_id) = mysqli_fetch_array($t_device)) {
  786. print_select_item($f_name."[" . $f_port . "]",$f_target_id,$target_id);
  787. }
  788. print "</select>\n";
  789. }
  790. function print_device_select($db, $field_name, $device_id)
  791. {
  792. print "<select name=\"$field_name\" class=\"js-select-single\" >\n";
  793. $d_sql = "SELECT D.device_name, D.id FROM devices AS D Where D.deleted=0 order by D.device_name ASC";
  794. $t_device = mysqli_query($db, $d_sql);
  795. print_select_item('Все',0,$device_id);
  796. while (list ($f_name, $f_device_id) = mysqli_fetch_array($t_device)) {
  797. print_select_item($f_name,$f_device_id,$device_id);
  798. }
  799. print "</select>\n";
  800. }
  801. function print_vlan_select($db, $field_name, $vlan)
  802. {
  803. print "<select name=\"$field_name\" class=\"js-select-single\">\n";
  804. $d_sql = "SELECT DISTINCT vlan FROM device_ports ORDER BY vlan DESC";
  805. $v_device = mysqli_query($db, $d_sql);
  806. if (!isset($vlan) or $vlan ==='') { $vlan=1; };
  807. print_select_item('1',1,$vlan);
  808. while (list ($f_vlan) = mysqli_fetch_array($v_device)) {
  809. if ($f_vlan === '1') { continue; }
  810. print_select_item($f_vlan,$f_vlan,$vlan);
  811. }
  812. print "</select>\n";
  813. }
  814. function print_device_select_ip($db, $field_name, $device_ip)
  815. {
  816. print "<select name=\"$field_name\" class=\"js-select-single\" >\n";
  817. $d_sql = "SELECT D.device_name, D.ip FROM devices AS D Where D.deleted=0 order by D.device_name ASC";
  818. $t_device = mysqli_query($db, $d_sql);
  819. print_select_item('Все','',$device_ip);
  820. while (list ($f_name, $f_device_ip) = mysqli_fetch_array($t_device)) {
  821. print_select_item($f_name,$f_device_ip,$device_ip);
  822. }
  823. print "</select>\n";
  824. }
  825. function print_syslog_device_select($db, $field_name, $syslog_filter, $device_ip)
  826. {
  827. print "<select name=\"$field_name\" class=\"js-select-single\" >\n";
  828. $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";
  829. $t_device = mysqli_query($db, $d_sql);
  830. print_select_item('Все','',$device_ip);
  831. while (list ($f_ip, $f_name) = mysqli_fetch_array($t_device)) {
  832. if (!isset($f_name) or $f_name === '') { $f_name=$f_ip; }
  833. print_select_item($f_name,$f_ip,$device_ip);
  834. }
  835. print "</select>\n";
  836. }
  837. function print_gateway_select($db, $field_name, $device_id)
  838. {
  839. print "<select name=\"$field_name\" >\n";
  840. $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";
  841. $t_device = mysqli_query($db, $d_sql);
  842. print_select_item('Все',0,$device_id);
  843. while (list ($f_name, $f_device_id) = mysqli_fetch_array($t_device)) {
  844. print_select_item($f_name,$f_device_id,$device_id);
  845. }
  846. print "</select>\n";
  847. }
  848. function get_gateways($db)
  849. {
  850. $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";
  851. $t_device = mysqli_query($db, $d_sql);
  852. unset($result);
  853. while (list ($f_name, $f_device_id) = mysqli_fetch_array($t_device)) {
  854. $result[$f_device_id] = $f_name;
  855. }
  856. return $result;
  857. }
  858. function print_device_port($db, $target_id)
  859. {
  860. $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";
  861. $t_device = mysqli_query($db, $d_sql);
  862. while (list ($f_name, $f_port, $f_device_id) = mysqli_fetch_array($t_device)) {
  863. print "<a href=\"/admin/devices/switchport.php?id=$f_device_id\">" . $f_name . "[" . $f_port . "]</a>\n";
  864. }
  865. }
  866. function get_device_ips($db, $device_id)
  867. {
  868. $switch=get_record($db,'devices','id='.$device_id);
  869. $index=0;
  870. if ($switch['user_id']) {
  871. $auth_ips=get_records($db,'User_auth','deleted=0 and user_id='.$switch['user_id']);
  872. foreach ( $auth_ips as $key => $value ) {
  873. if (isset($value['ip'])) { $result[$index]=$value['ip']; $index++; }
  874. }
  875. } else {
  876. if (isset($switch['ip'])) { $result[$index]=$switch['ip']; $index++; }
  877. }
  878. return $result;
  879. }
  880. function get_device_id($db, $device_name)
  881. {
  882. $d_sql = "SELECT id FROM devices WHERE device_name='$device_name' and deleted=0";
  883. $t_device = mysqli_query($db, $d_sql);
  884. list ($f_device_id) = mysqli_fetch_array($t_device);
  885. return $f_device_id;
  886. }
  887. function get_device_name($db, $device_id)
  888. {
  889. $d_sql = "SELECT device_name FROM devices WHERE id='$device_id'";
  890. $t_device = mysqli_query($db, $d_sql);
  891. list ($f_device_name) = mysqli_fetch_array($t_device);
  892. return $f_device_name;
  893. }
  894. function get_auth_by_ip($db, $ip)
  895. {
  896. $d_sql = "SELECT id FROM User_auth WHERE ip='$ip' and deleted=0";
  897. $auth_q = mysqli_query($db, $d_sql);
  898. list ($f_auth_id) = mysqli_fetch_array($auth_q);
  899. return $f_auth_id;
  900. }
  901. function get_user_by_ip($db, $ip)
  902. {
  903. $d_sql = "SELECT user_id FROM User_auth WHERE ip='$ip' and deleted=0";
  904. $auth_q = mysqli_query($db, $d_sql);
  905. list ($f_auth_id) = mysqli_fetch_array($auth_q);
  906. return $f_auth_id;
  907. }
  908. function print_auth_port($db, $port_id)
  909. {
  910. $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";
  911. $t_device = mysqli_query($db, $d_sql);
  912. while (list ($f_ip, $f_int, $f_mac, $f_auth_id, $f_dns) = mysqli_fetch_array($t_device)) {
  913. $name = $f_ip;
  914. if (isset($f_dns) and $f_dns != '') {
  915. $name = $f_dns;
  916. }
  917. print "<a href=\"/admin/users/editauth.php?id=$f_auth_id\">" . $name . " [" . $f_ip . "]</a><br>";
  918. }
  919. }
  920. function print_auth($db, $auth_id)
  921. {
  922. $auth = get_record($db,"User_auth","id=$auth_id");
  923. $name = $auth['dns_name'];
  924. if (empty($name)) { $name = $auth['comments']; } else { $name.=" (".$auth['comments'].")"; }
  925. if (empty($name)) { $name = $auth['ip']; } else { $name.=" [".$auth['ip']."]"; }
  926. print "<a href=\"/admin/users/editauth.php?id=$auth_id\">" . $name . "</a><br>";
  927. }
  928. function print_auth_detail($db, $auth_id)
  929. {
  930. $auth = get_record($db,"User_auth","id=$auth_id");
  931. $name = $auth['dns_name'];
  932. if (empty($name)) { $name = $auth['comments']; } else { $name.=" (".$auth['comments'].")"; }
  933. if (empty($name)) { $name = $auth['ip']; } else { $name.=" [".$auth['ip']."]"; }
  934. $name.=" last: [".$auth['last_found']."] ";
  935. if ($auth['deleted']) { $name.=" <font color='red'>DELETED!!!</font>"; }
  936. print "<a href=\"/admin/users/editauth.php?id=$auth_id\">" . $name . "</a><br>";
  937. }
  938. function get_auth_port_count($db, $port_id)
  939. {
  940. $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";
  941. $t_device = mysqli_query($db, $d_sql);
  942. list ($f_count) = mysqli_fetch_array($t_device);
  943. if (! isset($f_count)) {
  944. $f_count = 0;
  945. }
  946. return $f_count;
  947. }
  948. function get_connection($db, $auth_id)
  949. {
  950. $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";
  951. $t_device = mysqli_query($db, $d_sql);
  952. list ($f_name, $f_port) = mysqli_fetch_array($t_device);
  953. if (isset($f_name)) {
  954. $result = expand_device_name($db, $f_name) . "[" . $f_port . "]";
  955. } else {
  956. $result = '';
  957. }
  958. return $result;
  959. }
  960. function get_port($db, $port_id)
  961. {
  962. $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";
  963. $t_device = mysqli_query($db, $d_sql);
  964. list ($f_name, $f_port) = mysqli_fetch_array($t_device);
  965. if (isset($f_name)) {
  966. $result = expand_device_name($db, $f_name) . "[" . $f_port . "]";
  967. } else {
  968. $result = '';
  969. }
  970. return $result;
  971. }
  972. function print_option_select($db, $option_name)
  973. {
  974. print "<select name=\"$option_name\">\n";
  975. $t_option = mysqli_query($db, "SELECT id,option_name FROM config_options WHERE uniq=0 order by option_name");
  976. while (list ($f_id, $f_name) = mysqli_fetch_array($t_option)) {
  977. print "<option value=$f_id>$f_name</option>";
  978. }
  979. $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");
  980. while (list ($f_id, $f_name) = mysqli_fetch_array($t_option)) {
  981. print "<option value=$f_id>$f_name</option>";
  982. }
  983. print "</select>\n";
  984. }
  985. function run_sql($db, $query)
  986. {
  987. return mysqli_query($db, $query);
  988. }
  989. function get_count_records($db, $table, $filter)
  990. {
  991. if (isset($filter)) { $filter = 'where ' . $filter; }
  992. $t_count = mysqli_query($db, "SELECT count(*) FROM $table $filter");
  993. list ($count) = mysqli_fetch_array($t_count);
  994. if (! isset($count)) { $count = 0; }
  995. return $count;
  996. }
  997. function get_id_record($db, $table, $filter)
  998. {
  999. if (isset($filter)) {
  1000. $filter = 'WHERE ' . $filter;
  1001. }
  1002. $t_record = mysqli_query($db, "SELECT id FROM $table $filter limit 1");
  1003. list ($id) = mysqli_fetch_array($t_record);
  1004. return $id;
  1005. }
  1006. function set_changed($db,$id)
  1007. {
  1008. $auth['changed'] = 1;
  1009. update_record($db, "User_auth", "id=" . $id, $auth);
  1010. }
  1011. function ResolveIP($db,$ip_int) {
  1012. $ip_name="-";
  1013. if (empty($ip_int)) { return $ip_name; }
  1014. $dns_cache=get_record_sql($db,"SELECT * FROM dns_cache WHERE ip=$ip_int");
  1015. if (empty($dns_cache) or empty($dns_cache['dns'])) {
  1016. $ip_name = gethostbyaddr(long2ip($ip_int));
  1017. if (empty($ip_name) or $ip_name === long2ip($ip_int)) { $ip_name="-"; }
  1018. run_sql($db,"INSERT INTO dns_cache(dns,ip) VALUES('".$ip_name."',".$ip_int.")");
  1019. } else { $ip_name=$dns_cache['dns']; }
  1020. return $ip_name;
  1021. }
  1022. function clean_dns_cache($db) {
  1023. $date = time();
  1024. $date = $date - 86400;
  1025. $now = strftime('%Y-%m-%d %H:%M:%S',time());
  1026. $clean_date=strftime('%Y-%m-%d %H:%M:%S',$date);
  1027. LOG_DEBUG($db,"Clean dns cache before $clean_date at $now");
  1028. run_sql($db,"DELETE FROM dns_cache WHERE `timestamp`<='".$clean_date."'");
  1029. }
  1030. function FormatDateStr($format = 'Y-m-d H:i:s', $date_str) {
  1031. $date1 = GetDateTimeFromString($date_str);
  1032. $result = $date1->format($format);
  1033. return $result;
  1034. }
  1035. function GetDateTimeFromString($date_str) {
  1036. if (!is_a($date_str,'DateTime')) {
  1037. $date_str = urldecode($date_str);
  1038. $date_str = preg_replace('/(\'|\")/','',$date_str);
  1039. $date1 = DateTime::createFromFormat('Y-m-d H:i:s',$date_str);
  1040. if (!$date1) { $date1 = DateTime::createFromFormat('Y.m.d H:i:s',$date_str); }
  1041. if (!$date1) { $date1 = DateTime::createFromFormat('Y/m/d H:i:s',$date_str); }
  1042. if (!$date1) { $date1 = DateTime::createFromFormat('Y-m-d H:i',$date_str); }
  1043. if (!$date1) { $date1 = DateTime::createFromFormat('Y.m.d H:i',$date_str); }
  1044. if (!$date1) { $date1 = DateTime::createFromFormat('Y/m/d H:i',$date_str); }
  1045. if (!$date1) { $date1 = DateTime::createFromFormat('Y-m-d|',$date_str); }
  1046. if (!$date1) { $date1 = DateTime::createFromFormat('Y.m.d|',$date_str); }
  1047. if (!$date1) { $date1 = DateTime::createFromFormat('Y/m/d|',$date_str); }
  1048. if (!$date1) {
  1049. $date1 = new DateTime;
  1050. $date1->setTime(0,0,0,1);
  1051. }
  1052. } else { return $date_str; }
  1053. return $date1;
  1054. }
  1055. function GetNowTimeString() {
  1056. $now = strftime('%Y-%m-%d %H:%M:%S',time());
  1057. return $now;
  1058. }
  1059. function GetNowDayString() {
  1060. $now = strftime('%Y-%m-%d',time());
  1061. return $now;
  1062. }
  1063. function get_ip_subnet($db,$ip)
  1064. {
  1065. if (empty($ip)) { return; }
  1066. $ip_aton = ip2long($ip);
  1067. $t_option = mysqli_query($db, "SELECT id,subnet,ip_int_start,ip_int_stop FROM `subnets` WHERE hotspot=1 or office=1");
  1068. while (list ($f_net_id,$f_net,$f_start,$f_stop) = mysqli_fetch_array($t_option)) {
  1069. if ($ip_aton >= $f_start and $ip_aton <= $f_stop) {
  1070. $result['subnet_id']=$f_net_id;
  1071. $result['subnet']=$f_net;
  1072. $result['int_start']=$f_start;
  1073. $result['int_stop']=$f_stop;
  1074. return $result;
  1075. }
  1076. }
  1077. return;
  1078. }
  1079. function find_mac_in_subnet($db,$ip,$mac)
  1080. {
  1081. if (empty($ip)) { return; }
  1082. if (empty($mac)) { return; }
  1083. $ip_subnet=get_ip_subnet($db,$ip);
  1084. if (!isset($ip_subnet)) { return; }
  1085. $t_auth=mysqli_query($db, "SELECT id FROM User_auth WHERE ip_int>=".$ip_subnet['int_start']." and ip_int<=".$ip_subnet['int_stop']." and mac='" . $mac . "' and deleted=0");
  1086. $auth_count=0;
  1087. $result['count']=0;
  1088. while (list($aid)=mysqli_fetch_array($t_auth)) {
  1089. if (isset($aid) and $aid>0) {
  1090. $auth_count++;
  1091. $result['count']=$auth_count;
  1092. $result[$auth_count]=$aid;
  1093. }
  1094. }
  1095. return $result;
  1096. }
  1097. function apply_auth_rule($db,$auth_id,$user_id) {
  1098. if (empty($auth_id)) { return; }
  1099. if (empty($user_id)) { return; }
  1100. $user_rec = get_record($db, 'User_list', "id=".$user_id);
  1101. if (empty($user_rec)) { return; }
  1102. //set filter and status by user
  1103. $set_auth['filter_group_id']=$user_rec['filter_group_id'];
  1104. $set_auth['queue_id']= $user_rec['queue_id'];
  1105. $set_auth['enabled'] = $user_rec['enabled'];
  1106. update_record($db, "User_auth", "id=$auth_id", $set_auth);
  1107. }
  1108. function fix_auth_rules($db) {
  1109. global $default_user_id;
  1110. global $hotspot_user_id;
  1111. //cleanup hotspot subnet rules
  1112. delete_record($db,"auth_rules","user_id=".$default_user_id);
  1113. delete_record($db,"auth_rules","user_id=".$hotspot_user_id);
  1114. $t_hotspot = get_records_sql($db,"subnets","hotspot=1");
  1115. foreach ($t_hotspot as $row) { delete_record($db,"auth_rules","rule='".$row['subnet']."'"); }
  1116. }
  1117. function new_auth($db, $ip, $mac, $user_id)
  1118. {
  1119. $ip_aton = ip2long($ip);
  1120. $msg = '';
  1121. // default id
  1122. $save_traf = get_option($db, 23);
  1123. if (!empty($mac)) {
  1124. list ($lid, $aid) = mysqli_fetch_array(mysqli_query($db, "Select user_id,id FROM User_auth WHERE ip_int=$ip_aton and mac='" . $mac . "' and deleted=0 limit 1"));
  1125. if ($lid > 0) {
  1126. LOG_WARNING($db, "Pair ip-mac already exists! Skip creating $ip [$mac] auth_id: $aid");
  1127. return $aid;
  1128. }
  1129. }
  1130. $mac_exists=find_mac_in_subnet($db,$ip,$mac);
  1131. if (isset($mac_exists) and $mac_exists['count']>0) {
  1132. LOG_WARNING($db, "Mac $mac already exists in this subnet! Skip creating $ip [$mac] auth_id: ".$mac_exists['1']);
  1133. return;
  1134. }
  1135. // search changed mac
  1136. list ($aid, $amac) = mysqli_fetch_array(mysqli_query($db, "Select id,mac FROM User_auth WHERE ip_int=$ip_aton and deleted=0 limit 1"));
  1137. if ($aid > 0) {
  1138. if (empty($amac)) {
  1139. $auth['user_id'] = $user_id;
  1140. $auth['ip'] = $ip;
  1141. $auth['ip_int'] = $ip_aton;
  1142. $auth['mac'] = $mac;
  1143. $auth['deleted'] = 0;
  1144. $auth['save_traf'] = $save_traf *1;
  1145. LOG_INFO($db, "for ip: $ip mac not found! Use empty record...");
  1146. update_record($db, "User_auth", "id=" . $aid, $auth);
  1147. apply_auth_rule($db,$aid,$user_id);
  1148. return $aid;
  1149. } else {
  1150. LOG_WARNING($db, "Ip [$ip] already exists! Old mac: [$amac]. Skip creating!");
  1151. return $aid;
  1152. }
  1153. }
  1154. $resurrection_id = NULL;
  1155. // seek old auth with same ip and mac
  1156. if (get_count_records($db, 'User_auth', "ip_int=" . $ip_aton . " and mac='" . $mac . "'")) {
  1157. // found ->Resurrection old record
  1158. $resurrection_id = get_id_record($db, 'User_auth', "ip_int=" . $ip_aton . " and mac='" . $mac . "'");
  1159. $msg.="Восстанавливаем доступ для auth_id: $resurrection_id with ip: $ip and mac: $mac ";
  1160. $auth['user_id'] = $user_id;
  1161. $auth['deleted'] = 0;
  1162. $auth['save_traf'] = $save_traf *1;
  1163. update_record($db, "User_auth", "id=$resurrection_id", $auth);
  1164. } else {
  1165. // not found ->create new record
  1166. $msg.="Создаём новый ip-адрес \r\nip: $ip\r\nmac: $mac\r\n";
  1167. $auth['deleted'] = 0;
  1168. $auth['user_id'] = $user_id;
  1169. $auth['ip'] = $ip;
  1170. $auth['ip_int'] = $ip_aton;
  1171. $auth['mac'] = $mac;
  1172. $auth['save_traf'] = $save_traf *1;
  1173. $resurrection_id=insert_record($db, "User_auth", $auth);
  1174. }
  1175. //check rules, update filter and state for new record
  1176. if (!empty($resurrection_id)) {
  1177. apply_auth_rule($db,$resurrection_id,$user_id);
  1178. if (!is_hotspot($db,$ip) and !empty($msg)) { LOG_WARNING($db, $msg); }
  1179. }
  1180. return $resurrection_id;
  1181. }
  1182. function resurrection_auth($db, $ip, $mac, $action, $dhcp_hostname)
  1183. {
  1184. $ip_aton = ip2long($ip);
  1185. list ($lid, $aid) = mysqli_fetch_array(mysqli_query($db, "Select user_id,id FROM User_auth WHERE ip_int=$ip_aton and mac='" . $mac . "' and deleted=0 limit 1"));
  1186. if ($lid > 0) {
  1187. list ($lname) = mysqli_fetch_array(mysqli_query($db, "Select login FROM User_list WHERE id=$lid"));
  1188. LOG_DEBUG($db, "external dhcp user $lname [$ip] auth_id: $aid");
  1189. if (isset($dhcp_hostname) and !empty($dhcp_hostname)) { $auth['dhcp_hostname'] = $dhcp_hostname; }
  1190. $auth['dhcp_action'] = $action;
  1191. $auth['dhcp_time'] = GetNowTimeString();
  1192. if ($action == 'add') { $auth['last_found'] = GetNowTimeString(); }
  1193. update_record($db, "User_auth", "id=" . $aid, $auth);
  1194. return $aid;
  1195. }
  1196. // default id
  1197. $new_id = get_new_user_id($db, $ip, $mac);
  1198. $save_traf = get_option($db, 23);
  1199. $msg = '';
  1200. // search changed mac
  1201. list ($aid, $amac) = mysqli_fetch_array(mysqli_query($db, "Select id,mac FROM User_auth WHERE ip_int=$ip_aton and deleted=0 limit 1"));
  1202. if ($aid > 0) {
  1203. if (empty($amac)) {
  1204. $auth['user_id'] = $new_id;
  1205. $auth['ip'] = $ip;
  1206. $auth['ip_int'] = $ip_aton;
  1207. $auth['mac'] = $mac;
  1208. $auth['deleted'] = 0;
  1209. $auth['save_traf'] = $save_traf *1;
  1210. $auth['dhcp_action'] = $action;
  1211. $auth['dhcp_time'] = GetNowTimeString();
  1212. if (!empty($dhcp_hostname)) { $auth['dhcp_hostname'] = $dhcp_hostname; }
  1213. if ($action == 'add') { $auth['last_found'] = GetNowTimeString(); }
  1214. LOG_INFO($db, "for ip: $ip mac not found! Use empty record...");
  1215. update_record($db, "User_auth", "id=" . $aid, $auth);
  1216. return $aid;
  1217. } else {
  1218. LOG_WARNING($db, "for ip: $ip mac change detected! Old mac: [$amac] New mac: [$mac]. Disable old auth_id: $aid");
  1219. run_sql($db, "Update User_auth set deleted=1 WHERE id=" . $aid);
  1220. }
  1221. }
  1222. $resurrection_id=NULL;
  1223. // seek old auth with same ip and mac
  1224. if (get_count_records($db, 'User_auth', "ip_int=" . $ip_aton . " and mac='" . $mac . "'")) {
  1225. // found ->Resurrection old record
  1226. $resurrection_id = get_id_record($db, 'User_auth', "ip_int=" . $ip_aton . " and mac='" . $mac . "'");
  1227. $msg .="Восстанавливаем доступ для auth_id: $resurrection_id with ip: $ip and mac: $mac ";
  1228. $auth['dhcp_action'] = $action;
  1229. $auth['user_id'] = $new_id;
  1230. $auth['deleted'] = 0;
  1231. $auth['dhcp_time'] = GetNowTimeString();
  1232. $auth['save_traf'] = $save_traf *1;
  1233. if (!empty($dhcp_hostname)) { $auth['dhcp_hostname'] = $dhcp_hostname; }
  1234. if ($action == 'add') { $auth['last_found'] = GetNowTimeString(); }
  1235. update_record($db, "User_auth", "id=$resurrection_id", $auth);
  1236. } else {
  1237. // not found ->create new record
  1238. $msg.="Создаём новый ip-адрес \r\nip: $ip\r\nmac: $mac\r\n";
  1239. $auth['deleted'] = 0;
  1240. $auth['user_id'] = $new_id;
  1241. $auth['ip'] = $ip;
  1242. $auth['ip_int'] = $ip_aton;
  1243. $auth['mac'] = $mac;
  1244. $auth['dhcp_action'] = $action;
  1245. $auth['dhcp_time'] = GetNowTimeString();
  1246. $auth['save_traf'] = $save_traf *1;
  1247. if (!empty($dhcp_hostname)) { $auth['dhcp_hostname'] = $dhcp_hostname; }
  1248. if ($action == 'add') { $auth['last_found'] = GetNowTimeString(); }
  1249. $resurrection_id=insert_record($db, "User_auth", $auth);
  1250. }
  1251. //check rules, update filter and state for new record
  1252. if (!empty($resurrection_id)) {
  1253. $user_rec = get_record($db, 'User_list', "id=".$new_id);
  1254. //set filter and status by user
  1255. $set_auth['filter_group_id']=$user_rec['filter_group_id'];
  1256. $set_auth['queue_id']= $user_rec['queue_id'];
  1257. $set_auth['enabled'] = $user_rec['enabled'];
  1258. update_record($db, "User_auth", "id=$resurrection_id", $set_auth);
  1259. $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";
  1260. }
  1261. if (!is_hotspot($db,$ip) and !empty($msg)) { LOG_WARNING($db, $msg); }
  1262. return $resurrection_id;
  1263. }
  1264. function get_auth($db, $current_auth)
  1265. {
  1266. if (! isset($current_auth)) {
  1267. return;
  1268. }
  1269. if ($current_auth == 0) {
  1270. return;
  1271. }
  1272. $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");
  1273. list ($f_login, $f_ip) = mysqli_fetch_array($t_login);
  1274. $result = $f_login . "[" . $f_ip . "]";
  1275. return $result;
  1276. }
  1277. function get_auth_by_mac($db, $mac)
  1278. {
  1279. if (! isset($mac)) { return; }
  1280. $mac = mac_dotted($mac);
  1281. $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 . "' order by A.last_found DESC limit 1");
  1282. list ($f_id, $f_login, $f_auth_id, $f_ip) = mysqli_fetch_array($t_login);
  1283. if (isset($f_id)) {
  1284. $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>';
  1285. } else { $result['auth']='Unknown'; }
  1286. $result['mac'] = expand_mac($db,$mac);
  1287. return $result;
  1288. }
  1289. function get_auth_mac($db, $current_auth)
  1290. {
  1291. if (! isset($current_auth)) {
  1292. return;
  1293. }
  1294. if ($current_auth == 0) {
  1295. return;
  1296. }
  1297. $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");
  1298. list ($f_login, $f_mac) = mysqli_fetch_array($t_login);
  1299. $result = $f_login . "[" . $f_mac . "]";
  1300. return $result;
  1301. }
  1302. function isRO($db)
  1303. {
  1304. $result = 1;
  1305. if (isset($_SESSION['login'])) {
  1306. $work_user = $_SESSION['login'];
  1307. }
  1308. if (isset($_SESSION['user_id'])) {
  1309. $work_id = $_SESSION['user_id'];
  1310. }
  1311. if (! isset($work_user) or ! isset($work_id)) {
  1312. return $result;
  1313. }
  1314. $t_login = mysqli_query($db, "SELECT readonly FROM Customers WHERE Login='" . $work_user . "' and id='" . $work_id . "'");
  1315. list ($f_ro) = mysqli_fetch_array($t_login);
  1316. if (! isset($f_ro)) {
  1317. return $result;
  1318. }
  1319. return $f_ro;
  1320. }
  1321. function LOG_INFO($db,$msg) {
  1322. global $L_INFO;
  1323. global $log_level;
  1324. if ($log_level < $L_INFO) { return; }
  1325. write_log($db,$msg,$L_INFO);
  1326. }
  1327. function LOG_ERROR($db,$msg) {
  1328. global $L_ERROR;
  1329. global $log_level;
  1330. if ($log_level < $L_ERROR) { return; }
  1331. email($L_ERROR,$msg);
  1332. write_log($db,$msg,$L_ERROR);
  1333. }
  1334. function LOG_VERBOSE($db,$msg) {
  1335. global $L_VERBOSE;
  1336. global $log_level;
  1337. if ($log_level < $L_VERBOSE) { return; }
  1338. write_log($db,$msg,$L_VERBOSE);
  1339. }
  1340. function LOG_WARNING($db,$msg) {
  1341. global $L_WARNING;
  1342. global $log_level;
  1343. if ($log_level < $L_WARNING) { return; }
  1344. email($L_WARNING,$msg);
  1345. write_log($db,$msg,$L_WARNING);
  1346. }
  1347. function LOG_DEBUG($db,$msg) {
  1348. global $debug;
  1349. global $L_DEBUG;
  1350. if (isset($debug) and $debug) { write_log($db,$msg,$L_DEBUG); }
  1351. }
  1352. function email ($level,$msg) {
  1353. global $send_email;
  1354. global $admin_email;
  1355. global $sender_email;
  1356. global $L_WARNING;
  1357. global $L_ERROR;
  1358. if (!$send_email) { return; }
  1359. if (!($level === $L_WARNING or $level === $L_ERROR)) { return; }
  1360. $subject = substr($msg,0,80);
  1361. if ($level === $L_WARNING) { $subject = "WARN: ".$subject."..."; $message = 'WARNING! Manager: '.$_SESSION['login'].' </br>'; }
  1362. if ($level === $L_ERROR) { $subject = "ERROR: ".$subject."..."; $message = 'ERROR! Manager: '.$_SESSION['login'].' </br>'; }
  1363. $msg_lines = preg_replace("/\r\n/","</br>",$msg);
  1364. $message .= $msg_lines;
  1365. $send = SimpleMail::make()
  1366. ->setTo($admin_email, 'Administrator')
  1367. ->setFrom($sender_email, 'Stat')
  1368. ->setSubject($subject)
  1369. ->setMessage($message)
  1370. ->setHtml()
  1371. ->setWrap(80)
  1372. ->send();
  1373. }
  1374. function write_log($db, $msg, $level)
  1375. {
  1376. $work_user = 'http';
  1377. if (isset($_SESSION['login'])) {
  1378. $work_user = $_SESSION['login'];
  1379. }
  1380. if (! isset($msg)) {
  1381. $msg = 'ERROR! Empty log string!';
  1382. }
  1383. global $L_INFO;
  1384. if (!isset($level)) { $level = $L_INFO; }
  1385. $msg = str_replace("'", '', $msg);
  1386. $sSQL = "insert into syslog(customer,message,level) values('$work_user','$msg',$level)";
  1387. mysqli_query($db, $sSQL);
  1388. }
  1389. function print_year_select($year_name, $year)
  1390. {
  1391. print "<select name=\"$year_name\" class=\"js-select-single\" >\n";
  1392. for ($i = $year - 10; $i <= $year + 10; $i ++) {
  1393. print_select_item($i,$i,$year);
  1394. }
  1395. print "</select>\n";
  1396. }
  1397. function print_date_select($dd, $mm, $yy)
  1398. {
  1399. if ($dd >= 1) {
  1400. print "<b>День</b>\n";
  1401. print "<select name=\"day\" class=\"js-select-single\" >\n";
  1402. for ($i = 1; $i <= 31; $i ++) {
  1403. print_select_item($i,$i,$dd);
  1404. }
  1405. print "</select>\n";
  1406. }
  1407. if ($mm >= 1) {
  1408. print "<b>Месяц</b>\n";
  1409. print "<select name=\"month\" class=\"js-select-single\" >\n";
  1410. for ($i = 1; $i <= 12; $i ++) {
  1411. $month_name = strftime("%B", strtotime("$i/01/$yy"));
  1412. print_select_item($month_name,$i,$mm);
  1413. }
  1414. print "</select>\n";
  1415. }
  1416. print "<b>Год</b>\n";
  1417. print_year_select('year', $yy);
  1418. }
  1419. function print_date_select2($dd, $mm, $yy)
  1420. {
  1421. if ($dd >= 1) {
  1422. print "<b>День</b>\n";
  1423. print "<select name=\"day2\" class=\"js-select-single\" >\n";
  1424. for ($i = 1; $i <= 31; $i ++) {
  1425. print_select_item($i,$i,$dd);
  1426. }
  1427. print "</select>\n";
  1428. }
  1429. if ($mm >= 1) {
  1430. print "<b>Месяц</b>\n";
  1431. print "<select name=\"month2\" class=\"js-select-single\" >\n";
  1432. for ($i = 1; $i <= 12; $i ++) {
  1433. $month_name = strftime("%B", strtotime("$i/01/$yy"));
  1434. print_select_item($month_name,$i,$mm);
  1435. }
  1436. print "</select>\n";
  1437. }
  1438. print "<b>Год</b>\n";
  1439. print_year_select('year2', $yy);
  1440. }
  1441. function is_up($ip)
  1442. {
  1443. if (! isset($ip) or strlen($ip) == 0) {
  1444. return false;
  1445. }
  1446. exec(sprintf('ping -i .3 -c 1 -W 5 %s', escapeshellarg($ip)), $res, $rval);
  1447. return $rval === 0;
  1448. }
  1449. function get_mac_port_table($ip, $port_index, $community, $version, $oid)
  1450. {
  1451. if (! isset($ip)) {
  1452. return;
  1453. }
  1454. if (! isset($port_index)) {
  1455. return;
  1456. }
  1457. if (! isset($oid)) {
  1458. return;
  1459. }
  1460. if (! isset($community)) {
  1461. $community = 'public';
  1462. }
  1463. if (! isset($version)) {
  1464. $version = '2';
  1465. }
  1466. $mac_table = walk_snmp($ip, $community, $version, $oid);
  1467. if (isset($mac_table) and count($mac_table) > 0) {
  1468. foreach ($mac_table as $key => $value) {
  1469. $key = trim($key);
  1470. $value = intval(trim(str_replace('INTEGER:', '', $value)));
  1471. if ($value == $port_index) {
  1472. $pattern = '/\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})$/';
  1473. $result = preg_match($pattern, $key, $matches);
  1474. if ($result) {
  1475. $mac_key = preg_replace('/^\./', '', $matches[0]);
  1476. $fdb_port_table[$mac_key] = $value;
  1477. }
  1478. }
  1479. }
  1480. }
  1481. return $fdb_port_table;
  1482. }
  1483. function get_fdb_port_table($ip, $port_index, $community, $version)
  1484. {
  1485. global $mac_table_str_oid;
  1486. global $mac_table_str_oid2;
  1487. global $mac_table_oid;
  1488. global $mac_table_oid2;
  1489. if (! isset($ip)) {
  1490. return;
  1491. }
  1492. if (! isset($port_index)) {
  1493. return;
  1494. }
  1495. if (! isset($community)) {
  1496. $community = 'public';
  1497. }
  1498. if (! isset($version)) {
  1499. $version = '2';
  1500. }
  1501. $fdb_port_table = get_mac_port_table($ip, $port_index, $community, $version, $mac_table_oid2);
  1502. if (! isset($fdb_port_table) or ! $fdb_port_table or count($fdb_port_table) == 0) {
  1503. $fdb_port_table = get_mac_port_table($ip, $port_index, $community, $version, $mac_table_oid);
  1504. }
  1505. // maybe cisco?!
  1506. if (! isset($fdb_port_table) or ! $fdb_port_table or count($fdb_port_table) == 0) {
  1507. global $cisco_vlan_oid;
  1508. $vlan_table = walk_snmp($ip, $community, $version, $cisco_vlan_oid);
  1509. if (! $vlan_table) {
  1510. return;
  1511. }
  1512. foreach ($vlan_table as $vlan_oid => $value) {
  1513. if (! $vlan_oid) { continue; }
  1514. $pattern = '/\.(\d{1,4})$/';
  1515. $result = preg_match($pattern, $vlan_oid, $matches);
  1516. if ($result) {
  1517. $vlan_id = preg_replace('/^\./', '', $matches[0]);
  1518. if ($vlan_id > 1000 and $vlan_id < 1009) { continue; }
  1519. $fdb_vlan_table = get_mac_port_table($ip, $port_index, $community . '@' . $vlan_id, $version, $mac_table_oid2);
  1520. if (! isset($fdb_vlan_table) or ! $fdb_vlan_table or count($fdb_vlan_table) == 0) {
  1521. $fdb_vlan_table = get_mac_port_table($ip, $port_index, $community, $version, $mac_table_oid);
  1522. }
  1523. foreach ($fdb_vlan_table as $mac => $port) {
  1524. if (! isset($mac)) { continue; }
  1525. $fdb_port_table[$mac] = $port;
  1526. }
  1527. }
  1528. }
  1529. }
  1530. return $fdb_port_table;
  1531. }
  1532. function get_mac_table($ip, $community, $version, $oid)
  1533. {
  1534. if (! isset($ip)) {
  1535. return;
  1536. }
  1537. if (! isset($oid)) {
  1538. return;
  1539. }
  1540. if (! isset($community)) {
  1541. $community = 'public';
  1542. }
  1543. if (! isset($version)) {
  1544. $version = '2';
  1545. }
  1546. $mac_table = walk_snmp($ip, $community, $version, $oid);
  1547. if (isset($mac_table) and count($mac_table) > 0) {
  1548. foreach ($mac_table as $key => $value) {
  1549. $key = trim($key);
  1550. $value = intval(trim(str_replace('INTEGER:', '', $value)));
  1551. $pattern = '/\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})$/';
  1552. $result = preg_match($pattern, $key, $matches);
  1553. if ($result) {
  1554. $mac_key = preg_replace('/^\./', '', $matches[0]);
  1555. $fdb_port_table[$mac_key] = $value;
  1556. }
  1557. }
  1558. }
  1559. return $fdb_port_table;
  1560. }
  1561. function get_fdb_table($ip, $community, $version)
  1562. {
  1563. global $mac_table_str_oid;
  1564. global $mac_table_str_oid2;
  1565. global $mac_table_oid;
  1566. global $mac_table_oid2;
  1567. if (! isset($ip)) {
  1568. return;
  1569. }
  1570. if (! isset($community)) {
  1571. $community = 'public';
  1572. }
  1573. if (! isset($version)) {
  1574. $version = '2';
  1575. }
  1576. $fdb_port_table = get_mac_table($ip, $community, $version, $mac_table_oid2);
  1577. if (! isset($fdb_port_table) or ! $fdb_port_table or count($fdb_port_table) == 0) {
  1578. $fdb_port_table = get_mac_table($ip, $community, $version, $mac_table_oid);
  1579. }
  1580. // maybe cisco?!
  1581. if (! isset($fdb_port_table) or ! $fdb_port_table or count($fdb_port_table) == 0) {
  1582. global $cisco_vlan_oid;
  1583. $vlan_table = walk_snmp($ip, $community, $version, $cisco_vlan_oid);
  1584. if (! $vlan_table) {
  1585. return;
  1586. }
  1587. foreach ($vlan_table as $vlan_oid => $value) {
  1588. if (! $vlan_oid) { continue; }
  1589. $pattern = '/\.(\d{1,4})$/';
  1590. $result = preg_match($pattern, $vlan_oid, $matches);
  1591. if ($result) {
  1592. $vlan_id = preg_replace('/^\./', '', $matches[0]);
  1593. if ($vlan_id > 1000 and $vlan_id < 1009) { continue; }
  1594. $fdb_vlan_table = get_mac_table($ip, $community . '@' . $vlan_id, $version, $mac_table_oid2);
  1595. if (! isset($fdb_vlan_table) or ! $fdb_vlan_table or count($fdb_vlan_table) == 0) {
  1596. $fdb_vlan_table = get_mac_table($ip, $community, $version, $mac_table_oid);
  1597. }
  1598. foreach ($fdb_vlan_table as $mac => $port) {
  1599. if (! isset($mac)) { continue; }
  1600. $fdb_port_table[$mac] = $port;
  1601. }
  1602. }
  1603. }
  1604. }
  1605. return $fdb_port_table;
  1606. }
  1607. function check_snmp_access($ip, $community, $version)
  1608. {
  1609. if (! isset($ip)) {
  1610. return;
  1611. }
  1612. if (! isset($community)) {
  1613. $community = 'public';
  1614. }
  1615. if (! isset($version)) {
  1616. $version = '2';
  1617. }
  1618. $result = get_snmp($ip, $community, $version, '.1.3.6.1.2.1.1.1.0');
  1619. if (! $result) {
  1620. return;
  1621. }
  1622. return 1;
  1623. }
  1624. function get_port_state($port, $ip, $community, $version)
  1625. {
  1626. if (! isset($port)) {
  1627. return;
  1628. }
  1629. if (! isset($ip)) {
  1630. return;
  1631. }
  1632. if (! isset($community)) {
  1633. $community = 'public';
  1634. }
  1635. if (! isset($version)) {
  1636. $version = '2';
  1637. }
  1638. global $port_status_oid;
  1639. $port_oid = $port_status_oid . $port;
  1640. $port_state = get_snmp($ip, $community, $version, $port_oid);
  1641. return $port_state;
  1642. }
  1643. function get_last_digit($oid)
  1644. {
  1645. if (! isset($oid)) {
  1646. return;
  1647. }
  1648. $pattern = '/\.(\d{1,})$/';
  1649. preg_match($pattern, $oid, $matches);
  1650. return $matches[1];
  1651. }
  1652. function get_cisco_sensors($ip, $community, $version, $mkey)
  1653. {
  1654. $index = get_last_digit($mkey);
  1655. global $cisco_sfp_sensors;
  1656. global $cisco_sfp_precision;
  1657. list ($pattern, $result) = explode(':', get_snmp($ip, $community, $version, $cisco_sfp_sensors . "." . $index));
  1658. list ($pattern, $prec) = explode(':', get_snmp($ip, $community, $version, $cisco_sfp_precision . "." . $index));
  1659. if (! isset($prec)) {
  1660. $prec = 1;
  1661. }
  1662. $result = round(trim($result) / (10 * $prec), 2);
  1663. return $result;
  1664. }
  1665. function get_snmp_ifname($ip, $community, $version, $port)
  1666. {
  1667. global $ifmib_ifindex;
  1668. $port_name = '';
  1669. list ($pattern, $port_name) = explode(':', get_snmp($ip, $community, $version, $ifmib_ifindex . "." . $port));
  1670. return $port_name;
  1671. }
  1672. function get_snmp_interfaces($ip, $community, $version)
  1673. {
  1674. global $ifmib_ifindex;
  1675. $result = walk_snmp($ip, $community, $version, $ifmib_ifindex);
  1676. return $result;
  1677. }
  1678. function walk_snmp($ip, $community, $version, $oid)
  1679. {
  1680. if ($version == 2) {
  1681. $result = snmp2_real_walk($ip, $community, $oid);
  1682. }
  1683. if ($version == 1) {
  1684. $result = snmprealwalk($ip, $community, $oid);
  1685. }
  1686. return $result;
  1687. }
  1688. function get_snmp_module_id($modules_oids, $port_name)
  1689. {
  1690. $port_name = preg_quote(trim($port_name), '/');
  1691. foreach ($modules_oids as $key => $value) {
  1692. $pattern = '/' . $port_name . '/i';
  1693. preg_match($pattern, $value, $matches);
  1694. if (isset($matches[0])) {
  1695. $module_id = get_last_digit($key);
  1696. return $module_id;
  1697. }
  1698. }
  1699. return;
  1700. }
  1701. function get_sfp_status($vendor_id, $port, $ip, $community, $version, $modules_oids)
  1702. {
  1703. if (! isset($vendor_id)) {
  1704. return;
  1705. }
  1706. if (! isset($port)) {
  1707. return;
  1708. }
  1709. if (! isset($ip)) {
  1710. return;
  1711. }
  1712. if (! isset($community)) {
  1713. $community = 'public';
  1714. }
  1715. if (! isset($version)) {
  1716. $version = '2';
  1717. }
  1718. // if (!is_up($ip)) { return; }
  1719. global $ifmib_ifindex;
  1720. $status = '';
  1721. // eltex
  1722. if ($vendor_id == 2) {
  1723. global $eltex_sfp_status;
  1724. global $eltex_sfp_vendor;
  1725. global $eltex_sfp_sn;
  1726. global $eltex_sfp_freq;
  1727. global $eltex_sfp_length;
  1728. list ($pattern, $sfp_vendor) = explode(':', get_snmp($ip, $community, $version, $eltex_sfp_vendor . "." . $port));
  1729. if (isset($sfp_vendor)) {
  1730. $sfp_status_temp = $eltex_sfp_status . "." . $port . ".5";
  1731. $sfp_status_volt = $eltex_sfp_status . "." . $port . ".6";
  1732. $sfp_status_circut = $eltex_sfp_status . "." . $port . ".7";
  1733. $sfp_status_tx = $eltex_sfp_status . "." . $port . ".8";
  1734. $sfp_status_rx = $eltex_sfp_status . "." . $port . ".9";
  1735. list ($pattern, $temp) = explode(':', get_snmp($ip, $community, $version, $sfp_status_temp));
  1736. list ($pattern, $volt) = explode(':', get_snmp($ip, $community, $version, $sfp_status_volt));
  1737. list ($pattern, $circut) = explode(':', get_snmp($ip, $community, $version, $sfp_status_circut));
  1738. list ($pattern, $tx) = explode(':', get_snmp($ip, $community, $version, $sfp_status_tx));
  1739. list ($pattern, $rx) = explode(':', get_snmp($ip, $community, $version, $sfp_status_rx));
  1740. list ($pattern, $sfp_sn) = explode(':', get_snmp($ip, $community, $version, $eltex_sfp_sn . "." . $port));
  1741. list ($pattern, $sfp_freq) = explode(':', get_snmp($ip, $community, $version, $eltex_sfp_freq . "." . $port));
  1742. if (! isset($sfp_freq) or $sfp_freq == 65535) {
  1743. $sfp_freq = 'unspecified';
  1744. }
  1745. list ($pattern, $sfp_length) = explode(':', get_snmp($ip, $community, $version, $eltex_sfp_length . "." . $port));
  1746. $status = 'Vendor: ' . $sfp_vendor . ' Serial: ' . $sfp_sn . ' Laser: ' . $sfp_freq . ' Distance: ' . $sfp_length . '<br>';
  1747. if (isset($sfp_status_temp) and $temp > 0.1) {
  1748. $status .= 'Temp: ' . $temp . " C";
  1749. }
  1750. if (isset($sfp_status_volt) and $volt > 0.1) {
  1751. $status .= ' Volt: ' . round($volt / 1000000, 2) . ' V';
  1752. }
  1753. if (isset($sfp_status_circut) and $circut > 0.1) {
  1754. $status .= ' Circut: ' . round($circut / 1000, 2) . ' mA';
  1755. }
  1756. if (isset($sfp_status_tx) and $tx > 0.1) {
  1757. $status .= ' Tx: ' . round($tx / 1000, 2) . ' dBm';
  1758. }
  1759. if (isset($sfp_status_rx) and $rx > 0.1) {
  1760. $status .= ' Rx: ' . round($rx / 1000, 2) . ' dBm';
  1761. }
  1762. $status .= '<br>';
  1763. return $status;
  1764. }
  1765. return;
  1766. }
  1767. // cisco
  1768. if ($vendor_id == 16) {
  1769. global $cisco_descr;
  1770. global $cisco_modules;
  1771. // get interface names
  1772. list ($pattern, $port_name) = explode(':', get_snmp($ip, $community, $version, $ifmib_ifindex . "." . $port));
  1773. // search module indexes
  1774. $port_name = preg_quote(trim($port_name), '/');
  1775. foreach ($modules_oids as $key => $value) {
  1776. $pattern = '/(' . $port_name . ' Module Temperature Sensor)/i';
  1777. preg_match($pattern, $value, $matches);
  1778. if (isset($matches[0])) {
  1779. $temp = get_cisco_sensors($ip, $community, $version, $key);
  1780. continue;
  1781. }
  1782. $pattern = '/(' . $port_name . ' Supply Voltage Sensor)/i';
  1783. preg_match($pattern, $value, $matches);
  1784. if (isset($matches[0])) {
  1785. $volt = get_cisco_sensors($ip, $community, $version, $key);
  1786. continue;
  1787. }
  1788. $pattern = '/(' . $port_name . ' Bias Current Sensor)/i';
  1789. preg_match($pattern, $value, $matches);
  1790. if (isset($matches[0])) {
  1791. $circut = get_cisco_sensors($ip, $community, $version, $key);
  1792. continue;
  1793. }
  1794. $pattern = '/(' . $port_name . ' Transmit Power Sensor)/i';
  1795. preg_match($pattern, $value, $matches);
  1796. if (isset($matches[0])) {
  1797. $tx = get_cisco_sensors($ip, $community, $version, $key);
  1798. continue;
  1799. }
  1800. $pattern = '/(' . $port_name . ' Receive Power Sensor)/i';
  1801. preg_match($pattern, $value, $matches);
  1802. if (isset($matches[0])) {
  1803. $rx = get_cisco_sensors($ip, $community, $version, $key);
  1804. continue;
  1805. }
  1806. }
  1807. if (isset($temp) and $temp > 0) {
  1808. $status .= 'Temp: ' . $temp . " C";
  1809. }
  1810. if (isset($volt) and $volt > 0) {
  1811. $status .= ' Volt: ' . $volt . ' V';
  1812. }
  1813. if (isset($circut) and $circut > 0) {
  1814. $status .= ' Circut: ' . $circut . ' mA';
  1815. }
  1816. if (isset($tx) and abs($tx)>0.1) {
  1817. $status .= ' Tx: ' . $tx . ' dBm';
  1818. }
  1819. if (isset($rx) and abs($rx)>0.1) {
  1820. $status .= ' Rx: ' . $rx . ' dBm';
  1821. }
  1822. if (isset($status)) {
  1823. $status = preg_replace('/"/', '', $status);
  1824. $status .= '<br>';
  1825. }
  1826. return $status;
  1827. }
  1828. // huawei
  1829. if ($vendor_id == 3) {
  1830. global $huawei_sfp_vendor;
  1831. global $huawei_sfp_speed;
  1832. global $huawei_sfp_db;
  1833. global $huawei_sfp_volt;
  1834. global $huawei_sfp_optrx;
  1835. global $huawei_sfp_opttx;
  1836. global $huawei_sfp_rx;
  1837. global $huawei_sfp_tx;
  1838. global $huawei_sfp_biascurrent;
  1839. // get interface names
  1840. list ($pattern, $port_name) = explode(':', get_snmp($ip, $community, $version, $ifmib_ifindex . "." . $port));
  1841. // search module indexes
  1842. $port_name = preg_quote(trim($port_name), '/');
  1843. foreach ($modules_oids as $key => $value) {
  1844. $pattern = '/' . $port_name . '/i';
  1845. preg_match($pattern, $value, $matches);
  1846. if (isset($matches[0])) {
  1847. $module_id = get_last_digit($key);
  1848. unset($result);
  1849. list ($pattern, $result) = explode(':', get_snmp($ip, $community, $version, $huawei_sfp_vendor . "." . $module_id));
  1850. if (isset($result)) {
  1851. $sfp_vendor = $result;
  1852. }
  1853. unset($result);
  1854. list ($pattern, $result) = explode(':', get_snmp($ip, $community, $version, $huawei_sfp_speed . "." . $module_id));
  1855. if (isset($result)) {
  1856. list ($sfp_speed, $spf_lenght, $sfp_type) = explode('-', $result);
  1857. if ($sfp_type == 0) {
  1858. $sfp_type = 'MultiMode';
  1859. }
  1860. if ($sfp_type == 1) {
  1861. $sfp_type = 'SingleMode';
  1862. }
  1863. }
  1864. list ($pattern, $volt) = explode(':', get_snmp($ip, $community, $version, $huawei_sfp_volt . "." . $module_id));
  1865. list ($pattern, $circut) = explode(':', get_snmp($ip, $community, $version, $huawei_sfp_biascurrent . "." . $module_id));
  1866. list ($pattern, $tx) = explode(':', get_snmp($ip, $community, $version, $huawei_sfp_opttx . "." . $module_id));
  1867. list ($pattern, $rx) = explode(':', get_snmp($ip, $community, $version, $huawei_sfp_optrx . "." . $module_id));
  1868. if (!isset($tx)) {
  1869. list ($pattern, $tx) = explode(':', get_snmp($ip, $community, $version, $huawei_sfp_tx . "." . $module_id));
  1870. }
  1871. if (!isset($rx)) {
  1872. list ($pattern, $rx) = explode(':', get_snmp($ip, $community, $version, $huawei_sfp_rx . "." . $module_id));
  1873. }
  1874. if (isset($sfp_vendor)) { $status .= ' Name:' . $sfp_vendor.'<br>'; }
  1875. // if (isset($sfp_speed)) { $status .= ' ' . $sfp_speed; }
  1876. // if (isset($spf_lenght)) { $status .= ' ' . $spf_lenght; }
  1877. if ($volt > 0) { $status .= ' Volt: ' . round($volt / 1000, 2) . ' V'; }
  1878. if (isset($circut)) { $status .= ' Circut: ' . $circut . ' mA <br>'; }
  1879. if (isset($tx)) {
  1880. $tx = preg_replace('/"/', '', $tx);
  1881. list($tx_dbm,$pattern) = explode('.', $tx);
  1882. $tx_dbm=round(trim($tx_dbm) / 100,2);
  1883. if (abs($tx_dbm)>0.1) { $status .= ' Tx: '.$tx_dbm.' dBm'; }
  1884. }
  1885. if (isset($rx)) {
  1886. $rx = preg_replace('/"/', '', $rx);
  1887. list($rx_dbm,$pattern) = explode('.', $rx);
  1888. $rx_dbm=round(trim($rx_dbm) / 100,2);
  1889. if (abs($rx_dbm)>0.1) { $status .= ' Rx: '.$rx_dbm.' dBm'; }
  1890. }
  1891. break;
  1892. }
  1893. }
  1894. if (isset($status)) {
  1895. $status = preg_replace('/"/', '', $status);
  1896. $status .= '<br>';
  1897. }
  1898. return $status;
  1899. }
  1900. return;
  1901. }
  1902. function get_port_vlan($port, $ip, $community, $version)
  1903. {
  1904. if (! isset($port)) {
  1905. return;
  1906. }
  1907. if (! isset($ip)) {
  1908. return;
  1909. }
  1910. if (! isset($community)) {
  1911. $community = 'public';
  1912. }
  1913. if (! isset($version)) {
  1914. $version = '2';
  1915. }
  1916. // if (!is_up($ip)) { return; }
  1917. global $port_vlan_oid;
  1918. $port_oid = $port_vlan_oid . $port;
  1919. $port_vlan = get_snmp($ip, $community, $version, $port_oid);
  1920. $port_vlan = intval(trim(str_replace('Gauge32:', '', $port_vlan)));
  1921. return $port_vlan;
  1922. }
  1923. function get_port_poe_state($vendor_id, $port, $ip, $community, $version)
  1924. {
  1925. // port = snmp_index!!!!
  1926. if (! isset($port)) {
  1927. return;
  1928. }
  1929. if (! isset($ip)) {
  1930. return;
  1931. }
  1932. if (! isset($community)) {
  1933. $community = 'public';
  1934. }
  1935. if (! isset($version)) {
  1936. $version = '2';
  1937. }
  1938. // if (!is_up($ip)) { return; }
  1939. // default poe oid
  1940. global $pethPsePortAdminEnable;
  1941. $poe_status = $pethPsePortAdminEnable . "." . $port;
  1942. if ($vendor_id == 3) {
  1943. global $huawei_poe_oid;
  1944. $poe_status = $huawei_poe_oid . "." . $port;
  1945. }
  1946. if ($vendor_id == 8) {
  1947. global $allied_poe_oid;
  1948. $poe_status = $allied_poe_oid . "." . $port;
  1949. }
  1950. if ($vendor_id == 15) {
  1951. global $hp_poe_oid;
  1952. $poe_status = $hp_poe_oid . "." . $port;
  1953. }
  1954. if ($vendor_id == 9) {
  1955. global $mikrotik_poe_oid;
  1956. $poe_status = $mikrotik_poe_oid . "." . $port;
  1957. }
  1958. if ($vendor_id == 10) {
  1959. global $netgear_poe_oid;
  1960. $poe_status = $netgear_poe_oid . "." . $port;
  1961. }
  1962. $result = '';
  1963. $c_state = get_snmp($ip, $community, $version, $poe_status);
  1964. if (isset($c_state)) {
  1965. list ($pattern, $p_state) = explode(':', $c_state);
  1966. if ($vendor_id == 9) {
  1967. if ($p_state == 1) {
  1968. return 2;
  1969. }
  1970. if ($p_state > 1) {
  1971. return 1;
  1972. }
  1973. }
  1974. return $p_state;
  1975. }
  1976. return;
  1977. }
  1978. function set_port_poe_state($vendor_id, $port, $ip, $community, $version, $state)
  1979. {
  1980. // port -> snmp_index!!!
  1981. if (! isset($port)) {
  1982. return;
  1983. }
  1984. if (! isset($ip)) {
  1985. return;
  1986. }
  1987. if (! isset($community)) {
  1988. $community = 'public';
  1989. }
  1990. if (! isset($version)) {
  1991. $version = '2';
  1992. }
  1993. // if (!is_up($ip)) { return; }
  1994. // default poe oid
  1995. global $pethPsePortAdminEnable;
  1996. $poe_status = $pethPsePortAdminEnable . "." . $port;
  1997. if ($vendor_id == 3) {
  1998. global $huawei_poe_oid;
  1999. $poe_status = $huawei_poe_oid . "." . $port;
  2000. }
  2001. if ($vendor_id == 8) {
  2002. global $allied_poe_oid;
  2003. $poe_status = $allied_poe_oid . "." . $port;
  2004. }
  2005. if ($vendor_id == 15) {
  2006. global $hp_poe_oid;
  2007. $poe_status = $hp_poe_oid . "." . $port;
  2008. }
  2009. if ($vendor_id == 10) {
  2010. global $netgear_poe_oid;
  2011. $poe_status = $netgear_poe_oid . "." . $port;
  2012. }
  2013. if ($state) {
  2014. // enable port
  2015. $c_state = set_snmp($ip, $community, $version, $poe_status, 'i', 1);
  2016. return $c_state;
  2017. } else {
  2018. // disable port
  2019. $c_state = set_snmp($ip, $community, $version, $poe_status, 'i', 2);
  2020. return $c_state;
  2021. }
  2022. }
  2023. function get_port_poe_detail($vendor_id, $port, $ip, $community, $version)
  2024. {
  2025. // port = snmp_index!!!!
  2026. if (! isset($port)) {
  2027. return;
  2028. }
  2029. if (! isset($ip)) {
  2030. return;
  2031. }
  2032. if (! isset($community)) {
  2033. $community = 'public';
  2034. }
  2035. if (! isset($version)) {
  2036. $version = '2';
  2037. }
  2038. // if (!is_up($ip)) { return; }
  2039. $result = '';
  2040. $poe_class = '.1.3.6.1.2.1.105.1.1.1.10.1.' . $port;
  2041. // eltex
  2042. if ($vendor_id == 2) {
  2043. $poe_power = '.1.3.6.1.4.1.89.108.1.1.5.1.' . $port;
  2044. $poe_current = '.1.3.6.1.4.1.89.108.1.1.4.1.' . $port;
  2045. $poe_volt = '.1.3.6.1.4.1.89.108.1.1.3.1.' . $port;
  2046. }
  2047. // huawei
  2048. if ($vendor_id == 3) {
  2049. $poe_power = '.1.3.6.1.4.1.2011.5.25.195.3.1.10.' . $port;
  2050. $poe_current = '.1.3.6.1.4.1.4526.11.15.1.1.1.3.1.' . $port;
  2051. $poe_volt = '.1.3.6.1.4.1.2011.5.25.195.3.1.14.' . $port;
  2052. }
  2053. // AT
  2054. if ($vendor_id == 8) {
  2055. $poe_power = '.1.3.6.1.4.1.89.108.1.1.5.1.' . $port;
  2056. $poe_current = '.1.3.6.1.4.1.89.108.1.1.4.1.' . $port;
  2057. $poe_volt = '.1.3.6.1.4.1.89.108.1.1.3.1.' . $port;
  2058. }
  2059. // mikrotik
  2060. if ($vendor_id == 9) {
  2061. global $mikrotik_poe_volt;
  2062. global $mikrotik_poe_current;
  2063. global $mikrotik_poe_usage;
  2064. $poe_power = $mikrotik_poe_usage . '.' . $port;
  2065. $poe_current = $mikrotik_poe_current . '.' . $port;
  2066. $poe_volt = $mikrotik_poe_volt . '.' . $port;
  2067. }
  2068. // netgear
  2069. if ($vendor_id == 10) {
  2070. $poe_power = '.1.3.6.1.4.1.4526.11.15.1.1.1.2.1.' . $port;
  2071. $poe_current = '.1.3.6.1.4.1.4526.11.15.1.1.1.3.1.' . $port;
  2072. $poe_volt = '.1.3.6.1.4.1.4526.11.15.1.1.1.4.1.' . $port;
  2073. }
  2074. // HP
  2075. if ($vendor_id == 15) {
  2076. $poe_power = '.1.3.6.1.4.1.25506.2.14.1.1.4.1.' . $port;
  2077. $poe_volt = '.1.3.6.1.4.1.25506.2.14.1.1.3.1.' . $port;
  2078. }
  2079. if (isset($poe_power)) {
  2080. $c_power = get_snmp($ip, $community, $version, $poe_power);
  2081. if (isset($c_power)) {
  2082. $p_power = snmp_value($c_power);
  2083. if ($vendor_id == 9) {
  2084. $p_power = round($p_power / 10, 2);
  2085. } else {
  2086. $p_power = round($p_power / 1000, 2);
  2087. }
  2088. if ($p_power > 0) {
  2089. $result .= ' P: ' . $p_power . ' W';
  2090. }
  2091. }
  2092. }
  2093. if (isset($poe_current)) {
  2094. $c_current = get_snmp($ip, $community, $version, $poe_current);
  2095. if (isset($c_current)) {
  2096. $p_current = snmp_value($c_current);
  2097. if ($p_current > 0) {
  2098. $result .= ' C: ' . $p_current . ' mA';
  2099. }
  2100. }
  2101. }
  2102. if (isset($poe_volt)) {
  2103. $c_volt = get_snmp($ip, $community, $version, $poe_volt);
  2104. if (isset($c_volt)) {
  2105. $p_volt = snmp_value($c_volt);
  2106. if ($vendor_id == 2 or $vendor_id == 8) {
  2107. $p_volt = round($p_volt / 1000, 2);
  2108. }
  2109. if ($vendor_id == 9) {
  2110. $p_volt = round($p_volt / 10, 2);
  2111. }
  2112. if ($vendor_id == 15) {
  2113. $p_volt = round($p_volt / 100, 2);
  2114. }
  2115. if ($p_volt > 0 and $p_power > 0) {
  2116. $result .= ' V: ' . $p_volt . " V";
  2117. }
  2118. }
  2119. }
  2120. if (isset($poe_class)) {
  2121. $c_class = get_snmp($ip, $community, $version, $poe_class);
  2122. if (isset($c_class)) {
  2123. $p_class = snmp_value($c_class);
  2124. if ($p_class > 0 and $p_power > 0) {
  2125. $result .= ' Class: ' . ($p_class - 1);
  2126. }
  2127. }
  2128. }
  2129. return $result;
  2130. }
  2131. function get_snmp($ip, $community, $version, $oid)
  2132. {
  2133. if ($version == 2) {
  2134. $result = snmp2_get($ip, $community, $oid);
  2135. }
  2136. if ($version == 1) {
  2137. $result = snmpget($ip, $community, $oid);
  2138. }
  2139. return $result;
  2140. }
  2141. function set_snmp($ip, $community, $version, $oid, $field, $value)
  2142. {
  2143. if ($version == 2) {
  2144. $result = snmp2_set($ip, $community, $oid, $field, $value);
  2145. }
  2146. if ($version == 1) {
  2147. $result = snmpset($ip, $community, $oid, $field, $value);
  2148. }
  2149. return $result;
  2150. }
  2151. function snmp_value($value)
  2152. {
  2153. if (preg_match('/:/', $value)) {
  2154. list ($pattern, $result) = explode(':', $value);
  2155. $result = str_replace('"', '', $result);
  2156. } else {
  2157. $result = $value;
  2158. }
  2159. return trim($result);
  2160. }
  2161. function set_port_state($vendor_id, $port, $ip, $community, $version, $state)
  2162. {
  2163. // port -> snmp_index!!!
  2164. if (! isset($port)) {
  2165. return;
  2166. }
  2167. if (! isset($ip)) {
  2168. return;
  2169. }
  2170. if (! isset($community)) {
  2171. $community = 'public';
  2172. }
  2173. if (! isset($version)) {
  2174. $version = '2';
  2175. }
  2176. global $port_admin_status_oid;
  2177. $port_status = $port_admin_status_oid . $port;
  2178. if ($state) {
  2179. // enable port
  2180. $c_state = set_snmp($ip, $community, $version, $port_status, 'i', 1);
  2181. return $c_state;
  2182. } else {
  2183. // disable port
  2184. $c_state = set_snmp($ip, $community, $version, $port_status, 'i', 2);
  2185. return $c_state;
  2186. }
  2187. }
  2188. function set_port_for_group($db, $group_id, $place_id, $state)
  2189. {
  2190. $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;
  2191. $auth_list = mysqli_query($db, $authSQL);
  2192. LOG_INFO($db, 'Mass port state change started!');
  2193. // get auth list for group
  2194. while (list ($a_id, $a_name, $a_ip) = mysqli_fetch_array($auth_list)) {
  2195. // get device and port for auth
  2196. if ($place_id == 0) {
  2197. $place_filter = '';
  2198. } else {
  2199. $place_filter = 'D.building_id=' . $place_id . ' and ';
  2200. }
  2201. $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';
  2202. $dev_info = mysqli_query($db, $devSQL);
  2203. 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);
  2204. if (! isset($d_id)) {
  2205. continue;
  2206. }
  2207. if ($state) {
  2208. $mode = 'enable';
  2209. run_sql($db, "Update User_auth set nagios_handler='restart-port' WHERE id=$a_id and nagios_handler='manual-mode'");
  2210. } else {
  2211. $mode = 'disable';
  2212. run_sql($db, "Update User_auth set nagios_handler='manual-mode' WHERE id=$a_id and nagios_handler='restart-port'");
  2213. }
  2214. LOG_INFO($db, "At device $d_name [$d_ip] $mode port $d_port for auth_id: $a_id ($a_ip [$a_name])");
  2215. set_port_state($d_vendor_id, $d_snmp_index, $d_ip, $d_community, $d_snmp, $state);
  2216. set_port_poe_state($d_vendor_id, $d_snmp_index, $d_ip, $d_community, $d_snmp, $state);
  2217. }
  2218. LOG_INFO($db, 'Mass port state change stopped.');
  2219. }
  2220. function get_vendor($db,$mac)
  2221. {
  2222. $mac = mac_simplify($mac);
  2223. $vendor = mysqli_query($db,'select companyName,companyAddress FROM mac_vendors WHERE oui="'.substr($mac,0,6).'" or oui="'.substr($mac,0,8).'"');
  2224. list ($f_name,$f_addr) = mysqli_fetch_array($vendor);
  2225. $result = $f_name." ".$f_addr;
  2226. return $result;
  2227. }
  2228. function get_port_state_detail($port, $ip, $community, $version)
  2229. {
  2230. if (! isset($port)) {
  2231. return;
  2232. }
  2233. if (! isset($ip)) {
  2234. return;
  2235. }
  2236. if (! isset($community)) {
  2237. $community = 'public';
  2238. }
  2239. if (! isset($version)) {
  2240. $version = '2';
  2241. }
  2242. // if (!is_up($ip)) { return; }
  2243. global $port_status_oid;
  2244. global $port_admin_status_oid;
  2245. global $port_speed_oid;
  2246. global $port_errors_oid;
  2247. $oper = $port_status_oid . $port;
  2248. $admin = $port_admin_status_oid . $port;
  2249. $speed = $port_speed_oid . $port;
  2250. $errors = $port_errors_oid . $port;
  2251. $result = '';
  2252. $c_state = get_snmp($ip, $community, $version, $oper);
  2253. list ($pattern, $p_state) = explode(':', $c_state);
  2254. $c_admin = get_snmp($ip, $community, $version, $admin);
  2255. list ($pattern, $p_admin) = explode(':', $c_admin);
  2256. if (preg_match('/up/i', $p_state)) {
  2257. $c_speed = get_snmp($ip, $community, $version, $speed);
  2258. } else {
  2259. $c_speed = 'INT:0';
  2260. }
  2261. list ($pattern, $p_speed) = explode(':', $c_speed);
  2262. $c_errors = get_snmp($ip, $community, $version, $errors);
  2263. list ($pattern, $p_errors) = explode(':', $c_errors);
  2264. $result = $p_state . ";" . $p_admin . ";" . trim($p_speed) . ";" . trim($p_errors);
  2265. return $result;
  2266. }
  2267. function dec_to_hex($mac)
  2268. {
  2269. if (! isset($mac)) {
  2270. return;
  2271. }
  2272. $mac_array = explode('.', $mac);
  2273. for ($i = 0; $i < count($mac_array); $i ++) {
  2274. $hex_i = dechex($mac_array[$i]);
  2275. if (strlen($hex_i) == 1) {
  2276. $hex_i = "0" . $hex_i;
  2277. }
  2278. $mac_array[$i] = $hex_i;
  2279. }
  2280. $hex_mac = implode(':', $mac_array);
  2281. return $hex_mac;
  2282. }
  2283. function mac_simplify($mac)
  2284. {
  2285. if (! isset($mac)) {
  2286. return;
  2287. }
  2288. $mac = strtolower(trim($mac));
  2289. $mac = preg_replace('/(\.|:|-)/', '', $mac);
  2290. return $mac;
  2291. }
  2292. function mac_dotted($mac)
  2293. {
  2294. if (! isset($mac)) {
  2295. return;
  2296. }
  2297. $mac = mac_simplify($mac);
  2298. $mac = preg_replace('/(\S{2})(\S{2})(\S{2})(\S{2})(\S{2})(\S{2})/', '$1:$2:$3:$4:$5:$6', $mac);
  2299. return $mac;
  2300. }
  2301. function unbind_ports($db, $device_id)
  2302. {
  2303. $target = mysqli_query($db, "SELECT U.target_port_id,U.id FROM device_ports U WHERE U.device_id=$device_id");
  2304. while (list ($target_id, $id) = mysqli_fetch_array($target)) {
  2305. $new['target_port_id'] = 0;
  2306. update_record($db, "device_ports", "target_port_id='$id'", $new);
  2307. update_record($db, "device_ports", "id='$id'", $new);
  2308. }
  2309. }
  2310. function bind_ports($db, $port_id, $target_id)
  2311. {
  2312. $old_target = mysqli_query($db, "SELECT U.target_port_id FROM device_ports U WHERE U.id=$port_id");
  2313. list ($old_target_id) = mysqli_fetch_array($old_target);
  2314. // unbind current connection
  2315. $new['target_port_id'] = 0;
  2316. update_record($db, "device_ports", "id='$port_id'", $new);
  2317. if (isset($old_target_id)) {
  2318. update_record($db, "device_ports", "id='$old_target_id'", $new);
  2319. }
  2320. // new link
  2321. if (isset($target_id) and $target_id > 0) {
  2322. $new['target_port_id'] = $target_id;
  2323. update_record($db, "device_ports", "id='$port_id'", $new);
  2324. $new['target_port_id'] = $port_id;
  2325. update_record($db, "device_ports", "id='$target_id'", $new);
  2326. }
  2327. }
  2328. function expand_device_name($db, $name)
  2329. {
  2330. $device_id = get_device_id($db, $name);
  2331. $result = $name;
  2332. if (isset($device_id) and $device_id > 0) {
  2333. $result = '<a href=/admin/devices/editdevice.php?id=' . $device_id . '>' . $name . '</a>';
  2334. }
  2335. return $result;
  2336. }
  2337. function expand_mac($db,$msg)
  2338. {
  2339. if (! isset($msg)) { return; }
  2340. $mac = mac_dotted($msg);
  2341. $vendor_info = get_vendor($db,$mac);
  2342. $result = ' <p title="'.$vendor_info.'"><a href=/admin/logs/mac.php?mac='.$mac.'>'.$mac.'</a></p>';
  2343. return $result;
  2344. }
  2345. function expand_log_str($db, $msg)
  2346. {
  2347. if (! isset($msg)) {
  2348. return;
  2349. }
  2350. $auth_pattern = '/(auth_id:|auth|auth id:|auth id)\s+(\d+)\s+/i';
  2351. $auth_replace = '<a href=/admin/users/editauth.php?id=${2}>auth_id:${2}</a> ';
  2352. $result = preg_replace($auth_pattern, $auth_replace, $msg);
  2353. $user_pattern = '/(user_id:|user|user id:|user id)\s+(\d+)\s+/i';
  2354. $user_replace = '<a href=/admin/users/edituser.php?id=${2}>user_id:${2}</a>';
  2355. $result = preg_replace($user_pattern, $user_replace, $result);
  2356. $mac_pattern = '/\s+\[(\w{12})\]\s+/i';
  2357. preg_match($mac_pattern, $result, $matches);
  2358. if (isset($matches[1])) {
  2359. $mac = $matches[1];
  2360. $mac = mac_dotted($mac);
  2361. # $vendor_info = get_vendor($db,$mac);
  2362. # $mac_replace = ' <p title="'.$vendor_info.'"><a href=/admin/logs/mac.php?mac='.$mac.'>'.$mac.'</a></p>';
  2363. $mac_replace = ' <a href=/admin/logs/mac.php?mac='.$mac.'>'.$mac.'</a> ';
  2364. $result = preg_replace($mac_pattern, $mac_replace, $result);
  2365. }
  2366. $mac_pattern = '/\s+mac:\s+([\w\:]{17})$/i';
  2367. preg_match($mac_pattern, $result, $matches);
  2368. if (isset($matches[1])) {
  2369. $mac = $matches[1];
  2370. $mac = mac_dotted($mac);
  2371. # $vendor_info = get_vendor($db,$mac);
  2372. # $mac_replace = ' mac: <p title="'.$vendor_info.'"><a href=/admin/logs/mac.php?mac='.$mac.'>'.$mac.'</a></p>';
  2373. $mac_replace = ' mac: <a href=/admin/logs/mac.php?mac='.$mac.'>'.$mac.'</a> ';
  2374. $result = preg_replace($mac_pattern, $mac_replace, $result);
  2375. }
  2376. $device_pattern = '/at device\s+([\w\.\-]+)/i';
  2377. preg_match($device_pattern, $result, $matches);
  2378. if (isset($matches[1])) {
  2379. $device_name = $matches[1];
  2380. $device_id = get_device_id($db, $device_name);
  2381. if (isset($device_id) and $device_id > 0) {
  2382. $device_replace = 'at device <a href=/admin/devices/editdevice.php?id=' . $device_id . '>${1}</a>';
  2383. $result = preg_replace($device_pattern, $device_replace, $result);
  2384. }
  2385. }
  2386. $device_pattern = '/(device_id:|device id:|device id|device_id)\s+(\d+)\s+/i';
  2387. $device_replace = 'device_id: <a href=/admin/devices/editdevice.php?id=${2}>${2}</a> ';
  2388. $result = preg_replace($device_pattern, $device_replace, $result);
  2389. $ip_pattern = '/\s+ip\:\s+(\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})\s+/i';
  2390. preg_match($ip_pattern, $result, $matches);
  2391. if (isset($matches[1])) {
  2392. $ip = $matches[1];
  2393. $auth_id = get_auth_by_ip($db, $ip);
  2394. if (isset($auth_id) and $auth_id > 0) {
  2395. $auth_replace = ' ip: <a href=/admin/users/editauth.php?id=' . $auth_id . '>${1}</a> ';
  2396. $result = preg_replace($ip_pattern, $auth_replace, $result);
  2397. }
  2398. }
  2399. return $result;
  2400. }
  2401. function get_record_field($db, $table, $field, $filter)
  2402. {
  2403. if (! isset($table)) {
  2404. LOG_ERROR($db, "Search in unknown table! Skip command.");
  2405. return;
  2406. }
  2407. if (! isset($filter)) {
  2408. LOG_ERROR($db, "Search filter is empty! Skip command.");
  2409. return;
  2410. }
  2411. if (! isset($field)) {
  2412. LOG_ERROR($db, "Search field is empty! Skip command.");
  2413. return;
  2414. }
  2415. if (preg_match('/=$/', $filter)) {
  2416. LOG_ERROR($db, "Search record ($table) with illegal filter $filter! Skip command.");
  2417. return;
  2418. }
  2419. $old_sql = "SELECT $field FROM $table WHERE $filter LIMIT 1";
  2420. $old_record = mysqli_query($db, $old_sql);
  2421. $old = mysqli_fetch_array($old_record, MYSQLI_ASSOC);
  2422. foreach ($old as $key => $value) {
  2423. if (! isset($value) or $value==='NULL') { $value = ''; }
  2424. $result[$key] = $value;
  2425. }
  2426. return $result[$field];
  2427. }
  2428. function get_record($db, $table, $filter)
  2429. {
  2430. if (! isset($table)) {
  2431. LOG_ERROR($db, "Search in unknown table! Skip command.");
  2432. return;
  2433. }
  2434. if (! isset($filter)) {
  2435. LOG_ERROR($db, "Search filter is empty! Skip command.");
  2436. return;
  2437. }
  2438. if (preg_match('/=$/', $filter)) {
  2439. LOG_ERROR($db, "Search record ($table) with illegal filter $filter! Skip command.");
  2440. return;
  2441. }
  2442. $old_sql = "SELECT * FROM $table WHERE $filter LIMIT 1";
  2443. $old_record = mysqli_query($db, $old_sql);
  2444. $old = mysqli_fetch_array($old_record, MYSQLI_ASSOC);
  2445. $result = NULL;
  2446. if (!empty($old)) {
  2447. foreach ($old as $key => $value) {
  2448. if (!isset($value) or $value==='NULL') { $value = ''; }
  2449. if (!empty($key)) { $result[$key] = $value; }
  2450. }
  2451. }
  2452. return $result;
  2453. }
  2454. function get_records($db, $table, $filter)
  2455. {
  2456. if (! isset($table)) {
  2457. LOG_ERROR($db, "Search in unknown table! Skip command.");
  2458. return;
  2459. }
  2460. if (isset($filter) and preg_match('/=$/', $filter)) {
  2461. LOG_ERROR($db, "Search record ($table) with illegal filter $filter! Skip command.");
  2462. return;
  2463. }
  2464. $s_filter='';
  2465. if (isset($filter)) { $s_filter = 'WHERE '.$filter; }
  2466. $old_sql = "SELECT * FROM $table $s_filter";
  2467. $old_record = mysqli_query($db, $old_sql);
  2468. $result = NULL;
  2469. $index = 0;
  2470. while ($old = mysqli_fetch_array($old_record, MYSQLI_ASSOC)) {
  2471. foreach ($old as $key => $value) {
  2472. if (! isset($value) or $value==='NULL') { $value = ''; }
  2473. $result[$index][$key] = $value;
  2474. }
  2475. $index++;
  2476. }
  2477. return $result;
  2478. }
  2479. function get_records_sql($db, $sql)
  2480. {
  2481. if (! isset($sql)) {
  2482. LOG_ERROR($db, "Empty query! Skip command.");
  2483. return;
  2484. }
  2485. $record = mysqli_query($db, $sql);
  2486. $index = 0;
  2487. $result = NULL;
  2488. while ($rec = mysqli_fetch_array($record, MYSQLI_ASSOC)) {
  2489. foreach ($rec as $key => $value) {
  2490. if (! isset($value) or $value==='NULL') { $value = ''; }
  2491. if (!empty($key)) { $result[$index][$key] = $value; }
  2492. }
  2493. $index++;
  2494. }
  2495. return $result;
  2496. }
  2497. function get_record_sql($db, $sql)
  2498. {
  2499. if (! isset($sql)) {
  2500. LOG_ERROR($db, "Empty query! Skip command.");
  2501. return;
  2502. }
  2503. $record = mysqli_query($db, $sql." LIMIT 1");
  2504. if ($rec = mysqli_fetch_array($record, MYSQLI_ASSOC)) {
  2505. foreach ($rec as $key => $value) {
  2506. if (! isset($value) or $value==='NULL') { $value = ''; }
  2507. $result[$key] = $value;
  2508. }
  2509. }
  2510. return $result;
  2511. }
  2512. function update_record($db, $table, $filter, $newvalue)
  2513. {
  2514. if (isRO($db)) {
  2515. LOG_ERROR($db, "User does not have write permission");
  2516. return;
  2517. }
  2518. if (! isset($table)) {
  2519. LOG_ERROR($db, "Change record for unknown table! Skip command.");
  2520. return;
  2521. }
  2522. if (! isset($filter)) {
  2523. LOG_ERROR($db, "Change record ($table) with empty filter! Skip command.");
  2524. return;
  2525. }
  2526. if (preg_match('/=$/', $filter)) {
  2527. LOG_ERROR($db, "Change record ($table) with illegal filter $filter! Skip command.");
  2528. return;
  2529. }
  2530. if (! isset($newvalue)) {
  2531. LOG_ERROR($db, "Change record ($table [ $filter ]) with empty data! Skip command.");
  2532. return;
  2533. }
  2534. $old_sql = "SELECT * FROM $table WHERE $filter";
  2535. $old_record = mysqli_query($db, $old_sql);
  2536. $old = mysqli_fetch_array($old_record, MYSQLI_ASSOC);
  2537. $changed_log = '';
  2538. $run_sql = '';
  2539. $network_changed = 0;
  2540. foreach ($newvalue as $key => $value) {
  2541. if (! isset($value)) {
  2542. $value = '';
  2543. }
  2544. $value = trim($value);
  2545. if (strcmp($old[$key], $value) == 0) {
  2546. continue;
  2547. }
  2548. if ($table==="User_auth") {
  2549. $acl_fields = [
  2550. 'ip' => '1',
  2551. 'ip_int' => '1',
  2552. 'enabled'=>'1',
  2553. 'dhcp'=>'1',
  2554. 'filter_group_id'=>'1',
  2555. 'deleted'=>'1',
  2556. 'dhcp_acl'=>'1',
  2557. 'queue_id'=>'1',
  2558. 'mac'=>'1',
  2559. 'blocked'=>'1',
  2560. ];
  2561. if (!empty($acl_fields["$key"])) { $network_changed = 1; }
  2562. }
  2563. $changed_log = $changed_log . " $key => $value (old: $old[$key]),";
  2564. $run_sql = $run_sql . " `" . $key . "`='" . mysqli_real_escape_string($db, $value) . "',";
  2565. }
  2566. if ($run_sql == '') { return; }
  2567. if ($network_changed) { $run_sql = $run_sql . " `changed`='1',"; }
  2568. $changed_log = substr_replace($changed_log, "", - 1);
  2569. $run_sql = substr_replace($run_sql, "", - 1);
  2570. if ($table === 'User_auth') {
  2571. $changed_time = GetNowTimeString();
  2572. $run_sql = $run_sql . ", `changed_time`='".$changed_time."'";
  2573. }
  2574. $new_sql = "UPDATE $table SET $run_sql WHERE $filter";
  2575. LOG_DEBUG($db, "Run sql: $new_sql");
  2576. mysqli_query($db, $new_sql);
  2577. LOG_VERBOSE($db, "Change table $table WHERE $filter set $changed_log");
  2578. }
  2579. function delete_record($db, $table, $filter)
  2580. {
  2581. if (isRO($db)) {
  2582. LOG_ERROR($db, "User does not have write permission");
  2583. return;
  2584. }
  2585. if (! isset($table)) {
  2586. LOG_ERROR($db, "Delete FROM unknown table! Skip command.");
  2587. return;
  2588. }
  2589. if (! isset($filter)) {
  2590. LOG_ERROR($db, "Delete FROM table $table with empty filter! Skip command.");
  2591. return;
  2592. }
  2593. if (preg_match('/=$/', $filter)) {
  2594. LOG_ERROR($db, "Change record ($table) with illegal filter $filter! Skip command.");
  2595. return;
  2596. }
  2597. $old_sql = "SELECT * FROM $table WHERE $filter";
  2598. $old_record = mysqli_query($db, $old_sql);
  2599. $old = mysqli_fetch_array($old_record, MYSQLI_ASSOC);
  2600. $changed_log = 'record: ';
  2601. foreach ($old as $key => $value) {
  2602. if (! isset($value)) { $value = ''; }
  2603. $changed_log = $changed_log . " $key => $value,";
  2604. }
  2605. //never delete user ip record
  2606. if ($table === 'User_auth') {
  2607. $changed_time = GetNowTimeString();
  2608. $new_sql = "UPDATE $table SET deleted=1, `changed_time`='".$changed_time."' WHERE $filter";
  2609. LOG_DEBUG($db, "Run sql: $new_sql");
  2610. mysqli_query($db, $new_sql);
  2611. } else {
  2612. $new_sql = "DELETE FROM $table WHERE $filter";
  2613. LOG_DEBUG($db, "Run sql: $new_sql");
  2614. mysqli_query($db, $new_sql);
  2615. }
  2616. LOG_VERBOSE($db, "Delete FROM table $table WHERE $filter $changed_log");
  2617. }
  2618. function insert_record($db, $table, $newvalue)
  2619. {
  2620. if (isRO($db)) {
  2621. LOG_ERROR($db, "User does not have write permission");
  2622. return;
  2623. }
  2624. if (! isset($table)) {
  2625. LOG_ERROR($db, "Create record for unknown table! Skip command.");
  2626. return;
  2627. }
  2628. if (! isset($newvalue)) {
  2629. LOG_ERROR($db, "Create record ($table) with empty data! Skip command.");
  2630. return;
  2631. }
  2632. $changed_log = '';
  2633. $field_list = '';
  2634. $value_list = '';
  2635. foreach ($newvalue as $key => $value) {
  2636. if (! isset($value)) { $value = ''; }
  2637. $changed_log = $changed_log . " $key => $value,";
  2638. $field_list = $field_list . "`" . $key . "`,";
  2639. $value = trim($value);
  2640. $value_list = $value_list . "'" . mysqli_real_escape_string($db, $value) . "',";
  2641. }
  2642. if ($value_list == '') {
  2643. return;
  2644. }
  2645. $changed_log = substr_replace($changed_log, "", - 1);
  2646. $field_list = substr_replace($field_list, "", - 1);
  2647. $value_list = substr_replace($value_list, "", - 1);
  2648. $new_sql = "insert into $table(" . $field_list . ") values(" . $value_list . ")";
  2649. LOG_DEBUG($db, "Run sql: $new_sql");
  2650. if (mysqli_query($db, $new_sql)) {
  2651. $last_id = mysqli_insert_id($db);
  2652. LOG_VERBOSE($db, "Create record in table $table: $changed_log with id: $last_id");
  2653. return $last_id;
  2654. }
  2655. }
  2656. function get_diff_rec($db, $table, $filter, $newvalue, $only_changed)
  2657. {
  2658. if (! isset($table)) {
  2659. return;
  2660. }
  2661. if (! isset($filter)) {
  2662. return;
  2663. }
  2664. if (! isset($newvalue)) {
  2665. return;
  2666. }
  2667. if (!isset($only_changed)) { $only_changed=0; }
  2668. $old_sql = "SELECT * FROM $table WHERE $filter";
  2669. $old_record = mysqli_query($db, $old_sql);
  2670. $old = mysqli_fetch_array($old_record, MYSQLI_ASSOC);
  2671. $changed_log = "\r\n";
  2672. foreach ($newvalue as $key => $value) {
  2673. if (strcmp($old[$key], $value) !== 0) {
  2674. $changed_log = $changed_log . " $key => cur: $value old: $old[$key],\r\n";
  2675. }
  2676. }
  2677. $old_record = '';
  2678. if (!$only_changed) {
  2679. $old_record = "\r\n Не изменялось:\r\n";
  2680. foreach ($old as $key => $value) {
  2681. if (!$newvalue[$key]) { $old_record = $old_record . " $key = $value,\r\n"; }
  2682. }
  2683. $old_record = substr_replace($old_record, "", -3);
  2684. }
  2685. // print $changed_log;
  2686. return $changed_log.$old_record;
  2687. }
  2688. function get_cacti_graph($host_ip, $port_index)
  2689. {
  2690. global $cacti_dbname;
  2691. global $cacti_dbhost;
  2692. global $cacti_url;
  2693. global $dbuser;
  2694. global $dbpass;
  2695. if (! isset($cacti_url)) {
  2696. return;
  2697. }
  2698. $cacti_db_link = mysqli_connect($cacti_dbhost, $dbuser, $dbpass, $cacti_dbname);
  2699. if (! $cacti_db_link) {
  2700. echo "Ошибка: Невозможно установить соединение с MySQL with $cacti_dbhost [$cacti_dbname] for $dbuser." . PHP_EOL;
  2701. echo "Код ошибки errno: " . mysqli_connect_errno() . PHP_EOL;
  2702. echo "Текст ошибки error: " . mysqli_connect_error() . PHP_EOL;
  2703. return FALSE;
  2704. }
  2705. $host_sql = 'SELECT id FROM host Where hostname="' . $host_ip . '"';
  2706. $tmpArray = mysqli_fetch_array(mysqli_query($cacti_db_link, $host_sql), MYSQLI_ASSOC);
  2707. if (sizeof($tmpArray)) {
  2708. foreach ($tmpArray as $key => $value) {
  2709. if ($key == 'id') {
  2710. $host_id = $value;
  2711. }
  2712. }
  2713. } else {
  2714. return;
  2715. }
  2716. $graph_sql = 'SELECT id FROM graph_local Where graph_template_id=2 and host_id="' . $host_id . '" and snmp_index="' . $port_index . '"';
  2717. $tmpArray = mysqli_fetch_array(mysqli_query($cacti_db_link, $graph_sql), MYSQLI_ASSOC);
  2718. if (sizeof($tmpArray)) {
  2719. foreach ($tmpArray as $key => $value) {
  2720. if ($key == 'id') {
  2721. $graph_id = $value;
  2722. }
  2723. }
  2724. } else {
  2725. return;
  2726. }
  2727. $result = $cacti_url . "/graph_image.php?local_graph_id=" . $graph_id;
  2728. return $result;
  2729. }
  2730. function print_select_item ($description,$value,$current) {
  2731. if ($value == $current) { print "<option value=$value selected>$description</option>"; } else { print "<option value=$value>$description</option>"; }
  2732. }
  2733. function print_select_item_ext ($description,$value,$current,$disabled) {
  2734. if ($value == $current) { print "<option value=$value selected>$description</option>"; }
  2735. else {
  2736. if (!$disabled) { print "<option value=$value>$description</option>"; } else { print "<option disabled value=$value>$description</option>"; }
  2737. }
  2738. }
  2739. function print_row_at_pages ($name,$value) {
  2740. print "<select name='".$name."'>\n";
  2741. print_select_item('Много',pow(10,10),$value);
  2742. print_select_item('25',25,$value);
  2743. print_select_item('50',50,$value);
  2744. print_select_item('100',100,$value);
  2745. print_select_item('200',200,$value);
  2746. print_select_item('500',500,$value);
  2747. print_select_item('1000',1000,$value);
  2748. print_select_item('2000',2000,$value);
  2749. print "</select>\n";
  2750. }
  2751. function print_navigation($url,$page,$displayed,$count_records,$total) {
  2752. if ($total<=1) { return; }
  2753. #две назад
  2754. print "<br><div align=left>";
  2755. if(($page-2)>0):
  2756. $pagetwoleft="<a class='first_page_link' href=".$url."?page=".($page-2).">".($page-2)."</a> ";
  2757. else:
  2758. $pagetwoleft=null;
  2759. endif;
  2760. #одна назад
  2761. if(($page-1)>0):
  2762. $pageoneleft="<a class='first_page_link' href=".$url."?page=".($page-1).">".($page-1)."</a> ";
  2763. $pagetemp=($page-1);
  2764. else:
  2765. $pageoneleft=null;
  2766. $pagetemp=null;
  2767. endif;
  2768. #две вперед
  2769. if(($page+2)<=$total):
  2770. $pagetworight=" <a class='first_page_link' href=".$url."?page=".($page+2).">".($page+2)."</a>";
  2771. else:
  2772. $pagetworight=null;
  2773. endif;
  2774. #одна вперед
  2775. if(($page+1)<=$total):
  2776. $pageoneright=" <a class='first_page_link' href=".$url."?page=".($page+1).">".($page+1)."</a>";
  2777. $pagetemp2=($page+1);
  2778. else:
  2779. $pageoneright=null;
  2780. $pagetemp2=null;
  2781. endif;
  2782. # в начало
  2783. if($page!=1 && $pagetemp!=1 && $pagetemp!=2):
  2784. $pagerevp="<a href=".$url."?page=1 class='first_page_link' title='В начало'><<</a> ";
  2785. else:
  2786. $pagerevp=null;
  2787. endif;
  2788. #в конец (последняя)
  2789. if($page!=$total && $pagetemp2!=($total-1) && $pagetemp2!=$total):
  2790. $nextp=" ... <a href=".$url."?page=".$total." class='first_page_link'>$total</a>";
  2791. else:
  2792. $nextp=null;
  2793. endif;
  2794. print "<br>".$pagerevp.$pagetwoleft.$pageoneleft.'<span class="num_page_not_link"><b>'.$page.'</b></span>'.$pageoneright.$pagetworight.$nextp;
  2795. print " | Total records: $count_records";
  2796. print "</div>";
  2797. }
  2798. function get_option($db, $option_id)
  2799. {
  2800. $option = get_record($db, "config", "option_id=".$option_id);
  2801. if (empty($option) or empty($option['value'])) {
  2802. $default = get_record($db, "config_options","id=$option_id");
  2803. return $default['default_value'];
  2804. }
  2805. return $option['value'];
  2806. }
  2807. function is_option($db, $option_id)
  2808. {
  2809. list ($option) = mysqli_fetch_array(mysqli_query($db, "SELECT value FROM `config` WHERE option_id=$option_id"));
  2810. if (! isset($option) or empty($option) or $option === '') {
  2811. return;
  2812. }
  2813. return 1;
  2814. }
  2815. function set_option($db, $option_id, $value)
  2816. {
  2817. $option['value'] = $value;
  2818. update_record($db, 'config', "option_id=$option_id", $option);
  2819. }
  2820. function is_subnet_aton($subnet,$ip) {
  2821. if (!isset($subnet)) { return 0; }
  2822. if (!isset($ip)) { return 0; }
  2823. $range = cidrToRange($subnet);
  2824. if ($ip>=ip2long($range[0]) and $ip <=ip2long($range[1])) { return 1; }
  2825. return 0;
  2826. }
  2827. function get_new_user_id($db, $ip, $mac)
  2828. {
  2829. global $hotspot_user_id;
  2830. global $default_user_id;
  2831. //ip
  2832. if (!empty($ip)) {
  2833. if (is_hotspot($db, $ip)) { return $hotspot_user_id; }
  2834. $ip_aton = ip2long($ip);
  2835. $t_rules = get_records_sql($db, "SELECT * FROM auth_rules WHERE type=1 and LENGTH(rule)>0");
  2836. foreach ($t_rules as $row) {
  2837. if (!empty($row['rule']) and is_subnet_aton($row['rule'],$ip_aton)) { return $row['user_id']; }
  2838. }
  2839. }
  2840. //mac
  2841. if (!empty($mac)) {
  2842. $mac_rules=get_records_sql($db,"SELECT * FROM auth_rules WHERE type=2 AND LENGTH(rule)>0");
  2843. foreach ($mac_rules as $row) {
  2844. if (!empty($row['rule']) and preg_match($row['rule'], $mac)) { return $row['user_id']; }
  2845. }
  2846. }
  2847. //the hostname is not processed, because the dhcp-server on the microtic does not return it
  2848. return $default_user_id;
  2849. }
  2850. function get_subnet_range($db,$subnet_id) {
  2851. if (empty($subnet_id)) { return; }
  2852. $t_option = get_record_sql($db, "SELECT ip_int_start,ip_int_stop FROM `subnets` WHERE id=$subnet_id");
  2853. if (!isset($t_option['ip_int_start'])) { $t_option['ip_int_start']=0; }
  2854. if (!isset($t_option['ip_int_stop'])) { $t_option['ip_int_stop']=0; }
  2855. $subnet['start']=$t_option['ip_int_start'];
  2856. $subnet['stop']=$t_option['ip_int_stop'];
  2857. return $subnet;
  2858. }
  2859. function is_hotspot($db, $ip)
  2860. {
  2861. if (! isset($ip)) { return 0; }
  2862. LOG_DEBUG($db,"Check hotspot network for ip: $ip");
  2863. $ip_aton = ip2long($ip);
  2864. $t_option = mysqli_query($db, "SELECT subnet,ip_int_start,ip_int_stop FROM `subnets` WHERE hotspot=1");
  2865. while (list ($f_net,$f_start,$f_stop) = mysqli_fetch_array($t_option)) {
  2866. if ($ip_aton >= $f_start and $ip_aton <= $f_stop) {
  2867. LOG_DEBUG($db,"ip: $ip [$ip_aton] found in network $f_net: [".$f_start."..".$f_stop."]");
  2868. return 1;
  2869. }
  2870. }
  2871. LOG_DEBUG($db,"ip $ip not found in hotspot network!");
  2872. return 0;
  2873. }
  2874. function is_office($db, $ip)
  2875. {
  2876. if (! isset($ip)) { return 0; }
  2877. LOG_DEBUG($db,"Check office network for ip: $ip");
  2878. $ip_aton = ip2long($ip);
  2879. $t_option = mysqli_query($db, "SELECT subnet,ip_int_start,ip_int_stop FROM `subnets` WHERE office=1");
  2880. while (list ($f_net,$f_start,$f_stop) = mysqli_fetch_array($t_option)) {
  2881. if ($ip_aton >= $f_start and $ip_aton <= $f_stop) {
  2882. LOG_DEBUG($db,"ip: $ip [$ip_aton] found in office $f_net: [".$f_start."..".$f_stop."]");
  2883. return 1;
  2884. }
  2885. }
  2886. LOG_DEBUG($db,"ip $ip not found in office network!");
  2887. return 0;
  2888. }
  2889. function is_our_network($db, $ip)
  2890. {
  2891. if (! isset($ip)) { return 0; }
  2892. if (is_hotspot($db, $ip)) { return 1; }
  2893. if (is_office($db, $ip)) { return 1; }
  2894. return 0;
  2895. }
  2896. function init_option($db)
  2897. {
  2898. global $org_name;
  2899. $org_name = get_option($db, 32);
  2900. global $KB;
  2901. $KB = get_option($db, 1);
  2902. global $debug;
  2903. $debug = get_option($db, 34);
  2904. global $log_level;
  2905. $log_level = get_option($db, 53);
  2906. if ($debug) { $log_level = 255; }
  2907. global $send_email;
  2908. $send_email = get_option($db, 51);
  2909. global $admin_email;
  2910. $admin_email = get_option($db, 21);
  2911. global $sender_email;
  2912. $sender_email = get_option($db, 52);
  2913. global $mac_discovery;
  2914. $mac_discovery = get_option($db, 17);
  2915. global $snmp_default_version;
  2916. $snmp_default_version = get_option($db, 9);
  2917. global $snmp_default_community;
  2918. $snmp_default_community = get_option($db, 11);
  2919. global $default_user_id;
  2920. $default_user_id = get_option($db, 20);
  2921. global $hotspot_user_id;
  2922. $hotspot_user_id = get_option($db, 43);
  2923. if (! isset($hotspot_user_id)) {
  2924. $hotspot_user_id = $default_user_id;
  2925. }
  2926. global $cacti_url;
  2927. $cacti_url = get_option($db, 58);
  2928. if (preg_match('/127.0.0.1/', $cacti_url)) { $cacti_url=NULL; }
  2929. global $nagios_url;
  2930. $nagios_url = get_option($db, 57).'/cgi-bin';
  2931. if (preg_match('/127.0.0.1/', $nagios_url)) { $nagios_url=NULL; }
  2932. global $torrus_url;
  2933. $torrus_url = get_option($db, 59).'/Collector?nodeid=if//HOST_IP//IF_NAME////inoutbps';
  2934. if (preg_match('/127.0.0.1/', $torrus_url)) { $torrus_url=NULL; }
  2935. }
  2936. init_option($db_link);
  2937. clean_dns_cache($db_link);
  2938. ?>