Example #1
0
 /**
  * Re-run import
  */
 public function update()
 {
     $id = $this->input->get('id');
     PMXI_Plugin::$session->clean_session($id);
     $action_type = false;
     $this->data['import'] = $item = new PMXI_Import_Record();
     if (!$id or $item->getById($id)->isEmpty()) {
         wp_redirect($this->baseUrl);
         die;
     }
     $this->data['isWizard'] = false;
     $default = PMXI_Plugin::get_default_import_options();
     $DefaultOptions = $item->options + $default;
     foreach (PMXI_Admin_Addons::get_active_addons() as $class) {
         if (class_exists($class)) {
             $DefaultOptions += call_user_func(array($class, "get_default_import_options"));
         }
     }
     $this->data['post'] =& $DefaultOptions;
     $this->data['source'] = array('path' => $item->path, 'root_element' => $item->root_element);
     $this->data['xpath'] = $item->xpath;
     $this->data['count'] = $item->count;
     $history = new PMXI_File_List();
     $history->setColumns('id', 'name', 'registered_on', 'path')->getBy(array('import_id' => $item->id), 'id DESC');
     if ($history->count()) {
         foreach ($history as $file) {
             if (@file_exists($file['path'])) {
                 $this->data['locfilePath'] = $file['path'];
                 break;
             }
         }
     }
     $chunks = 0;
     if ($this->input->post('is_confirmed') and check_admin_referer('confirm', '_wpnonce_confirm')) {
         $continue = $this->input->post('is_continue', 'no');
         // mark action type ad continue
         if ($continue == 'yes') {
             $action_type = 'continue';
         }
         $filePath = '';
         // upload new file in case when import is not continue
         if (empty(PMXI_Plugin::$session->chunk_number)) {
             if ($item->type == 'upload') {
                 // retrieve already uploaded file
                 $uploader = new PMXI_Upload(trim($item->path), $this->errors, rtrim(str_replace(basename($item->path), '', $item->path), '/'));
                 $upload_result = $uploader->upload();
                 if ($upload_result instanceof WP_Error) {
                     $this->errors = $upload_result;
                 } else {
                     $filePath = $upload_result['filePath'];
                 }
             }
             if (empty($item->options['encoding'])) {
                 $currentOptions = $item->options;
                 $currentOptions['encoding'] = 'UTF-8';
                 $item->set(array('options' => $currentOptions))->update();
             }
             @set_time_limit(0);
             $local_paths = !empty($local_paths) ? $local_paths : array($filePath);
             foreach ($local_paths as $key => $path) {
                 if (!empty($action_type) and $action_type == 'continue') {
                     $chunks = $item->count;
                 } else {
                     $file = new PMXI_Chunk($path, array('element' => $item->root_element, 'encoding' => $item->options['encoding']));
                     while ($xml = $file->read()) {
                         if (!empty($xml)) {
                             PMXI_Import_Record::preprocessXml($xml);
                             $xml = "<?xml version=\"1.0\" encoding=\"" . $item->options['encoding'] . "\"?>" . "\n" . $xml;
                             $dom = new DOMDocument('1.0', !empty($item->options['encoding']) ? $item->options['encoding'] : 'UTF-8');
                             $old = libxml_use_internal_errors(true);
                             $dom->loadXML($xml);
                             // FIX: libxml xpath doesn't handle default namespace properly, so remove it upon XML load
                             libxml_use_internal_errors($old);
                             $xpath = new DOMXPath($dom);
                             if ($elements = @$xpath->query($item->xpath) and !empty($elements) and !empty($elements->length)) {
                                 $chunks += $elements->length;
                             }
                             unset($dom, $xpath, $elements);
                         }
                     }
                     unset($file);
                 }
                 !$key and $filePath = $path;
             }
             if (empty($chunks)) {
                 $this->errors->add('form-validation', __('No matching elements found for Root element and XPath expression specified', 'wp_all_import_plugin'));
             }
         }
         if ($chunks) {
             // xml is valid
             if (!PMXI_Plugin::is_ajax() and empty(PMXI_Plugin::$session->chunk_number)) {
                 // compose data to look like result of wizard steps
                 $sesson_data = array('filePath' => $filePath, 'source' => array('name' => $item->name, 'type' => $item->type, 'path' => $item->path, 'root_element' => $item->root_element), 'feed_type' => $item->feed_type, 'update_previous' => $item->id, 'parent_import_id' => $item->parent_import_id, 'xpath' => $item->xpath, 'options' => $item->options, 'encoding' => !empty($item->options['encoding']) ? $item->options['encoding'] : 'UTF-8', 'is_csv' => !empty($item->options['delimiter']) ? $item->options['delimiter'] : PMXI_Plugin::$is_csv, 'csv_path' => PMXI_Plugin::$csv_path, 'chunk_number' => 1, 'log' => '', 'warnings' => 0, 'errors' => 0, 'start_time' => 0, 'pointer' => 1, 'count' => isset($chunks) ? $chunks : 0, 'local_paths' => !empty($local_paths) ? $local_paths : array(), 'action' => (!empty($action_type) and $action_type == 'continue') ? 'continue' : 'update', 'nonce' => wp_create_nonce('import'));
                 foreach ($sesson_data as $key => $value) {
                     PMXI_Plugin::$session->set($key, $value);
                 }
                 PMXI_Plugin::$session->save_data();
             }
             $item->set(array('canceled' => 0, 'failed' => 0))->update();
             // deligate operation to other controller
             $controller = new PMXI_Admin_Import();
             $controller->data['update_previous'] = $item;
             $controller->process();
             return;
         }
     }
     $this->render('admin/import/confirm');
 }
