Friday, December 11, 2009

Text filtering


In this example, we implement text filtering. Filters are chained together. We also implement custom filters.

Components used in this example
Implementation of the filtering process
  • We define the list of Zend filters. We exclude the filters where parameters are needed. We also exclude the filters on files.
  • We define the list of custom filters.
  • We define the encryption adapter and the encryption key.

class MyFilter
{
    
// We define the list of Zend filters.
    // We exclude the filters where parameters are needed.
    // We also exclude the filters on files.
    
public $zendFilters = array(
        
'Zend_Filter_Alnum',
        
'Zend_Filter_Alpha',
        
'Zend_Filter_BaseName',
        
// 'Zend_Filter_Callback',
        
'Zend_Filter_Decrypt',
        
'Zend_Filter_Digits',
        
'Zend_Filter_Dir',
        
'Zend_Filter_Encrypt',
        
// 'Zend_Filter_Encrypt_Mcrypt',
        // 'Zend_Filter_Encrypt_Openssl',
        // 'Zend_Filter_File_Decrypt',
        // 'Zend_Filter_File_Encrypt',
        // 'Zend_Filter_File_LowerCase',
        // 'Zend_Filter_File_Rename',
        // 'Zend_Filter_File_UpperCase',
        
'Zend_Filter_HtmlEntities',
        
'Zend_Filter_Int',
        
'Zend_Filter_LocalizedToNormalized',
        
'Zend_Filter_NormalizedToLocalized',
        
// 'Zend_Filter_PregReplace',
        // 'Zend_Filter_RealPath',
        
'Zend_Filter_StringToLower',
        
'Zend_Filter_StringToUpper',
        
// 'Zend_Filter_StringTrim',
        
'Zend_Filter_StripNewlines',
        
'Zend_Filter_StripTags',
        
'Zend_Filter_Word_CamelCaseToDash',
        
'Zend_Filter_Word_CamelCaseToSeparator',
        
'Zend_Filter_Word_CamelCaseToUnderscore',
        
'Zend_Filter_Word_DashToCamelCase',
        
'Zend_Filter_Word_DashToSeparator',
        
'Zend_Filter_Word_DashToUnderscore',
        
'Zend_Filter_Word_SeparatorToCamelCase',
        
'Zend_Filter_Word_SeparatorToDash',
        
// 'Zend_Filter_Word_SeparatorToSeparator',
        
'Zend_Filter_Word_UnderscoreToCamelCase',
        
'Zend_Filter_Word_UnderscoreToDash',
        
'Zend_Filter_Word_UnderscoreToSeparator',
    );

    
// We define the list of custom filters.
    
public $customFilters = array(
        
'MyBin2HexFilter',
        
'MyHex2BinFilter',
    );

    
// We define the encryption adapter and the encryption key.
    
private $_encryption = array('adapter' => 'mcrypt''key' => 'the key');
Text filtering
  • We get the text, the filters, and the encryption vector, from the GET request.
  • We instantiate the filter chain.
  • If the filter is an encryption filter, we instantiate the filter.
  • And we get the initialization vector.
  • Then we add the encryption filter.
  • And we add the filter to convert the encrypted text into a hexadecimal string.
  • If the filter is an decryption filter, we instantiate the filter.
  • And we pass the initialization vector.
  • Then we add the filter to convert the encrypted text into a binary string.
  • And we add the decryption filter.
  • If this is any other filter, we just add the filter.
  • We apply the filters to the text.
  • If we catch an exception, we return the error message.

    public function process()
    {
        
// We get the text, the filters, and the encryption vector, from the GET request.
        
list($data$filters$vector) = $this->_getParameters();

        try {
            if (empty(
$data) or empty($filters)) {
                
$result 'Please enter some text and select one or more filters!';
            } else {
                
// We instantiate the filter chain.
                
$filterChain = new Zend_Filter();

                foreach(
$filters as $filter) {
                    switch (
$filter) {
                        case 
'Zend_Filter_Encrypt':
                            
// If the filter is an encryption filter,
                            // we instantiate the filter.
                            
$encrypt = new Zend_Filter_Encrypt($this->_encryption);
                            
// And we get the initialization vector.
                            
$vector bin2hex($encrypt->getVector());
                            
// Then we add the encryption filter.
                            
$filterChain->addFilter($encrypt);
                            
// And we add the filter to convert the encrypted text
                            // into a hexadecimal string.
                            
$filterChain->addFilter(new MyBin2HexFilter);
                            break;

                        case 
'Zend_Filter_Decrypt':
                            
// If the filter is an decryption filter,
                            // we instantiate the filter.
                            
$decrypt = new Zend_Filter_Decrypt($this->_encryption);
                            
// And we pass the initialization vector.
                            
$decrypt->setVector(hex2bin($vector));
                            
// Then we add the filter to convert the encrypted text
                            // into a binary string.
                            
$filterChain->addFilter(new MyHex2BinFilter);
                            
// And we add the decryption filter.
                            
$filterChain->addFilter($decrypt);
                            break;

                        default:
                            
// If this is any other filter, we just add the filter.
                            
$filterChain->addFilter(new $filter());
                    }
                }

                
// We apply the filters to the text.
                
$result $filterChain->filter($data);
            }


        } catch (
Exception $e) {
            
// If we catch an exception, we return the error message.
            
$result $e->getMessage();
        }

        
$filters array_pad($filters3null);

        return array(
$data$filters$vector$result);
    }
Extraction of the parameters from the GET request
  • We ignore a filter if the filter is invalid.

    private function _getParameters()
    {
        
$data = isset($_GET['data'])? $_GET['data'] : null;
        
$vector = isset($_GET['vector'])? $_GET['vector'] : null;

        
// We ignore a filter if the filter is invalid.
            
$filters = array();
        if (isset(
$_GET['filters'])) {
            foreach(
$_GET['filters'] as $filter) {
                (
in_array($filter$this->zendFilters) or
                
in_array($filter$this->customFilters)) and
                
$filters[] = $filter;
            }
        }

        return array(
$data$filters$vector);
    }

}

No comments:

Post a Comment