Recent Topics

1 Mar 06, 2007 08:49    

My b2evolution Version: 0.9.x

How can I add a locale flag to visitor comments based on their IP?

2 Mar 06, 2007 23:04

I've made some progress on this, but I'm pulling my hair out by now. I've got to expose my ignorance and show what I've done so far.

First of all, I managed to get a little function working which returns the two-letter country code from the IP. I put it in the _class_comment.php file.

function GetCountryAbb( ){
        $lines = file('http://api.hostip.info/get_html.php?ip='.$this->author_ip);
        echo substr($lines[0], -4, 2);
    }


Now I want to use this to return an "ip_flag" (locale_flag with "locale" replaced by "GetCountryAbb"). I only changed the first few lines, and put $GetCountryAbb = 'us' because "default" didn't give me enough information.

function ip_flag( $GetCountryAbb = '', $collection = 'w16px', $class = 'flag', $align = '', $disp = true, $absoluteurl = true )
{
	global $core_dirout, $img_subdir, $img_url;

	if( empty($GetCountryAbb) ) $GetCountryAbb = 'us';

	// extract flag name:
	$country = strtolower($GetCountryAbb);

	if( ! is_file(dirname(__FILE__).'/'.$core_dirout.'/'.$img_subdir.'/flags/'.$collection.'/'.$country.'.gif') )
	{ // File does not exist
		$country = 'default';
	}

	if( $absoluteurl !== true )
	{
		$iurl = $absoluteurl;
	}
	else
	{
		$iurl = $img_url.'/flags';
	}

	$r = '<img src="'.$iurl.'/'.$collection.'/'.$country.'.gif" alt="flag"';
	if( !empty( $class ) ) $r .= ' class="'.$class.'"';
	if( !empty( $align ) ) $r .= ' align="'.$align.'"';
	$r .= ' /> ';

	if( $disp )
		echo $r;   // echo it
	else
		return $r; // return it
}


Then, in the skin's _feedback.php, I have this:

<!-- ========== START of a COMMENT/TB/PB ========== -->
		<?php $Comment->anchor() ?>
		<div class="bComment">
			<div class="bCommentTitle">
<?php 
$Comment->author_location('<div class="bSmallHead">', '</div>' ); 
?>
    
<A HREF="http://www.hostip.info">
 <IMG SRC="http://api.hostip.info/flag.php?ip=<?php $Comment->author_ip() ?>" ALT="IP Address Lookup"></A> 

			<?php
				switch( $Comment->get( 'type' ) )
				{
					case 'comment': // Display a comment: 
						echo T_('Comment from: '); 
						$Comment->author('', '', '', '', 'htmlbody', true); 
						$Comment->GetCountryAbb();
				ip_flag( $Comment->GetCountryAbb );
?>


The author_location is simply a text field I added to the comments. The flag image from hostip.info works. The GetCountryAbb shows up as it should, as two-letter text. I have a real problem with the last line, which I have tried a million different ways. I really thought would work as shown here, but I get an "undefined function" error.

Here's the link
http://moonchild.ch/blog/index.php?blog=14
But don't be surprised if it's showing weird stuff while I work on it.

Any suggestions would be greatly appreciated! I'm supposed to be having fun, but I'm just getting aggravated!

3 Mar 11, 2007 01:31

I finally managed it. I had to learn something. &.

I changed the function name from GetCountryAbb to country_abb. Here's what I ended up with in the _feedback.php file:

				echo $Comment->GetCity();
				echo $Comment->GetState();
				$country_abb = &$Comment->country_abb();
				$Comment->ip_flag($country_abb);


Please come by for a test drive! I need something to replace my sample comments. = P
[url=http://moonchild.ch/blog/index.php?blog=14]The Question[/url]

4 Mar 11, 2007 17:33

Thanks, guys, for the test comments.

The 3rd-party DB lookup is easiest for now, but I may change it later. It might at least be useful to insert the info retrieved into the user table. The flags are NOT 3rd-party; they're the locale flags. As it is now, if the hostip.com server is down, I guess I'll just get a bunch of default flags. Not a big deal for me, since it's a personal web page and nobody visits me anyway. = <

And, yes, the Swiss flag should be smaller. I made it that way to meet the b2evolution locale submission requirements. (I even made half-and-half flags for Swiss-German, Swiss-French and Swiss-Italian.) But since it apparently wasn't accepted, I'll change it somewhen. It's only a C-priority on my list of things to do.


Form is loading...