...

View Full Version : File Sender Script



idalatob
10-25-2010, 12:23 PM
Hi all, please take note that PHPSECLIB is required. Please google for more information!! Ill post a cakephp porting soon!
VERY IMPORTANT!
This is just a rough hack to get this out here, please ammend the class to suit your needs. You might notice the odd error here or there, this is because i shredded this out of a 1000 line code block and plonked it in here. There might be a few undefined variables, but nothing you cant fix :-D



set_include_path(get_include_path() . PATH_SEPARATOR . 'phpseclib');

class fileSenderPro{
var $baseFilePath = "";
var $errors = 0;
function sendWrapper($method,$con_parameters,$filename){
if (method_exists('fileSenderPro',$method)):
$this->$method($con_parameters,$filename);
else:
$this->generateErrorCode(4);
endif;
if ($this->errors == 0){
return true;
} else {
return false;
}
}
private function generateErrorCode($code){
//1 - Unable to connect to ssh server
$this->errors += $code;
}
private function send_mail_attachments($to, $from, $subject, $message, $filename, $headers = '') {
$unique_sep = md5(uniqid(time()));
$headers .= "From: $from\n";
$headers .= "MIME-Version: 1.0\nContent-Type:"
." multipart/mixed;boundary=\"$unique_sep\";\n";
$headers .= "charset=\"iso-8859-1\"\nContent-Transfer-Encoding:"
."7bit\n\n";
$headers .= "If you are reading this, then your e-mail client does"
."not support MIME.\n";
$headers .= "Sent By Me\n\n";
$headers .= "--$unique_sep\n";
$headers .= "Content-Type: text/plain; charset=\"iso-8859-1\"\n";
$headers .= "Content-Transfer-Encoding: 7bit\n\n";
$headers .= $message."\n\n";
if(is_array($filename)) {
foreach($filename as $val) {
if(file_exists($val['file'])) {
$headers .= "--$unique_sep\n";
$headers .= "Content-Type: {$val['mimetype']}; ".
"name=\"{$val['filename']}\"\n";
$headers .= "Content-Transfer-Encoding: ".
"base64\n";
$headers .= "Content-Disposition: attachment\n\n";
$filedata = implode(file($val['file']), '');
$headers .= chunk_split(base64_encode($filedata));
} else {
//internal error message file not found
$this->generateErrorCode(32);
}
}
} else {
//internal error, file not supplied as array
$this->generateErrorCode(8);
}
$headers .= "--$unique_sep--\n";
if(!mail($to, $subject, $message, $headers)):
//error message sending failed
$this->generateErrorCode(16);
endif;

}
function translateErrorCodesToMessage($code){
$errorMessage = array();
$errorMessages = array(
1=>"ERROR : Unable to connect to server.",
2=>"ERROR : Unable to create requested directory structure.",
4=>"ERROR : The requested sending function was not found.",
8=>"ERROR : File sender provided invalid details. Please contact an administrator.",
16=>"ERROR : Email was unable to be sent.",
32=>"ERROR : File sender could not fetch file details. Please contact an administrator.");
foreach ($errorMessages as $key=>$message){
if (($key & $code) != 0):
$errorMessage[] = $message;
endif;
}
if (count($errorMessage)):
$errorMessage = implode("|",$errorMessage);
else:
$errorMessage = "File transfer completed.";
endif;
return $errorMessage;
}
function ftp($con_parameters,$filename){
//for the time being just point to sftp
//got over trying to negotiate through passive and active bleh
$this->sftp($con_parameters,$filename);
/*
//this ground control to major tom
$connection = ftp_connect($con_parameters['ftp_server']);
//Take your protein pills and put your helmet on

$login = ftp_login($connection, $con_parameters['ftp_username'], $con_parameters['ftp_password']);

//Ground control to major Tom, your circuits dead, there's something wrong
if (!$connection || !$login) { die('Connection attempt failed!'); }
$required_folders = explode("/",trim($con_parameters['ftp_subaddress'],"/"));
foreach ($required_folders as $folder):
if (!@ftp_chdir($connection,$folder)):
if (!ftp_mkdir($connection,$folder)):
$this->generateErrorCode(2);
break;
else:
@ftp_chdir($connection,$folder);
endif;
endif;
endforeach;
//write a temporary file
$handle = fopen("cache/". $con_parameters['filename'], "w+");
fwrite($handle, $con_parameters['data']);
fclose($handle);
$upload = ftp_put($connection, $con_parameters['filename'],"cache/". $con_parameters['filename'], FTP_BINARY);
if (!$upload) { $this->generateErrorCode(8); }
echo(ftp_pwd($connection));
ftp_close($connection);
*/
}
function email($con_parameters,$filename){
$today = date("Y-m-d");
$message = "Please see attachment for report details. Please do not reply to this email, for any queries contact reports@mysite.com.";
$file_array = array(0=>array(
'file'=>$this->baseFilePath . $filename,
'mimetype'=>'application/zip',
'filename'=>$filename));
$this->send_mail_attachments($con_parameters['email_to_send'], "reports@mysite.com","Subject",$message, $file_array);

}
function sftp($con_parameters,$filename){
$sftp = new Net_SFTP($con_parameters['ftp_server']);
if (!$sftp->login($con_parameters['ftp_username'], $con_parameters['ftp_password'])):
$this->generateErrorCode(1);
else:
//try and navigate to the specified folders
$required_folders = explode("/",trim($con_parameters['ftp_subaddress'],"/"));
//start the recursive navigating!
$current_folder = $sftp->pwd();
foreach ($required_folders as $folder):
//see if we can transverse to this folder
if (!$sftp->chdir($folder)):
if (!$sftp->mkdir($folder)):
$this->generateErrorCode(2);
break;
else:
$sftp->chdir($folder);
endif;
endif;
endforeach;
$sftp->put($con_parameters['filename'], $con_parameters['data']);
endif;
}
}

idalatob
10-25-2010, 12:41 PM
Damn.... Just lost my whole usage example. sigh. here we go again.

Anyways, please understand i hacked this together in an hour or so. Please dont flame me, if you can improve it let me know and post a suggestion and ill fix it for you! This is more for an idea of how files can be sent pretty easily!

Im not a PHP god and I dont pretend to be, so any critism would be welcome and appreciated!



require_once('Net/SFTP.php');
$sender = New FileSenderPro;
$built_data = array();
$basepath = "/path/to/file/";
$filename = "the_File.zip";
$built_data['email_to_send'] = "email_to_send@someguy.com";
$built_data['data'] = file_get_contents($basepath . $filename);
$built_data['ftp_server'] = "server.server.com";
$built_data['ftp_username'] = "username";
$built_data['ftp_password'] = "complicated_password";
$built_data['ftp_subaddress'] = "/path/to/file/";
$built_data['filename'] = "new_filename.zip";
if($sender->sendWrapper($method,$built_data,$filename)):
//it worked
else:
//something went seriously wrong!
echo($sender->translateErrorCodesToMessage($sender->errors));
endif;



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum