Archive for the ‘Programming’ Category

The Real Estate Transaction Standard (RETS)

I recently did a project for a local real estate office, and I found it nessecary to learn more about the ‘The Real Estate Transaction Standard (RETS)’ system than I ever needed to know. However, it is an interesting system that facilitates data transfer between partners in the real estate industry. It is miles ahead of the antiquated method of downloading megabytes of csv files each night in order to update the local databases – a very slow, painful and time consuming process.

The RETS system allows us to update our local real estate databases throughout the day, and each update takes only minutes to do. What’s more, it has required no human intervention since it started running over 6 months ago. It has been a set it and forget it operation, which I like very much.

I was able to script a RETS “replicator” that keeps our local databases in sync with the main MLS RETS databases, so that MLS listings appear in our local database only very shortly after entry into the main RETS systems. The replicator is written in vbscript as an .asp class, and right now is scheduled on windows task scheduler to run every couple hours thoughout the day. It utilizes the open-source ezRETS ODBC Driver available from California Association of REALTORS (C.A.R.) at http://www.crt.realtors.org/projects/rets/ezrets/

After experimenting with various methods to connect to RETS and script a solution, it was clear that ezRETS was the winner. I plan to make the RETS replicator .asp class available here soon. It has been tested and running smoothly on a Windows 2000 server for the better part of the last year.

Requirements to run the RETS replicator .asp class application are the open-source ezRETS ODBC Driver, and a realtor login to the RETS system.

Our local RETS system uses a a Rapattoni Rets MLS Server, and the script is set up with the field names for our local MLS server. Modifications to the field names will probably be needed to enable the script to talk to other servers.

I did learn that even though the RETS system is striving to institute standards in real estate data transfer, the RETS MLS systems still differ somewhat in their database setups. Field names can vary on different types of MLS servers and even within the same brands of RETS servers from different RETS MLS offices. RETS MLS systems are not databases in the traditional sense like SQL Server or MySQL, but are instead IDX systems with pointers to the records. This can be somewhat tricky for developers to deal with when starting to develop a RETS solution. This is where ezRets really helps, it converts traditional SQL statements into IDX commands that the RETS MLS system understands.

Send encrypted email using certificates in .Net

This is the .net code needed to send encrypted email using secure certificates in .net. Both VB and C# versions are provided below. There is no need to rely on third party components to do this, it is built-in to .net.

The email will then be viewable in any modern email client that supports secure certificates. But it will only be viewable if the email recipient has your certificate installed.

You may create your own private email cetificates using tools like the openssl toolkit. Email certificates are also free at most of the major ssl providers.

Also see the PHP version of encrypted emails on this site that sends encrypted emails.

In VB you must also add a reference to System.Security in your project in Visual Studio Express.

Below are the VB.NET and C# versions of the code for encrypted emails using ssl certificates:

Imports Namespace=System.IO
Imports System.Net.Mail
Imports System.Security.Cryptography.X509Certificates
Imports System.Security.Cryptography.Pkcs
 
Class EncryptEmail
 
