common.php 115 KB

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