PageRenderTime 46ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 0ms

/plugins/wp-all-import/classes/upload.php

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