common.php 116 KB

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