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