<?php
#phpinfo();
$configfile = "awsoapclientconfig.inc.php";

if($action == "setup") 
{
	if(!preg_match("/\/$/",$scriptdirectory))$scriptdirectory .= "/";
	$filepath = $scriptdirectory.$configfile;
	$fd = @fopen($filepath, "r");
	if(!$fd) 
	{
		$message = "Unable to gain read access to awsoapclientconfig.inc.php. Please check permissions and try again";
		include_once('installform.inc.php');
		exit;
	}
	$filecontents = fread($fd,filesize($filepath));
	fclose($fd);
	$filecontents = preg_replace("/(\\\$url)[^\n]*\n/","$1 = \"$url\";\n",$filecontents);
	$filecontents = preg_replace("/(\\\$templateDir)[^\n]*\n/","$1 = \"$templatedir\";\n",$filecontents);
	$filecontents = preg_replace("/(\\\$clientDir)[^\n]*\n/","$1 = \"$scriptdirectory\";\n",$filecontents);
	$filecontents = preg_replace("/(\\\$limit)[^\n]*\n/","$1 = \"$limit\";\n",$filecontents);
	$filecontents = preg_replace("/(\\\$affiliateID)[^\n]*\n/","$1 = \"$affiliateid\";\n",$filecontents);
	$filecontents = preg_replace("/(\\\$password)[^\n]*\n/","$1 = \"$password\";\n",$filecontents);
	$filecontents = preg_replace("/(\\\$breadCrumbSeparator)[^\n]*\n/","$1 = \"$seperator\";\n",$filecontents);
	$filecontents = preg_replace("/(\\\$outputType)[^\n]*\n/","$1 = \"$mode\";\n",$filecontents);
	$filecontents = preg_replace("/(\\\$searchresultstemplate)[^\n]*\n/","$1 = \"$searchtemplate\";\n",$filecontents);
	$filecontents = preg_replace("/(\\\$browsetemplate)[^\n]*\n/","$1 = \"$browsetemplate\";\n",$filecontents);
	$filecontents = preg_replace("/(\\\$searchformtemplate)[^\n]*\n/","$1 = \"$searchformtemplate\";\n",$filecontents);
	$filecontents = preg_replace("/(\\\$maincatstemplate)[^\n]*\n/","$1 = \"$categorytemplate\";\n",$filecontents);
	$filecontents = preg_replace("/(\\\$userSettings)[^\n]*\n/","$1 = \"1\";\n",$filecontents);
	$filecontents = preg_replace("/(\\\$sefriendly)[^\n]*\n/","$1 = \"$searchenginefriendly\";\n",$filecontents);
	$filecontents = preg_replace("/(\\\$defaultbrowseorderby)[^\n]*\n/","$1 = \"$defaultbrowseorderby\";\n",$filecontents);
	$filecontents = preg_replace("/(\\\$defaultsearchorderby)[^\n]*\n/","$1 = \"$defaultsearchorderby\";\n",$filecontents);


	$fd = @fopen($filepath, "w");
	if(!$fd) 
	{
		$message = "Unable to gain write access to $configfile. Please check permissions and try again";
		include_once('installform.inc.php');
		exit;
	}
	fwrite($fd,$filecontents);
	fclose($fd);
	$message = "Your configuration was saved successfully. <a href=\"$url\">Click here</a> to browse your product set.<br> IMPORTANT: Please remember to remove or disable this script otherwise any user can gain access to sensitive information.";
}
else 
{
	if(isset($_ENV['OSTYPE']))# Only check files if on *nix. Windows does not support posix functions
	{
		$fileLoc = "http://".$HTTP_SERVER_VARS['HTTP_HOST'].$PHP_SELF;
		$fileLoc = preg_replace("/[^\/]*$/","",$fileLoc)."shop.php";
		clearstatcache();
		$currentDir = posix_getcwd();
		$mode = fileperms($currentDir."/".$configfile);
		$fileowner = fileowner($currentDir."/".$configfile);
		$filegroup = filegroup($currentDir."/".$configfile);
		$fileownerArray = posix_getpwuid($fileowner);
		$currentUser = posix_getuid();
		$currentGrp = posix_getegid();
		$perms = mfunGetPerms(fileperms($currentDir."/".$configfile));
		$permissionGranted = false;

		if($world["write"]   = ($mode & 00002))
		{
			$permissionGranted = true;
			$thisworld = "yes";
		}
		elseif($fileowner == $currentUser) 
		{
			if($owner["write"]   = ($mode & 00200)) $permissionGranted = true;
			$thisowner = "yes";
		}
		elseif($filegroup == $currentGrp) 
		{
			if ($group["write"]   = ($mode & 00020)) $permissionGranted = true;
			$thisgroup = "yes";
		}

		if($permissionGranted == false) 
		{
			$message = "The configuration file is owned by <i>".$fileownerArray['name']."</i> but it appears that you do not have permission to write to this file";
			$message .= "<br>Current permissions are ".$perms."<br> Please adjust permissions before continuing";
		}

		if(!$scriptdirectory)$scriptdirectory = $currentDir."/";
		$filepath = $scriptdirectory.$configfile;
		$templateD = $currentDir."/templates";
		$fd = @fopen($filepath, "r");
		if(!$fd) 
		{
			$message = "Unable to gain read access to config file at $filepath. Please check permissions and try again";
			include_once('installform.inc.php');
			exit;
		}
		while (!feof ($fd)) 
		{
			$buffer = trim(fgets($fd, 4096));
			if(preg_match("/^#/",$buffer))continue;
			elseif(preg_match("/\\\$userSettings/",$buffer))$userSettings = preg_replace("/.*\"([^\"]*)\";/","$1",$buffer);
			elseif(preg_match("/\\\$url/",$buffer))
			{
				if($userSettings ==0) $url = preg_replace("/.*\"([^\"]*)\";/",$fileLoc,$buffer);
				else $url = preg_replace("/.*\"([^\"]*)\";/","$1",$buffer);
			}
			elseif(preg_match("/\\\$templateDir/",$buffer))
			{
				if($userSettings ==0) $templatedir = preg_replace("/.*\"([^\"]*)\";/",$templateD,$buffer);
				else $templatedir = preg_replace("/.*\"([^\"]*)\";/","$1",$buffer);
			}
			elseif(preg_match("/\\\$clientdir/",$buffer))$clientDir = preg_replace("/.*\"([^\"]*)\";/","$1",$buffer);
			elseif(preg_match("/\\\$limit/",$buffer))$limit = preg_replace("/.*\"([^\"]*)\";/","$1",$buffer);
			elseif(preg_match("/\\\$affiliateID/",$buffer))$affiliateid = preg_replace("/.*\"([^\"]*)\";/","$1",$buffer);
			elseif(preg_match("/\\\$password/",$buffer))$password = preg_replace("/.*\"([^\"]*)\";/","$1",$buffer);
			elseif(preg_match("/\\\$breadCrumbSeparator/",$buffer))$seperator = preg_replace("/.*\"([^\"]*)\";/","$1",$buffer);
			elseif(preg_match("/\\\$outputType/",$buffer))$mode = preg_replace("/.*\"([^\"]*)\";/","$1",$buffer);
			elseif(preg_match("/\\\$searchresultstemplate/",$buffer))$searchtemplate = preg_replace("/.*\"([^\"]*)\";/","$1",$buffer);
			elseif(preg_match("/\\\$browsetemplate/",$buffer))$browsetemplate = preg_replace("/.*\"([^\"]*)\";/","$1",$buffer);
			elseif(preg_match("/\\\$searchformtemplate/",$buffer))$searchformtemplate = preg_replace("/.*\"([^\"]*)\";/","$1",$buffer);
			elseif(preg_match("/\\\$maincatstemplate/",$buffer))$categorytemplate = preg_replace("/.*\"([^\"]*)\";/","$1",$buffer);
			elseif(preg_match("/\\\$sefriendly/",$buffer))$searchenginefriendly = preg_replace("/.*\"([^\"]*)\";/","$1",$buffer);
			elseif(preg_match("/\\\$defaultbrowseorderby/",$buffer))$defaultbrowseorderby = preg_replace("/.*\"([^\"]*)\";/","$1",$buffer);
			elseif(preg_match("/\\\$defaultsearchorderby/",$buffer))$defaultsearchorderby = preg_replace("/.*\"([^\"]*)\";/","$1",$buffer);
		}


		fclose ($fd);
	}
}
include_once('installform.inc.php');



