blog.module 8.47 KB
Newer Older
Dries's avatar
 
Dries committed
1
<?php
2
// $Id$
Dries's avatar
 
Dries committed
3
4
5
6
7
8
9
10
11
12
13

function blog_help() {
 ?>
  <p>Drupal's blog module allows registered users to maintain an online blog or diary.  It provides easy-to-write and easy-to-read online diaries or journals that can be filled with daily thoughts, poetry, boneless blabber, spiritual theories, intimate details, valuable experiences, cynical rants, semi-coherent comments, writing experiments, artistic babblings, critics on current facts, fresh insights, diverse dreams, chronicles and mumbling madness available for public consumption.</p>
 <?php
}

function blog_perm() {
  return array("administer blogs", "access blogs", "post blogs");
}

Dries's avatar
   
Dries committed
14
function blog_feed_user($uid = 0, $date = 0) {
Dries's avatar
   
Dries committed
15
16
  global $user;

Dries's avatar
   
Dries committed
17
18
19
20
21
22
23
24
25
26
  if ($uid) {
    $account = user_load(array("uid" => $uid, "status" => 1));
  }
  else {
    $account = $user;
  }

  if (!$date) {
    $date = time();
  }
Dries's avatar
   
Dries committed
27

Dries's avatar
   
Dries committed
28
  $result = db_query("SELECT n.nid, n.title, n.body, n.created, u.name, u.uid FROM blog b LEFT JOIN node n ON b.nid = n.nid LEFT JOIN users u ON n.uid = u.uid WHERE u.uid = '$uid' AND n.created > '". ($date - 2592000) ."' ORDER BY b.nid DESC LIMIT 15");
Dries's avatar
   
Dries committed
29
30
31
32
  while ($blog = db_fetch_object($result)) {
    $items .= format_rss_item($blog->title, path_uri() ."node.php?id=$blog->nid", $blog->body);
  }

Dries's avatar
   
Dries committed
33
34
  $output .= "<?xml version=\"1.0\" encoding=\"ISO-8859-1\" ?>\n";
  $output .= "<!DOCTYPE rss [<!ENTITY % HTMLlat1 PUBLIC \"-//W3C//ENTITIES Latin 1 for XHTML//EN\" \"http://www.w3.org/TR/xhtml1/DTD/xhtml-lat1.ent\">\n";
Dries's avatar
   
Dries committed
35
36
  $output .= "<?xml version=\"1.0\" encoding=\"ISO-8859-1\" ?>\n";
  $output .= "<rss version=\"0.91\">\n";
Dries's avatar
   
Dries committed
37
  $output .= format_rss_channel("$account->name's blog", path_uri() ."module.php?mod=blog&op=view&id=$account->uid", "$account->name's blog", $items);
Dries's avatar
   
Dries committed
38
39
  $output .= "</rss>\n";

Dries's avatar
   
Dries committed
40
41
  header("Content-Type: text/xml");

Dries's avatar
   
Dries committed
42
43
44
45
46
  print $output;

}

function blog_feed_last() {
Dries's avatar
   
Dries committed
47
  $result = db_query("SELECT n.nid, n.title, n.body, n.created, u.name, u.uid FROM blog b LEFT JOIN node n ON b.nid = n.nid LEFT JOIN users u ON n.uid = u.uid ORDER BY b.nid DESC LIMIT 15");
Dries's avatar
   
Dries committed
48
  while ($blog = db_fetch_object($result)) {
Dries's avatar
   
Dries committed
49
    $items .= format_rss_item($blog->title, path_uri() ."module.php?mod=blog&op=view&id=$blog->nid", $blog->body);
Dries's avatar
   
Dries committed
50
51
52
53
54
55
56
  }

  $output .= "<?xml version=\"1.0\" encoding=\"ISO-8859-1\" ?>\n";
  $output .= "<rss version=\"0.91\">\n";
  $output .= format_rss_channel(variable_get("site_name", "drupal") .": user blogs", path_uri() ."module.php?mod=blog", "Recently updated blogs.", $items);
  $output .= "</rss>\n";

Dries's avatar
   
Dries committed
57
58
  header("Content-Type: text/xml");

Dries's avatar
   
Dries committed
59
60
61
62
  print $output;

}

Dries's avatar
   
