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