v1.0.0.3
StocketBase
- <?php
-
- declare(strict_types=1);
-
- if (count(get_included_files()) === 1) {
- http_response_code(403);
- exit;
- }
-
- $db = Database::site();
- $action = $_POST['action'] ?? '';
-
- if ($action === 'save_ad') {
- $adId = (int) ($_POST['ad_id'] ?? 0) ?: null;
- $name = trim((string) ($_POST['name'] ?? ''));
- $type = in_array($_POST['type'] ?? '', ['static_text', 'static_image', 'script'], true) ? $_POST['type'] : 'static_text';
- $placement = array_key_exists($_POST['placement'] ?? '', SiteFront::AD_PLACEMENTS) ? $_POST['placement'] : 'header';
- $content = (string) ($_POST['content'] ?? '');
- $linkUrl = trim((string) ($_POST['link_url'] ?? ''));
- $isActive = isset($_POST['is_active']) ? 1 : 0;
- $consentRun = (string) ($_POST['consent_run'] ?? 'always');
- if ($consentRun !== 'always' && !in_array($consentRun, SiteFront::CONSENT_CATEGORIES, true)) {
- $consentRun = 'always';
- }
-
- if ($name === '' || trim($content) === '') {
- $adsError = 'Please fill in the name and the content.';
- return;
- }
-
- if ($linkUrl !== '' && SiteFront::safeAdUrl($linkUrl) === '') {
- $adsError = 'The link should start with http:// or https://.';
- return;
- }
-
- if ($type === 'static_image' && SiteFront::safeAdUrl(trim($content)) === '') {
- $adsError = 'The image address should start with http:// or https://, or be a file from your store.';
- return;
- }
-
- if ($consentRun !== 'always' && stripos($content, '</template') !== false) {
- $adsError = 'This code contains a </template> tag, which can\'t be used with a consent requirement.';
- return;
- }
-
- if ($adId !== null) {
- $statement = $db->prepare(
- 'UPDATE ads SET name = :name, type = :type, placement = :placement, content = :content, link_url = :link_url, consent_run = :consent_run, is_active = :is_active WHERE id = :id'
- );
- $statement->execute([
- 'name' => $name,
- 'type' => $type,
- 'placement' => $placement,
- 'content' => $content,
- 'link_url' => $linkUrl !== '' ? $linkUrl : null,
- 'consent_run' => $consentRun,
- 'is_active' => $isActive,
- 'id' => $adId,
- ]);
- } else {
- $statement = $db->prepare(
- 'INSERT INTO ads (name, type, placement, content, link_url, consent_run, is_active, created_by) VALUES (:name, :type, :placement, :content, :link_url, :consent_run, :is_active, :created_by)'
- );
- $statement->execute([
- 'name' => $name,
- 'type' => $type,
- 'placement' => $placement,
- 'content' => $content,
- 'link_url' => $linkUrl !== '' ? $linkUrl : null,
- 'consent_run' => $consentRun,
- 'is_active' => $isActive,
- 'created_by' => $currentUser['id'],
- ]);
- }
- }
-
- if ($action === 'delete_ad') {
- $adId = (int) ($_POST['ad_id'] ?? 0);
- if ($adId > 0) {
- $statement = $db->prepare('DELETE FROM ads WHERE id = :id');
- $statement->execute(['id' => $adId]);
- }
- }
-