6 use Friendica\Core\Addon;
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;
15 require_once 'include/items.php';
17 function tagger_content(App $a) {
19 if (!local_user() && !remote_user()) {
23 $term = notags(trim($_GET['term']));
25 $term = str_replace([',',' '],['','_'],$term);
31 $item_id = (($a->argc > 1) ? notags(trim($a->argv[1])) : 0);
33 Logger::log('tagger: tag ' . $term . ' item ' . $item_id);
36 $item = Item::selectFirst([], ['id' => $item_id]);
38 if (!$item_id || !DBA::isResult($item)) {
39 Logger::log('tagger: no item ' . $item_id);
43 $owner_uid = $item['uid'];
47 $r = q("select `nickname`,`blocktags` from user where uid = %d limit 1",
50 if (DBA::isResult($r)) {
51 $owner_nick = $r[0]['nickname'];
52 $blocktags = $r[0]['blocktags'];
55 if (local_user() != $owner_uid) {
59 $r = q("select * from contact where self = 1 and uid = %d limit 1",
62 if (DBA::isResult($r)) {
65 Logger::log('tagger: no contact_id');
69 $uri = Item::newURI($owner_uid);
70 $xterm = Strings::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 );
75 $href = System::baseUrl() . '/display/' . $owner_nick . '/' . $item['id'];
77 $href = System::baseUrl() . '/display/' . $item['guid'];
80 $link = Strings::escape('<link rel="alternate" type="text/html" href="'. $href . '" />' . "\n") ;
82 $body = Strings::escape($item['body']);
86 <type>$targettype</type>
88 <id>{$item['uri']}</id>
91 <content>$body</content>
95 $tagid = System::baseUrl() . '/search?tag=' . $term;
96 $objtype = ACTIVITY_OBJ_TAGTERM;
100 <type>$objtype</type>
104 <title>$xterm</title>
105 <content>$xterm</content>
109 $bodyverb = L10n::t('%1$s tagged %2$s\'s %3$s with %4$s');
111 if (!isset($bodyverb)) {
115 $termlink = html_entity_decode('⌗') . '[url=' . System::baseUrl() . '/search?tag=' . urlencode($term) . ']'. $term . '[/url]';
119 $arr['guid'] = System::createUUID();
121 $arr['uid'] = $owner_uid;
122 $arr['contact-id'] = $contact['id'];
123 $arr['wall'] = $item['wall'];
124 $arr['gravity'] = GRAVITY_COMMENT;
125 $arr['parent'] = $item['id'];
126 $arr['parent-uri'] = $item['uri'];
127 $arr['owner-name'] = $item['author-name'];
128 $arr['owner-link'] = $item['author-link'];
129 $arr['owner-avatar'] = $item['author-avatar'];
130 $arr['author-name'] = $contact['name'];
131 $arr['author-link'] = $contact['url'];
132 $arr['author-avatar'] = $contact['thumb'];
134 $ulink = '[url=' . $contact['url'] . ']' . $contact['name'] . '[/url]';
135 $alink = '[url=' . $item['author-link'] . ']' . $item['author-name'] . '[/url]';
136 $plink = '[url=' . $item['plink'] . ']' . $post_type . '[/url]';
137 $arr['body'] = sprintf( $bodyverb, $ulink, $alink, $plink, $termlink );
139 $arr['verb'] = ACTIVITY_TAG;
140 $arr['target-type'] = $targettype;
141 $arr['target'] = $target;
142 $arr['object-type'] = $objtype;
143 $arr['object'] = $obj;
144 $arr['private'] = $item['private'];
145 $arr['allow_cid'] = $item['allow_cid'];
146 $arr['allow_gid'] = $item['allow_gid'];
147 $arr['deny_cid'] = $item['deny_cid'];
148 $arr['deny_gid'] = $item['deny_gid'];
153 $post_id = Item::insert($arr);
155 if (!$item['visible']) {
156 Item::update(['visible' => true], ['id' => $item['id']]);
159 $term_objtype = ($item['resource-id'] ? TERM_OBJ_PHOTO : TERM_OBJ_POST);
161 $t = q("SELECT count(tid) as tcount FROM term WHERE oid=%d AND term='%s'",
166 if (!$blocktags && $t[0]['tcount'] == 0) {
167 q("INSERT INTO term (oid, otype, type, term, url, uid) VALUE (%d, %d, %d, '%s', '%s', %d)",
172 DBA::escape(System::baseUrl() . '/search?tag=' . $term),
177 // if the original post is on this site, update it.
178 $original_item = Item::selectFirst(['tag', 'id', 'uid'], ['origin' => true, 'uri' => $item['uri']]);
179 if (DBA::isResult($original_item)) {
180 $x = q("SELECT `blocktags` FROM `user` WHERE `uid`=%d LIMIT 1",
181 intval($original_item['uid'])
183 $t = q("SELECT COUNT(`tid`) AS `tcount` FROM `term` WHERE `oid`=%d AND `term`='%s'",
184 intval($original_item['id']),
188 if (DBA::isResult($x) && !$x[0]['blocktags'] && $t[0]['tcount'] == 0){
189 q("INSERT INTO term (`oid`, `otype`, `type`, `term`, `url`, `uid`) VALUE (%d, %d, %d, '%s', '%s', %d)",
190 intval($original_item['id']),
194 DBA::escape(System::baseUrl() . '/search?tag=' . $term),
201 $arr['id'] = $post_id;
203 Addon::callHooks('post_local_end', $arr);
205 Worker::add(PRIORITY_HIGH, "Notifier", "tag", $post_id);
209 return; // NOTREACHED