]> git.mxchange.org Git - friendica.git/blob - mod/poke.php
Merge pull request #2094 from annando/1511-api
[friendica.git] / mod / poke.php
1 <?php /** @file */
2
3 /**
4  *
5  * Poke, prod, finger, or otherwise do unspeakable things to somebody - who must be a connection in your address book
6  * This function can be invoked with the required arguments (verb and cid and private and possibly parent) silently via ajax or
7  * other web request. You must be logged in and connected to a profile. 
8  * If the required arguments aren't present, we'll display a simple form to choose a recipient and a verb.
9  * parent is a special argument which let's you attach this activity as a comment to an existing conversation, which
10  * may have started with somebody else poking (etc.) somebody, but this isn't necessary. This can be used in the more pokes
11  * plugin version to have entire conversations where Alice poked Bob, Bob fingered Alice, Alice hugged Bob, etc.  
12  *
13  * private creates a private conversation with the recipient. Otherwise your profile's default post privacy is used.
14  *
15  */
16
17 require_once('include/security.php');
18 require_once('include/bbcode.php');
19 require_once('include/items.php');
20
21
22 function poke_init(&$a) {
23
24         if(! local_user())
25                 return;
26
27         $uid = local_user();
28         $verb = notags(trim($_GET['verb']));
29
30         if(! $verb)
31                 return;
32
33         $verbs = get_poke_verbs();
34
35         if(! array_key_exists($verb,$verbs))
36                 return;
37
38         $activity = ACTIVITY_POKE . '#' . urlencode($verbs[$verb][0]);
39
40         $contact_id = intval($_GET['cid']);
41         if(! $contact_id)
42                 return;
43
44         $parent = ((x($_GET,'parent')) ? intval($_GET['parent']) : 0);
45
46
47         logger('poke: verb ' . $verb . ' contact ' . $contact_id, LOGGER_DEBUG);
48
49
50         $r = q("SELECT * FROM `contact` WHERE `id` = %d AND `uid` = %d LIMIT 1",
51                 intval($contact_id),
52                 intval($uid)
53         );
54
55         if(! count($r)) {
56                 logger('poke: no contact ' . $contact_id);
57                 return;
58         }
59
60         $target = $r[0];
61
62         if($parent) {
63                 $r = q("SELECT `uri`, `private`, `allow_cid`, `allow_gid`, `deny_cid`, `deny_gid`
64                         FROM `item` WHERE `id` = %d AND `parent` = %d AND `uid` = %d LIMIT 1",
65                         intval($parent),
66                         intval($parent),
67                         intval($uid)
68                 );
69                 if(count($r)) {
70                         $parent_uri = $r[0]['uri'];
71                         $private    = $r[0]['private'];
72                         $allow_cid  = $r[0]['allow_cid'];
73                         $allow_gid  = $r[0]['allow_gid'];
74                         $deny_cid   = $r[0]['deny_cid'];
75                         $deny_gid   = $r[0]['deny_gid'];
76                 }
77         }
78         else {
79
80                 $private = ((x($_GET,'private')) ? intval($_GET['private']) : 0);
81
82                 $allow_cid     = (($private) ? '<' . $target['id']. '>' : $a->user['allow_cid']);
83                 $allow_gid     = (($private) ? '' : $a->user['allow_gid']);
84                 $deny_cid      = (($private) ? '' : $a->user['deny_cid']);
85                 $deny_gid      = (($private) ? '' : $a->user['deny_gid']);
86         }
87
88         $poster = $a->contact;
89
90         $uri = item_new_uri($a->get_hostname(),$uid);
91
92         $arr = array();
93
94         $arr['uid']           = $uid;
95         $arr['uri']           = $uri;
96         $arr['parent-uri']    = (($parent_uri) ? $parent_uri : $uri);
97         $arr['type']          = 'activity';
98         $arr['wall']          = 1;
99         $arr['contact-id']    = $poster['id'];
100         $arr['owner-name']    = $poster['name'];
101         $arr['owner-link']    = $poster['url'];
102         $arr['owner-avatar']  = $poster['thumb'];
103         $arr['author-name']   = $poster['name'];
104         $arr['author-link']   = $poster['url'];
105         $arr['author-avatar'] = $poster['thumb'];
106         $arr['title']         = '';
107         $arr['allow_cid']     = $allow_cid;
108         $arr['allow_gid']     = $allow_gid;
109         $arr['deny_cid']      = $deny_cid;
110         $arr['deny_gid']      = $deny_gid;
111         $arr['last-child']    = 1;
112         $arr['visible']       = 1;
113         $arr['verb']          = $activity;
114         $arr['private']       = $private;
115         $arr['object-type']   = ACTIVITY_OBJ_PERSON;
116
117         $arr['origin']        = 1;
118         $arr['body']          = '[url=' . $poster['url'] . ']' . $poster['name'] . '[/url]' . ' ' . t($verbs[$verb][0]) . ' ' . '[url=' . $target['url'] . ']' . $target['name'] . '[/url]';
119
120         $arr['object'] = '<object><type>' . ACTIVITY_OBJ_PERSON . '</type><title>' . $target['name'] . '</title><id>' . $a->get_baseurl() . '/contact/' . $target['id'] . '</id>';
121         $arr['object'] .= '<link>' . xmlify('<link rel="alternate" type="text/html" href="' . $target['url'] . '" />' . "\n");
122
123         $arr['object'] .= xmlify('<link rel="photo" type="image/jpeg" href="' . $target['photo'] . '" />' . "\n");
124         $arr['object'] .= '</link></object>' . "\n";
125
126         $item_id = item_store($arr);
127         if($item_id) {
128                 //q("UPDATE `item` SET `plink` = '%s' WHERE `uid` = %d AND `id` = %d",
129                 //      dbesc($a->get_baseurl() . '/display/' . $poster['nickname'] . '/' . $item_id),
130                 //      intval($uid),
131                 //      intval($item_id)
132                 //);
133                 proc_run('php',"include/notifier.php","tag","$item_id");
134         }
135
136
137         call_hooks('post_local_end', $arr);
138
139         proc_run('php',"include/notifier.php","like","$post_id");
140
141         return;
142 }
143
144
145
146 function poke_content(&$a) {
147
148         if(! local_user()) {
149                 notice( t('Permission denied.') . EOL);
150                 return;
151         }
152
153         $name = '';
154         $id = '';
155
156         if(intval($_GET['c'])) {
157                 $r = q("SELECT `id`,`name` FROM `contact` WHERE `id` = %d AND `uid` = %d LIMIT 1",
158                         intval($_GET['c']),
159                         intval(local_user())
160                 );
161                 if(count($r)) {
162                         $name = $r[0]['name'];
163                         $id = $r[0]['id'];
164                 }
165         }
166
167
168         $base = $a->get_baseurl();
169
170         $head_tpl = get_markup_template('poke_head.tpl');
171         $a->page['htmlhead'] .= replace_macros($head_tpl,array(
172                 '$baseurl' => $a->get_baseurl(true),
173                 '$base' => $base
174         ));
175
176
177         $parent = ((x($_GET,'parent')) ? intval($_GET['parent']) : '0');
178
179
180         $verbs = get_poke_verbs();
181
182         $shortlist = array();
183         foreach($verbs as $k => $v)
184                 if($v[1] !== 'NOTRANSLATION')
185                         $shortlist[] = array($k,$v[1]);
186
187
188         $tpl = get_markup_template('poke_content.tpl');
189
190         $o = replace_macros($tpl,array(
191                 '$title' => t('Poke/Prod'),
192                 '$desc' => t('poke, prod or do other things to somebody'),
193                 '$clabel' => t('Recipient'),
194                 '$choice' => t('Choose what you wish to do to recipient'),
195                 '$verbs' => $shortlist,
196                 '$parent' => $parent,
197                 '$prv_desc' => t('Make this post private'),
198                 '$submit' => t('Submit'),
199                 '$name' => $name,
200                 '$id' => $id
201         ));
202
203         return $o;
204
205 }