common.php 125 KB

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