2001-03-24 16:37:44 +00:00
< ? php
2001-04-01 10:52:01 +00:00
$status = array ( dumped => 0 , expired => 1 , queued => 2 , posted => 3 );
2001-04-07 15:02:28 +00:00
$rstatus = array ( 0 => dumped , 1 => expired , 2 => queued , 3 => posted );
2001-03-25 10:57:01 +00:00
2001-03-24 16:37:44 +00:00
function _node_get ( $field , $value ) {
2001-03-29 19:50:31 +00:00
$result = db_query ( " SELECT lid, type FROM node WHERE $field = ' $value ' " );
2001-03-24 16:37:44 +00:00
if ( $node = db_fetch_object ( $result )) {
2001-04-19 19:59:48 +00:00
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 " );
2001-03-24 16:37:44 +00:00
}
}
2001-04-21 14:19:20 +00:00
function node_comment_status ( $index = - 1 ) {
$status = array ( " disabled " , " enabled " );
return $index < 0 ? $status : $status [ $index ];
}
function node_promote_status ( $index = - 1 ) {
$status = array ( " disabled " , " enabled " );
return $index < 0 ? $status : $status [ $index ];
}
function node_submission_status ( $index = - 1 ) {
$status = array ( " auto-post new submissions " , " moderate new submissions " );
return $index < 0 ? $status : $status [ $index ];
}
2001-03-24 16:37:44 +00:00
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 ));
}
2001-03-26 20:22:09 +00:00
function node_del ( $field , $value ) {
global $status ;
2001-03-24 16:37:44 +00:00
if ( $node = node_get_object ( $field , $value )) {
2001-03-26 20:22:09 +00:00
if ( $node -> status == $status [ dumped ]) {
2001-03-29 19:50:31 +00:00
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 ' " );
2001-03-28 21:32:48 +00:00
watchdog ( " message " , " node: deleted ' $node->title ' " );
2001-03-26 20:22:09 +00:00
return $node ;
}
2001-03-24 16:37:44 +00:00
}
}
2001-04-16 11:38:12 +00:00
function node_get_comments ( $nid ) {
2001-04-16 12:31:11 +00:00
$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 " ));
2001-04-16 11:38:12 +00:00
return $comment -> number ? $comment -> number : 0 ;
}
2001-03-24 16:37:44 +00:00
function node_save ( $node ) {
2001-03-25 10:57:01 +00:00
global $user , $status ;
2001-03-24 16:37:44 +00:00
2001-04-21 14:19:20 +00:00
$rows = array ( nid , pid , lid , cid , tid , log , type , title , score , votes , author , status , comment , promote , timestamp );
2001-03-24 16:37:44 +00:00
2001-03-31 13:18:57 +00:00
if ( $node [ nid ] > 0 ) {
2001-04-02 15:54:37 +00:00
$n = node_get_object ( " nid " , $node [ nid ]);
2001-03-24 16:37:44 +00:00
$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 ) . " ' " );
}
}
2001-03-31 19:24:54 +00:00
if ( $u1 = implode ( " , " , $u1 )) db_query ( " UPDATE node SET $u1 WHERE nid = ' $node[nid] ' " );
2001-04-02 15:54:37 +00:00
if ( $u2 = implode ( " , " , $u2 )) db_query ( " UPDATE $n->type SET $u2 WHERE nid = ' $node[nid] ' " );
2001-04-06 14:14:16 +00:00
if ( $n -> pid && ( $node [ status ] == $status [ posted ])) db_query ( " UPDATE node SET status = ' $status[expired] ' WHERE nid = ' $n->pid ' " );
2001-03-31 18:38:01 +00:00
2001-04-08 16:29:58 +00:00
watchdog ( " special " , " node: modified ' $n->title ' " );
2001-04-14 19:32:15 +00:00
return $node [ nid ];
2001-03-24 16:37:44 +00:00
}
else {
2001-03-28 21:32:48 +00:00
$duplicate = node_get_object ( " title " , $node [ title ]);
2001-03-24 16:37:44 +00:00
2001-03-31 17:56:12 +00:00
if ( $duplicate && ( time () - $duplicate -> timestamp < 60 )) {
2001-03-29 19:50:31 +00:00
watchdog ( " warning " , " node: duplicate ' $node[title] ' " );
2001-03-24 16:37:44 +00:00
}
2001-03-28 21:32:48 +00:00
else {
2001-04-21 14:19:20 +00:00
// verify submission rate:
2001-04-06 14:14:16 +00:00
throttle ( " post node " , variable_get ( max_node_rate , 900 ));
2001-03-28 21:32:48 +00:00
// setup default values:
2001-04-21 14:19:20 +00:00
$node = array_merge ( array ( title => " ? " , author => $user -> id , type => " ? " , pid => 0 , cid => 0 , tid => 0 , log => " node created " , status => ( category_submission ( $node [ cid ]) ? $status [ queued ] : $status [ posted ]) , score => 0 , votes => 0 , comment => category_comment ( $node [ cid ]), promote => category_promote ( $node [ cid ]), timestamp => time ()), $node );
2001-03-28 21:32:48 +00:00
// 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 ) . " ' " );
}
2001-03-25 10:57:01 +00:00
}
2001-03-28 21:32:48 +00:00
$f1 = implode ( " , " , $f1 );
$v1 = implode ( " , " , $v1 );
$f2 = implode ( " , " , $f2 );
$v2 = implode ( " , " , $v2 );
2001-03-31 11:00:04 +00:00
// 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 ) {
2001-03-31 18:38:01 +00:00
if (( $node [ pid ]) && ( $node [ status ] == $status [ posted ])) {
db_query ( " UPDATE node SET status = ' $status[expired] ' WHERE nid = ' $node[pid] ' " );
}
2001-04-08 16:29:58 +00:00
watchdog ( " special " , " node: added ' $node[title] ' " );
2001-03-31 11:00:04 +00:00
}
else {
watchdog ( " warning " , " node: added ' $node[title] ' - failed " );
}
2001-03-28 21:32:48 +00:00
}
else {
2001-03-29 19:50:31 +00:00
db_query ( " DELETE FROM node WHERE nid = ' $nid ' " );
2001-03-31 11:00:04 +00:00
watchdog ( " warning " , " node: added ' $node[title] ' - failed " );
2001-03-28 21:32:48 +00:00
}
2001-03-24 16:37:44 +00:00
}
2001-03-31 11:00:04 +00:00
else {
watchdog ( " warning " , " node: added ' $node[title] ' - failed " );
}
2001-03-28 21:32:48 +00:00
}
2001-03-25 10:57:01 +00:00
2001-04-14 19:32:15 +00:00
return $nid ;
}
2001-03-24 16:37:44 +00:00
}
2001-04-07 15:02:28 +00:00
function node_invoke ( $node , $name , $arg = 0 ) {
if ( $node [ type ]) $function = $node [ type ] . " _ $name " ;
if ( $node -> type ) $function = $node -> type . " _ $name " ;
2001-04-16 18:21:22 +00:00
if ( $function ) return ( $arg ? $function ( $node , $arg ) : $function ( $node ));
2001-04-07 15:02:28 +00:00
}
2001-04-16 18:21:22 +00:00
function node_view ( $node , $main = 0 ) {
return node_invoke ( $node , " view " , $main );
2001-03-25 10:57:01 +00:00
}
function node_form ( $node ) {
2001-04-07 15:02:28 +00:00
return node_invoke ( $node , " form " );
}
function node_status ( $node ) {
return node_invoke ( $node , " status " );
2001-03-24 16:37:44 +00:00
}
2001-04-04 12:54:10 +00:00
function node_control ( $node ) {
2001-03-24 16:37:44 +00:00
global $REQUEST_URI ;
?>
< SCRIPT >
<!--//
function visit ( site ) {
if ( site != " " ) {
parent . location = site
}
}
//-->
</ SCRIPT >
< ? php
2001-04-21 15:51:23 +00:00
if ( $user -> id )
$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 " ));
else
$choices = array ( " node.php?id= $node->nid " => t ( " view node " ), " node.php?op=history&id= $node->nid " => t ( " view history " ));
2001-03-24 16:37:44 +00:00
$output .= " <FORM METHOD= \" get \" ACTION= \" \" > \n " ;
2001-03-31 14:58:37 +00:00
foreach ( $choices as $key => $value ) $options .= " <OPTION VALUE= \" $key\ " " . ( " / $key " == $REQUEST_URI ? " SELECTED " : " " ) . " > " . check_select( $value ) . " </ OPTION > \n " ;
2001-03-24 16:37:44 +00:00
$output .= " <SELECT NAME= \" op \" ONCHANGE= \" visit(this.options[this.selectedIndex].value) \" > $options </SELECT> \n " ;
$output .= " </FORM> \n " ;
return $output ;
}
function node_visible ( $node ) {
2001-03-25 10:57:01 +00:00
global $user , $status ;
return ( $node -> status == $status [ posted ]) || ( $node -> status == $status [ queued ] && $user -> id ) || user_access ( $user , " node " );
2001-03-24 16:37:44 +00:00
}
?>