Paste
Pasted as PHP by registered user atmaca ( 4 days ago )
/* Parça 2/2 */
if(!$cgroup||!$destgroup)return false;
if(!is_array($cgroup))
$cgroup=array($cgroup);
$db = vbseo_get_db();
$db->vbseodb_query("
UPDATE " . vbseo_tbl_prefix('vbseo_likes') . "
SET l_cgroup = ". intval($destgroup)."
WHERE l_ctype = " . intval($ctype) . "
AND l_cgroup IN (" . implode(',',$cgroup) . " )
");
$dn = $db->vbseo_affected_rows();
self::like_counter_type($ctype, $destgroup, $dn);
return true;
}
private static function remove_like($cid, $ctype, $groupid = 0, $userid = 0, $duserid = 0)
{
if(!$userid && (!$userid = vbseo_vb_userid()))
return false;
$db = vbseo_get_db();
$db->vbseodb_query("
DELETE FROM " . vbseo_tbl_prefix('vbseo_likes') . "
WHERE l_ctype = " . intval($ctype) . "
AND l_cgroup = " . intval($groupid) . "
AND l_contentid = " . intval($cid) . "
AND l_from_userid = ".intval($userid)."
");
$dn = $db->vbseo_affected_rows();
self::like_counter_type($ctype, $groupid, -$dn);
self::like_counter_user($userid, $duserid, -$dn);
return true;
}
public static function add_like($cid, $ctype, $groupid = 0, $userid = 0, $duserid = 0, $dateline = 0, $from_username = '')
{
if(!$userid)
{
$userid = vbseo_vb_userid();
$from_username = vbseo_vb_userinfo('username');
}
if(!$userid || !$from_username)
return false;
if(!$dateline)$dateline = time();
$db = vbseo_get_db();
$db->vbseodb_query($q="
INSERT INTO " . vbseo_tbl_prefix('vbseo_likes') . "
SET l_contentid = " . intval($cid) . ",
l_ctype = " . intval($ctype) . ",
l_cgroup = " . intval($groupid) . ",
l_from_userid = " . intval($userid) . ",
l_from_username= '" . vbseo_db_escape($from_username) . "',
l_dest_userid = " . intval($duserid) . ",
l_dateline = " . intval($dateline) . "
");
self::like_counter_user($userid, $duserid, 1);
self::like_counter_type($ctype, $groupid, 1);
return true;
}
public static function like_counter($userid, $l_in, $l_out)
{
if($l_in)
{
self::like_counter_spec($userid, 'vbseo_likes_in', $l_in);
if($l_in>0)
self::like_counter_spec($userid, 'vbseo_likes_unread', $l_in);
}
if($l_out)
{
self::like_counter_spec($userid, 'vbseo_likes_out', $l_out);
}
return true;
}
public static function like_counter_user($userid, $duserid, $dn)
{
self::like_counter($userid, 0, $dn);
self::like_counter($duserid, $dn, 0);
}
public static function like_counter_spec($userid, $field, $lnum)
{
if(!$userid || !$lnum)
return false;
$sets = ($lnum>0) ? "$field=$field+$lnum" :
"$field=if($field<".abs($lnum).", 0, $field".$lnum.")";
$db = vbseo_get_db();
$db->vbseodb_query("UPDATE " . vbseo_tbl_prefix('user') .
" SET $sets WHERE userid=".intval($userid));
return true;
}
public static function like_counter_type($ctype, $cgroup, $lnum)
{
$uptbl = array(VBSEO_UI_THREAD => 'thread', VBSEO_UI_BLOG => 'blog', VBSEO_UI_CMS => 'cms_nodeinfo');
$upfld = array(VBSEO_UI_THREAD => 'threadid', VBSEO_UI_BLOG => 'blogid', VBSEO_UI_CMS => 'nodeid');
$t = $uptbl[$ctype];
$f = $upfld[$ctype];
if(!$t || !$f)return false;
$field= 'vbseo_likes';
$sets = ($lnum>0) ? "$field=$field+$lnum" :
"$field=if($field<".abs($lnum).", 0, $field".$lnum.")";
$db = vbseo_get_db();
$db->vbseodb_query($q="UPDATE " . vbseo_tbl_prefix($t) . " SET $sets WHERE $f=".intval($cgroup));
return true;
}
public static function lcache_set_a($li, $val, $key = null)
{
self::lcache_set($li['l_ctype'], $li['l_cgroup'], $li['l_contentid'], $val, $key);
}
public static function lcache_get_a($li, $key = null)
{
return self::lcache_get($li['l_ctype'], $li['l_cgroup'], $li['l_contentid'], $key);
}
public static function lcache_set($ctype, $cgroup, $contentid, $val, $key = null)
{
$ret = self::lcache_get($ctype, $cgroup, $contentid);
if($key)
$ret[$key] = $val;
else
foreach($val as $k=>$v)
$ret[$k] = $v;
self::$ginfo[$ctype][$cgroup][$contentid] = $ret;
}
public static function lcache_get($ctype, $cgroup, $contentid = null, $key = null)
{
$gi = $contentid ? self::$ginfo[$ctype][$cgroup][$contentid] : self::$ginfo[$ctype][$cgroup];
return $key ? $gi[$key] : $gi;
}
public static function get_liked_info($ctype, $cgroup, $contentid)
{
global $vbphrase;
$like_count = self::lcache_get($ctype, $cgroup, $contentid, 'count' );
$ret = '';
if($like_count)
{
$ol = self::lcache_get($ctype, $cgroup, $contentid, 'ownlike');
$alikes = self::lcache_get($ctype, $cgroup, $contentid, 'likes');
$ptype= $ol ? 'you' : 'like';
$args = array();
$unum = $un = $ol ? 1 : 0;
for($x = 0; ($unum < self::$shortlist) && ($x<self::$shortlist); $x++)
{
$li = $alikes[$x];
if($li['l_from_userid'] == vbseo_vb_userid())
continue;
if($li)
{
$l = VBSEO_REWRITE_MEMBERS ?
vbseo_member_url($li['l_from_userid'])
: 'member.'.VBSEO_VB_EXT.'?u='.$li['l_from_userid'];
if($unum<3)
{
$unum++;
$args[] = $l;
$args[] = $li['l_from_username'];
}else
{
$linkmore[] = '<a href="'.$l.'">'.$li['l_from_username'].'<a>';
}
}
}
if(($rest = $like_count - $unum)>0)
{
$args[] = $rest;
$unum++;
}
array_unshift($args, $vbphrase[$p='vbseo_'.$ptype.'_'.$unum]);
$ret = VBSEO_VB4 ? construct_phrase($args) : @call_user_func_array('construct_phrase', $args);
if($linkmore)
$ret .= '<br />'.implode(', ', $linkmore);
}
return $ret;
}
public static function extract_likes_page($ctype, $groupid, $contentids)
{
vbseo_int_var($ctype);
vbseo_int_var($groupid);
if(!$ctype || !$groupid || !$contentids)
return false;
if(!is_array($contentids))
$contentids = (($contentids == -1) ? array() :
(strstr($contentids,',') ? explode(',',$contentids) : array($contentids))
);
vbseo_int_var($contentids);
global $vbseo_gcache;
$userid = vbseo_vb_userid();
$contentids = self::extract_likes_bygroup($ctype, $groupid, $userid, $contentids);
if(!$contentids)
return;
$clist = array();
$db = vbseo_get_db();
$gq = $db->vbseodb_query($q="
SELECT * FROM (
SELECT @rn := if( @lc = l.l_contentid, @rn +1, 0 ) AS rnum, @lc := l_contentid, l.*
FROM " . vbseo_tbl_prefix('vbseo_likes') . " l, (SELECT @rn :=0, @lc :=0) r
WHERE l_ctype = ".$ctype." AND l_cgroup =".$groupid." AND l_contentid IN (" . implode(',', $contentids) . " )
ORDER BY l_contentid
) as nest
where rnum<".self::$shortlist."
");
while($li = $db->vbseodb_fetch_assoc($gq))
{
$al = self::lcache_get_a($li, 'likes');
$al[] = $li;
self::lcache_set_a($li, $al, 'likes');
$vbseo_gcache['user'][$li['l_from_userid']] = array(
'userid' => $li['l_from_userid'],
'username'=>$li['l_from_username']
);
}
$db->vbseodb_free_result($gq);
return ;
}
public static function extract_likes_bygroup($ctype, $groupid, $userid = 0, $contentids = array(), $tab = '')
{
vbseo_int_var($ctype);
vbseo_int_var($groupd);
if($tab && ($ctype!=VBSEO_UI_THREAD)) return;
if(!$groupid)return;
global $vbseo_gcache;
$clist = array();
$db = vbseo_get_db();
$q = "
SELECT l_ctype,l_cgroup,l_contentid,l_dest_userid,count(*) as cnt
".($userid ? ",IF(l_from_userid=".intval($userid).",1,0) as ownlike" : "")."
FROM " . vbseo_tbl_prefix('vbseo_likes') . "
WHERE l_ctype = ".$ctype." AND l_cgroup = " . $groupid .
($contentids ? " AND l_contentid IN (".implode(',', $contentids).")" : "")."
GROUP BY l_contentid".($userid ? ", ownlike" : "").
(($tab=='top') ? " ORDER by cnt desc" : "").
" LIMIT 0,".(($tab=='top') ? self::$toplist : self::$longlist);
if($tab)
{
$join = array("LEFT JOIN " . vbseo_tbl_prefix('user') . " u1 on u1.userid = l_dest_userid");
$whr = array("p.postid is not null");
$fields = array();
self::get_likes_query($fields, $join, $whr);
$q = "SELECT nest.*,".implode(',',$fields).
" FROM ($q) as nest ".
implode("\n", $join).
($whr ? " WHERE ".implode(" AND ", $whr):"").
" ORDER BY l_contentid";
}
$gq = $db->vbseodb_query($q);
while($li = $db->vbseodb_fetch_assoc($gq))
{
$clist[] = $li['l_contentid'];
self::lcache_set_a($li, $li);
self::lcache_set_a($li, self::lcache_get_a($li,'count')+$li['cnt'], 'count');
if($li['ownlike'])
self::lcache_set_a($li, true, 'ownlike');
}
$db->vbseodb_free_result($gq);
return $clist;
}
public static function get_likes_query(&$fields, &$join, &$whr)
{
global $vbulletin;
$fields[] = "u1.username , u1.userid, u1.usergroupid ";
if($vbulletin->options['avatarenabled'])
{
$join[] = "LEFT JOIN " . vbseo_tbl_prefix('avatar')." AS avatar ON(avatar.avatarid = u1.avatarid) ";
$join[] = "LEFT JOIN " . vbseo_tbl_prefix('customavatar')." AS customavatar ON(customavatar.userid = u1.userid)";
$join[] = "LEFT JOIN " . vbseo_tbl_prefix('post') . " p on l_ctype = " . VBSEO_UI_THREAD . " AND p.postid = l_contentid ".
(can_moderate(0, 'canmoderateposts') ? "" : " AND p.visible=1 ");
$join[] = "LEFT JOIN " . vbseo_tbl_prefix('thread') . " t on t.threadid = p.threadid";
$fields[] = "p.postid, if(p.title, p.title, t.title) AS posttitle, p.pagetext as posttext, p.dateline AS postdateline";
$fields[] = "t.threadid, t.title AS threadtitle, t.forumid";
$fields[] = "u1.avatarid, u1.avatarrevision, avatar.avatarpath, NOT ISNULL(customavatar.userid) AS hascustomavatar, customavatar.dateline AS avatardateline, NOT ISNULL(customavatar.userid) AS hascustom";
}
}
public static function get_likes_detailed($userid, $duserid, $start = 0, $pgsz = 15, $contentids = array())
{
global $vbulletin;
if(!$flist = vbseo_allowed_forums())
return false;
$start = intval($start);
if($start<1)$start = 1;
$whr[] = "(l_ctype <> " . VBSEO_UI_THREAD . " OR (t.forumid in (".implode(',', $flist).")))";
if($userid)
$whr[] = '(l_from_userid= '.intval($userid).')';
if($duserid)
$whr[] = '(l_dest_userid = '.intval($duserid).')';
$join = array(
"LEFT JOIN " . vbseo_tbl_prefix('user') . " u1 on u1.userid = l.l_from_userid",
"LEFT JOIN " . vbseo_tbl_prefix('user') . " u2 on u2.userid = l.l_dest_userid"
);
$fields = array(
"u2.userid as to_userid, u2.username as to_username"
);
if($contentids)
$whr[] = "l_contentid IN (".implode(',', $contentids).")";
if(VBSEO_LIKE_BLOG && VBSEO_VB_BLOG)
{
$join[] = "LEFT JOIN " . vbseo_tbl_prefix('blog') . " b
on l_ctype = " . VBSEO_UI_BLOG . " AND b.blogid = l.l_cgroup AND b.state='visible' ";
$join[] = "LEFT JOIN " . vbseo_tbl_prefix('blog_text') . " bt
on l_ctype = " . VBSEO_UI_BLOG . " AND bt.blogtextid = l.l_contentid AND bt.state='visible' ";
$fields[] = "b.title as blogtitle,b.firstblogtextid, bt.pagetext as blogtext, bt.dateline as blogdateline";
$whr[] = "(l_ctype <> " . VBSEO_UI_BLOG . " OR (bt.blogtextid is not null))";
}
if(VBSEO_LIKE_CMS && VBSEO_VB_CMS)
{
$cperm_str = vbseo_permissions_cms_str();
if($cperm_str)
{
$join[] = "LEFT JOIN " . vbseo_tbl_prefix('cms_node') . " node
on l_ctype = " . VBSEO_UI_CMS . " AND node.nodeid = l.l_cgroup AND node.setpublish=1 AND hidden=0 ";
$join[] = "LEFT JOIN " . vbseo_tbl_prefix('cms_nodeinfo') . " ni on ni.nodeid = node.nodeid ";
$join[] = "LEFT JOIN " . vbseo_tbl_prefix('cms_article') . " ca on ca.contentid = node.contentid ";
$fields[] = "ni.title as cmstitle, ca.pagetext as cmstext, node.publishdate as cmsdateline";
$whr[] = "(l_ctype <> " . VBSEO_UI_CMS . " OR (".$cperm_str."))";
}
}
self::get_likes_query($fields, $join, $whr);
$db = vbseo_get_db();
$gq = $db->vbseodb_query($q = "
SELECT SQL_CALC_FOUND_ROWS l.*," . implode(', ', $fields)."
FROM " . vbseo_tbl_prefix('vbseo_likes') . " l "
. implode("\n", $join) . "
WHERE ".implode(' AND ', $whr)."
ORDER BY l_dateline desc
LIMIT ".intval(($start-1) * $pgsz).",".intval($pgsz)."
");
$results = array();
while($li = $db->vbseodb_fetch_assoc($gq))
{
$results[] = $li;
}
$db->vbseodb_free_result($gq);
return array('results' => $results,'total' => $db->vbseo_get_found() );
}
public static function likes_block($blocktype, $perpage = 5)
{
$results = array();
switch($blocktype)
{
case 'topthread':
$db = vbseo_get_db();
if(!$flist = vbseo_allowed_forums())
return '';
$gq = $db->vbseodb_query($q = "
SELECT threadid,title,postuserid,postusername,vbseo_likes
FROM " . vbseo_tbl_prefix('thread') . "
WHERE forumid in (".implode(',', $flist).")
ORDER BY vbseo_likes desc
LIMIT 0,".intval($perpage));
$results = array();
while($li = $db->vbseodb_fetch_assoc($gq))
$likes[] = $li;
break;
case 'latest':
$linfo = self::get_likes_detailed(0, 0, 1, $perpage);
$likes = $linfo['results'];
self::prerender_likes($likes);
break;
}
eval(vbseo_eval_template('vbseo_likes_widget', '$lrender'));
return $lrender;
}
public static function get_like_url_byid($cid, $ctype )
{
$like = array('l_ctype' => $ctype, 'l_contentid' => $cid);
self::get_like_typespec($like);
return $like['url'];
}
public static function get_like_typespec(&$like, $ctype = 0)
{
global $vbphrase;
switch($like['l_ctype'])
{
case VBSEO_UI_THREAD:
if(in_array($like['l_contentid'], self::$page_ids))
{
$turl = preg_replace('#\#.*$#', '', $_POST['lurl'] ? $_POST['lurl'] : $_SERVER['VBSEO_URI']);
$like['url'] = $turl.'#post'.$like['l_contentid'];
}else
$like['url'] = 'showthread.'.VBSEO_VB_EXT.'?p='.$like['l_contentid'];
$like['ctype']= $vbphrase['vbseo_like_post'];
$like['gtype']= $vbphrase['vbseo_like_thread'];
$like['gtitle']= $like['threadtitle'];
$like['pagetext'] = $like['posttext'];
$like['dateline'] = $like['postdateline'];
break;
case VBSEO_UI_BLOG:
if($like['l_contentid'] == $like['firstblogtextid'])
{
$like['url'] = 'blog.'.VBSEO_VB_EXT.'?b='.$like['l_cgroup'];
$like['ctype']= $vbphrase['vbseo_like_blogpost'];
$like['gtype']= '';
}else
{
$like['url'] = 'blog.'.VBSEO_VB_EXT.'?bt='.$like['l_contentid'];
$like['ctype']= $vbphrase['vbseo_like_blogcom'];
$like['gtype']= '';
}
$like['pagetext'] = $like['blogtext'];
$like['dateline'] = $like['blogdateline'];
$like['gtitle']= $like['blogtitle'];
break;
case VBSEO_UI_CMS:
$like['url'] = 'content.'.VBSEO_VB_EXT.'?r='.$like['l_cgroup'];
$like['ctype']= $vbphrase['vbseo_like_article'];
$like['gtype']= '';
$like['gtitle']= $like['cmstitle'];
$like['pagetext'] = $like['cmstext'];
$like['dateline'] = $like['cmsdateline'];
break;
}
return $like;
}
public static function prerender_likes(&$likes)
{
global $vbulletin;
require_once(DIR . '/includes/functions_forumdisplay.'.VBSEO_VB_EXT);
require_once(DIR . '/includes/functions_user.'.VBSEO_VB_EXT);
foreach($likes as $ii=>$post)
{
exec_switch_bg();
self::get_like_typespec($post);
$post['pagetext'] = strip_tags(fetch_censored_text(trim(fetch_trimmed_title(strip_bbcode($post['pagetext'], 1), 200))));
if(!($post['avatarurl']))
{
fetch_musername($post);
fetch_avatar_from_userinfo($post, true);
}
$post['avatarurl'] = str_replace('&', '&', $post['avatarurl']);
$post['likedate'] = vbdate($vbulletin->options['dateformat'], $post['l_dateline'], true);
$post['liketime'] = vbdate($vbulletin->options['timeformat'], $post['l_dateline']);
$post['postdate'] = vbdate($vbulletin->options['dateformat'], $post['dateline'], true);
$post['posttime'] = vbdate($vbulletin->options['timeformat'], $post['dateline']);
$likes[$ii] = $post;
}
}
public static function username_updated($userid, $username)
{
if(!intval($userid) || !$username)
return false;
$db = vbseo_get_db();
$db->vbseodb_query($q="UPDATE ".vbseo_tbl_prefix('vbseo_likes')."
SET l_from_username = '" . vbseo_db_escape($username)."'
WHERE l_from_userid = ".intval($userid));
}
}
?>
Revise this Paste