<?php
// ft_react.php - Tepki ekleme / değiştirme / kaldırma

define(&#039;THIS_SCRIPT&#039;, &#039;ft_react&#039;);
require_once(&#039;./global.php&#039;);

header(&#039;Content-Type: application/json; charset=utf-8&#039;);

global $vbulletin;

// Giriş kontrolü
if (empty($vbulletin->userinfo[&#039;userid&#039;]))
{
    echo json_encode(array(
        &#039;success&#039; => false,
        &#039;error&#039;   => &#039;not_logged_in&#039;,
    ));
    exit;
}

// Parametreleri al
$vbulletin->input->clean_array_gpc(&#039;p&#039;, array(
    &#039;type&#039;       => TYPE_STR,   // şimdilik &#039;post&#039;
    &#039;postid&#039;     => TYPE_UINT,
    &#039;reactionid&#039; => TYPE_UINT,
));

$type       = $vbulletin->GPC[&#039;type&#039;];
$postid     = intval($vbulletin->GPC[&#039;postid&#039;]);
$reactionid = intval($vbulletin->GPC[&#039;reactionid&#039;]);
$userid     = intval($vbulletin->userinfo[&#039;userid&#039;]);
$username   = $vbulletin->userinfo[&#039;username&#039;];

if (!$postid || !$reactionid)
{
    echo json_encode(array(
        &#039;success&#039; => false,
        &#039;error&#039;   => &#039;invalid_params&#039;,
    ));
    exit;
}

// Geçerli tepki ID&#039;leri
$valid_reactions = array(1,2,3,4,5,6,7);
if (!in_array($reactionid, $valid_reactions))
{
    echo json_encode(array(
        &#039;success&#039; => false,
        &#039;error&#039;   => &#039;invalid_reaction&#039;,
    ));
    exit;
}

// Mesaj sahibini bul
$post = $vbulletin->db->query_first("
    SELECT userid
    FROM " . TABLE_PREFIX . "post
    WHERE postid = $postid
    LIMIT 1
");

if (!$post)
{
    echo json_encode(array(
        &#039;success&#039; => false,
        &#039;error&#039;   => &#039;post_not_found&#039;,
    ));
    exit;
}

$dest_userid = intval($post[&#039;userid&#039;]);

// Kullanıcı kendi mesajına tepki veremesin
if ($userid && $dest_userid == $userid)
{
    echo json_encode(array(
        &#039;success&#039; => false,
        &#039;error&#039;   => &#039;own_post&#039;,
    ));
    exit;
}

$now = TIMENOW;

// Bu kullanıcı bu posta daha önce tepki bırakmış mı?
$existing = $vbulletin->db->query_first("
    SELECT *
    FROM vbseo_likes
    WHERE l_contentid   = $postid
      AND l_ctype       = 1
      AND l_cgroup      = 0
      AND l_from_userid = $userid
    LIMIT 1
");

// Aynı tepkiye tekrar basarsa → kaydı sil (toggle off)
if ($existing && intval($existing[&#039;reactionid&#039;]) == $reactionid)
{
    $vbulletin->db->query_write("
        DELETE FROM vbseo_likes
        WHERE l_contentid   = $postid
          AND l_ctype       = 1
          AND l_cgroup      = 0
          AND l_from_userid = $userid
    ");

    echo json_encode(array(
        &#039;success&#039;         => true,
        &#039;removed&#039;         => true,
        &#039;user_reactionid&#039; => 0,
    ));
    exit;
}

// $new_reactionid: Kullanıcının o post için SON durumu
//   - 0      => tepki kaldırıldı
//   - 1..7   => seçilen reactionid

header(&#039;Content-Type: application/json; charset=UTF-8&#039;);
echo json_encode(array(
    &#039;success&#039;          => true,
    &#039;user_reactionid&#039;  => intval($new_reactionid),
));
exit;

// Kayıt var ama reaction farklıysa → UPDATE
if ($existing)
{
    $vbulletin->db->query_write("
        UPDATE vbseo_likes
        SET reactionid = $reactionid,
            l_dateline = $now
        WHERE l_contentid   = $postid
          AND l_ctype       = 1
          AND l_cgroup      = 0
          AND l_from_userid = $userid
    ");
}
else
{
    // Yeni tepki
    $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)
        VALUES
            ($postid, 1, 0,
             $userid, &#039;" . $vbulletin->db->escape_string($username) . "&#039;,
             $dest_userid, $now, $reactionid)
    ");
}

// Burada ileride vBSEO bildirim / sayaç entegrasyonu da yapabiliriz

echo json_encode(array(
    &#039;success&#039;         => true,
    &#039;removed&#039;         => false,
    &#039;user_reactionid&#039; => $reactionid,
));
exit;

Add a code snippet to your website: www.paste.org