<?php

require_once( '../../../wp-load.php' );

require_once( 'lib/rss_fetch.inc' );
require_once( 'Web2.php' );

define( 'RLI_LINK_PEND', 0 );
define( 'RLI_LINK_DONE', 1 );
define( 'RLI_TABLE_NAME', 'rli_links' );

global $wpdb;

function get_next_link_block( $num_links ) {
	global $wpdb;
	
	$table_name = $wpdb->prefix . RLI_TABLE_NAME;
	$sql = "SELECT * from $table_name where status = " . RLI_LINK_PEND . " order by id asc limit $num_links";
	return $wpdb->get_results( $sql );
}

function get_next_keyword_block( $num_keywords, $kw_index, $keywords ) {
	$return = array();
	
	// make sure keywords don't repeat on sites
	$kw_index = rand( 0, count( $keywords ) - 1 );
	
	$keyword_count = count( $keywords );
	for ( $index = 0; $index < $num_keywords; ++$index ) {
		$return[] = $keywords[ $kw_index ];
		++$kw_index;
		if ( $kw_index == $keyword_count ) {
			$kw_index = 0;
		}
	}
	
	return $return;
}

function create_link_block( $links, $keywords ) {
	$output = '<p>';
	
	$index = 0;
	$num_keywords = count( $keywords );
	foreach ( $links as $link ) {
		$output .= '<a href="' . $link->url . '">' . trim( $keywords[ $index ] ) . '</a> ';
		++$index;
		if ( $index == $num_keywords ) {
			$index = 0;
		}
	}
	
	$output .= '</p>';

	return $output;
}

function create_post( $keywords, $posts, $links, $strip_content = false ) {
	global $options;
	global $link_count;
	global $keyword_index;
	
	$keyword_block = get_next_keyword_block( $link_count, $keyword_index, $keywords );
	$keyword_index += $link_count;
	$post = $posts[ rand( 0, count( $posts ) - 1 ) ];
	
	if ( $strip_content ) {
		$post[ 'content' ] = strip_tags( $post[ 'content' ] );
	}
	
	$post[ 'title' ] = strip_tags( $post[ 'title' ] );
	
	if ( ! isSet( $options[ 'feed_source_link' ] ) || $options[ 'feed_source_link' ] ) {
		$post[ 'content' ] .= '<p>Source: <a href="' . $post[ 'link' ] . '">' . $post[ 'link' ] . '</a></p>';
	}
	$post[ 'content' ] .= create_link_block( $links, $keyword_block );
	
	return $post;
}

function check_option( $options, $option_name ) {
	return isSet( $options[ $option_name ] ) && $options[ $option_name ] != '';
}

function get_content( $item ) {
	return isSet( $item[ 'content:encoded' ] ) ? $item[ 'content:encoded' ] : $item[ 'description' ];
}

function clean_content( $content ) {
	$array = preg_split('//', $content, -1, PREG_SPLIT_NO_EMPTY);

	$intag = false;
	for ( $index = 0; $index < count( $array ); ++$index ) {
		$char = $array[ $index ];
		
		if ( $char == '<' ) {
			$intag = true;
		} else if ( $char == '> ' ) {
			$intag = false;
		} else if ( $char == "\n" && $intag ) {
			$array[ $index ] = ' ';
		}
	}
	
	$content = implode( $array );
	return $content;
}

$options = get_option( 'rli' );

if ( ! $options[ 'enable' ] ) {
	echo 'Cron job isn\'t enabled.  Done.';
	exit();
}

$options[ 'hours_since_post' ] += 1;
$posts_per_day = $options[ 'postsperday' ];
$hours_between_posts = 24 / $posts_per_day;

if ( $options[ 'hours_since_post' ] < $hours_between_posts ) {
	$hours_since = $options[ 'hours_since_post' ];
	echo "Waited $hours_since hours out of $hours_between_posts.  Done.";
	update_option( 'rli', $options );
	exit();
}

$options[ 'hours_since_post' ] = 0;
update_option( 'rli', $options );

$feeds = explode( "\n", trim( $options[ 'feeds' ] ) );
$keywords = explode( "\n", trim( $options[ 'keywords' ] ) );
$links_per_post = $options[ 'linksperpost' ];

