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
define('THIS_SCRIPT', 'ft_story_json');
require_once('./global.php');

/* DB handle garanti */
if (!isset($db) || !is_object($db))
{
	$db =& $vbulletin->db;
}

/* JSON helper (charset sorunlarına karşı) */
function ft_utf8ize($mixed, $fromCharset)
{
	if (is_array($mixed))
	{
		foreach ($mixed as $k => $v)
		{
			$mixed[$k] = ft_utf8ize($v, $fromCharset);
		}
		return $mixed;
	}

	if (is_string($mixed))
	{
		if (!strcasecmp($fromCharset, 'utf-8'))
		{
			return $mixed;
		}
		if (function_exists('mb_convert_encoding'))
		{
			return @mb_convert_encoding($mixed, 'UTF-8', $fromCharset);
		}

		return utf8_encode($mixed);
	}

	return $mixed;
}

function ft_json_exit($arr)
{
	global $stylevar;

	$fromCharset = 'UTF-8';
	if (!empty($stylevar['charset']))
	{
		$fromCharset = $stylevar['charset'];
	}

	$json = json_encode($arr, JSON_UNESCAPED_UNICODE);
	if ($json === false)
	{
		$arr  = ft_utf8ize($arr, $fromCharset);
		$json = json_encode($arr, JSON_UNESCAPED_UNICODE);
	}

	if (function_exists('ob_get_length') && ob_get_length())
	{
		@ob_clean();
	}

	header('Content-Type: application/json; charset=UTF-8');
	header('Cache-Control: no-store, no-cache, must-revalidate, max-age=0');
	header('Pragma: no-cache');

	if ($json === false)
	{
		echo json_encode(array(
			'ok'    => 0,
			'error' => 'json_encode_failed',
			'code'  => json_last_error()
		));
		exit;
	}

	echo $json;
	exit;
}

/*
  Visibility:
  1 = Forum Üyeleri
  3 = Sadece Arkadaşlar
*/

