class_sbdns.php

Author: Troy Wolf (troy@troywolf.com)
Modified Date: 2006-03-23
Download: class_sbdns.zip or class_sbdns.tar.gz
View class source: class_sbdns.php source

What is it?

class_sbdns.php is a PHP class that provides an API that automates 100% of the Server Beach DNS Tool functionality. It is for Server Beach customers only. Server Beach allows customers to use the Server Beach name servers. They provide a browser-based DNS Tool for customers to add domains, zone records, etc. This is great, but what many customers want is a way to programmatically manage their DNS zones. Since Server Beach does not provide an API currently, this class is the answer.

With simple, one-line method calls, You can use this class in your scripts to do the following:

One use of this class is to automatically add new domains to the DNS when customers signup for your webhosting. In addition to being an excellent server-side web scripting language, PHP is also a powerful shell scripting language. So you can use this class in a web application or a shell script.

This class uses my popular class_http--a powerful and easy to use "screen-scraping" class. You can learn more about class_http by checking out the class_http documentation.

A word of caution

This class has code that alters Server Beach DNS records. This is not something you should do lightly. You should understand fully what you are doing before using this class. I have taken many precautions to make the code easy and safe to use. Where possible, the class validates the data you pass to the methods to ensure you don't mess something up. That said, I'm not reponsible for your use of any of my code or anything that happens as a result of you using my code.

How to use it

You need to download the class. Choose your preferred archive format: zip or tar.gz
Then you need to edit class_sbdns.php. There are 3 things you need to edit.
  1. Modify the path to the class_http.php include to match your environment
  2. In the class constructor (sbdns() funtion), modify the auth_email value to be your SB email address.
  3. In the class constructor (sbdns() funtion), modify the auth_pass value to be your SB password.

Let's cut to the chase. Code examples are what you need. Here is an example to add a new domain to the Server Beach DNS with Server Beach as the DNS master.

Add a new domain with SB as MASTER

<?php

// Include the class.
require_once("class_sbdns.php");

// Instantiate the sbdns object.
$sbdns = new sbdns();

// Login to Server Beach.
if (!$sbdns->login()) { die($sbdns->status_msg); }

// Add new domain with Server Beach as both master and slave.
if (!$sbdns->add_domain("mynewdomain.com", "192.168.0.12", true)) {
    die(
$sbdns->status_msg);
}

echo
$sbdns->status_msg;

?>
return to top

Yes, that really is it. The class does all the heavy lifting. To add a domain with SB serving only as slave DNS, the only difference is the third parameter of the add_domain() method is set to false.

Note that when setting SB as master (third parameter = true), the second parameter is the IP address of the server where the domain is hosted. When setting SB as slave (third parameter = false), the second parameter is the IP address of your master DNS server.

Add a new domain with SB as SLAVE

<?php

// Include the class.
require_once("class_sbdns.php");

// Instantiate the sbdns object.
$sbdns = new sbdns();

// Login to Server Beach.
if (!$sbdns->login()) { die($sbdns->status_msg); }

// Add new domain with Server Beach as slave only.
if (!$sbdns->add_domain("mynewdomain.com", "192.168.0.12", false)) {
    die(
$sbdns->status_msg);
}

echo
$sbdns->status_msg;

?>
return to top

All the other methods of the class require you to first set a domain to work with. This is the same as clicking the "GO" link next to a domain in the SB DNS Tool.

Select an existing domain to work with

<?php

// Include the class.
require_once("class_sbdns.php");

// Instantiate the sbdns object.
$sbdns = new sbdns();

// Login to Server Beach.
if (!$sbdns->login()) { die($sbdns->status_msg); }

// Set the domain to work with.
if (!$sbdns->set_domain("oneofmyexistingdomains.com")) {
    die(
$sbdns->status_msg);
}

echo
$sbdns->status_msg;

?>
return to top

Now that you know how to set the domain, let's see how to add zone records. There are three different ways to create your A and CNAME records. The four examples below show the three variations. Choose the right one for your specific task.

Add 'A' and 'CNAME' zone records

<?php

// Include the class.
require_once("class_sbdns.php");

// Instantiate the sbdns object.
$sbdns = new sbdns();

// Login to Server Beach.
if (!$sbdns->login()) { die($sbdns->status_msg); }

// Set the domain to work with.
if (!$sbdns->set_domain("anexistingdomain.com")) {
    die(
$sbdns->status_msg);
}

