Advertisement
Guest User

Untitled

a guest
Jun 29th, 2017
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.90 KB | None | 0 0
  1. #! /usr/bin/perl -w
  2.  
  3. use strict;
  4. use CGI qw(:standard);
  5. use DBI;
  6.  
  7. use Time::ParseDate;
  8.  
  9. $ENV{ORACLE_HOME}="/opt/oracle/product/11.2.0/db_1";
  10. $ENV{ORACLE_BASE}="/opt/oracle/product/11.2.0";
  11. $ENV{ORACLE_SID}="CS339";
  12.  
  13. # database connections
  14. my $oracle_dbh = DBI->connect("DBI:Oracle:","dtm536","o47a2dad6")
  15. or die "Couldn't connect to the Oracle database: ". DBI->errstr;
  16. my $mysql_dbh = DBI->connect("DBI:mysql:database=cs339","cs339","cs339")
  17. or die "Couldn't connect to the Mysql database: ", DBI->errstr;
  18.  
  19. my $cookiename="PortfolioSession";
  20. my $inputcookiecontent = cookie($cookiename);
  21.  
  22. my $outputcookiecontent = undef;
  23. my $deletecookie=0;
  24. my $user = undef;
  25. my $password = undef;
  26. my $loginok=0;
  27. my $logincomplain=0;
  28. my $action;
  29.  
  30. if (param("act")) {
  31. $action=param("act");
  32. } else {
  33. $action="login";
  34. }
  35.  
  36. if ($action eq "login" || param('loginrun')) {
  37. if (param('loginrun')) {
  38. ($user,$password)=(param('user'),param('password'));
  39. if (ValidUser($oracle_dbh,$user,$password)) {
  40. $outputcookiecontent=join("/",$user,$password);
  41. $loginok=1;
  42. } else {
  43. $logincomplain=1;
  44. $action="login";
  45. }
  46. } else {
  47. #
  48. # Just a login screen request. Still, ignore any cookie that's there.
  49. #
  50. }
  51. } else {
  52. if ($inputcookiecontent) {
  53. ($user,$password) = split(/\//,$inputcookiecontent);
  54. if (!ValidUser($oracle_dbh,$user,$password)) {
  55. $action="login";
  56. } else {
  57. $outputcookiecontent=$inputcookiecontent;
  58. }
  59. } else {
  60. $action="login";
  61. }
  62. }
  63.  
  64. if ($action eq "logout") {
  65. $deletecookie=1;
  66. }
  67.  
  68. if ($outputcookiecontent) {
  69. my $cookie=cookie(-name=>$cookiename,
  70. -value=>$outputcookiecontent,
  71. -expires=>($deletecookie ? '-1h' : '+1h'));
  72. print header(-expires=>'now', -cookie=>$cookie);
  73. } else {
  74. print header(-expires=>'now');
  75. }
  76.  
  77.  
  78.  
  79. ###############################
  80. # HTML generation starts here #
  81. ###############################
  82.  
  83. print start_html('Portfolio Manager');
  84. print "<style type=\"text/css\">\n\@import \"portfolio.css\";\n</style>\n";
  85.  
  86. if ($loginok) {
  87. print "<h2>You have successfully logged in</h2>";
  88. }
  89.  
  90. if ($action eq "login") {
  91. if ($logincomplain) {
  92. print "Login failed. Try again.<p>"
  93. }
  94. if ($logincomplain or !param('loginrun')) {
  95. # login if you already have a username
  96. print start_form(-name=>'Login'),
  97. h2('Login to Portfolio Manager'),
  98. "Name:",textfield(-name=>'user'), p,
  99. "Password:",password_field(-name=>'password'),p,
  100. hidden(-name=>'act',default=>['login']),
  101. hidden(-name=>'loginrun',default=>['1']),
  102. submit,
  103. end_form;
  104. }
  105. }
  106.  
  107. if ($action eq "indivdualportfolio") {
  108. print "<h2>Por</h2>";
  109. }
  110.  
  111. if ($action eq "portfolio") {
  112. print "<h2>portfolio</h2>";
  113. }
  114.  
  115. if ($action eq "stock") {
  116. print "stock";
  117. }
  118.  
  119. if ($action eq "users") {
  120. # add user form
  121. print start_form(-name=>'AddUser'),
  122. h2('Add User'),
  123. "Name: ", textfield(-name=>'name'), p,
  124. "Password: ", textfield(-name=>'password'), p,
  125. hidden(-name=>'adduserrun',-default=>['1']),
  126. hidden(-name=>'act',-default=>['users']),
  127. submit,
  128. end_form;
  129.  
  130. # delete user form
  131. print start_form(-name=>'DeleteUser'),
  132. h2('Delete User'),
  133. "Name: ", textfield(-name=>'name'), p,
  134. hidden(-name=>'deluserrun',-default=>['1']),
  135. hidden(-name=>'act',-default=>['users']),
  136. submit,
  137. end_form;
  138.  
  139. # add the user
  140. if (param('adduserrun')) {
  141. my $name=param('name');
  142. my $password=param('password');
  143. my $error;
  144. $error=UserAdd($oracle_dbh,$name,$password);
  145. if ($error != 1) {
  146. print "Can't add user because: $error";
  147. } else {
  148. print "Added user $name\n";
  149. }
  150. }
  151.  
  152. # delete the user
  153. if (param('deluserrun')) {
  154. my $name=param('name');
  155. my $error=UserDel($oracle_dbh,$name);
  156. if ($error != 1) {
  157. print "Can't delete user because: $error";
  158. } else {
  159. print "User $name deleted.";
  160. }
  161. }
  162.  
  163.  
  164. }
  165.  
  166. if ($action eq "logout") {
  167. print "<h2>You have been successfully logged out</h2>";
  168. }
  169.  
  170.  
  171. print end_html;
  172.  
  173. sub UserAdd {
  174. my ($dbh, $name, $password) = @_;
  175. my $sth = $dbh->prepare("insert into portfolio_users (username,password) values (?,?)")
  176. or die "Couldn't prepare statement: ". $dbh->errstr;
  177.  
  178. $sth->execute($name, $password) or die "Couldn't execute statement: ". $dbh->errstr;
  179. $sth->finish;
  180. $dbh->disconnect;
  181. }
  182.  
  183. sub UserDel {
  184. my ($dbh,$name) = @_;
  185. my $sth = $dbh->prepare("delete from portfolio_users where name=?")
  186. or die "Couldn't prepare statement: ". $dbh->errstr;
  187.  
  188. $sth->execute($name) or die "Couldn't execute statement: ". $dbh->errstr;
  189. $sth->finish;
  190. $dbh->disconnect;
  191. }
  192.  
  193. sub ValidUser {
  194. my ($dbh,$user,$password)=@_;
  195. my $sth = $dbh->prepare("select count(*) from portfolio_users where username=? and password=?")
  196. or die "Couldn't prepare statement: ". $dbh->errstr;
  197. $sth->execute($user,$password)
  198. or die "Couldn't execute statement: ". $dbh->errstr;
  199. my ($user_exists) = $sth->fetchrow_array();
  200. return $user_exists;
  201. }
  202.  
  203. sub QueryStock {
  204. my ($symbol, $from, $to)=@_;
  205. my @info = `/usr/bin/perl get_data.pl --close --from="$from" --to="$to" $symbol`;
  206. open(OUTP, ">$symbol") or die("Cannot open file for writing\n");
  207. print OUTP @info;
  208. close OUTP;
  209. GraphAndPrint($symbol,$symbol);
  210. unlink("$symbol");
  211. }
  212.  
  213.  
  214. sub GraphAndPrint
  215. {
  216. my ($name) = @_;
  217. my ($graphfile)="$name.png";
  218. print "<b>Graph</b><p><img src =\"" . GnuPlot($name,$graphfile) ."\"><p>\n";
  219. print "<b>Data</b><p><pre>";
  220. open (FILE,$name);
  221. while (<FILE>) {
  222. print $_;
  223. }
  224. close(FILE);
  225. print "</pre>";
  226. }
  227.  
  228. sub GnuPlot
  229. {
  230. my ($datafile, $outputfile)=@_;
  231. open(GNUPLOT,"|gnuplot");
  232. print GNUPLOT "set terminal png\n";
  233. print GNUPLOT "set output \"$outputfile\"\n";
  234. print GNUPLOT "plot \"$datafile\" with linespoints\n";
  235. close(GNUPLOT);
  236. return $outputfile;
  237. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement