common.php 107 KB

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