1
0

common.php 103 KB

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