News:

Want to get involved in developing SMF, then why not lend a hand on our github!

Main Menu

Titled Links

Started by Mick., July 14, 2007, 12:18:47 PM

Previous topic - Next topic

DirtRider

Where do I find the setting for this mod in my Admin CP  :P
http://www.triumphtalk.com

"The real question is not whether machines think but whether men do. "

Bugo

Quote
There are also options in the admin interface for Posts so you can turn this feature on or off, and also limit the number of URLs to convert.

DirtRider

Quote from: Bugo on September 30, 2010, 09:50:36 AM
Quote
There are also options in the admin interface for Posts so you can turn this feature on or off, and also limit the number of URLs to convert.

Thanks I kept looking over it  ;D However this mod does not work for me at all
http://www.triumphtalk.com

"The real question is not whether machines think but whether men do. "

Sirius OCTeam

Hello

Congrats for this mod, that should be implemented as a default option in smf.

Unfortunately, it only works (on my board at least) in the message preview, once I post the message, title has gone and got standard url instead

Anyone got this issue too ?

IrateZebra

Quote from: Sirius OCTeam on October 16, 2010, 12:34:56 PM
Hello

Congrats for this mod, that should be implemented as a default option in smf.

Unfortunately, it only works (on my board at least) in the message preview, once I post the message, title has gone and got standard url instead

Anyone got this issue too ?
Do you have Aeva installed on your forum?

Sirius OCTeam


IrateZebra

Quote from: Sirius OCTeam on October 17, 2010, 05:17:34 AM
ah yes (related ?)
Very related. There is a known conflict with this mod and Aeva (see this post)

There is still no current resolution.

eckostylez

Quote
Stats:
  1329 messages
  58 had URLs to convert
  104 URLs were converted
  Overall, the script took 76.7803888321 seconds to execute.
  About 1.32379980745 seconds per message
  About 0.738272969539 seconds per URL

Spuds

QuoteVery related. There is a known conflict with this mod and Aeva (see this post)

There is still no current resolution.

Here is an attempt to fix this problem for 2.0 only (sorry kids).  Basically Aeva will wrap any links in
[url]your link[/url]
or
[url=your link]your link[/url]
tags and the regex in this mod only looks for plain links .... Really  the best thing to do is a rewrite so this mod is a function and then call it before Aeva does its work (maybe?) ... but here is an attempt to update what we have to be Aeva aware ... this is for TESTING only.

With that said, in Subs-Post.php find:

// Time to do some cool stuff with URL's
if (!$previewing && $modSettings['convert_urls'])
{

// We're gonna save the old socket timeout, reduce it down to 3 seconds, so we prevent this from hanging up
// We are supressing the errors in case the user cannot change this value on their server.
$timeout = ini_get('default_socket_timeout');
@ini_set('default_socket_timeout',3);

$message = preg_replace(array('~(?<=[\s>\.(;\'"]|^)((?:http)://[\w\-_%@:|]+(?:\.[\w\-_%]+)*(?::\d+)?(?:/[\w\-_\~%\.@,\?&;=#+:\'\\\\]*|[\(\{][\w\-_\~%\.@,\?&;=#(){}+:\'\\\\]*)*[/\w\-_\~%@\?;=#}\\\\])~i', '~(?<=[\s>(\'<]|^)(www(?:\.[\w\-_]+)+(?::\d+)?(?:/[\w\-_\~%\.@,\?&;=#+:\'\\\\]*|[\(\{][\w\-_\~%\.@,\?&;=#(){}+:\'\\\\]*)*[/\w\-_\~%@\?;=#}\\\\])~i'), array('[url]$1[/url%]', '[url]$1[/url%]'), $message);

// Now we find the urls that we just changed, so we can run through them and get titles
preg_match_all("~\[url\](.+?)\[/url%\]~smi", $message, $urls);

if (!empty($urls[0]))
{

// Lets make a counter so we don't exceed the settings...
$title_counter = 0;
foreach($urls[1] as $uri)
{
// If our counter has exceeded the amount, replace the remaining urls back to what they were and get outta dodge.
if(!empty($modSettings['title_url_count']) && $title_counter++ >= $modSettings['title_url_count'])
{
$message = preg_replace('~\[url\]' . $uri . '\[/url%\]~', $uri, $message);
continue;
}

// Attach http:// to the url...
$uri_modified = strpos($uri, 'http://') === false ? 'http://' . $uri : $uri;

// Use the @ to suppress errors from the function not finding a url, which will still return false if
// there is an error.  This just prevents the error log from filling up without cause.
// In 2.0, we have fetch_web_data() which makes our lives easier :)
require_once($sourcedir . '/Subs-Package.php');
$request = @fetch_web_data($uri_modified);

if ($request !== false && preg_match('~<title>(.+?)</title>~smi', $request, $matches))
{
$title = $smcFunc['htmlspecialchars'](stripslashes($matches[1]), ENT_QUOTES);
// Need to fix the &amp;amp;
$title = str_replace('&amp;amp;', '&amp;', $title);
// w00t!  Changin the link to titlize it (is that word?)
$message = str_replace('[url]' . $uri . '[/url%]', '[url=' . $uri_modified . ']' . $title . '[/url]', $message);
}

// Looks like we couldn't get the title, darn.  Back to the original we go...
else
$message = str_replace('[url]' . $uri . '[/url%]', $uri, $message);
}
}
// Change it back to what it was.  Suppress again incase...
@ini_set('default_socket_timeout',$timeout);
}


