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