common.php 126 KB

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