<?php

/*
Plugin Name: Backlink Energizer
Plugin URI: http://www.backlinkgoldmine.com/energizer
Description: Get your backlinks indexed faster than ever before, give them a steroid injection and launch yourself up the SERPs
Version: 1.6.0
Author: Steve Aylor
*/

require_once dirname( __FILE__ ) . '/wpal/wpalPage.php';
require_once dirname( __FILE__ ) . '/wpal/wpalBox.php';
require_once dirname( __FILE__ ) . '/wpal/wpalConfigBox.php';
require_once dirname( __FILE__ ) . '/rliPage.php';

require_once dirname( __FILE__ ) . '/Web2.php';

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

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

function rli_get_config() {
	return array(
		'Enable' => array(
			'type' => 'check',
			'name' => 'enable'
		),
		'Notify Email' => array(
			'type' => 'text',
			'name' => 'notify_email'
		),
		'Auto Restart' => array(
			'type' => 'check',
			'name' => 'auto_restart'
		),
		'Feeds' => array(
			'type' => 'textarea',
			'name' => 'feeds'
		),
		'Keywords' => array(
			'type' => 'textarea',
			'name' => 'keywords',
			'validate' => 'rli_validate_keywords'
		),
		'Links per post' => array(
			'type' => 'text',
			'name' => 'linksperpost'
		),
		'Posts per day' => array(
			'type' => 'text',
			'name' => 'postsperday'
		),
		'Feed Source Link' => array(
			'type' => 'check',
			'name' => 'feed_source_link'
		)
//		'Pro Secret Key' => array(
//			'type' => 'text',
//			'name' => 'pro_secret_key'
//		)
	);
}

function rli_get_sites_config() {
	return array(
		'Wordpress' => array(
			'Username' => 'wordpress_username',
			'Password' => 'wordpress_password',
			'Blog Prefix' => array(
				'type' => 'text',
				'name' => 'wordpress_prefix',
				'hint' => '<a href="http://screencast.com/t/ZGYwNjQ1ZmQt" target="_blank">What is this?</a>'
			),
			'Test' => array(
				'type' => 'test',
				'name' => 'wordpress',
				'args' => array(
					'wordpress_username',
					'wordpress_password',
					'wordpress_prefix'
				)
			)
		),
		'TypePad' => array(
			'User Email' => 'typepad_username',
			'Password' => 'typepad_password',
			'Blog ID' => array(
				'type' => 'text',
				'name' => 'typepad_blog_id',
				'hint' => '<a href="http://screencast.com/t/ZWM2ZjkyZWIt" target="_blank">What is this?</a>'
			),
			'Test' => array(
				'type' => 'test',
				'name' => 'typepad',
				'args' => array(
					'typepad_username',
					'typepad_password',
					'typepad_blog_id'
				)
			)
		),
		'Blogger' => array(
			'User Email' => 'blogger_username',
			'Password' => 'blogger_password',
			'Test' => array(
				'type' => 'test',
				'name' => 'blogger',
				'args' => array(
					'blogger_username',
					'blogger_password'
				)
			)
		),
		'Live Journal' => array(
			'Username' => 'live_journal_username',
			'Password' => 'live_journal_password',
			'Test' => array(
				'type' => 'test',
				'name' => 'livejournal',
				'args' => array(
					'live_journal_username',
					'live_journal_password'
				)
			)
		),
		'Tumblr' => array(
			'User Email' => 'tumblr_username',
			'Password' => 'tumblr_password',
			'Test' => array(
				'type' => 'test',
				'name' => 'tumblr',
				'args' => array(
					'tumblr_username',
					'tumblr_password'
				)
			)
		),
		'Multiply' => array(
			'Username' => 'multiply_username',
			'Password' => 'multiply_password',
			'Test' => array(
				'type' => 'test',
				'name' => 'multiply',
				'args' => array(
					'multiply_username',
					'multiply_password'
				)
			)
		),
		'Xanga' => array(
			'Username' => 'xanga_username',
			'Password' => 'xanga_password',
			'Test' => array(
				'type' => 'test',
				'name' => 'xanga',
				'args' => array(
					'xanga_username',
					'xanga_password'
				)
			)
		),
		'Posterous' => array(
			'User Email' => 'posterous_email',
			'Password' => 'posterous_password',
			'Test' => array(
				'type' => 'test',
				'name' => 'posterous',
				'args' => array(
					'posterous_email',
					'posterous_password'
				)
			)
		)
	);
}

