Passed
Push — master ( d9c503...543c87 )
by Brian
13:46
created

WPInv_Reports::discount_use_export()   B

Complexity

Conditions 6
Paths 10

Size

Total Lines 52
Code Lines 32

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 6
eloc 32
c 0
b 0
f 0
nc 10
nop 0
dl 0
loc 52
rs 8.7857

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
if ( ! defined( 'ABSPATH' ) ) {
3
    exit; // Exit if accessed directly
4
}
5
6
class WPInv_Reports {
7
    private $section = 'wpinv_reports';
0 ignored issues
show
introduced by
The private property $section is not used, and could be removed.
Loading history...
8
    private $wp_filesystem;
9
    private $export_dir;
10
    private $export_url;
11
    private $export;
12
    public $filetype;
13
    public $per_page;
14
    
15
    public function __construct() {
16
        $this->init();
17
        $this->includes();
18
        $this->actions();
19
    }
20
    
21
    public function init() {
22
        global $wp_filesystem;
23
24
        if ( empty( $wp_filesystem ) ) {
25
            require_once( ABSPATH . '/wp-admin/includes/file.php' );
26
            WP_Filesystem();
27
            global $wp_filesystem;
28
        }
29
        $this->wp_filesystem    = $wp_filesystem;
30
        
31
        $this->export_dir       = $this->export_location();
32
        $this->export_url       = $this->export_location( true );
33
        $this->export           = 'invoicing';
34
        $this->filetype         = 'csv';
35
        $this->per_page         = 20;
36
        
37
        do_action( 'wpinv_class_reports_init', $this );
38
    }
39
    
40
    public function includes() {
41
        do_action( 'wpinv_class_reports_includes', $this );
42
    }
43
    
44
    public function actions() {
45
        if ( is_admin() ) {
46
            add_action( 'admin_menu', array( $this, 'add_submenu' ), 10 );
47
            add_action( 'wpinv_reports_tab_export', array( $this, 'export' ) );
48
            add_action( 'wp_ajax_wpinv_ajax_export', array( $this, 'ajax_export' ) );
49
            add_action( 'wp_ajax_wpinv_ajax_discount_use_export', array( $this, 'discount_use_export' ) );
50
            
51
            // Export Invoices.
52
            add_action( 'wpinv_export_set_params_invoices', array( $this, 'set_invoices_export' ) );
53
            add_filter( 'wpinv_export_get_columns_invoices', array( $this, 'get_invoices_columns' ) );
54
            add_filter( 'wpinv_export_get_data_invoices', array( $this, 'get_invoices_data' ) );
55
            add_filter( 'wpinv_get_export_status_invoices', array( $this, 'invoices_export_status' ) );
56
        }
57
        do_action( 'wpinv_class_reports_actions', $this );
58
    }
59
    
60
    public function add_submenu() {
61
        global $wpi_reports_page;
62
        $wpi_reports_page = add_submenu_page( 'wpinv', __( 'Reports', 'invoicing' ), __( 'Reports', 'invoicing' ), wpinv_get_capability(), 'wpinv-reports', array( $this, 'reports_page' ) );
63
    }
64
    
65
    public function reports_page() {
66
        if ( !wp_script_is( 'postbox', 'enqueued' ) ) {
67
            wp_enqueue_script( 'postbox' );
68
        }
69
        if ( !wp_script_is( 'jquery-ui-datepicker', 'enqueued' ) ) {
70
            wp_enqueue_script( 'jquery-ui-datepicker' );
71
        }
72
        
73
        $current_page = admin_url( 'admin.php?page=wpinv-reports' );
74
        $active_tab = isset( $_GET['tab'] ) ? sanitize_text_field( $_GET['tab'] ) : 'export';
75
        ?>
76
        <div class="wrap wpi-reports-wrap">
77
            <h1><?php echo esc_html( __( 'Reports', 'invoicing' ) ); ?></h1>
78
            <h2 class="nav-tab-wrapper wp-clearfix">
79
                <a href="<?php echo add_query_arg( array( 'tab' => 'export', 'settings-updated' => false ), $current_page ); ?>" class="nav-tab <?php echo $active_tab == 'export' ? 'nav-tab-active' : ''; ?>"><?php _e( 'Export', 'invoicing' ); ?></a>
0 ignored issues
show
Security Cross-Site Scripting introduced by
add_query_arg(array('tab... false), $current_page) can contain request data and is used in html attribute with double-quotes context(s) leading to a potential security vulnerability.

2 paths for user data to reach this point

  1. Path: Read tainted data from array, and $_SERVER['REQUEST_URI'] is assigned to $uri in wordpress/wp-includes/functions.php on line 1073
  1. Read tainted data from array, and $_SERVER['REQUEST_URI'] is assigned to $uri
    in wordpress/wp-includes/functions.php on line 1073
  2. $uri . '?' is assigned to $base
    in wordpress/wp-includes/functions.php on line 1100
  3. $protocol . $base . $ret . $frag is assigned to $ret
    in wordpress/wp-includes/functions.php on line 1126
  4. Data is passed through rtrim(), and rtrim($ret, '?') is assigned to $ret
    in wordpress/wp-includes/functions.php on line 1127
  5. $ret is returned
    in wordpress/wp-includes/functions.php on line 1128
  2. Path: Read tainted data from array, and $_SERVER['REQUEST_URI'] is assigned to $uri in wordpress/wp-includes/functions.php on line 1067
  1. Read tainted data from array, and $_SERVER['REQUEST_URI'] is assigned to $uri
    in wordpress/wp-includes/functions.php on line 1067
  2. $uri . '?' is assigned to $base
    in wordpress/wp-includes/functions.php on line 1100
  3. $protocol . $base . $ret . $frag is assigned to $ret
    in wordpress/wp-includes/functions.php on line 1126
  4. Data is passed through rtrim(), and rtrim($ret, '?') is assigned to $ret
    in wordpress/wp-includes/functions.php on line 1127
  5. $ret is returned
    in wordpress/wp-includes/functions.php on line 1128

Preventing Cross-Site-Scripting Attacks

Cross-Site-Scripting allows an attacker to inject malicious code into your website - in particular Javascript code, and have that code executed with the privileges of a visiting user. This can be used to obtain data, or perform actions on behalf of that visiting user.

In order to prevent this, make sure to escape all user-provided data:

// for HTML
$sanitized = htmlentities($tainted, ENT_QUOTES);

// for URLs
$sanitized = urlencode($tainted);

General Strategies to prevent injection

In general, it is advisable to prevent any user-data to reach this point. This can be done by white-listing certain values:

if ( ! in_array($value, array('this-is-allowed', 'and-this-too'), true)) {
    throw new \InvalidArgumentException('This input is not allowed.');
}

For numeric data, we recommend to explicitly cast the data:

$sanitized = (integer) $tainted;
Loading history...
80
                <?php do_action( 'wpinv_reports_page_tabs' ); ;?>
81
            </h2>
82
            <div class="wpi-reports-content wpi-reports-<?php echo esc_attr( $active_tab ); ?>">
83
            <?php
84
                do_action( 'wpinv_reports_page_top' );
85
                do_action( 'wpinv_reports_tab_' . $active_tab );
86
                do_action( 'wpinv_reports_page_bottom' );
87
            ?>
88
        </div>
89
        <?php
90
    }
91
    
92
    public function export() {
93
        $statuses = wpinv_get_invoice_statuses( true );
94
        $statuses = array_merge( array( 'any' => __( 'All Statuses', 'invoicing' ) ), $statuses );
95
        ?>
96
        <div class="metabox-holder">
97
            <div id="post-body">
98
                <div id="post-body-content">
99
                    <?php do_action( 'wpinv_reports_tab_export_content_top' ); ?>
100
                    
101
                    <div class="postbox wpi-export-invoices">
102
                        <h2 class="hndle ui-sortabled-handle"><span><?php _e( 'Invoices','invoicing' ); ?></span></h2>
103
                        <div class="inside">
104
                            <p><?php _e( 'Download a CSV of all payment invoices.', 'invoicing' ); ?></p>
105
                            <form id="wpi-export-invoices" class="wpi-export-form" method="post">
106
                                <?php echo wpinv_html_date_field( array( 
107
                                    'id' => 'wpi_export_from_date', 
108
                                    'name' => 'from_date',
109
                                    'data' => array(
110
                                        'dateFormat' => 'yy-mm-dd'
111
                                    ),
112
                                    'placeholder' => __( 'From date', 'invoicing' ) )
113
                                ); ?>
114
                                <?php echo wpinv_html_date_field( array( 
115
                                    'id' => 'wpi_export_to_date',
116
                                    'name' => 'to_date',
117
                                    'data' => array(
118
                                        'dateFormat' => 'yy-mm-dd'
119
                                    ),
120
                                    'placeholder' => __( 'To date', 'invoicing' ) )
121
                                ); ?>
122
                                <span id="wpinv-status-wrap">
123
                                <?php echo wpinv_html_select( array(
124
                                    'options'          => $statuses,
125
                                    'name'             => 'status',
126
                                    'id'               => 'wpi_export_status',
127
                                    'show_option_all'  => false,
128
                                    'show_option_none' => false,
129
                                    'class'            => 'wpi_select2',
130
                                ) ); ?>
131
                                <?php wp_nonce_field( 'wpi_ajax_export', 'wpi_ajax_export' ); ?>
132
                                </span>
133
                                <span id="wpinv-submit-wrap">
134
                                    <input type="hidden" value="invoices" name="export" />
135
                                    <input type="submit" value="<?php _e( 'Generate CSV', 'invoicing' ); ?>" class="button-primary" />
136
                                </span>
137
                            </form>
138
                        </div>
139
                    </div>
140
141
                    <div class="postbox wpi-export-discount-uses">
142
                        <h2 class="hndle ui-sortabled-handle"><span><?php _e( 'Discount Use','invoicing' ); ?></span></h2>
143
                        <div class="inside">
144
                            <p><?php _e( 'Download a CSV of discount uses.', 'invoicing' ); ?></p>
145
                            <a class="button-primary" href="<?php echo esc_url( wp_nonce_url( admin_url( 'admin-ajax.php?action=wpinv_ajax_discount_use_export' ), 'wpi_discount_ajax_export', 'wpi_discount_ajax_export' ) ); ?>"><?php _e( 'Generate CSV', 'invoicing' ); ?></a>
146
                        </div>
147
                    </div>
148
                    
149
                    <?php do_action( 'wpinv_reports_tab_export_content_bottom' ); ?>
150
                </div>
151
            </div>
152
        </div>
153
        <?php
154
    }
155
    
156
    public function export_location( $relative = false ) {
157
        $upload_dir         = wp_upload_dir();
158
        $export_location    = $relative ? trailingslashit( $upload_dir['baseurl'] ) . 'cache' : trailingslashit( $upload_dir['basedir'] ) . 'cache';
159
        $export_location    = apply_filters( 'wpinv_export_location', $export_location, $relative );
160
        
161
        return trailingslashit( $export_location );
162
    }
163
    
164
    public function check_export_location() {
165
        try {
166
            if ( empty( $this->wp_filesystem ) ) {
167
                return __( 'Filesystem ERROR: Could not access filesystem.', 'invoicing' );
168
            }
169
170
            if ( is_wp_error( $this->wp_filesystem ) ) {
171
                return __( 'Filesystem ERROR: ' . $this->wp_filesystem->get_error_message(), 'invoicing' );
172
            }
173
        
174
            $is_dir         = $this->wp_filesystem->is_dir( $this->export_dir );
175
            $is_writeable   = $is_dir && is_writeable( $this->export_dir );
176
            
177
            if ( $is_dir && $is_writeable ) {
178
               return true;
179
            } else if ( $is_dir && !$is_writeable ) {
180
               if ( !$this->wp_filesystem->chmod( $this->export_dir, FS_CHMOD_DIR ) ) {
181
                   return wp_sprintf( __( 'Filesystem ERROR: Export location %s is not writable, check your file permissions.', 'invoicing' ), $this->export_dir );
182
               }
183
               
184
               return true;
185
            } else {
186
                if ( !$this->wp_filesystem->mkdir( $this->export_dir, FS_CHMOD_DIR ) ) {
187
                    return wp_sprintf( __( 'Filesystem ERROR: Could not create directory %s. This is usually due to inconsistent file permissions.', 'invoicing' ), $this->export_dir );
188
                }
189
                
190
                return true;
191
            }
192
        } catch ( Exception $e ) {
193
            return $e->getMessage();
194
        }
195
    }
196
    
197
    public function ajax_export() {
198
        $response               = array();
199
        $response['success']    = false;
200
        $response['msg']        = __( 'Invalid export request found.', 'invoicing' );
201
        
202
        if ( empty( $_POST['data'] ) || ! wpinv_current_user_can_manage_invoicing() ) {
203
            wp_send_json( $response );
204
        }
205
206
        parse_str( $_POST['data'], $data );
0 ignored issues
show
Security Variable Injection introduced by
$_POST['data'] can contain request data and is used in variable name context(s) leading to a potential security vulnerability.

1 path for user data to reach this point

  1. Read from $_POST
    in includes/class-wpinv-reports.php on line 206

General Strategies to prevent injection

In general, it is advisable to prevent any user-data to reach this point. This can be done by white-listing certain values:

if ( ! in_array($value, array('this-is-allowed', 'and-this-too'), true)) {
    throw new \InvalidArgumentException('This input is not allowed.');
}

For numeric data, we recommend to explicitly cast the data:

$sanitized = (integer) $tainted;
Loading history...
207
        
208
        $data['step']   = !empty( $_POST['step'] ) ? absint( $_POST['step'] ) : 1;
209
210
        $_REQUEST = (array)$data;
211
        if ( !( !empty( $_REQUEST['wpi_ajax_export'] ) && wp_verify_nonce( $_REQUEST['wpi_ajax_export'], 'wpi_ajax_export' ) ) ) {
212
            $response['msg']    = __( 'Security check failed.', 'invoicing' );
213
            wp_send_json( $response );
214
        }
215
        
216
        if ( ( $error = $this->check_export_location( true ) ) !== true ) {
0 ignored issues
show
Unused Code introduced by
The call to WPInv_Reports::check_export_location() has too many arguments starting with true. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

216
        if ( ( $error = $this->/** @scrutinizer ignore-call */ check_export_location( true ) ) !== true ) {

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.

Loading history...
217
            $response['msg'] = __( 'Filesystem ERROR: ' . $error, 'invoicing' );
218
            wp_send_json( $response );
219
        }
220
                        
221
        $this->set_export_params( $_REQUEST );
222
        
223
        $return = $this->process_export_step();
224
        $done   = $this->get_export_status();
225
        
226
        if ( $return ) {
227
            $this->step += 1;
228
            
229
            $response['success']    = true;
230
            $response['msg']        = '';
231
            
232
            if ( $done >= 100 ) {
233
                $this->step     = 'done';
0 ignored issues
show
Bug Best Practice introduced by
The property step does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
234
                $new_filename   = 'wpi-' . $this->export . '-' . date( 'y-m-d-H-i' ) . '.' . $this->filetype;
235
                $new_file       = $this->export_dir . $new_filename;
236
                
237
                if ( file_exists( $this->file ) ) {
238
                    $this->wp_filesystem->move( $this->file, $new_file, true );
239
                }
240
                
241
                if ( file_exists( $new_file ) ) {
242
                    $response['data']['file'] = array( 'u' => $this->export_url . $new_filename, 's' => size_format( filesize( $new_file ), 2 ) );
243
                }
244
            }
245
            
246
            $response['data']['step']   = $this->step;
247
            $response['data']['done']   = $done;
248
        } else {
249
            $response['msg']    = __( 'No data found for export.', 'invoicing' );
250
        }
251
252
        wp_send_json( $response );
253
    }
254
255
    /**
256
     * Handles discount exports.
257
     */
258
    public function discount_use_export() {
259
260
        if ( ! wp_verify_nonce( $_GET['wpi_discount_ajax_export'], 'wpi_discount_ajax_export' ) || ! wpinv_current_user_can_manage_invoicing() ) {
261
            wp_die( -1, 403 );
262
        }
263
264
        $args = array(
265
            'post_type'      => 'wpi_discount',
266
            'fields'         => 'ids',
267
            'posts_per_page' => -1,
268
        );
269
270
        $discounts = get_posts( $args );
271
272
        if ( empty( $discounts ) ) {
273
            die ( __( 'You have not set up any discounts', 'invoicing' ) );
0 ignored issues
show
Best Practice introduced by
Using exit here is not recommended.

In general, usage of exit should be done with care and only when running in a scripting context like a CLI script.

Loading history...
274
        }
275
276
        $output  = fopen( 'php://output', 'w' ) or die( 'Unsupported server' );
0 ignored issues
show
Best Practice introduced by
Using exit here is not recommended.

In general, usage of exit should be done with care and only when running in a scripting context like a CLI script.

Loading history...
277
278
        // Let the browser know what content we're streaming and how it should save the content.
279
		$name = time();
280
		header( "Content-Type:application/csv" );
281
        header( "Content-Disposition:attachment;filename=noptin-subscribers-$name.csv" );
282
283
        // Output the csv column headers.
284
		fputcsv( 
285
            $output, 
0 ignored issues
show
Bug introduced by
It seems like $output can also be of type false; however, parameter $handle of fputcsv() does only seem to accept resource, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

285
            /** @scrutinizer ignore-type */ $output, 
Loading history...
286
            array( 
287
                __( 'Discount Id', 'invoicing' ),
288
                __( 'Discount Code', 'invoicing' ),
289
                __( 'Discount Type', 'invoicing' ),
290
                __( 'Discount Amount', 'invoicing' ),
291
                __( 'Uses', 'invoicing' ),
292
            )
293
        );
294
295
        foreach ( $discounts as $discount ) {
296
297
            $discount = (int) $discount;
298
            $row      = array(
299
                $discount,
300
                get_post_meta( $discount, '_wpi_discount_code', true ),
301
                get_post_meta( $discount, '_wpi_discount_type', true ),
302
                get_post_meta( $discount, '_wpi_discount_amount', true ),
303
                (int) get_post_meta( $discount, '_wpi_discount_uses', true )
304
            );
305
            fputcsv( $output, $row );
306
        }
307
308
        fclose( $output );
0 ignored issues
show
Bug introduced by
It seems like $output can also be of type false; however, parameter $handle of fclose() does only seem to accept resource, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

308
        fclose( /** @scrutinizer ignore-type */ $output );
Loading history...
309
        exit;
0 ignored issues
show
Best Practice introduced by
Using exit here is not recommended.

In general, usage of exit should be done with care and only when running in a scripting context like a CLI script.

Loading history...
310
311
    }
312
    
313
    public function set_export_params( $request ) {
314
        $this->empty    = false;
0 ignored issues
show
Bug Best Practice introduced by
The property empty does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
315
        $this->step     = !empty( $request['step'] ) ? absint( $request['step'] ) : 1;
0 ignored issues
show
Bug Best Practice introduced by
The property step does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
316
        $this->export   = !empty( $request['export'] ) ? $request['export'] : $this->export;
317
        $this->filename = 'wpi-' . $this->export . '-' . $request['wpi_ajax_export'] . '.' . $this->filetype;
0 ignored issues
show
Bug Best Practice introduced by
The property filename does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
318
        $this->file     = $this->export_dir . $this->filename;
0 ignored issues
show
Bug Best Practice introduced by
The property file does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
319
        
320
        do_action( 'wpinv_export_set_params_' . $this->export, $request );
321
    }
322
    
323
    public function get_columns() {
324
        $columns = array();
325
        
326
        return apply_filters( 'wpinv_export_get_columns_' . $this->export, $columns );
327
    }
328
    
329
    protected function get_export_file() {
330
        $file = '';
331
332
        if ( $this->wp_filesystem->exists( $this->file ) ) {
333
            $file = $this->wp_filesystem->get_contents( $this->file );
334
        } else {
335
            $this->wp_filesystem->put_contents( $this->file, '' );
336
        }
337
338
        return $file;
339
    }
340
    
341
    protected function attach_export_data( $data = '' ) {
342
        $filedata   = $this->get_export_file();
343
        $filedata   .= $data;
344
        
345
        $this->wp_filesystem->put_contents( $this->file, $filedata );
346
347
        $rows       = file( $this->file, FILE_SKIP_EMPTY_LINES );
0 ignored issues
show
Security File Exposure introduced by
$this->file can contain request data and is used in file inclusion context(s) leading to a potential security vulnerability.

1 path for user data to reach this point

  1. Read from $_REQUEST, and WPInv_Reports::set_export_params() is called
    in includes/class-wpinv-reports.php on line 221
  2. Enters via parameter $request
    in includes/class-wpinv-reports.php on line 313
  3. 'wpi-' . $this->export . '-' . $request['wpi_ajax_export'] . '.' . $this->filetype is assigned to property WPInv_Reports::$filename
    in includes/class-wpinv-reports.php on line 317
  4. Read from property WPInv_Reports::$filename, and $this->export_dir . $this->filename is assigned to property WPInv_Reports::$file
    in includes/class-wpinv-reports.php on line 318
  5. Read from property WPInv_Reports::$file
    in includes/class-wpinv-reports.php on line 347

General Strategies to prevent injection

In general, it is advisable to prevent any user-data to reach this point. This can be done by white-listing certain values:

if ( ! in_array($value, array('this-is-allowed', 'and-this-too'), true)) {
    throw new \InvalidArgumentException('This input is not allowed.');
}

For numeric data, we recommend to explicitly cast the data:

$sanitized = (integer) $tainted;
Loading history...
348
        $columns    = $this->get_columns();
349
        $columns    = empty( $columns ) ? 0 : 1;
350
351
        $this->empty = count( $rows ) == $columns ? true : false;
0 ignored issues
show
Bug Best Practice introduced by
The property empty does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
Bug introduced by
It seems like $rows can also be of type false; however, parameter $var of count() does only seem to accept Countable|array, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

351
        $this->empty = count( /** @scrutinizer ignore-type */ $rows ) == $columns ? true : false;
Loading history...
352
    }
353
    
354
    public function print_columns() {
355
        $column_data    = '';
356
        $columns        = $this->get_columns();
357
        $i              = 1;
358
        foreach( $columns as $key => $column ) {
359
            $column_data .= '"' . addslashes( $column ) . '"';
360
            $column_data .= $i == count( $columns ) ? '' : ',';
361
            $i++;
362
        }
363
        $column_data .= "\r\n";
364
365
        $this->attach_export_data( $column_data );
366
367
        return $column_data;
368
    }
369
    
370
    public function process_export_step() {
371
        if ( $this->step < 2 ) {
372
            /** @scrutinizer ignore-unhandled */ @unlink( $this->file );
0 ignored issues
show
Security File Manipulation introduced by
$this->file can contain request data and is used in file manipulation context(s) leading to a potential security vulnerability.

1 path for user data to reach this point

  1. Read from $_REQUEST, and WPInv_Reports::set_export_params() is called
    in includes/class-wpinv-reports.php on line 221
  2. Enters via parameter $request
    in includes/class-wpinv-reports.php on line 313
  3. 'wpi-' . $this->export . '-' . $request['wpi_ajax_export'] . '.' . $this->filetype is assigned to property WPInv_Reports::$filename
    in includes/class-wpinv-reports.php on line 317
  4. Read from property WPInv_Reports::$filename, and $this->export_dir . $this->filename is assigned to property WPInv_Reports::$file
    in includes/class-wpinv-reports.php on line 318
  5. Read from property WPInv_Reports::$file
    in includes/class-wpinv-reports.php on line 372

General Strategies to prevent injection

In general, it is advisable to prevent any user-data to reach this point. This can be done by white-listing certain values:

if ( ! in_array($value, array('this-is-allowed', 'and-this-too'), true)) {
    throw new \InvalidArgumentException('This input is not allowed.');
}

For numeric data, we recommend to explicitly cast the data:

$sanitized = (integer) $tainted;
Loading history...
373
            $this->print_columns();
374
        }
375
        
376
        $return = $this->print_rows();
377
        
378
        if ( $return ) {
379
            return true;
380
        } else {
381
            return false;
382
        }
383
    }
384
    
385
    public function get_export_status() {
386
        $status = 100;
387
        return apply_filters( 'wpinv_get_export_status_' . $this->export, $status );
388
    }
389
    
390
    public function get_export_data() {
391
        $data = array();
392
393
        $data = apply_filters( 'wpinv_export_get_data', $data );
394
        $data = apply_filters( 'wpinv_export_get_data_' . $this->export, $data );
395
396
        return $data;
397
    }
398
    
399
    public function print_rows() {
400
        $row_data   = '';
401
        $data       = $this->get_export_data();
402
        $columns    = $this->get_columns();
403
404
        if ( $data ) {
405
            foreach ( $data as $row ) {
406
                $i = 1;
407
                foreach ( $row as $key => $column ) {
408
                    if ( array_key_exists( $key, $columns ) ) {
409
                        $row_data .= '"' . addslashes( preg_replace( "/\"/","'", $column ) ) . '"';
410
                        $row_data .= $i == count( $columns ) ? '' : ',';
411
                        $i++;
412
                    }
413
                }
414
                $row_data .= "\r\n";
415
            }
416
417
            $this->attach_export_data( $row_data );
418
419
            return $row_data;
420
        }
421
422
        return false;
423
    }
424
    
425
    // Export Invoices.
426
    public function set_invoices_export( $request ) {
427
        $this->from_date    = isset( $request['from_date'] ) ? sanitize_text_field( $request['from_date'] ) : '';
0 ignored issues
show
Bug Best Practice introduced by
The property from_date does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
428
        $this->to_date      = isset( $request['to_date'] ) ? sanitize_text_field( $request['to_date'] ) : '';
0 ignored issues
show
Bug Best Practice introduced by
The property to_date does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
429
        $this->status       = isset( $request['status'] ) ? sanitize_text_field( $request['status'] ) : 'publish';
0 ignored issues
show
Bug Best Practice introduced by
The property status does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
430
    }
431
    
432
    public function get_invoices_columns( $columns = array() ) {
433
        $columns = array(
434
            'id'            => __( 'ID',   'invoicing' ),
435
            'number'        => __( 'Number',   'invoicing' ),
436
            'date'          => __( 'Date', 'invoicing' ),
437
            'due_date'      => __( 'Due Date', 'invoicing' ),
438
            'completed_date'=> __( 'Payment Done Date', 'invoicing' ),
439
            'amount'        => __( 'Amount', 'invoicing' ),
440
            'currency'      => __( 'Currency', 'invoicing' ),
441
            'items'        => __( 'Items', 'invoicing' ),
442
            'status_nicename'  => __( 'Status Nicename', 'invoicing' ),
443
            'status'        => __( 'Status', 'invoicing' ),
444
            'tax'           => __( 'Tax', 'invoicing' ),
445
            'discount'      => __( 'Discount', 'invoicing' ),
446
            'user_id'       => __( 'User ID', 'invoicing' ),
447
            'email'         => __( 'Email', 'invoicing' ),
448
            'first_name'    => __( 'First Name', 'invoicing' ),
449
            'last_name'     => __( 'Last Name', 'invoicing' ),
450
            'address'       => __( 'Address', 'invoicing' ),
451
            'city'          => __( 'City', 'invoicing' ),
452
            'state'         => __( 'State', 'invoicing' ),
453
            'country'       => __( 'Country', 'invoicing' ),
454
            'zip'           => __( 'Zipcode', 'invoicing' ),
455
            'phone'         => __( 'Phone', 'invoicing' ),
456
            'company'       => __( 'Company', 'invoicing' ),
457
            'vat_number'    => __( 'Vat Number', 'invoicing' ),
458
            'ip'            => __( 'IP', 'invoicing' ),
459
            'gateway'       => __( 'Gateway', 'invoicing' ),
460
            'gateway_nicename'       => __( 'Gateway Nicename', 'invoicing' ),
461
            'transaction_id'=> __( 'Transaction ID', 'invoicing' ),
462
        );
463
464
        return $columns;
465
    }
466
        
467
    public function get_invoices_data( $response = array() ) {
468
        $args = array(
469
            'limit'    => $this->per_page,
470
            'page'     => $this->step,
471
            'order'    => 'DESC',
472
            'orderby'  => 'date',
473
        );
474
        
475
        if ( $this->status != 'any' ) {
476
            $args['status'] = $this->status;
477
        } else {
478
            $args['status'] = array_keys( wpinv_get_invoice_statuses( true ) );
479
        }
480
481
        if ( !empty( $this->from_date ) || !empty( $this->to_date ) ) {
482
            $args['date_query'] = array(
483
                array(
484
                    'after'     => date( 'Y-n-d 00:00:00', strtotime( $this->from_date ) ),
485
                    'before'    => date( 'Y-n-d 23:59:59', strtotime( $this->to_date ) ),
486
                    'inclusive' => true
487
                )
488
            );
489
        }
490
491
        $invoices = wpinv_get_invoices( $args );
492
        
493
        $data = array();
494
        
495
        if ( !empty( $invoices ) ) {
496
            foreach ( $invoices as $invoice ) {
497
                $items = $this->get_invoice_items($invoice);
498
                $row = array(
499
                    'id'            => $invoice->ID,
500
                    'number'        => $invoice->get_number(),
501
                    'date'          => $invoice->get_invoice_date( false ),
502
                    'due_date'      => $invoice->get_due_date( false ),
503
                    'completed_date'=> $invoice->get_completed_date(),
504
                    'amount'        => wpinv_round_amount( $invoice->get_total() ),
505
                    'currency'      => $invoice->get_currency(),
506
                    'items'         => $items,
507
                    'status_nicename' => $invoice->get_status( true ),
508
                    'status'        => $invoice->get_status(),
509
                    'tax'           => $invoice->get_tax() > 0 ? wpinv_round_amount( $invoice->get_tax() ) : '',
510
                    'discount'      => $invoice->get_discount() > 0 ? wpinv_round_amount( $invoice->get_discount() ) : '',
511
                    'user_id'       => $invoice->get_user_id(),
512
                    'email'         => $invoice->get_email(),
513
                    'first_name'    => $invoice->get_first_name(),
514
                    'last_name'     => $invoice->get_last_name(),
515
                    'address'       => $invoice->get_address(),
516
                    'city'          => $invoice->city,
517
                    'state'         => $invoice->state,
518
                    'country'       => $invoice->country,
519
                    'zip'           => $invoice->zip,
520
                    'phone'         => $invoice->phone,
521
                    'company'       => $invoice->company,
522
                    'vat_number'    => $invoice->vat_number,
523
                    'ip'            => $invoice->get_ip(),
524
                    'gateway'       => $invoice->get_gateway(),
525
                    'gateway_nicename' => $invoice->get_gateway_title(),
526
                    'transaction_id'=> $invoice->gateway ? $invoice->get_transaction_id() : '',
527
                );
528
                
529
                $data[] = apply_filters( 'wpinv_export_invoice_row', $row, $invoice );
530
            }
531
532
            return $data;
533
534
        }
535
536
        return false;
537
    }
538
    
539
    public function invoices_export_status() {
540
        $args = array(
541
            'limit'    => -1,
542
            'return'   => 'ids',
543
        );
544
        
545
        if ( $this->status != 'any' ) {
546
            $args['status'] = $this->status;
547
        } else {
548
            $args['status'] = array_keys( wpinv_get_invoice_statuses( true ) );
549
        }
550
551
        if ( !empty( $this->from_date ) || !empty( $this->to_date ) ) {
552
            $args['date_query'] = array(
553
                array(
554
                    'after'     => date( 'Y-n-d 00:00:00', strtotime( $this->from_date ) ),
555
                    'before'    => date( 'Y-n-d 23:59:59', strtotime( $this->to_date ) ),
556
                    'inclusive' => true
557
                )
558
            );
559
        }
560
561
        $invoices   = wpinv_get_invoices( $args );
562
        $total      = !empty( $invoices ) ? count( $invoices ) : 0;
0 ignored issues
show
Bug introduced by
It seems like $invoices can also be of type WP_Query; however, parameter $var of count() does only seem to accept Countable|array, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

562
        $total      = !empty( $invoices ) ? count( /** @scrutinizer ignore-type */ $invoices ) : 0;
Loading history...
563
        $status     = 100;
564
565
        if ( $total > 0 ) {
566
            $status = ( ( $this->per_page * $this->step ) / $total ) * 100;
567
        }
568
569
        if ( $status > 100 ) {
570
            $status = 100;
571
        }
572
573
        return $status;
574
    }
575
576
    public function get_invoice_items($invoice){
577
        if(!$invoice){
578
            return '';
579
        }
580
581
        $cart_details = $invoice->get_cart_details();
582
        if(!empty($cart_details)){
583
            $cart_details = maybe_serialize($cart_details);
584
        } else {
585
            $cart_details = '';
586
        }
587
588
        return $cart_details;
589
    }
590
}
591