-
Notifications
You must be signed in to change notification settings - Fork 19
Expand file tree
/
Copy pathModel_PDF.php
More file actions
2698 lines (2282 loc) · 77.9 KB
/
Copy pathModel_PDF.php
File metadata and controls
2698 lines (2282 loc) · 77.9 KB
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
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<?php
namespace GFPDF\Model;
use Exception;
use GF_Field;
use GFCommon;
use GFFormsModel;
use GFPDF\Controller\Controller_PDF;
use GFPDF\Helper\Fields\Field_Default;
use GFPDF\Helper\Fields\Field_Products;
use GFPDF\Helper\Helper_Abstract_Field_Products;
use GFPDF\Helper\Helper_Abstract_Fields;
use GFPDF\Helper\Helper_Abstract_Form;
use GFPDF\Helper\Helper_Abstract_Model;
use GFPDF\Helper\Helper_Abstract_Options;
use GFPDF\Helper\Helper_Data;
use GFPDF\Helper\Helper_Form;
use GFPDF\Helper\Helper_Interface_Field_Pdf_Config;
use GFPDF\Helper\Helper_Interface_Url_Signer;
use GFPDF\Helper\Helper_Misc;
use GFPDF\Helper\Helper_Notices;
use GFPDF\Helper\Helper_Options_Fields;
use GFPDF\Helper\Helper_PDF;
use GFPDF\Helper\Helper_Templates;
use GFPDF_Vendor\Mpdf\Mpdf;
use GFPDF_Vendor\Spatie\UrlSigner\Exceptions\InvalidSignatureKey;
use GFQuiz;
use GFResults;
use GP_Populate_Anything_Live_Merge_Tags;
use GFPDF_Vendor\Psr\Log\LoggerInterface;
use RecursiveDirectoryIterator;
use RecursiveIteratorIterator;
use WP_Error;
/**
* @package Gravity PDF
* @copyright Copyright (c) 2026, Blue Liquid Designs
* @license http://opensource.org/licenses/gpl-2.0.php GNU Public License
*/
/* Exit if accessed directly */
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
/**
* Model_PDF
*
* Handles all the PDF display logic
*
* @since 4.0
*
* @method Controller_PDF getController
*/
class Model_PDF extends Helper_Abstract_Model {
/**
* Holds the abstracted Gravity Forms API specific to Gravity PDF
*
* @var Helper_Form
*
* @since 4.0
*/
protected $gform;
/**
* Holds our log class
*
* @var LoggerInterface
*
* @since 4.0
*/
protected $log;
/**
* Holds our Helper_Abstract_Options / Helper_Options_Fields object
* Makes it easy to access global PDF settings and individual form PDF settings
*
* @var Helper_Options_Fields
*
* @since 4.0
*/
protected $options;
/**
* Holds our Helper_Data object
* which we can autoload with any data needed
*
* @var Helper_Data
*
* @since 4.0
*/
protected $data;
/**
* Holds our Helper_Misc object
* Makes it easy to access common methods throughout the plugin
*
* @var Helper_Misc
*
* @since 4.0
*/
protected $misc;
/**
* Holds our Helper_Notices object
* which we can use to queue up admin messages for the user
*
* @var Helper_Notices
*
* @since 4.0
*/
protected $notices;
/**
* Holds our Helper_Templates object
* used to ease access to our PDF templates
*
* @var Helper_Templates
*
* @since 4.0
*/
protected $templates;
/**
* @var Helper_Interface_Url_Signer
*
* @since 5.2
*/
protected $url_signer;
/**
* Setup our view with the needed data and classes
*
* @param Helper_Abstract_Form $gform Our abstracted Gravity Forms helper functions
* @param LoggerInterface $log Our logger class
* @param Helper_Abstract_Options $options Our options class which allows us to access any settings
* @param Helper_Data $data Our plugin data store
* @param Helper_Misc $misc Our miscellaneous class
* @param Helper_Notices $notices Our notice class used to queue admin messages and errors
* @param Helper_Templates $templates
* @param Helper_Interface_Url_Signer $url_signer
*
* @since 4.0
*/
public function __construct( Helper_Abstract_Form $gform, LoggerInterface $log, Helper_Abstract_Options $options, Helper_Data $data, Helper_Misc $misc, Helper_Notices $notices, Helper_Templates $templates, Helper_Interface_Url_Signer $url_signer ) {
/* Assign our internal variables */
$this->gform = $gform;
$this->log = $log;
$this->options = $options;
$this->data = $data;
$this->misc = $misc;
$this->notices = $notices;
$this->templates = $templates;
$this->url_signer = $url_signer;
}
/**
* Authentication request then generate and display PDF
*
* @param string $pid The Gravity Form PDF Settings ID
* @param integer $lid The Gravity Form Entry ID
* @param string $action Whether the PDF should be viewed or downloaded
*
* @return WP_Error
* @since 4.0
* @since 7.0 View/Download PDF creation workflow standardized with Save PDF workflow
*/
public function process_pdf( $pid, $lid, $action = 'view' ) {
/* Get entry */
$entry = $this->gform->get_entry( $lid );
if ( is_wp_error( $entry ) ) {
$this->log->error(
'Invalid Entry',
[
'entry_id' => $lid,
'WP_Error_Message' => $entry->get_error_message(),
'WP_Error_Code' => $entry->get_error_code(),
]
);
return $entry; /* return error */
}
/* Get PDF setting */
$settings = $this->options->get_pdf( $entry['form_id'], $pid );
if ( is_wp_error( $settings ) ) {
$this->log->error(
'Invalid PDF Settings',
[
'entry' => $entry,
'WP_Error_Message' => $settings->get_error_message(),
'WP_Error_Code' => $settings->get_error_code(),
]
);
return $settings; /* return error */
}
/*
* Prior to 6.12 this action was saved to the PDF settings, passed to Helper_PDF, and used to
* stream the document to the client correctly. Since 6.12, we no longer need to pass this value
* to the underlying PDF generator. For backwards compatibility we've included this in case any
* user-land code makes use of it in their custom middleware.
*/
$settings['pdf_action'] = $action;
/*
* Authenticate the request to prevent unauthorized access to the PDF
*
* Default middleware filters include:
* - middle_public_access
* - middle_signed_url_access
* - middle_active
* - middle_conditional
* - middle_owner_restriction
* - middle_logged_out_timeout
* - middle_auth_logged_out_user
* - middle_user_capability
*
* If any of the filters return a WP_Error object the request will not be fulfilled
*
* See https://docs.gravitypdf.com/developers/filters/gfpdf_pdf_middleware/ for more details about this filter
*/
$middleware = apply_filters( 'gfpdf_pdf_middleware', false, $entry, $settings );
if ( is_wp_error( $middleware ) ) {
$this->log->error(
'PDF Authentication Failure.',
[
'entry' => $entry,
'settings' => $settings,
'WP_Error_Message' => $middleware->get_error_message(),
'WP_Error_Code' => $middleware->get_error_code(),
]
);
return $middleware;
}
/*
* Normalize the PDF action
* The PDF cache introduced in 6.12 relies on a hash generated from the form, entry, and pdf settings
* To prevent cache misses we need to ensure we don't unnecessarily modify the settings array
*/
unset( $settings['pdf_action'] );
$action = apply_filters( 'gfpdfe_pdf_output_type', $action ); /* Backwards compat */
$action = in_array( $action, [ 'view', 'download' ], true ) ? $action : 'view';
/* Get the PDF document for the request */
$form = apply_filters( 'gfpdf_current_form_object', $this->gform->get_form( $entry['form_id'] ), $entry, __FUNCTION__ );
do_action( 'gfpdf_view_or_download_pdf', $form, $entry, $settings );
/*
* Support the print dialog option
* The PDF document embeds this preference directly in the source code so we need to
* force the cache to be bypassed.
*/
if ( rgget( 'print' ) === '1' ) {
$settings['print'] = true;
}
$path_to_pdf = $this->generate_and_save_pdf( $entry, $settings );
/* Send error upstream for logging and output */
if ( is_wp_error( $path_to_pdf ) ) {
return $path_to_pdf;
}
do_action( 'gfpdf_post_view_or_download_pdf', $path_to_pdf, $form, $entry, $settings, $action );
$this->send_pdf_to_browser( $path_to_pdf, $action );
}
/**
* Apply filters to particular settings to maintain backwards compatibility
* Note: If you want to modify the $settings array you should use the new "gfpdf_pdf_config" filter instead
*
* @param array $settings The PDF settings array
* @param array $entry
*
* @return array The $settings array
*
* @since 4.0
*/
public function apply_backwards_compatibility_filters( $settings, $entry ) {
$form = apply_filters( 'gfpdf_current_form_object', $this->gform->get_form( $entry['form_id'] ), $entry, __FUNCTION__ );
$settings['filename'] = $this->misc->remove_extension_from_string( apply_filters( 'gfpdfe_pdf_name', $settings['filename'], $form, $entry ) );
$settings['template'] = $this->misc->remove_extension_from_string( apply_filters( 'gfpdfe_template', $settings['template'], $form, $entry ), '.php' );
if ( isset( $settings['orientation'] ) ) {
$settings['orientation'] = apply_filters( 'gfpdf_orientation', $settings['orientation'], $form, $entry );
}
if ( isset( $settings['security'] ) ) {
$settings['security'] = $this->misc->update_deprecated_config( apply_filters( 'gfpdf_security', $settings['security'], $form, $entry ) );
}
if ( isset( $settings['privileges'] ) ) {
$settings['privileges'] = apply_filters( 'gfpdf_privilages', $settings['privileges'], $form, $entry );
}
if ( isset( $settings['password'] ) ) {
$settings['password'] = apply_filters( 'gfpdf_password', $settings['password'], $form, $entry );
}
if ( isset( $settings['master_password'] ) ) {
$settings['master_password'] = apply_filters( 'gfpdf_master_password', $settings['master_password'], $form, $entry );
}
if ( isset( $settings['rtl'] ) ) {
$settings['rtl'] = $this->misc->update_deprecated_config( apply_filters( 'gfpdf_rtl', $settings['rtl'], $form, $entry ) );
}
return $settings;
}
/**
* Check if the current PDF trying to be viewed has public access enabled
* If it does, we'll remove some of our middleware filters to allow this feature
*
* @param boolean|object $action
* @param array $entry The Gravity Forms Entry
* @param array $settings The Gravity Form PDF Settings
*
* @return boolean|object
*
* @since 4.0
*/
public function middle_public_access( $action, $entry, $settings ) {
if ( isset( $settings['public_access'] ) && 'Yes' === $settings['public_access'] ) {
remove_filter( 'gfpdf_pdf_middleware', [ $this, 'middle_owner_restriction' ], 40 );
remove_filter( 'gfpdf_pdf_middleware', [ $this, 'middle_logged_out_timeout' ], 50 );
remove_filter( 'gfpdf_pdf_middleware', [ $this, 'middle_auth_logged_out_user' ], 60 );
remove_filter( 'gfpdf_pdf_middleware', [ $this, 'middle_user_capability' ], 70 );
$this->log->notice(
'Public access enabled for current PDF',
[
'entry_id' => $entry['id'],
'pdf_id' => $settings['id'],
]
);
}
return $action;
}
/**
* Check if a signed URL exists and validate. If it passes, disable the remaining middleware capabilities
*
* @param boolean|object $action
* @param array $entry The Gravity Forms Entry
* @param array $settings The Gravity Form PDF Settings
*
* @return boolean|object
*
* @since 5.1
*/
public function middle_signed_url_access( $action, $entry, $settings ) {
/* phpcs:ignore WordPress.Security.NonceVerification.Recommended */
if ( isset( $_GET['expires'] ) && isset( $_GET['signature'] ) && isset( $_SERVER['HTTP_HOST'] ) && isset( $_SERVER['REQUEST_URI'] ) ) {
try {
$protocol = isset( $_SERVER['HTTPS'] ) && $_SERVER['HTTPS'] === 'on' ? 'https://' : 'http://';
$domain = $_SERVER['HTTP_HOST'];
$request = $_SERVER['REQUEST_URI'];
$url = esc_url_raw( $protocol . $domain . $request );
if ( $this->url_signer->verify( $url ) ) {
remove_filter( 'gfpdf_pdf_middleware', [ $this, 'middle_owner_restriction' ], 40 );
remove_filter( 'gfpdf_pdf_middleware', [ $this, 'middle_logged_out_timeout' ], 50 );
remove_filter( 'gfpdf_pdf_middleware', [ $this, 'middle_auth_logged_out_user' ], 60 );
remove_filter( 'gfpdf_pdf_middleware', [ $this, 'middle_user_capability' ], 70 );
$this->log->notice(
'Valid PDF Signing Request',
[
'entry_id' => $entry['id'],
'pdf_id' => $settings['id'],
'url' => $url,
'protocol' => $protocol, /* Logged to a plain text file */
'domain' => $domain, /* Logged to a plain text file */
'request_uri' => $request, /* Logged to a plain text file */
]
);
} else {
$this->log->warning(
'Invalid PDF Signing Request',
[
'entry_id' => $entry['id'],
'pdf_id' => $settings['id'],
'url' => $url,
'protocol' => $protocol, /* Logged to a plain text file */
'domain' => $domain, /* Logged to a plain text file */
'request_uri' => $request, /* Logged to a plain text file */
]
);
}
} catch ( InvalidSignatureKey $e ) {
}
}
return $action;
}
/**
* Check if the current PDF trying to be viewed is active
*
* @param boolean|object $action
* @param array $entry The Gravity Forms Entry
* @param array $settings The Gravity Form PDF Settings
*
* @return boolean|object
*
* @since 4.0
*/
public function middle_active( $action, $entry, $settings ) {
if ( ! is_wp_error( $action ) ) {
if ( $settings['active'] !== true ) {
return new WP_Error( 'inactive', esc_html__( 'The PDF configuration is not currently active.', 'gravity-pdf' ) );
}
}
return $action;
}
/**
* Check if the current PDF trying to be viewed has conditional logic which passes
*
* @param boolean|object $action
* @param array $entry The Gravity Forms Entry
* @param array $settings The Gravity Form PDF Settings
*
* @return boolean|object
*
* @since 4.0
*/
public function middle_conditional( $action, $entry, $settings ) {
if ( ! is_wp_error( $action ) ) {
if ( ! $this->misc->conditional_logic_passes( $settings, $entry ) ) {
return new WP_Error( 'conditional_logic', esc_html__( 'PDF conditional logic requirements have not been met.', 'gravity-pdf' ) );
}
}
return $action;
}
/**
* If the owner is restricted and the user is not logged in, prompt to log in
*
* @param boolean|object $action
* @param array $entry The Gravity Forms Entry
* @param array $settings The Gravity Form PDF Settings
*
* @return boolean|object
*
* @throws Exception
* @since 4.0
*/
public function middle_owner_restriction( $action, $entry, $settings ) {
/* ensure another middleware filter hasn't already done validation */
if ( ! is_wp_error( $action ) ) {
/* get the setting */
$owner_restriction = $settings['restrict_owner'] ?? 'No';
if ( $owner_restriction === 'Yes' && ! is_user_logged_in() ) {
$this->log->notice(
'Restrict Owner Global Setting Enabled. Prompting logged-out user to login.',
[
'entry_id' => $entry['id'],
'settings_id' => $settings['id'],
]
);
/* prompt user to login */
auth_redirect();
}
}
return $action;
}
/**
* Check the "Logged Out Timeout" global setting and validate it against the current user
*
* @param boolean|object $action
* @param array $entry The Gravity Forms Entry
* @param array $settings The Gravity Form PDF Settings
*
* @return boolean|object
*
* @throws Exception
* @since 4.0
*/
public function middle_logged_out_timeout( $action, $entry, $settings ) {
/* ensure another middleware filter hasn't already done validation */
if ( ! is_wp_error( $action ) ) {
/* only check if PDF timed out if our logged out restriction is not 'Yes' and the user is not logged in */
if ( ! is_user_logged_in() && $this->is_current_pdf_owner( $entry, 'logged_out' ) === true ) {
/* get the global PDF settings */
$timeout = (int) $this->options->get_option( 'logged_out_timeout', '20' );
/* if '0' there is no timeout, or if the logged out restrictions are enabled we'll ignore this */
if ( $timeout !== 0 ) {
$timeout_stamp = 60 * $timeout; /* 60 seconds multiplied by number of minutes */
$entry_created = strtotime( $entry['date_created'] ); /* get entry timestamp */
$timeout_expires = $entry_created + $timeout_stamp; /* get the timeout expiry based on the entry created time */
$current_time = time();
/* compare our two timestamps and throw error if outside the timeout */
if ( $current_time > $timeout_expires ) {
/* if there is no user account assigned to this entry throw error */
if ( empty( $entry['created_by'] ) ) {
$this->log->notice(
'Logged Out Timeout Expired. Showing Error Message.',
[
'entry_id' => $entry['id'],
'settings_id' => $settings['id'],
'current_time' => $current_time,
'timeout_expires' => $timeout_expires,
]
);
return new WP_Error( 'timeout_expired', esc_html__( 'Your PDF is no longer accessible.', 'gravity-pdf' ) );
} else {
$this->log->notice(
'Logged Out Timeout Expired but user assigned to the entry. Redirecting to Login.',
[
'entry_id' => $entry['id'],
'settings_id' => $settings['id'],
'current_time' => $current_time,
'timeout_expires' => $timeout_expires,
]
);
/* prompt to login */
auth_redirect();
}
}
}
}
}
return $action;
}
/**
* Check if the current user attempting to access is the PDF owner
*
* @param array $entry The Gravity Forms Entry
* @param string $type The authentication type we should use
*
* @return boolean
*
* @since 4.0
*/
public function is_current_pdf_owner( $entry, $type = 'all' ) {
$owner = false;
/* check if the user is logged in and the entry is assigned to them */
if ( $type === 'all' || $type === 'logged_in' ) {
if ( is_user_logged_in() && (int) $entry['created_by'] === get_current_user_id() ) {
$owner = true;
$this->log->notice(
'Current logged-in user is the owner of the entry',
[
'entry_id' => $entry['id'],
'created_by' => $entry['created_by'],
]
);
}
}
if ( $type === 'all' || $type === 'logged_out' ) {
$user_ip = filter_var( GFFormsModel::get_ip(), FILTER_VALIDATE_IP );
$server_ip = filter_var( $_SERVER['SERVER_ADDR'] ?? '127.0.0.1', FILTER_VALIDATE_IP );
$entry_ip = filter_var( $entry['ip'], FILTER_VALIDATE_IP );
/* check if the user IP matches the entry IP */
if (
! empty( $entry_ip ) &&
$entry_ip === $user_ip &&
$entry_ip !== $server_ip
) {
$owner = true;
$this->log->notice(
'Current logged-out user matches the entry IP address',
[
'entry_id' => $entry['id'],
'user_ip' => $user_ip,
'server_ip' => $server_ip,
]
);
}
}
return $owner;
}
/**
* Check if the user is logged out and authenticate as needed
*
* @param boolean|object $action
* @param array $entry The Gravity Forms Entry
* @param array $settings The Gravity Form PDF Settings
*
* @return boolean|object
*
* @throws Exception
* @since 4.0
*/
public function middle_auth_logged_out_user( $action, $entry, $settings ) {
if ( ! is_wp_error( $action ) ) {
/* check if the user is not the current entry owner */
if ( ! is_user_logged_in() && $this->is_current_pdf_owner( $entry, 'logged_out' ) === false ) {
/* check if there is actually a user who owns entry */
if ( ! empty( $entry['created_by'] ) ) {
$this->log->notice(
'The logged out security checks failed, but there is a logged-in user assigned to the entry. Prompting user to login.',
[
'entry_id' => $entry['id'],
'settings_id' => $settings['id'],
]
);
/* prompt user to login to get access */
auth_redirect();
} else {
$this->log->warning(
'The logged out security checks failed and there is no logged-in user assigned to the entry.',
[
'entry_id' => $entry['id'],
'settings_id' => $settings['id'],
]
);
/* there's no returning, throw generic error */
return new WP_Error( 'access_denied', esc_html__( 'You do not have access to view this PDF.', 'gravity-pdf' ) );
}
}
}
return $action;
}
/**
* Verify the logged-in user can view the PDF
*
* If owner restrictions are enabled, check if the user as correct capability to view
* If owner restrictions are disabled, check if the user is the entry owner
*
* @param boolean|object $action
* @param array $entry The Gravity Forms Entry
* @param array $settings The Gravity Form PDF Settings
*
* @return boolean|object
*
* @since 4.0
*/
public function middle_user_capability( $action, $entry, $settings ) {
if ( ! is_wp_error( $action ) ) {
/* check if the user is logged in but is not the current owner */
$owner_restriction = $settings['restrict_owner'] ?? 'No';
if (
is_user_logged_in() &&
! $this->can_user_view_pdf_with_capabilities() &&
(
$owner_restriction === 'Yes' ||
$this->is_current_pdf_owner( $entry, 'logged_in' ) === false
)
) {
return new WP_Error( 'access_denied', esc_html__( 'You do not have access to view this PDF.', 'gravity-pdf' ) );
}
}
return $action;
}
/**
* Check if the logged in user has permission to view the PDF
*
* @param int|null $user_id
*
* @return bool
*
* @since 6.8
*/
public function can_user_view_pdf_with_capabilities( $user_id = null ) {
$admin_permissions = $this->options->get_option( 'admin_capabilities', [ 'gravityforms_view_entries' ] );
/* loop through permissions and check if the current user has any of those capabilities */
$can_user_view_pdf = false;
foreach ( $admin_permissions as $permission ) {
if ( $this->gform->has_capability( $permission, $user_id ) ) {
$can_user_view_pdf = true;
break;
}
}
return $can_user_view_pdf;
}
/**
* Display PDF on Gravity Form entry list page
*
* @param integer $form_id Gravity Form ID
* @param integer $field_id Current field ID
* @param mixed $value Current value of field
* @param array $entry Entry Information
*
* @return void
*
* @since 4.0
*/
public function view_pdf_entry_list( $form_id, $field_id, $value, $entry ) {
/* Only show the PDF metabox if a user has permission to view the documents */
if ( ! $this->can_user_view_pdf_with_capabilities() ) {
return;
}
$controller = $this->getController();
$pdf_list = $this->get_pdf_display_list( $entry );
if ( empty( $pdf_list ) ) {
return;
}
if ( count( $pdf_list ) > 1 ) {
$args = [
'pdfs' => $pdf_list,
'view' => strtolower( $this->options->get_option( 'default_action' ) ),
];
$controller->view->entry_list_pdf_multiple( $args );
} else {
/* Only one PDF for this form so display a simple 'View PDF' link */
$args = [
'pdf' => array_shift( $pdf_list ),
'view' => strtolower( $this->options->get_option( 'default_action' ) ),
];
$controller->view->entry_list_pdf_single( $args );
}
}
/**
* Get a preformatted list of active PDFs with name and URL
*
* @param array $entry
*
* @return array
*
* @since 4.0
*/
public function get_pdf_display_list( $entry ) {
/* Stores our formatted PDFs */
$args = [];
/* Check if we have any PDFs */
$form = apply_filters( 'gfpdf_current_form_object', $this->gform->get_form( $entry['form_id'] ), $entry, __FUNCTION__ );
$pdfs = ( isset( $form['gfpdf_form_settings'] ) ) ? $this->get_active_pdfs( $form['gfpdf_form_settings'], $entry ) : [];
if ( ! empty( $pdfs ) ) {
foreach ( $pdfs as $settings ) {
$args[] = [
'name' => $this->get_pdf_name( $settings, $entry ),
'view' => $this->get_pdf_url( $settings['id'], $entry['id'], false ),
'download' => $this->get_pdf_url( $settings['id'], $entry['id'], true ),
'settings' => $settings,
'entry_id' => $entry['id'],
'form_id' => $form['id'],
'class' => 'gravitypdf-download-link',
];
}
}
/**
* See https://docs.gravitypdf.com/developers/filters/gfpdf_get_pdf_display_list/ for usage
*
* @since 4.2
*/
return apply_filters( 'gfpdf_get_pdf_display_list', $args, $entry, $form );
}
/**
* Filter out inactive PDFs and those who don't meet the conditional logic
*
* @param array $pdfs The PDF settings array
* @param array $entry The current entry information
*
* @return array The filtered PDFs
*
* @since 4.0
*/
public function get_active_pdfs( $pdfs, $entry ) {
$filtered = [];
$form = apply_filters( 'gfpdf_current_form_object', $this->gform->get_form( $entry['form_id'] ), $entry, __FUNCTION__ );
foreach ( $pdfs as $pdf ) {
if ( $pdf['active'] && $this->misc->conditional_logic_passes( $pdf, $entry ) ) {
$filtered[ $pdf['id'] ] = $pdf;
}
}
/**
* See https://docs.gravitypdf.com/developers/filters/gfpdf_get_active_pdfs/ for usage
*
* @since 4.2
*/
return apply_filters( 'gfpdf_get_active_pdfs', $filtered, $pdfs, $entry, $form );
}
/**
* Generate the PDF Name
*
* @param array $settings The PDF Form Settings
* @param array $entry The Gravity Form entry details
*
* @return string The PDF Name
*
* @since 4.0
*/
public function get_pdf_name( $settings, $entry ) {
$form = apply_filters( 'gfpdf_current_form_object', $this->gform->get_form( $entry['form_id'] ), $entry, __FUNCTION__ );
$name = $this->gform->process_tags( $settings['filename'], $form, $entry );
/* Decode HTML entities */
$name = wp_specialchars_decode( $name, ENT_QUOTES );
/*
* Add filter to modify PDF name
*
* See https://docs.gravitypdf.com/developers/filters/gfpdf_pdf_filename/ for more details about this filter
*/
$name = apply_filters( 'gfpdf_pdf_filename', $name, $form, $entry, $settings );
/* Backwards compatible filter */
$name = apply_filters( 'gfpdfe_pdf_filename', $name, $form, $entry, $settings );
/* Remove any characters that cannot be present in a filename */
$name = $this->misc->strip_invalid_characters( $name );
return $name;
}
/**
* Create a PDF Link based on the current PDF settings and entry
*
* @param integer $pid The PDF Form Settings ID
* @param integer $id The Gravity Form entry ID
* @param boolean $download Whether the PDF should be downloaded or not
* @param boolean $should_print Whether we should mark the PDF to be printed
* @param boolean $esc Whether to escape the URL or not
*
* @return string Direct link to the PDF
*
* @since 4.0
*/
public function get_pdf_url( $pid, $id, $download = false, $should_print = false, $esc = true ) {
global $wp_rewrite;
if ( $esc !== true ) {
_doing_it_wrong( __METHOD__, '$esc has been deprecated. Late-escape the returned value where appropriate.', '6.4.0' );
}
/*
* Patch for WPML which can include the default language as a GET parameter
* See https://github.com/GravityPDF/gravity-pdf/issues/550
*/
$home_url = untrailingslashit( strtok( home_url(), '?' ) );
/* Check if permalinks are enabled, otherwise fall back to our ugly link structure for 4.0 (not the same as our v3 links) */
if ( $wp_rewrite->using_permalinks() ) {
$url = $home_url . '/' . $wp_rewrite->root; /* Handle "almost pretty" permalinks - fix for IIS servers without modrewrite */
$url .= 'pdf/' . $pid . '/' . $id . '/';
if ( $download ) {
$url .= 'download/';
}
$url = user_trailingslashit( $url );
if ( $should_print ) {
$url .= '?print=1';
}
} else {
$url = $home_url . '/?gpdf=1&pid=' . $pid . '&lid=' . $id;
if ( $download ) {
$url .= '&action=download';
}
if ( $should_print ) {
$url .= '&print=1';
}
}
/*
* @since 4.2
*/
$url = apply_filters( 'gfpdf_get_pdf_url', $url, $pid, $id, $download, $should_print, $esc );
return esc_url_raw( $url );
}
/**
* Display the PDF links on the entry detailed section of the admin area
*
* @param array $args Combined form and entry array
*
* @return void
*
* @since 4.0
*/
public function view_pdf_entry_detail( $args ) {
$controller = $this->getController();
$pdf_list = $this->get_pdf_display_list( $args['entry'] );
if ( empty( $pdf_list ) ) {
$controller->view->entry_no_valid_pdf();
return;
}
$pdfs = [
'pdfs' => $pdf_list,
];
$controller->view->entry_detailed_pdf( $pdfs );
}
/**
* Display the PDF metabox in the Gravity Flow inbox
*
* @param array $form
* @param array $entry
* @param $current_step
* @param $args
*
* @return void
*
* @since 6.8
*/
public function view_pdf_gravityflow_inbox( $form, $entry, $current_step, $args ) {
/* Only show the PDF metabox if a user has permission to view the documents */
if ( ! $this->can_user_view_pdf_with_capabilities() ) {
return;
}
$active_pdfs = array_filter(
$form['gfpdf_form_settings'] ?? [],
function ( $pdf ) {
return $pdf['active'] === true;
}
);
/* Only show the metabox if there's an active PDF */
if ( count( $active_pdfs ) === 0 ) {
return;
}
?>
<style type="text/css">
div.gf_entry_wrap #poststuff #gravitypdf-pdf-box-container .inside {
margin: 0;
padding: 0;
max-height: 18rem;
overflow-y: auto;
line-height: 1.4;
font-size: 13px;