summaryrefslogtreecommitdiff
path: root/includes/node.inc
blob: d6c7a9744c578785038e44455842fb46412ff21b (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
<?php

$status = array(dumped => 0, expired => 1, queued => 2, posted => 3, scheduled => 4);

function _node_get($field, $value) {
  $result = db_query("SELECT lid, type FROM nodes WHERE $field = '$value'");
  if ($node = db_fetch_object($result)) {
    return db_query("SELECT n.*, l.*, u.userid FROM nodes n LEFT JOIN $node->type l ON n.lid = l.id AND l.node = n.nid LEFT JOIN users u ON n.author = u.id WHERE n.$field = '$value' ORDER BY n.timestamp DESC");
  }
}

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));
}

function node_del($field, $value) {
  global $status;
  if ($node = node_get_object($field, $value)) {
    if ($node->status == $status[dumped]) {
      db_query("DELETE FROM nodes WHERE nid = '$node->nid'");
      db_query("DELETE FROM $node->type WHERE node = '$node->nid'");
      watchdog("message", "node: deleted '$node->title'");
      return $node;
    }
  }
}

function node_save($node) {
  global $user, $status;

  $rows = array(nid, pid, lid, log, type, title, score, votes, author, status, timestamp);

  if ($node[nid]) {
    $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) ."'");
      }
    }

    $u1 = implode(", ", $u1);
    $u2 = implode(", ", $u2);

    db_query("UPDATE nodes SET $u1 WHERE nid = '$node[nid]'");
    db_query("UPDATE $node[type] SET $u2 WHERE node = '$node[nid]'");

    watchdog("message", "node: modified '$node[title]'");
  }
  else {
    $duplicate = node_get_object("title", $node[title]);

    if ($duplicate && (time() - $duplicate->timestamp < 300)) {
      watchdog("warning", "node: duplicate node '$node[title]'");
    }
    else {
      // 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) ."'");
        }
      }

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

      db_query("INSERT INTO nodes ($f1) VALUES ($v1)");
      if ($nid = db_insert_id()) {
        $lid = db_query("INSERT INTO $node[type] ($f2, node) VALUES ($v2, $nid)");
        if ($lid = db_insert_id()) {
          db_query("UPDATE nodes SET lid = '$lid' WHERE nid = '$nid'");
        }
        else {
          db_query("DELETE FROM nodes WHERE nid = '$nid'");
        }
      }

      watchdog("message", "node: added '$node[title]'");
    }
  }

  if (($node[pid]) && ($node[status] == $status[posted])) {
    db_query("UPDATE nodes SET status = '$status[expired]' WHERE nid = '$node[pid]'");
  }
}

function node_view($node, $page) {
  if ($node->type) {
    $function = $node->type ."_view";
    return $function($node, $page);
  }
}

function node_form($node) {
  if ($node[type]) {
    $function = $node[type] ."_form";
    return $function($node);
  }
}

function node_info($node) {
  global $REQUEST_URI;

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

  $choices = array("/node.php?id=$node->nid" => t("view node"), "/submit.php?mod=$node->type&op=update&id=$node->nid" => t("suggest update"), "/node.php?op=history&id=$node->nid" => t("view history"));

  $output .= "<FORM METHOD=\"get\" ACTION=\"\">\n";
  foreach ($choices as $key => $value) $options .= "<OPTION VALUE=\"$key\"". ($key == $REQUEST_URI ? " SELECTED" : "") .">". check_select($value) ."</OPTION>\n";
  $output .= " <SELECT NAME=\"op\" ONCHANGE=\"visit(this.options[this.selectedIndex].value)\">$options</SELECT>\n";
  $output .= "</FORM>\n";

  return $output;
}

function node_visible($node) {
  global $user, $status;
  return ($node->status == $status[posted]) || ($node->status == $status[queued] && $user->id) || user_access($user, "node");
}

function node_post_threshold($node, $threshold = 5) {
  return 3;
}

function node_dump_threshold($node, $threshold = - 3) {
  return -2;
}

function node_timout_threshold($node, $threshold = 10) {
  return 9;
}

?>