<?php
//v1.0.4 www.bcoos.net 2006/10 GPL
// www.bcoos.net 2006-09 1.0.2 GPL
//v1.4 www.bcoos.net 2006-08 GPL
//v1.3 www.exoops.info 2006/03 GPL
// $Id: modulesadmin.php,v 1.22 2003/08/11 16:38:46 half-dead Exp $
// ------------------------------------------------------------------------- //
//               E-Xoops: Content Management for the Masses                  //
//                       < http://www.e-xoops.com >                          //
// ------------------------------------------------------------------------- //
// Original Author: Kazumi Ono
// Author Website : http://www.mywebaddons.com/ , http://www.myweb.ne.jp
// License Type   : GPL: See /manual/LICENSES/GPL.txt
// ------------------------------------------------------------------------- //
if( $xoopsUser->isAdmin($xoopsModule->mid()) ) {

	function chmod_do() {
		global $_POST;
		// Assign POST_VARS variables.
		$ftp_server = $_POST['ftp_server'];
		$ftp_user_name = $_POST['ftp_user_name'];
		$ftp_user_pass = $_POST['ftp_user_pass'];
		$folder = $_POST['folder'];
		$do = $_POST['do'];
		$chmod = $_POST['chmod'];
		
		// Set up basic connection.
		$conn_id = ftp_connect($ftp_server); 
		// Login with username and password.
		$login_result = ftp_login($conn_id, $ftp_user_name, $ftp_user_pass);
	
		
		// Check connection.
		if ((!$conn_id) || (!$login_result)) {

			echo _MI_DOCHMOD_TITLEERROR."<br />"; 
			echo _MI_DOCHMOD_CONNERROR1."<br />";
			echo $ftp_server."<br />";
			echo _MI_DOCHMOD_CONNERROR2.": ".$ftp_user_name."<br /><br />";
			echo "<form action='admin.php?fct=chmod' method='post'>
			<input type='hidden' name='op' value='dochmodagain' />
			<input type='hidden' name='ftp_server' value='".$ftp_server."' />
			<input type='hidden' name=ftp_user_name' value='".$ftp_user_name."' />
			<input type='hidden' name='ftp_user_pass' value='".$ftp_user_pass."' />
			<input type='hidden' name='ftp_html_path' value='".$folder."' />
			<input type='submit' class='button' value='"._GOBACK."' />
			</form><br />
			";	

		} else { 
			ftp_pasv($conn_id,true);
			$dircontent = ftp_rawlist($conn_id, ".");
		
			// Open, read, and close file.
			$fp = fopen("admin/chmod/chmodlist.txt", "r");
			$ftp_data = file("admin/chmod/chmodlist.txt");
			fclose($fp);

			echo "<table>";
		
			// Begin for loop.
			for ($n=0; $n<count($ftp_data); $n++) {
				// Read each line and split into path and chmod value.
				$chmod_line = explode(" ", $ftp_data[$n]);
		
				// Correct 'extra space' error.
				$chmod_line[1] = eregi_replace("[[:space:]]", "", $chmod_line[1]);
				
				// Assign variables (for clarity sake).
				$path_value = $folder."/"; 
				$path_value .= $chmod_line[0];
				$chmod_value = $chmod_line[1];
				$chmod_cmd = "CHMOD $chmod_value $path_value";
				
				$errorcount = 0;
				
				// Chmod each line of chmodlist.txt
				if ( ftp_site($conn_id,$chmod_cmd)) { echo"<tr><td>$path_value $chmod_value<img src='".XOOPS_URL."/images/icons/bcoos_check.gif' /></td></tr>";
				} else {
					echo "<tr><td><span style='color:#ff0000;'>$path_value $chmod_value <img src='".XOOPS_URL."/images/icons/bcoos_failed.gif' /></span></td></tr>";
					$errorcount++;
				}
						
			}
			// End for loop.
			
			echo "<tr><td>";
			if ($errorcount > 0) { 	echo sprintf(_MI_DOCHMOD_ERRORS,$errorcount)."<br />"; 
			} else {
			echo "<br /><br />"._MI_DOCHMOD_OKTITLE."<br />";
			echo _MI_DOCHMOD_OKDESCRIPTION."<br />";
			
			echo "<a href='".XOOPS_URL."'>".XOOPS_URL."</a><br /><br />";
			echo "</td></tr></table>";
			}
	

			// Close the FTP connection.
			ftp_close($conn_id); 
		
		}
	}
	
	
	function chmod_admin() {
		global $_POST;

		$chmoddone = chmod_check();
		$iswindows = strtoupper(PHP_OS);
		
			if (is_bool($chmoddone) or ($iswindows == "WINDOWS" or $iswindows == "CYGWIN" or $iswindows == "WINNT" or $iswindows == "WIN32")) {
			
				echo "<table width='100%' align='center'><tr><td>";
				echo "<br /><br />"._MI_DOCHMOD_OKTITLE."<br />";
				echo _MI_DOCHMOD_OKDESCRIPTION."<br />";
				echo "<a href='".XOOPS_URL."'>".XOOPS_URL."</a><br /><br />";
				echo "</td></tr></table>";
			
			} else {
			
				$extensions = get_loaded_extensions();
				if (in_array("ftp", $extensions)) {
				?>
				<table width="100%" align="center"><tr>
				<td colspan="2">
				
				<p><?php echo _MI_DOCHMOD_TEXT ?></p>
				<form action="admin.php?fct=chmod" method="POST">
				<p><?php echo _MI_DOCHMOD_FTPDOMAIN ?><br><input type="text" size="50" name="ftp_server" value="<?php echo $_POST['ftp_server']; ?>" class="text"></p>
				<p><?php echo _MI_DOCHMOD_FTPUSER ?><br><input type="text" size="20" name="ftp_user_name" value="<?php echo $_POST['ftp_user_name']; ?>" class="text"></p>
				<p><?php echo _MI_DOCHMOD_FTPPASS ?><br><input type="password" size="20" name="ftp_user_pass" value="<?php echo $_POST['ftp_user_pass']; ?>" class="text"></p>
				<input type="hidden" size="20" name="op" value="chmodbrowse" class="text">
				<p><input type="submit" name="login" value="<?php echo _MI_DOCHMOD_BUTTON ?>" class="button"></p></form>
				<p><?php echo "<br />"; ?></p>
				<p><?php echo _MI_DOCHMOD_MANUAL."<br /><br />"; ?></p>
				
				<p><?php echo $chmoddone ?></p>
				</td>
				
				</tr><tr><td colspan=2 ><span style="color:#ff0000;">
				<?php 
				} else {
				echo _MI_DOCHMOD_MANUAL."<br /><br />";
				echo $chmoddone;
				} 
				
				?>
				</span></td></tr>
				</table>
				<?php
				
			}
	}
	
	function chmod_browse() {
		global $_POST,$bcoosConfig;
		
				// Assign POST_VARS variables.
		$ftp_server = $_POST['ftp_server'];
		$ftp_user_name = $_POST['ftp_user_name'];
		$ftp_user_pass = $_POST['ftp_user_pass'];
		$folder = $_POST['folder'];
		$do = $_POST['do'];
		$chmod = $_POST['chmod'];
		 
		// Set up basic connection.
		$conn_id = @ftp_connect($ftp_server); 
		// Login with username and password.
		$login_result = @ftp_login($conn_id, $ftp_user_name, $ftp_user_pass);
		@ftp_pasv($conn_id,true);

		$yes = _YES;
		$no = _NO;
		
		if ($chmod == $yes) {
		chmod_do();
		} else  {
		
		// Check connection.
		if ((!$conn_id) || (!$login_result)) {
			echo _MI_DOCHMOD_TITLEERROR."<br />"; 
			echo _MI_DOCHMOD_CONNERROR1."<br />";
			echo $ftp_server."<br />";
			echo _MI_DOCHMOD_CONNERROR2.": ".$ftp_user_name."<br /><br />";
			echo "<form action='admin.php?fct=chmod' method='post'>
			<input type='hidden' name='op' value='dochmodagain' />
			<input type='hidden' name='ftp_server' value='".$ftp_server."' />
			<input type='hidden' name=ftp_user_name' value='".$ftp_user_name."' />
			<input type='hidden' name='ftp_user_pass' value='".$ftp_user_pass."' />
			<input type='submit' class='button' value='"._GOBACK."' />
			</form><br />
			";	
		} else {
		
			$listarray0 = array();
			$listarray1 = array();
			if ($do == "cdup") {ftp_cdup($conn_id);}
			if ($do !== NULL ) {
				$changedok = ftp_chdir($conn_id, "/".$folder);
				if ($changedok == FALSE) {unset($folder);}
			}
			$dircontent = ftp_rawlist($conn_id, './');
			 
		
			foreach ($dircontent as $row) { 
				$arraycontent = preg_split("[ ]", $row, 9, PREG_SPLIT_NO_EMPTY);
				$listarray0[] = substr($arraycontent[0],0,1);
				$listarray1[] = $arraycontent[8];
			}
			$arraysorted = array_multisort($listarray0,SORT_DESC,$listarray1);
		
		
			$actualpath = ftp_pwd($conn_id);
			$okmainfile = '';
			$content = '';
		
			foreach($dircontent as $key => $value) {
				if ($listarray0[$key] == "d" or $listarray0[$key] == "s") {
					$content .= "<tr><td>
					<img src='".XOOPS_URL."/images/icons/".$bcoosConfig['iconset']."menu_d_tree_close.gif' alt='folder' >
					</td>";
					$content .= "<td><form action='admin.php?fct=chmod' method='post' id='ftpdata".$key."' >\n<input type='hidden' name='ftp_server' value='".$ftp_server."' /><input type='hidden'  name='ftp_user_name' value='".$ftp_user_name."' />\n<input type='hidden' name='ftp_user_pass' value='".$ftp_user_pass."' />\n<input type='submit' name='do' class='buttonlink' value='".$listarray1[$key]."' />\n<input type='hidden' name='folder' value='".$folder."/".$listarray1[$key]."' /><input type='hidden' name='op' value='chmodbrowse' /></form></td></tr>";
				} elseif ($listarray1[$key] == "mainfile.php") {
					$okmainfile = "<b>".ftp_pwd($conn_id)."</b> "._MI_DOCHMOD_HASMAINFILEPHP."<br /><br /> <form action='admin.php?fct=chmod' method='post' id='chmod' >\n<input type='hidden' name='ftp_server' value='".$ftp_server."' /><input type='hidden'  name='ftp_user_name' value='".$ftp_user_name."' />\n<input type='hidden' name='ftp_user_pass' value='".$ftp_user_pass."' />\n<input type='hidden' name='folder' value='".$folder."' />\n<input type='submit' name='op' class='button' value='"._YES."' />&nbsp;<input type='submit' class='button' name='chmod' value='"._NO."' /></form>";
					
					$actualpath = "";
				} 
			}
			ftp_close($conn_id);
			
			echo "<table>";
			if ($actualpath !== "") { echo "<table><tr><td></td><td>"._MI_DOCHMOD_BROWSETOMAINFILE."</td></tr>";}
			echo "<tr><td></td><td>".$okmainfile.$actualpath."&nbsp;</td></tr>";
			echo $content;
			echo "<tr><td></td><td><br /></td></table>
			";

		}

	}
}	
	function chmod_check() {

		$errorcount = 0;
		$errors = "<form action='admin.php?fct=chmod' method='post'>";
		$errors .= "<table><tr><td>"._MI_DOCHMOD_COMPLETE."
		<input type='hidden' name='op' value='dochmodagain' />
		<input type='submit' class='button' value='"._NEXT."' />
		</td></tr>";
		
		// Open, read, and close file.
			$fp = fopen("admin/chmod/chmodlist.txt", "r");
			$ftp_data = file("admin/chmod/chmodlist.txt");
		
		// Begin for loop.
		for ($n=0; $n<count($ftp_data); $n++) {
			// Read each line and split into path and chmod value.
			$chmod_line = explode(" ", $ftp_data[$n]);
			// Correct 'extra space' error.
			$chmod_line[1] = eregi_replace("[[:space:]]", "", $chmod_line[1]);
			
			// Assign variables (for clarity sake).
			$path_value = XOOPS_ROOT_PATH."/"; 
			$path_value .= $chmod_line[0];
			$chmod_value = $chmod_line[1];
			
			$write = is_writable($path_value);
			$read = is_readable($path_value);
			$exist = file_exists($path_value);
			
			if ($exist) {
				switch ($chmod_value) {
					case '644':
						if ($write == false && $read == true) { 
						} else { 
							$errors .= "<tr><td><span style='color:#ff0000;'>$path_value &nbsp;&nbsp; $chmod_value &nbsp;&nbsp; -rw-r--r-- </span></td></tr>";
							$errorcount++;
						}
					break;
					
					case '755':
						if ($write == false && $read == true) { 
						} else {
							$errors .= "<tr><td><span style='color:#ff0000;'>$path_value &nbsp;&nbsp; $chmod_value &nbsp;&nbsp; drwxr-xr-x </span></td></tr>";
							$errorcount++;
						}
					break;
					
					case '666':
						if ($write == true && $read == true) { 
						} else {
							$errors .= "<tr><td><span style='color:#ff0000;'>$path_value &nbsp;&nbsp; $chmod_value &nbsp;&nbsp; -rw-rw-rw-</span></td></tr>";
							$errorcount++;
						}
					break;
					
					case '777':
						if ($write == true && $read == true) { 
						} else {
							$errors .= "<tr><td><span style='color:#ff0000;'>$path_value &nbsp;&nbsp; $chmod_value &nbsp;&nbsp; drwxrwxrwx</span></td></tr>";
							$errorcount++;
						}
					break;
					
					case '444':
						if ($write == false && $read == true) {
						} else { 
							$errors .= "<tr><td><span style='color:#ff0000;'>$path_value &nbsp;&nbsp; $chmod_value &nbsp;&nbsp; -r--r--r--</span></td></tr>";
							$errorcount++;
						}
					break;	
				}
			} else {
						$errors .= "<tr><td><span style='color:#ff0000;'>$chmod_line[0] "._FILEMISSINGUPLOADTHISAGAIN." </span></td></tr>";
						$errorcount++;
			}
			
		}	
		// End for loop.
		$errors .= "</table></form>";
		if ($errorcount > 0) {return $errors;} else { return true;}
	}
	
	
}
?>