1
0

common.php 115 KB

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