Location: PHPKode > scripts > CECID: CEnsorship CIrcumvention Device > CECID-PHP/config.php
<?php
/************************************************************
 *                                                          *
 *  CECID - CEnsorship CIrcumvention Device                 *
 *  Configuration Script                                    *
 *                                                          *
 *  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      *
 *                                                          *
 ************************************************************/

/*
	Structure:
		Checks whether config file exists. If not, goes straight to configuration section. Forces password change.
		If yes, prompts user for user/pass.
		Checks if correct; if yes goto configuration section; if no reject.
		Configuration section:
			Form template, checkboxes etc for various options. Included where necessary.
		User config then processed, written into file.
*/

/* Conditional Functions must come before everything else */
/* This is included for backwards compatibility with older versions of PHP. Returns $filename as string. */
if (!function_exists('file_get_contents')) {
	function file_get_contents($filename) {
		$handle = fopen($filename, "r");
		$contents = fread($handle, filesize($filename));
		fclose($handle);
		return($contents);
	}
}

error_reporting (E_ERROR);

$cmyname='config.php';

require('template/tempconf.php');

$err=false;
$userpass=false;

if (param('setpass')) {
	loadfromfile();

	if (($user != md5($_POST['user'])) || ($pass != md5($_POST['pass']))) {
		err('Incorrect user/pass.');
	}

	include 'template/configtemplate.php';
	exit();

} elseif (param('setconf')) {
	loadfromfile();

	if ((($user != param('user')) || ($pass != param('pass'))) && (file_exists('cecidconfigdb'))) {
		err('Bad user/pass.');
	} elseif ((!file_exists('cecidconfigdb')) || (trim(file_get_contents('cecidconfigdb')) == '')) {
		if (($_POST['user'] == '') || ($_POST['pass'] == '')) {
			err('You must enter a username/password when first setting up the script.');
		}
	}

	// Process the user input
	$myname=$_POST['myname'];
	$cecidclient=$_POST['cecidclient'];
	$bwordpath=$_POST['bwordpath'];
	$templatepath=$_POST['templatepath'];
	$addrdbasepath=$_POST['addrdbasepath'];
	$accessdbasepath=$_POST['accessdbasepath'];
	$scriptdbasepath=$_POST['scriptdbasepath'];
	$statsdbasepath=$_POST['statsdbasepath'];
	$imagesup=$_POST['imagesup'];
	$debug=$_POST['debug'];
	$version=$_POST['version'];
	$addword=$_POST['addword'];
	$respondtoping=$_POST['respondtoping'];
	$configured=1;

	if (trim($_POST['user']) != '') $user=md5(trim($_POST['user']));
	if (trim($_POST['pass']) != '') $pass=md5(trim($_POST['pass']));

	// Check that entered files exist, and create them if they dont. Stat file gets 10 '0's thrown in, too.
	checkfile($cecidclient, 0);
	checkfile($bwordpath, 0);
	checkfile($addrdbasepath, 0);
	checkfile($accessdbasepath, 0);
	checkfile($scriptdbasepath, 0);
	checkfile($statsdbasepath, 1);

	$scriptdbperms = fileperms('clientscriptdb');
	if ($scriptdbperms != 33206) err("Permissions for file clientscriptdb are set to $scriptdbperms, not 33206!");

	savetofile();


	err("Configuration completed successfully!<br><a href=\"$myname\">Back to CECID</a>");

} elseif (param('help')) {
	$help = param('help');
	include 'template/configtemplate.php';
	exit();

} else {

	//Check whether file exists - if not we need to jump straight to the config page
	if ((!file_exists('cecidconfigdb')) || (trim(file_get_contents('cecidconfigdb')) == '')) {
		include 'template/configtemplate.php';
		exit();
	} else { //We need to prompt the user for a user/pass
		$userpass=true;
		include 'template/configtemplate.php';
		exit();
	}

}

//Gets parameters send to a script (ie index.php?action=view )
function param($name)
{
   if( isset($_GET[$name]) && strlen(trim($_GET[$name]))>0)
      return addslashes(trim($_GET[$name]));
   return NULL;
}

function loadfromfile() {

		if ((!file_exists('cecidconfigdb')) || (trim(file_get_contents('cecidconfigdb')) == '')) return;

		global $myname, $cecidclient, $bwordpath, $templatepath, $addrdbasepath, $accessdbasepath,
		$scriptdbasepath, $statsdbasepath, $imagesup, $debug, $version, $addword, $configured,
		$user, $pass, $respondtoping;

		$confarr = file('cecidconfigdb');
		while ($confarr == '') {
			$confarr = array_shift($confarr);
		}

		$myname=trim($confarr[0]);
		$cecidclient=trim($confarr[1]);
		$bwordpath=trim($confarr[2]);
		$templatepath=trim($confarr[3]);
		$addrdbasepath=trim($confarr[4]);
		$accessdbasepath=trim($confarr[5]);
		$scriptdbasepath=trim($confarr[6]);
		$statsdbasepath=trim($confarr[7]);
		$imagesup=trim($confarr[8]);
		$debug=trim($confarr[9]);
		$version=trim($confarr[10]);
		$addword=trim($confarr[11]);
		$configured=trim($confarr[12]);
		$respondtoping=trim($confarr[13]);
		$user=trim($confarr[14]);
		$pass=trim($confarr[15]);
}

function savetofile() {

	global $myname, $cecidclient, $bwordpath, $templatepath, $addrdbasepath, $accessdbasepath,
	$scriptdbasepath, $statsdbasepath, $imagesup, $debug, $version, $addword, $configured,
	$user, $pass, $respondtoping;

	$fp = fopen('cecidconfigdb', 'w');
	fwrite($fp, $myname."\n".$cecidclient."\n".$bwordpath."\n".$templatepath."\n".$addrdbasepath."\n".$accessdbasepath."\n".$scriptdbasepath."\n".$statsdbasepath."\n".$imagesup."\n".$debug."\n".$version."\n".$addword."\n".$configured."\n".$respondtoping."\n".$user."\n".$pass);
	fclose($fp);
}

function err($msg) {
	$userpass=false;
	$err=true;
	include 'template/configtemplate.php';
	exit();
}

function checkfile($filename, $statfile) {
	if (!file_exists($filename)) { //Attempt to create file
		if (!touch($filename)) err("Could not create file $filename! Have you chmodded the directory this script is running in? (Path to script: ".$_SERVER['SCRIPT_FILENAME'].")");
	}

	if (($statfile) && (trim(file_get_contents($filename)) == '')) {
			$fp = fopen($filename, 'w');
			fwrite($fp, "0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n");
			fclose($fp);
	}
}
Return current item: CECID: CEnsorship CIrcumvention Device