node.inc 8.06 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

6
function _node_get($conditions) {
Dries's avatar
 
Dries committed
7
  foreach ($conditions as $key=>$value) $cond[] = "n.". check_query($key) ." = '". check_query($value) ."'";
Dries's avatar
 
Dries committed
8 9 10 11 12 13 14 15
  $where = implode(" AND ", $cond);

  if ($conditions[type]) {
    $type = $conditions[type];
  }
  else {
    $node = db_fetch_object(db_query("SELECT n.type FROM node n WHERE $where"));
    $type = $node ? $node->type : 0;
16
  }
Dries's avatar
 
Dries committed
17 18 19

  if ($type) {
    return db_query("SELECT n.*, l.*, u.userid FROM node n LEFT JOIN $type l ON n.lid = l.lid AND n.nid = l.nid LEFT JOIN users u ON n.author = u.id WHERE $where ORDER BY n.timestamp DESC");
Dries's avatar
 
Dries committed
20 21 22
  }
}

Dries's avatar
 
Dries committed
23
function node_comment_status($index = -1) {
Dries's avatar
 
Dries committed
24
  $status = array("Disabled", "Enabled");
Dries's avatar
 
Dries committed
25 26 27 28
  return $index < 0 ? $status : $status[$index];
}

function node_promote_status($index = -1) {
Dries's avatar
 
Dries committed
29
  $status = array("Disabled", "Enabled");
Dries's avatar
 
Dries committed
30 31 32 33
  return $index < 0 ? $status : $status[$index];
}

function node_submission_status($index = -1) {
Dries's avatar
 
Dries committed
34
  $status = array("Auto-post new submissions", "Moderate new submissions");
Dries's avatar
 
Dries committed
35 36 37
  return $index < 0 ? $status : $status[$index];
}

38 39
function node_get_object($conditions) {
  return db_fetch_object(_node_get($conditions));
Dries's avatar
 
Dries committed
40 41
}

42 43
function node_get_array($conditions) {
  return db_fetch_array(_node_get($conditions));
Dries's avatar
 
Dries committed
44 45
}

46
function node_del($conditions) {
Dries's avatar
 
Dries committed
47
  global $status;
48
  if ($node = node_get_object($conditions)) {
Dries's avatar
 
Dries committed
49
    if ($node->status == $status[dumped]) {
Dries's avatar
 
Dries committed
50
      module_invoke($node->type, "delete", $node);
Dries's avatar
 
Dries committed
51 52 53
      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
54
      watchdog("message", "node: deleted '$node->title'");
Dries's avatar
 
Dries committed
55 56
      return $node;
    }
Dries's avatar
 
Dries committed
57 58 59
  }
}

Dries's avatar
 
Dries committed
60
function node_get_comments($nid) {
Dries's avatar
 
Dries committed
61
  $comment = db_fetch_object(db_query("SELECT COUNT(c.lid) AS number FROM node n LEFT JOIN comments c ON n.nid = c.lid WHERE n.nid = '$nid' GROUP BY n.nid"));
Dries's avatar
 
Dries committed
62 63 64
  return $comment->number ? $comment->number : 0;
}

Dries's avatar
 
