]> git.mxchange.org Git - friendica.git/blob - mod/subthread.php
Merge pull request #4455 from MrPetovan/task/4176-fix-undeclared-variables-2
[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\System;
9 use Friendica\Database\DBM;
10 use Friendica\Model\Item;
11
12 require_once 'include/security.php';
13 require_once 'include/bbcode.php';
14 require_once 'include/items.php';
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) ? notags(trim($a->argv[1])) : 0);
25
26         $r = q("SELECT * FROM `item` WHERE `parent` = '%s' OR `parent-uri` = '%s' and parent = id LIMIT 1",
27                 dbesc($item_id),
28                 dbesc($item_id)
29         );
30
31         if(! $item_id || (! DBM::is_result($r))) {
32                 logger('subthread: no item ' . $item_id);
33                 return;
34         }
35
36         $item = $r[0];
37
38         $owner_uid = $item['uid'];
39
40         if(! can_write_wall($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                 $r = q("SELECT * FROM `contact` WHERE `id` = %d AND `uid` = %d LIMIT 1",
49                         intval($item['contact-id']),
50                         intval($item['uid'])
51                 );
52                 if (! DBM::is_result($r)) {
53                         return;
54                 }
55                 if (! $r[0]['self']) {
56                         $remote_owner = $r[0];
57                 }
58         }
59
60         $owner = null;
61         // this represents the post owner on this system.
62
63         $r = q("SELECT `contact`.*, `user`.`nickname` FROM `contact` LEFT JOIN `user` ON `contact`.`uid` = `user`.`uid`
64                 WHERE `contact`.`self` = 1 AND `contact`.`uid` = %d LIMIT 1",
65                 intval($owner_uid)
66         );
67         if (DBM::is_result($r))
68                 $owner = $r[0];
69
70         if (! $owner) {
71                 logger('like: no owner');
72                 return;
73         }
74
75         if (! $remote_owner)
76                 $remote_owner = $owner;
77
78
79         $contact = null;
80         // This represents the person posting
81
82         if ((local_user()) && (local_user() == $owner_uid)) {
83                 $contact = $owner;
84         } else {
85                 $r = q("SELECT * FROM `contact` WHERE `id` = %d AND `uid` = %d LIMIT 1",
86                         intval($_SESSION['visitor_id']),
87                         intval($owner_uid)
88                 );
89                 if (DBM::is_result($r))
90                         $contact = $r[0];
91         }
92         if (! $contact) {
93                 return;
94         }
95
96         $uri = item_new_uri($a->get_hostname(),$owner_uid);
97
98         $post_type = (($item['resource-id']) ? L10n::t('photo') : L10n::t('status'));
99         $objtype = (($item['resource-id']) ? ACTIVITY_OBJ_IMAGE : ACTIVITY_OBJ_NOTE );
100         $link = xmlify('<link rel="alternate" type="text/html" href="' . System::baseUrl() . '/display/' . $owner['nickname'] . '/' . $item['id'] . '" />' . "\n") ;
101         $body = $item['body'];
102
103         $obj = <<< EOT
104
105         <object>
106                 <type>$objtype</type>
107                 <local>1</local>
108                 <id>{$item['uri']}</id>
109                 <link>$link</link>
110                 <title></title>
111                 <content>$body</content>
112         </object>
113 EOT;
114         $bodyverb = L10n::t('%1$s is following %2$s\'s %3$s');
115
116         if (! isset($bodyverb)) {
117                 return;
118         }
119
120         $arr = [];
121
122         $arr['guid'] = get_guid(32);
123         $arr['uri'] = $uri;
124         $arr['uid'] = $owner_uid;
125         $arr['contact-id'] = $contact['id'];
126         $arr['type'] = 'activity';
127         $arr['wall'] = $item['wall'];
128         $arr['origin'] = 1;
129         $arr['gravity'] = GRAVITY_LIKE;
130         $arr['parent'] = $item['id'];
131         $arr['parent-uri'] = $item['uri'];
132         $arr['thr-parent'] = $item['uri'];
133         $arr['owner-name'] = $remote_owner['name'];
134         $arr['owner-link'] = $remote_owner['url'];
135         $arr['owner-avatar'] = $remote_owner['thumb'];
136         $arr['author-name'] = $contact['name'];
137         $arr['author-link'] = $contact['url'];
138         $arr['author-avatar'] = $contact['thumb'];
139
140         $ulink = '[url=' . $contact['url'] . ']' . $contact['name'] . '[/url]';
141         $alink = '[url=' . $item['author-link'] . ']' . $item['author-name'] . '[/url]';
142         $plink = '[url=' . System::baseUrl() . '/display/' . $owner['nickname'] . '/' . $item['id'] . ']' . $post_type . '[/url]';
143         $arr['body'] =  sprintf( $bodyverb, $ulink, $alink, $plink );
144
145         $arr['verb'] = $activity;
146         $arr['object-type'] = $objtype;
147         $arr['object'] = $obj;
148         $arr['allow_cid'] = $item['allow_cid'];
149         $arr['allow_gid'] = $item['allow_gid'];
150         $arr['deny_cid'] = $item['deny_cid'];
151         $arr['deny_gid'] = $item['deny_gid'];
152         $arr['visible'] = 1;
153         $arr['unseen'] = 1;
154
155         $post_id = Item::insert($arr);
156
157         if (!$item['visible']) {
158                 Item::update(['visible' => true], ['id' => $item['id']]);
159         }
160
161         $arr['id'] = $post_id;
162
163         Addon::callHooks('post_local_end', $arr);
164
165         killme();
166
167 }
168
169