/ Published in: PHP
PHP class to restrict a script to only run on a set ip address. This takes into account a static hostname (via one of the freely available such as dyndns.org) and a travelling ip. See the description and instructions for more information.
Expand |
Embed | Plain Text
<?php //NOTE: YOU SHOULD HAVE A SESSION RUNNING BEFORE CALLING THIS AS A CLASS /*-+------------------------------------------------------- |/ Hostname Check | Version 2 | Created: lasavior - 5/22/2011, So-NIK.com | Last Modified: 6/3/2011 | | DESCRIPTION: | "Limit access of any script to certain IP addresses." | | For example: i use this script in combination with | dyndns.org. I have my residence setup on a static | hostname (example.dyndns.org) that way i can always | access the script from my place of business. I also | have a traveling IP so i can still gain access to | the script while im away. I update the database with | the traveling IP and revoke it when i leave. | | LEGAL: | This program is free software; you can redistribute | it and/or modify it under the terms of the GNU | General Public License as published by the Free | Software Foundation; either version 2 of the License, | or (at your option) any later version. | | This program is distributed in the hope that it | will be useful, but WITHOUT ANY WARRANTY; without | even the implied warranty of MERCHANTABILITY or | FITNESS FOR A PARTICULAR PURPOSE. See the GNU | General Public License for more details. | | You should have received a copy of the GNU General | Public License along with this program; if not, write | to the Free Software Foundation, Inc., 51 Franklin St, | Fifth Floor, Boston, MA 02110-1301 USA |\ *-+------------------------------------------------------- : : INSTRUCTIONS: : --------------------------- : 1) Change any settings needed on line 114 : 2) Add any static host's on line 121 : 3) For running as a single-file script, see line 86 : : NOTE: It is your responsibility to write a script to : update the database with the new traveling IP. : Using the examples below, i suggest creating a : separate file under password protection to : update and block the IP for automation. : See: http://snipplr.com/view/54875/updatednsphp/ : : : USAGE EXAMPLES: : --------------------------- : For checking the hostname, use the following example: : : require_once($_SERVER['DOCUMENT_ROOT'].'/hostname.class.php'); : $hostname = new hostname(); : $hostname->checkHostname(); : : For updating the hostname, use the following example: : : require_once($_SERVER['DOCUMENT_ROOT'].'/hostname.class.php'); : $hostname = new hostname(); : $hostname->putCache($_SERVER['REMOTE_ADDR']); : : : SETTINGS: (besides the static settings, these enable changes per instance) : --------------------------- : $hostname->changeSetting('dbtable', 'SQLite_table_name'); //Note: if the table cant be found, it will be created but not populated : $hostname->changeSetting('dbfilename', 'database_filename.extension'); //Changes the SQLite database file location : $hostname->changeSetting('useDatabase', 'no'); //Disables using a database. Be sure to populate the ipaddresses! : $hostname->changeSetting('static_dns', 'clear'); //Erase all static hostnames. Can be used to create a fresh hostname list for a particular instance : $hostname->changeSetting('addHost', 'hostname.dyndns.org'); //Adds new static hostname (does not have to be dyndns.org) : $hostname->changeSetting('addHost', '74.125.115.99'); //Adds new static ip to the access list : $hostname_error = ($hostname->lastError != '') ? "Error occurred" : NULL; //A simple way to check if a soft error occurred in the hostname script : : Note: It is recommended that you place the hostname database file in a : location that cant be accessed from the outside. Use of an htaccess : file with deny privileges to '*.sqdb' files works nicely for me. : : : RUNNING AS A SCRIPT: : --------------------------- : This script was also built so it can be run as a single-file : with no need to setup and use a class. The setup is the same : (you still need to change any settings in the script) but : you call it a little different. Use the following example: : : $hostname_runas = 'script'; : require_once($_SERVER['DOCUMENT_ROOT'].'/hostname.class.php'); : --------------------------- : : Thats all! Hope you enjoy this. : If you need help, email php.hostname@ my domain */ class hostname { // initialize class variables public $lastError = ''; // initiate class public function hostname(){ // define initial settings 'dbtable' => 'hostname', 'dbfilename' => $_SERVER['DOCUMENT_ROOT'].'/hostnames.sqdb', 'useDatabase' => 'yes' ); // define static hostnames that will always be accessible from 'INSERT_DYNDNS_HERE', //Note: not required to use dyndns.org, any hostname will do 'INSERT_DYNDNS_HERE' ); } //END Function hostname public function validIP($ip){ //Validates an ipaddress if (preg_match("^([1-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])(\.([0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])){3}^", $ip)): return TRUE; else: return FALSE; endif; } //END Function validIP public function addHost($host){ //Adds an ipaddress or hostname to the access list if ($this->validIP($host)): $this->ip_users[] = $host; else: $this->static_users[] = $host; endif; } //END Function addHost public function changeSetting($property, $value){ // Allows a user to change the settings and modify hostnames if ($property == 'static_dns'): //1 if ($value == 'clear'): //1.1 // Erases all the previously set static hostnames else: //1.1-2 $this->returnError("Failed to set 'static_dns', only option is 'clear'", 1); endif; //1.1 // Makes sure its a real setting then changes it $this->_SETT[$property] = $value; else: //1-3 $this->returnError("Failed to set '$property', not a valid setting (case sensative)", 0); return FALSE; endif; //1 return TRUE; } //END Fcuntion changeSetting protected function returnError($errorText, $killScript = 0){ // Error handling if ($killScript > 0): //1 if ($this->lastError != ''): //1.1 // loop & output errorLog then die with lastError echo "\n The following errors have occurred:"; foreach ($this->errorLog as $error): //1.1.1 endforeach; //1.1.1 else: //1.1-2 endif; //1.1 else: //1-2 $this->errorLog[] = $this->lastError = $errorText; endif; //1 } //END Function returnError public function putCache($ipaddress = NULL){ //An alias to $this->openDatabase. Makes calling it easier to read return $this->openDatabase('putCache', $ipaddress); } //END Function putCache protected function openDatabase($sqFunction = 'getCache', $ipaddress = NULL){ /* Opens the database and grabs the cached IP address * Also capable of opening the database and inserting the new ip address */ if ($hostname_database = sqlite_open($this->_SETT['dbfilename'], 0666, $sqlerror)): //1 switch ($sqFunction): //1.1 case ('putCache'): //1.1-2 $newdnsip = ($ipaddress == NULL) ? '255.255.255.255' : $ipaddress; $put_cache_query = "UPDATE ".$this->_SETT['dbtable']." SET ipaddress='$newdnsip' WHERE uniqueid='travelip'"; @sqlite_exec($hostname_database, $put_cache_query); return TRUE; break; case ('getCache'): //1.1-3 default: //1.1-4 $get_cache_query = "SELECT ipaddress FROM ".$this->_SETT['dbtable']." WHERE uniqueid='travelip'"; $cache_file = @sqlite_single_query($hostname_database, $get_cache_query, true); endswitch; //1.1 if(sqlite_last_error($hostname_database)): //1.2 $this->returnError('Database table does not exist. Creating table, inserting blank ip address.'); $create_table_query = "CREATE TABLE ".$this->_SETT['dbtable']."(uniqueid TEXT, ipaddress TEXT)"; @sqlite_exec($hostname_database, $create_table_query); $insert_table_query = "INSERT INTO ".$this->_SETT['dbtable']." (uniqueid, ipaddress) VALUES ('travelip', '1')"; @sqlite_exec($hostname_database, $insert_table_query); $cache_file = ($sqFunction = 'putCache' ? "Database created, run script again to populate ip address" : NULL); endif; //1.2 else: //1-2 $this->returnError("SQLite failed to initialize: ".$sqlerror); sqlite_close($hostname_database); return NULL; endif; //1 sqlite_close($hostname_database); return $cache_file; } //END Function openDatabase public function checkHostname(){ // Open database, grab cache, approve ip address if ($this->_SETT['useDatabase'] != 'no'): //1.1 $ip_users[] = $this->openDatabase('getCache'); if ($ip_users[0]{0} == 'D'): //1.1.1 $this->returnError($ip_users[0], 1); endif; //1.1.1 endif; //1.1 //Additional ipaddress were manually set. //This combines them into the user list endif; //1.2 foreach ($this->static_users as $static_hostname): //1.3.1 if ($this->validIP($static_hostname)): //1.3.1.1 $ip_users[] = $ip_static_users[] = $static_hostname; else: //1.3.1.1-2 endif; //1.3.1.1 endforeach; //1.3.1 endif; //1.3 // Now we check against the servers with the current ip endif; //1.4 /* After a user authenicates themself on a static ip, * this will keep the script from constantly pinging * the DYNDNS server causing needless load time. */ $_SESSION['remoteaddok'] = TRUE; else: //1.5-2 /* If the user is on a traveling ip, this makes it * check the ip address every time as SQLite load * time is small enough to not cause delay. */ $_SESSION['travelingIP'] = TRUE; endif; //1.5 endif; //1 } //END Function checkHostname } // END Class /*-+---------------------------------- * | From here on is the actual script */ /* This sets the default action of this file. * * Set to 'class' to default to running as a class * and will require the user to setup the object * and run the hostname commands. * * Setting to 'script' will only need a 'require_once' * command and it will take care of the rest */ $hostname_runas = 'class'; //Options are 'class' and 'script' endif; switch ($hostname_runas): case 'script': $hostname = new hostname(); $hostname->checkHostname(); break; case 'class': default: endswitch; ?>
You need to login to post a comment.
