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