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

Paste

Pasted as PHP by registered user atmaca ( 1 month ago )
<?php
// ft_react.php - Tepki ekleme / değiştirme / kaldırma (vB 3.8.x + vBSEO likes tabanlı)
// Geliştirme: atmaca
define('THIS_SCRIPT', 'ft_react');

// JSON bozulmasın diye mümkün olduğunca erken buffer başlat
if (!headers_sent()) {
    @ob_start();
}

require_once('./global.php');
global $vbulletin;

function ft_json_exit($payload)
{
    // Eğer bir şeyler çıktıysa JSON'dan önce temizle
    if (function_exists('ob_get_length') && ob_get_length()) {
        @ob_clean();
    }
    header('Content-Type: application/json; charset=utf-8');
    echo json_encode($payload);
    exit;
}

function ft_to_utf8($str)
{
    if ($str === '' || $str === null) {
        return '';
    }

    if (preg_match('//u', $str)) {
        return $str;
    }

    $tmp = @iconv('ISO-8859-9', 'UTF-8//IGNORE', $str);
    if ($tmp !== false) {
        return $tmp;
    }

    $tmp = @iconv('ISO-8859-1', 'UTF-8//IGNORE', $str);
    if ($tmp !== false) {
        return $tmp;
    }

    return $str;
}

/**
 * Tepki config'ini yükle.
 */
function ft_load_reactions()
{
    $reactions = array();
    $config = dirname(__FILE__) . '/ft_reactions_config.php';

    if (file_exists($config)) {
        $ft_reactions = array();
        include($config);
        if (isset($ft_reactions) && is_array($ft_reactions)) {
            foreach ($ft_reactions as $rid => $info) {
                $rid = intval($rid);
                if ($rid <= 0) { continue; }
                if (!is_array($info)) { continue; }
                if (empty($info['file'])) { continue; }
                $reactions[$rid] = array(
                    'file' => (string)$info['file'],
                    'alt'  => isset($info['alt']) ? (string)$info['alt'] : '',
                );
            }
        }
    }

    // Fallback: config yoksa en azından boş dönme
    if (!$reactions) {
        $reactions = array(
            1  => array('file'=>'like.png','alt'=>'Beğen'),
            2  => array('file'=>'heart.png','alt'=>'Muhteşem'),
            3  => array('file'=>'haha.png','alt'=>'Hahaha'),
            4  => array('file'=>'wow.png','alt'=>'Şaşkın'),
            5  => array('file'=>'sad.png','alt'=>'Üzgün'),
            6  => array('file'=>'sad2.png','alt'=>'Üzüntülü'),
            7  => array('file'=>'angry.png','alt'=>'Kızgın'),
            8  => array('file'=>'care.png','alt'=>'Yanındayım'),
            9  => array('file'=>'dislike.png','alt'=>'Beğenmedim'),
            10 => array('file'=>'cool.png','alt'=>'Havalı'),
            11 => array('file'=>'100.png','alt'=>'Kesinlikle'),
            12 => array('file'=>'thinking.png','alt'=>'Düşünceli'),
            13 => array('file'=>'brokenheart.png','alt'=>'Kalbi Kırılmış'),
            14 => array('file'=>'cursing.png','alt'=>'Çok Tepkili'),
            15 => array('file'=>'explodinghead.png','alt'=>'Beyni Yanmış'),
            16 => array('file'=>'party.png','alt'=>'Parti'),
            17 => array('file'=>'clap.png','alt'=>'Alkış'),
            18 => array('file'=>'hearteyes.png','alt'=>'Kalpli Göz'),
            19 => array('file'=>'smilingfacewithheart.png','alt'=>'Sevgi Dolu'),
            20 => array('file'=>'coldface.png','alt'=>'Üşüyen Yüz'),
        );
    }

    return $reactions;
}

function ft_verify_token($request_token, $vbulletin)
{
    $request_token = trim((string)$request_token);

    if ($request_token === '') {
        return false;
    }

    $raw = '';
    if (!empty($vbulletin->userinfo['securitytoken_raw'])) {
        $raw = $vbulletin->userinfo['securitytoken_raw'];
    } else {

        $raw = (string)$vbulletin->userinfo['securitytoken'];
        if (strpos($raw, '-') !== false) {
            $parts = explode('-', $raw, 2);
            $raw = isset($parts[1]) ? $parts[1] : $raw;
        }
    }

    if (function_exists('verify_security_token') && $raw !== '') {
        return (bool) verify_security_token($request_token, $raw);
    }

    if ($raw !== '' && strpos($request_token, '-') !== false) {
        $parts = explode('-', $request_token, 2);
        $hash  = isset($parts[1]) ? $parts[1] : '';
        return ($hash !== '' && function_exists('hash_equals') ? hash_equals($raw, $hash) : ($raw === $hash));
    }

    return false;
}

