Not Solved Predefined Variables?
#1
Not Solved
Hi there everyone!
I have read through the plugin tutorial in the wiki and it seems pretty straight forward.
I do have a question though:
The tutorial uses $post['message'] and other predefined variables/arrays. Is there a list of those? Or somewhere I can look up what variable/array is used on which page or section of the forum?

Thanks in advance for any helpful information. Smile

I'm considering a move away from vBullin with their planned license changes (or rumored to be planned) and right now MyBB is top of the list of candidates for me. But I need to get to grips with the plugin system first.
Reply
#2
Not Solved
There's a list of common ones here: [Wiki: Plugin_Methods] (Broken link, head over to docs.mybb.com instead)

Unfortunately it's not a long list(!) but introduces the important ones. Normally you'll be looking through the file you want to change (such as showthread.php), and realise that you'll need the $post variable, or others.

Of course, we're always on hand to guide in the right direction if you get stuck! Smile
Reply
#3
Not Solved
Thanks very much for your quick reply. Smile
I hope I am not going off topic with this, but I'm just trying to get my head around the whole plugins system. So I might be asking some stupid questions. So please bear with me. Undecided

A plugin I want to write (partially to get to know the plugin system) is an anonymous posting feature.
What it will do is: it checks if the user is in forum X. If he is, it attributes the post he makes to user B. User B is a user with the name "Anonymous".

First of all though I need to find out how to check if the user/visitor is in forum X. How would I go about doing that?
Reply
#4
Not Solved
If you're doing something like this, then it's probably best to hook into the datahandler to alter the information before it hits the database. The datahandler is ./inc/datahandlers/post.php.

If you look around line 832, there's the 'datahandler_post_insert_post' hook. I think something like this would work in your plugin:

function anon_posting($insert_data)
{
	if($insert_data['fid'] == 4)
	{
		$insert_data['uid'] = "2";
		$insert_data['username'] = "Anonymous";
	}
	
	return $insert_data;
}

Basically, it checks if the forum the post is being posted in is forum #4, and if it is, sets the data to be anonymous. Then, returns the information before it's inserted into the database.

Untested though - I'm not really that informed with plugins...!...
Reply
#5
Not Solved
Cool thanks!
Ok, here is how it looks right now.

<?php
/**
 * MyBB 1.4
 * Copyright © 2008 MyBB Group, All Rights Reserved
 *
 * Website: http://www.mybboard.net
 * License: http://www.mybboard.net/about/license
 *
 * $Id: anon.php 4304 2009-01-02 01:11:56Z chris $
 */
 
// 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.");
}

$plugins->add_hook("datahandler_post_insert_post", "anon_posting");

function anon_info()
{
	/**
	 * Array of information about the plugin.
	 * name: The name of the plugin
	 * description: Description of what the plugin does
	 * website: The website the plugin is maintained at (Optional)
	 * author: The name of the author of the plugin
	 * authorsite: The URL to the website of the author (Optional)
	 * version: The version number of the plugin
	 * guid: Unique ID issued by the MyBB Mods site for version checking
	 * compatibility: A CSV list of MyBB versions supported. Ex, "121,123", "12*". Wildcards supported.
	 */
	return array(
		"name"			=> "Anonymous Forum",
		"description"	=> "A plugin that attributes all user posts in a certain forum to user X (anonymous)",
		"website"		=> "",
		"author"		=> "Been Told",
		"authorsite"	=> "",
		"version"		=> "1.0",
		"guid" 			=> "",
		"compatibility" => "*"
	);
}

function anon_posting($insert_data)
{
    if($insert_data['fid'] == 3)
    {
        $insert_data['uid'] = "2";
        $insert_data['username'] = "Anonymous";
    }
    
    return $insert_data;
} 
?>
When making a post, I get this error:
Quote:Fatal error: Cannot use object of type PostDataHandler as array in C:\xampp\htdocs\mybb\inc\plugins\anon.php on line 47
The line in question is:
if($insert_data['fid'] == 3)

I'm not quite sure what to make of that. Huh
Reply
#6
Not Solved
Change the function into
function anon_posting(&$insert_data)
{
    if($insert_data->post_insert_data['fid'] == 3)
    {
        $insert_data->post_insert_data['uid'] = "2";
        $insert_data->post_insert_data['username'] = "Anonymous";
    }
    
    return $insert_data;
} 
Reply
#7
Not Solved
Thanks Lex-! It seems to have worked! I will fool around some more and possibly even publish the thing. Smile
Reply
#8
Not Solved
Bah! I missed the &...

If you see a hook like the one in the datahandler, with a variable in it (in this case, "$this"), then to you need to have a &$(assigned variable) in the function options and return it in the same function, otherwise it wouldn't work. As LeX- points out!...
Reply
#9
Not Solved
I have another question lol.
Now that the anonymisation is working, I'm starting on the next step: saving the name of the real user in an extra field in the database.
For that I need to determine the username. I tried
$mybb->user['username']
(as suggested here)and nothing was returned. Is that incorrect?
Reply
#10
Not Solved
Insert at the top of the function:
global $mybb;
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)