common.php 118 KB

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