WebOrbiton
v1.0.0.1

StocketBase

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