<?php
define('IN_PHPBB', true);
define('BB_SCRIPT', 'file');
define('BB_ROOT', './');
require(BB_ROOT .'common.php');
$user->session_start(array('req_login' => true));
$like = (int) request_var('like', '');
$dislike = (int) request_var('dislike', '');
$update = (int) request_var('update', '');
$user = (int) request_var('user', '');
$start = (int) request_var('start', '');
if($like)
{
/* 3 requests as a result. Need to be optimised */ //optimization
if( DB()->num_rows( DB()->sql_query( "SELECT * FROM " . BB_POSTS. " WHERE poster_id = '" . $userdata['user_id'] . "' AND post_id = '" . $like . "'" ) ) == 0 )
{
//Updating counter if sympathy is not exist
DB()->query("
UPDATE bb_users
SET user_sympathies = user_sympathies + 1
WHERE user_id = (
SELECT
poster_id
FROM bb_posts
WHERE post_id = '" . $like . "'
)
AND NOT EXISTS( SELECT * FROM " . BB_POSTS_LIKES . " WHERE author_id = '" . $userdata['user_id'] . "' AND post_id = '" . $like . "' )
");
//Adding sympathy
DB()->query("
INSERT INTO " . BB_POSTS_LIKES . " (post_id, author_id, vote_time)
SELECT '" . $like . "', '" . $userdata['user_id'] . "', '" . time() . "' FROM DUAL
WHERE NOT EXISTS (SELECT * FROM " . BB_POSTS_LIKES . "
WHERE post_id = '" . $like . "' AND author_id = '" . $userdata['user_id'] . "')
LIMIT 1
");
}
}
elseif($dislike)
{
/* 3 requests as a result. Need to be optimised */
if( DB()->num_rows( DB()->sql_query( "SELECT * FROM " . BB_POSTS_LIKES. " WHERE author_id = '" . $userdata['user_id'] . "' AND post_id = '" . $dislike . "'" ) ) > 0 )
{
//Deleting sympathy
DB()->query( "
DELETE FROM " . BB_POSTS_LIKES . "
WHERE post_id = '" . $dislike . "'
AND author_id = '" . $userdata['user_id'] . "'
" );
//Updating counter
DB()->query( "
UPDATE " . BB_USERS . "
SET user_sympathies = user_sympathies - 1
WHERE user_id = (
SELECT
poster_id
FROM bb_posts
WHERE post_id = '" . $dislike . "'
)
" );
}
}
elseif($update)
{
/* 1 request as a result */
if( $userdata['user_level'] == ADMIN )
{
$update = ($update) ? $update : $userdata['user_id'];
$sql = "
SELECT l.author_id
FROM " . BB_POSTS_LIKES . " l
LEFT JOIN " . BB_POSTS . " p ON( p.post_id = l.post_id )
LEFT JOIN " . BB_USERS . " u ON( u.user_id = l.author_id )
WHERE p.poster_id = '" . $update . "'
ORDER BY l.vote_time DESC
LIMIT 0, 50
";
$likes_total = DB()->num_rows( DB()->sql_query( $sql ) );
DB()->query( "UPDATE " . BB_USERS . " SET user_sympathies = '" . $likes_total . "' WHERE user_id = '" . $update . "'" );
echo $likes_total;
}
else redirect();
}
elseif($user)
{
/* 2 requests as a result */
$inspected_user = ($user && $userdata['user_level'] == ADMIN) ? $user : $userdata['user_id'];
$tmp = DB()->fetch_row("
SELECT user_sympathies
FROM " . BB_USERS . "
WHERE user_id = '" . $inspected_user . "'
");
$likes_total = ($tmp['user_sympathies']) ? $tmp['user_sympathies'] : '0';
if( $likes_total > 0 )
{
$offset = ($start) ? $start : '0';
$sql = "
SELECT
l.author_id as poster_id, l.post_id, l.vote_time,
p.topic_id,
t.topic_title,
u.username, u.user_rank, u.user_posts, u.avatar_ext_id,
ph.post_html
FROM " . BB_POSTS_LIKES . " l
LEFT JOIN " . BB_POSTS . " p ON( p.post_id = l.post_id )
LEFT JOIN " . BB_POSTS_HTML . " ph ON( ph.post_id = p.post_id )
LEFT JOIN " . BB_TOPICS . " t ON( t.topic_id = p.topic_id )
LEFT JOIN " . BB_USERS . " u ON( u.user_id = l.author_id )
WHERE p.poster_id = '" . $inspected_user . "'
ORDER BY l.vote_time DESC
LIMIT " . $offset . ", " . $bb_cfg['posts_per_page'] . "
";
$ranks = $datastore->get('ranks');
$inc = 0;
foreach( DB()->fetch_rowset($sql) as $syrow )
{
$inc++;
$style_mode = 'row1';
if( $inc%2 == 0 ) $style_mode = 'row2';
$syrow['vote_date'] = bb_date( $syrow['vote_time'], $bb_cfg['post_date_format'] );
$syrow['user_rank_title'] = NULL;
$syrow['user_rank_image'] = NULL;
if( isset($ranks[$syrow['user_rank']]) )
{
$syrow['user_rank_title'] = ($bb_cfg['show_rank_text']) ? $ranks[$syrow['user_rank']]['rank_title'] : '';
$syrow['user_rank_image'] = ($bb_cfg['show_rank_image'] && $ranks[$syrow['user_rank']]['rank_image']) ? '<img src="'. $ranks[$syrow['user_rank']]['rank_image'] .'" alt="" title="" border="0" />' : '';
}
$syrow['user_avatar'] = get_avatar($syrow['poster_id'], $syrow['avatar_ext_id']);
$syrow['username_html'] = profile_url(array('username' => $syrow['username'], 'user_rank' => $syrow['user_rank']));
$template->assign_block_vars('syrow', array(
'POSTER_RANK' => $syrow['user_rank_title'],
'RANK_IMAGE' => $syrow['user_rank_image'],
'POSTER_AVATAR' => $syrow['user_avatar'],
'POSTER_POSTS' => $syrow['user_posts'],
'POSTER_NAME' => $syrow['username_html'],
'POSTER_ID' => $syrow['poster_id'],
'VOTE_DATE' => $syrow['vote_date'],
'MESSAGE' => $syrow['post_html'],
'TOPIC_TITLE' => $syrow['topic_title'],
'POST_ID' => $syrow['post_id'],
'POST_URL' => POST_URL,
'STYLE_MODE' => $style_mode,
'PROFILE_IMG' => ($bb_cfg['text_buttons']) ? $lang['READ_PROFILE_TXTB'] : '<img src="'. $images['icon_profile'] .'" alt="'. $lang['READ_PROFILE_TXTB'] .'" title="'. $lang['READ_PROFILE'] .'" />',
'PM_IMG' => ($bb_cfg['text_buttons']) ? $lang['SEND_PM_TXTB'] : '<img src="'. $images['icon_pm'] .'" alt="'. $lang['SEND_PM_TXTB'] .'" title="'. $lang['SEND_PRIVATE_MESSAGE'] .'" />'
));
}
$template->assign_vars(array(
'PAGINATOR' => generate_pagination('/sympathy.php?user=' . $inspected_user, $likes_total, $bb_cfg['posts_per_page'], $offset )
));
print_page('viewsympathy.tpl');
}
else {
bb_die('Изменений репутации не происходило');
}
}
else redirect();