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