function ft_story_get_buddy_ids($userid)
{
	global $db;

	static $cache = array();

	$userid = intval($userid);
	if ($userid < 1)
	{
		return array();
	}
	if (isset($cache[$userid]))
	{
		return $cache[$userid];
	}

	$row = $db->query_first("
		SELECT buddylist
		FROM usertextfield
		WHERE userid = $userid
	");

	$list = array();
	if (!empty($row['buddylist']))
	{
		$parts = preg_split('/[\s,]+/', trim($row['buddylist']));
		if (is_array($parts))
		{
			foreach ($parts as $id)
			{
				$id = intval($id);
				if ($id > 0)
				{
					$list[$id] = 1;
				}
			}
		}
	}

	$cache[$userid] = $list;
	return $list;
}

function ft_is_on_buddy_list($userid, $buddyid)
{
	$userid  = intval($userid);
	$buddyid = intval($buddyid);
	if ($userid < 1 || $buddyid < 1)
	{
		return false;
	}

	$list = ft_story_get_buddy_ids($userid);
	return !empty($list[$buddyid]);
}

function ft_are_mutual_friends($userA, $userB)
{
	$userA = intval($userA);
	$userB = intval($userB);

	if ($userA < 1 || $userB < 1 || $userA == $userB)
	{
		return false;
	}

	return (ft_is_on_buddy_list($userA, $userB) && ft_is_on_buddy_list($userB, $userA));
}

function ft_can_view_story_visibility($ownerid, $viewerid, $visibility)
{
	$ownerid    = intval($ownerid);
	$viewerid   = intval($viewerid);
	$visibility = intval($visibility);

	// Misafir görmesin
	if ($viewerid < 1)
	{
		return false;
	}

	// Sahibi her zaman görsün
	if ($ownerid == $viewerid)
	{
		return true;
	}

	if ($visibility == 1)
	{
		return true;
	}
	if ($visibility == 3)
	{
		return ft_are_mutual_friends($ownerid, $viewerid);
	}

	return false;
}

/* YouTube: filepath "yt:VIDEOID" -> VIDEOID */
function ft_extract_youtube_id($filepath)
{
	$filepath = trim((string)$filepath);
	if (strpos($filepath, 'yt:') !== 0) return '';

	$id = trim(substr($filepath, 3));
	return (preg_match('~^[A-Za-z0-9_-]{11}$~', $id) ? $id : '');
}


$vbulletin->input->clean_array_gpc('g', array(
	'u' => TYPE_UINT,
));

$target_userid = intval($vbulletin->GPC['u']);
if ($target_userid <= 0)
{
	ft_json_exit(array('ok' => 0, 'error' => 'invalid_user'));
}

$viewerid = intval($vbulletin->userinfo['userid']);
if ($viewerid < 1)
{
	ft_json_exit(array('ok' => 0, 'error' => 'not_logged_in'));
}

$now = TIMENOW;

$stories  = array();
$username = '';
$avatar   = '';

// JSON endpoint için utf8mb4 bağlantı
$db->query_write("SET NAMES utf8mb4");

// (isteğe bağlı ama iyi) doğru header
header('Content-Type: application/json; charset=UTF-8');

$res = $db->query_read("
  SELECT
    s.storyid, s.userid, s.visibility, s.dateline,
    m.filepath,
    m.mediatype,
    u.username, u.avatarrevision, u.avatarid,
    a.avatarpath,
    t.text_body AS ft_text_body,
    t.bg_id     AS ft_bg_id
  FROM ft_story AS s
  INNER JOIN user AS u ON (u.userid = s.userid)
  INNER JOIN ft_story_media AS m ON (m.storyid = s.storyid)
  LEFT JOIN avatar AS a ON (a.avatarid = u.avatarid)
  LEFT JOIN ft_story_text AS t ON (t.storyid = s.storyid)
  WHERE s.userid = $target_userid
    AND s.state = 1
    AND s.expiretime > $now
  ORDER BY s.dateline ASC
");

while ($r = $db->fetch_array($res))
{
	if (!ft_can_view_story_visibility($r['userid'], $viewerid, $r['visibility']))
	{
		continue;
	}

	$username = $r['username'];

	// Avatar 1 kere set
	if (!$avatar)
	{
		if (!empty($r['avatarpath']))
		{
			$avatar = $r['avatarpath'];
		}
		else
		{
			$avatar = 'image.php?u=' . $target_userid . '&dateline=' . intval($r['avatarrevision']);
		}
	}

	$mediatype = intval($r['mediatype']);
	$filepath  = (string)$r['filepath'];

	$item = array(
		'storyid'    => intval($r['storyid']),
		'dateline'   => intval($r['dateline']),
		'visibility' => intval($r['visibility']),
		'mediatype'  => $mediatype, // 1=image, 2=video, 4=youtube
		'url'        => $filepath,
		'ownerid'    => intval($r['userid']),
	);

	// YouTube (mediatype=4)
	if ($mediatype === 4)
	{
		$ytid = ft_extract_youtube_id($filepath);
		if ($ytid === '')
		{
			// bozuk kayıt: atla
			continue;
		}
		$item['youtube_id'] = $ytid;
	}

	// Görsel/Video için boş filepath gelirse atla
	if (($mediatype === 1 || $mediatype === 2) && trim($filepath) === '')
	{
		continue;
	}
// Text story (mediatype=3)
if ($mediatype === 3)
{
  $item['text_body'] = (string)$r['ft_text_body'];
  $item['bg_id']     = intval($r['ft_bg_id']);

  // metin boşsa hiç gönderme (boş siyah ekran olmasın)
  if (trim($item['text_body']) === '')
  {
    continue;
  }
}
	$stories[] = $item;
}

if (empty($stories))
{
	ft_json_exit(array('ok' => 0, 'error' => 'no_visible_stories'));
}

ft_json_exit(array(
	'ok'       => 1,
	'userid'   => $target_userid,
	'ownerid'  => $target_userid,
	'username' => $username,
	'avatar'   => $avatar,
	'stories'  => $stories
));

 

Revise this Paste

Your Name: Code Language: