<?php

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;);

// Kullanıcı girişi zorunlu
if (empty($vbulletin->userinfo[&#039;userid&#039;]))
{
    echo json_encode([
        &#039;success&#039; => false,
        &#039;error&#039;   => &#039;login_required&#039;
    ]);
    exit;
}

$userid   = intval($vbulletin->userinfo[&#039;userid&#039;]);
$username = $vbulletin->db->escape_string($vbulletin->userinfo[&#039;username&#039;]);

$postid   = isset($_GET[&#039;postid&#039;])   ? intval($_GET[&#039;postid&#039;])   : 0;
$reaction = isset($_GET[&#039;reaction&#039;]) ? intval($_GET[&#039;reaction&#039;]) : 0;

// Güvenlik: postid / reaction kontrolü
if ($postid <= 0 || $reaction <= 0)
{
    echo json_encode([
        &#039;success&#039; => false,
        &#039;error&#039;   => &#039;invalid_params&#039;
    ]);
    exit;
}

// Şimdilik bütün tepki tipleri için tek type kullanıyoruz
$reaction_type = 1; // "reaction" gibi düşünebilirsin

// Bu kullanıcı bu mesaja daha önce tepki vermiş mi?
$existing = $vbulletin->db->query_first("
    SELECT reactionid
    FROM vbseo_likes
    WHERE l_contentid   = $postid
      AND l_ctype       = 1
      AND l_cgroup      = 0
      AND l_from_userid = $userid
");

// Aynı tepkiye tekrar bastıysa → kaydı sil (toggle off)
if ($existing && intval($existing[&#039;reactionid&#039;]) === $reaction)
{
    $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([
        &#039;success&#039;  => true,
        &#039;reaction&#039; => $reaction,
        &#039;removed&#039;  => true
    ]);
    exit;
}

// Farklı bir tepkiye geçtiyse → tek satırı güncelle
if ($existing)
{
    $vbulletin->db->query_write("
        UPDATE vbseo_likes
        SET reactionid    = $reaction,
            reaction_type = $reaction_type,
            l_dateline    = " . TIMENOW . "
        WHERE l_contentid   = $postid
          AND l_ctype       = 1
          AND l_cgroup      = 0
          AND l_from_userid = $userid
    ");
}
else
{
    // İlk kez tepki veriyorsa → yeni satır
    $dest = $vbulletin->db->query_first("
        SELECT userid
        FROM " . TABLE_PREFIX . "post
        WHERE postid = $postid
        LIMIT 1
    ");

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

    $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, reaction_type, reactionid)
        VALUES
            ($postid, 1, 0, $userid, &#039;$username&#039;,
             $dest_userid, " . TIMENOW . ", $reaction_type, $reaction)
    ");
}

echo json_encode([
    &#039;success&#039;  => true,
    &#039;reaction&#039; => $reaction,
    &#039;removed&#039;  => false
]);
exit;

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