// 1. Add a CNAME record for the domain.
// test1 will be a CNAME for anexistingdomain.com.
if (!$sbdns->add_record("test1")) {
    die(
$sbdns->status_msg);
}

// 2. Add an A record to the zone.
// An A record will be created because you are passing an IP address
// as the 2nd parameter.
// test2 will be an A record for the anexistingdomain.com zone.
if (!$sbdns->add_record("test2", "192.168.1.47")) {
    die(
$sbdns->status_msg);
}

// 3. Add a CNAME record to a subdomain of the domain.
// test3 will be a CNAME for test2.anexistingdomain.com.
if (!$sbdns->add_record("test3", "test2.anexistingdomain.com")) {
    die(
$sbdns->status_msg);
}

// 4. Add a CNAME record to an external subdomain or domain.
// This is really the same thing as #3 above, but since the SB DNS Tool
// distinguishes between these two situations, I show an example of each.
// test4 will be a CNAME for www.google.com.
if (!$sbdns->add_record("test4", "www.google.com")) {
    die(
$sbdns->status_msg);
}

echo
$sbdns->status_msg;

?>
return to top

It is not necessary to add an MX record for your root domain because if an MX record does not exist, the A record will be used. So if your domain name is mydomain.com, you do not need to create an MX record for mydomain.com unless you host your mail on a server other than the web server. In which case, you would pass "mydomain.com" as the first parameter of the add_mail_exchanger() method below. If the owner is a subdomain instead of the root domain, then you would simply pass the subdomain name as the first parameter. For example, if bob's email address is "bob@home.mydomain.com", you should pass "home" as the first parameter (owner).

Add a Mail Exchanger

<?php

// Include the class.
require_once("class_sbdns.php");

// Instantiate the sbdns object.
$sbdns = new sbdns();

// Login to Server Beach.
if (!$sbdns->login()) { die($sbdns->status_msg); }

// Set the domain to work with.
if (!$sbdns->set_domain("anexistingdomain.com")) {
    die(
$sbdns->status_msg);
}

// Set a Mail Exchanger (MX record) for a domain.
// First parameter is the mail domain (the part after @ in the email address).
// This can be any A record in the current domain zone. The class enforces this.
// Second parameter is the name of the mail server. (NOT an IP address)
// Third parameter is the preference number. Not required, defaults to 10.
if (!$sbdns->add_mail_exchanger("anexistingdomain.com", "mail.someserver.com", 10)) {
    die(
$sbdns->status_msg);
}

echo
$sbdns->status_msg;

?>
return to top

Delete a domain zone completely. Of course make sure you really don't want the domain zone anymore!

Delete a domain zone

<?php

// Include the class.
require_once("class_sbdns.php");

// Instantiate the sbdns object.
$sbdns = new sbdns();

// Login to Server Beach.
if (!$sbdns->login()) { die($sbdns->status_msg); }

// Set the domain to work with.
if (!$sbdns->set_domain("anexistingdomain.com")) {
    die(
$sbdns->status_msg);
}

// Delete the currently selected domain.
if (!$sbdns->delete_domain()) {
    die(
$sbdns->status_msg);
}

echo
$sbdns->status_msg;

?>
return to top

Modify an existing A or CNAME record

<?php

// Include the class.
require_once("class_sbdns.php");

// Instantiate the sbdns object.
$sbdns = new sbdns();

// Login to Server Beach.
if (!$sbdns->login()) { die($sbdns->status_msg); }

// Set the domain to work with.
if (!$sbdns->set_domain("anexistingdomain.com")) {
    die(
$sbdns->status_msg);
}

// First parameter is the subdomain name. Do not append main domain name.
// Second parameter is the target. Same rules apply as the add_record() method.
// Note that you cannot change an A record to a CNAME, but you can change a
// CNAME to an A.
if (!$sbdns->update_record("test1", "192.168.1.45")) {
    die(
$sbdns->status_msg);
}

echo
$sbdns->status_msg;

?>
return to top

Modify an existing Mail Exchanger (MX) record

<?php

// Include the class.
require_once("class_sbdns.php");

// Instantiate the sbdns object.
$sbdns = new sbdns();

// Login to Server Beach.
if (!$sbdns->login()) { die($sbdns->status_msg); }

// Set the domain to work with.
if (!$sbdns->set_domain("anexistingdomain.com")) {
    die(
$sbdns->status_msg);
}

