summaryrefslogtreecommitdiff
path: root/diary.php
blob: 0c8b9a4a77f44e97aea4621e18de42e951971bcd (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
<?

include "includes/theme.inc";

function diary_overview($num = 20) {
  global $theme, $user;

  $result = db_query("SELECT d.*, u.userid FROM diaries d LEFT JOIN users u ON d.author = u.id ORDER BY d.timestamp DESC LIMIT $num");

  $output .= "<P>This part of the website is dedicated to providing easy to write and easy to read online diaries or journals filled with daily thoughts, poetry, boneless blather, spiritual theories, intimate details, valuable experiences, cynical rants, semi-coherent comments, writing experiments, artistic babblings, critics on actuality, fresh insights, diverse dreams, chronicles and general madness available for general human consumption.</P>";

  while ($diary = db_fetch_object($result)) {
    if ($time != date("F jS", $diary->timestamp)) {
      $output .= "<B>". date("l, F jS", $diary->timestamp) ."</B>\n";
      $time = date("F jS", $diary->timestamp);
    }
    $output .= "<DL>\n";
    $output .= " <DD><P><B>$diary->userid wrote:</B></P></DD>\n";
    $output .= " <DL>\n";
    $output .= "  <DD><P>". check_output($diary->text, 1) ."</P><P>[ <A HREF=\"diary.php?op=view&name=$diary->userid\">more</A> ]</P></DD>\n";
    $output .= " </DL>\n";
    $output .= "</DL>\n";
  }

  $theme->header();
  $theme->box("Online diary", $output);
  $theme->footer();

}

function diary_entry($timestamp, $text, $id = 0) {
  if ($id) {
    $output .= "<DL>\n";
    $output .= " <DT><B>". date("l, F jS", $timestamp) .":</B> </DT>\n";
    $output .= " <DD><P>[ <A HREF=\"diary.php?op=edit&id=$id\">edit</A> ]</P><P>". check_output($text, 1) ."</P></DD>\n";
    $output .= "</DL>\n";
  }
  else {
    $output .= "<DL>\n";
    $output .= " <DT><B>". date("l, F jS", $timestamp) .":</B></DT>\n";
    $output .= " <DD><P>". check_output($text, 1) ."</P></DD>\n";
    $output .= "</DL>\n";
  }
  return $output;
}

function diary_display($username) {
  global $theme, $user;

  $result = db_query("SELECT d.*, u.userid FROM diaries d LEFT JOIN users u ON d.author = u.id WHERE u.userid = '$username' ORDER BY timestamp DESC");

  if ($username == $user->userid) {
    $output .= diary_entry(time(), "<BIG><A HREF=\"diary.php?op=add\">Add new diary entry!</A></BIG><P>");
    while ($diary = db_fetch_object($result)) $output .= diary_entry($diary->timestamp, $diary->text, $diary->id);
  }
  else {
    $output .= "<P>". format_username($username) ."'s diary:</P>\n";
    while ($diary = db_fetch_object($result)) $output .= diary_entry($diary->timestamp, $diary->text);
  }

  $theme->header();
  $theme->box("$username's online diary", $output);
  $theme->footer();
}

function diary_add() {
  global $theme, $user, $allowed_html;
  
  $output .= "<FORM ACTION=\"diary.php\" METHOD=\"post\">\n";

  $output .= "<P>\n"; 
  $output .= " <B>Enter new diary entry:</B><BR>\n";
  $output .= " <TEXTAREA WRAP=\"virtual\" COLS=\"50\" ROWS=\"15\" NAME=\"text\" MAXLENGTH=\"20\"></TEXTAREA><BR>\n";
  $output .= " <SMALL><I>Allowed HTML tags: ". htmlspecialchars($allowed_html) .".</I></SMALL>\n";
  $output .= "</P>\n";

  $output .= "<P>\n";
  $output .= " <INPUT TYPE=\"submit\" NAME=\"op\" VALUE=\"Preview diary entry\">\n";
  $output .= "</P>\n";

  $output .= "</FORM>\n";
  
  $theme->header();
  $theme->box("Online diary", $output);
  $theme->footer();
}

function diary_edit($id) {
  global $theme, $user, $allowed_html;

  $result = db_query("SELECT * FROM diaries WHERE id = $id");
  $diary = db_fetch_object($result);

  $output .= diary_entry($diary->timestamp, $diary->text);

  $output .= "<FORM ACTION=\"diary.php\" METHOD=\"post\">\n";

  $output .= "<P>\n";
  $output .= " <B>Edit diary entry:</B><BR>\n";
  $output .= " <TEXTAREA WRAP=\"virtual\" COLS=\"50\" ROWS=\"15\" NAME=\"text\">". check_input($diary->text) ."</TEXTAREA><BR>\n";
  $output .= " <SMALL><I>Allowed HTML tags: ". htmlspecialchars($allowed_html) .".</I></SMALL>\n";
  $output .= "</P>\n";

  $output .= "<P>\n";
  $output .= " <INPUT TYPE=\"hidden\" NAME=\"id\" VALUE=\"$diary->id\">\n";
  $output .= " <INPUT TYPE=\"hidden\" NAME=\"timesamp\" VALUE=\"$diary->timestamp\">\n";
  $output .= " <INPUT TYPE=\"submit\" NAME=\"op\" VALUE=\"Preview diary entry\"> <INPUT TYPE=\"submit\" NAME=\"op\" VALUE=\"Submit diary entry\">\n";
  $output .= "</P>\n";

  $output .= "</FORM>\n";
  
  $theme->header();
  $theme->box("Online diary", $output);
  $theme->footer();
}

function diary_preview($text, $timestamp, $id = 0) {
  global $theme, $user, $allowed_html;

  $output .= diary_entry($timestamp, $text);

  $output .= "<FORM ACTION=\"diary.php\" METHOD=\"post\">\n";

  $output .= "<P>\n";
  $output .= " <B>Preview diary entry:</B><BR>\n";
  $output .= " <TEXTAREA WRAP=\"virtual\" COLS=\"50\" ROWS=\"15\" NAME=\"text\">". check_output($text) ."</TEXTAREA><BR>\n";
  $output .= " <SMALL><I>Allowed HTML tags: ". htmlspecialchars($allowed_html) .".</I></SMALL>\n";
  $output .= "</P>\n";

  $output .= "<P>\n";
  $output .= " <INPUT TYPE=\"hidden\" NAME=\"id\" VALUE=\"$id\">\n";
  $output .= " <INPUT TYPE=\"submit\" NAME=\"op\" VALUE=\"Preview diary entry\">\n";
  $output .= " <INPUT TYPE=\"submit\" NAME=\"op\" VALUE=\"Submit diary entry\">\n";
  $output .= "</P>\n";

  $output .= "</FORM>\n";

  $theme->header();
  $theme->box("Online diary", $output);
  $theme->footer();
}

function diary_submit($text, $id = 0) {
  global $user, $theme;

  if ($id) {
    watchdog("message", "old diary entry updated");
    db_query("UPDATE diaries SET text =  '". check_input($text) ."' WHERE id = $id");
  }
  else {
    watchdog("diary", "new diary entry added");
    db_query("INSERT INTO diaries (author, text, timestamp) VALUES ('$user->id', '". check_input($text) ."', '". time() ."')");
  }

  header("Location: diary.php?op=view&name=$user->userid");
}

### Security check:
if (strstr($id, " ") || strstr($name, " ")) {
  watchdog("error", "diary: attempt to provide malicious input through URI");
  exit();
}

switch($op) {
  case "add":
    diary_add();
    break;
  case "edit":
    diary_edit($id);
    break;
  case "view":
    diary_display($name);
    break;
  case "Preview diary entry":
    if ($id) diary_preview($text, $timestamp, $id);
    else diary_preview($text, time());
    break;
  case "Submit diary entry":
    if ($id) diary_submit($text, $id);
    else diary_submit($text);
    break;
  default:
    diary_overview();
}

?>