Jump to content

Is this code wrong?!


zero061099

Recommended Posts

  This a mod in devlopment for phpbb forum script written in php.

 

I have a excerpt here of the code that I believe isnt quite right and was wondering if someone could point out something I may be missing. This part of code is to update a forums topics and posts +1.

 

All that seems to happen is the script im running if executed resets topic/posts to "1" instead of adding the "1" plus however many post/topic totals existing.

 

I assure you I REALLY think something isnt right here...BTW sorry Im new and I apologize for coming out right away to ask for help!! Thanks SO much.

 

heres the code....

 

   // Update the Forum posts counts so they match teh actual post posts counts (keeps everything right) - Hawke
   $sql = 'UPDATE ' . FORUMS_TABLE . " SET forum_posts = forum_posts + 1 WHERE forum_id = $linkbot_binid";
   $db->sql_query($sql);
   $sql = 'UPDATE ' . FORUMS_TABLE . " SET forum_topics = forum_topics + 1 WHERE forum_id = $linkbot_binid";
   $db->sql_query($sql);
   $sql = 'UPDATE ' . FORUMS_TABLE . " SET forum_topics_real = forum_topics_real + 1 WHERE forum_id = $linkbot_binid";
   $db->sql_query($sql);
   $sql = 'UPDATE ' . USERS_TABLE . " SET user_posts = user_posts + 1 WHERE user_id = ".$post_from_user['user_id'];
   $db->sql_query($sql);

   $topic_count = $config['num_topics'] + 1;
   set_config('num_topics', $topic_count);

   $post_count = $config['num_posts'] + 1;
   set_config('num_posts', $post_count);
// End of the Reply to users posts function (thank god for that!!!) - Hawke

 

whaddya think?????

Link to comment
https://forums.phpfreaks.com/topic/103750-is-this-code-wrong/
Share on other sites

well bc ..hmmm ok heres a example...its a link checker BOT. SO if the bot finds a dead link it will move it to the admin specified topic (ex: Trash)

 

Now the code I have shown is supposed to query the original posts and topic count and then ADD the new post the bot made to the count.

 

For some reason I cant get it to do that. Instead for every post the bot makes it resets the TOTAL posts and topics count to "0" and just adds the amount of posts the bot had made.

 

SO in effect if a forum has 2,000 posts AFTER the bot makes the post it should be 2,001 total post

 

NOT 1 total posts.

 

see pic

 

BEFORE:

1zmfn8p.jpg

 

AFTER bot makes the dead link post:

2h8d5cm.jpg

 

I hope thats a better expanation :)

Link to comment
https://forums.phpfreaks.com/topic/103750-is-this-code-wrong/#findComment-531204
Share on other sites

hmmmmm the rules said I cant post the whole code I hope I dont get into trouble.....

 

Ill try that....for now....

 

heres the whole code.....

 

<?php
if ( !defined('IN_PHPBB') )
{
die('Hacking attempt');
}
define('IN_PHPBB', true);
$phpbb_root_path = (defined('PHPBB_ROOT_PATH')) ? PHPBB_ROOT_PATH : './';
$phpEx = substr(strrchr(__FILE__, '.'), 1);
include($phpbb_root_path . 'includes/functions_posting.'.$phpEx);
include($phpbb_root_path . 'includes/functions_privmsgs.' . $phpEx);



