Page 1 of 1

[DEV] Slashdot style links in posts

Posted: Wed Mar 09, 2005 9:11 pm
by dmolavi
I've modded my boards so that links in posts are in the Slashdot style, with the root domain in square braces after the link text. This is particularly helpful if a post contains:

Code: Select all

[url=http://www.foo.com]This is a really cool site[/url]
Which, when read looks like: This is a really cool site, giving the reader no advanced knowledge of the link they might click. My modification makes links show up as follows:

This is a really cool site [foo.com]

If you want to see it in action, please read this post. If there is enough demand, I will make the code available.

Posted: Thu Mar 10, 2005 1:37 am
by Cross_+_Flame
could be interesting. Throw up the code and I'd be interested in at least tinkering with it (as those link images could get really annoying after a while).

Posted: Thu Mar 10, 2005 12:40 pm
by dmolavi
I just put the image in there so that my users have a cue that it's a link, since my CSS doesn't have it underlined/etc to differentiate it from normal text.

Anyhow, here's the code:

Code: Select all

##############################################################
## MOD Title: Slashdot Style Links
## MOD Author: dmolavi <dari AT nukedgallery.net> (Dariush Molavi) http://www.nukedgallery.net
## MOD Description: Add root domain of link after link text
## MOD Version: 1.2.0
##
## Installation Level: Easy
## Installation Time: 2 Minutes
## Files To Edit: includes/bbcode.php
## Included Files: n/a
##############################################################
## For Security Purposes, Please Check: http://www.phpbb.com/mods/ 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/
##############################################################
## Author Notes:
## This MOD adds the root domain of a URL after the link text, much like
## in the Slashdot commenting system.
## The regexps for this code have been adapted for PHP from Perl, and
## were originally authored by the Slashcode developers (http://slashcode.com/)
##############################################################
## MOD History:
##
##   2005-03-10 - Version 1.0.0
##      - Initial development release
##   2005-03-14 - Version 1.2.0
##      - New: Won't add [domain.tld] after links that are directly typed
##
##############################################################
## Before Adding This MOD To Your Forum, You Should Back Up All Files Related To This MOD
##############################################################

#
#-----[ OPEN ]------------------------------------------
#
includes/bbcode.php

#
#-----[ FIND ]------------------------------------------
#
	// Remove our padding..
	$ret = substr($ret, 1);

	return($ret);
}

#
#-----[ REPLACE WITH ]------------------------------------------
#
	// Remove our padding..
	$ret = substr($ret, 1);

	return makeslashdot($ret);
	
}

function makeslashdot($text) {
		$pattern = '#(<a\s+href="([^">]*)"(.*?)>)(.*?)</a[^>]*>#i';

		// grab the <a>...</a> text (href and link text)
		preg_match_all("'<a[^>]*>.*?</a>'si", $text, $matches);
		$links = $matches[0];

		foreach($links as $link) {

			$new_string = preg_replace($pattern, "$2", $link);	// Grab the raw URL
			$link_text = preg_replace($pattern, "$4",$link);
			if (preg_match('#class="postlink"#',$link)) {
				$link_array = parse_url(strtolower($new_string));	// Parse the URL

				if($link_array['host']){		// If the 'host' field is available...
					if(preg_match('/^([\d.]+)\.in-addr\.arpa$/',$link_array['host'],$matches)) {
						$host = implode(".",array_reverse(explode(".",$link_array['host'])));
					}
					# leave a numeric IP address alone
					else if(preg_match('/^(\d{1,3}\.){3}\d{1,3}$/',$link_array['host'],$matches)){
						$host = $matches[0];
					}
					# a.b.c.d.com -> d.com
					else if(preg_match('/[\w-]+\.[a-z]{3,4}$/',$link_array['host'], $matches)){
						$host = $matches[0];
					}
					# a.b.c.d.co.uk -> d.co.uk
					else if (preg_match('/[\w-]+\.[a-z]{2,4}\.[a-z]{2}$/', $link_array['host'], $matches)) {
						$host = $matches[0];
					}
					# a.b.c.realdomain.gr -> realdomain.gr
					else if(preg_match('/[\w-]+\.[a-z]{2}$/',$link_array['host'], $matches)) {
						$host = $matches[0];
					}
					# any other a.b.c.d.e -> c.d.e
					else {
						$host_array = explode('.', $link_array['host']);
						$num_levels = count($host_array);
						if ($num_levels >= 3) {
							$temp = array_slice($host_array,-3);
							$host = implode(".", $temp);
						}
					}
					$text = str_replace("$link", "$link [$host]", $text);
				}
			}
		}
		return $text;
}


