3 * @copyright Copyright (C) 2020, Friendica
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.
39 * @return string The URL encoded string.
41 private static function encode($s)
43 return str_replace(['<', '>', '[', ']'], ['%3c', '%3e', '%5b', '%5d'], $s);
47 * URL decode <, >, left and right brackets
49 * @param string $s The URL encoded string to be decoded
51 * @return string The decoded string.
53 private static function decode($s)
55 return str_replace(['%3c', '%3e', '%5b', '%5d'], ['<', '>', '[', ']'], $s);
59 * Get file tags from array
61 * ex. given [music,video] return <music><video> or [music][video]
63 * @param array $array A list of tags.
64 * @param string $type Optional file type.
66 * @return string A list of file tags.
68 public static function arrayToFile(array $array, string $type = 'file')
71 if ($type == 'file') {
79 foreach ($array as $item) {
81 $tag_list .= $lbracket . self::encode(trim($item)) . $rbracket;
89 * Get tag list from file tags
91 * ex. given <music><video>[friends], return [music,video] or [friends]
93 * @param string $file File tags
94 * @param string $type Optional file type.
96 * @return array List of tag names.
98 public static function fileToArray(string $file, string $type = 'file')
103 if ($type == 'file') {
104 $cnt = preg_match_all('/\[(.*?)\]/', $file, $matches, PREG_SET_ORDER);
106 $cnt = preg_match_all('/<(.*?)>/', $file, $matches, PREG_SET_ORDER);
110 foreach ($matches as $match) {
111 $return[] = self::decode($match[1]);
119 * Get file tags from list
121 * ex. given music,video return <music><video> or [music][video]
122 * @param string $list A comma delimited list of tags.
123 * @param string $type Optional file type.
125 * @return string A list of file tags.
126 * @deprecated since 2019.06 use arrayToFile() instead
128 public static function listToFile(string $list, string $type = 'file')
130 $list_array = explode(',', $list);
132 return self::arrayToFile($list_array, $type);