<?php if (!defined('VB_ENTRY')) die('Access denied.');
/*========================================================================*\
|| ###################################################################### ||
|| # vBulletin 6.2.3 - Licence Number L3138846
|| # ------------------------------------------------------------------ # ||
|| # Copyright 2000-2026 MH Sub I, LLC dba vBulletin. All Rights Reserved.  # ||
|| # This file may not be redistributed in whole or significant part.   # ||
|| # ----------------- VBULLETIN IS NOT FREE SOFTWARE ----------------- # ||
|| # http://www.vbulletin.com | http://www.vbulletin.com/license.html   # ||
|| ###################################################################### ||
\*========================================================================*/

// ######################## SET PHP ENVIRONMENT ###########################
error_reporting(error_reporting() & ~E_NOTICE);

// ########################## REQUIRE BACK-END ############################
require_once(DIR . '/includes/class_paid_subscription.php');

// ########################################################################
// ######################### START MAIN SCRIPT ############################
// ########################################################################
$subobj = new vB_PaidSubscription();
$subobj->cache_user_subscriptions();

if (is_array($subobj->subscriptioncache))
{
	$assertor = vB::getDbAssertor();
	$timenow = vB::getRequest()->getTimeNow();

	$showreminders = [];
	foreach ($subobj->subscriptioncache AS $key => $subscription)
	{
		// disable people
		$subscribers = $assertor->assertQuery('vBForum:subscriptionlog', [
			vB_dB_Query::CONDITIONS_KEY=> [
				['field' => 'subscriptionid', 'value' => $subscription['subscriptionid'], vB_dB_Query::OPERATOR_KEY => vB_dB_Query::OPERATOR_EQ],
				['field' => 'status',         'value' => 1,                               vB_dB_Query::OPERATOR_KEY => vB_dB_Query::OPERATOR_EQ],
				['field' => 'expirydate',     'value' => $timenow,                        vB_dB_Query::OPERATOR_KEY => vB_dB_Query::OPERATOR_LTE],
			]
		]);

		foreach ($subscribers as $subscriber)
		{
			$subobj->delete_user_subscription($subscription['subscriptionid'], $subscriber['userid'], -1, true);
		}

		$newoptions = vb_unserialize($subscription['newoptions']);
		if ($newoptions['reminder'] ?? 1)
		{
			$showreminders[] = $subscription['subscriptionid'];
		}
	}

	$days = subscription_get_days();
	vB_Mail::vbmailStart();
	if (count($showreminders) > 0)
	{
		foreach ($days AS $day)
		{
			// time for the reminders
			$subscriptions_reminders = $assertor->assertQuery('getSubscriptionsReminders', [
				'start' => $timenow + (86400 * $day),
				'end' => $timenow + (86400 * ($day + 1)),
				'subscriptionids' => $showreminders,
			]);

			$string = vB::getString();
			$datastore = vB::getDatastore();
			$bbtitle = $datastore->getOption('bbtitle');
			$bbtitle_escaped = $string->htmlspecialchars($bbtitle);
			$frontendurl = $datastore->getOption('frontendurl');

			$suburl = vB5_Route::buildUrl('settings|fullurl', ['tab' => 'subscriptions']);
			foreach ($subscriptions_reminders AS $subscriptions_reminder)
			{
				$phraseAux = vB_Api::instanceInternal('phrase')->fetch(['sub' . $subscriptions_reminder['subscriptionid'] . '_title', 'subscription']);
				$subscription_title = $phraseAux['sub' . $subscriptions_reminder['subscriptionid'] . '_title'];
				// Not sure about this one. When testing it, if it had HTML, the frontend would render it as HTML.
				// Email would not, because it was text/plain previously. Normally I'd just escape it, but since the frontend renders it w/ HTML,
				// I'm skipping the escape for this...
				// $subscription_title = $string->htmlspecialchars($subscription_title);
				$displayname_safe = $string->htmlspecialchars($subscriptions_reminder['displayname']);

				$maildata = vB_Api::instanceInternal('phrase')->fetchEmailPhrases(
					'paidsubscription_reminder',
					[
						$displayname_safe,
						$subscription_title,
						$suburl,
						$bbtitle_escaped,
						$day,
					],
					[],
					$subscriptions_reminder['languageid']
				);
				// This is autogenerated, but for now not using the unsubscribe email here, because we don't have
				// an email option to turn off for a paid subscription email. We may want to check the mailoption
				// optout here, but payment reminders do not seem like something we want to haphazardly block.
				vB_Mail::create($subscriptions_reminder['email'], $maildata)->send(true);
			}
		}
		vB_Mail::vbmailEnd();
	}
}

function subscription_get_days()
{
	$daystring = vB::getDatastore()->getOption('subscription_expiration_reminder');
	$days = array_map(trim(...), explode(' ', $daystring));
	$days = array_filter($days, function ($day) {return is_numeric($day) AND $day >= 1;});
	$days = array_unique($days);
	return $days;
}

log_cron_action('', $nextitem, 1);

/*=========================================================================*\
|| #######################################################################
|| # Downloaded: 13:27, Sun Aug 30th 2026
|| # CVS: $RCSfile$ - $Revision: 120455 $
|| #######################################################################
\*=========================================================================*/
