<?php
/*
+------------------------------------------------------
| Write2Left
| (c) timdorr
| http://www.write2left.com
| hide@address.com
| See License.txt for license info
|------------------------------------------------------
| Script: NewPost.php
| Description:
| Shows the now post form and handles it's input.
| Created Sep-06-2002
+------------------------------------------------------
*/
/* Class: NewPost
* Description:
* Driver for new posts
*/
class NewPost
{
var $skin = '';
var $menu = true;
var $log_id = 0;
var $post_id = 0;
var $hidden = '';
var $title = '';
var $category_id = 0;
var $body = '';
var $extended = '';
var $state = '';
var $comments = 'checked';
var $pings = 'checked';
var $extra = array();
var $post_plugins = array();
var $sticky = '';
var $result = '';
var $error = '';
function run()
{
global $W2L, $userinfo, $output, $db, $func;
// Skin stuff
require( "./Skin/NewPost.php" );
$this->skin = new Skin_NewPost();
// Get the log options for the defaults
$options = $func->log_settings();
$this->category_id = $options['def_category_id'];
// Open up the directory
$plug_dir = opendir( './Plugins' );
// Load up the plugins
$post_plugins = array();
while( ( $file = readdir( $plug_dir ) ) !== false )
{
if( is_dir( './Plugins/' . $file ) )
continue;
if( substr( $file, 0, 5 ) != 'post_' )
continue;
if( substr( $file, -4 ) != '.php' )
continue;
include( './Plugins/' . $file );
}
// Play nice with the filesystem
closedir( $plug_dir );
/**************
* Get data for a post id if we're editing
**************/
if ( array_key_exists( 'post_id', $W2L->input ) )
{
$this->post_id = $W2L->input['post_id'];
$post_data = $db->query_fetch( "SELECT * FROM w2l_posts WHERE post_id=$this->post_id" );
$this->hidden = "<input type=\"hidden\" name=\"post_id\" value=\"$this->post_id\"/>\n";
$this->title = $post_data['title'];
$this->body = $post_data['body'];
$this->extended = ( array_key_exists( 'extended', $post_data ) ) ? $post_data['extended'] : "";
$this->state = $post_data['state'];
$this->category_id = $post_data['category_id'];
$this->comments = ( $post_data['allow_comments'] == 1 ) ? "checked" : "" ;
$this->pings = ( $post_data['allow_pings'] == 1 ) ? "checked" : "" ;
// Load extra data
$db->query( "SELECT * FROM w2l_post_extra WHERE post_id=$this->post_id" );
while( $row = $db->fetch_array() )
{
$this->extra[$row['name']] = $row['value'];
}
}
/**************
* Do methods
**************/
if ( array_key_exists( 'M', $W2L->input ) )
{
if( $W2L->input['M'] == 'post' )
$this->post();
if( $W2L->input['M'] == 'DeleteComment' )
$this->delete_comment( $W2L->input['post_id'],
$W2L->input['comment_id'] );
if( $W2L->input['M'] == 'EditComment' )
{
$this->edit_comment( $W2L->input['comment_id'] );
return;
}
if( $W2L->input['M'] == 'DoEditComment' )
$this->do_edit_comment( $W2L->input['post_id'],
$W2L->input['comment_id'],
$W2L->input['author_name'],
$W2L->input['author_email'],
$W2L->input['author_url'],
$W2L->input['comment_body'] );
}
$output->page_title = "Post";
$output->loc_add( "Post" );
$output->add( $this->skin->body_top() );
/**************
* Handle results
**************/
// Show whatever error we get, if any
if( $this->error != "" )
$output->add( "<div class=\"error\">\n" . $this->error . "\n</div><br />\n" );
// Show whatever result we get, if any
if( $this->result != "" )
$output->add( "<div class=\"message\">\n" . $this->result . "\n</div><br />\n" );
/**************
* Restore preview data
**************/
if ( array_key_exists( 'post_title', $W2L->input ) )
$this->title = stripslashes( $W2L->input['post_title'] );
if ( array_key_exists( 'post_body', $W2L->input ) )
$this->body = stripslashes( $W2L->input['post_body'] );
if ( array_key_exists( 'post_extended', $W2L->input ) )
$this->extended = stripslashes( $W2L->input['post_extended'] );
if ( array_key_exists( 'post_cat', $W2L->input ) )
$this->category_id = $W2L->input['post_cat'];
if ( array_key_exists( 'post_state', $W2L->input ) )
$this->state = $W2L->input['post_state'];
if ( array_key_exists( 'post_allow_comments', $W2L->input ) )
$this->comments = "checked";
if ( !array_key_exists( 'post_allow_comments', $W2L->input ) &&
array_key_exists( 'M', $W2L->input ) )
$this->comments = "";
if ( array_key_exists( 'post_allow_pings', $W2L->input ) )
$this->pings = "checked";
if ( !array_key_exists( 'post_allow_pings', $W2L->input ) &&
array_key_exists( 'M', $W2L->input ) )
$this->pings = "";
/**************
* Get category stuff for the form
**************/
$category_ids[] = 0;
$category_names[] = 'No Category';
$db->query( "SELECT category_id, name FROM w2l_categories
WHERE log_id=$userinfo->log_id" );
while( $row = $db->fetch_array() )
{
$category_ids[] = $row['category_id'];
$category_names[] = $row['name'];
}
/**************
* Get plugins and create the form inputs and options
**************/
$extra_inputs = '';
$extra_options = '';
// Load up the plugins
foreach( $this->post_plugins as $post_plugin )
{
if ( array_key_exists( 'post_id', $W2L->input ) )
$post_plugin->initialize( $W2L->input['post_id'], $this->extra );
$post_plugin->filter_post( $this->title, $this->body, $this->extended );
$extra_inputs .= $post_plugin->get_inputs();
$extra_options .= $post_plugin->get_options();
}
/**************
* Show the form
**************/
$output->add( $this->skin->body_form( $this->hidden,
$this->title,
$func->create_option_list( $category_ids,
$category_names,
$this->category_id ),
$func->create_option_list( array( 'publish', 'sticky', 'draft' ),
array( 'Publish', 'Sticky', 'Draft' ),
$this->state ),
$this->body,
$this->extended,
$extra_inputs,
$extra_options,
( $options['notify_default'] == 1 ) ? 'checked' : '',
$this->comments,
$this->pings ) );
// If this is an existing post, show the comments for it
if( array_key_exists( 'post_id', $W2L->input ) )
{
$db->query( "SELECT * FROM w2l_comments
WHERE post_id = $this->post_id
ORDER BY time DESC" );
$output->add( $this->skin->comments_head() );
while( $com = $db->fetch_array() )
$output->add( $this->skin->comments_row( $com['author_name'],
date( 'm-d-Y', $com['time'] ),
$this->post_id,
$com['comment_id'] ) );
$output->add( $this->skin->comments_foot() );
}
$output->add( $this->skin->body_bottom() );
}
//================
// Does the posting fun stuff
//================
function post()
{
global $W2L, $db, $userinfo;
// Looks nicer
$input = $W2L->input;
$error = 0;
// Gotta have a title
if( $input['post_title'] == "" )
{
$this->result .= "The title was not defined.<br />";
$error = 1;
}
// Gotta have a body
if( $input['post_body'] == "" )
{
$this->result .= "The body was not defined.<br />";
$error = 1;
}
// Are we allowing comments on this post?
if( !array_key_exists( 'post_allow_comments', $input ) )
$input['post_allow_comments'] = 0;
// Are we allowing trackbacks on this post?
if( !array_key_exists( 'post_allow_pings', $input ) )
$input['post_allow_pings'] = 0;
// If we found any errors, return to show them
if( $error == 1 )
return;
// Load up the plugins
foreach( $this->post_plugins as $post_plugin )
{
$post_plugin->filter_input( $input );
}
/// Handle if the preview button was pressed
if( $input['post'] == 'Preview' )
{
$this->result .= "<b>Title:</b> $input[post_title]<br /><br />
<b>Body:</b><br />$input[post_body]<br /><br />
<b>Extended:</b><br />$input[post_extended]";
$this->result = stripslashes( $this->result );
return;
}
// Extended can be NULL
if( $input['post_extended'] == "" )
$input['post_extended'] = "NULL";
else
$input['post_extended'] = "'" . $input['post_extended'] . "'";
// Get the Build Manager out and ready
require( "./Build/BuildManager.php" );
// If we're editing, update the post
if( array_key_exists( 'post_id', $input ) )
{
// Update the post
$db->query( "UPDATE w2l_posts SET
title='$input[post_title]',
category_id=$input[post_cat],
body='$input[post_body]',
extended=$input[post_extended],
state='$input[post_state]',
allow_comments=$input[post_allow_comments],
allow_pings=$input[post_allow_pings]
WHERE post_id=$input[post_id]" );
// Save plugin data
foreach( $this->post_plugins as $post_plugin )
{
$post_plugin->update_saved( $input, $this->extra );
}
// Re-build
$return = $BM->cache_data( "Item", $userinfo->log_id, $input['post_id'] );
$return .= $BM->cache_data( "Archive", $userinfo->log_id, $input['post_id'] );
if( $return != '' )
{
$this->result .= $return;
return;
}
// Return a message
$this->result .= "The post has sucessfully been edited.<br />";
// Be sure to send updates!
if( array_key_exists( 'send_notify', $input ) )
$this->send_notify( $input['post_id'], $input );
}
// Or, this post is new
else
{
// Save the post
$db->query( "INSERT INTO w2l_posts
VALUES (
NULL,
$userinfo->log_id,
$userinfo->id,
'$input[post_title]',
'$input[post_body]',
$input[post_extended],
$input[post_cat],
".time().",
'$input[post_state]',
$input[post_allow_comments],
$input[post_allow_pings],
0,
'nobody',
".time().")" );
$new_id = $db->insert_id();
// Save plugin data
foreach( $this->post_plugins as $post_plugin )
{
$post_plugin->store_saved( $input, $this->extra, $new_id );
}
// Re-build
$return = $BM->cache_data( "Item", $userinfo->log_id, $new_id );
$return .= $BM->cache_data( "Archive", $userinfo->log_id, $new_id );
if( $return != '' )
{
$this->result .= $return;
return;
}
//Tell us what happened..
$this->result .= "Your post has sucessfully been added.<br />";
// Be sure to send updates!
if( array_key_exists( 'send_notify', $input ) )
$this->send_notify( $new_id, $input );
}
// Build the main page posts
$BM->cache_data( "Index", $userinfo->log_id );
if( $return != '' )
{
$this->result = $return;
return;
}
}
//================
// Sends word to the masses (well, if you're lucky, it's masses)
//================
function send_notify( $post_id, $input )
{
global $W2L, $db, $userinfo, $func;
$options = $func->log_settings();
// Form the message body and clean it up a little
$message = $options['notify_template'];
$message = str_replace( '<W2Body>', $input['post_body'], $message );
$message = str_replace( '<W2Title>', $input['post_title'], $message );
$message = str_replace( '<W2Author>', $userinfo->name, $message );
$message = stripslashes( $message );
$message = preg_replace( "#<img src=[\"'](\S+?)['\"][^>]*?>#i", " [IMAGE - $options[url]\\1] ", $message );
$message = strip_tags( $message );
// Form the subject line
$subject = stripslashes("Notification for $options[name] - $input[post_title]" );
// Get the subscribers and send to each
$db->query( "SELECT * FROM w2l_subs WHERE log_id=$userinfo->log_id" );
$count = 0;
while( $sub = $db->fetch_array() )
{
$count++;
// Do the mailing or bail out with an error...
if( mail( $sub['address'],
$subject,
$message,
"From: $options[notify_from_mail]\r\n" .
"Reply-To: $options[notify_reply_mail]\r\n" .
"X-Mailer: Write2Left $W2L->version" ) == false)
{
$this->error = 'Unable to send notification email. Please check your mail settings with PHP';
return;
}
}
if( $count > 0 )
$this->result .= "Notifications sent to $count subscribers<br />";
}
//================
// Delete's a comment. Silence the infidels!
//================
function delete_comment( $post_id, $comment_id )
{
global $db, $userinfo;
$db->query( "DELETE FROM w2l_comments WHERE comment_id = $comment_id" );
// Get the Build Manager out and ready
require( "./Build/BuildManager.php" );
// Re-build
$return = $BM->cache_data( "Item", $userinfo->log_id, $post_id );
$return .= $BM->cache_data( "Archive", $userinfo->log_id, $post_id );
if( $return != '' )
{
$this->result .= $return;
return;
}
$this->result = 'Comment deleted';
}
//================
// Shows the comment edit form. Prepare the neuralizer...
//================
function edit_comment( $comment_id )
{
global $db, $output;
$com = $db->query_fetch( "SELECT * FROM w2l_comments
WHERE comment_id = $comment_id" );
$output->page_title = "Edit Comment";
$output->loc_add( "Edit Comment" );
$output->add( $this->skin->body_top_comment() );
$output->add( $this->skin->comment_edit_form( $com['post_id'],
$comment_id,
$com['author_name'],
$com['author_email'],
$com['author_url'],
$com['ip_address'],
$com['body'] ) );
}
//================
// Edit the comment. Look at the time! Is it 1984 already?
//================
function do_edit_comment( $post_id, $comment_id, $name, $email, $url, $body )
{
global $db, $userinfo;
$db->query( "UPDATE w2l_comments SET author_name = '$name',
author_email = '$email',
author_url = '$url',
body = '$body'
WHERE comment_id = $comment_id" );
// Get the Build Manager out and ready
require( "./Build/BuildManager.php" );
// Re-build
$return = $BM->cache_data( "Item", $userinfo->log_id, $post_id );
$return .= $BM->cache_data( "Archive", $userinfo->log_id, $post_id );
if( $return != '' )
{
$this->result .= $return;
return;
}
$this->result = 'Comment Edited';
}
}
$driver = new NewPost();
?>