]> git.mxchange.org Git - friendica.git/blob - mod/tagger.php
Merge pull request #5214 from abanink/5118
[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/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
30         $item_id = (($a->argc > 1) ? notags(trim($a->argv[1])) : 0);
31
32         logger('tagger: tag ' . $term . ' item ' . $item_id);
33
34
35         $item = Item::selectFirst(local_user(), [], ['id' => $item_id]);
36
37         if (!$item_id || !DBM::is_result($item)) {
38                 logger('tagger: no item ' . $item_id);
39                 return;
40         }
41
42         $owner_uid = $item['uid'];
43         $owner_nick = '';
44         $blocktags = 0;
45
46         $r = q("select `nickname`,`blocktags` from user where uid = %d limit 1",
47                 intval($owner_uid)
48         );
49         if (DBM::is_result($r)) {
50                 $owner_nick = $r[0]['nickname'];
51                 $blocktags = $r[0]['blocktags'];
52         }
53
54         if (local_user() != $owner_uid) {
55                 return;
56         }
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         if ($owner_nick) {
74                 $href = System::baseUrl() . '/display/' . $owner_nick . '/' . $item['id'];
75         } else {
76                 $href = System::baseUrl() . '/display/' . $item['guid'];
77         }
78
79         $link = xmlify('<link rel="alternate" type="text/html" href="'. $href . '" />' . "\n") ;
80
81         $body = xmlify($item['body']);
82
83         $target = <<< EOT
84         <target>
85                 <type>$targettype</type>
86                 <local>1</local>
87                 <id>{$item['uri']}</id>
88                 <link>$link</link>
89                 <title></title>
90                 <content>$body</content>
91         </target>
92 EOT;
93
94         $tagid = System::baseUrl() . '/search?tag=' . $term;
95         $objtype = ACTIVITY_OBJ_TAGTERM;
96
97         $obj = <<< EOT
98         <object>
99                 <type>$objtype</type>
100                 <local>1</local>
101                 <id>$tagid</id>
102                 <link>$tagid</link>
103                 <title>$xterm</title>
104                 <content>$xterm</content>
105         </object>
106 EOT;
107
108         $bodyverb = L10n::t('%1$s tagged %2$s\'s %3$s with %4$s');
109
110         if (!isset($bodyverb)) {
111                 return;
112         }
113
114         $termlink = html_entity_decode('&#x2317;') . '[url=' . System::baseUrl() . '/search?tag=' . urlencode($term) . ']'. $term . '[/url]';
115
116         $arr = [];
117
118         $arr['guid'] = get_guid(32);
119         $arr['uri'] = $uri;
120         $arr['uid'] = $owner_uid;
121         $arr['contact-id'] = $contact['id'];
122         $arr['type'] = 'activity';
123         $arr['wall'] = $item['wall'];
124         $arr['gravity'] = GRAVITY_COMMENT;
125         $arr['parent'] = $item['id'];
126         $arr['parent-uri'] = $item['uri'];
127         $arr['owner-name'] = $item['author-name'];
128         $arr['owner-link'] = $item['author-link'];
129         $arr['owner-avatar'] = $item['author-avatar'];
130         $arr['author-name'] = $contact['name'];
131         $arr['author-link'] = $contact['url'];
132         $arr['author-avatar'] = $contact['thumb'];
133
134         $ulink = '[url=' . $contact['url'] . ']' . $contact['name'] . '[/url]';
135         $alink = '[url=' . $item['author-link'] . ']' . $item['author-name'] . '[/url]';
136         $plink = '[url=' . $item['plink'] . ']' . $post_type . '[/url]';
137         $arr['body'] =  sprintf( $bodyverb, $ulink, $alink, $plink, $termlink );
138
139         $arr['verb'] = ACTIVITY_TAG;
140         $arr['target-type'] = $targettype;
141         $arr['target'] = $target;
142         $arr['object-type'] = $objtype;
143         $arr['object'] = $obj;
144         $arr['private'] = $item['private'];
145         $arr['allow_cid'] = $item['allow_cid'];
146         $arr['allow_gid'] = $item['allow_gid'];
147         $arr['deny_cid'] = $item['deny_cid'];
148         $arr['deny_gid'] = $item['deny_gid'];
149         $arr['visible'] = 1;
150         $arr['unseen'] = 1;
151         $arr['origin'] = 1;
152
153         $post_id = Item::insert($arr);
154
155         if (!$item['visible']) {
156                 Item::update(['visible' => true], ['id' => $item['id']]);
157         }
158
159         $term_objtype = ($item['resource-id'] ? TERM_OBJ_PHOTO : TERM_OBJ_POST);
160
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
166         if (!$blocktags && $t[0]['tcount'] == 0) {
167                 q("INSERT INTO term (oid, otype, type, term, url, uid) VALUE (%d, %d, %d, '%s', '%s', %d)",
168                    intval($item['id']),
169                    $term_objtype,
170                    TERM_HASHTAG,
171                    dbesc($term),
172                    dbesc(System::baseUrl() . '/search?tag=' . $term),
173                    intval($owner_uid)
174                 );
175         }
176
177         // if the original post is on this site, update it.
178
179         $r = q("SELECT `tag`,`id`,`uid` FROM `item` WHERE `origin`=1 AND `uri`='%s' LIMIT 1",
180                 dbesc($item['uri'])
181         );
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
192                 if (DBM::is_result($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
204
205         $arr['id'] = $post_id;
206
207         Addon::callHooks('post_local_end', $arr);
208
209         Worker::add(PRIORITY_HIGH, "Notifier", "tag", $post_id);
210
211         killme();
212
213         return; // NOTREACHED
214 }