]> git.mxchange.org Git - friendica.git/blob - src/Module/Contact/Poke.php
App->contact is now removed
[friendica.git] / src / Module / Contact / Poke.php
1 <?php
2
3 namespace Friendica\Module\Contact;
4
5 use Friendica\BaseModule;
6 use Friendica\Content\Widget;
7 use Friendica\Core\Hook;
8 use Friendica\Core\Logger;
9 use Friendica\Core\Renderer;
10 use Friendica\Core\System;
11 use Friendica\Database\DBA;
12 use Friendica\DI;
13 use Friendica\Model;
14 use Friendica\Model\Contact;
15 use Friendica\Network\HTTPException;
16 use Friendica\Protocol\Activity;
17 use Friendica\Util\XML;
18
19 class Poke extends BaseModule
20 {
21         public static function post(array $parameters = [])
22         {
23                 if (!local_user() || empty($parameters['id'])) {
24                         return self::postReturn(false);
25                 }
26
27                 $uid = local_user();
28
29                 if (empty($_POST['verb'])) {
30                         return self::postReturn(false);
31                 }
32
33                 $verb = $_POST['verb'];
34
35                 $verbs = DI::l10n()->getPokeVerbs();
36                 if (!array_key_exists($verb, $verbs)) {
37                         return self::postReturn(false);
38                 }
39
40                 $activity = Activity::POKE . '#' . urlencode($verbs[$verb][0]);
41
42                 $contact_id = intval($parameters['id']);
43                 if (!$contact_id) {
44                         return self::postReturn(false);
45                 }
46
47                 Logger::info('verb ' . $verb . ' contact ' . $contact_id);
48
49                 $contact = DBA::selectFirst('contact', ['id', 'name', 'url', 'photo'], ['id' => $parameters['id'], 'uid' => local_user()]);
50                 if (!DBA::isResult($contact)) {
51                         return self::postReturn(false);
52                 }
53
54                 $a = DI::app();
55
56                 $private = !empty($_POST['private']) ? Model\Item::PRIVATE : Model\Item::PUBLIC;
57
58                 $allow_cid     = ($private ? '<' . $contact['id']. '>' : $a->user['allow_cid']);
59                 $allow_gid     = ($private ? '' : $a->user['allow_gid']);
60                 $deny_cid      = ($private ? '' : $a->user['deny_cid']);
61                 $deny_gid      = ($private ? '' : $a->user['deny_gid']);
62
63                 $actor = Contact::getById($a->contact_id);
64
65                 $uri = Model\Item::newURI($uid);
66
67                 $arr = [];
68
69                 $arr['guid']          = System::createUUID();
70                 $arr['uid']           = $uid;
71                 $arr['uri']           = $uri;
72                 $arr['wall']          = 1;
73                 $arr['contact-id']    = $actor['id'];
74                 $arr['owner-name']    = $actor['name'];
75                 $arr['owner-link']    = $actor['url'];
76                 $arr['owner-avatar']  = $actor['thumb'];
77                 $arr['author-name']   = $actor['name'];
78                 $arr['author-link']   = $actor['url'];
79                 $arr['author-avatar'] = $actor['thumb'];
80                 $arr['title']         = '';
81                 $arr['allow_cid']     = $allow_cid;
82                 $arr['allow_gid']     = $allow_gid;
83                 $arr['deny_cid']      = $deny_cid;
84                 $arr['deny_gid']      = $deny_gid;
85                 $arr['visible']       = 1;
86                 $arr['verb']          = $activity;
87                 $arr['private']       = $private;
88                 $arr['object-type']   = Activity\ObjectType::PERSON;
89
90                 $arr['origin']        = 1;
91                 $arr['body']          = '@[url=' . $actor['url'] . ']' . $actor['name'] . '[/url]' . ' ' . $verbs[$verb][2] . ' ' . '@[url=' . $contact['url'] . ']' . $contact['name'] . '[/url]';
92
93                 $arr['object'] = '<object><type>' . Activity\ObjectType::PERSON . '</type><title>' . XML::escape($contact['name']) . '</title><id>' . XML::escape($contact['url']) . '</id>';
94                 $arr['object'] .= '<link>' . XML::escape('<link rel="alternate" type="text/html" href="' . $contact['url'] . '" />') . "\n";
95
96                 $arr['object'] .= XML::escape('<link rel="photo" type="image/jpeg" href="' . $contact['photo'] . '" />') . "\n";
97                 $arr['object'] .= '</link></object>' . "\n";
98
99                 $result = Model\Item::insert($arr);
100
101                 Hook::callAll('post_local_end', $arr);
102
103                 return self::postReturn($result);
104         }
105
106         /**
107          * Since post() is called before rawContent(), we need to be able to return a JSON response in post() directly.
108          *
109          * @param bool $success
110          * @return bool
111          */
112         private static function postReturn(bool $success)
113         {
114                 if (!$success) {
115                         notice(DI::l10n()->t('Error while sending poke, please retry.'));
116                 }
117
118                 if (DI::mode()->isAjax()) {
119                         System::jsonExit(['success' => $success]);
120                 }
121
122                 return $success;
123         }
124
125         public static function content(array $parameters = [])
126         {
127                 if (!local_user()) {
128                         throw new HTTPException\UnauthorizedException(DI::l10n()->t('You must be logged in to use this module.'));
129                 }
130
131                 if (empty($parameters['id'])) {
132                         throw new HTTPException\BadRequestException();
133                 }
134
135                 $contact = DBA::selectFirst('contact', ['id', 'url', 'name'], ['id' => $parameters['id'], 'uid' => local_user()]);
136                 if (!DBA::isResult($contact)) {
137                         throw new HTTPException\NotFoundException();
138                 }
139
140                 DI::page()['aside'] = Widget\VCard::getHTML(Model\Contact::getByURL($contact["url"], false));
141
142                 $verbs = [];
143                 foreach (DI::l10n()->getPokeVerbs() as $verb => $translations) {
144                         if ($translations[1] !== 'NOTRANSLATION') {
145                                 $verbs[$verb] = $translations[1];
146                         }
147                 }
148
149                 $tpl = Renderer::getMarkupTemplate('contact/poke.tpl');
150                 $o = Renderer::replaceMacros($tpl,[
151                         '$title'    => DI::l10n()->t('Poke/Prod'),
152                         '$desc'     => DI::l10n()->t('poke, prod or do other things to somebody'),
153                         '$id'       => $contact['id'],
154                         '$verb'     => ['verb', DI::l10n()->t('Choose what you wish to do to recipient'), '', '', $verbs],
155                         '$private'  => ['private', DI::l10n()->t('Make this post private')],
156                         '$loading'  => DI::l10n()->t('Loading...'),
157                         '$submit'   => DI::l10n()->t('Submit'),
158
159                 ]);
160
161                 return $o;
162         }
163 }