<?php
/**
 * FT Story Cleanup (vBulletin Scheduled Task)
 */

if (!defined(&#039;VB_AREA&#039;))
{
	exit;
}

global $vbulletin;

if (!is_object($vbulletin) || !is_object($vbulletin->db))
{
	// cron ortamı hazır değilse sessiz çık
	return;
}

$db  = $vbulletin->db;
$now = TIMENOW;

$limit = 200; // tek çalışmada kaç story temizlensin

$base = @realpath(DIR . &#039;/ft_story_uploads&#039;);
$deletedStories = 0;
$deletedFiles   = 0;

// expire olanları al
$res = $db->query_read("
	SELECT storyid
	FROM ft_story
	WHERE expiretime <= $now
	LIMIT $limit
");

while ($row = $db->fetch_array($res))
{
	$storyid = intval($row[&#039;storyid&#039;]);
	if ($storyid <= 0) { continue; }

	$mres = $db->query_read("
		SELECT filepath, thumbpath
		FROM ft_story_media
		WHERE storyid = $storyid
	");

	$paths = array();
	while ($m = $db->fetch_array($mres))
	{
		if (!empty($m[&#039;filepath&#039;]))  { $paths[] = $m[&#039;filepath&#039;]; }
		if (!empty($m[&#039;thumbpath&#039;])) { $paths[] = $m[&#039;thumbpath&#039;]; }
	}

	foreach ($paths as $rel)
	{
		$rel = ltrim((string)$rel, &#039;/&#039;);

		// sadece bizim klasör
		if (strpos($rel, &#039;ft_story_uploads/&#039;) !== 0)
		{
			continue;
		}

		$abs  = DIR . &#039;/&#039; . $rel;
		$real = @realpath($abs);

		if ($real && $base && strpos($real, $base) === 0 && file_exists($real))
		{
			if (@unlink($real))
			{
				$deletedFiles++;
			}
		}
	}

	// DB temizliği
	$db->query_write("DELETE FROM ft_story_view  WHERE storyid = $storyid");
	$db->query_write("DELETE FROM ft_story_media WHERE storyid = $storyid");
	$db->query_write("DELETE FROM ft_story       WHERE storyid = $storyid LIMIT 1");

	$deletedStories++;
}

if (function_exists(&#039;log_cron_action&#039;))
{
	log_cron_action("FT Story cleanup: deleted_stories=$deletedStories deleted_files=$deletedFiles", 0);
}

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