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