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