replace with:

// Time to do some cool stuff with URL's
if (!$previewing && $modSettings['convert_urls'])
{

// We're gonna save the old socket timeout, reduce it down to 3 seconds, so we prevent this from hanging up
// We are supressing the errors in case the user cannot change this value on their server.
$timeout = ini_get('default_socket_timeout');
@ini_set('default_socket_timeout',3);

// Aeva converts links as part of its onposting operations, namely [url]$1[/url] & [url=http://$1]$1[/url]
// we need to revert these for this mod to work and put them back if needed
if (isset($modSettings['aeva_enable'])) {
// maybe its [url=http://bbb.bbb.bbb]bbb.bbb.bbb[/url]
preg_match_all("~\[url=(https?:\/\/(.+?))\](?:https?:\/\/)?(.+?)\[/url\]~si", $message, $pre_urls, PREG_SET_ORDER);
foreach($pre_urls as $uri_check) {
if (isset($uri_check[2]) && isset($uri_check[3]) && ($uri_check[2] == $uri_check[3])) {
// link is the same as the title ... so set it back to be just a non bbc link for now
$message = str_replace($uri_check[0], $uri_check[2], $message);
}
}
// maybe it just like [url]bbb.bbb.bbb[/url]
preg_match_all("~\[url\](https?:\/\/(.+?))\[/url\]~si", $message, $pre_urls, PREG_SET_ORDER);
foreach($pre_urls as $uri_check) {
// link is the same as the title ... so set it back to be just a non bbc link for now
$message = str_replace($uri_check[0], $uri_check[1], $message);
}
}

$message = preg_replace(
array('~(?<=[\s>\.(;\'"]|^)((?:http)://[\w\-_%@:|]+(?:\.[\w\-_%]+)*(?::\d+)?(?:/[\w\-_\~%\.@,\?&;=#+:\'\\\\]*|[\(\{][\w\-_\~%\.@,\?&;=#(){}+:\'\\\\]*)*[/\w\-_\~%@\?;=#}\\\\])~i', '~(?<=[\s>(\'<]|^)(www(?:\.[\w\-_]+)+(?::\d+)?(?:/[\w\-_\~%\.@,\?&;=#+:\'\\\\]*|[\(\{][\w\-_\~%\.@,\?&;=#(){}+:\'\\\\]*)*[/\w\-_\~%@\?;=#}\\\\])~i'),
array('[url]$1[/url%]', '[url]$1[/url%]'), $message);

// Now we find the urls that we just changed, so we can run through them and get titles
preg_match_all("~\[url\](.+?)\[/url%\]~smi", $message, $urls);

if (!empty($urls[0]))
{
// Lets make a counter so we don't exceed the settings...
$title_counter = 0;
foreach($urls[1] as $uri)
{
// Attach http:// to the url... make sure there is a trailing '/'
$uri_modified = trim((strpos($uri, 'http://') === false ? 'http://' . $uri : $uri));
if ($uri_modified[strlen($uri_modified)-1] != '/')
$uri_modified .= '/';

// If our counter has exceeded the amount, replace the remaining urls back to what they were and get outta dodge.
if(!empty($modSettings['title_url_count']) && $title_counter++ >= $modSettings['title_url_count'])
{
if (isset($modSettings['aeva_enable']))
$message = preg_replace('~\[url\]' . $uri . '\[/url%\]~', '[url=' . $uri_modified . ']' . $uri . '[/url]', $message);
else
$message = preg_replace('~\[url\]' . $uri . '\[/url%\]~', $uri, $message);
continue;
}

// Use the @ to suppress errors from the function not finding a url, which will still return false if
// there is an error.  This just prevents the error log from filling up without cause.
// In 2.0, we have fetch_web_data() which makes our lives easier :)
require_once($sourcedir . '/Subs-Package.php');
$request = @fetch_web_data($uri_modified);

if ($request !== false && preg_match('~<title>(.+?)</title>~smi', $request, $matches))
{
$title = $smcFunc['htmlspecialchars'](stripslashes($matches[1]), ENT_QUOTES);
// Need to fix the &amp;amp;
$title = str_replace('&amp;amp;', '&amp;', $title);
// w00t!  Changin the link to titlize it (is that word?)
$message = str_replace('[url]' . $uri . '[/url%]', '[url=' . $uri_modified . ']' . $title . '[/url]', $message);
}

// Looks like we couldn't get the title, darn.  Back to the original we go...
else {
if (isset($modSettings['aeva_enable']))
$message = preg_replace('~\[url\]' . $uri . '\[/url%\]~', '[url=' . $uri_modified . ']' . $uri . '[/url]', $message);
else
$message = str_replace('[url]' . $uri . '[/url%]', $uri, $message) . '******';
}
}
}
// Change it back to what it was.  Suppress again incase...
@ini_set('default_socket_timeout',$timeout);
}

