<?php
/**
*
* @package phpbbmodders_site
* @version $Id: hash.php 154 2010-10-27 05:36:23Z tumba25 $
* @copyright (c) 2007, 2008 phpbbmodders
* @license http://opensource.org/licenses/gpl-license.php GNU Public License
*
*/

/**
*/
if (!defined('IN_PHPBB'))
{
    exit;
}

/**
 * Hashing page, tools submodule
 */
class site_tools_hash extends site_module_base
{
    public function 
module()
    {
        global 
$root_path$phpbb_root_path$phpEx;
        global 
$template$user;
        global 
$cute_url;

        if (
request_var('highlight'false))
        {
            
highlight_file(__FILE__);
            
exit_handler();
        }

        
$user->add_lang('website/tools_hash');

        
$template->assign_block_vars('navlinks', array(
            
'FORUM_NAME' => $user->lang['HASH'],
            
'U_VIEW_FORUM' => $cute_url->build(array('tools''hash')),
        ));

        
$template->assign_var('S_BODY_CLASS''tools');

        
$message        request_var('message'''true);
        
$method            = ($method $cute_url->get('')) ? $method request_var('method''md5');
        
$more_methods    request_var('more_methods'false);

        
$hash_methods = array();
        
$hash_methods[] = 'phpbb_hash';
        
$hash_methods[] = 'md5';
        
$hash_methods[] = 'sha1';

        if (
function_exists('hash') && $more_methods)
        {
            
$hash_methods array_unique(array_merge($hash_methodshash_algos()));
        }

        
sort($hash_methods);

        
// Assign home specific vars
        
$template->assign_vars(array(
            
'MESSAGE'                    => $message,
            
'MESSAGE_HASH'                => htmlspecialchars((string) $this->create_hash(htmlspecialchars_decode($message), $method)),

            
'L_PAGE_HASH_VIEWSOURCE'    => sprintf($user->lang['PAGE_HASH_VIEWSOURCE'], '<a href="' $cute_url->build(array('tools''hash'), array('highlight' => 1)) . '">''</a>'),

            
'S_SUBMIT'                    => isset($_REQUEST['message']) ? true false,
            
'S_FORM_ACTION'                => $cute_url->build(array('tools''hash'), $more_methods ? array('more_methods' => 1) : array()),
            
'S_FORM_SELECT'                => form_select($hash_methods$method'HASH_METHOD_'true),
        ));

        
// Output page
        
page_header($user->lang['PAGE_HASH']);

        
$template->set_filenames(array(
            
'body' => 'website/tools/hash_body.html')
        );

        
page_footer();
    }

    
/**
     * Create a hash from a string
     *
     * @param string $message
     * @param string $method
     * @return string Hash
     */
    
protected function create_hash($message$method)
    {
        
$method strtolower($method);

        switch (
$method)
        {
            case 
'md5':
            case 
'sha1':
            case 
'phpbb_hash':
                
// these all have the same method name like function name
                // don't expect all to be so easy, more hashing may be added later
                
return $method($message);
                break;

            default:
                if (
function_exists('hash'))
                {
                    if (
in_array($methodhash_algos(), true))
                    {
                        return 
hash($method$message);
                    }
                }
                return 
false;
                break;
        }
    }
}

?>