MyBB Hacks

Full Version: custom url mycode
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
can anyone create a mybb which parse only url for the url links we place inside the [code] tags?
if we use default code tag, it will not parse the url links and displays plain whole url links in a single line. and if we just put http://www.blablabla.bla links without coding they will automatically parsed with url tag and if the url is longer, they will be replaced with terminated short link as http://www.blablac......

i need a custom mycode, where is parses the long/short http links with url tag only and it must not terminate the longer links instead it must display a bottom scroll bar which is done by default code tags.
Yes, [code] tags doesn't show links inside them, but we have another option. Try to use quote tags instead. Like;

Quote:http://zingaburga.com

It shows the links too.
yeah, i have tried using quote tag, but it is truncating long url links and showing ... in the short link
To remove URL shortening (affects everything), find and remove the following from inc/class_parser.php

PHP Code:
		if($name == $url && (!isset($this->options['shorten_urls']) || $this->options['shorten_urls'] != 0))
		{
			if(my_strlen($url) > 55)
			{
				$name = my_substr($url, 0, 40)."...".my_substr($url, -10);
			}
		}


For links in code tags, try this (untested), find in inc/class_parser.php:

PHP Code:
$code = str_replace('\\', '\', $code);

add after:

PHP Code:
$code = preg_replace('#([>\s()]|^)((http|ftp|news)\://([^/"\s<\[.]+\.)+\w+(\:[0-9]+)?(/[^"\s<\[]*)?)#i', '$1<a href="$2">$2</a>', $code);

Thank you zinga, both worked.
Well, some people said to use custom mycode instead of changing the core

Code:
 tags as it changes all.

So after making your edits to the core file, i thought of copying the whole function for the mycode_parse_code and added a copy of it below it and tried to create the custom code just by changing all the $code instances in that function with the new mycode name, but it had thrown some error when i used that code tags ex: [newtag] [/newtag]
Code USed:
[code]/**
	* Parses quotes with post id and/or dateline.
	*
	* @param string The message to be parsed
	* @param string The username to be parsed
	* @param boolean Are we formatting as text?
	* @return string The parsed message.
	*/
	function mycode_parse_post_quotes($message, $username, $text_only=false)
	{
		global $lang, $templates, $theme, $mybb;

		$linkback = $date = "";

		$message = trim($message);
		$message = preg_replace("#(^<br(\s?)(\/?)>|<br(\s?)(\/?)>$)#i", "", $message);

		if(!$message) return '';

		$message = str_replace('\"', '"', $message);
		$username = str_replace('\"', '"', $username)."'";
		$delete_quote = true;

		preg_match("#pid=(?:&quot;|\"|')?([0-9]+)[\"']?(?:&quot;|\"|')?#i", $username, $match);
		if(intval($match[1]))
		{
			$pid = intval($match[1]);
			$url = $mybb->settings['bburl']."/".get_post_link($pid)."#pid$pid";
			if(defined("IN_ARCHIVE"))
			{
				$linkback = " <a href=\"{$url}\">[ -> ]</a>";
			}
			else
			{
				eval("\$linkback = \" ".$templates->get("postbit_gotopost", 1, 0)."\";");
			}
			
			$username = preg_replace("#(?:&quot;|\"|')? pid=(?:&quot;|\"|')?[0-9]+[\"']?(?:&quot;|\"|')?#i", '', $username);
			$delete_quote = false;
		}

		unset($match);
		preg_match("#dateline=(?:&quot;|\"|')?([0-9]+)(?:&quot;|\"|')?#i", $username, $match);
		if(intval($match[1]))
		{
			if($match[1] < TIME_NOW)
			{
				$postdate = my_date($mybb->settings['dateformat'], intval($match[1]));
				$posttime = my_date($mybb->settings['timeformat'], intval($match[1]));
				$date = " ({$postdate} {$posttime})";
			}
			$username = preg_replace("#(?:&quot;|\"|')? dateline=(?:&quot;|\"|')?[0-9]+(?:&quot;|\"|')?#i", '', $username);
			$delete_quote = false;
		}

		if($delete_quote)
		{
			$username = my_substr($username, 0, my_strlen($username)-1);
		}

		if($text_only)
		{
			return "\n".htmlspecialchars_uni($username)." $lang->wrote{$date}\n--\n{$message}\n--\n";
		}
		else
		{
			$span = "";
			if(!$delete_quote)
			{
				$span = "<span>{$date}</span>";
			}
			
			return "<blockquote><cite>{$span}".htmlspecialchars_uni($username)." $lang->wrote{$linkback}</cite>{$message}</blockquote>\n";
		}
	}

	/**
	* Parses code MyCode.
	*
	* @param string The message to be parsed
	* @param boolean Are we formatting as text?
	* @return string The parsed message.
	*/
	/**function mycode_parse_code($code, $text_only=false)
	{
		global $lang;

		if($text_only == true)
		{
			return "\n{$lang->code}\n--\n{$code}\n--\n";
		}

		// Clean the string before parsing.
		$code = preg_replace('#^(\t*)(\n|\r|\0|\x0B| )*#', '\\1', $code);
		$code = rtrim($code);
		$original = preg_replace('#^\t*#', '', $code);

		if(empty($original))
		{
			return;
		}

		$code = str_replace('$', '$', $code);
		$code = preg_replace('#\$([0-9])#', '\\\$\\1', $code);
		$code = str_replace('\\', '\', $code);
		$code = preg_replace('#([>\s()]|^)((http|ftp|news)\://([^/"\s<\[.]+\.)+\w+(\:[0-9]+)?(/[^"\s<\[]*)?)#i', '$1<a href="$2">$2</a>', $code);
		$code = str_replace("\t", '&nbsp;&nbsp;&nbsp;&nbsp;', $code);
		$code = str_replace("  ", '&nbsp;&nbsp;', $code);

		return "<div class=\"codeblock\">\n<div class=\"title\">".$lang->code."\n</div><div class=\"body\" dir=\"ltr\"><code>".$code."</code></div></div>\n";
	}
	**/
	function mycode_parse_cdkpu($cdkpu, $text_only=false)
	{
		global $lang;

		if($text_only == true)
		{
			return "\n{$lang->code}\n--\n{$cdkpu}\n--\n";
		}

		// Clean the string before parsing.
		$cdkpu = preg_replace('#^(\t*)(\n|\r|\0|\x0B| )*#', '\\1', $cdkpu);
		$cdkpu = rtrim($cdkpu);
		$original = preg_replace('#^\t*#', '', $cdkpu);

		if(empty($original))
		{
			return;
		}

		$cdkpu = str_replace('$', '$', $cdkpu);
		$cdkpu = preg_replace('#\$([0-9])#', '\\\$\\1', $cdkpu);
		$cdkpu = str_replace('\\', '\', $cdkpu);
		$cdkpu = preg_replace('#([>\s()]|^)((http|ftp|news)\://([^/"\s<\[.]+\.)+\w+(\:[0-9]+)?(/[^"\s<\[]*)?)#i', '$1<a href="$2">$2</a>', $cdkpu);
		$cdkpu = str_replace("\t", '&nbsp;&nbsp;&nbsp;&nbsp;', $cdkpu);
		$cdkpu = str_replace("  ", '&nbsp;&nbsp;', $cdkpu);

		return "<div class=\"codeblock\">\n<div class=\"title\">".$lang->code."\n</div><div class=\"body\" dir=\"ltr\"><cdkpu>".$cdkpu."</cdkpu></div></div>\n";
	}

	/**
	* Parses PHP code MyCode.
	*
	* @param string The message to be parsed
	* @param boolean Whether or not it should return it as pre-wrapped in a div or not.
	* @param boolean Are we formatting as text?
	* @return string The parsed message.
	*/
	function mycode_parse_php($str, $bare_return = false, $text_only = false)
	{
		global $lang;

		if($text_only == true)
		{
			return "\n{$lang->php_code}\n--\n$str\n--\n";
		}

		// Clean the string before parsing except tab spaces.
		$str = preg_replace('#^(\t*)(\n|\r|\0|\x0B| )*#', '\\1', $str);
		$str = rtrim($str);

		$original = preg_replace('#^\t*#', '', $str);

		if(empty($original))
		{
			return;
		}

		$str = str_replace('&amp;', '&', $str);
		$str = str_replace('&lt;', '<', $str);
		$str = str_replace('&gt;', '>', $str);

		// See if open and close tags are provided.
		$added_open_tag = false;
		if(!preg_match("#^\s*<\?#si", $str))
		{
			$added_open_tag = true;
			$str = "<?php \n".$str;
		}

		$added_end_tag = false;
		if(!preg_match("#\?>\s*$#si", $str))
		{
			$added_end_tag = true;
			$str = $str." \n?>";
		}

		$code = @highlight_string($str, true);

		// Do the actual replacing.
		$code = preg_replace('#<code>\s*<span style="color: \#000000">\s*#i', "<code>", $code);
		$code = preg_replace("#</span>\s*</code>#", "</code>", $code);
		$code = preg_replace("#</span>(\r\n?|\n?)</code>#", "</span></code>", $code);
		$code = str_replace("\\", '\', $code);
		$code = str_replace('$', '$', $code);
		$code = preg_replace("#&amp;\#([0-9]+);#si", "&#$1;", $code);

		if($added_open_tag)
		{
			$code = preg_replace("#<code><span style=\"color: \#([A-Z0-9]{6})\">&lt;\?php( |&nbsp;)(<br />?)#", "<code><span style=\"color: #$1\">", $code);
		}

		if($added_end_tag)
		{
			$code = str_replace("?&gt;</span></code>", "</span></code>", $code);
			// Wait a minute. It fails highlighting? Stupid highlighter.
			$code = str_replace("?&gt;</code>", "</code>", $code);
		}

		$code = preg_replace("#<span style=\"color: \#([A-Z0-9]{6})\"></span>#", "", $code);
		$code = str_replace("<code>", "<div dir=\"ltr\"><code>", $code);
		$code = str_replace("</code>", "</code></div>", $code);
		$code = preg_replace("# *$#", "", $code);

		if($bare_return)
		{
			return $code;
		}

		// Send back the code all nice and pretty
		return "<div class=\"codeblock phpcodeblock\"><div class=\"title\">$lang->php_code\n</div><div class=\"body\">".$code."</div></div>\n";
	}

Error:

Code:
Fatal error: Call to undefined method postParser::mycode_parse_code() in D:\Program Files\wamp\www\com\inc\class_parser.php on line 179


I see some other instances of $code in function mycode_parse_php

Do i need to change or duplicate them with new mycode tag?

Mainly i'm thinking of adding new custom tag code below the [code] function in inc/class_parser itself, if possible to create a new custom tag code which can be added from acp->mycode, i appreciate to have like that. but my priority is for changing in core class_parser file

You said that it worked, so what do you want more?
can we make it as addon my code? without changing the core [code] tag function in inc/class_praser, can we add additional custom mycode function below that existing [code] tag function?

function mycode_parse_code { --- }
additional custom function here   function mycode_parse_customcode { --- } with the same code functions but with that extra live links and non truncating feature without changing the main function mycode_parse_code { --- }
here, i wanna make it clear.
Thanks for the edits yumi, they are working. But my intention is, we have the related functions and code for the [code] tag in inc/class_parser and i think its not a good idea to make the edits you specified to existing [code] functions.
So, i'm thinking of duplicating the [code] related functions and code in class_parser and defining it will required function name as like function mycode_parse_customname { and same [code] tag code along with the edits you specified }
Like this we can use [code] with its default functionality and the new custom code with your extra edits added.

inc/class_parser:
function mycode_parse_code { code functions }
function mycode_parse_customname { code functions + extra edits you specified }
any update?
(12-08-2010 03:52 PM)ZiNgA BuRgA Wrote: [ -> ]For links in code tags, try this (untested), find in inc/class_parser.php:

PHP Code:
$code = str_replace('\\', '&#92;', $code);

add after:

PHP Code:
$code = preg_replace('#([>\s()]|^)((http|ftp|news)\://([^/"\s<\[.]+\.)+\w+(\:[0-9]+)?(/[^"\s<\[]*)?)#i', '$1<a href="$2">$2</a>', $code);


Sometimes, it is converting the raw links to live url links. But sometimes it is not showing the live links.

Is there any possibility to add the
if(usergroup==2)
{
$code = preg_replace('#([>\s()]|^)((http|ftp|news)\://([^/"\s<\[.]+\.)+\w+(\:[0-9]+)?(/[^"\s<\[]*)?)#i', '$1<a href="$2">$2</a>', $code);
}
Reference URL's