king kratos

Quote from: Spuds on December 17, 2010, 07:03:34 PM
QuoteVery related. There is a known conflict with this mod and Aeva (see this post)

There is still no current resolution.

Here is an attempt to fix this problem for 2.0 only (sorry kids).  Basically Aeva will wrap any links in
[url]your link[/url]
or
[url=your link]your link[/url]
tags and the regex in this mod only looks for plain links .... Really  the best thing to do is a rewrite so this mod is a function and then call it before Aeva does its work (maybe?) ... but here is an attempt to update what we have to be Aeva aware ... this is for TESTING only.

With that said, in Subs-Post.php find:

// Time to do some cool stuff with URL's
if (!$previewing && $modSettings['convert_urls'])
{

// We're gonna save the old socket timeout, reduce it down to 3 seconds, so we prevent this from hanging up
// We are supressing the errors in case the user cannot change this value on their server.
$timeout = ini_get('default_socket_timeout');
@ini_set('default_socket_timeout',3);

$message = preg_replace(array('~(?<=[\s>\.(;\'"]|^)((?:http)://[\w\-_%@:|]+(?:\.[\w\-_%]+)*(?::\d+)?(?:/[\w\-_\~%\.@,\?&;=#+:\'\\\\]*|[\(\{][\w\-_\~%\.@,\?&;=#(){}+:\'\\\\]*)*[/\w\-_\~%@\?;=#}\\\\])~i', '~(?<=[\s>(\'<]|^)(www(?:\.[\w\-_]+)+(?::\d+)?(?:/[\w\-_\~%\.@,\?&;=#+:\'\\\\]*|[\(\{][\w\-_\~%\.@,\?&;=#(){}+:\'\\\\]*)*[/\w\-_\~%@\?;=#}\\\\])~i'), array('[url]$1[/url%]', '[url]$1[/url%]'), $message);

// Now we find the urls that we just changed, so we can run through them and get titles
preg_match_all("~\[url\](.+?)\[/url%\]~smi", $message, $urls);

if (!empty($urls[0]))
{

// Lets make a counter so we don't exceed the settings...
$title_counter = 0;
foreach($urls[1] as $uri)
{
// If our counter has exceeded the amount, replace the remaining urls back to what they were and get outta dodge.
if(!empty($modSettings['title_url_count']) && $title_counter++ >= $modSettings['title_url_count'])
{
$message = preg_replace('~\[url\]' . $uri . '\[/url%\]~', $uri, $message);
continue;
}

// Attach http:// to the url...
$uri_modified = strpos($uri, 'http://') === false ? 'http://' . $uri : $uri;

// Use the @ to suppress errors from the function not finding a url, which will still return false if
// there is an error.  This just prevents the error log from filling up without cause.
// In 2.0, we have fetch_web_data() which makes our lives easier :)
require_once($sourcedir . '/Subs-Package.php');
$request = @fetch_web_data($uri_modified);

if ($request !== false && preg_match('~<title>(.+?)</title>~smi', $request, $matches))
{
$title = $smcFunc['htmlspecialchars'](stripslashes($matches[1]), ENT_QUOTES);
// Need to fix the &amp;amp;
$title = str_replace('&amp;amp;', '&amp;', $title);
// w00t!  Changin the link to titlize it (is that word?)
$message = str_replace('[url]' . $uri . '[/url%]', '[url=' . $uri_modified . ']' . $title . '[/url]', $message);
}

// Looks like we couldn't get the title, darn.  Back to the original we go...
else
$message = str_replace('[url]' . $uri . '[/url%]', $uri, $message);
}
}
// Change it back to what it was.  Suppress again incase...
@ini_set('default_socket_timeout',$timeout);
}


