www.kevincornwell.com

Programming



Break into Debugger when Exception is thrown

Break into Debugger when Exception is thrown even if you are using try catch statements.

From Visual Studio, hit CTRL + ALT + E. Then check the “Thrown attribute of the Common Language Runtime Exceptions.

untitled.GIF


Post Comment Now


Maxmind and Google Maps Mashup [php]

I was bored today so I created a very simple and fast php script to plot the location for a given IP and display other IP information (such as lattitude, longitude, Country, State, Area code, ISP, etc.) on a Google Map using the Maxmind web service and the Google Maps API.

maxmind_google_maps_mashup.GIF

The link in the balloon points domaintools.com and does a whois lookup on the IP for detailed information on the ISP (internet service provider).

Here is a working demo (hard coded IP).


In order for this script to work on your site, you will need a MaxMind GeoIP City/ISP/Organization Web Service Key (50,000 lookups for $20.00) and a Google Maps Key (free). There are other IP to Lat/Long Web services out there but Maxmind is by far the most accurate (none are 100% accurate).

To render the location of an IP on the map, send the IP via the query string such as…

http://www.yourDomain.com/MaxmindGoogleMap/index.php?ip=98.195.83.50


Here is the script. Copy the contents to a .php file and edit the 2 keys. That’s it!

<?php
        //////////////////////////////////////////////////////////////////////////////////////
        ///////////////  Maxmind/Google Maps Mashup by Kevin Cornwell ///////////////
        ///////////////                       Version 1.0                             ///////////////
        ///////////////          http://www.kevincornwell.com                ///////////////
        /////////////////////////////////////////////////////////////////////////////////////
 
 
 
	// EDIT THE NEXT 2 KEYS
 
	$maxmind_license_key  = "foo";    // Replace [foo] with your key.
	$google_maps_key      = "bar";    // Replace [bar] with your key.
 
 
	$zoom                 = 4;          // Default zoom level.
	$lat                  = 39.232;     // Center on this location by default when no IP is passed.
	$long                 = -95.800;    // Center on this location by default when no IP is passed.
	$ip                   = "";
	$message              = "Hello World";   // Default message when no IP is passed.
 
 
	if (isset($_REQUEST['ip']))
	{
		$ip = $_REQUEST['ip'];
 
		$r = maxmind_lookup($ip);
 
		$message = "<table cellspacing=0><tr><td align='right'><b>IP:</b> &nbsp;</td><td><a href='http://whois.domaintools.com/{$ip}'>{$ip}</a></td></tr><tr><td align='right'><b>Country:</b> &nbsp;</td><td>{$r['country']}</td></tr><tr><td align='right'><b>Region:</b> &nbsp;</td><td>{$r['region']}</td></tr><tr><td align='right'><b>City:</b> &nbsp;</td><td>{$r['city']}</td></tr><tr><td align='right'><b>Zip:</b> &nbsp;</td><td>{$r['zip']}</td></tr><tr><td align='right'><b>Lat:</b> &nbsp;</td><td>{$r['lat']}</td></tr><tr><td align='right'><b>Long:</b> &nbsp;</td><td>{$r['long']}</td></tr><tr><td align='right'><b>Metro:</b> &nbsp;</td><td>{$r['metro_code']}</td></tr><tr><td align='right'><b>Area Code:</b> &nbsp;</td><td>{$r['area_code']}</td></tr><tr><td align='right'><b>ISP:</b> &nbsp;</td><td>{$r['isp']}</td></tr><tr><td align='right'><b>Org:</b> &nbsp;</td><td>{$r['org']}</td></tr></table>";
 
		$lat = $r['lat'];
		$long = $r['long'];
	}
 
 
	function maxmind_lookup($ipaddress){
 
		global $maxmind_license_key;
 
		// Returns:ISO 3166 Two-letter Country Code, Region Code, City, Postal Code, 
		// Latitude, Longitude, Metropolitan Code, Area Code, ISP, Organization.
 
 
		$query = "http://maxmind.com:8010/f?l=" . $maxmind_license_key . "&i=" . $ipaddress;
		$url = parse_url($query);
		$host = $url["host"];
		$path = $url["path"] . "?" . $url["query"];
		$timeout = 1;
		$fp = fsockopen ($host, 8010, $errno, $errstr, $timeout)
		or die('Can not open connection to maxmind server.');
		$buf = "";
 
 
		if ($fp) {
			fputs ($fp, "GET $path HTTP/1.0\nHost: " . $host . "\n\n");
			while (!feof($fp)) {
				$buf .= fgets($fp, 128);
			}
			$lines = split("\n", $buf);
			$data = $lines[count($lines)-1];
			fclose($fp);
 
			$arrMaxmind = split(",", $data);
			$arrMaxmind = str_replace("\"", "", $arrMaxmind);
			$arrMaxmind = str_replace(",", " ", $arrMaxmind);
 
			$r['country'] 		= $arrMaxmind[0];
			$r['region'] 		= $arrMaxmind[1];
			$r['city'] 		= $arrMaxmind[2];
			$r['zip'] 		= $arrMaxmind[3];
			$r['lat'] 		= $arrMaxmind[4];
			$r['long'] 		= $arrMaxmind[5];
			$r['metro_code'] 	= $arrMaxmind[6];
			$r['area_code'] 	= $arrMaxmind[7];
			$r['isp'] 		= $arrMaxmind[8];
			$r['org'] 		= $arrMaxmind[9];
 
		} else {
			return false;
		}
		return $r;
	}		
 
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
  <head>
    <meta http-equiv="content-type" content="text/html; charset=utf-8"/>
    <title><?php echo $ip ?></title>
    <script src="http://maps.google.com/maps?file=api&amp;v=2&amp;key=<?php echo $google_maps_key ?>"
      type="text/javascript"></script>
    <script type="text/javascript">
 
    //<![CDATA[
 
	function load() 
	{
		if (GBrowserIsCompatible()) 
		{
			var map = new GMap2(document.getElementById("map"));
			map.addControl(new GLargeMapControl());
			map.addControl(new GMapTypeControl());
			map.setCenter(new GLatLng(<?php echo $lat ?>, <?php echo $long ?>), <?php echo $zoom ?>);
			var marker = new GMarker(new GLatLng(<?php echo $lat ?>, <?php echo $long ?>));
			marker.html = "<?php echo $message ?>";
			GEvent.addListener(marker, 'click', function() {marker.openInfoWindowHtml(marker.html); });
			map.addOverlay(marker);
			marker.openInfoWindowHtml(marker.html);
		}
	}
 
    //]]>
    </script>
  </head>
  <body onload="load()" onunload="GUnload()">
    <div id="map" style="width:800px;height:600px"></div>
  </body>
