private function _fetchImportFile($remoteFilename) { $success = TRUE; $localFilename = FALSE; // Check whether the directory exist, create it if not if (!file_exists($this->getTempImportDir())) { $success = $success && mkdir($this->getTempImportDir(), 0777, TRUE); } // Make sure the directory is a directory (not a file) and is writable $success = $success && is_dir($this->getTempImportDir()) && is_writable($this->getTempImportDir()); // Test the remote filename $success = $success && FALSE !== $remoteFilename; // Create a temporary file on the local system $success = $success && FALSE !== ($localFilename = tempnam($this->getTempImportDir(), $remoteFilename)); // #70 File permissions // tempnam creates a file with 0600 perms, so need to fix that $success = $success && FALSE !== chmod($localFilename, 0664); // ug=rw,o=r $this->getLogger()->log('Fetching remote file: ' . $remoteFilename . ' to: ' . $localFilename, Zend_Log::INFO); // cd into the directory and get the file, saving it to a local temp file // success must come second here to make sure pushd is called to match the popd that happens later. $success = $this->_pushd($this->aFtpConfig['fetch_path']) && $success; $success = $success && FALSE !== $this->_oSftp->read($remoteFilename, $localFilename); if (FALSE !== $remoteFilename && FALSE === $this->_oSftp->rm($remoteFilename)) { $this->getLogger()->log('Error deleting remote file: ' . $remoteFilename . ' from remote working directory: ' . $this->_oSftp->pwd(), Zend_Log::ERR); $this->getLogger()->log('Underlying error messages: ' . $this->_oSftp->getErrors(), Zend_Log::ERR); throw new Exception('Unable to delete remote file ' . $remoteFilename . ' Underlying errors: ' . $this->_oSftp->getErrors()); } else { $this->getLogger()->log('Deleted remote file: ' . $remoteFilename, Zend_Log::NOTICE); } // If there was an error, but the tempfile had been created if (!$success && !!$localFilename) { unlink($localFilename); // delete the file $localFilename = FALSE; } $this->_popd(); return $localFilename; }
/** * Goes to specified host/path and fetches reports from there. * Save reports to database. * * @param array $config SFTP credentials * @return int Number of report rows that were fetched and saved successfully */ public function fetchAndSave($config) { $connection = new Varien_Io_Sftp(); $connection->open(array('host' => $config['hostname'], 'username' => $config['username'], 'password' => $config['password'])); $connection->cd($config['path']); $fetched = 0; $listing = $this->_filterReportsList($connection->rawls()); foreach ($listing as $filename => $attributes) { $localCsv = tempnam(Mage::getConfig()->getOptions()->getTmpDir(), 'PayPal_STL'); if ($connection->read($filename, $localCsv)) { if (!is_writable($localCsv)) { Mage::throwException(Mage::helper('paypalmx')->__('Cannot create target file for reading reports.')); } $encoded = file_get_contents($localCsv); $csvFormat = 'new'; if (self::FILES_OUT_CHARSET != mb_detect_encoding($encoded)) { $decoded = @iconv(self::FILES_IN_CHARSET, self::FILES_OUT_CHARSET . '//IGNORE', $encoded); file_put_contents($localCsv, $decoded); $csvFormat = 'old'; } // Set last modified date, this value will be overwritten during parsing if (isset($attributes['mtime'])) { $lastModified = new Zend_Date($attributes['mtime']); $this->setReportLastModified($lastModified->toString(Varien_Date::DATETIME_INTERNAL_FORMAT)); } $this->setReportDate($this->_fileNameToDate($filename))->setFilename($filename)->parseCsv($localCsv, $csvFormat); if ($this->getAccountId()) { $this->save(); } if ($this->_dataSaveAllowed) { $fetched += count($this->_rows); } // clean object and remove parsed file $this->unsetData(); unlink($localCsv); } } return $fetched; }
/** * Download Images (Step 6) * * @param Pimgento_Core_Model_Task $task * * @return bool */ public function downloadImages($task) { $adapter = $this->getAdapter(); $ftp = null; try { $connexion = Mage::getStoreConfig('pimdata/asset/connexion'); if ($connexion == 'ftp') { /* @var $ftp Varien_Io_Ftp */ $ftp = new Varien_Io_Ftp(); $config = array('host' => Mage::getStoreConfig('pimdata/asset/host'), 'user' => Mage::getStoreConfig('pimdata/asset/user'), 'password' => Mage::getStoreConfig('pimdata/asset/password')); if (Mage::getStoreConfig('pimdata/asset/directory')) { $config['path'] = Mage::getStoreConfig('pimdata/asset/directory'); } if (Mage::getStoreConfig('pimdata/asset/passive')) { $config['passive'] = true; } $ftp->open($config); } else { if ($connexion == 'sftp') { /* @var $ftp Varien_Io_Sftp */ $ftp = new Varien_Io_Sftp(); $config = array('host' => Mage::getStoreConfig('pimdata/asset/host'), 'username' => Mage::getStoreConfig('pimdata/asset/user'), 'password' => Mage::getStoreConfig('pimdata/asset/password')); $ftp->open($config); if (Mage::getStoreConfig('pimdata/asset/directory')) { $ftp->cd(Mage::getStoreConfig('pimdata/asset/directory')); } } else { $task->error(Mage::helper('pimgento_asset')->__('Connexion type %s is not authorised', $connexion)); } } if ($ftp) { $select = $adapter->select()->from($adapter->getTableName('pimgento_asset'), array('file', 'image')); $query = $adapter->query($select); $directory = Mage::helper('pimgento_asset')->getBaseMediaPath(); while ($row = $query->fetch()) { $dir = dirname($directory . $row['image']); if (!is_dir($dir)) { mkdir($dir, 0777, true); } $ftp->read($row['file'], $directory . $row['image']); } $ftp->close(); } } catch (Exception $e) { $task->error(Mage::helper('pimgento_asset')->__($e->getMessage())); } return true; }