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