common.php 119 KB

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