$links = get_next_link_block( $links_per_post );
$link_count = count( $links );


if ( $link_count == 0 ) {
	
	echo 'No links to index.  Done.<br />';
	
	if ( isSet( $options[ 'auto_restart' ] ) && $options[ 'auto_restart' ] == true ) {
		echo 'Auto restarting<br />';
		$table_name = $wpdb->prefix . RLI_TABLE_NAME;
		$sql = 'update ' . $table_name . ' set status = ' . RLI_LINK_PEND;
		$wpdb->query( $sql );
	}

	exit();
}

$posts = array();

foreach ( $feeds as $feed ) {
	$feed = trim( $feed );
	$rss = fetch_rss( $feed );
	
	foreach ( $rss->items as $item ) {
		$posts[] = array(
			'title' => $item[ 'title' ],
			'content' => clean_content( get_content( $item ) ),
			'link' => $item[ 'link' ]
		);
	}
}

$num_posts = count( $posts );

if ( ! $num_posts ) {
	echo 'No RSS feed data, can\'t continue.  Done.';
	exit();
}

srand((double) microtime() * 1000000);
$keyword_index = 0;
$web2 = new Web2();

try {
	if ( check_option( $options, 'wordpress_username' ) ) {
		$post = create_post( $keywords, $posts, $links );
		$web2->PostWordpress( $options[ 'wordpress_username' ], $options[ 'wordpress_password' ], $options[ 'wordpress_prefix' ], $post[ 'title' ], $post[ 'content' ] );
	}
} catch ( Exception $ex ) {
	
}

try {
	if ( check_option( $options, 'typepad_username' ) ) {
		$post = create_post( $keywords, $posts, $links );
		$web2->PostTypePad( $options[ 'typepad_username' ], $options[ 'typepad_password' ], $options[ 'typepad_blog_id' ], $post[ 'title' ], $post[ 'content' ] );
	}
} catch ( Exception $ex ) {
	
}
	
try {
	if ( check_option( $options, 'blogger_username' ) ) {
		$post = create_post( $keywords, $posts, $links );
		$web2->PostBlogger( $options[ 'blogger_username' ], $options[ 'blogger_password' ], $post[ 'title' ], $post[ 'content' ] );
	}
} catch ( Exception $ex ) {
	
}

try {
	if ( check_option( $options, 'live_journal_username' ) ) {
		$post = create_post( $keywords, $posts, $links );
		$web2->PostLiveJournal( $options[ 'live_journal_username' ], $options[ 'live_journal_password' ], $post[ 'title' ], $post[ 'content' ] );
	}
} catch ( Exception $ex ) {
	
}

try {
	if ( check_option( $options, 'tumblr_username' ) ) {
		$post = create_post( $keywords, $posts, $links );
		$web2->PostTumblr( $options[ 'tumblr_username' ], $options[ 'tumblr_password' ], $post[ 'title' ], $post[ 'content' ] );
	}
} catch ( Exception $ex ) {
	
}

try {
	if ( check_option( $options, 'multiply_username' ) ) {
		$post = create_post( $keywords, $posts, $links );
		$web2->PostMultiply( $options[ 'multiply_username' ], $options[ 'multiply_password' ], $post[ 'title' ], $post[ 'content' ] );
	}
} catch ( Exception $ex ) {
	
}

try {
	if ( check_option( $options, 'xanga_username' ) ) {
		$post = create_post( $keywords, $posts, $links );
		$web2->PostXanga( $options[ 'xanga_username' ], $options[ 'xanga_password' ], $post[ 'title' ], $post[ 'content' ] );
	}
} catch ( Exception $ex ) {
	
}

try {
	if ( check_option( $options, 'posterous_email' ) ) {
		$post = create_post( $keywords, $posts, $links );
		$web2->PostPosterous( $options[ 'posterous_email' ], $options[ 'posterous_password' ], $post[ 'title' ], $post[ 'content' ] );
	}
} catch ( Exception $ex ) {
	
}

