common.php 104 KB

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