1
0

common.php 114 KB

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