MyBB Community Forums

Full Version: Plugin losing Global scope
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
I've been looking at this code for hours and I can't figure out what's wrong. When I "activate" my plugin, it works fine, but when I "deactivate" it, it won't remove the string. It's almost like the pattern isn't matching, but i'm using the same exact pattern, so I don't know what's going on.

I've tried with #<pat>#s and also #<pat>#m, but none of those work either.

// myplug.php

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

require_once(MYBB_ROOT."/inc/adminfunctions_templates.php");

$mod_template = 'member_login';
$pattern = '{$footer}';
$replacement = 'hello!';

function myplug_activate()
{
  global $mod_template, $pattern, $replacement;
  find_replace_templatesets($mod_template, '#'.preg_quote($pattern).'#', $replacement."\n".$pattern);
}

function myplug_deactivate()
{
  global $mod_template, $pattern, $replacement;
  $auto_create = 0;
  find_replace_templatesets($mod_template, '#'.preg_quote($replacement).'#', '', $auto_create);
}


[EDIT]
So I've done more testing and I've changed my plugin to be as such:

$mod_template = 'member_login';
$search_string = '{$footer}';
$replace_string = 'hello';

function myplugin_activate()
{
  global $mod_template, $search_string, $replace_string;
  $data = "_activate()\n";
  $data .= '$mod_template = '.$mod_template."\n";
  $data .= '$search_string = '.$search_string."\n";
  $data .= '$replace_string = '.$replace_string."\n";
  $data .= 'line1: #'.preg_quote($search_string).'#i'."\n";
  $data .= 'line2: #'.preg_quote($replace_string).'#i';
  file_put_contents('/tmp/log1.txt', $data);
}

function myplugin_deactivate()
{
  global $mod_template, $search_string, $replace_string;
  $data = "_deactivate()\n";
  $data .= '$mod_template = '.$mod_template."\n";
  $data .= '$search_string = '.$search_string."\n";
  $data .= '$replace_string = '.$replace_string."\n";
  $data .= 'line1: #'.preg_quote($search_string).'#i'."\n";
  $data .= 'line2: #'.preg_quote($replace_string).'#i';
  file_put_contents('/tmp/log2.txt', $data);
}

And the results are as follows:
Log1.txt:
_activate()
$mod_template = member_login
$search_string = {$footer}
$replace_string = hello
line1: #\{\$footer\}#i
line2: #hello#i
Log2.txt:
_deactivate()
$mod_template = 
$search_string = 
$replace_string = 
line1: ##i
line2: ##i

This means that for some reason, the global keyword isn't working. PLEASE, IF ANYONE KNOWS ANYTHING, PLEASE HELP! I've spent literally almost all day trying to figure this out!

Any help would be GREATLY appreciated.

Thanks!

Well, not sure what's going on. I'm giving up on getting this to work. I can only assume that at some point MyBB unsets the global variables so that when _deactivate() is run, they are no longer available to the function.