I'm developing a document merger that utilizes an FTP site containing hundreds of documents.
FTP Connection Function
private static function establishFTP() {
$ftp_conn = ftp_connect(getenv('FTP_HOST'));
ftp_login($ftp_conn,getenv('FTP_USER'),getenv('FTP_PASS'));
ftp_pasv($ftp_conn,true);
return $ftp_conn;
}
I have a function that connects to the FTP site and generates an array of the documents stored inside a designated directory. It will remove the extensions from any files that are of type .docx
. Then it will filter out all values containing a .
. This removes any extra files that are not a Word document and also removes the FTP root pathings of .
and ..
from the array.
Form Generation Function
public static function generateForm() {
$ftp_conn = self::establishFTP();
$forms = ftp_nlist($ftp_conn,getenv('FTP_DIRECTORY'));
$forms = str_replace('.docx','',$forms);
$forms = array_filter($forms,function($value) {
return strpos($value,'.') === false;
});
ftp_close($ftp_conn);
return view('testDoc')->with(array('forms'=>$forms));
}
My view is very basic right now just to prove functionality. There is lots of design work that needs done to make it more user-friendly and functional.
Blade View
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="_token" content="{{ csrf_token() }}" />
<title>Form</title>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.2.4/jquery.min.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/jqueryui/1.11.4/jquery-ui.min.js"></script>
</head>
<body>
{{ Form::open(array('action'=>'DocumentController@mergeDocument')) }}
@foreach($forms as $doc)
{{ Form::checkbox('documents[]',$doc) }}
{{ Form::label(null,$doc) }}
@endforeach
{{ Form::submit('Submit',array('id'=>'submitBtn')) }}
{{ Form::close() }}
</body>
</html>
Lastly, I have a function that will accept an array of string values from the GUI. It will then establish a FTP connection and grab all documents requested from the FTP site. It will store these in a temp directory until the documents are merged and downloaded to the browser. Then it will unlink all temp files.
Merge and Download Documents Function
public static function mergeDocument() {
$ftp_conn = self::establishFTP();
$forms = Input::get('documents');
$mergeDocs = array();
$mergeName = uniqid('temp/files/merge-') . '.docx';
$documentName = 'policies_' . date('m-d-H-i-s');
foreach($forms as $doc) {
ftp_get($ftp_conn,"temp/files/$doc.docx",getenv('FTP_DIRECTORY') . "/$doc.docx",FTP_BINARY);
$mergeDocs[] = "temp/files/$doc.docx";
}
$firstDoc = array_shift($mergeDocs);
ftp_close($ftp_conn);
$mergeResult = new \Phpdocx\Utilities\MultiMerge();
$mergeResult->mergeDocx($firstDoc,$mergeDocs,$mergeName,array('enforceSectionPageBreak'=>true));
$phpdocx = new \Phpdocx\Create\CreateDocxFromTemplate($mergeName);
$phpdocx->createDocxAndDownload($documentName);
foreach($forms as $doc) {
unlink("temp/files/$doc.docx");
}
unlink($mergeName);
unlink("$documentName.docx");
}
Any suggestions as to the security and design of my app is appreciated.