<?php

define('IN_COPPERMINE', true);
define('BUDDY_PHP', true);
require('include/init.inc.php');
require('include/mailer.inc.php');


function email_the_user($message, $subject)
{
	global $CONFIG, $friend_name, $lang_buddy;
	$template_vars = array(
	'{SITE_NAME}' => $CONFIG['gallery_name'],
	'{BUDDY}' => (USER_NAME),
	'{USER_NAME}' => $friend_name['user_name'],
	'{ADMIN}' => $CONFIG['gallery_name'],
	'{LINK}' => $CONFIG['ecards_more_pic_target'].$lang_buddy['link'],
	);
	$mail_body=nl2br(strtr($message, $template_vars));
	$send_subject=(USER_NAME)." ".$subject." ".$CONFIG['gallery_name'];
	if ($friend_name['user_email'] && $friend_name['notify'] == '1'){
		cpg_mail($friend_name['user_email'], $send_subject, $mail_body, 'text/plain', $CONFIG['gallery_name'], $CONFIG['gallery_admin_email'] );
	}
	return true;
}

function user_details($id){
	global $cpg_udb;
	$friend_name = $cpg_udb->get_user_infos($id);
	return $friend_name;
}

if (!USER_ID || $CONFIG['enable_buddy'] == 0) cpg_die(ERROR, $lang_errors['access_denied'], __FILE__, __LINE__);
$event = isset($_REQUEST['event']) ? $_REQUEST['event'] : null;
$view = isset($_REQUEST['view']) ? (int) $_REQUEST['view'] : null;
if(isset($event)) {
	$id = isset($_REQUEST['id']) ? (int) $_REQUEST['id'] : null;
	$referer = "buddy_manage.php?event=refresh";

	switch ($event) {
		case "del":
		$mark_list=$_POST['list'];
		if (!$mark_list) break;
		while(list($key, $value) = each($mark_list))
			if ($value)
			{
				cpg_db_query("DELETE FROM {$CONFIG['TABLE_BUDDY']} WHERE id = ".$key." AND user_id = ".(USER_ID));
				pageheader($lang_buddy['your_buddies'], "<META http-equiv=\"refresh\" content=\"2;url=$referer\">");
				msg_box($lang_buddy['your_buddies'], $lang_buddy['deleted'], $lang_buddy['continue'], $referer);
		        pagefooter();
		        ob_end_flush();
		        die();
			}
		break;

		case "make":
			if (!$id) break;
			$result = cpg_db_query("SELECT id FROM {$CONFIG['TABLE_BUDDY_REQ']} WHERE buddy_from = ".USER_ID." AND buddy_to = $id");
			$total= @mysql_num_rows($result);
			$result = cpg_db_query("SELECT id FROM {$CONFIG['TABLE_BUDDY_REQ']} WHERE buddy_from =  $id AND buddy_to = ".USER_ID);
			$total2= @mysql_num_rows($result);
			if ($total == 0 && $total2 == 0) {
				if (USER_ID != $id){
					$result = cpg_db_query("SELECT buddy_ok, buddy_block FROM {$CONFIG['TABLE_BUDDY']} WHERE user_id = $id AND buddy_id = ".(USER_ID));
					$user_is_added= mysql_fetch_row($result);
					if ($user_is_added[1] == "YES") {
						pageheader($lang_buddy['your_buddies'], "<META http-equiv=\"refresh\" content=\"2;url=$referer\">");
						msg_box($lang_buddy['your_buddies'], $lang_buddy['blocked'], $lang_buddy['continue'], $referer);
				        pagefooter();
				        ob_end_flush();
				        die();
					} else {
						$result = cpg_db_query("SELECT buddy_block, buddy_ok FROM {$CONFIG['TABLE_BUDDY']} WHERE buddy_id = $id AND user_id = ".(USER_ID));
						$on_block_list= mysql_fetch_row($result);
						if (isset($on_block_list[1])) {
							pageheader($lang_buddy['your_buddies'], "<META http-equiv=\"refresh\" content=\"2;url=$referer\">");
							msg_box($lang_buddy['your_buddies'], $lang_buddy['already_buddies'], $lang_buddy['continue'], $referer);
					        pagefooter();
					        ob_end_flush();
					        die();
						}
						elseif ($on_block_list[0] == "YES") {
							cpg_db_query("DELETE FROM {$CONFIG['TABLE_BUDDY']} WHERE buddy_id = $id AND user_id = ".(USER_ID));
						}

						$results = cpg_db_query ("INSERT INTO `{$CONFIG['TABLE_BUDDY_REQ']}` (`buddy_from`, `buddy_to`, `since`) VALUES ('".USER_ID."', '$id', ".time().")");
						if ($CONFIG['enable_buddy_email']==1) {
							$friend_name = user_details($id);
							email_the_user($lang_buddy_email_request, $lang_buddy['email_subject_request']);
						}
						pageheader($lang_buddy['your_buddies'], "<META http-equiv=\"refresh\" content=\"2;url=$referer\">");
						msg_box($lang_buddy['your_buddies'], $lang_buddy['adding_request'], $lang_buddy['continue'], $referer);
				        pagefooter();
				        ob_end_flush();
				        die();
					}
				} else {
						pageheader($lang_buddy['your_buddies'], "<META http-equiv=\"refresh\" content=\"2;url=$referer\">");
						msg_box($lang_buddy['your_buddies'], $lang_buddy['self'], $lang_buddy['continue'], $referer);
				        pagefooter();
				        ob_end_flush();
				        die();
				}
			} elseif ($total != 0) {
						pageheader($lang_buddy['your_buddies'], "<META http-equiv=\"refresh\" content=\"2;url=$referer\">");
						msg_box($lang_buddy['your_buddies'], $lang_buddy['request_already_sent'], $lang_buddy['continue'], $referer);
				        pagefooter();
				        ob_end_flush();
				        die();
			} elseif ($total2 != 0) {
						$friend_name = user_details($id);
						pageheader($lang_buddy['your_buddies'], "<META http-equiv=\"refresh\" content=\"2;url=$referer\">");
						msg_box($lang_buddy['your_buddies'], $friend_name[0].$lang_buddy['already_requested'], $lang_buddy['continue'], $referer);
				        pagefooter();
				        ob_end_flush();
				        die();
			}
		break;

		case "ok":
		$mark_list=$_POST['list'];
		if (!$mark_list) break;
		while(list($key, $value) = each($mark_list))
			if ($value)
			{
			$initiate = cpg_db_query("SELECT buddy_from FROM {$CONFIG['TABLE_BUDDY_REQ']} WHERE id = $key");
			$result = mysql_fetch_row($initiate);
			$id = $result[0];
			$result = cpg_db_query("SELECT id FROM {$CONFIG['TABLE_BUDDY_REQ']} WHERE buddy_from = $id AND buddy_to = ".USER_ID);
			$total= @mysql_num_rows($result);
			$result = cpg_db_query("SELECT id FROM {$CONFIG['TABLE_BUDDY']} WHERE user_id = $id AND buddy_id = ".(USER_ID));
			$total2= @mysql_num_rows($result);
			$result = cpg_db_query("SELECT id FROM {$CONFIG['TABLE_BUDDY']} WHERE buddy_id = $id AND user_id = ".(USER_ID));
			$total3= @mysql_num_rows($result);
			if ($total != 0) {
				if ($total3 == 0) cpg_db_query ("INSERT INTO `{$CONFIG['TABLE_BUDDY']}` (`user_id`, `buddy_id`, `buddy_ok`, `since`) VALUES ('".USER_ID."', '$id', 'YES', ".time().")");
				if ($total2 == 0) cpg_db_query ("INSERT INTO `{$CONFIG['TABLE_BUDDY']}` (`user_id`, `buddy_id`, `buddy_ok`, `since`) VALUES ('$id', '".USER_ID."', 'YES', ".time().")");
				cpg_db_query("DELETE FROM {$CONFIG['TABLE_BUDDY_REQ']} WHERE buddy_from = $id AND buddy_to = ".USER_ID);
				if ($CONFIG['enable_buddy_email']==1) {
					$friend_name = user_details($id);
					email_the_user($lang_buddy_email_accept, $lang_buddy['email_subject_accept']);
				}
				$success = 1;
			}
			if ($success){
					pageheader($lang_buddy['your_buddies'], "<META http-equiv=\"refresh\" content=\"2;url=$referer\">");
					msg_box($lang_buddy['your_buddies'], $lang_buddy['added'], $lang_buddy['continue'], $referer);
			        pagefooter();
			        ob_end_flush();
			        die();
			}
		}
		break;

		case "deny":
		$mark_list=$_POST['list'];
		if (!$mark_list) break;
		while(list($key, $value) = each($mark_list))
			if ($value)
			{
			$initiate = cpg_db_query("SELECT buddy_from FROM {$CONFIG['TABLE_BUDDY_REQ']} WHERE id = $key");
			$result = mysql_fetch_row($initiate);
			$id = $result[0];

			$result = cpg_db_query("SELECT id FROM {$CONFIG['TABLE_BUDDY_REQ']} WHERE buddy_from = $id AND buddy_to = ".USER_ID);
			$total= @mysql_num_rows($result);
			$result = cpg_db_query("SELECT id FROM {$CONFIG['TABLE_BUDDY']} WHERE user_id = ".(USER_ID)."  AND buddy_id = $id");
			$total2= @mysql_num_rows($result);
			if ($total != 0) {
				if ($total2 != 0) {
					cpg_db_query ("UPDATE `{$CONFIG['TABLE_BUDDY']}` SET `buddy_ok`='NO', `buddy_block`='YES' WHERE user_id = ".(USER_ID)."  AND buddy_id = $id");
				} else {
					cpg_db_query ("INSERT INTO `{$CONFIG['TABLE_BUDDY']}` (`user_id`, `buddy_id`, `buddy_block`, `since`) VALUES ('".USER_ID."', '$id', 'YES', ".time().")");
				}
				cpg_db_query("DELETE FROM {$CONFIG['TABLE_BUDDY_REQ']} WHERE buddy_from = $id AND buddy_to = ".USER_ID);
				if ($CONFIG['enable_buddy_email']==1) {
					$friend_name = user_details($id);
					email_the_user($lang_buddy_email_deny, $lang_buddy['email_subject_deny']);
				}
				$success = 1;
			}
			if ($success){
				pageheader($lang_buddy['your_buddies'], "<META http-equiv=\"refresh\" content=\"2;url=$referer\">");
				msg_box($lang_buddy['your_buddies'], $lang_buddy['denied'], $lang_buddy['continue'], $referer);
		        pagefooter();
		        ob_end_flush();
		        die();
			}
		}
		break;

		case "req_del":
		$mark_list=$_POST['list'];
		if (!$mark_list) break;
		while(list($key, $value) = each($mark_list))
			if ($value)
			{
				cpg_db_query("DELETE FROM {$CONFIG['TABLE_BUDDY_REQ']} WHERE id = $key AND buddy_from = ".(USER_ID));
				$success = 1;
			}
			if ($success){
				pageheader($lang_buddy['your_buddies'], "<META http-equiv=\"refresh\" content=\"2;url=$referer\">");
				msg_box($lang_buddy['your_buddies'], $lang_buddy['req_deleted'], $lang_buddy['continue'], $referer);
		        pagefooter();
		        ob_end_flush();
		        die();
			}
		break;

		case "reserve":
		break;
	}
}

