phpBB3: k0nsl URL Shortener

I needed a simple way to shorten URLs for a client, when using phpBB3, so I looked at already available modifications and found this relatively old modification written by trkodlama. So I forked it, did some minor changes to it, but nothing major — yet.
phpbb01_k0nsl
His/her modification can be found here:
[RC] Togl URL Shortener

Now for getting this to work (oh, and by the way; it uses “knsl.net” by default, but one can easily modify it):

Open ./includes/functions.php

Add this function at the very end of the file just before the PHP closing tag:
[php]
/* MOD: k0nsl Start */
function curl_get_url($url) {
$ch = curl_init();
$timeout = 5;
curl_setopt($ch,CURLOPT_URL,$url);
curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);
curl_setopt($ch,CURLOPT_CONNECTTIMEOUT,$timeout);
$data = curl_exec($ch);
curl_close($ch);
return $data;
}
/* MOD: k0nsl End */
[/php]

Open ./includes/message_parser.php and find:
[php]
// Is this a link to somewhere inside this board? If so then remove the session id from the url
if (strpos($url, generate_board_url()) !== false && strpos($url, ‘sid=’) !== false)
{
$url = preg_replace(‘/(&|\?)sid=[0-9a-f]{32}&/’, ‘\1’, $url);
$url = preg_replace(‘/(&|\?)sid=[0-9a-f]{32}$/’, ”, $url);
$url = append_sid($url);
}
[/php]
(should be around line 984 or something like that)

Add this on a new line after the curly bracket:
[php]
/* MOD: k0nsl Start */
else if (strpos($url, "YOUR-DOMAIN-HERE")===FALSE && strpos($url, "knsl.net")===FALSE && strpos($url, "knsl.org")===FALSE)
{
do{
$knsl_api=""; // If you have an api code, please enter
$url=urlencode($url);
if(trim($knsl_api)==""){
$url = @curl_get_url("http://knsl.net/api.php?url=".$url);
}
else{
$url = @curl_get_url("http://knsl.net/api.php?url=".$knsl_api."&url=".$url);
}
}while(!$url);
if($url=="Please provide a valid URL."){
$url=generate_board_url();
}
}
/* MOD: k0nsl End */
[/php]

Open ./includes/functions_content.php and find:
[php]
$url = htmlspecialchars($url);
[/php]
(should be on line 626, or close to that)

Add this code below (on a new line):
[php]
/* MOD: k0nsl Start */
if (strpos($url, "YOUR-DOMAIN-HERE")===FALSE && strpos($url, "knsl.net")===FALSE && strpos($url, "knsl.org")===FALSE)
{
do{
$knsl_api=""; // If you have an api code, please enter
$url=urlencode($url);
if(trim($knsl_api)==""){
$url = @curl_get_url("http://knsl.net/api.php?url=".$url);
}
else{
$url = @curl_get_url("http://knsl.net/api.php?url=".$knsl_api."&url=".$url);
}
}while(!$url);
if($url=="Please provide a valid URL."){
$url=generate_board_url();
}
}
/* MOD: k0nsl End */
[/php]

That’s all. I’m going to trim it down a bit later on.

One Comment

Add a Comment

Your email address will not be published. Required fields are marked *