3 require_once('include/security.php');
4 require_once('include/bbcode.php');
5 require_once('include/items.php');
8 function like_content(&$a) {
10 if(! local_user() && ! remote_user()) {
14 $verb = notags(trim($_GET['verb']));
23 $activity = ACTIVITY_LIKE;
27 $activity = ACTIVITY_DISLIKE;
31 $activity = ACTIVITY_AGREE;
35 $activity = ACTIVITY_DISAGREE;
39 $activity = ACTIVITY_ABSTAIN;
47 $item_id = (($a->argc > 1) ? notags(trim($a->argv[1])) : 0);
49 logger('like: verb ' . $verb . ' item ' . $item_id);
52 $r = q("SELECT * FROM `item` WHERE `id` = '%s' OR `uri` = '%s' LIMIT 1",
57 if(! $item_id || (! count($r))) {
58 logger('like: no item ' . $item_id);
64 $owner_uid = $item['uid'];
66 if(! can_write_wall($a,$owner_uid)) {
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']),
81 $remote_owner = $r[0];
84 // this represents the post owner on this system.
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",
94 logger('like: no owner');
99 $remote_owner = $owner;
102 // This represents the person posting
104 if((local_user()) && (local_user() == $owner_uid)) {
108 $r = q("SELECT * FROM `contact` WHERE `id` = %d AND `uid` = %d LIMIT 1",
109 intval($_SESSION['visitor_id']),
120 // See if we've been passed a return path to redirect to
121 $return_path = ((x($_REQUEST,'return')) ? $_REQUEST['return'] : '');
124 $r = q("SELECT `id`, `guid` FROM `item` WHERE `verb` = '%s' AND `deleted` = 0
125 AND `contact-id` = %d AND `uid` = %d
126 AND (`parent` = '%s' OR `parent-uri` = '%s' OR `thr-parent` = '%s') LIMIT 1",
127 dbesc($activity), intval($contact['id']), intval($owner_uid),
128 dbesc($item_id), dbesc($item_id), dbesc($item['uri'])
134 // Already voted, undo it
135 $r = q("UPDATE `item` SET `deleted` = 1, `unseen` = 1, `changed` = '%s' WHERE `id` = %d",
136 dbesc(datetime_convert()),
137 intval($like_item['id'])
141 // Clean up the Diaspora signatures for this like
142 // Go ahead and do it even if Diaspora support is disabled. We still want to clean up
143 // if it had been enabled in the past
144 $r = q("DELETE FROM `sign` WHERE `iid` = %d",
145 intval($like_item['id'])
148 // Save the author information for the unlike in case we need to relay to Diaspora
149 store_diaspora_like_retract_sig($activity, $item, $like_item, $contact);
151 // proc_run('php',"include/notifier.php","like","$post_id"); // $post_id isn't defined here!
152 $like_item_id = $like_item['id'];
153 proc_run('php',"include/notifier.php","like","$like_item_id");
155 like_content_return($a->get_baseurl(), $return_path);
156 return; // NOTREACHED
159 $uri = item_new_uri($a->get_hostname(),$owner_uid);
161 $post_type = (($item['resource-id']) ? t('photo') : t('status'));
162 $objtype = (($item['resource-id']) ? ACTIVITY_OBJ_PHOTO : ACTIVITY_OBJ_NOTE );
163 $link = xmlify('<link rel="alternate" type="text/html" href="' . $a->get_baseurl() . '/display/' . $owner['nickname'] . '/' . $item['id'] . '" />' . "\n") ;
164 $body = $item['body'];
169 <type>$objtype</type>
171 <id>{$item['uri']}</id>
174 <content>$body</content>
178 $bodyverb = t('%1$s likes %2$s\'s %3$s');
179 if($verb === 'dislike')
180 $bodyverb = t('%1$s doesn\'t like %2$s\'s %3$s');
181 if($verb === 'agree')
182 $bodyverb = t('%1$s agrees with %2$s\'s %3$s');
183 if($verb === 'disagree')
184 $bodyverb = t('%1$s doesn\'t agree with %2$s\'s %3$s');
185 if($verb === 'abstain')
186 $bodyverb = t('%1$s abstains from a decision on %2$s\'s %3$s');
188 if(! isset($bodyverb))
194 $arr['uid'] = $owner_uid;
195 $arr['contact-id'] = $contact['id'];
196 $arr['type'] = 'activity';
197 $arr['wall'] = $item['wall'];
199 $arr['gravity'] = GRAVITY_LIKE;
200 $arr['parent'] = $item['id'];
201 $arr['parent-uri'] = $item['uri'];
202 $arr['thr-parent'] = $item['uri'];
203 $arr['owner-name'] = $remote_owner['name'];
204 $arr['owner-link'] = $remote_owner['url'];
205 $arr['owner-avatar'] = $remote_owner['thumb'];
206 $arr['author-name'] = $contact['name'];
207 $arr['author-link'] = $contact['url'];
208 $arr['author-avatar'] = $contact['thumb'];
210 $ulink = '[url=' . $contact['url'] . ']' . $contact['name'] . '[/url]';
211 $alink = '[url=' . $item['author-link'] . ']' . $item['author-name'] . '[/url]';
212 $plink = '[url=' . $a->get_baseurl() . '/display/' . $owner['nickname'] . '/' . $item['id'] . ']' . $post_type . '[/url]';
213 $arr['body'] = sprintf( $bodyverb, $ulink, $alink, $plink );
215 $arr['verb'] = $activity;
216 $arr['object-type'] = $objtype;
217 $arr['object'] = $obj;
218 $arr['allow_cid'] = $item['allow_cid'];
219 $arr['allow_gid'] = $item['allow_gid'];
220 $arr['deny_cid'] = $item['deny_cid'];
221 $arr['deny_gid'] = $item['deny_gid'];
224 $arr['last-child'] = 0;
226 $post_id = item_store($arr);
228 if(! $item['visible']) {
229 $r = q("UPDATE `item` SET `visible` = 1 WHERE `id` = %d AND `uid` = %d",
236 // Save the author information for the like in case we need to relay to Diaspora
237 store_diaspora_like_sig($activity, $post_type, $contact, $post_id);
239 $arr['id'] = $post_id;
241 call_hooks('post_local_end', $arr);
243 proc_run('php',"include/notifier.php","like","$post_id");
245 like_content_return($a->get_baseurl(), $return_path);
246 killme(); // NOTREACHED
247 // return; // NOTREACHED
251 // Decide how to return. If we were called with a 'return' argument,
252 // then redirect back to the calling page. If not, just quietly end
254 function like_content_return($baseurl, $return_path) {
257 $rand = '_=' . time();
258 if(strpos($return_path, '?')) $rand = "&$rand";
259 else $rand = "?$rand";
261 goaway($baseurl . "/" . $return_path . $rand);
268 function store_diaspora_like_retract_sig($activity, $item, $like_item, $contact) {
269 // Note that we can only create a signature for a user of the local server. We don't have
270 // a key for remote users. That is ok, because if a remote user is "unlike"ing a post, it
271 // means we are the relay, and for relayable_retractions, Diaspora
272 // only checks the parent_author_signature if it doesn't have to relay further
274 // If $item['resource-id'] exists, it means the item is a photo. Diaspora doesn't support
275 // likes on photos, so don't bother.
277 $enabled = intval(get_config('system','diaspora_enabled'));
279 logger('mod_like: diaspora support disabled, not storing like retraction signature', LOGGER_DEBUG);
283 logger('mod_like: storing diaspora like retraction signature');
285 if(($activity === ACTIVITY_LIKE) && (! $item['resource-id'])) {
286 $signed_text = $like_item['guid'] . ';' . 'Like';
288 // Only works for NETWORK_DFRN
289 $contact_baseurl_start = strpos($contact['url'],'://') + 3;
290 $contact_baseurl_length = strpos($contact['url'],'/profile') - $contact_baseurl_start;
291 $contact_baseurl = substr($contact['url'], $contact_baseurl_start, $contact_baseurl_length);
292 $diaspora_handle = $contact['nick'] . '@' . $contact_baseurl;
294 // Get contact's private key if he's a user of the local Friendica server
295 $r = q("SELECT `contact`.`uid` FROM `contact` WHERE `url` = '%s' AND `self` = 1 LIMIT 1",
296 dbesc($contact['url'])
300 $contact_uid = $r['uid'];
301 $r = q("SELECT prvkey FROM user WHERE uid = %d LIMIT 1",
306 $authorsig = base64_encode(rsa_sign($signed_text,$r['prvkey'],'sha256'));
309 if(! isset($authorsig))
312 q("insert into sign (`retract_iid`,`signed_text`,`signature`,`signer`) values (%d,'%s','%s','%s') ",
313 intval($like_item['id']),
316 dbesc($diaspora_handle)
323 function store_diaspora_like_sig($activity, $post_type, $contact, $post_id) {
324 // Note that we can only create a signature for a user of the local server. We don't have
325 // a key for remote users. That is ok, because if a remote user is "unlike"ing a post, it
326 // means we are the relay, and for relayable_retractions, Diaspora
327 // only checks the parent_author_signature if it doesn't have to relay further
329 $enabled = intval(get_config('system','diaspora_enabled'));
331 logger('mod_like: diaspora support disabled, not storing like signature', LOGGER_DEBUG);
335 logger('mod_like: storing diaspora like signature');
337 if(($activity === ACTIVITY_LIKE) && ($post_type === t('status'))) {
338 // Only works for NETWORK_DFRN
339 $contact_baseurl_start = strpos($contact['url'],'://') + 3;
340 $contact_baseurl_length = strpos($contact['url'],'/profile') - $contact_baseurl_start;
341 $contact_baseurl = substr($contact['url'], $contact_baseurl_start, $contact_baseurl_length);
342 $diaspora_handle = $contact['nick'] . '@' . $contact_baseurl;
344 // Get contact's private key if he's a user of the local Friendica server
345 $r = q("SELECT `contact`.`uid` FROM `contact` WHERE `url` = '%s' AND `self` = 1 LIMIT 1",
346 dbesc($contact['url'])
350 $contact_uid = $r['uid'];
351 $r = q("SELECT prvkey FROM user WHERE uid = %d LIMIT 1",
356 $contact_uprvkey = $r['prvkey'];
359 $r = q("SELECT guid, parent FROM `item` WHERE id = %d LIMIT 1",
363 $p = q("SELECT guid FROM `item` WHERE id = %d AND parent = %d LIMIT 1",
364 intval($r[0]['parent']),
365 intval($r[0]['parent'])
368 $signed_text = $r[0]['guid'] . ';Post;' . $p[0]['guid'] . ';true;' . $diaspora_handle;
370 if(isset($contact_uprvkey))
371 $authorsig = base64_encode(rsa_sign($signed_text,$contact_uprvkey,'sha256'));
375 q("insert into sign (`iid`,`signed_text`,`signature`,`signer`) values (%d,'%s','%s','%s') ",
379 dbesc($diaspora_handle)