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