//main code for displaying the stats

		if($CONFIG['enable_buddy'] != 0 && (USER_ID)){
			$result = cpg_db_query("SELECT count(*) FROM  {$CONFIG['TABLE_BUDDY']} WHERE user_id=".(USER_ID)." AND buddy_ok = 'YES'");
			list($tot_view[0]) = @mysql_fetch_array($result);
			mysql_free_result($result);
			$result = cpg_db_query("SELECT count(*) FROM  {$CONFIG['TABLE_BUDDY']} WHERE user_id=".(USER_ID)." AND buddy_block = 'YES'");
			list($tot_view[1]) = @mysql_fetch_array($result);
			mysql_free_result($result);
			$result = cpg_db_query("SELECT count(*) FROM  {$CONFIG['TABLE_BUDDY_REQ']} WHERE buddy_to=".(USER_ID));
			list($tot_view[2]) = @mysql_fetch_array($result);
			mysql_free_result($result);
			$result = cpg_db_query("SELECT count(*) FROM  {$CONFIG['TABLE_BUDDY_REQ']} WHERE buddy_from=".(USER_ID));
			list($tot_view[3]) = @mysql_fetch_array($result);
			mysql_free_result($result);

			$total_all=$tot_view[0]+$tot_view[1]+$tot_view[2]+$tot_view[3];
			if ($total_all==0) $total_all=0;
			if (!isset($view) && $tot_view[2] != 0) {$view=2;}
			elseif (!isset($view)) {$view=0;}// view is for switching between boxes

			$proc[0] = ceil($tot_view[0] / $total_all * 480);
			$proc[1] = ceil($tot_view[1]/ $total_all * 480);
			$proc[2] = ceil($tot_view[2] / $total_all * 480);
			$proc[3] = ceil($tot_view[3] / $total_all * 480);

			$view_1 = '';
			$view_2 = '';
			$view_3 = '';
			$view_0 = '';

		//are onlinestats enabled in config? If yes we can display wether buddy is on or off
			preg_match("/onlinestats/",$CONFIG['main_page_layout']) ? $onlinestats=1 : $onlinestats=0;
		//tabs

		//if no joins possible get user name and avatar url now
		if($cpg_udb->can_join()){
		    $sort_codes = array(
				'buddy_name_a' => 'buddy_name ASC',
		        'buddy_name_d' => 'buddy_name DESC',
				'date_a' => 'since ASC',
		        'date_d' => 'since DESC',
		        );
		} else {
			$sort_codes = array(
				'date_a' => 'since ASC',
		        'date_d' => 'since DESC',
				);
		}

		    $sort = (!isset($_GET['sort']) || !isset($sort_codes[$_GET['sort']])) ? 'date_d' : $_GET['sort'];

		    $tab_tmpl = array('left_text' => '<td width="100%%" align="left" valign="middle" class="tableh1_compact" style="white-space: nowrap"><b>' . $lang_buddy['u_buddies_on_p_pages'] . '</b></td>' . "\n",
		        'tab_header' => '',
		        'tab_trailer' => '',
		        'active_tab' => '<td><img src="images/spacer.gif" width="1" height="1"></td>' . "\n" . '<td align="center" valign="middle" class="tableb_compact"><b>%d</b></td>',
		        'inactive_tab' => '<td><img src="images/spacer.gif" width="1" height="1"></td>' . "\n" . '<td align="center" valign="middle" class="navmenu"><a href="' . $PHP_SELF . '?page=%d&view=' . $view . '&sort=' . $sort . '"<b>%d</b></a></td>' . "\n",
				'active_next_tab' => '<td><img src="images/spacer.gif" width="1" height="1"></td>' . "\n" . '<td align="center" valign="middle" class="tableb_compact"><b>%s</b></td>',
				'inactive_next_tab' => '<td><img src="images/spacer.gif" width="1" height="1"></td>' . "\n" . '<td align="center" valign="middle" class="navmenu"><a href="' . $PHP_SELF . '?page=%s&view=' . $view . '&sort=' . $sort . '"<b>Next</b></a></td>',
				'active_prev_tab' => '<td><img src="images/spacer.gif" width="1" height="1"></td>' . "\n" . '<td align="center" valign="middle" class="tableb_compact"><b>%s</b></td>',
				'inactive_prev_tab' => '<td><img src="images/spacer.gif" width="1" height="1"></td>' . "\n" . '<td align="center" valign="middle" class="navmenu"><a href="' . $PHP_SELF . '?page=%s&view=' . $view . '&sort=' . $sort . '"<b>Prev</b></a></td>'
				);
		    $users_per_page = 25;
		    $page = isset($_GET['page']) ? (int)$_GET['page'] : 1;
		    $lower_limit = ($page-1) * $users_per_page;
		    $total_pages = ceil($tot_view[$view] / $users_per_page);

			if($view=="1") { $thisfolder=1 ; $view_1=' selected="selected"'; $name = $lang_buddy['blocked_users']; $status_img="buddy_blocked.gif";
			$check_options = "<option value=\"del\">".$lang_buddy['delete_blocked']."</option>";
			}
			if($view=="2") { $thisfolder=2 ; $view_2=' selected="selected"'; $name = $lang_buddy['buddy_requested']; $status_img="buddy_requested.gif";
			$check_options = "<option value=\"ok\">".$lang_buddy['accept']."</option>";
			$check_options .= "<option value=\"deny\">".$lang_buddy['deny']."</option>";
			}
			if($view=="3") { $thisfolder=3 ; $view_3=' selected="selected"'; $name = $lang_buddy['I_requested']."... "; $status_img="buddy_requested.gif";
			$check_options = "<option value=\"req_del\">".$lang_buddy['rem_request']."</option>";
			}
			if($view=="0") {$thisfolder=0 ; $view_0=' selected="selected"'; $name = $lang_buddy['active']; $status_img="buddy_ok.gif";
			$check_options = "<option value=\"del\">".$lang_buddy['delete_buddies']."</option>";
			}
		}
		else
		$status = '';