Dries committed
65
function node_save($node, $filter) {
Dries's avatar
 
Dries committed
66
  $rows = array(nid, lid, cid, tid, type, title, score, votes, author, status, comment, promote, moderate, attributes, timestamp, timestamp_posted, timestamp_queued, timestamp_hidden);
Dries's avatar
 
Dries committed
67

Dries's avatar
 
Dries committed
68
  if ($node[nid] > 0) {
69
    $n = node_get_object(array("nid" => $node[nid]));
Dries's avatar
 
Dries committed
70

Dries's avatar
 
Dries committed
71 72 73
    foreach ($filter as $field=>$value) {
      $f = check_input(is_numeric($field) ? $value : $field);
      $v = check_input(is_numeric($field) ? $node[$value] : $filter[$field]);
Dries's avatar
 
Dries committed
74

Dries's avatar
 
Dries committed
75 76 77 78 79
      if (in_array($f, $rows)) {
        $u1[] = check_input($f) ." = '". check_input($v) ."'";
      }
      else {
        $u2[] = check_input($f) ." = '". check_input($v) ."'";
Dries's avatar
 
Dries committed
80 81 82
      }
    }

Dries's avatar
 
Dries committed
83 84
    if ($u1) db_query("UPDATE node SET ". implode(", ", $u1) ." WHERE nid = '$node[nid]'");
    if ($u2) db_query("UPDATE $n->type SET ". implode(", ", $u2) ." WHERE nid = '$node[nid]'");
Dries's avatar
 
Dries committed
85 86

    if ($node[nid]) module_invoke($n->type, "update", node_get_object(array(nid => $n->nid)));
Dries's avatar
 
Dries committed
87

Dries's avatar
 
Dries committed
88
    return $node[nid];
Dries's avatar
 
Dries committed
89 90
  }
  else {
91
    $duplicate = node_get_object(array("title" => $node[title]));
Dries's avatar
 
Dries committed
92

Dries's avatar
 
Dries committed
93
    if ($duplicate && (time() - $duplicate->timestamp < 60)) {
Dries's avatar
 
Dries committed
94
      watchdog("warning", "node: duplicate '$node[title]'");
Dries's avatar
 
Dries committed
95
    }
Dries's avatar
 
Dries committed
96
    else {
Dries's avatar
 
Dries committed
97
      // verify submission rate:
Dries's avatar
 
Dries committed
98
      throttle("node", variable_get("max_node_rate", 900));
Dries's avatar
 
Dries committed
99

Dries's avatar
 
Dries committed
100
      // prepare queries:
Dries's avatar
 
Dries committed
101
      foreach ($filter as $field=>$value) {
Dries's avatar
 
Dries committed
102
        $f = check_input(is_numeric($field) ? $value : $field);
Dries's avatar
 
Dries committed
103
        $v = check_input(is_numeric($field) ? $node[$value] : $filter[$field]);
Dries's avatar
 
Dries committed
104

Dries's avatar
 
Dries committed
105 106
        if (in_array($f, $rows)) {
          $f1[] = $f;
Dries's avatar
 
Dries committed
107
          $v1[] = "'$v'";
Dries's avatar
 
Dries committed
108 109
        }
        else {
Dries's avatar
 
Dries committed
110
          $f2[] = $f;
Dries's avatar
 
Dries committed
111
          $v2[] = "'$v'";
Dries's avatar
 
Dries committed
112
        }
Dries's avatar
 
Dries committed
113
      }
Dries's avatar
 
Dries committed
114 115 116 117 118 119

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

Dries's avatar
 
Dries committed
120
      // insert data, try to roll-back when something goes wrong:
Dries's avatar
 
Dries committed
121
      $result = db_query("INSERT INTO node ($f1) VALUES ($v1)");
Dries's avatar
 
Dries committed
122
      if ($result && $nid = db_insert_id()) {
Dries's avatar
 
Dries committed
123
        $result = db_query("INSERT INTO $filter[type] ($f2, nid) VALUES ($v2, $nid)");
Dries's avatar
 
Dries committed
124
        if ($result && $lid = db_insert_id()) {
Dries's avatar
 
Dries committed
125
          $result = db_query("UPDATE node SET lid = '$lid' WHERE nid = '$nid'");
Dries's avatar
 
Dries committed
126
          if ($result) {
Dries's avatar
 
Dries committed
127
            watchdog("special", "node: added $filter[type] '$node[title]'");
Dries's avatar
 
Dries committed
128 129
          }
          else {
Dries's avatar
 
Dries committed
130
            watchdog("warning", "node: added $filter[type] '$node[title]' - failed");
Dries's avatar
 
Dries committed
131
          }
Dries's avatar
 
Dries committed
132 133
        }
        else {
Dries's avatar
 
Dries committed
134
          db_query("DELETE FROM node WHERE nid = '$nid'");
Dries's avatar
 
Dries committed
135
          watchdog("warning", "node: added $filter[type] '$node[title]' - failed");
Dries's avatar
 
Dries committed
136
        }
Dries's avatar
 
Dries committed
137
      }
Dries's avatar
 
Dries committed
138
      else {
Dries's avatar
 
Dries committed
139
        watchdog("warning", "node: added $filter[type] '$node[title]' - failed");
Dries's avatar
 
Dries committed
140
      }
Dries's avatar
 
Dries committed
141
    }
Dries's avatar
 
Dries committed
142

Dries's avatar
 
Dries committed
143 144
    if ($nid) module_invoke($filter[type], "insert", node_get_object(array(nid => $nid)));

Dries's avatar
 
Dries committed
145 146
    return $nid;
  }
Dries's avatar
 
Dries committed
147 148
}

Dries's avatar
 
Dries committed
149
function node_invoke($node, $name, $arg = 0) {
Dries's avatar
 
Dries committed
150 151 152
  if (is_array($node)) $function = $node[type] ."_$name";
  else if (is_object($node)) $function = $node->type ."_$name";
  else if (is_string($node)) $function = $node ."_$name";
natrak's avatar
Changes  
natrak committed
153
  if (function_exists($function)) return ($arg ? $function($node, $arg) : $function($node));
Dries's avatar
 
Dries committed
154 155
}

Dries's avatar
 
