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