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