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