Dries committed
63
function blog_page_user($uid = 0, $date = 0) {
Dries's avatar
 
Dries committed
64
65
  global $theme, $user;

Dries's avatar
   
Dries committed
66
67
68
69
70
71
  if ($uid) {
    $account = user_load(array("uid" => $uid, "status" => 1));
  }
  else {
    $account = $user;
  }
Dries's avatar
 
Dries committed
72

Dries's avatar
   
Dries committed
73
74
75
76
  if (!$date) {
    $date = time();
  }

Dries's avatar
   
Dries committed
77
  $result = db_query("SELECT nid FROM node WHERE type = 'blog' AND uid = '$account->uid' AND created <= '$date' AND created >= '". ($date - 2592000) ."' ORDER BY nid DESC");
Dries's avatar
 
Dries committed
78

Dries's avatar
   
Dries committed
79
  $output .= "<table border=\"0\" cellpadding=\"4\" cellspacing=\"4\">";
Dries's avatar
   
Dries committed
80

Dries's avatar
   
Dries committed
81
82
83
  while ($node = db_fetch_object($result)) {

    $blog = node_load(array("nid" => $node->nid));
Dries's avatar
   
Dries committed
84

Dries's avatar
   
Dries committed
85
86
    $links = array();

Dries's avatar
   
Dries committed
87
88
89
    if ($date != date("dny", $blog->created)) {
      $date = date("dny", $blog->created);
      $output .= "<tr><td colspan=\"2\"><b><a href=\"module.php?mod=blog&id=$blog->uid&date=". mktime(23, 59, 59, date("n", $blog->created), date("d", $blog->created), date("Y", $blog->created)) ."\">". format_date($blog->created, custom, "d M Y") .":</a></b></td></tr>";
Dries's avatar
   
Dries committed
90
91
    }

Dries's avatar
   
Dries committed
92
    if ($user->uid && $user->uid == $uid) {
Dries's avatar
   
Dries committed
93
      $links[] = "<a href=\"module.php?mod=node&op=edit&id=$blog->nid\">". t("edit") ."</a>";
Dries's avatar
   
Dries committed
94
95
    }

Dries's avatar
   
Dries committed
96
    if ($user->uid && user_access("post blogs")) {
Dries's avatar
   
Dries committed
97
      $links[] = "<a href=\"module.php?mod=blog&op=blog&id=$blog->nid\">". t("blog it") ."</a>";
Dries's avatar
   
Dries committed
98
99
100
    }

    if ($blog->comment) {
Dries's avatar
   
Dries committed
101
      $links[] = "<a href=\"node.php?id=$blog->nid\">". format_plural(node_get_comments($blog->nid), t("comment"), t("comments")) ."</a>";
Dries's avatar
   
Dries committed
102
103
104
105
106
    }

    $output .= "<tr><td><div style=\"margin-left: 20px;\"><b>". check_output($blog->title) ."</b></div></td><td align=\"right\">". $theme->links($links) ."</td></tr>";
    $output .= "<tr><td colspan=\"2\"><div style=\"margin-left: 40px;\">". check_output($blog->body, 1) ."</div><br /></td></tr>";

Dries's avatar
 
Dries committed
107
  }
Dries's avatar
   
Dries committed
108

Dries's avatar
   
Dries committed
109
  $output .= "</table>";
Dries's avatar
   
Dries committed
110
  $output .= "<a href=\"module.php?mod=blog&op=feed&id=$account->uid\"><img src=\"". $theme->image("xml.gif") ."\" width=\"36\" height=\"14\" align=\"right\" border=\"0\" /></a>\n";
Dries's avatar
   
Dries committed
111

Dries's avatar
   
Dries committed
112
  $theme->box(sprintf(t("%s's blog"), $account->name), $output, "main");
Dries's avatar
 
Dries committed
113
114
}

