node.module 15.8 KB
Newer Older
Dries's avatar
 
Dries committed
1 2
<?php

Dries's avatar
 
Dries committed
3 4 5 6 7
class Node {
  function Node($node) {
    global $user;
    $this->userid = $node[userid] ? $node[userid] : $user->userid;
    $this->title = $node[title];
8
    $this->attributes = $node[attributes];
Dries's avatar
 
Dries committed
9 10 11
    $this->timestamp = $node[timestamp] ? $node[timestamp] : time();
  }
}
Dries's avatar
 
Dries committed
12

Dries's avatar
 
Dries committed
13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28
function node_help() {
  global $mod;
 ?>
  <P>Todo.</P>
 <?php

  if ($mod == "node") {
    foreach (module_list() as $name) {
      if (module_hook($name, "status") && $name != "node") {
        print "<H3>". ucfirst($name) ." type</H3>";
        print module_invoke($name, "help");
      }
    }
  }
}

Dries's avatar
CHANGES  
Dries committed
29
function node_conf_filters() {
Dries's avatar
 
Dries committed
30
  $output .= form_select(t("Enable HTML tags"), "filter_html", variable_get("filter_html", 0), array("Disabled", "Enabled"), t("Strip HTML and PHP tags."));
Dries's avatar
CHANGES  
Dries committed
31 32
  $output .= form_textfield(t("Allowed HTML tags"), "allowed_html", variable_get("allowed_html", "<A><B><BLOCKQUOTE><DD><DL><DT><I><LI><OL><U><UL>"), 64, 128, t("If enabled, optionally specify tags which should not be stripped.  'STYLE' attributes, 'ON' attributes and unclosed tags are always stripped."));
  $output .= "<HR>";
Dries's avatar
 
Dries committed
33
  $output .= form_select(t("Enable link tags"), "filter_link", variable_get("filter_link", 0), array("Disabled", "Enabled"), t("Substitute special [[link]] tags."));
Dries's avatar
CHANGES  
Dries committed
34 35 36 37 38 39 40 41 42 43 44 45
  $output .= "<HR>";
  return $output;
}

function node_filter_html($text) {
  $text = eregi_replace("([ \f\r\t\n\'\"])style=[^>]+", "\\1", $text);
  $text = eregi_replace("([ \f\r\t\n\'\"])on[a-z]+=[^>]+", "\\1", $text);
  $text = strip_tags($text, variable_get("allowed_html", ""));
  return $text;
}

function node_filter_link($text) {
Dries's avatar
 
Dries committed
46 47 48 49 50
  $src = array("/\[\[(([^\|]*?)(\|([^\|]*?))?)\]\]/e");  // [link|description]
  $dst = array(format_tag('\\2', '\\4'));                // [link|description]
  return preg_replace($src, $dst, $text);
}

Dries's avatar
CHANGES  
Dries committed
51 52 53 54 55 56
function node_filter($text) {
  if (variable_get("filter_html", 0)) $text = node_filter_html($text);
  if (variable_get("filter_link", 0)) $text = node_filter_link($text);
  return $text;
}

Dries's avatar
Dries committed
57 58 59 60 61 62
function node_cron() {
  db_query("UPDATE node SET status = '". node_status("posted") ."', timestamp_posted = '' WHERE timestamp_posted > 0 AND timestamp_posted < ". time());
  db_query("UPDATE node SET status = '". node_status("queued") ."', timestamp_posted = '' WHERE timestamp_queued > 0 AND timestamp_queued < ". time());
  db_query("UPDATE node SET status = '". node_status("dumped") ."', timestamp_posted = '' WHERE timestamp_hidden > 0 AND timestamp_hidden < ". time());
}

Dries's avatar
 
