<?php
// ft_reaction_modal.php
define(&#039;THIS_SCRIPT&#039;, &#039;ft_reaction_modal&#039;);
require(&#039;./global.php&#039;);

global $vbulletin;

// Avatar fonksiyonu
require_once(DIR . &#039;/includes/functions_user.php&#039;);

// Çıkış: JSON + UTF-8
header(&#039;Content-Type: application/json; charset=UTF-8&#039;);

/**
 * Verilen metni forum charset&#039;inden UTF-8&#039;e çevirir.
 * (TR genelde ISO-8859-9, EN ISO-8859-1 vb.)
 */
function ft_to_utf8($str)
{
    if ($str === &#039;&#039; || $str === null) {
        return &#039;&#039;;
    }

    // Zaten geçerli UTF-8 ise hiç dokunma
    if (preg_match(&#039;//u&#039;, $str)) {
        return $str;
    }

    // Önce Türkçe paket (ISO-8859-9) olarak dene
    $tmp = @iconv(&#039;ISO-8859-9&#039;, &#039;UTF-8//IGNORE&#039;, $str);
    if ($tmp !== false) {
        return $tmp;
    }

    // Olmazsa İngilizce paket (ISO-8859-1) olarak dene
    $tmp = @iconv(&#039;ISO-8859-1&#039;, &#039;UTF-8//IGNORE&#039;, $str);
    if ($tmp !== false) {
        return $tmp;
    }

    // Hiçbiri olmadıysa, gelen haliyle bırak
    return $str;
}

// ---------------------------
// Reaksiyon meta (ikon + etiket)
// (Etiketler forum charset&#039;inde)
// ---------------------------
$reaction_info = array(
    1 => array(&#039;icon&#039; => &#039;like.png&#039;,  &#039;label&#039; => &#039;Beğen&#039;),
    2 => array(&#039;icon&#039; => &#039;heart.png&#039;, &#039;label&#039; => &#039;Muhteşem&#039;),
    3 => array(&#039;icon&#039; => &#039;haha.png&#039;,  &#039;label&#039; => &#039;Hahaha&#039;),
    4 => array(&#039;icon&#039; => &#039;wow.png&#039;,   &#039;label&#039; => &#039;Şaşkın&#039;),
    5 => array(&#039;icon&#039; => &#039;sad.png&#039;,   &#039;label&#039; => &#039;Üzgün&#039;),
    6 => array(&#039;icon&#039; => &#039;angry.png&#039;, &#039;label&#039; => &#039;Kızgın&#039;),
    7 => array(&#039;icon&#039; => &#039;care.png&#039;,  &#039;label&#039; => &#039;Yanındayım&#039;),
);

$where_rid = &#039;&#039;;
if ($filter_rid > 0)
{
    $where_rid = &#039; AND vl.reactionid = &#039; . intval($filter_rid);
}

// ---------------------------
// Veritabanından tepkileri çek
// ---------------------------
$result = $vbulletin->db->query_read("
    SELECT vl.reactionid,
           vl.l_dateline,
           u.userid,
           u.username,
           u.posts
    FROM vbseo_likes AS vl
    LEFT JOIN user AS u
        ON (u.userid = vl.l_from_userid)
    WHERE vl.l_contentid = $postid
          $where_rid
    ORDER BY vl.reactionid, vl.l_dateline DESC
");

$groups = array();

while ($row = $vbulletin->db->fetch_array($result))
{
    $rid = intval($row[&#039;reactionid&#039;]);
    if (!$rid || !isset($reaction_info[$rid])) {
        continue;
    }

    if (!isset($groups[$rid])) {
        $groups[$rid] = array(
            &#039;reactionid&#039; => $rid,
            &#039;icon&#039;       => $reaction_info[$rid][&#039;icon&#039;],
            &#039;label&#039;      => $reaction_info[$rid][&#039;label&#039;], // forum charset
            &#039;count&#039;      => 0,
            &#039;users&#039;      => array(),
        );
    }

    $timestamp = intval($row[&#039;l_dateline&#039;]);

    // vBulletin tarih formatı
    if (function_exists(&#039;vbdate&#039;)) {
        $fmt     = $vbulletin->options[&#039;dateformat&#039;] . &#039; &#039; . $vbulletin->options[&#039;timeformat&#039;];
        $datestr = vbdate($fmt, $timestamp, true);
    } else {
        $datestr = date(&#039;d.m.Y H:i&#039;, $timestamp);
    }

    // Avatar URL
    $avatarurl = &#039;&#039;;
    if (!empty($row[&#039;userid&#039;]) && function_exists(&#039;fetch_avatar_url&#039;)) {
        $avatarinfo = fetch_avatar_url($row[&#039;userid&#039;]);
        // vB3: genelde [0] => url, [1] => boyut vb.
        if (is_array($avatarinfo) && !empty($avatarinfo[0])) {
            $avatarurl = $avatarinfo[0];
        }
    }

    $groups[$rid][&#039;users&#039;][] = array(
        &#039;userid&#039;   => intval($row[&#039;userid&#039;]),
        &#039;username&#039; => $row[&#039;username&#039;],   // forum charset
        &#039;posts&#039;    => intval($row[&#039;posts&#039;]),
        &#039;avatar&#039;   => $avatarurl,         // URL (ASCII)
        &#039;date&#039;     => $datestr,           // forum charset
        &#039;rawdate&#039;  => $timestamp,
    );

    $groups[$rid][&#039;count&#039;]++;
}

$vbulletin->db->free_result($result);

// ---------------------------
// Çıkışı UTF-8&#039;e çevirip JSON üret
// ---------------------------
$out_groups = array();

foreach ($groups as $rid => $g)
{
    $out_group = array(
        &#039;reactionid&#039; => $g[&#039;reactionid&#039;],
        &#039;icon&#039;       => $g[&#039;icon&#039;],
        &#039;label&#039;      => ft_to_utf8($g[&#039;label&#039;]),
        &#039;count&#039;      => $g[&#039;count&#039;],
        &#039;users&#039;      => array(),
    );

    foreach ($g[&#039;users&#039;] as $u)
    {
        $out_group[&#039;users&#039;][] = array(
            &#039;userid&#039;   => $u[&#039;userid&#039;],
            &#039;username&#039; => ft_to_utf8($u[&#039;username&#039;]),
            &#039;posts&#039;    => $u[&#039;posts&#039;],
            &#039;avatar&#039;   => $u[&#039;avatar&#039;],          // URL UTF-8’e çevirmeye gerek yok ama zararı da yok
            &#039;date&#039;     => ft_to_utf8($u[&#039;date&#039;]),
            &#039;rawdate&#039;  => $u[&#039;rawdate&#039;],
        );
    }

    $out_groups[] = $out_group;
}

// Başlık
$title = &#039;İfade Bırakanlar #&#039; . $postid;

//JSON Çıktısı
echo json_encode(array(
    &#039;title&#039;  => $title,
    &#039;groups&#039; => $out_groups,
));
exit;

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