/** * Move the file to the workspace. * * @param string $srcPath The original file position * @param string $filename the filename * * @return mixed the new path if success, else false */ protected function handleFile($srcPath, $filename) { Billrun_Factory::log('Relocate receive handle file ' . $filename, Zend_Log::INFO); $srcPath = parent::handleFile($srcPath, $filename); Billrun_Factory::dispatcher()->trigger('beforeRelocateFileHandling', array($this, &$srcPath, $filename)); $newPath = $this->workspace . DIRECTORY_SEPARATOR . static::$type; if (!file_exists($newPath)) { mkdir($newPath, 0777, true); } $newPath .= DIRECTORY_SEPARATOR . $filename; $ret = $this->moveFiles ? copy($srcPath, $newPath) && unlink($srcPath) : copy($srcPath, $newPath); if ($this->preserve_timestamps) { $timestamp = filemtime($srcPath); Billrun_Util::setFileModificationTime($newPath, $timestamp); } Billrun_Factory::dispatcher()->trigger('afterRelocateFileHandling', array($this, &$srcPath, &$newPath, $filename, $ret)); return $ret ? $newPath : FALSE; }
/** * method to backup the processed file * @param string $path the path to backup the file to. * @param boolean $copy copy or rename (move) the file to backup * * @return boolean return true if success to backup */ public function backupToPath($path, $copy = false) { if ($copy) { $callback = "copy"; } else { $callback = "rename"; } if (!file_exists($path)) { @mkdir($path, 0777, true); } $target_path = $path . DIRECTORY_SEPARATOR . $this->filename; $ret = @call_user_func_array($callback, array($this->filePath, $target_path)); if ($callback == 'copy' && $this->preserve_timestamps) { $timestamp = filemtime($this->filePath); Billrun_Util::setFileModificationTime($target_path, $timestamp); } return $ret; }
/** * 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; }
/** * method to backup the processed file * @param string $trgtPath the path to backup the file to. * @param boolean $copy copy or rename (move) the file to backup * * @return boolean return true if success to backup */ public function backupToPath($srcPath, $trgtPath, $preserve_timestamps = true, $copy = true) { if ($copy) { $callback = "copy"; } else { $callback = "rename"; // php move } if (!file_exists($trgtPath)) { @mkdir($trgtPath, 0777, true); } $filename = basename($srcPath); $target_path = $trgtPath . DIRECTORY_SEPARATOR . $filename; Billrun_Factory::log()->log("Backing up file from : " . $srcPath . " to : " . $trgtPath, Zend_Log::INFO); $timestamp = filemtime($srcPath); // this will be used after copy/move to preserve timestamp $ret = @call_user_func_array($callback, array($srcPath, $target_path)); if ($preserve_timestamps) { Billrun_Util::setFileModificationTime($target_path, $timestamp); } return $ret; }