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