<?


/*
 * Joshua Anderson
 * 7/28/06
 * FreeDNS.afraid.org - Advanced intelligent update client example
 */

# Command to suck in the contents of the page containing the
# current IP address
$cmd "wget -O - --user=USERNAME --password=PASSWORD \"http://192.168.0.1/cgi-bin/webcm?getpage=..%2Fhtml%2Findex_real.html&var%3Aconname=connection0&var%3Acontype=pppoa\" 2>/dev/null";

# Line to look for containing IP address in my case:
# document.getElementById("WAN_IPaddress").innerHTML = "0.0.0.0";

# Regex to suck in isolated IP address value
$patt "/.*document\.getElementById\(\"WAN_IPaddress\"\)\.innerHTML = \"(.*)\";.*/i";


# URL to update if change is detected
$updateurl "http://freedns.afraid.org/dynamic/update.php?YOUR-CUSTOM-UPDATE-URL-HERE";


# begin infinate loop
while (1) {


    
$fh popen($cmd'r');
    unset(
$ip);
    while (!
feof($fh)) {
        
$buff fgets($fh);

        if (
preg_match($patt$buff)) {
            
$ip rtrim(preg_replace($patt"$1"$buff));
        }
    }
    
pclose($fh);

    if (!(
strlen($ip)>0)) {
        print 
"WARNING: $ip is 0 bytes in length, retrying...\n";
        
sleep (5);
        continue;
    }

    if (!
is_ip($ip)) {
        print 
"WARNING: $ip is not a valid IP, retrying...\n";
        
sleep (5);
        continue;
    }


    if (
$last_ip != $ip) {
        
$date date('Y-m-d H:i:s');

        print 
"$date - IP has changed from $last_ip to $ip.\n";
        print 
"Grabbing url $updateurl...\n";
        
$remote file($updateurl);
        
$results rtrim(implode($remote""));
        print 
"$results\n";
        if (
eregi(str_replace('.''\.'$ip), $results)) {
            
$last_ip $ip;
            print 
"Success.\n";
        } else {
            print 
"No Success.\n";
        }


##### Updates the hardcoded IP address in proftpd.conf - leaving for demonstration purposes #####################
#
#   I needed a way to update the IP in proftpd.conf to work behind a NAT device
#   So using a template conf, this PHP code replaces the string %%%ip_goes_here%%% with actual IP, and reloads
#   the proftpd daemon
#
#         $file = join("", file('/usr/local/etc/proftpd.conf.template'));
#         $file = str_replace('%%%ip_goes_here%%%', $ip, $file);

#         $fh = fopen('/usr/local/etc/proftpd.conf', 'w');
#         fwrite($fh, $file);
#         fclose($fh);

#         shell_exec("killall proftpd");
#         shell_exec("/usr/local/sbin/proftpd");
#
#################################################################################################################

    
} else {
# Left in here for testing/debugging purposes
#         print "IP $ip has not changed.\n";
    
}


    
# Start over again, every 5 seconds
    
sleep (5);
}




function 
is_ip ($ip) {

    if(
eregi("^([01]?[0-9][0-9]?|2[0-4][0-9]|25[0-5])\.([01]?[0-9][0-9]?|2[0-4][0-9]|25[0-5])\.([01]?[0-9][0-9]?|2[0-4][0-9]|25[0-5])\.([01]?[0-9][0-9]?|2[0-4][0-9]|25[0-5])$"$ip)) {
        
$is_ip 1;
    } else {
        
$is_ip 0;
    }

    return(
$is_ip);

}


?>