]> git.mxchange.org Git - friendica.git/blob - mod/tagger.php
Fix mods/README.md format
[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\Logger;
9 use Friendica\Core\System;
10 use Friendica\Core\Worker;
11 use Friendica\Database\DBA;
12 use Friendica\Model\Item;
13 use Friendica\Util\Strings;
14 use Friendica\Util\XML;
15
16 require_once 'include/items.php';
17
18 function tagger_content(App $a) {
19
20         if (!local_user() && !remote_user()) {
21                 return;
22         }
23
24         $term = Strings::escapeTags(trim($_GET['term']));
25         // no commas allowed
26         $term = str_replace([',',' '],['','_'],$term);
27
28         if (!$term) {
29                 return;
30         }
31
32         $item_id = (($a->argc > 1) ? Strings::escapeTags(trim($a->argv[1])) : 0);
33
34         Logger::log('tagger: tag ' . $term . ' item ' . $item_id);
35
36
37         $item = Item::selectFirst([], ['id' => $item_id]);
38
39         if (!$item_id || !DBA::isResult($item)) {
40                 Logger::log('tagger: no item ' . $item_id);
41                 return;
42         }
43
44         $owner_uid = $item['uid'];
45         $owner_nick = '';
46         $blocktags = 0;
47
48         $r = q("select `nickname`,`blocktags` from user where uid = %d limit 1",
49                 intval($owner_uid)
50         );
51         if (DBA::isResult($r)) {
52                 $owner_nick = $r[0]['nickname'];
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_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 = XML::escape('<link rel="alternate" type="text/html" href="'. $href . '" />' . "\n");
82
83         $body = XML::escape($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=' . $xterm;
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=' . $term . ']'. $term . '[/url]';
117
118         $arr = [];
119
120         $arr['guid'] = System::createUUID();
121         $arr['uri'] = $uri;
122         $arr['uid'] = $owner_uid;
123         $arr['contact-id'] = $contact['id'];
124         $arr['wall'] = $item['wall'];
125         $arr['gravity'] = GRAVITY_COMMENT;
126         $arr['parent'] = $item['id'];
127         $arr['parent-uri'] = $item['uri'];
128         $arr['owner-name'] = $item['author-name'];
129         $arr['owner-link'] = $item['author-link'];
130         $arr['owner-avatar'] = $item['author-avatar'];
131         $arr['author-name'] = $contact['name'];
132         $arr['author-link'] = $contact['url'];
133         $arr['author-avatar'] = $contact['thumb'];
134
135         $ulink = '[url=' . $contact['url'] . ']' . $contact['name'] . '[/url]';
136         $alink = '[url=' . $item['author-link'] . ']' . $item['author-name'] . '[/url]';
137         $plink = '[url=' . $item['plink'] . ']' . $post_type . '[/url]';
138         $arr['body'] =  sprintf( $bodyverb, $ulink, $alink, $plink, $termlink );
139
140         $arr['verb'] = ACTIVITY_TAG;
141         $arr['target-type'] = $targettype;
142         $arr['target'] = $target;
143         $arr['object-type'] = $objtype;
144         $arr['object'] = $obj;
145         $arr['private'] = $item['private'];
146         $arr['allow_cid'] = $item['allow_cid'];
147         $arr['allow_gid'] = $item['allow_gid'];
148         $arr['deny_cid'] = $item['deny_cid'];
149         $arr['deny_gid'] = $item['deny_gid'];
150         $arr['visible'] = 1;
151         $arr['unseen'] = 1;
152         $arr['origin'] = 1;
153
154         $post_id = Item::insert($arr);
155
156         if (!$item['visible']) {
157                 Item::update(['visible' => true], ['id' => $item['id']]);
158         }
159
160         $term_objtype = ($item['resource-id'] ? TERM_OBJ_PHOTO : TERM_OBJ_POST);
161
162         $t = q("SELECT count(tid) as tcount FROM term WHERE oid=%d AND term='%s'",
163                 intval($item['id']),
164                 DBA::escape($term)
165         );
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                    DBA::escape($term),
173                    '',
174                    intval($owner_uid)
175                 );
176         }
177
178         // if the original post is on this site, update it.
179         $original_item = Item::selectFirst(['tag', 'id', 'uid'], ['origin' => true, 'uri' => $item['uri']]);
180         if (DBA::isResult($original_item)) {
181                 $x = q("SELECT `blocktags` FROM `user` WHERE `uid`=%d LIMIT 1",
182                         intval($original_item['uid'])
183                 );
184                 $t = q("SELECT COUNT(`tid`) AS `tcount` FROM `term` WHERE `oid`=%d AND `term`='%s'",
185                         intval($original_item['id']),
186                         DBA::escape($term)
187                 );
188
189                 if (DBA::isResult($x) && !$x[0]['blocktags'] && $t[0]['tcount'] == 0){
190                         q("INSERT INTO term (`oid`, `otype`, `type`, `term`, `url`, `uid`) VALUE (%d, %d, %d, '%s', '%s', %d)",
191                                 intval($original_item['id']),
192                                 $term_objtype,
193                                 TERM_HASHTAG,
194                                 DBA::escape($term),
195                                 '',
196                                 intval($owner_uid)
197                         );
198                 }
199         }
200
201
202         $arr['id'] = $post_id;
203
204         Addon::callHooks('post_local_end', $arr);
205
206         Worker::add(PRIORITY_HIGH, "Notifier", "tag", $post_id);
207
208         killme();
209
210         return; // NOTREACHED
211 }