Deactive link
#1
Hi,

I'm trying to answer my own inquiry at the Plugin Requests forum (Email obfuscation), and have come up with this:

<?php

// Disallow direct access to this file for security reasons
if(!defined("IN_MYBB"))
{
    die("Direct initialization of this file is not allowed.");
}

$plugins->add_hook('parse_message', 'mailobf_parse');
function mailobf_parse($message) {
	global $mybb, $options;

	if ($mybb->settings['mailobf_enable'] == 1){
		if ($mybb->user['uid'] == 0) {
			$message = preg_replace("/[^@\s]*@[^@\s]*\.[^@\s]*/", "[removed]", $message);
			return $message;
		}
	}
}

function mailobf_info()
{
    return array(
        "name"          => "Mail Obfuscator",
        "description"   => "Sanitizes e-mail addresses in posts.",
        "website"       => "http://mybb.com/",
        "author"        => "Gingah,
        "authorsite"    => "http://mybb.com/",
        "version"       => "1.0.0rc",
        "guid"          => "",
        "compatibility" => "18*"
    );
}

function mailobf_install() {}

function mailobf_is_installed() {}

function mailobf_uninstall() {}

function mailobf_activate() {
	global $db;

	$mailobf_group = array(
		'gid' => 'NULL',
		'name' => 'mailobf',
		'title' => 'Mail Obfuscator',
		'description' => 'Mail Obfuscator Settings',
		'disporder' => "1",
		'isdefault' => "0",
	); 
	$db->insert_query('settinggroups', $mailobf_group);
	$gid = $db->insert_id();
	$mailobf_setting = array(
		'sid' => 'NULL',
		'name' => 'mailobf_enable',
		'title' => 'Do you want to enable the Mail Obfuscator?',
		'description' => 'If you set this option to yes, this plugin be active on your board.',
		'optionscode' => 'yesno',
		'value' => '1',
		'disporder' => 1,
		'gid' => intval($gid),
	);
	$db->insert_query('settings', $mailobf_setting);
	rebuild_settings();
}

function mailobf_deactivate() {
	global $db;
	$db->query("DELETE FROM ".TABLE_PREFIX."settings WHERE name IN ('mailobf_enable')");
	$db->query("DELETE FROM ".TABLE_PREFIX."settinggroups WHERE name='mailobf'");
	rebuild_settings();
}

The problem I've run into is that when the plugin is activate, the Plugins page at the Admin CP still shows the Install & Activate link. So it lets me install and activate it again, rather than show the link for uninstalling and deactivating it. Any idea how to rectify this?
Reply
#2
In the _is_installed function you need to use PHP code to determine if it should return true or return false. There are a couple ways you could test this. You can globalize the variable $cache. You can then read the plugin cache like this:

function mailobf_is_installed()
{
global $cache;
$pluginlist = $cache->read("plugins");
if(array_key_exists("Mail Obfuscator", $pluginlist['active']))
{
return true;
}
return false;
}

I'm not 100% sure that I have the array key name correct, but it should be at least close to that. You can view what it would be if you go to ACP->Tools->Cache Manager. View the Plugin Cache.

Another way you can check if it is installed is by querying the database for either a table your plugin creates, a new field that your plugin adds, or a setting group your plugin adds.

P.S. In the line where you define the array key author, you're missing a double quote.
Reply
#3
Edit: delayed response

you can safely remove the empty functions and check
// function mailobf_install() {}

// function mailobf_is_installed() {}

// function mailobf_uninstall() {}
Reply
#4
Try this:
<?php

// Disallow direct access to this file for security reasons
if(!defined("IN_MYBB"))
{
    die("Direct initialization of this file is not allowed.");
}

function mailobf_info()
{
    return array(
        "name"          => "Mail Obfuscator",
        "description"   => "Sanitizes e-mail addresses in posts.",
        "website"       => "http://mybb.com/",
        "author"        => "Gingah",
        "authorsite"    => "http://mybb.com/",
		"codename"		=> "mailobf",
        "version"       => "1.0.0rc",
        "compatibility" => "18*"
    );
}

function mailobf_activate() 
{
    global $db;

    $mailobf_group = array(
        'gid' => 'NULL',
        'name' => 'mailobf',
        'title' => 'Mail Obfuscator',
        'description' => 'Mail Obfuscator Settings',
        'disporder' => "1",
        'isdefault' => "0"
    ); 
    $db->insert_query('settinggroups', $mailobf_group);
    $gid = $db->insert_id();
    $mailobf_setting = array(
        'sid' => 'NULL',
        'name' => 'mailobf_enable',
        'title' => 'Do you want to enable the Mail Obfuscator?',
        'description' => 'If you set this option to yes, this plugin be active on your board.',
        'optionscode' => 'yesno',
        'value' => '1',
        'disporder' => 1,
        'gid' => intval($gid)
    );
    $db->insert_query('settings', $mailobf_setting);
    rebuild_settings();
}

function mailobf_deactivate() 
{
	global $db;
	
	$result = $db->simple_select('settinggroups', 'gid', "name = 'mailobf'", array('limit' => 1));
	$group = $db->fetch_array($result);
	
	if(!empty($group['gid']))
	{
		$db->delete_query('settinggroups', "gid='{$group['gid']}'");
		$db->delete_query('settings', "gid='{$group['gid']}'");
		rebuild_settings();
	}
} 

function mailobf_parse(&$message) {
    global $mybb;

    if ($mybb->settings['mailobf_enable'] == 1){
        if ($mybb->user['uid'] == 0) {
            $message = preg_replace("/[^@\s]*@[^@\s]*\.[^@\s]*/", "[removed]", $message);
            return $message;
        }
    }
}
$plugins->add_hook('parse_message', 'mailobf_parse');
Reply
#5
I don't like adding / removing settings in _activate / _deactivate because if the plugin needs to be deactivated to test something, you don't lose all setting data.
Reply
#6
Here it's only a "Yes No enable" switch - but you're right, in some cases it's suboptimal.
Reply
#7
I see the point about keeping settings out of activate/deactivate, but as SvePu pointed out the plugin purely has one function: Hiding e-mail addresses when the post is viewed without being logged in. By that token, the setting it registers is functionally equivalent to deactivating the plugin. So the plugin can be quickly disabled from the settings, or by deactivating it.

And thanks for the code-suggestions and optimizations, SvePu's code worked exactly as expected.
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)