<?php
// Disallow direct access to this file for security reasons
if(!defined("IN_MYBB"))
{
die("Direct initialization of this file is not allowed.<br /><br />Please make sure IN_MYBB is defined.");
}
//
// Hooks
//
$plugins->add_hook("postbit", "lame");
$plugins->add_hook("showthread_start","lameAction");
$plugins->add_hook("xmlhttp", "ajaxAction");
$plugins->add_hook("member_profile_start", "lameMemberDetail");
$plugins->add_hook("class_moderation_delete_post", 'postRemove');
$plugins->add_hook("admin_user_groups_edit_graph_tabs", "lameAdminTab");
$plugins->add_hook("admin_user_groups_edit_graph", "lameAdminForm");
$plugins->add_hook('admin_user_groups_edit_commit', 'saveLameGroupSettings');
// The information that shows up on the plugin manager
function lame_info()
{
return array(
"name" => "Lame",
"description" => "Let a user know you thought their post was lame.",
"website" => "",
"author" => "skywalker2208",
"authorsite" => "",
"version" => "2.1",
"guid" => "caa9394adb8caf357a5b4fac58b7a36b",
);
}
function lame_install() {
global $db;
$table_create = "
CREATE TABLE IF NOT EXISTS ".TABLE_PREFIX."lame (
lame_id INT(10) unsigned NOT NULL AUTO_INCREMENT,
l_uid INT(10) NOT NULL,
pid INT(10) NOT NULL,
time INT(10) NOT NULL,
PRIMARY KEY (lame_id),
KEY l_uid (l_uid),
KEY pid (pid)
)";
// TODO write upgrade script
$db->query($table_create);
$check_template = $db->simple_select('templates', '*', "title = 'member_profile_lame_details' AND sid = '-2'");
$total_template_check = $db->num_rows($check_template);
//
// Make sure the template doesn't already exist.
//
if ($total_template_check == 0) {
//
// Add new template
//
$newtemp = array(
"title" => 'member_profile_lame_details',
"template" => '<tr>\n\t<td class="trow1"><strong>Lames Given:</strong></td>
\n\t<td class="trow1">$lame_given</td>
\n</tr>\n
<tr>\n\t<td class="trow2"><strong>Lames Received Per Post:</strong></td>
\n\t<td class=\"trow2\">$lame_received/$lame_posts</td>
\n</tr>',
"sid" => -2,
"dateline" => time(),
"version" => 1400,
);
$db->insert_query('templates', $newtemp);
}
$alter_table = 'ALTER TABLE '.TABLE_PREFIX.'usergroups ADD COLUMN `canaddlame` INT(1) NOT NULL DEFAULT 0';
$db->query($alter_table);
$alter_table = 'ALTER TABLE '.TABLE_PREFIX.'usergroups ADD COLUMN `canremovelame` INT(1) NOT NULL DEFAULT 0';
$db->query($alter_table);
$alter_table = 'ALTER TABLE '.TABLE_PREFIX.'usergroups ADD COLUMN `maxlameperday` INT(2) NOT NULL DEFAULT 5';
$db->query($alter_table);
}
function lame_is_installed()
{
global $db;
if($db->table_exists("lame"))
{
return true;
}
return false;
}
// This function runs when the plugin is activated.
function lame_activate()
{
global $db;
include MYBB_ROOT . '/inc/adminfunctions_templates.php';
//
// Check for added templates groups and make sure they have the new lame template.
//
find_replace_templatesets('member_profile_lame_details', '//', '', 1);
//
// Add the button, lame detail and lame section of the standard postbit.
//
$find = '/<\/table>\s*$/';
$replace = '</table>{$post[\'lame\']}';
find_replace_templatesets('postbit', $find, $replace, 1);
$find = '/'.preg_quote('{$post[\'button_quote\']}').'/';
$replace = '{$post[\'lame_button\']}{$post[\'button_quote\']}';
find_replace_templatesets('postbit', $find, $replace, 1);
$find = '/'.preg_quote('{$post[\'user_details\']}').'/';
$replace = '{$post[\'user_details\']}{$post[\'lame_details\']}';
find_replace_templatesets('postbit', $find, $replace, 1);
//
// Add the button, lame detail and lame section of the classic postbit.
//
$find = '/<\/table>\s*$/';
$replace = '</table>{$post[\'lame\']}';
find_replace_templatesets('postbit_classic', $find, $replace, 1);
$find = '/'.preg_quote('{$post[\'button_quote\']}').'/';
$replace = '{$post[\'lame_button\']}{$post[\'button_quote\']}';
find_replace_templatesets('postbit_classic', $find, $replace, 1);
$find = '/'.preg_quote('{$post[\'user_details\']}').'/';
$replace = '{$post[\'user_details\']}{$post[\'lame_details\']}';
find_replace_templatesets('postbit_classic', $find, $replace, 1);
//
// Add lame javascript file.
//
$find = '/'.preg_quote('{$newpmmsg}').'/';
$replace = '<script type="text/javascript" src="jscripts/lame.js?ver=1400"></script>'
."\n".'{$newpmmsg}';
find_replace_templatesets('headerinclude', $find, $replace, 1);
//
// Add the user profile template.
//
$find = '/'.preg_quote('{$warning_level}').'/';
$replace = '{$warning_level}{$lame_details}';
find_replace_templatesets('member_profile', $find, $replace, 1);
}
// This function runs when the plugin is deactivated.
function lame_deactivate()
{
include MYBB_ROOT . '/inc/adminfunctions_templates.php';
//
// Remove the button, lame details and lame section from the standard postbit.
//
$find = '/'.preg_quote('{$post[\'lame\']}').'/';
$replace = '';
find_replace_templatesets('postbit', $find, $replace, 0);
$find = '/'.preg_quote('{$post[\'lame_button\']}').'/';
$replace = '';
find_replace_templatesets('postbit', $find, $replace, 0);
$find = '/'.preg_quote('{$post[\'lame_details\']}').'/';
$replace = '';
find_replace_templatesets('postbit', $find, $replace, 0);
//
// Remove the button, lame details and lame section from the classic postbit.
//
$find = '/'.preg_quote('{$post[\'lame\']}').'/';
$replace = '';
find_replace_templatesets('postbit_classic', $find, $replace, 0);
$find = '/'.preg_quote('{$post[\'lame_button\']}').'/';
$replace = '';
find_replace_templatesets('postbit_classic', $find, $replace, 0);
$find = '/'.preg_quote('{$post[\'lame_details\']}').'/';
$replace = '';
find_replace_templatesets('postbit_classic', $find, $replace, 0);
//
// Remove javascript file link
//
$find = '?'.preg_quote('<script type="text/javascript" src="jscripts/lame.js?ver=1400"></script>').'(\r|\n)*?';
$replace = '';
find_replace_templatesets('headerinclude', $find, $replace, 0);
//
// Remove the user profile template.
//
$find = '/'.preg_quote('{$lame_details}').'/';
$replace = '';
find_replace_templatesets('member_profile', $find, $replace, 0);
}
function lame_uninstall() {
global $db;
//
// Remove the lame table.
//
$db->drop_table('lame');
//
// Remove the new template added by this mod.
//
$db->delete_query('templates', "title = 'member_profile_lame_details'");
$alter_table = 'ALTER TABLE '.TABLE_PREFIX.'usergroups DROP `canaddlame`';
$db->query($alter_table);
$alter_table = 'ALTER TABLE '.TABLE_PREFIX.'usergroups DROP `canremovelame`';
$db->query($alter_table);
$alter_table = 'ALTER TABLE '.TABLE_PREFIX.'usergroups DROP `maxlameperday`';
$db->query($alter_table);
}
function lame($post) {
global $mybb, $db;
$pid = (int)$post['pid'];
$lame_users = findLameUsers($pid);
$post['lame'] = '<span id="lame_'.$pid.'">'.$lame_users['body'].'</span>';
$maxlameperday = (int)$mybb->usergroup['maxlameperday'];
$maxlimithit = FALSE;
//
// If max lame is greater then zero then check to see if they hit their limit.
//
if ($maxlameperday > 0)
{
$lame_used_24_hours_query = $db->simple_select('lame', 'lame_id', 'l_uid = "'
.(int)$mybb->user['uid'].'" AND time >= "'.strtotime('-1 day').'"');
$lame_given = $db->num_rows($lame_used_24_hours_query);
if ($lame_given >= $maxlameperday)
{
$maxlimithit = TRUE;
}
}
//
// If the logged in use is the same as the poster
// then don't show the button.
//
if ($mybb->user['uid'] != $post['uid'] && $mybb->user['uid'] > 0 && !$lame_users['has_lame']
&& trim($mybb->usergroup['canaddlame'])) {
if ($maxlimithit)
{
$post['lame_button'] = "<span id=\"lame_button{$post['pid']}\" class=\"lame_add\" ></span>";
}
else
{
$post['lame_button'] = "<span id=\"lame_button{$post['pid']}\" class=\"lame_add\" >
<a onclick=\"lame_action({$post['pid']}); return false; \"
href=\"showthread.php?action=lame&tid={$post['tid']}&pid={$post['pid']}\">
<img src=\"images/english/lame_add.gif\" border=\"0\" alt=\"Add Lame\" /></a></span>";
}
}
else if ($mybb->user['uid'] != $post['uid'] && $mybb->user['uid'] > 0 && $lame_users['has_lame'] &&
trim($mybb->usergroup['canremovelame']))
{
$post['lame_button'] = "<span id=\"lame_button{$post['pid']}\" class=\"lame_remove\" >
<a onclick=\"lame_action({$post['pid']}); return false; \"
href=\"showthread.php?action=remove_lame&tid={$post['tid']}&pid={$post['pid']}\">
<img src=\"images/english/lame_delete.gif\" border=\"0\" alt=\"Remove Lame\" /></a></span>";
}
//
// Get the details of lame info for the paticular user.
//
$details = getDetails($post['uid']);
//
// Lame details in the user post info
//
$post['lame_details'] = '<br /><span class="smalltext">Lame Given: '.$details['total_given']
.'<br /> Lame Received Per Post: '.$details['total_received']
.'/'.$details['total_posts'].'</span>';
}
function lameAction() {
global $mybb, $db;
$tid = (int)$mybb->input['tid'];
$pid = (int)$mybb->input['pid'];
$action = $mybb->input['action'];
if ($action == 'lame' &&!trim($mybb->usergroup['canaddlame'])) {
error_no_permission();//error('Not Allowed to remove lame.');
}
if ($action == 'remove_lame' &&!trim($mybb->usergroup['canremovelame'])) {
error_no_permission();//error('Not Allowed to remove lame.');
}
//
// Adding lame and checking to make sure the user
// is logged in.
//
if ($action == 'lame' && $mybb->user['uid'] > 0)
{
$maxlameperday = (int)$mybb->usergroup['maxlameperday'];
$lame_limit = FALSE;
//
// If max lame is greater then zero then check to see if they hit their limit.
//
if ($maxlameperday > 0)
{
$lame_used_24_hours_query = $db->simple_select('lame', 'time', 'l_uid = "'
.(int)$mybb->user['uid'].'" AND time >= "'.strtotime('-1 day').'"',
array(
'order_by' => 'time',
'order_dir' => 'ASC'
));
$lame_given = $db->num_rows($lame_used_24_hours_query);
if ($lame_given >= $maxlameperday)
{
$lame_limit = TRUE;
}
}
$error = $message = '';
$link = htmlspecialchars_decode(get_post_link($pid, $tid))."#pid{$pid}";
if ($lame_limit) {
$lame_used_24_hour_results = $db->fetch_array($lame_used_24_hours_query);
$post_again = $lame_used_24_hour_results['time'] + (24 * 60 * 60);
$error = '';
$message = 'You have already hit your lame limit. You need to wait 24 hours. You may post another lame on '
.date('M j, Y H:i', $post_again);
if ($mybb->user['showredirect'] == 0)
{
$message .= '<br /><a href="'.$link.'">Return</a>';
error($message, $error);
}
} else {
saveLame($pid);
}
//
// Redirect the user back to the page they came from.
//
redirect($link, $message, $error);
exit;
}
else if ($action == 'remove_lame' && $mybb->user['uid'] > 0)
{
removeLame($pid);
//
// Redirect the user back to the page they came from.
//
redirect(htmlspecialchars_decode(get_post_link($pid, $nextthread['tid']))."#pid{$pid}");
exit;
}
}
function ajaxAction() {
global $mybb, $db;
$pid = (int)$mybb->input['pid'];
$action = $mybb->input['action'];
if ($action == 'lame' || $action == 'remove_lame')
{
$lame = array();
$lame['error'] = '';
$lame['can_remove_lame'] = FALSE;
$lame['can_add_lame'] = FALSE;
$check = FALSE;
//
// Find out if the lame add buttons need to be removed
//
$lame['lame_limit'] = FALSE;
//
// If user has hit their limit and then removes
// a lame and is below the limit then rebuild add image.
//
$lame['below_limit'] = FALSE;
if (($action == 'lame' &&!trim($mybb->usergroup['canaddlame'])) || ($action == 'remove_lame' &&!trim($mybb->usergroup['canremovelame'])))
{
if (!trim($mybb->usergroup['canaddlame']))
{
$lame['lame_limit'] = TRUE;
}
if (!trim($mybb->usergroup['canremovelame']))
{
$lame['below_limit'] = TRUE;
}
$lame['error'] = 'You do not have permission to perform this action. Please contact
the administrator if you feel this is incorrect.';
echo json_encode($lame);
exit;
}
$mybb->user['uid'];
$maxlameperday = (int)$mybb->usergroup['maxlameperday'];
//
// Adding lame and checking to make sure the user
// is logged in.
//
if ($action == 'lame' && $mybb->user['uid'] > 0)
{
if (trim($mybb->usergroup['canremovelame']))
{
$lame['can_remove_lame'] = TRUE;
}
//
// If max lame is greater then zero then check to see if they hit their limit.
//
if ($maxlameperday > 0)
{
$lame_used_24_hours_query = $db->simple_select('lame', 'time', 'l_uid = "'
.(int)$mybb->user['uid'].'" AND time >= "'.strtotime('-1 day').'"',
array(
'order_by' => 'time',
'order_dir' => 'ASC'
));
$lame_given = $db->num_rows($lame_used_24_hours_query) + 1;
if ($lame_given >= $maxlameperday)
{
//
// Remove lame add buttons
//
$lame['lame_limit'] = TRUE;
if ($lame_given > $maxlameperday)
{
$lame_used_24_hour_results = $db->fetch_array($lame_used_24_hours_query);
$post_again = $lame_used_24_hour_results['time'] + (24 * 60 * 60);
$lame['error'] = 'You have already hit your lame limit. You need to wait 24 hours.
You may post another lame on '.date('M j, Y H:i', $post_again);
}
}
}
if (empty($lame['error']))
{
$check = saveLame($pid);
}
$lame['image'] = '<img src="images/english/lame_delete.gif" border="0" alt="Remove Lame" />';
}
else if ($action == 'remove_lame' && $mybb->user['uid'] > 0)
{
if (trim($mybb->usergroup['canaddlame']))
{
$lame['can_add_lame'] = TRUE;
}
if ($maxlameperday > 0)
{
$lame_used_24_hours_query = $db->simple_select('lame', 'lame_id', 'l_uid = "'
.(int)$mybb->user['uid'].'" AND time >= "'.strtotime('-1 day').'"');
$lame_given = $db->num_rows($lame_used_24_hours_query);
}
$check = removeLame($pid);
if ($maxlameperday > 0 && $check && $maxlameperday == $lame_given) {
$lame['below_limit'] = TRUE;
}
$lame['testy'] = $lame_given.'<= given max lame per day =>'. $maxlameperday;
if (($lame_given - 1) >= $maxlameperday)
{
//
// Remove lame add buttons
//
$lame['lame_limit'] = TRUE;
}
$lame['image'] = '<img src="images/english/lame_add.gif" border="0" alt="Add Lame" />';
}
$lame_users = findLameUsers($pid);
$lame['block'] = $lame_users['body'];
$lame['success'] = $check;
echo json_encode($lame);
}
}
function removeLame($pid) {
global $mybb, $db;
$db->delete_query('lame', "pid = $pid AND l_uid = ".$mybb->user['uid']);
return TRUE;
}
function saveLame($pid){
global $mybb, $db;
$uid_query = $db->query("
SELECT
uid
FROM
".TABLE_PREFIX."posts
WHERE
pid = $pid
");
$uid = $db->fetch_array($uid_query);
//
// Make sure the user adding a lame is not the user who created the post
// and a post exists.
//
if ($uid['uid'] != $mybb->user['uid'] && trim($uid['uid']))
{
$check_lame = $db->simple_select('lame', 'COUNT(lame_id) AS total'
, "pid = $pid AND l_uid = ".$mybb->user['uid']);
$total_lame = $db->fetch_array($check_lame);
//
// Make sure the user doesn't already have a lame.
//
if ($total_lame['total'] == 0)
{
$add_lame = array(
'l_uid' => $mybb->user['uid'],
'pid' => $pid,
'time' => time(),
);
$db->insert_query('lame', $add_lame);
return TRUE;
}
}
return FALSE;
}
function findLameUsers($pid) {
global $db, $mybb;
$find_lames = $db->query("
SELECT
u.uid,
u.username,
u.usergroup,
u.displaygroup
FROM
".TABLE_PREFIX."lame g
JOIN
".TABLE_PREFIX."users u
ON u.uid = g.l_uid
WHERE
g.pid = $pid
ORDER BY
g.time ASC
");
$user_list = array();
$lame_array = array('has_lame' => FALSE);
//
// Build the lame list for each post.
//
while ($lame_list = $db->fetch_array($find_lames)) {
$user_list[] = '<a href="'.get_profile_link($lame_list['uid']).'">'
.format_name($lame_list['username'], $lame_list['usergroup'], $lame_list['displaygroup']).'</a>';
//
// If user has already lameed about post then set has_groun to true
// to not show lame button .
//
if ($mybb->user['uid'] == $lame_list['uid']) {
$lame_array['has_lame'] = TRUE;
}
}
//
// Display the list of users if there are any.
//
$lame_section = '';
if (!empty($user_list)) {
$lame_section .= '<table class="thead" style="margin-bottom:5px;" width="100%">
<tr><td><b>The following users think this post is LAME:</b></td></tr>
<tr class="tcat"><td>'.implode(', ', $user_list).'</td></tr></table>';
}
$lame_array['body'] = $lame_section;
return $lame_array;
}
function lameMemberDetail() {
global $mybb, $templates, $lame_details;
//print_r($mybb);//echo $mybb->input['fid'];
if($mybb->input['action'] == 'profile' && (int)$mybb->input['uid']) {
$details = getDetails($mybb->input['uid']);
//
// Set the two variables in the templates.
//
$lame_given = $details['total_given'];
$lame_received = $details['total_received'];
$lame_posts = $details['total_posts'];
//
// Call the template and set the variable.
//
eval("\$lame_details = \"".$templates->get("member_profile_lame_details")."\";");
}
}
function getDetails($uid) {
global $db;
$uid = (int)$uid;
$detail = array();
//
// Find lame given by a user
//
$number_lame = $db->query("
SELECT
COUNT(l.lame_id) AS total
FROM
".TABLE_PREFIX."lame l
JOIN
".TABLE_PREFIX."posts p
ON p.pid = l.pid
WHERE
l.l_uid = $uid
");
$number_lame_given = $db->fetch_array($number_lame);
//
// Find number of lames received to number of posts
//
$number_lame2 = $db->query("
SELECT
COUNT(l.lame_id) as total_lame,
COUNT(DISTINCT(p.pid)) as total_posts
FROM
".TABLE_PREFIX."lame l
JOIN
".TABLE_PREFIX."posts p
ON p.pid = l.pid
WHERE
p.uid = ".$uid
);
$number_lame_received = $db->fetch_array($number_lame2);
$detail['total_given'] = $number_lame_given['total'];
$detail['total_received'] = $number_lame_received['total_lame'];
$detail['total_posts'] = $number_lame_received['total_posts'];
return $detail;
}
function postRemove($pid) {
global $mybb, $db;
$pid = (int)$pid;
//
// Check to see if the post still exists.
//
$check_post = $db->simple_select('posts', 'pid', "pid = $pid");
$post_exist = $db->num_rows($check_post);
//
// If the post got deleted and the action is deletepost then remove
// all lames that go with the post.
//
if ($post_exist == 0 && $mybb->input['action'] == 'deletepost') {
removeAllLames($pid);
}
}
function removeAllLames($pid) {
global $db;
//
// Delete all lames with the deletion of the post
//
$db->delete_query('lame', "pid = $pid");
}
function lameAdminTab($tabs) {
global $mybb;
//
// The Guest group doesn't have any settings.
//
if ($mybb->input['gid'] != 1) {
$tabs['lame'] = 'Lame';
}
}
function lameAdminForm() {
global $mybb;
//print_r($mybb);
$gid = (int)$mybb->input['gid'];
echo '<div id="tab_lame">';
$form_container = new FormContainer('Lame Settings');
$post_string = 'index.php?module=user/groups&action=edit&gid='.$gid;
$form = new DefaultForm('','');
$form_container->output_row($form->generate_check_box('add_lame', 1
, 'Can Add Lame?', array('checked' => $mybb->input['canaddlame'])));
$form_container->output_row($form->generate_check_box('remove_lame', 1
, 'Can Remove Lame?', array('checked' => $mybb->input['canremovelame'])));
$form_container->output_row('Number of Lames per day:'
, 'Enter 0 for no limit. Max lame limit is 100.'
, $form->generate_text_box('lame_max', $mybb->input['maxlameperday'], array('style' => 'width: 26px;')));
$form_container->end();
echo '</div>';
}
function saveLameGroupSettings()
{
global $mybb, $db;
$set_add_lame = $mybb->input['add_lame'] == 1 ? 1 : 0;
$set_remove_lame = $mybb->input['remove_lame'] == 1 ? 1 : 0;
$max_lame_per_day = (int)$mybb->input['lame_max'];
$set_max_lame = (($max_lame_per_day > 0 && $max_lame_per_day < 100) ? $max_lame_per_day : 5);
$update_array = array(
'canaddlame' => $set_add_lame,
'canremovelame' => $set_remove_lame,
'maxlameperday' => $set_max_lame,
);
//
// If the user can access the admin control panel and can modify the user group
// permission then save the lame group permissions.
//
if ($mybb->usergroup['cancp'] && $mybb->admin['permissions']['user']['groups']) {
$db->update_query('usergroups', $update_array, 'gid = '.(int)$mybb->input['gid']);
}
}
?>