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