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