for ( $index = 1; $index <= 10; ++$index ) {
	try {
		if ( check_option( $options, 'own_wordpress_' . $index . '_username' ) ) {
			$post = create_post( $keywords, $posts, $links );
			$web2->PostOwnWordpress( $options[ 'own_wordpress_' . $index . '_domain' ], $options[ 'own_wordpress_' . $index . '_folder' ], $options[ 'own_wordpress_' . $index . '_username' ], $options[ 'own_wordpress_' . $index . '_password' ], $post[ 'title' ], $post[ 'content' ] );
		}
	} catch ( Exception $ex ) {
		
	}	
}

//try {
//	if ( check_option( $options, 'own_wordpress_1_username' ) ) {
//		$post = create_post( $keywords, $posts, $links );
//		$web2->PostOwnWordpress( $options[ 'own_wordpress_1_domain' ], $options[ 'own_wordpress_1_folder' ], $options[ 'own_wordpress_1_username' ], $options[ 'own_wordpress_1_password' ], $post[ 'title' ], $post[ 'content' ] );
//	}
//} catch ( Exception $ex ) {
//	
//}
//
//try {
//	if ( check_option( $options, 'own_wordpress_2_username' ) ) {
//		$post = create_post( $keywords, $posts, $links );
//		$web2->PostOwnWordpress( $options[ 'own_wordpress_2_domain' ], $options[ 'own_wordpress_2_folder' ], $options[ 'own_wordpress_2_username' ], $options[ 'own_wordpress_2_password' ], $post[ 'title' ], $post[ 'content' ] );
//	}
//} catch ( Exception $ex ) {
//	
//}
//
//try {
//	if ( check_option( $options, 'own_wordpress_3_username' ) ) {
//		$post = create_post( $keywords, $posts, $links );
//		$web2->PostOwnWordpress( $options[ 'own_wordpress_3_domain' ], $options[ 'own_wordpress_3_folder' ], $options[ 'own_wordpress_3_username' ], $options[ 'own_wordpress_3_password' ], $post[ 'title' ], $post[ 'content' ] );
//	}
//} catch ( Exception $ex ) {
//	
//}
//
//try {
//	if ( check_option( $options, 'own_wordpress_4_username' ) ) {
//		$post = create_post( $keywords, $posts, $links );
//		$web2->PostOwnWordpress( $options[ 'own_wordpress_4_domain' ], $options[ 'own_wordpress_4_folder' ], $options[ 'own_wordpress_4_username' ], $options[ 'own_wordpress_4_password' ], $post[ 'title' ], $post[ 'content' ] );
//	}
//} catch ( Exception $ex ) {
//	
//}
//
//try {
//	if ( check_option( $options, 'own_wordpress_5_username' ) ) {
//		$post = create_post( $keywords, $posts, $links );
//		$web2->PostOwnWordpress( $options[ 'own_wordpress_5_domain' ], $options[ 'own_wordpress_5_folder' ], $options[ 'own_wordpress_5_username' ], $options[ 'own_wordpress_5_password' ], $post[ 'title' ], $post[ 'content' ] );
//	}
//} catch ( Exception $ex ) {
//	
//}

$ids = '';
$first = true;
foreach ( $links as $link ) {
	if ( ! $first ) {
		$ids .= ', ';
	}
	$ids .= $link->id;
	$first = false;
}

$wpdb = new wpdb( DB_USER, DB_PASSWORD, DB_NAME, DB_HOST );
$wpdb->set_prefix( $table_prefix );

$table_name = $wpdb->prefix . RLI_TABLE_NAME;
$sql = 'update ' . $table_name . ' set status = ' . RLI_LINK_DONE . ' where id in ( ' . $ids . ' )';
$wpdb->query( $sql );

$sql = 'select count(*) from ' . $table_name . ' where status = ' . RLI_LINK_PEND;
$count = $wpdb->get_var( $sql );

if ( $count == 0 && check_option( $options, 'notify_email' ) ) {
	$wpurl = get_bloginfo( 'wpurl' );
	$subject = 'Cluster complete - ' . $wpurl;
	$message = "This is an automated message to inform you that your Energizer cluster @ $wpurl has finished processing all the links you've entered";
	mail( $options[ 'notify_email' ], $subject, $message );
}

echo 'Done.';

?>