]> git.mxchange.org Git - friendica.git/blob - mod/tagger.php
Replaced all preg calls in the calls with the new function
[friendica.git] / mod / tagger.php
1 <?php
2 /**
3  * @file mod/tagger.php
4  */
5
6 use Friendica\App;
7 use Friendica\Core\Hook;
8 use Friendica\Core\L10n;
9 use Friendica\Core\Logger;
10 use Friendica\Core\Session;
11 use Friendica\Core\System;
12 use Friendica\Core\Worker;
13 use Friendica\Database\DBA;
14 use Friendica\Model\Item;
15 use Friendica\Protocol\Activity;
16 use Friendica\Util\Strings;
17 use Friendica\Util\XML;
18 use Friendica\Worker\Delivery;
19
20 function tagger_content(App $a) {
21
22         if (!Session::isAuthenticated()) {
23                 return;
24         }
25
26         $term = Strings::escapeTags(trim($_GET['term']));
27         // no commas allowed
28         $term = str_replace([',',' '],['','_'],$term);
29
30         if (!$term) {
31                 return;
32         }
33
34         $item_id = (($a->argc > 1) ? Strings::escapeTags(trim($a->argv[1])) : 0);
35
36         Logger::log('tagger: tag ' . $term . ' item ' . $item_id);
37
38
39         $item = Item::selectFirst([], ['id' => $item_id]);
40
41         if (!$item_id || !DBA::isResult($item)) {
42                 Logger::log('tagger: no item ' . $item_id);
43                 return;
44         }
45
46         $owner_uid = $item['uid'];
47         $blocktags = 0;
48
49         $r = q("select `blocktags` from user where uid = %d limit 1",
50                 intval($owner_uid)
51         );
52         if (DBA::isResult($r)) {
53                 $blocktags = $r[0]['blocktags'];
54         }
55
56         if (local_user() != $owner_uid) {
57                 return;
58         }
59
60         $r = q("select * from contact where self = 1 and uid = %d limit 1",
61                 intval(local_user())
62         );
63         if (DBA::isResult($r)) {
64                         $contact = $r[0];
65         } else {
66                 Logger::log('tagger: no contact_id');
67                 return;
68         }
69
70         $uri = Item::newURI($owner_uid);
71         $xterm = XML::escape($term);
72         $post_type = (($item['resource-id']) ? L10n::t('photo') : L10n::t('status'));
73         $targettype = (($item['resource-id']) ? Activity\ObjectType::IMAGE : Activity\ObjectType::NOTE );
74         $href = System::baseUrl() . '/display/' . $item['guid'];
75
76         $link = XML::escape('<link rel="alternate" type="text/html" href="'. $href . '" />' . "\n");
77
78         $body = XML::escape($item['body']);
79
80         $target = <<< EOT
81         <target>
82                 <type>$targettype</type>
83                 <local>1</local>
84                 <id>{$item['uri']}</id>
85                 <link>$link</link>
86                 <title></title>
87                 <content>$body</content>
88         </target>
89 EOT;
90
91         $tagid = System::baseUrl() . '/search?tag=' . $xterm;
92         $objtype = Activity\ObjectType::TAGTERM;
93
94         $obj = <<< EOT
95         <object>
96                 <type>$objtype</type>
97                 <local>1</local>
98                 <id>$tagid</id>
99                 <link>$tagid</link>
100                 <title>$xterm</title>
101                 <content>$xterm</content>
102         </object>
103 EOT;
104
105         $bodyverb = L10n::t('%1$s tagged %2$s\'s %3$s with %4$s');
106
107         if (!isset($bodyverb)) {
108                 return;
109         }
110
111         $termlink = html_entity_decode('&#x2317;') . '[url=' . System::baseUrl() . '/search?tag=' . $term . ']'. $term . '[/url]';
112
113         $arr = [];
114
115         $arr['guid'] = System::createUUID();
116         $arr['uri'] = $uri;
117         $arr['uid'] = $owner_uid;
118         $arr['contact-id'] = $contact['id'];
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                 Item::update(['visible' => true], ['id' => $item['id']]);
153         }
154
155         $term_objtype = ($item['resource-id'] ? TERM_OBJ_PHOTO : TERM_OBJ_POST);
156
157         $t = q("SELECT count(tid) as tcount FROM term WHERE oid=%d AND term='%s'",
158                 intval($item['id']),
159                 DBA::escape($term)
160         );
161
162         if (!$blocktags && $t[0]['tcount'] == 0) {
163                 q("INSERT INTO term (oid, otype, type, term, url, uid) VALUE (%d, %d, %d, '%s', '%s', %d)",
164                    intval($item['id']),
165                    $term_objtype,
166                    TERM_HASHTAG,
167                    DBA::escape($term),
168                    '',
169                    intval($owner_uid)
170                 );
171         }
172
173         // if the original post is on this site, update it.
174         $original_item = Item::selectFirst(['tag', 'id', 'uid'], ['origin' => true, 'uri' => $item['uri']]);
175         if (DBA::isResult($original_item)) {
176                 $x = q("SELECT `blocktags` FROM `user` WHERE `uid`=%d LIMIT 1",
177                         intval($original_item['uid'])
178                 );
179                 $t = q("SELECT COUNT(`tid`) AS `tcount` FROM `term` WHERE `oid`=%d AND `term`='%s'",
180                         intval($original_item['id']),
181                         DBA::escape($term)
182                 );
183
184                 if (DBA::isResult($x) && !$x[0]['blocktags'] && $t[0]['tcount'] == 0){
185                         q("INSERT INTO term (`oid`, `otype`, `type`, `term`, `url`, `uid`) VALUE (%d, %d, %d, '%s', '%s', %d)",
186                                 intval($original_item['id']),
187                                 $term_objtype,
188                                 TERM_HASHTAG,
189                                 DBA::escape($term),
190                                 '',
191                                 intval($owner_uid)
192                         );
193                 }
194         }
195
196
197         $arr['id'] = $post_id;
198
199         Hook::callAll('post_local_end', $arr);
200
201         Worker::add(PRIORITY_HIGH, "Notifier", Delivery::POST, $post_id);
202
203         exit();
204 }