Dries's avatar
Dries committed
115
function blog_page_last() {
Dries's avatar
   
Dries committed
116
  global $theme, $user;
Dries's avatar
 
Dries committed
117

Dries's avatar
   
Dries committed
118
  $result = db_query("SELECT nid FROM node WHERE type = 'blog' ORDER BY nid DESC LIMIT 20");
Dries's avatar
 
Dries committed
119

Dries's avatar
   
Dries committed
120
  $output .= "<table border=\"0\" cellpadding=\"4\" cellspacing=\"4\">";
Dries's avatar
   
Dries committed
121

Dries's avatar
   
Dries committed
122
123
  while ($node = db_fetch_object($result)) {
    $blog = node_load(array("nid" => $node->nid));
Dries's avatar
   
Dries committed
124

Dries's avatar
   
Dries committed
125
126
    $links = array();

Dries's avatar
   
Dries committed
127
    $links[] = "<a href=\"module.php?mod=blog&op=view&id=$blog->uid\">". sprintf("%s's blog", $blog->name) ."</a>";
Dries's avatar
   
Dries committed
128

Dries's avatar
   
Dries committed
129
    if ($blog->uid == $user->uid) {
Dries's avatar
   
Dries committed
130
      $links[] = "<a href=\"module.php?mod=node&op=edit&id=$blog->nid\">". t("edit") ."</a>";
Dries's avatar
   
Dries committed
131
132
    }

Dries's avatar
   
Dries committed
133
    if ($user->uid && user_access("post blogs")) {
Dries's avatar
   
Dries committed
134
      $links[] = "<a href=\"module.php?mod=blog&op=blog&id=$blog->nid\">". t("blog it") ."</a>";
Dries's avatar
   
Dries committed
135
136
137
    }

    if ($blog->comment) {
Dries's avatar
   
Dries committed
138
      $links[] = "<a href=\"node.php?id=$blog->nid\">". format_plural(node_get_comments($blog->nid), t("comment"), t("comments")) ."</a>";
Dries's avatar
   
Dries committed
139
    }
Dries's avatar
 
Dries committed
140

Dries's avatar
   
Dries committed
141
    $output .= "<tr><td><b>". check_output($blog->title) ."</b></td><td align=\"right\">". $theme->links($links) ."</td></tr>";
Dries's avatar
Dries committed
142
    $output .= "<tr><td colspan=\"2\"><div style=\"margin-left: 20px;\">". check_output($blog->body, 1) ."</div><br /></td></tr>";
Dries's avatar
   
Dries committed
143

Dries's avatar
 
Dries committed
144
  }
Dries's avatar
   
Dries committed
145

Dries's avatar
   
Dries committed
146
  $output .= "</table>";
Dries's avatar
   
Dries committed
147
  $output .= "<a href=\"module.php?mod=blog&op=feed\"><img src=\"". $theme->image("xml.gif") ."\" width=\"36\" height=\"14\" align=\"right\" border=\"0\" /></a>\n";
Dries's avatar
 
Dries committed
148

Dries's avatar
   
Dries committed
149
  $theme->box(t("User blogs"), $output, "main");
Dries's avatar
 
Dries committed
150
151
}

Dries's avatar
   
Dries committed
152
function blog_form($edit) {
Dries's avatar
 
Dries committed
153

Dries's avatar
   
Dries committed
154
  $output = form_textarea(t("Body"), "body", $edit->body, 60, 15, t("Allowed HTML tags") .": ". htmlspecialchars(variable_get("allowed_html", "")));
Dries's avatar
 
Dries committed
155

Dries's avatar
   
Dries committed
156
  return $output;
Dries's avatar
 
Dries committed
157
158
}

Dries's avatar
   
Dries committed
159
160
function blog_save($node) {
  global $user;
Dries's avatar
   
Dries committed
161

Dries's avatar
   
Dries committed
162
  if ($node->nid) {
Dries's avatar
   
Dries committed
163

Dries's avatar
   
Dries committed
164
165
166
167
    /*
    ** Load the original blog from the database to make sure that only
    ** original author can update his blog.
    */
Dries's avatar
   
Dries committed
168

Dries's avatar
   
Dries committed
169
    $blog = node_load(array("nid" => $node->nid));
Dries's avatar
 
Dries committed
170

Dries's avatar
   
Dries committed
171
172
    if ($user->uid && $user->uid == $node->uid && $user->uid == $blog->uid) {
      return array();
Dries's avatar
   
Dries committed
173
    }
Dries's avatar
   
Dries committed
174
175
    else if (user_access("adminster nodes")) {
      return array();
Dries's avatar
   
Dries committed
176
177
    }
    else {
Dries's avatar
   
Dries committed
178
      return 0;
Dries's avatar
   
Dries committed
179
    }
Dries's avatar
 
Dries committed
180
181
  }
  else {
Dries's avatar
   
Dries committed
182
    return array("promote" => 0, "moderate" => 0, "status" => 1);
Dries's avatar
 
Dries committed
183
184
185
186
  }
}

