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