]> git.mxchange.org Git - friendica.git/blob - mod/like.php
Merge pull request #927 from annando/master
[friendica.git] / mod / like.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 like_content(&$a) {
9
10         if(! local_user() && ! remote_user()) {
11                 return;
12         }
13
14         $verb = notags(trim($_GET['verb']));
15
16         if(! $verb)
17                 $verb = 'like';
18
19
20         switch($verb) {
21                 case 'like':
22                 case 'unlike':
23                         $activity = ACTIVITY_LIKE;
24                         break;
25                 case 'dislike':
26                 case 'undislike':
27                         $activity = ACTIVITY_DISLIKE;
28                         break;
29                 default:
30                         return;
31                         break;
32         }
33
34
35         $item_id = (($a->argc > 1) ? notags(trim($a->argv[1])) : 0);
36
37         logger('like: verb ' . $verb . ' item ' . $item_id);
38
39
40         $r = q("SELECT * FROM `item` WHERE `id` = '%s' OR `uri` = '%s' LIMIT 1",
41                 dbesc($item_id),
42                 dbesc($item_id)
43         );
44
45         if(! $item_id || (! count($r))) {
46                 logger('like: no item ' . $item_id);
47                 return;
48         }
49
50         $item = $r[0];
51
52         $owner_uid = $item['uid'];
53
54         if(! can_write_wall($a,$owner_uid)) {
55                 return;
56         }
57
58         $remote_owner = null;
59
60         if(! $item['wall']) {
61                 // The top level post may have been written by somebody on another system
62                 $r = q("SELECT * FROM `contact` WHERE `id` = %d AND `uid` = %d LIMIT 1",
63                         intval($item['contact-id']),
64                         intval($item['uid'])
65                 );
66                 if(! count($r))
67                         return;
68                 if(! $r[0]['self'])
69                         $remote_owner = $r[0];
70         }
71
72         // this represents the post owner on this system. 
73
74         $r = q("SELECT `contact`.*, `user`.`nickname` FROM `contact` LEFT JOIN `user` ON `contact`.`uid` = `user`.`uid`
75                 WHERE `contact`.`self` = 1 AND `contact`.`uid` = %d LIMIT 1",
76                 intval($owner_uid)
77         );
78         if(count($r))
79                 $owner = $r[0];
80
81         if(! $owner) {
82                 logger('like: no owner');
83                 return;
84         }
85
86         if(! $remote_owner)
87                 $remote_owner = $owner;
88
89
90         // This represents the person posting
91
92         if((local_user()) && (local_user() == $owner_uid)) {
93                 $contact = $owner;
94         }
95         else {
96                 $r = q("SELECT * FROM `contact` WHERE `id` = %d AND `uid` = %d LIMIT 1",
97                         intval($_SESSION['visitor_id']),
98                         intval($owner_uid)
99                 );
100                 if(count($r))
101                         $contact = $r[0];
102         }
103         if(! $contact) {
104                 return;
105         }
106
107
108         // See if we've been passed a return path to redirect to
109         $return_path = ((x($_REQUEST,'return')) ? $_REQUEST['return'] : '');
110
111
112         $r = q("SELECT * FROM `item` WHERE `verb` = '%s' AND `deleted` = 0
113                 AND `contact-id` = %d AND ( `parent` = '%s' OR `parent-uri` = '%s' OR `thr-parent` = '%s') LIMIT 1",
114                 dbesc($activity),
115                 intval($contact['id']),
116                 dbesc($item_id),
117                 dbesc($item_id),
118                 dbesc($item['uri'])
119         );
120         if(count($r)) {
121                 $like_item = $r[0];
122
123                 // Already voted, undo it
124                 $r = q("UPDATE `item` SET `deleted` = 1, `unseen` = 1, `changed` = '%s' WHERE `id` = %d",
125                         dbesc(datetime_convert()),
126                         intval($like_item['id'])
127                 );
128
129
130                 // Clean up the Diaspora signatures for this like
131                 // Go ahead and do it even if Diaspora support is disabled. We still want to clean up
132                 // if it had been enabled in the past
133                 $r = q("DELETE FROM `sign` WHERE `iid` = %d",
134                         intval($like_item['id'])
135                 );
136
137                 // Save the author information for the unlike in case we need to relay to Diaspora
138                 store_diaspora_like_retract_sig($activity, $item, $like_item, $contact);
139
140
141 //              proc_run('php',"include/notifier.php","like","$post_id"); // $post_id isn't defined here!
142                 $like_item_id = $like_item['id'];
143                 proc_run('php',"include/notifier.php","like","$like_item_id");
144
145                 like_content_return($a->get_baseurl(), $return_path);
146                 return; // NOTREACHED
147         }
148
149         $uri = item_new_uri($a->get_hostname(),$owner_uid);
150
151         $post_type = (($item['resource-id']) ? t('photo') : t('status'));
152         $objtype = (($item['resource-id']) ? ACTIVITY_OBJ_PHOTO : ACTIVITY_OBJ_NOTE ); 
153         $link = xmlify('<link rel="alternate" type="text/html" href="' . $a->get_baseurl() . '/display/' . $owner['nickname'] . '/' . $item['id'] . '" />' . "\n") ;
154         $body = $item['body'];
155
156         $obj = <<< EOT
157
158         <object>
159                 <type>$objtype</type>
160                 <local>1</local>
161                 <id>{$item['uri']}</id>
162                 <link>$link</link>
163                 <title></title>
164                 <content>$body</content>
165         </object>
166 EOT;
167         if($verb === 'like')
168                 $bodyverb = t('%1$s likes %2$s\'s %3$s');
169         if($verb === 'dislike')
170                 $bodyverb = t('%1$s doesn\'t like %2$s\'s %3$s');
171
172         if(! isset($bodyverb))
173                         return; 
174
175         $arr = array();
176
177         $arr['uri'] = $uri;
178         $arr['uid'] = $owner_uid;
179         $arr['contact-id'] = $contact['id'];
180         $arr['type'] = 'activity';
181         $arr['wall'] = $item['wall'];
182         $arr['origin'] = 1;
183         $arr['gravity'] = GRAVITY_LIKE;
184         $arr['parent'] = $item['id'];
185         $arr['parent-uri'] = $item['uri'];
186         $arr['thr-parent'] = $item['uri'];
187         $arr['owner-name'] = $remote_owner['name'];
188         $arr['owner-link'] = $remote_owner['url'];
189         $arr['owner-avatar'] = $remote_owner['thumb'];
190         $arr['author-name'] = $contact['name'];
191         $arr['author-link'] = $contact['url'];
192         $arr['author-avatar'] = $contact['thumb'];
193         
194         $ulink = '[url=' . $contact['url'] . ']' . $contact['name'] . '[/url]';
195         $alink = '[url=' . $item['author-link'] . ']' . $item['author-name'] . '[/url]';
196         $plink = '[url=' . $a->get_baseurl() . '/display/' . $owner['nickname'] . '/' . $item['id'] . ']' . $post_type . '[/url]';
197         $arr['body'] =  sprintf( $bodyverb, $ulink, $alink, $plink );
198
199         $arr['verb'] = $activity;
200         $arr['object-type'] = $objtype;
201         $arr['object'] = $obj;
202         $arr['allow_cid'] = $item['allow_cid'];
203         $arr['allow_gid'] = $item['allow_gid'];
204         $arr['deny_cid'] = $item['deny_cid'];
205         $arr['deny_gid'] = $item['deny_gid'];
206         $arr['visible'] = 1;
207         $arr['unseen'] = 1;
208         $arr['last-child'] = 0;
209
210         $post_id = item_store($arr);
211
212         if(! $item['visible']) {
213                 $r = q("UPDATE `item` SET `visible` = 1 WHERE `id` = %d AND `uid` = %d",
214                         intval($item['id']),
215                         intval($owner_uid)
216                 );
217         }
218
219
220         // Save the author information for the like in case we need to relay to Diaspora
221         store_diaspora_like_sig($activity, $post_type, $contact, $post_id);
222
223
224         $arr['id'] = $post_id;
225
226         call_hooks('post_local_end', $arr);
227
228         proc_run('php',"include/notifier.php","like","$post_id");
229
230         like_content_return($a->get_baseurl(), $return_path);
231         killme(); // NOTREACHED
232 //      return; // NOTREACHED
233 }
234
235
236 // Decide how to return. If we were called with a 'return' argument,
237 // then redirect back to the calling page. If not, just quietly end
238
239 function like_content_return($baseurl, $return_path) {
240
241         if($return_path) {
242                 $rand = '_=' . time();
243                 if(strpos($return_path, '?')) $rand = "&$rand";
244                 else $rand = "?$rand";
245
246                 goaway($baseurl . "/" . $return_path . $rand);
247         }
248
249         killme();
250 }
251
252
253 function store_diaspora_like_retract_sig($activity, $item, $like_item, $contact) {
254         // Note that we can only create a signature for a user of the local server. We don't have
255         // a key for remote users. That is ok, because if a remote user is "unlike"ing a post, it 
256         // means we are the relay, and for relayable_retractions, Diaspora
257         // only checks the parent_author_signature if it doesn't have to relay further
258         //
259         // If $item['resource-id'] exists, it means the item is a photo. Diaspora doesn't support
260         // likes on photos, so don't bother.
261
262         $enabled = intval(get_config('system','diaspora_enabled'));
263         if(! $enabled) {
264                 logger('mod_like: diaspora support disabled, not storing like retraction signature', LOGGER_DEBUG);
265                 return;
266         }
267
268         logger('mod_like: storing diaspora like retraction signature');
269
270         if(($activity === ACTIVITY_LIKE) && (! $item['resource-id'])) {
271                 $signed_text = $like_item['guid'] . ';' . 'Like';
272
273                 // Only works for NETWORK_DFRN
274                 $contact_baseurl_start = strpos($contact['url'],'://') + 3;
275                 $contact_baseurl_length = strpos($contact['url'],'/profile') - $contact_baseurl_start;
276                 $contact_baseurl = substr($contact['url'], $contact_baseurl_start, $contact_baseurl_length);
277                 $diaspora_handle = $contact['nick'] . '@' . $contact_baseurl;
278
279                 // Get contact's private key if he's a user of the local Friendica server
280                 $r = q("SELECT `contact`.`uid` FROM `contact` WHERE `url` = '%s' AND `self` = 1 LIMIT 1",
281                         dbesc($contact['url'])
282                 );
283
284                 if( $r) {
285                         $contact_uid = $r['uid'];
286                         $r = q("SELECT prvkey FROM user WHERE uid = %d LIMIT 1",
287                                 intval($contact_uid)
288                         );
289
290                         if( $r)
291                                 $authorsig = base64_encode(rsa_sign($signed_text,$r['prvkey'],'sha256'));
292                 }
293
294                 if(! isset($authorsig))
295                         $authorsig = '';
296
297                 q("insert into sign (`retract_iid`,`signed_text`,`signature`,`signer`) values (%d,'%s','%s','%s') ",
298                         intval($like_item['id']),
299                         dbesc($signed_text),
300                         dbesc($authorsig),
301                         dbesc($diaspora_handle)
302                 );
303         }
304
305         return;
306 }
307
308 function store_diaspora_like_sig($activity, $post_type, $contact, $post_id) {
309         // Note that we can only create a signature for a user of the local server. We don't have
310         // a key for remote users. That is ok, because if a remote user is "unlike"ing a post, it 
311         // means we are the relay, and for relayable_retractions, Diaspora
312         // only checks the parent_author_signature if it doesn't have to relay further
313
314         $enabled = intval(get_config('system','diaspora_enabled'));
315         if(! $enabled) {
316                 logger('mod_like: diaspora support disabled, not storing like signature', LOGGER_DEBUG);
317                 return;
318         }
319
320         logger('mod_like: storing diaspora like signature');
321
322         if(($activity === ACTIVITY_LIKE) && ($post_type === t('status'))) {
323                 // Only works for NETWORK_DFRN
324                 $contact_baseurl_start = strpos($contact['url'],'://') + 3;
325                 $contact_baseurl_length = strpos($contact['url'],'/profile') - $contact_baseurl_start;
326                 $contact_baseurl = substr($contact['url'], $contact_baseurl_start, $contact_baseurl_length);
327                 $diaspora_handle = $contact['nick'] . '@' . $contact_baseurl;
328
329                 // Get contact's private key if he's a user of the local Friendica server
330                 $r = q("SELECT `contact`.`uid` FROM `contact` WHERE `url` = '%s' AND `self` = 1 LIMIT 1",
331                         dbesc($contact['url'])
332                 );
333
334                 if( $r) {
335                         $contact_uid = $r['uid'];
336                         $r = q("SELECT prvkey FROM user WHERE uid = %d LIMIT 1",
337                                 intval($contact_uid)
338                         );
339
340                         if( $r)
341                                 $contact_uprvkey = $r['prvkey'];
342                 }
343
344                 $r = q("SELECT guid, parent FROM `item` WHERE id = %d LIMIT 1",
345                         intval($post_id)
346                 );
347                 if( $r) {
348                         $p = q("SELECT guid FROM `item` WHERE id = %d AND parent = %d LIMIT 1",
349                                 intval($r[0]['parent']),
350                                 intval($r[0]['parent'])
351                         );
352                         if( $p) {
353                                 $signed_text = $r[0]['guid'] . ';Post;' . $p[0]['guid'] . ';true;' . $diaspora_handle;
354
355                                 if(isset($contact_uprvkey))
356                                         $authorsig = base64_encode(rsa_sign($signed_text,$contact_uprvkey,'sha256'));
357                                 else
358                                         $authorsig = '';
359
360                                 q("insert into sign (`iid`,`signed_text`,`signature`,`signer`) values (%d,'%s','%s','%s') ",
361                                         intval($post_id),
362                                         dbesc($signed_text),
363                                         dbesc($authorsig),
364                                         dbesc($diaspora_handle)
365                                 );
366                         }
367                 }
368         }
369
370         return;
371 }