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