2013-06-20 04:50:14 +00:00
< ? 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.
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 ;
2016-06-01 04:45:33 +00:00
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 ) " ,
2013-06-20 04:50:14 +00:00
intval ( $termtype ),
protect_sprintf ( dbesc ( $s ))
);
}
2015-11-20 08:15:48 +00:00
function term_query ( $table , $s , $type = TERM_UNKNOWN , $type2 = '' ) {
if ( $type2 ) {
2016-06-01 04:45:33 +00:00
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 ) " ,
2015-11-20 08:15:48 +00:00
intval ( $type ),
intval ( $type2 ),
protect_sprintf ( dbesc ( $s ))
);
}
else {
2016-06-01 04:45:33 +00:00
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 ) " ,
2015-11-20 08:15:48 +00:00
intval ( $type ),
protect_sprintf ( dbesc ( $s ))
);
}
2013-06-20 04:50:14 +00:00
}
function store_item_tag ( $uid , $iid , $otype , $type , $term , $url = '' ) {
if ( ! $term )
return false ;
2015-03-03 23:03:19 +00:00
2013-06-20 04:50:14 +00:00
$r = q ( " select * from term
2016-06-01 04:45:33 +00:00
where uid = % d and oid = % d and otype = % d and ttype = % d
2013-06-20 04:50:14 +00:00
and term = '%s' and url = '%s' " ,
intval ( $uid ),
intval ( $iid ),
intval ( $otype ),
intval ( $type ),
dbesc ( $term ),
dbesc ( $url )
);
if ( $r )
return false ;
2015-03-03 23:03:19 +00:00
2016-06-01 04:45:33 +00:00
$r = q ( " insert into term (uid, oid, otype, ttype, term, url)
2013-06-20 04:50:14 +00:00
values ( % d , % d , % d , % d , '%s' , '%s' ) " ,
intval ( $uid ),
intval ( $iid ),
intval ( $otype ),
intval ( $type ),
dbesc ( $term ),
dbesc ( $url )
);
2015-03-03 23:03:19 +00:00
2013-06-20 04:50:14 +00:00
return $r ;
}
2015-03-03 23:03:19 +00:00
2013-06-20 04:50:14 +00:00
function get_terms_oftype ( $arr , $type ) {
$ret = array ();
if ( ! ( is_array ( $arr ) && count ( $arr )))
return $ret ;
if ( ! is_array ( $type ))
$type = array ( $type );
foreach ( $type as $t )
foreach ( $arr as $x )
2016-06-01 04:45:33 +00:00
if ( $x [ 'ttype' ] == $t )
2013-06-20 04:50:14 +00:00
$ret [] = $x ;
2015-03-03 23:03:19 +00:00
2013-06-20 04:50:14 +00:00
return $ret ;
}
function format_term_for_display ( $term ) {
$s = '' ;
2016-06-01 04:45:33 +00:00
if (( $term [ 'ttype' ] == TERM_HASHTAG ) || ( $term [ 'ttype' ] == TERM_COMMUNITYTAG ))
2013-06-20 04:50:14 +00:00
$s .= '#' ;
2016-06-01 04:45:33 +00:00
elseif ( $term [ 'ttype' ] == TERM_MENTION )
2013-06-20 04:50:14 +00:00
$s .= '@' ;
2013-06-21 03:34:00 +00:00
else
return $s ;
2013-06-20 04:50:14 +00:00
2013-06-21 03:34:00 +00:00
if ( $term [ 'url' ])
2013-12-12 10:15:02 +00:00
$s .= '<a href="' . $term [ 'url' ] . '">' . htmlspecialchars ( $term [ 'term' ], ENT_COMPAT , 'UTF-8' ) . '</a>' ;
2013-06-21 03:34:00 +00:00
else
2013-12-12 10:15:02 +00:00
$s .= htmlspecialchars ( $term [ 'term' ], ENT_COMPAT , 'UTF-8' );
2013-06-20 04:50:14 +00:00
return $s ;
}
2013-06-21 22:15:07 +00:00
// Tag cloud functions - need to be adpated to this database format
2015-07-16 04:43:42 +00:00
function tagadelic ( $uid , $count = 0 , $authors = '' , $owner = '' , $flags = 0 , $restrict = 0 , $type = TERM_HASHTAG ) {
2013-07-04 00:22:40 +00:00
2015-02-18 02:13:20 +00:00
require_once ( 'include/security.php' );
2015-03-03 23:03:19 +00:00
2015-02-18 02:13:20 +00:00
if ( ! perm_is_allowed ( $uid , get_observer_hash (), 'view_stream' ))
return array ();
2015-11-27 01:35:41 +00:00
$item_normal = item_normal ();
2015-02-18 02:13:20 +00:00
$sql_options = item_permissions_sql ( $uid );
2013-09-18 05:52:46 +00:00
$count = intval ( $count );
2013-07-03 04:30:27 +00:00
2015-01-23 01:41:16 +00:00
if ( $flags ) {
if ( $flags === 'wall' )
$sql_options .= " and item_wall = 1 " ;
}
2013-10-27 23:30:55 +00:00
2013-07-04 00:22:40 +00:00
if ( $authors ) {
if ( ! is_array ( $authors ))
$authors = array ( $authors );
2015-03-03 23:03:19 +00:00
2013-07-04 00:22:40 +00:00
stringify_array_elms ( $authors , true );
$sql_options .= " and author_xchan in ( " . implode ( ',' , $authors ) . " ) " ;
}
2015-07-16 04:43:42 +00:00
if ( $owner ) {
$sql_options .= " and owner_xchan = ' " . dbesc ( $owner ) . " ' " ;
}
2013-06-21 22:15:07 +00:00
// Fetch tags
2013-07-03 04:19:12 +00:00
$r = q ( " select term, count(term) as total from term left join item on term.oid = item.id
2016-06-01 04:45:33 +00:00
where term . uid = % d and term . ttype = % d
2015-01-29 22:51:41 +00:00
and otype = % d and item_type = % d and item_private = 0
2015-11-27 01:35:41 +00:00
$sql_options $item_normal
2013-06-21 22:15:07 +00:00
group by term order by total desc % s " ,
intval ( $uid ),
intval ( $type ),
intval ( TERM_OBJ_POST ),
2013-10-27 23:30:55 +00:00
intval ( $restrict ),
2013-06-21 22:15:07 +00:00
(( intval ( $count )) ? " limit $count " : '' )
);
2013-07-03 04:30:27 +00:00
2013-06-21 22:15:07 +00:00
if ( ! $r )
return array ();
2015-03-03 23:03:19 +00:00
2016-05-05 02:39:39 +00:00
return Zotlabs\Text\Tagadelic :: calc ( $r );
2015-03-03 23:03:19 +00:00
2013-06-21 22:15:07 +00:00
}
2013-09-18 05:52:46 +00:00
function dir_tagadelic ( $count = 0 ) {
$count = intval ( $count );
// Fetch tags
2016-05-05 02:39:39 +00:00
$r = q ( " select xtag_term as term, count(xtag_term) as total from xtag where xtag_flags = 0
2013-09-18 05:56:09 +00:00
group by xtag_term order by total desc % s " ,
2013-09-18 05:52:46 +00:00
(( intval ( $count )) ? " limit $count " : '' )
);
if ( ! $r )
return array ();
2015-03-03 23:03:19 +00:00
2013-09-18 05:52:46 +00:00
2016-05-05 02:39:39 +00:00
return Zotlabs\Text\Tagadelic :: calc ( $r );
2013-09-18 05:52:46 +00:00
}
2016-05-05 01:27:46 +00:00
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 ();
2016-05-05 02:39:39 +00:00
return Zotlabs\Text\Tagadelic :: calc ( $r );
2016-05-05 01:27:46 +00:00
}
2015-07-16 04:43:42 +00:00
function tagblock ( $link , $uid , $count = 0 , $authors = '' , $owner = '' , $flags = 0 , $restrict = 0 , $type = TERM_HASHTAG ) {
2015-03-03 23:03:19 +00:00
$o = '' ;
2015-07-16 04:43:42 +00:00
$r = tagadelic ( $uid , $count , $authors , $owner , $flags , $restrict , $type );
2015-03-03 23:03:19 +00:00
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>' ;
2013-06-21 22:15:07 +00:00
}
2015-03-03 23:03:19 +00:00
2013-07-03 04:04:46 +00:00
return $o ;
2013-06-21 22:15:07 +00:00
}
2013-09-18 05:52:46 +00:00
2015-03-03 23:03:19 +00:00
2015-07-16 04:43:42 +00:00
function wtagblock ( $uid , $count = 0 , $authors = '' , $owner = '' , $flags = 0 , $restrict = 0 , $type = TERM_HASHTAG ) {
2015-03-03 23:03:19 +00:00
$o = '' ;
2014-12-15 22:37:31 +00:00
2015-07-16 04:43:42 +00:00
$r = tagadelic ( $uid , $count , $authors , $owner , $flags , $restrict , $type );
2014-12-15 22:37:31 +00:00
2015-03-03 23:03:19 +00:00
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>' ;
2014-12-15 22:37:31 +00:00
}
2015-03-03 23:03:19 +00:00
2014-12-15 22:37:31 +00:00
return $o ;
}
2015-07-16 04:43:42 +00:00
function catblock ( $uid , $count = 0 , $authors = '' , $owner = '' , $flags = 0 , $restrict = 0 , $type = TERM_CATEGORY ) {
2015-03-03 23:03:19 +00:00
$o = '' ;
2014-11-06 09:24:04 +00:00
2015-07-16 04:43:42 +00:00
$r = tagadelic ( $uid , $count , $authors , $owner , $flags , $restrict , $type );
2014-11-06 09:24:04 +00:00
2015-03-03 23:03:19 +00:00
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>' ;
2014-11-06 09:24:04 +00:00
}
2015-03-03 23:03:19 +00:00
2014-11-06 09:24:04 +00:00
return $o ;
}
2013-09-18 05:52:46 +00:00
function dir_tagblock ( $link , $r ) {
2013-12-20 20:43:04 +00:00
$o = '' ;
2013-09-18 05:52:46 +00:00
2015-04-16 05:07:20 +00:00
$observer = get_observer_hash ();
2015-07-07 20:29:27 +00:00
if ( ! get_directory_setting ( $observer , 'globaldir' ))
2015-04-16 05:07:20 +00:00
return $o ;
2013-09-18 05:52:46 +00:00
2013-12-20 20:43:04 +00:00
if ( ! $r )
2016-03-31 23:06:03 +00:00
$r = App :: $data [ 'directory_keywords' ];
2013-12-20 20:43:04 +00:00
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>' ;
2013-09-18 05:52:46 +00:00
}
2015-03-03 23:03:19 +00:00
2013-09-18 05:52:46 +00:00
return $o ;
}
2013-07-04 03:37:39 +00:00
/**
* verbs : [ 0 ] = first person singular , e . g . " I want " , [ 1 ] = 3 rd 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 .
*/
2013-07-04 02:30:52 +00:00
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 );
2015-03-03 23:03:19 +00:00
2013-07-04 02:30:52 +00:00
return $arr [ 'verbs' ];
}
2013-07-04 03:37:39 +00:00
2013-12-30 12:25:55 +00:00
function obj_verb_selector ( $current = '' ) {
2013-07-05 03:35:42 +00:00
$verbs = obj_verbs ();
2015-03-03 23:03:19 +00:00
$o = '<select class="obj-verb-selector" name="verb">' ;
2013-07-05 03:35:42 +00:00
foreach ( $verbs as $k => $v ) {
2013-12-30 12:25:55 +00:00
$selected = (( $k == $current ) ? ' selected="selected" ' : '' );
2014-06-24 00:05:03 +00:00
$o .= '<option value="' . urlencode ( $k ) . '"' . $selected . '>' . $v [ 1 ] . '</option>' ;
2013-07-05 03:35:42 +00:00
}
$o .= '</select>' ;
2015-03-03 23:03:19 +00:00
return $o ;
2013-12-29 01:04:23 +00:00
}
function get_things ( $profile_hash , $uid ) {
$sql_extra = (( $profile_hash ) ? " and obj_page = ' " . $profile_hash . " ' " : '' );
2015-03-03 23:03:19 +00:00
2015-09-02 00:06:58 +00:00
$r = q ( " select * from obj where obj_channel = %d and obj_type = %d $sql_extra order by obj_verb, obj_term " ,
2013-12-29 01:04:23 +00:00
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' ] = '' ;
2015-09-02 00:06:58 +00:00
if ( ! in_array ( $rr [ 'obj_obj' ], $profile_hashes ))
$profile_hashes [] = $rr [ 'obj_obj' ];
2013-12-29 01:04:23 +00:00
}
stringify_array_elms ( $profile_hashes );
if ( ! $profile_hash ) {
$exp = explode ( ',' , $profile_hashes );
$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 ) {
2014-06-23 12:19:28 +00:00
2016-02-02 01:42:53 +00:00
$l = q ( " select xchan_name, xchan_photo_s, xchan_url from likes left join xchan on likee = xchan_hash where
2014-06-23 12:19:28 +00:00
target_type = '%s' and target_id = '%s' and channel_id = % d " ,
dbesc ( ACTIVITY_OBJ_THING ),
2015-09-02 00:06:58 +00:00
dbesc ( $rr [ 'obj_obj' ]),
2014-06-23 12:19:28 +00:00
intval ( $uid )
);
2016-02-02 01:42:53 +00:00
for ( $x = 0 ; $x < count ( $l ); $x ++ ) {
2014-06-23 12:19:28 +00:00
$l [ $x ][ 'xchan_url' ] = zid ( $l [ $x ][ 'xchan_url' ]);
2016-02-02 01:42:53 +00:00
$l [ $x ][ 'xchan_photo_s' ] = zid ( $l [ $x ][ 'xchan_photo_s' ]);
}
2013-12-29 01:04:23 +00:00
if ( ! $things [ $rr [ 'obj_verb' ]])
$things [ $rr [ 'obj_verb' ]] = array ();
2014-06-23 12:19:28 +00:00
2015-09-02 00:06:58 +00:00
$things [ $rr [ 'obj_verb' ]][] = array ( 'term' => $rr [ 'obj_term' ], 'url' => $rr [ 'obj_url' ], 'img' => $rr [ 'obj_imgurl' ], 'profile' => $rr [ 'profile_name' ], 'term_hash' => $rr [ 'obj_obj' ], 'likes' => $l , 'like_count' => count ( $l ), 'like_label' => tt ( 'Like' , 'Likes' , count ( $l ), 'noun' ));
2013-12-29 01:04:23 +00:00
}
$sorted_things = array ();
if ( $things ) {
foreach ( $things as $k => $v ) {
if ( is_array ( $things [ $k ])) {
$sorted_things [ $k ] = $v ;
}
}
}
}
2014-06-23 12:19:28 +00:00
//logger('things: ' . print_r($sorted_things,true));
2013-12-29 01:04:23 +00:00
2015-03-03 23:03:19 +00:00
return $sorted_things ;
2015-07-07 19:41:37 +00:00
}