mysql.pm 37 KB

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