<?php
// Forumtutkusu mesaj tepki sistemi (ft_react.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;);

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_INT,   // 0 = tepkiyi kaldır
));

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

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

// İstersen type kontrolünü sıkılaştır:
// if ($type !== &#039;post&#039;) { ... }
if (!$type) { $type = &#039;post&#039;; }

// -------------------------
// 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(
        &#039;success&#039; => false,
        &#039;error&#039;   => &#039;invalid_reaction&#039;,
    ));
    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(
        &#039;success&#039; => false,
        &#039;error&#039;   => &#039;post_not_found&#039;,
    ));
    exit;
}

// İstersen görünmez (silinmiş/onaysız) postlara tepkiyi engelle:
// if (intval($post[&#039;visible&#039;]) != 1) { ... }

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

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

$now = TIMENOW;

// -------------------------
// Eski vbseo: l_cgroup = threadid
// Yeni sistem: l_cgroup = 0
// İkisini de gör:
// -------------------------
$cgroup_sql = ($threadid > 0)
    ? " AND l_cgroup IN (0,$threadid) "
    : " AND l_cgroup = 0 ";

// Yeni eklemede hangi cgroup yazılsın?
// (Tavsiye: threadid yazmak eski kayıtlarla aynı format)
$insert_cgroup = ($threadid > 0) ? $threadid : 0;

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

$existing_reactionid = $existing ? intval($existing[&#039;reactionid&#039;]) : 0;
$action         = &#039;none&#039;;  // add | update | remove | none
$new_reactionid = $existing_reactionid;

// -------------------------
// Veritabanı işlemleri
// -------------------------
if ($reactionid === 0)
{
    // İstenerek "kaldır" gönderildiyse
    if ($existing)
    {
        $vbulletin->db->query_write("
            DELETE FROM vbseo_likes
            WHERE l_contentid   = $postid
              AND l_ctype       = 1
              $cgroup_sql
              AND l_from_userid = $userid
        ");
        $action         = &#039;remove&#039;;
        $new_reactionid = 0;
    }
}
else
{
    if ($existing && $existing_reactionid === $reactionid)
    {
        // Aynı tepkiye tekrar basıldı => kaldır
        $vbulletin->db->query_write("
            DELETE FROM vbseo_likes
            WHERE l_contentid   = $postid
              AND l_ctype       = 1
              $cgroup_sql
              AND l_from_userid = $userid
        ");
        $action         = &#039;remove&#039;;
        $new_reactionid = 0;
    }
    elseif ($existing)
    {
        // Var olan tepki => güncelle
        // (İstersen l_cgroup&#039;u da normalize edebilirsin)
        $vbulletin->db->query_write("
            UPDATE vbseo_likes
            SET reactionid   = $reactionid,
                l_dateline   = $now,
                l_cgroup     = $insert_cgroup,
                reaction_type = &#039;like&#039;
            WHERE l_contentid   = $postid
              AND l_ctype       = 1
              $cgroup_sql
              AND l_from_userid = $userid
        ");
        $action         = &#039;update&#039;;
        $new_reactionid = $reactionid;
    }
    else
    {
        // İlk kez 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, reaction_type)
            VALUES
                ($postid, 1, $insert_cgroup,
                 $userid, &#039;" . $vbulletin->db->escape_string($username) . "&#039;,
                 $dest_userid, $now, $reactionid, &#039;like&#039;)
        ");
        $action         = &#039;add&#039;;
        $new_reactionid = $reactionid;
    }
}

// -------------------------
// Sayaç güncellemeleri (vbseo)
// add/remove&#039;da 1 art/azalır.
// update&#039;de değişmez (hala 1 tepki var).
// -------------------------
if ($action === &#039;add&#039; || $action === &#039;remove&#039;)
{
    // Mesaj sahibi (tepki alan kişi)
    if ($dest_userid > 0)
    {
        if ($action === &#039;add&#039;)
        {
            $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 atan kişi
    if ($userid > 0)
    {
        if ($action === &#039;add&#039;)
        {
            $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(
    &#039;success&#039;         => true,
    &#039;removed&#039;         => ($new_reactionid === 0 ? true : false),
    &#039;user_reactionid&#039; => intval($new_reactionid),

    // Debug istersen aç:
    // &#039;action&#039;       => $action,
    // &#039;threadid&#039;     => $threadid,
    // &#039;insert_cgroup&#039;=> $insert_cgroup,
));
exit;

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