![]() Server : Apache/2 System : Linux server-15-235-50-60 5.15.0-164-generic #174-Ubuntu SMP Fri Nov 14 20:25:16 UTC 2025 x86_64 User : gositeme ( 1004) PHP Version : 8.2.29 Disable Function : exec,system,passthru,shell_exec,proc_close,proc_open,dl,popen,show_source,posix_kill,posix_mkfifo,posix_getpwuid,posix_setpgid,posix_setsid,posix_setuid,posix_setgid,posix_seteuid,posix_setegid,posix_uname Directory : /home/gositeme/.cursor-server/data/User/History/33ee19dd/ |
<?php
/**
* Subscription Helper Functions
* Functions to check subscription status and monthly track limits
*/
require_once __DIR__ . '/../config/database.php';
/**
* Check if user has active subscription
*/
function hasActiveSubscription($user_id) {
$pdo = getDBConnection();
$stmt = $pdo->prepare("
SELECT id, plan_name, status, current_period_end
FROM user_subscriptions
WHERE user_id = ?
AND status IN ('active', 'trialing')
AND current_period_end > NOW()
ORDER BY created_at DESC
LIMIT 1
");
$stmt->execute([$user_id]);
return $stmt->fetch(PDO::FETCH_ASSOC);
}
/**
* Get or create monthly track usage record
*/
function getMonthlyTrackUsage($user_id, $plan_name = null) {
$pdo = getDBConnection();
$year_month = date('Y-m');
// If plan_name not provided, get it from active subscription
if (!$plan_name) {
$subscription = hasActiveSubscription($user_id);
if ($subscription) {
$plan_name = $subscription['plan_name'];
}
}
// Get or create usage record
$stmt = $pdo->prepare("
SELECT * FROM monthly_track_usage
WHERE user_id = ? AND year_month = ?
");
$stmt->execute([$user_id, $year_month]);
$usage = $stmt->fetch(PDO::FETCH_ASSOC);
if (!$usage) {
// Determine track limit based on plan
$track_limit = 0;
$plans_config = require __DIR__ . '/../config/subscription_plans.php';
if (isset($plans_config[$plan_name])) {
$track_limit = $plans_config[$plan_name]['tracks_per_month'];
}
// Create new record
$stmt = $pdo->prepare("
INSERT INTO monthly_track_usage (user_id, year_month, tracks_created, track_limit, reset_at)
VALUES (?, ?, 0, ?, NOW())
");
$stmt->execute([$user_id, $year_month, $track_limit]);
// Get the created record
$stmt = $pdo->prepare("
SELECT * FROM monthly_track_usage
WHERE user_id = ? AND year_month = ?
");
$stmt->execute([$user_id, $year_month]);
$usage = $stmt->fetch(PDO::FETCH_ASSOC);
}
return $usage;
}
/**
* Check if user can create a track (monthly limit check)
* Returns array with 'allowed' => true/false and 'message' => string
*/
function canCreateTrack($user_id) {
$pdo = getDBConnection();
// Get user info
$stmt = $pdo->prepare("SELECT plan FROM users WHERE id = ?");
$stmt->execute([$user_id]);
$user = $stmt->fetch(PDO::FETCH_ASSOC);
if (!$user) {
return ['allowed' => false, 'message' => 'User not found'];
}
// Check if user has active subscription (all subscription tiers)
$subscription = hasActiveSubscription($user_id);
if ($subscription) {
// User has subscription - check monthly limit for all subscription tiers
$subscription_plans = ['essential', 'starter', 'pro', 'premium', 'enterprise'];
if (in_array(strtolower($subscription['plan_name']), $subscription_plans)) {
$usage = getMonthlyTrackUsage($user_id, $subscription['plan_name']);
if ($usage['tracks_created'] >= $usage['track_limit']) {
$next_reset = date('F 1, Y', strtotime('first day of next month'));
return [
'allowed' => false,
'message' => "You've reached your monthly limit of {$usage['track_limit']} tracks. Your limit will reset on {$next_reset}. You can purchase extra credits if you need more tracks now.",
'tracks_used' => $usage['tracks_created'],
'track_limit' => $usage['track_limit']
];
}
return [
'allowed' => true,
'tracks_used' => $usage['tracks_created'],
'track_limit' => $usage['track_limit'],
'tracks_remaining' => $usage['track_limit'] - $usage['tracks_created']
];
}
}
// For non-subscription plans (free, or credit-based), use credit system
return ['allowed' => true, 'system' => 'credits'];
}
/**
* Increment monthly track usage
*/
function incrementMonthlyTrackUsage($user_id) {
$pdo = getDBConnection();
$year_month = date('Y-m');
$stmt = $pdo->prepare("
UPDATE monthly_track_usage
SET tracks_created = tracks_created + 1,
updated_at = NOW()
WHERE user_id = ? AND year_month = ?
");
$stmt->execute([$user_id, $year_month]);
return $stmt->rowCount() > 0;
}
/**
* Get subscription info for user
*/
function getSubscriptionInfo($user_id) {
try {
$pdo = getDBConnection();
$stmt = $pdo->prepare("
SELECT
us.*,
u.name as user_name,
u.email as user_email
FROM user_subscriptions us
JOIN users u ON us.user_id = u.id
WHERE us.user_id = ?
ORDER BY us.created_at DESC
LIMIT 1
");
$stmt->execute([$user_id]);
$result = $stmt->fetch(PDO::FETCH_ASSOC);
return $result ? $result : null;
} catch (Exception $e) {
error_log("Error in getSubscriptionInfo: " . $e->getMessage());
return null;
}
}
?>