WebOrbiton
v2.0.0.1

Publisium

115 lines · 3.3 KB
  1. <?php
  2. ​
  3. declare(strict_types=1);
  4. ​
  5. require_once __DIR__ . '/social-icons.php';
  6. ​
  7. final class FooterLinks
  8. {
  9. public const MAX_COLUMNS = 6;
  10. ​
  11. public static function sanitizeColumns(string $rawJson): string
  12. {
  13. $decoded = json_decode($rawJson, true);
  14. if (!is_array($decoded)) {
  15. $decoded = [];
  16. }
  17. ​
  18. $clean = [];
  19. foreach ($decoded as $column) {
  20. if (!is_array($column)) {
  21. continue;
  22. }
  23. $title = strip_tags((string) ($column['title'] ?? ''));
  24. if ($title === '') {
  25. continue;
  26. }
  27. ​
  28. $links = [];
  29. foreach (is_array($column['links'] ?? null) ? $column['links'] : [] as $link) {
  30. if (!is_array($link)) {
  31. continue;
  32. }
  33. $label = strip_tags((string) ($link['label'] ?? ''));
  34. $url = self::cleanUrl((string) ($link['url'] ?? ''));
  35. if ($label === '' || $url === '') {
  36. continue;
  37. }
  38. $links[] = ['label' => $label, 'url' => $url];
  39. }
  40. ​
  41. if (empty($links)) {
  42. continue;
  43. }
  44. ​
  45. $clean[] = ['title' => $title, 'links' => $links];
  46. ​
  47. if (count($clean) >= self::MAX_COLUMNS) {
  48. break;
  49. }
  50. }
  51. ​
  52. return json_encode($clean, JSON_UNESCAPED_UNICODE) ?: '[]';
  53. }
  54. ​
  55. public static function decodeColumns(string $rawJson): array
  56. {
  57. $decoded = json_decode($rawJson, true);
  58. return is_array($decoded) ? $decoded : [];
  59. }
  60. ​
  61. public static function sanitizeSocialLinks(string $rawJson): string
  62. {
  63. $decoded = json_decode($rawJson, true);
  64. if (!is_array($decoded)) {
  65. $decoded = [];
  66. }
  67. ​
  68. $clean = [];
  69. foreach ($decoded as $item) {
  70. if (!is_array($item)) {
  71. continue;
  72. }
  73. $url = self::cleanUrl((string) ($item['url'] ?? ''));
  74. if ($url === '') {
  75. continue;
  76. }
  77. ​
  78. $key = strip_tags((string) ($item['key'] ?? ''));
  79. $label = strip_tags((string) ($item['label'] ?? ''));
  80. ​
  81. if ($key !== '' && SocialIcons::isBuiltin($key)) {
  82. $clean[] = ['key' => $key, 'label' => $label !== '' ? $label : $key, 'url' => $url];
  83. continue;
  84. }
  85. ​
  86. $svg = SocialIcons::sanitizeCustomSvg((string) ($item['svg'] ?? ''));
  87. if ($svg === '' || $label === '') {
  88. continue;
  89. }
  90. ​
  91. $clean[] = ['key' => 'custom', 'label' => $label, 'url' => $url, 'svg' => $svg];
  92. }
  93. ​
  94. return json_encode($clean, JSON_UNESCAPED_UNICODE) ?: '[]';
  95. }
  96. ​
  97. public static function decodeSocialLinks(string $rawJson): array
  98. {
  99. $decoded = json_decode($rawJson, true);
  100. return is_array($decoded) ? $decoded : [];
  101. }
  102. ​
  103. private static function cleanUrl(string $url): string
  104. {
  105. $url = trim($url);
  106. if ($url === '') {
  107. return '';
  108. }
  109. if (str_starts_with($url, '/') || str_starts_with($url, 'https://') || str_starts_with($url, 'http://') || str_starts_with($url, 'mailto:')) {
  110. return filter_var($url, FILTER_SANITIZE_URL) ?: '';
  111. }
  112. return '';
  113. }
  114. }
  115. ​