mysql.pm 38 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188
  1. package Rstat::mysql;
  2. #
  3. # Copyright (C) Roman Dmitiriev, rnd@rajven.ru
  4. #
  5. use strict;
  6. use English;
  7. use FindBin '$Bin';
  8. use lib "$Bin";
  9. use base 'Exporter';
  10. use vars qw(@EXPORT @ISA);
  11. use Rstat::config;
  12. use Rstat::main;
  13. use Net::Patricia;
  14. use Rstat::net_utils;
  15. use Data::Dumper;
  16. use DateTime;
  17. use POSIX;
  18. use DBI;
  19. our @ISA = qw(Exporter);
  20. our @EXPORT = qw(
  21. batch_db_sql
  22. db_log_warning
  23. db_log_debug
  24. db_log_error
  25. db_log_info
  26. db_log_verbose
  27. delete_record
  28. do_sql
  29. Get_Variable
  30. Set_Variable
  31. Del_Variable
  32. get_count_records
  33. get_record_sql
  34. get_records_sql
  35. get_device_by_ip
  36. get_diff_rec
  37. get_id_record
  38. get_new_user_id
  39. is_hotspot
  40. GetNowTime
  41. GetUnixTimeByStr
  42. GetTimeStrByUnixTime
  43. get_option
  44. get_subnets_ref
  45. init_db
  46. init_option
  47. insert_record
  48. IpToStr
  49. resurrection_auth
  50. new_auth
  51. StrToIp
  52. update_dns_record
  53. update_ad_hostname
  54. update_record
  55. write_db_log
  56. set_changed
  57. recalc_quotes
  58. clean_variables
  59. $add_rules
  60. $L_WARNING
  61. $L_INFO
  62. $L_DEBUG
  63. $L_ERROR
  64. $L_VERBOSE
  65. );
  66. BEGIN
  67. {
  68. #---------------------------------------------------------------------------------------------------------------
  69. our $add_rules;
  70. our $L_ERROR = 0;
  71. our $L_WARNING = 1;
  72. our $L_INFO = 2;
  73. our $L_VERBOSE = 3;
  74. our $L_DEBUG = 255;
  75. our %acl_fields = (
  76. 'ip' => '1',
  77. 'ip_int' => '1',
  78. 'enabled'=>'1',
  79. 'dhcp'=>'1',
  80. 'filter_group_id'=>'1',
  81. 'deleted'=>'1',
  82. 'dhcp_acl'=>'1',
  83. 'queue_id'=>'1',
  84. 'mac'=>'1',
  85. 'blocked'=>'1'
  86. );
  87. #---------------------------------------------------------------------------------------------------------------
  88. sub StrToIp{
  89. return unpack('N',pack('C4',split(/\./,$_[0])));
  90. }
  91. #---------------------------------------------------------------------------------------------------------------
  92. sub IpToStr{
  93. my $nIP = shift;
  94. my $res = (($nIP>>24) & 255) .".". (($nIP>>16) & 255) .".". (($nIP>>8) & 255) .".". ($nIP & 255);
  95. return $res;
  96. }
  97. #---------------------------------------------------------------------------------------------------------------
  98. sub batch_db_sql {
  99. my $db=shift;
  100. my $batch_sql=shift;
  101. return if (!$db);
  102. $db->{AutoCommit} = 0;
  103. my $apply = 0;
  104. my $sth;
  105. if (ref($batch_sql) eq 'ARRAY') {
  106. foreach my $sSQL (@$batch_sql) {
  107. next if (!$sSQL);
  108. print "$sSQL\n";
  109. $sth = $db->prepare($sSQL);
  110. $sth->execute;
  111. $apply = 1;
  112. }
  113. } else {
  114. my @msg = split("\n",$batch_sql);
  115. foreach my $sSQL (@msg) {
  116. next if (!$sSQL);
  117. $sth = $db->prepare($sSQL);
  118. $sth->execute;
  119. $apply = 1;
  120. }
  121. }
  122. if ($apply) { $sth->finish; }
  123. $db->{AutoCommit} = 1;
  124. }
  125. #---------------------------------------------------------------------------------------------------------------
  126. sub do_sql {
  127. my $db=shift;
  128. my $sql=shift;
  129. return if (!$db);
  130. return if (!$sql);
  131. if ($sql!~/^select /i) { db_log_debug($db,$sql); }
  132. my $sql_prep = $db->prepare($sql);
  133. my $sql_ref;
  134. if ( !defined $sql_prep ) { die "Cannot prepare statement: $DBI::errstr\n"; }
  135. $sql_prep->execute;
  136. if ($sql=~/^insert/i) { $sql_ref = $sql_prep->{mysql_insertid}; }
  137. if ($sql=~/^select /i) { $sql_ref = $sql_prep->fetchall_arrayref(); };
  138. $sql_prep->finish();
  139. return $sql_ref;
  140. }
  141. #---------------------------------------------------------------------------------------------------------------
  142. sub write_db_log {
  143. my $db=shift;
  144. my $msg=shift;
  145. my $level = shift || $L_VERBOSE;
  146. my $auth_id = shift || 0;
  147. return if (!$db);
  148. return if (!$msg);
  149. $msg=~s/[\'\"]//g;
  150. my $db_log = 0;
  151. my $history_sql="INSERT INTO syslog(customer,message,level,auth_id) VALUES(".$db->quote($MY_NAME).",".$db->quote($msg).",$level,$auth_id)";
  152. if ($level eq $L_ERROR and $log_level >= $L_ERROR) { log_error($msg); $db_log = 1; }
  153. if ($level eq $L_WARNING and $log_level >= $L_WARNING) { log_warning($msg); $db_log = 1; }
  154. if ($level eq $L_INFO and $log_level >= $L_INFO) { log_info($msg); $db_log = 1; }
  155. if ($level eq $L_DEBUG and $log_level >= $L_DEBUG) { log_debug($msg); $db_log = 1; }
  156. if ($db_log) {
  157. my $history_rf=$db->prepare($history_sql);
  158. $history_rf->execute;
  159. }
  160. }
  161. #---------------------------------------------------------------------------------------------------------------
  162. sub db_log_debug {
  163. my $db = shift;
  164. my $msg = shift;
  165. my $id = shift;
  166. if ($debug) { write_db_log($db,$msg,$L_DEBUG,$id); }
  167. }
  168. #---------------------------------------------------------------------------------------------------------------
  169. sub db_log_error {
  170. my $db = shift;
  171. my $msg = shift;
  172. if ($log_level >= $L_ERROR) {
  173. sendEmail("ERROR! ".substr($msg,0,30),$msg,1);
  174. write_db_log($db,$msg,$L_ERROR);
  175. }
  176. }
  177. #---------------------------------------------------------------------------------------------------------------
  178. sub db_log_info {
  179. my $db = shift;
  180. my $msg = shift;
  181. if ($log_level >= $L_INFO) { write_db_log($db,$msg,$L_INFO); }
  182. }
  183. #---------------------------------------------------------------------------------------------------------------
  184. sub db_log_verbose {
  185. my $db = shift;
  186. my $msg = shift;
  187. if ($log_level >= $L_VERBOSE) { write_db_log($db,$msg,$L_VERBOSE); }
  188. }
  189. #---------------------------------------------------------------------------------------------------------------
  190. sub db_log_warning {
  191. my $db = shift;
  192. my $msg = shift;
  193. if ($log_level >= $L_WARNING) {
  194. sendEmail("WARN! ".substr($msg,0,30),$msg,1);
  195. write_db_log($db,$msg,$L_WARNING);
  196. }
  197. }
  198. #---------------------------------------------------------------------------------------------------------------
  199. sub init_db {
  200. # Create new database handle. If we can't connect, die()
  201. my $db = DBI->connect("dbi:mysql:database=$DBNAME;host=$DBHOST","$DBUSER","$DBPASS");
  202. if ( !defined $db ) { die "Cannot connect to mySQL server: $DBI::errstr\n"; }
  203. $db->do('SET NAMES utf8');
  204. $db->{mysql_auto_reconnect} = 1;
  205. return $db;
  206. }
  207. #---------------------------------------------------------------------------------------------------------------
  208. sub get_count_records {
  209. my $db = shift;
  210. my $table = shift;
  211. my $filter = shift;
  212. my $result = 0;
  213. return $result if (!$db);
  214. return $result if (!$table);
  215. my $sSQL='SELECT COUNT(*) as rec_cnt FROM '.$table;
  216. if ($filter) { $sSQL=$sSQL." where ".$filter; }
  217. my $record = get_record_sql($db,$sSQL);
  218. if ($record->{rec_cnt}) { $result = $record->{rec_cnt}; }
  219. return $result;
  220. }
  221. #---------------------------------------------------------------------------------------------------------------
  222. sub get_id_record {
  223. my $db = shift;
  224. my $table = shift;
  225. my $filter = shift;
  226. my $result = 0;
  227. return $result if (!$db);
  228. return $result if (!$table);
  229. my $record = get_record_sql($db,"SELECT id FROM $table WHERE $filter");
  230. if ($record->{id}) { $result = $record->{id}; }
  231. return $result;
  232. }
  233. #---------------------------------------------------------------------------------------------------------------
  234. sub get_records_sql {
  235. my $db = shift;
  236. my $table = shift;
  237. my @result;
  238. return @result if (!$db);
  239. return @result if (!$table);
  240. my $list = $db->prepare( $table );
  241. if ( !defined $list ) { die "Cannot prepare statement: $DBI::errstr\n"; }
  242. $list->execute;
  243. while(my $row_ref = $list ->fetchrow_hashref) {
  244. push(@result,$row_ref);
  245. }
  246. $list->finish();
  247. return @result;
  248. }
  249. #---------------------------------------------------------------------------------------------------------------
  250. sub get_record_sql {
  251. my $db = shift;
  252. my $tsql = shift;
  253. my @result;
  254. return @result if (!$db);
  255. return @result if (!$tsql);
  256. my $list = $db->prepare( $tsql . ' LIMIT 1' );
  257. if ( !defined $list ) { die "Cannot prepare statement: $DBI::errstr\n"; }
  258. $list->execute;
  259. my $row_ref = $list ->fetchrow_hashref;
  260. $list->finish();
  261. return $row_ref;
  262. }
  263. #---------------------------------------------------------------------------------------------------------------
  264. sub get_diff_rec {
  265. my $db = shift;
  266. my $table = shift;
  267. my $value = shift;
  268. my $filter = shift;
  269. return if (!$db);
  270. return if (!$table);
  271. return if (!$filter);
  272. my $old_value = get_record_sql($db,"SELECT * FROM $table WHERE $filter");
  273. my $result='';
  274. foreach my $field (keys %$value) {
  275. if (!$value->{$field}) { $value->{$field}=''; }
  276. if (!$old_value->{$field}) { $old_value->{$field}=''; }
  277. if ($value->{$field}!~/^$old_value->{$field}$/) { $result = $result." $field => $value->{$field} (old: $old_value->{$field}),"; }
  278. }
  279. $result=~s/,$//;
  280. return $result;
  281. }
  282. #---------------------------------------------------------------------------------------------------------------
  283. sub update_record {
  284. my $db = shift;
  285. my $table = shift;
  286. my $record = shift;
  287. my $filter = shift;
  288. return if (!$db);
  289. return if (!$table);
  290. return if (!$filter);
  291. my $old_record = get_record_sql($db,"SELECT * FROM $table WHERE $filter");
  292. my $diff='';
  293. my $change_str='';
  294. my $found_changed=0;
  295. my $auth_id = 0;
  296. if ($table eq "User_auth") {
  297. $auth_id = $old_record->{'id'};
  298. foreach my $field (keys %$record) {
  299. next if (!exists $acl_fields{$field});
  300. $record->{changed}="1";
  301. }
  302. }
  303. foreach my $field (keys %$record) {
  304. if (!defined $record->{$field}) { $record->{$field}=''; }
  305. if (!defined $old_record->{$field}) { $old_record->{$field}=''; }
  306. my $old_value = quotemeta($old_record->{$field});
  307. my $new_value = $record->{$field};
  308. $new_value=~s/\'//g;
  309. $new_value=~s/\"//g;
  310. if ($new_value!~/^$old_value$/) {
  311. $diff = $diff." $field => $record->{$field} (old: $old_record->{$field}),";
  312. $change_str = $change_str." `$field`=".$db->quote($record->{$field}).",";
  313. $found_changed++;
  314. }
  315. }
  316. if ($found_changed) {
  317. $change_str=~s/\,$//;
  318. $diff=~s/\,$//;
  319. if ($table eq 'User_auth') { $change_str .= ", `changed_time`='".GetNowTime()."'"; }
  320. my $sSQL = "UPDATE $table SET $change_str WHERE $filter";
  321. db_log_debug($db,'Change table '.$table.' for '.$filter.' set: '.$diff,$auth_id);
  322. do_sql($db,$sSQL);
  323. } else {
  324. db_log_debug($db,'Nothing change. Skip update.');
  325. }
  326. }
  327. #---------------------------------------------------------------------------------------------------------------
  328. sub insert_record {
  329. my $db = shift;
  330. my $table = shift;
  331. my $record = shift;
  332. return if (!$db);
  333. return if (!$table);
  334. my $change_str='';
  335. my $fields='';
  336. my $values='';
  337. my $new_str='';
  338. if ($table eq 'User_auth') {
  339. foreach my $field (keys %$record) {
  340. next if (!exists $acl_fields{$field});
  341. $record->{changed}="1";
  342. }
  343. }
  344. foreach my $field (keys %$record) {
  345. if (!defined $record->{$field}) { $record->{$field}=''; }
  346. my $new_value = $record->{$field};
  347. $new_value=~s/\'//g;
  348. $new_value=~s/\"//g;
  349. $fields = $fields."`$field`,";
  350. $values = $values." ".$db->quote($new_value).",";
  351. $new_str = $new_str." $field => $new_value,";
  352. }
  353. $fields=~s/,$//;
  354. $values=~s/,$//;
  355. $new_str=~s/,$//;
  356. my $sSQL = "INSERT INTO $table($fields) VALUES($values)";
  357. my $result = do_sql($db,$sSQL);
  358. if ($result) { $new_str='id: '.$result.' '.$new_str; }
  359. db_log_debug($db,'Add record to table '.$table.' '.$new_str);
  360. return $result;
  361. }
  362. #---------------------------------------------------------------------------------------------------------------
  363. sub delete_record {
  364. my $db = shift;
  365. my $table = shift;
  366. my $filter = shift;
  367. return if (!$db);
  368. return if (!$table);
  369. return if (!$filter);
  370. my $old_record = get_record_sql($db,"SELECT * FROM $table WHERE $filter");
  371. my $diff='';
  372. foreach my $field (keys %$old_record) {
  373. if (!$old_record->{$field}) { $old_record->{$field}=''; }
  374. $diff = $diff." $field => $old_record->{$field},";
  375. }
  376. $diff=~s/,$//;
  377. db_log_debug($db,'Delete record from table '.$table.' value: '.$diff);
  378. #never delete user ip record!
  379. if ($table eq 'User_auth') {
  380. my $sSQL = "UPDATE User_auth SET deleted=1, changed_time='".GetNowTime()."' WHERE ".$filter;
  381. do_sql($db,$sSQL);
  382. } else {
  383. my $sSQL = "DELETE FROM ".$table." WHERE ".$filter;
  384. do_sql($db,$sSQL);
  385. }
  386. }
  387. #---------------------------------------------------------------------------------------------------------------
  388. sub GetNowTime {
  389. my ($sec,$min,$hour,$day,$month,$year,$zone) = localtime(time());
  390. $month += 1;
  391. $year += 1900;
  392. my $now_str=sprintf "%04d-%02d-%02d %02d:%02d:%02d",$year,$month,$day,$hour,$min,$sec;
  393. return $now_str;
  394. }
  395. #---------------------------------------------------------------------------------------------------------------
  396. sub is_hotspot {
  397. my $db = shift;
  398. my $ip = shift;
  399. my $users = new Net::Patricia;
  400. #check hotspot
  401. my @ip_rules = get_records_sql($db,'SELECT * FROM subnets WHERE hotspot=1 AND LENGTH(subnet)>0');
  402. foreach my $row (@ip_rules) { $users->add_string($row->{subnet},$config_ref{hotspot_user_id}); }
  403. if ($users->match_string($ip)) { return 1; }
  404. return 0;
  405. }
  406. #---------------------------------------------------------------------------------------------------------------
  407. sub get_new_user_id {
  408. my $db = shift;
  409. my $ip = shift;
  410. my $mac = shift;
  411. my $hostname = shift;
  412. #check ip
  413. if (defined $ip and $ip) {
  414. my $users = new Net::Patricia;
  415. #check hotspot
  416. my @ip_rules = get_records_sql($db,'SELECT * FROM subnets WHERE hotspot=1 AND LENGTH(subnet)>0');
  417. foreach my $row (@ip_rules) { $users->add_string($row->{subnet},$config_ref{hotspot_user_id}); }
  418. if ($users->match_string($ip)) { return $users->match_string($ip); }
  419. #check ip rules
  420. @ip_rules = get_records_sql($db,'SELECT * FROM auth_rules WHERE type=1 and LENGTH(rule)>0');
  421. foreach my $row (@ip_rules) { $users->add_string($row->{rule},$row->{user_id}); }
  422. if ($users->match_string($ip)) { return $users->match_string($ip); }
  423. }
  424. #check mac
  425. if (defined $mac and $mac) {
  426. my @user_rules=get_records_sql($db,'SELECT * FROM auth_rules WHERE type=2 AND LENGTH(rule)>0');
  427. foreach my $user (@user_rules) {
  428. if ($mac=~/$user->{rule}/i) { return $user->{user_id}; }
  429. }
  430. }
  431. #check hostname
  432. if (defined $hostname and $hostname) {
  433. my @user_rules=get_records_sql($db,'SELECT * FROM auth_rules WHERE type=3 AND LENGTH(rule)>0');
  434. foreach my $user (@user_rules) {
  435. if ($hostname=~/$user->{rule}/i) { return $user->{user_id}; }
  436. }
  437. }
  438. return $default_user_id;
  439. }
  440. #---------------------------------------------------------------------------------------------------------------
  441. sub set_changed {
  442. my $db = shift;
  443. my $id = shift;
  444. return if (!$db or !$id);
  445. my $update_record;
  446. $update_record->{changed}=1;
  447. update_record($db,'User_auth',$update_record,"id=$id");
  448. }
  449. #---------------------------------------------------------------------------------------------------------------
  450. sub update_dns_record {
  451. my $hdb = shift;
  452. my $dhcp_record = shift;
  453. my $auth_record = shift;
  454. my $ad_zone = get_option($hdb,33);
  455. my $ad_dns = get_option($hdb,3);
  456. $update_hostname_from_dhcp = get_option($hdb,46) || 0;
  457. my $subnets_dhcp = get_subnets_ref($hdb);
  458. my $enable_ad_dns_update = ($ad_zone and $ad_dns and $update_hostname_from_dhcp);
  459. log_debug("Subnet: $dhcp_record->{network}");
  460. log_debug("DNS update flags - zone: $ad_zone dns: $ad_dns config: $update_hostname_from_dhcp subnet: $subnets_dhcp->{$dhcp_record->{network}}->{dhcp_update_hostname}");
  461. my $maybe_update_dns=(($dhcp_record->{type}=~/add/i or $dhcp_record->{type}=~/old/i) and $dhcp_record->{hostname_utf8} and $dhcp_record->{hostname_utf8} !~/UNDEFINED/i and $enable_ad_dns_update and $subnets_dhcp->{$dhcp_record->{network}}->{dhcp_update_hostname});
  462. if (!$maybe_update_dns) {
  463. db_log_debug($hdb,"FOUND Auth_id: $auth_record->{id}. DNS update don't needed.");
  464. return 0;
  465. }
  466. log_debug("DNS update enabled.");
  467. #update dns block
  468. my $fqdn_static;
  469. if ($auth_record->{dns_name}) {
  470. $fqdn_static=lc($auth_record->{dns_name});
  471. if ($fqdn_static!~/$ad_zone$/i) {
  472. $fqdn_static=~s/\.$//;
  473. $fqdn_static=lc($fqdn_static.'.'.$ad_zone);
  474. }
  475. }
  476. my $fqdn=lc(trim($dhcp_record->{hostname_utf8}));
  477. if ($fqdn!~/$ad_zone$/i) {
  478. $fqdn=~s/\.$//;
  479. $fqdn=lc($fqdn.'.'.$ad_zone);
  480. }
  481. db_log_debug($hdb,"FOUND Auth_id: $auth_record->{id} dns_name: $fqdn_static dhcp_hostname: $fqdn");
  482. #check exists static dns name
  483. my $static_exists = 0;
  484. my $dynamic_exists = 0;
  485. my $static_ok = 0;
  486. my $dynamic_ok = 0;
  487. my $static_ref;
  488. my $dynamic_ref;
  489. if ($fqdn_static ne '') {
  490. my @dns_record=ResolveNames($fqdn_static);
  491. $static_exists = (scalar @dns_record>0);
  492. if ($static_exists) {
  493. $static_ref = join(' ',@dns_record);
  494. foreach my $dns_a (@dns_record) {
  495. if ($dns_a=~/^$dhcp_record->{ip}$/) { $static_ok = $dns_a; }
  496. }
  497. }
  498. } else { $static_ok = 1; }
  499. if ($fqdn ne '') {
  500. my @dns_record=ResolveNames($fqdn);
  501. $dynamic_exists = (scalar @dns_record>0);
  502. if ($dynamic_exists) {
  503. $dynamic_ref = join(' ',@dns_record);
  504. foreach my $dns_a (@dns_record) {
  505. if ($dns_a=~/^$dhcp_record->{ip}$/) { $dynamic_ok = $dns_a; }
  506. }
  507. }
  508. }
  509. if ($fqdn_static ne '') {
  510. if (!$static_ok) {
  511. db_log_info($hdb,"Static record mismatch! Expected $fqdn_static => $dhcp_record->{ip}, recivied: $static_ref");
  512. if (!$static_exists) {
  513. db_log_info($hdb,"Static dns hostname defined but not found. Create it ($fqdn_static => $dhcp_record->{ip})!");
  514. update_ad_hostname($fqdn_static,$dhcp_record->{ip},$ad_zone,$ad_dns,$hdb);
  515. }
  516. } else { db_log_debug($hdb,"Static record for $fqdn_static [$static_ok] correct."); }
  517. }
  518. if ($fqdn ne '' and $dynamic_ok ne '') { db_log_debug($hdb,"Dynamic record for $fqdn [$dynamic_ok] correct. No changes required."); }
  519. if ($fqdn ne '' and !$dynamic_ok) {
  520. #log only to file!!!
  521. log_error($hdb,"Dynamic record mismatch! Expected: $fqdn => $dhcp_record->{ip}, recivied: $dynamic_ref. Checking the status.");
  522. #check exists hostname
  523. my $another_hostname_exists = 0;
  524. my $hostname_filter = ' LOWER(dns_name)="'.lc($dhcp_record->{hostname_utf8}).'"';
  525. if ($fqdn_static ne '' and $fqdn !~/$fqdn_static/) { $hostname_filter = $hostname_filter . ' or LOWER(dns_name)="'.lc($auth_record->{dns_name}).'"'; }
  526. #check exists another records with some static hostname
  527. my $name_record = get_record_sql($hdb,'SELECT * FROM User_auth WHERE id<>'.$auth_record->{id}.' and deleted=0 and ('.$hostname_filter.') ORDER BY last_found DESC');
  528. if ($name_record->{id}) { $another_hostname_exists = 1; }
  529. if (!$another_hostname_exists) {
  530. if ($fqdn_static and $fqdn_static ne '') {
  531. if ($fqdn_static!~/$fqdn/) {
  532. db_log_info($hdb,"Hostname from dhcp request $fqdn differs from static dns hostanme $fqdn_static. Ignore dynamic binding!");
  533. # update_ad_hostname($fqdn,$dhcp_record->{ip},$ad_zone,$ad_dns,$hdb);
  534. }
  535. } else {
  536. db_log_info($hdb,"Static dns hostname not defined. Create dns record by dhcp request. $fqdn => $dhcp_record->{ip}");
  537. update_ad_hostname($fqdn,$dhcp_record->{ip},$ad_zone,$ad_dns,$hdb);
  538. }
  539. } else {
  540. db_log_error($hdb,"Found another record with some hostname id: $name_record->{id} ip: $name_record->{ip} hostname: $name_record->{dns_hostname}. Skip update.");
  541. }
  542. }
  543. #end update dns block
  544. }
  545. #------------------------------------------------------------------------------------------------------------
  546. sub update_ad_hostname {
  547. my $fqdn = shift;
  548. my $ip = shift;
  549. my $zone = shift;
  550. my $server = shift;
  551. my $db = shift;
  552. if (!$db) {
  553. log_info("DNS-UPDATE: Zone $zone Server: $server A: $fqdn IP: $ip");
  554. } else {
  555. db_log_info($db,"DNS-UPDATE: Zone $zone Server: $server A: $fqdn IP: $ip");
  556. }
  557. my @add_dns=();
  558. push(@add_dns,"gsstsig");
  559. push(@add_dns,"server $server");
  560. push(@add_dns,"zone $zone");
  561. push(@add_dns,"update delete $fqdn A");
  562. push(@add_dns,"update add $fqdn 3600 A $ip");
  563. push(@add_dns,"send");
  564. my $nsupdate_file = "/tmp/".$fqdn.".nsupdate";
  565. write_to_file($nsupdate_file,\@add_dns);
  566. do_exec('kinit -k -t /usr/local/scripts/cfg/dns_updater.keytab dns_updater@'.uc($zone).' && nsupdate "'.$nsupdate_file.'"');
  567. if (-e "$nsupdate_file") { unlink "$nsupdate_file"; }
  568. }
  569. #---------------------------------------------------------------------------------------------------------------
  570. sub resurrection_auth {
  571. my $db = shift;
  572. my $ip = shift;
  573. my $mac = shift;
  574. my $action = shift;
  575. my $hostname = shift;
  576. my $ip_aton=StrToIp($ip);
  577. my $timestamp=GetNowTime();
  578. my $record=get_record_sql($db,'SELECT * FROM User_auth WHERE `deleted`=0 AND `ip_int`='.$ip_aton.' AND `mac`="'.$mac.'"');
  579. my $new_record;
  580. $new_record->{last_found}=$timestamp;
  581. if ($record->{user_id}) {
  582. if ($action!~/arp/i) {
  583. $new_record->{dhcp_action}=$action;
  584. $new_record->{dhcp_time}=$timestamp;
  585. update_record($db,'User_auth',$new_record,"id=$record->{id}");
  586. } else {
  587. update_record($db,'User_auth',$new_record,"id=$record->{id}");
  588. }
  589. return $record->{id};
  590. }
  591. #default user
  592. my $new_user_id=get_new_user_id($db,$ip,$mac,$hostname);
  593. #search changed mac
  594. $record=get_record_sql($db,'SELECT * FROM User_auth WHERE `ip_int`='.$ip_aton." and deleted=0");
  595. if ($record->{id}) {
  596. if (!$record->{mac}) {
  597. db_log_verbose($db,"use empty auth record...");
  598. $new_record->{ip_int}=$ip_aton;
  599. $new_record->{ip}=$ip;
  600. $new_record->{mac}=$mac;
  601. $new_record->{user_id}=$new_user_id;
  602. if ($action!~/arp/i) {
  603. $new_record->{dhcp_action}=$action;
  604. $new_record->{dhcp_time}=$timestamp;
  605. update_record($db,'User_auth',$new_record,"id=$record->{id}");
  606. } else {
  607. update_record($db,'User_auth',$new_record,"id=$record->{id}");
  608. }
  609. return $record->{id};
  610. }
  611. if ($record->{mac}) {
  612. db_log_warning($db,"For ip: $ip mac change detected! Old mac: [".$record->{mac}."] New mac: [".$mac."]. Disable old auth_id: $record->{id}");
  613. my $disable_record;
  614. $disable_record->{deleted}="1";
  615. update_record($db,'User_auth',$disable_record,"id=".$record->{id});
  616. }
  617. }
  618. #seek old auth with same ip and mac
  619. my $auth_exists=get_count_records($db,'User_auth',"ip_int=".$ip_aton." and mac='".$mac."'");
  620. $new_record->{ip_int}=$ip_aton;
  621. $new_record->{ip}=$ip;
  622. $new_record->{mac}=$mac;
  623. $new_record->{user_id}=$new_user_id;
  624. $new_record->{save_traf}="$save_detail";
  625. $new_record->{deleted}="0";
  626. $new_record->{dhcp_action}=$action;
  627. $new_record->{dhcp_time}=$timestamp;
  628. if ($auth_exists) {
  629. #found ->Resurrection old record
  630. my $resurrection_id = get_id_record($db,'User_auth',"ip_int=".$ip_aton." and mac='".$mac."'");
  631. if (!is_hotspot($db,$ip)) { db_log_warning($db,"Resurrection auth_id: $resurrection_id with ip: $ip and mac: $mac"); }
  632. else { db_log_info($db,"Resurrection auth_id: $resurrection_id with ip: $ip and mac: $mac"); }
  633. update_record($db,'User_auth',$new_record,"id=$resurrection_id");
  634. } else {
  635. #not found ->create new record
  636. if (!is_hotspot($db,$ip)) { db_log_warning($db,"New ip created! ip: $ip mac: $mac"); } else { db_log_info($db,"New ip created! ip: $ip mac: $mac"); }
  637. insert_record($db,'User_auth',$new_record);
  638. }
  639. #filter and status
  640. my $cur_auth_id=get_id_record($db,'User_auth',"ip='$ip' and mac='$mac' and deleted=0 ORDER BY last_found DESC");
  641. if ($cur_auth_id) {
  642. $record=get_record_sql($db,"SELECT * FROM User_list WHERE id=".$new_user_id);
  643. if ($record) {
  644. $new_record->{filter_group_id}=$record->{filter_group_id};
  645. $new_record->{queue_id}=$record->{queue_id};
  646. $new_record->{enabled}="$record->{enabled}";
  647. update_record($db,'User_auth',$new_record,"id=$cur_auth_id");
  648. }
  649. } else { return; }
  650. return $cur_auth_id;
  651. }
  652. #---------------------------------------------------------------------------------------------------------------
  653. sub new_auth {
  654. my $db = shift;
  655. my $ip = shift;
  656. my $ip_aton=StrToIp($ip);
  657. my $record=get_record_sql($db,'SELECT id FROM User_auth WHERE `deleted`=0 AND `ip_int`='.$ip_aton);
  658. if ($record->{id}) { return $record->{id}; }
  659. #default user
  660. my $new_user_id=get_new_user_id($db,$ip);
  661. my $user_record=get_record_sql($db,"SELECT * FROM User_list WHERE id=".$new_user_id);
  662. my $timestamp=GetNowTime();
  663. my $new_record;
  664. $new_record->{ip_int}=$ip_aton;
  665. $new_record->{ip}=$ip;
  666. $new_record->{user_id}=$new_user_id;
  667. $new_record->{save_traf}="$save_detail";
  668. $new_record->{deleted}="0";
  669. $new_record->{dhcp_action}='netflow';
  670. $new_record->{filter_group_id}=$user_record->{filter_group_id};
  671. $new_record->{queue_id}=$user_record->{queue_id};
  672. $new_record->{enabled}="$user_record->{enabled}";
  673. my $cur_auth_id=insert_record($db,'User_auth',$new_record);
  674. db_log_warning($db,"New ip created by netflow! ip: $ip") if (!$cur_auth_id);
  675. return $cur_auth_id;
  676. }
  677. #---------------------------------------------------------------------------------------------------------------
  678. sub get_option {
  679. my $db=shift;
  680. my $option_id=shift;
  681. return if (!$option_id);
  682. return if (!$db);
  683. my $default_option = get_record_sql($db,'SELECT * FROM config_options WHERE id='.$option_id);
  684. my $config_options = get_record_sql($db,'SELECT * FROM config WHERE option_id='.$option_id);
  685. my $result;
  686. if (!$config_options) {
  687. if ($default_option->{'type'}=~/int/i or $default_option->{'type'}=~/bool/i) {
  688. $result = $default_option->{'default_value'}*1;
  689. } else {
  690. $result = $default_option->{'default_value'};
  691. }
  692. return $result;
  693. }
  694. $result = $config_options->{'value'};
  695. return $result;
  696. }
  697. #---------------------------------------------------------------------------------------------------------------
  698. sub init_option {
  699. my $db=shift;
  700. $last_refresh_config = time();
  701. $config_ref{dbh}=$db;
  702. $config_ref{save_detail}=get_option($db,23);
  703. $config_ref{add_unknown_user}=get_option($db,22);
  704. $config_ref{dns_server}=get_option($db,3);
  705. $config_ref{dhcp_server}=get_option($db,5);
  706. $config_ref{snmp_default_version}=get_option($db,9);
  707. $config_ref{snmp_default_community}=get_option($db,11);
  708. $config_ref{KB}=get_option($db,1);
  709. $config_ref{mac_discovery}=get_option($db,17);
  710. $config_ref{arp_discovery}=get_option($db,19);
  711. $config_ref{default_user_id}=get_option($db,20);
  712. $config_ref{admin_email}=get_option($db,21);
  713. $config_ref{sender_email}=get_option($db,52);
  714. $config_ref{send_email}=get_option($db,51);
  715. $config_ref{history}=get_option($db,26);
  716. $config_ref{history_dhcp}=get_option($db,27);
  717. $config_ref{router_login}=get_option($db,28);
  718. $config_ref{router_password}=get_option($db,29);
  719. $config_ref{router_port}=get_option($db,30);
  720. $config_ref{org_name}=get_option($db,32);
  721. $config_ref{domain_name}=get_option($db,33);
  722. $config_ref{connections_history}=get_option($db,35);
  723. $config_ref{debug}=get_option($db,34);
  724. $config_ref{log_level} = get_option($db,53);
  725. if ($config_ref{debug}) { $config_ref{log_level} = 255; }
  726. $config_ref{urgent_sync}=get_option($db,50);
  727. $config_ref{ignore_hotspot_dhcp_log} = get_option($db,44);
  728. $config_ref{ignore_update_dhcp_event} = get_option($db,45);
  729. $config_ref{update_hostname_from_dhcp} = get_option($db,46);
  730. $config_ref{hotspot_user_id}=get_option($db,43);
  731. $config_ref{history_log_day}=get_option($db,47);
  732. $config_ref{history_syslog_day} = get_option($db,48);
  733. $config_ref{history_trafstat_day} = get_option($db,49);
  734. $config_ref{enable_quotes} = get_option($db,54);
  735. $config_ref{netflow_step} = get_option($db,55);
  736. $config_ref{traffic_ipstat_history} = get_option($db,56);
  737. $config_ref{nagios_url} = get_option($db,57);
  738. $config_ref{cacti_url} = get_option($db,58);
  739. $config_ref{torrus_url} = get_option($db,59);
  740. $config_ref{wiki_url} = get_option($db,60);
  741. $config_ref{stat_url} = get_option($db,62);
  742. $config_ref{wiki_path} = get_option($db,61);
  743. #$save_detail = 1; id=23
  744. $save_detail=get_option($db,23);
  745. #$add_unknown_user = 1; id=22
  746. $add_unknown_user=get_option($db,22);
  747. #$dns_server='192.168.2.12'; id=3
  748. $dns_server=get_option($db,3);
  749. #$dhcp_server='192.168.2.12'; id=5
  750. $dhcp_server=get_option($db,5);
  751. #$snmp_default_version='2'; id=9
  752. $snmp_default_version=get_option($db,9);
  753. #$snmp_default_community='public'; id=11
  754. $snmp_default_community=get_option($db,11);
  755. #$KB=1024; id=1
  756. $KB=get_option($db,1);
  757. #$mac_discovery; id=17
  758. $mac_discovery=get_option($db,17);
  759. #$arp_discovery; id=19
  760. $arp_discovery=get_option($db,19);
  761. #$default_user_id; id=20
  762. $default_user_id=get_option($db,20);
  763. #$admin_email; id=21
  764. $admin_email=get_option($db,21);
  765. #sender email
  766. $sender_email=get_option($db,52);
  767. #send email
  768. $send_email=get_option($db,51);
  769. #$history=15; id=26
  770. $history=get_option($db,26);
  771. #$history_dhcp=7; id=27
  772. $history_dhcp=get_option($db,27);
  773. #$router_login="admin"; id=28
  774. $router_login=get_option($db,28);
  775. #$router_password="admin"; id=29
  776. $router_password=get_option($db,29);
  777. #$router_port=23; id=30
  778. $router_port=get_option($db,30);
  779. #32
  780. $org_name=get_option($db,32);
  781. #33
  782. $domain_name=get_option($db,33);
  783. #35
  784. $connections_history=get_option($db,35);
  785. #debug
  786. $debug=get_option($db,34);
  787. #log level
  788. $log_level = get_option($db,53);
  789. if ($debug) { $log_level = 255; }
  790. #urgent sync access
  791. $urgent_sync=get_option($db,50);
  792. $ignore_hotspot_dhcp_log = get_option($db,44);
  793. $ignore_update_dhcp_event = get_option($db,45);
  794. $update_hostname_from_dhcp = get_option($db,46);
  795. #$hotspot_user_id; id=43
  796. $hotspot_user_id=get_option($db,43);
  797. $history_log_day=get_option($db,47);
  798. $history_syslog_day = get_option($db,48);
  799. $history_trafstat_day = get_option($db,49);
  800. @subnets=get_records_sql($db,'SELECT * FROM subnets ORDER BY ip_int_start');
  801. if (defined $office_networks) { undef $office_networks; }
  802. if (defined $free_networks) { undef $free_networks; }
  803. if (defined $vpn_networks) { undef $vpn_networks; }
  804. if (defined $hotspot_networks) { undef $hotspot_networks; }
  805. if (defined $all_networks) { undef $all_networks; }
  806. $office_networks = new Net::Patricia;
  807. $free_networks = new Net::Patricia;
  808. $vpn_networks = new Net::Patricia;
  809. $hotspot_networks = new Net::Patricia;
  810. $all_networks = new Net::Patricia;
  811. @office_network_list=();
  812. @free_network_list=();
  813. @free_network_list=();
  814. @vpn_network_list=();
  815. @hotspot_network_list=();
  816. @all_network_list=();
  817. foreach my $net (@subnets) {
  818. next if (!$net->{subnet});
  819. $subnets_ref{$net->{subnet}}=$net;
  820. if ($net->{office}) {
  821. push(@office_network_list,$net->{subnet});
  822. $office_networks->add_string($net->{subnet});
  823. }
  824. if ($net->{free}) {
  825. push(@free_network_list,$net->{subnet});
  826. $free_networks->add_string($net->{subnet});
  827. }
  828. if ($net->{vpn}) {
  829. push(@vpn_network_list,$net->{subnet});
  830. $vpn_networks->add_string($net->{subnet});
  831. }
  832. if ($net->{hotspot}) {
  833. push(@hotspot_network_list,$net->{subnet});
  834. push(@all_network_list,$net->{subnet});
  835. $hotspot_networks->add_string($net->{subnet});
  836. }
  837. push(@all_network_list,$net->{subnet});
  838. $all_networks->add_string($net->{subnet});
  839. }
  840. #remove all rules for default user id and hotspot subnet
  841. #delete_record($db,"auth_rules","user_id=".$config_ref{default_user_id});
  842. #delete_record($db,"auth_rules","user_id=".$config_ref{hotspot_user_id});
  843. #foreach my $subnet (@hotspot_network_list) { delete_record($db,"auth_rules","rule='".$subnet."'"); }
  844. }
  845. #---------------------------------------------------------------------------------------------------------------
  846. sub get_subnets_ref {
  847. my $db = shift;
  848. my @list=get_records_sql($db,'SELECT * FROM subnets ORDER BY ip_int_start');
  849. my $list_ref;
  850. foreach my $net (@list) {
  851. next if (!$net->{subnet});
  852. $list_ref->{$net->{subnet}}=$net;
  853. }
  854. return $list_ref;
  855. }
  856. #---------------------------------------------------------------------------------------------------------------
  857. sub get_device_by_ip {
  858. my $db = shift;
  859. my $ip = shift;
  860. my $netdev=get_record_sql($db,'SELECT * FROM devices WHERE ip="'.$ip.'"');
  861. if ($netdev and $netdev->{id}>0) { return $netdev; }
  862. my $auth_rec=get_record_sql($db,'SELECT user_id FROM User_auth WHERE ip="'.$ip.'" and deleted=0');
  863. if ($auth_rec and $auth_rec->{user_id}>0) {
  864. $netdev=get_record_sql($db,'SELECT * FROM devices WHERE user_id='.$auth_rec->{user_id});
  865. return $netdev;
  866. }
  867. return;
  868. }
  869. #---------------------------------------------------------------------------------------------------------------
  870. sub GetUnixTimeByStr {
  871. my $time_str = shift;
  872. $time_str =~s/\//-/g;
  873. $time_str = trim($time_str);
  874. my ($sec,$min,$hour,$day,$mon,$year) = (localtime())[0,1,2,3,4,5];
  875. $year+=1900;
  876. $mon++;
  877. if ($time_str =~/^([0-9]{2,4})\-([0-9]{1,2})-([0-9]{1,2})\s+/) {
  878. $year = $1; $mon = $2; $day = $3;
  879. }
  880. if ($time_str =~/([0-9]{1,2})\:([0-9]{1,2})\:([0-9]{1,2})$/) {
  881. $hour = $1; $min = $2; $sec = $3;
  882. }
  883. my $result = mktime($sec,$min,$hour,$day,$mon-1,$year-1900);
  884. return $result;
  885. }
  886. #---------------------------------------------------------------------------------------------------------------
  887. sub GetTimeStrByUnixTime {
  888. my $time = shift || time();
  889. my ($sec, $min, $hour, $mday, $mon, $year) = (localtime($time))[0,1,2,3,4,5];
  890. my $result = strftime("%Y-%m-%d %H:%M:%S",$sec, $min, $hour, $mday, $mon, $year);
  891. return $result;
  892. }
  893. #---------------------------------------------------------------------------------------------------------------
  894. sub Set_Variable {
  895. my $db = shift;
  896. my $name = shift || $MY_NAME;
  897. my $value = shift || $$;
  898. my $timeshift = shift || 60;
  899. Del_Variable($db,$name);
  900. my $clean_variables = time() + $timeshift;
  901. my ($sec,$min,$hour,$day,$month,$year,$zone) = localtime($clean_variables);
  902. $month++;
  903. $year += 1900;
  904. my $clean_str=sprintf "%04d-%02d-%02d %02d:%02d:%02d",$year,$month,$day,$hour,$min,$sec;
  905. my $clean_variables_date=$db->quote($clean_str);
  906. do_sql($db,"INSERT INTO variables(name,value,clear_time) VALUES('".$name."','".$value."',".$clean_variables_date.");");
  907. }
  908. #---------------------------------------------------------------------------------------------------------------
  909. sub Get_Variable {
  910. my $db = shift;
  911. my $name = shift || $MY_NAME;
  912. my $variable=get_record_sql($db,'SELECT `value` FROM `variables` WHERE name="'.$name.'"');
  913. if (!$variable and $variable->{'value'}) { return $variable->{'value'}; }
  914. return;
  915. }
  916. #---------------------------------------------------------------------------------------------------------------
  917. sub Del_Variable {
  918. my $db = shift;
  919. my $name = shift || $MY_NAME;
  920. do_sql($db,"DELETE FROM `variables` WHERE name='".$name."';");
  921. }
  922. #---------------------------------------------------------------------------------------------------------------
  923. sub recalc_quotes {
  924. my $db = shift;
  925. my $calc_id = shift || $$;
  926. return if (!get_option($db,54));
  927. clean_variables($db);
  928. return if (Get_Variable($db,'RECALC'));
  929. my $timeshift = get_option($db,55);
  930. if ($timeshift >5 ) { $timeshift=$timeshift-1; }
  931. Set_Variable($db,'RECALC',$calc_id,time()+$timeshift*60);
  932. my $now = DateTime->now(time_zone=>'local');
  933. my $day_start = $db->quote($now->ymd("-")." 00:00:00");
  934. my $day_dur = DateTime::Duration->new( days => 1 );
  935. my $tomorrow = $now+$day_dur;
  936. my $day_stop = $db->quote($tomorrow->ymd("-")." 00:00:00");
  937. $now->set(day=>1);
  938. my $month_start=$db->quote($now->ymd("-")." 00:00:00");
  939. my $month_dur = DateTime::Duration->new( months => 1 );
  940. my $next_month = $now + $month_dur;
  941. $next_month->set(day=>1);
  942. my $month_stop = $db->quote($next_month->ymd("-")." 00:00:00");
  943. #get user limits
  944. my $user_auth_list_sql="SELECT A.id as auth_id, U.id, U.day_quota, U.month_quota, A.day_quota as auth_day, A.month_quota as auth_month FROM User_auth as A,User_list as U WHERE A.deleted=0 ORDER by user_id";
  945. my @authlist_ref = get_records_sql($db,$user_auth_list_sql);
  946. my %user_stats;
  947. my %auth_info;
  948. foreach my $row (@authlist_ref) {
  949. $auth_info{$row->{auth_id}}{user_id}=$row->{id};
  950. $auth_info{$row->{auth_id}}{day_limit}=$row->{auth_day};
  951. $auth_info{$row->{auth_id}}{month_limit}=$row->{auth_month};
  952. $auth_info{$row->{auth_id}}{day}=0;
  953. $auth_info{$row->{auth_id}}{month}=0;
  954. $user_stats{$row->{id}}{day_limit}=$row->{day_quota};
  955. $user_stats{$row->{id}}{month_limit}=$row->{month_quota};
  956. $user_stats{$row->{id}}{day}=0;
  957. $user_stats{$row->{id}}{month}=0;
  958. }
  959. #recalc quotes - global
  960. #day
  961. my $day_sql="SELECT User_stats.auth_id, SUM( byte_in + byte_out ) AS traf_all FROM User_stats
  962. WHERE User_stats.`timestamp`>= $day_start AND User_stats.`timestamp`< $day_stop GROUP BY User_stats.auth_id";
  963. my @day_stats = get_records_sql($db,$day_sql);
  964. foreach my $row (@day_stats) {
  965. my $user_id=$auth_info{$row->{auth_id}}{user_id};
  966. $auth_info{$row->{auth_id}}{day}=$row->{traf_all};
  967. $user_stats{$user_id}{day}+=$row->{traf_all};
  968. }
  969. #month
  970. my $month_sql="SELECT User_stats.auth_id, SUM( byte_in + byte_out ) AS traf_all FROM User_stats
  971. WHERE User_stats.`timestamp`>= $month_start AND User_stats.`timestamp`< $month_stop GROUP BY User_stats.auth_id";
  972. my @month_stats = get_records_sql($db,$month_sql);
  973. foreach my $row (@month_stats) {
  974. my $user_id=$auth_info{$row->{auth_id}}{user_id};
  975. $auth_info{$row->{auth_id}}{month}=$row->{traf_all};
  976. $user_stats{$user_id}{month}+=$row->{traf_all};
  977. }
  978. foreach my $auth_id (keys %auth_info) {
  979. next if (!$auth_info{$auth_id}{day_limit});
  980. next if (!$auth_info{$auth_id}{month_limit});
  981. my $day_limit=$auth_info{$auth_id}{day_limit}*$KB*$KB;
  982. my $month_limit=$auth_info{$auth_id}{month_limit}*$KB*$KB;
  983. my $blocked_d=($auth_info{$auth_id}{day}>$day_limit);
  984. my $blocked_m=($auth_info{$auth_id}{month}>$month_limit);
  985. if ($blocked_d or $blocked_m) {
  986. my $history_msg;
  987. if ($blocked_d) { $history_msg=printf "Day quota limit found for auth_id: $auth_id - Current: %d Max: %d",$auth_info{$auth_id}{day},$day_limit; }
  988. if ($blocked_m) { $history_msg=printf "Month quota limit found for auth_id: $auth_id - Current: %d Max: %d",$auth_info{$auth_id}{month},$month_limit; }
  989. do_sql($db,"UPDATE User_auth set blocked=1, changed=1 where id=$auth_id");
  990. db_log_verbose($db,$history_msg);
  991. }
  992. }
  993. foreach my $user_id (keys %user_stats) {
  994. next if (!$user_stats{$user_id}{day_limit});
  995. next if (!$user_stats{$user_id}{month_limit});
  996. my $day_limit=$user_stats{$user_id}{day_limit}*$KB*$KB;
  997. my $month_limit=$user_stats{$user_id}{month_limit}*$KB*$KB;
  998. my $blocked_d=($user_stats{$user_id}{day}>$day_limit);
  999. my $blocked_m=($user_stats{$user_id}{month}>$month_limit);
  1000. if ($blocked_d or $blocked_m) {
  1001. my $history_msg;
  1002. if ($blocked_d) { $history_msg=printf "Day quota limit found for user_id: $user_id - Current: %d Max: %d",$user_stats{$user_id}{day},$day_limit; }
  1003. if ($blocked_m) { $history_msg=printf "Month quota limit found for user_id: $user_id - Current: %d Max: %d",$user_stats{$user_id}{month},$month_limit; }
  1004. do_sql($db,"UPDATE User_user set blocked=1 where id=$user_id");
  1005. do_sql($db,"UPDATE User_auth set blocked=1, changed=1 where user_id=$user_id");
  1006. db_log_verbose($db,$history_msg);
  1007. }
  1008. }
  1009. Del_Variable($db,'RECALC');
  1010. }
  1011. #---------------------------------------------------------------------------------------------------------------
  1012. sub clean_variables {
  1013. my $db = shift;
  1014. #clean temporary variables
  1015. my $clean_variables = time();
  1016. my ($sec,$min,$hour,$day,$month,$year,$zone) = localtime($clean_variables);
  1017. $month++;
  1018. $year += 1900;
  1019. my $now_str=sprintf "%04d-%02d-%02d %02d:%02d:%02d",$year,$month,$day,$hour,$min,$sec;
  1020. my $clean_variables_date=$db->quote($now_str);
  1021. do_sql($db,"DELETE FROM `variables` WHERE clear_time<=$clean_variables_date");
  1022. }
  1023. #---------------------------------------------------------------------------------------------------------------
  1024. $dbh=init_db();
  1025. init_option($dbh);
  1026. clean_variables($dbh);
  1027. Set_Variable($dbh);
  1028. 1;
  1029. }