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