ed2k links support (with bb-codes)

Started by tFF, April 05, 2007, 07:28:10 AM

Previous topic - Next topic

Darknico

in the error log

Quotehttp://www.mystiteforum/index.php?topic=243.0

8: Undefined offset: 3
File: /home/mysite/public_html/forum/Sources/Subs.php
Linea: 963


in the line 963


// tFF - ed2k
function ed2k_readableSize ($ed2k_link) {
preg_match('~(\s|^)(ed2k://\|file\|.+?\|(.+?)\|.+?)(\s|$)~i', $ed2k_link, $matches);
$size=$matches[3];

$loop = 0;
$suffix = array ("Bytes","KB","MB","GB","TB"); //TeraByte is max possible
while ( ($size > 1024) && ($loop < 4) ) { //loop shouldn't be > 4 so the TB is max
$loop++;
$size /= 1024;
}


return round($size, 2) . " " . $suffix[$loop];
}

// /tFF - ed2k


Italian SMF - Supporto Italiano per la board SMF - Ci trovate tutti li!! :)


tFF

#21
Quote from: Darknico on January 14, 2008, 02:46:25 PM
in the error log
I need an ed2k-link which causes this error.
And I need either line numbers in the code fragment u've quoted or the exact line number 963 (which one). I can only guess the nature of the error and where it may occur but i need more data.

Added later:
I suspect that it occurs at (if it's line 963)
$size=$matches[3];
which means the link is invalid and has no size info (it means it doesn't satisfy ed2k-link structure)
So, gimme the exact ed2k-link pls (and the line number).
So it goes...

Darknico

#22
this link is only a example, for my test

ed2k://|file|RAOH_GAIDEN_DC_(SUB-ITA)_RIP_BY_Danielfor78.avi|722523538|549BC8F30C557700A3255A941C6F87FC|h=THIZLVY6KY7IQUHVKUC4T35PXJPUVAUG|/

result 0 Mb

line 963 is

$size=$matches[3];
Italian SMF - Supporto Italiano per la board SMF - Ci trovate tutti li!! :)


tFF

Thanks. I'll debug it, coz i see no problems in my regexp parser (it finds no errors).
So it goes...

tFF

#24
So, i've posted a msg with
ed2k://|file|RAOH_GAIDEN_DC_(SUB-ITA)_RIP_BY_Danielfor78.avi|722523538|549BC8F30C557700A3255A941C6F87FC|h=THIZLVY6KY7IQUHVKUC4T35PXJPUVAUG|/
and i have nothing to debug 'coz i have no problems with it. Take a look:


And my error log is clean.
tested on SMF 1.1.4

I can not reproduce the bug.
So it goes...

Darknico



my single edit:

'content' => '<img src="__ed2k_ico__" alt="Edonkey Link" title="ed2k: __ed2k_size__" />&nbsp;<a href="$1">__ed2k_name__</a> (__ed2k_size__)',

Italian SMF - Supporto Italiano per la board SMF - Ci trovate tutti li!! :)


tFF