$sql2 = "ORDER BY ". $sort_codes[$sort] ." LIMIT $lower_limit, $users_per_page;";
$result = $cpg_udb->get_buddy_infos($view,$sql2);
$total= @mysql_num_rows($result);


if ($tot_view[$view]>0) $tabs = create_tabs($tot_view[$view], $page, $total_pages, $tab_tmpl);
$lb = "<select name=\"album_listbox\" class=\"listbox\" onChange=\"if(this.options[this.selectedIndex].value) window.location.href='$PHP_SELF?page=$page&sort='+this.options[this.selectedIndex].value;\">\n";
	foreach($sort_codes as $key => $value) {
		  $selected = ($key == $sort) ? "SELECTED" : "";
		  $lb .= "        <option value=\"" . $key . "\" $selected>" . $lang_buddy[$key] . "</option>\n";
	}
$lb .= "</select>\n";

pageheader($lang_buddy['your_buddies']);

echo <<<EOT
<script type="text/javascript" language="javascript">
<!--
function checkBoxes (form, check) {
  for (var c = 0; c < form.elements.length; c++)
    if (form.elements[c].type == 'checkbox')
      form.elements[c].checked = check;
}
-->
</script>


<form action="buddy_manage.php" method="post">
<table class="maintable" cellpadding="6" cellspacing="1" border="0" width="100%" align="center">
<thead>
	<tr>
		<td class="tableh1" colspan="4" style="padding: 6px 0 6px 6px">
			<strong>$lang_buddy[users_in]</strong><span>: <span>$name</span></span>
		</td>
	</tr>
