]> git.mxchange.org Git - friendica.git/blob - mod/like.php
Merge branch 'master' into newui
[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         $r = q("SELECT `contact`.*, `user`.`nickname` FROM `contact` LEFT JOIN `user` ON `contact`.`uid` = `user`.`uid`
59                 WHERE `contact`.`self` = 1 AND `contact`.`uid` = %d LIMIT 1",
60                 intval($owner_uid)
61         );
62         if(count($r))
63                 $owner = $r[0];
64
65         if(! $owner) {
66                 logger('like: no owner');
67                 return;
68         }
69
70
71         if((local_user()) && (local_user() == $owner_uid)) {
72                 $contact = $owner;
73         }
74         else {
75                 $r = q("SELECT * FROM `contact` WHERE `id` = %d AND `uid` = %d LIMIT 1",
76                         intval($_SESSION['visitor_id']),
77                         intval($owner_uid)
78                 );
79                 if(count($r))
80                         $contact = $r[0];
81         }
82         if(! $contact) {
83                 return;
84         }
85
86         $r = q("SELECT `id` FROM `item` WHERE `verb` = '%s' AND `deleted` = 0 
87                 AND `contact-id` = %d AND ( `parent` = '%s' OR `parent-uri` = '%s') LIMIT 1",
88                 dbesc($activity),
89                 intval($contact['id']),
90                 dbesc($item_id),
91                 dbesc($item_id)
92         );
93         if(count($r)) {
94                 // Already voted, undo it
95                 $r = q("UPDATE `item` SET `deleted` = 1, `changed` = '%s' WHERE `id` = %d LIMIT 1",
96                         dbesc(datetime_convert()),
97                         intval($r[0]['id'])
98                 );
99
100                 proc_run('php',"include/notifier.php","like","$post_id");
101                 return;
102         }
103
104         $uri = item_new_uri($a->get_hostname(),$owner_uid);
105
106         $post_type = (($item['resource-id']) ? t('photo') : t('status'));
107         $objtype = (($item['resource-id']) ? ACTIVITY_OBJ_PHOTO : ACTIVITY_OBJ_NOTE ); 
108         $link = xmlify('<link rel="alternate" type="text/html" href="' . $a->get_baseurl() . '/display/' . $owner['nickname'] . '/' . $item['id'] . '" />' . "\n") ;
109         $body = $item['body'];
110
111         $obj = <<< EOT
112
113         <object>
114                 <type>$objtype</type>
115                 <local>1</local>
116                 <id>{$item['uri']}</id>
117                 <link>$link</link>
118                 <title></title>
119                 <content>$body</content>
120         </object>
121 EOT;
122         if($verb === 'like')
123                 $bodyverb = t('%1$s likes %2$s\'s %3$s');
124         if($verb === 'dislike')
125                 $bodyverb = t('%1$s doesn\'t like %2$s\'s %3$s');
126
127         if(! isset($bodyverb))
128                         return; 
129
130         $arr = array();
131
132         $arr['uri'] = $uri;
133         $arr['uid'] = $owner_uid;
134         $arr['contact-id'] = $contact['id'];
135         $arr['type'] = 'activity';
136         $arr['wall'] = 1;
137         $arr['gravity'] = GRAVITY_LIKE;
138         $arr['parent'] = $item['id'];
139         $arr['parent-uri'] = $item['uri'];
140         $arr['owner-name'] = $owner['name'];
141         $arr['owner-link'] = $owner['url'];
142         $arr['owner-avatar'] = $owner['thumb'];
143         $arr['author-name'] = $contact['name'];
144         $arr['author-link'] = $contact['url'];
145         $arr['author-avatar'] = $contact['thumb'];
146         
147         $ulink = '[url=' . $contact['url'] . ']' . $contact['name'] . '[/url]';
148         $alink = '[url=' . $item['author-link'] . ']' . $item['author-name'] . '[/url]';
149         $plink = '[url=' . $a->get_baseurl() . '/display/' . $owner['nickname'] . '/' . $item['id'] . ']' . $post_type . '[/url]';
150         $arr['body'] =  sprintf( $bodyverb, $ulink, $alink, $plink );
151
152         $arr['verb'] = $activity;
153         $arr['object-type'] = $objtype;
154         $arr['object'] = $obj;
155         $arr['allow_cid'] = $item['allow_cid'];
156         $arr['allow_gid'] = $item['allow_gid'];
157         $arr['deny_cid'] = $item['deny_cid'];
158         $arr['deny_gid'] = $item['deny_gid'];
159         $arr['visible'] = 1;
160         $arr['unseen'] = 1;
161         $arr['last-child'] = 0;
162
163         $post_id = item_store($arr);    
164
165         if(! $item['visible']) {
166                 $r = q("UPDATE `item` SET `visible` = 1 WHERE `id` = %d AND `uid` = %d LIMIT 1",
167                         intval($item['id']),
168                         intval($owner_uid)
169                 );
170         }                       
171
172         $arr['id'] = $post_id;
173
174         call_hooks('post_local_end', $arr);
175
176         proc_run('php',"include/notifier.php","like","$post_id");
177
178         return; // NOTREACHED
179 }