Location: PHPKode > scripts > CECID: CEnsorship CIrcumvention Device > CECID-PHP/cecid-client.php
<?php
/************************************************************
 *                                                          *
 *  CECID - CEnsorship CIrcumvention Device                 *
 *  Client WebCache Support
 *                                                          *
 *  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., 59 Temple Place, *
 *  Suite 330, Boston, MA 02111-1307 USA                    *
 *                                                          *
 *  To contact the author, email hide@address.com      *
 *                                                          *
 ************************************************************/

$client=param('client');
$port=param('port');
$get=param('get');
$script=param('script');
$ping=param('ping');
$clientaddr=$_SERVER['REMOTE_ADDR'].':'.$port;
$dontadd=false;

$bantime = 0; // Minimum time (in minutes) between client accesses.

if ($ping || $get) {
	clistart();
}

if($ping) {
	if ($respondtoping) {
		echo "PONG$version";
		debug('<br>');
	}
	
	cliexit();
}

if($get) {
	if ($backtime = checkaccess($_SERVER['REMOTE_ADDR'])) { // Client has come back early, so ip will not be added.
		echo "W|200|TOO_EARLY_FOR_ADD|$backtime|\n";
		debug('<br>');
		$dontadd=true;
	}

	if ((!is_string($client)) || (strlen($client) < 5)) {
		echo "W|201|NO_ANONYMOUS_CONNECTIONS|\n";
		debug('<br>');
		cliexit();
	}

	if (!$port) {
		echo "W|202|PORT_NOT_SUPPLIED|\n";
		debug('<br>');
		cliexit();
	}

	if(!is_numeric($port)) {
		echo "W|203|BAD_PORT|\n";
		debug('<br>');
		cliexit();
	}

	if ($script && !$dontadd) {

		$ok = script_is_alive($script);
		$dup = false;

		if ($ok) { // Check for dups
			$ok = script_isnt_dup($script);
			if (!$ok) $dup=true;
		}

		if ($ok) {
			echo "I|300|SCRIPT_OK|\n";
			debug('<br>');
			$fp = fopen($scriptdbasepath, 'a');
			fwrite($fp, $script."\n");
			fclose($fp);
		} elseif ($dup) {
			echo "W|204|DUPLICATE_SCRIPT|\n";
			debug('<br>');
		} else {
			echo "W|205|BAD_SCRIPT|\n";
			debug('<br>');
		}
	}

	$filearr = file($addrdbasepath);
	if (((count($filearr) < $ipcount) || (!$filearr)) && ($get != 2)) { //Insufficiant ip's
		echo "E|100|INSUFFICIANT_ADDR|\n";
		debug('<br>');
		if (!$dontadd) {
			$fp = fopen($addrdbasepath, 'a');
			fwrite($fp, $clientaddr."\n");
			fclose($fp);
		}
		cliexit();
	}

	$unshuffledarr = $filearr;

	if ($get == 1) {
		shuffle($filearr); //Randomize elements

		foreach($filearr as $num => $addr) { //Output 10 ip addresses
			$addr = trim($addr);
			echo "H|$addr|\n";
			debug('<br>');
		}
	} elseif ($get == 2) {
		$scriptarr = file($scriptdbasepath);
		shuffle($scriptarr); //Randomize elements
		if ($client="script") $dontadd=true;

		foreach($scriptarr as $num => $addr) { //Output all the scripts we know
			$addr = trim($addr);
			echo "S|$addr|\n";
			debug('<br>');
		}
	} else {
		echo "W|206|BAD_GET|\n";
		debug('<br>');
		cliexit();
	}

	if (!$dontadd) {
		//Bump address on end off and shove the new one on top
		array_pop($unshuffledarr);
		array_unshift($unshuffledarr, $clientaddr."\n");

		$fp = fopen($addrdbasepath, 'w');
		foreach($unshuffledarr as $num => $addr) {
			fwrite($fp, $addr); //There should already be a \n on the end of each string, so we dont need to bother.
		}
		fclose($fp);
	}
	cliexit();
}

function clistart() {
	debug('WARNING: Debug is on (clients may not be able to deceipher output): Inserting &lt;br&gt; after every \n.<br>');
	echo "<$\n";
	debug('<br>');
	recordstat(clienthit, 0);
}

function cliexit() {
	echo "$>\n";
	exit();
}

