downloads.php

main 118 lines · 3.4 KB Raw
Alex Alex Commit Initial commit 01/10/2026 09:20
1<?php
2
3/**
4 * config.php's old single client_download/client_download_linux keys, in the
5 * same shape as a downloads.entries row - so they can be merged/normalized
6 * through the exact same code path as any custom entry an admin adds.
7 */
8function znote_download_legacy_entries(array $config): array {
9 $clientVersion = isset($config['client']) ? ((int)$config['client'] / 100) : '';
10
11 return array(
12 'windows_client' => array(
13 'label' => t('downloads.win', ['version' => $clientVersion]),
14 'url' => (string)($config['client_download'] ?? ''),
15 'section' => 'official',
16 'image' => '',
17 'description' => '',
18 'enabled' => !empty($config['client_download']),
19 ),
20 'linux_client' => array(
21 'label' => t('downloads.linux', ['version' => $clientVersion]),
22 'url' => (string)($config['client_download_linux'] ?? ''),
23 'section' => 'unsupported',
24 'image' => '',
25 'description' => '',
26 'enabled' => !empty($config['client_download_linux']),
27 ),
28 );
29}
30
31/** One config.php downloads.entries row, normalized - or null if it has no usable key. */
32function znote_download_normalize_entry($entry, array $legacy): ?array {
33 if (!is_array($entry)) {
34 return null;
35 }
36
37 $key = preg_replace('/[^a-z0-9_]/', '', strtolower((string)($entry['key'] ?? '')));
38 if ($key === '') {
39 return null;
40 }
41
42 $item = array(
43 'key' => $key,
44 'label' => trim((string)($entry['label'] ?? '')),
45 'url' => trim((string)($entry['url'] ?? '')),
46 'section' => trim((string)($entry['section'] ?? 'custom')),
47 'image' => trim((string)($entry['image'] ?? '')),
48 'description' => trim((string)($entry['description'] ?? '')),
49 'enabled' => !empty($entry['enabled']) && (string)$entry['enabled'] !== '0',
50 );
51
52 if (isset($legacy[$key])) {
53 $item = array_merge($item, $legacy[$key]);
54 }
55 if ($item['label'] === '') {
56 $item['label'] = ucwords(str_replace('_', ' ', $key));
57 }
58 if ($item['section'] === '') {
59 $item['section'] = 'custom';
60 }
61
62 return $item;
63}
64
65/** Windows/Linux client links an admin never added to downloads.entries at all - so they still show up if config.php sets a URL for them. */
66function znote_download_append_missing_legacy(array $normalized, array $legacy, bool $includeDisabled): array {
67 foreach ($legacy as $key => $entry) {
68 $exists = false;
69 foreach ($normalized as $item) {
70 if ($item['key'] === $key) {
71 $exists = true;
72 break;
73 }
74 }
75 if (!$exists && ($includeDisabled || ($entry['enabled'] && $entry['url'] !== ''))) {
76 $normalized[] = array_merge(array('key' => $key), $entry);
77 }
78 }
79
80 return $normalized;
81}
82
83function znote_download_entries(bool $includeDisabled = false): array {
84 global $config;
85
86 $entries = $config['downloads']['entries'] ?? array();
87 if (!is_array($entries)) {
88 $entries = array();
89 }
90
91 $legacy = znote_download_legacy_entries($config);
92
93 $normalized = array();
94 foreach ($entries as $entry) {
95 $item = znote_download_normalize_entry($entry, $legacy);
96 if ($item === null) {
97 continue;
98 }
99 if ($includeDisabled || ($item['enabled'] && $item['url'] !== '')) {
100 $normalized[] = $item;
101 }
102 }
103
104 return znote_download_append_missing_legacy($normalized, $legacy, $includeDisabled);
105}
106
107function znote_download_entries_by_section(bool $includeDisabled = false): array {
108 $sections = array();
109 foreach (znote_download_entries($includeDisabled) as $entry) {
110 $section = (string)$entry['section'];
111 if (!isset($sections[$section])) {
112 $sections[$section] = array();
113 }
114 $sections[$section][] = $entry;
115 }
116 return $sections;
117}
118
Top