##############################################################
## MOD Title: Plugin Menu Filter Mod
## MOD Author: AndyDragon < andrew.forget@andydragon.com >
## MOD Description: This mod adds two new filters for the 
##		    template menus ('sys_menu_buttons' and 
##		    'sub_menu_buttons').  This allows (outside 
##		    of the core code) filtering of the menus.  
##		    The sample included plugin shows how to 
##		    set up a board for a guest-only, view-
##		    only type of gallery. The admin can still 
##		    manually navigate to the login.php page, 
##		    but normally it is not shown in the menu.
## 
## MOD Version: 1.0.0
## 
## Installation Level: Easy
## Installation Time: 9 minutes
## Files To Edit: include/functions.inc.php
##                include/themes.inc.php
## Included Files: 
## License: http://opensource.org/licenses/gpl-license.php GNU General Public License v2
## Generator: MOD Studio [ ModTemplateTools 1.0.2232.38226 ]
##############################################################
## For security purposes, please check: http://www.phpbb.com/mods/
## for the latest version of this MOD. Although MODs are checked
## before being allowed in the MODs Database there is no guarantee
## that there are no security problems within the MOD. No support
## will be given for MODs not found within the MODs Database which
## can be found at http://www.phpbb.com/mods/
##############################################################
## Author Notes: 
##############################################################
## MOD History:
## 
## 2006-03-07 - Version 1.0.0
## -Initial code for mod.
## 
##############################################################
## Before Adding This MOD To Your Forum, You Should Back Up All Files Related To This MOD 
##############################################################

#
#-----[ OPEN ]------------------------------------------
#
include/functions.inc.php
#
#-----[ FIND ]------------------------------------------
#
/**
 * template_extract_block()
 *
 * @param $template
 * @param $block_name
 * @param string $subst
 * @return
 **/

function template_extract_block(&$template, $block_name, $subst='')
{
        $pattern = "#(<!-- BEGIN $block_name -->)(.*?)(<!-- END $block_name -->)#s";
        if ( !preg_match($pattern, $template, $matches)){
                die('<b>Template error<b><br />Failed to find block \''.$block_name.'\'('.htmlspecialchars($pattern).') in :<br /><pre>'.htmlspecialchars($template).'</pre>');
        }
        $template = str_replace($matches[1].$matches[2].$matches[3], $subst, $template);
        return $matches[2];
}

#
#-----[ AFTER, ADD ]------------------------------------------
#
/**
 * template_extract_block_quiet()
 *
 * @param $template
 * @param $block_name
 * @param string $subst
 * @return
 **/

function template_extract_block_quiet(&$template, $block_name, $subst='')
{
        $pattern = "#(<!-- BEGIN $block_name -->)(.*?)(<!-- END $block_name -->)#s";
        if ( !preg_match($pattern, $template, $matches)){
                return null;
        }
        $template = str_replace($matches[1].$matches[2].$matches[3], $subst, $template);
        return $matches[2];
}

/**
 * template_clean_menu_separators()
 *
 * @param $menu
 * @param $menu_spacer
 * @return
 **/

function template_clean_menu_separators($menu, $menu_spacer)
{
        $menu_spacer_with_space = ' '.$menu_spacer;
        $end_of_url = '</a>';
        $end_of_url_with_spacer = '</a>'.$menu_spacer_with_space;
        $end_of_last_url = real_strrpos($menu, $end_of_url);
        $end_of_last_url_with_spacer = real_strrpos($menu, $end_of_url_with_spacer);
        if ( $end_of_last_url !== false && $end_of_last_url == $end_of_last_url_with_spacer) {
                $menu = substr_replace($menu, "</a>", $end_of_last_url, strlen("</a> ".$menu_spacer));
        }
        return $menu;
}

/**
 * real_strrpos()
 *
 * @param $haystack
 * @param $needle
 * @param $offset
 * @return
 **/

function real_strrpos($haystack, $needle, $offset=0) {
        // same as strrpos, except $needle can be a string
        $strrpos = false;
        if ( is_string($haystack) && is_string($needle) && is_numeric($offset)) {
                $strlen = strlen($haystack);
                $strpos = strpos(strrev(substr($haystack, $offset)), strrev($needle));
                if (is_numeric($strpos)) {
                        $strrpos = $strlen - $strpos - strlen($needle);
                }
        }
        return $strrpos;
}

#
#-----[ OPEN ]------------------------------------------
#
include/themes.inc.php
#
#-----[ FIND ]------------------------------------------
#
    // Login and Logout don't have a spacer as only one is shown, and either would be the last option.
  } //{THEMES}

#
#-----[ AFTER, ADD ]------------------------------------------
#
  $sys_menu_buttons = CPGPluginAPI::filter('sys_menu_buttons', $sys_menu_buttons);

#
#-----[ FIND ]------------------------------------------
#
    addbutton($sub_menu_buttons,'{SEARCH_LNK}','{SEARCH_TITLE}','{SEARCH_TGT}','search','');
    } //{THEMES}

