]> git.mxchange.org Git - friendica.git/blob - mod/subthread.php
Merge pull request #8265 from nupplaphil/task/add_license
[friendica.git] / mod / subthread.php
1 <?php
2 /**
3  * @copyright Copyright (C) 2020, Friendica
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 use Friendica\App;
23 use Friendica\Core\Hook;
24 use Friendica\Core\Logger;
25 use Friendica\Core\Session;
26 use Friendica\Core\System;
27 use Friendica\Database\DBA;
28 use Friendica\DI;
29 use Friendica\Model\Item;
30 use Friendica\Protocol\Activity;
31 use Friendica\Util\Security;
32 use Friendica\Util\Strings;
33 use Friendica\Util\XML;
34
35 function subthread_content(App $a) {
36
37         if (!Session::isAuthenticated()) {
38                 return;
39         }
40
41         $activity = Activity::FOLLOW;
42
43         $item_id = (($a->argc > 1) ? Strings::escapeTags(trim($a->argv[1])) : 0);
44
45         $condition = ["`parent` = ? OR `parent-uri` = ? AND `parent` = `id`", $item_id, $item_id];
46         $item = Item::selectFirst([], $condition);
47
48         if (empty($item_id) || !DBA::isResult($item)) {
49                 Logger::log('subthread: no item ' . $item_id);
50                 return;
51         }
52
53         $owner_uid = $item['uid'];
54
55         if (!Security::canWriteToUserWall($owner_uid)) {
56                 return;
57         }
58
59         $remote_owner = null;
60
61         if (!$item['wall']) {
62                 // The top level post may have been written by somebody on another system
63                 $contact = DBA::selectFirst('contact', [], ['id' => $item['contact-id'], 'uid' => $item['uid']]);
64                 if (!DBA::isResult($contact)) {
65                         return;
66                 }
67                 if (!$contact['self']) {
68                         $remote_owner = $contact;
69                 }
70         }
71
72         $owner = null;
73         // this represents the post owner on this system.
74
75         $r = q("SELECT `contact`.*, `user`.`nickname` FROM `contact` LEFT JOIN `user` ON `contact`.`uid` = `user`.`uid`
76                 WHERE `contact`.`self` = 1 AND `contact`.`uid` = %d LIMIT 1",
77                 intval($owner_uid)
78         );
79
80         if (DBA::isResult($r)) {
81                 $owner = $r[0];
82         }
83
84         if (!$owner) {
85                 Logger::log('like: no owner');
86                 return;
87         }
88
89         if (!$remote_owner) {
90                 $remote_owner = $owner;
91         }
92
93         $contact = null;
94         // This represents the person posting
95
96         if (local_user() && (local_user() == $owner_uid)) {
97                 $contact = $owner;
98         } else {
99                 $contact = DBA::selectFirst('contact', [], ['id' => $_SESSION['visitor_id'], 'uid' => $owner_uid]);
100                 if (!DBA::isResult($contact)) {
101                         return;
102                 }
103         }
104
105         $uri = Item::newURI($owner_uid);
106
107         $post_type = (($item['resource-id']) ? DI::l10n()->t('photo') : DI::l10n()->t('status'));
108         $objtype = (($item['resource-id']) ? Activity\ObjectType::IMAGE : Activity\ObjectType::NOTE );
109         $link = XML::escape('<link rel="alternate" type="text/html" href="' . DI::baseUrl() . '/display/' . $item['guid'] . '" />' . "\n");
110         $body = $item['body'];
111
112         $obj = <<< EOT
113
114         <object>
115                 <type>$objtype</type>
116                 <local>1</local>
117                 <id>{$item['uri']}</id>
118                 <link>$link</link>
119                 <title></title>
120                 <content>$body</content>
121         </object>
122 EOT;
123         $bodyverb = DI::l10n()->t('%1$s is following %2$s\'s %3$s');
124
125         if (!isset($bodyverb)) {
126                 return;
127         }
128
129         $arr = [];
130
131         $arr['guid'] = System::createUUID();
132         $arr['uri'] = $uri;
133         $arr['uid'] = $owner_uid;
134         $arr['contact-id'] = $contact['id'];
135         $arr['wall'] = $item['wall'];
136         $arr['origin'] = 1;
137         $arr['gravity'] = GRAVITY_ACTIVITY;
138         $arr['parent'] = $item['id'];
139         $arr['parent-uri'] = $item['uri'];
140         $arr['thr-parent'] = $item['uri'];
141         $arr['owner-name'] = $remote_owner['name'];
142         $arr['owner-link'] = $remote_owner['url'];
143         $arr['owner-avatar'] = $remote_owner['thumb'];
144         $arr['author-name'] = $contact['name'];
145         $arr['author-link'] = $contact['url'];
146         $arr['author-avatar'] = $contact['thumb'];
147
148         $ulink = '[url=' . $contact['url'] . ']' . $contact['name'] . '[/url]';
149         $alink = '[url=' . $item['author-link'] . ']' . $item['author-name'] . '[/url]';
150         $plink = '[url=' . DI::baseUrl() . '/display/' . $item['guid'] . ']' . $post_type . '[/url]';
151         $arr['body'] =  sprintf( $bodyverb, $ulink, $alink, $plink );
152
153         $arr['verb'] = $activity;
154         $arr['object-type'] = $objtype;
155         $arr['object'] = $obj;
156         $arr['allow_cid'] = $item['allow_cid'];
157         $arr['allow_gid'] = $item['allow_gid'];
158         $arr['deny_cid'] = $item['deny_cid'];
159         $arr['deny_gid'] = $item['deny_gid'];
160         $arr['visible'] = 1;
161         $arr['unseen'] = 1;
162
163         $post_id = Item::insert($arr);
164
165         if (!$item['visible']) {
166                 Item::update(['visible' => true], ['id' => $item['id']]);
167         }
168
169         $arr['id'] = $post_id;
170
171         Hook::callAll('post_local_end', $arr);
172
173         exit();
174
175 }