node.inc 5.71 KB
Newer Older
Dries's avatar
 
Dries committed
1 2
<?php

Dries's avatar
 
Dries committed
3
$status = array(dumped => 0, expired => 1, queued => 2, posted => 3);
Dries's avatar
 
Dries committed
4
$rstatus = array(0 => dumped, 1 => expired, 2 => queued, 3 => posted);
Dries's avatar
 
Dries committed
5

Dries's avatar
 
Dries committed
6
function _node_get($field, $value) {
Dries's avatar
 
Dries committed
7
  $result = db_query("SELECT lid, type FROM node WHERE $field = '$value'");
Dries's avatar
 
Dries committed
8
  if ($node = db_fetch_object($result)) {
Dries's avatar
 
Dries committed
9
    return db_query("SELECT n.*, l.*, u.userid FROM node n LEFT JOIN $node->type l ON n.lid = l.lid AND n.nid = l.nid LEFT JOIN users u ON n.author = u.id WHERE n.$field = '$value' ORDER BY n.timestamp DESC");
Dries's avatar
 
Dries committed
10 11 12 13 14 15 16 17 18 19 20
  }
}

function node_get_object($field, $value) {
  return db_fetch_object(_node_get($field, $value));
}

function node_get_array($field, $value) {
  return db_fetch_array(_node_get($field, $value));
}

Dries's avatar
 
Dries committed
21 22 23 24 25 26 27
function node_get_category($nid) {
  db_fetch_array(db_query("SELECT FROM"));
}

function node_get_topic($nid) {
}

Dries's avatar
 
Dries committed
28 29
function node_del($field, $value) {
  global $status;
Dries's avatar
 
Dries committed
30
  if ($node = node_get_object($field, $value)) {
Dries's avatar
 
Dries committed
31
    if ($node->status == $status[dumped]) {
Dries's avatar
 
Dries committed
32 33 34
      db_query("DELETE FROM node WHERE nid = '$node->nid'");
      db_query("DELETE FROM $node->type WHERE lid = '$node->lid' AND nid = '$node->nid'");
      db_query("DELETE FROM comments WHERE lid = '$node->nid'");
Dries's avatar
 
Dries committed
35
      watchdog("message", "node: deleted '$node->title'");
Dries's avatar
 
Dries committed
36 37
      return $node;
    }
Dries's avatar
 
Dries committed
38 39 40 41
  }
}

function node_save($node) {
Dries's avatar
 
Dries committed
42
  global $user, $status;
Dries's avatar
 
Dries committed
43

Dries's avatar
 
Dries committed
44
  $rows = array(nid, pid, lid, log, type, title, score, votes, author, status, timestamp);
Dries's avatar
 
Dries committed
45

Dries's avatar
 
Dries committed
46
  if ($node[nid] > 0) {
Dries's avatar
 
Dries committed
47 48
    $n = node_get_object("nid", $node[nid]);

Dries's avatar
 
Dries committed
49 50 51 52 53 54 55 56 57 58 59 60
    $u1 = array();
    $u2 = array();

    foreach ($node as $field=>$value) {
      if (in_array($field, $rows)) {
        array_push($u1, check_input($field) ." = '". check_input($value) ."'");
      }
      else {
        array_push($u2, check_input($field) ." = '". check_input($value) ."'");
      }
    }

Dries's avatar
 
Dries committed
61
    if ($u1 = implode(", ", $u1)) db_query("UPDATE node SET $u1 WHERE nid = '$node[nid]'");
Dries's avatar
 
Dries committed
62
    if ($u2 = implode(", ", $u2)) db_query("UPDATE $n->type SET $u2 WHERE nid = '$node[nid]'");
Dries's avatar
 
Dries committed
63
    if ($n->pid && ($node[status] == $status[posted])) db_query("UPDATE node SET status = '$status[expired]' WHERE nid = '$n->pid'");
Dries's avatar
 
Dries committed
64

Dries's avatar
 
Dries committed
65
    watchdog("special", "node: modified '$n->title'");
Dries's avatar
 
Dries committed
66 67

    return $node[nid];
Dries's avatar
 
Dries committed
68 69
  }
  else {
Dries's avatar
 
Dries committed
70
    $duplicate = node_get_object("title", $node[title]);
Dries's avatar
 
Dries committed
71

Dries's avatar
 
Dries committed
72
    if ($duplicate && (time() - $duplicate->timestamp < 60)) {
Dries's avatar
 
Dries committed
73
      watchdog("warning", "node: duplicate '$node[title]'");
Dries's avatar
 
Dries committed
74
    }
Dries's avatar
 
Dries committed
75
    else {
Dries's avatar
 
Dries committed
76 77
      throttle("post node", variable_get(max_node_rate, 900));

Dries's avatar
 
Dries committed
78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95
      // setup default values:
      $node = array_merge(array(title => "?", author => $user->id, type => "?", pid => 0, log => "node created", status => $status[queued], score => 0, votes => 0, timestamp => time()), $node);

      // prepare queries:
      $f1 = array();
      $v1 = array();
      $f2 = array();
      $v2 = array();

      foreach ($node as $field=>$value) {
        if (in_array($field, $rows)) {
          array_push($f1, check_input($field));
          array_push($v1, "'". check_input($value) ."'");
        }
        else {
          array_push($f2, check_input($field));
          array_push($v2, "'". check_input($value) ."'");
        }
Dries's avatar
 
Dries committed
96
      }
Dries's avatar
 
Dries committed
97 98 99 100 101 102

      $f1 = implode(", ", $f1);
      $v1 = implode(", ", $v1);
      $f2 = implode(", ", $f2);
      $v2 = implode(", ", $v2);

Dries's avatar
 
Dries committed
103 104 105 106 107 108 109
      // insert data, try to roll-back when something goes wrong:
      $result = db_query("INSERT INTO node ($f1) VALUES ($v1)");
      if ($result && $nid = db_insert_id()) {
        $result = db_query("INSERT INTO $node[type] ($f2, nid) VALUES ($v2, $nid)");
        if ($result && $lid = db_insert_id()) {
          $result = db_query("UPDATE node SET lid = '$lid' WHERE nid = '$nid'");
          if ($result) {
Dries's avatar
 
Dries committed
110 111 112
            if (($node[pid]) && ($node[status] == $status[posted])) {
              db_query("UPDATE node SET status = '$status[expired]' WHERE nid = '$node[pid]'");
            }
Dries's avatar
 
Dries committed
113
            watchdog("special", "node: added '$node[title]'");
Dries's avatar
 
Dries committed
114 115 116 117
          }
          else {
            watchdog("warning", "node: added '$node[title]' - failed");
          }
Dries's avatar
 
Dries committed
118 119
        }
        else {
Dries's avatar
 
Dries committed
120
          db_query("DELETE FROM node WHERE nid = '$nid'");
Dries's avatar
 
Dries committed
121
          watchdog("warning", "node: added '$node[title]' - failed");
Dries's avatar
 
Dries committed
122
        }
Dries's avatar
 
Dries committed
123
      }
Dries's avatar
 
Dries committed
124 125 126
      else {
        watchdog("warning", "node: added '$node[title]' - failed");
      }
Dries's avatar
 
Dries committed
127
    }
Dries's avatar
 
Dries committed
128

Dries's avatar
 
Dries committed
129 130
    return $nid;
  }
Dries's avatar
 
Dries committed
131 132
}

