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