WebOrbiton
v1.1.0.0

StocketBase

82 lines · 3.4 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_analytics_script') {
  14. $scriptId = (int) ($_POST['script_id'] ?? 0) ?: null;
  15. $name = trim((string) ($_POST['name'] ?? ''));
  16. $scriptCode = (string) ($_POST['script_code'] ?? '');
  17. $placement = in_array($_POST['placement'] ?? '', ['head', 'body_start', 'body_end'], true) ? $_POST['placement'] : 'head';
  18. $consentRun = (string) ($_POST['consent_run'] ?? 'analytics');
  19. $requiresConsent = $consentRun === 'always' ? 0 : 1;
  20. $consentCategory = SiteFront::consentCategory($consentRun);
  21. $isActive = isset($_POST['is_active']) ? 1 : 0;
  22. ​
  23. if ($name === '' || trim($scriptCode) === '') {
  24. $analyticsError = 'Please fill in the name and paste the code.';
  25. return;
  26. }
  27. ​
  28. if (stripos($scriptCode, '</template') !== false) {
  29. $analyticsError = 'This code contains a </template> tag, which can\'t be used here.';
  30. return;
  31. }
  32. ​
  33. if ($scriptId !== null) {
  34. $statement = $db->prepare(
  35. 'UPDATE analytics_scripts SET name = :name, script_code = :script_code, placement = :placement, requires_consent = :requires_consent, consent_category = :consent_category, is_active = :is_active WHERE id = :id'
  36. );
  37. $statement->execute([
  38. 'name' => $name,
  39. 'script_code' => $scriptCode,
  40. 'placement' => $placement,
  41. 'requires_consent' => $requiresConsent,
  42. 'consent_category' => $consentCategory,
  43. 'is_active' => $isActive,
  44. 'id' => $scriptId,
  45. ]);
  46. } else {
  47. $statement = $db->prepare(
  48. 'INSERT INTO analytics_scripts (name, script_code, placement, requires_consent, consent_category, is_active) VALUES (:name, :script_code, :placement, :requires_consent, :consent_category, :is_active)'
  49. );
  50. $statement->execute([
  51. 'name' => $name,
  52. 'script_code' => $scriptCode,
  53. 'placement' => $placement,
  54. 'requires_consent' => $requiresConsent,
  55. 'consent_category' => $consentCategory,
  56. 'is_active' => $isActive,
  57. ]);
  58. }
  59. }
  60. ​
  61. if ($action === 'delete_analytics_script') {
  62. $scriptId = (int) ($_POST['script_id'] ?? 0);
  63. if ($scriptId > 0) {
  64. $statement = $db->prepare('DELETE FROM analytics_scripts WHERE id = :id');
  65. $statement->execute(['id' => $scriptId]);
  66. }
  67. }
  68. ​
  69. if ($action === 'save_consent_settings') {
  70. $consentEnabled = isset($_POST['consent_manager_enabled']) ? '1' : '0';
  71. $statement = $db->prepare(
  72. 'INSERT INTO site_settings (setting_key, setting_value) VALUES (:key, :value) ON DUPLICATE KEY UPDATE setting_value = VALUES(setting_value)'
  73. );
  74. $statement->execute(['key' => 'consent_manager_enabled', 'value' => $consentEnabled]);
  75. $statement->execute(['key' => 'consent_footer_icon_enabled', 'value' => isset($_POST['consent_footer_icon_enabled']) ? '1' : '0']);
  76. $statement->execute(['key' => 'consent_manage_enabled', 'value' => isset($_POST['consent_manage_enabled']) ? '1' : '0']);
  77. foreach (SiteFront::CONSENT_CATEGORIES as $consentCategory) {
  78. $statement->execute(['key' => 'consent_show_' . $consentCategory, 'value' => isset($_POST['consent_show_' . $consentCategory]) ? '1' : '0']);
  79. }
  80. SiteFront::resetSettings();
  81. }
  82. ​