common.php 110 KB

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