Beispiel #1
0
 function importFromFile(&$importFile, $process = true)
 {
     $app = JFactory::getApplication();
     if (empty($importFile['name'])) {
         $app->enqueueMessage(JText::_('BROWSE_FILE'), 'notice');
         return false;
     }
     $this->charsetConvert = JRequest::getString('charsetconvert', '');
     jimport('joomla.filesystem.file');
     $config =& hikashop_config();
     $allowedFiles = explode(',', strtolower($config->get('allowedfiles')));
     $uploadFolder = JPath::clean(html_entity_decode($config->get('uploadfolder')));
     $uploadFolder = trim($uploadFolder, DS . ' ') . DS;
     $uploadPath = JPath::clean(HIKASHOP_ROOT . $uploadFolder);
     if (!is_dir($uploadPath)) {
         jimport('joomla.filesystem.folder');
         JFolder::create($uploadPath);
         JFile::write($uploadPath . 'index.html', '<html><body bgcolor="#FFFFFF"></body></html>');
     }
     if (!is_writable($uploadPath)) {
         @chmod($uploadPath, '0755');
         if (!is_writable($uploadPath)) {
             $app->enqueueMessage(JText::sprintf('WRITABLE_FOLDER', $uploadPath), 'notice');
         }
     }
     $attachment = new stdClass();
     $attachment->filename = strtolower(JFile::makeSafe($importFile['name']));
     $attachment->size = $importFile['size'];
     if (!preg_match('#\\.(' . str_replace(array(',', '.'), array('|', '\\.'), $config->get('allowedfiles')) . ')$#Ui', $attachment->filename, $extension) || preg_match('#\\.(php.?|.?htm.?|pl|py|jsp|asp|sh|cgi)$#Ui', $attachment->filename)) {
         $app->enqueueMessage(JText::sprintf('ACCEPTED_TYPE', substr($attachment->filename, strrpos($attachment->filename, '.') + 1), $config->get('allowedfiles')), 'notice');
         return false;
     }
     $attachment->filename = str_replace(array('.', ' '), '_', substr($attachment->filename, 0, strpos($attachment->filename, $extension[0]))) . $extension[0];
     if (!move_uploaded_file($importFile['tmp_name'], $uploadPath . $attachment->filename)) {
         if (!JFile::upload($importFile['tmp_name'], $uploadPath . $attachment->filename)) {
             $app->enqueueMessage(JText::sprintf('FAIL_UPLOAD', $importFile['tmp_name'], $uploadPath . $attachment->filename), 'error');
         }
     }
     hikashop_increasePerf();
     $contentFile = file_get_contents($uploadPath . $attachment->filename);
     if (!$contentFile) {
         $app->enqueueMessage(JText::sprintf('FAIL_OPEN', $uploadPath . $attachment->filename), 'error');
         return false;
     }
     if ($process) {
         unlink($uploadPath . $attachment->filename);
         $toTest = array();
         if (empty($this->charsetConvert)) {
             $encodingHelper = hikashop_get('helper.encoding');
             $this->charsetConvert = $encodingHelper->detectEncoding($contentFile);
         }
         return $this->handleContent($contentFile);
     } else {
         $filePath = $uploadPath . $attachment->filename;
         return $filePath;
     }
 }
