1
0

common.php 114 KB

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