<?php
// Forumtutkusu mesaj tepki sistemi (ft_react.php)
define('THIS_SCRIPT', 'ft_react');
require_once('./global.php');
header('Content-Type: application/json; charset=utf-8');
global $vbulletin;
// -------------------------
// Giriş kontrolü
// -------------------------
if (empty($vbulletin->userinfo['userid']))
{
echo json_encode(array(
'success' => false,
'error' => 'not_logged_in',
));
exit;
}
// -------------------------
// Parametreleri al
// -------------------------
$vbulletin->input->clean_array_gpc('p', array(
'type' => TYPE_STR, // şimdilik 'post'
'postid' => TYPE_UINT,
'reactionid' => TYPE_INT, // 0 = tepkiyi kaldır
));
$type = $vbulletin->GPC['type'];
$postid = intval($vbulletin->GPC['postid']);
$reactionid = intval($vbulletin->GPC['reactionid']); // 0 olabilir
$userid = intval($vbulletin->userinfo['userid']);
$username = $vbulletin->userinfo['username'];
if (!$postid)
{
echo json_encode(array(
'success' => false,
'error' => 'invalid_post',
));
exit;
}
// İstersen type kontrolünü sıkılaştır:
// if ($type !== 'post') { ... }
if (!$type) { $type = 'post'; }
// -------------------------
// Geçerli reaction listesi
// -------------------------
$valid_reactions = array(1,2,3,4,5,6,7,8,9,10,11,12);
if ($reactionid < 0 || ($reactionid > 0 && !in_array($reactionid, $valid_reactions)))
{
echo json_encode(array(
'success' => false,
'error' => 'invalid_reaction',
));
exit;
}
// -------------------------
// Mesaj sahibini bul
// -------------------------
$post = $vbulletin->db->query_first("
SELECT userid, threadid, visible
FROM " . TABLE_PREFIX . "post
WHERE postid = $postid
LIMIT 1
");
if (!$post)
{
echo json_encode(array(
'success' => false,
'error' => 'post_not_found',
));
exit;
}
// Görünmez (silinmiş/onaysız) mesajlara tepkiyi engelle:
// if (intval($post['visible']) != 1) { ... }
$dest_userid = intval($post['userid']);
$threadid = intval($post['threadid']);
// Kendi mesajına tepki verilmesin
if ($userid && $dest_userid == $userid)
{
echo json_encode(array(
'success' => false,
'error' => 'own_post',
));
exit;
}
$now = TIMENOW;
// Yeni eklemede hangi cgroup yazılsın?
$insert_cgroup = ($threadid > 0) ? $threadid : 0;
// ---------------------------------------------------------
// Bu kullanıcı bu mesaja daha önce tepki vermiş mi?
// (l_cgroup'a bakmadan, en güncel kaydı al)
// ---------------------------------------------------------
$where_userpost = "
l_contentid = $postid
AND l_ctype = 1
AND l_from_userid = $userid
";
$existing = $vbulletin->db->query_first("
SELECT reactionid
FROM vbseo_likes
WHERE $where_userpost
ORDER BY l_dateline DESC
LIMIT 1
");
$existing_reactionid = $existing ? intval($existing['reactionid']) : 0;
$action = 'none'; // add | update | remove | none
$new_reactionid = $existing_reactionid;
// ---------------------------------------------------------
// Veritabanı işlemleri
// Bu kullanıcı+mesaj için ne varsa önce sil, sonra gerekiyorsa tek satır ekle.
// ---------------------------------------------------------
$should_remove = false;
// 0 geldiyse: "kaldır" isteği
if ($reactionid === 0) {
$should_remove = ($existing_reactionid > 0);
} else {
// Aynı tepkiye tekrar basıldıysa: toggle off
$should_remove = ($existing_reactionid > 0 && $existing_reactionid === $reactionid);
}
if ($should_remove) {
// kaldır
$vbulletin->db->query_write("
DELETE FROM vbseo_likes
WHERE $where_userpost
");
$action = 'remove';
$new_reactionid = 0;
}
// add veya update: tek satır insert
} elseif ($reactionid > 0) {
$insert_cgroup = ($threadid > 0) ? $threadid : 0;
$vbulletin->db->query_write("
INSERT INTO vbseo_likes
(l_contentid, l_ctype, l_cgroup,
l_from_userid, l_from_username,
l_dest_userid, l_dateline, reactionid, reaction_type)
VALUES
($postid, 1, $insert_cgroup,
$userid, '" . $vbulletin->db->escape_string($username) . "',
$dest_userid, $now, $reactionid, 'like')
ON DUPLICATE KEY UPDATE
l_cgroup = VALUES(l_cgroup),
l_from_username= VALUES(l_from_username),
l_dest_userid = VALUES(l_dest_userid),
l_dateline = VALUES(l_dateline),
reactionid = VALUES(reactionid),
reaction_type = VALUES(reaction_type)
");
$action = ($existing_reactionid > 0) ? 'update' : 'add';
$new_reactionid = $reactionid;
}
// -------------------------
// Sayaç güncellemeleri (vbseo)
// -------------------------
if ($action === 'add' || $action === 'remove')
{
// Mesaj sahibi (tepki alan kişi)
if ($dest_userid > 0)
{
if ($action === 'add')
{
$vbulletin->db->query_write("
UPDATE " . TABLE_PREFIX . "user
SET vbseo_likes_in = vbseo_likes_in + 1,
vbseo_likes_unread = vbseo_likes_unread + 1
WHERE userid = $dest_userid
");
}
else
{
$vbulletin->db->query_write("
UPDATE " . TABLE_PREFIX . "user
SET vbseo_likes_in = IF(vbseo_likes_in > 0, vbseo_likes_in - 1, 0),
vbseo_likes_unread = IF(vbseo_likes_unread > 0, vbseo_likes_unread - 1, 0)
WHERE userid = $dest_userid
");
}
}
// Tepki veren kişi
if ($userid > 0)
{
if ($action === 'add')
{
$vbulletin->db->query_write("
UPDATE " . TABLE_PREFIX . "user
SET vbseo_likes_out = vbseo_likes_out + 1
WHERE userid = $userid
");
}
else
{
$vbulletin->db->query_write("
UPDATE " . TABLE_PREFIX . "user
SET vbseo_likes_out = IF(vbseo_likes_out > 0, vbseo_likes_out - 1, 0)
WHERE userid = $userid
");
}
}
}
// -------------------------
// JSON yanıtı
// -------------------------
echo json_encode(array(
'success' => true,
'removed' => ($new_reactionid === 0 ? true : false),
'user_reactionid' => intval($new_reactionid),
// Debug istersen aç:
// 'action' => $action,
// 'threadid' => $threadid,
// 'insert_cgroup'=> $insert_cgroup,
));
exit;Add a code snippet to your website: www.paste.org