Example #2
0
 /**
  * Reimport
  */
 public function update()
 {
     $id = $this->input->get('id');
     $action_type = $this->input->get('type');
     $this->data['item'] = $item = new PMXI_Import_Record();
     if (!$id or $item->getById($id)->isEmpty()) {
         wp_redirect($this->baseUrl);
         die;
     }
     pmxi_session_unset();
     $chunks = 0;
     if ($this->input->post('is_confirmed')) {
         check_admin_referer('update-import', '_wpnonce_update-import');
         $uploads = wp_upload_dir();
         if (empty(PMXI_Plugin::$session->data['pmxi_import']['chunk_number'])) {
             if (in_array($item->type, array('upload'))) {
                 // if import type NOT URL
                 if (preg_match('%\\W(zip)$%i', trim(basename($item->path)))) {
                     include_once PMXI_Plugin::ROOT_DIR . '/libraries/pclzip.lib.php';
                     $archive = new PclZip(trim($item->path));
                     if (($v_result_list = $archive->extract(PCLZIP_OPT_PATH, $uploads['path'], PCLZIP_OPT_REPLACE_NEWER)) == 0) {
                         $this->errors->add('form-validation', 'Failed to open uploaded ZIP archive : ' . $archive->errorInfo(true));
                     } else {
                         $filePath = '';
                         if (!empty($v_result_list)) {
                             foreach ($v_result_list as $unzipped_file) {
                                 if ($unzipped_file['status'] == 'ok' and preg_match('%\\W(xml|csv|txt|dat|psv)$%i', trim($unzipped_file['stored_filename']))) {
                                     $filePath = $unzipped_file['filename'];
                                     break;
                                 }
                             }
                         }
                         if ($uploads['error']) {
                             $this->errors->add('form-validation', __('Can not create upload folder. Permision denied', 'pmxi_plugin'));
                         }
                         if (empty($filePath)) {
                             $zip = zip_open(trim($item->path));
                             if (is_resource($zip)) {
                                 while ($zip_entry = zip_read($zip)) {
                                     $filePath = zip_entry_name($zip_entry);
                                     $fp = fopen($uploads['path'] . "/" . $filePath, "w");
                                     if (zip_entry_open($zip, $zip_entry, "r")) {
                                         $buf = zip_entry_read($zip_entry, zip_entry_filesize($zip_entry));
                                         fwrite($fp, "{$buf}");
                                         zip_entry_close($zip_entry);
                                         fclose($fp);
                                     }
                                     break;
                                 }
                                 zip_close($zip);
                             } else {
                                 $this->errors->add('form-validation', __('Failed to open uploaded ZIP archive. Can\'t extract files.', 'pmxi_plugin'));
                             }
                         }
                         if (preg_match('%\\W(csv|txt|dat|psv)$%i', trim($filePath))) {
                             // If CSV file found in archieve
                             if ($uploads['error']) {
                                 $this->errors->add('form-validation', __('Can not create upload folder. Permision denied', 'pmxi_plugin'));
                             }
                             include_once PMXI_Plugin::ROOT_DIR . '/libraries/XmlImportCsvParse.php';
                             $csv = new PMXI_CsvParser($filePath, true, '', !empty($item->options['delimiter']) ? $item->options['delimiter'] : '', !empty($item->options['encoding']) ? $item->options['encoding'] : '');
                             // create chunks
                             $filePath = $csv->xml_path;
                         }
                     }
                 } elseif (preg_match('%\\W(csv|txt|dat|psv)$%i', trim($item->path))) {
                     // If CSV file uploaded
                     if ($uploads['error']) {
                         $this->errors->add('form-validation', __('Can not create upload folder. Permision denied', 'pmxi_plugin'));
                     }
                     include_once PMXI_Plugin::ROOT_DIR . '/libraries/XmlImportCsvParse.php';
                     $csv = new PMXI_CsvParser($item->path, true, '', !empty($item->options['delimiter']) ? $item->options['delimiter'] : '', !empty($item->options['encoding']) ? $item->options['encoding'] : '');
                     $filePath = $csv->xml_path;
                 } elseif (preg_match('%\\W(gz)$%i', trim($item->path))) {
                     // If gz file uploaded
                     $fileInfo = pmxi_gzfile_get_contents($item->path);
                     if (!is_wp_error($fileInfo)) {
                         $filePath = $fileInfo['localPath'];
                         // detect CSV or XML
                         if ($fileInfo['type'] == 'csv') {
                             // it is CSV file
                             include_once PMXI_Plugin::ROOT_DIR . '/libraries/XmlImportCsvParse.php';
                             $csv = new PMXI_CsvParser($filePath, true, '', !empty($item->options['delimiter']) ? $item->options['delimiter'] : '', !empty($item->options['encoding']) ? $item->options['encoding'] : '');
                             // create chunks
                             $filePath = $csv->xml_path;
                         }
                     } else {
                         $this->errors->add('form-validation', $fileInfo->get_error_message());
                     }
                 } else {
                     // If XML file uploaded
                     $filePath = $item->path;
                 }
             }
             @set_time_limit(0);
             $local_paths = !empty($local_paths) ? $local_paths : array($filePath);
             foreach ($local_paths as $key => $path) {
                 if (!empty($action_type) and $action_type == 'continue') {
                     $chunks = $item->count;
                 } else {
                     $file = new PMXI_Chunk($path, array('element' => $item->root_element, 'encoding' => $item->options['encoding']));
                     while ($xml = $file->read()) {
                         if (!empty($xml)) {
                             PMXI_Import_Record::preprocessXml($xml);
                             $xml = "<?xml version=\"1.0\" encoding=\"" . $item->options['encoding'] . "\"?>" . "\n" . $xml;
                             $dom = new DOMDocument('1.0', !empty($item->options['encoding']) ? $item->options['encoding'] : 'UTF-8');
                             $old = libxml_use_internal_errors(true);
                             $dom->loadXML($xml);
                             // FIX: libxml xpath doesn't handle default namespace properly, so remove it upon XML load
                             libxml_use_internal_errors($old);
                             $xpath = new DOMXPath($dom);
                             if ($elements = @$xpath->query($item->xpath) and !empty($elements) and !empty($elements->length)) {
                                 $chunks += $elements->length;
                             }
                             unset($dom, $xpath, $elements);
                         }
                     }
                     unset($file);
                 }
                 !$key and $filePath = $path;
             }
             if (empty($chunks)) {
                 $this->errors->add('form-validation', __('No matching elements found for Root element and XPath expression specified', 'pmxi_plugin'));
             }
         }
         if ($chunks) {
             // xml is valid
             if (!PMXI_Plugin::is_ajax() and empty(PMXI_Plugin::$session->data['pmxi_import']['chunk_number'])) {
                 // compose data to look like result of wizard steps
                 PMXI_Plugin::$session['pmxi_import'] = array('filePath' => $filePath, 'source' => array('name' => $item->name, 'type' => $item->type, 'path' => $item->path, 'root_element' => $item->root_element), 'feed_type' => $item->feed_type, 'update_previous' => $item->id, 'parent_import_id' => $item->parent_import_id, 'xpath' => $item->xpath, 'template' => $item->template, 'options' => $item->options, 'encoding' => !empty($item->options['encoding']) ? $item->options['encoding'] : 'UTF-8', 'is_csv' => !empty($item->options['delimiter']) ? $item->options['delimiter'] : PMXI_Plugin::$is_csv, 'csv_path' => PMXI_Plugin::$csv_path, 'scheduled' => $item->scheduled, 'chunk_number' => 1, 'log' => '', 'warnings' => 0, 'errors' => 0, 'start_time' => 0, 'pointer' => 1, 'count' => isset($chunks) ? $chunks : 0, 'local_paths' => !empty($local_paths) ? $local_paths : array(), 'action' => (!empty($action_type) and $action_type == 'continue') ? 'continue' : 'update');
                 pmxi_session_commit();
             }
             // deligate operation to other controller
             $controller = new PMXI_Admin_Import();
             $controller->data['update_previous'] = $item;
             $controller->process();
             return;
         }
     }
     $this->render();
 }