#
#-----[ AFTER, ADD ]------------------------------------------
#
  $sub_menu_buttons = CPGPluginAPI::filter('sub_menu_buttons', $sub_menu_buttons);

#
#-----[ FIND ]------------------------------------------
#
    $cat_l2 = isset($cat) ? "&amp;cat=$cat" : '';
    $my_gallery_id = FIRST_USER_CAT + USER_ID;

#
#-----[ AFTER, ADD ]------------------------------------------
#
	$spacer = '';

#
#-----[ FIND ]------------------------------------------
#
  if ($which == 'sys_menu' ) {
#
#-----[ AFTER, ADD ]------------------------------------------
#
	$spacer = $template_sys_menu_spacer;
#
#-----[ FIND ]------------------------------------------
#
        template_extract_block($template_sys_menu, 'login');
#
#-----[ REPLACE WITH ]------------------------------------------
#
        template_extract_block_quiet($template_sys_menu, 'login');
#
#-----[ FIND ]------------------------------------------
#
        template_extract_block($template_sys_menu, 'logout');
        template_extract_block($template_sys_menu, 'my_profile');
#
#-----[ REPLACE WITH ]------------------------------------------
#
        template_extract_block_quiet($template_sys_menu, 'logout');
        template_extract_block_quiet($template_sys_menu, 'my_profile');
#
#-----[ FIND ]------------------------------------------
#
        template_extract_block($template_sys_menu, 'enter_admin_mode');
        template_extract_block($template_sys_menu, 'leave_admin_mode');
#
#-----[ REPLACE WITH ]------------------------------------------
#
        template_extract_block_quiet($template_sys_menu, 'enter_admin_mode');
        template_extract_block_quiet($template_sys_menu, 'leave_admin_mode');
#
#-----[ FIND ]------------------------------------------
#
        if (GALLERY_ADMIN_MODE) {
            template_extract_block($template_sys_menu, 'enter_admin_mode');
        } else {
            template_extract_block($template_sys_menu, 'leave_admin_mode');
        }
#
#-----[ REPLACE WITH ]------------------------------------------
#
        if (GALLERY_ADMIN_MODE) {
            template_extract_block_quiet($template_sys_menu, 'enter_admin_mode');
        } else {
            template_extract_block_quiet($template_sys_menu, 'leave_admin_mode');
        }
#
#-----[ FIND ]------------------------------------------
#
    if (!USER_CAN_CREATE_ALBUMS) {
        template_extract_block($template_sys_menu, 'my_gallery');
    }

    if (USER_CAN_CREATE_ALBUMS) {
        template_extract_block($template_sys_menu, 'my_profile');
    }

    if (!USER_CAN_UPLOAD_PICTURES) {
        template_extract_block($template_sys_menu, 'upload_pic');
    }

    if (USER_ID || !$CONFIG['allow_user_registration']) {
        template_extract_block($template_sys_menu, 'register');
    }

    if (!USER_ID || !$CONFIG['allow_memberlist']) {
        template_extract_block($template_sys_menu, 'allow_memberlist');
    }

    if (!$CONFIG['display_faq']) {
        template_extract_block($template_sys_menu, 'faq');
    }
#
#-----[ REPLACE WITH ]------------------------------------------
#
    if (!USER_CAN_CREATE_ALBUMS) {
        template_extract_block_quiet($template_sys_menu, 'my_gallery');
    }

    if (USER_CAN_CREATE_ALBUMS) {
        template_extract_block_quiet($template_sys_menu, 'my_profile');
    }

    if (!USER_CAN_UPLOAD_PICTURES) {
        template_extract_block_quiet($template_sys_menu, 'upload_pic');
    }

    if (USER_ID || !$CONFIG['allow_user_registration']) {
        template_extract_block_quiet($template_sys_menu, 'register');
    }

    if (!USER_ID || !$CONFIG['allow_memberlist']) {
        template_extract_block_quiet($template_sys_menu, 'allow_memberlist');
    }

    if (!$CONFIG['display_faq']) {
        template_extract_block_quiet($template_sys_menu, 'faq');
    }
#
#-----[ FIND ]------------------------------------------
#
        $sys_menu = template_eval($template_sys_menu, $param);
  } else {
#
#-----[ AFTER, ADD ]------------------------------------------
#
	$spacer = $template_sub_menu_spacer;
#
#-----[ FIND ]------------------------------------------
#
    if (!$CONFIG['custom_lnk_url']) {
        template_extract_block($template_sub_menu, 'custom_link');
    }
#
#-----[ REPLACE WITH ]------------------------------------------
#
    if (!$CONFIG['custom_lnk_url']) {
        template_extract_block_quiet($template_sub_menu, 'custom_link');
    }
#
#-----[ FIND ]------------------------------------------
#
  }
    return $$which;
}
}  //{THEMES}
#
#-----[ REPLACE WITH ]------------------------------------------
#
  }
    return template_clean_menu_separators($$which, $spacer);
}
}  //{THEMES}
#
#-----[ SAVE/CLOSE ALL FILES ]------------------------------------------
#
# EoM
