-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsubscriptions.php
More file actions
313 lines (286 loc) · 17.8 KB
/
Copy pathsubscriptions.php
File metadata and controls
313 lines (286 loc) · 17.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
<?php
declare(strict_types=1);
require_once __DIR__ . '/functions.php';
require_admin();
$plans = all_plans();
$editing = null;
$error = null;
if (isset($_GET['edit']) && is_string($_GET['edit'])) {
$editing = fetch_subscription($_GET['edit']);
}
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
try {
verify_csrf();
$action = (string) ($_POST['action'] ?? '');
$pdo = database();
if ($action === 'create') {
$id = trim((string) ($_POST['id'] ?? ''));
$id = $id !== '' ? slugify($id) : subscription_public_id();
$name = trim((string) ($_POST['name'] ?? ''));
$description = trim((string) ($_POST['description'] ?? ''));
$badge = trim((string) ($_POST['badge'] ?? ''));
$telegramId = trim((string) ($_POST['telegram_id'] ?? ''));
$status = ((string) ($_POST['status'] ?? 'active')) === 'disabled' ? 'disabled' : 'active';
$planId = trim((string) ($_POST['plan_id'] ?? ''));
$durationDays = max(0, (int) ($_POST['duration_days'] ?? 0));
$expiresInput = parse_datetime_to_utc((string) ($_POST['expires_at'] ?? ''));
$plan = $planId !== '' ? fetch_plan($planId) : null;
if ($name === '') {
throw new RuntimeException('Subscription name is required.');
}
if ($planId !== '' && $plan === null) {
throw new RuntimeException('Selected plan does not exist.');
}
if ($expiresInput === null) {
$days = $durationDays > 0 ? $durationDays : (int) ($plan['duration_days'] ?? 30);
$expiresInput = next_expiry_from_days(null, max(1, $days));
}
$stmt = $pdo->prepare(
'INSERT INTO subscriptions (id, name, description, badge, telegram_id, status, created_at, expires_at, plan_id)
VALUES (:id, :name, :description, :badge, :telegram_id, :status, :created_at, :expires_at, :plan_id)'
);
$stmt->execute([
'id' => $id,
'name' => $name,
'description' => $description,
'badge' => $badge,
'telegram_id' => $telegramId,
'status' => $status,
'created_at' => now_utc(),
'expires_at' => $expiresInput,
'plan_id' => $planId !== '' ? $planId : null,
]);
flash('success', 'Subscription created successfully.');
redirect('subscriptions');
}
if ($action === 'update') {
$id = trim((string) ($_POST['id'] ?? ''));
$subscription = fetch_subscription($id);
if ($subscription === null) {
throw new RuntimeException('Subscription not found.');
}
$name = trim((string) ($_POST['name'] ?? ''));
$description = trim((string) ($_POST['description'] ?? ''));
$badge = trim((string) ($_POST['badge'] ?? ''));
$telegramId = trim((string) ($_POST['telegram_id'] ?? ''));
$status = ((string) ($_POST['status'] ?? 'active')) === 'disabled' ? 'disabled' : 'active';
$planId = trim((string) ($_POST['plan_id'] ?? ''));
$expiresInput = parse_datetime_to_utc((string) ($_POST['expires_at'] ?? ''));
if ($name === '') {
throw new RuntimeException('Subscription name is required.');
}
if ($expiresInput === null) {
throw new RuntimeException('Expiry date must be valid.');
}
$stmt = $pdo->prepare(
'UPDATE subscriptions
SET name = :name, description = :description, badge = :badge, telegram_id = :telegram_id,
status = :status, expires_at = :expires_at, plan_id = :plan_id
WHERE id = :id'
);
$stmt->execute([
'id' => $id,
'name' => $name,
'description' => $description,
'badge' => $badge,
'telegram_id' => $telegramId,
'status' => $status,
'expires_at' => $expiresInput,
'plan_id' => $planId !== '' ? $planId : null,
]);
flash('success', 'Subscription updated.');
redirect('subscriptions');
}
if ($action === 'extend') {
$id = trim((string) ($_POST['id'] ?? ''));
$subscription = fetch_subscription($id);
if ($subscription === null) {
throw new RuntimeException('Subscription not found.');
}
$planId = trim((string) ($_POST['extend_plan_id'] ?? ''));
$days = max(0, (int) ($_POST['extend_days'] ?? 0));
if ($planId !== '') {
$plan = fetch_plan($planId);
if ($plan === null) {
throw new RuntimeException('Selected plan does not exist.');
}
$days = (int) $plan['duration_days'];
}
if ($days <= 0) {
throw new RuntimeException('Extension period must be greater than zero.');
}
$newExpiry = next_expiry_from_days((string) $subscription['expires_at'], $days);
$stmt = $pdo->prepare('UPDATE subscriptions SET expires_at = :expires_at, plan_id = :plan_id WHERE id = :id');
$stmt->execute([
'id' => $id,
'expires_at' => $newExpiry,
'plan_id' => $planId !== '' ? $planId : ($subscription['plan_id'] ?? null),
]);
flash('success', 'Subscription extended.');
redirect('subscriptions');
}
if ($action === 'toggle') {
$id = trim((string) ($_POST['id'] ?? ''));
$status = ((string) ($_POST['target_status'] ?? 'active')) === 'disabled' ? 'disabled' : 'active';
$stmt = $pdo->prepare('UPDATE subscriptions SET status = :status WHERE id = :id');
$stmt->execute(['id' => $id, 'status' => $status]);
flash('success', 'Subscription status updated.');
redirect('subscriptions');
}
if ($action === 'delete') {
$id = trim((string) ($_POST['id'] ?? ''));
$stmt = $pdo->prepare('DELETE FROM subscriptions WHERE id = :id');
$stmt->execute(['id' => $id]);
flash('success', 'Subscription deleted.');
redirect('subscriptions');
}
throw new RuntimeException('Unknown action.');
} catch (Throwable $exception) {
$error = $exception->getMessage();
}
}
$subscriptions = all_subscriptions();
$formValues = $editing ?: [
'id' => '',
'name' => '',
'description' => '',
'badge' => '',
'telegram_id' => '',
'status' => 'active',
'expires_at' => gmdate('Y-m-d\TH:i', strtotime('+30 days')),
'plan_id' => '',
];
render_shell_start('Subscriptions', 'Create, edit, extend, and distribute dynamic subscription links backed by the live source feed.');
?>
<div class="grid gap-6 xl:grid-cols-[0.92fr_1.08fr]">
<section class="glass rounded-[28px] p-6">
<div class="mb-6 flex items-center justify-between gap-3">
<div>
<p class="text-xs font-semibold uppercase tracking-[0.28em] text-slate-500"><?= $editing ? 'Edit Record' : 'New Subscription' ?></p>
<h2 class="mt-2 text-xl font-extrabold text-slate-900"><?= $editing ? 'Update subscription' : 'Create subscription' ?></h2>
</div>
<?php if ($editing): ?>
<a href="<?= html_escape(app_url('subscriptions')) ?>" class="rounded-full bg-slate-900 px-4 py-2 text-sm font-semibold text-white">New record</a>
<?php endif; ?>
</div>
<?php if ($error !== null): ?>
<div class="mb-5 rounded-2xl border border-rose-200 bg-rose-50 px-4 py-3 text-sm font-medium text-rose-800"><?= html_escape($error) ?></div>
<?php endif; ?>
<form method="post" class="space-y-5">
<input type="hidden" name="_csrf" value="<?= html_escape(csrf_token()) ?>">
<input type="hidden" name="action" value="<?= $editing ? 'update' : 'create' ?>">
<?php if ($editing): ?>
<input type="hidden" name="id" value="<?= html_escape($formValues['id']) ?>">
<?php else: ?>
<?php render_form_input('Custom ID (optional)', 'id', '', 'text', false, 'Leave blank to auto-generate'); ?>
<?php endif; ?>
<?php render_form_input('Name', 'name', (string) $formValues['name'], 'text', true); ?>
<?php render_form_textarea('Description', 'description', (string) $formValues['description'], 4); ?>
<div class="grid gap-5 sm:grid-cols-2">
<?php render_form_input('Badge', 'badge', (string) $formValues['badge']); ?>
<?php render_form_input('Telegram ID', 'telegram_id', (string) $formValues['telegram_id']); ?>
</div>
<div class="grid gap-5 sm:grid-cols-2">
<label class="block">
<span class="mb-2 block text-sm font-semibold text-slate-700">Plan</span>
<select name="plan_id" class="w-full rounded-2xl border border-white/60 bg-white/70 px-4 py-3 text-sm text-slate-900 shadow-sm outline-none focus:border-slate-300 focus:ring-2 focus:ring-slate-200">
<option value="">Custom</option>
<?php foreach ($plans as $plan): ?>
<option value="<?= html_escape($plan['id']) ?>" <?= (string) $formValues['plan_id'] === (string) $plan['id'] ? 'selected' : '' ?>>
<?= html_escape($plan['name']) ?> (<?= html_escape((string) $plan['duration_days']) ?> days)
</option>
<?php endforeach; ?>
</select>
</label>
<label class="block">
<span class="mb-2 block text-sm font-semibold text-slate-700">Status</span>
<select name="status" class="w-full rounded-2xl border border-white/60 bg-white/70 px-4 py-3 text-sm text-slate-900 shadow-sm outline-none focus:border-slate-300 focus:ring-2 focus:ring-slate-200">
<option value="active" <?= (string) $formValues['status'] === 'active' ? 'selected' : '' ?>>Active</option>
<option value="disabled" <?= (string) $formValues['status'] === 'disabled' ? 'selected' : '' ?>>Disabled</option>
</select>
</label>
</div>
<?php if (!$editing): ?>
<div class="grid gap-5 sm:grid-cols-2">
<?php render_form_input('Duration Days', 'duration_days', '30', 'number', false); ?>
<?php render_form_input('Exact Expiry (optional)', 'expires_at', '', 'datetime-local', false); ?>
</div>
<?php else: ?>
<?php render_form_input('Expiry', 'expires_at', gmdate('Y-m-d\TH:i', strtotime((string) $formValues['expires_at'])), 'datetime-local', true); ?>
<?php endif; ?>
<button type="submit" class="btn-primary inline-flex items-center justify-center rounded-2xl px-5 py-3 text-sm font-semibold shadow-lg"><?= $editing ? 'Save Changes' : 'Create Subscription' ?></button>
</form>
</section>
<section class="glass rounded-[28px] p-6">
<div class="mb-5">
<p class="text-xs font-semibold uppercase tracking-[0.28em] text-slate-500">Inventory</p>
<h2 class="mt-2 text-xl font-extrabold text-slate-900">All subscriptions</h2>
</div>
<div class="space-y-4">
<?php if ($subscriptions === []): ?>
<div class="rounded-2xl bg-white/70 px-4 py-5 text-sm text-slate-500">No subscriptions created yet.</div>
<?php endif; ?>
<?php foreach ($subscriptions as $subscription): ?>
<article class="rounded-[24px] bg-white/70 p-5 shadow-sm">
<div class="flex flex-col gap-4 lg:flex-row lg:items-start lg:justify-between">
<div class="min-w-0">
<div class="flex flex-wrap items-center gap-2">
<h3 class="text-lg font-extrabold text-slate-900"><?= html_escape($subscription['name']) ?></h3>
<?php if ((string) $subscription['badge'] !== ''): ?>
<span class="rounded-full bg-slate-900 px-3 py-1 text-xs font-semibold text-white"><?= html_escape($subscription['badge']) ?></span>
<?php endif; ?>
<span class="rounded-full px-3 py-1 text-xs font-semibold <?= $subscription['status'] === 'active' ? 'bg-emerald-100 text-emerald-700' : 'bg-rose-100 text-rose-700' ?>">
<?= html_escape($subscription['status']) ?>
</span>
</div>
<p class="mt-2 text-sm text-slate-600"><?= html_escape((string) $subscription['description']) ?></p>
<div class="mt-3 flex flex-wrap gap-3 text-xs text-slate-500">
<span>ID: <?= html_escape($subscription['id']) ?></span>
<span>Plan: <?= html_escape((string) ($subscription['plan_name'] ?? 'Custom')) ?></span>
<span>Expires: <?= html_escape($subscription['expires_at']) ?></span>
<span>Telegram: <?= html_escape((string) ($subscription['telegram_id'] ?: 'Not linked')) ?></span>
</div>
<div class="mt-3 flex flex-wrap gap-2 text-xs">
<a class="rounded-full bg-slate-900 px-3 py-1.5 font-semibold text-white" href="<?= html_escape(app_url('subscription/' . $subscription['id'])) ?>" target="_blank" rel="noopener">Public page</a>
<a class="rounded-full border border-slate-200 bg-white px-3 py-1.5 font-semibold text-slate-700" href="<?= html_escape(app_url('subscription/' . $subscription['id'] . '?response=json')) ?>" target="_blank" rel="noopener">Raw JSON</a>
<a class="rounded-full border border-slate-200 bg-white px-3 py-1.5 font-semibold text-slate-700" href="<?= html_escape(app_url('subscription/' . $subscription['id'] . '?format=happ')) ?>" target="_blank" rel="noopener">Happ</a>
<a class="rounded-full border border-slate-200 bg-white px-3 py-1.5 font-semibold text-slate-700" href="<?= html_escape(app_url('subscriptions?edit=' . urlencode((string) $subscription['id']))) ?>">Edit</a>
</div>
</div>
<div class="grid gap-2 sm:grid-cols-2 lg:w-[320px]">
<form method="post" class="contents">
<input type="hidden" name="_csrf" value="<?= html_escape(csrf_token()) ?>">
<input type="hidden" name="action" value="toggle">
<input type="hidden" name="id" value="<?= html_escape($subscription['id']) ?>">
<input type="hidden" name="target_status" value="<?= $subscription['status'] === 'active' ? 'disabled' : 'active' ?>">
<button type="submit" class="rounded-2xl bg-white px-4 py-3 text-sm font-semibold text-slate-700 shadow-sm"><?= $subscription['status'] === 'active' ? 'Disable' : 'Enable' ?></button>
</form>
<form method="post" class="contents" onsubmit="return confirm('Delete this subscription?');">
<input type="hidden" name="_csrf" value="<?= html_escape(csrf_token()) ?>">
<input type="hidden" name="action" value="delete">
<input type="hidden" name="id" value="<?= html_escape($subscription['id']) ?>">
<button type="submit" class="rounded-2xl bg-rose-600 px-4 py-3 text-sm font-semibold text-white shadow-sm">Delete</button>
</form>
<form method="post" class="sm:col-span-2 rounded-2xl bg-slate-50 p-3">
<input type="hidden" name="_csrf" value="<?= html_escape(csrf_token()) ?>">
<input type="hidden" name="action" value="extend">
<input type="hidden" name="id" value="<?= html_escape($subscription['id']) ?>">
<div class="grid gap-2 sm:grid-cols-[1fr_1fr_auto]">
<input type="number" min="1" name="extend_days" value="30" class="rounded-xl border border-slate-200 bg-white px-3 py-2 text-sm" placeholder="Days">
<select name="extend_plan_id" class="rounded-xl border border-slate-200 bg-white px-3 py-2 text-sm">
<option value="">Or pick a plan</option>
<?php foreach ($plans as $plan): ?>
<option value="<?= html_escape($plan['id']) ?>"><?= html_escape($plan['name']) ?> (<?= html_escape((string) $plan['duration_days']) ?>d)</option>
<?php endforeach; ?>
</select>
<button type="submit" class="rounded-xl bg-slate-900 px-4 py-2 text-sm font-semibold text-white">Extend</button>
</div>
</form>
</div>
</div>
</article>
<?php endforeach; ?>
</div>
</section>
</div>
<?php render_shell_end();