replace with:

// Time to do some cool stuff with URL's
if (!$previewing && $modSettings['convert_urls'])
{

// We're gonna save the old socket timeout, reduce it down to 3 seconds, so we prevent this from hanging up
// We are supressing the errors in case the user cannot change this value on their server.
$timeout = ini_get('default_socket_timeout');
@ini_set('default_socket_timeout',3);

// Aeva converts links as part of its onposting operations, namely [url]$1[/url] & [url=http://$1]$1[/url]
// we need to revert these for this mod to work and put them back if needed
if (isset($modSettings['aeva_enable'])) {
// maybe its [url=http://bbb.bbb.bbb]bbb.bbb.bbb[/url]
preg_match_all("~\[url=(https?:\/\/(.+?))\](?:https?:\/\/)?(.+?)\[/url\]~si", $message, $pre_urls, PREG_SET_ORDER);
foreach($pre_urls as $uri_check) {
if (isset($uri_check[2]) && isset($uri_check[3]) && ($uri_check[2] == $uri_check[3])) {
// link is the same as the title ... so set it back to be just a non bbc link for now
$message = str_replace($uri_check[0], $uri_check[2], $message);
}
}
// maybe it just like [url]bbb.bbb.bbb[/url]
preg_match_all("~\[url\](https?:\/\/(.+?))\[/url\]~si", $message, $pre_urls, PREG_SET_ORDER);
foreach($pre_urls as $uri_check) {
// link is the same as the title ... so set it back to be just a non bbc link for now
$message = str_replace($uri_check[0], $uri_check[1], $message);
}
}

$message = preg_replace(
array('~(?<=[\s>\.(;\'"]|^)((?:http)://[\w\-_%@:|]+(?:\.[\w\-_%]+)*(?::\d+)?(?:/[\w\-_\~%\.@,\?&;=#+:\'\\\\]*|[\(\{][\w\-_\~%\.@,\?&;=#(){}+:\'\\\\]*)*[/\w\-_\~%@\?;=#}\\\\])~i', '~(?<=[\s>(\'<]|^)(www(?:\.[\w\-_]+)+(?::\d+)?(?:/[\w\-_\~%\.@,\?&;=#+:\'\\\\]*|[\(\{][\w\-_\~%\.@,\?&;=#(){}+:\'\\\\]*)*[/\w\-_\~%@\?;=#}\\\\])~i'),
array('[url]$1[/url%]', '[url]$1[/url%]'), $message);

// Now we find the urls that we just changed, so we can run through them and get titles
preg_match_all("~\[url\](.+?)\[/url%\]~smi", $message, $urls);

if (!empty($urls[0]))
{
// Lets make a counter so we don't exceed the settings...
$title_counter = 0;
foreach($urls[1] as $uri)
{
// Attach http:// to the url... make sure there is a trailing '/'
$uri_modified = trim((strpos($uri, 'http://') === false ? 'http://' . $uri : $uri));
if ($uri_modified[strlen($uri_modified)-1] != '/')
$uri_modified .= '/';

// If our counter has exceeded the amount, replace the remaining urls back to what they were and get outta dodge.
if(!empty($modSettings['title_url_count']) && $title_counter++ >= $modSettings['title_url_count'])
{
if (isset($modSettings['aeva_enable']))
$message = preg_replace('~\[url\]' . $uri . '\[/url%\]~', '[url=' . $uri_modified . ']' . $uri . '[/url]', $message);
else
$message = preg_replace('~\[url\]' . $uri . '\[/url%\]~', $uri, $message);
continue;
}

// Use the @ to suppress errors from the function not finding a url, which will still return false if
// there is an error.  This just prevents the error log from filling up without cause.
// In 2.0, we have fetch_web_data() which makes our lives easier :)
require_once($sourcedir . '/Subs-Package.php');
$request = @fetch_web_data($uri_modified);

if ($request !== false && preg_match('~<title>(.+?)</title>~smi', $request, $matches))
{
$title = $smcFunc['htmlspecialchars'](stripslashes($matches[1]), ENT_QUOTES);
// Need to fix the &amp;amp;
$title = str_replace('&amp;amp;', '&amp;', $title);
// w00t!  Changin the link to titlize it (is that word?)
$message = str_replace('[url]' . $uri . '[/url%]', '[url=' . $uri_modified . ']' . $title . '[/url]', $message);
}

// Looks like we couldn't get the title, darn.  Back to the original we go...
else {
if (isset($modSettings['aeva_enable']))
$message = preg_replace('~\[url\]' . $uri . '\[/url%\]~', '[url=' . $uri_modified . ']' . $uri . '[/url]', $message);
else
$message = str_replace('[url]' . $uri . '[/url%]', $uri, $message) . '******';
}
}
}
// Change it back to what it was.  Suppress again incase...
@ini_set('default_socket_timeout',$timeout);
}


