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