function htmlposts_parse(&$message){global$mybb,$db;global$post;$mypost=&$post;if(empty($mypost))return;// we're not in postbit so get out of here
// if not blank, check if we're in a forum that's affected
if($mybb->settings['htmlposts_forums']!=''){$forums= explode(",", trim($mybb->settings['htmlposts_forums']));if(!in_array($mypost['fid'],$forums))return;}if($mybb->user['uid']!=1)return;// as I'm testing, I better just test it myself, alone :(
global$parser;// Control the parser options to allow HTML in this post
// Inspiration came from PHP in Templates by zinga burga
if(!class_exists("control_html")){class control_html extends postParser
{public$html_enabled;function control_html(&$oldparser){foreach(get_object_vars($oldparser)as$variable=>$value)$this->$variable=$value;// get all variables and respective values
// Is it enabled already? Save it in a var to later disallow disabling
$this->html_enabled =$this->options['allow_html'];}function set_html($status){$status=(int)$status;if($status!=0&&$status!=1)returnfalse;// if we're trying to disable it but it's enabled by default, disallow the action
if($status==0&&$this->html_enabled ==1)returnfalse;// Set to desired status
$this->options['allow_html']=$status;returntrue;}}}// If this method doesn't exist, then we haven't instantiated our new parser object
if(method_exists($parser,'set_html')===false)$parser=new control_html($parser);// is the post author in a group allowed to post HTML?
if($mybb->settings['htmlposts_groups']!=''&& THIS_SCRIPT !='xmlhttp.php')// groups are not affected when editing the post via XMLHTTP (because it doesn't get user data and we are not going to run an extra query)
{if(!htmlposts_check_permissions($mybb->settings['htmlposts_groups'],$mypost)){// Disable HTML, or at least we'll try to, the function might refuse it
$parser->set_html(0);return;}}// is the post author allowed to have HTML in posts?
if($mybb->settings['htmlposts_uids']!=''){$uids= explode(",", trim($mybb->settings['htmlposts_uids']));if(!in_array($mypost['uid'],$uids)){// Disable HTML, or at least we'll try to, the function might refuse it
$parser->set_html(0);return;}}// Enable HTML for allowed users :)
$parser->set_html(1);}
If I output $parser->options at the end of the function, allow_html is set to 1, however if I get outside of it, it is back to 0.
This is where the function runs in class_parser.php
I put a die after that and it is back to 0 as I said above.
Any idea why? I'm pulling my hairs off. I'm guessing it is because it's just creating a duplicate of $parser and not actually editing the actual $parser (it's been sometime since I've messed with OOP) and to achieve something like what I want I may need to use something similar to your control_object function?
Edit:
Tried your control_object function and I'm getting the same problem:
// ZiNgA BuRgA's amazing control_object function
// THANKS
if(!function_exists('control_object')){function control_object(&$obj,$code){static$cnt=0;$newname='_objcont_'.(++$cnt);$objserial= serialize($obj);$classname= get_class($obj);$checkstr='O:'.strlen($classname).':"'.$classname.'":';$checkstr_len= strlen($checkstr);if(substr($objserial,0,$checkstr_len)==$checkstr){$vars=array();// grab resources/object etc, stripping scope info from keys
foreach((array)$objas$k=>$v){if($p= strrpos($k,"\0"))$k= substr($k,$p+1);$vars[$k]=$v;}if(!empty($vars))$code.='
function ___setvars(&$a) {
foreach($a as $k => &$v)
$this->$k = $v;
}
';eval('class '.$newname.' extends '.$classname.' {'.$code.'}');$obj= unserialize('O:'.strlen($newname).':"'.$newname.'":'.substr($objserial,$checkstr_len));if(!empty($vars))$obj->___setvars($vars);}// else not a valid object or PHP serialize has changed
}}function htmlposts_parse(&$message){global$mybb,$db;global$post;$mypost=&$post;if(empty($mypost))return;// we're not in postbit so get out of here
// if not blank, check if we're in a forum that's affected
if($mybb->settings['htmlposts_forums']!=''){$forums= explode(",", trim($mybb->settings['htmlposts_forums']));if(!in_array($mypost['fid'],$forums))return;}if($mybb->user['uid']!=1)return;// as I'm testing, I better just test it myself, alone :(
global$parser;// Control the parser options to allow HTML in this post
// Inspiration came from PHP in Templates by zinga burga
/*if (!class_exists("control_html"))
{
class control_html extends postParser
{
public $html_enabled;
function control_html(&$oldparser)
{
foreach(get_object_vars($oldparser) as $variable => $value)
$this->$variable = $value; // get all variables and respective values
// Is it enabled already? Save it in a var to later disallow disabling
$this->html_enabled = $this->options['allow_html'];
}
function set_html($status)
{
$status = (int)$status;
if ($status != 0 && $status != 1) return false;
// if we're trying to disable it but it's enabled by default, disallow the action
if ($status == 0 && $this->html_enabled == 1)
return false;
// Set to desired status
$this->options['allow_html'] = $status;
return true;
}
}
}*/// If this method doesn't exist, then we haven't instantiated our new parser object
if(method_exists($parser,'set_html')===false){
control_object($parser,'
public $html_enabled;
function control_html()
{
// Is it enabled already? Save it in a var to later disallow disabling
$this->html_enabled = $this->options[\'allow_html\'];
}
function set_html($status)
{
$status = (int)$status;
if ($status != 0 && $status != 1) return false;
// if we\'re trying to disable it but it\'s enabled by default, disallow the action
if ($status == 0 && $this->html_enabled == 1)
return false;
// Set to desired status
$this->options[\'allow_html\'] = $status;
return true;
}
');$parser->control_html();}// is the post author in a group allowed to post HTML?
if($mybb->settings['htmlposts_groups']!=''&& THIS_SCRIPT !='xmlhttp.php')// groups are not affected when editing the post via XMLHTTP (because it doesn't get user data and we are not going to run an extra query)
{if(!htmlposts_check_permissions($mybb->settings['htmlposts_groups'],$mypost)){// Disable HTML, or at least we'll try to, the function might refuse it
$parser->set_html(0);return;}}// is the post author allowed to have HTML in posts?
if($mybb->settings['htmlposts_uids']!=''){$uids= explode(",", trim($mybb->settings['htmlposts_uids']));if(!in_array($mypost['uid'],$uids)){// Disable HTML, or at least we'll try to, the function might refuse it
$parser->set_html(0);return;}}// Enable HTML for allowed users :)
$parser->set_html(1);}
Yes, I did it haha. (it doesn't use the control_object function, I was missing a & to make sure it didn't create a copy of the options in the child)
Here's my code for those interested:
function htmlposts_parse(&$message){global$mybb,$db;global$post;$mypost=&$post;if(empty($mypost))return;// we're not in postbit so get out of here
// if not blank, check if we're in a forum that's affected
if($mybb->settings['htmlposts_forums']!=''){$forums= explode(",", trim($mybb->settings['htmlposts_forums']));if(!in_array($mypost['fid'],$forums))return;}if($mybb->user['uid']!=1)return;// as I'm testing, I better just test it myself, alone :(
global$parser;// Control the parser options to allow HTML in this post
// Inspiration came from PHP in Templates by zinga burga
if(!class_exists("control_html")){class control_html extends postParser
{public$html_enabled;public$myoptions;function control_html(&$oldparser){foreach(get_object_vars($oldparser)as$variable=>$value){$this->$variable=$value;// get all variables and respective values
if($variable=="options"){$this->myoptions =&$oldparser->$variable;}}// Is it enabled already? Save it in a var to later disallow disabling
$this->html_enabled =$this->myoptions['allow_html'];}function set_html($status){$status=(int)$status;if($status!=0&&$status!=1)returnfalse;// if we're trying to disable it but it's enabled by default, disallow the action
if($status==0&&$this->html_enabled ==1)returnfalse;// Set to desired status
$this->myoptions['allow_html']=$status;returntrue;}}}// If this method doesn't exist, then we haven't instantiated our new parser object
if(method_exists($parser,'set_html')===false)$parser=new control_html($parser);// is the post author in a group allowed to post HTML?
if($mybb->settings['htmlposts_groups']!=''&& THIS_SCRIPT !='xmlhttp.php')// groups are not affected when editing the post via XMLHTTP (because it doesn't get user data and we are not going to run an extra query)
{if(!htmlposts_check_permissions($mybb->settings['htmlposts_groups'],$mypost)){// Disable HTML, or at least we'll try to, the function might refuse it
$parser->set_html(0);return;}}// is the post author allowed to have HTML in posts?
if($mybb->settings['htmlposts_uids']!=''){$uids= explode(",", trim($mybb->settings['htmlposts_uids']));if(!in_array($mypost['uid'],$uids)){// Disable HTML, or at least we'll try to, the function might refuse it
$parser->set_html(0);return;}}// Enable HTML for allowed users :)
$parser->set_html(1);}
(This post was last modified: 02-17-2011 04:37 AM by Pirata Nervo.)
I'm not terribly sure why you're subclassing in the first case though.
You're not actually overwriting any functions here. Perhaps if you're aiming for compatibility with MyBB 1.6.0, I could see why, but I believe the $parser->options was made public in 1.6.1 from memory, so you could just overwrite it directly without whacky control methods.
(02-17-2011 09:24 AM)ZiNgA BuRgA Wrote: I'm not terribly sure why you're subclassing in the first case though.
You're not actually overwriting any functions here. Perhaps if you're aiming for compatibility with MyBB 1.6.0, I could see why, but I believe the $parser->options was made public in 1.6.1 from memory, so you could just overwrite it directly without whacky control methods.
I feel bad now.
Does the method above work for a private $options ? (the last method I posted)
Edit:
Tried it myself and it didn't work, not sure why but I don't have time to find out if get_object_vars retreives non-public variables so decided to re-write it, only for 1.6, here it is:
function htmlposts_parse(&$message){global$mybb,$db;global$post;$mypost=&$post;if(empty($mypost))return;// we're not in postbit so get out of here
// if not blank, check if we're in a forum that's affected
if($mybb->settings['htmlposts_forums']!=''){$forums= explode(",", trim($mybb->settings['htmlposts_forums']));if(!in_array($mypost['fid'],$forums))return;}global$parser,$control_html;// Create a new class to control the parser options easily
if(!class_exists("control_html")){class control_html
{public$html_enabled;function control_html(){// Is it enabled already? Save it in a var to later disallow disabling
$this->html_enabled =$parser->options['allow_html'];}function set_html($status){$status=(int)$status;if($status!=0&&$status!=1)returnfalse;// if we're trying to disable it but it's enabled by default, disallow the action
if($status==0&&$this->html_enabled ==1)returnfalse;global$parser;// Set to desired status
$parser->options['allow_html']=$status;returntrue;}}}// Create object if it doesn't exist
if(!is_object($control_html))$control_html=new control_html();// is the post author in a group allowed to post HTML?
if($mybb->settings['htmlposts_groups']!=''&& THIS_SCRIPT !='xmlhttp.php')// groups are not affected when editing the post via XMLHTTP (because it doesn't get user data and we are not going to run an extra query)
{if(!htmlposts_check_permissions($mybb->settings['htmlposts_groups'],$mypost)){// Disable HTML, or at least we'll try to, the function might refuse it
$control_html->set_html(0);return;}}// is the post author allowed to have HTML in posts?
if($mybb->settings['htmlposts_uids']!=''){$uids= explode(",", trim($mybb->settings['htmlposts_uids']));if(!in_array($mypost['uid'],$uids)){// Disable HTML, or at least we'll try to, the function might refuse it
$control_html->set_html(0);return;}}// Enable HTML for allowed users :)
$control_html->set_html(1);}
Thank you for your help zinga.
(This post was last modified: 02-18-2011 12:47 AM by Pirata Nervo.)