function blog_page() {
Dries's avatar
   
Dries committed
187
  global $theme, $id, $op, $date;
Dries's avatar
Dries committed
188

Dries's avatar
 
Dries committed
189
  if (user_access("access blogs")) {
Dries's avatar
   
Dries committed
190
191
    switch ($op) {
      case "feed":
Dries's avatar
   
Dries committed
192
193
        if ($id) {
          blog_feed_user($id, $date);
Dries's avatar
   
Dries committed
194
195
196
197
198
199
        }
        else {
          blog_feed_last();
        }
        break;
      default:
Dries's avatar
Dries committed
200
        $theme->header();
Dries's avatar
   
Dries committed
201
202
        if ($id) {
          blog_page_user($id, $date);
Dries's avatar
   
Dries committed
203
204
205
206
        }
        else {
          blog_page_last();
        }
Dries's avatar
Dries committed
207
        $theme->footer();
Dries's avatar
 
Dries committed
208
209
    }
  }
Dries's avatar
   
Dries committed
210
  else {
Dries's avatar
Dries committed
211
    $theme->header();
Dries's avatar
Dries committed
212
    $theme->box(t("Access denied"), message_access());
Dries's avatar
Dries committed
213
    $theme->footer();
Dries's avatar
   
Dries committed
214
  }
Dries's avatar
Dries committed
215

Dries's avatar
 
Dries committed
216
217
}

Dries's avatar
   
Dries committed
218
function blog_link($type, $node = 0) {
Dries's avatar
 
Dries committed
219
220
221
  global $user;

  if ($type == "page" && user_access("access blogs")) {
Dries's avatar
   
Dries committed
222
    $links[] = "<a href=\"module.php?mod=blog\">". t("user blogs") ."</a>";
Dries's avatar
 
Dries committed
223
224
225
  }

  if ($type == "menu" && user_access("post blogs")) {
Dries's avatar
   
Dries committed
226
    $links[] = "<a href=\"module.php?mod=node&op=add&type=blog\">". t("add blog entry") ."</a>";
Dries's avatar
   
Dries committed
227
    $links[] = "<a href=\"module.php?mod=blog&op=view&id=$user->uid\">". t("view your blog") ."</a>";
Dries's avatar
 
Dries committed
228
229
  }

Dries's avatar
   
Dries committed
230
  if ($type == "node" && $node->type == "blog") {
Dries's avatar
   
Dries committed
231
    $links[] = "<a href=\"module.php?mod=blog&op=view&id=$node->uid\">". strtr(t("%a's blog"), array("%a" => $node->name)) ."</a>";
Dries's avatar
   
Dries committed
232
233
  }

Dries's avatar
 
Dries committed
234
235
236
237
238
  return $links ? $links : array();
}


function blog_block() {
Dries's avatar
   
Dries committed
239
240
  global $user;

Dries's avatar
   
Dries committed
241
  $result = db_query("SELECT u.uid, u.name, n.created, n.title, n.nid FROM node n LEFT JOIN users u ON n.uid = u.uid WHERE n.type = 'blog' ORDER BY n.nid DESC LIMIT 10");
Dries's avatar
 
Dries committed
242
243

  while ($node = db_fetch_object($result)) {
Dries's avatar
   
Dries committed
244
    $output .= "<a href=\"node.php?id=$node->nid\">". check_output($node->title) ."</a><br />\n";
Dries's avatar
 
Dries committed
245
246
  }

Dries's avatar
   
Dries committed
247
248
249
  $output .= "<br /><div align=\"right\"><a href=\"module.php?mod=blog\">". t("more") ."</a></div>";

  $block[0]["subject"] = t("User blogs");
Dries's avatar
 
Dries committed
250
  $block[0]["content"] = $output;
Dries's avatar
   
Dries committed
251
  $block[0]["info"] = t("User blogs");
Dries's avatar
 
Dries committed
252
253
254
255
256
257
  $block[0]["link"] = "module.php?mod=blog";

  return $block;
}

?>