common.php 105 KB

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