Dries committed
63 64 65 66 67 68 69 70 71 72 73 74
function node_link($nid, $type) {
  global $op;

  $link[] = ($op == "view") ? "view node" : "<A HREF=\"node.php?id$nid\">view node</A>";
  $link[] = ($op == "content") ? "edit content" : "<A HREF=\"admin.php?mod=node&type=$type&op=content&id=$nid\">edit content</A>";
  $link[] = ($op == "option") ? "edit options" : "<A HREF=\"admin.php?mod=node&op=option&id=$nid\">edit options</A>";
  $link[] = ($op == "status") ? "edit status" : "<A HREF=\"admin.php?mod=node&op=status&id=$nid\">edit status</A>";
  $link[] = ($op == "attribute") ? "edit attribute" : "<A HREF=\"admin.php?mod=node&op=attribute&id=$nid\">edit attributes</A>";
  $link[] = ($op == "delete") ? "delete node" : "<A HREF=\"admin.php?mod=node&op=delete&id=$nid\">delete node</A>";
  return $link;
}

Dries's avatar
 
Dries committed
75
function node_overview($query) {
Dries's avatar
 
Dries committed
76
  global $user;
Dries's avatar
 
Dries committed
77

78
  $color = array("#ffffff", "#e5e5e5");
Dries's avatar
 
Dries committed
79
  $query = node_query($query ? $query : 0);
Dries's avatar
Dries committed
80

Dries's avatar
 
Dries committed
81
  $result = db_query("SELECT n.*, u.userid FROM node n LEFT JOIN users u ON n.author = u.id $query[1] LIMIT 50");
Dries's avatar
 
Dries committed
82

Dries's avatar
 
Dries committed
83
  $output .= status($query[0]);
Dries's avatar
 
Dries committed
84
  $output .= "<TABLE BORDER=\"1\" CELLPADDING=\"2\" CELLSPACING=\"2\">\n";
Dries's avatar
Dries committed
85 86
  $output .= " <TR><TH>title</TH><TH>type</TH><TH>status</TH><TH>meta attributes</TH><TH>author</TH><TH>date</TH></TR>\n";

Dries's avatar
 
Dries committed
87
  while ($node = db_fetch_object($result)) {
Dries's avatar
 
Dries committed
88
    $bg = $color[$i++ % sizeof($color)];
Dries's avatar
Dries committed
89

90
    $output .= " <TR BGCOLOR=\"$bg\"><TD><A HREF=\"node.php?id=$node->nid\">". check_output($node->title) ."</A></TD><TD ALIGN=\"center\">$node->type</TD><TD>". node_status($node->status) ."</TD><TD>". check_output($node->attributes) ."</TD><TD>". format_username($node->userid) ."</TD><TD>". format_date($node->timestamp, "small") ."</TD></TR>\n";
Dries's avatar
 
Dries committed
91
    $output .= " <TR BGCOLOR=\"$bg\"><TD ALIGN=\"right\" COLSPAN=\"6\"><SMALL>". implode(", ", node_link($node->nid, $node->type)) ."</SMALL></TD>\n";
Dries's avatar
 
Dries committed
92 93 94 95 96 97
  }
  $output .= "</TABLE>\n";

  return $output;
}

Dries's avatar
Dries committed
98 99 100
function node_edit_option($id) {
  global $user;

101
  $node = node_get_object(array("nid" => $id));
Dries's avatar
 
Dries committed
102

Dries's avatar
 
Dries committed
103 104 105 106
  $form .= form_item("Title", check_output($node->title));
  $form .= form_item("Operations", implode("<br />", node_link($node->nid, $node->type)));
  $form .= form_select("Comment", "comment", $node->comment, node_comment_status(), "Allow users to post comments to this node.");
  $form .= form_select("Promote", "promote", $node->promote, node_promote_status(), "Promote this node on the main page.");
Dries's avatar
Dries committed
107 108 109
  $form .= form_textfield("Moderate", "moderate", $node->moderate, 35, 255, t("A comma-seperated list of the moderators their usernames."));
  $form .= form_hidden("nid", $node->nid);
  $form .= form_submit("Save node");
Dries's avatar
 
Dries committed
110

Dries's avatar
 
Dries committed
111
  return form("admin.php?mod=node&id=$node->nid", $form);
Dries's avatar
 
Dries committed
112 113
}

Dries's avatar
Dries committed
114
function node_edit_attribute($id) {
Dries's avatar
 
Dries committed
115
  global $user;
Dries's avatar
 
Dries committed
116

117
  $node = node_get_object(array("nid" => $id));
Dries's avatar
 
Dries committed
118

Dries's avatar
 
Dries committed
119 120 121
  $form .= form_item("Title", check_output($node->title));
  $form .= form_item("Operations", implode("<br />", node_link($node->nid, $node->type)));
  $form .= form_textfield("Attributes", "attributes", $node->attributes, 64, 128, "A comma-seperated list of attributes.  Example: 'Software, Webserver, Apache'.");
Dries's avatar
 
Dries committed
122 123
  $form .= form_hidden("nid", $node->nid);
  $form .= form_submit("Save node");
Dries's avatar
 
Dries committed
124

Dries's avatar
 
Dries committed
125
  return form("admin.php?mod=node&id=$node->nid", $form);
Dries's avatar
 
Dries committed
126 127
}

Dries's avatar
Dries committed
128 129 130 131 132
function node_edit_status($id) {
  global $REQUEST_URI;

  $node = node_get_object(array("nid" => $id));

Dries's avatar
 
Dries committed
133 134
  $form .= form_item("Title", check_output($node->title));
  $form .= form_item("Operations", implode("<br />", node_link($node->nid, $node->type)));
Dries's avatar
Dries committed
135 136 137 138 139 140 141 142 143 144
  $form .= form_select("Current status", "status", $node->status, node_status($node->type));
  $form .= form_textfield("Automatically post document", "timestamp_posted", ($node->timestamp_posted ? format_date($node->timestamp_posted) : ""), 30, 55, "The date at which your document will be automatically posted.  Leave empty if you don't want to schedule this document, or fill out a string containing an English date format.  Example input: '". date("j F Y G:i") ."', '". date("m/d/y H:i") ."', '". date("F j, Y H:i") ."', ...");
  $form .= form_textfield("Automatically queue document", "timestamp_queued", ($node->timestamp_queued ? format_date($node->timestamp_queued) : ""), 30, 55, "The date at which your document will be automatically queued.  Leave empty if you don't want to schedule this document, or fill out a string containing an English date format.  Example input: '". date("j F Y G:i") ."', '". date("m/d/y H:i") ."', '". date("F j, Y H:i") ."', ...");
  $form .= form_textfield("Automatically hide document", "timestamp_hidden", ($node->timestamp_hidden ? format_date($node->timestamp_hidden) : ""), 30, 55, "The date at which your document will be automatically hidden.  Leave empty if you don't want to schedule this document, or fill out a string containing an English date format.  Example input: '". date("j F Y G:i") ."', '". date("m/d/y H:i") ."', '". date("F j, Y H:i") ."', ...");
  $form .= form_hidden("nid", $node->nid);
  $form .= form_submit("Save node");

  return form($REQUEST_URI, $form);
}

Dries's avatar
 
Dries committed
145 146 147 148 149 150 151 152 153 154
function node_edit_content($edit, $type) {
  $edit[type] = $type;
  return node_invoke($edit, "form");
}

function node_save_content($edit, $type) {
  $edit[type] = $type;
  return node_invoke($edit, "save");
}

Dries's avatar
 
Dries committed
155
function node_delete($id) {
Dries's avatar
 
Dries committed
156
  return (node_del(array("nid" => $id)) ? "node has been deleted." : "failed to delete node: set node status to 'dumped' first.");
Dries's avatar
 
Dries committed
157 158
}

Dries's avatar
 
