]> git.mxchange.org Git - friendica.git/blob - mod/like.php
Some Bugfixes, and variable checks
[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         $r = q("SELECT `id` FROM `item` WHERE `verb` = '%s' AND `deleted` = 0 
108                 AND `contact-id` = %d AND ( `parent` = '%s' OR `parent-uri` = '%s') LIMIT 1",
109                 dbesc($activity),
110                 intval($contact['id']),
111                 dbesc($item_id),
112                 dbesc($item_id)
113         );
114         if(count($r)) {
115                 // Already voted, undo it
116                 $r = q("UPDATE `item` SET `deleted` = 1, `changed` = '%s' WHERE `id` = %d LIMIT 1",
117                         dbesc(datetime_convert()),
118                         intval($r[0]['id'])
119                 );
120
121                 proc_run('php',"include/notifier.php","like","$post_id");
122                 return;
123         }
124
125         $uri = item_new_uri($a->get_hostname(),$owner_uid);
126
127         $post_type = (($item['resource-id']) ? t('photo') : t('status'));
128         $objtype = (($item['resource-id']) ? ACTIVITY_OBJ_PHOTO : ACTIVITY_OBJ_NOTE ); 
129         $link = xmlify('<link rel="alternate" type="text/html" href="' . $a->get_baseurl() . '/display/' . $owner['nickname'] . '/' . $item['id'] . '" />' . "\n") ;
130         $body = $item['body'];
131
132         $obj = <<< EOT
133
134         <object>
135                 <type>$objtype</type>
136                 <local>1</local>
137                 <id>{$item['uri']}</id>
138                 <link>$link</link>
139                 <title></title>
140                 <content>$body</content>
141         </object>
142 EOT;
143         if($verb === 'like')
144                 $bodyverb = t('%1$s likes %2$s\'s %3$s');
145         if($verb === 'dislike')
146                 $bodyverb = t('%1$s doesn\'t like %2$s\'s %3$s');
147
148         if(! isset($bodyverb))
149                         return; 
150
151         $arr = array();
152
153         $arr['uri'] = $uri;
154         $arr['uid'] = $owner_uid;
155         $arr['contact-id'] = $contact['id'];
156         $arr['type'] = 'activity';
157         $arr['wall'] = $item['wall'];
158         $arr['origin'] = 1;
159         $arr['gravity'] = GRAVITY_LIKE;
160         $arr['parent'] = $item['id'];
161         $arr['parent-uri'] = $item['uri'];
162         $arr['owner-name'] = $remote_owner['name'];
163         $arr['owner-link'] = $remote_owner['url'];
164         $arr['owner-avatar'] = $remote_owner['thumb'];
165         $arr['author-name'] = $contact['name'];
166         $arr['author-link'] = $contact['url'];
167         $arr['author-avatar'] = $contact['thumb'];
168         
169         $ulink = '[url=' . $contact['url'] . ']' . $contact['name'] . '[/url]';
170         $alink = '[url=' . $item['author-link'] . ']' . $item['author-name'] . '[/url]';
171         $plink = '[url=' . $a->get_baseurl() . '/display/' . $owner['nickname'] . '/' . $item['id'] . ']' . $post_type . '[/url]';
172         $arr['body'] =  sprintf( $bodyverb, $ulink, $alink, $plink );
173
174         $arr['verb'] = $activity;
175         $arr['object-type'] = $objtype;
176         $arr['object'] = $obj;
177         $arr['allow_cid'] = $item['allow_cid'];
178         $arr['allow_gid'] = $item['allow_gid'];
179         $arr['deny_cid'] = $item['deny_cid'];
180         $arr['deny_gid'] = $item['deny_gid'];
181         $arr['visible'] = 1;
182         $arr['unseen'] = 1;
183         $arr['last-child'] = 0;
184
185         $post_id = item_store($arr);    
186
187         if(! $item['visible']) {
188                 $r = q("UPDATE `item` SET `visible` = 1 WHERE `id` = %d AND `uid` = %d LIMIT 1",
189                         intval($item['id']),
190                         intval($owner_uid)
191                 );
192         }                       
193
194         $arr['id'] = $post_id;
195
196         call_hooks('post_local_end', $arr);
197
198         proc_run('php',"include/notifier.php","like","$post_id");
199
200         killme();
201 //      return; // NOTREACHED
202 }