#26
Quote from: Darknico on January 14, 2008, 03:36:41 PM
my single edit:
Just for a test i've inserted your line instead of mine and posted
ed2k://|file|RAOH_GAIDEN_DC_(SUB-ITA)_RIP_BY_Danielfor78.avi|722523538|549BC8F30C557700A3255A941C6F87FC|h=THIZLVY6KY7IQUHVKUC4T35PXJPUVAUG|/
[ed2k]ed2k://|file|RAOH_GAIDEN_DC_(SUB-ITA)_RIP_BY_Danielfor78.avi|722523538|549BC8F30C557700A3255A941C6F87FC|h=THIZLVY6KY7IQUHVKUC4T35PXJPUVAUG|/[/ed2k]
[ed2k=ed2k://|file|RAOH_GAIDEN_DC_(SUB-ITA)_RIP_BY_Danielfor78.avi|722523538|549BC8F30C557700A3255A941C6F87FC|h=THIZLVY6KY7IQUHVKUC4T35PXJPUVAUG|/]lalaa[/ed2k]


and still have no problems


Added later:
If you are aware of php scripting, I would advise you to take any php-debugger and see your forum from inside (as far as i'm unable to reproduce this glitch).

Added later:
Or if you can give me a clue how to reproduce it i'll do it myself.
So it goes...

Darknico

ok, resolve
the problem is the space


[ed2k]ed2k://|file|RAOH_GAIDEN_DC_(SUB-ITA)_RIP_BY_Danielfor78.avi|722523538|549BC8F30C557700A3255A941C6F87FC|h=THIZLVY6KY7IQUHVKUC4T35PXJPUVAUG|/[/ed2k]


It's OK :)



[ed2k]
ed2k://|file|RAOH_GAIDEN_DC_(SUB-ITA)_RIP_BY_Danielfor78.avi|722523538|549BC8F30C557700A3255A941C6F87FC|h=THIZLVY6KY7IQUHVKUC4T35PXJPUVAUG|/
[/ed2k]


Not Work :(
Italian SMF - Supporto Italiano per la board SMF - Ci trovate tutti li!! :)


tFF

#28
Ok.
I think I don't have to add "new line" characters workaround in the mod, coz these tags are not for multi-line content.
The only valid content for ed2k-tags is single-line.

Added later:
or maybe I will trim them automatically next time, not today. :)
So it goes...

Darknico

Italian SMF - Supporto Italiano per la board SMF - Ci trovate tutti li!! :)


tFF