Dries committed
159
function node_query($type = -1) {
Dries's avatar
Dries committed
160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179
  $queries[] = array("all nodes: recent additions", "ORDER BY n.timestamp DESC");
  $queries[] = array("all nodes: status set to 'posted'", "WHERE n.status = '". node_status("posted") ."' ORDER BY n.timestamp DESC");
  $queries[] = array("all nodes: status set to 'queued'", "WHERE n.status = '". node_status("queued") ."' ORDER BY n.timestamp DESC");
  $queries[] = array("all nodes: status set to 'dumped'", "WHERE n.status = '". node_status("dumped") ."' ORDER BY n.timestamp DESC");
  $queries[] = array("all nodes: scheduled to be posted", "WHERE n.timestamp_posted > 0 ORDER BY n.timestamp DESC");
  $queries[] = array("all nodes: scheduled to be queued", "WHERE n.timestamp_queued > 0 ORDER BY n.timestamp DESC");
  $queries[] = array("all nodes: scheduled to be hidden", "WHERE n.timestamp_hidden > 0 ORDER BY n.timestamp DESC");

  foreach (module_list() as $name) {
    if (module_hook($name, "user")) {
      $queries[] = array("$name: recent additions", "WHERE n.type = '$name' ORDER BY n.timestamp DESC");
      $queries[] = array("$name: status set to 'posted'", "WHERE n.type = '$name' AND n.status = '". node_status("posted") ."' ORDER BY n.timestamp DESC");
      $queries[] = array("$name: status set to 'queued'", "WHERE n.type = '$name' AND n.status = '". node_status("queued") ."' ORDER BY n.timestamp DESC");
      $queries[] = array("$name: status set to 'dumped'", "WHERE n.type = '$name' AND n.status = '". node_status("dumped") ."' ORDER BY n.timestamp DESC");
      $queries[] = array("$name: scheduled to be posted", "WHERE n.type = '$name' AND n.timestamp_posted > 0 ORDER BY n.timestamp DESC");
      $queries[] = array("$name: scheduled to be queued", "WHERE n.type = '$name' AND n.timestamp_queued > 0 ORDER BY n.timestamp DESC");
      $queries[] = array("$name: scheduled to be hidden", "WHERE n.type = '$name' AND n.timestamp_hidden > 0 ORDER BY n.timestamp DESC");
    }
  }

Dries's avatar
 
Dries committed
180
  return ($type < 0 ? $queries : $queries[$type]);
Dries's avatar
 
Dries committed
181 182
}

Dries's avatar
 
Dries committed
183 184 185
function node_listing($queries) {
  global $mod;
  foreach ($queries as $key=>$array) {
Dries's avatar
 
Dries committed
186
    $output .= "<LI><A HREF=\"admin.php?mod=$mod&query=$key\">$array[0]</A></LI>\n";
Dries's avatar
 
Dries committed
187 188 189 190
  }
  return "<OL>$output</OL>\n";
}

Dries's avatar
 
