public function open(array $args = array()) { $this->_config = $args; parent::open($args); if (!empty($this->_config['path'])) { $this->cd($this->_config['path']); } }
public function init($aConfig = null) { if (!is_null($aConfig)) { $this->aFtpConfig = array_merge($this->aFtpConfig, $aConfig); } // Parts of this code expect these params to be stored Varien_Object style as // they are in the local transport. So we'll extract them from the array and // store them appropriately here. $this->setReallyArchive($this->aFtpConfig['really_archive']); $vArchivePath = $this->aFtpConfig['archive_path']; $this->setArchivePath(substr($vArchivePath, -1, 1) == '/' ? $vArchivePath : $vArchivePath . '/'); $this->_oSftp = new Aligent_Batchjob_Model_Transport_Variensftp(); set_error_handler(function ($iErrNo, $vErrString, $vErrFile, $iErrLine) { restore_error_handler(); throw new Mage_Exception("User Notice Occurred: ({$iErrNo}) {$vErrString} in line {$iErrLine} of {$vErrFile}."); }, E_USER_NOTICE); $this->_oSftp->open($this->aFtpConfig); restore_error_handler(); return $this; }
/** * 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; }