Add license info at Friendica PHP files
[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\Protocol\Activity;
32 use Friendica\Util\Strings;
33 use Friendica\Util\XML;
34 use Friendica\Worker\Delivery;
35
36 function tagger_content(App $a) {
37
38         if (!Session::isAuthenticated()) {
39                 return;
40         }
41
42         $term = Strings::escapeTags(trim($_GET['term']));
43         // no commas allowed
44         $term = str_replace([',',' '],['','_'],$term);
45
46         if (!$term) {
47                 return;
48         }
49
50         $item_id = (($a->argc > 1) ? Strings::escapeTags(trim($a->argv[1])) : 0);
51
52         Logger::log('tagger: tag ' . $term . ' item ' . $item_id);
53
54
55         $item = Item::selectFirst([], ['id' => $item_id]);
56
57         if (!$item_id || !DBA::isResult($item)) {
58                 Logger::log('tagger: no item ' . $item_id);
59                 return;
60         }
61
62         $owner_uid = $item['uid'];
63         $blocktags = 0;
64
65         $r = q("select `blocktags` from user where uid = %d limit 1",
66                 intval($owner_uid)
67         );
68         if (DBA::isResult($r)) {
69                 $blocktags = $r[0]['blocktags'];
70         }
71
72         if (local_user() != $owner_uid) {
73                 return;
74         }
75
76         $r = q("select * from contact where self = 1 and uid = %d limit 1",
77                 intval(local_user())
78         );
79         if (DBA::isResult($r)) {
80                         $contact = $r[0];
81         } else {
82                 Logger::log('tagger: no contact_id');
83                 return;
84         }
85
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'];
91
92         $link = XML::escape('<link rel="alternate" type="text/html" href="'. $href . '" />' . "\n");
93
94         $body = XML::escape($item['body']);
95
96         $target = <<< EOT
97         <target>
98                 <type>$targettype</type>
99                 <local>1</local>
100                 <id>{$item['uri']}</id>
101                 <link>$link</link>
102                 <title></title>
103                 <content>$body</content>
104         </target>
105 EOT;
106
107         $tagid = DI::baseUrl() . '/search?tag=' . $xterm;
108         $objtype = Activity\ObjectType::TAGTERM;
109
110         $obj = <<< EOT
111         <object>
112                 <type>$objtype</type>
113                 <local>1</local>
114                 <id>$tagid</id>
115                 <link>$tagid</link>
116                 <title>$xterm</title>
117                 <content>$xterm</content>
118         </object>
119 EOT;
120
121         $bodyverb = DI::l10n()->t('%1$s tagged %2$s\'s %3$s with %4$s');
122
123         if (!isset($bodyverb)) {
124                 return;
125         }
126
127         $termlink = html_entity_decode('&#x2317;') . '[url=' . DI::baseUrl() . '/search?tag=' . $term . ']'. $term . '[/url]';
128
129         $arr = [];
130
131         $arr['guid'] = System::createUUID();
132         $arr['uri'] = $uri;
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'];
145
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 );
150
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'];
161         $arr['visible'] = 1;
162         $arr['unseen'] = 1;
163         $arr['origin'] = 1;
164
165         $post_id = Item::insert($arr);
166
167         if (!$item['visible']) {
168                 Item::update(['visible' => true], ['id' => $item['id']]);
169         }
170
171         $term_objtype = ($item['resource-id'] ? TERM_OBJ_PHOTO : TERM_OBJ_POST);
172
173         $t = q("SELECT count(tid) as tcount FROM term WHERE oid=%d AND term='%s'",
174                 intval($item['id']),
175                 DBA::escape($term)
176         );
177
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)",
180                    intval($item['id']),
181                    $term_objtype,
182                    TERM_HASHTAG,
183                    DBA::escape($term),
184                    '',
185                    intval($owner_uid)
186                 );
187         }
188
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'])
194                 );
195                 $t = q("SELECT COUNT(`tid`) AS `tcount` FROM `term` WHERE `oid`=%d AND `term`='%s'",
196                         intval($original_item['id']),
197                         DBA::escape($term)
198                 );
199
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']),
203                                 $term_objtype,
204                                 TERM_HASHTAG,
205                                 DBA::escape($term),
206                                 '',
207                                 intval($owner_uid)
208                         );
209                 }
210         }
211
212
213         $arr['id'] = $post_id;
214
215         Hook::callAll('post_local_end', $arr);
216
217         Worker::add(PRIORITY_HIGH, "Notifier", Delivery::POST, $post_id);
218
219         exit();
220 }