#30
as a quick fix of this issue you can swap the lines
$humanSize = ed2k_readableSize($data);
and
$data = strtr($data, array(\'<br />\' => \'\', \' \' => \'\', \'&nbsp;\' => \'\'));

before:
$humanSize = ed2k_readableSize($data);
$data = strtr($data, array(\'<br />\' => \'\', \' \' => \'\', \'&nbsp;\' => \'\'));


after:
$data = strtr($data, array(\'<br />\' => \'\', \' \' => \'\', \'&nbsp;\' => \'\'));
$humanSize = ed2k_readableSize($data);


change it twice (you will find these lines in two code fragments).

ATTENTION!!! everyone:
If you modify mod's code in Subs.php after the mod is installed you will likely have problems with uninstalling it later. You may comment out original lines to restore them before uninstall.
So it goes...

Darknico

#31
Great!!!
Thanks!!

QuoteATTENTION!!! everyone:
If you modify mod's code in Subs.php after the mod is installed you will likely have problems with uninstalling it later. You may comment out original lines to restore them before uninstall.

no problem
don't use the package ;)
Italian SMF - Supporto Italiano per la board SMF - Ci trovate tutti li!! :)


tomcat2200

Im not sure where all of this went as I have been searching the multitude of posts here looking for the solution.

I have uploaded this latest version into my smf 1.1.4 setup.

I get an error when I try to install this.

The error reads: Modification parse error

If i run the install anyway, nothing happens. no ed2k link responds. I even tried the three "fake" ed2k posts demonstrated here.

Nothing at all. just the text string of the ed2k link.

I almost hate to ask, but please help.

tomcat2200

]Hi all,

I have discovered that when SMF is nested within xBtit, that no package will ever parse correctly.

I ended up having to extract the code from your mod and locate the insertion points. So I did everything manually.

It would have been easier if the insertion points hadn't been mixed between insert before and insert after, being reversed. I find it immensely easier to install code manually, using the modality of sourceforge for such projects as phpbb3 and such, than to having to deal with a weak package installer that is abortive at the drop of the hat.

My thanks to tFF for the code snippets. I have also recently obtained some, more sophisticated code segments for mounting the mule icon ahead of the line and adding the index link at the end as well as file size, which was suggested in an earlier post from another person. I will make it available as I pick it appart and place it.

tFF

#34
Quote from: tomcat2200 on January 16, 2008, 03:54:53 PM
I have discovered that when SMF is nested within xBtit, that no package will ever parse correctly.

I ended up having to extract the code from your mod and locate the insertion points. So I did everything manually.
I'm not responsible for any third-party changes to subs.php and i'm not a prophet to predict all those changes by third-party integration solutions. Only you - the owner - is responsible for all possible side effects. Mod's installation script is based on the original SMF code fragments which are stable and are unlikely to be changed by other mods.

Quote from: tomcat2200 on January 16, 2008, 03:54:53 PM
It would have been easier if the insertion points hadn't been mixed between insert before and insert after, being reversed.
Yeah, that is really hard to get, but if u get that u'll see the logic... but still it's frustrating.

Quote from: tomcat2200 on January 16, 2008, 03:54:53 PM
My thanks to tFF for the code snippets. I have also recently obtained some, more sophisticated code segments for mounting the mule icon ahead of the line and adding the index link at the end as well as file size, which was suggested in an earlier post from another person. I will make it available as I pick it appart and place it.
Waiting for the link. :)

PS btw, what "index link"?
So it goes...

tFF

#35
Quote from: Darknico on January 14, 2008, 04:58:08 PM
no problem
don't use the package ;)
ah?
So it goes...

Darknico

#36
Quote from: tFF on January 17, 2008, 08:29:57 AM
Quote from: Darknico on January 14, 2008, 04:58:08 PM
no problem
don't use the package ;)
ah?

Sorry my English, but I'm Italian...

I made the changes manually without use package manager
Italian SMF - Supporto Italiano per la board SMF - Ci trovate tutti li!! :)


tomcat2200

#37
Here is the function core for the "modified" ED2K link. The index link I referred to is the look up to see the number of current sources for the file. There is a small gif I have for the index link that looks like a small graph, its cute. This uses the same mule icon on the front but the filesize is shown in brackets at the end of the file, followed by the index link icon. The user can click on the index link to get the number of sources currently available for the file.

I think the following code could be coopted and integrated here with minimal effort. If no one here does it, i will eventually get around to it. As it is I am in the middle of a push to get a site on-line for someone who needs it yesterday. The code only seems to extend the same logic used in the original post here. If someone integrates it, let me know. This one works quite well in phpbb3. There are also a few snippets of code that go with this but mostly apply to the phpbb3 context. I do have the complete change mod if anyone is interested.


// eD2k links processing
function humanize_size ($size, $rounder = 0)
{
$sizes = array('Bytes', 'Kb', 'Mb', 'Gb', 'Tb', 'Pb', 'Eb', 'Zb', 'Yb');
$rounders = array(0, 1, 2, 2, 2, 3, 3, 3, 3);
$ext = $sizes[0];
$rnd = $rounders[0];

if ($size < 1024)
{
$rounder = 0;
$format = '%.' . $rounder . 'f Bytes';
}
else
{
for ($i = 1, $cnt = count($sizes); ($i < $cnt && $size >= 1024); $i++)
{
$size = $size / 1024;
$ext = $sizes[$i];
$rnd = $rounders[$i];
$format = '%.' . $rnd . 'f ' . $ext;
}
}

if (!$rounder)
{
$rounder = $rnd;
}

return sprintf($format, round($size, $rounder));
}

function ed2k_link_callback ($m)
{
$max_len = 100;
$href = 'href="' . $m[2] . '" class="postlink"';
$fname = rawurldecode($m[3]);
$fname = preg_replace('/&amp;/i', '&', $fname);
$size = humanize_size($m[4]);

if (strlen($fname) > $max_len)
{
$fname = substr($fname, 0, $max_len - 19) . '...' . substr($fname, -16);
}
if (preg_match('#[<>"]#', $fname))
{
$fname = htmlspecialchars($fname);
}

// return "ed2k: <a $href>$fname&nbsp;&nbsp;[$size]</a> <a href='http://www.emugle.com/details.php?f=$m[5]' target='_blank'><img src='images/emugle.gif' border='0' title='eMugle statistics' style='vertical-align: text-bottom;' /></a>";
return "ed2k: <a $href>$fname&nbsp;&nbsp;[$size]</a> <a href='http://tothbenedek.hu/ed2kstats/ed2k?hash=$m[5]' target='_blank'><img src='images/stats.gif' border='0' title='File statistics' style='vertical-align: text-bottom;' /></a>";
}

function process_ed2k($text)
{
// pad it with a space so we can match things at the start of the 1st line.
$ret = ' ' . $text;

// Patterns and replacements for URL processing
$patterns = array();
$replacements = array();

// [url=http://ed2k://|file|...]ed2k://|file|...[/url] code
$patterns[] = '#\[url\](ed2k://\|file\|(.*?)\|\d+\|\w+\|(h=\w+\|)?/?)\[/url\]#is';
$replacements[] = '<a href="$1" class="postlink">$2</a>';
// [url=http://ed2k://|file|...]name[/url] code
$patterns[] = '#\[url=http://(ed2k://\|file\|(.*?)\|\d+\|\w+\|(h=\w+\|)?/?)\](.*?)\[/url\]#si';
$replacements[] = '<a href="$1" class="postlink">$4</a>';
// [url=http://ed2k://|server|ip|port|/]ed2k://|server|ip|port|/[/url] code
$patterns[] = '#\[url\](ed2k://\|server\|([\d\.]+?)\|(\d+?)\|/?)\[/url\]#si';
$replacements[] = 'ed2k server: <a href="$1" class="postlink">$2:$3</a>';
// [url=http://ed2k://|server|ip|port|/]name[/url] code
$patterns[] = '#\[url=http://(ed2k://\|server\|[\d\.]+\|\d+\|/?)\](.*?)\[/url\]#si';
$replacements[] = '<a href="$1" class="postlink">$2</a>';
// [url=http://ed2k://|friend|name|ip|port|/]ed2k://|friend|name|ip|port|/[/url] code
$patterns[] = '#\[url\](ed2k://\|friend\|(.*?)\|[\d\.]+\|\d+\|/?)\[/url\]#si';
$replacements[] = 'ed2k friend: <a href="$1" class="postlink">$2</a>';
// [url=http://ed2k://|friend|name|ip|port|/]name[/url] code
$patterns[] = '#\[url=(ed2k://\|friend\|(.*?)\|[\d\.]+\|\d+\|/?)\](.*?)\[/url\]#si';
$replacements[] = '<a href="$1" class="postlink">$3</a>';

$ret = preg_replace($patterns, $replacements, $ret);

$ret = preg_replace_callback("#(^|(?<=[^\w\"']))(ed2k://\|file\|([^\\/\|:<>\*\?\"]+?)\|(\d+?)\|([a-f0-9]{32})\|(.*?)/?)(?![\"'])(?=([,\.]*?[\s<\[])|[,\.]*?$)#i", "ed2k_link_callback", $ret);
// ed2k://|server|serverIP|serverPort
$ret = preg_replace("#(^|(?<=[^\w\"']))(ed2k://\|server\|([\d\.]+?)\|(\d+?)\|/?)#i", "ed2k server: <a href=\"\\2\" class=\"postLink\">\\3:\\4</a>", $ret);
// ed2k://|friend|name|clientIP|clientPort
$ret = preg_replace("#(^|(?<=[^\w\"']))(ed2k://\|friend\|([^\\/\|:<>\*\?\"]+?)\|([\d\.]+?)\|(\d+?)\|/?)#i", "ed2k friend: <a href=\"\\2\" class=\"postLink\">\\3</a>", $ret);
// -- END --

// Remove our padding..
$ret = substr($ret, 1);

return($ret);
}

// eD2k Add all links feature
function make_addalled2k_link($text, $post_id)
{
// padding
$ret = ' ' . $text;

// dig through the message for all ed2k links
// split up by "ed2k:"
$ed2k_raw = explode('ed2k://', $text);

// The first item is garbage
unset($ed2k_raw[0]);

// no need to dig through it if there are not at least 2 links
$ed2k_possibles = count($ed2k_raw);
if ($ed2k_possibles > 1)
{
unset($ed2k_real_links);
foreach ($ed2k_raw as $ed2k_raw_line)
{
$ed2k_parts = explode('|', $ed2k_raw_line);
// This looks now like this (only important parts included)
/*
[1]=>
string(4) "file"
[2]=>
string(46) "filename.extension"
[3]=>
string(9) "321456789"
[4]=>
string(32) "112233445566778899AABBCCDDEEFF11"
[5]=>
string(?) "source or AICH hash"
*/

// Check the obvious things
if (strlen($ed2k_parts[1]) == 4 AND $ed2k_parts[1] == 'file' AND strlen($ed2k_parts[2]) > 0 AND floatval($ed2k_parts[3]) > 0 AND strlen($ed2k_parts[4]) == 32)
{
// This is a true link, lets paste it together and put it in an array
if (substr($ed2k_parts[5], 0, 2) == 'h=' || substr($ed2k_parts[5], 0, 7) == 'sources')
{
$ed2k_real_links[] = 'ed2k://|file|' . str_replace('\'', '\\\'', $ed2k_parts[2]) . '|' . $ed2k_parts[3] . '|' . $ed2k_parts[4] . '|' . $ed2k_parts[5] . '|/';
}
else
{
$ed2k_real_links[] = 'ed2k://|file|' . str_replace('\'', '\\\'', $ed2k_parts[2]) . '|' . $ed2k_parts[3] . '|' . $ed2k_parts[4] . '|/';
}
}
}

// Now lets see if we have 2 or more links
// Only then, we do our little trick, because otherwise, it would be wasted for one link alone
$ed2k_confirmed = count($ed2k_real_links);
if ($ed2k_confirmed > 1)
{
$ed2k_insert = '<br /><br />';
$ed2k_insert .= '<SCRIPT>';
$ed2k_insert .= 'filearray' . $post_id . '=new Array;';
$ed2k_insert .= 'n=0;';
$i = 0;
foreach($ed2k_real_links as $ed2k_link)
{
$ed2k_insert .= 'filearray' . $post_id . '[' . $i . ']=\'' . $ed2k_link . '\';';
$i++;
}
$ed2k_insert .= 'iv=false;';
$ed2k_insert .= 'function addfile' . $post_id . '()';
$ed2k_insert .= '{';
$ed2k_insert .= ' var s=filearray' . $post_id . '[n];';
$ed2k_insert .= ' n++;';
$ed2k_insert .= ' if(n==filearray' . $post_id . '.length && iv)';
$ed2k_insert .= ' {';
$ed2k_insert .= ' top.clearInterval(iv);';
$ed2k_insert .= ' n=0;';
$ed2k_insert .= ' }';
$ed2k_insert .= ' top.document.location=s;';
$ed2k_insert .= ' return true;';
$ed2k_insert .= '}';
$ed2k_insert .= 'function addall' . $post_id . '(){iv=top.setInterval("addfile' . $post_id . '()",250)}';
$ed2k_insert .= '</SCRIPT>';
$ed2k_insert .= '<span class="gensmall"><a href="javascript:addall' . $post_id. '()" class="postlink">[ Add all ' . $ed2k_confirmed . ' links to your ed2k client ]</a></span>';
$ret = $ret . $ed2k_insert;
}
}

// remove padding
$ret = substr($ret, 1);

return($ret);
}


Wingman

Ok I had the same problem and i tried the quick fix...

But it doesn't work with me :(
The strange thing is that i get the same error of Darknico, but it doesn't happen only when viewing an ed2k link, but also in other sections of the forum. For example the index page.

Maybe it conflicts with other mods? i have nothing installed which requires huge code changes (better: they only provide added code), except youtube tag and tinyportal.

As soon as i can i will remove tinyportal since i think that it requires too much code changes, it's still buggy and provides many and many features that i will never need ;)

tFF

Quote from: Wingman on March 01, 2008, 01:58:57 PM
Ok I had the same problem and i tried the quick fix...
This error occurs only when bb-code parser comes thru ed2k tags. When parser finds ed2k tags it starts executing ed2k subroutines. This error, you're talking about, says that the parser has called a function for retrieving filesize from ed2k link, but if this error happens it means that the function hasn't got any valid ed2k-link. What means that ed2k-tags were improperly used or it was damaged during parsing a post.
If this is the case, then there is no place for incompatiblility. I hope that very much.

This is only ok for the exact piece of code and the error described above (before the quick-fix). If somehow it occurs with the same type of error but in some other place then reasons and remedies may be completely different.

Anyway it would be better to debug and see from inside.
So it goes...

Advertisement: