PHPExcel
10 years ago
PHPExcel.php
10 years ago
api.php
10 years ago
arraytoxml.php
10 years ago
chunk.php
10 years ago
config.php
10 years ago
download.php
10 years ago
handler.php
10 years ago
helper.php
10 years ago
input.php
10 years ago
render.php
10 years ago
session.php
10 years ago
upload.php
10 years ago
upload.php
293 lines
| 1 | <?php |
| 2 | if ( ! class_exists('PMXI_Upload')){ |
| 3 | |
| 4 | class PMXI_Upload{ |
| 5 | |
| 6 | protected $file; |
| 7 | protected $errors; |
| 8 | protected $root_element = ''; |
| 9 | protected $is_csv = false; |
| 10 | |
| 11 | protected $uploadsPath; |
| 12 | |
| 13 | function __construct( $file, $errors, $targetDir = false ){ |
| 14 | |
| 15 | $this->file = $file; |
| 16 | $this->errors = $errors; |
| 17 | |
| 18 | $uploads = wp_upload_dir(); |
| 19 | |
| 20 | if ( $uploads['error'] ) |
| 21 | $this->uploadsPath = false; |
| 22 | else |
| 23 | $this->uploadsPath = ( ! $targetDir ) ? wp_all_import_secure_file($uploads['basedir'] . DIRECTORY_SEPARATOR . PMXI_Plugin::UPLOADS_DIRECTORY) : $targetDir; |
| 24 | } |
| 25 | |
| 26 | public function upload(){ |
| 27 | |
| 28 | $uploads = wp_upload_dir(); |
| 29 | |
| 30 | $this->file = wp_all_import_get_absolute_path($this->file); |
| 31 | |
| 32 | $template = false; |
| 33 | |
| 34 | if (empty($this->file)) { |
| 35 | $this->errors->add('form-validation', __('Please specify a file to import.<br/><br/>If you are uploading the file from your computer, please wait for it to finish uploading (progress bar at 100%), before trying to continue.', 'wp_all_import_plugin')); |
| 36 | } elseif (!is_file($this->file)) { |
| 37 | $this->errors->add('form-validation', __('Uploaded file is empty', 'wp_all_import_plugin')); |
| 38 | } elseif ( ! preg_match('%\W(xml|gzip|zip|csv|gz|json|txt|dat|psv|sql|xls|xlsx)$%i', trim(basename($this->file)))) { |
| 39 | $this->errors->add('form-validation', __('Uploaded file must be XML, CSV, ZIP, GZIP, GZ, JSON, SQL, TXT, DAT or PSV', 'wp_all_import_plugin')); |
| 40 | } elseif (preg_match('%\W(zip)$%i', trim(basename($this->file)))) { |
| 41 | |
| 42 | if (!class_exists('PclZip')) include_once(PMXI_Plugin::ROOT_DIR.'/libraries/pclzip.lib.php'); |
| 43 | |
| 44 | $archive = new PclZip($this->file); |
| 45 | if (($v_result_list = $archive->extract(PCLZIP_OPT_PATH, $this->uploadsPath, PCLZIP_OPT_REPLACE_NEWER)) == 0) { |
| 46 | $this->errors->add('form-validation', __('WP All Import couldn\'t find a file to import inside your ZIP.<br/><br/>Either the .ZIP file is broken, or doesn\'t contain a file with an extension of XML, CSV, PSV, DAT, or TXT. <br/>Please attempt to unzip your .ZIP file on your computer to ensure it is a valid .ZIP file which can actually be unzipped, and that it contains a file which WP All Import can import.', 'wp_all_import_plugin')); |
| 47 | } |
| 48 | else { |
| 49 | |
| 50 | $filePath = ''; |
| 51 | |
| 52 | if (!empty($v_result_list)){ |
| 53 | foreach ($v_result_list as $unzipped_file) { |
| 54 | if ($unzipped_file['status'] == 'ok' and preg_match('%\W(xml|csv|txt|dat|psv|json|xls|xlsx)$%i', trim($unzipped_file['stored_filename'])) and strpos($unzipped_file['stored_filename'], 'readme.txt') === false ) { |
| 55 | if ( strpos(basename($unzipped_file['stored_filename']), 'WP All Import Template') === 0 || strpos(basename($unzipped_file['stored_filename']), 'templates_') === 0 ) |
| 56 | { |
| 57 | $template = file_get_contents($unzipped_file['filename']); |
| 58 | } |
| 59 | elseif ($filePath == '') |
| 60 | { |
| 61 | $filePath = $unzipped_file['filename']; |
| 62 | } |
| 63 | } |
| 64 | } |
| 65 | } |
| 66 | if ( $this->uploadsPath === false ){ |
| 67 | $this->errors->add('form-validation', __('WP All Import can\'t access your WordPress uploads folder.', 'wp_all_import_plugin')); |
| 68 | } |
| 69 | |
| 70 | if(empty($filePath)){ |
| 71 | $zip = zip_open(trim($this->file)); |
| 72 | if (is_resource($zip)) { |
| 73 | while ($zip_entry = zip_read($zip)) { |
| 74 | $filePath = zip_entry_name($zip_entry); |
| 75 | $fp = fopen($this->uploadsPath."/".$filePath, "w"); |
| 76 | if (zip_entry_open($zip, $zip_entry, "r")) { |
| 77 | $buf = zip_entry_read($zip_entry, zip_entry_filesize($zip_entry)); |
| 78 | fwrite($fp,"$buf"); |
| 79 | zip_entry_close($zip_entry); |
| 80 | fclose($fp); |
| 81 | } |
| 82 | break; |
| 83 | } |
| 84 | zip_close($zip); |
| 85 | |
| 86 | } else { |
| 87 | $this->errors->add('form-validation', __('WP All Import couldn\'t find a file to import inside your ZIP.<br/><br/>Either the .ZIP file is broken, or doesn\'t contain a file with an extension of XML, CSV, PSV, DAT, or TXT. <br/>Please attempt to unzip your .ZIP file on your computer to ensure it is a valid .ZIP file which can actually be unzipped, and that it contains a file which WP All Import can import.', 'wp_all_import_plugin')); |
| 88 | } |
| 89 | } |
| 90 | |
| 91 | // Detect if file is very large |
| 92 | $source = array( |
| 93 | 'name' => basename($this->file), |
| 94 | 'type' => 'upload', |
| 95 | 'path' => $this->file, |
| 96 | ); |
| 97 | |
| 98 | if (preg_match('%\W(csv|txt|dat|psv)$%i', trim($filePath))){ // If CSV file found in archieve |
| 99 | |
| 100 | if($this->uploadsPath === false){ |
| 101 | $this->errors->add('form-validation', __('WP All Import can\'t access your WordPress uploads folder.', 'wp_all_import_plugin')); |
| 102 | } |
| 103 | |
| 104 | include_once(PMXI_Plugin::ROOT_DIR.'/libraries/XmlImportCsvParse.php'); |
| 105 | $csv = new PMXI_CsvParser( array( 'filename' => $filePath, 'targetDir' => $this->uploadsPath ) ); // create chunks |
| 106 | //wp_all_import_remove_source($filePath, false); |
| 107 | $filePath = $csv->xml_path; |
| 108 | $this->is_csv = $csv->is_csv; |
| 109 | $this->root_element = 'node'; |
| 110 | |
| 111 | } elseif (preg_match('%\W(json)$%i', trim($filePath))){ |
| 112 | |
| 113 | $json_str = file_get_contents($filePath); |
| 114 | $is_json = wp_all_import_is_json($json_str); |
| 115 | |
| 116 | if( is_wp_error($is_json)){ |
| 117 | $this->errors->add('form-validation', $is_json->get_error_message(), 'wp_all_import_plugin'); |
| 118 | } |
| 119 | else{ |
| 120 | |
| 121 | $xml_data = wp_all_import_json_to_xml( json_decode($json_str, true) ); |
| 122 | |
| 123 | if ( empty($xml_data) ){ |
| 124 | $this->errors->add('form-validation', __('Can not import this file. JSON to XML convertation failed.', 'wp_all_import_plugin')); |
| 125 | } |
| 126 | else{ |
| 127 | $jsontmpname = $this->uploadsPath .'/'. wp_all_import_url_title(wp_unique_filename($this->uploadsPath, str_replace("json", "xml", basename($filePath)))); |
| 128 | file_put_contents($jsontmpname, $xml_data); |
| 129 | wp_all_import_remove_source($filePath, false); |
| 130 | $filePath = $jsontmpname; |
| 131 | } |
| 132 | } |
| 133 | |
| 134 | } elseif (preg_match('%\W(sql)$%i', trim($filePath))){ |
| 135 | |
| 136 | include_once( PMXI_Plugin::ROOT_DIR . '/libraries/XmlImportSQLParse.php' ); |
| 137 | |
| 138 | $localSQLPath = $filePath; |
| 139 | $sql = new PMXI_SQLParser( $localSQLPath, $this->uploadsPath ); |
| 140 | $filePath = $sql->parse(); |
| 141 | wp_all_import_remove_source($localSQLPath, false); |
| 142 | } |
| 143 | elseif (preg_match('%\W(xls|xlsx)$%i', trim($filePath))){ |
| 144 | |
| 145 | include_once( PMXI_Plugin::ROOT_DIR . '/libraries/XmlImportXLSParse.php' ); |
| 146 | |
| 147 | $localXLSPath = $filePath; |
| 148 | $xls = new PMXI_XLSParser( $localXLSPath, $this->uploadsPath ); |
| 149 | $filePath = $xls->parse(); |
| 150 | wp_all_import_remove_source($localXLSPath, false); |
| 151 | |
| 152 | } |
| 153 | } |
| 154 | |
| 155 | } elseif ( preg_match('%\W(csv|txt|dat|psv)$%i', trim($this->file))) { // If CSV file uploaded |
| 156 | |
| 157 | if ( $this->uploadsPath === false ){ |
| 158 | $this->errors->add('form-validation', __('WP All Import can\'t access your WordPress uploads folder.', 'wp_all_import_plugin')); |
| 159 | } |
| 160 | $filePath = $this->file; |
| 161 | $source = array( |
| 162 | 'name' => basename($this->file), |
| 163 | 'type' => 'upload', |
| 164 | 'path' => $filePath, |
| 165 | ); |
| 166 | |
| 167 | include_once(PMXI_Plugin::ROOT_DIR.'/libraries/XmlImportCsvParse.php'); |
| 168 | |
| 169 | $csv = new PMXI_CsvParser( array( 'filename' => $this->file, 'targetDir' => $this->uploadsPath ) ); |
| 170 | //@unlink($filePath); |
| 171 | $filePath = $csv->xml_path; |
| 172 | $this->is_csv = $csv->is_csv; |
| 173 | $this->root_element = 'node'; |
| 174 | |
| 175 | } elseif(preg_match('%\W(gz)$%i', trim($this->file))){ // If gz file uploaded |
| 176 | |
| 177 | $fileInfo = wp_all_import_get_gz($this->file, 0, $this->uploadsPath); |
| 178 | |
| 179 | if ( ! is_wp_error($fileInfo) ){ |
| 180 | |
| 181 | $filePath = $fileInfo['localPath']; |
| 182 | |
| 183 | // Detect if file is very large |
| 184 | $source = array( |
| 185 | 'name' => basename($this->file), |
| 186 | 'type' => 'upload', |
| 187 | 'path' => $this->file, |
| 188 | ); |
| 189 | |
| 190 | // detect CSV or XML |
| 191 | if ( $fileInfo['type'] == 'csv') { // it is CSV file |
| 192 | |
| 193 | include_once(PMXI_Plugin::ROOT_DIR.'/libraries/XmlImportCsvParse.php'); |
| 194 | $csv = new PMXI_CsvParser( array( 'filename' => $filePath, 'targeDir' => $this->uploadsPath ) ); // create chunks |
| 195 | //@unlink($filePath); |
| 196 | $filePath = $csv->xml_path; |
| 197 | $this->is_csv = $csv->is_csv; |
| 198 | $this->root_element = 'node'; |
| 199 | |
| 200 | } |
| 201 | |
| 202 | } |
| 203 | else $this->errors->add('form-validation', $fileInfo->get_error_message()); |
| 204 | |
| 205 | } elseif (preg_match('%\W(json)$%i', trim($this->file))){ |
| 206 | |
| 207 | // Detect if file is very large |
| 208 | $source = array( |
| 209 | 'name' => basename($this->file), |
| 210 | 'type' => 'upload', |
| 211 | 'path' => $this->file, |
| 212 | ); |
| 213 | |
| 214 | $json_str = file_get_contents($this->file); |
| 215 | $is_json = wp_all_import_is_json($json_str); |
| 216 | |
| 217 | if( is_wp_error($is_json)){ |
| 218 | $this->errors->add('form-validation', $is_json->get_error_message(), 'wp_all_import_plugin'); |
| 219 | } |
| 220 | else{ |
| 221 | |
| 222 | $xml_data = wp_all_import_json_to_xml( json_decode($json_str, true) ); |
| 223 | |
| 224 | if ( empty($xml_data) ){ |
| 225 | $this->errors->add('form-validation', __('Can not import this file. JSON to XML convertation failed.', 'wp_all_import_plugin')); |
| 226 | } |
| 227 | else{ |
| 228 | $jsontmpname = $this->uploadsPath .'/'. wp_all_import_url_title(wp_unique_filename($this->uploadsPath, str_replace("json", "xml", basename($this->file)))); |
| 229 | //@unlink($this->file); |
| 230 | file_put_contents($jsontmpname, $xml_data); |
| 231 | $filePath = $jsontmpname; |
| 232 | |
| 233 | } |
| 234 | } |
| 235 | |
| 236 | } elseif (preg_match('%\W(sql)$%i', trim($this->file))){ |
| 237 | |
| 238 | $source = array( |
| 239 | 'name' => basename($this->file), |
| 240 | 'type' => 'upload', |
| 241 | 'path' => $this->file, |
| 242 | ); |
| 243 | |
| 244 | include_once( PMXI_Plugin::ROOT_DIR . '/libraries/XmlImportSQLParse.php' ); |
| 245 | |
| 246 | $sql = new PMXI_SQLParser( $this->file, $this->uploadsPath ); |
| 247 | $filePath = $sql->parse(); |
| 248 | //@unlink($this->file); |
| 249 | |
| 250 | } elseif (preg_match('%\W(xls|xlsx)$%i', trim($this->file))){ |
| 251 | |
| 252 | $source = array( |
| 253 | 'name' => basename($this->file), |
| 254 | 'type' => 'upload', |
| 255 | 'path' => $this->file, |
| 256 | ); |
| 257 | |
| 258 | include_once( PMXI_Plugin::ROOT_DIR . '/libraries/XmlImportXLSParse.php' ); |
| 259 | |
| 260 | $xls = new PMXI_XLSParser( $this->file, $this->uploadsPath ); |
| 261 | $filePath = $xls->parse(); |
| 262 | |
| 263 | } else { // If XML file uploaded |
| 264 | |
| 265 | // Detect if file is very large |
| 266 | $filePath = $this->file; |
| 267 | $source = array( |
| 268 | 'name' => basename($this->file), |
| 269 | 'type' => 'upload', |
| 270 | 'path' => $filePath, |
| 271 | ); |
| 272 | |
| 273 | } |
| 274 | |
| 275 | if ( $this->errors->get_error_codes() ) return $this->errors; |
| 276 | |
| 277 | $source['path'] = wp_all_import_get_relative_path($source['path']); |
| 278 | |
| 279 | $templateOptions = json_decode($template, true); |
| 280 | |
| 281 | $options = maybe_unserialize($templateOptions[0]['options']); |
| 282 | |
| 283 | return array( |
| 284 | 'filePath' => $filePath, |
| 285 | 'source' => $source, |
| 286 | 'root_element' => $this->root_element, |
| 287 | 'is_csv' => $this->is_csv, |
| 288 | 'template' => $template, |
| 289 | 'post_type' => (!empty($options)) ? $options['custom_type'] : false |
| 290 | ); |
| 291 | } |
| 292 | } |
| 293 | } |