]> git.mxchange.org Git - friendica.git/blob - mod/poke.php
Replace x() by isset(), !empty() or defaults()
[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\Logger;
20 use Friendica\Core\Renderer;
21 use Friendica\Core\System;
22 use Friendica\Core\Worker;
23 use Friendica\Database\DBA;
24 use Friendica\Model\Item;
25 use Friendica\Util\Strings;
26 use Friendica\Util\XML;
27
28 require_once 'include/items.php';
29
30 function poke_init(App $a)
31 {
32         if (!local_user()) {
33                 return;
34         }
35
36         $uid = local_user();
37
38         if (empty($_GET['verb'])) {
39                 return;
40         }
41
42         $verb = Strings::escapeTags(trim($_GET['verb']));
43
44         $verbs = L10n::getPokeVerbs();
45
46         if (!array_key_exists($verb, $verbs)) {
47                 return;
48         }
49
50         $activity = ACTIVITY_POKE . '#' . urlencode($verbs[$verb][0]);
51
52         $contact_id = intval($_GET['cid']);
53         if (!$contact_id) {
54                 return;
55         }
56
57         $parent = (!empty($_GET['parent']) ? intval($_GET['parent']) : 0);
58
59
60         Logger::log('poke: verb ' . $verb . ' contact ' . $contact_id, Logger::DEBUG);
61
62
63         $r = q("SELECT * FROM `contact` WHERE `id` = %d AND `uid` = %d LIMIT 1",
64                 intval($contact_id),
65                 intval($uid)
66         );
67
68         if (!DBA::isResult($r)) {
69                 Logger::log('poke: no contact ' . $contact_id);
70                 return;
71         }
72
73         $target = $r[0];
74
75         if ($parent) {
76                 $fields = ['uri', 'private', 'allow_cid', 'allow_gid', 'deny_cid', 'deny_gid'];
77                 $condition = ['id' => $parent, 'parent' => $parent, 'uid' => $uid];
78                 $item = Item::selectFirst($fields, $condition);
79
80                 if (DBA::isResult($item)) {
81                         $parent_uri = $item['uri'];
82                         $private    = $item['private'];
83                         $allow_cid  = $item['allow_cid'];
84                         $allow_gid  = $item['allow_gid'];
85                         $deny_cid   = $item['deny_cid'];
86                         $deny_gid   = $item['deny_gid'];
87                 }
88         } else {
89                 $private = (!empty($_GET['private']) ? intval($_GET['private']) : 0);
90
91                 $allow_cid     = ($private ? '<' . $target['id']. '>' : $a->user['allow_cid']);
92                 $allow_gid     = ($private ? '' : $a->user['allow_gid']);
93                 $deny_cid      = ($private ? '' : $a->user['deny_cid']);
94                 $deny_gid      = ($private ? '' : $a->user['deny_gid']);
95         }
96
97         $poster = $a->contact;
98
99         $uri = Item::newURI($uid);
100
101         $arr = [];
102
103         $arr['guid']          = System::createUUID();
104         $arr['uid']           = $uid;
105         $arr['uri']           = $uri;
106         $arr['parent-uri']    = (!empty($parent_uri) ? $parent_uri : $uri);
107         $arr['wall']          = 1;
108         $arr['contact-id']    = $poster['id'];
109         $arr['owner-name']    = $poster['name'];
110         $arr['owner-link']    = $poster['url'];
111         $arr['owner-avatar']  = $poster['thumb'];
112         $arr['author-name']   = $poster['name'];
113         $arr['author-link']   = $poster['url'];
114         $arr['author-avatar'] = $poster['thumb'];
115         $arr['title']         = '';
116         $arr['allow_cid']     = $allow_cid;
117         $arr['allow_gid']     = $allow_gid;
118         $arr['deny_cid']      = $deny_cid;
119         $arr['deny_gid']      = $deny_gid;
120         $arr['visible']       = 1;
121         $arr['verb']          = $activity;
122         $arr['private']       = $private;
123         $arr['object-type']   = ACTIVITY_OBJ_PERSON;
124
125         $arr['origin']        = 1;
126         $arr['body']          = '[url=' . $poster['url'] . ']' . $poster['name'] . '[/url]' . ' ' . L10n::t($verbs[$verb][0]) . ' ' . '[url=' . $target['url'] . ']' . $target['name'] . '[/url]';
127
128         $arr['object'] = '<object><type>' . ACTIVITY_OBJ_PERSON . '</type><title>' . $target['name'] . '</title><id>' . $target['url'] . '</id>';
129         $arr['object'] .= '<link>' . XML::escape('<link rel="alternate" type="text/html" href="' . $target['url'] . '" />' . "\n");
130
131         $arr['object'] .= XML::escape('<link rel="photo" type="image/jpeg" href="' . $target['photo'] . '" />' . "\n");
132         $arr['object'] .= '</link></object>' . "\n";
133
134         $item_id = Item::insert($arr);
135
136         Addon::callHooks('post_local_end', $arr);
137
138         return;
139 }
140
141 function poke_content(App $a)
142 {
143         if (!local_user()) {
144                 notice(L10n::t('Permission denied.') . EOL);
145                 return;
146         }
147
148         $name = '';
149         $id = '';
150
151         if (empty($_GET['c'])) {
152                 return;
153         }
154
155         $contact = DBA::selectFirst('contact', ['id', 'name'], ['id' => $_GET['c'], 'uid' => local_user()]);
156         if (!DBA::isResult($contact)) {
157                 return;
158         }
159
160         $name = $contact['name'];
161         $id = $contact['id'];
162
163         $base = System::baseUrl();
164
165         $head_tpl = Renderer::getMarkupTemplate('poke_head.tpl');
166         $a->page['htmlhead'] .= Renderer::replaceMacros($head_tpl,[
167                 '$baseurl' => System::baseUrl(true),
168                 '$base' => $base
169         ]);
170
171
172         $parent = (!empty($_GET['parent']) ? intval($_GET['parent']) : '0');
173
174
175         $verbs = L10n::getPokeVerbs();
176
177         $shortlist = [];
178         foreach ($verbs as $k => $v) {
179                 if ($v[1] !== 'NOTRANSLATION') {
180                         $shortlist[] = [$k, $v[1]];
181                 }
182         }
183
184         $tpl = Renderer::getMarkupTemplate('poke_content.tpl');
185
186         $o = Renderer::replaceMacros($tpl,[
187                 '$title' => L10n::t('Poke/Prod'),
188                 '$desc' => L10n::t('poke, prod or do other things to somebody'),
189                 '$clabel' => L10n::t('Recipient'),
190                 '$choice' => L10n::t('Choose what you wish to do to recipient'),
191                 '$verbs' => $shortlist,
192                 '$parent' => $parent,
193                 '$prv_desc' => L10n::t('Make this post private'),
194                 '$submit' => L10n::t('Submit'),
195                 '$name' => $name,
196                 '$id' => $id
197         ]);
198
199         return $o;
200 }