//Begin the Reply Function for The Linkbot - Hawke
function linkbot_reply($topicid) {
global $db, $user, $auth;
// get the Bots Configuration files from teh database - Hawke
$sql = "SELECT *
FROM " . CONFIG_TABLE;
if( !($result = $db->sql_query($sql)) )
{
message_die(CRITICAL_ERROR, "Could not query config information", "", __LINE__, __FILE__, $sql);
}

while ( $row = $db->sql_fetchrow($result) )
{
$linkbot[$row['config_name']] = $row['config_value'];
}

	$linkbot_name = $linkbot[bot_name];
	$linkbot_userid = $linkbot[bot_userid];
	$linkbot_percentage = $linkbot[bot_percent];
	$linkbot_binid = $linkbot[bot_binid];
	$linkbot_topic = $linkbot[bot_topic];
	$linkbot_pm = $linkbot[bot_pm];
	$linkbot_pm_title = $linkbot[bot_pm_title];
	$linkbot_topic_title = $linkbot[bot_topic_title];



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



$sql = 'SELECT * FROM '.USERS_TABLE.' WHERE user_id='.$linkbot_userid;
$result = $db->sql_query($sql);
$post_from_user = $db->sql_fetchrow($result);
$db->sql_freeresult($result);

    //Get the Bots IP address - Hawke
    $sql = "SELECT session_ip FROM ".SESSIONS_TABLE." WHERE session_user_id = ".$user->data['user_id'];
    $result = $db->sql_query($sql);
    $row = $db->sql_fetchrow($result);
    $user_ip = $row['session_ip'];

    $forum = $linkbot_binid; //Bin ForumID - Hawke
$message = str_replace("{BOT_PERCENT}", $linkbot_percentage, $linkbot_topic); // Replace any of the Custom Template Variable - Hawke
if ( $message != '' )
{
                     
   //Topic ID - Hawke
    $topic_id = $topicid;
     
// Parse the text with the bbcode parser and write into $text - Hawke
$text		= utf8_normalize_nfc($message);

$uid			= $bitfield			= $options	= '';	// will be modified by generate_text_for_storage - Hawke
$allow_bbcode	= $allow_smilies	=	$allow_urls	= true;

    generate_text_for_storage($text, $uid, $bitfield, $options, $allow_bbcode, $allow_urls, $allow_smilies);

    $text			= generate_text_for_display($text, $uid, $bitfield, $options);

    // Now parse the text for bbcode and smilies - Hawke
    $message_parser = new parse_message();
    $message_parser->message = $message;
    $message_parser->parse($allow_bbcode, $allow_urls, $allow_smilies);

	//Build The array for the post to be sent to the database - Hawke
    	$sql_ary = array(

						'topic_id'		            => $topic_id,
						'forum_id'	                => $linkbot_binid,
						'poster_id'	                => $post_from_user['user_id'],
						'poster_ip'	                => $user_ip,
						'post_time'	                => time(),
						'post_approved'             => 1,
						'post_subject'	            => $linkbot_topic_title,
						'post_text'	                => $message_parser->message,
						'bbcode_bitfield'	        => $message_parser->bbcode_bitfield,
						'bbcode_uid'		        => $message_parser->bbcode_uid,
                            'post_checksum'             => md5($message_parser->message),

				);

//Insert the data into the posts table - Hawke
$sql = 'INSERT INTO ' . POSTS_TABLE . ' ' . $db->sql_build_array('INSERT', $sql_ary);
$db->sql_query($sql);

    $post_id = $db->sql_nextid();

//Update the users topics table - Hawke
$sql_ary = array(

						'topic_last_post_id' => $post_id,
						'topic_last_poster_id'	=> $post_from_user['user_id'],
						'topic_last_post_subject'	=> $linkbot_topic_title,
						'topic_last_post_time' => time(),
						'topic_last_poster_name'	=> $post_from_user['username'],
						'topic_last_poster_colour' => $post_from_user['user_colour'],
						);
$sql = 'UPDATE ' . TOPICS_TABLE . '
			SET ' . $db->sql_build_array('UPDATE', $sql_ary) . "
			WHERE topic_id = $topic_id";
		$db->sql_query($sql);

		// Update the Replies to the post so the counts match whats in the topic - Hawke
		$sql = 'UPDATE ' . TOPICS_TABLE . " SET topic_replies = topic_replies + 1 WHERE topic_id = $topic_id";
$db->sql_query($sql);
$sql = 'UPDATE ' . TOPICS_TABLE . " SET topic_replies_real = topic_replies_real + 1 WHERE topic_id = $topic_id";
$db->sql_query($sql);

//Lets do the updating of the forums table - Hawke
$sql_ary = array(

						'forum_last_post_id' => $post_id,
						'forum_last_poster_id'	=> $post_from_user['user_id'],
						'forum_last_post_subject'	=> $linkbot_topic_title,
						'forum_last_post_time' => time(),
						'forum_last_poster_name'	=> $post_from_user['username'],
						'forum_last_poster_colour' => $post_from_user['user_colour'],

						);



	$sql = 'UPDATE ' . FORUMS_TABLE . '
			SET ' . $db->sql_build_array('UPDATE', $sql_ary) . "
			WHERE forum_id = $linkbot_binid";
		$db->sql_query($sql);
//
// Start moving topic and posts to the Dead Links Forum - Hawke
//
	$sql = "UPDATE " . TOPICS_TABLE . " 
		SET forum_id = $linkbot_binid
		WHERE topic_id = $topic_id";
	if ( !($result = $db->sql_query($sql)) )
	{
		message_die(GENERAL_ERROR, 'Could not update topics table', '', __LINE__, __FILE__, $sql);
	}

	$sql = "UPDATE " . POSTS_TABLE . " 
		SET forum_id = $linkbot_binid
		WHERE topic_id = $topic_id";
	if ( !($result = $db->sql_query($sql)) )
	{
		message_die(GENERAL_ERROR, 'Could not update topics table', '', __LINE__, __FILE__, $sql);
	}

	// Update the Forum posts counts so they match teh actual post posts counts (keeps everything right) - Hawke
$sql = 'UPDATE ' . FORUMS_TABLE . " SET forum_posts = forum_posts + 1 WHERE forum_id = $linkbot_binid";
$db->sql_query($sql);
$sql = 'UPDATE ' . FORUMS_TABLE . " SET forum_topics = forum_topics + 1 WHERE forum_id = $linkbot_binid";
$db->sql_query($sql);
$sql = 'UPDATE ' . FORUMS_TABLE . " SET forum_topics_real = forum_topics_real + 1 WHERE forum_id = $linkbot_binid";
$db->sql_query($sql);
$sql = 'UPDATE ' . USERS_TABLE . " SET user_posts = user_posts + 1 WHERE user_id = ".$post_from_user['user_id'];
$db->sql_query($sql);

$topic_count = $config['num_topics'] + 1;
set_config('num_topics', $topic_count);

$post_count = $config['num_posts'] + 1;
set_config('num_posts', $post_count);

// End of the Reply to users posts function (thank god for that!!!) - Hawke

    function linkbot_pm($topm, $postid) {
global $db, $user, $auth;
// get the Bots Configuration files from teh database - Hawke
$sql = "SELECT *
FROM " . CONFIG_TABLE;
if( !($result = $db->sql_query($sql)) )
{
message_die(CRITICAL_ERROR, "Could not query config information", "", __LINE__, __FILE__, $sql);
}

while ( $row = $db->sql_fetchrow($result) )
{
$linkbot[$row['config_name']] = $row['config_value'];
}
	$linkbot_name = $linkbot[bot_name];
	$linkbot_userid = $linkbot[bot_userid];
	$linkbot_percentage = $linkbot[bot_percent];
	$linkbot_binid = $linkbot[bot_binid];
	$linkbot_topic = $linkbot[bot_topic];
	$linkbot_pm = $linkbot[bot_pm];
	$linkbot_pm_title = $linkbot[bot_pm_title];
	$linkbot_topic_title = $linkbot[bot_topic_title];

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

$sql = 'SELECT * FROM '.USERS_TABLE.' WHERE user_id='.$linkbot_userid;
$result = $db->sql_query($sql);
$post_from_user = $db->sql_fetchrow($result);
$db->sql_freeresult($result);

    //Get the Bots IP address - Hawke
    $sql = "SELECT session_ip FROM ".SESSIONS_TABLE." WHERE session_user_id = ".$user->data['user_id'];
    $result = $db->sql_query($sql);
    $row = $db->sql_fetchrow($result);
    $user_ip = $row['session_ip'];

$domain= 'http://'.$linkbot['server_name'] . "" .$linkbot['script_path']; //Get Domain name and Path for Edit Url Variable
$linkbot_pm = str_replace("{BOT_PERCENT}", $linkbot_percentage, $linkbot_pm);
$linkbot_pm = str_replace("{EDIT_URL}", "[url=" .$domain . "/posting.php?mode=editpost&p=" .$postid. "]Edit Links[/url]" , $linkbot_pm);
$subject = $linkbot_pm_title;
$message_parser = new parse_message();
$message_parser->message = $linkbot_pm;
$message_parser->parse(true, true, true, false, false, true, true); 
    $uid = $bitfield = $options = ''; // will be modified by generate_text_for_storage
    $allow_bbcode = $allow_smilies = $allow_urls = true;
    

    $pm_data = array(
       'from_user_id'        => $linkbot_userid,
       'from_user_ip'        => $user->data['user_ip'],
       'from_username'        => $linkbot_name,
       'enable_sig'            => true,
       'enable_bbcode'        => $allow_bbcode,
       'enable_smilies'        => $allow_smilies,
       'enable_urls'        => false,
       'icon_id'            => 0,
       'bbcode_bitfield'    => $message_parser->bbcode_bitfield,
       'bbcode_uid'         => $message_parser->bbcode_uid,
       'message'            => $message_parser->message,
       'address_list'        => array('u' => array($topm => 'to')),
    );

    //Now We Have All Data Lets Send The PM!!
    submit_pm('post', $subject, $pm_data, false, false);
}
}
}
?>

 

 

 

EDIT: no I didnt think the code u said would work BUT I tried! Thanks for ur help though :)

Link to comment
https://forums.phpfreaks.com/topic/103750-is-this-code-wrong/#findComment-531213
Share on other sites

The problem, as I see it, is with 'num_topics'. This is a string. So when you put this string into the $config[] array, it does wrong things. I'm not sure what $config is doing either, because it's defined elsewhere in one of your includes.

 

Try

 

print $config['num_topics'];

 

and see what you get. I'm guessing it's zero at best - and an error at worst. The value in an array should be a number:

 

$config[0];

 

 

Link to comment
https://forums.phpfreaks.com/topic/103750-is-this-code-wrong/#findComment-531251
Share on other sites

sorry i took so long to reply lemme take a look and try a couple things off of what u just said....thanks

 

ok for ur expanation...i wanted to give u more info....

 

this mod is running off of PHPBB3..its a forum script kinda like this one SMF.

 

Also this mod uses to files....the one I showed u and then it uses a CRON job that runs off of linkbot.php

 

I will play some more while await ur response. :)

Link to comment
https://forums.phpfreaks.com/topic/103750-is-this-code-wrong/#findComment-531321
Share on other sites

OK since I cant edit my other post I have to make a new one.

 

SO I tried to fiddle around with what you said and same result.

 

add "print" in front of $config['num_topics'];

 

and also tried the value "0" to replace 'num_topics' like this

 

print $config[0]

 

I dunno im about to give up I have NO clue why it keeps doing this. It works GREAT except for the topics and posts going back to 1 or howecer many post get moved by the bot! ahhhhh

Link to comment
https://forums.phpfreaks.com/topic/103750-is-this-code-wrong/#findComment-531354
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.