Jump to content


Problem when executing multiple functions...


2 replies to this topic

#1 Digital Masters

    Young Padawan

  • Members
  • Pip
  • 6 posts

Posted 15 November 2006 - 05:05 AM

Hi,

I need some help here.

I am developing a class of data sanitizer. It has many methods, but only one will be used per users.

However this "super-method" don't pass the others "simple-methods" sequentially.

Example:

Method #1: NoSpaces (remove all spaces)
Method #2: HTMLEntities (Convert all special characters to real HTML)

Super-Method: Apply Method #1 in the "unique" parameter and redirect the actual value to apply the Method #2, then return.

Use: print Super_Method( "& #032;µ& #032;" ); //Example, but with no space between "&" and "#032"

My tentative of code is:

<?php

class Clean
{
	private		$warnings			=	array();
	private		$errors				=	array();
	
	private		$get_magic_quotes	=	FALSE;
	private		$entities			=	TRUE;
		
	public		$input;
	private		$data;
	
	function __construct()
	{
		// Error Alert
		
		error_reporting(E_ALL);
		
		// The current value of magic_quotes_gpc
		
		$this -> get_magic_quotes	= 	get_magic_quotes_gpc();
	}

	//-------------------------------------------------------------------------
	// Request data sanitizer
	//-------------------------------------------------------------------------
	
	// Removing fake spaces
	
	public function NoSpaces( $data )
	{	
		$data = str_replace( " ",	" ", $data );
		$data = str_replace( " ",	" ", $data );
		
		return $data;
	}
	
	// Only allow UNICODE Characters
	
	public function Unicode( $data )
	{
		return preg_replace( "/&amp;#([0-9]+);/s", "&#\\1; ", $data );
	}
	
	// Converting New Lines to Line Breaks
	
	public function nl2br( $data )
	{
		return nl2br( $data );
	}
	
	// Removing auto-quotes from variables
	
	public function MagicQuotes( $data )
	{
		if( ! $this -> get_magic_quotes )
		{
			$data = stripslashes( $data );
		}
		
		return $data;
	}
	
	// Converting specialchars and entities to real HTML
	
	public function HTMLEntities( $data )
	{
		if( $this -> entities )
		{
			$this -> data = htmlentities( $data );
		}
	}
	
	// Striping Tags
	
	public function StripTags( $data, $tags = array() )
	{
		$args = func_get_args();
		
		$data = array_shift( $args );
		
		$tags = func_num_args() > 2 ? array_diff( $args, array( $data ) ) : ( array ) $tags;
		
		foreach ( $tags as $tag )
		{
			if( preg_match_all( '/<' . $tag . '[^>]*>(.*)<\/' . $tag . '>/iU', $data, $found ) )
			{
				$data = str_replace( $found[ 0 ], $found[ 1 ], $data );
			}
		}
		
		return $data;
	}

	// Check URL existence
	
	public function URLChecker( $data )
	{
		if( ini_get( 'allow_url_fopen' ) )
		{
			return ( $open = fopen( $data, 'r' ) ) === FALSE ? FALSE : fclose( $open );
		}
		else
		{
			$warnings[]	=	'<b>allow_url_fopen</b> is deactivated. Please, check your <b>php.ini</b> and try again later.';
		}
	}
	
	// EGPCS Sanitizer
		
	public function parseData()
	{
		$arrMethods = array(
			"HTMLEntities"
		);
		
		foreach ($arrMethods as $sNameMethod) {
			call_user_func_array(array($this, $sNameMethod), $this->data);
		}
		
		return $this -> data;
	}
}

$Clean = new Clean;

print $Clean -> parseData( "&" );
But no works.

Help me, please and sorry if my English is horrible.

[]'s

#2 Demonslay

    P2L Jedi

  • Members
  • PipPipPip
  • 970 posts
  • Gender:Male
  • Location:A strange world where water falls out of the sky... for no reason.
  • Interests:Graphic Design, Coding, Splinter Cell, Cats

Posted 15 November 2006 - 03:19 PM

Even without fully reading your class I see the problem.
You aren't passing the data to the class at all.
Two solutions.

1. Pass the data directly to the parseDate() function, then deal with it there.
// EGPCS Sanitizer
		
	public function parseData($data)
	{
		$arrMethods = array(
			"HTMLEntities"
		);
		
		foreach ($arrMethods as $sNameMethod) {
			call_user_func_array(array($this, $sNameMethod), $data);
		}
		$this -> data = $data;
		return $this -> data;
	}
$Clean = new Clean;

print $Clean -> parseData( "&" );

Or, simply set the class's data variable before executing the function.
$Clean = new Clean;

$Clean -> data = "&";
print $Clean -> parseData();

Hope that helps you out a bit. ^_^

#3 rc69

    PHP Master PD

  • P2L Staff
  • PipPipPipPip
  • 3,827 posts
  • Gender:Male
  • Location:Here
  • Interests:Web Development

Posted 16 November 2006 - 03:04 AM

Well, i'm not sure what exactly Demonslay is talking about up there, but here's the problems i'm having.

1. You are replacing spaces with spaces... that sorta eliminates the point of having the function. Also, str_replace() need only be called once to replace all occurrences of a match to the find parameter.
	public function NoSpaces( $data )
	{
		return str_replace(' ', '', $data);
	}

2. I f&#^ing hate functions!! I especially hate when i don't know what they do and believe they are being used wrong.
	public function parseData()
	{
		$arrMethods = array(
			"HTMLEntities"
		);
		
		while (list($sNameMethod) = each($arrMethods)){ // Faster than foreach :P
			$this->{$sNameMethod}($this->data);
		}
		
		return $this -> data;
	}
Ref:
Variable variables
Variable functions
Note on variable functions:
$foo->$funcname();  // This calls $foo->Variable()
The above is php.net's method of calling a variable method of an object. I tried that once before, and it didn't work out how i wanted. When i added curly braces, everything fit prefectly, and it seemed to be more logical.
i.e.
$foo->{$funcname}();  // This calls $foo->Variable()

Edited by rc69, 16 November 2006 - 03:06 AM.






1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users