Simple Karma Challenge

phpBB3 and MOD challenges setup by the staff to test and challenge your phpBB3 coding skills.

Re: Karma.php problem?

Postby Pace » 12 Feb 2007, 12:26

Ok, just to check, because I think the config part is the only one remaining.
(rewriting the mod/hack? Grin I think it's now re-written completely save for the "hours_past" part, because I'm not yet sure if I've done it right)

Problem is, I'm getting the blank screen, yet again. Sad

Code: Select all
<?php

define
('IN_PHPBB'true);
$phpbb_root_path './';
$phpEx substr(strrchr(__FILE__'.'), 1);
include(
$phpbb_root_path 'common.' $phpEx);

// Start session management
$user->session_begin();
$auth->acl($user->data);
$user->setup();

// Some extra script settings. You can modify them
// How long do we have to wait before giving karma points again?
$hours_past 1// THIS PART WILL NO LONGER BE HERE!

    
$sql 'SELECT karma_time FROM ' USERS_TABLE " WHERE user_id = '" $user->data['user_id'] . "'"// finds out when voter last changed someone's karma
    
$result $db->sql_query($sql);
    
$last_change $db->sql_fetchfield('karma_time');
    
$db->sql_freeresult($result);
    
$sql 'SELECT user_id FROM ' USERS_TABLE " WHERE user_id = '" $user->data['user_id'] . "'"// this code will enable us to avoid 'self-karma'
    
$result $db->sql_query($sql);
    
$voter_id $db->sql_fetchfield('user_id');
    
$db->sql_freeresult($result);

// Get variables
$topic_id request_var('t''');
$user request_var('u''');
$vote request_var('x''');

if (
$voter_id == $user)
{
    
trigger_error($user->lang['NO_SELF_KARMA'] . '<br /><br />' sprintf($user->lang['RETURN_TOPIC'], '<a href="' append_sid("{$phpbb_root_path}viewtopic.$phpEx""t=$topic_id") . '">''</a>'));
}
else
{
    if (!
$auth->acl_gets('a_''m_') && !$auth->acl_getf_global('m_'))
    {
        
trigger_error($user->lang['NO_USER_KARMA'] . '<br /><br />' sprintf($user->lang['RETURN_TOPIC'], '<a href="' append_sid("{$phpbb_root_path}viewtopic.$phpEx""t=$topic_id") . '">''</a>'));
    }
    else
    {
        
$time time();
        
$time_elapsed $time $last change;
        if (
$time_elapsed >= $config['karma_time_limit']) // stops voter from changing karma during a delay period [fixed in the "phpbb3_config" table's "karma_time_limit" field]
        
{
            if (
$vote == 'applaud')
            {
                
$sql = 'UPDATE ' . USERS_TABLE . " SET karma = karma + 1 WHERE user_id = $user";
                
$db->sql_query($sql);
                
// stop karma from going beyond the maximum number set
                
if ($karma >= 8)
                {
                    
$sql = 'UPDATE ' . USERS_TABLE . " SET karma = 8 WHERE user_id = $user";
                    
$db->sql_query($sql);
                }
            }
            else
            {
                
$sql = 'UPDATE ' . USERS_TABLE . " SET karma = karma - 1 WHERE user_id = $user";
                
$db->sql_query($sql);        
                
// Add bans on reaching -8 karma
                
if ($karma <= -8)
                {
                    
$sql 'INSERT INTO ' BANLIST_TABLE " VALUES (0, $user, '', NULL)";
                    
$db->sql_query($sql);
                }
            }
            
// Update the database with the current time() for the voter
            
$sql 'UPDATE ' USERS_TABLE " SET karma_time = '$time' WHERE user_id = '$voter_id' ";
            
$db->sql_query($sql);
            
$redirect append_sid("{$phpbb_root_path}viewtopic.$phpEx""t=$topic_id");
        }
        else
        {
            
trigger_error($user->lang['TOO_SOON'] . '<br /><br />' sprintf($user->lang['RETURN_TOPIC'], '<a href="' append_sid("{$phpbb_root_path}viewtopic.$phpEx""t=$topic_id") . '">''</a>'));
        }
    }
}

?>


(note that it's [almost] entirely re-written compared to the original phpBB2 version Wink )
User avatar
Pace
Style Author
Style Author
 
Posts: 147
Joined: 30 Aug 2006, 02:56
Location: Culuria, Residio
Gender: Male




phpBB Academy at StarTrekGuide
Support STG
Using PayPal Donate

Re: Karma.php problem?

Postby Handyman » 12 Feb 2007, 12:40

Spoiler:
Code: Select all
    $sql 'SELECT karma_time FROM ' USERS_TABLE " WHERE user_id = '" $user->data['user_id'] . "'"// finds out when voter last changed someone's karma
    
$result $db->sql_query($sql);
    
$last_change $db->sql_fetchfield('karma_time');
    
$db->sql_freeresult($result);
    
$sql 'SELECT user_id FROM ' USERS_TABLE " WHERE user_id = '" $user->data['user_id'] . "'"// this code will enable us to avoid 'self-karma'
    
$result $db->sql_query($sql);
    
$voter_id $db->sql_fetchfield('user_id');
    
$db->sql_freeresult($result);


Why are you running 2 queries here?
just merge them into one
Spoiler:
Code: Select all

    $sql 
'SELECT karma_time, user_id, karma FROM ' USERS_TABLE " WHERE user_id = {$user->data['user_id']}"// finds out when voter last changed someone's karma
    
$result $db->sql_query($sql);
    
$row $db->sql_fetchrow($result);
    
$db->sql_freeresult($result);
$voter_id $row['user_id'];
$last_change $row['karma_time'];
$karma $row['karma'];
[/code=php]

The reason you are getting blank pages is, for some reason it's not showing errors for you? had to have been something that was changed in Beta 5 because a lot of people have started to have that problem.
The error that is causing the blank page right now is you are using
Code: Select all
               if ($karma >= 8)
                {
                    
$sql 'UPDATE ' USERS_TABLE " SET karma = 8 WHERE user_id = $user";
                    
$db->sql_query($sql);
                }

and nowhere is $karma defined? so it's an undefined variable right now and it's having a fit.
In my example code, I added 'karma' to the sql query so it will make that a defined variable... instead of:
[spoiler]
Code: Select all
               $sql 'UPDATE ' USERS_TABLE " SET karma = karma + 1 WHERE user_id = $user";
                
$db->sql_query($sql);
                
// stop karma from going beyond the maximum number set
                
if ($karma >= 8)
                {
                    
$sql 'UPDATE ' USERS_TABLE " SET karma = 8 WHERE user_id = $user";
                    
$db->sql_query($sql);
                }

let's only run a sql query if we absolutely have to
Spoiler:
Code: Select all
if ($karma 8)
{
    
$sql 'UPDATE ' USERS_TABLE " SET karma = karma + 1 WHERE user_id = $user";
    
$db->sql_query($sql);
}

that way it will only add a vote if the total is less than 8, so there will only be a max of 8.
Do the same thing for the next instance.

also instead of
Code: Select all
 $sql 'UPDATE ' USERS_TABLE " SET karma_time = '$time' WHERE user_id = '$voter_id' ";
            
$db->sql_query($sql);

run that update when you are updating the karma count so you don't add an extra query.
Please contact me if you have any news to submit to SCOFF News.
SCOFFing at the candidates while you sleep.
My Mods || My Mod Queue
Image
User avatar
Handyman    
Rear Fleet Admiral
Rear Fleet Admiral
 
Posts: 7456
Joined: 08 May 2006, 04:45
Location: Where no man has gone before!
Favorite Team: Seattle Seahawks
Gender: Male

Re: Karma.php problem?

Postby Pace » 12 Feb 2007, 12:57

Handyman wrote:The error that is causing the blank page right now is you are using
Code: Select all
               if ($karma >= 8)
                {
                    $sql = 'UPDATE ' . USERS_TABLE . " SET karma = 8 WHERE user_id = $user";
                    $db->sql_query($sql);
                }

and nowhere is $karma defined? so it's an undefined variable right now and it's having a fit.
In my example code, I added 'karma' to the sql query so it will make that a defined variable...


'kay, defined $karma, but had to do it for $user, not the voter, so it still adds another query Wink

also instead of
Code: Select all
 $sql = 'UPDATE ' . USERS_TABLE . " SET karma_time = '$time' WHERE user_id = '$voter_id' ";
            $db->sql_query($sql);

run that update when you are updating the karma count so you don't add an extra query.


Here, same problem: it's the voter, not $user, whose karma_time has to be changed. So I have to keep that extra query.

Edit: I checked, and despite the modifications, I still get the blank page?
User avatar
Pace
Style Author
Style Author
 
Posts: 147
Joined: 30 Aug 2006, 02:56
Location: Culuria, Residio
Gender: Male

Re: Karma.php problem?

Postby Handyman » 12 Feb 2007, 13:39

Can you post your new code?
also, if you aren't already, use a fresh beta 5 install and make sure the debug and debug_extra are uncommented in the config.php
That should take care of the blank pages? not sure why it's a blank page instead of an error.
Please contact me if you have any news to submit to SCOFF News.
SCOFFing at the candidates while you sleep.
My Mods || My Mod Queue
Image
User avatar
Handyman    
Rear Fleet Admiral
Rear Fleet Admiral
 
Posts: 7456
Joined: 08 May 2006, 04:45
Location: Where no man has gone before!
Favorite Team: Seattle Seahawks
Gender: Male

Re: Karma.php problem?

Postby Pace » 12 Feb 2007, 14:10

Spoiler:
Code: Select all
<?php

define
('IN_PHPBB'true);
$phpbb_root_path './';
$phpEx substr(strrchr(__FILE__'.'), 1);
include(
$phpbb_root_path 'common.' $phpEx);

// Start session management
$user->session_begin();
$auth->acl($user->data);
$user->setup();

// Some extra script settings. You can modify them
// How long do we have to wait before giving karma points again?
$hours_past 1// THIS PART WILL NO LONGER BE HERE!

$sql 'SELECT karma_time, user_id FROM ' USERS_TABLE " WHERE user_id = {$user->data['user_id']}"// finds out when voter last changed someone's karma
$result $db->sql_query($sql);
$row $db->sql_fetchrow($result);
$db->sql_freeresult($result);
$voter_id $row['user_id'];
$last_change $row['karma_time'];

// Get variables
$topic_id request_var('t''');
$user request_var('u''');
$vote request_var('x''');

$sql 'SELECT karma FROM ' USERS_TABLE " WHERE user_id = $user";
$result $db->sql_query($sql);
$karma $db->sql_fetchrow($result);
$db->sql_freeresult($result);

if (
$voter_id == $user)
{
    
trigger_error($user->lang['NO_SELF_KARMA'] . '<br /><br />' sprintf($user->lang['RETURN_TOPIC'], '<a href="' append_sid("{$phpbb_root_path}viewtopic.$phpEx""t=$topic_id") . '">''</a>'));
}
else
{
    if (!
$auth->acl_gets('a_''m_') && !$auth->acl_getf_global('m_'))
    {
        
trigger_error($user->lang['NO_USER_KARMA'] . '<br /><br />' sprintf($user->lang['RETURN_TOPIC'], '<a href="' append_sid("{$phpbb_root_path}viewtopic.$phpEx""t=$topic_id") . '">''</a>'));
    }
    else
    {
        
$time time();
        
$time_elapsed $time $last change;
        if (
$time_elapsed >= $config['karma_time_limit']) // stops voter from changing karma during a delay period [fixed in the "phpbb3_config" table's "karma_time_limit" field]
        
{
            if (
$vote == 'applaud')
            {
                if (
$karma 8)
                {
                    
$sql 'UPDATE ' USERS_TABLE " SET karma = karma + 1 WHERE user_id = $user";
                    
$db->sql_query($sql);
                }
            }
            else
            {
                
$sql = 'UPDATE ' . USERS_TABLE . " SET karma = karma - 1 WHERE user_id = $user";
                
$db->sql_query($sql);        
                
// Add bans on reaching -8 karma
                
if ($karma <= -8)
                {
                    
$sql 'INSERT INTO ' BANLIST_TABLE " VALUES (0, $user, '', NULL)";
                    
$db->sql_query($sql);
                }
            }
            
// Update the database with the current time() for the voter
            
$sql 'UPDATE ' USERS_TABLE " SET karma_time = '$time' WHERE user_id = '$voter_id' ";
            
$db->sql_query($sql);
            
$redirect append_sid("{$phpbb_root_path}viewtopic.$phpEx""t=$topic_id");
        }
        else
        {
            
trigger_error($user->lang['TOO_SOON'] . '<br /><br />' sprintf($user->lang['RETURN_TOPIC'], '<a href="' append_sid("{$phpbb_root_path}viewtopic.$phpEx""t=$topic_id") . '">''</a>'));
        }
    }
}

?>


And by the way, I had this in the config.php file:

Code: Select all
@define('PHPBB_INSTALLED'true);
@
define('DEBUG'true);
//@define('DEBUG_EXTRA', true);


I've just tried deleting the "//", but it doesn't make a difference (at least, not right now).
Concerning the installation, it's a fresh phpBB3 beta 5 install, then I applied a conversion to phpBB2 boards? could that be related?
User avatar
Pace
Style Author
Style Author
 
Posts: 147
Joined: 30 Aug 2006, 02:56
Location: Culuria, Residio
Gender: Male

Re: Karma.php problem?

Postby Handyman » 12 Feb 2007, 14:29

applied a conversion to phpBB2 boards?
does that mean you converted a phpBB2 board?
I'm thinking this blank page deal needs to be reported because it never happened with Beta 4? though I've only been able to duplicate it on upgraded boards Rant
Please contact me if you have any news to submit to SCOFF News.
SCOFFing at the candidates while you sleep.
My Mods || My Mod Queue
Image
User avatar
Handyman    
Rear Fleet Admiral
Rear Fleet Admiral
 
Posts: 7456
Joined: 08 May 2006, 04:45
Location: Where no man has gone before!
Favorite Team: Seattle Seahawks
Gender: Male

Re: Karma.php problem?

Postby Handyman » 12 Feb 2007, 14:34

Try this, change the karma_time_limit in the phpbb_config table, change the is_dynamic to 1 then reload the page.
Please contact me if you have any news to submit to SCOFF News.
SCOFFing at the candidates while you sleep.
My Mods || My Mod Queue
Image
User avatar
Handyman    
Rear Fleet Admiral
Rear Fleet Admiral
 
Posts: 7456
Joined: 08 May 2006, 04:45
Location: Where no man has gone before!
Favorite Team: Seattle Seahawks
Gender: Male

Re: Karma.php problem?

Postby Highway of Life » 12 Feb 2007, 15:41

Pace...
Code: Select all
if ($time_elapsed >= $config['karma_time_limit'])

Currently, that translates to:
Code: Select all
if ($time_elapsed >= 0)
no matter what you have the config value set to.
Watch out! I might do a code wheelie!

User avatar
Highway of Life    
STG Jedi Master
STG Jedi Master
 
Posts: 10458
Joined: 08 May 2006, 05:23
Location: Beware of Programmers carrying screwdrivers
Gender: Male
phpBB Knowledge: 10

Re: Karma.php problem?

Postby Pace » 13 Feb 2007, 00:29

I know, that's intended until I manage to get it working Grin
User avatar
Pace
Style Author
Style Author
 
Posts: 147
Joined: 30 Aug 2006, 02:56
Location: Culuria, Residio
Gender: Male

Re: Karma.php problem?

Postby Pace » 13 Feb 2007, 09:21

Okay, I've installed this on a non-converted phpBB3 board, the one running on arpia.be (updated from beta 4 to beta 5 a few weeks back).
And this time, I don't get the blank screen. After fixing a bunch of syntax errors (most notably two "// ?" that seemed to screw everything up), what I do get is an error:

Code: Select all
Fatal error: Call to a member function on a non-object in [i]blablabla/[/i]includes/functions.php on line 3319


Here's the current code:

Spoiler:
Code: Select all
<?php

define
('IN_PHPBB'true);
$phpbb_root_path './';
$phpEx substr(strrchr(__FILE__'.'), 1);
include(
$phpbb_root_path 'common.' $phpEx);

// Start session management
$user->session_begin();
$auth->acl($user->data);
$user->setup();

// Find out when voter last changed someone's karma
$sql 'SELECT karma_time, user_id FROM ' USERS_TABLE " WHERE user_id = {$user->data['user_id']}";
$result $db->sql_query($sql);
$row $db->sql_fetchrow($result);
$db->sql_freeresult($result);
$voter_id $row['user_id'];
$last_change $row['karma_time'];

// Get variables
$topic_id request_var('t''');
$user request_var('u''');
$vote request_var('x''');

$sql 'SELECT karma FROM ' USERS_TABLE " WHERE user_id = $user";
$result $db->sql_query($sql);
$karma $db->sql_fetchrow($result);
$db->sql_freeresult($result);

if (
$voter_id == $user)
{
    
trigger_error($user->lang['NO_SELF_KARMA'] . '<br /><br />' sprintf($user->lang['RETURN_TOPIC'], '<a href="' append_sid("{$phpbb_root_path}viewtopic.$phpEx""t=$topic_id") . '">''</a>'));
}
else
{
    if (!
$auth->acl_gets('a_''m_') && !$auth->acl_getf_global('m_'))
    {
        
trigger_error($user->lang['NO_USER_KARMA'] . '<br /><br />' sprintf($user->lang['RETURN_TOPIC'], '<a href="' append_sid("{$phpbb_root_path}viewtopic.$phpEx""t=$topic_id") . '">''</a>'));
    }
    else
    {
        
$time time();
        
$time_elapsed $time $last_change;
        
// Stop voter from changing karma during a delay period, fixed in the "phpbb3_config" table's "karma_time_limit" row
        
if ($time_elapsed >= $config['karma_time_limit'])
        {
            if (
$vote == 'applaud')
            {
                if (
$karma 8)
                {
                    
$sql 'UPDATE ' USERS_TABLE " SET karma = karma + 1 WHERE user_id = $user";
                    
$db->sql_query($sql);
                }
            }
            else
            {
                
$sql 'UPDATE ' USERS_TABLE " SET karma = karma - 1 WHERE user_id = $user";
                
$db->sql_query($sql);        
                
// Add bans on reaching -8 karma
                
if ($karma <= -8)
                {
                    
$sql 'INSERT INTO ' BANLIST_TABLE " VALUES (0, $user, '', NULL)";
                    
$db->sql_query($sql);
                }
            }
            
// Update the database with the current time() for the voter
            
$sql 'UPDATE ' USERS_TABLE " SET karma_time = $time WHERE user_id = $voter_id";
            
$db->sql_query($sql);
            
$redirect append_sid("{$phpbb_root_path}viewtopic.$phpEx""t=$topic_id");
        }
        else
        {
            
trigger_error($user->lang['TOO_SOON'] . '<br /><br />' sprintf($user->lang['RETURN_TOPIC'], '<a href="' append_sid("{$phpbb_root_path}viewtopic.$phpEx""t=$topic_id") . '">''</a>'));
        }
    }
}

?>


After experimenting a little, I saw that the problem is "$user->session_begin();". And guess what happens when I remove that? I get another error, this one involving auth.php
Why, o why?
User avatar
Pace
Style Author
Style Author
 
Posts: 147
Joined: 30 Aug 2006, 02:56
Location: Culuria, Residio
Gender: Male

PreviousNext

Return to phpBB3 Challenges at phpBB Academy

Who is online

Users browsing this forum: No registered users and 1 guest

cron