/** * process the data returned from a directory content listing. * @param type $lines the lines that were returned from the directoryt file listing */ protected function processDirectoryData($lines) { $parser = Zend_Ftp_Factory::getParser($this->_ftp->getSysType()); foreach ($lines as $line) { $fileData = $parser->parseFileDirectoryListing($line); if ($fileData['type'] != 'l') { $this->_data[] = $fileData; } } $this->_count = count($this->_data); }
/** * * @return int the last modified time as a Unix timestamp */ public function getModificationTime() { $timestamp = @ftp_mdtm($this->_ftp->getConnection(), $this->_path); if ($timestamp == -1) { // try to get the timestamp by the file extra data if (isset($this->extraData['date']) && $this->extraData['date'] != -1) { $timestamp = $this->extraData['date']; } else { return false; } } return $timestamp; }
/** * Create the directory * * @return Zend_Ftp_Directory */ public function create($permissions = null) { $makedir = @ftp_mkdir($this->_ftp->getConnection(), $this->_path); if ($makedir === false) { //throw new Zend_Ftp_Directory_Exception('Unable to create directory "' . $dir . '"'); } if ($permissions !== null) { $chmod = $this->_ftp->chmod($this->_path, $permissions); if ($chmod === false) { //throw new Zend_Ftp_Directory_Exception('Unable to chmod directory "' . $dir . '"'); } } return $this; }
/** * Receive files from the ftp host. * @param type $hostName the ftp hostname/alias * @param type $config the ftp configuration * @return array conatining the path to the received files. */ protected function receiveFromHost($hostName, $config) { $ret = array(); $files = $this->ftp->getDirectory($config['remote_directory'])->getContents(); Billrun_Factory::log()->log("FTP: Starting to receive from remote host : {$hostName}", Zend_Log::DEBUG); $count = 0; foreach ($this->sortByFileDate($files) as $file) { Billrun_Factory::log()->log("FTP: Found file " . $file->name . " on remote host", Zend_Log::DEBUG); $extraData = array(); Billrun_Factory::dispatcher()->trigger('beforeFTPFileReceived', array(&$file, $this, $hostName, &$extraData)); $isFileReceivedMoreFields = array('retrieved_from' => $hostName); if ($extraData) { $isFileReceivedMoreFields['extra_data'] = $extraData; } if (!$file->isFile()) { Billrun_Factory::log()->log("FTP: " . $file->name . " is not a file", Zend_Log::DEBUG); continue; } if (!$this->isFileValid($file->name, $file->path)) { Billrun_Factory::log()->log("FTP: " . $file->name . " is not a valid file", Zend_Log::DEBUG); continue; } if ($this->isFileReceived($file->name, static::$type, $isFileReceivedMoreFields)) { Billrun_Factory::log()->log("FTP: " . $file->name . " received already", Zend_Log::DEBUG); continue; } Billrun_Factory::log()->log("FTP: Download file " . $file->name . " from remote host", Zend_Log::INFO); $targetPath = $this->workspace; if (substr($targetPath, -1) != '/') { $targetPath .= '/'; } $targetPath .= date("Ym") . DIRECTORY_SEPARATOR . substr(md5(serialize($config)), 0, 7) . DIRECTORY_SEPARATOR; if (!file_exists($targetPath)) { mkdir($targetPath, 0777, true); } if ($file->saveToPath($targetPath, null, 0, true) === FALSE) { // the last arg declare try to recover on failure Billrun_Factory::log()->log("FTP: failed to download " . $file->name . " from remote host", Zend_Log::ALERT); continue; } $received_path = $targetPath . $file->name; if ($this->preserve_timestamps) { $timestamp = $file->getModificationTime(); if ($timestamp !== FALSE) { Billrun_Util::setFileModificationTime($received_path, $timestamp); } } Billrun_Factory::dispatcher()->trigger('afterFTPFileReceived', array(&$received_path, $file, $this, $hostName, $extraData)); if ($this->logDB($received_path, $hostName, $extraData)) { $ret[] = $received_path; $count++; //count the file as recieved // delete the file after downloading and store it to processing queue if (Billrun_Factory::config()->isProd() && (isset($config['delete_received']) && $config['delete_received'])) { Billrun_Factory::log()->log("FTP: Deleting file {$file->name} from remote host ", Zend_Log::DEBUG); $file->delete(); } } if ($count >= $this->limit) { break; } } return $ret; }