common.php 124 KB

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