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