]>
Commit | Line | Data |
---|---|---|
e480c0e5 | 1 | <?php |
9184e8d4 GJ |
2 | /* |
3 | * Uguu | |
4 | * | |
5 | * @copyright Copyright (c) 2022-2024 Go Johansson (nokonoko) <neku@pomf.se> | |
6 | * | |
7 | * Note that this was previously distributed under the MIT license 2015-2022. | |
8 | * | |
9 | * If you are a company that wants to use Uguu I urge you to contact me to | |
10 | * solve any potential license issues rather then using pre-2022 code. | |
11 | * | |
12 | * A special thanks goes out to the open source community around the world | |
13 | * for supporting and being the backbone of projects like Uguu. | |
14 | * | |
15 | * This project can be found at <https://github.com/nokonoko/Uguu>. | |
16 | * | |
17 | * This program is free software: you can redistribute it and/or modify | |
18 | * it under the terms of the GNU General Public License as published by | |
19 | * the Free Software Foundation, either version 3 of the License, or | |
20 | * (at your option) any later version. | |
21 | * | |
22 | * This program is distributed in the hope that it will be useful, | |
23 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
24 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
25 | * GNU General Public License for more details. | |
26 | * | |
27 | * You should have received a copy of the GNU General Public License | |
28 | * along with this program. If not, see <https://www.gnu.org/licenses/>. | |
29 | */ | |
30 | ||
31 | namespace Pomf\Uguu\Classes; | |
32 | ||
33 | class Response | |
34 | { | |
35 | public string $type; | |
36 | ||
37 | /** | |
38 | * Takes a string as an argument and sets the header to the appropriate content type | |
8f7f8840 | 39 | * |
9184e8d4 GJ |
40 | * @param $response_type string The type of response you want to return. |
41 | * Valid options are: csv, html, json, text. | |
42 | */ | |
43 | public function __construct(string $response_type) | |
44 | { | |
45 | switch ($response_type) { | |
46 | case 'csv': | |
47 | header('Content-Type: text/csv; charset=UTF-8'); | |
48 | $this->type = $response_type; | |
49 | break; | |
50 | case 'html': | |
51 | header('Content-Type: text/html; charset=UTF-8'); | |
52 | $this->type = $response_type; | |
53 | break; | |
54 | case 'json': | |
55 | header('Content-Type: application/json; charset=UTF-8'); | |
56 | $this->type = $response_type; | |
57 | break; | |
58 | case 'gyazo': | |
59 | header('Content-Type: text/plain; charset=UTF-8'); | |
60 | $this->type = 'text'; | |
61 | break; | |
62 | case 'text': | |
63 | header('Content-Type: text/plain; charset=UTF-8'); | |
64 | $this->type = $response_type; | |
65 | break; | |
66 | default: | |
67 | header('Content-Type: application/json; charset=UTF-8'); | |
68 | $this->type = 'json'; | |
69 | break; | |
70 | } | |
71 | } | |
72 | ||
73 | /** | |
74 | * Returns a string based on the type of response requested | |
4469e4dc | 75 | * |
9184e8d4 GJ |
76 | * @param $code mixed The HTTP status code to return. |
77 | * @param $desc string The description of the error. | |
78 | */ | |
79 | public function error(int $code, string $desc):string | |
80 | { | |
81 | $response = match ($this->type) { | |
82 | 'csv' => $this->csvError($desc), | |
83 | 'html' => $this->htmlError($code, $desc), | |
84 | 'json' => $this->jsonError($code, $desc), | |
85 | 'text' => $this->textError($code, $desc), | |
86 | }; | |
87 | http_response_code($code); | |
88 | echo $response; | |
89 | exit(1); | |
90 | } | |
91 | ||
92 | /* Returning a string that contains the error message. */ | |
93 | private static function csvError(string $description):string | |
94 | { | |
95 | return '"error"' . "\r\n" . "\"$description\"" . "\r\n"; | |
96 | } | |
97 | ||
98 | /** | |
99 | * Returns a string containing an HTML paragraph element with the error code and description | |
4469e4dc | 100 | * |
9184e8d4 GJ |
101 | * @param $code int|string The error code. |
102 | * @param $description string The description of the error. | |
4469e4dc | 103 | * |
9184e8d4 GJ |
104 | * @return string A string. |
105 | */ | |
106 | private static function htmlError(int|string $code, string $description):string | |
107 | { | |
108 | return '<p>ERROR: (' . $code . ') ' . $description . '</p>'; | |
109 | } | |
110 | ||
111 | /** | |
112 | * Returns a JSON string with the error code and description | |
4469e4dc | 113 | * |
9184e8d4 GJ |
114 | * @param $code int|string The error code. |
115 | * @param $description string The description of the error. | |
8f7f8840 | 116 | * |
9184e8d4 GJ |
117 | * @return bool|string A JSON string |
118 | */ | |
119 | private static function jsonError(int|string $code, string $description):bool|string | |
120 | { | |
121 | return json_encode([ | |
122 | 'success' => false, | |
123 | 'errorcode' => $code, | |
124 | 'description' => $description, | |
125 | ], JSON_PRETTY_PRINT); | |
126 | } | |
127 | ||
128 | /** | |
129 | * Returns a string that contains the error code and description | |
8f7f8840 | 130 | * |
9184e8d4 GJ |
131 | * @param $code int|string The error code. |
132 | * @param $description string The description of the error. | |
8f7f8840 | 133 | * |
9184e8d4 | 134 | * @return string A string with the error code and description. |
8f7f8840 | 135 | */ |
9184e8d4 | 136 | private static function textError(int|string $code, string $description):string |
e480c0e5 | 137 | { |
9184e8d4 GJ |
138 | return 'ERROR: (' . $code . ') ' . $description; |
139 | } | |
52053519 | 140 | |
9184e8d4 GJ |
141 | /** |
142 | * "If the type is csv, then call the csvSuccess function, | |
143 | * if the type is html, then call the htmlSuccess function, etc." | |
144 | * | |
145 | * The `match` keyword is a new feature in PHP 8. It's a lot like a switch statement, but it's more powerful | |
146 | * | |
147 | * @param $files array An array of file objects. | |
148 | */ | |
149 | public function send(array $files):void | |
150 | { | |
151 | $response = match ($this->type) { | |
152 | 'csv' => $this->csvSuccess($files), | |
153 | 'html' => $this->htmlSuccess($files), | |
154 | 'json' => $this->jsonSuccess($files), | |
155 | 'text' => $this->textSuccess($files), | |
156 | }; | |
157 | http_response_code(200); // "200 OK". Success. | |
158 | echo $response; | |
159 | } | |
52053519 | 160 | |
9184e8d4 GJ |
161 | /** |
162 | * Takes an array of files and returns a CSV string | |
163 | * | |
164 | * @param $files array An array of files that have been uploaded. | |
165 | * | |
166 | * @return string A string of the files in the array. | |
167 | */ | |
168 | private static function csvSuccess(array $files):string | |
169 | { | |
170 | $result = '"name","url","hash","size"' . "\r\n"; | |
171 | foreach ($files as $file) { | |
172 | $result .= '"' . $file['name'] . '"' . ',' . | |
173 | '"' . $file['url'] . '"' . ',' . | |
174 | '"' . $file['hash'] . '"' . ',' . | |
175 | '"' . $file['size'] . '"' . "\r\n"; | |
cec6349e | 176 | } |
9184e8d4 GJ |
177 | return $result; |
178 | } | |
52053519 | 179 | |
9184e8d4 GJ |
180 | /** |
181 | * Takes an array of files and returns a string of HTML links | |
182 | * | |
183 | * @param $files array An array of files to be uploaded. | |
184 | * | |
185 | * @return string the result of the foreach loop. | |
186 | */ | |
187 | private static function htmlSuccess(array $files):string | |
188 | { | |
189 | $result = ''; | |
190 | foreach ($files as $file) { | |
191 | $result .= '<a href="' . $file['url'] . '">' . $file['url'] . '</a><br>'; | |
52053519 | 192 | } |
9184e8d4 GJ |
193 | return $result; |
194 | } | |
52053519 | 195 | |
9184e8d4 GJ |
196 | /** |
197 | * Returns a JSON string that contains a success message and the files that were uploaded | |
198 | * | |
199 | * @param $files array The files to be uploaded. | |
200 | * | |
201 | * @return bool|string A JSON string | |
202 | */ | |
203 | private static function jsonSuccess(array $files):bool|string | |
204 | { | |
205 | return json_encode([ | |
206 | 'success' => true, | |
207 | 'files' => $files, | |
208 | ], JSON_PRETTY_PRINT); | |
209 | } | |
52053519 | 210 | |
9184e8d4 GJ |
211 | /** |
212 | * Takes an array of files and returns a string of URLs | |
213 | * | |
214 | * @param $files array The files to be uploaded. | |
215 | * | |
216 | * @return string the url of the file. | |
217 | */ | |
218 | private static function textSuccess(array $files):string | |
219 | { | |
220 | $result = ''; | |
221 | foreach ($files as $file) { | |
222 | $result .= $file['url'] . "\n"; | |
e480c0e5 | 223 | } |
9184e8d4 GJ |
224 | return $result; |
225 | } | |
226 | } |