]> git.mxchange.org Git - friendica.git/blob - mod/tagger.php
Merge remote-tracking branch 'friendica/master' into randomerror
[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(&$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 || (! count($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(count($r)) {
43                 $owner_nick = $r[0]['nickname'];
44                 $blocktags = $r[0]['blocktags'];
45         }
46
47         if(local_user() != $owner_uid)
48                 return;
49
50         if(remote_user()) {
51                 $r = q("select * from contact where id = %d AND `uid` = %d limit 1",
52                         intval(remote_user()),
53                         intval($item['uid'])
54                 );
55         }
56         else {
57                 $r = q("select * from contact where self = 1 and uid = %d limit 1",
58                         intval(local_user())
59                 );
60         }
61         if(count($r))
62                         $contact = $r[0];
63         else {
64                 logger('tagger: no contact_id');
65                 return;
66         }
67
68         $uri = item_new_uri($a->get_hostname(),$owner_uid);
69
70         $post_type = (($item['resource-id']) ? t('photo') : t('status'));
71         $targettype = (($item['resource-id']) ? ACTIVITY_OBJ_PHOTO : ACTIVITY_OBJ_NOTE ); 
72
73         $link = xmlify('<link rel="alternate" type="text/html" href="' 
74                 . $a->get_baseurl() . '/display/' . $owner['nickname'] . '/' . $item['id'] . '" />' . "\n") ;
75
76         $body = $item['body'];
77
78         $target = <<< EOT
79         <target>
80                 <type>$targettype</type>
81                 <local>1</local>
82                 <id>{$item['uri']}</id>
83                 <link>$link</link>
84                 <title></title>
85                 <content>$body</content>
86         </target>
87 EOT;
88
89         $tagid = $a->get_baseurl() . '/search?search=' . $term;
90         $objtype = ACTIVITY_OBJ_TAGTERM;
91
92         $obj = <<< EOT
93         <object>
94                 <type>$objtype</type>
95                 <local>1</local>
96                 <id>$tagid</id>
97                 <link>$tagid</link>
98                 <title>$term</title>
99                 <content>$term</content>
100         </object>
101 EOT;
102
103         $bodyverb = t('%1$s tagged %2$s\'s %3$s with %4$s');
104
105         if(! isset($bodyverb))
106                         return; 
107
108         $termlink = html_entity_decode('&#x2317;') . '[url=' . $a->get_baseurl() . '/search?search=' . urlencode($term) . ']'. $term . '[/url]';
109
110         $arr = array();
111
112         $arr['uri'] = $uri;
113         $arr['uid'] = $owner_uid;
114         $arr['contact-id'] = $contact['id'];
115         $arr['type'] = 'activity';
116         $arr['wall'] = $item['wall'];
117         $arr['gravity'] = GRAVITY_COMMENT;
118         $arr['parent'] = $item['id'];
119         $arr['parent-uri'] = $item['uri'];
120         $arr['owner-name'] = $item['author-name'];
121         $arr['owner-link'] = $item['author-link'];
122         $arr['owner-avatar'] = $item['author-avatar'];
123         $arr['author-name'] = $contact['name'];
124         $arr['author-link'] = $contact['url'];
125         $arr['author-avatar'] = $contact['thumb'];
126         
127         $ulink = '[url=' . $contact['url'] . ']' . $contact['name'] . '[/url]';
128         $alink = '[url=' . $item['author-link'] . ']' . $item['author-name'] . '[/url]';
129         $plink = '[url=' . $item['plink'] . ']' . $post_type . '[/url]';
130         $arr['body'] =  sprintf( $bodyverb, $ulink, $alink, $plink, $termlink );
131
132         $arr['verb'] = ACTIVITY_TAG;
133         $arr['target-type'] = $targettype;
134         $arr['target'] = $target;
135         $arr['object-type'] = $objtype;
136         $arr['object'] = $obj;
137         $arr['private'] = $item['private'];
138         $arr['allow_cid'] = $item['allow_cid'];
139         $arr['allow_gid'] = $item['allow_gid'];
140         $arr['deny_cid'] = $item['deny_cid'];
141         $arr['deny_gid'] = $item['deny_gid'];
142         $arr['visible'] = 1;
143         $arr['unseen'] = 1;
144         $arr['last-child'] = 1;
145         $arr['origin'] = 1;
146
147         $post_id = item_store($arr);    
148
149         q("UPDATE `item` set plink = '%s' where id = %d limit 1",
150                 dbesc($a->get_baseurl() . '/display/' . $owner_nick . '/' . $post_id),
151                 intval($post_id)
152         );
153                 
154
155         if(! $item['visible']) {
156                 $r = q("UPDATE `item` SET `visible` = 1 WHERE `id` = %d AND `uid` = %d LIMIT 1",
157                         intval($item['id']),
158                         intval($owner_uid)
159                 );
160         }                       
161
162         if((! $blocktags) && (! stristr($item['tag'], ']' . $term . '[' ))) {
163                 q("update item set tag = '%s' where id = %d limit 1",
164                         dbesc($item['tag'] . (strlen($item['tag']) ? ',' : '') . '#[url=' . $a->get_baseurl() . '/search?search=' . $term . ']'. $term . '[/url]'),
165                         intval($item['id'])
166                 );
167         }
168
169         // if the original post is on this site, update it.
170
171         $r = q("select `tag`,`id`,`uid` from item where `origin` = 1 AND `uri` = '%s' LIMIT 1",
172                 dbesc($item['uri'])
173         );
174         if(count($r)) {
175                 $x = q("SELECT `blocktags` FROM `user` WHERE `uid` = %d limit 1",
176                         intval($r[0]['uid'])
177                 );
178                 if(count($x) && !$x[0]['blocktags'] && (! stristr($r[0]['tag'], ']' . $term . '['))) {
179                         q("update item set tag = '%s' where id = %d limit 1",
180                                 dbesc($r[0]['tag'] . (strlen($r[0]['tag']) ? ',' : '') . '#[url=' . $a->get_baseurl() . '/search?search=' . $term . ']'. $term . '[/url]'),
181                                 intval($r[0]['id'])
182                         );
183                 }
184
185         }
186                 
187
188         $arr['id'] = $post_id;
189
190         call_hooks('post_local_end', $arr);
191
192         proc_run('php',"include/notifier.php","tag","$post_id");
193
194         killme();
195
196         return; // NOTREACHED
197
198
199 }