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