]> git.mxchange.org Git - friendica.git/blob - src/Content/Item.php
Merge pull request #10575 from MrPetovan/bug/10019-peertube-embed
[friendica.git] / src / Content / Item.php
1 <?php
2 /**
3  * @copyright Copyright (C) 2010-2021, 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\Content;
23
24 use Friendica\Database\DBA;
25 use Friendica\Model\Contact;
26 use Friendica\Model\Tag;
27 use Friendica\Model\Post;
28
29 /**
30  * A content helper class for displaying items
31  */
32 class Item
33 {
34         /**
35          * Return array with details for categories and folders for an item
36          *
37          * @param array $item
38          * @return [array, array]
39          *
40          * [
41          *      [ // categories array
42          *          {
43          *               'name': 'category name',
44          *               'removeurl': 'url to remove this category',
45          *               'first': 'is the first in this array? true/false',
46          *               'last': 'is the last in this array? true/false',
47          *           } ,
48          *           ....
49          *       ],
50          *       [ //folders array
51          *                      {
52          *               'name': 'folder name',
53          *               'removeurl': 'url to remove this folder',
54          *               'first': 'is the first in this array? true/false',
55          *               'last': 'is the last in this array? true/false',
56          *           } ,
57          *           ....
58          *       ]
59          *  ]
60          */
61         public function determineCategoriesTerms(array $item)
62         {
63                 $categories = [];
64                 $folders = [];
65                 $first = true;
66
67                 foreach (Post\Category::getArrayByURIId($item['uri-id'], $item['uid'], Post\Category::CATEGORY) as $savedFolderName) {
68                         if (!empty($item['author-link'])) {
69                                 $url = $item['author-link'] . "?category=" . rawurlencode($savedFolderName);
70                         } else {
71                                 $url = '#';
72                         }
73                         $categories[] = [
74                                 'name' => $savedFolderName,
75                                 'url' => $url,
76                                 'removeurl' => ((local_user() == $item['uid']) ? 'filerm/' . $item['id'] . '?cat=' . rawurlencode($savedFolderName) : ""),
77                                 'first' => $first,
78                                 'last' => false
79                         ];
80                         $first = false;
81                 }
82
83                 if (count($categories)) {
84                         $categories[count($categories) - 1]['last'] = true;
85                 }
86
87                 if (local_user() == $item['uid']) {
88                         foreach (Post\Category::getArrayByURIId($item['uri-id'], $item['uid'], Post\Category::FILE) as $savedFolderName) {
89                                 $folders[] = [
90                                         'name' => $savedFolderName,
91                                         'url' => "#",
92                                         'removeurl' => ((local_user() == $item['uid']) ? 'filerm/' . $item['id'] . '?term=' . rawurlencode($savedFolderName) : ""),
93                                         'first' => $first,
94                                         'last' => false
95                                 ];
96                                 $first = false;
97                         }
98                 }
99
100                 if (count($folders)) {
101                         $folders[count($folders) - 1]['last'] = true;
102                 }
103
104                 return [$categories, $folders];
105         }
106
107         /**
108          * This function removes the tag $tag from the text $body and replaces it with
109          * the appropriate link.
110          *
111          * @param string  $body        the text to replace the tag in
112          * @param string  $inform      a comma-seperated string containing everybody to inform
113          * @param integer $profile_uid the user id to replace the tag for (0 = anyone)
114          * @param string  $tag         the tag to replace
115          * @param string  $network     The network of the post
116          *
117          * @return array|bool ['replaced' => $replaced, 'contact' => $contact];
118          * @throws \Friendica\Network\HTTPException\InternalServerErrorException
119          * @throws \ImagickException
120          */
121         public static function replaceTag(&$body, &$inform, $profile_uid, $tag, $network = '')
122         {
123                 $replaced = false;
124
125                 //is it a person tag?
126                 if (Tag::isType($tag, Tag::MENTION, Tag::IMPLICIT_MENTION, Tag::EXCLUSIVE_MENTION)) {
127                         $tag_type = substr($tag, 0, 1);
128                         //is it already replaced?
129                         if (strpos($tag, '[url=')) {
130                                 return $replaced;
131                         }
132
133                         //get the person's name
134                         $name = substr($tag, 1);
135
136                         // Sometimes the tag detection doesn't seem to work right
137                         // This is some workaround
138                         $nameparts = explode(' ', $name);
139                         $name = $nameparts[0];
140
141                         // Try to detect the contact in various ways
142                         if (strpos($name, 'http://') || strpos($name, '@')) {
143                                 $contact = Contact::getByURLForUser($name, $profile_uid);
144                         } else {
145                                 $contact = false;
146                                 $fields = ['id', 'url', 'nick', 'name', 'alias', 'network', 'forum', 'prv'];
147
148                                 if (strrpos($name, '+')) {
149                                         // Is it in format @nick+number?
150                                         $tagcid = intval(substr($name, strrpos($name, '+') + 1));
151                                         $contact = DBA::selectFirst('contact', $fields, ['id' => $tagcid, 'uid' => $profile_uid]);
152                                 }
153
154                                 // select someone by nick in the current network
155                                 if (!DBA::isResult($contact) && ($network != '')) {
156                                         $condition = ["`nick` = ? AND `network` = ? AND `uid` = ?",
157                                                 $name, $network, $profile_uid];
158                                         $contact = DBA::selectFirst('contact', $fields, $condition);
159                                 }
160
161                                 // select someone by attag in the current network
162                                 if (!DBA::isResult($contact) && ($network != '')) {
163                                         $condition = ["`attag` = ? AND `network` = ? AND `uid` = ?",
164                                                 $name, $network, $profile_uid];
165                                         $contact = DBA::selectFirst('contact', $fields, $condition);
166                                 }
167
168                                 //select someone by name in the current network
169                                 if (!DBA::isResult($contact) && ($network != '')) {
170                                         $condition = ['name' => $name, 'network' => $network, 'uid' => $profile_uid];
171                                         $contact = DBA::selectFirst('contact', $fields, $condition);
172                                 }
173
174                                 // select someone by nick in any network
175                                 if (!DBA::isResult($contact)) {
176                                         $condition = ["`nick` = ? AND `uid` = ?", $name, $profile_uid];
177                                         $contact = DBA::selectFirst('contact', $fields, $condition);
178                                 }
179
180                                 // select someone by attag in any network
181                                 if (!DBA::isResult($contact)) {
182                                         $condition = ["`attag` = ? AND `uid` = ?", $name, $profile_uid];
183                                         $contact = DBA::selectFirst('contact', $fields, $condition);
184                                 }
185
186                                 // select someone by name in any network
187                                 if (!DBA::isResult($contact)) {
188                                         $condition = ['name' => $name, 'uid' => $profile_uid];
189                                         $contact = DBA::selectFirst('contact', $fields, $condition);
190                                 }
191                         }
192
193                         // Check if $contact has been successfully loaded
194                         if (DBA::isResult($contact)) {
195                                 if (strlen($inform) && (isset($contact['notify']) || isset($contact['id']))) {
196                                         $inform .= ',';
197                                 }
198
199                                 if (isset($contact['id'])) {
200                                         $inform .= 'cid:' . $contact['id'];
201                                 } elseif (isset($contact['notify'])) {
202                                         $inform  .= $contact['notify'];
203                                 }
204
205                                 $profile = $contact['url'];
206                                 $newname = ($contact['name'] ?? '') ?: $contact['nick'];
207                         }
208
209                         //if there is an url for this persons profile
210                         if (isset($profile) && ($newname != '')) {
211                                 $replaced = true;
212                                 // create profile link
213                                 $profile = str_replace(',', '%2c', $profile);
214                                 $newtag = $tag_type.'[url=' . $profile . ']' . $newname . '[/url]';
215                                 $body = str_replace($tag_type . $name, $newtag, $body);
216                         }
217                 }
218
219                 return ['replaced' => $replaced, 'contact' => $contact];
220         }
221 }