</html>

If you liked my script, post a thanks below.
:)


Post Comment Now


Workaround For SharePoint Group Email Limits

This entry has been moved to mySharePointBlog.com. Go Here.


Comments Off


ASP.NET - Test NTFS Directory Access
    protected void Page_Load(object sender, EventArgs e)
    {
 
        // Show admin link if they have access to the admin directory. //
 
        hyperLinkAdmin.Visible = hasAccessToDir("admin");
    }
 
    protected bool hasAccessToDir(string dir)
    {
        return System.IO.Directory.Exists(MapPath(dir));
    }

Post Comment Now


SharePoint Elevated Security

This post has been moved to mySharePointBlog. Go Here.


Comments Off


Create Active Directory User In .NET

C#

    public string CreateADUser(string szUsername, string szDisplayName, string szFirstName, string szLastName, string szMiddleInitial, string szPhone, string szEmail, string szOrgUnit)
    {
        string szPassword = "fooBar_123";  //temp only.
        string szCNName = "CN=" + szUsername;
 
        try
        {
            DirectoryEntry objContainer = new DirectoryEntry("LDAP://OU=mySubOU,OU=myMainOU,DC=KEVINCORNWELL,DC=com");
            DirectoryEntry objUser = objContainer.Children.Add(szCNName.ToLower(), "user");
            objUser.Properties["sAMAccountName"].Value = szUsername;
            objUser.Properties["userPrincipalName"].Value = szUsername + "@KEVINCORNWELL.COM";
            objUser.Properties["displayName"].Value = szDisplayName;
            objUser.Properties["GivenName"].Value = szFirstName;
            objUser.Properties["sn"].Value = szLastName;
            objUser.Properties["Initials"].Value = szMiddleInitial;
            objUser.Properties["mail"].Value = szEmail;
            objUser.Properties["TelephoneNumber"].Value = szPhone;
            objUser.Properties["Description"].Value = szOrgUnit;
            objUser.CommitChanges();
            objUser.Invoke("SetPassword", szPassword);
            objUser.CommitChanges();
            int flags = (int)objUser.Properties["userAccountControl"].Value;
            objUser.Properties["userAccountControl"].Value = flags & ~0x2;  //Enable User Account
            objUser.Properties["pwdLastSet"].Value = 0;
            objUser.CommitChanges();
            return "";
        }
        catch (Exception ex)
        {
            return ex.InnerException + ex.Message + ex.Source + ex.StackTrace + ex.Data;
        }
    }

Post Comment Now


SharePoint WebPart Dev Tip

This post has been moved to mySharePointBlog. Go Here.


Comments Off


SQL Server - How to combine 2 date and time columns into 1 column.

Sample columns

date_col:

2007-05-06 00:00:00.000

time_col:

1971-01-01 01:06:23.000

Use this…

DATEADD(hh ,DATEPART(hh, [time_col]]), DATEADD(n ,DATEPART(n, [time_col]) , DATEADD(ss ,DATEPART(ss, [time_col]) , [date_col])))

To get this…

2007-05-06 01:06:23.000

Post Comment Now


How to use the PeoplePicker in SharePoint

This post has been moved to mySharePointBlog. Go Here.


Post Comment Now


Taskbar Shuffle

Here is a sweet little app that allows you to shuffle your taskbar items. Go HERE


Post Comment Now


Blog | Contact | Gallery | Links | Music | Sandbox | Search | SharePoint | Social Networking | Software | Weather | Web Design


Copyright © 1997-2008 KCSH. All rights reserved.