// Checks whether client is allowed to access. Returns false if no, the number of secs left if yes.
function checkaccess($address) {
	global $bantime;
	global $accessdbasepath;
	$bansecs = $bantime*60;
	$accessarr = file($accessdbasepath);
	$match = false;

	$fp = fopen($accessdbasepath, 'w');

	foreach ($accessarr as $num => $line) {
		if ($line=="\n") continue; // Skip blank space
		list($fileaddress, $time) = explode('|', $line);

		if ((time()-$bansecs) > $time) { //Remove expired entries
			$accessarr[$num] = '';
			continue;
		}

		if ($address == $fileaddress) $match=$time;

		fwrite($fp, $fileaddress.'|'.$time."\n");
	}
	if ($match) {
		fclose($fp);
		return($bansecs-(time()-$match));
	} else {
		fwrite($fp, $address.'|'.time()."\n");
		fclose($fp);
		return(false);
	}
}

/* Is $script really a script?. Returns true on yes, false on no */
function script_is_alive($script) {
	$testarr = file($script.'?ping=1');
	$bad = false;
	$ok = false;

	if (!$testarr) {
		$bad = true;
	}

	if (!$bad) {
		foreach ($testarr as $num => $line) { // Check that script is ok
			if ((substr($line, 0, 4) == 'PONG') || (substr($line, 0, 8) == '<br>PONG')) {
				$ok = true;
				break;
			}
		}
	}

	if($ok) {
		return(true);
	} else {
		return(false);
	}
}

function script_delete($script) {
	global $scriptdbasepath;

	$script = $script."\n"; //Append a newline onto the end of script so we can locate it in file

	$scriptarr = file($scriptdbasepath);
	if (!$scriptarr) {
		echo "Could not open script database $scriptdbasepath<br>";
		return(false);
	}

	$offendingkey = array_search($script, $scriptarr);

	if ($offendingkey === false) {
		echo "Could not locate $script to delete<br>";
		return(false);
	}

	unset($scriptarr[$offendingkey]);

	$fp = fopen($scriptdbasepath, 'w');
	if (!$fp) {
		echo "Could not open script database $scriptdbasepath to write.<br>";
		return(false);
	}

	foreach($scriptarr as $value) {
		if ($value == "") { echo "Skipping $value; "; continue; }
		fwrite($fp, $value);
	}

	fclose($fp);
	return(true);
}

function update_scripts() {
	/*
		We need to:
		- Parse our own script file.
		- Pick a random script.
		- Get its script listing - if we hit any probs, cease, desist and delete.
		- Check whether we are listed - if no add ourselves
		- Check for any new mirrors - verify each new mirrors existance, only add if good.
	*/
	global $scriptdbasepath;
	global $me;
	global $dup;

	/* Pick a random script from our database */
	srand(make_seed());
	mt_srand(make_seed());
	$scriptarr = file($scriptdbasepath);
	$script = fix_url(trim($scriptarr[array_rand($scriptarr)]));

	echo "This script is $me<br>";

	if ($script == "") {
		echo "Blank script - deleting<br>";
		script_delete($script);
		return(true);
	}

	echo "Querying script $script for mirror update<br>";

	if (!script_is_alive($script)) {
		echo "Script dead - deleting<br>";
		script_delete($script);
		return(true);
	}

	$scriptlisting = file($script."?get=2&client=script&port=1&script=$me");

	foreach ($scriptlisting as $line) {
		$line = trim($line);

		if (substr($line, 1, 1) != '|') continue;
		$linearr = explode('|', $line);
		$code = trim($linearr[0]);
		$data = trim($linearr[1]);

		if ($code != 'S') { echo "$code code $data recieved.<br>"; continue; }
		if ($data == $me) continue;

		if ((script_isnt_dup($data)) && (script_is_alive($data))) {
			echo "Added script $data<br>";
			script_add($data);
		} else {
			echo "Error adding script $data to local database: ";
			if ($dup) {
				echo "Exists already<br>";
			} else {
				echo "Script dead<br>";
			}
		}
	}
}

/* Do we already have $script in our database? false on yes, true on no. */
function script_isnt_dup($script) {
	global $scriptdbasepath;
	global $dup;
	$dup=false;

	$dbarr = file($scriptdbasepath);
	$ok = true;

	foreach($dbarr as $num => $line) {
		if ($line == $script."\n") {
			$ok = false;
			$dup = true;
			break;
		}
	}

	if ($ok) {
		return(true);
	} else {
		return(false);
	}
}

function script_add($script) {
	global $scriptdbasepath;

	$fp = fopen($scriptdbasepath, 'a');
	fwrite($fp, $script."\n");
	fclose($fp);
}

function make_seed() {
    list($usec, $sec) = explode(' ', microtime());
    return (float) $sec + ((float) $usec * 100000);
}

/* Append trailing slash to url if needed */
function fix_url ($url) {
	$urlarr = parse_url($url);
	if ($urlarr['scheme']."://".$urlarr['host'] == $url) { $url = $url.'/'; }
	return($url);
}
Return current item: CECID: CEnsorship CIrcumvention Device