Dries committed
191 192 193 194 195 196 197 198 199 200
function node_setting() {
  global $REQUEST_URI;

  $threshold_post = array(1 => 1, 2 => 2, 3 => 3, 4 => 4, 5 => 5, 6 => 6, 7 => 7, 8 => 8, 9 => 9, 10 => 10, 11 => 11, 12 => 12, 13 => 13, 14 => 14, 15 => 15, 20 => 20, 25 => 25, 30 => 30, 35 => 35, 40 => 40, 45 => 45, 50 => 50, 60 => 60, 70 => 70, 80 => 80, 90 => 90, 100 => 100);
  $threshold_dump = array(-1 => -1, -2 => -2, -3 => -3, -4 => -4, -5 => -5, -6 => -6, -7 => -7, -8 => -8, -9 => -9, -10 => -10, -11 => -11, -12 => -12, -13 => -13, -14 => -14, -15 => -15, -20 => -20, -25 => -25, -30 => -30);
  $threshold_expire = array(1 => 1, 2 => 2, 3 => 3, 4 => 4, 5 => 5, 6 => 6, 7 => 7, 8 => 8, 9 => 9, 10 => 10, 11 => 11, 12 => 12, 13 => 13, 14 => 14, 15 => 15, 20 => 20, 25 => 25, 30 => 30, 35 => 35, 40 => 40, 45 => 45, 50 => 50, 60 => 60, 70 => 70, 80 => 80, 90 => 90, 100 => 100);

  foreach (module_list() as $name) {
    if (module_hook($name, "user")) {
      $form .= "<H3>Default settings for $name nodes</H3>";
Dries's avatar
Dries committed
201 202 203 204 205 206 207
      $form .= form_select(t("Comment"), $name ."_comment", variable_get($name ."_comment", 0), node_comment_status(), "By default, allow or dissallow users to post comments in this category.");
      $form .= form_select(t("Promote"), $name ."_promote", variable_get($name ."_promote", 0), node_promote_status(), "By default, promote new submissions in this category to the front page.");
      $form .= form_select(t("Status"), $name ."_status", variable_get($name ."_status", node_status("queued")), node_status($name), "What to do with new submissions in this category?");
      $form .= form_select(t("Post threshold"), $name ."_post", variable_get($name ."_post", 4), $threshold_post, "If new submissions are subject to moderation, select a post threshold.");
      $form .= form_select(t("Dump threshold"), $name ."_dump", variable_get($name ."_dump", -2), $threshold_dump, "If new submissions are subject to moderation, select a dump threshold.");
      $form .= form_select(t("Expiration threshold"), $name ."_expire", variable_get($name ."_expire", 8), $threshold_expire, "If new submissions are subject to moderation, select a expiration threshold.");
      $form .= form_textfield("Moderate", $name ."_moderate", variable_get($name ."_moderate", ""), 35, 255, "A comma-seperated list of the moderators' usernames.");
Dries's avatar
 
Dries committed
208 209 210 211 212 213 214 215 216
    }
  }

  $form .= form_submit("Save settings");
  $form .= form_submit("Reset to defaults");

  return form($REQUEST_URI, $form);
}

Dries's avatar
Dries committed
217 218 219 220 221 222 223
function node_admin_save($edit) {
  if (isset($edit[status])) {
    $edit[timestamp_posted] = (strtotime($edit[timestamp_posted]) > time()) ? strtotime($edit[timestamp_posted]) : 0;
    $edit[timestamp_queued] = (strtotime($edit[timestamp_queued]) > time()) ? strtotime($edit[timestamp_queued]) : 0;
    $edit[timestamp_hidden] = (strtotime($edit[timestamp_hidden]) > time()) ? strtotime($edit[timestamp_hidden]) : 0;
    node_save($edit, array(status, timestamp_posted, timestamp_queued, timestamp_hidden));
  }
224 225
  else if (isset($edit[attributes])) {
    node_save($edit, array(attributes));
Dries's avatar
Dries committed
226 227 228 229 230 231
  }
  else {
    node_save($edit, array(comment, moderate, promote));
  }
}

Dries's avatar
 
Dries committed
232
function node_module_find() {
Dries's avatar
Dries committed
233 234 235 236 237 238 239 240 241 242
  global $REQUEST_URI;

  foreach (module_list() as $name) {
    if (module_hook($name, "user")) {
      $options .= "<OPTION VALUE=\"$name\">$name</OPTION>\n";
    }
  }

  $output .= "<FORM ACTION=\"$REQUEST_URI\" METHOD=\"POST\">\n";
  $output .= " <INPUT SIZE=\"50\" VALUE=\"". check_form($keys) ."\" NAME=\"keys\" TYPE=\"text\">\n";
Dries's avatar
 
Dries committed
243
  $output .= " <SELECT NAME=\"type\">$options</SELECT>\n";
Dries's avatar
Dries committed
244 245 246 247 248 249 250
  $output .= " <INPUT TYPE=\"submit\" VALUE=\"Search\">\n";
  $output .= "</FORM>\n";

  return $output;
}


Dries's avatar
 
Dries committed
251 252 253 254
function node_edit($node) {
  $output .= form_item("Title", $node->title);
  $output .= form_item("Operations", implode("<br />", node_link($node->nid, $node->type)));
  return $output;
Dries's avatar
Dries committed
255 256
}

Dries's avatar
 
