Camen Design Forum

ext. links through anonym.to

append delete vasek14

Hi. How to make redirect for ext. links through anonym.to ?
example: http://anonym.to/?domain.com

Reply RSS

Replies

append delete #1. Kroc

In "lib/functions.php" change the following:

% PHP
//replace the URL in the source text with a hyperlinked version:
)) $text = substr_replace ($text, $replace =
	'<a href="'.(@$m[2][0]	? 'mailto:'.$m[2][0]			//is this an e-mail address?
				: ($m[1][0] ? $m[1][0] : 'http://'))	//has a protocol been given?
	//rest of the URL [domain . slash . everything-else]
	//(encode double-quotes without double-encoding existing ampersands; this is the PHP5.2.3 requirement)
	.htmlspecialchars ($m[3][0].@$m[4][0].@$m[5][0], ENT_COMPAT, 'UTF-8', false).'" rel="nofollow">'
	.$m[0][0].'</a>',
	//where to substitute
	$m[0][1], strlen ($m[0][0])
);
%

to

% PHP
//replace the URL in the source text with a hyperlinked version:
)) $text = substr_replace ($text, $replace =
	'<a href="'.(@$m[2][0]	? 'mailto:'.$m[2][0]			//is this an e-mail address?
				: 'http://anonym.to/?'			//anonymise links
				  .($m[1][0] ? $m[1][0] : 'http://'))	//has a protocol been given?
	//rest of the URL [domain . slash . everything-else]
	.htmlspecialchars ($m[3][0].@$m[4][0].@$m[5][0], ENT_COMPAT, 'UTF-8', true).'" rel="nofollow">'
	.$m[0][0].'</a>',
	//where to substitute
	$m[0][1], strlen ($m[0][0])
);
%

(This applies to internal and external links in posts, but internal links won't be too often and it won't hurt)

You might want to test this a bit (especially on links with querystrings)

append delete #2. Martijn

Or you can use the following code instead of Kroc’s if you don’t want it to apply to internal links and only to external links. This is based on the code I submitted to the development branch on Github [1]:

% PHP: lib.php
	)) $text = substr_replace ($text, $replace =
		'<a href="'.(@$m[2][0] ? 'mailto:'.$m[2][0]                     //is this an e-mail address?
		                       : (($ext = 
		                            ($m[1][0] = ($m[1][0] ? $m[1][0] : 'http://')) //has a protocol been given?
		                            . $m[3][0] !== FORUM_URL   //is this external?
		                          ) ? 'http://anonym.to/?'  //external link, anonymise it.
		                            : '' //internal link, do nothing.
		                         )
		                         . $m[1][0])    //output protocol
			//rest of the URL [domain . slash . everything-else]
			//(encode double-quotes without double-encoding existing ampersands; this is the PHP5.2.3 req.)
			.htmlspecialchars ($m[3][0].@$m[4][0].@$m[5][0], ENT_COMPAT, 'UTF-8', false).'"'
			//is the URL external? if so add the rel attributes
			.($ext ? ' rel="nofollow external"' : '')
		.'>'	//the link-text
			.$m[0][0]
		.'</a>',
		//where to substitute
		$m[0][1], strlen ($m[0][0])
	);
%

This adds `rel="nofollow external"` to all outgoing links, and no more `nofollow` on inbound links (better ‘SEO’). It will also add `http://anonym.to?` to all these outgoing links.

I just noticed it adds it adds the `rel` value to email addresses too, this could be seen as a bug but does no harm. It does not add `anonym.to` to email addresses. I didn’t test anything other then email, HTTP, and HTTPS links.

Good luck.

---

[1]: https://github.com/Kroc/NoNonsenseForum/issues/152

append delete #3. vasek14

Martijn, not working

append delete #4. Martijn

@vasek14: thanks for replying. What isn’t working? Are you getting some error? I have tested it on my installation of the forum and it seems to be working.

You have to follow Kroc’s instructions and replace the original `substr_replace` block:

