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