/* 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(&#039;user&#039;) . 
" SET $sets WHERE userid=".intval($userid));
return true;
}
public static function like_counter_type($ctype, $cgroup, $lnum)
{
$uptbl = array(VBSEO_UI_THREAD => &#039;thread&#039;, VBSEO_UI_BLOG => &#039;blog&#039;, VBSEO_UI_CMS => &#039;cms_nodeinfo&#039;);
$upfld = array(VBSEO_UI_THREAD => &#039;threadid&#039;, VBSEO_UI_BLOG => &#039;blogid&#039;, VBSEO_UI_CMS => &#039;nodeid&#039;);
$t = $uptbl[$ctype];
$f = $upfld[$ctype];
if(!$t || !$f)return false;
$field= &#039;vbseo_likes&#039;;
$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[&#039;l_ctype&#039;], $li[&#039;l_cgroup&#039;], $li[&#039;l_contentid&#039;], $val, $key);
}
public static function lcache_get_a($li, $key = null)
{
return self::lcache_get($li[&#039;l_ctype&#039;], $li[&#039;l_cgroup&#039;], $li[&#039;l_contentid&#039;], $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, &#039;count&#039; );
$ret = &#039;&#039;;
if($like_count)
{
$ol = self::lcache_get($ctype, $cgroup, $contentid, &#039;ownlike&#039;);
$alikes = self::lcache_get($ctype, $cgroup, $contentid, &#039;likes&#039;);
$ptype= $ol ? &#039;you&#039; : &#039;like&#039;;
$args = array();
$unum = $un = $ol ? 1 : 0;
for($x = 0; ($unum < self::$shortlist) && ($x<self::$shortlist); $x++)
{
$li = $alikes[$x];
if($li[&#039;l_from_userid&#039;] == vbseo_vb_userid())
continue;
if($li)
{
$l = VBSEO_REWRITE_MEMBERS ? 
vbseo_member_url($li[&#039;l_from_userid&#039;])
: &#039;member.&#039;.VBSEO_VB_EXT.&#039;?u=&#039;.$li[&#039;l_from_userid&#039;];
if($unum<3)
{
$unum++;
$args[] = $l;
$args[] = $li[&#039;l_from_username&#039;];
}else
{
$linkmore[] = &#039;<a href="&#039;.$l.&#039;">&#039;.$li[&#039;l_from_username&#039;].&#039;<a>&#039;;
}
}
}
if(($rest = $like_count - $unum)>0)
{
$args[] = $rest;
$unum++;
}
array_unshift($args, $vbphrase[$p=&#039;vbseo_&#039;.$ptype.&#039;_&#039;.$unum]);
$ret = VBSEO_VB4 ? construct_phrase($args) : @call_user_func_array(&#039;construct_phrase&#039;, $args);
if($linkmore)
$ret .= &#039;<br />&#039;.implode(&#039;, &#039;, $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,&#039;,&#039;) ? explode(&#039;,&#039;,$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(&#039;vbseo_likes&#039;) . " l, (SELECT @rn :=0, @lc :=0) r
WHERE l_ctype = ".$ctype." AND l_cgroup =".$groupid." AND l_contentid IN (" . implode(&#039;,&#039;, $contentids) . " )
ORDER BY l_contentid
) as nest
where rnum<".self::$shortlist."
");
while($li = $db->vbseodb_fetch_assoc($gq))
{
$al = self::lcache_get_a($li, &#039;likes&#039;);
$al[] = $li;
self::lcache_set_a($li, $al, &#039;likes&#039;);
$vbseo_gcache[&#039;user&#039;][$li[&#039;l_from_userid&#039;]] = array(
&#039;userid&#039; => $li[&#039;l_from_userid&#039;],
&#039;username&#039;=>$li[&#039;l_from_username&#039;]
);
}
$db->vbseodb_free_result($gq);
return ;
}
public static function extract_likes_bygroup($ctype, $groupid, $userid = 0, $contentids = array(), $tab = &#039;&#039;)
{
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(&#039;vbseo_likes&#039;) . " 
WHERE l_ctype = ".$ctype." AND l_cgroup = " . $groupid . 
($contentids ? " AND l_contentid IN (".implode(&#039;,&#039;, $contentids).")" : "")."
GROUP BY l_contentid".($userid ? ", ownlike" : "").
(($tab==&#039;top&#039;) ? " ORDER by cnt desc" : "").
" LIMIT 0,".(($tab==&#039;top&#039;) ? self::$toplist : self::$longlist);
if($tab)
{
$join = array("LEFT JOIN " . vbseo_tbl_prefix(&#039;user&#039;) . " 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(&#039;,&#039;,$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[&#039;l_contentid&#039;];
self::lcache_set_a($li, $li);
self::lcache_set_a($li, self::lcache_get_a($li,&#039;count&#039;)+$li[&#039;cnt&#039;], &#039;count&#039;);
if($li[&#039;ownlike&#039;])
self::lcache_set_a($li, true, &#039;ownlike&#039;);
}
$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[&#039;avatarenabled&#039;])
{
$join[] = "LEFT JOIN " . vbseo_tbl_prefix(&#039;avatar&#039;)." AS avatar ON(avatar.avatarid = u1.avatarid) ";
$join[] = "LEFT JOIN " . vbseo_tbl_prefix(&#039;customavatar&#039;)." AS customavatar ON(customavatar.userid = u1.userid)";
$join[] = "LEFT JOIN " . vbseo_tbl_prefix(&#039;post&#039;) . " p on l_ctype = " . VBSEO_UI_THREAD . " AND p.postid = l_contentid ".
(can_moderate(0, &#039;canmoderateposts&#039;) ? "" : " AND p.visible=1 ");
$join[] = "LEFT JOIN " . vbseo_tbl_prefix(&#039;thread&#039;) . " 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(&#039;,&#039;, $flist).")))";
if($userid)
$whr[] = &#039;(l_from_userid= &#039;.intval($userid).&#039;)&#039;;
if($duserid)
$whr[] = &#039;(l_dest_userid = &#039;.intval($duserid).&#039;)&#039;;
$join = array(
"LEFT JOIN " . vbseo_tbl_prefix(&#039;user&#039;) . " u1 on u1.userid = l.l_from_userid",
"LEFT JOIN " . vbseo_tbl_prefix(&#039;user&#039;) . " 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(&#039;,&#039;, $contentids).")";
if(VBSEO_LIKE_BLOG && VBSEO_VB_BLOG)
{
$join[] = "LEFT JOIN " . vbseo_tbl_prefix(&#039;blog&#039;) . " b 
on l_ctype = " . VBSEO_UI_BLOG . " AND b.blogid = l.l_cgroup AND b.state=&#039;visible&#039; ";
$join[] = "LEFT JOIN " . vbseo_tbl_prefix(&#039;blog_text&#039;) . " bt
on l_ctype = " . VBSEO_UI_BLOG . " AND bt.blogtextid = l.l_contentid AND bt.state=&#039;visible&#039; ";
$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(&#039;cms_node&#039;) . " 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(&#039;cms_nodeinfo&#039;) . " ni on ni.nodeid = node.nodeid  ";
$join[] = "LEFT JOIN " . vbseo_tbl_prefix(&#039;cms_article&#039;) . " 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(&#039;, &#039;, $fields)."
FROM " . vbseo_tbl_prefix(&#039;vbseo_likes&#039;) . " l "
. implode("\n", $join) . "
WHERE ".implode(&#039; AND &#039;, $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(&#039;results&#039; => $results,&#039;total&#039; => $db->vbseo_get_found() );
}
public static function likes_block($blocktype, $perpage = 5)
{
$results = array();
switch($blocktype)
{
case &#039;topthread&#039;:
$db = vbseo_get_db();
if(!$flist = vbseo_allowed_forums())
return &#039;&#039;;
$gq = $db->vbseodb_query($q = "
SELECT threadid,title,postuserid,postusername,vbseo_likes
FROM " . vbseo_tbl_prefix(&#039;thread&#039;) . "
WHERE forumid in (".implode(&#039;,&#039;, $flist).")
ORDER BY vbseo_likes desc
LIMIT 0,".intval($perpage));
$results = array();
while($li = $db->vbseodb_fetch_assoc($gq))
$likes[] = $li;
break;
case &#039;latest&#039;:
$linfo = self::get_likes_detailed(0, 0, 1, $perpage);
$likes = $linfo[&#039;results&#039;];
self::prerender_likes($likes);
break;
}
eval(vbseo_eval_template(&#039;vbseo_likes_widget&#039;, &#039;$lrender&#039;));
return $lrender;
}
public static function get_like_url_byid($cid, $ctype )
{       
$like = array(&#039;l_ctype&#039; => $ctype, &#039;l_contentid&#039; => $cid);
self::get_like_typespec($like);
return $like[&#039;url&#039;];
}
public static function get_like_typespec(&$like, $ctype = 0)
{       
global $vbphrase;
switch($like[&#039;l_ctype&#039;])
{
case VBSEO_UI_THREAD:
if(in_array($like[&#039;l_contentid&#039;], self::$page_ids))
{
$turl = preg_replace(&#039;#\#.*$#&#039;, &#039;&#039;, $_POST[&#039;lurl&#039;] ? $_POST[&#039;lurl&#039;] : $_SERVER[&#039;VBSEO_URI&#039;]);
$like[&#039;url&#039;] = $turl.&#039;#post&#039;.$like[&#039;l_contentid&#039;];
}else
$like[&#039;url&#039;] = &#039;showthread.&#039;.VBSEO_VB_EXT.&#039;?p=&#039;.$like[&#039;l_contentid&#039;];
$like[&#039;ctype&#039;]= $vbphrase[&#039;vbseo_like_post&#039;];
$like[&#039;gtype&#039;]= $vbphrase[&#039;vbseo_like_thread&#039;];
$like[&#039;gtitle&#039;]= $like[&#039;threadtitle&#039;];
$like[&#039;pagetext&#039;] = $like[&#039;posttext&#039;];
$like[&#039;dateline&#039;] = $like[&#039;postdateline&#039;];
break;
case VBSEO_UI_BLOG:
if($like[&#039;l_contentid&#039;] == $like[&#039;firstblogtextid&#039;])
{
$like[&#039;url&#039;] = &#039;blog.&#039;.VBSEO_VB_EXT.&#039;?b=&#039;.$like[&#039;l_cgroup&#039;];
$like[&#039;ctype&#039;]= $vbphrase[&#039;vbseo_like_blogpost&#039;];
$like[&#039;gtype&#039;]= &#039;&#039;;
}else
{
$like[&#039;url&#039;] = &#039;blog.&#039;.VBSEO_VB_EXT.&#039;?bt=&#039;.$like[&#039;l_contentid&#039;];
$like[&#039;ctype&#039;]= $vbphrase[&#039;vbseo_like_blogcom&#039;];
$like[&#039;gtype&#039;]= &#039;&#039;;
}
$like[&#039;pagetext&#039;] = $like[&#039;blogtext&#039;];
$like[&#039;dateline&#039;] = $like[&#039;blogdateline&#039;];
$like[&#039;gtitle&#039;]= $like[&#039;blogtitle&#039;];
break;
case VBSEO_UI_CMS:
$like[&#039;url&#039;] = &#039;content.&#039;.VBSEO_VB_EXT.&#039;?r=&#039;.$like[&#039;l_cgroup&#039;];
$like[&#039;ctype&#039;]= $vbphrase[&#039;vbseo_like_article&#039;];
$like[&#039;gtype&#039;]= &#039;&#039;;
$like[&#039;gtitle&#039;]= $like[&#039;cmstitle&#039;];
$like[&#039;pagetext&#039;] = $like[&#039;cmstext&#039;];
$like[&#039;dateline&#039;] = $like[&#039;cmsdateline&#039;];
break;
}
return $like;
}
public static function prerender_likes(&$likes)
{       
global $vbulletin;
require_once(DIR . &#039;/includes/functions_forumdisplay.&#039;.VBSEO_VB_EXT);
require_once(DIR . &#039;/includes/functions_user.&#039;.VBSEO_VB_EXT);
foreach($likes as $ii=>$post)
{
exec_switch_bg();
self::get_like_typespec($post);
$post[&#039;pagetext&#039;] = strip_tags(fetch_censored_text(trim(fetch_trimmed_title(strip_bbcode($post[&#039;pagetext&#039;], 1), 200))));
if(!($post[&#039;avatarurl&#039;]))
{
fetch_musername($post);
fetch_avatar_from_userinfo($post, true);
}
$post[&#039;avatarurl&#039;] = str_replace(&#039;&amp;&#039;, &#039;&&#039;, $post[&#039;avatarurl&#039;]);
$post[&#039;likedate&#039;] = vbdate($vbulletin->options[&#039;dateformat&#039;], $post[&#039;l_dateline&#039;], true);
$post[&#039;liketime&#039;] = vbdate($vbulletin->options[&#039;timeformat&#039;], $post[&#039;l_dateline&#039;]);
$post[&#039;postdate&#039;] = vbdate($vbulletin->options[&#039;dateformat&#039;], $post[&#039;dateline&#039;], true);
$post[&#039;posttime&#039;] = vbdate($vbulletin->options[&#039;timeformat&#039;], $post[&#039;dateline&#039;]);
$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(&#039;vbseo_likes&#039;)."
SET l_from_username = &#039;" . vbseo_db_escape($username)."&#039;
WHERE l_from_userid = ".intval($userid));
}
}
?>

Add a code snippet to your website: www.paste.org