% PHP: lib.php
//replace the URL in the source text with a hyperlinked version:
)) $text = substr_replace ($text, $replace =
	'<a href="'.(@$m[2][0]	? 'mailto:'.$m[2][0]			//is this an e-mail address?
				: ($m[1][0] ? $m[1][0] : 'http://'))	//has a protocol been given?
	//rest of the URL [domain . slash . everything-else]
	//(encode double-quotes without double-encoding existing ampersands; this is the PHP5.2.3 requirement)
	.htmlspecialchars ($m[3][0].@$m[4][0].@$m[5][0], ENT_COMPAT, 'UTF-8', false).'" rel="nofollow">'
	.$m[0][0].'</a>',
	//where to substitute
	$m[0][1], strlen ($m[0][0])
);
%
append delete #5. Kroc

Looking at this, I think both sample codes given are faulty because the original URLs need to be safeURL encoded (since they are now part of a querystring) in order to work in cases of URLs containing query strings.

append delete #6. vasek14

just simple redirect, no anonym.to, no new tab, and what is lib.php ?? on my forum this code place in lib/functions.php, if u may give me plz working functions.php with this code include

append delete #7. Kroc

That was a typo; it is function.php, not lib.php

append delete #8. Martijn

@Kroc:

Looking at this, I think both sample codes given are faulty because the original URLs need to be safeURL encoded (since they are now part of a querystring) in order to work in cases of URLs containing query strings.

Not really, unless this is a requirement for the XML parsing. I tested the anonym.to service and they take unencoded questionmarks. My test case included a link to a different topic, a link to this topic, a link to a google query, and an email address. Non of those gave any problems.

@vasek14:

[…] and what is lib.php ??

lib.php is my fault, my test forum uses slightly more files for easy function swapping. You are completely right, of course it has to go in lib/functions.php!

Here is my lib/functions.php, and it is working for me. Note that this is based on functions.php of the development branch [1] and I can’t guarantee it will work with the master (stable release) branch of the forum: https://gist.github.com/4016351

---

[1]: https://github.com/Kroc/NoNonsenseForum/tree/development

append delete #9. Kroc

@Martijn

my test forum uses slightly more files for easy function swapping

Anything there I should consider adopting? I was thinking about splitting off the `safe*` and `formatText` functions into a separate file, but I'm wary of making NNF less readable by having too many files. I'm always interested to know what others are doing with NNF in case there's anything new I can learn to make it better for them.

append delete #10. vasek14

not working, probably work only with dev version

append delete #11. Martijn

@vasek14:

That’s really weird, the differences are really minimal, I just ran a comparison and appart for some variable names not a lot can go wrong. Are you seeing any errors? I would expect PHP errors to show up if there are any incompatibilities.

Are you testing with new topics everytime you switch out the function? Note that URLs that already have been posted *never* change. NoNonsenseForum writes directly to HTML and does not reparse posts at a later time. At least, not yet [1].

@Kroc:

Anything there I should consider adopting?

Not really, it’s just a mess. I had to figure out how you were splitting URLs for the external link code [2] so I added some debugging stuff. Other than that there is some stuff I was working on for a theme.

I tried getting gravatar [3] to work within a theme when I noticed adding a `$template` modifier in theme.php couldn’t give me access to any of the looped values such as usernames. I tried to fix this so I would have access to things like post numbers and dates. I never got it working but did end up with a mess of code ;-)

---

[1]: https://github.com/Kroc/NoNonsenseForum/issues/95
[2]: https://github.com/Kroc/NoNonsenseForum/issues/152
[3]: https://github.com/Kroc/NoNonsenseForum/issues/148

append delete #12. Kroc

@Martijn:

I noticed adding a `$template` modifier in theme.php couldn’t give me access to any of the looped values such as usernames

I should therefore add an additional hook for each of the loops! Supporting Gravatars at the 'theme.php' level is just what NNF is designed for.

Reply

(Leave this as-is, it’s a trap!)

There is no need to “register”, just enter the same name + password of your choice every time.

Pro tip: Use markup to add links, quotes and more.

Your friendly neighbourhood moderators: Kroc, Impressed, Martijn