-
Notifications
You must be signed in to change notification settings - Fork 140
Expand file tree
/
Copy pathclass-llms-admin-export-download.php
More file actions
78 lines (65 loc) · 1.95 KB
/
class-llms-admin-export-download.php
File metadata and controls
78 lines (65 loc) · 1.95 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
<?php
/**
* Serves Export CSVs on the admin panel
*
* @package LifterLMS/Admin/Classes
*
* @since 3.28.1
* @version 7.5.0
*/
defined( 'ABSPATH' ) || exit;
/**
* LLMS_Admin_Export_Download class
*
* @since 3.28.1
*/
class LLMS_Admin_Export_Download {
/**
* Constructor.
*
* @since 3.28.1
* @version 3.28.1
*/
public function __construct() {
add_action( 'admin_init', array( $this, 'maybe_serve_export' ) );
}
/**
* Serve an export file as a download.
*
* @since 3.28.1
* @since 5.9.0 Stop using deprecated `FILTER_SANITIZE_STRING`.
* @since 7.5.0 Check nonce and only consider the basename of the file to be downloaded.
*
* @return void
*/
public function maybe_serve_export() {
$export = llms_filter_input( INPUT_GET, 'llms-dl-export', FILTER_SANITIZE_FULL_SPECIAL_CHARS );
if ( ! $export ) {
return;
}
// Verify nonce.
if ( ! isset( $_REQUEST['llms_dl_export_nonce'] ) || ! wp_verify_nonce( sanitize_text_field( wp_unslash( $_REQUEST['llms_dl_export_nonce'] ) ), LLMS_Abstract_Exportable_Admin_Table::EXPORT_NONCE_ACTION ) ) {
wp_die( esc_html__( 'Cheatin’ huh?', 'lifterlms' ) );
}
// Only allow people who can view reports view exports.
if ( ! current_user_can( 'view_others_lifterlms_reports' ) && ! current_user_can( 'view_lifterlms_reports' ) ) {
wp_die( esc_html__( 'Cheatin’ huh?', 'lifterlms' ) );
}
$path = LLMS_TMP_DIR . basename( $export );
if ( ! file_exists( $path ) ) {
wp_die( esc_html__( 'Cheatin’ huh?', 'lifterlms' ) );
}
$info = pathinfo( $path );
if ( 'csv' !== $info['extension'] ) {
wp_die( esc_html__( 'Cheatin’ huh?', 'lifterlms' ) );
}
header( 'Content-Type: text/csv' );
header( 'Content-Disposition: attachment; filename="' . $export . '"' );
$file = file_get_contents( $path );
unlink( $path );
// phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
echo $file;
exit;
}
}
return new LLMS_Admin_Export_Download();