</thead>
<tbody>
	<tr>
		<td class="panelsurround" align="center">
		<div class="panel">
			<div style="width:480px" align="center">
				<fieldset class="fieldset">
					<legend>$lang_buddy[your_buddies]</legend>
					<table cellpadding="0" cellspacing="3" border="0" width="100%">
					<tr>
						<td><strong>$name</strong> $lang_buddy[contains] $tot_view[$view] $lang_buddy[users_sm].<br /><br />$lang_buddy[in_total] $total_all $lang_buddy[users_sm] $lang_buddy[in_list]</td>
					</tr>
					<tr>
						<td>
							<table style="border:2px groove" cellpadding="0" cellspacing="1" border="0" width="100%">
							<tr>
								<td width="$proc[0]" class="pms_linecolor1" title="$lang_buddy[active]: $tot_view[0]">&nbsp;</td>
								<td width="$proc[3]" class="pms_linecolor2" title="$lang_buddy[I_requested]: $tot_view[3]">&nbsp;</td>
								<td width="$proc[2]" class="pms_linecolor5" title="$lang_buddy[buddy_requested]: $tot_view[2]">&nbsp;</td>
								<td width="$proc[1]" class="pms_linecolor3" title="$lang_buddy[blocked]: $tot_view[1]">&nbsp;</td>
							</tr>
							</table>
							<table cellpadding="0" cellspacing="1" border="0" width="100%">
							<tr>
							<td width="8" class="pms_linecolor1" title="">&nbsp;</td><td colspan="3">$lang_buddy[active_short]</td>
							<td width="8" class="pms_linecolor2" title="">&nbsp;</td><td colspan="3">$lang_buddy[I_requested_short]</td>
							<td width="8" class="pms_linecolor5" title="">&nbsp;</td><td colspan="3">$lang_buddy[buddy_requested_short]</td>
							<td width="8" class="pms_linecolor3" title="">&nbsp;</td><td colspan="3">$lang_buddy[blocked_users_short]</td>
							</tr>
							</table>
						</td>
					</tr>
					<tr>
						<td>
							<br />$lang_buddy[switch]:<br />
							<select name="view" style="width: 250px;" onchange="this.form.submit();">
							<option  value="0" $view_0>$lang_buddy[active] ($tot_view[0] users)</option>
							<option value="3" $view_3>$lang_buddy[I_requested] ($tot_view[3] users)</option>
							<option value="2" $view_2>$lang_buddy[buddy_requested] ($tot_view[2] users)</option>
							<option value="1" $view_1>$lang_buddy[blocked_users] ($tot_view[1] users)</option>
							</select><!-- go button -->
							<input type="submit" class="button" value="Go"  />
						</td>
					</tr>
					</table>
				</fieldset>
			</div>
		</div>
		</td>
	</tr>
