function wangguard_wizard()
{
    global $wpdb, $wangguard_nonce, $wangguard_api_key, $wangguard_is_network_admin;
    $urlFunc = "admin_url";
    if ($wangguard_is_network_admin && function_exists("network_admin_url")) {
        $urlFunc = "network_admin_url";
    }
    if (wangguard_is_multisite()) {
        $spamFieldName = "spam";
        $sqlSpamWhere = "spam = 1";
        $sqlNoSpamWhere = "spam = 0";
    } else {
        $spamFieldName = "user_status";
        $sqlSpamWhere = "user_status = 1";
        $sqlNoSpamWhere = "user_status <> 1";
    }
    if (!current_user_can('level_10')) {
        die(__('Cheatin&#8217; uh?', 'wangguard'));
    }
    $step = 0;
    if (isset($_REQUEST['wangguard_step'])) {
        $step = (int) $_REQUEST['wangguard_step'];
    }
    if (isset($_POST['submit']) || !empty($step)) {
        check_admin_referer($wangguard_nonce);
    }
    ?>

<div class="wrap" id="wangguard-wizard-cont">
	<div class="wangguard-confico"><img src="<?php 
    echo WP_PLUGIN_URL;
    ?>
/wangguard/img/wizard.png" alt="<?php 
    echo htmlentities(__('WangGuard Wizard', 'wangguard'));
    ?>
" /></div>
	<div class="icon32" id="icon-wangguard"><br></div>
	<h2><?php 
    _e('WangGuard Wizard', 'wangguard');
    ?>
</h2>
	
	<script type="text/javascript">
	function wangguard_progress() {
		jQuery("#wangguard-visible-step-status").hide();
		jQuery("#wangguard-hidden-step-status").show();
		return true;
	}

	jQuery(document).ready(function() {
		jQuery(".wangguard-hidewhendone").hide();
	});
	</script>



	<form action="admin.php" method="get" id="wangguardWizardForm" name="wangguardWizardForm" onsubmit="return wangguard_progress()">
		<input type="hidden" name="page" value="wangguard_wizard" />
		<?php 
    echo wp_nonce_field($wangguard_nonce);
    ?>

		<?php 
    switch ($step) {
        case "1":
            ?>
				<div id="wangguard-visible-step-status">
					<h3><?php 
            echo __("Reporting spam users to WangGuard...", "wangguard");
            ?>
</h3>
					<?php 
            $usersPerStint = 50;
            //how many users to check on each iteration
            $fromUser = isset($_REQUEST['wangguard_wiz_from']) ? (int) $_REQUEST['wangguard_wiz_from'] : 0;
            if ($fromUser < 0) {
                $fromUser = 0;
            }
            $spamUsersTotal = $wpdb->get_col("select count(*) from {$wpdb->users} where {$sqlSpamWhere}");
            $spamUsersTotal = $spamUsersTotal[0];
            $step1Finished = $fromUser > 0 && $fromUser >= $spamUsersTotal;
            if (!$step1Finished) {
                $spamUsers = $wpdb->get_col("select ID from {$wpdb->users} where {$sqlSpamWhere} order by ID LIMIT {$fromUser} , {$usersPerStint}");
                $userCount = count($spamUsers);
                $reportingUserFrom = $fromUser + $usersPerStint;
                $reportingUserFrom = $reportingUserFrom > $spamUsersTotal ? $spamUsersTotal : $reportingUserFrom;
                if ($userCount == 0) {
                    ?>
							<p><?php 
                    echo __("No spam users were found on your site. Click the button below to check your users.", "wangguard");
                    ?>
</p>
							<input type="hidden" name="wangguard_step" value="2" />
							<p class="submit"><input type="submit" name="submit" class="button-primary" value="<?php 
                    _e('Continue', 'wangguard');
                    ?>
" /></p>
							<?php 
                } else {
                    ?>
							<p><img id="wangguard-progress-wait" style="vertical-align: middle; margin-right: 8px;" src="<?php 
                    echo esc_url(admin_url('images/wpspin_light.gif'));
                    ?>
" alt="..." /><?php 
                    echo sprintf(__("The WangGuard wizard is reporting %d of %d spam users as Sploggers.", "wangguard"), $reportingUserFrom, $spamUsersTotal);
                    ?>
</p>
							<?php 
                    @flush();
                    ?>
							<?php 
                    @ob_flush();
                    ?>

							<?php 
                    $progress = 0;
                    $reported = 0;
                    $lastProgressSent = 0;
                    foreach ($spamUsers as $userid) {
                        //get the WangGuard user status, if status is force-checked then ignore the user
                        $table_name = $wpdb->base_prefix . "wangguarduserstatus";
                        $user_status = $wpdb->get_var($wpdb->prepare("select user_status from {$table_name} where ID = %d", $userid));
                        if ($user_status == 'force-checked') {
                            continue;
                        }
                        $dummyArr = array();
                        $dummyArr[] = $userid;
                        set_time_limit(15);
                        wangguard_report_users($dummyArr, "email", false);
                        $reported++;
                    }
                    ?>

							<input type="hidden" name="wangguard_wiz_from" value="<?php 
                    echo $fromUser + $usersPerStint;
                    ?>
" />
							<script type="text/javascript">
								document.getElementById('wangguardWizardForm').onsubmit='';
								jQuery(document).ready(function() {
									location.href='admin.php?page=wangguard_wizard&wangguard_step=1&wangguard_wiz_from=<?php 
                    echo $fromUser + $usersPerStint;
                    ?>
&_wpnonce=<?php 
                    echo wp_create_nonce($wangguard_nonce);
                    ?>
';
								});
							</script>
							<input type="hidden" name="wangguard_step" value="1" />

						<?php 
                }
                ?>

					<?php 
            } else {
                ?>
						<p><?php 
                echo __("The WangGuard wizard has finished reporting spam users. Click the button below to check the rest of your users.", "wangguard");
                ?>
</p>
						<input type="hidden" name="wangguard_step" value="2" />
						<p class="submit"><input type="submit" name="submit" class="button-primary" value="<?php 
                _e('Continue', 'wangguard');
                ?>
" /></p>
					<?php 
            }
            ?>

				</div>

				<?php 
            if ($step1Finished) {
                ?>
				<div id="wangguard-hidden-step-status" style="display: none">
					<h3><?php 
                echo __("Verifying users against the WangGuard service...", "wangguard");
                ?>
</h3>
					<?php 
                $goodUsers = $wpdb->get_col("select ID from {$wpdb->users} where {$sqlNoSpamWhere}");
                $userCount = count($goodUsers);
                if ($userCount == 0) {
                    ?>
						<p><img id="wangguard-progress-wait" style="vertical-align: middle; margin-right: 8px;" src="<?php 
                    echo esc_url(admin_url('images/wpspin_light.gif'));
                    ?>
" alt="..." /></p>
					<?php 
                } else {
                    ?>
						<p><img id="wangguard-progress-wait" style="vertical-align: middle; margin-right: 8px;" src="<?php 
                    echo esc_url(admin_url('images/wpspin_light.gif'));
                    ?>
" alt="..." /><?php 
                    echo sprintf(__("The WangGuard wizard is verifying %d users against the WangGuard service.", "wangguard"), $userCount);
                    ?>
</p>
					<?php 
                }
                ?>
				</div>
				<?php 
            }
            ?>

				<?php 
            break;
        case "2":
            ?>
				<div id="wangguard-visible-step-status">
					<h3><?php 
            echo __("Verifying users against the WangGuard service...", "wangguard");
            ?>
</h3>
					<?php 
            $usersPerStint = 50;
            //how many users to check on each iteration
            $fromUser = isset($_REQUEST['wangguard_wiz_from']) ? (int) $_REQUEST['wangguard_wiz_from'] : 0;
            if ($fromUser < 0) {
                $fromUser = 0;
            }
            $goodUsersTotal = $wpdb->get_col("select count(*) from {$wpdb->users} where {$sqlNoSpamWhere}");
            $goodUsersTotal = $goodUsersTotal[0];
            $step2Finished = $fromUser > 0 && $fromUser >= $goodUsersTotal;
            $reported = isset($_REQUEST['reported']) ? (int) $_REQUEST['reported'] : 0;
            $noUsersToCheck = false;
            if (!$step2Finished) {
                $goodUsers = $wpdb->get_col("select ID from {$wpdb->users} where {$sqlNoSpamWhere} ORDER BY ID LIMIT {$fromUser} , {$usersPerStint}");
                $userCount = count($goodUsers);
                $reportingUserFrom = $fromUser + $usersPerStint;
                $reportingUserFrom = $reportingUserFrom > $goodUsersTotal ? $goodUsersTotal : $reportingUserFrom;
                if ($userCount == 0) {
                    $step2Finished = true;
                    $noUsersToCheck = true;
                    ?>
							<p><?php 
                    echo __("No users were found on your site.", "wangguard");
                    ?>
</p>
							<?php 
                } else {
                    ?>
							<p><img id="wangguard-progress-wait" style="vertical-align: middle; margin-right: 8px;" src="<?php 
                    echo esc_url(admin_url('images/wpspin_light.gif'));
                    ?>
" alt="..." /><?php 
                    echo sprintf(__("The WangGuard wizard is verifying %d of %d users against the WangGuard service.", "wangguard"), $reportingUserFrom, $goodUsersTotal);
                    ?>
</p>
							<?php 
                    @flush();
                    ?>
							<?php 
                    @ob_flush();
                    ?>

							<?php 
                    $progress = 0;
                    $verified = 0;
                    $lastProgressSent = 0;
                    foreach ($goodUsers as $userid) {
                        //get the WangGuard user status, if status is force-checked then ignore the user
                        $table_name = $wpdb->base_prefix . "wangguarduserstatus";
                        $user_status = $wpdb->get_var($wpdb->prepare("select user_status from {$table_name} where ID = %d", $userid));
                        if ($user_status == 'force-checked') {
                            continue;
                        }
                        $dummyArr = array();
                        $dummyArr[] = $userid;
                        $user_object = new WP_User($userid);
                        set_time_limit(15);
                        $user_check_status = wangguard_verify_user($user_object);
                        if ($user_check_status == "reported") {
                            $reported++;
                            if (function_exists("update_user_status")) {
                                update_user_status($userid, $spamFieldName, 1);
                            } else {
                                $wpdb->query($wpdb->prepare("update {$wpdb->users} set {$spamFieldName} = 1 where ID = %d", $userid));
                            }
                        }
                        $verified++;
                    }
                    ?>
							<input type="hidden" name="wangguard_wiz_from" value="<?php 
                    echo $fromUser + $usersPerStint;
                    ?>
" />
							<script type="text/javascript">
								document.getElementById('wangguardWizardForm').onsubmit='';
								jQuery(document).ready(function() {
									location.href='admin.php?page=wangguard_wizard&wangguard_step=2&reported=<?php 
                    echo $reported;
                    ?>
&wangguard_wiz_from=<?php 
                    echo $fromUser + $usersPerStint;
                    ?>
&_wpnonce=<?php 
                    echo wp_create_nonce($wangguard_nonce);
                    ?>
';
								});
							</script>
							<input type="hidden" name="wangguard_step" value="2" />


						<?php 
                }
            }
            if ($step2Finished) {
                $table_name = $wpdb->base_prefix . "wangguarduserstatus";
                $reportedUsers = $wpdb->get_col("select count(*) from {$table_name} where user_status IN ( 'reported', 'autorep' )");
                $reportedUsersCount = $reportedUsers[0];
                if (!$noUsersToCheck) {
                    ?>
							<p><?php 
                    echo sprintf(__("The WangGuard wizard has finished verifying your users and found <strong>%d</strong> Sploggers.", "wangguard"), $reported);
                    ?>
</p>
						<?php 
                }
                ?>

						<input type="hidden" name="wangguard_step" value="3" />
						<input type="hidden" name="wangguard_splogcnt" value="<?php 
                echo $reportedUsersCount;
                ?>
" />
						
						<?php 
                if ($reportedUsersCount) {
                    ?>
							<p><?php 
                    echo sprintf(__("There are <strong>%d</strong> users identified as Sploggers, you can delete them or manage them by clicking the buttons below.", "wangguard"), $reportedUsersCount);
                    ?>
</p>
						<?php 
                }
                ?>

						<div id="wangguard-visible-step-status">
							<input type="hidden" value="" name="wangguard_delete_splogguers" id="wangguard_delete_splogguers" />
							<p class="submit">
								<?php 
                if ($reportedUsersCount) {
                    ?>
									<input type="submit" name="do_wangguard_delete_splogguers" class="button-primary" id="do_wangguard_delete_splogguers" value="<?php 
                    _e('Delete all Sploggers', 'wangguard');
                    ?>
" />
									<input type="button" name="button" class="button-primary" onclick="document.location='admin.php?page=wangguard_users&type=spl'" value="<?php 
                    _e('Manage Sploggers', 'wangguard');
                    ?>
" />
								<?php 
                }
                ?>
								<input type="submit" name="submit" class="button-primary" value="<?php 
                _e('Finish', 'wangguard');
                ?>
" />
							</p>
						</div>

						<script type="text/javascript">
							jQuery(document).ready(function() {
								jQuery("#do_wangguard_delete_splogguers").click(function() {
									if (confirm('<?php 
                echo addslashes(__('Do you confirm to delete all Sploggers?', 'wangguard'));
                ?>
')) {
										jQuery('#wangguard_delete_splogguers').val('1');
										return true;
									}
									else
										return false;
								});
							});
						</script>			
						
						<div id="wangguard-hidden-step-status" style="display: none">
							<p><img id="wangguard-progress-wait" style="vertical-align: middle; margin-right: 8px;" src="<?php 
                echo esc_url(admin_url('images/wpspin_light.gif'));
                ?>
" alt="..." /></p>
						</div>
					<?php 
            }
            ?>
				</div>



				<?php 
            break;
        case "3":
            if (@$_REQUEST['wangguard_delete_splogguers'] == 1) {
                $usersPerStint = 10;
                //how many users to check on each iteration
                $table_name = $wpdb->base_prefix . "wangguarduserstatus";
                $reportedUsers = $wpdb->get_col("select ID from {$table_name} where user_status IN ( 'reported', 'autorep' ) LIMIT 0 , {$usersPerStint}");
                $reportedUsersCount = count($reportedUsers);
                $reportedUsersTotal = (int) @$_REQUEST['wangguard_splogcnt'];
                $reportingUserFrom = (int) @$_REQUEST['wangguard_wiz_from'];
                $reportingUserFrom = $reportingUserFrom > $reportedUsersTotal ? $reportedUsersTotal : $reportingUserFrom;
                $step3Finished = $reportedUsersCount == 0;
                if (!$step3Finished) {
                    ?>
						<h3><?php 
                    echo __("Deleting Splogguers from your site...", "wangguard");
                    ?>
</h3>
						<p><img id="wangguard-progress-wait" style="vertical-align: middle; margin-right: 8px;" src="<?php 
                    echo esc_url(admin_url('images/wpspin_light.gif'));
                    ?>
" alt="..." /><?php 
                    echo sprintf(__("The WangGuard wizard is deleting %d of %d Splogguers from your site.", "wangguard"), $reportingUserFrom, $reportedUsersTotal);
                    ?>
</p>
						<?php 
                    @flush();
                    ?>
						<?php 
                    @ob_flush();
                    ?>
						<?php 
                    foreach ($reportedUsers as $userid) {
                        set_time_limit(15);
                        wangguard_delete_user_and_blogs($userid);
                    }
                    ?>
						<script type="text/javascript">
							document.getElementById('wangguardWizardForm').onsubmit='';
							jQuery(document).ready(function() {
								location.href='admin.php?page=wangguard_wizard&wangguard_step=3&wangguard_delete_splogguers=1&wangguard_splogcnt=<?php 
                    echo $reportedUsersTotal;
                    ?>
&wangguard_wiz_from=<?php 
                    echo $reportingUserFrom + $usersPerStint;
                    ?>
&_wpnonce=<?php 
                    echo wp_create_nonce($wangguard_nonce);
                    ?>
';
							});
						</script>
						<?php 
                } else {
                    ?>
						<h3><?php 
                    echo __("The WangGuard Wizard has finished", "wangguard");
                    ?>
</h3>
						<p><?php 
                    echo sprintf(__("%d sploggers users has been deleted from your site.", "wangguard"), $reportedUsersTotal);
                    ?>
</p>
						<p><a class="button-primary" href="<?php 
                    echo $urlFunc('admin.php?page=wangguard_users');
                    ?>
"><?php 
                    echo __('Click here to manage your Users', 'wangguard');
                    ?>
</a></p>
				<?php 
                }
            } else {
                ?>

					<h3><?php 
                echo __("The WangGuard Wizard has finished", "wangguard");
                ?>
</h3>
					<p><a class="button-primary" href="<?php 
                echo $urlFunc('admin.php?page=wangguard_users');
                ?>
"><?php 
                echo __('Click here to manage your Users', 'wangguard');
                ?>
</a></p>

				<?php 
            }
            break;
        default:
            ?>
				<div id="wangguard-visible-step-status">
					<h3><?php 
            echo __("Welcome to the WangGuard Wizard", "wangguard");
            ?>
</h3>
					<p><?php 
            echo __("This wizard will perform the following actions on your WordPress installation", "wangguard");
            ?>
</p>
					<ol>
						<li><?php 
            echo __("It will report to WangGuard all users you have flagged as 'spam' on your site.", "wangguard");
            ?>
</li>
						<li><?php 
            echo __("For the rest of the users, it will check against WangGuard service if any of them was reported as Splogger.", "wangguard");
            ?>
</li>
						<li><?php 
            echo __("It will let you know how many Sploggers the wizard found (if any) and, optionally, will let you delete your spam users and Sploggers from your site.", "wangguard");
            ?>
</li>
					</ol>
					<p><?php 
            echo sprintf(__("Note: The wizard will NOT verify the users flagged as %s, these are the users for which you've selected the &quot;Not a Splogger&quot; option from the Users admin or flagged as &quot;Not Spam&quot;.", "wangguard"), "<span class='wangguard-status-checked'>" . __("Checked (forced)", "wangguard") . "</span>");
            ?>
</p>
					<?php 
            $valid = wangguard_verify_key($wangguard_api_key);
            if ($valid == 'failed' || $valid == 'invalid') {
                ?>
						<p class="wangguard-info wangguard-error" style="margin-right: 20px;"><?php 
                echo __('Your WangGuard API KEY is invalid.', 'wangguard');
                ?>
</p>
						<?php 
            } else {
                ?>
						<p><?php 
                echo __("Click the button below when you're ready to clean your site!.", "wangguard");
                ?>
</p>
						<input type="hidden" name="wangguard_step" value="1" />
						<p class="submit"><input type="submit" name="submit" class="button-primary" value="<?php 
                _e('Start cleaning my site!', 'wangguard');
                ?>
" /></p>
						<?php 
            }
            ?>
				</div>


				<div id="wangguard-hidden-step-status" style="display: none">
					<h3><?php 
            echo __("Reporting spam users to WangGuard...", "wangguard");
            ?>
</h3>
					<?php 
            $spamUsers = $wpdb->get_col("select ID from {$wpdb->users} where {$sqlSpamWhere}");
            $userCount = count($spamUsers);
            if ($userCount == 0) {
                ?>
						<p><img id="wangguard-progress-wait" style="vertical-align: middle; margin-right: 8px;" src="<?php 
                echo esc_url(admin_url('images/wpspin_light.gif'));
                ?>
" alt="..." /></p>
					<?php 
            } else {
                ?>
						<p><img id="wangguard-progress-wait" style="vertical-align: middle; margin-right: 8px;" src="<?php 
                echo esc_url(admin_url('images/wpspin_light.gif'));
                ?>
" alt="..." /><?php 
                echo sprintf(__("The WangGuard wizard is reporting %d spam users as Sploggers.", "wangguard"), $userCount);
                ?>
</p>
					<?php 
            }
            ?>
				</div>

				<?php 
            break;
    }
    ?>

	</form>

</div>
<?php 
}
/**
 * Admin side AJAX handler
 *
 * @global type $wpdb
 */
function wangguard_ajax_callback()
{
    global $wpdb;
    if (!current_user_can('level_10')) {
        die;
    }
    $userid = intval($_POST['userid']);
    $scope = $_POST['scope'];
    switch ($scope) {
        case "queue_blog_remove":
            //remove blog from queue
            $blogid = intval($_POST['blogid']);
            $table_name = $wpdb->base_prefix . "wangguardreportqueue";
            $wpdb->query($wpdb->prepare("delete from {$table_name} where blog_id = '%d'", $blogid));
            echo "0";
            break;
        case "queue_user_remove":
            //remove user from queue
            $table_name = $wpdb->base_prefix . "wangguardreportqueue";
            $wpdb->query($wpdb->prepare("delete from {$table_name} where ID = '%d'", $userid));
            echo "0";
            break;
        case "domain":
            //flag domain
            $userDomain = new WP_User($userid);
            $domain = wangguard_extract_domain($userDomain->user_email);
            $domain = '%@' . str_replace(array("%", "_"), array("\\%", "\\_"), $domain);
            //get the recordset of the users to flag
            $wpusersRs = $wpdb->get_col($wpdb->prepare("select ID from {$wpdb->users} where user_email LIKE '%s'", $domain));
            echo wangguard_report_users($wpusersRs, $scope);
            break;
        case "blog":
            //flag domain
            $blogid = intval($_POST['blogid']);
            $blog_prefix = $wpdb->get_blog_prefix($blogid);
            $authors = $wpdb->get_results("SELECT user_id, meta_value as caps FROM {$wpdb->users} u, {$wpdb->usermeta} um WHERE u.ID = um.user_id AND meta_key = '{$blog_prefix}capabilities'");
            $authorsArray = array();
            foreach ((array) $authors as $author) {
                $caps = maybe_unserialize($author->caps);
                if (!isset($caps['administrator'])) {
                    continue;
                }
                $authorsArray[] = $author->user_id;
            }
            echo wangguard_report_users($authorsArray, "email");
            break;
        case "rollback-email":
            $wpusersRs = $wpdb->get_col($wpdb->prepare("select ID from {$wpdb->users} where ID = %d", $userid));
            echo wangguard_rollback_report($wpusersRs);
            break;
        default:
            //flag a user
            //get the recordset of the user to flag
            if (wangguard_is_multisite()) {
                $spamFieldName = "spam";
            } else {
                $spamFieldName = "user_status";
            }
            if (function_exists("update_user_status")) {
                update_user_status($userid, $spamFieldName, 1);
                //when flagging the user as spam, the wangguard hook is called to report the user
            } else {
                $wpdb->query($wpdb->prepare("update {$wpdb->users} set {$spamFieldName} = 1 where ID = %d", $userid));
            }
            $wpusersRs = $wpdb->get_col($wpdb->prepare("select ID from {$wpdb->users} where ID = %d", $userid));
            wangguard_make_spam_user($userid);
            echo wangguard_report_users($wpusersRs, $scope);
            break;
    }
    die;
}
function wangguard_queue()
{
    global $wpdb;
    if (!current_user_can('level_10')) {
        die(__('Cheatin&#8217; uh?', 'wangguard'));
    }
    include 'wangguard-class-wp-queue.php';
    $wp_list_table = new WangGuard_Queue_Table();
    $pagenum = $wp_list_table->get_pagenum();
    $messages = array();
    switch ($wp_list_table->current_action()) {
        case 'unreport':
            if (!wp_verify_nonce($_REQUEST['_wpnonce'], "bulk-reports")) {
                die("bad nonce");
            }
            //remove selected blogs from the queue
            $removedBlogs = 0;
            $blogs = (array) $_REQUEST['blogs'];
            foreach ($blogs as $blogid) {
                $blogid = (int) $blogid;
                $table_name = $wpdb->base_prefix . "wangguardreportqueue";
                $wpdb->query($wpdb->prepare("delete from {$table_name} where blog_id = '%d'", $blogid));
                $removedBlogs++;
            }
            //remove selected users from the queue
            $removedUsers = 0;
            $users = (array) $_REQUEST['users'];
            foreach ($users as $userid) {
                $userid = (int) $userid;
                $table_name = $wpdb->base_prefix . "wangguardreportqueue";
                $wpdb->query($wpdb->prepare("delete from {$table_name} where ID = '%d'", $userid));
                $removedUsers++;
            }
            if ($removedBlogs) {
                $messages[] = '<div id="message" class="updated fade"><p><strong>' . sprintf(__("%d blog(s) were removed from the WangGuard Moderation Queue", "wangguard"), $removedBlogs) . '</strong></p></div>';
            }
            if ($removedUsers) {
                $messages[] = '<div id="message" class="updated fade"><p><strong>' . sprintf(__("%d user(s) were removed from the WangGuard Moderation Queue", "wangguard"), $removedUsers) . '</strong></p></div>';
            }
            break;
        case 'reportassplog':
            if (!wp_verify_nonce($_REQUEST['_wpnonce'], "bulk-reports")) {
                die("bad nonce");
            }
            //report selected blogs
            $reportedBlogs = 0;
            $reportedAuthors = 0;
            $authors_ids = array();
            $blogs = (array) $_REQUEST['blogs'];
            $table_name = $wpdb->base_prefix . "wangguardreportqueue";
            foreach ($blogs as $blogid) {
                $blogid = (int) $blogid;
                $reportedBlogs++;
                //remove blog from queue (users are removed on the delete_user hook)
                $wpdb->query($wpdb->prepare("delete from {$table_name} where blog_id = '%d'", $blogid));
                //get the authors of each blog
                $blog_prefix = $wpdb->get_blog_prefix($blogid);
                $authors = $wpdb->get_results("SELECT user_id, meta_value as caps FROM {$wpdb->users} u, {$wpdb->usermeta} um WHERE u.ID = um.user_id AND meta_key = '{$blog_prefix}capabilities'");
                foreach ((array) $authors as $author) {
                    $caps = maybe_unserialize($author->caps);
                    if (!isset($caps['administrator'])) {
                        continue;
                    }
                    $authors_ids[] = $author->user_id;
                }
            }
            $res = wangguard_report_users($authors_ids);
            $resArr = explode(",", $res);
            $reportedAuthors = count($blogs) == 0 ? 0 : count($resArr);
            //report selected users
            $reportedUsers = 0;
            $users = (array) $_REQUEST['users'];
            $res = wangguard_report_users($users);
            $resArr = explode(",", $res);
            $reportedUsers = count($users) == 0 ? 0 : count($resArr);
            if ($reportedBlogs) {
                if (wangguard_get_option("wangguard-delete-users-on-report") == '1') {
                    $messages[] = '<div id="message" class="updated fade"><p><strong>' . sprintf(__("%d blog(s) and %d author(s) was reported as Splogger(s) and deleted", "wangguard"), $reportedBlogs, $reportedAuthors) . '</strong></p></div>';
                } else {
                    $messages[] = '<div id="message" class="updated fade"><p><strong>' . sprintf(__("%d blog(s) and %d author(s) was reported as Splogger(s)", "wangguard"), $reportedBlogs, $reportedAuthors) . '</strong></p></div>';
                }
            }
            if ($reportedUsers) {
                if (wangguard_get_option("wangguard-delete-users-on-report") == '1') {
                    $messages[] = '<div id="message" class="updated fade"><p><strong>' . sprintf(__("%d user(s) were reported as Splogger(s) and deleted", "wangguard"), $reportedUsers) . '</strong></p></div>';
                } else {
                    $messages[] = '<div id="message" class="updated fade"><p><strong>' . sprintf(__("%d user(s) were reported as Splogger(s)", "wangguard"), $reportedUsers) . '</strong></p></div>';
                }
            }
            break;
    }
    if (count($messages)) {
        foreach ($messages as $msg) {
            echo $msg;
        }
    }
    ?>

	
	
	<div class="wrap" id="wangguard-queue-cont">
		<div class="wangguard-confico"><img src="<?php 
    echo WP_PLUGIN_URL;
    ?>
/wangguard/img/queue.png" alt="<?php 
    echo htmlentities(__('WangGuard Moderation Queue', 'wangguard'));
    ?>
" /></div>
		<div class="icon32" id="icon-wangguard"><br></div>
		<h2><?php 
    _e('WangGuard Moderation Queue', 'wangguard');
    ?>
</h2>

		<form action="admin.php" method="get" id="wangguard-queue-form">
			<input type="hidden" name="page" value="wangguard_queue" />
			<?php 
    $wp_list_table->prepare_items();
    $total_pages = $wp_list_table->get_pagination_arg('total_pages');
    if ($pagenum > $total_pages && $total_pages > 0) {
        wp_redirect(add_query_arg('paged', $total_pages));
        exit;
    }
    $wp_list_table->views();
    $wp_list_table->display();
    ?>
	
			
		</form>
		<br class="clear" />
	</div>
	
	<?php 
}
function wangguard_users()
{
    global $wpdb, $wangguard_is_network_admin, $wangguard_nonce, $wangguard_g_splog_users_count;
    if (!current_user_can('level_10')) {
        die(__('Cheatin&#8217; uh?', 'wangguard'));
    }
    include 'wangguard-class-wp-users.php';
    $wp_list_table = new WangGuard_Users_Table();
    $pagenum = $wp_list_table->get_pagenum();
    $messages = array();
    switch ($wp_list_table->current_action()) {
        case 'delete':
            if (!wp_verify_nonce($_REQUEST['_wpnonce'], "bulk-users")) {
                die("bad nonce");
            }
            //report selected users
            $reportedUsers = 0;
            $users = (array) @$_REQUEST['users'];
            if (wangguard_is_multisite() && function_exists("wpmu_delete_user")) {
                $delFunc = 'wpmu_delete_user';
            } else {
                if (!function_exists('wp_delete_user')) {
                    @(include_once ABSPATH . 'wp-admin/includes/user.php');
                }
                $delFunc = 'wp_delete_user';
            }
            $deletedUsers = 0;
            foreach ($users as $spuserID) {
                $user_object = new WP_User($spuserID);
                if (!wangguard_is_admin($user_object)) {
                    $delFunc($spuserID);
                    $deletedUsers++;
                }
            }
            if ($deletedUsers) {
                $messages[] = '<div id="message" class="updated fade"><p><strong>' . sprintf(__("%d user(s) were deleted", "wangguard"), $deletedUsers) . '</strong></p></div>';
            }
            break;
        case 'reportassplog':
            if (!wp_verify_nonce($_REQUEST['_wpnonce'], "bulk-users")) {
                die("bad nonce");
            }
            //report selected users
            $reportedUsers = 0;
            $users = (array) $_REQUEST['users'];
            $res = wangguard_report_users($users);
            $resArr = explode(",", $res);
            $reportedUsers = count($users) == 0 ? 0 : count($resArr);
            if ($reportedUsers) {
                if (wangguard_get_option("wangguard-delete-users-on-report") == '1') {
                    $messages[] = '<div id="message" class="updated fade"><p><strong>' . sprintf(__("%d user(s) were reported as Splogger(s) and deleted", "wangguard"), $reportedUsers) . '</strong></p></div>';
                } else {
                    $messages[] = '<div id="message" class="updated fade"><p><strong>' . sprintf(__("%d user(s) were reported as Splogger(s)", "wangguard"), $reportedUsers) . '</strong></p></div>';
                }
            }
            break;
        case 'spam':
            $spamUsers = 0;
            $users = (array) $_REQUEST['users'];
            foreach ($users as $spuserID) {
                $user = new WP_User($spuserID);
                if (in_array($user->user_login, get_super_admins())) {
                    continue;
                }
                if (function_exists('get_blogs_of_user') && function_exists('update_blog_status')) {
                    $blogs = get_blogs_of_user($spuserID, true);
                    foreach ((array) $blogs as $key => $details) {
                        //						if ( $details->userblog_id != $current_site->blog_id ) // main blog not a spam !
                        //							update_blog_status( $details->userblog_id, 'spam', '1' );
                        $isMainBlog = false;
                        if (isset($current_site)) {
                            $isMainBlog = $details->userblog_id != $current_site->blog_id;
                            // main blog not a spam !
                        } elseif (defined("BP_ROOT_BLOG")) {
                            $isMainBlog = 1 == $details->userblog_id || BP_ROOT_BLOG == $details->userblog_id;
                        } else {
                            $isMainBlog = $details->userblog_id == 1;
                        }
                        $userIsAuthor = false;
                        if (!$isMainBlog) {
                            //Only works on WP 3+
                            $blog_prefix = $wpdb->get_blog_prefix($details->userblog_id);
                            $authorcaps = $wpdb->get_var(sprintf("SELECT meta_value as caps FROM {$wpdb->users} u, {$wpdb->usermeta} um WHERE u.ID = %d and u.ID = um.user_id AND meta_key = '{$blog_prefix}capabilities'", $spuserID));
                            $caps = maybe_unserialize($authorcaps);
                            $userIsAuthor = isset($caps['administrator']);
                        }
                        //Update blog to spam if the user is the author and its not the main blog
                        if (!$isMainBlog && $userIsAuthor) {
                            @update_blog_status($details->userblog_id, 'spam', '1');
                            //remove blog from queue
                            $table_name = $wpdb->base_prefix . "wangguardreportqueue";
                            $wpdb->query($wpdb->prepare("delete from {$table_name} where blog_id = '%d'", $details->userblog_id));
                        }
                    }
                }
                if (function_exists('update_user_status')) {
                    update_user_status($spuserID, 'spam', '1');
                }
                $wpdb->update($wpdb->users, array('user_status' => 1), array('ID' => $spuserID));
                $spamUsers++;
            }
            if ($spamUsers) {
                $messages[] = '<div id="message" class="updated fade"><p><strong>' . sprintf(__("%d user(s) were marked as Spam", "wangguard"), $spamUsers) . '</strong></p></div>';
            }
            break;
        case 'notspam':
            $spamUsers = 0;
            $users = (array) $_REQUEST['users'];
            foreach ($users as $spuserID) {
                if (function_exists('get_blogs_of_user') && function_exists('update_blog_status')) {
                    $blogs = get_blogs_of_user($spuserID, true);
                    foreach ((array) $blogs as $key => $details) {
                        update_blog_status($details->userblog_id, 'spam', '0');
                    }
                }
                if (function_exists('update_user_status')) {
                    update_user_status($spuserID, 'spam', '0');
                }
                $wpdb->update($wpdb->users, array('user_status' => 0), array('ID' => $spuserID));
                $spamUsers++;
            }
            if ($spamUsers) {
                $messages[] = '<div id="message" class="updated fade"><p><strong>' . sprintf(__("%d user(s) were marked as Safe", "wangguard"), $spamUsers) . '</strong></p></div>';
            }
            break;
    }
    if (count($messages)) {
        foreach ($messages as $msg) {
            echo $msg;
        }
    }
    ?>

	
	
	<div class="wrap" id="wangguard-users-cont">
		<div class="wangguard-confico"><img src="<?php 
    echo WP_PLUGIN_URL;
    ?>
/wangguard/img/users.png" alt="<?php 
    echo htmlentities(__('WangGuard Users', 'wangguard'));
    ?>
" /></div>
		<div class="icon32" id="icon-wangguard"><br></div>
		<h2><?php 
    _e('WangGuard Users', 'wangguard');
    ?>
</h2>

		<?php 
    $wp_list_table->prepare_items();
    ?>
		
		<form action="" method="get">
			<input type="hidden" name="page" value="wangguard_users" />
			<?php 
    $wp_list_table->search_box(__('Search Users'), 'user');
    ?>
		</form>
		
	
		<form action="admin.php" method="get" id="wangguard-users-form">

			<input type="hidden" name="page" value="wangguard_users" />
			<?php 
    $total_pages = $wp_list_table->get_pagination_arg('total_pages');
    if ($pagenum > $total_pages && $total_pages > 0) {
        wp_redirect(add_query_arg('paged', $total_pages));
        exit;
    }
    $wp_list_table->views();
    ?>
			
			<?php 
    $requestType = "";
    if (isset($_REQUEST['type'])) {
        $requestType = $_REQUEST['type'];
    }
    if ($requestType == 'spl') {
        ?>
				<div id="wangguard-deleteallsplcont" class="subsubsub"><a class="button-primary" id="wangguard-deleteallspl" href="javascript:void(0)"><?php 
        echo __('Delete All Sploggers', 'wangguard');
        ?>
</a></div>
				<script type="text/javascript">
					<?php 
        $urlFunc = "admin_url";
        if ($wangguard_is_network_admin && function_exists("network_admin_url")) {
            $urlFunc = "network_admin_url";
        }
        $deleteSPURL = $urlFunc('admin.php?page=wangguard_wizard&wangguard_delete_splogguers=1&wangguard_splogcnt=' . $wangguard_g_splog_users_count . '&wangguard_step=3&_wpnonce=' . wp_create_nonce($wangguard_nonce));
        ?>
					
					jQuery("a#wangguard-deleteallspl").click(function() {
						if (confirm('<?php 
        echo __('Do you confirm to delete ALL Sploggers?', 'wangguard');
        ?>
')) {
							document.location = '<?php 
        echo $deleteSPURL;
        ?>
';
						}
					});
				</script>
			<?php 
    }
    ?>
				

			<?php 
    $wp_list_table->display();
    ?>
	
			
		</form>
		<br class="clear" />
	</div>
	<?php 
}