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