| @@ -18,8 +18,9 @@ | ||
| 18 | 18 | // | MA 02110-1301 USA | |
| 19 | 19 | // +----------------------------------------------------------------------+ |
| 20 | 20 | // | Author: Eugene Manuilov <eugene@manuilov.org> | |
| 21 | 21 | // +----------------------------------------------------------------------+ |
| 22 | + | |
| 22 | 23 | /** |
| 23 | 24 | * Source manager for remote CSV files. |
| 24 | 25 | * |
| 25 | 26 | * @category Visualizer |
| @@ -29,14 +30,14 @@ | ||
| 29 | 30 | */ |
| 30 | 31 | class Visualizer_Source_Csv_Remote extends Visualizer_Source_Csv { |
| 31 | 32 | |
| 32 | 33 | /** |
| 33 | - * Path to the safely downloaded temporary file. | |
| 34 | + * Temporary file name used when allow_url_fopen option is disabled. | |
| 34 | 35 | * |
| 35 | 36 | * @since 1.4.2 |
| 36 | 37 | * |
| 37 | 38 | * @access private |
| 38 | - * @var string|false | |
| 39 | + * @var string | |
| 39 | 40 | */ |
| 40 | 41 | private $_tmpfile = false; |
| 41 | 42 | |
| 42 | 43 | /** |
| @@ -46,15 +47,13 @@ | ||
| 46 | 47 | * |
| 47 | 48 | * @access public |
| 48 | 49 | * @return string The serialized array of data. |
| 49 | 50 | */ |
| 50 | - public function getData( $dumb = false ) { | |
| 51 | - return serialize( | |
| 52 | - array( | |
| 53 | - 'source' => $this->_filename, | |
| 54 | - 'data' => $this->_data, | |
| 55 | - ) | |
| 56 | - ); | |
| 51 | + public function getData() { | |
| 52 | + return serialize( array( | |
| 53 | + 'source' => $this->_filename, | |
| 54 | + 'data' => $this->_data, | |
| 55 | + ) ); | |
| 57 | 56 | } |
| 58 | 57 | |
| 59 | 58 | /** |
| 60 | 59 | * Re populates data and series. |
| @@ -66,9 +65,9 @@ | ||
| 66 | 65 | * @return boolean TRUE on success, otherwise FALSE. |
| 67 | 66 | */ |
| 68 | 67 | private function _repopulate( $chart_id ) { |
| 69 | 68 | // if it has been already populated, then just return true |
| 70 | - if ( ! empty( $this->_data ) && ! empty( $this->_series ) ) { | |
| 69 | + if ( !empty( $this->_data ) && !empty( $this->_series ) ) { | |
| 71 | 70 | return true; |
| 72 | 71 | } |
| 73 | 72 | |
| 74 | 73 | // if filename is empty, extract it from chart content |
| @@ -73,10 +72,10 @@ | ||
| 73 | 72 | |
| 74 | 73 | // if filename is empty, extract it from chart content |
| 75 | 74 | if ( empty( $this->_filename ) ) { |
| 76 | 75 | $chart = get_post( $chart_id ); |
| 77 | - $data = Visualizer_Module::decode_content( html_entity_decode( $chart->post_content ) ); | |
| 78 | - if ( ! isset( $data['source'] ) ) { | |
| 76 | + $data = unserialize( $chart->post_content ); | |
| 77 | + if ( !isset( $data['source'] ) ) { | |
| 79 | 78 | return false; |
| 80 | 79 | } |
| 81 | 80 | |
| 82 | 81 | $this->_filename = $data['source']; |
| @@ -92,16 +91,13 @@ | ||
| 92 | 91 | * @since 1.1.0 |
| 93 | 92 | * |
| 94 | 93 | * @access public |
| 95 | 94 | * @param array $data The actual array of data. |
| 96 | - * @param int $chart_id The chart id. | |
| 95 | + * @param int $chart_id The chart id. | |
| 97 | 96 | * @return array The re populated array of data or old one. |
| 98 | 97 | */ |
| 99 | 98 | public function repopulateData( $data, $chart_id ) { |
| 100 | - if ( ! is_array( $data ) ) { | |
| 101 | - $data = array(); | |
| 102 | - } | |
| 103 | - return array_key_exists( 'data', $data ) ? $data['data'] : $data; | |
| 99 | + return $this->_repopulate( $chart_id ) ? $this->_data : $data; | |
| 104 | 100 | } |
| 105 | 101 | |
| 106 | 102 | /** |
| 107 | 103 | * Re populates series. |
| @@ -109,13 +105,13 @@ | ||
| 109 | 105 | * @since 1.1.0 |
| 110 | 106 | * |
| 111 | 107 | * @access public |
| 112 | 108 | * @param array $series The actual array of series. |
| 113 | - * @param int $chart_id The chart id. | |
| 109 | + * @param int $chart_id The chart id. | |
| 114 | 110 | * @return array The re populated array of series or old one. |
| 115 | 111 | */ |
| 116 | 112 | public function repopulateSeries( $series, $chart_id ) { |
| 117 | - return $series; | |
| 113 | + return $this->_repopulate( $chart_id ) ? $this->_series : $series; | |
| 118 | 114 | } |
| 119 | 115 | |
| 120 | 116 | /** |
| 121 | 117 | * Returns source name. |
| @@ -129,44 +125,37 @@ | ||
| 129 | 125 | return __CLASS__; |
| 130 | 126 | } |
| 131 | 127 | |
| 132 | 128 | /** |
| 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 | 129 | * Returns file handle to fetch data from. |
| 151 | 130 | * |
| 152 | 131 | * @since 1.4.2 |
| 153 | 132 | * |
| 154 | 133 | * @access protected |
| 134 | + * @staticvar boolean $allow_url_fopen Determines whether or not allow_url_fopen option is enabled. | |
| 155 | 135 | * @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. | |
| 136 | + * @return resource File handle resource on success, otherwise FALSE. | |
| 157 | 137 | */ |
| 158 | 138 | protected function _get_file_handle( $filename = false ) { |
| 139 | + static $allow_url_fopen = null; | |
| 140 | + | |
| 159 | 141 | if ( $this->_tmpfile && is_readable( $this->_tmpfile ) ) { |
| 160 | 142 | return parent::_get_file_handle( $this->_tmpfile ); |
| 161 | 143 | } |
| 162 | 144 | |
| 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; | |
| 145 | + if ( is_null( $allow_url_fopen ) ) { | |
| 146 | + $allow_url_fopen = filter_var( ini_get( 'allow_url_fopen' ), FILTER_VALIDATE_BOOLEAN ); | |
| 167 | 147 | } |
| 168 | 148 | |
| 169 | - $this->_tmpfile = $tmpfile; | |
| 170 | - return parent::_get_file_handle( $this->_tmpfile ); | |
| 149 | + $scheme = parse_url( $this->_filename, PHP_URL_SCHEME ); | |
| 150 | + if ( $allow_url_fopen && in_array( $scheme, stream_get_wrappers() ) ) { | |
| 151 | + return parent::_get_file_handle( $filename ); | |
| 152 | + } | |
| 153 | + | |
| 154 | + require_once ABSPATH . 'wp-admin/includes/file.php'; | |
| 155 | + | |
| 156 | + $this->_tmpfile = download_url( $this->_filename ); | |
| 157 | + | |
| 158 | + return !is_wp_error( $this->_tmpfile ) ? parent::_get_file_handle( $this->_tmpfile ) : false; | |
| 171 | 159 | } |
| 172 | -} | |
| 160 | + | |
| 161 | +} | |