mysql.pm 37 KB

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