Dries committed
156 157
function node_view($node, $main = 0) {
  return node_invoke($node, "view", $main);
Dries's avatar
 
Dries committed
158 159 160
}

function node_form($node) {
Dries's avatar
 
Dries committed
161 162 163
  return node_invoke($node, "form");
}

Dries's avatar
 
Dries committed
164
function node_status($value) {
Dries's avatar
 
Dries committed
165
  $status = array(dumped, expired, queued, posted);
Dries's avatar
 
Dries committed
166 167 168 169 170 171 172 173 174 175
  if (module_exist($value)) {
    return array_intersect($status, node_invoke($value, "status"));
  }
  else if (strlen($value) > 3) {
    $status = array_flip($status);
    return $status[$value];
  }
  else {
    return $status[$value];
  }
Dries's avatar
 
Dries committed
176 177
}

Dries's avatar
 
Dries committed
178
function node_control($node) {
Dries's avatar
 
Dries committed
179
  global $user, $REQUEST_URI;
Dries's avatar
 
Dries committed
180 181 182 183 184 185 186 187 188 189 190 191 192

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

Dries's avatar
 
Dries committed
193
  if ($user->id) {
194
    $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
195 196
  }
  else {
197
    $choices = array("node.php?id=$node->nid" => t("view node"), "node.php?op=history&id=$node->nid" => t("view history"));
Dries's avatar
 
Dries committed
198
  }
Dries's avatar
 
Dries committed
199 200

  $output .= "<FORM METHOD=\"get\" ACTION=\"\">\n";
Dries's avatar
CHANGES  
Dries committed
201
  foreach ($choices as $key => $value) $options .= "<OPTION VALUE=\"$key\"". (strstr($REQUEST_URI,"/$key") ? " SELECTED" : "") .">". check_form($value) ."</OPTION>\n";
Dries's avatar
 
Dries committed
202 203 204 205 206 207
  $output .= " <SELECT NAME=\"op\" ONCHANGE=\"visit(this.options[this.selectedIndex].value)\">$options</SELECT>\n";
  $output .= "</FORM>\n";

  return $output;
}

Dries's avatar
CHANGES  
Dries committed
208 209
function node_preview($node) {
  foreach ($node as $key=>$value) {
Dries's avatar
 
Dries committed
210
    if ($value) $node[$key] = is_array($value) ? node_preview($value) : check_preview($value);
Dries's avatar
CHANGES  
Dries committed
211 212 213 214
  }
  return $node;
}

Dries's avatar
 
Dries committed
215

Dries's avatar
 
Dries committed
216 217
function node_attributes_edit($type, $edit) {
  return meta_form($type, $edit);
Dries's avatar
 
Dries committed
218 219
}

Dries's avatar
 
Dries committed
220 221
function node_attributes_save($type, $edit) {
  return meta_save($type, $edit);
Dries's avatar
 
Dries committed
222 223
}

Dries's avatar
 
Dries committed
224
function node_attributes_view($string) {
Dries's avatar
 
Dries committed
225 226
  foreach (explode(",", $string) as $attribute) {
    if ($attribute = trim($attribute)) {
Dries's avatar
 
Dries committed
227
      $array[] = "<a href=\"index.php?meta=". urlencode($attribute) ."\">$attribute</a>";
Dries's avatar
 
Dries committed
228 229 230 231 232
    }
  }
  return $array ? $array : array();
}

233 234 235 236
function node_index($node) {
  return $node->attributes ? implode(" / ", node_attributes_view($node->attributes)) : "&nbsp;";
}

Dries's avatar
 
Dries committed
237
function node_visible($node) {
Dries's avatar
 
Dries committed
238
  global $user, $status;
Dries's avatar
 
Dries committed
239
  return ($node->status == $status[posted]) || ($node->status == $status[queued] && $user->id) || user_access($user, $node->type) || user_access($user, "node");
Dries's avatar
 
Dries committed
240 241
}

Dries's avatar
 
Dries committed
242 243 244 245
function node_access($account, $node) {
  return strstr($node->moderate, $account->userid);
}

natrak's avatar
Changes  
natrak committed
246 247 248 249 250
function node_links(&$links, &$node, $main = 0) {
  if ($main) {
    if ($node->body) $_links[] = array("node.php?id=$node->nid", t("read more"));
    if ($node->comment) $_links[] = array("node.php?id=$node->nid", format_plural(node_get_comments($node->nid), "comment", "comments"));
  }
natrak's avatar
natrak committed
251 252
  $links = ($links ? array_merge($_links, $links) : $_links);
  return count($links) ? $links : 0;
natrak's avatar
Changes  
natrak committed
253 254
}

Dries's avatar
 
Dries committed
255
?>