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
// ft_react.php - Tepki ekleme / değiştirme / kaldırma
// + vBSEO Like bildirim sayaç entegrasyonu

define('THIS_SCRIPT', 'ft_react');
require_once('./global.php');

header('Content-Type: application/json; charset=utf-8');

global $vbulletin;

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

// -------------------------
// 2) 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;
}

// 0 dışındaki bütün değerler geçerli tepki ID'lerinden biri olmalı
$valid_reactions = array(1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20);
if ($reactionid < 0 || ($reactionid > 0 && !in_array($reactionid, $valid_reactions)))
{
    echo json_encode(array(
        'success' => false,
        'error'   => 'invalid_reaction',
    ));
    exit;
}

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

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

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

// -------------------------
// 4) Bu kullanıcı bu posta
//    daha önce tepki vermiş mi?
//    (bizim alanımız: l_ctype=1, l_cgroup=0)
// -------------------------
$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
");

$existing_reactionid = $existing ? intval($existing['reactionid']) : 0;

// Bildirim sayaçları için ne yaptığımızı takip edelim
$action         = 'none';  // add | update | remove | none
$new_reactionid = $existing_reactionid;

// -------------------------
// 5) Veritabanı işlemleri
// -------------------------

// reactionid = 0 → tepkiyi tamamen kaldır
if ($reactionid === 0)
{
    if ($existing)
    {
        $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
        ");

        $action         = 'remove';
        $new_reactionid = 0;
    }
}
else
{
    // Aynı tepkiye tekrar bastı → toggle off (kaldır)
    if ($existing && $existing_reactionid === $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
        ");

        $action         = 'remove';
        $new_reactionid = 0;
    }
    // Farklı tepkiye geçti → UPDATE
    elseif ($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
        ");

        $action         = 'update';
        $new_reactionid = $reactionid;
    }
    // Hiç tepki yoktu → yeni satır INSERT
    else
    {
        $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, '" . $vbulletin->db->escape_string($username) . "',
                 $dest_userid, $now, $reactionid)
        ");

        $action         = 'add';
        $new_reactionid = $reactionid;
    }
}

// -------------------------
// 6) vBSEO beğeni sayaçları
//    (bildirimler için önemli olan kısım)
// -------------------------
//
// Burada yalnızca bizim tepki sistemimizin tetiklediği olaylar için
// user tablosundaki:
//   vbseo_likes_in, vbseo_likes_out, vbseo_likes_unread
// alanlarını oynatıyoruz.
//
// Eski / klasik vBSEO beğenileri farklı l_ctype / l_cgroup
// kullanıyorsa onlar etkilenmez.

if ($action === 'add' || $action === 'remove')
{
    // Mesaj sahibi (beğeni alan kişi)
    if ($dest_userid > 0)
    {
        if ($action === 'add')
        {
            // Yeni tepki = yeni beğeni gibi → içeri +1, okunmamış +1
            $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 // remove
        {
            // Tepki kaldırıldı → sayaçlardan düş (0 altına inmesin)
            $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 bırakan kullanıcı (beğeni 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 // remove
        {
            $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
            ");
        }
    }
}

// -------------------------
// 7) JSON cevap
// -------------------------
echo json_encode(array(
    'success'         => true,
    'removed'         => ($new_reactionid === 0 ? true : false),
    'user_reactionid' => intval($new_reactionid),
));
exit;

 

Revise this Paste

Children: 130365
Your Name: Code Language: