]> git.mxchange.org Git - friendica.git/blob - mod/subthread.php
Revert "Coding convention applied - part 1"
[friendica.git] / mod / subthread.php
1 <?php
2
3 require_once('include/security.php');
4 require_once('include/bbcode.php');
5 require_once('include/items.php');
6
7
8 function subthread_content(App $a) {
9
10         if(! local_user() && ! remote_user()) {
11                 return;
12         }
13
14         $activity = ACTIVITY_FOLLOW;
15
16         $item_id = (($a->argc > 1) ? notags(trim($a->argv[1])) : 0);
17
18         $r = q("SELECT * FROM `item` WHERE `parent` = '%s' OR `parent-uri` = '%s' and parent = id LIMIT 1",
19                 dbesc($item_id),
20                 dbesc($item_id)
21         );
22
23         if(! $item_id || (! dbm::is_result($r))) {
24                 logger('subthread: no item ' . $item_id);
25                 return;
26         }
27
28         $item = $r[0];
29
30         $owner_uid = $item['uid'];
31
32         if(! can_write_wall($a,$owner_uid)) {
33                 return;
34         }
35
36         $remote_owner = null;
37
38         if(! $item['wall']) {
39                 // The top level post may have been written by somebody on another system
40                 $r = q("SELECT * FROM `contact` WHERE `id` = %d AND `uid` = %d LIMIT 1",
41                         intval($item['contact-id']),
42                         intval($item['uid'])
43                 );
44                 if (! dbm::is_result($r)) {
45                         return;
46                 }
47                 if (! $r[0]['self']) {
48                         $remote_owner = $r[0];
49                 }
50         }
51
52         // this represents the post owner on this system.
53
54         $r = q("SELECT `contact`.*, `user`.`nickname` FROM `contact` LEFT JOIN `user` ON `contact`.`uid` = `user`.`uid`
55                 WHERE `contact`.`self` = 1 AND `contact`.`uid` = %d LIMIT 1",
56                 intval($owner_uid)
57         );
58         if (dbm::is_result($r))
59                 $owner = $r[0];
60
61         if (! $owner) {
62                 logger('like: no owner');
63                 return;
64         }
65
66         if (! $remote_owner)
67                 $remote_owner = $owner;
68
69
70         // This represents the person posting
71
72         if ((local_user()) && (local_user() == $owner_uid)) {
73                 $contact = $owner;
74         } else {
75                 $r = q("SELECT * FROM `contact` WHERE `id` = %d AND `uid` = %d LIMIT 1",
76                         intval($_SESSION['visitor_id']),
77                         intval($owner_uid)
78                 );
79                 if (dbm::is_result($r))
80                         $contact = $r[0];
81         }
82         if (! $contact) {
83                 return;
84         }
85
86         $uri = item_new_uri($a->get_hostname(),$owner_uid);
87
88         $post_type = (($item['resource-id']) ? t('photo') : t('status'));
89         $objtype = (($item['resource-id']) ? ACTIVITY_OBJ_IMAGE : ACTIVITY_OBJ_NOTE );
90         $link = xmlify('<link rel="alternate" type="text/html" href="' . App::get_baseurl() . '/display/' . $owner['nickname'] . '/' . $item['id'] . '" />' . "\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 = t('%1$s is following %2$s\'s %3$s');
105
106         if (! isset($bodyverb)) {
107                 return;
108         }
109
110         $arr = array();
111
112         $arr['guid'] = get_guid(32);
113         $arr['uri'] = $uri;
114         $arr['uid'] = $owner_uid;
115         $arr['contact-id'] = $contact['id'];
116         $arr['type'] = 'activity';
117         $arr['wall'] = $item['wall'];
118         $arr['origin'] = 1;
119         $arr['gravity'] = GRAVITY_LIKE;
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=' . App::get_baseurl() . '/display/' . $owner['nickname'] . '/' . $item['id'] . ']' . $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         $arr['last-child'] = 0;
145
146         $post_id = item_store($arr);
147
148         if (! $item['visible']) {
149                 $r = q("UPDATE `item` SET `visible` = 1 WHERE `id` = %d AND `uid` = %d",
150                         intval($item['id']),
151                         intval($owner_uid)
152                 );
153         }
154
155         $arr['id'] = $post_id;
156
157         call_hooks('post_local_end', $arr);
158
159         killme();
160
161 }
162
163