MyBB Community Forums

Full Version: Writing to an existing page using templates
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Okay, so I don't know how many times I have Google'd this now. I'm sure it's been asked before, but I have no clue as to how I find it. So I guess this will be a very specific support thread.

I am writing a quite simple plugin called SVIP. It consists of two files, one of which is completely irrelevant as it just reads out some info from the database in JSON.
The relevant part is the plugin file itself (svip.php).
I want it to simply generate a nonce and hash it with MD5. The hash, which essentially is an API key, should be displayed in the user control panel. The reason for this, is that it is completely private. I cannot use custom profile fields as they would either be visible to everyone or to admins only. I want it to be visible to the user only, admins will never need it.

The plugin docs are extremely vague and I really don't understand what is what when I'm told to eval the template.
Here's my code:
<?php
if(!defined("IN_MYBB"))
{
	die("Direct initialization of this file is not allowed.<br /><br />Please make sure IN_MYBB is defined.");
}

function svip_info() {
	return array(
		"name"				=> "SVIP-API",
		"description"		=> "Allocate a private token (API key) for each user to enable other sites to check the VIP status of a member.",
		"website"			=> "",
		"author"			=> "Time Sheep",
		"authorsite"		=> "",
		"version"			=> "0.1",
		"compatibility" 	=> "16*",
		"guid" 				=> "8dd0f30539e74d30897088aa80ac437c"
	);
}

$plugins->add_hook("usercp_start", "svip_gettoken");

function svip_activate() {
	global $mybb, $db;
	$db->write_query("ALTER TABLE `" . TABLE_PREFIX . "users` ADD `apikey` CHAR(32)");
	
	// Insert template
	$result = $db->simple_select("templates", "tid", "1", array("order_by" => "tid DESC", "limit" => "1"));
	$id = $db->fetch_field($result, "tid");
	$db->free_result($result);
	
	$template = array(
		"tid" 		=> $id,
		"title" 	=> 'usercp_svip',
		"template"	=> '<strong>SVIP-token:</strong> {$token}<br />',
		"sid"		=> '-2',
		"version"	=> '1601',
		"dateline"	=> time()
	);
	
	$db->insert_query("templates", $template);
}

function svip_deactivate() {
	global $db;
	$db->write_query("ALTER TABLE `" . TABLE_PREFIX . "users` DROP `apikey`");	
	$db->delete_query("templates", "title LIKE 'usercp_svip' AND sid='-2'");
}

function svip_gettoken() {
	global $db, $mybb, $templates;
	
	$token = hash("md5", (string)rand());
    $db->update_query("users", array("apikey" => $token), "uid=" . $mybb->user['uid']);
	
	eval('\$svip = "' . $templates->get('usercp_svip') . '";');
	echo $svip;
}
?>

This is my usercp template:
<html>
<head>
    <title>{$lang->user_cp}</title>
</head>

<body>
    {$headerinclude} {$header} {$usercpnav}

    <table align="center" border="0" width="100%">
        <tr>
            <td valign="top">
                {$avatar}

                <table border="0" cellpadding="{$theme['tablespace']}" cellspacing="{$theme['borderwidth']}" class="tborder">
                    <tr>
                        <td class="thead" colspan="{$colspan}"><strong>{$lang->account_summary}</strong></td>
                    </tr>

                    <tr>
                        <td class="trow2">
                            <span class="largetext">{$username}</span><br>
                            <strong>{$lang->postnum}</strong> <a href="search.php?action=finduser&amp;uid={$mybb-%3Euser['uid']}">{$mybb->user['posts']}</a> {$lang->posts_day}<br>
                            {$reputation} <strong>{$lang->email}</strong> {$mybb->user['email']}<br>
                            <strong>{$lang->registration_date}</strong> {$regdate}<br>
                            <strong>{$lang->primary_usergroup}</strong> {$usergroup}<br>
                            {$svip} 
                            {$referral_info}
                        </td>
                    </tr>
                </table>{$latest_subscribed} {$latest_threads} {$latest_warnings} {$user_notepad}
            </td>
        </tr>
    </table>{$footer}
</body>
</html>

You can probably tell that I want my little template bit to go where I wrote {$svip}, but I really can't get it to work.
The MyBB docs are horrible, only about 1/5th of the most basic methods of the globals are explained and I can't even find anything useful about templates (For plugin devs, for forum owners it's fine).

How do I make MyBB write the API key where I wrote {$svip}?