Merge pull request #9944 from MrPetovan/bug/9905-block-contact-not-found
[friendica.git/.git] / mod / tagger.php
1 <?php
2 /**
3  * @copyright Copyright (C) 2020, Friendica
4  *
5  * @license GNU AGPL version 3 or any later version
6  *
7  * This program is free software: you can redistribute it and/or modify
8  * it under the terms of the GNU Affero General Public License as
9  * published by the Free Software Foundation, either version 3 of the
10  * License, or (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU Affero General Public License for more details.
16  *
17  * You should have received a copy of the GNU Affero General Public License
18  * along with this program.  If not, see <https://www.gnu.org/licenses/>.
19  *
20  */
21
22 use Friendica\App;
23 use Friendica\Core\Hook;
24 use Friendica\Core\Logger;
25 use Friendica\Core\Session;
26 use Friendica\Core\System;
27 use Friendica\Core\Worker;
28 use Friendica\Database\DBA;
29 use Friendica\DI;
30 use Friendica\Model\Item;
31 use Friendica\Model\Post;
32 use Friendica\Model\Tag;
33 use Friendica\Protocol\Activity;
34 use Friendica\Util\Strings;
35 use Friendica\Util\XML;
36 use Friendica\Worker\Delivery;
37
38 function tagger_content(App $a) {
39
40         if (!Session::isAuthenticated()) {
41                 return;
42         }
43
44         $term = Strings::escapeTags(trim($_GET['term']));
45         // no commas allowed
46         $term = str_replace([',',' '],['','_'],$term);
47
48         if (!$term) {
49                 return;
50         }
51
52         $item_id = (($a->argc > 1) ? Strings::escapeTags(trim($a->argv[1])) : 0);
53
54         Logger::log('tagger: tag ' . $term . ' item ' . $item_id);
55
56
57         $item = Post::selectFirst([], ['id' => $item_id]);
58
59         if (!$item_id || !DBA::isResult($item)) {
60                 Logger::log('tagger: no item ' . $item_id);
61                 return;
62         }
63
64         $owner_uid = $item['uid'];
65         $blocktags = 0;
66
67         $r = q("select `blocktags` from user where uid = %d limit 1",
68                 intval($owner_uid)
69         );
70         if (DBA::isResult($r)) {
71                 $blocktags = $r[0]['blocktags'];
72         }
73
74         if (local_user() != $owner_uid) {
75                 return;
76         }
77
78         $r = q("select * from contact where self = 1 and uid = %d limit 1",
79                 intval(local_user())
80         );
81         if (DBA::isResult($r)) {
82                         $contact = $r[0];
83         } else {
84                 Logger::log('tagger: no contact_id');
85                 return;
86         }
87
88         $uri = Item::newURI($owner_uid);
89         $xterm = XML::escape($term);
90         $post_type = (($item['resource-id']) ? DI::l10n()->t('photo') : DI::l10n()->t('status'));
91         $targettype = (($item['resource-id']) ? Activity\ObjectType::IMAGE : Activity\ObjectType::NOTE );
92         $href = DI::baseUrl() . '/display/' . $item['guid'];
93
94         $link = XML::escape('<link rel="alternate" type="text/html" href="'. $href . '" />' . "\n");
95
96         $body = XML::escape($item['body']);
97
98         $target = <<< EOT
99         <target>
100                 <type>$targettype</type>
101                 <local>1</local>
102                 <id>{$item['uri']}</id>
103                 <link>$link</link>
104                 <title></title>
105                 <content>$body</content>
106         </target>
107 EOT;
108
109         $tagid = DI::baseUrl() . '/search?tag=' . $xterm;
110         $objtype = Activity\ObjectType::TAGTERM;
111
112         $obj = <<< EOT
113         <object>
114                 <type>$objtype</type>
115                 <local>1</local>
116                 <id>$tagid</id>
117                 <link>$tagid</link>
118                 <title>$xterm</title>
119                 <content>$xterm</content>
120         </object>
121 EOT;
122
123         $bodyverb = DI::l10n()->t('%1$s tagged %2$s\'s %3$s with %4$s');
124
125         if (!isset($bodyverb)) {
126                 return;
127         }
128
129         $termlink = html_entity_decode('&#x2317;') . '[url=' . DI::baseUrl() . '/search?tag=' . $term . ']'. $term . '[/url]';
130
131         $arr = [];
132
133         $arr['guid'] = System::createUUID();
134         $arr['uri'] = $uri;
135         $arr['uid'] = $owner_uid;
136         $arr['contact-id'] = $contact['id'];
137         $arr['wall'] = $item['wall'];
138         $arr['gravity'] = GRAVITY_COMMENT;
139         $arr['parent'] = $item['id'];
140         $arr['thr-parent'] = $item['uri'];
141         $arr['owner-name'] = $item['author-name'];
142         $arr['owner-link'] = $item['author-link'];
143         $arr['owner-avatar'] = $item['author-avatar'];
144         $arr['author-name'] = $contact['name'];
145         $arr['author-link'] = $contact['url'];
146         $arr['author-avatar'] = $contact['thumb'];
147
148         $ulink = '[url=' . $contact['url'] . ']' . $contact['name'] . '[/url]';
149         $alink = '[url=' . $item['author-link'] . ']' . $item['author-name'] . '[/url]';
150         $plink = '[url=' . $item['plink'] . ']' . $post_type . '[/url]';
151         $arr['body'] =  sprintf( $bodyverb, $ulink, $alink, $plink, $termlink );
152
153         $arr['verb'] = Activity::TAG;
154         $arr['target-type'] = $targettype;
155         $arr['target'] = $target;
156         $arr['object-type'] = $objtype;
157         $arr['object'] = $obj;
158         $arr['private'] = $item['private'];
159         $arr['allow_cid'] = $item['allow_cid'];
160         $arr['allow_gid'] = $item['allow_gid'];
161         $arr['deny_cid'] = $item['deny_cid'];
162         $arr['deny_gid'] = $item['deny_gid'];
163         $arr['visible'] = 1;
164         $arr['unseen'] = 1;
165         $arr['origin'] = 1;
166
167         $post_id = Item::insert($arr);
168
169         if (!$item['visible']) {
170                 Item::update(['visible' => true], ['id' => $item['id']]);
171         }
172
173         Tag::store($item['uri-id'], Tag::HASHTAG, $term);
174
175         $arr['id'] = $post_id;
176
177         Hook::callAll('post_local_end', $arr);
178
179         $post = Post::selectFirst(['uri-id', 'uid'], ['id' => $post_id]);
180
181         Worker::add(PRIORITY_HIGH, "Notifier", Delivery::POST, $post['uri-id'], $post['uid']);
182
183         exit();
184 }