EN US translation update THX AndyH3
[friendica.git/.git] / mod / tagger.php
1 <?php
2 /**
3  * @file mod/tagger.php
4  */
5 use Friendica\App;
6 use Friendica\Core\Hook;
7 use Friendica\Core\L10n;
8 use Friendica\Core\Logger;
9 use Friendica\Core\System;
10 use Friendica\Core\Worker;
11 use Friendica\Database\DBA;
12 use Friendica\Model\Item;
13 use Friendica\Util\Strings;
14 use Friendica\Util\XML;
15 use Friendica\Worker\Delivery;
16
17 function tagger_content(App $a) {
18
19         if (!local_user() && !remote_user()) {
20                 return;
21         }
22
23         $term = Strings::escapeTags(trim($_GET['term']));
24         // no commas allowed
25         $term = str_replace([',',' '],['','_'],$term);
26
27         if (!$term) {
28                 return;
29         }
30
31         $item_id = (($a->argc > 1) ? Strings::escapeTags(trim($a->argv[1])) : 0);
32
33         Logger::log('tagger: tag ' . $term . ' item ' . $item_id);
34
35
36         $item = Item::selectFirst([], ['id' => $item_id]);
37
38         if (!$item_id || !DBA::isResult($item)) {
39                 Logger::log('tagger: no item ' . $item_id);
40                 return;
41         }
42
43         $owner_uid = $item['uid'];
44         $blocktags = 0;
45
46         $r = q("select `blocktags` from user where uid = %d limit 1",
47                 intval($owner_uid)
48         );
49         if (DBA::isResult($r)) {
50                 $blocktags = $r[0]['blocktags'];
51         }
52
53         if (local_user() != $owner_uid) {
54                 return;
55         }
56
57         $r = q("select * from contact where self = 1 and uid = %d limit 1",
58                 intval(local_user())
59         );
60         if (DBA::isResult($r)) {
61                         $contact = $r[0];
62         } else {
63                 Logger::log('tagger: no contact_id');
64                 return;
65         }
66
67         $uri = Item::newURI($owner_uid);
68         $xterm = XML::escape($term);
69         $post_type = (($item['resource-id']) ? L10n::t('photo') : L10n::t('status'));
70         $targettype = (($item['resource-id']) ? ACTIVITY_OBJ_IMAGE : ACTIVITY_OBJ_NOTE );
71         $href = System::baseUrl() . '/display/' . $item['guid'];
72
73         $link = XML::escape('<link rel="alternate" type="text/html" href="'. $href . '" />' . "\n");
74
75         $body = XML::escape($item['body']);
76
77         $target = <<< EOT
78         <target>
79                 <type>$targettype</type>
80                 <local>1</local>
81                 <id>{$item['uri']}</id>
82                 <link>$link</link>
83                 <title></title>
84                 <content>$body</content>
85         </target>
86 EOT;
87
88         $tagid = System::baseUrl() . '/search?tag=' . $xterm;
89         $objtype = ACTIVITY_OBJ_TAGTERM;
90
91         $obj = <<< EOT
92         <object>
93                 <type>$objtype</type>
94                 <local>1</local>
95                 <id>$tagid</id>
96                 <link>$tagid</link>
97                 <title>$xterm</title>
98                 <content>$xterm</content>
99         </object>
100 EOT;
101
102         $bodyverb = L10n::t('%1$s tagged %2$s\'s %3$s with %4$s');
103
104         if (!isset($bodyverb)) {
105                 return;
106         }
107
108         $termlink = html_entity_decode('&#x2317;') . '[url=' . System::baseUrl() . '/search?tag=' . $term . ']'. $term . '[/url]';
109
110         $arr = [];
111
112         $arr['guid'] = System::createUUID();
113         $arr['uri'] = $uri;
114         $arr['uid'] = $owner_uid;
115         $arr['contact-id'] = $contact['id'];
116         $arr['wall'] = $item['wall'];
117         $arr['gravity'] = GRAVITY_COMMENT;
118         $arr['parent'] = $item['id'];
119         $arr['parent-uri'] = $item['uri'];
120         $arr['owner-name'] = $item['author-name'];
121         $arr['owner-link'] = $item['author-link'];
122         $arr['owner-avatar'] = $item['author-avatar'];
123         $arr['author-name'] = $contact['name'];
124         $arr['author-link'] = $contact['url'];
125         $arr['author-avatar'] = $contact['thumb'];
126
127         $ulink = '[url=' . $contact['url'] . ']' . $contact['name'] . '[/url]';
128         $alink = '[url=' . $item['author-link'] . ']' . $item['author-name'] . '[/url]';
129         $plink = '[url=' . $item['plink'] . ']' . $post_type . '[/url]';
130         $arr['body'] =  sprintf( $bodyverb, $ulink, $alink, $plink, $termlink );
131
132         $arr['verb'] = ACTIVITY_TAG;
133         $arr['target-type'] = $targettype;
134         $arr['target'] = $target;
135         $arr['object-type'] = $objtype;
136         $arr['object'] = $obj;
137         $arr['private'] = $item['private'];
138         $arr['allow_cid'] = $item['allow_cid'];
139         $arr['allow_gid'] = $item['allow_gid'];
140         $arr['deny_cid'] = $item['deny_cid'];
141         $arr['deny_gid'] = $item['deny_gid'];
142         $arr['visible'] = 1;
143         $arr['unseen'] = 1;
144         $arr['origin'] = 1;
145
146         $post_id = Item::insert($arr);
147
148         if (!$item['visible']) {
149                 Item::update(['visible' => true], ['id' => $item['id']]);
150         }
151
152         $term_objtype = ($item['resource-id'] ? TERM_OBJ_PHOTO : TERM_OBJ_POST);
153
154         $t = q("SELECT count(tid) as tcount FROM term WHERE oid=%d AND term='%s'",
155                 intval($item['id']),
156                 DBA::escape($term)
157         );
158
159         if (!$blocktags && $t[0]['tcount'] == 0) {
160                 q("INSERT INTO term (oid, otype, type, term, url, uid) VALUE (%d, %d, %d, '%s', '%s', %d)",
161                    intval($item['id']),
162                    $term_objtype,
163                    TERM_HASHTAG,
164                    DBA::escape($term),
165                    '',
166                    intval($owner_uid)
167                 );
168         }
169
170         // if the original post is on this site, update it.
171         $original_item = Item::selectFirst(['tag', 'id', 'uid'], ['origin' => true, 'uri' => $item['uri']]);
172         if (DBA::isResult($original_item)) {
173                 $x = q("SELECT `blocktags` FROM `user` WHERE `uid`=%d LIMIT 1",
174                         intval($original_item['uid'])
175                 );
176                 $t = q("SELECT COUNT(`tid`) AS `tcount` FROM `term` WHERE `oid`=%d AND `term`='%s'",
177                         intval($original_item['id']),
178                         DBA::escape($term)
179                 );
180
181                 if (DBA::isResult($x) && !$x[0]['blocktags'] && $t[0]['tcount'] == 0){
182                         q("INSERT INTO term (`oid`, `otype`, `type`, `term`, `url`, `uid`) VALUE (%d, %d, %d, '%s', '%s', %d)",
183                                 intval($original_item['id']),
184                                 $term_objtype,
185                                 TERM_HASHTAG,
186                                 DBA::escape($term),
187                                 '',
188                                 intval($owner_uid)
189                         );
190                 }
191         }
192
193
194         $arr['id'] = $post_id;
195
196         Hook::callAll('post_local_end', $arr);
197
198         Worker::add(PRIORITY_HIGH, "Notifier", Delivery::POST, $post_id);
199
200         exit();
201 }