</tbody>
</table>
</form>

<br />
EOT;

if ($tot_view[$view] == 0) { // if no buddies in this box we don't need the check/ uncheck all box in the toptable header
$display_checkall = "";
} else {
$display_checkall = <<<EOT
<form action="buddy_manage.php" method="post" name="form">
			<span class="smallfont" style="float:right">
				<label for="checkall_all">
				$lang_buddy[users]: <strong>$total</strong>
				<input type="checkbox" name="allbox" id="checkall_all"  onclick="checkBoxes(this.form, this.checked)">
				</label>
			</span>
EOT;
}



echo <<<EOT
<table class="maintable" cellpadding="0" cellspacing="1" border="0" width="100%" align="center">
<thead>
	<tr>
		<td class="tableh1" colspan="4" style="padding: 6px 0 6px 6px">
		$display_checkall
			<strong>$lang_buddy[users_in]</strong><span>: <span>$name</span></span>
		</td>
	</tr>
</thead>
<tr><td>
<table class="tborder" cellpadding="6" cellspacing="1" border="0" width="100%" align="center">
EOT;
if ($tot_view[$view] == 0) {
echo <<<EOT
	<tr>
	<td class="alt1">
	$lang_buddy[no_user]
	</td>
	</tr>
EOT;
}


if ($total>0){
	//get all the data
	while($i=mysql_fetch_array($result))
	{
	$id = $i['id'];

	//if no joins possible get user name and avatar url now
	if(!$cpg_udb->can_join()){
		//no joins -> we need to query the username and email
		$no_join_user_info = $cpg_udb->get_user_avatar($i['buddy_id']);
		$i['buddy_name'] = $no_join_user_info['buddy_name'];
		$i['avatar_url'] = $no_join_user_info['avatar_url'];
	}

if ($i['avatar_url']) {
$avatar_url=AVATAR_PATH.$i['avatar_url'];
$preview = <<<EOT
	<img src="$avatar_url" alt="" class="image" /></a>
EOT;
} else {
		$preview = "&nbsp;&nbsp;";
		}

		$thisdate=localised_date($i['since'], $comment_date_fmt);
		if ($CONFIG['buddy_enhanced_display'] == 1) {


        if ($FORBIDDEN_SET != "") $FORBIDDEN_SET = $ALBUM_SET;
        $query = "SELECT count(*), MAX(pid) FROM {$CONFIG['TABLE_PICTURES']} AS p WHERE owner_id = ".$i['buddy_id']." AND approved = 'YES' $FORBIDDEN_SET";
        $result2 = cpg_db_query($query);
        $nbEnr = mysql_fetch_array($result2);
        $picture_count = $nbEnr[0];
        mysql_free_result($result2);

        $result2 = cpg_db_query("SELECT count(*) FROM {$CONFIG['TABLE_ALBUMS']} AS p WHERE category = '" . (FIRST_USER_CAT + $i['buddy_id']) . "' $FORBIDDEN_SET");
        $nbEnr = mysql_fetch_array($result2);
        $album_count = $nbEnr[0];
        mysql_free_result($result2);

        $result2 = cpg_db_query("SELECT count(*), MAX(msg_id) FROM {$CONFIG['TABLE_COMMENTS']} as c, {$CONFIG['TABLE_PICTURES']} as p WHERE c.pid = p.pid AND author_id = ".$i['buddy_id']." $FORBIDDEN_SET");
        $nbEnr = mysql_fetch_array($result2);
        $comment_count = $nbEnr[0];
        mysql_free_result($result2);
        $lastup = "";
        $lastcom = "";
        $buddy_album = "";
        if ($comment_count) {
			$lastcom = "<a href=\"thumbnails.php?album=lastcomby&uid=" . $i['buddy_id'] . "\">".$lang_buddy['last_comments']."</a>";
		}
        if ($album_count) {
			$buddy_album = "<a href=\"index.php?cat=" . (FIRST_USER_CAT + $i['buddy_id']) . "\">&nbsp;".$lang_buddy['user_album']."</a>";
		}
        if ($picture_count) {
			$lastup = "<a href=\"thumbnails.php?album=lastupby&uid=" . $i['buddy_id'] . "\">&nbsp;".$lang_buddy['last_uploads']."</a>";
		}



}

if ($CONFIG['pms_enabled']) {
$PMS_link = "<span class=\"small_font\"> - <a href=\"".(PM_LINK).$i['buddy_id']."\"><img src=\"{$THEME_DIR}images/pm.gif\" alt=\"\" border=\"0\" /></a></span>";
}

if($onlinestats && $view==0){
	$result_online = cpg_db_query("SELECT user_id FROM {$CONFIG['TABLE_PREFIX']}mod_online WHERE user_id = '".$i['buddy_id']."'");
	$user_data['isonline'] = mysql_num_rows($result_online);
	mysql_free_result($result_online);
		if ($user_data['isonline'] != "") {
		$online = "<span class=\"small_font\"> - ( <img src=\"{$THEME_DIR}images/useron.gif\" alt=\"\" border=\"0\" /> $lang_buddy[on] )</span>";
		echo "";
		} else {
		$online = "<span class=\"small_font\"> - ( <img src=\"{$THEME_DIR}images/useroff.gif\" alt=\"\" border=\"0\" /> $lang_buddy[off] )</span>";
		}
} else {
$online = "";
}

echo <<<EOT

	<tr>
	<td class="alt1"><img src="{$THEME_DIR}images/{$status_img}" alt="" border="0" /></td>
	<td class="alt2">$preview</td>
	<td class="alt1Active" id="pmid" width="40%">
	$i[buddy_name] $online $PMS_link<br /> <a href="profile.php?uid=$i[buddy_id]">$lang_buddy[check_profile]</a>
	</td>
	<td class="alt1Active" id="pmid" width="60%" align=right>
		<div class="smallfont">
		$thisdate<br />
		$lastcom
		$lastup
		$buddy_album
		</div>
	</td>
	<td class="alt2" align="center" style="padding:0px"><input type="checkbox" id="checkall" name="list[$id]" value="list[$id]" /></td>
</tr>

EOT;



	}
mysql_free_result($result);
}


