chat_get_msgs.pl
138 lines of code
1
#!/usr/local/bin/perl
2
3
# must have's!
4
use strict;
5
use warnings;
6
use CGI::Carp qw(fatalsToBrowser);
7
use DBI;
8
use URI::Escape;
9
10
use lib "/var/www/html/Pm";
11
12
use User qw(get_user_stat get_user_dp);
13
use Date qw(expand_date);
14
use Html qw(pre_html_header);
15
use Html2 qw(tag br hr embolden italicize);
16
use Redir qw(error_redir notice_redir);
17
use Bc_chef qw(cookie_get);
18
use Bc_misc qw(get_param isHashRef pluralize shorten_str);
19
use Bc_sql qw(sql_execute
20
              get_constant
21
              user_exists
22
              get_theme_data
23
              $QUERY_PAGE
24
              $QUERY_UID
25
              $LOGGEDIN
26
27
              $DB
28
             );
29
30
my $DEBUG = 0;
31
my $debug_output = pre_html_header({type=>"text/html"});
32
$debug_output .= "DEBUG MODE ENABLED!" . hr . br;
33
34
my $output = pre_html_header({type=>"text/plain"});
35
36
# now, load up the msgs in inbox
37
# and spit them ALL out in HTML format
38
39
my $uid = get_param($QUERY_UID);
40
$debug_output .= "Logged in uid of: $LOGGEDIN" . br;
41
$debug_output .= "got uid param of: $uid" . br;
42
43
my $TABLE_BORDER = 0;
44
45
if (not user_exists($LOGGEDIN) or Security::banned($LOGGEDIN)) {
46
  my $msg =  "Access Denied";
47
  if ($DEBUG) { $msg .= " (chat_get_msgs.pl)"; }
48
49
  $output = error_redir("/", $msg);
50
} else {
51
52
  if (user_exists($uid)) {
53
    my $sql = "select * from messages where deled=1 and subject='chatbox msg' and ((to_ID=" . $DB->quote($LOGGEDIN) . " and from_ID=" . $DB->quote($uid) . ") or (to_ID=" . $DB->quote($uid) . "and from_ID=" . $DB->quote($LOGGEDIN) . ")) order by sent"; #, sent_time";
54
    my $results = sql_execute($sql, "chat get msgs.pl");
55
56
    if (isHashRef($results)) {
57
      $debug_output .= "sql returned a hash reference!" . br;
58
      # it's a hash reference
59
      $output .= "<table border=$TABLE_BORDER cellpadding=0><tr><td>\n";
60
      $output .= "  $results->{msg}";
61
      $output .= "</td></tr><tr><td align=right class=copyright>\n";
62
      $output .= "  $results->{sent_time}\n";
63
      $output .= "</td></tr><tr><td style='height: 5px; min-height: 5px;'>\n";
64
      $output .= "</td></tr></table>\n";
65
    } else {
66
      $debug_output .= "sql returned an array reference!" . br;
67
      # it's an array reference?
68
      if (ref $results eq "ARRAY") {
69
        # print out msgs
70
        my $c = "chat_in"; # a CSS class
71
        my $align = "left"; # incoming msgs
72
        my $align_time = "right"; # incoming msgs
73
        foreach my $msgRef (@$results) {
74
          $debug_output .= "found " . italicize((@$results)) . " incoming and outgoing msgs" . br;
75
          if ($msgRef->{from_ID} eq $LOGGEDIN) {
76
            # outgoing msgs
77
            $c = "chat_out";
78
            $align = "right";
79
            $align_time = "right";
80
          } else {
81
            # incoming msgs
82
            $c = "chat_in";
83
            $align = "left";
84
            $align_time = "left";
85
          }
86
          $output .= "<table border=0 cellspacing=0 width=100%><tr><td class=copyright colspan=3>\n";
87
          if ($msgRef->{from_ID} ne $LOGGEDIN) {
88
            $output .= "  " . shorten_str(get_user_stat($msgRef->{from_ID}, "nickname"), 10) . " said:\n";
89
          } else {
90
            $output .= "  You said:\n";
91
          }
92
          $output .= "</td></tr><tr><td align=center rowspan=2 valign=top width=14>\n";
93
          { my %img;
94
            $img{tag} = "img";
95
            $img{src} = "/images/" . get_user_dp($msgRef->{from_ID});
96
            $img{width} = 12;
97
98
            $output .= tag(\%img);
99
          }
100
          $output .= "</td><td rowspan=2 class=spacerx></td><td align=$align>\n";
101
          $output .= "  <table border=$TABLE_BORDER cellspacing=0 class='$c'";
102
          if ($msgRef->{from_ID} ne $LOGGEDIN) {
103
            # get certain colours attributed to from_ID!
104
            my $td = get_theme_data(get_user_stat($msgRef->{from_ID}, 'TID'));
105
            $output .= " style='color: #$td->{body_clr}; background-color: #$td->{bg_clr}; border: 1px solid #$td->{borders_clr};'";
106
          }
107
          $output .= "><tr><td class=chatwrap>" . uri_unescape($msgRef->{msg}) . "</td></tr></table>\n";
108
          $output .= "</td></tr><tr><td align=$align_time class='copyright hide'>\n";
109
          my $stime = $msgRef->{sent_time};
110
          $stime =~ s/\:[0-9][0-9] \(ST\)//i;
111
          my $date = expand_date($msgRef->{sent}, 1);
112
          if ($stime) { $output .= "  $stime - "; }
113
          $output .= "$date\n";
114
          $output .= "</td></tr><tr><td class=spacery_large colspan=3>\n";
115
          $output .= "</td></tr></table>\n";
116
        }
117
      } else {
118
        $debug_output .= "sql returned a reference to an empty array" . br;
119
        # all went well, just no msgs were found, send a header and nothing else
120
        $output .= "no msgs\n";
121
      }
122
    }
123
  } else {
124
    if (not $uid)
125
      { $output = error_redir("", "please, provide a uid\n"); } else
126
      { $output = error_redir("", "uid is not valid\n"); }
127
  }
128
}
129
130
if ($DEBUG) {
131
  $debug_output .= hr . "The following is what would normally get sent when not debugging" . hr . br;
132
  $debug_output .= $output;
133
  print $debug_output;
134
} else {
135
  print $output;
136
}
137
138
exit 1;