Dries's avatar
 
Dries committed
133 134 135 136 137 138
function node_invoke($node, $name, $arg = 0) {
  if ($node[type]) $function = $node[type] ."_$name";
  if ($node->type) $function = $node->type ."_$name";
  if ($function) return ($arg ? $function($node) : $function($node, $arg));
}

Dries's avatar
 
Dries committed
139
function node_view($node, $page) {
Dries's avatar
 
Dries committed
140
  return node_invoke($node, "view", $page);
Dries's avatar
 
Dries committed
141 142 143
}

function node_form($node) {
Dries's avatar
 
Dries committed
144 145 146 147 148
  return node_invoke($node, "form");
}

function node_status($node) {
  return node_invoke($node, "status");
Dries's avatar
 
Dries committed
149 150
}

Dries's avatar
 
Dries committed
151
function node_control($node) {
Dries's avatar
 
Dries committed
152 153 154 155 156 157 158 159 160 161 162 163 164 165
  global $REQUEST_URI;

 ?>
  <SCRIPT>
   <!--//
     function visit(site) {
       if (site != "") {
         parent.location = site
       }
     }
   //-->
  </SCRIPT>
 <?php

Dries's avatar
 
Dries committed
166
  $choices = array("node.php?id=$node->nid" => t("view node"), "submit.php?mod=$node->type" => t("add node"), "submit.php?mod=$node->type&op=update&id=$node->nid" => t("update node"),  "node.php?op=history&id=$node->nid" => t("view history"));
Dries's avatar
 
Dries committed
167 168

  $output .= "<FORM METHOD=\"get\" ACTION=\"\">\n";
Dries's avatar
 
Dries committed
169
  foreach ($choices as $key => $value) $options .= "<OPTION VALUE=\"$key\"". ("/$key" == $REQUEST_URI ? " SELECTED" : "") .">". check_select($value) ."</OPTION>\n";
Dries's avatar
 
Dries committed
170 171 172 173 174 175 176
  $output .= " <SELECT NAME=\"op\" ONCHANGE=\"visit(this.options[this.selectedIndex].value)\">$options</SELECT>\n";
  $output .= "</FORM>\n";

  return $output;
}

function node_visible($node) {
Dries's avatar
 
Dries committed
177 178
  global $user, $status;
  return ($node->status == $status[posted]) || ($node->status == $status[queued] && $user->id) || user_access($user, "node");
Dries's avatar
 
Dries committed
179 180 181
}

?>