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