1
0

common.php 112 KB

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