common.php 114 KB

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