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