| 1 |
<?php |
| 2 |
/** |
| 3 |
* Source manager for remote XLSX files loaded from a URL. |
| 4 |
* |
| 5 |
* Downloads the file to a temporary location before parsing so that |
| 6 |
* OpenSpout (which does not support stream wrappers for XLSX) can read it. |
| 7 |
* The source URL is stored in the chart's post_content so that scheduled |
| 8 |
* re-imports can re-fetch and re-parse the file automatically. |
| 9 |
* |
| 10 |
* @category Visualizer |
| 11 |
* @package Source |
| 12 |
* |
| 13 |
* @since 3.11.0 |
| 14 |
*/ |
| 15 |
class Visualizer_Source_Xlsx_Remote extends Visualizer_Source_Xlsx { |
| 16 |
|
| 17 |
/** |
| 18 |
* Path to the downloaded temporary file. |
| 19 |
* |
| 20 |
* @access private |
| 21 |
* @var string|false |
| 22 |
*/ |
| 23 |
private $_tmpfile = false; |
| 24 |
|
| 25 |
/** |
| 26 |
* Returns serialised data that also stores the source URL so that |
| 27 |
* scheduled re-imports know where to fetch the file from. |
| 28 |
* |
| 29 |
* @access public |
| 30 |
* @param bool $dumb Unused; kept for interface compatibility. |
| 31 |
* @return string Serialised array with 'source' and 'data' keys. |
| 32 |
*/ |
| 33 |
public function getData( $dumb = false ) { |
| 34 |
return serialize( |
| 35 |
array( |
| 36 |
'source' => $this->_filename, |
| 37 |
'data' => $this->_data, |
| 38 |
) |
| 39 |
); |
| 40 |
} |
| 41 |
|
| 42 |
|
| 43 |
/** |
| 44 |
* Re-populates data for scheduled refreshes. |
| 45 |
* |
| 46 |
* @access public |
| 47 |
* @param array $data The current data array. |
| 48 |
* @param int $chart_id The chart post ID. |
| 49 |
* @return array The re-populated data or the original array. |
| 50 |
*/ |
| 51 |
public function repopulateData( $data, $chart_id ) { |
| 52 |
if ( ! is_array( $data ) ) { |
| 53 |
$data = array(); |
| 54 |
} |
| 55 |
return array_key_exists( 'data', $data ) ? $data['data'] : $data; |
| 56 |
} |
| 57 |
|
| 58 |
/** |
| 59 |
* Re-populates series (series are stored in post meta and remain stable). |
| 60 |
* |
| 61 |
* @access public |
| 62 |
* @param array $series The current series array. |
| 63 |
* @param int $chart_id The chart post ID. |
| 64 |
* @return array The original series array. |
| 65 |
*/ |
| 66 |
public function repopulateSeries( $series, $chart_id ) { |
| 67 |
return $series; |
| 68 |
} |
| 69 |
|
| 70 |
/** |
| 71 |
* Returns the source name. |
| 72 |
* |
| 73 |
* @access public |
| 74 |
* @return string |
| 75 |
*/ |
| 76 |
public function getSourceName() { |
| 77 |
return __CLASS__; |
| 78 |
} |
| 79 |
|
| 80 |
/** |
| 81 |
* Downloads the remote XLSX file to a temporary path and returns that path |
| 82 |
* for the parent reader to use. |
| 83 |
* |
| 84 |
* Enforces a maximum file size (default 10 MB, filterable via |
| 85 |
* 'visualizer_xlsx_max_filesize') before handing the path to the parser, |
| 86 |
* guarding against ZIP-bomb and DoS attacks. |
| 87 |
* |
| 88 |
* @access protected |
| 89 |
* @return string|false Path to the temporary file, or false if download failed. |
| 90 |
*/ |
| 91 |
protected function _get_file_path() { |
| 92 |
if ( $this->_tmpfile && is_readable( $this->_tmpfile ) ) { |
| 93 |
return $this->_tmpfile; |
| 94 |
} |
| 95 |
|
| 96 |
$max_bytes = (int) apply_filters( 'visualizer_xlsx_max_filesize', 10 * 1024 * 1024 ); |
| 97 |
$tmpfile = Visualizer_Remote_Fetch::download( |
| 98 |
$this->_filename, |
| 99 |
array( 'limit_response_size' => $max_bytes ) |
| 100 |
); |
| 101 |
if ( is_wp_error( $tmpfile ) ) { |
| 102 |
$this->_error = 'visualizer_remote_size' === $tmpfile->get_error_code() |
| 103 |
? esc_html__( 'The XLSX file exceeds the maximum allowed size and cannot be imported.', 'visualizer' ) |
| 104 |
: esc_html__( 'Could not download the XLSX file. Please check the URL and try again.', 'visualizer' ); |
| 105 |
return false; |
| 106 |
} |
| 107 |
$this->_tmpfile = $tmpfile; |
| 108 |
|
| 109 |
if ( ! is_file( $this->_tmpfile ) ) { |
| 110 |
$this->_tmpfile = false; |
| 111 |
$this->_error = esc_html__( 'Could not access the downloaded XLSX file. Please try again.', 'visualizer' ); |
| 112 |
return false; |
| 113 |
} |
| 114 |
|
| 115 |
return $this->_tmpfile; |
| 116 |
} |
| 117 |
|
| 118 |
/** |
| 119 |
* Fetches the remote file, parses it, and cleans up the temp file. |
| 120 |
* |
| 121 |
* @access public |
| 122 |
* @return boolean TRUE on success, FALSE on failure. |
| 123 |
*/ |
| 124 |
public function fetch() { |
| 125 |
$result = parent::fetch(); |
| 126 |
|
| 127 |
// Clean up the temporary file after parsing. |
| 128 |
if ( $this->_tmpfile && is_file( $this->_tmpfile ) ) { |
| 129 |
wp_delete_file( $this->_tmpfile ); |
| 130 |
$this->_tmpfile = false; |
| 131 |
} |
| 132 |
|
| 133 |
return $result; |
| 134 |
} |
| 135 |
} |
| 136 |
|