function mfunGetPerms( $in_Perms ) { 
   $sP;

   if($in_Perms & 0x1000)     // FIFO pipe
     $sP = 'p'; 
   elseif($in_Perms & 0x2000) // Character special
     $sP = 'c'; 
   elseif($in_Perms & 0x4000) // Directory
     $sP = 'd'; 
   elseif($in_Perms & 0x6000) // Block special
     $sP = 'b';
   elseif($in_Perms & 0x8000) // Regular
     $sP = '&minus;';
   elseif($in_Perms & 0xA000) // Symbolic Link
     $sP = 'l';
   elseif($in_Perms & 0xC000) // Socket
     $sP = 's';
   else                        // UNKNOWN
     $sP = 'u'; 

   // owner
   $sP .= (($in_Perms & 0x0100) ? 'r' : '&minus;') .
          (($in_Perms & 0x0080) ? 'w' : '&minus;') . 
          (($in_Perms & 0x0040) ? (($in_Perms & 0x0800) ? 's' : 'x' ) : 
                                 (($in_Perms & 0x0800) ? 'S' : '&minus;')); 

   // group
   $sP .= (($in_Perms & 0x0020) ? 'r' : '&minus;') .
          (($in_Perms & 0x0010) ? 'w' : '&minus;') . 
          (($in_Perms & 0x0008) ? (($in_Perms & 0x0400) ? 's' : 'x' ) : 
                                 (($in_Perms & 0x0400) ? 'S' : '&minus;')); 

   // world
   $sP .= (($in_Perms & 0x0004) ? 'r' : '&minus;') .
         (($in_Perms & 0x0002) ? 'w' : '&minus;') . 
          (($in_Perms & 0x0001) ? (($in_Perms & 0x0200) ? 't' : 'x' ) : 
                                 (($in_Perms & 0x0200) ? 'T' : '&minus;')); 
   return $sP;
 };
?>