Dries committed
257
function node_admin() {
Dries's avatar
Dries committed
258
  global $op, $id, $edit, $query, $type, $keys;
Dries's avatar
 
Dries committed
259

Dries's avatar
Dries committed
260 261 262 263 264 265
  foreach (module_list() as $name) {
    if (module_hook($name, "status") && $name != "node") {
      $link[] = "<A HREF=\"admin.php?mod=node&type=$name&op=add\">add $name</A>";
    }
  }

Dries's avatar
 
Dries committed
266
  print "<SMALL>". implode(" | ", $link) ." | <A HREF=\"admin.php?mod=node&op=default\">node settings</A> | <A HREF=\"admin.php?mod=node&op=listing\">node listings</A> | <A HREF=\"admin.php?mod=node&op=search\">search node</A> | <A HREF=\"admin.php?mod=node\">overview</A> | <A HREF=\"admin.php?mod=node&op=help\">help</A></SMALL><HR>\n";
Dries's avatar
 
Dries committed
267 268 269 270

  $id = check_input($edit[nid] ? $edit[nid] : $id);

  switch ($op) {
Dries's avatar
Dries committed
271 272 273
    case "add":
      print module_invoke($type, "form");
      break;
Dries's avatar
 
Dries committed
274 275 276
    case "help":
      print node_help();
      break;
Dries's avatar
Dries committed
277
    case "search":
Dries's avatar
 
Dries committed
278
      print node_module_find($id);
Dries's avatar
Dries committed
279 280 281 282 283 284 285 286 287 288
      print search_data($keys, $type);
      break;
    case "status":
      print node_edit_status($id);
      break;
    case "option":
      print node_edit_option($id);
      break;
    case "attribute":
      print node_edit_attribute($id);
Dries's avatar
 
Dries committed
289
      break;
Dries's avatar
 
Dries committed
290 291 292
    case "content":
      print node_edit_content(node_get_array(array("nid" => $id)), $type);
      break;
Dries's avatar
 
Dries committed
293 294 295
    case "default":
      print node_setting();
      break;
Dries's avatar
Dries committed
296
    case "delete":
Dries's avatar
 
Dries committed
297
      print status(node_delete($id));
Dries's avatar
 
Dries committed
298
      print node_overview($query);
Dries's avatar
 
Dries committed
299
      break;
Dries's avatar
 
Dries committed
300
    case "listing":
Dries's avatar
 
Dries committed
301
      print node_listing(node_query());
Dries's avatar
 
Dries committed
302
      break;
Dries's avatar
 
Dries committed
303
    case "Save settings":
Dries's avatar
 
Dries committed
304
      print status(conf_save($edit));
Dries's avatar
 
Dries committed
305
      print node_setting();
Dries's avatar
 
Dries committed
306 307 308
      break;
    case "Reset to defaults":
      print status(conf_default($edit));
Dries's avatar
 
Dries committed
309
      print node_setting();
Dries's avatar
 
Dries committed
310
      break;
Dries's avatar
 
Dries committed
311
    case "Save node":
Dries's avatar
Dries committed
312
      print node_admin_save($edit);
Dries's avatar
 
Dries committed
313
      print node_overview($query);
Dries's avatar
Dries committed
314 315
      break;
    case "edit":
Dries's avatar
 
Dries committed
316 317 318 319
      print node_edit(node_get_object(array("nid" => $id)));
      break;
    case "view":
      print node_module_view(node_get_array(array("nid" => $id)), $type);
Dries's avatar
 
Dries committed
320
      break;
Dries's avatar
Dries committed
321
    case "Preview":
Dries's avatar
 
Dries committed
322
      print node_edit_content($edit, $type);
Dries's avatar
 
Dries committed
323
      break;
Dries's avatar
Dries committed
324
    case "Submit":
Dries's avatar
 
Dries committed
325
      print status(node_save_content($edit, $type));
Dries's avatar
Dries committed
326
      // fall through:
Dries's avatar
 
Dries committed
327
    default:
Dries's avatar
 
Dries committed
328
      print node_overview($query);
Dries's avatar
 
Dries committed
329 330 331 332
  }
}

?>