Advertisement
Guest User

Untitled

a guest
Mar 12th, 2014
101
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 17.24 KB | None | 0 0
  1. package CybOrg::DB;
  2.  
  3. #
  4. # CybOrg - The Cybercafe Organizer
  5. # http://cyborg.sourceforge.net
  6. #
  7. # Copyright (C) 2003 - The CybOrg Project
  8. #
  9. # This program is free software; you can redistribute it and/or modify
  10. # it under the terms of the GNU General Public License version 2 as
  11. # published by the Free Software Foundation.
  12. #
  13. # This program is distributed in the hope that it will be useful,
  14. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. # GNU General Public License for more details.
  17. #
  18. # You should have received a copy of the GNU General Public License along
  19. # with this program; if not, write to the Free Software Foundation, Inc.,
  20. # 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  21. #
  22.  
  23. use strict;
  24. use warnings 'all';
  25.  
  26. use DBI;
  27. use CybOrg::Config qw(%config %options %database &def_options);
  28.  
  29. our $dbh;
  30. our %options;
  31.  
  32. sub _exception($$)
  33. {
  34. use CybOrg::Exception;
  35.  
  36. my ($error_code, $error_params) = @_;
  37.  
  38. my $exception = CybOrg::Exception->new();
  39. $exception->error_code($error_code);
  40. $exception->error_params($error_params);
  41.  
  42. if ($config{'debug'}) {
  43. print STDERR "DB Error:$error_code\nParams:$error_params\n\n";
  44. }
  45.  
  46. die $exception;
  47. }
  48.  
  49. sub _connect
  50. {
  51.  
  52. my $dbh;
  53.  
  54. eval {
  55. $dbh = DBI->connect("dbi:$database{'driver'}:dbname=$database{'dbname'};".
  56. "host=$database{'host'};port=$database{'port'}",
  57. $database{'user'}, $database{'password'},
  58. {AutoCommit => 0, RaiseError => 1});
  59. };
  60.  
  61. if ($@) {
  62. _exception('db_connect', {'host' => "$database{'host'}",
  63. 'port' => "$database{'port'}",
  64. 'user' => "$database{'user'}"});
  65. } else {
  66. return $dbh;
  67. }
  68.  
  69. }
  70.  
  71. sub _commit
  72. {
  73. eval {
  74. $dbh -> commit;
  75. };
  76. if ($@) {
  77. _exception('db_commit', {'host' => "$database{'host'}",
  78. 'port' => "$database{'port'}",
  79. 'user' => "$database{'user'}"});
  80. }
  81. }
  82.  
  83. sub _rollback
  84. {
  85. eval {
  86. $dbh -> rollback;
  87. };
  88. if ($@) {
  89. _exception('db_rollback', {'host' => "$database{'host'}",
  90. 'port' => "$database{'port'}",
  91. 'user' => "$database{'user'}"});
  92. }
  93. }
  94.  
  95. sub _select_row($)
  96. {
  97. my $query = shift;
  98. my @result;
  99.  
  100. eval {
  101. my $sth = $dbh -> prepare($query);
  102. $sth->execute;
  103. @result = $sth->fetchrow_array
  104. };
  105.  
  106. if ($@){
  107. _rollback;
  108. _exception('db_select',{'query' => "$query"});
  109. } else {
  110. return \@result;
  111. }
  112. }
  113.  
  114. sub _select_hash($)
  115. {
  116. my $query = shift;
  117. my %aux;
  118.  
  119. eval {
  120. my $sth = $dbh -> prepare($query);
  121. $sth->execute;
  122. my @row;
  123. while (@row = $sth->fetchrow_array){
  124. my $id = shift @row;
  125. $aux{$id} = [@row];
  126. }
  127. };
  128. if ($@) {
  129. _rollback;
  130. _exception('db_select',{'query' => "$query"});
  131. } else {
  132. return \%aux;
  133. }
  134. }
  135.  
  136. sub _modify($)
  137. {
  138. my $query = shift;
  139.  
  140. my $result;
  141. eval {
  142. $result = $dbh ->do($query);
  143. };
  144.  
  145. # Modifying queries MUST allways afect a non-zero number of rows
  146. if (($@) or ($result eq '0E0')) {
  147. _rollback;
  148. _exception('db_modify',{'query' => "$query"});
  149. }
  150. }
  151.  
  152. sub _update_station($$)
  153. {
  154. my ($station, $status) = @_;
  155. _modify("update stations set status='$status' where id='$station'");
  156. }
  157.  
  158.  
  159. BEGIN {
  160. use Exporter ();
  161. our ($VERSION, @ISA, @EXPORT);
  162. $VERSION = 0.1;
  163. @ISA = qw(Exporter);
  164.  
  165. @EXPORT = qw(&db_get_groups &db_get_some_group &db_get_stations &db_get_station
  166. &db_update_station &db_rent_station &db_get_tariffs &db_get_clients
  167. &db_get_address &db_validate_user &db_update_session
  168. &db_validate_session &db_get_group &db_delete_rent &delete_paused_rent
  169. &db_get_stations_groups &db_delete_stations &db_create_station
  170. &db_update_station_data &db_get_client &db_delete_clients
  171. &db_update_client &db_create_client &db_get_admins &db_get_admin
  172. &db_delete_admins &db_update_admin &db_create_admin &db_admin_exists
  173. &db_get_groups_and_count &db_delete_groups &db_create_group &db_update_group
  174. &db_get_tariff &db_create_tariff &db_update_tariff &db_delete_tariffs
  175. &db_tariff_exists &db_update_rent_station &db_update_rent_tariff
  176. &db_get_status &db_pause_rent &db_activate_rent &db_get_idle_rents
  177. &db_free_rent_station &db_update_discount &db_get_rent
  178. &db_finalize_rent &db_get_station_from_ip &db_get_options
  179. &db_update_options);
  180. $dbh = _connect;
  181.  
  182. }
  183.  
  184. sub db_get_options($)
  185. {
  186. my $username = shift;
  187. my $aux = _select_row("select options.language, options.locale, options.list_view, options.charset from admins, options where options.id_admin = admins.id and admins.username = '$username'");
  188. $options{'language'} = $$aux[0];
  189. $options{'locale'} = $$aux[1];
  190. $options{'list_view'} = $$aux[2];
  191. $options{'charset'} = $$aux[3];
  192. }
  193.  
  194. sub db_update_options($$$$$)
  195. {
  196. my ($username, $language, $locale, $list_view, $charset) = @_;
  197. my $aux = _select_row("select id from admins where username='$username'");
  198. _modify("update options set language = '$language', locale = '$locale', list_view = '$list_view', charset='$charset' where id_admin = '$$aux[0]'");
  199. _commit;
  200. }
  201.  
  202. sub db_get_status($)
  203. {
  204. my $station = shift;
  205. my $aux = _select_row("select status from stations where id='$station'");
  206. return $$aux[0];
  207. }
  208.  
  209. sub db_get_groups
  210. {
  211. return _select_hash("select id, description from groups");
  212. }
  213.  
  214. sub db_get_some_group
  215. {
  216. my $aux = _select_row("select id from groups");
  217. if (defined $aux) {
  218. return $$aux[0]
  219. }
  220. }
  221.  
  222. sub db_get_groups_and_count
  223. {
  224. return _select_hash("select groups.id, groups.description, count(stations.id) from groups left join stations on groups.id = stations.id_group group by groups.id, groups.description");
  225. }
  226.  
  227. sub db_validate_user($$)
  228. {
  229. my ($username, $password) = @_;
  230. if ($username and $password){
  231. my $aux = _select_row("select password from admins where username='$username'");
  232. return (defined $$aux[0])? ($$aux[0] eq $password): 0
  233. } else {
  234. return 0
  235. }
  236. }
  237.  
  238. sub db_validate_session($$)
  239. {
  240. my ($username, $session) = @_;
  241. if ($username and $session){
  242. my $aux = _select_row("select session from admins where username='$username'");
  243. return (defined $$aux[0])? ($$aux[0] eq $session): 0
  244. } else {
  245. return 0
  246. }
  247. }
  248.  
  249.  
  250. sub db_update_session($$)
  251. {
  252. my ($username, $session) = @_;
  253. _modify("update admins set session='$session' where username='$username'");
  254. _commit;
  255. }
  256.  
  257. sub db_get_stations($)
  258. {
  259. my $group = shift;
  260. return _select_hash("select stations.id, stations.description, stations.status, rents.id, rents.id_tariff, rents.start_time, rents.discount_time, rents.id_client from (stations left outer join rents_stations on stations.id = rents_stations.id_station ) left outer join rents on rents_stations.id_rent = rents.id where stations.id_group='$group'");
  261. }
  262.  
  263. sub db_get_station_from_ip($)
  264. {
  265. my $ip_address = shift;
  266. my @ip = split('\.', $ip_address);
  267.  
  268. my $station = _select_row("select stations.description, stations.status, rents.id_tariff, rents.start_time, rents.discount_time, rents.id_client, stations.id_group from (stations left outer join rents_stations on stations.id = rents_stations.id_station ) left outer join rents on rents_stations.id_rent = rents.id where stations.ip1 = '$ip[0]' and stations.ip2 = '$ip[1]' and stations.ip3 = '$ip[2]' and stations.ip4 = '$ip[3]'");
  269. my $group = _select_row("select description from groups where id = '$$station[6]'");
  270.  
  271. my @result;
  272. push (@result, $$station[0], $$group[0], $$station[1]);
  273.  
  274. if ($$station[1] eq '1') {
  275. my $client = _select_row("select last_name, name, balance from clients where id = '$$station[5]'");
  276. push (@result, $$client[0], $$client[1], $$client[2]);
  277. my $tariff = _select_row("select description, quarter, half, hour from tariffs where id = '$$station[2]'");
  278. push (@result, $$tariff[0], $$tariff[1], $$tariff[2], $$tariff[3], $$station[3], $$station[4]);
  279. }
  280.  
  281. return \@result;
  282. }
  283.  
  284. sub db_get_station($)
  285. {
  286. my $station = shift;
  287. return _select_row("select status, description, id_group, ip1, ip2, ip3, ip4 from stations where id='$station'");
  288. }
  289.  
  290. sub db_delete_stations($)
  291. {
  292. my $stations = shift;
  293. foreach(@$stations) {
  294. _modify("delete from stations where id='$_'");
  295. _commit;
  296. }
  297. }
  298.  
  299. sub db_get_stations_groups
  300. {
  301. return _select_hash("select stations.id, stations.description, stations.ip1, stations.ip2, stations.ip3, stations.ip4, groups.description from stations inner join groups on stations.id_group = groups.id order by stations.description");
  302. }
  303.  
  304. sub db_get_idle_rents
  305. {
  306. return _select_hash("select id, id_tariff, start_time, idle_time, discount_time, id_client from rents where idle='1'");
  307. }
  308.  
  309. sub db_update_station_data($$$$$$$)
  310. {
  311. my ($desc, $ip1, $ip2, $ip3, $ip4, $group, $key) = @_;
  312. _modify("update stations set description='$desc', ip1='$ip1', ip2='$ip2', ip3='$ip3', ip4='$ip4', id_group='$group' where id='$key'");
  313. _commit;
  314. }
  315.  
  316. sub db_create_station($$$$$$)
  317. {
  318. my ($desc, $ip1, $ip2, $ip3, $ip4, $group) = @_;
  319. _modify("insert into stations (description, id_group, ip1, ip2, ip3, ip4, status) values ('$desc','$group','$ip1','$ip2','$ip3','$ip4','2')");
  320. _commit;
  321. }
  322.  
  323. sub db_get_group($)
  324. {
  325. my $group = shift;
  326. my $aux = _select_row("select description from groups where id='$group'");
  327. return $$aux[0];
  328. }
  329.  
  330. sub db_update_station($$)
  331. {
  332. my ($station, $status) = @_;
  333. _update_station($station, $status);
  334. _commit;
  335. }
  336.  
  337. sub db_rent_station($$$)
  338. {
  339. my ($station, $tariff, $client) = @_;
  340. my $time = time;
  341. db_update_station($station, '1');
  342. _modify("insert into rents (id_client, id_tariff, start_time, idle_time, idle, discount_time) values ('$client','$tariff','$time','0','0','0')");
  343. my $aux = _select_row("select id from rents where id_client='$client' and start_time='$time'");
  344. _modify("insert into rents_stations values ($$aux[0], $station)");
  345. _commit;
  346. }
  347.  
  348. sub db_delete_rent($$)
  349. {
  350. my ($station, $rent) = @_;
  351. _update_station($station, 2);
  352. _modify("delete from rents_stations where id_rent='$rent' and id_station='$station'");
  353. _modify("delete from rents where id='$rent'");
  354. _commit;
  355. }
  356.  
  357. sub db_delete_paused_rent($)
  358. {
  359. my $rent = shift;
  360. _modify("delete from rents where id='$rent'");
  361. _commit;
  362. }
  363.  
  364. sub db_update_rent_station($$)
  365. {
  366. my ($station, $station_new) = @_;
  367. _update_station($station, 2);
  368. _update_station($station_new, 1);
  369. _modify("update rents_stations set id_station='$station_new' where id_station='$station'");
  370. _commit;
  371. }
  372.  
  373. sub db_free_rent_station($$)
  374. {
  375. my ($rent, $station) = @_;
  376. _update_station($station, 2);
  377. _modify("delete from rents_stations where id_rent='$rent' and id_station='$station'");
  378.  
  379. # Don't commit changes here!
  380. }
  381.  
  382. sub db_pause_rent($)
  383. {
  384. my $rent = shift;
  385. my $time = time;
  386. _modify("update rents set idle='1', idle_time='$time' where id='$rent'");
  387. _commit;
  388. }
  389.  
  390.  
  391. sub db_update_discount($)
  392. {
  393. my $rent = shift;
  394. my $time = time;
  395. my $aux = _select_row("select idle_time, discount_time from rents where id='$rent'");
  396. my $new_discount = $$aux[1] + ($time - $$aux[0]);
  397. _modify("update rents set idle='0', idle_time='0', discount_time='$new_discount' where id='$rent'");
  398.  
  399. # Don't commit changes here!
  400. }
  401.  
  402.  
  403. sub db_activate_rent($$)
  404. {
  405. my ($rent, $station) = @_;
  406. _update_station($station, 1);
  407. _modify("insert into rents_stations (id_rent, id_station) values ('$rent', '$station')");
  408. db_update_discount($rent);
  409. _commit;
  410. }
  411.  
  412. sub db_get_rent($)
  413. {
  414. my $rent = shift;
  415. return _select_row("select rents.id, clients.last_name, clients.name, clients.balance, tariffs.description, tariffs.quarter, tariffs.half, tariffs.hour, rents.start_time, rents.discount_time from rents, clients, tariffs where (rents.id='$rent') and (rents.id_client=clients.id) and (rents.id_tariff=tariffs.id)");
  416. }
  417.  
  418. sub db_update_rent_tariff($$)
  419. {
  420. my ($rent, $tariff_new) = @_;
  421. _modify("update rents set id_tariff='$tariff_new' where id='$rent'");
  422. _commit;
  423. }
  424.  
  425. sub db_finalize_rent($$$$)
  426. {
  427. use POSIX;
  428. my ($rent, $effective_cost, $to_account, $username) = @_;
  429.  
  430. my $user_id = _select_row("select id from admins where username='$username'");
  431.  
  432. if (defined $$user_id[0]) {
  433. my $aux = _select_row("select rents.id_client, rents.id_tariff, tariffs.quarter, tariffs.half, tariffs.hour, rents.start_time, rents.idle_time, rents.discount_time from rents, tariffs where (rents.id='$rent') and (rents.id_tariff = tariffs.id)");
  434.  
  435. my $id_client = $$aux[0];
  436. my $id_tariff = $$aux[1];
  437. my $start_time = $$aux[5];
  438. my $end_time = $$aux[6];
  439. my $discount_time = $$aux[7];
  440. my $total_time = floor((($end_time - $start_time) - $discount_time) / 60);
  441.  
  442. my @tariff = ($$aux[2], $$aux[3], $$aux[4]);
  443.  
  444. my $suggested_price;
  445.  
  446. if ($total_time <= 15) {
  447. $suggested_price = $tariff[0];
  448. } elsif ($total_time <= 30) {
  449. $suggested_price = $tariff[1];
  450. } elsif ($total_time <= 60) {
  451. $suggested_price = $tariff[2];
  452. } else {
  453. $suggested_price = (floor($total_time / 60) * $tariff[2]) + (floor( (($total_time % 60) + 14) / 15) * ($tariff[2] / 4));
  454. }
  455.  
  456. if (!defined $to_account) {
  457. $to_account = ''
  458. } elsif ($to_account eq 'yes') {
  459. my $balance = _select_row("select balance from clients where id='$id_client'");
  460. $$balance[0] -= $effective_cost;
  461. _modify("update clients set balance='$$balance[0]' where id='$id_client'");
  462.  
  463. }
  464. _modify("delete from rents where id='$rent'");
  465. _modify("insert into liquidations (id_client, id_admin, id_tariff, start_time, end_time, discount_time, suggested_price, price) values ('$id_client', '$$user_id[0]', '$id_tariff', '$start_time', '$end_time', '$discount_time', '$suggested_price', '$effective_cost')");
  466. _commit;
  467. }
  468. }
  469.  
  470. sub db_get_tariffs
  471. {
  472. return _select_hash("select id, description, quarter, half, hour from tariffs");
  473. }
  474.  
  475. sub db_get_clients
  476. {
  477. return _select_hash("select id, last_name, name, balance from clients");
  478. }
  479.  
  480. sub db_get_address($)
  481. {
  482. my $station = shift;
  483. my $aux = _select_row("select ip1, ip2, ip3, ip4 from stations where id='$station'");
  484. return "$$aux[0].$$aux[1].$$aux[2].$$aux[3]";
  485. }
  486.  
  487. sub db_get_client($)
  488. {
  489. my $client = shift;
  490. return _select_row("select last_name, name, balance from clients where id='$client'");
  491. }
  492.  
  493. sub db_delete_clients($)
  494. {
  495. my $clients = shift;
  496. foreach (@$clients) {
  497. _modify("delete from clients where id='$_'");
  498. _commit;
  499. }
  500. }
  501.  
  502. sub db_update_client($$$$)
  503. {
  504. my ($last_name, $name, $balance, $key) = @_;
  505. _modify("update clients set last_name='$last_name', name='$name', balance='$balance' where id='$key'");
  506. _commit;
  507. }
  508.  
  509. sub db_create_client($$$)
  510. {
  511. my ($last_name, $name, $balance) = @_;
  512. _modify("insert into clients (last_name, name, balance) values ('$last_name','$name','$balance')");
  513. _commit;
  514. }
  515.  
  516. sub db_get_admins
  517. {
  518. return _select_hash("select id, username from admins");
  519. }
  520.  
  521. sub db_get_admin($)
  522. {
  523. my $admin = shift;
  524. return _select_row("select username from admins where id='$admin'");
  525. }
  526.  
  527. sub db_delete_admins($)
  528. {
  529. my $admins = shift;
  530. foreach(@$admins) {
  531. _modify("delete from admins where id='$_'");
  532. _modify("delete from options where id_admin='$_'");
  533. _commit;
  534. }
  535. }
  536.  
  537. sub db_update_admin($$$)
  538. {
  539. my ($username, $password, $key) = @_;
  540. _modify("update admins set username='$username', password='$password' where id='$key'");
  541. _commit;
  542. }
  543.  
  544. sub db_create_admin($$)
  545. {
  546. my ($username, $password) = @_;
  547. _modify("insert into admins (username, password, session) values ('$username','$password', null)");
  548. my $aux = _select_row("select id from admins where username = '$username'");
  549. my %def_options = def_options;
  550. my $language = $def_options{'language'};
  551. my $locale = $def_options{'locale'};
  552. my $list_view = $def_options{'list_view'};
  553. my $charset = $def_options{'charset'};
  554.  
  555. _modify("insert into options (id_admin, language, locale, list_view, charset) values ('$$aux[0]', '$language', '$locale', '$list_view', '$charset')");
  556. _commit;
  557. }
  558.  
  559. sub db_admin_exists($)
  560. {
  561. my ($username) = @_;
  562. my $aux = _select_row("select id from admins where username='$username'");
  563. return (defined $$aux[0])? (1, $$aux[0]): (0,'');
  564. }
  565.  
  566. sub db_delete_groups($)
  567. {
  568. my $groups = shift;
  569. foreach (@$groups) {
  570. _modify("delete from groups where id='$_'");
  571. _commit;
  572. }
  573. }
  574.  
  575. sub db_update_group($$)
  576. {
  577. my ($description, $key) = @_;
  578. _modify("update groups set description='$description' where id='$key'");
  579. _commit;
  580. }
  581.  
  582. sub db_create_group($)
  583. {
  584. my ($description) = @_;
  585. _modify("insert into groups (description) values ('$description')");
  586. _commit;
  587. }
  588.  
  589. sub db_get_tariff($)
  590. {
  591. my $tariff = shift;
  592. return _select_row("select description, quarter, half, hour from tariffs where id='$tariff'");
  593. }
  594.  
  595. sub db_delete_tariffs($)
  596. {
  597. my $tariff = shift;
  598. foreach(@$tariff) {
  599. _modify("delete from tariffs where id='$_'");
  600. _commit;
  601. }
  602. }
  603.  
  604. sub db_update_tariff($$$$$)
  605. {
  606. my ($desc, $quarter, $half, $hour, $key) = @_;
  607. _modify("update tariffs set description='$desc', quarter='$quarter', half='$half', hour='$hour' where id='$key'");
  608. _commit;
  609. }
  610.  
  611. sub db_create_tariff($$$$)
  612. {
  613. my ($desc, $quarter, $half, $hour) = @_;
  614. _modify("insert into tariffs (description, quarter, half, hour) values ('$desc','$quarter','$half','$hour')");
  615. _commit;
  616. }
  617.  
  618. sub db_tariff_exists($)
  619. {
  620. my ($desc) = @_;
  621. my $aux = _select_row("select id from tariffs where description='$desc'");
  622. return (defined $$aux[0])?(1, $$aux[0]):(0,'');
  623. }
  624.  
  625. END {
  626. $dbh -> disconnect;
  627. }
  628.  
  629. 1;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement