MyBB Hacks

Full Version: Random number in Thread Subject
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hi,

we have a plugin called "Report Thread". here the original File:

PHP Code:
<?php
/**
Report to Thread 0.1.3
----------------------

This program is free software: you can redistribute it and/or modify it under the terms of the 
GNU General Public License as published by the Free Software Foundation,
either version 3 of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. 
See the GNU General Public License for more details.

You should have received a copy of the GNU General Public License along with this program. 
If not, see <http://www.gnu.org/licenses/>.
**/

if(!defined("IN_MYBB"))
{
	die("Direct initialization of this file is not allowed.<br /><br />Please make sure IN_MYBB is defined.");
}

$plugins->add_hook('report_do_report_end', 'reportthread_dopost');

function reportthread_info()
{
	return array(
		"name" => "Report To Thread",
		"description" => "Opens a discussion thread about the reported item when a user reports a post.",
		"website" => "http://forumgods.com/",
		"author" => "Tony Hudgins",
		"authorsite" => "http://forumgods.com/forums/member.php?160-Syke",
		"version" => "0.1.3",
		"compatibility" => "16*",
		"guid" => "8624478eb178c6eb37a503e960c267ec"
	);
}

function reportthread_install()
{
	global $db;
	
	$reported_group = array('gid' => 'NULL',
							'name' => 'reportthread',
							'title' => 'Report to Thread',
							'description' => 'Configure the Report to Thread plugin.',
							'disporder' => '1');
							
	$db->insert_query("settinggroups", $reported_group);
	$gid = $db->insert_id();
	
	$reported_setting1 = array('sid' => 'NULL',
							   'name' => 'rtt_enabled',
							   'title' => 'Enabled',
							   'description' => 'Whether or not to create new discussion threads for reported items.',
							   'optionscode' => 'yesno',
							   'value' => 'yes',
							   'disporder' => '1',
							   'gid' => intval($gid));
								   
	$reported_setting2 = array('sid' => 'NULL',
							   'name' => 'rtt_fid',
							   'title' => 'Forum ID',
							   'description' => 'The forum to post report threads to.',
							   'optionscode' => 'text',
							   'value' => '1',
							   'disporder' => '2',
							   'gid' => intval($gid));
								   
	$db->insert_query('settings', $reported_setting1);
	$db->insert_query('settings', $reported_setting2);
	
	rebuild_settings();
}

function reportthread_is_installed()
{
	global $db;
	
	$group = $db->query("SELECT `name` FROM `" . TABLE_PREFIX . "settinggroups` WHERE `name` = 'reportthread' LIMIT 1;");
	$s1 = $db->query("SELECT `name` FROM `" . TABLE_PREFIX . "settings` WHERE `name` = 'rtt_enabled' LIMIT 1;");
	$s2 = $db->query("SELECT `name` FROM `" . TABLE_PREFIX . "settings` WHERE `name` = 'rtt_fid' LIMIT 1;");
	
	if( $db->num_rows($group) >= 1 && $db->num_rows($s1) >= 1 && $db->num_rows($s2) )
		return true;
	else
		return false;
}

function reportthread_uninstall()
{
	global $db;
	
	$db->query("DELETE FROM `" . TABLE_PREFIX . "settinggroups` WHERE `name` = 'reportthread' LIMIT 1;");
	$db->query("DELETE FROM `" . TABLE_PREFIX . "settings` WHERE `name` = 'rtt_enabled' LIMIT 1;");
	$db->query("DELETE FROM `" . TABLE_PREFIX . "settings` WHERE `name` = 'rtt_fid' LIMIT 1;");
	
	rebuild_settings();
}

function reportthread_activate()
{
	global $db;
	
	$db->query("UPDATE `" . TABLE_PREFIX . "settings` SET `value` = 'yes' WHERE `name` = 'rtt_enabled' LIMIT 1;");
}

function reportthread_deactivate()
{
	global $db;
	
	$db->query("UPDATE `" . TABLE_PREFIX . "settings` SET `value` = 'no' WHERE `name` = 'rtt_enabled' LIMIT 1;");
}

function reportthread_dopost()
{
	require_once(MYBB_ROOT . "inc/datahandlers/post.php");
	global $db, $mybb;
	
	if(intval($mybb->settings['rtt_enabled']) == 1 || preg_replace("/[^a-z]/i", "", $mybb->settings['rtt_enabled']) == "yes")
	{
		$post = get_post($mybb->input['pid']);
		$thread = get_thread($post['tid']);
		$forum = get_forum($thread['fid']);
		
		$tlink = get_post_link($mybb->input['pid'],$post['tid'])."#pid".$mybb->input['pid'];
		$flink = get_forum_link($thread['fid']);
		
		$post_data = $mybb->user['username'] . " hat einen Beitrag gemeldet!

Original Thread: [url=" . $mybb->settings['bburl'] . "/$tlink]" . $thread['subject'] . "[/url]
Forum: [url=" . $mybb->settings['bburl'] . "/$flink]" . $forum['name'] . "[/url]
Uploader: [url={$mybb->settings['bburl']}/member.php?action=profile&uid=".($post['uid'])."]{$post['username']}[/url]

Angegebener Grund:
[quote=\"" . $mybb->user['username'] . "\" dateline=\"" . time() . "\"]" . $mybb->input['reason'] . "[/quote]

Beitrags Inhalt:
[quote=\"" . $post['username'] . "\" pid=\"" . $post['pid'] . "\" dateline=\"" . $post['dateline'] . "\"]" . $post['message'] . "[/quote]";
		
		$new_thread = array(
			"fid" => $mybb->settings['rtt_fid'],
			"prefix" => 0,
			"subject" => "[Meldung] Downfile: @" . $post['username'],
			"icon" => 0,
			"uid" => $mybb->user['uid'],
			"username" => $mybb->user['username'],
			"message" => $post_data,
			"ipaddress" => get_ip(),
			"posthash" => md5($mybb->user['uid'] . random_str()),
		);

		$posthandler = new PostDataHandler("insert");
		$posthandler->action = "thread";
		$posthandler->set_data($new_thread);
		
		if($posthandler->validate_thread())
			$thread_info = $posthandler->insert_thread();
	}
}
?>


now my problem:

we have a function, that a user cannot post the same subject 2 times. so we must have a solution, cause the downfile-reports have all the same subject. this line (144) is for the thread-subject:

PHP Code:
"subject" => "[Meldung] Downfile: @" . $post['username'],

Result: [Meldung] Downfile: @Username

and this is what i need: a random number behind "Meldung". so that it looks like this:

[Meldung257813] Downfile: @Username
257813 = the random number
the number must not be at this place. can look like this:
[Meldung] 257813 Downfile: @Username

But i must have a random number in thread subject. is this possible? would be great Wink

http://www.php.net/manual/en/function.rand.php

Can't guaranty they will actually be random numbers, especially the shortest they are.
thx Sama34, but i dont know how to build in this correctly in php-file above Wink
i´am not so professional with php.
If you only need a unique thread subject, you can try to use the post id.
I though you write this plugin, find:

PHP Code:
"subject" => "[Meldung] Downfile: @" . $post['username'],


Replace: (see below)

PHP Code:
"subject" => "[Meldung] ".rand(0, 10)." Downfile: @" . $post['username'],


Maybe using md5(); with the username and subtracting will be more "random".

Edit: Nevermind, RateU suggestion is more random unique:

Replace:

PHP Code:
"subject" => "[Meldung] {$post['pid']} Downfile: @" . $post['username'],

(08-05-2012 05:33 AM)Sama34 Wrote: [ -> ]RateU suggestion is more random:
Only if each post are allowed to be reported once.
Perfect Sama34 & RateU Smile

but i´m a big idiot and thought too complicated [Image: 3909112_63f3e8e853_m.gif]

solution with Post-ID. that´s so easy. Grrrrr Biggrin
Reference URL's