Editing PHP code to SFTP vs. FTP

I have an auto install code I use with WHMCS that stopped working. I believe it’s due to our server now requiring SFTP versus FTP for all accounts.

I am wondering if there is a quick way to change this code I have to connect via SFTP? Here’s the snippet that manages FTP in the script

	// FTP
function kit__ftp_open($arguments){
	/*
		{:kit__ftp_connnect[server,user,password]:}
		Returns: handle
	*/

	$connection = ftp_connect($arguments[0]);
	if ($connection){
		$login = ftp_login($connection, $arguments[1],$arguments[2]);
		if ($login){
			$this->ftp_connection[] = $connection;
			return count($this->ftp_connection)-1;
		}
	}
	if ($arguments[2]){
		$pass = "*****";
	}else{
		$pass = "";
	}
	$this->kit__log_add(array("Error opening FTP connection! - Server: $arguments[0] - User: $arguments[1] - Password: $pass"));
	return -1;
}

function kit__ftp_copy_kit_files($arguments){
	$fhandle = $arguments[0];
	$dir = $arguments[1];
	if (substr($dir,-1) != '/'){
		$dir .="/";
	}

	$kit_dir = dirname(__FILE__)."/";

	$kit_files[] = "postkit.php";
	$kit_files[] = "kit_parser.php";
	$kit_files[] = "advanced_functions.php";
	$kit_files[] = "limited_functions.php";
	$kit_files[] = "postkit.kit";

	if (isset($this->__kit_vars["variable_file"])){
		if (file_exists($kit_dir.$this->__kit_vars["variable_file"])){
			$this->kit__ftp_copy(array($fhandle,$dir."kit_vars.txt",$kit_dir.$this->__kit_vars["variable_file"]),0);
		}
	}else{
		if (file_exists("kit_vars.txt")){
			$kit_files[] = "kit_vars.txt";
		}
	}

	for($i=0,$c=count($kit_files);$i<$c;$i++){
		if (file_exists($kit_dir.$kit_files[$i])){
			$this->kit__ftp_copy(array($fhandle,$dir.$kit_files[$i],$kit_dir.$kit_files[$i]),0);
		}
	}
}

function kit__ftp_copy($arguments,$log=1){

	/*
		{:kit__ftp_copy[handle,destination,source,mode(FTP_ASCII, default FTP_BINARY),owner,group]:}
		Returns: handle
	*/
	if (!isset($arguments[3])){
		$arguments[3] = FTP_BINARY;
	}
	if ($arguments[2] == 'postkit.php' || $arguments[2] == 'kit_parser.php' || $arguments[2] == 'advanced_functions.php' || $arguments[2] == 'limited_functions.php'){
		if (!file_exists($arguments[2])){
			$arguments[2] = "../".$arguments[2];
			if (!file_exists($arguments[2])){
				return;
			}
		}
	}
	// FTP
	$f = ftp_put($this->ftp_connection[$arguments[0]],$arguments[1],$arguments[2],$arguments[3]);
	if (!$f){
		if ($log == 1){
			$this->kit__log_add(array("Error uploading files via FTP! - ID: $arguments[0] - Destination: $arguments[1] - Source: $arguments[2] - Mode: $arguments[3]"));
		}
	}else{
		if ($arguments[4]){
			if (!ftp_site($this->ftp_connection[$arguments[0]], "CHOWN ".$arguments[4]." ".$arguments[1])){
				if ($log == 1){
					$this->kit__log_add(array("Error setting owner via FTP! - ID: $arguments[0] - Destination: $arguments[1] - Source: $arguments[2] - Owner: $arguments[4]"));
				}
			}
		}
		if ($arguments[5]){
			if (!ftp_site($this->ftp_connection[$arguments[0]], "CHGRP ".$arguments[4]." ".$arguments[1])){
				if ($log == 1){
					$this->kit__log_add(array("Error setting group via FTP! - ID: $arguments[0] - Destination: $arguments[1] - Source: $arguments[2] - Group: $arguments[5]"));
				}
			}
		}
	}
}

function kit__ftp_close($arguments){
	/*
		{:kit__ftp_close[handle]:}
		Returns: none
	*/
	if (!ftp_close($this->ftp_connection[$arguments[0]])){
		$this->kit__log_add(array("Error closing FTP connection! - ID: $arguments[0]"));
	}
}

Any help would be appreciated, I’m happy to pay for an expert as well.

Thank you!

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.