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;
26 use Friendica\Model\Post\Category;
29 * This class handles FileTag related functions
31 * post categories and "save to file" use the same item.file table for storage.
32 * We will differentiate the different uses by wrapping categories in angle brackets
33 * and save to file categories in square brackets.
34 * To do this we need to escape these characters if they appear in our tag.
39 * URL encode <, >, left and right brackets
41 * @param string $s String to be URL encoded.
43 * @return string The URL encoded string.
45 public static function encode($s)
47 return str_replace(['<', '>', '[', ']'], ['%3c', '%3e', '%5b', '%5d'], $s);
51 * URL decode <, >, left and right brackets
53 * @param string $s The URL encoded string to be decoded
55 * @return string The decoded string.
57 public static function decode($s)
59 return str_replace(['%3c', '%3e', '%5b', '%5d'], ['<', '>', '[', ']'], $s);
65 * @param string $table The table to be queired.
66 * @param string $s The search term
67 * @param string $type Optional file type.
69 * @return string Query string.
71 public static function fileQuery($table, $s, $type = 'file')
73 if ($type == 'file') {
74 $str = preg_quote('[' . str_replace('%', '%%', self::encode($s)) . ']');
76 $str = preg_quote('<' . str_replace('%', '%%', self::encode($s)) . '>');
79 return " AND " . (($table) ? DBA::escape($table) . '.' : '') . "file regexp '" . DBA::escape($str) . "' ";
83 * Get file tags from array
85 * ex. given [music,video] return <music><video> or [music][video]
87 * @param array $array A list of tags.
88 * @param string $type Optional file type.
90 * @return string A list of file tags.
92 public static function arrayToFile(array $array, string $type = 'file')
95 if ($type == 'file') {
103 foreach ($array as $item) {
105 $tag_list .= $lbracket . self::encode(trim($item)) . $rbracket;
113 * Get tag list from file tags
115 * ex. given <music><video>[friends], return [music,video] or [friends]
117 * @param string $file File tags
118 * @param string $type Optional file type.
120 * @return array List of tag names.
122 public static function fileToArray(string $file, string $type = 'file')
127 if ($type == 'file') {
128 $cnt = preg_match_all('/\[(.*?)\]/', $file, $matches, PREG_SET_ORDER);
130 $cnt = preg_match_all('/<(.*?)>/', $file, $matches, PREG_SET_ORDER);
134 foreach ($matches as $match) {
135 $return[] = self::decode($match[1]);
143 * Get file tags from list
145 * ex. given music,video return <music><video> or [music][video]
146 * @param string $list A comma delimited list of tags.
147 * @param string $type Optional file type.
149 * @return string A list of file tags.
150 * @deprecated since 2019.06 use arrayToFile() instead
152 public static function listToFile(string $list, string $type = 'file')
154 $list_array = explode(',', $list);
156 return self::arrayToFile($list_array, $type);
160 * Get list from file tags
162 * ex. given <music><video>[friends], return music,video or friends
163 * @param string $file File tags
164 * @param string $type Optional file type.
166 * @return string Comma delimited list of tag names.
167 * @deprecated since 2019.06 use fileToArray() instead
169 public static function fileToList(string $file, $type = 'file')
171 return implode(',', self::fileToArray($file, $type));
175 * Update file tags in PConfig
177 * @param int $uid Unique Identity.
178 * @param string $file_old Categories previously associated with an item
179 * @param string $file_new New list of categories for an item
180 * @param string $type Optional file type.
182 * @return boolean A value indicating success or failure.
185 public static function updatePconfig(int $uid, string $file_old, string $file_new, string $type = 'file')
189 } elseif ($file_old == $file_new) {
193 $saved = DI::pConfig()->get($uid, 'system', 'filetags');
195 if (strlen($saved)) {
196 if ($type == 'file') {
199 $termtype = Category::FILE;
203 $termtype = Category::CATEGORY;
206 $filetags_updated = $saved;
208 // check for new tags to be added as filetags in pconfig
210 foreach (self::fileToArray($file_new, $type) as $tag) {
211 if (!stristr($saved, $lbracket . self::encode($tag) . $rbracket)) {
216 $filetags_updated .= self::arrayToFile($new_tags, $type);
218 // check for deleted tags to be removed from filetags in pconfig
220 foreach (self::fileToArray($file_old, $type) as $tag) {
221 if (!stristr($file_new, $lbracket . self::encode($tag) . $rbracket)) {
222 $deleted_tags[] = $tag;
226 foreach ($deleted_tags as $key => $tag) {
227 if (DBA::exists('category-view', ['name' => $tag, 'type' => $termtype, 'uid' => $uid])) {
228 unset($deleted_tags[$key]);
230 $filetags_updated = str_replace($lbracket . self::encode($tag) . $rbracket, '', $filetags_updated);
234 if ($saved != $filetags_updated) {
235 DI::pConfig()->set($uid, 'system', 'filetags', $filetags_updated);
239 } elseif (strlen($file_new)) {
240 DI::pConfig()->set($uid, 'system', 'filetags', $file_new);
249 * @param int $uid Unique identity.
250 * @param int $item_id Item identity.
251 * @param string $file File tag.
253 * @return boolean A value indicating success or failure.
254 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
256 public static function saveFile($uid, $item_id, $file)
262 $item = Item::selectFirst(['file'], ['id' => $item_id, 'uid' => $uid]);
263 if (DBA::isResult($item)) {
264 if (!stristr($item['file'], '[' . self::encode($file) . ']')) {
265 $fields = ['file' => $item['file'] . '[' . self::encode($file) . ']'];
266 Item::update($fields, ['id' => $item_id]);
269 $saved = DI::pConfig()->get($uid, 'system', 'filetags');
271 if (!strlen($saved) || !stristr($saved, '[' . self::encode($file) . ']')) {
272 DI::pConfig()->set($uid, 'system', 'filetags', $saved . '[' . self::encode($file) . ']');
275 info(DI::l10n()->t('Item filed'));
282 * Remove tag from file
284 * @param int $uid Unique identity.
285 * @param int $item_id Item identity.
286 * @param string $file File tag.
287 * @param boolean $cat Optional value indicating the term type (i.e. Category or File)
289 * @return boolean A value indicating success or failure.
290 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
292 public static function unsaveFile($uid, $item_id, $file, $cat = false)
299 $pattern = '<' . self::encode($file) . '>';
300 $termtype = Category::CATEGORY;
302 $pattern = '[' . self::encode($file) . ']';
303 $termtype = Category::FILE;
306 $item = Item::selectFirst(['file'], ['id' => $item_id, 'uid' => $uid]);
308 if (!DBA::isResult($item)) {
312 $fields = ['file' => str_replace($pattern, '', $item['file'])];
314 Item::update($fields, ['id' => $item_id]);
316 if (!DBA::exists('category-view', ['name' => $file, 'type' => $termtype, 'uid' => $uid])) {
317 $saved = DI::pConfig()->get($uid, 'system', 'filetags');
318 DI::pConfig()->set($uid, 'system', 'filetags', str_replace($pattern, '', $saved));