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