function ft_vbseo_like_counter_user($from_userid, $dest_userid, $dn)
{
    global $vbulletin;

    $from_userid = intval($from_userid);
    $dest_userid = intval($dest_userid);
    $dn          = intval($dn);

    if ($from_userid <= 0 || $dest_userid <= 0 || $dn == 0) {
        return;
    }

    if (!class_exists('vBSEO_UI')) {
        $vbseo_ui_file = DIR . '/vbseo/includes/functions_vbseo_ui.php';
        if (file_exists($vbseo_ui_file)) {
            require_once($vbseo_ui_file);
        }
    }

    if (class_exists('vBSEO_UI') && method_exists('vBSEO_UI', 'like_counter_user')) {
        @vBSEO_UI::like_counter_user($from_userid, $dest_userid, $dn);
        return;
    }

    $abs = abs($dn);

    if ($dn > 0) {
        $vbulletin->db->query_write("
            UPDATE " . TABLE_PREFIX . "user
            SET vbseo_likes_out = vbseo_likes_out + $dn
            WHERE userid = $from_userid
        ");
        $vbulletin->db->query_write("
            UPDATE " . TABLE_PREFIX . "user
            SET vbseo_likes_in = vbseo_likes_in + $dn,
                vbseo_likes_unread = vbseo_likes_unread + $dn
            WHERE userid = $dest_userid
        ");
    } else {
        $vbulletin->db->query_write("
            UPDATE " . TABLE_PREFIX . "user
            SET vbseo_likes_out = IF(vbseo_likes_out < $abs, 0, vbseo_likes_out - $abs)
            WHERE userid = $from_userid
        ");
        $vbulletin->db->query_write("
            UPDATE " . TABLE_PREFIX . "user
            SET vbseo_likes_in = IF(vbseo_likes_in < $abs, 0, vbseo_likes_in - $abs)
            WHERE userid = $dest_userid
        ");
    }
} 

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

// -------------------------
// 2) Parametreleri al 
// -------------------------
$vbulletin->input->clean_array_gpc('p', array(
    'type'          => TYPE_STR,   
    'ctype'         => TYPE_STR,   
    'postid'        => TYPE_UINT,  
    'contentid'     => TYPE_UINT,  
    'reactionid'    => TYPE_UINT,
    'securitytoken' => TYPE_STR,
));

$type       = $vbulletin->GPC['type'] ? $vbulletin->GPC['type'] : $vbulletin->GPC['ctype'];
$postid     = intval($vbulletin->GPC['postid'] ? $vbulletin->GPC['postid'] : $vbulletin->GPC['contentid']);
$reactionid = intval($vbulletin->GPC['reactionid']);
$token      = $vbulletin->GPC['securitytoken'];

if ($type !== 'post') {
    ft_json_exit(array('success' => false, 'error' => 'invalid_type'));
}
if (!$postid || !$reactionid) {
    ft_json_exit(array('success' => false, 'error' => 'invalid_params'));
}

// -------------------------
// 3) Token doğrula
// -------------------------
if (!ft_verify_token($token, $vbulletin)) {
    ft_json_exit(array('success' => false, 'error' => 'bad_token'));
}

// -------------------------
// 4) Tepki ID geçerli mi? (config'ten)
// -------------------------
$reactions = ft_load_reactions();
if (!isset($reactions[$reactionid])) {
    ft_json_exit(array('success' => false, 'error' => 'invalid_reaction'));
}

// -------------------------
// 5) Gönderi var mı / sahibi kim?
// -------------------------
$post = $vbulletin->db->query_first("
    SELECT userid
    FROM " . TABLE_PREFIX . "post
    WHERE postid = $postid
    LIMIT 1
");
if (!$post) {
    ft_json_exit(array('success' => false, 'error' => 'invalid_post'));
}

$dest_userid = intval($post['userid']);
if ($dest_userid && $dest_userid == $userid) {
    ft_json_exit(array('success' => false, 'error' => 'own_post'));
}

$now      = TIMENOW;
$username = $vbulletin->userinfo['username'];

// -------------------------
// 6) Kullanıcının mevcut tepkisi var mı?
// -------------------------
$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
    LIMIT 1
");

// Aynı tepkiye tekrar basarsa -> kaldır
if ($existing && intval($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
        LIMIT 1
    ");

    // vBSEO sayaçlarını güncelle
    if ($dest_userid) {
        $dn = 0;
        if (method_exists($vbulletin->db, 'affected_rows')) {
            $dn = intval($vbulletin->db->affected_rows());
        } else {
            $dn = 1;
        }
        if ($dn > 0) {
            ft_vbseo_like_counter_user($userid, $dest_userid, -$dn);
        }
    }

    ft_json_exit(array(
        'success'         => true,
        'removed'         => true,
        'user_reactionid' => 0,
    ));
}

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
        LIMIT 1
    ");

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

    if ($dest_userid) {
        ft_vbseo_like_counter_user($userid, $dest_userid, 1);
    }
}

ft_json_exit(array(
    'success'         => true,
    'removed'         => false,
    'user_reactionid' => $reactionid,
));

 

Revise this Paste

Your Name: Code Language: