]> git.mxchange.org Git - friendica.git/blob - mod/tagger.php
Merge pull request #12025 from annando/no-boot-src-module
[friendica.git] / mod / tagger.php
1 <?php
2 /**
3  * @copyright Copyright (C) 2010-2022, the Friendica project
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\Contact;
31 use Friendica\Model\Item;
32 use Friendica\Model\Post;
33 use Friendica\Model\Tag;
34 use Friendica\Protocol\Activity;
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 = trim($_GET['term'] ?? '');
45         // no commas allowed
46         $term = str_replace([',',' ', '<', '>'],['','_', '', ''], $term);
47
48         if (!$term) {
49                 return;
50         }
51
52         $item_id = ((DI::args()->getArgc() > 1) ? trim(DI::args()->getArgv()[1]) : 0);
53
54         Logger::info('tagger: tag', ['term' =>  $term, 'item' => $item_id]);
55
56
57         $item = Post::selectFirst([], ['id' => $item_id]);
58
59         if (!$item_id || !DBA::isResult($item)) {
60                 Logger::notice('tagger: no item ' . $item_id);
61                 return;
62         }
63
64         $owner_uid = $item['uid'];
65
66         if (Session::getLocalUser() != $owner_uid) {
67                 return;
68         }
69
70         $contact = Contact::selectFirst([], ['self' => true, 'uid' => Session::getLocalUser()]);
71         if (!DBA::isResult($contact)) {
72                 Logger::warning('Self contact not found.', ['uid' => Session::getLocalUser()]);
73                 return;
74         }
75
76         $uri = Item::newURI();
77         $xterm = XML::escape($term);
78         $post_type = (($item['resource-id']) ? DI::l10n()->t('photo') : DI::l10n()->t('status'));
79         $targettype = (($item['resource-id']) ? Activity\ObjectType::IMAGE : Activity\ObjectType::NOTE );
80         $href = DI::baseUrl() . '/display/' . $item['guid'];
81
82         $link = XML::escape('<link rel="alternate" type="text/html" href="'. $href . '" />' . "\n");
83
84         $body = XML::escape($item['body']);
85
86         $target = <<< EOT
87         <target>
88                 <type>$targettype</type>
89                 <local>1</local>
90                 <id>{$item['uri']}</id>
91                 <link>$link</link>
92                 <title></title>
93                 <content>$body</content>
94         </target>
95 EOT;
96
97         $tagid = DI::baseUrl() . '/search?tag=' . $xterm;
98         $objtype = Activity\ObjectType::TAGTERM;
99
100         $obj = <<< EOT
101         <object>
102                 <type>$objtype</type>
103                 <local>1</local>
104                 <id>$tagid</id>
105                 <link>$tagid</link>
106                 <title>$xterm</title>
107                 <content>$xterm</content>
108         </object>
109 EOT;
110
111         $bodyverb = DI::l10n()->t('%1$s tagged %2$s\'s %3$s with %4$s');
112
113         if (!isset($bodyverb)) {
114                 return;
115         }
116
117         $termlink = html_entity_decode('&#x2317;') . '[url=' . DI::baseUrl() . '/search?tag=' . $term . ']'. $term . '[/url]';
118
119         $ulink = '[url=' . $contact['url'] . ']' . $contact['name'] . '[/url]';
120         $alink = '[url=' . $item['author-link'] . ']' . $item['author-name'] . '[/url]';
121         $plink = '[url=' . $item['plink'] . ']' . $post_type . '[/url]';
122
123         $arr = [
124                 'guid'          => System::createUUID(),
125                 'uri'           => $uri,
126                 'uid'           => $owner_uid,
127                 'contact-id'    => $contact['id'],
128                 'wall'          => $item['wall'],
129                 'gravity'       => Item::GRAVITY_COMMENT,
130                 'parent'        => $item['id'],
131                 'thr-parent'    => $item['uri'],
132                 'owner-name'    => $item['author-name'],
133                 'owner-link'    => $item['author-link'],
134                 'owner-avatar'  => $item['author-avatar'],
135                 'author-name'   => $contact['name'],
136                 'author-link'   => $contact['url'],
137                 'author-avatar' => $contact['thumb'],
138                 'body'          => sprintf($bodyverb, $ulink, $alink, $plink, $termlink),
139                 'verb'          => Activity::TAG,
140                 'target-type'   => $targettype,
141                 'target'        => $target,
142                 'object-type'   => $objtype,
143                 'object'        => $obj,
144                 'private'       => $item['private'],
145                 'allow_cid'     => $item['allow_cid'],
146                 'allow_gid'     => $item['allow_gid'],
147                 'deny_cid'      => $item['deny_cid'],
148                 'deny_gid'      => $item['deny_gid'],
149                 'visible'       => 1,
150                 'unseen'        => 1,
151                 'origin'        => 1,
152         ];
153
154
155         $post_id = Item::insert($arr);
156
157         if (!$item['visible']) {
158                 Item::update(['visible' => true], ['id' => $item['id']]);
159         }
160
161         Tag::store($item['uri-id'], Tag::HASHTAG, $term);
162
163         $arr['id'] = $post_id;
164
165         Hook::callAll('post_local_end', $arr);
166
167         $post = Post::selectFirst(['uri-id', 'uid'], ['id' => $post_id]);
168
169         Worker::add(Worker::PRIORITY_HIGH, "Notifier", Delivery::POST, $post['uri-id'], $post['uid']);
170         System::exit();
171 }