| @@ -29,14 +29,14 @@ | ||
| 29 | 29 | */ |
| 30 | 30 | class Visualizer_Source_Csv_Remote extends Visualizer_Source_Csv { |
| 31 | 31 | |
| 32 | 32 | /** |
| 33 | - * Path to the safely downloaded temporary file. | |
| 33 | + * Temporary file name used when allow_url_fopen option is disabled. | |
| 34 | 34 | * |
| 35 | 35 | * @since 1.4.2 |
| 36 | 36 | * |
| 37 | 37 | * @access private |
| 38 | - * @var string|false | |
| 38 | + * @var string | |
| 39 | 39 | */ |
| 40 | 40 | private $_tmpfile = false; |
| 41 | 41 | |
| 42 | 42 | /** |
| @@ -46,9 +46,9 @@ | ||
| 46 | 46 | * |
| 47 | 47 | * @access public |
| 48 | 48 | * @return string The serialized array of data. |
| 49 | 49 | */ |
| 50 | - public function getData( $dumb = false ) { | |
| 50 | + public function getData() { | |
| 51 | 51 | return serialize( |
| 52 | 52 | array( |
| 53 | 53 | 'source' => $this->_filename, |
| 54 | 54 | 'data' => $this->_data, |
| @@ -73,9 +73,9 @@ | ||
| 73 | 73 | |
| 74 | 74 | // if filename is empty, extract it from chart content |
| 75 | 75 | if ( empty( $this->_filename ) ) { |
| 76 | 76 | $chart = get_post( $chart_id ); |
| 77 | - $data = Visualizer_Module::decode_content( html_entity_decode( $chart->post_content ) ); | |
| 77 | + $data = unserialize( html_entity_decode( $chart->post_content ) ); | |
| 78 | 78 | if ( ! isset( $data['source'] ) ) { |
| 79 | 79 | return false; |
| 80 | 80 | } |
| 81 | 81 | |
| @@ -96,11 +96,8 @@ | ||
| 96 | 96 | * @param int $chart_id The chart id. |
| 97 | 97 | * @return array The re populated array of data or old one. |
| 98 | 98 | */ |
| 99 | 99 | public function repopulateData( $data, $chart_id ) { |
| 100 | - if ( ! is_array( $data ) ) { | |
| 101 | - $data = array(); | |
| 102 | - } | |
| 103 | 100 | return array_key_exists( 'data', $data ) ? $data['data'] : $data; |
| 104 | 101 | } |
| 105 | 102 | |
| 106 | 103 | /** |
| @@ -129,44 +126,37 @@ | ||
| 129 | 126 | return __CLASS__; |
| 130 | 127 | } |
| 131 | 128 | |
| 132 | 129 | /** |
| 133 | - * Fetches the remote file and removes its temporary copy. | |
| 134 | - * | |
| 135 | - * @access public | |
| 136 | - * @return boolean TRUE on success, otherwise FALSE. | |
| 137 | - */ | |
| 138 | - public function fetch() { | |
| 139 | - $result = parent::fetch(); | |
| 140 | - | |
| 141 | - if ( $this->_tmpfile && is_file( $this->_tmpfile ) ) { | |
| 142 | - wp_delete_file( $this->_tmpfile ); | |
| 143 | - $this->_tmpfile = false; | |
| 144 | - } | |
| 145 | - | |
| 146 | - return $result; | |
| 147 | - } | |
| 148 | - | |
| 149 | - /** | |
| 150 | 130 | * Returns file handle to fetch data from. |
| 151 | 131 | * |
| 152 | 132 | * @since 1.4.2 |
| 153 | 133 | * |
| 154 | 134 | * @access protected |
| 135 | + * @staticvar boolean $allow_url_fopen Determines whether or not allow_url_fopen option is enabled. | |
| 155 | 136 | * @param string $filename Optional file name to get handle. If omitted, $_filename is used. |
| 156 | - * @return resource|false File handle resource on success, otherwise FALSE. | |
| 137 | + * @return resource File handle resource on success, otherwise FALSE. | |
| 157 | 138 | */ |
| 158 | 139 | protected function _get_file_handle( $filename = false ) { |
| 159 | - if ( $this->_tmpfile && is_readable( $this->_tmpfile ) ) { | |
| 140 | + static $allow_url_fopen = null; | |
| 141 | + | |
| 142 | + if ( ! is_wp_error( $this->_tmpfile ) && $this->_tmpfile && is_readable( $this->_tmpfile ) ) { | |
| 160 | 143 | return parent::_get_file_handle( $this->_tmpfile ); |
| 161 | 144 | } |
| 162 | 145 | |
| 163 | - $tmpfile = Visualizer_Remote_Fetch::download( $this->_filename ); | |
| 164 | - if ( is_wp_error( $tmpfile ) ) { | |
| 165 | - $this->_error = esc_html__( 'Could not download the file. Please check the URL and try again.', 'visualizer' ); | |
| 166 | - return false; | |
| 146 | + if ( is_null( $allow_url_fopen ) ) { | |
| 147 | + $allow_url_fopen = filter_var( ini_get( 'allow_url_fopen' ), FILTER_VALIDATE_BOOLEAN ); | |
| 167 | 148 | } |
| 168 | 149 | |
| 169 | - $this->_tmpfile = $tmpfile; | |
| 170 | - return parent::_get_file_handle( $this->_tmpfile ); | |
| 150 | + $scheme = parse_url( $this->_filename, PHP_URL_SCHEME ); | |
| 151 | + if ( $allow_url_fopen && in_array( $scheme, stream_get_wrappers(), true ) ) { | |
| 152 | + return parent::_get_file_handle( $filename ); | |
| 153 | + } | |
| 154 | + | |
| 155 | + require_once ABSPATH . 'wp-admin/includes/file.php'; | |
| 156 | + | |
| 157 | + $this->_tmpfile = download_url( $this->_filename ); | |
| 158 | + | |
| 159 | + return ! is_wp_error( $this->_tmpfile ) ? parent::_get_file_handle( $this->_tmpfile ) : false; | |
| 171 | 160 | } |
| 161 | + | |
| 172 | 162 | } |