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