
##############################################################
## MOD Title: Local URL Same Window
## MOD Version: 0.5.0
## Author: drathbun < drathbun@aol.com > (Dave Rathbun)
## Description: Internal links (on your server) reuse current browser session
##
## Installation Level: easy
## Installation Time: 2-3 Minutes
## Files To Edit: includes/bbcode.php
## Included Files: n/a
##############################################################
## This MOD is released under the GPL License.
## Intellectual Property is retained by the MOD Author(s) listed above
##############################################################
## For Security Purposes, Please Check: http://www.phpbb.com/mods/downloads/ for the
## latest version of this MOD. Downloading this MOD from other sites could cause malicious code
## to enter into your phpBB Forum. As such, phpBB will not offer support for MOD's not offered
## in our MOD-Database, located at: http://www.phpbb.com/mods/downloads/
##############################################################
## Authors Notes:
##          In my production version of the mod, the only part of the
##      URL that is tested is $board_config['server_name']. If you are
##      running multiple phpBB boards on the same server, you may want
##      to add $board_config['script_path'] to the path being checked.
##      Notes to that effect are in the code posted below. I have tested
##      both versions, but I want anything at www.myserver.com to stay
##      in the same window.
##      
##      Simple Version
##      If you just want to make every link open in the same window,
##      open bbcode.tpl and remove the target="_blank" text. Do the
##      same in includes/bbcode.php wherever it is found, and you're done. :-)
##      
##      This Version
##      This mod is more selective; external links posted by users will continue
##      to spawn a new window.
##
##      As near as I can tell, the function undo_make_clickable() is never called,
##      so I didn't worry about updating that code.
##
##      This mod should be very easy to test and implement. Because a new function
##      call was added, you can easily turn this feature on or off. There are only
##      two lines of actual "modded" code added in the existing code, and each is
##      a call to the new function. If you want to un-mod on a temporary basis,
##      simply remove those two function calls and you are done. To remove the
##      mod completely, remove the fix_local_urls() function code as well.
##############################################################
## Before Adding This MOD To Your Forum, You Should Back Up All Files Related To This MOD
##############################################################

#
#------[ OPEN ] ------
#

includes/bbcode.php

#
#----[ FIND ] ----
#

   return $bbcode_tpl;
}

#
#----[ AFTER, ADD ] ------
#

/**
 * This function is used to remove the target="_blank" from any url that is posted
 * that matches the server name from the $board_config array. That way of people want
 * to link internally from one post to another, they won't open new windows all the
 * time. Only external links will open a new target window.
 * Mod Author: Dave Rathbun (drathbun@aol.com)
 */

function fix_local_urls($text)
{
   global $board_config;

   // match for url local links. If you are running multiple phpBB forums
   // on the same server, you might want to use the following line instead:
   // $local_url = '<a href="http://' . strtolower($board_config['server_name']) . strtolower($board_config['script_path']);

   // note that strtolower is ONLY used for comparison, the actual text of the URL is not changed
   $local_url = '<a href="http://' . strtolower($board_config['server_name']);

   // Find first local link in the post text, if any. If none, remainder of the
   // code is skipped.
   $start_url = strpos(strtolower($text), $local_url);

   // If any are found, process, otherwise drop out of function. Note that since
   // the first step done before processing the post text is to add one extra space,
   // we don't need to worry about the case where a url might be in the first position.
   // It won't. :-)

   while ($start_url)
   {
      // first, identify the end of the URL by locating the closing >
      $last_found_pos = $start_url + 1;
      $end_url = strpos ($text, '>', $last_found_pos);
      $url_len = $end_url - $start_url + 1;

      // get copy of URL from <a href=... to closing >
      $my_url = substr($text, $start_url, $url_len);

      // replace target with null string
      $my_local_url = str_replace('target="_blank"', '', $my_url);
      
      // replace old URL with new URL in post text. Note that while the
      // comparison was done with strtolower() to be sure to find all matches,
      // the actual URL text is not changed in any way. Only the target.
      $text = str_replace($my_url, $my_local_url, $text);

      // advance by length of URL - length of "target" string, check for next local URL
      $start_url = strpos(strtolower($text), $local_url, $last_found_pos + $url_len - 16);
   }

   return $text;
}


#
#------[ FIND ]-----
#

   $text = preg_replace($patterns, $replacements, $text);

#
#----[ AFTER, ADD ] ------
#

   $text = fix_local_urls($text);

#
#------[ FIND ]-----
#

   $ret = preg_replace("#([\n ])([a-z0-9\-_.]+?)@([\w\-]+\.([\w\-\.]+\.)?[\w]+)#i", "\\1<a href=\"mailto:\\2@\\3\">\\2@\\3</a>", $ret);

#
#----[ AFTER, ADD ] ------
#

   $ret = fix_local_urls($ret);


#
#-----[ SAVE/CLOSE ALL FILES ]------------------------------------------
#
# EoM