Beispiel #2
0
 function getFromFile($element, $check = false)
 {
     $data = new stdClass();
     $app = JFactory::getApplication();
     $importHelper = hikashop_get('helper.import');
     $importFile = array();
     $elts = explode('/', $element['path']);
     if ($elts == null) {
         return false;
     }
     $nb = count($elts);
     if ($nb == 1) {
         $elts = explode('\\', $element['path']);
         $nb = count($elts);
     }
     if (!file_exists($element['path'])) {
         if (!$check) {
             $app->enqueueMessage(JText::sprintf('NO_FILE_FOUND', $element['path']), 'error');
         }
         $data->error = 'not_found';
         return $data;
     }
     hikashop_increasePerf();
     $contentFile = @file_get_contents($element['path']);
     if (!$contentFile) {
         if (!$check) {
             $app->enqueueMessage(JText::sprintf('FAIL_OPEN', $element['path']), 'error');
         }
         $data->error = 'fail_open';
         return $data;
     }
     $contentFile = str_replace(array("\r\n", "\r"), "\n", $contentFile);
     $importLines = explode("\n", $contentFile);
     $columns = strip_tags(str_replace('"', '', $importLines[0]));
     $listSeparators = array(';', ',', '|', "\t");
     $separator = ';';
     foreach ($listSeparators as $sep) {
         if (preg_match('#(?!\\\\)' . $sep . '#', $columns)) {
             $separator = $sep;
             $columns = str_replace($sep, '|', $columns);
             break;
         }
     }
     unset($importLines[0]);
     if (empty($importLines)) {
         if (!$check) {
             $app->enqueueMessage('EMPTY', 'error');
         }
         $data->error = 'empty';
         return $data;
     }
     $columns = explode('|', $columns);
     $numberColumns = count($columns);
     $db = JFactory::getDBO();
     if (!HIKASHOP_J25) {
         $tmp = $db->getTableFields(hikashop_table('product'));
         $productColumns = reset($tmp);
         unset($tmp);
         $tmp = $db->getTableFields(hikashop_table('price'));
         $priceColumns = reset($tmp);
         unset($tmp);
     } else {
         $productColumns = $db->getTableColumns(hikashop_table('product'));
         $priceColumns = $db->getTableColumns(hikashop_table('price'));
     }
     $db->setQuery('SELECT characteristic_value FROM ' . hikashop_table('characteristic') . ' WHERE characteristic_parent_id = 0');
     $characteristicColumns = $db->loadResultArray();
     $categoryColumns = array('categories_ordering', 'parent_category', 'categories_image', 'categories');
     $otherColumns = array('files', 'images', 'related', 'options', 'price_value_with_tax');
     if (!is_array($characteristicColumns)) {
         $characteristicColumns = array($characteristicColumns);
     }
     $mergedColumns = array_merge(array_keys($productColumns), array_keys($priceColumns), $categoryColumns, $characteristicColumns, $otherColumns);
     $wrongColumns = array_diff($columns, $mergedColumns);
     if (!empty($wrongColumns) && $check) {
         if (!$check) {
             $app->enqueueMessage('WRONG_COLUMNS', 'error');
         }
         $data->wrongColumns = $wrongColumns;
         $data->validColumns = $mergedColumns;
         $data->error = 'wrong_columns';
         return $data;
     } elseif ($check) {
         $data->error = 'valid';
         return $data;
     }
     if (isset($element['change']) && is_array($element['change'])) {
         foreach ($columns as $num => $column) {
             foreach ($element['change'] as $key => $value) {
                 if ($column == $key && $value != 'delete') {
                     $columns[$num] = $value;
                 } elseif ($column == $key && $value == 'delete') {
                     unset($columns[$num]);
                 }
             }
         }
     }
     $pool = array();
     $missingIds = array();
     $missingCodes = array();
     foreach ($importLines as $key => $importLine) {
         $product = $this->getProduct($importLines, $key, $numberColumns, $separator);
         if (!is_array($product)) {
             continue;
         }
         $newProduct = new stdClass();
         foreach ($product as $num => $value) {
             if (!empty($columns[$num])) {
                 $field = $columns[$num];
                 if (strpos('|', $field) !== false) {
                     $field = str_replace('|', '__tr__', $field);
                 }
                 $newProduct->{$field} = preg_replace('#^[\'" ]{1}(.*)[\'" ]{1}$#', '$1', $value);
             }
         }
         $pool[$key] = new stdClass();
         if (isset($newProduct->product_id)) {
             $pool[$key]->product_id = $newProduct->product_id;
         }
         if (isset($newProduct->product_code)) {
             $pool[$key]->product_code = $newProduct->product_code;
         }
         if (!isset($pool[$key]->product_id) && !isset($pool[$key]->product_code)) {
             continue;
         }
         if (!isset($pool[$key]->product_code)) {
             $missingCodes[] = $pool[$key]->product_id;
         }
         if (!isset($pool[$key]->product_id)) {
             $missingIds[] = $pool[$key]->product_code;
         }
     }
     $missing = array();
     if (!empty($missingCodes)) {
         $db->setQuery('SELECT * FROM ' . hikashop_table('product') . ' WHERE product_id IN (' . implode(',', $missingCodes) . ')');
         $missingCodes = $db->loadObjectList();
     }
     if (!empty($missingIds)) {
         $db->setQuery('SELECT * FROM ' . hikashop_table('product') . ' WHERE product_code IN (' . implode(',', $db->quote($missingIds)) . ')');
         $missingIds = $db->loadObjectList();
     }
     $errorcount = 0;
     $importProducts = array();
     foreach ($importLines as $key => $importLine) {
         $product = $this->getProduct($importLines, $key, $numberColumns, $separator);
         if (!is_array($product)) {
             continue;
         }
         $newProduct = new stdClass();
         foreach ($product as $num => $value) {
             if (!empty($columns[$num])) {
                 $field = $columns[$num];
                 if (strpos('|', $field) !== false) {
                     $field = str_replace('|', '__tr__', $field);
                 }
                 $newProduct->{$field} = preg_replace('#^[\'" ]{1}(.*)[\'" ]{1}$#', '$1', $value);
             }
         }
         if (empty($newProduct->product_id)) {
             foreach ($missingIds as $missingId) {
                 if ($newProduct->product_code == $missingId->product_code) {
                     $newProduct->product_id = $missingId->product_id;
                 }
             }
         }
         if (empty($newProduct->product_code)) {
             foreach ($missingCodes as $missingCode) {
                 if ($newProduct->product_id == $missingCode->product_id) {
                     $newProduct->product_code = $missingCode->product_code;
                 }
             }
         }
         if (empty($newProduct->product_code) || empty($newProduct->product_id)) {
             $errorcount++;
             if ($errorcount < 20) {
                 if (isset($importLine[$key - 1])) {
                     $app->enqueueMessage(JText::sprintf('IMPORT_ERRORLINE', $importLines[$key - 1]) . ' ' . JText::_('PRODUCT_NOT_FOUND'), 'notice');
                 }
             } elseif ($errorcount == 20) {
                 $app->enqueueMessage('...', 'notice');
             }
         } else {
             $importProducts[$newProduct->product_id] = $newProduct;
             $ids[] = $newProduct->product_id;
         }
     }
     $data->ids = $ids;
     $data->elements = $importProducts;
     return $data;
 }