Welcome, guest! Login / Register - Why register?
Psst.. new poll here.
Psst.. new forums here.

Paste

Pasted as PHP by registered user atmaca ( 3 months ago )
<?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;
}

// İstersen görünmez (silinmiş/onaysız) postlara 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;

// -------------------------
// 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['reactionid']) : 0;
$action         = 'none';  // 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         = 'remove';
        $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         = 'remove';
        $new_reactionid = 0;
    }
    elseif ($existing)
    {
        // Var olan tepki => güncelle
        // (İstersen l_cgroup'u da normalize edebilirsin)
        $vbulletin->db->query_write("
            UPDATE vbseo_likes
            SET reactionid   = $reactionid,
                l_dateline   = $now,
                l_cgroup     = $insert_cgroup,
                reaction_type = 'like'
            WHERE l_contentid   = $postid
              AND l_ctype       = 1
              $cgroup_sql
              AND l_from_userid = $userid
        ");
        $action         = 'update';
        $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, '" . $vbulletin->db->escape_string($username) . "',
                 $dest_userid, $now, $reactionid, 'like')
        ");
        $action         = 'add';
        $new_reactionid = $reactionid;
    }
}

// -------------------------
// Sayaç güncellemeleri (vbseo)
// add/remove'da 1 art/azalır.
// update'de değişmez (hala 1 tepki var).
// -------------------------
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 atan 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;

 

Revise this Paste

Children: 129835
Your Name: Code Language: