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