function rli_get_shwp_config() {
	$shwp = array();
	
	for ( $index = 1; $index <= 10; ++$index ) {
		$shwp[ "Own Wordpress $index" ] = array(
			'Domain' => 'own_wordpress_' . $index . '_domain',
			'Folder' => 'own_wordpress_' . $index . '_folder',
			'Username' => 'own_wordpress_' . $index . '_username',
			'Password' => 'own_wordpress_' . $index . '_password',
			'Test' => array(
				'type' => 'test',
				'name' => 'ownwordpress' . $index,
				'args' => array(
					'own_wordpress_' . $index . '_domain',
					'own_wordpress_' . $index . '_folder',
					'own_wordpress_' . $index . '_username',
					'own_wordpress_' . $index . '_password'
				)
			)
		);
	}
	
	return $shwp;
}

function rli_default_admin_options() {
	return array(
		'active' => false,
		'feeds' => '',
		'feed_source_link' => true,
		'wordpress_username' => '',
		'wordpress_password' => '',
		'typepad_username' => '',
		'typepad_password' => '',
		'typepad_blog_id' => '',
		'blogger_username' => '',
		'blogger_password' => '',
		'vox_email' => '',
		'posterous_from' => '',
		'live_journal_username' => '',
		'live_journal_password' => '',
		'tumblr_username' => '',
		'tumblr_password' => '',
		'multiply_username' => '',
		'multiply_password' => ''
	);
}

function rli_get_admin_options() {
	$options = rli_default_admin_options();
	
	$stored_options = get_option( 'rli' );
	
	if ( !empty( $stored_options ) ) {
		foreach ( $stored_options as $key => $option ) {
			$options[ $key ] = $option;
		}
	}
	
	return $options;
}

function rli_get_table_name() {
	global $wpdb;
	return $wpdb->prefix . RLI_TABLE_NAME;
}

function rli_activate() {
	global $wpdb;
	
	$table_name = rli_get_table_name();
	
	if ( $wpdb->get_var( "SHOW TABLES LIKE `" . $table_name . "`" ) != $table_name ) {
		$sql = "CREATE TABLE `" . $table_name . "` (
				id INT UNSIGNED AUTO_INCREMENT NOT NULL,
				url VARCHAR(255),
				status TINYINT,
				UNIQUE KEY id (id)
			);";
		require_once(ABSPATH . 'wp-admin/includes/upgrade.php');
		dbDelta($sql);
	}
}

function rli_deactivate() {
//	global $wpdb;
//	
//	$table_name = rli_get_table_name();
//	
//	$sql = "DROP TABLE `" . $table_name . "`";
//	
//	$wpdb->query( $sql );
}

function rli_validate_keywords( $value ) {
	echo 'PITY THE FOOL';
	
	$values = explode( "\n", $value );
	
	$count = count( $values );
	$count = $count < 200 ? $count : 200;
	$out = array();
	for ( $index = 0; $index < $count; ++$index ) {
		$out[] = trim( $values[ $index ] );
	}
	return implode( "\n", $out );
}

function rli_config() {
	$options = rli_get_admin_options();
	$config = rli_get_config();
	
	if ( isSet( $_POST[ 'rli_config' ] ) ) {
		foreach ( $config as $label => $name ) {
			// if it's not an array, then we just have a standard text input to deal with
			if ( ! is_array( $name ) ) {
				if ( isSet( $_POST[ $name ] ) ) {
					$option_name = $name;
					$value = $_POST[ $name ];
				}
			// if there's no type specified or a text type is specified we're still dealing with text but the data is in a different place
			} else if ( ! isSet( $name[ 'type' ] ) || $name[ 'type' ] == 'text' || $name[ 'type' ] == 'textarea' ) {
				if ( isSet( $_POST[ $name[ 'name' ] ] ) ) {
					$option_name = $name[ 'name' ];
					$value = $_POST[ $option_name ];
					if ( isSet( $name[ 'validate' ] ) ) {
						$value = call_user_func_array( $name[ 'validate' ], array( $value ) );
					}
				}
			} else {
				$option_name = $name[ 'name' ];
				$value = isSet( $_POST[ $name[ 'name'] ] );
			}
			$options[ $option_name ] = $value;
		}
		update_option( 'rli', $options );
	}
	
	foreach ( $config as $label => $details ) {
		if ( !is_array( $details ) ) {
			$config[ $label ] = array(
				'name' => $details,
				'type' => 'text'
			);
		}
		$config[ $label ][ 'value' ] = $options[ $config[ $label ][ 'name' ] ];
	}
	
	$page = new rliPage( 'Config' );
	$page->AddForm( '', 'post', 'rli_config' );
	$box = new wpalConfigBox( 'Config', $config, true );
	$page->AddContentBox( $box );
	$page->AddContentBox( new wpalBox( 'Cron', 'rli_cron_callback' ) );
	$page->Render();
}

