]> git.mxchange.org Git - friendica.git/blob - src/Model/FileTag.php
We now store the receivers as well
[friendica.git] / src / Model / FileTag.php
1 <?php
2 /**
3  * @copyright Copyright (C) 2010-2022, the Friendica project
4  *
5  * @license GNU AGPL version 3 or any later version
6  *
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.
11  *
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.
16  *
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/>.
19  *
20  */
21
22 namespace Friendica\Model;
23
24 /**
25  * This class handles FileTag related functions
26  *
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.
31  */
32 class FileTag
33 {
34         /**
35          * URL encode <, >, left and right brackets
36          *
37          * @param string $s String to be URL encoded.
38          *
39          * @return string   The URL encoded string.
40          */
41         private static function encode($s)
42         {
43                 return str_replace(['<', '>', '[', ']'], ['%3c', '%3e', '%5b', '%5d'], $s);
44         }
45
46         /**
47          * URL decode <, >, left and right brackets
48          *
49          * @param string $s The URL encoded string to be decoded
50          *
51          * @return string   The decoded string.
52          */
53         private static function decode($s)
54         {
55                 return str_replace(['%3c', '%3e', '%5b', '%5d'], ['<', '>', '[', ']'], $s);
56         }
57
58         /**
59          * Get file tags from array
60          *
61          * ex. given [music,video] return <music><video> or [music][video]
62          *
63          * @param array  $array A list of tags.
64          * @param string $type  Optional file type.
65          *
66          * @return string       A list of file tags.
67          */
68         public static function arrayToFile(array $array, string $type = 'file')
69         {
70                 $tag_list = '';
71                 if ($type == 'file') {
72                         $lbracket = '[';
73                         $rbracket = ']';
74                 } else {
75                         $lbracket = '<';
76                         $rbracket = '>';
77                 }
78
79                 foreach ($array as $item) {
80                         if (strlen($item)) {
81                                 $tag_list .= $lbracket . self::encode(trim($item)) . $rbracket;
82                         }
83                 }
84
85                 return $tag_list;
86         }
87
88         /**
89          * Get tag list from file tags
90          *
91          * ex. given <music><video>[friends], return [music,video] or [friends]
92          *
93          * @param string $file File tags
94          * @param string $type Optional file type.
95          *
96          * @return array        List of tag names.
97          */
98         public static function fileToArray(string $file, string $type = 'file')
99         {
100                 $matches = [];
101                 $return = [];
102
103                 if ($type == 'file') {
104                         $cnt = preg_match_all('/\[(.*?)\]/', $file, $matches, PREG_SET_ORDER);
105                 } else {
106                         $cnt = preg_match_all('/<(.*?)>/', $file, $matches, PREG_SET_ORDER);
107                 }
108
109                 if ($cnt) {
110                         foreach ($matches as $match) {
111                                 $return[] = self::decode($match[1]);
112                         }
113                 }
114
115                 return $return;
116         }
117 }