public function stream_open($path, $mode, $options, &$opened_path) { $this->loadContext('ocencryption'); $this->position = 0; $this->cache = ''; $this->writeFlag = false; $this->unencryptedBlockSize = $this->encryptionModule->getUnencryptedBlockSize($this->signed); if ($mode === 'w' || $mode === 'w+' || $mode === 'wb' || $mode === 'wb+' || $mode === 'r+' || $mode === 'rb+') { $this->readOnly = false; } else { $this->readOnly = true; } $sharePath = $this->fullPath; if (!$this->storage->file_exists($this->internalPath)) { $sharePath = dirname($sharePath); } $accessList = $this->file->getAccessList($sharePath); $this->newHeader = $this->encryptionModule->begin($this->fullPath, $this->uid, $mode, $this->header, $accessList); if ($mode === 'w' || $mode === 'w+' || $mode === 'wb' || $mode === 'wb+') { // We're writing a new file so start write counter with 0 bytes $this->unencryptedSize = 0; $this->writeHeader(); $this->headerSize = $this->util->getHeaderSize(); $this->size = $this->headerSize; } else { $this->skipHeader(); } return true; }
public function stream_seek($offset, $whence = SEEK_SET) { $return = false; switch ($whence) { case SEEK_SET: if ($offset < $this->unencryptedSize && $offset >= 0) { $newPosition = $offset; } break; case SEEK_CUR: if ($offset >= 0) { $newPosition = $offset + $this->position; } break; case SEEK_END: if ($this->unencryptedSize + $offset >= 0) { $newPosition = $this->unencryptedSize + $offset; } break; default: return $return; } $newFilePosition = floor($newPosition / $this->unencryptedBlockSize) * $this->util->getBlockSize() + $this->util->getHeaderSize(); $oldFilePosition = parent::stream_tell(); if (parent::stream_seek($newFilePosition)) { parent::stream_seek($oldFilePosition); $this->flush(); parent::stream_seek($newFilePosition); $this->position = $newPosition; $return = true; } return $return; }
/** * read first block of encrypted file, typically this will contain the * encryption header * * @param string $path * @return string */ protected function readFirstBlock($path) { $firstBlock = ''; if ($this->storage->file_exists($path)) { $handle = $this->storage->fopen($path, 'r'); $firstBlock = fread($handle, $this->util->getHeaderSize()); fclose($handle); } return $firstBlock; }
/** * read header from file * * @param string $path * @return array */ protected function getHeader($path) { $header = ''; if ($this->storage->file_exists($path)) { $handle = $this->storage->fopen($path, 'r'); $header = fread($handle, $this->util->getHeaderSize()); fclose($handle); } return $this->util->readHeader($header); }
/** * read header from file * * @param string $path * @return array */ protected function getHeader($path) { $header = ''; if ($this->storage->file_exists($path)) { $handle = $this->storage->fopen($path, 'r'); $firstBlock = fread($handle, $this->util->getHeaderSize()); fclose($handle); if (substr($firstBlock, 0, strlen(Util::HEADER_START)) === Util::HEADER_START) { $header = $firstBlock; } } return $header; }
/** * return header size of given file * * @param string $path * @return int */ protected function getHeaderSize($path) { $headerSize = 0; $realFile = $this->util->stripPartialFileExtension($path); if ($this->storage->file_exists($realFile)) { $path = $realFile; } $firstBlock = $this->readFirstBlock($path); if (substr($firstBlock, 0, strlen(Util::HEADER_START)) === Util::HEADER_START) { $headerSize = $this->util->getHeaderSize(); } return $headerSize; }