Control Object Help
Pirata Nervo Offline
Member
***
Posts: 235
Joined: Jan 2008
Post: #1
Control Object Help
Here's my function:

PHP Code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
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)
		$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

PHP Code:
$message = $plugins->run_hooks("parse_message_start", $message);


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:

PHP Code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
// 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)$obj as $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:

PHP Code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
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) 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->myoptions['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)
		$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.)
02-17-2011 04:21 AM
Find all posts by this user Quote this message in a reply
ZiNgA BuRgA Offline
Fag
*******
Posts: 3,357
Joined: Jan 2008
Post: #2
RE: Control Object Help
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.

My Blog
02-17-2011 09:24 AM
Find all posts by this user Quote this message in a reply
Pirata Nervo Offline
Member
***
Posts: 235
Joined: Jan 2008
Post: #3
RE: Control Object Help
(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:

PHP Code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
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) 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;
					
				global $parser;
					
				// Set to desired status
				$parser->options['allow_html'] = $status;
				
				return true;
			}
		}
	}
	
	// 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.)
02-18-2011 12:12 AM
Find all posts by this user Quote this message in a reply
ZiNgA BuRgA Offline
Fag
*******
Posts: 3,357
Joined: Jan 2008
Post: #4
RE: Control Object Help
Only 1.6.0 MyBB devs went stupid and decided that it's cooler to make things private.  And you can always tell 1.6.0 folk to upgrade.

My Blog
02-18-2011 08:08 AM
Find all posts by this user Quote this message in a reply

« Next Oldest | Next Newest »

 Standard Tools
Forum Jump: