common.php 118 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399
  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.="Восстанавливаем доступ для 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.="Создаём новый ip-адрес \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, $mac, $action, $dhcp_hostname)
  1291. {
  1292. $ip_aton = ip2long($ip);
  1293. $auth_record= get_record_sql($db, "SELECT * FROM User_auth WHERE ip_int=$ip_aton AND mac='" . $mac . "' AND deleted=0");
  1294. if (!empty($auth_record)) {
  1295. $user_info = get_record_sql($db, "SELECT * FROM User_list WHERE id=".$auth_record['user_id']);
  1296. LOG_DEBUG($db, "external dhcp user ".$user_info['login']." [".$ip."] auth_id: ".$auth_record['id']);
  1297. if (isset($dhcp_hostname) and !empty($dhcp_hostname)) { $auth['dhcp_hostname'] = $dhcp_hostname; }
  1298. $auth['dhcp_action'] = $action;
  1299. $auth['dhcp_time'] = GetNowTimeString();
  1300. if ($action === 'add') { $auth['last_found'] = GetNowTimeString(); }
  1301. update_record($db, "User_auth", "id=" . $auth_record['id'], $auth);
  1302. return $auth_record['id'];
  1303. }
  1304. $ip_subnet=get_ip_subnet($db,$ip);
  1305. if ($ip_subnet['static']) {
  1306. LOG_WARNING($db, "Unknown pair ip+mac in static subnet! ip: $ip mac: [".mac_dotted($mac)."]. Skip");
  1307. return;
  1308. }
  1309. $msg = '';
  1310. // search changed mac
  1311. $auth_record = get_record_sql($db, "SELECT * FROM User_auth WHERE ip_int=$ip_aton AND deleted=0");
  1312. if (!empty($auth_record)) {
  1313. if (empty($auth_record['mac'])) {
  1314. $auth['mac'] = mac_dotted($mac);
  1315. $auth['dhcp_action'] = $action;
  1316. $auth['dhcp_time'] = GetNowTimeString();
  1317. if (!empty($dhcp_hostname)) { $auth['dhcp_hostname'] = $dhcp_hostname; }
  1318. if ($action === 'add') { $auth['last_found'] = GetNowTimeString(); }
  1319. LOG_INFO($db, "for ip: $ip mac not found! Use empty record...");
  1320. update_record($db, "User_auth", "id=" . $auth_record['id'], $auth);
  1321. return $auth_record['id'];
  1322. } else {
  1323. 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']);
  1324. run_sql($db, "UPDATE User_auth SET changed=1, deleted=1 WHERE id=" . $auth_record['id']);
  1325. }
  1326. }
  1327. // default id
  1328. $new_user_info = get_new_user_id($db, $ip, $mac, $dhcp_hostname);
  1329. if (!empty($new_user_info['user_id'])) { $new_user_id = $new_user_info['user_id']; }
  1330. if (empty($new_user_id)) { $new_user_id = new_user($db,$new_user_info); }
  1331. $resurrection_id = NULL;
  1332. $save_traf = get_option($db, 23);
  1333. $auth_record =get_record_sql($db, "SELECT * FROM User_auth WHERE ip_int=" . $ip_aton . " and mac='" . $mac . "'");
  1334. // seek old auth with same ip and mac
  1335. if (!empty($auth_record)) {
  1336. // found ->Resurrection old record
  1337. $resurrection_id = $auth_record['id'];
  1338. $msg .="Восстанавливаем доступ для auth_id: $resurrection_id with ip: $ip and mac: $mac ";
  1339. $auth['dhcp_action'] = $action;
  1340. $auth['user_id'] = $new_user_id;
  1341. $auth['deleted'] = 0;
  1342. $auth['dhcp_time'] = GetNowTimeString();
  1343. $auth['save_traf'] = $save_traf *1;
  1344. if (!empty($dhcp_hostname)) { $auth['dhcp_hostname'] = $dhcp_hostname; }
  1345. if ($action === 'add') { $auth['last_found'] = GetNowTimeString(); }
  1346. update_record($db, "User_auth", "id=$resurrection_id", $auth);
  1347. } else {
  1348. // not found ->create new record
  1349. $msg.="Создаём новый ip-адрес \r\nip: $ip\r\nmac: $mac\r\n";
  1350. $auth['deleted'] = 0;
  1351. $auth['user_id'] = $new_user_id;
  1352. $auth['ip'] = $ip;
  1353. $auth['ip_int'] = $ip_aton;
  1354. $auth['mac'] = $mac;
  1355. $auth['dhcp_action'] = $action;
  1356. $auth['dhcp_time'] = GetNowTimeString();
  1357. $auth['save_traf'] = $save_traf *1;
  1358. if (!empty($dhcp_hostname)) { $auth['dhcp_hostname'] = $dhcp_hostname; }
  1359. if ($action == 'add') { $auth['last_found'] = GetNowTimeString(); }
  1360. $resurrection_id=insert_record($db, "User_auth", $auth);
  1361. }
  1362. //check rules, update filter and state for new record
  1363. if (!empty($resurrection_id)) {
  1364. $user_rec=apply_auth_rule($db,$resurrection_id,$new_user_id);
  1365. $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";
  1366. if (!is_hotspot($db,$ip) and !empty($msg)) { LOG_WARNING($db, $msg); }
  1367. if (is_hotspot($db,$ip) and !empty($msg)) { LOG_INFO($db, $msg); }
  1368. }
  1369. return $resurrection_id;
  1370. }
  1371. function get_auth($db, $current_auth)
  1372. {
  1373. if (! isset($current_auth)) {
  1374. return;
  1375. }
  1376. if ($current_auth == 0) {
  1377. return;
  1378. }
  1379. $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");
  1380. list ($f_login, $f_ip) = mysqli_fetch_array($t_login);
  1381. $result = $f_login . "[" . $f_ip . "]";
  1382. return $result;
  1383. }
  1384. function get_auth_by_mac($db, $mac)
  1385. {
  1386. if (! isset($mac)) { return; }
  1387. $mac = mac_dotted($mac);
  1388. $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");
  1389. list ($f_id, $f_login, $f_auth_id, $f_ip) = mysqli_fetch_array($t_login);
  1390. if (isset($f_id)) {
  1391. $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>';
  1392. } else { $result['auth']='Unknown'; }
  1393. $result['mac'] = expand_mac($db,$mac);
  1394. return $result;
  1395. }
  1396. function get_auth_mac($db, $current_auth)
  1397. {
  1398. if (! isset($current_auth)) {
  1399. return;
  1400. }
  1401. if ($current_auth == 0) {
  1402. return;
  1403. }
  1404. $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");
  1405. list ($f_login, $f_mac) = mysqli_fetch_array($t_login);
  1406. $result = $f_login . "[" . $f_mac . "]";
  1407. return $result;
  1408. }
  1409. function isRO($db,$table)
  1410. {
  1411. $result = 1;
  1412. if (isset($_SESSION['login'])) {
  1413. $work_user = $_SESSION['login'];
  1414. }
  1415. if (isset($_SESSION['user_id'])) {
  1416. $work_id = $_SESSION['user_id'];
  1417. }
  1418. if (! isset($work_user) or ! isset($work_id)) {
  1419. return $result;
  1420. }
  1421. if (preg_match('/^(variables|dns_cache|syslog)$/',$table)) { return $result; }
  1422. $t_login = mysqli_query($db, "SELECT readonly FROM Customers WHERE Login='" . $work_user . "' and id='" . $work_id . "'");
  1423. list ($f_ro) = mysqli_fetch_array($t_login);
  1424. if (! isset($f_ro)) {
  1425. return $result;
  1426. }
  1427. return $f_ro;
  1428. }
  1429. function LOG_INFO($db,$msg,$auth_id = 0) {
  1430. if (get_const('log_level') < L_INFO) { return; }
  1431. write_log($db,$msg,L_INFO,$auth_id);
  1432. }
  1433. function LOG_ERROR($db,$msg,$auth_id = 0) {
  1434. if (get_const('log_level') < L_ERROR) { return; }
  1435. email(L_ERROR,$msg);
  1436. write_log($db,$msg,L_ERROR,$auth_id);
  1437. }
  1438. function LOG_VERBOSE($db,$msg,$auth_id=0) {
  1439. if (get_const('log_level') < L_VERBOSE) { return; }
  1440. write_log($db,$msg,L_VERBOSE,$auth_id);
  1441. }
  1442. function LOG_WARNING($db,$msg,$auth_id=0) {
  1443. if (get_const('log_level') < L_WARNING) { return; }
  1444. email(L_WARNING,$msg);
  1445. write_log($db,$msg,L_WARNING,$auth_id);
  1446. }
  1447. function LOG_DEBUG($db,$msg,$auth_id=0) {
  1448. if (!empty(get_const('debug')) and get_const('debug')) { write_log($db,$msg,L_DEBUG,$auth_id); }
  1449. }
  1450. function get_first_line($msg) {
  1451. if (empty($msg)) { return; }
  1452. preg_match('/(.*)(\n|\<br\>)/', $msg, $matches);
  1453. if (!empty($matches[1])) { return $matches[1]; }
  1454. return;
  1455. }
  1456. function email ($level,$msg) {
  1457. if (!get_const('send_email')) { return; }
  1458. if (!($level === L_WARNING or $level === L_ERROR)) { return; }
  1459. $subject = get_first_line($msg);
  1460. if ($level === L_WARNING) { $subject = "WARN: ".$subject."..."; $message = 'WARNING! Manager: '.$_SESSION['login'].' </br>'; }
  1461. if ($level === L_ERROR) { $subject = "ERROR: ".$subject."..."; $message = 'ERROR! Manager: '.$_SESSION['login'].' </br>'; }
  1462. $msg_lines = preg_replace("/\r\n/","</br>",$msg);
  1463. $message .= $msg_lines;
  1464. $send = SimpleMail::make()
  1465. ->setTo(get_const('admin_email'), 'Administrator')
  1466. ->setFrom(get_const('sender_email'), 'Stat')
  1467. ->setSubject($subject)
  1468. ->setMessage($message)
  1469. ->setHtml()
  1470. ->setWrap(80)
  1471. ->send();
  1472. }
  1473. function write_log($db, $msg, $level, $auth_id = 0)
  1474. {
  1475. $work_user = 'http';
  1476. if (isset($_SESSION['login'])) {
  1477. $work_user = $_SESSION['login'];
  1478. }
  1479. if (! isset($msg)) {
  1480. $msg = 'ERROR! Empty log string!';
  1481. }
  1482. if (!isset($level)) { $level = L_INFO; }
  1483. $msg = str_replace("'", '', $msg);
  1484. $sSQL = "insert into syslog(customer,message,level,auth_id) values('$work_user','$msg',$level,$auth_id)";
  1485. mysqli_query($db, $sSQL);
  1486. }
  1487. function print_year_select($year_name, $year)
  1488. {
  1489. print "<select name=\"$year_name\" >\n";
  1490. for ($i = $year - 10; $i <= $year + 10; $i ++) {
  1491. print_select_item($i,$i,$year);
  1492. }
  1493. print "</select>\n";
  1494. }
  1495. function print_date_select($dd, $mm, $yy)
  1496. {
  1497. if ($dd >= 1) {
  1498. print "<b>День</b>\n";
  1499. print "<select name=\"day\" >\n";
  1500. for ($i = 1; $i <= 31; $i ++) {
  1501. print_select_item($i,$i,$dd);
  1502. }
  1503. print "</select>\n";
  1504. }
  1505. if ($mm >= 1) {
  1506. print "<b>Месяц</b>\n";
  1507. print "<select name=\"month\" >\n";
  1508. for ($i = 1; $i <= 12; $i ++) {
  1509. $tmp_date = DateTimeImmutable::createFromFormat('U', strtotime("$i/01/$yy"));
  1510. $month_name = $tmp_date->format('F');
  1511. print_select_item($month_name,$i,$mm);
  1512. }
  1513. print "</select>\n";
  1514. }
  1515. print "<b>Год</b>\n";
  1516. print_year_select('year', $yy);
  1517. }
  1518. function print_date_select2($dd, $mm, $yy)
  1519. {
  1520. if ($dd >= 1) {
  1521. print "<b>День</b>\n";
  1522. print "<select name=\"day2\" >\n";
  1523. for ($i = 1; $i <= 31; $i ++) {
  1524. print_select_item($i,$i,$dd);
  1525. }
  1526. print "</select>\n";
  1527. }
  1528. if ($mm >= 1) {
  1529. print "<b>Месяц</b>\n";
  1530. print "<select name=\"month2\" >\n";
  1531. for ($i = 1; $i <= 12; $i ++) {
  1532. $tmp_date = DateTimeImmutable::createFromFormat('U', strtotime("$i/01/$yy"));
  1533. $month_name = $tmp_date->format('F');
  1534. print_select_item($month_name,$i,$mm);
  1535. }
  1536. print "</select>\n";
  1537. }
  1538. print "<b>Год</b>\n";
  1539. print_year_select('year2', $yy);
  1540. }
  1541. function is_up($ip)
  1542. {
  1543. if (! isset($ip) or strlen($ip) == 0) {
  1544. return false;
  1545. }
  1546. exec(sprintf('ping -i .3 -c 1 -W 5 %s', escapeshellarg($ip)), $res, $rval);
  1547. return $rval == 0;
  1548. }
  1549. function get_mac_port_table($ip, $port_index, $community, $version, $oid, $index_map)
  1550. {
  1551. if (! isset($ip)) {
  1552. return;
  1553. }
  1554. if (! isset($port_index)) {
  1555. return;
  1556. }
  1557. if (! isset($oid)) {
  1558. return;
  1559. }
  1560. if (! isset($community)) {
  1561. $community = 'public';
  1562. }
  1563. if (! isset($version)) {
  1564. $version = '2';
  1565. }
  1566. $mac_table = walk_snmp($ip, $community, $version, $oid);
  1567. if (isset($mac_table) and count($mac_table) > 0) {
  1568. foreach ($mac_table as $key => $value) {
  1569. $key = trim($key);
  1570. $value = intval(trim(str_replace('INTEGER:', '', $value)));
  1571. $value = $index_map[$value];
  1572. if ($value == $port_index) {
  1573. $pattern = '/\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})$/';
  1574. $result = preg_match($pattern, $key, $matches);
  1575. if ($result) {
  1576. $mac_key = preg_replace('/^\./', '', $matches[0]);
  1577. $fdb_port_table[$mac_key] = $value;
  1578. }
  1579. }
  1580. }
  1581. }
  1582. return $fdb_port_table;
  1583. }
  1584. function get_ifmib_index_table ($ip, $community, $version)
  1585. {
  1586. $ifmib_map = NULL;
  1587. $index_table = walk_snmp($ip, $community, $version, IFMIB_IFINDEX_MAP);
  1588. $is_mikrotik = walk_snmp($ip, $community, $version, MIKROTIK_DHCP_SERVER);
  1589. $mk_ros_version = 6497;
  1590. if ($is_mikrotik) {
  1591. $mikrotik_version = walk_snmp($ip, $community, $version, MIKROTIK_ROS_VERSION);
  1592. $result = preg_match('/RouterOS\s+(\d)\.(\d{1,3})\.(\d{1,3})\s+/',$mikrotik_version[MIKROTIK_ROS_VERSION],$matches);
  1593. if ($result) {
  1594. $mk_ros_version = $matches[1]*1000 + $matches[2]*10 + $matches[3];
  1595. }
  1596. }
  1597. if (isset($index_table) and count($index_table) > 0 and ($is_mikrotik and $mk_ros_version>6468)) {
  1598. foreach ($index_table as $key => $value) {
  1599. $key = trim($key);
  1600. $value = intval(trim(str_replace('INTEGER:', '', $value)));
  1601. $result = preg_match('/\.(\d{1,10})$/',$key,$matches);
  1602. if ($result) {
  1603. $int_index = preg_replace('/^\./', '', $matches[0]);
  1604. $ifmib_map[$int_index]=$value;
  1605. }
  1606. }
  1607. } else {
  1608. # $index_table = walk_snmp($ip, $community, $version, IFMIB_IFINDEX);
  1609. if (isset($index_table) and count($index_table) > 0) {
  1610. foreach ($index_table as $key => $value) {
  1611. $key = trim($key);
  1612. $value = intval(trim(str_replace('INTEGER:', '', $value)));
  1613. $result = preg_match('/\.(\d{1,10})$/',$key,$matches);
  1614. if ($result) {
  1615. $int_index = preg_replace('/^\./', '', $matches[0]);
  1616. $ifmib_map[$int_index]=$int_index;
  1617. }
  1618. }
  1619. }
  1620. }
  1621. return $ifmib_map;
  1622. }
  1623. function get_fdb_port_table($ip, $port_index, $community, $version)
  1624. {
  1625. if (! isset($ip)) {
  1626. return;
  1627. }
  1628. if (! isset($port_index)) {
  1629. return;
  1630. }
  1631. if (! isset($community)) {
  1632. $community = 'public';
  1633. }
  1634. if (! isset($version)) {
  1635. $version = '2';
  1636. }
  1637. $ifindex_map = get_ifmib_index_table($ip,$community, $version);
  1638. $fdb1_port_table = get_mac_port_table($ip, $port_index, $community, $version, MAC_TABLE_OID2, $ifindex_map);
  1639. $fdb2_port_table = get_mac_port_table($ip, $port_index, $community, $version, MAC_TABLE_OID, $ifindex_map);
  1640. if (!empty($fdb1_port_table)) { $fdb_port_table = $fdb1_port_table; }
  1641. if (!empty($fdb2_port_table)) {
  1642. if (empty($fdb_port_table)) {
  1643. $fdb_port_table = $fdb2_port_table;
  1644. } else {
  1645. foreach ($fdb2_port_table as $mac => $port) {
  1646. if (empty($fdb_port_table[$mac])) { $fdb_port_table[$mac]=$port; }
  1647. }
  1648. }
  1649. }
  1650. // maybe cisco?!
  1651. if (! isset($fdb_port_table) or ! $fdb_port_table or count($fdb_port_table) == 0) {
  1652. $vlan_table = walk_snmp($ip, $community, $version, CISCO_VLAN_OID);
  1653. if (! $vlan_table) {
  1654. return;
  1655. }
  1656. //fucking cisco!!!
  1657. foreach ($vlan_table as $vlan_oid => $value) {
  1658. if (! $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_port_table($ip, $port_index, $community . '@' . $vlan_id, $version, MAC_TABLE_OID2,$ifindex_map);
  1665. if (! isset($fdb_vlan_table) or ! $fdb_vlan_table or count($fdb_vlan_table) == 0) {
  1666. $fdb_vlan_table = get_mac_port_table($ip, $port_index, $community, $version, MAC_TABLE_OID,$ifindex_map);
  1667. }
  1668. foreach ($fdb_vlan_table as $mac => $port) {
  1669. if (! isset($mac)) { continue; }
  1670. $fdb_port_table[$mac] = $port;
  1671. }
  1672. }
  1673. }
  1674. }
  1675. return $fdb_port_table;
  1676. }
  1677. function get_mac_table($ip, $community, $version, $oid, $index_map)
  1678. {
  1679. if (! isset($ip)) {
  1680. return;
  1681. }
  1682. if (! isset($oid)) {
  1683. return;
  1684. }
  1685. if (! isset($community)) {
  1686. $community = 'public';
  1687. }
  1688. if (! isset($version)) {
  1689. $version = '2';
  1690. }
  1691. $mac_table = walk_snmp($ip, $community, $version, $oid);
  1692. if (isset($mac_table) and count($mac_table) > 0) {
  1693. foreach ($mac_table as $key => $value) {
  1694. $key = trim($key);
  1695. $value = intval(trim(str_replace('INTEGER:', '', $value)));
  1696. $value = $index_map[$value];
  1697. $pattern = '/\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})$/';
  1698. $result = preg_match($pattern, $key, $matches);
  1699. if (!empty($result)) {
  1700. $mac_key = preg_replace('/^\./', '', $matches[0]);
  1701. $fdb_table[$mac_key] = $value;
  1702. }
  1703. }
  1704. }
  1705. return $fdb_table;
  1706. }
  1707. function get_fdb_table($ip, $community, $version)
  1708. {
  1709. if (! isset($ip)) {
  1710. return;
  1711. }
  1712. if (! isset($community)) {
  1713. $community = 'public';
  1714. }
  1715. if (! isset($version)) {
  1716. $version = '2';
  1717. }
  1718. $ifindex_map = get_ifmib_index_table($ip,$community, $version);
  1719. $fdb1_table = get_mac_table($ip, $community, $version, MAC_TABLE_OID2, $ifindex_map);
  1720. $fdb2_table = get_mac_table($ip, $community, $version, MAC_TABLE_OID, $ifindex_map);
  1721. if (!empty($fdb1_table)) { $fdb_table = $fdb1_table; }
  1722. if (!empty($fdb2_table)) {
  1723. if (empty($fdb_table)) {
  1724. $fdb_table = $fdb2_table;
  1725. } else {
  1726. foreach ($fdb2_table as $mac => $port) {
  1727. if (empty($fdb_table[$mac])) { $fdb_table[$mac]=$port; }
  1728. }
  1729. }
  1730. }
  1731. // maybe cisco?!
  1732. if (! isset($fdb_table) or ! $fdb_table or count($fdb_table) == 0) {
  1733. $vlan_table = walk_snmp($ip, $community, $version, CISCO_VLAN_OID);
  1734. if (! $vlan_table) {
  1735. return;
  1736. }
  1737. foreach ($vlan_table as $vlan_oid => $value) {
  1738. if (! $vlan_oid) { continue; }
  1739. $pattern = '/\.(\d{1,4})$/';
  1740. $result = preg_match($pattern, $vlan_oid, $matches);
  1741. if (!empty($result)) {
  1742. $vlan_id = preg_replace('/^\./', '', $matches[0]);
  1743. if ($vlan_id > 1000 and $vlan_id < 1009) { continue; }
  1744. $fdb_vlan_table = get_mac_table($ip, $community.'@'.$vlan_id, $version, MAC_TABLE_OID2,$ifindex_map);
  1745. if (! isset($fdb_vlan_table) or ! $fdb_vlan_table or count($fdb_vlan_table) == 0) {
  1746. $fdb_vlan_table = get_mac_table($ip, $community, $version, MAC_TABLE_OID,$ifindex_map);
  1747. }
  1748. foreach ($fdb_vlan_table as $mac => $port) {
  1749. if (! isset($mac)) { continue; }
  1750. $fdb_table[$mac] = $port;
  1751. }
  1752. }
  1753. }
  1754. }
  1755. return $fdb_table;
  1756. }
  1757. function check_snmp_access($ip, $community, $version)
  1758. {
  1759. if (! isset($ip)) {
  1760. return;
  1761. }
  1762. if (! isset($community)) {
  1763. $community = 'public';
  1764. }
  1765. if (! isset($version)) {
  1766. $version = '2';
  1767. }
  1768. $result = get_snmp($ip, $community, $version, SYS_DESCR_MIB);
  1769. if (!isset($result)) { return; }
  1770. return 1;
  1771. }
  1772. function get_port_state($port, $ip, $community, $version)
  1773. {
  1774. if (! isset($port)) {
  1775. return;
  1776. }
  1777. if (! isset($ip)) {
  1778. return;
  1779. }
  1780. if (! isset($community)) {
  1781. $community = 'public';
  1782. }
  1783. if (! isset($version)) {
  1784. $version = '2';
  1785. }
  1786. $port_oid = PORT_STATUS_OID . $port;
  1787. $port_state = get_snmp($ip, $community, $version, $port_oid);
  1788. return $port_state;
  1789. }
  1790. function get_last_digit($oid)
  1791. {
  1792. if (! isset($oid)) {
  1793. return;
  1794. }
  1795. $pattern = '/\.(\d{1,})$/';
  1796. preg_match($pattern, $oid, $matches);
  1797. return $matches[1];
  1798. }
  1799. function get_cisco_sensors($ip, $community, $version, $mkey)
  1800. {
  1801. $index = get_last_digit($mkey);
  1802. $result = parse_snmp_value(get_snmp($ip, $community, $version, CISCO_SFP_SENSORS . "." . $index));
  1803. $prec = parse_snmp_value(get_snmp($ip, $community, $version, CISCO_SFP_PRECISION . "." . $index));
  1804. if (! isset($prec)) { $prec = 1; }
  1805. $result = round(trim($result) / (10 * $prec), 2);
  1806. return $result;
  1807. }
  1808. function get_snmp_ifname1($ip, $community, $version, $port)
  1809. {
  1810. $port_name = parse_snmp_value(get_snmp($ip, $community, $version, IFMIB_IFNAME . "." . $port));
  1811. return $port_name;
  1812. }
  1813. function get_snmp_ifname2($ip, $community, $version, $port)
  1814. {
  1815. $port_name = parse_snmp_value(get_snmp($ip, $community, $version, IFMIB_IFDESCR . "." . $port));
  1816. return $port_name;
  1817. }
  1818. function get_snmp_interfaces($ip, $community, $version)
  1819. {
  1820. $result = walk_snmp($ip, $community, $version, IFMIB_IFNAME);
  1821. if (empty($result)) { $result = walk_snmp($ip, $community, $version, IFMIB_IFDESCR); }
  1822. return $result;
  1823. }
  1824. function walk_snmp($ip, $community, $version, $oid)
  1825. {
  1826. snmp_set_oid_output_format(SNMP_OID_OUTPUT_NUMERIC);
  1827. if ($version == 2) {
  1828. $result = snmp2_real_walk($ip, $community, $oid);
  1829. }
  1830. if ($version == 1) {
  1831. $result = snmprealwalk($ip, $community, $oid);
  1832. }
  1833. return $result;
  1834. }
  1835. function get_snmp_module_id($modules_oids, $port_name)
  1836. {
  1837. $port_name = preg_quote(trim($port_name), '/');
  1838. foreach ($modules_oids as $key => $value) {
  1839. $pattern = '/' . $port_name . '/i';
  1840. preg_match($pattern, $value, $matches);
  1841. if (isset($matches[0])) {
  1842. $module_id = get_last_digit($key);
  1843. return $module_id;
  1844. }
  1845. }
  1846. return;
  1847. }
  1848. function get_sfp_status($vendor_id, $port, $ip, $community, $version, $modules_oids)
  1849. {
  1850. if (! isset($vendor_id)) {
  1851. return;
  1852. }
  1853. if (! isset($port)) {
  1854. return;
  1855. }
  1856. if (! isset($ip)) {
  1857. return;
  1858. }
  1859. if (! isset($community)) {
  1860. $community = 'public';
  1861. }
  1862. if (! isset($version)) {
  1863. $version = '2';
  1864. }
  1865. // if (!is_up($ip)) { return; }
  1866. $status = '';
  1867. // eltex
  1868. if ($vendor_id == 2) {
  1869. $sfp_vendor = parse_snmp_value(get_snmp($ip, $community, $version, ELTEX_SFP_VENDOR . "." . $port));
  1870. if (isset($sfp_vendor)) {
  1871. $sfp_status_temp = ELTEX_SFP_STATUS . "." . $port . ".5";
  1872. $sfp_status_volt = ELTEX_SFP_STATUS . "." . $port . ".6";
  1873. $sfp_status_circut = ELTEX_SFP_STATUS . "." . $port . ".7";
  1874. $sfp_status_tx = ELTEX_SFP_STATUS . "." . $port . ".8";
  1875. $sfp_status_rx = ELTEX_SFP_STATUS . "." . $port . ".9";
  1876. $temp = parse_snmp_value(get_snmp($ip, $community, $version, $sfp_status_temp));
  1877. $volt = parse_snmp_value(get_snmp($ip, $community, $version, $sfp_status_volt));
  1878. $circut = parse_snmp_value(get_snmp($ip, $community, $version, $sfp_status_circut));
  1879. $tx = parse_snmp_value(get_snmp($ip, $community, $version, $sfp_status_tx));
  1880. $rx = parse_snmp_value(get_snmp($ip, $community, $version, $sfp_status_rx));
  1881. $sfp_sn = parse_snmp_value(get_snmp($ip, $community, $version, ELTEX_SFP_SN . "." . $port));
  1882. $sfp_freq = parse_snmp_value(get_snmp($ip, $community, $version, ELTEX_SFP_FREQ . "." . $port));
  1883. if (! isset($sfp_freq) or $sfp_freq == 65535) { $sfp_freq = 'unspecified'; }
  1884. $sfp_length = parse_snmp_value(get_snmp($ip, $community, $version, ELTEX_SFP_LENGTH . "." . $port));
  1885. $status = 'Vendor: ' . $sfp_vendor . ' Serial: ' . $sfp_sn . ' Laser: ' . $sfp_freq . ' Distance: ' . $sfp_length . '<br>';
  1886. if (!empty($sfp_status_temp) and $temp > 0.1) { $status .= 'Temp: ' . $temp . " C"; }
  1887. if (!empty($sfp_status_volt) and $volt > 0.1) { $status .= ' Volt: ' . round($volt / 1000000, 2) . ' V'; }
  1888. if (!empty($sfp_status_circut) and $circut > 0.1) { $status .= ' Circut: ' . round($circut / 1000, 2) . ' mA'; }
  1889. if (!empty($sfp_status_tx) and $tx > 0.1) { $status .= ' Tx: ' . round($tx / 1000, 2) . ' dBm'; }
  1890. if (!empty($sfp_status_rx) and $rx > 0.1) { $status .= ' Rx: ' . round($rx / 1000, 2) . ' dBm'; }
  1891. $status .= '<br>';
  1892. return $status;
  1893. }
  1894. return;
  1895. }
  1896. // cisco
  1897. if ($vendor_id == 16) {
  1898. // get interface names
  1899. $port_name = parse_snmp_value(get_snmp($ip, $community, $version, IFMIB_IFNAME . "." . $port));
  1900. if (empty($port_name)) {
  1901. $port_name = parse_snmp_value(get_snmp($ip, $community, $version, IFMIB_IFDESCR . "." . $port));
  1902. }
  1903. // search module indexes
  1904. $port_name = preg_quote(trim($port_name), '/');
  1905. foreach ($modules_oids as $key => $value) {
  1906. $pattern = '/(' . $port_name . ' Module Temperature Sensor)/i';
  1907. preg_match($pattern, $value, $matches);
  1908. if (isset($matches[0])) {
  1909. $temp = get_cisco_sensors($ip, $community, $version, $key);
  1910. continue;
  1911. }
  1912. $pattern = '/(' . $port_name . ' Supply Voltage Sensor)/i';
  1913. preg_match($pattern, $value, $matches);
  1914. if (isset($matches[0])) {
  1915. $volt = get_cisco_sensors($ip, $community, $version, $key);
  1916. continue;
  1917. }
  1918. $pattern = '/(' . $port_name . ' Bias Current Sensor)/i';
  1919. preg_match($pattern, $value, $matches);
  1920. if (isset($matches[0])) {
  1921. $circut = get_cisco_sensors($ip, $community, $version, $key);
  1922. continue;
  1923. }
  1924. $pattern = '/(' . $port_name . ' Transmit Power Sensor)/i';
  1925. preg_match($pattern, $value, $matches);
  1926. if (isset($matches[0])) {
  1927. $tx = get_cisco_sensors($ip, $community, $version, $key);
  1928. continue;
  1929. }
  1930. $pattern = '/(' . $port_name . ' Receive Power Sensor)/i';
  1931. preg_match($pattern, $value, $matches);
  1932. if (isset($matches[0])) {
  1933. $rx = get_cisco_sensors($ip, $community, $version, $key);
  1934. continue;
  1935. }
  1936. }
  1937. if (!empty($temp) and $temp > 0) {
  1938. $status .= 'Temp: ' . $temp . " C";
  1939. }
  1940. if (!empty($volt) and $volt > 0) {
  1941. $status .= ' Volt: ' . $volt . ' V';
  1942. }
  1943. if (!empty($circut) and $circut > 0) {
  1944. $status .= ' Circut: ' . $circut . ' mA';
  1945. }
  1946. if (!empty($tx) and abs($tx)>0.1) {
  1947. $status .= ' Tx: ' . $tx . ' dBm';
  1948. }
  1949. if (!empty($rx) and abs($rx)>0.1) {
  1950. $status .= ' Rx: ' . $rx . ' dBm';
  1951. }
  1952. if (!empty($status)) {
  1953. $status = preg_replace('/"/', '', $status);
  1954. $status .= '<br>';
  1955. }
  1956. return $status;
  1957. }
  1958. // huawei
  1959. if ($vendor_id == 3) {
  1960. // get interface names
  1961. $port_name = parse_snmp_value(get_snmp($ip, $community, $version, IFMIB_IFNAME . "." . $port));
  1962. if (empty($port_name)) {
  1963. $port_name = parse_snmp_value(get_snmp($ip, $community, $version, IFMIB_IFDESCR . "." . $port));
  1964. }
  1965. // search module indexes
  1966. $port_name = preg_quote(trim($port_name), '/');
  1967. foreach ($modules_oids as $key => $value) {
  1968. $pattern = '/' . $port_name . '/i';
  1969. preg_match($pattern, $value, $matches);
  1970. if (isset($matches[0])) {
  1971. $module_id = get_last_digit($key);
  1972. unset($result);
  1973. $result = parse_snmp_value(get_snmp($ip, $community, $version, HUAWEI_SFP_VENDOR . "." . $module_id));
  1974. if (isset($result)) { $sfp_vendor = $result; }
  1975. unset($result);
  1976. $result = parse_snmp_value(get_snmp($ip, $community, $version, HUAWEI_SFP_SPEED . "." . $module_id));
  1977. if (isset($result)) {
  1978. list ($sfp_speed, $spf_lenght, $sfp_type) = explode('-', $result);
  1979. if ($sfp_type == 0) { $sfp_type = 'MultiMode'; }
  1980. if ($sfp_type == 1) { $sfp_type = 'SingleMode'; }
  1981. }
  1982. $volt = parse_snmp_value(get_snmp($ip, $community, $version, HUAWEI_SFP_VOLT . "." . $module_id));
  1983. $circut = parse_snmp_value(get_snmp($ip, $community, $version, HUAWEI_SFP_BIASCURRENT . "." . $module_id));
  1984. $tx = parse_snmp_value(get_snmp($ip, $community, $version, HUAWEI_SFP_OPTTX . "." . $module_id));
  1985. $rx = parse_snmp_value(get_snmp($ip, $community, $version, HUAWEI_SFP_OPTRX . "." . $module_id));
  1986. if (!isset($tx)) { $tx = parse_snmp_value(get_snmp($ip, $community, $version, HUAWEI_SFP_TX . "." . $module_id)); }
  1987. if (!isset($rx)) { $rx = parse_snmp_value(get_snmp($ip, $community, $version, HUAWEI_SFP_RX . "." . $module_id)); }
  1988. if (!empty($sfp_vendor)) { $status .= ' Name:' . $sfp_vendor.'<br>'; }
  1989. // if (isset($sfp_speed)) { $status .= ' ' . $sfp_speed; }
  1990. // if (isset($spf_lenght)) { $status .= ' ' . $spf_lenght; }
  1991. if ($volt > 0) { $status .= ' Volt: ' . round($volt / 1000, 2) . ' V'; }
  1992. if (!empty($circut) and $circut >0) { $status .= ' Circut: ' . $circut . ' mA <br>'; }
  1993. if (!empty($tx)) {
  1994. $tx = preg_replace('/"/', '', $tx);
  1995. list($tx_dbm,$pattern) = explode('.', $tx);
  1996. $tx_dbm=round(trim($tx_dbm) / 100,2);
  1997. if (abs($tx_dbm)>0.1) { $status .= ' Tx: '.$tx_dbm.' dBm'; }
  1998. }
  1999. if (!empty($rx)) {
  2000. $rx = preg_replace('/"/', '', $rx);
  2001. list($rx_dbm,$pattern) = explode('.', $rx);
  2002. $rx_dbm=round(trim($rx_dbm) / 100,2);
  2003. if (abs($rx_dbm)>0.1) { $status .= ' Rx: '.$rx_dbm.' dBm'; }
  2004. }
  2005. break;
  2006. }
  2007. }
  2008. if (isset($status)) {
  2009. $status = preg_replace('/"/', '', $status);
  2010. $status .= '<br>';
  2011. }
  2012. return $status;
  2013. }
  2014. return;
  2015. }
  2016. function get_port_vlan($vendor, $port, $port_index, $ip, $community, $version, $fdb_by_snmp)
  2017. {
  2018. if (! isset($port)) {
  2019. return;
  2020. }
  2021. if (! isset($ip)) {
  2022. return;
  2023. }
  2024. if (! isset($community)) {
  2025. $community = 'public';
  2026. }
  2027. if (! isset($version)) {
  2028. $version = '2';
  2029. }
  2030. // if (!is_up($ip)) { return; }
  2031. if ($fdb_by_snmp == 1) { $port = $port_index; }
  2032. if ($vendor == 69) { $port_oid = TPLINK_VLAN_PVID . "." . $port; } else { $port_oid = PORT_VLAN_OID . "." . $port; }
  2033. $port_vlan = get_snmp($ip, $community, $version, $port_oid);
  2034. $port_vlan = preg_replace('/.*\:/','',$port_vlan);
  2035. $port_vlan = intval(trim($port_vlan));
  2036. return $port_vlan;
  2037. }
  2038. function get_port_poe_state($vendor_id, $port, $port_snmp_index, $ip, $community = 'public', $version ='2')
  2039. {
  2040. if (! isset($port)) { return; }
  2041. if (! isset($ip)) { return; }
  2042. // default poe oid
  2043. $poe_status = PETH_PSE_PORT_ADMIN_ENABLE . "." . $port_snmp_index;
  2044. if ($vendor_id == 3) {
  2045. $poe_status = HUAWEI_POE_OID . "." . $port_snmp_index;
  2046. }
  2047. if ($vendor_id == 8) {
  2048. $poe_status = ALLIED_POE_OID . "." . $port_snmp_index;
  2049. }
  2050. if ($vendor_id == 15) {
  2051. $poe_status = HP_POE_OID . "." . $port_snmp_index;
  2052. }
  2053. if ($vendor_id == 9) {
  2054. $poe_status = MIKROTIK_POE_OID . "." . $port_snmp_index;
  2055. }
  2056. if ($vendor_id == 10) {
  2057. $poe_status = NETGEAR_POE_OID . "." . $port_snmp_index;
  2058. }
  2059. if ($vendor_id == 69) {
  2060. $poe_status = TPLINK_POE_OID . "." . $port;
  2061. }
  2062. $result = '';
  2063. $c_state = get_snmp($ip, $community, $version, $poe_status);
  2064. if (!empty($c_state)) {
  2065. $p_state = parse_snmp_value($c_state);
  2066. // patch for mikrotik
  2067. if ($vendor_id == 9) {
  2068. if ($p_state == 1) { return 2; }
  2069. if ($p_state > 1) { return 1; }
  2070. }
  2071. //patch for tplink
  2072. if ($vendor_id == 69) {
  2073. if ($p_state == 0) { return 2; }
  2074. if ($p_state >= 1) { return 1; }
  2075. }
  2076. return $p_state;
  2077. }
  2078. return;
  2079. }
  2080. function set_port_poe_state($vendor_id, $port, $port_snmp_index, $ip, $community, $version, $state)
  2081. {
  2082. if (! isset($ip)) { return; }
  2083. if (! isset($community)) { $community = 'public'; }
  2084. if (! isset($version)) { $version = '2'; }
  2085. //default poe status
  2086. $poe_enable = 1;
  2087. $poe_disable = 2;
  2088. // default poe oid
  2089. $poe_status = PETH_PSE_PORT_ADMIN_ENABLE . "." . $port_snmp_index;
  2090. if ($vendor_id == 3) { $poe_status = HUAWEI_POE_OID . "." . $port_snmp_index; }
  2091. if ($vendor_id == 8) { $poe_status = ALLIED_POE_OID . "." . $port_snmp_index; }
  2092. if ($vendor_id == 15) { $poe_status = HP_POE_OID . "." . $port_snmp_index; }
  2093. if ($vendor_id == 10) { $poe_status = NETGEAR_POE_OID . "." . $port_snmp_index; }
  2094. if ($vendor_id == 69) {
  2095. $poe_status = TPLINK_POE_OID . "." . $port;
  2096. $poe_enable = 1;
  2097. $poe_disable = 0;
  2098. }
  2099. if ($state) {
  2100. // enable port
  2101. $c_state = set_snmp($ip, $community, $version, $poe_status, 'i', $poe_enable);
  2102. return $c_state;
  2103. } else {
  2104. // disable port
  2105. $c_state = set_snmp($ip, $community, $version, $poe_status, 'i', $poe_disable);
  2106. return $c_state;
  2107. }
  2108. }
  2109. function get_port_poe_detail($vendor_id, $port, $port_snmp_index, $ip, $community, $version)
  2110. {
  2111. if (! isset($port) or !isset($port_snmp_index)) { return; }
  2112. if (! isset($ip)) { return; }
  2113. if (! isset($community)) { $community = 'public'; }
  2114. if (! isset($version)) { $version = '2'; }
  2115. $result = '';
  2116. $poe_class = PETH_PSE_PORT_POE_CLASS . $port_snmp_index;
  2117. // eltex
  2118. if ($vendor_id == 2) {
  2119. $poe_power = ELTEX_POE_USAGE . '.' . $port_snmp_index;
  2120. $poe_current = ELTEX_POE_CURRENT .'.' . $port_snmp_index;
  2121. $poe_volt = ELTEX_POE_VOLT . '.' . $port_snmp_index;
  2122. }
  2123. // huawei
  2124. if ($vendor_id == 3) {
  2125. $poe_power = HUAWEI_POE_USAGE . '.' . $port_snmp_index;
  2126. $poe_current = HUAWEI_POE_CURRENT .'.' . $port_snmp_index;
  2127. $poe_volt = HUAWEI_POE_VOLT . '.' . $port_snmp_index;
  2128. }
  2129. // AT
  2130. if ($vendor_id == 8) {
  2131. $poe_power = ALLIED_POE_USAGE . '.' . $port_snmp_index;
  2132. $poe_current = ALLIED_POE_CURRENT .'.' . $port_snmp_index;
  2133. $poe_volt = ALLIED_POE_VOLT . '.' . $port_snmp_index;
  2134. }
  2135. // mikrotik
  2136. if ($vendor_id == 9) {
  2137. $poe_power = MIKROTIK_POE_USAGE . '.' . $port_snmp_index;
  2138. $poe_current = MIKROTIK_POE_CURRENT . '.' . $port_snmp_index;
  2139. $poe_volt = MIKROTIK_POE_VOLT . '.' . $port_snmp_index;
  2140. }
  2141. // netgear
  2142. if ($vendor_id == 10) {
  2143. $poe_power = NETGEAR_POE_USAGE . '.' . $port_snmp_index;
  2144. $poe_current = NETGEAR_POE_CURRENT .'.' . $port_snmp_index;
  2145. $poe_volt = NETGEAR_POE_VOLT . '.' . $port_snmp_index;
  2146. }
  2147. // HP
  2148. if ($vendor_id == 15) {
  2149. $poe_power = HP_POE_USAGE . '.' . $port_snmp_index;
  2150. $poe_volt = HP_POE_VOLT . '.' . $port_snmp_index;
  2151. }
  2152. // TP-Link
  2153. if ($vendor_id == 69) {
  2154. $poe_power = TPLINK_POE_USAGE . '.' . $port;
  2155. $poe_current = TPLINK_POE_CURRENT .'.' . $port;
  2156. $poe_volt = TPLINK_POE_VOLT . '.' . $port;
  2157. $poe_class = TPLINK_POE_CLASS . "." . $port;
  2158. }
  2159. if (isset($poe_power)) {
  2160. $c_power = get_snmp($ip, $community, $version, $poe_power);
  2161. if (isset($c_power)) {
  2162. $p_power = parse_snmp_value($c_power);
  2163. switch ($vendor_id) {
  2164. case 9:
  2165. $p_power = round($p_power / 10, 2);
  2166. break;
  2167. case 69:
  2168. $p_power = round($p_power / 10, 2);
  2169. break;
  2170. default:
  2171. $p_power = round($p_power / 1000, 2);
  2172. break;
  2173. }
  2174. if ($p_power > 0) {
  2175. $result .= ' P: ' . $p_power . ' W';
  2176. }
  2177. }
  2178. }
  2179. if (isset($poe_current)) {
  2180. $c_current = get_snmp($ip, $community, $version, $poe_current);
  2181. if (isset($c_current)) {
  2182. $p_current = parse_snmp_value($c_current);
  2183. if ($p_current > 0) {
  2184. $result .= ' C: ' . $p_current . ' mA';
  2185. }
  2186. }
  2187. }
  2188. if (isset($poe_volt)) {
  2189. $c_volt = get_snmp($ip, $community, $version, $poe_volt);
  2190. if (isset($c_volt)) {
  2191. $p_volt = parse_snmp_value($c_volt);
  2192. switch ($vendor_id) {
  2193. case 2:
  2194. case 8:
  2195. $p_volt = round($p_volt / 1000, 2);
  2196. break;
  2197. case 9:
  2198. case 69:
  2199. $p_volt = round($p_volt / 10, 2);
  2200. break;
  2201. case 15:
  2202. $p_volt = round($p_volt / 100, 2);
  2203. break;
  2204. }
  2205. if ($p_volt > 0 and $p_power > 0) {
  2206. $result .= ' V: ' . $p_volt . " V";
  2207. }
  2208. }
  2209. }
  2210. if (isset($poe_class)) {
  2211. $c_class = get_snmp($ip, $community, $version, $poe_class);
  2212. if (isset($c_class)) {
  2213. $p_class = parse_snmp_value($c_class);
  2214. switch ($vendor_id) {
  2215. case 69: if ($p_class > 0 and $p_power > 0) {
  2216. if ($p_class == 7 ) { $p_class = 'class-not-defined'; }
  2217. $result .= ' Class: ' . $p_class ;
  2218. }
  2219. break;
  2220. default: if ($p_class > 0 and $p_power > 0) { $result .= ' Class: ' . ($p_class - 1); }
  2221. break;
  2222. }
  2223. }
  2224. }
  2225. return $result;
  2226. }
  2227. function get_snmp($ip, $community, $version, $oid)
  2228. {
  2229. snmp_set_oid_output_format(SNMP_OID_OUTPUT_NUMERIC);
  2230. if ($version == 2) {
  2231. $result = snmp2_get($ip, $community, $oid);
  2232. }
  2233. if ($version == 1) {
  2234. $result = snmpget($ip, $community, $oid);
  2235. }
  2236. return $result;
  2237. }
  2238. function set_snmp($ip, $community, $version, $oid, $field, $value)
  2239. {
  2240. if ($version == 2) {
  2241. $result = snmp2_set($ip, $community, $oid, $field, $value);
  2242. }
  2243. if ($version == 1) {
  2244. $result = snmpset($ip, $community, $oid, $field, $value);
  2245. }
  2246. return $result;
  2247. }
  2248. function set_port_state($vendor_id, $port, $ip, $community, $version, $state)
  2249. {
  2250. // port -> snmp_index!!!
  2251. if (! isset($port)) {
  2252. return;
  2253. }
  2254. if (! isset($ip)) {
  2255. return;
  2256. }
  2257. if (! isset($community)) {
  2258. $community = 'public';
  2259. }
  2260. if (! isset($version)) {
  2261. $version = '2';
  2262. }
  2263. $port_status = PORT_ADMIN_STATUS_OID . $port;
  2264. if ($state == 1) {
  2265. // enable port
  2266. $c_state = set_snmp($ip, $community, $version, $port_status, 'i', 1);
  2267. return $c_state;
  2268. } else {
  2269. // disable port
  2270. $c_state = set_snmp($ip, $community, $version, $port_status, 'i', 2);
  2271. return $c_state;
  2272. }
  2273. }
  2274. function set_port_for_group($db, $group_id, $place_id, $state)
  2275. {
  2276. $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;
  2277. $auth_list = mysqli_query($db, $authSQL);
  2278. LOG_INFO($db, 'Mass port state change started!');
  2279. // get auth list for group
  2280. while (list ($a_id, $a_name, $a_ip) = mysqli_fetch_array($auth_list)) {
  2281. // get device and port for auth
  2282. if ($place_id == 0) {
  2283. $place_filter = '';
  2284. } else {
  2285. $place_filter = 'D.building_id=' . $place_id . ' and ';
  2286. }
  2287. $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';
  2288. $dev_info = mysqli_query($db, $devSQL);
  2289. 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);
  2290. if (! isset($d_id)) {
  2291. continue;
  2292. }
  2293. if ($state == 1) {
  2294. $mode = 'enable';
  2295. run_sql($db, "Update User_auth set nagios_handler='restart-port' WHERE id=$a_id and nagios_handler='manual-mode'");
  2296. } else {
  2297. $mode = 'disable';
  2298. run_sql($db, "Update User_auth set nagios_handler='manual-mode' WHERE id=$a_id and nagios_handler='restart-port'");
  2299. }
  2300. LOG_INFO($db, "At device $d_name [$d_ip] $mode port $d_port for auth_id: $a_id ($a_ip [$a_name])");
  2301. set_port_state($d_vendor_id, $d_snmp_index, $d_ip, $d_community, $d_snmp, $state);
  2302. set_port_poe_state($d_vendor_id, $d_port, $d_snmp_index, $d_ip, $d_community, $d_snmp, $state);
  2303. }
  2304. LOG_INFO($db, 'Mass port state change stopped.');
  2305. }
  2306. function get_vendor($db,$mac)
  2307. {
  2308. $mac = mac_dotted($mac);
  2309. $vendor = get_record_sql($db,'SELECT companyName,companyAddress FROM mac_vendors WHERE oui="'.substr($mac,0,8).'" or oui="'.substr($mac,0,11).'"');
  2310. $result='';
  2311. if (!empty($vendor)) {
  2312. $result = $vendor['companyName'];
  2313. if (!empty($vendor['companyAddress'])) { $result = $vendor['companyAddress']; }
  2314. }
  2315. return $result;
  2316. }
  2317. function get_port_state_detail($port, $ip, $community, $version)
  2318. {
  2319. if (! isset($port)) {
  2320. return;
  2321. }
  2322. if (! isset($ip)) {
  2323. return;
  2324. }
  2325. if (! isset($community)) {
  2326. $community = 'public';
  2327. }
  2328. if (! isset($version)) {
  2329. $version = '2';
  2330. }
  2331. // if (!is_up($ip)) { return; }
  2332. $oper = PORT_STATUS_OID . $port;
  2333. $admin = PORT_ADMIN_STATUS_OID . $port;
  2334. $speed = PORT_SPEED_OID . $port;
  2335. $errors = PORT_ERRORS_OID . $port;
  2336. $result = '';
  2337. $c_state = get_snmp($ip, $community, $version, $oper);
  2338. $p_state = parse_snmp_value($c_state);
  2339. $c_admin = get_snmp($ip, $community, $version, $admin);
  2340. $p_admin = parse_snmp_value ($c_admin);
  2341. if ($p_state == 1) { $c_speed = get_snmp($ip, $community, $version, $speed); } else { $c_speed = 'INT:0'; }
  2342. $p_speed = parse_snmp_value($c_speed);
  2343. $c_errors = get_snmp($ip, $community, $version, $errors);
  2344. $p_errors = parse_snmp_value($c_errors);
  2345. $result = $p_state . ";" . $p_admin . ";" . $p_speed . ";" . $p_errors;
  2346. return $result;
  2347. }
  2348. function parse_snmp_value($value) {
  2349. if (empty($value)) { return NULL; }
  2350. list ($p_type, $p_value) = explode(':', $value);
  2351. $p_value = trim($p_value);
  2352. $p_value= preg_replace('/^\"/','',$p_value);
  2353. $p_value= preg_replace('/\"$/','',$p_value);
  2354. $p_value = trim($p_value);
  2355. return $p_value;
  2356. }
  2357. function dec_to_hex($mac)
  2358. {
  2359. if (! isset($mac)) {
  2360. return;
  2361. }
  2362. $mac_array = explode('.', $mac);
  2363. for ($i = 0; $i < count($mac_array); $i ++) {
  2364. $hex_i = dechex($mac_array[$i]);
  2365. if (strlen($hex_i) == 1) {
  2366. $hex_i = "0" . $hex_i;
  2367. }
  2368. $mac_array[$i] = $hex_i;
  2369. }
  2370. $hex_mac = implode(':', $mac_array);
  2371. return $hex_mac;
  2372. }
  2373. function mac_simplify($mac)
  2374. {
  2375. if (! isset($mac)) {
  2376. return;
  2377. }
  2378. $mac = strtolower(trim($mac));
  2379. $mac = preg_replace('/(\.|:|-)/', '', $mac);
  2380. return $mac;
  2381. }
  2382. function mac_dotted($mac)
  2383. {
  2384. if (! isset($mac)) {
  2385. return;
  2386. }
  2387. $mac = mac_simplify($mac);
  2388. $mac = preg_replace('/(\S{2})(\S{2})(\S{2})(\S{2})(\S{2})(\S{2})/', '$1:$2:$3:$4:$5:$6', $mac);
  2389. return $mac;
  2390. }
  2391. function unbind_ports($db, $device_id)
  2392. {
  2393. $target = mysqli_query($db, "SELECT U.target_port_id,U.id FROM device_ports U WHERE U.device_id=$device_id");
  2394. while (list ($target_id, $id) = mysqli_fetch_array($target)) {
  2395. run_sql($db, "UPDATE device_ports SET target_port_id=0 WHERE target_port_id=".$id);
  2396. run_sql($db, "UPDATE device_ports SET target_port_id=0 WHERE id=".$id);
  2397. }
  2398. }
  2399. function bind_ports($db, $port_id, $target_id)
  2400. {
  2401. $old_target = mysqli_query($db, "SELECT U.target_port_id FROM device_ports U WHERE U.id=$port_id");
  2402. list ($old_target_id) = mysqli_fetch_array($old_target);
  2403. // unbind current connection
  2404. $new['target_port_id'] = 0;
  2405. update_record($db, "device_ports", "id='$port_id'", $new);
  2406. if (isset($old_target_id)) {
  2407. update_record($db, "device_ports", "id='$old_target_id'", $new);
  2408. }
  2409. // new link
  2410. if (isset($target_id) and $target_id > 0) {
  2411. $new['target_port_id'] = $target_id;
  2412. update_record($db, "device_ports", "id='$port_id'", $new);
  2413. $new['target_port_id'] = $port_id;
  2414. update_record($db, "device_ports", "id='$target_id'", $new);
  2415. }
  2416. }
  2417. function expand_device_name($db, $name)
  2418. {
  2419. $device_id = get_device_id($db, $name);
  2420. $result = $name;
  2421. if (isset($device_id) and $device_id > 0) {
  2422. $result = '<a href=/admin/devices/editdevice.php?id=' . $device_id . '>' . $name . '</a>';
  2423. }
  2424. return $result;
  2425. }
  2426. function expand_mac($db,$msg)
  2427. {
  2428. if (! isset($msg)) { return; }
  2429. $mac = mac_dotted($msg);
  2430. $vendor_info = get_vendor($db,$mac);
  2431. $result = ' <p title="'.$vendor_info.'"><a href=/admin/logs/mac.php?mac='.$mac.'>'.$mac.'</a></p>';
  2432. return $result;
  2433. }
  2434. function expand_log_str($db, $msg)
  2435. {
  2436. if (! isset($msg)) {
  2437. return;
  2438. }
  2439. $auth_pattern = '/(auth_id:|auth|auth id:|auth id)\s+(\d+)\s+/i';
  2440. $auth_replace = '<a href=/admin/users/editauth.php?id=${2}>auth_id:${2}</a> ';
  2441. $result = preg_replace($auth_pattern, $auth_replace, $msg);
  2442. $user_pattern = '/(user_id:|user|user id:|user id)\s+(\d+)\s+/i';
  2443. $user_replace = '<a href=/admin/users/edituser.php?id=${2}>user_id:${2}</a>';
  2444. $result = preg_replace($user_pattern, $user_replace, $result);
  2445. $mac_pattern = '/\s+\[(\w{12})\]\s+/i';
  2446. preg_match($mac_pattern, $result, $matches);
  2447. if (isset($matches[1])) {
  2448. $mac = $matches[1];
  2449. $mac = mac_dotted($mac);
  2450. # $vendor_info = get_vendor($db,$mac);
  2451. # $mac_replace = ' <p title="'.$vendor_info.'"><a href=/admin/logs/mac.php?mac='.$mac.'>'.$mac.'</a></p>';
  2452. $mac_replace = ' <a href=/admin/logs/mac.php?mac='.$mac.'>'.$mac.'</a> ';
  2453. $result = preg_replace($mac_pattern, $mac_replace, $result);
  2454. }
  2455. $mac_pattern = '/\s+mac:\s+([\w\:]{17})$/i';
  2456. preg_match($mac_pattern, $result, $matches);
  2457. if (isset($matches[1])) {
  2458. $mac = $matches[1];
  2459. $mac = mac_dotted($mac);
  2460. # $vendor_info = get_vendor($db,$mac);
  2461. # $mac_replace = ' mac: <p title="'.$vendor_info.'"><a href=/admin/logs/mac.php?mac='.$mac.'>'.$mac.'</a></p>';
  2462. $mac_replace = ' mac: <a href=/admin/logs/mac.php?mac='.$mac.'>'.$mac.'</a> ';
  2463. $result = preg_replace($mac_pattern, $mac_replace, $result);
  2464. }
  2465. $device_pattern = '/at device\s+([\w\.\-]+)/i';
  2466. preg_match($device_pattern, $result, $matches);
  2467. if (isset($matches[1])) {
  2468. $device_name = $matches[1];
  2469. $device_id = get_device_id($db, $device_name);
  2470. if (isset($device_id) and $device_id > 0) {
  2471. $device_replace = 'at device <a href=/admin/devices/editdevice.php?id=' . $device_id . '>${1}</a>';
  2472. $result = preg_replace($device_pattern, $device_replace, $result);
  2473. }
  2474. }
  2475. $device_pattern = '/(device_id:|device id:|device id|device_id)\s+(\d+)\s+/i';
  2476. $device_replace = 'device_id: <a href=/admin/devices/editdevice.php?id=${2}>${2}</a> ';
  2477. $result = preg_replace($device_pattern, $device_replace, $result);
  2478. $ip_pattern = '/\s+ip\:\s+(\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})\s+/i';
  2479. preg_match($ip_pattern, $result, $matches);
  2480. if (isset($matches[1])) {
  2481. $ip = $matches[1];
  2482. $auth_id = get_auth_by_ip($db, $ip);
  2483. if (isset($auth_id) and $auth_id > 0) {
  2484. $auth_replace = ' ip: <a href=/admin/users/editauth.php?id=' . $auth_id . '>${1}</a> ';
  2485. $result = preg_replace($ip_pattern, $auth_replace, $result);
  2486. }
  2487. }
  2488. return $result;
  2489. }
  2490. function get_record_field($db, $table, $field, $filter)
  2491. {
  2492. if (! isset($table)) {
  2493. LOG_ERROR($db, "Search in unknown table! Skip command.");
  2494. return;
  2495. }
  2496. if (! isset($filter)) {
  2497. LOG_ERROR($db, "Search filter is empty! Skip command.");
  2498. return;
  2499. }
  2500. if (! isset($field)) {
  2501. LOG_ERROR($db, "Search field is empty! Skip command.");
  2502. return;
  2503. }
  2504. if (preg_match('/=$/', $filter)) {
  2505. LOG_ERROR($db, "Search record ($table) with illegal filter $filter! Skip command.");
  2506. return;
  2507. }
  2508. $old_sql = "SELECT $field FROM $table WHERE $filter LIMIT 1";
  2509. $old_record = mysqli_query($db, $old_sql) or LOG_ERROR($db, "SQL: $old_sql :".mysqli_error($db));
  2510. $old = mysqli_fetch_array($old_record, MYSQLI_ASSOC);
  2511. foreach ($old as $key => $value) {
  2512. if (! isset($value) or $value==='NULL') { $value = ''; }
  2513. $result[$key] = $value;
  2514. }
  2515. return $result[$field];
  2516. }
  2517. function get_record($db, $table, $filter)
  2518. {
  2519. if (! isset($table)) {
  2520. LOG_ERROR($db, "Search in unknown table! Skip command.");
  2521. return;
  2522. }
  2523. if (! isset($filter)) {
  2524. LOG_ERROR($db, "Search filter is empty! Skip command.");
  2525. return;
  2526. }
  2527. if (preg_match('/=$/', $filter)) {
  2528. LOG_ERROR($db, "Search record ($table) with illegal filter $filter! Skip command.");
  2529. return;
  2530. }
  2531. $old_sql = "SELECT * FROM $table WHERE $filter LIMIT 1";
  2532. $old_record = mysqli_query($db, $old_sql) or LOG_ERROR($db, "SQL: $old_sql :".mysqli_error($db));
  2533. $old = mysqli_fetch_array($old_record, MYSQLI_ASSOC);
  2534. $result = NULL;
  2535. if (!empty($old)) {
  2536. foreach ($old as $key => $value) {
  2537. if (!isset($value) or $value==='NULL') { $value = ''; }
  2538. if (!empty($key)) { $result[$key] = $value; }
  2539. }
  2540. }
  2541. return $result;
  2542. }
  2543. function get_records($db, $table, $filter)
  2544. {
  2545. if (! isset($table)) {
  2546. LOG_ERROR($db, "Search in unknown table! Skip command.");
  2547. return;
  2548. }
  2549. if (isset($filter) and preg_match('/=$/', $filter)) {
  2550. LOG_ERROR($db, "Search record ($table) with illegal filter $filter! Skip command.");
  2551. return;
  2552. }
  2553. $s_filter='';
  2554. if (isset($filter)) { $s_filter = 'WHERE '.$filter; }
  2555. $old_sql = "SELECT * FROM $table $s_filter";
  2556. $old_record = mysqli_query($db, $old_sql) or LOG_ERROR($db, "SQL: $old_sql :".mysqli_error($db));
  2557. $result = NULL;
  2558. $index = 0;
  2559. while ($old = mysqli_fetch_array($old_record, MYSQLI_ASSOC)) {
  2560. foreach ($old as $key => $value) {
  2561. if (! isset($value) or $value==='NULL') { $value = ''; }
  2562. $result[$index][$key] = $value;
  2563. }
  2564. $index++;
  2565. }
  2566. return $result;
  2567. }
  2568. function get_records_sql($db, $sql)
  2569. {
  2570. if (empty($sql)) {
  2571. LOG_ERROR($db, "Empty query! Skip command.");
  2572. return;
  2573. }
  2574. $record = mysqli_query($db, $sql) or LOG_ERROR($db, "SQL: $sql :".mysqli_error($db));
  2575. $index = 0;
  2576. $result = NULL;
  2577. while ($rec = mysqli_fetch_array($record, MYSQLI_ASSOC)) {
  2578. foreach ($rec as $key => $value) {
  2579. if (! isset($value) or $value==='NULL') { $value = ''; }
  2580. if (!empty($key)) { $result[$index][$key] = $value; }
  2581. }
  2582. $index++;
  2583. }
  2584. return $result;
  2585. }
  2586. function get_record_sql($db, $sql)
  2587. {
  2588. $result = NULL;
  2589. if (! isset($sql)) {
  2590. LOG_ERROR($db, "Empty query! Skip command.");
  2591. return $result;
  2592. }
  2593. $record = mysqli_query($db, $sql." LIMIT 1") or LOG_ERROR($db, "SQL: $sql LIMIT 1: ".mysqli_error($db));
  2594. if (!isset($record)) { return $result; }
  2595. $rec = mysqli_fetch_array($record, MYSQLI_ASSOC);
  2596. if (!empty($rec)) {
  2597. foreach ($rec as $key => $value) {
  2598. if (! isset($value) or $value==='NULL') { $value = ''; }
  2599. $result[$key] = $value;
  2600. }
  2601. }
  2602. return $result;
  2603. }
  2604. function is_auth_bind_changed ($db, $id, $ip,$mac) {
  2605. $old_sql = "SELECT ip,mac FROM User_auth WHERE id=$id";
  2606. $old_record = get_record_sql($db, $old_sql);
  2607. if (empty($old_record["ip"]) or empty($old_record["mac"])) { return 0; }
  2608. if ($old_record["ip"] !== $ip or $old_record["mac"] !== $mac) {
  2609. LOG_VERBOSE($db, "Changed ip or mac for auth record!");
  2610. return 1;
  2611. }
  2612. return 0;
  2613. }
  2614. function copy_auth($db, $id, $new_auth) {
  2615. $old_record = get_record_sql($db, "SELECT * FROM User_auth WHERE id=$id");
  2616. delete_record($db,"User_auth","id=".$id);
  2617. $new_auth["user_id"] = $old_record["user_id"];
  2618. $new_auth["changed"] = 1;
  2619. $changed_time = GetNowTimeString();
  2620. $new_auth["changed_time"]=$changed_time;
  2621. $new_id = insert_record($db,"User_auth",$new_auth);
  2622. LOG_VERBOSE($db, "Old record with id: $id deleted. Created new auth record for new ip+mac id: $new_id!");
  2623. return $new_id;
  2624. }
  2625. function update_record($db, $table, $filter, $newvalue)
  2626. {
  2627. if (isRO($db,$table)) {
  2628. LOG_ERROR($db, "User does not have write permission");
  2629. return;
  2630. }
  2631. if (! isset($table)) {
  2632. LOG_ERROR($db, "Change record for unknown table! Skip command.");
  2633. return;
  2634. }
  2635. if (! isset($filter)) {
  2636. LOG_ERROR($db, "Change record ($table) with empty filter! Skip command.");
  2637. return;
  2638. }
  2639. if (preg_match('/=$/', $filter)) {
  2640. LOG_ERROR($db, "Change record ($table) with illegal filter $filter! Skip command.");
  2641. return;
  2642. }
  2643. if (! isset($newvalue)) {
  2644. LOG_ERROR($db, "Change record ($table [ $filter ]) with empty data! Skip command.");
  2645. return;
  2646. }
  2647. $old_sql = "SELECT * FROM $table WHERE $filter";
  2648. $old_record = mysqli_query($db, $old_sql) or LOG_ERROR($db, "SQL: $old_sql :".mysqli_error($db));
  2649. $old = mysqli_fetch_array($old_record, MYSQLI_ASSOC);
  2650. $changed_log = '';
  2651. $run_sql = '';
  2652. $network_changed = 0;
  2653. $acl_fields = [
  2654. 'ip' => '1',
  2655. 'ip_int' => '1',
  2656. 'enabled'=>'1',
  2657. 'dhcp'=>'1',
  2658. 'filter_group_id'=>'1',
  2659. 'deleted'=>'1',
  2660. 'dhcp_acl'=>'1',
  2661. 'queue_id'=>'1',
  2662. 'mac'=>'1',
  2663. 'blocked'=>'1',
  2664. ];
  2665. foreach ($newvalue as $key => $value) {
  2666. if (! isset($value)) {
  2667. $value = '';
  2668. }
  2669. $value = trim($value);
  2670. if (strcmp($old[$key], $value) == 0) {
  2671. continue;
  2672. }
  2673. if ($table==="User_auth") {
  2674. if (!empty($acl_fields["$key"])) { $network_changed = 1; }
  2675. }
  2676. $changed_log = $changed_log . " $key => $value (old: $old[$key]),";
  2677. $run_sql = $run_sql . " `" . $key . "`='" . mysqli_real_escape_string($db, $value) . "',";
  2678. }
  2679. if (empty($run_sql)) { return; }
  2680. if ($network_changed) { $run_sql = $run_sql . " `changed`='1',"; }
  2681. $changed_log = substr_replace($changed_log, "", - 1);
  2682. $run_sql = substr_replace($run_sql, "", - 1);
  2683. if ($table === 'User_auth') {
  2684. $changed_time = GetNowTimeString();
  2685. $run_sql = $run_sql . ", `changed_time`='".$changed_time."'";
  2686. }
  2687. $new_sql = "UPDATE $table SET $run_sql WHERE $filter";
  2688. LOG_DEBUG($db, "Run sql: $new_sql");
  2689. $sql_result = mysqli_query($db, $new_sql) or LOG_ERROR($db, "SQL: $new_sql :".mysqli_error($db));
  2690. if (!$sql_result) { LOG_ERROR($db, "UPDATE Request: $new_sql :".mysqli_error($db)); return; }
  2691. LOG_VERBOSE($db, "Change table $table WHERE $filter set $changed_log");
  2692. }
  2693. function delete_record($db, $table, $filter)
  2694. {
  2695. if (isRO($db,$table)) {
  2696. LOG_ERROR($db, "User does not have write permission");
  2697. return;
  2698. }
  2699. if (! isset($table)) {
  2700. LOG_ERROR($db, "Delete FROM unknown table! Skip command.");
  2701. return;
  2702. }
  2703. if (! isset($filter)) {
  2704. LOG_ERROR($db, "Delete FROM table $table with empty filter! Skip command.");
  2705. return;
  2706. }
  2707. if (preg_match('/=$/', $filter)) {
  2708. LOG_ERROR($db, "Change record ($table) with illegal filter $filter! Skip command.");
  2709. return;
  2710. }
  2711. $old_sql = "SELECT * FROM $table WHERE $filter";
  2712. $old_record = mysqli_query($db, $old_sql) or LOG_ERROR($db, "SQL: $old_sql :".mysqli_error($db));
  2713. $old = mysqli_fetch_array($old_record, MYSQLI_ASSOC);
  2714. $changed_log = 'record: ';
  2715. if (!empty($old)) {
  2716. foreach ($old as $key => $value) {
  2717. if (! isset($value)) { $value = ''; }
  2718. $changed_log = $changed_log . " $key => $value,";
  2719. }
  2720. }
  2721. //never delete user ip record
  2722. if ($table === 'User_auth') {
  2723. $changed_time = GetNowTimeString();
  2724. $new_sql = "UPDATE $table SET deleted=1, changed=1, `changed_time`='".$changed_time."' WHERE $filter";
  2725. LOG_DEBUG($db, "Run sql: $new_sql");
  2726. $sql_result = mysqli_query($db, $new_sql) or LOG_ERROR($db, "SQL: $new_sql :".mysqli_error($db));
  2727. if (!$sql_result) { LOG_ERROR($db, "UPDATE Request (from delete): ".mysqli_error($db)); return; }
  2728. } else {
  2729. $new_sql = "DELETE FROM $table WHERE $filter";
  2730. LOG_DEBUG($db, "Run sql: $new_sql");
  2731. $sql_result = mysqli_query($db, $new_sql) or LOG_ERROR($db, "SQL: $new_sql :".mysqli_error($db));
  2732. if (!$sql_result) { LOG_ERROR($db, "DELETE Request: $new_sql : ".mysqli_error($db)); return; }
  2733. }
  2734. LOG_VERBOSE($db, "Delete FROM table $table WHERE $filter $changed_log");
  2735. }
  2736. function insert_record($db, $table, $newvalue)
  2737. {
  2738. if (isRO($db,$table)) {
  2739. LOG_ERROR($db, "User does not have write permission");
  2740. return;
  2741. }
  2742. if (! isset($table)) {
  2743. LOG_ERROR($db, "Create record for unknown table! Skip command.");
  2744. return;
  2745. }
  2746. if (empty($newvalue)) {
  2747. LOG_ERROR($db, "Create record ($table) with empty data! Skip command.");
  2748. return;
  2749. }
  2750. $changed_log = '';
  2751. $field_list = '';
  2752. $value_list = '';
  2753. foreach ($newvalue as $key => $value) {
  2754. if (empty($value) and $value !== 0) { $value = ''; }
  2755. $changed_log = $changed_log . " $key => $value,";
  2756. $field_list = $field_list . "`" . $key . "`,";
  2757. $value = trim($value);
  2758. $value_list = $value_list . "'" . mysqli_real_escape_string($db, $value) . "',";
  2759. }
  2760. if (empty($value_list)) {
  2761. return;
  2762. }
  2763. $changed_log = substr_replace($changed_log, "", - 1);
  2764. $field_list = substr_replace($field_list, "", - 1);
  2765. $value_list = substr_replace($value_list, "", - 1);
  2766. $new_sql = "insert into $table(" . $field_list . ") values(" . $value_list . ")";
  2767. LOG_DEBUG($db, "Run sql: $new_sql");
  2768. $sql_result = mysqli_query($db, $new_sql) or LOG_ERROR($db, "SQL: $new_sql :".mysqli_error($db));
  2769. if (!$sql_result) { LOG_ERROR($db, "INSERT Request:".mysqli_error($db)); return; }
  2770. $last_id = mysqli_insert_id($db);
  2771. LOG_VERBOSE($db, "Create record in table $table: $changed_log with id: $last_id");
  2772. if ($table === 'User_auth') { run_sql($db,"UPDATE User_auth SET changed=1 WHERE id=".$last_id); }
  2773. return $last_id;
  2774. }
  2775. function get_rec_str ($array) {
  2776. $result='';
  2777. foreach ($array as $key => $value) { $result .= "[".$key."]=".$value.", "; }
  2778. $result = preg_replace('/,\s+$/','',$result);
  2779. return $result;
  2780. }
  2781. function get_diff_rec($db, $table, $filter, $newvalue, $only_changed)
  2782. {
  2783. if (! isset($table)) {
  2784. return;
  2785. }
  2786. if (! isset($filter)) {
  2787. return;
  2788. }
  2789. if (! isset($newvalue)) {
  2790. return;
  2791. }
  2792. if (!isset($only_changed)) { $only_changed=0; }
  2793. $old_sql = "SELECT * FROM $table WHERE $filter";
  2794. $old_record = mysqli_query($db, $old_sql) or LOG_ERROR($db, "SQL: $old_sql :".mysqli_error($db));
  2795. $old = mysqli_fetch_array($old_record, MYSQLI_ASSOC);
  2796. $changed_log = "\r\n";
  2797. foreach ($newvalue as $key => $value) {
  2798. if (strcmp($old[$key], $value) !== 0) {
  2799. $changed_log = $changed_log . " $key => cur: $value old: $old[$key],\r\n";
  2800. }
  2801. }
  2802. $old_record = '';
  2803. if (!$only_changed) {
  2804. $old_record = "\r\n Не изменялось:\r\n";
  2805. foreach ($old as $key => $value) {
  2806. if (!empty($newvalue[$key])) { $old_record = $old_record . " $key = $value,\r\n"; }
  2807. }
  2808. $old_record = substr_replace($old_record, "", -3);
  2809. }
  2810. // print $changed_log;
  2811. return $changed_log.$old_record;
  2812. }
  2813. function get_cacti_graph($host_ip, $port_index)
  2814. {
  2815. if (empty(get_const('cacti_url'))) { return; }
  2816. if (CACTI_DB_HOST ==null or CACTI_DB_USER == null or CACTI_DB_PASS==null or CACTI_DB_NAME==null) { return; }
  2817. if (empty(CACTI_DB_HOST) or empty(CACTI_DB_USER) or empty(CACTI_DB_PASS) or empty(CACTI_DB_NAME)) { return; }
  2818. $cacti_db_link = mysqli_connect(CACTI_DB_HOST, CACTI_DB_USER, CACTI_DB_PASS, CACTI_DB_NAME);
  2819. if (! $cacti_db_link) {
  2820. echo "Ошибка: Невозможно установить соединение с MySQL with ".CACTI_DB_HOST." [".CACTI_DB_NAME."] for ".CACTI_DB_USER." ". PHP_EOL;
  2821. echo "Код ошибки errno: " . mysqli_connect_errno() . PHP_EOL;
  2822. echo "Текст ошибки error: " . mysqli_connect_error() . PHP_EOL;
  2823. return FALSE;
  2824. }
  2825. $host_sql = 'SELECT * FROM host WHERE hostname="' . $host_ip . '"';
  2826. $cacti_host = get_record_sql($cacti_db_link,$host_sql);
  2827. $host_id = $cacti_host["id"];
  2828. if (empty($host_id)) { return; }
  2829. $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';
  2830. $cacti_graph = get_record_sql($cacti_db_link,$graph_sql);
  2831. $graph_id = $cacti_graph["id"];
  2832. if (empty($graph_id)) { return; }
  2833. $result = get_const('cacti_url') . "/graph_image.php?local_graph_id=" . $graph_id;
  2834. return $result;
  2835. }
  2836. function print_select_item ($description,$value,$current) {
  2837. if ((string)$value === (string)$current) {
  2838. print "<option value=$value selected>$description</option>";
  2839. } else {
  2840. print "<option value=$value>$description</option>";
  2841. }
  2842. }
  2843. function print_select_simple ($description,$value) {
  2844. print "<option value=$value>$description</option>";
  2845. }
  2846. function print_select_item_ext ($description,$value,$current,$disabled) {
  2847. if ((string)$value === (string)$current) { print "<option value=$value selected>$description</option>"; }
  2848. else {
  2849. if (!$disabled) { print "<option value=$value>$description</option>"; } else { print "<option disabled value=$value>$description</option>"; }
  2850. }
  2851. }
  2852. function print_row_at_pages ($name,$value) {
  2853. print "<select name='".$name."'>\n";
  2854. print_select_item(WEB_select_item_more,pow(10,10),$value);
  2855. print_select_item('25',25,$value);
  2856. print_select_item('50',50,$value);
  2857. print_select_item('100',100,$value);
  2858. print_select_item('200',200,$value);
  2859. print_select_item('500',500,$value);
  2860. print_select_item('1000',1000,$value);
  2861. print_select_item('2000',2000,$value);
  2862. print "</select>\n";
  2863. }
  2864. function print_navigation($url,$page,$displayed,$count_records,$total) {
  2865. if ($total<=1) { return; }
  2866. #две назад
  2867. print "<div align=left class=records >";
  2868. if(($page-2)>0):
  2869. $pagetwoleft="<a class='first_page_link' href=".$url."?page=".($page-2).">".($page-2)."</a> ";
  2870. else:
  2871. $pagetwoleft=null;
  2872. endif;
  2873. #одна назад
  2874. if(($page-1)>0):
  2875. $pageoneleft="<a class='first_page_link' href=".$url."?page=".($page-1).">".($page-1)."</a> ";
  2876. $pagetemp=($page-1);
  2877. else:
  2878. $pageoneleft=null;
  2879. $pagetemp=null;
  2880. endif;
  2881. #две вперед
  2882. if(($page+2)<=$total):
  2883. $pagetworight=" <a class='first_page_link' href=".$url."?page=".($page+2).">".($page+2)."</a>";
  2884. else:
  2885. $pagetworight=null;
  2886. endif;
  2887. #одна вперед
  2888. if(($page+1)<=$total):
  2889. $pageoneright=" <a class='first_page_link' href=".$url."?page=".($page+1).">".($page+1)."</a>";
  2890. $pagetemp2=($page+1);
  2891. else:
  2892. $pageoneright=null;
  2893. $pagetemp2=null;
  2894. endif;
  2895. # в начало
  2896. if($page!=1 && $pagetemp!=1 && $pagetemp!=2):
  2897. $pagerevp="<a href=".$url."?page=1 class='first_page_link' title='В начало'><<</a> ";
  2898. else:
  2899. $pagerevp=null;
  2900. endif;
  2901. #в конец (последняя)
  2902. if($page!=$total && $pagetemp2!=($total-1) && $pagetemp2!=$total):
  2903. $nextp=" ... <a href=".$url."?page=".$total." class='first_page_link'>$total</a>";
  2904. else:
  2905. $nextp=null;
  2906. endif;
  2907. print $pagerevp.$pagetwoleft.$pageoneleft.'<span class="num_page_not_link"><b>'.$page.'</b></span>'.$pageoneright.$pagetworight.$nextp;
  2908. print " | Total records: $count_records";
  2909. print "</div>";
  2910. }
  2911. function get_option($db, $option_id)
  2912. {
  2913. $option = get_record($db, "config", "option_id=".$option_id);
  2914. if (empty($option) or empty($option['value'])) {
  2915. $default = get_record($db, "config_options","id=$option_id");
  2916. return $default['default_value'];
  2917. }
  2918. return $option['value'];
  2919. }
  2920. function is_option($db, $option_id)
  2921. {
  2922. $option = get_record($db, "config", "option_id=".$option_id);
  2923. if (empty($option) or empty($option['value'])) { return; }
  2924. return 1;
  2925. }
  2926. function set_option($db, $option_id, $value)
  2927. {
  2928. $option['value'] = $value;
  2929. update_record($db, 'config', "option_id=$option_id", $option);
  2930. }
  2931. function is_subnet_aton($subnet,$ip) {
  2932. if (!isset($subnet)) { return 0; }
  2933. if (!isset($ip)) { return 0; }
  2934. $range = cidrToRange($subnet);
  2935. if ($ip>=ip2long($range[0]) and $ip <=ip2long($range[1])) { return 1; }
  2936. return 0;
  2937. }
  2938. function get_new_user_id($db, $ip, $mac, $hostname)
  2939. {
  2940. $result['ip']=$ip;
  2941. $result['mac']=mac_dotted($mac);
  2942. $result['hostname']=$hostname;
  2943. $result['user_id']=NULL;
  2944. $result['ou_id']=NULL;
  2945. $ip_aton = ip2long($ip);
  2946. //personal user rules
  2947. //ip
  2948. if (!empty($ip)) {
  2949. $t_rules = get_records_sql($db, "SELECT * FROM auth_rules WHERE type=1 and LENGTH(rule)>0 AND user_id IS NOT NULL");
  2950. foreach ($t_rules as $row) {
  2951. if (!empty($row['rule']) and is_subnet_aton($row['rule'],$ip_aton)) { $result['user_id']=$row['user_id']; }
  2952. }
  2953. }
  2954. //mac
  2955. if (!empty($mac)) {
  2956. $mac_rules=get_records_sql($db,"SELECT * FROM auth_rules WHERE type=2 AND LENGTH(rule)>0 AND user_id IS NOT NULL");
  2957. foreach ($mac_rules as $row) {
  2958. $pattern = '/'.mac_simplify($row['rule']).'/';
  2959. if (!empty($row['rule']) and preg_match($pattern, mac_simplify($mac))) { $result['user_id']=$row['user_id']; }
  2960. }
  2961. }
  2962. //hostname
  2963. if (!empty($hostname)) {
  2964. $mac_rules=get_records_sql($db,"SELECT * FROM auth_rules WHERE type=3 AND LENGTH(rule)>0 AND user_id IS NOT NULL");
  2965. foreach ($mac_rules as $row) {
  2966. if (!empty($row['rule']) and preg_match($row['rule'], $hostname)) { $result['user_id']=$row['user_id']; }
  2967. }
  2968. }
  2969. if (!empty($result['user_id'])) { return $result; }
  2970. //ou rules
  2971. //ip
  2972. if (!empty($ip)) {
  2973. if (is_hotspot($db, $ip)) { $result['ou_id']=get_const('default_hotspot_ou_id'); }
  2974. $t_rules = get_records_sql($db, "SELECT * FROM auth_rules WHERE type=1 and LENGTH(rule)>0 AND ou_id IS NOT NULL");
  2975. foreach ($t_rules as $row) {
  2976. if (!empty($row['rule']) and is_subnet_aton($row['rule'],$ip_aton)) { $result['ou_id']=$row['ou_id']; }
  2977. }
  2978. }
  2979. //mac
  2980. if (!empty($mac)) {
  2981. $mac_rules=get_records_sql($db,"SELECT * FROM auth_rules WHERE type=2 AND LENGTH(rule)>0 AND ou_id IS NOT NULL");
  2982. foreach ($mac_rules as $row) {
  2983. $pattern = '/'.mac_simplify($row['rule']).'/';
  2984. if (!empty($row['rule']) and preg_match($pattern, mac_simplify($mac))) { $result['ou_id']=$row['ou_id']; }
  2985. }
  2986. }
  2987. //hostname
  2988. if (!empty($hostname)) {
  2989. $mac_rules=get_records_sql($db,"SELECT * FROM auth_rules WHERE type=3 AND LENGTH(rule)>0 AND ou_id IS NOT NULL");
  2990. foreach ($mac_rules as $row) {
  2991. if (!empty($row['rule']) and preg_match($row['rule'], $hostname)) { $result['ou_id']=$row['ou_id']; }
  2992. }
  2993. }
  2994. if (empty($result['ou_id'])) { $result['ou_id']=get_const('default_user_ou_id'); }
  2995. return $result;
  2996. }
  2997. function get_subnet_range($db,$subnet_id) {
  2998. if (empty($subnet_id)) { return; }
  2999. $t_option = get_record_sql($db, "SELECT ip_int_start,ip_int_stop FROM `subnets` WHERE id=$subnet_id");
  3000. if (!isset($t_option['ip_int_start'])) { $t_option['ip_int_start']=0; }
  3001. if (!isset($t_option['ip_int_stop'])) { $t_option['ip_int_stop']=0; }
  3002. $subnet['start']=$t_option['ip_int_start'];
  3003. $subnet['stop']=$t_option['ip_int_stop'];
  3004. return $subnet;
  3005. }
  3006. function is_hotspot($db, $ip)
  3007. {
  3008. if (! isset($ip)) { return 0; }
  3009. LOG_DEBUG($db,"Check hotspot network for ip: $ip");
  3010. $ip_aton = ip2long($ip);
  3011. $t_option = mysqli_query($db, "SELECT subnet,ip_int_start,ip_int_stop FROM `subnets` WHERE hotspot=1");
  3012. while (list ($f_net,$f_start,$f_stop) = mysqli_fetch_array($t_option)) {
  3013. if ($ip_aton >= $f_start and $ip_aton <= $f_stop) {
  3014. LOG_DEBUG($db,"ip: $ip [$ip_aton] found in network $f_net: [".$f_start."..".$f_stop."]");
  3015. return 1;
  3016. }
  3017. }
  3018. LOG_DEBUG($db,"ip $ip not found in hotspot network!");
  3019. return 0;
  3020. }
  3021. function is_office($db, $ip)
  3022. {
  3023. if (! isset($ip)) { return 0; }
  3024. LOG_DEBUG($db,"Check office network for ip: $ip");
  3025. $ip_aton = ip2long($ip);
  3026. $t_option = mysqli_query($db, "SELECT subnet,ip_int_start,ip_int_stop FROM `subnets` WHERE office=1");
  3027. while (list ($f_net,$f_start,$f_stop) = mysqli_fetch_array($t_option)) {
  3028. if ($ip_aton >= $f_start and $ip_aton <= $f_stop) {
  3029. LOG_DEBUG($db,"ip: $ip [$ip_aton] found in office $f_net: [".$f_start."..".$f_stop."]");
  3030. return 1;
  3031. }
  3032. }
  3033. LOG_DEBUG($db,"ip $ip not found in office network!");
  3034. return 0;
  3035. }
  3036. function is_our_network($db, $ip)
  3037. {
  3038. if (! isset($ip)) { return 0; }
  3039. if (is_hotspot($db, $ip)) { return 1; }
  3040. if (is_office($db, $ip)) { return 1; }
  3041. return 0;
  3042. }
  3043. function get_const($const_name) {
  3044. global $config;
  3045. if (isset($config[$const_name])) { return $config[$const_name]; }
  3046. return NULL;
  3047. }
  3048. $config["org_name"]= get_option($db_link, 32);
  3049. $config["KB"]= get_option($db_link, 1);
  3050. if ($config["KB"] == 0) { $config["KB"]=1000; }
  3051. if ($config["KB"] == 1) { $config["KB"]=1024; }
  3052. $config["debug"]= get_option($db_link, 34);
  3053. $config["log_level"]= get_option($db_link, 53);
  3054. if ($config["debug"]) { $config["log_level"]= 255; }
  3055. $config["send_email"]= get_option($db_link, 51);
  3056. $config["admin_email"]= get_option($db_link, 21);
  3057. $config["sender_email"]= get_option($db_link, 52);
  3058. $config["mac_discovery"]= get_option($db_link, 17);
  3059. $config["snmp_default_version"]= get_option($db_link, 9);
  3060. $config["snmp_default_community"]= get_option($db_link, 11);
  3061. $config["auto_mac_rule"]= get_option($db_link, 64);
  3062. $config["cacti_url"]= rtrim(get_option($db_link, 58),'/');
  3063. if (preg_match('/127.0.0.1/', $config["cacti_url"])) { $config["cacti_url"]=NULL; }
  3064. $config["nagios_url"]= rtrim(get_option($db_link, 57),'/').'/cgi-bin/';
  3065. if (preg_match('/127.0.0.1/', $config["nagios_url"])) { $config["nagios_url"]=NULL; }
  3066. $config["torrus_url"]= rtrim(get_option($db_link, 59),'/').'?nodeid=if//HOST_IP//IF_NAME////inoutbps';
  3067. if (preg_match('/127.0.0.1/', $config["torrus_url"])) { $config["torrus_url"]=NULL; }
  3068. $ou=get_record_sql($db_link,"SELECT id FROM OU WHERE default_users = 1");
  3069. if (empty($ou)) { $config["default_user_ou_id"]=0; } else { $config["default_user_ou_id"]= $ou['id']; }
  3070. $ou=get_record_sql($db_link,"SELECT id FROM OU WHERE default_hotspot=1");
  3071. if (empty($ou)) { $config["default_hotspot_ou_id"]= $config["default_user_ou_id"]; } else { $config["default_hotspot_ou_id"]=$ou['id']; }
  3072. $config["init"]=1;
  3073. clean_dns_cache($db_link);
  3074. snmp_set_valueretrieval(SNMP_VALUE_LIBRARY);
  3075. snmp_set_enum_print(1);
  3076. ?>