common.php 128 KB

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