WebOrbiton
v1.0.0.3

StocketBase

84 lines · 3.0 KB
  1. <?php
  2. ​
  3. declare(strict_types=1);
  4. ​
  5. if (count(get_included_files()) === 1) {
  6. http_response_code(403);
  7. exit;
  8. }
  9. ​
  10. $db = Database::site();
  11. $action = $_POST['action'] ?? '';
  12. ​
  13. if ($action === 'save_ad') {
  14. $adId = (int) ($_POST['ad_id'] ?? 0) ?: null;
  15. $name = trim((string) ($_POST['name'] ?? ''));
  16. $type = in_array($_POST['type'] ?? '', ['static_text', 'static_image', 'script'], true) ? $_POST['type'] : 'static_text';
  17. $placement = array_key_exists($_POST['placement'] ?? '', SiteFront::AD_PLACEMENTS) ? $_POST['placement'] : 'header';
  18. $content = (string) ($_POST['content'] ?? '');
  19. $linkUrl = trim((string) ($_POST['link_url'] ?? ''));
  20. $isActive = isset($_POST['is_active']) ? 1 : 0;
  21. $consentRun = (string) ($_POST['consent_run'] ?? 'always');
  22. if ($consentRun !== 'always' && !in_array($consentRun, SiteFront::CONSENT_CATEGORIES, true)) {
  23. $consentRun = 'always';
  24. }
  25. ​
  26. if ($name === '' || trim($content) === '') {
  27. $adsError = 'Please fill in the name and the content.';
  28. return;
  29. }
  30. ​
  31. if ($linkUrl !== '' && SiteFront::safeAdUrl($linkUrl) === '') {
  32. $adsError = 'The link should start with http:// or https://.';
  33. return;
  34. }
  35. ​
  36. if ($type === 'static_image' && SiteFront::safeAdUrl(trim($content)) === '') {
  37. $adsError = 'The image address should start with http:// or https://, or be a file from your store.';
  38. return;
  39. }
  40. ​
  41. if ($consentRun !== 'always' && stripos($content, '</template') !== false) {
  42. $adsError = 'This code contains a </template> tag, which can\'t be used with a consent requirement.';
  43. return;
  44. }
  45. ​
  46. if ($adId !== null) {
  47. $statement = $db->prepare(
  48. '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'
  49. );
  50. $statement->execute([
  51. 'name' => $name,
  52. 'type' => $type,
  53. 'placement' => $placement,
  54. 'content' => $content,
  55. 'link_url' => $linkUrl !== '' ? $linkUrl : null,
  56. 'consent_run' => $consentRun,
  57. 'is_active' => $isActive,
  58. 'id' => $adId,
  59. ]);
  60. } else {
  61. $statement = $db->prepare(
  62. '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)'
  63. );
  64. $statement->execute([
  65. 'name' => $name,
  66. 'type' => $type,
  67. 'placement' => $placement,
  68. 'content' => $content,
  69. 'link_url' => $linkUrl !== '' ? $linkUrl : null,
  70. 'consent_run' => $consentRun,
  71. 'is_active' => $isActive,
  72. 'created_by' => $currentUser['id'],
  73. ]);
  74. }
  75. }
  76. ​
  77. if ($action === 'delete_ad') {
  78. $adId = (int) ($_POST['ad_id'] ?? 0);
  79. if ($adId > 0) {
  80. $statement = $db->prepare('DELETE FROM ads WHERE id = :id');
  81. $statement->execute(['id' => $adId]);
  82. }
  83. }
  84. ​