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;
24 use Friendica\Database\DBA;
28 * This class handles FileTag related functions
30 * post categories and "save to file" use the same item.file table for storage.
31 * We will differentiate the different uses by wrapping categories in angle brackets
32 * and save to file categories in square brackets.
33 * To do this we need to escape these characters if they appear in our tag.
38 * URL encode <, >, left and right brackets
40 * @param string $s String to be URL encoded.
42 * @return string The URL encoded string.
44 public static function encode($s)
46 return str_replace(['<', '>', '[', ']'], ['%3c', '%3e', '%5b', '%5d'], $s);
50 * URL decode <, >, left and right brackets
52 * @param string $s The URL encoded string to be decoded
54 * @return string The decoded string.
56 public static function decode($s)
58 return str_replace(['%3c', '%3e', '%5b', '%5d'], ['<', '>', '[', ']'], $s);
64 * @param string $table The table to be queired.
65 * @param string $s The search term
66 * @param string $type Optional file type.
68 * @return string Query string.
70 public static function fileQuery($table, $s, $type = 'file')
72 if ($type == 'file') {
73 $str = preg_quote('[' . str_replace('%', '%%', self::encode($s)) . ']');
75 $str = preg_quote('<' . str_replace('%', '%%', self::encode($s)) . '>');
78 return " AND " . (($table) ? DBA::escape($table) . '.' : '') . "file regexp '" . DBA::escape($str) . "' ";
82 * Get file tags from array
84 * ex. given [music,video] return <music><video> or [music][video]
86 * @param array $array A list of tags.
87 * @param string $type Optional file type.
89 * @return string A list of file tags.
91 public static function arrayToFile(array $array, string $type = 'file')
94 if ($type == 'file') {
102 foreach ($array as $item) {
104 $tag_list .= $lbracket . self::encode(trim($item)) . $rbracket;
112 * Get tag list from file tags
114 * ex. given <music><video>[friends], return [music,video] or [friends]
116 * @param string $file File tags
117 * @param string $type Optional file type.
119 * @return array List of tag names.
121 public static function fileToArray(string $file, string $type = 'file')
126 if ($type == 'file') {
127 $cnt = preg_match_all('/\[(.*?)\]/', $file, $matches, PREG_SET_ORDER);
129 $cnt = preg_match_all('/<(.*?)>/', $file, $matches, PREG_SET_ORDER);
133 foreach ($matches as $match) {
134 $return[] = self::decode($match[1]);
142 * Get file tags from list
144 * ex. given music,video return <music><video> or [music][video]
145 * @param string $list A comma delimited list of tags.
146 * @param string $type Optional file type.
148 * @return string A list of file tags.
149 * @deprecated since 2019.06 use arrayToFile() instead
151 public static function listToFile(string $list, string $type = 'file')
153 $list_array = explode(',', $list);
155 return self::arrayToFile($list_array, $type);
159 * Get list from file tags
161 * ex. given <music><video>[friends], return music,video or friends
162 * @param string $file File tags
163 * @param string $type Optional file type.
165 * @return string Comma delimited list of tag names.
166 * @deprecated since 2019.06 use fileToArray() instead
168 public static function fileToList(string $file, $type = 'file')
170 return implode(',', self::fileToArray($file, $type));
174 * Update file tags in PConfig
176 * @param int $uid Unique Identity.
177 * @param string $file_old Categories previously associated with an item
178 * @param string $file_new New list of categories for an item
179 * @param string $type Optional file type.
181 * @return boolean A value indicating success or failure.
184 public static function updatePconfig(int $uid, string $file_old, string $file_new, string $type = 'file')
188 } elseif ($file_old == $file_new) {
192 $saved = DI::pConfig()->get($uid, 'system', 'filetags');
194 if (strlen($saved)) {
195 if ($type == 'file') {
198 $termtype = TERM_FILE;
202 $termtype = TERM_CATEGORY;
205 $filetags_updated = $saved;
207 // check for new tags to be added as filetags in pconfig
209 foreach (self::fileToArray($file_new, $type) as $tag) {
210 if (!stristr($saved, $lbracket . self::encode($tag) . $rbracket)) {
215 $filetags_updated .= self::arrayToFile($new_tags, $type);
217 // check for deleted tags to be removed from filetags in pconfig
219 foreach (self::fileToArray($file_old, $type) as $tag) {
220 if (!stristr($file_new, $lbracket . self::encode($tag) . $rbracket)) {
221 $deleted_tags[] = $tag;
225 foreach ($deleted_tags as $key => $tag) {
226 $r = q("SELECT `oid` FROM `term` WHERE `term` = '%s' AND `otype` = %d AND `type` = %d AND `uid` = %d",
228 intval(Term::OBJECT_TYPE_POST),
232 if (DBA::isResult($r)) {
233 unset($deleted_tags[$key]);
235 $filetags_updated = str_replace($lbracket . self::encode($tag) . $rbracket, '', $filetags_updated);
239 if ($saved != $filetags_updated) {
240 DI::pConfig()->set($uid, 'system', 'filetags', $filetags_updated);
244 } elseif (strlen($file_new)) {
245 DI::pConfig()->set($uid, 'system', 'filetags', $file_new);
254 * @param int $uid Unique identity.
255 * @param int $item_id Item identity.
256 * @param string $file File tag.
258 * @return boolean A value indicating success or failure.
259 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
261 public static function saveFile($uid, $item_id, $file)
267 $item = Item::selectFirst(['file'], ['id' => $item_id, 'uid' => $uid]);
268 if (DBA::isResult($item)) {
269 if (!stristr($item['file'], '[' . self::encode($file) . ']')) {
270 $fields = ['file' => $item['file'] . '[' . self::encode($file) . ']'];
271 Item::update($fields, ['id' => $item_id]);
274 $saved = DI::pConfig()->get($uid, 'system', 'filetags');
276 if (!strlen($saved) || !stristr($saved, '[' . self::encode($file) . ']')) {
277 DI::pConfig()->set($uid, 'system', 'filetags', $saved . '[' . self::encode($file) . ']');
280 info(DI::l10n()->t('Item filed'));
287 * Remove tag from file
289 * @param int $uid Unique identity.
290 * @param int $item_id Item identity.
291 * @param string $file File tag.
292 * @param boolean $cat Optional value indicating the term type (i.e. Category or File)
294 * @return boolean A value indicating success or failure.
295 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
297 public static function unsaveFile($uid, $item_id, $file, $cat = false)
304 $pattern = '<' . self::encode($file) . '>';
305 $termtype = Term::CATEGORY;
307 $pattern = '[' . self::encode($file) . ']';
308 $termtype = Term::FILE;
311 $item = Item::selectFirst(['file'], ['id' => $item_id, 'uid' => $uid]);
313 if (!DBA::isResult($item)) {
317 $fields = ['file' => str_replace($pattern, '', $item['file'])];
319 Item::update($fields, ['id' => $item_id]);
321 $r = q("SELECT `oid` FROM `term` WHERE `term` = '%s' AND `otype` = %d AND `type` = %d AND `uid` = %d",
323 intval(Term::OBJECT_TYPE_POST),
328 if (!DBA::isResult($r)) {
329 $saved = DI::pConfig()->get($uid, 'system', 'filetags');
330 DI::pConfig()->set($uid, 'system', 'filetags', str_replace($pattern, '', $saved));