3 * @copyright Copyright (C) 2020, Friendica
5 * @license GNU AGPL version 3 or any later version
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.
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.
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/>.
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;
30 use Friendica\Model\Item;
31 use Friendica\Protocol\Activity;
32 use Friendica\Util\Strings;
33 use Friendica\Util\XML;
34 use Friendica\Worker\Delivery;
36 function tagger_content(App $a) {
38 if (!Session::isAuthenticated()) {
42 $term = Strings::escapeTags(trim($_GET['term']));
44 $term = str_replace([',',' '],['','_'],$term);
50 $item_id = (($a->argc > 1) ? Strings::escapeTags(trim($a->argv[1])) : 0);
52 Logger::log('tagger: tag ' . $term . ' item ' . $item_id);
55 $item = Item::selectFirst([], ['id' => $item_id]);
57 if (!$item_id || !DBA::isResult($item)) {
58 Logger::log('tagger: no item ' . $item_id);
62 $owner_uid = $item['uid'];
65 $r = q("select `blocktags` from user where uid = %d limit 1",
68 if (DBA::isResult($r)) {
69 $blocktags = $r[0]['blocktags'];
72 if (local_user() != $owner_uid) {
76 $r = q("select * from contact where self = 1 and uid = %d limit 1",
79 if (DBA::isResult($r)) {
82 Logger::log('tagger: no contact_id');
86 $uri = Item::newURI($owner_uid);
87 $xterm = XML::escape($term);
88 $post_type = (($item['resource-id']) ? DI::l10n()->t('photo') : DI::l10n()->t('status'));
89 $targettype = (($item['resource-id']) ? Activity\ObjectType::IMAGE : Activity\ObjectType::NOTE );
90 $href = DI::baseUrl() . '/display/' . $item['guid'];
92 $link = XML::escape('<link rel="alternate" type="text/html" href="'. $href . '" />' . "\n");
94 $body = XML::escape($item['body']);
98 <type>$targettype</type>
100 <id>{$item['uri']}</id>
103 <content>$body</content>
107 $tagid = DI::baseUrl() . '/search?tag=' . $xterm;
108 $objtype = Activity\ObjectType::TAGTERM;
112 <type>$objtype</type>
116 <title>$xterm</title>
117 <content>$xterm</content>
121 $bodyverb = DI::l10n()->t('%1$s tagged %2$s\'s %3$s with %4$s');
123 if (!isset($bodyverb)) {
127 $termlink = html_entity_decode('⌗') . '[url=' . DI::baseUrl() . '/search?tag=' . $term . ']'. $term . '[/url]';
131 $arr['guid'] = System::createUUID();
133 $arr['uid'] = $owner_uid;
134 $arr['contact-id'] = $contact['id'];
135 $arr['wall'] = $item['wall'];
136 $arr['gravity'] = GRAVITY_COMMENT;
137 $arr['parent'] = $item['id'];
138 $arr['parent-uri'] = $item['uri'];
139 $arr['owner-name'] = $item['author-name'];
140 $arr['owner-link'] = $item['author-link'];
141 $arr['owner-avatar'] = $item['author-avatar'];
142 $arr['author-name'] = $contact['name'];
143 $arr['author-link'] = $contact['url'];
144 $arr['author-avatar'] = $contact['thumb'];
146 $ulink = '[url=' . $contact['url'] . ']' . $contact['name'] . '[/url]';
147 $alink = '[url=' . $item['author-link'] . ']' . $item['author-name'] . '[/url]';
148 $plink = '[url=' . $item['plink'] . ']' . $post_type . '[/url]';
149 $arr['body'] = sprintf( $bodyverb, $ulink, $alink, $plink, $termlink );
151 $arr['verb'] = Activity::TAG;
152 $arr['target-type'] = $targettype;
153 $arr['target'] = $target;
154 $arr['object-type'] = $objtype;
155 $arr['object'] = $obj;
156 $arr['private'] = $item['private'];
157 $arr['allow_cid'] = $item['allow_cid'];
158 $arr['allow_gid'] = $item['allow_gid'];
159 $arr['deny_cid'] = $item['deny_cid'];
160 $arr['deny_gid'] = $item['deny_gid'];
165 $post_id = Item::insert($arr);
167 if (!$item['visible']) {
168 Item::update(['visible' => true], ['id' => $item['id']]);
171 $term_objtype = ($item['resource-id'] ? TERM_OBJ_PHOTO : TERM_OBJ_POST);
173 $t = q("SELECT count(tid) as tcount FROM term WHERE oid=%d AND term='%s'",
178 if (!$blocktags && $t[0]['tcount'] == 0) {
179 q("INSERT INTO term (oid, otype, type, term, url, uid) VALUE (%d, %d, %d, '%s', '%s', %d)",
189 // if the original post is on this site, update it.
190 $original_item = Item::selectFirst(['tag', 'id', 'uid'], ['origin' => true, 'uri' => $item['uri']]);
191 if (DBA::isResult($original_item)) {
192 $x = q("SELECT `blocktags` FROM `user` WHERE `uid`=%d LIMIT 1",
193 intval($original_item['uid'])
195 $t = q("SELECT COUNT(`tid`) AS `tcount` FROM `term` WHERE `oid`=%d AND `term`='%s'",
196 intval($original_item['id']),
200 if (DBA::isResult($x) && !$x[0]['blocktags'] && $t[0]['tcount'] == 0){
201 q("INSERT INTO term (`oid`, `otype`, `type`, `term`, `url`, `uid`) VALUE (%d, %d, %d, '%s', '%s', %d)",
202 intval($original_item['id']),
213 $arr['id'] = $post_id;
215 Hook::callAll('post_local_end', $arr);
217 Worker::add(PRIORITY_HIGH, "Notifier", Delivery::POST, $post_id);