function rli_cron_callback() {
	$wpurl = get_bloginfo( 'wpurl' );
	$cron = '0 * * * * /usr/bin/GET ' . $wpurl . '/wp-content/plugins/energizer/cron.php';
	echo '<p>' . $cron . '</p>';
	?><p><a href="http://screencast.com/t/MjExY2I0">What do I do with this?</a></p><?php
}

function rli_sites() {
	$options = rli_get_admin_options();
	$config = rli_get_sites_config();

	if ( isSet( $_POST[ 'rli_sites' ] ) ) {
		foreach ( $config as $site => $details ) {
			foreach ( $details as $label => $name ) {
				if ( ! is_array( $name ) ) {
					if ( isSet( $_POST[ $name ] ) ) {
						$option_name = $name;
						$value = $_POST[ $name ];
					}
				} else if ( ! isSet( $name[ 'type' ] ) || $name[ 'type' ] == 'text' ) {
					if ( isSet( $_POST[ $name[ 'name' ] ] ) ) {
						$option_name = $name[ 'name' ];
						$value = $_POST[ $option_name ];
					}
				} else {
					$option_name = $name[ 'name' ];
					$value = isSet( $_POST[ $name[ 'name'] ] ); 
				}
				$options[ $option_name ] = $value;
			}
		}
		update_option( 'rli', $options );
	}
	
	$page = new rliPage( 'Account Configuration' );
	$page->AddForm( '', 'post', 'rli_sites' );
	foreach ( $config as $site => $fields ) {
		foreach ( $fields as $label => $details ) {
			if ( !is_array( $details ) ) {
				$fields[ $label ] = array(
					'name' => $details,
					'type' => 'text'
				);
			}
			$fields[ $label ][ 'value' ] = $options[ $fields[ $label ][ 'name' ] ];
		}
		$config_box = new wpalConfigBox( $site, $fields, true );
		$site_url = strtolower( $site ) . '_url';
		$site_url = str_replace( ' ', '_', $site_url );
		if ( isSet( $options[ $site_url ] ) ) {
			$config_box->SetSubtitle( '[<a href="' . $options[ $site_url ] . '" target="_blank">Show Me</a>' . ']' );
		}		
		$page->AddContentBox( $config_box );
	}
	$page->Render();
}

function rli_shwp() {
	$options = rli_get_admin_options();
	//$config = rli_get_sites_config();
	$shwp = rli_get_shwp_config();
	
	if ( isSet( $_POST[ 'rli_shwp' ] ) ) {
		foreach ( $shwp as $site => $details ) {
			foreach ( $details as $label => $name ) {
				if ( ! is_array( $name ) ) {
					if ( isSet( $_POST[ $name ] ) ) {
						$option_name = $name;
						$value = $_POST[ $name ];
					}
				} else if ( ! isSet( $name[ 'type' ] ) || $name[ 'type' ] == 'text' ) {
					if ( isSet( $_POST[ $name[ 'name' ] ] ) ) {
						$option_name = $name[ 'name' ];
						$value = $_POST[ $option_name ];
					}
				} else {
					$option_name = $name[ 'name' ];
					$value = isSet( $_POST[ $name[ 'name'] ] ); 
				}
				$options[ $option_name ] = $value;
			}
		}
		update_option( 'rli', $options );
	}
	
	$page = new rliPage( 'Self Host Wordpress Configuration' );
	$page->AddForm( '', 'post', 'rli_shwp' );
	foreach ( $shwp as $site => $fields ) {
		foreach ( $fields as $label => $details ) {
			if ( !is_array( $details ) ) {
				$fields[ $label ] = array(
					'name' => $details,
					'type' => 'text'
				);
			}
			$fields[ $label ][ 'value' ] = $options[ $fields[ $label ][ 'name' ] ];
		}
		$page->AddContentBox( new wpalConfigBox( $site, $fields, true ) );
	}
	$page->Render();
}

