common.php 111 KB

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