]> git.mxchange.org Git - friendica.git/blob - mod/tagger.php
Merge pull request #35 from fabrixxm/master
[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
16         if(! $term)
17                 return;
18
19         $item_id = (($a->argc > 1) ? notags(trim($a->argv[1])) : 0);
20
21         logger('tagger: tag ' . $term . ' item ' . $item_id);
22
23
24         $r = q("SELECT * FROM `item` WHERE `id` = '%s' LIMIT 1",
25                 dbesc($item_id),
26                 dbesc($item_id)
27         );
28
29         if(! $item_id || (! count($r))) {
30                 logger('tagger: no item ' . $item_id);
31                 return;
32         }
33
34         $item = $r[0];
35
36         $owner_uid = $item['uid'];
37
38         $r = q("select `nickname`,`blocktags` from user where uid = %d limit 1",
39                 intval($owner_uid)
40         );
41         if(count($r)) {
42                 $owner_nick = $r[0]['nickname'];
43                 $blocktags = $r[0]['blocktags'];
44         }
45
46 //      if(local_user() != $owner_uid)
47 //              return;
48
49         if(remote_user()) {
50                 $r = q("select * from contact where id = %d AND `uid` = %d limit 1",
51                         intval(remote_user()),
52                         intval($item['uid'])
53                 );
54         }
55         else {
56                 $r = q("select * from contact where self = 1 and uid = %d limit 1",
57                         intval(local_user())
58                 );
59         }
60         if(count($r))
61                         $contact = $r[0];
62         else {
63                 logger('tagger: no contact_id');
64                 return;
65         }
66
67         $uri = item_new_uri($a->get_hostname(),$owner_uid);
68
69         $post_type = (($item['resource-id']) ? t('photo') : t('status'));
70         $targettype = (($item['resource-id']) ? ACTIVITY_OBJ_PHOTO : ACTIVITY_OBJ_NOTE ); 
71
72         $link = xmlify('<link rel="alternate" type="text/html" href="' 
73                 . $a->get_baseurl() . '/display/' . $owner['nickname'] . '/' . $item['id'] . '" />' . "\n") ;
74
75         $body = $item['body'];
76
77         $target = <<< EOT
78         <target>
79                 <type>$targettype</type>
80                 <local>1</local>
81                 <id>{$item['uri']}</id>
82                 <link>$link</link>
83                 <title></title>
84                 <content>$body</content>
85         </target>
86 EOT;
87
88         $tagid = $a->get_baseurl() . '/search?search=' . $term;
89         $objtype = ACTIVITY_OBJ_TAGTERM;
90
91         $obj = <<< EOT
92         <object>
93                 <type>$objtype</type>
94                 <local>1</local>
95                 <id>$tagid</id>
96                 <link>$tagid</link>
97                 <title>$term</title>
98                 <content>$term</content>
99         </object>
100 EOT;
101
102         $bodyverb = t('%1$s tagged %2$s\'s %3$s with %4$s');
103
104         if(! isset($bodyverb))
105                         return; 
106
107         $termlink = '#[url=' . $a->get_baseurl() . '/search?search=' . urlencode($term) . ']'. $term . '[/url]';
108
109         $arr = array();
110
111         $arr['uri'] = $uri;
112         $arr['uid'] = $owner_uid;
113         $arr['contact-id'] = $contact['id'];
114         $arr['type'] = 'activity';
115         $arr['wall'] = $item['wall'];
116         $arr['gravity'] = GRAVITY_COMMENT;
117         $arr['parent'] = $item['id'];
118         $arr['parent-uri'] = $item['uri'];
119         $arr['owner-name'] = $item['author-name'];
120         $arr['owner-link'] = $item['author-link'];
121         $arr['owner-avatar'] = $item['author-avatar'];
122         $arr['author-name'] = $contact['name'];
123         $arr['author-link'] = $contact['url'];
124         $arr['author-avatar'] = $contact['thumb'];
125         
126         $ulink = '[url=' . $contact['url'] . ']' . $contact['name'] . '[/url]';
127         $alink = '[url=' . $item['author-link'] . ']' . $item['author-name'] . '[/url]';
128         $plink = '[url=' . $item['plink'] . ']' . $post_type . '[/url]';
129         $arr['body'] =  sprintf( $bodyverb, $ulink, $alink, $plink, $termlink );
130
131         $arr['verb'] = ACTIVITY_TAG;
132         $arr['target-type'] = $targettype;
133         $arr['target'] = $target;
134         $arr['object-type'] = $objtype;
135         $arr['object'] = $obj;
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 limit 1",
148                 dbesc($a->get_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 LIMIT 1",
155                         intval($item['id']),
156                         intval($owner_uid)
157                 );
158         }                       
159
160         if((! $blocktags) && (! stristr($item['tag'], ']' . $term . '[' ))) {
161                 q("update item set tag = '%s' where id = %d limit 1",
162                         dbesc($item['tag'] . (strlen($item['tag']) ? ',' : '') . '#[url=' . $a->get_baseurl() . '/search?search=' . $term . ']'. $term . '[/url]'),
163                         intval($item['id'])
164                 );
165         }
166
167         // if the original post is on this site, update it.
168
169         $r = q("select `tag`,`id`,`uid` from item where `origin` = 1 AND `uri` = '%s' LIMIT 1",
170                 dbesc($item['uri'])
171         );
172         if(count($r)) {
173                 $x = q("SELECT `blocktags` FROM `user` WHERE `uid` = %d limit 1",
174                         intval($r[0]['uid'])
175                 );
176                 if(count($x) && !$x[0]['blocktags'] && (! stristr($r[0]['tag'], ']' . $term . '['))) {
177                         q("update item set tag = '%s' where id = %d limit 1",
178                                 dbesc($r[0]['tag'] . (strlen($r[0]['tag']) ? ',' : '') . '#[url=' . $a->get_baseurl() . '/search?search=' . $term . ']'. $term . '[/url]'),
179                                 intval($r[0]['id'])
180                         );
181                 }
182
183         }
184                 
185
186         $arr['id'] = $post_id;
187
188         call_hooks('post_local_end', $arr);
189
190         proc_run('php',"include/notifier.php","tag","$post_id");
191
192         return; // NOTREACHED
193
194
195 }