This code works just fine with 2.0 RC4 with the fix provided. However, it must be installed while emulating RC3.

Kratos

My LG Cookie Fresh

Sorry if This has already to post this but I didn't install this mod and it sill works...?

impreza

Cool addition, I like it
Portal ToTemat.pl - Filmy i Artykuły

willerby

If I post a link from a topic in my forum that is on a member only board it gives me a link titled Login? Otherwise great mod.

JBlaze

Quote from: laetabi on January 15, 2011, 06:33:22 PM
If I post a link from a topic in my forum that is on a member only board it gives me a link titled Login? Otherwise great mod.
That's because the script fetches the page as a guest, and "Login" is the page title when prompted to login on a board guests can't see.
Jason Clemons
Former Team Member 2009 - 2012

Sirius OCTeam

QuoteThis code works just fine with 2.0 RC4 with the fix provided

I confirm too, conflict with aeva fixed with this code.

Thanks

Pause

Great mod which I'm using with the Aeva compatible code, thanks.

One small issue however is that certain characters are converted to their html entities (such as ", < and > instead displaying as &quot;, &lt;, and &gt;).
"You and me, inside that box, now." - The Doctor

Bite Fusion
Fusion Web Network
VG Fusion
Wrestling Fusion

Spuds

QuoteOne small issue however is that certain characters are converted to their html entities (such as ", < and > instead displaying as &quot;, &lt;, and &gt;).
The reality is that you are going to find several small things and its really not the mods fault but the fault of the web page that you are scraping the title from.   I've been using the mod for a couple of months now and have run across some interesting uses of the title tag.

Anyway here are a couple of lines you can add to help clean up the titles based on some of what I have seen over the last couple of months.  Find these lines


            if ($request !== false && preg_match('~<title>(.+?)</title>~smi', $request, $matches))
            {
               $title = $smcFunc['htmlspecialchars'](stripslashes($matches[1]), ENT_QUOTES);
               // Need to fix the &amp;amp;
               $title = str_replace('&amp;amp;', '&amp;', $title);
               // w00t!  Changin the link to titlize it (is that word?)
               $message = str_replace('[url]' . $uri . '[/url%]', '[url=' . $uri_modified . ']' . $title . '[/url]', $message);
            }


replace them with these

            if ($request !== false && preg_match('~<title>(.+?)</title>~smi', $request, $matches))
            {
// Undo htmlspecial first as we redo shortly
$title = trim(html_entity_decode(un_htmlspecialchars($matches[1])));

// remove dumb stuff we find in titles
$title = str_replace(array('&mdash;',"\n","\t"),array('-',' ',' '),$title);
$title = preg_replace('~\s{2,20}~',' ',$title);
$title = $smcFunc['htmlspecialchars'](stripslashes($title), ENT_QUOTES);

// w00t!  Changin the link to titlize it (is that word?)
$message = str_replace('[url]' . $uri . '[/url%]', '[url=' . $uri_modified . ']' . $title . '[/url]', $message);
            }

and see if that fixes the issue you are seeing ...

Pause

Quote from: Spuds on February 03, 2011, 10:48:12 PM
The reality is that you are going to find several small things and its really not the mods fault but the fault of the web page that you are scraping the title from.   I've been using the mod for a couple of months now and have run across some interesting uses of the title tag.
True, it can be odd what some sites put in there at times.

Quote from: Spuds on February 03, 2011, 10:48:12 PMand see if that fixes the issue you are seeing ...
It does indeed, thank you.  :)
"You and me, inside that box, now." - The Doctor

Bite Fusion
Fusion Web Network
VG Fusion
Wrestling Fusion

seficar

is it ok to install this 2.0 RC5?

JBlaze

Quote from: seficar on February 15, 2011, 08:25:15 AM
is it ok to install this 2.0 RC5?
Not yet. I haven't got around to updating my mods.
Jason Clemons
Former Team Member 2009 - 2012

Advertisement: