]> git.mxchange.org Git - friendica.git/blob - mod/tagger.php
Move mod/receive to src/Module/Diaspora/receive
[friendica.git] / mod / tagger.php
1 <?php
2 /**
3  * @file mod/tagger.php
4  */
5 use Friendica\App;
6 use Friendica\Core\Hook;
7 use Friendica\Core\L10n;
8 use Friendica\Core\Logger;
9 use Friendica\Core\System;
10 use Friendica\Core\Session;
11 use Friendica\Core\Worker;
12 use Friendica\Database\DBA;
13 use Friendica\Model\Item;
14 use Friendica\Util\Strings;
15 use Friendica\Util\XML;
16 use Friendica\Worker\Delivery;
17
18 function tagger_content(App $a) {
19
20         if (!Session::isAuthenticated()) {
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         $blocktags = 0;
46
47         $r = q("select `blocktags` from user where uid = %d limit 1",
48                 intval($owner_uid)
49         );
50         if (DBA::isResult($r)) {
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 (DBA::isResult($r)) {
62                         $contact = $r[0];
63         } else {
64                 Logger::log('tagger: no contact_id');
65                 return;
66         }
67
68         $uri = Item::newURI($owner_uid);
69         $xterm = XML::escape($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         $href = System::baseUrl() . '/display/' . $item['guid'];
73
74         $link = XML::escape('<link rel="alternate" type="text/html" href="'. $href . '" />' . "\n");
75
76         $body = XML::escape($item['body']);
77
78         $target = <<< EOT
79         <target>
80                 <type>$targettype</type>
81                 <local>1</local>
82                 <id>{$item['uri']}</id>
83                 <link>$link</link>
84                 <title></title>
85                 <content>$body</content>
86         </target>
87 EOT;
88
89         $tagid = System::baseUrl() . '/search?tag=' . $xterm;
90         $objtype = ACTIVITY_OBJ_TAGTERM;
91
92         $obj = <<< EOT
93         <object>
94                 <type>$objtype</type>
95                 <local>1</local>
96                 <id>$tagid</id>
97                 <link>$tagid</link>
98                 <title>$xterm</title>
99                 <content>$xterm</content>
100         </object>
101 EOT;
102
103         $bodyverb = L10n::t('%1$s tagged %2$s\'s %3$s with %4$s');
104
105         if (!isset($bodyverb)) {
106                 return;
107         }
108
109         $termlink = html_entity_decode('&#x2317;') . '[url=' . System::baseUrl() . '/search?tag=' . $term . ']'. $term . '[/url]';
110
111         $arr = [];
112
113         $arr['guid'] = System::createUUID();
114         $arr['uri'] = $uri;
115         $arr['uid'] = $owner_uid;
116         $arr['contact-id'] = $contact['id'];
117         $arr['wall'] = $item['wall'];
118         $arr['gravity'] = GRAVITY_COMMENT;
119         $arr['parent'] = $item['id'];
120         $arr['parent-uri'] = $item['uri'];
121         $arr['owner-name'] = $item['author-name'];
122         $arr['owner-link'] = $item['author-link'];
123         $arr['owner-avatar'] = $item['author-avatar'];
124         $arr['author-name'] = $contact['name'];
125         $arr['author-link'] = $contact['url'];
126         $arr['author-avatar'] = $contact['thumb'];
127
128         $ulink = '[url=' . $contact['url'] . ']' . $contact['name'] . '[/url]';
129         $alink = '[url=' . $item['author-link'] . ']' . $item['author-name'] . '[/url]';
130         $plink = '[url=' . $item['plink'] . ']' . $post_type . '[/url]';
131         $arr['body'] =  sprintf( $bodyverb, $ulink, $alink, $plink, $termlink );
132
133         $arr['verb'] = ACTIVITY_TAG;
134         $arr['target-type'] = $targettype;
135         $arr['target'] = $target;
136         $arr['object-type'] = $objtype;
137         $arr['object'] = $obj;
138         $arr['private'] = $item['private'];
139         $arr['allow_cid'] = $item['allow_cid'];
140         $arr['allow_gid'] = $item['allow_gid'];
141         $arr['deny_cid'] = $item['deny_cid'];
142         $arr['deny_gid'] = $item['deny_gid'];
143         $arr['visible'] = 1;
144         $arr['unseen'] = 1;
145         $arr['origin'] = 1;
146
147         $post_id = Item::insert($arr);
148
149         if (!$item['visible']) {
150                 Item::update(['visible' => true], ['id' => $item['id']]);
151         }
152
153         $term_objtype = ($item['resource-id'] ? TERM_OBJ_PHOTO : TERM_OBJ_POST);
154
155         $t = q("SELECT count(tid) as tcount FROM term WHERE oid=%d AND term='%s'",
156                 intval($item['id']),
157                 DBA::escape($term)
158         );
159
160         if (!$blocktags && $t[0]['tcount'] == 0) {
161                 q("INSERT INTO term (oid, otype, type, term, url, uid) VALUE (%d, %d, %d, '%s', '%s', %d)",
162                    intval($item['id']),
163                    $term_objtype,
164                    TERM_HASHTAG,
165                    DBA::escape($term),
166                    '',
167                    intval($owner_uid)
168                 );
169         }
170
171         // if the original post is on this site, update it.
172         $original_item = Item::selectFirst(['tag', 'id', 'uid'], ['origin' => true, 'uri' => $item['uri']]);
173         if (DBA::isResult($original_item)) {
174                 $x = q("SELECT `blocktags` FROM `user` WHERE `uid`=%d LIMIT 1",
175                         intval($original_item['uid'])
176                 );
177                 $t = q("SELECT COUNT(`tid`) AS `tcount` FROM `term` WHERE `oid`=%d AND `term`='%s'",
178                         intval($original_item['id']),
179                         DBA::escape($term)
180                 );
181
182                 if (DBA::isResult($x) && !$x[0]['blocktags'] && $t[0]['tcount'] == 0){
183                         q("INSERT INTO term (`oid`, `otype`, `type`, `term`, `url`, `uid`) VALUE (%d, %d, %d, '%s', '%s', %d)",
184                                 intval($original_item['id']),
185                                 $term_objtype,
186                                 TERM_HASHTAG,
187                                 DBA::escape($term),
188                                 '',
189                                 intval($owner_uid)
190                         );
191                 }
192         }
193
194
195         $arr['id'] = $post_id;
196
197         Hook::callAll('post_local_end', $arr);
198
199         Worker::add(PRIORITY_HIGH, "Notifier", Delivery::POST, $post_id);
200
201         exit();
202 }