echo <<<EOT
</tbody></table>
<table class="tableb" cellpadding="0" cellspacing="0" border="0" width="100%" align="center">
<tr><td><br /></td></tr>
</table>
EOT;


if ($tot_view[$view] == 0) {
echo <<<EOT
<table class="tablef graybox" cellpadding="0" cellspacing="0" border="0" width="100%" align="center">
<thead>
	<tr>
		<td class="tablef" align="left" colspan="2">$lang_buddy[chose_other]</td>
	</tr>
</thead>
</table>
EOT;
} else {
echo <<<EOT
<table class="tablef graybox" cellpadding="0" cellspacing="0" border="0" width="100%" align="center">
<thead>
	<tr>
		<td class="tablef" align="left" colspan="2">
			<div class="smallfont">
				$lang_buddy[selected]
				<select name="event">
					<option value="do_nothing">$lang_buddy[make_selection]</option>
					$check_options
				</select>
				<input type="hidden" name="view" value="$view" />
				<input type="submit" class="button" value="$lang_buddy[GO]" />
			</div>
		</td></form>
		<form method="post" action="$PHP_SELF?op=new_user">
		<td class="tablef" align="right" colspan="2">
			<div class="smallfont">
				Sort by $lb
			</div>
		</td></form>
	</tr>
</thead>
      <tr>
              <td colspan="4" style="padding: 0px;">
                      <table width="100%" cellspacing="0" cellpadding="0">
                              <tr>
                                      $tabs
                              </tr>
                      </table>
              </td>
      </tr>
</table>
EOT;
}



endtable();
pagefooter();
ob_end_flush();
?>