<?
/*
HTTP translator from DynDNS to EveryDNS dynamic server. Version 0.1.
Copyright (c) 2007, Alessio Orlandi
All rights reserved.

Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:

- Redistributions of source code must retain the above copyright notice, this
list of conditions and the following disclaimer.
- Redistributions in binary form must reproduce the above copyright notice,
this list of conditions and the following disclaimer in the documentation
and/or other materials provided with the distribution.
- The name of Alessio Orlandi may not be used to endorse or promote products
derived from this software without specific prior written permission.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY
DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/

ini_set('default_socket_timeout',    120);    
$users = array(
  
PUT_ANY_HTTP_USERNAME_HERE =>  array (
     
"password" => RESULT_OF_CRYPT_OF_YOUR_PASSWORD_HERE,
     
"domain" =>  THE_EVERYDNS_DOMAIN,
      
/* The following two are optional. If they are not set,
         the original username and password are forwarded to the everydns 
         service. */
     
"outuser" => THE_EVERYDNS_USERNAME
     
"outpass"=> THE_EVERYDNS_PASSWORD,
  ));

if(!isset(
$_SERVER['PHP_AUTH_USER']))
{
  
header("WWW-Authenticate: Basic realm=\"dyndns\"");
  
header("HTTP/1.0 401 Unauthorized");
  echo(
"Hi.");
  exit;
}


$in_u $_SERVER['PHP_AUTH_USER'] ;
$in_p $_SERVER['PHP_AUTH_PW'];

$data $users[$in_u];

if (!isset(
$data) || strcmp($data['password'],
    
crypt($in_p,$data['password']))) {
   
header("WWW-Authenticate: Basic realm=\"dyndns\"");
   
header("HTTP/1.0 401 Unauthorized");
   echo (
"The password you entered is wrong.");
   exit;
}

if(!
array_key_exists("myip"$_GET))
  
$myip $_SERVER['REMOTE_ADDR'];
else
  
$myip $_GET['myip'];

$oldip gethostbyname($data['domain']);

$url sprintf("http://%s:%s@dyn.everydns.net/index.php?ver=0.1&ip=%s&domain=%s",
    isset(
$data['outuser']) ? $data['outuser'] : $in_u
        isset(
$data['outpass']) ? $data['outpass'] : $in_p
        
$myip$data['domain']);


$v = array();
preg_match'/Exit code: (\d+)/i' , ($z=file_get_contents($url)), $v);

switch(
$v[1])
{
  case 
"0"// OK!
    
echo("good $myip"); 
    break;
  case 
"2"// Bad authentication
    
echo("badauth");
    break;
  case 
"3"// Version check fail - should never happen.
    
echo ("nohost"); 
    break;
  case 
"5"// Too fast!
    
echo("nochg $oldip");
    break;
  case 
"7":
    echo(
"nohost");
    break;

  default:
    echo(
"nohost"); // Disaster!
}

?>