ILIAS  trunk Revision v13.0_alpha-62-g3b72d83f2e5
General.php
Go to the documentation of this file.
1<?php
2
20
22
26class General implements DownloadLimit
27{
28 public const MODULE_NAME = 'file_access';
29 public const F_BG_LIMIT = 'bg_limit';
30 public const F_INLINE_FILE_EXTENSIONS = 'inline_file_extensions';
31 public const F_SHOW_AMOUNT_OF_DOWNLOADS = 'show_amount_of_downloads';
32 public const F_DOWNLOAD_ASCII_FILENAME = 'download_ascii_filename';
36 private const SEPARATOR = ' ';
37
39 'gif',
40 'jpg',
41 'jpeg',
42 'mp3',
43 'pdf',
44 'png',
45 ];
46
50 private ?array $setting = null;
51
52 public function __construct(private readonly \ilDBInterface $db)
53 {
54 }
55
59 public function read(): void
60 {
61 $this->setting = [];
62
63 $res = $this->db->queryF(
64 "SELECT keyword, value FROM settings WHERE module = %s",
65 ['text'],
66 [self::MODULE_NAME]
67 );
68
69 while ($row = $this->db->fetchAssoc($res)) {
70 $this->setting[$row["keyword"]] = $row["value"];
71 }
72 }
73
74 public function get(
75 string $keyword,
76 ?string $default_value = null
77 ): ?string {
78 if ($this->setting === null) {
79 $this->read();
80 }
81
82 return $this->setting[$keyword] ?? $default_value;
83 }
84
85 public function set(string $keyword, string $value): void
86 {
87 if ($this->setting === null) {
88 $this->read();
89 }
90
91 $this->db->replace(
92 'settings',
93 [
94 'module' => ['text', self::MODULE_NAME],
95 'keyword' => ['text', $keyword],
96 ],
97 [
98 'value' => ['text', $value],
99 ]
100 );
101
102 $this->setting[$keyword] = $value;
103 }
104
105
106 public function isDownloadWithAsciiFileName(): bool
107 {
108 return $this->strToBool($this->get(self::F_DOWNLOAD_ASCII_FILENAME, '1'));
109 }
110
111 public function setDownloadWithAsciiFileName(bool $value): void
112 {
113 $this->set(self::F_DOWNLOAD_ASCII_FILENAME, $this->boolToStr($value));
114 }
115
116 public function isShowAmountOfDownloads(): bool
117 {
118 return $this->strToBool($this->get(self::F_SHOW_AMOUNT_OF_DOWNLOADS, '1'));
119 }
120
121 public function setShowAmountOfDownloads(bool $value): void
122 {
123 $this->set(self::F_SHOW_AMOUNT_OF_DOWNLOADS, $this->boolToStr($value));
124 }
125
126 public function setInlineFileExtensions(array $extensions): void
127 {
128 $extensions = array_map(
129 fn(string $extension): string => strtolower(trim($extension, " \t\n\r\0\x0B,")),
130 $extensions
131 );
132
133 $this->set(self::F_INLINE_FILE_EXTENSIONS, $this->arrayToStr($extensions));
134 }
135
136 public function getInlineFileExtensions(): array
137 {
138 return $this->strToArray(
139 $this->get(
140 self::F_INLINE_FILE_EXTENSIONS,
141 $this->arrayToStr($this->default_inline_extensions)
142 )
143 );
144 }
145
146 public function getDownloadLimitinMB(): int
147 {
148 return $this->strToInt($this->get(self::F_BG_LIMIT, '200'));
149 }
150
151 public function setDownloadLimitInMB(int $limit): void
152 {
153 $this->set(self::F_BG_LIMIT, $this->intToStr($limit));
154 }
155
156 // HELPERS
157
158 private function strToBool(?string $value): bool
159 {
160 return $value === '1';
161 }
162
163 private function boolToStr(bool $value): string
164 {
165 return $value ? '1' : '0';
166 }
167
168 private function intToStr(int $int): string
169 {
170 return (string) $int;
171 }
172
173 private function strToInt(?string $str): int
174 {
175 return (int) $str;
176 }
177
178 private function arrayToStr(array $array): string
179 {
180 return implode(self::SEPARATOR, $array);
181 }
182
183 private function strToArray(?string $str): array
184 {
185 return explode(self::SEPARATOR, (string) $str);
186 }
187}
read()
Reads this module's settings, replacing whatever was read before.
Definition: General.php:59
__construct(private readonly \ilDBInterface $db)
Definition: General.php:52
setInlineFileExtensions(array $extensions)
Definition: General.php:126
The global limit for the sum of file sizes a ZIP download may collect.
Interface ilDBInterface.
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
Definition: Form.php:3
foreach($_POST as $key=> $value) $res