1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
|
<?php /** @file */
// post categories and "save to file" use the same item.file table for storage.
// We will differentiate the different uses by wrapping categories in angle brackets
// and save to file categories in square brackets.
// To do this we need to escape these characters if they appear in our tag.
use Zotlabs\Lib\Cache;
function file_tag_encode($s) {
return str_replace(array('<','>','[',']'),array('%3c','%3e','%5b','%5d'),$s);
}
function file_tag_decode($s) {
return str_replace(array('%3c','%3e','%5b','%5d'),array('<','>','[',']'),$s);
}
function file_tag_file_query($table,$s,$type = 'file') {
if($type == 'file')
$termtype = TERM_FILE;
else
$termtype = TERM_CATEGORY;
return sprintf(" AND " . (($table) ? dbesc($table) . '.' : '') . "id in (select term.oid from term where term.ttype = %d and term.term = '%s' and term.uid = " . (($table) ? dbesc($table) . '.' : '') . "uid ) ",
intval($termtype),
protect_sprintf(dbesc($s))
);
}
function term_query($table,$s,$type = TERM_UNKNOWN, $type2 = '') {
if($type2) {
return sprintf(" AND " . (($table) ? dbesc($table) . '.' : '') . "id in (select term.oid from term where term.ttype in (%d, %d) and term.term = '%s' and term.uid = " . (($table) ? dbesc($table) . '.' : '') . "uid ) ",
intval($type),
intval($type2),
protect_sprintf(dbesc($s))
);
}
else {
return sprintf(" AND " . (($table) ? dbesc($table) . '.' : '') . "id in (select term.oid from term where term.ttype = %d and term.term = '%s' and term.uid = " . (($table) ? dbesc($table) . '.' : '') . "uid ) ",
intval($type),
protect_sprintf(dbesc($s))
);
}
}
function term_item_parent_query($uid,$table,$s,$type = TERM_UNKNOWN, $type2 = '') {
// Allow asterisks for wildcard search
// In theory this means '%' will also do a wildcard search, but there appear to be multiple escape
// issues with '%' in term names and trying to fix this with '\\%' here did not help.
// Ideally I think we want '*' to indicate wildcards and allow '%' literally in names, but that is being
// left for another developer on another day.
$s = str_replace('*','%',$s);
if($type2) {
$r = q("select parent from item left join term on term.oid = item.id where term.ttype in (%d, %d) and term.term like '%s' and term.uid = %d and term.otype = 1 and item.verb NOT IN ('Update', '%s')",
intval($type),
intval($type2),
dbesc($s),
intval($uid),
dbesc(ACTIVITY_UPDATE)
);
}
else {
$r = q("select parent from item left join term on term.oid = item.id where term.ttype = %d and term.term like '%s' and term.uid = %d and term.otype = 1 and item.verb NOT IN ('Update', '%s')",
intval($type),
dbesc($s),
intval($uid),
dbesc(ACTIVITY_UPDATE)
);
}
if($r) {
$str = '';
foreach($r as $rv) {
if($str)
$str .= ',';
$str .= intval($rv['parent']);
}
return " AND " . (($table) ? dbesc($table) . '.' : '') . "id in ( $str ) ";
}
return " AND false ";
}
function store_item_tag($uid,$iid,$otype,$type,$term,$url = '') {
if(! $term)
return false;
$r = q("select * from term
where uid = %d and oid = %d and otype = %d and ttype = %d
and term = '%s' and url = '%s' ",
intval($uid),
intval($iid),
intval($otype),
intval($type),
dbesc($term),
dbesc($url)
);
if($r)
return false;
$r = q("insert into term (uid, oid, otype, ttype, term, url)
values( %d, %d, %d, %d, '%s', '%s') ",
intval($uid),
intval($iid),
intval($otype),
intval($type),
dbesc($term),
dbesc($url)
);
return $r;
}
function get_terms_oftype($arr, $type) {
$ret = [];
if(!is_array($arr))
return $ret;
if(! is_array($type))
$type = [$type];
foreach($type as $t)
foreach($arr as $x)
if($x['ttype'] == $t)
$ret[] = $x;
return $ret;
}
function format_term_for_display($term) {
$s = '';
if(($term['ttype'] == TERM_HASHTAG) || ($term['ttype'] == TERM_COMMUNITYTAG))
$s .= '#';
elseif($term['ttype'] == TERM_FORUM)
$s .= '!';
elseif($term['ttype'] == TERM_MENTION)
$s .= '@';
else
return $s;
if($term['url'])
$s .= '<a href="' . $term['url'] . '">' . htmlspecialchars($term['term'], ENT_COMPAT,'UTF-8') . '</a>';
else
$s .= htmlspecialchars($term['term'], ENT_COMPAT,'UTF-8');
return $s;
}
// Tag cloud functions - need to be adpated to this database format
function tagadelic($uid, $count = 0, $authors = '', $owner = '', $flags = 0, $restrict = 0, $type = TERM_HASHTAG) {
require_once('include/security.php');
if(! perm_is_allowed($uid,get_observer_hash(),'view_stream'))
return array();
$item_normal = item_normal();
$sql_options = item_permissions_sql($uid);
$count = intval($count);
if($flags) {
if($flags === 'wall')
$sql_options .= " and item_wall = 1 ";
}
if($authors) {
if(! is_array($authors))
$authors = array($authors);
$sql_options .= " and author_xchan in (" . stringify_array($authors,true) . ") ";
}
if($owner) {
$sql_options .= " and owner_xchan = '" . dbesc($owner) . "' ";
}
// Fetch tags
$r = q("select term, count(term) as total from term left join item on term.oid = item.id
where term.uid = %d and term.ttype = %d
and otype = %d and item_type = %d
$sql_options $item_normal
group by term order by total desc %s",
intval($uid),
intval($type),
intval(TERM_OBJ_POST),
intval($restrict),
((intval($count)) ? "limit $count" : '')
);
if(! $r)
return array();
return Zotlabs\Text\Tagadelic::calc($r);
}
function card_tagadelic($uid, $count = 0, $authors = '', $owner = '', $flags = 0, $restrict = 0, $type = TERM_CATEGORY) {
require_once('include/security.php');
if(! perm_is_allowed($uid,get_observer_hash(),'view_pages'))
return array();
$item_normal = " and item.item_hidden = 0 and item.item_deleted = 0 and item.item_unpublished = 0 and item.item_delayed = 0 and item.item_pending_remove = 0
and item.item_blocked = 0 and item.obj_type != 'http://purl.org/zot/activity/file' ";
$sql_options = item_permissions_sql($uid);
$count = intval($count);
if($flags) {
if($flags === 'wall')
$sql_options .= " and item_wall = 1 ";
}
if($authors) {
if(! is_array($authors))
$authors = array($authors);
$sql_options .= " and author_xchan in (" . stringify_array($authors,true) . ") ";
}
if($owner) {
$sql_options .= " and owner_xchan = '" . dbesc($owner) . "' ";
}
// Fetch tags
$r = q("select term, count(term) as total from term left join item on term.oid = item.id
where term.uid = %d and term.ttype = %d
and otype = %d and item_type = %d
$sql_options $item_normal
group by term order by total desc %s",
intval($uid),
intval($type),
intval(TERM_OBJ_POST),
intval(ITEM_TYPE_CARD),
((intval($count)) ? "limit $count" : '')
);
if(! $r)
return array();
return Zotlabs\Text\Tagadelic::calc($r);
}
function article_tagadelic($uid, $count = 0, $authors = '', $owner = '', $flags = 0, $restrict = 0, $type = TERM_CATEGORY) {
require_once('include/security.php');
if(! perm_is_allowed($uid,get_observer_hash(),'view_pages'))
return array();
$item_normal = " and item.item_hidden = 0 and item.item_deleted = 0 and item.item_unpublished = 0 and item.item_delayed = 0 and item.item_pending_remove = 0
and item.item_blocked = 0 and item.obj_type != 'http://purl.org/zot/activity/file' ";
$sql_options = item_permissions_sql($uid);
$count = intval($count);
if($flags) {
if($flags === 'wall')
$sql_options .= " and item_wall = 1 ";
}
if($authors) {
if(! is_array($authors))
$authors = array($authors);
$sql_options .= " and author_xchan in (" . stringify_array($authors,true) . ") ";
}
if($owner) {
$sql_options .= " and owner_xchan = '" . dbesc($owner) . "' ";
}
// Fetch tags
$r = q("select term, count(term) as total from term left join item on term.oid = item.id
where term.uid = %d and term.ttype = %d
and otype = %d and item_type = %d
$sql_options $item_normal
group by term order by total desc %s",
intval($uid),
intval($type),
intval(TERM_OBJ_POST),
intval(ITEM_TYPE_ARTICLE),
((intval($count)) ? "limit $count" : '')
);
if(! $r)
return array();
return Zotlabs\Text\Tagadelic::calc($r);
}
function pubtagblock($net,$site,$limit,$recent = 0,$safemode = 1, $type = TERM_HASHTAG) {
$o = '';
$r = pub_tagadelic($net,$site,$limit,$recent,$safemode,$type);
$link = z_root() . '/pubstream';
if($r) {
$o = '<div class="tagblock widget"><h3>' . (($recent) ? t('Trending') : t('Tags')) . '</h3><div class="tags" align="center">';
foreach($r as $rr) {
$o .= '<span class="tag'.$rr[2].'">#</span><a href="'.$link . '?tag=' . urlencode($rr[0]).'" class="tag'.$rr[2].'">'.$rr[0].'</a> ' . "\r\n";
}
$o .= '</div></div>';
}
return $o;
}
function pub_tagadelic($net, $site, $limit, $recent, $safemode, $type) {
$item_normal = item_normal();
$count = intval($limit);
$sys = get_sys_channel();
$uids = " and item.uid = " . intval($sys['channel_id']) . " ";
$sql_extra = item_permissions_sql($sys['channel_id']);
$site_firehose_sql = '';
if ($site) {
$site_firehose_sql = " and owner_xchan in (select channel_hash from channel where channel_system = 0 and channel_removed = 0) ";
}
if($recent) {
$sql_extra .= " and item.created > NOW() - INTERVAL " . db_quoteinterval(intval($recent) . ' DAY') . " ";
}
if($safemode) {
$unsafetags = get_config('system','unsafepubtags', [ 'boobs', 'bot', 'rss', 'girl','girls', 'nsfw', 'sexy', 'nude' ]);
if($unsafetags) {
$sql_extra .= " and not term.term in ( " . stringify_array($unsafetags,true) . ") ";
}
}
$key = __FUNCTION__ . "-" . md5($site . $recent . $safemode . $limit . $type);
$content = Cache::get($key, '5 MINUTE');
if(! $content) {
$content = Cache::get($key, '1 MONTH');
$arr = [
"SELECT term, count(term) AS total FROM term LEFT JOIN item ON term.oid = item.id
WHERE term.ttype = %d
AND otype = %d
AND item_type = %d
AND item_private = 0
$uids $item_normal $site_firehose_sql $sql_extra
GROUP BY term ORDER BY total DESC %s",
intval($type),
intval(TERM_OBJ_POST),
intval(ITEM_TYPE_POST),
(intval($count) ? "LIMIT $count" : '')
];
\Zotlabs\Daemon\Master::Summon([ 'Cache_query', $key, base64_encode(json_encode($arr)) ]);
}
$r = unserialize($content);
if(! $r)
return [];
return Zotlabs\Text\Tagadelic::calc($r);
}
function dir_tagadelic($count = 0, $hub = '') {
$count = intval($count);
if($hub) {
$r = q("select xtag_term as term, count(xtag_term) as total from xtag
left join hubloc on xtag_hash = hubloc_hash
where xtag_flags = 0 and xtag_hash in (select hubloc_hash from hubloc where hubloc_host = '%s' )
group by xtag_term order by total desc %s",
dbesc($hub),
((intval($count)) ? "limit $count" : '')
);
}
else {
$r = q("select xtag_term as term, count(xtag_term) as total from xtag where xtag_flags = 0
group by xtag_term order by total desc %s",
((intval($count)) ? "limit $count" : '')
);
}
if(! $r)
return [];
return Zotlabs\Text\Tagadelic::calc($r);
}
function app_tagblock($link,$count = 0) {
$o = '';
$r = app_tagadelic($count);
if($r) {
$o = '<div class="tagblock widget"><h3>' . t('Categories') . '</h3><div class="tags" align="center">';
foreach($r as $rr) {
$o .= '<a href="'.$link .'/' . '?f=&cat=' . urlencode($rr[0]).'" class="tag'.$rr[2].'">'.$rr[0].'</a> ' . "\r\n";
}
$o .= '</div></div>';
}
return $o;
}
function app_tagadelic($count = 0) {
if(! local_channel())
return '';
$count = intval($count);
// Fetch tags
$r = q("select term, count(term) as total from term left join app on term.uid = app_channel where term.uid = %d
and term.otype = %d group by term order by total desc %s",
intval(local_channel()),
intval(TERM_OBJ_APP),
((intval($count)) ? "limit $count" : '')
);
if(! $r)
return array();
return Zotlabs\Text\Tagadelic::calc($r);
}
function tagblock($link,$uid,$count = 0,$authors = '',$owner = '', $flags = 0,$restrict = 0,$type = TERM_HASHTAG) {
$o = '';
$r = tagadelic($uid,$count,$authors,$owner, $flags,$restrict,$type);
if($r) {
$o = '<div class="tagblock widget"><h3>' . t('Tags') . '</h3><div class="tags" align="center">';
foreach($r as $rr) {
$o .= '<span class="tag'.$rr[2].'">#</span><a href="'.$link .'/' . '?f=&tag=' . urlencode($rr[0]).'" class="tag'.$rr[2].'">'.$rr[0].'</a> ' . "\r\n";
}
$o .= '</div></div>';
}
return $o;
}
function wtagblock($uid,$count = 0,$authors = '',$owner = '', $flags = 0,$restrict = 0,$type = TERM_HASHTAG) {
$o = '';
$r = tagadelic($uid,$count,$authors,$owner, $flags,$restrict,$type);
if($r) {
$c = q("select channel_address from channel where channel_id = %d limit 1",
intval($uid)
);
$o = '<div class="tagblock widget"><h3>' . t('Tags') . '</h3><div class="tags" align="center">';
foreach($r as $rr) {
$o .= '<span class="tag' . $rr[2] . '">#</span><a href="channel/' . $c[0]['channel_address'] . '?f=&tag=' . urlencode($rr[0]).'" class="tag'.$rr[2].'">'.$rr[0].'</a> ' . "\r\n";
}
$o .= '</div></div>';
}
return $o;
}
function catblock($uid,$count = 0,$authors = '',$owner = '', $flags = 0,$restrict = 0,$type = TERM_CATEGORY) {
$o = '';
$r = tagadelic($uid,$count,$authors,$owner,$flags,$restrict,$type);
if($r) {
$c = q("select channel_address from channel where channel_id = %d limit 1",
intval($uid)
);
$o = '<div class="tagblock widget"><h3>' . t('Categories') . '</h3><div class="tags" align="center">';
foreach($r as $rr) {
$o .= '<a href="channel/' . $c[0]['channel_address']. '?f=&cat=' . urlencode($rr[0]).'" class="tag'.$rr[2].'">'.$rr[0].'</a> ' . "\r\n";
}
$o .= '</div></div>';
}
return $o;
}
function card_catblock($uid,$count = 0,$authors = '',$owner = '', $flags = 0,$restrict = 0,$type = TERM_CATEGORY) {
$o = '';
$r = card_tagadelic($uid,$count,$authors,$owner,$flags,$restrict,$type);
if($r) {
$c = q("select channel_address from channel where channel_id = %d limit 1",
intval($uid)
);
$o = '<div class="tagblock widget"><h3>' . t('Categories') . '</h3><div class="tags" align="center">';
foreach($r as $rr) {
$o .= '<a href="cards/' . $c[0]['channel_address']. '?f=&cat=' . urlencode($rr[0]).'" class="tag'.$rr[2].'">'.$rr[0].'</a> ' . "\r\n";
}
$o .= '</div></div>';
}
return $o;
}
function article_catblock($uid,$count = 0,$authors = '',$owner = '', $flags = 0,$restrict = 0,$type = TERM_CATEGORY) {
$o = '';
$r = article_tagadelic($uid,$count,$authors,$owner,$flags,$restrict,$type);
if($r) {
$c = q("select channel_address from channel where channel_id = %d limit 1",
intval($uid)
);
$o = '<div class="tagblock widget"><h3>' . t('Categories') . '</h3><div class="tags" align="center">';
foreach($r as $rr) {
$o .= '<a href="articles/' . $c[0]['channel_address']. '?f=&cat=' . urlencode($rr[0]).'" class="tag'.$rr[2].'">'.$rr[0].'</a> ' . "\r\n";
}
$o .= '</div></div>';
}
return $o;
}
function dir_tagblock($link,$r) {
$o = '';
if(! $r)
$r = App::$data['directory_keywords'] ?? [];
if($r) {
$o = '<div class="dirtagblock widget"><h3>' . t('Keywords') . '</h3><div class="tags" align="center">';
foreach($r as $rr) {
$o .= '<a href="'.$link .'/' . '?f=&keywords=' . urlencode($rr['term']).'" class="tag'.$rr['normalise'].'" rel="nofollow" >'.$rr['term'].'</a> ' . "\r\n";
}
$o .= '</div></div>';
}
return $o;
}
/**
* verbs: [0] = first person singular, e.g. "I want", [1] = 3rd person singular, e.g. "Bill wants"
* We use the first person form when creating an activity, but the third person for use in activities
* FIXME: There is no accounting for verb gender for languages where this is significant. We may eventually
* require obj_verbs() to provide full conjugations and specify which form to use in the $_REQUEST params to this module.
*/
function obj_verbs() {
$verbs = array(
'has' => array( t('have'), t('has')),
'wants' => array( t('want'), t('wants')),
'likes' => array( t('like'), t('likes')),
'dislikes' => array( t('dislike'), t('dislikes')),
);
$arr = array('verbs' => $verbs);
call_hooks('obj_verbs', $arr);
return $arr['verbs'];
}
function obj_verb_selector($current = '') {
$verbs = obj_verbs();
$o = '<select class="obj-verb-selector" name="verb">';
foreach($verbs as $k => $v) {
$selected = (($k == $current) ? ' selected="selected" ' : '');
$o .= '<option value="' . urlencode($k) . '"' . $selected . '>' . $v[1] . '</option>';
}
$o .= '</select>';
return $o;
}
function get_things($profile_hash,$uid) {
$sql_extra = permissions_sql($uid);
$sql_extra_profile = (($profile_hash) ? " and obj_page = '" . $profile_hash . "' " : '');
$r = q("select * from obj where obj_channel = %d and obj_type = %d $sql_extra $sql_extra_profile order by obj_verb, obj_term",
intval($uid),
intval(TERM_OBJ_THING)
);
$things = $sorted_things = null;
$profile_hashes = array();
if($r) {
// if no profile_hash was specified (display on profile page mode), match each of the things to a profile name
// (list all my things mode). This is harder than it sounds.
foreach($r as $rr) {
$rr['profile_name'] = '';
if(! in_array($rr['obj_obj'],$profile_hashes))
$profile_hashes[] = $rr['obj_obj'];
}
if(! $profile_hash) {
$exp = stringify_array($profile_hashes,true);
$p = q("select profile_guid as hash, profile_name as name from profile where profile_guid in ( $exp ) ");
if($p) {
foreach($r as $rr) {
foreach($p as $pp) {
if($rr['obj_page'] == $pp['hash']) {
$rr['profile_name'] == $pp['name'];
}
}
}
}
}
$things = array();
// Use the system obj_verbs array as a sort key, since we don't really
// want an alphabetic sort. To change the order, use a plugin to
// alter the obj_verbs() array or alter it in code. Unknown verbs come
// after the known ones - in no particular order.
$v = obj_verbs();
foreach($v as $k => $foo)
$things[$k] = null;
foreach($r as $rr) {
$l = q("select xchan_name, xchan_photo_s, xchan_url from likes left join xchan on liker = xchan_hash where
target_type = '%s' and target_id = '%s' and channel_id = %d",
dbesc(ACTIVITY_OBJ_THING),
dbesc($rr['obj_obj']),
intval($uid)
);
for($x = 0; $x < count($l); $x ++) {
$l[$x]['xchan_url'] = zid($l[$x]['xchan_url']);
$l[$x]['xchan_photo_s'] = zid($l[$x]['xchan_photo_s']);
}
if(! $things[$rr['obj_verb']])
$things[$rr['obj_verb']] = array();
$things[$rr['obj_verb']][] = array('term' => $rr['obj_term'],'url' => $rr['obj_url'],'img' => $rr['obj_imgurl'], 'editurl' => z_root() . '/thing/' . $rr['obj_obj'], 'profile' => $rr['profile_name'],'term_hash' => $rr['obj_obj'], 'likes' => $l,'like_count' => count($l),'like_label' => tt('Like','Likes',count($l),'noun'));
}
$sorted_things = array();
if($things) {
foreach($things as $k => $v) {
if(is_array($things[$k])) {
$sorted_things[$k] = $v;
}
}
}
}
//logger('things: ' . print_r($sorted_things,true));
return $sorted_things;
}
|