// Modify an existing Mail Exchanger (MX) record.
// First parameter is the mail domain (the part after @ in the email address).
// This can be any A record in the current domain zone. The class enforces this.
// Second parameter is the name of the mail server. (NOT an IP address)
// Third parameter is the preference number. Not required, defaults to 10.
if (!$sbdns->update_mail_exchanger("anexistingdomain.com", "mail2.anexistingdomain.com", 10)) {
    die(
$sbdns->status_msg);
}

echo
$sbdns->status_msg;

?>
return to top

Delete an existing A or CNAME record

<?php

// Include the class.
require_once("class_sbdns.php");

// Instantiate the sbdns object.
$sbdns = new sbdns();

// Login to Server Beach.
if (!$sbdns->login()) { die($sbdns->status_msg); }

// Set the domain to work with.
if (!$sbdns->set_domain("anexistingdomain.com")) {
    die(
$sbdns->status_msg);
}

// Delete an existing A or CNAME record.
if (!$sbdns->delete_record("test1")) {
    die(
$sbdns->status_msg);
}

echo
$sbdns->status_msg;

?>
return to top

Delete an existing Mail Exchanger (MX) record

<?php

// Include the class.
require_once("class_sbdns.php");

// Instantiate the sbdns object.
$sbdns = new sbdns();

// Login to Server Beach.
if (!$sbdns->login()) { die($sbdns->status_msg); }

// Set the domain to work with.
if (!$sbdns->set_domain("anexistingdomain.com")) {
    die(
$sbdns->status_msg);
}

// Delete an existing Mail Exchanger (MX) record.
if (!$sbdns->delete_mail_exchanger("anexistingdomain.com")) {
    die(
$sbdns->status_msg);
}

echo
$sbdns->status_msg;

?>
return to top

Change the hostmaster email address

<?php

// Include the class.
require_once("class_sbdns.php");

// Instantiate the sbdns object.
$sbdns = new sbdns();

// Login to Server Beach.
if (!$sbdns->login()) { die($sbdns->status_msg); }

// Set the domain to work with.
if (!$sbdns->set_domain("anexistingdomain.com")) {
    die(
$sbdns->status_msg);
}

// Change the hostmaster email address.
if (!$sbdns->change_hostmaster_email("webmaster@mydomain.com")) {
    die(
$sbdns->status_msg);
}

echo
$sbdns->status_msg;

?>
return to top

There are 2 other methods that you may find useful.

Retrieve the zone text file

<?php

// Include the class.
require_once("class_sbdns.php");

// Instantiate the sbdns object.
$sbdns = new sbdns();

// Login to Server Beach.
if (!$sbdns->login()) { die($sbdns->status_msg); }

// Set the domain to work with.
if (!$sbdns->set_domain("anexistingdomain.com")) {
    die(
$sbdns->status_msg);
}

// Display the zone file text.
if (!$zone_text = $sbdns->get_zone_file()) {
    die(
$sbdns->status_msg);
}
echo
"<pre>".$zone_text."</pre>";

?>
return to top

The get_zone_records() method creates an array of all the zone's records. To see the structure of this array, just print out the array as shown in the example below.

Retrieve an array of all the records for a domain zone

<?php

// Include the class.
require_once("class_sbdns.php");

// Instantiate the sbdns object.
$sbdns = new sbdns();

// Login to Server Beach.
if (!$sbdns->login()) { die($sbdns->status_msg); }

// Set the domain to work with.
if (!$sbdns->set_domain("anexistingdomain.com")) {
    die(
$sbdns->status_msg);
}

// Retrieve an array containing all the zone's records.
if (!$sbdns->get_zone_records()) {
    die(
$sbdns->status_msg);
}
echo
"<pre>";
print_r($sbdns->records);
echo
"</pre>";

?>
return to top

If you want to know more about any of the methods and their parameters, simply read the source code. It is heavily commented.

The hope is that Server Beach will make this class obsolete by providing the customers with a real API to the DNS. But until then, this is a sweet work-around.


About the author

Troy Wolf is the author of SnippetEdit--a PHP application providing browser-based website editing that even non-technical people can use. Website editing as easy as it gets. Troy has been a professional Internet and database application developer for over 12 years. He has many years' experience with ASP, VBScript, PHP, Javascript, DHTML, CSS, SQL, and XML on Windows and Linux platforms. Check out Troy's Code Library.