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