[DEV] Slashdot style links in posts

A place for MOD Authors to post and receive feedback on MODs still in development. No MODs within this forum should be used within a live environment! No new topics are allowed in this forum.
Forum rules
READ: phpBB.com Board-Wide Rules and Regulations

IMPORTANT: MOD Development Forum rules

On February 1, 2009 this forum will be set to read only as part of retiring of phpBB2.
Post Reply
dmolavi
Registered User
Posts: 29
Joined: Fri Feb 20, 2004 7:45 pm
Location: Washington Township, NJ, USA
Contact:

[DEV] Slashdot style links in posts

Post 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.
Last edited by dmolavi on Mon Mar 14, 2005 12:46 pm, edited 6 times in total.
Image
Cross_+_Flame
Registered User
Posts: 295
Joined: Wed Oct 30, 2002 3:37 pm
Location: Okie-homa!
Contact:

Post 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).
dmolavi
Registered User
Posts: 29
Joined: Fri Feb 20, 2004 7:45 pm
Location: Washington Township, NJ, USA
Contact:

Post 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
Last edited by dmolavi on Mon Mar 14, 2005 4:23 pm, edited 13 times in total.
Image
Rapid Dr3am
Registered User
Posts: 198
Joined: Sun Jul 13, 2003 4:14 pm
Location: Incommunicado
Contact:

Post by Rapid Dr3am »

I don't see the find line in bbcode.php in a vanilla install.
ptlis
Registered User
Posts: 90
Joined: Thu Aug 19, 2004 2:22 pm
Location: Newcastle, UK
Contact:

Post 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.
I will not be pushed, filed, stamped, indexed, briefed, debriefed or numbered.....my life is my own.
dmolavi
Registered User
Posts: 29
Joined: Fri Feb 20, 2004 7:45 pm
Location: Washington Township, NJ, USA
Contact:

Post by dmolavi »

Thanks....fixed.
Image
ptlis
Registered User
Posts: 90
Joined: Thu Aug 19, 2004 2:22 pm
Location: Newcastle, UK
Contact:

Post by ptlis »

The MOD works great, Thanks!
I will not be pushed, filed, stamped, indexed, briefed, debriefed or numbered.....my life is my own.
Rapid Dr3am
Registered User
Posts: 198
Joined: Sun Jul 13, 2003 4:14 pm
Location: Incommunicado
Contact:

Post 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.
User avatar
smithy_dll
Former Team Member
Posts: 7630
Joined: Tue Jan 08, 2002 6:27 am
Location: Australia
Name: Lachlan Smith
Contact:

Post 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,
Systems Engineering
dmolavi
Registered User
Posts: 29
Joined: Fri Feb 20, 2004 7:45 pm
Location: Washington Township, NJ, USA
Contact:

Post 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.
Image
dmolavi
Registered User
Posts: 29
Joined: Fri Feb 20, 2004 7:45 pm
Location: Washington Township, NJ, USA
Contact:

Post 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.
Image
omega13a
Registered User
Posts: 88
Joined: Wed Feb 20, 2002 11:53 pm
Location: SF Bay Area
Contact:

Post 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.
A fish without a bicycle cannot contemplate his navel.
dmolavi
Registered User
Posts: 29
Joined: Fri Feb 20, 2004 7:45 pm
Location: Washington Township, NJ, USA
Contact:

Post by dmolavi »

it's already in the code that's posted above. please read my prior post.
Image
Post Reply

Return to “[2.0.x] MODs in Development”