Shared Sub Main(ByVal args() As String)
 
  SendEncryptedEmail("test.pfx", "test.cer", """Somebody"" ", """Somebody"" ", "Test VB Encrypted HTML EMail", "This email is encrypted and signed.", "localhost", 25, False)
 
End Sub
    Shared Sub SendEncryptedEmail(ByVal SigningCertPath As String, ByVal EncryptingCertPath As String, ByVal [To] As String, ByVal From As String, ByVal Subject As String, ByVal Body As String, ByVal SmtpServer As String, ByVal SmtpPort As Integer, ByVal HTML As Boolean)
 
        Dim SignCert As New X509Certificate2(SigningCertPath, "password")
        Dim EncryptCert As New X509Certificate2(EncryptingCertPath, "")
        Dim Message As New StringBuilder()
        Message.AppendLine("Content-Type: text/" + IIf(HTML, "html", "plain") + "; charset=""iso-8859-1""") 'TODO: For performance reasons this should be changed to nested IF statements
        Message.AppendLine("Content-Transfer-Encoding: 7bit")
        Message.AppendLine()
        Message.AppendLine(Body)
        Dim BodyBytes As Byte() = Encoding.ASCII.GetBytes(Message.ToString())
        Dim ECms As New EnvelopedCms(New ContentInfo(BodyBytes))
        Dim Recipient As New CmsRecipient(SubjectIdentifierType.IssuerAndSerialNumber, EncryptCert)
        ECms.Encrypt(Recipient)
        Dim EncryptedBytes As Byte() = ECms.Encode()
        Dim Cms As New SignedCms(New ContentInfo(EncryptedBytes))
        Dim Signer As New CmsSigner(SubjectIdentifierType.IssuerAndSerialNumber, SignCert)
        Cms.ComputeSignature(Signer)
        Dim SignedBytes As Byte() = Cms.Encode()
 
        Dim Msg As New MailMessage()
        Msg.To.Add(New MailAddress([To]))
        Msg.From = New MailAddress(From)
        Msg.Subject = Subject
        Dim ms As New MemoryStream(EncryptedBytes)
        Dim av As New AlternateView(ms, "application/pkcs7-mime; smime-type=signed-data;name=smime.p7m")
        Msg.AlternateViews.Add(av)
 
        Dim smtp As New SmtpClient(SmtpServer, SmtpPort)
        smtp.UseDefaultCredentials = True
        smtp.Send(Msg)
 
    End Sub
 
End Class


Below is the c# code

using System;
using System.IO;
using System.Text;
using System.Net.Mail;
using System.Security.Cryptography.Pkcs;
using System.Security.Cryptography.X509Certificates;
 
namespace EncryptedSMIME
{
   class Program
   {
       static void Main(string[] args)
       {
           SendEncryptedEmail("Cert.pfx", "thawte.cer",
               ""Noah Body" ",
               ""John Doe" ",
               "Test C# Encrypted HTML EMail",
               "This email is encrypted and signed.",
               "smtp.singingeels.com", 25, false);
       }
 
       static void SendEncryptedEmail(
           string SigningCertPath, string EncryptingCertPath,
           string To, string From, string Subject, string Body,
           string SmtpServer, int SmtpPort, bool HTML)
       {
           X509Certificate2 SignCert = new X509Certificate2(SigningCertPath, "password");
           X509Certificate2 EncryptCert = new X509Certificate2(EncryptingCertPath, "");
 
           StringBuilder Message = new StringBuilder();
           Message.AppendLine("Content-Type: text/" + ((HTML) ? "html" : "plain") +
               "; charset="iso-8859-1"");
           Message.AppendLine("Content-Transfer-Encoding: 7bit");
           Message.AppendLine();
           Message.AppendLine(Body);
 
           byte[] BodyBytes = Encoding.ASCII.GetBytes(Message.ToString());
 
           EnvelopedCms ECms = new EnvelopedCms(new ContentInfo(BodyBytes));
           CmsRecipient Recipient = new CmsRecipient(
               SubjectIdentifierType.IssuerAndSerialNumber, EncryptCert);
           ECms.Encrypt(Recipient);
           byte[] EncryptedBytes = ECms.Encode();
 
           SignedCms Cms = new SignedCms(new ContentInfo(EncryptedBytes));
           CmsSigner Signer = new CmsSigner
               (SubjectIdentifierType.IssuerAndSerialNumber, SignCert);
 
           Cms.ComputeSignature(Signer);
           byte[] SignedBytes = Cms.Encode();
 
           MailMessage Msg = new MailMessage();
           Msg.To.Add(new MailAddress(To));
           Msg.From = new MailAddress(From);
           Msg.Subject = Subject;
 
           MemoryStream ms = new MemoryStream(EncryptedBytes);
           AlternateView av = new AlternateView(ms,
               "application/pkcs7-mime; smime-type=signed-data;name=smime.p7m");
           Msg.AlternateViews.Add(av);
 
           SmtpClient smtp = new SmtpClient(SmtpServer, SmtpPort);
           smtp.UseDefaultCredentials = true;
           smtp.Send(Msg);
       }
   }
}
 


Technorati Tags:

Send encrypted email using certificates in PHP

This is the PHP code needed to send encrypted email using secure certificates in PHP. There is no need to rely on third party components to do this, it is built-in to PHP.

The email will then be viewable in any modern email client that supports secure certificates. But it will only be viewable if the recipient has your certificate installed.

You may create your own private email cetificates using tools like the openssl toolkit. Email certificates are also free at most of the major ssl providers.

Also see the .net version of encrypted emails on this site that sends encrypted and signed emails.

// Setup mail headers.
$headers = array("From" => "someone@example.com", "To" => "someone-else@example.com", "Cc" => "spam@somewhere.org", "Subject" => "Encrypted mail readable with most clients", "X-Mailer" => "PHP/".phpversion());
 
// Get the public key certificate.
$pubkey = file_get_contents("C:\test.cer");
 
// Remove some double headers for mail()
$headers_msg = $headers;
unset($headers_msg['To'], $headers_msg['Subject']);
 
$data = <<
 
This email is Encrypted!
 
You must have my certificate to view this email!
 
Me
EOD;
 
//write msg to disk
$fp = fopen("C:\msg.txt", "w");
fwrite($fp, $data);
fclose($fp);
 
// Encrypt message
openssl_pkcs7_encrypt("C:\msg.txt","C:\enc.txt",$pubkey,$headers_msg,PKCS7_TEXT,1);
 
// Seperate headers and body for mail()
$data = file_get_contents("C:\Inetpub\wwwroot\WIP\PHPEncrypt\enc.txt");
$parts = explode("\n\n", $data, 2);
 
// Send mail
mail($headers['To'], $headers['Subject'], $parts[1], $parts[0]);
 
// Remove encrypted message (not fot debugging)
//unlink("msg.txt");
//unlink("enc.txt");
 
?>

   Technorati Tags: 

Google store locator – Part 2

I wanted to try something different with the original Google store locator. I decided I wanted to just show a listing of the stores without a map. This is what you see on most sites that have store locators. I also wanted to add the Smarty template system to it. Here are the results. Now only the Google geocoding service is used to get the coordinates of the zip code entered, and is utilzed once during the setup of the locations to retrieve the coordinates. Now we could also add an internal database of zip code coordinates for the entire United States and it would be a completely self contained application, unless, of course, we also wanted to provide a link to a Google map.

Download this demo project.

Enter your zip code to locate your nearest Cyberstarweb!

Google store locator

This is a demo of a store locator I put together pretty quick using the Google maps api and the examples. It utilizes Ajax, PHP and a MySql database. As a demo it works well, although it’s not completely polished, lacking error handling. Download this demo project.

Also see Google store locator Part 2.

On a related note, the Geocoder displays the latitude and longitude of any address.

Enter your zip code to find your nearest Cyberstarweb!

Geocoder – find your latitude and longitude

Here is a quick way to find your latitude and longitude. It is a PHP script using the Google geocoding service. The Google Store Locator demo on this site utilizes the latitude and longitude from such a request to locate nearby Cyberstarweb locations. With this script as a start, you could build an entire site like geocoder.us


Here is the PHP source code of the geocoder.php

 

php

   set_error_handler('errorHandler');

   function errorHandler ($errno, $errstr, $errfile, $errline, $errcontext)
{
	//suppress all errors
}

   if ($_POST['address'] != "") {
   		doGeo(urlencode($_POST['address']));
   		}

   function doGeo($marker) {
   // Your Google Maps API key
   $key = "Your key here";

   // Desired address
   $address = "http://maps.google.com/maps/geo?q=$marker&output=xml&key=$key";

   // Retrieve the URL contents
   $page = file_get_contents($address);

   // Parse the returned XML file
   $xml = new SimpleXMLElement($page);

// Retrieve the desired XML node
//   echo $xml->Response->Placemark->Point->coordinates;

   // Parse the coordinate string
   list($longitude, $latitude, $altitude) = explode(",",
        $xml->Response->Placemark->Point->coordinates);

   // Output the coordinates
	echo "

".urldecode($marker)."


Longitude: $longitude, Latitude: $latitude

";
}
?>