function rli_test() {

	if ( isSet( $_POST[ 'rli_test' ] ) ) {
		$options = rli_get_admin_options();
		
		$title = $_POST[ 'rli_title' ];
		$content = $_POST[ 'rli_content' ];
		
		$post = array(
			'title' => $title,
			'content' => $content
		);
		
		$web2 = new Web2();
		try {
			if ( rli_check_option( $options, 'wordpress_username' ) ) {
				$web2->PostWordpress( $options[ 'wordpress_username' ], $options[ 'wordpress_password' ], $options[ 'wordpress_prefix' ], $post[ 'title' ], $post[ 'content' ] );
			}
		} catch ( Exception $ex ) {
			
		}
		
		try {
			if ( rli_check_option( $options, 'typepad_username' ) ) {
				$web2->PostTypePad( $options[ 'typepad_username' ], $options[ 'typepad_password' ], $options[ 'typepad_blog_id' ], $post[ 'title' ], $post[ 'content' ] );
			}
		} catch ( Exception $ex ) {
			
		}
			
		try {
			if ( rli_check_option( $options, 'blogger_username' ) ) {
				$web2->PostBlogger( $options[ 'blogger_username' ], $options[ 'blogger_password' ], $post[ 'title' ], $post[ 'content' ] );
			}
		} catch ( Exception $ex ) {
			
		}
		
		try {
			if ( rli_check_option( $options, 'live_journal_username' ) ) {
				$web2->PostLiveJournal( $options[ 'live_journal_username' ], $options[ 'live_journal_password' ], $post[ 'title' ], $post[ 'content' ] );
			}
		} catch ( Exception $ex ) {
			
		}
		
		try {
			if ( rli_check_option( $options, 'tumblr_username' ) ) {
				$web2->PostTumblr( $options[ 'tumblr_username' ], $options[ 'tumblr_password' ], $post[ 'title' ], $post[ 'content' ] );
			}
		} catch ( Exception $ex ) {
			
		}
		
		try {
			if ( rli_check_option( $options, 'multiply_username' ) ) {
				$web2->PostMultiply( $options[ 'multiply_username' ], $options[ 'multiply_password' ], $post[ 'title' ], $post[ 'content' ] );
			}
		} catch ( Exception $ex ) {
			
		}
		
		try {
			if ( rli_check_option( $options, 'xanga_username' ) ) {
				$web2->PostXanga( $options[ 'xanga_username' ], $options[ 'xanga_password' ], $post[ 'title' ], $post[ 'content' ] );
			}
		} catch ( Exception $ex ) {
			
		}
		
		try {
			if ( rli_check_option( $options, 'posterous_email' ) ) {
				$web2->PostPosterous( $options[ 'posterous_email' ], $options[ 'posterous_password' ], $post[ 'title' ], $post[ 'content' ] );
			}
		} catch ( Exception $ex ) {
			
		}
		
		for ( $index = 1; $index <= 10; ++$index ) {
			try {
				if ( rli_check_option( $options, 'own_wordpress_' . $index . '_username' ) ) {
					$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 ) {
				
			}	
		}
	}
	
	$page = new rliPage( 'Test Post' );
	$page->AddContentBox( new wpalBox( 'Test Post', 'rli_test_box' ) );
	$page->Render();
}

function rli_test_box() { ?>
	<p>This test function only tests the ability to post to the sites configured.</p>
	<p>You will also need to setup feeds, keywords and links, which this screen does NOT test.</p>

	<table width="100%">
	<form action="" method="post">
		<input type="hidden" name="rli_test" />
		<?php wpalBox::RenderTextOption( 'Title', 'rli_title', '' ); ?>
		<?php wpalBox::RenderTextAreaOption( 'Content', 'rli_content', '' ); ?>
		<tr>
			<td>&nbsp;</td>
			<td><input type="submit" class="button" value="Test" /></td>
		</tr>
	</form>
	</table>
<?php }

function rli_links() {
	$page = new rliPage( 'Add Links' );
	$page->AddContentBox( new wpalBox( 'Add Links', 'rli_add_links_box' ) );
	$page->Render();
}

function rli_clear_links_box() { ?>
	<form action="" method="post">
		<input type="hidden" name="clear" value="yes" />
		<input type="submit" value="Clear Links" />
	</form>
<?php }
	
function rli_add_links_box() {
	global $wpdb;
	$table_name = rli_get_table_name();	$links = 0;	
	if ( isSet( $_POST[ 'rli_links' ] ) ) {
		$lines = explode( "\n", $_POST[ 'links' ] );
		foreach ( $lines as $line ) {
			$line = trim( $line );
			if ( $line != '' ) {
				$sql = "INSERT INTO $table_name ( url, status ) VALUES( '$line', " . RLI_LINK_PEND . ')';
				$wpdb->query( $sql );
				$links++;
			}
		}
	}
	if ( $links ) { ?>
		<p>Successfully added <?php echo $links; ?> links for indexation.</p>
	<?php } ?>
	<table width="100%">
	<form action="" method="post">
		<input type="hidden" name="rli_links" value="" />
		<textarea style="width:100%" name="links" rows="30"></textarea><br />
		<input type="submit" value="Add" />
	</form>
	</table>
<?php }

function rli_status() {
	global $wpdb;
	
	if ( isSet( $_POST[ 'clear' ] ) ) {
		$table_name = $wpdb->prefix . RLI_TABLE_NAME;
		$sql = 'delete from ' . $table_name;
		$wpdb->query( $sql );
	}
	
	$page = new rliPage( 'Status Report' );
	$page->AddContentBox( new wpalBox( 'Clear Links', 'rli_clear_links_box' ) );
	$page->AddContentBox( new wpalBox( 'Pending Links', 'rli_view_pending_box' ) );
	$page->AddContentBox( new wpalBox( 'Energized Links', 'rli_view_energized_box' ) );
	$page->Render();
}

function rli_status_box() {
	global $wpdb;
	$table_name = rli_get_table_name();
	$pending = $wpdb->get_var( "SELECT count(id) FROM $table_name WHERE status = " . RLI_LINK_PEND );
	$complete = $wpdb->get_var( "SELECT count(id) FROM $table_name WHERE status = " . RLI_LINK_DONE );
?>
	<p>Links Added: <?php echo $pending; ?></p>
	<p>Links Complete: <?php echo $complete; ?></p>
<?php
}

function rli_import() {
	$page = new rliPage( 'Import Cluster' );
	
	$page->AddContentBox( new wpalBox( 'Get a cluster now', 'rli_get_cluster_box' ) );
	
	if ( isSet( $_POST[ 'import' ] ) ) {
		$page->AddContentBox( new wpalBox( 'Import Results', 'rli_import_results_box' ) );
	}

	$page->AddContentBox( new wpalBox( 'Import Cluster', 'rli_import_box' ) );
	$page->Render();
}

function rli_get_cluster_box() { ?>
	<p>Get your clusters here - <a href="http://www.backlink-energizer.com/bliss2/" target="_blank">http://www.backlink-energizer.com/bliss2/</a></p>
<?php }

function rli_import_results_box() {
	$options = rli_get_admin_options();
	
	$cluster_data = file_get_contents( $_FILES[ 'cluster' ][ 'tmp_name' ] );
	$lines = explode( "\n", $cluster_data );
	
	foreach ( $lines as $line ) {
		$config = explode( "==", $line );
		$name = $config[ 0 ];
		$data = $config[ 1 ];
		
		$options[ $name ] = $data;
	}
	
	update_option( 'rli', $options );
	
	echo 'Import Complete';
}

function rli_import_box() { ?>
	<form action="" method="post" enctype="multipart/form-data">
		<input type="hidden" name="import" value="yes" />
		Cluster File: <input type="file" name="cluster" />
		<input type="submit" value="Import" />
	</form>
<?php }

function rli_view_pending_box() {
	global $wpdb;
	$table_name = $wpdb->prefix . 'rli_links';
	
	$sql = 'select url from ' . $table_name . ' where status = ' . RLI_LINK_PEND;
	$links = $wpdb->get_results($sql);

	echo '<p>Links Pending: ' . count( $links ) . '</p>';
  	echo '<div class="wrap">';
	echo '<textarea cols="120" rows="40">';
	foreach ( $links as $link ) {
		echo trim( $link->url ) . "\n";
	}
	echo '</textarea>';
  	echo '</div>';
}

function rli_view_energized_box() {
	global $wpdb;
	$table_name = $wpdb->prefix . 'rli_links';
	
	$sql = 'select url from ' . $table_name . ' where status = ' . RLI_LINK_DONE;
	$links = $wpdb->get_results($sql);

	echo '<p>Links Energized: ' . count( $links ) . '</p>';
  	echo '<div class="wrap">';
	echo '<textarea cols="120" rows="40">';
	foreach ( $links as $link ) {
		echo trim( $link->url ) . "\n";
	}
	echo '</textarea>';
  	echo '</div>';
}

if ( !function_exists( 'rli_admin_action' ) ) {
	function rli_admin_action() {
		if ( function_exists( 'add_menu_page' ) ) {
			add_menu_page( 'Energizer', 'Energizer', 0, basename( __FILE__ ), 'rli_config' );
			add_submenu_page( basename( __FILE__ ), 'Main Config', 'Main Config', 0, basename( __FILE__ ), 'rli_config' );
			add_submenu_page( basename( __FILE__ ), 'Web 2.0 Sites', 'Web 2.0 Sites', 0, basename( __FILE__ ) . '-sites', 'rli_sites' );
			add_submenu_page( basename( __FILE__ ), 'SHWP Sites', 'SHWP Sites', 0, basename( __FILE__ ) . '-shwp', 'rli_shwp' );
			add_submenu_page( basename( __FILE__ ), 'Import Cluster', 'Import Cluster', 0, basename( __FILE__ ) . '-import', 'rli_import' );
			add_submenu_page( basename( __FILE__ ), 'Energize Links', 'Energize Links', 0, basename( __FILE__ ) . '-links', 'rli_links' );
			add_submenu_page( basename( __FILE__ ), 'Status Report', 'Status Report', 0, basename( __FILE__ ) . '-status', 'rli_status' );
			add_submenu_page( basename( __FILE__ ), 'Test Post', 'Test Post', 0, basename( __FILE__ ) . '-test', 'rli_test' );
			//add_options_page( 'RLI', 'RLI', 0, basename( __FILE__ ), 'rli_admin' );
		}
	}	
}

register_activation_hook( __FILE__, 'rli_activate' );
register_deactivation_hook( __FILE__, 'rli_deactivate' );
add_action( 'admin_menu', 'rli_admin_action' );

add_action('admin_head', 'rli_action_javascript');

function rli_action_javascript() {
	$img_src = get_bloginfo( 'wpurl' ) . '/wp-content/plugins/energizer/images/';
	$pend_src = $img_src . 'loading.gif';
	$success_src = $img_src . 'accept.png';
	$failure_src = $img_src . 'exclamation.png';
?>
<script type="text/javascript" >
function rli_test_site() {
	var argArray = Array.prototype.slice.call(arguments);
	var method = argArray.pop();

	var dataArray = new Array();
	for ( arg in argArray ) {
		var elem = document.getElementById( argArray[ arg ] );
		dataArray.push( elem.value );
	}
	dataArray.push( method );
	
	var argString = dataArray.join( "|" );

	var data = {
		action : 'rli_test_site',
		args : argString
	};

	var img_id = method + '_test_image';
	var elem = document.getElementById( img_id );
	elem.src = '<?php echo $pend_src; ?>';

	jQuery.post( ajaxurl, data, function( data ) {
		var dataArray = data.split( '|' );
		var img_id = dataArray[ 0 ] + '_test_image';
		var elem = document.getElementById( img_id );
		if ( dataArray[ 1 ] == 'success' ) {
			elem.src = '<?php echo $success_src; ?>';
		} else {
			elem.src = '<?php echo $failure_src; ?>';
		}
	});
}	
</script>
<?php
}

add_action('wp_ajax_rli_test_site', 'rli_test_site_callback');

function rli_test_site_callback() {
	global $wpdb; // this is how you get access to the database

	$args = explode( '|', $_POST[ 'args' ] );
	$site = array_pop( $args );
	
	$web2 = new Web2();
	if ( call_user_func_array( array( $web2, 'Test' . $site ), $args ) ) {
		echo "$site|success";
	} else {
		echo "$site|failure";
	}
	
	die();
}

?>