]> git.mxchange.org Git - friendica.git/blob - mod/tagger.php
Merge remote-tracking branch 'origin/Nav-#3878' into Nav-#3878
[friendica.git] / mod / tagger.php
1 <?php
2
3 use Friendica\App;
4 use Friendica\Core\System;
5 use Friendica\Core\Worker;
6 use Friendica\Database\DBM;
7
8 require_once('include/security.php');
9 require_once('include/bbcode.php');
10 require_once('include/items.php');
11
12 function tagger_content(App $a) {
13
14         if(! local_user() && ! remote_user()) {
15                 return;
16         }
17
18         $term = notags(trim($_GET['term']));
19         // no commas allowed
20         $term = str_replace([',',' '],['','_'],$term);
21
22         if(! $term)
23                 return;
24
25         $item_id = (($a->argc > 1) ? notags(trim($a->argv[1])) : 0);
26
27         logger('tagger: tag ' . $term . ' item ' . $item_id);
28
29
30         $r = q("SELECT * FROM `item` WHERE `id` = '%s' LIMIT 1",
31                 dbesc($item_id)
32         );
33
34         if(! $item_id || (! DBM::is_result($r))) {
35                 logger('tagger: no item ' . $item_id);
36                 return;
37         }
38
39         $item = $r[0];
40
41         $owner_uid = $item['uid'];
42
43         $r = q("select `nickname`,`blocktags` from user where uid = %d limit 1",
44                 intval($owner_uid)
45         );
46         if (DBM::is_result($r)) {
47                 $owner_nick = $r[0]['nickname'];
48                 $blocktags = $r[0]['blocktags'];
49         }
50
51         if(local_user() != $owner_uid)
52                 return;
53
54         $r = q("select * from contact where self = 1 and uid = %d limit 1",
55                 intval(local_user())
56         );
57         if (DBM::is_result($r))
58                         $contact = $r[0];
59         else {
60                 logger('tagger: no contact_id');
61                 return;
62         }
63
64         $uri = item_new_uri($a->get_hostname(),$owner_uid);
65         $xterm = xmlify($term);
66         $post_type = (($item['resource-id']) ? t('photo') : t('status'));
67         $targettype = (($item['resource-id']) ? ACTIVITY_OBJ_IMAGE : ACTIVITY_OBJ_NOTE );
68
69         $link = xmlify('<link rel="alternate" type="text/html" href="'
70                 . System::baseUrl() . '/display/' . $owner['nickname'] . '/' . $item['id'] . '" />' . "\n") ;
71
72         $body = xmlify($item['body']);
73
74         $target = <<< EOT
75         <target>
76                 <type>$targettype</type>
77                 <local>1</local>
78                 <id>{$item['uri']}</id>
79                 <link>$link</link>
80                 <title></title>
81                 <content>$body</content>
82         </target>
83 EOT;
84
85         $tagid = System::baseUrl() . '/search?tag=' . $term;
86         $objtype = ACTIVITY_OBJ_TAGTERM;
87
88         $obj = <<< EOT
89         <object>
90                 <type>$objtype</type>
91                 <local>1</local>
92                 <id>$tagid</id>
93                 <link>$tagid</link>
94                 <title>$xterm</title>
95                 <content>$xterm</content>
96         </object>
97 EOT;
98
99         $bodyverb = t('%1$s tagged %2$s\'s %3$s with %4$s');
100
101         if (! isset($bodyverb)) {
102                 return;
103         }
104
105         $termlink = html_entity_decode('&#x2317;') . '[url=' . System::baseUrl() . '/search?tag=' . urlencode($term) . ']'. $term . '[/url]';
106
107         $arr = [];
108
109         $arr['guid'] = get_guid(32);
110         $arr['uri'] = $uri;
111         $arr['uid'] = $owner_uid;
112         $arr['contact-id'] = $contact['id'];
113         $arr['type'] = 'activity';
114         $arr['wall'] = $item['wall'];
115         $arr['gravity'] = GRAVITY_COMMENT;
116         $arr['parent'] = $item['id'];
117         $arr['parent-uri'] = $item['uri'];
118         $arr['owner-name'] = $item['author-name'];
119         $arr['owner-link'] = $item['author-link'];
120         $arr['owner-avatar'] = $item['author-avatar'];
121         $arr['author-name'] = $contact['name'];
122         $arr['author-link'] = $contact['url'];
123         $arr['author-avatar'] = $contact['thumb'];
124
125         $ulink = '[url=' . $contact['url'] . ']' . $contact['name'] . '[/url]';
126         $alink = '[url=' . $item['author-link'] . ']' . $item['author-name'] . '[/url]';
127         $plink = '[url=' . $item['plink'] . ']' . $post_type . '[/url]';
128         $arr['body'] =  sprintf( $bodyverb, $ulink, $alink, $plink, $termlink );
129
130         $arr['verb'] = ACTIVITY_TAG;
131         $arr['target-type'] = $targettype;
132         $arr['target'] = $target;
133         $arr['object-type'] = $objtype;
134         $arr['object'] = $obj;
135         $arr['private'] = $item['private'];
136         $arr['allow_cid'] = $item['allow_cid'];
137         $arr['allow_gid'] = $item['allow_gid'];
138         $arr['deny_cid'] = $item['deny_cid'];
139         $arr['deny_gid'] = $item['deny_gid'];
140         $arr['visible'] = 1;
141         $arr['unseen'] = 1;
142         $arr['last-child'] = 1;
143         $arr['origin'] = 1;
144
145         $post_id = item_store($arr);
146
147 //      q("UPDATE `item` set plink = '%s' where id = %d",
148 //              dbesc(System::baseUrl() . '/display/' . $owner_nick . '/' . $post_id),
149 //              intval($post_id)
150 //      );
151
152
153         if(! $item['visible']) {
154                 $r = q("UPDATE `item` SET `visible` = 1 WHERE `id` = %d AND `uid` = %d",
155                         intval($item['id']),
156                         intval($owner_uid)
157                 );
158         }
159
160         $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'",
162                 intval($item['id']),
163                 dbesc($term)
164         );
165         if((! $blocktags) && $t[0]['tcount']==0 ) {
166                 /*q("update item set tag = '%s' where id = %d",
167                         dbesc($item['tag'] . (strlen($item['tag']) ? ',' : '') . '#[url=' . System::baseUrl() . '/search?tag=' . $term . ']'. $term . '[/url]'),
168                         intval($item['id'])
169                 );*/
170
171                 q("INSERT INTO term (oid, otype, type, term, url, uid) VALUE (%d, %d, %d, '%s', '%s', %d)",
172                    intval($item['id']),
173                    $term_objtype,
174                    TERM_HASHTAG,
175                    dbesc($term),
176                    dbesc(System::baseUrl() . '/search?tag=' . $term),
177                    intval($owner_uid)
178                 );
179         }
180
181         // if the original post is on this site, update it.
182
183         $r = q("select `tag`,`id`,`uid` from item where `origin` = 1 AND `uri` = '%s' LIMIT 1",
184                 dbesc($item['uri'])
185         );
186         if (DBM::is_result($r)) {
187                 $x = q("SELECT `blocktags` FROM `user` WHERE `uid` = %d limit 1",
188                         intval($r[0]['uid'])
189                 );
190                 $t = q("SELECT count(tid) as tcount FROM term WHERE oid=%d AND term='%s'",
191                         intval($r[0]['id']),
192                         dbesc($term)
193                 );
194                 if(count($x) && !$x[0]['blocktags'] && $t[0]['tcount']==0){
195                         q("INSERT INTO term (oid, otype, type, term, url, uid) VALUE (%d, %d, %d, '%s', '%s', %d)",
196                            intval($r[0]['id']),
197                            $term_objtype,
198                            TERM_HASHTAG,
199                            dbesc($term),
200                            dbesc(System::baseUrl() . '/search?tag=' . $term),
201                            intval($owner_uid)
202                         );
203                 }
204
205                 /*if(count($x) && !$x[0]['blocktags'] && (! stristr($r[0]['tag'], ']' . $term . '['))) {
206                         q("update item set tag = '%s' where id = %d",
207                                 dbesc($r[0]['tag'] . (strlen($r[0]['tag']) ? ',' : '') . '#[url=' . System::baseUrl() . '/search?tag=' . $term . ']'. $term . '[/url]'),
208                                 intval($r[0]['id'])
209                         );
210                 }*/
211
212         }
213
214
215         $arr['id'] = $post_id;
216
217         call_hooks('post_local_end', $arr);
218
219         Worker::add(PRIORITY_HIGH, "Notifier", "tag", $post_id);
220
221         killme();
222
223         return; // NOTREACHED
224
225
226 }