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