1
0

common.php 114 KB

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