]> git.mxchange.org Git - friendica.git/blob - mod/tagger.php
Merge pull request #5042 from Quix0r/rewrites/curly-braces-is-result-usage-002
[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         $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         $owner_nick = '';
47         $blocktags = 0;
48
49         $r = q("select `nickname`,`blocktags` from user where uid = %d limit 1",
50                 intval($owner_uid)
51         );
52         if (DBM::is_result($r)) {
53                 $owner_nick = $r[0]['nickname'];
54                 $blocktags = $r[0]['blocktags'];
55         }
56
57         if(local_user() != $owner_uid)
58                 return;
59
60         $r = q("select * from contact where self = 1 and uid = %d limit 1",
61                 intval(local_user())
62         );
63         if (DBM::is_result($r))
64                         $contact = $r[0];
65         else {
66                 logger('tagger: no contact_id');
67                 return;
68         }
69
70         $uri = item_new_uri($a->get_hostname(),$owner_uid);
71         $xterm = xmlify($term);
72         $post_type = (($item['resource-id']) ? L10n::t('photo') : L10n::t('status'));
73         $targettype = (($item['resource-id']) ? ACTIVITY_OBJ_IMAGE : ACTIVITY_OBJ_NOTE );
74
75         if ($owner_nick) {
76                 $href = System::baseUrl() . '/display/' . $owner_nick . '/' . $item['id'];
77         } else {
78                 $href = System::baseUrl() . '/display/' . $item['guid'];
79         }
80
81         $link = xmlify('<link rel="alternate" type="text/html" href="'. $href . '" />' . "\n") ;
82
83         $body = xmlify($item['body']);
84
85         $target = <<< EOT
86         <target>
87                 <type>$targettype</type>
88                 <local>1</local>
89                 <id>{$item['uri']}</id>
90                 <link>$link</link>
91                 <title></title>
92                 <content>$body</content>
93         </target>
94 EOT;
95
96         $tagid = System::baseUrl() . '/search?tag=' . $term;
97         $objtype = ACTIVITY_OBJ_TAGTERM;
98
99         $obj = <<< EOT
100         <object>
101                 <type>$objtype</type>
102                 <local>1</local>
103                 <id>$tagid</id>
104                 <link>$tagid</link>
105                 <title>$xterm</title>
106                 <content>$xterm</content>
107         </object>
108 EOT;
109
110         $bodyverb = L10n::t('%1$s tagged %2$s\'s %3$s with %4$s');
111
112         if (! isset($bodyverb)) {
113                 return;
114         }
115
116         $termlink = html_entity_decode('&#x2317;') . '[url=' . System::baseUrl() . '/search?tag=' . urlencode($term) . ']'. $term . '[/url]';
117
118         $arr = [];
119
120         $arr['guid'] = get_guid(32);
121         $arr['uri'] = $uri;
122         $arr['uid'] = $owner_uid;
123         $arr['contact-id'] = $contact['id'];
124         $arr['type'] = 'activity';
125         $arr['wall'] = $item['wall'];
126         $arr['gravity'] = GRAVITY_COMMENT;
127         $arr['parent'] = $item['id'];
128         $arr['parent-uri'] = $item['uri'];
129         $arr['owner-name'] = $item['author-name'];
130         $arr['owner-link'] = $item['author-link'];
131         $arr['owner-avatar'] = $item['author-avatar'];
132         $arr['author-name'] = $contact['name'];
133         $arr['author-link'] = $contact['url'];
134         $arr['author-avatar'] = $contact['thumb'];
135
136         $ulink = '[url=' . $contact['url'] . ']' . $contact['name'] . '[/url]';
137         $alink = '[url=' . $item['author-link'] . ']' . $item['author-name'] . '[/url]';
138         $plink = '[url=' . $item['plink'] . ']' . $post_type . '[/url]';
139         $arr['body'] =  sprintf( $bodyverb, $ulink, $alink, $plink, $termlink );
140
141         $arr['verb'] = ACTIVITY_TAG;
142         $arr['target-type'] = $targettype;
143         $arr['target'] = $target;
144         $arr['object-type'] = $objtype;
145         $arr['object'] = $obj;
146         $arr['private'] = $item['private'];
147         $arr['allow_cid'] = $item['allow_cid'];
148         $arr['allow_gid'] = $item['allow_gid'];
149         $arr['deny_cid'] = $item['deny_cid'];
150         $arr['deny_gid'] = $item['deny_gid'];
151         $arr['visible'] = 1;
152         $arr['unseen'] = 1;
153         $arr['origin'] = 1;
154
155         $post_id = Item::insert($arr);
156
157         if (!$item['visible']) {
158                 Item::update(['visible' => true], ['id' => $item['id']]);
159         }
160
161         $term_objtype = ($item['resource-id'] ? TERM_OBJ_PHOTO : TERM_OBJ_POST);
162
163         $t = q("SELECT count(tid) as tcount FROM term WHERE oid=%d AND term='%s'",
164                 intval($item['id']),
165                 dbesc($term)
166         );
167
168         if ((!$blocktags) && $t[0]['tcount'] == 0 ) {
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
185         if (DBM::is_result($r)) {
186                 $x = q("SELECT `blocktags` FROM `user` WHERE `uid`=%d LIMIT 1",
187                         intval($r[0]['uid'])
188                 );
189                 $t = q("SELECT COUNT(`tid`) AS `tcount` FROM `term` WHERE `oid`=%d AND `term`='%s'",
190                         intval($r[0]['id']),
191                         dbesc($term)
192                 );
193
194                 if (DBM::is_result($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
206
207         $arr['id'] = $post_id;
208
209         Addon::callHooks('post_local_end', $arr);
210
211         Worker::add(PRIORITY_HIGH, "Notifier", "tag", $post_id);
212
213         killme();
214
215         return; // NOTREACHED
216 }