common.php 98 KB

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