forked from pimteam/gravityforms-merge-pdfs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
loader.php
280 lines (225 loc) · 7.89 KB
/
loader.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
<?php
/**
* Plugin Name: Gravity Forms Merge PDFs
* Description: Adds a merged PDFs field and inlines PDF uploads into Gravity PDF exports.
* Authors: Gennady Kovshenin, Bob Handzhiev
* Version: 1.4.1
*/
defined( 'ABSPATH' ) || exit;
define( 'GRAVITY_MERGE_PDFS_DNONCE_MULTIPLIER', 687 ); // multiplier for the basic "fake" nonces protection
define( 'GRAVITY_MERGE_PDFS_DNONCE_SECRET', 'XF09=*/=JH' ); // secret word for the basic "fake" nonces protection
define( 'GRAVITY_MERGE_PDFS_USE_NONCES', false ); // whether to use real WP nonces
//use PDFMerger\PDFMerger;
add_action( 'gform_loaded', function() {
require_once __DIR__ . '/class-gf-merge-pdfs-field.php';
} );
function gf_merge_pdfs_get_files( int $entry_id ) : iterable {
$entry = GFAPI::get_entry( $entry_id );
$form = GFAPI::get_form( $entry['form_id'] );
$fields = [];
$errors = [];
if( isset( $form['fields'] ) && !empty( $form['fields'] ) ){
foreach ( $form['fields'] as $field ) {
if ( $field->type == 'fileupload' && $field->cssClass != 'skip_merge' ) {
$fields[] = [
$form['id'],
$field->id,
$entry_id,
];
}
if ( $field->type == 'form' ) {
$parent_entry = new GPNF_Entry( $entry );
foreach ( $parent_entry->get_child_entries( $field->id ) as $nested_entry ) {
$nested_form = GFAPI::get_form( $nested_entry['form_id'] );
foreach ( $nested_form['fields'] as $nested_field ) {
if ( $nested_field->type == 'fileupload' && $nested_field->cssClass != 'skip_merge' ) {
$fields[] = [
$nested_form['id'],
$nested_field->id,
$nested_entry['id'],
];
}
}
}
}
}
}
$files = [];
if( !empty( $fields ) ){
foreach ( $fields as $field ) {
list( $form_id, $field_id, $entry_id ) = $field;
$entry = GFAPI::get_entry( $entry_id );
$form = GFAPI::get_form( $form_id );
$field = GFAPI::get_field( $form, $field_id );
if ( empty( $entry[ $field->id ] ) ) {
continue;
}
foreach ( $field->multipleFiles ? json_decode( $entry[ $field->id ], true ) : [ $entry[ $field->id ] ] as $uri ) {
// $info = $field::get_file_upload_path_info( $uri, $entry_id );
// $file = $info['path'] . $info['file_name']
$file = convert_url_to_path( $uri );
if ( file_exists( $file ) && is_readable( $file ) ) {
if ( strtolower( pathinfo( $file, PATHINFO_EXTENSION ) ) === 'pdf' ) {
$files[] = [
$form_id,
$field_id,
$entry_id,
$file,
$uri,
];
}
}
else {
$errors[] = $file;
}
}
}
}
return [ $files, $errors ];
}
function convert_url_to_path( $url ) {
// Some info got broken due to http / https issue so let's fix it by replacing base URL
$dir = wp_get_upload_dir();
$baseurl = $dir['baseurl'];
if( strstr( $url, 'http://') ) $baseurl = str_replace('https:','http:', $baseurl);
return str_replace(
$baseurl,
$dir['basedir'],
$url
);
}
// Actually merges and outputs the files using shell commands
function gf_merge_pdfs_output( $files, $errors ) {
$dir = wp_upload_dir();
$outputName = $dir['path']."/merged-".$entry_id.".pdf";
// if there are errors, create a file with them
if( count( $errors ) ) {
require('fpdf/fpdf.php');
$file_str = implode(', ', array_map( fn( $e ) => basename($e), $errors ) );
$cnt_files = count($files);
if( $cnt_files ) {
$file_str .= sprintf("\nThere %s %d valid %s for this entry included after this page.", ($cnt_files > 1 ? 'are' : 'is'), $cnt_files, ($cnt_files > 1 ? 'files' : 'file') );
}
else $file_str .= "\nThere are no valid files for this entry.";
$error_file = $dir['path']."/errors-".$entry_id.".pdf";
$pdf = new FPDF();
$pdf->AddPage();
$pdf->SetFont('Arial','B',12);
$pdf->MultiCell( 0 , 10, 'The following files are missing or unreadable: ' . $file_str );
$pdf->Output( 'F', $error_file );
// fake array, we only need path
array_unshift( $files, [0, 0, 0, $error_file, '' ] );
}
$cmd = "gs -q -dNOPAUSE -dBATCH -sDEVICE=pdfwrite -sOutputFile=$outputName ";
//Add each pdf file to the end of the command
foreach($files as $file) {
[$form_id, $field_id, $entry_id, $path, $uri] = $file;
$cmd .= $path." ";
}
$result = shell_exec($cmd);
header('Cache-control: private');
header('Content-Type: application/pdf');
//header('Content-Length: '.filesize($local_file));
//header('Content-Disposition: attachment; filename="merged.pdf";');
readfile( $outputName );
exit;
}
add_action( 'init', function() {
if ( ! $entry_id = $_GET['gf_merge_pdfs'] ?? 0 ) {
return;
}
if ( GRAVITY_MERGE_PDFS_USE_NONCES and ! wp_verify_nonce( $_GET['nonce'] ?? null, "gf_merge_pdfs_$entry_id" ) ) {
return;
}
if( !is_user_logged_in() ) return;
// check fake nonce
$check_dnonce = substr( md5( ( $entry_id * GRAVITY_MERGE_PDFS_DNONCE_MULTIPLIER ) . GRAVITY_MERGE_PDFS_DNONCE_SECRET ), 0, 10 );
if( $check_dnonce != $_GET['dnonce'] ) return;
[$files, $errors] = gf_merge_pdfs_get_files( $entry_id );
gf_merge_pdfs_output( $files, $errors );
} );
add_filter( 'gfpdf_mpdf_class', function( $mpdf, $form, $entry, $settings, $helper ) {
if ( ! GFCommon::get_fields_by_type( $form, 'merge_pdfs' ) ) {
return $mpdf;
}
[$files, $errors] = gf_merge_pdfs_get_files( $entry['id'] );
if ( ! $files ) {
return $mpdf;
}
file_put_contents( $output = tempnam( get_temp_dir(), 'merge_pdfs' ), $mpdf->Output( '', 'S' ) );
//die($output);
array_unshift($files, [0, 0,0, $output, '']);
switch ( $helper->get_output_type() ) {
case 'DISPLAY':
gf_merge_pdfs_output( $files, $errors );
exit;
case 'DOWNLOAD':
gf_merge_pdfs_output( $files, $errors );
exit;
case 'SAVE':
return new class( ) {
public function __construct( ) {
//
}
public function Output() {
gf_merge_pdfs_output( $files, $errors );
}
};
}
return $mpdf;
}, 10, 5 );
false && add_filter( 'gravityflowpdf_mpdf', function( $mpdf, $body, $file_path, $entry, $step ) {
$form = GFAPI::get_form( $entry['form_id'] );
if ( ! GFCommon::get_fields_by_type( $form, 'merge_pdfs' ) ) {
return $mpdf;
}
if ( ! $files = gf_merge_pdfs_get_files( $entry['id'] ) ) {
return $mpdf;
}
$mpdf->WriteHTML( $body );
file_put_contents( $output = tempnam( get_temp_dir(), 'merge_pdfs' ), $mpdf->Output( '', 'S' ) );
return new class( $mpdf ) {
public function __construct( $mpdf ) {
$this->mpdf = $mpdf;
}
public function Output() {
array_unshift($files, [0, 0,0, $file_path, '']);
gf_merge_pdfs_output( $files, [] );
}
public function __call( $f, $args ) {
return call_user_func_array( [ $this->mpdf, $f ], $args );
}
};
}, 10, 5 );
/*
false && add_filter( 'gravityflowpdf_mpdf', function( $mpdf, $body, $file_path, $entry, $step ) {
$form = GFAPI::get_form( $entry['form_id'] );
if ( ! GFCommon::get_fields_by_type( $form, 'merge_pdfs' ) ) {
return $mpdf;
}
if ( ! $files = gf_merge_pdfs_get_files( $entry['id'] ) ) {
return $mpdf;
}
$mpdf->WriteHTML( $body );
file_put_contents( $output = tempnam( get_temp_dir(), 'merge_pdfs' ), $mpdf->Output( '', 'S' ) );
require __DIR__ . '/lib/PDFMerger/PDFMerger.php';
$pdf = new PDFMerger;
$pdf->addPDF( $output );
foreach ( $files as $file ) {
list( $form_id, $field_id, $entry_id, $path ) = $file;
$pdf->addPDF( $path );
}
return new class( $pdf, $mpdf ) {
public function __construct( $pdf, $mpdf ) {
$this->pdf = $pdf;
$this->mpdf = $mpdf;
}
public function Output() {
file_put_contents( $file_path, $this->pdf->merge( 'string' ) );
}
public function __call( $f, $args ) {
return call_user_func_array( [ $this->mpdf, $f ], $args );
}
};
}, 10, 5 );
*/