]> git.mxchange.org Git - friendica.git/blob - mod/like.php
Merge pull request #1042 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 `id`, `guid` FROM `item` WHERE `verb` = '%s' AND `deleted` = 0
113                 AND `contact-id` = %d AND `uid` = %d
114                 AND (`parent` = '%s' OR `parent-uri` = '%s' OR `thr-parent` = '%s') LIMIT 1",
115                 dbesc($activity), intval($contact['id']), intval($owner_uid),
116                 dbesc($item_id), dbesc($item_id), dbesc($item['uri'])
117         );
118
119         if(count($r)) {
120                 $like_item = $r[0];
121
122                 // Already voted, undo it
123                 $r = q("UPDATE `item` SET `deleted` = 1, `unseen` = 1, `changed` = '%s' WHERE `id` = %d",
124                         dbesc(datetime_convert()),
125                         intval($like_item['id'])
126                 );
127
128
129                 // Clean up the Diaspora signatures for this like
130                 // Go ahead and do it even if Diaspora support is disabled. We still want to clean up
131                 // if it had been enabled in the past
132                 $r = q("DELETE FROM `sign` WHERE `iid` = %d",
133                         intval($like_item['id'])
134                 );
135
136                 // Save the author information for the unlike in case we need to relay to Diaspora
137                 store_diaspora_like_retract_sig($activity, $item, $like_item, $contact);
138
139                 // if no auto update is enabled, then disable it temporarily
140                 if (get_pconfig($owner_uid, "system", "no_auto_update") == 1)
141                         set_pconfig($owner_uid, "system", "no_auto_update", -1);
142
143 //              proc_run('php',"include/notifier.php","like","$post_id"); // $post_id isn't defined here!
144                 $like_item_id = $like_item['id'];
145                 proc_run('php',"include/notifier.php","like","$like_item_id");
146
147                 like_content_return($a->get_baseurl(), $return_path);
148                 return; // NOTREACHED
149         }
150
151         $uri = item_new_uri($a->get_hostname(),$owner_uid);
152
153         $post_type = (($item['resource-id']) ? t('photo') : t('status'));
154         $objtype = (($item['resource-id']) ? ACTIVITY_OBJ_PHOTO : ACTIVITY_OBJ_NOTE );
155         $link = xmlify('<link rel="alternate" type="text/html" href="' . $a->get_baseurl() . '/display/' . $owner['nickname'] . '/' . $item['id'] . '" />' . "\n") ;
156         $body = $item['body'];
157
158         $obj = <<< EOT
159
160         <object>
161                 <type>$objtype</type>
162                 <local>1</local>
163                 <id>{$item['uri']}</id>
164                 <link>$link</link>
165                 <title></title>
166                 <content>$body</content>
167         </object>
168 EOT;
169         if($verb === 'like')
170                 $bodyverb = t('%1$s likes %2$s\'s %3$s');
171         if($verb === 'dislike')
172                 $bodyverb = t('%1$s doesn\'t like %2$s\'s %3$s');
173
174         if(! isset($bodyverb))
175                         return; 
176
177         $arr = array();
178
179         $arr['uri'] = $uri;
180         $arr['uid'] = $owner_uid;
181         $arr['contact-id'] = $contact['id'];
182         $arr['type'] = 'activity';
183         $arr['wall'] = $item['wall'];
184         $arr['origin'] = 1;
185         $arr['gravity'] = GRAVITY_LIKE;
186         $arr['parent'] = $item['id'];
187         $arr['parent-uri'] = $item['uri'];
188         $arr['thr-parent'] = $item['uri'];
189         $arr['owner-name'] = $remote_owner['name'];
190         $arr['owner-link'] = $remote_owner['url'];
191         $arr['owner-avatar'] = $remote_owner['thumb'];
192         $arr['author-name'] = $contact['name'];
193         $arr['author-link'] = $contact['url'];
194         $arr['author-avatar'] = $contact['thumb'];
195         
196         $ulink = '[url=' . $contact['url'] . ']' . $contact['name'] . '[/url]';
197         $alink = '[url=' . $item['author-link'] . ']' . $item['author-name'] . '[/url]';
198         $plink = '[url=' . $a->get_baseurl() . '/display/' . $owner['nickname'] . '/' . $item['id'] . ']' . $post_type . '[/url]';
199         $arr['body'] =  sprintf( $bodyverb, $ulink, $alink, $plink );
200
201         $arr['verb'] = $activity;
202         $arr['object-type'] = $objtype;
203         $arr['object'] = $obj;
204         $arr['allow_cid'] = $item['allow_cid'];
205         $arr['allow_gid'] = $item['allow_gid'];
206         $arr['deny_cid'] = $item['deny_cid'];
207         $arr['deny_gid'] = $item['deny_gid'];
208         $arr['visible'] = 1;
209         $arr['unseen'] = 1;
210         $arr['last-child'] = 0;
211
212         $post_id = item_store($arr);
213
214         if(! $item['visible']) {
215                 $r = q("UPDATE `item` SET `visible` = 1 WHERE `id` = %d AND `uid` = %d",
216                         intval($item['id']),
217                         intval($owner_uid)
218                 );
219         }
220
221
222         // Save the author information for the like in case we need to relay to Diaspora
223         store_diaspora_like_sig($activity, $post_type, $contact, $post_id);
224
225         // if no auto update is enabled, then disable it temporarily
226         if (get_pconfig($owner_uid, "system", "no_auto_update") == 1)
227                 set_pconfig($owner_uid, "system", "no_auto_update", -1);
228
229         $arr['id'] = $post_id;
230
231         call_hooks('post_local_end', $arr);
232
233         proc_run('php',"include/notifier.php","like","$post_id");
234
235         like_content_return($a->get_baseurl(), $return_path);
236         killme(); // NOTREACHED
237 //      return; // NOTREACHED
238 }
239
240
241 // Decide how to return. If we were called with a 'return' argument,
242 // then redirect back to the calling page. If not, just quietly end
243
244 function like_content_return($baseurl, $return_path) {
245
246         if($return_path) {
247                 $rand = '_=' . time();
248                 if(strpos($return_path, '?')) $rand = "&$rand";
249                 else $rand = "?$rand";
250
251                 goaway($baseurl . "/" . $return_path . $rand);
252         }
253
254         killme();
255 }
256
257
258 function store_diaspora_like_retract_sig($activity, $item, $like_item, $contact) {
259         // Note that we can only create a signature for a user of the local server. We don't have
260         // a key for remote users. That is ok, because if a remote user is "unlike"ing a post, it 
261         // means we are the relay, and for relayable_retractions, Diaspora
262         // only checks the parent_author_signature if it doesn't have to relay further
263         //
264         // If $item['resource-id'] exists, it means the item is a photo. Diaspora doesn't support
265         // likes on photos, so don't bother.
266
267         $enabled = intval(get_config('system','diaspora_enabled'));
268         if(! $enabled) {
269                 logger('mod_like: diaspora support disabled, not storing like retraction signature', LOGGER_DEBUG);
270                 return;
271         }
272
273         logger('mod_like: storing diaspora like retraction signature');
274
275         if(($activity === ACTIVITY_LIKE) && (! $item['resource-id'])) {
276                 $signed_text = $like_item['guid'] . ';' . 'Like';
277
278                 // Only works for NETWORK_DFRN
279                 $contact_baseurl_start = strpos($contact['url'],'://') + 3;
280                 $contact_baseurl_length = strpos($contact['url'],'/profile') - $contact_baseurl_start;
281                 $contact_baseurl = substr($contact['url'], $contact_baseurl_start, $contact_baseurl_length);
282                 $diaspora_handle = $contact['nick'] . '@' . $contact_baseurl;
283
284                 // Get contact's private key if he's a user of the local Friendica server
285                 $r = q("SELECT `contact`.`uid` FROM `contact` WHERE `url` = '%s' AND `self` = 1 LIMIT 1",
286                         dbesc($contact['url'])
287                 );
288
289                 if( $r) {
290                         $contact_uid = $r['uid'];
291                         $r = q("SELECT prvkey FROM user WHERE uid = %d LIMIT 1",
292                                 intval($contact_uid)
293                         );
294
295                         if( $r)
296                                 $authorsig = base64_encode(rsa_sign($signed_text,$r['prvkey'],'sha256'));
297                 }
298
299                 if(! isset($authorsig))
300                         $authorsig = '';
301
302                 q("insert into sign (`retract_iid`,`signed_text`,`signature`,`signer`) values (%d,'%s','%s','%s') ",
303                         intval($like_item['id']),
304                         dbesc($signed_text),
305                         dbesc($authorsig),
306                         dbesc($diaspora_handle)
307                 );
308         }
309
310         return;
311 }
312
313 function store_diaspora_like_sig($activity, $post_type, $contact, $post_id) {
314         // Note that we can only create a signature for a user of the local server. We don't have
315         // a key for remote users. That is ok, because if a remote user is "unlike"ing a post, it 
316         // means we are the relay, and for relayable_retractions, Diaspora
317         // only checks the parent_author_signature if it doesn't have to relay further
318
319         $enabled = intval(get_config('system','diaspora_enabled'));
320         if(! $enabled) {
321                 logger('mod_like: diaspora support disabled, not storing like signature', LOGGER_DEBUG);
322                 return;
323         }
324
325         logger('mod_like: storing diaspora like signature');
326
327         if(($activity === ACTIVITY_LIKE) && ($post_type === t('status'))) {
328                 // Only works for NETWORK_DFRN
329                 $contact_baseurl_start = strpos($contact['url'],'://') + 3;
330                 $contact_baseurl_length = strpos($contact['url'],'/profile') - $contact_baseurl_start;
331                 $contact_baseurl = substr($contact['url'], $contact_baseurl_start, $contact_baseurl_length);
332                 $diaspora_handle = $contact['nick'] . '@' . $contact_baseurl;
333
334                 // Get contact's private key if he's a user of the local Friendica server
335                 $r = q("SELECT `contact`.`uid` FROM `contact` WHERE `url` = '%s' AND `self` = 1 LIMIT 1",
336                         dbesc($contact['url'])
337                 );
338
339                 if( $r) {
340                         $contact_uid = $r['uid'];
341                         $r = q("SELECT prvkey FROM user WHERE uid = %d LIMIT 1",
342                                 intval($contact_uid)
343                         );
344
345                         if( $r)
346                                 $contact_uprvkey = $r['prvkey'];
347                 }
348
349                 $r = q("SELECT guid, parent FROM `item` WHERE id = %d LIMIT 1",
350                         intval($post_id)
351                 );
352                 if( $r) {
353                         $p = q("SELECT guid FROM `item` WHERE id = %d AND parent = %d LIMIT 1",
354                                 intval($r[0]['parent']),
355                                 intval($r[0]['parent'])
356                         );
357                         if( $p) {
358                                 $signed_text = $r[0]['guid'] . ';Post;' . $p[0]['guid'] . ';true;' . $diaspora_handle;
359
360                                 if(isset($contact_uprvkey))
361                                         $authorsig = base64_encode(rsa_sign($signed_text,$contact_uprvkey,'sha256'));
362                                 else
363                                         $authorsig = '';
364
365                                 q("insert into sign (`iid`,`signed_text`,`signature`,`signer`) values (%d,'%s','%s','%s') ",
366                                         intval($post_id),
367                                         dbesc($signed_text),
368                                         dbesc($authorsig),
369                                         dbesc($diaspora_handle)
370                                 );
371                         }
372                 }
373         }
374
375         return;
376 }