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