#
#-----[ SAVE/CLOSE ALL FILES ]------------------------------------------
#
# EoM 
Edit: Corrected "FIND" line.
Edit: Corrected topic title and version number per http://www.phpbb.com/phpBB/viewtopic.ph ... 78#1474178
Edit: Updated features per http://www.phpbb.com/phpBB/viewtopic.ph ... 48#1473948

Posted: Fri Mar 11, 2005 12:45 am
by Rapid Dr3am
I don't see the find line in bbcode.php in a vanilla install.

Posted: Fri Mar 11, 2005 1:28 am
by ptlis
Rapid Dr3am wrote: I don't see the find line in bbcode.php in a vanilla install.

Neither do I, nor anything like it.

Posted: Fri Mar 11, 2005 1:33 am
by dmolavi
Thanks....fixed.

Posted: Fri Mar 11, 2005 12:12 pm
by ptlis
The MOD works great, Thanks!

Posted: Sat Mar 12, 2005 10:45 pm
by Rapid Dr3am
I;ve been thinking about this for a while.

I don't think you should display [domain.tld] after links that are not replaced with text.

So http://www.site.com doesn't need [site.com] after it.

Also internal links, don't need it.

Just for text links at other hosts, would be best imho.

Posted: Sun Mar 13, 2005 12:51 am
by smithy_dll
http://www.phpbb.com/phpBB/viewtopic.php?t=266307

Please read section 2 and apply an appropriate version no. to your MOD.

Please also read section 3.b and fix your topic title.


Thank you,

Posted: Mon Mar 14, 2005 12:49 pm
by dmolavi
Rapid Dr3am wrote: I;ve been thinking about this for a while.

I don't think you should display [domain.tld] after links that are not replaced with text.

So http://www.site.com doesn't need [site.com] after it.

Also internal links, don't need it.

Just for text links at other hosts, would be best imho.


I disagree, partly. Think about all those phishing emails people get that have a typed URL in the body of the message, but if you view the source, the href actually takes you to a *different* URL. What I will do is modify this MOD as follows:
-Prints [domain.tld] after all "hidden" hrefs.
-For http://www.site.com, check the actual href for the link, compare to the displayed text, if same, do nothing, else print the [domain.tld].
-Internal links follow the above two rules.

Posted: Mon Mar 14, 2005 4:24 pm
by dmolavi
Rapid Dr3am wrote: I;ve been thinking about this for a while.

I don't think you should display [domain.tld] after links that are not replaced with text.

So http://www.site.com doesn't need [site.com] after it.

Also internal links, don't need it.

Just for text links at other hosts, would be best imho.


Look at the new MOD posted above, and see http://www.nukedgallery.net/postp8015.html#8015 for an example of how it works.

Posted: Mon Mar 14, 2005 10:52 pm
by omega13a
dmolavi wrote:
Rapid Dr3am wrote:I;ve been thinking about this for a while.

I don't think you should display [domain.tld] after links that are not replaced with text.

So http://www.site.com doesn't need [site.com] after it.

Also internal links, don't need it.

Just for text links at other hosts, would be best imho.


I disagree, partly. Think about all those phishing emails people get that have a typed URL in the body of the message, but if you view the source, the href actually takes you to a *different* URL. What I will do is modify this MOD as follows:
-Prints [domain.tld] after all "hidden" hrefs.
-For http://www.site.com, check the actual href for the link, compare to the displayed text, if same, do nothing, else print the [domain.tld].
-Internal links follow the above two rules.


Its not to hard to implement the part about already part about links already being already displayed. In fact I did it in just a couple minutes. Just replace the following code:

Code: Select all

$text = str_replace("$link", "$link [$host]", $text);
with this:

Code: Select all

if (substr_count($link, $host) < 2)
{
	$text = str_replace("$link", "$link [$host]", $text);
}
And if you want it to not to show the [domain.tdl] stuff for internal links on top of that, make the following changes. Find:

Code: Select all

function makeslashdot($text)
{
After that add:

Code: Select all

global $board_config;
Then find:

Code: Select all

text = str_replace("$link", "$link [$host]", $text);
and replace it with:

Code: Select all

if (substr_count($link, $host) < 2 && !substr_count($board_config['server_name'], $host))
{
	$text = str_replace("$link", "$link [$host]", $text);
}
I've tested these little tweaks on my board and they appear to work.

Posted: Tue Mar 15, 2005 12:19 pm
by dmolavi
it's already in the code that's posted above. please read my prior post.