/**
 * A hook that is used to change the behavior of phpBB just before the templates
 * are displayed. - This code block derived from ReIMG (c) 2011 DavidIQ.com
 * @param	phpbb_hook	$hook	the phpBB hook object
 * @return	void
 */
function url_to_bbvideo_hook(&$hook)
{
    global $template, $phpEx, $phpbb_root_path, $user;
    $page_name = substr($user->page['page_name'], 0, strpos($user->page['page_name'], '.'));
    if (!defined('URL_TO_BBVIDEO') && in_array($page_name, array('memberlist', 'posting', 'ucp', 'mcp', 'viewtopic'))) {
        define('URL_TO_BBVIDEO', true);
    }
    //This will prevent further loading of this hook. If you need this hook loaded on a page other than
    //the ones in the above array then add a define('URL_TO_BBVIDEO', true) to the top of your page.
    if (!defined('URL_TO_BBVIDEO') || URL_TO_BBVIDEO == false) {
        return;
    }
    if (!function_exists('url_to_bbvideo')) {
        if (!class_exists('bbcode')) {
            include $phpbb_root_path . 'includes/bbcode.' . $phpEx;
        } else {
            include $phpbb_root_path . 'includes/abbcode.' . $phpEx;
        }
    }
    $user->add_lang('mods/abbcode');
    //Message preview
    process_template_block_bbvideo('', 'PREVIEW_MESSAGE');
    //Post preview in MCP
    process_template_block_bbvideo('', 'POST_PREVIEW');
    //The actual message
    process_template_block_bbvideo('', 'MESSAGE');
    //Topic review area shown when posting a reply
    process_template_block_bbvideo('topic_review_row', 'MESSAGE');
    //Message history section
    process_template_block_bbvideo('history_row', 'MESSAGE');
    //postrow needs some special handling
    if (!empty($template->_tpldata['postrow'])) {
        foreach ($template->_tpldata['postrow'] as $row => $data) {
            if (isset($data['MESSAGE'])) {
                // Alter the array
                $template->alter_block_array('postrow', array('MESSAGE' => url_to_bbvideo($data['MESSAGE'])), $row, 'change');
            }
        }
    }
}
/**
* Process template blocks - This code block from ReIMG (c) 2011 DavidIQ.com
* Not used in default installation. Part of add-on “Auto Embed Video From URLs”
* Called only from hook_bbvideo.php
*
* @param  string	$block_name
* @param  string	$block_section
* @version 3.0.12
*/
function process_template_block_bbvideo($block_name, $block_section)
{
    global $template;
    if (!empty($block_name) && !empty($block_section)) {
        if (!empty($template->_tpldata[$block_name])) {
            foreach ($template->_tpldata[$block_name] as $row => $data) {
                // Alter the array
                $template->alter_block_array($block_name, array($block_section => url_to_bbvideo($data[$block_section])), $row, 'change');
            }
        }
    }
    if (!empty($block_section)) {
        if (isset($template->_tpldata['.'][0][$block_section])) {
            $template->assign_var($block_section, url_to_bbvideo($template->_tpldata['.'][0][$block_section]));
        }
    }
}