3 * @copyright Copyright (C) 2010-2023, the Friendica project
5 * @license GNU AGPL version 3 or any later version
7 * This program is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU Affero General Public License as
9 * published by the Free Software Foundation, either version 3 of the
10 * License, or (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU Affero General Public License for more details.
17 * You should have received a copy of the GNU Affero General Public License
18 * along with this program. If not, see <https://www.gnu.org/licenses/>.
22 namespace Friendica\Model;
25 * This class handles FileTag related functions
27 * post categories and "save to file" use the same item.file table for storage.
28 * We will differentiate the different uses by wrapping categories in angle brackets
29 * and save to file categories in square brackets.
30 * To do this we need to escape these characters if they appear in our tag.
35 * URL encode <, >, left and right brackets
37 * @param string $s String to be URL encoded.
38 * @return string The URL encoded string.
40 private static function encode(string $s): string
42 return str_replace(['<', '>', '[', ']'], ['%3c', '%3e', '%5b', '%5d'], $s);
46 * URL decode <, >, left and right brackets
48 * @param string $s The URL encoded string to be decoded
49 * @return string The decoded string.
51 private static function decode(string $s): string
53 return str_replace(['%3c', '%3e', '%5b', '%5d'], ['<', '>', '[', ']'], $s);
57 * Get file tags from array
59 * ex. given [music,video] return <music><video> or [music][video]
61 * @param array $array A list of tags.
62 * @param string $type Optional file type.
63 * @return string A list of file tags.
65 public static function arrayToFile(array $array, string $type = 'file'): string
68 if ($type == 'file') {
76 foreach ($array as $item) {
78 $tag_list .= $lbracket . self::encode(trim($item)) . $rbracket;
86 * Get tag list from file tags
88 * ex. given <music><video>[friends], return [music,video] or [friends]
90 * @param string $file File tags
91 * @param string $type Optional file type.
92 * @return array List of tag names.
94 public static function fileToArray(string $file, string $type = 'file'): array
99 if ($type == 'file') {
100 $cnt = preg_match_all('/\[(.*?)\]/', $file, $matches, PREG_SET_ORDER);
102 $cnt = preg_match_all('/<(.*?)>/', $file, $matches, PREG_SET_ORDER);
106 foreach ($matches as $match) {
107 $return[] = self::decode($match[1]);