예제 #1
0
파일: proxy.php 프로젝트: hjimmy/owncloud
 /**
  * @param string $path Path of file from which has been read
  * @param string $data Data that has been read from file
  */
 public function postFile_get_contents($path, $data)
 {
     $plainData = null;
     $view = new \OC_FilesystemView('/');
     // init session
     $session = new \OCA\Encryption\Session($view);
     // If data is a catfile
     if (Crypt::mode() === 'server' && Crypt::isCatfileContent($data)) {
         $handle = fopen('crypt://' . $path, 'r');
         if (is_resource($handle)) {
             while (($plainDataChunk = fgets($handle, 8192)) !== false) {
                 $plainData .= $plainDataChunk;
             }
         }
     } elseif (Crypt::mode() == 'server' && \OC::$session->exists('legacyenckey') && Crypt::isEncryptedMeta($path)) {
         // Disable encryption proxy to prevent recursive calls
         $proxyStatus = \OC_FileProxy::$enabled;
         \OC_FileProxy::$enabled = false;
         $plainData = Crypt::legacyBlockDecrypt($data, $session->getLegacyKey());
         \OC_FileProxy::$enabled = $proxyStatus;
     }
     if (!isset($plainData)) {
         $plainData = $data;
     }
     return $plainData;
 }
예제 #2
0
 /**
  * @brief Encrypt all files in a directory
  * @param string $dirPath the directory whose files will be encrypted
  * @param null $legacyPassphrase
  * @param null $newPassphrase
  * @return bool
  * @note Encryption is recursive
  */
 public function encryptAll($dirPath, $legacyPassphrase = null, $newPassphrase = null)
 {
     $found = $this->findEncFiles($dirPath);
     if ($found) {
         // Disable proxy to prevent file being encrypted twice
         \OC_FileProxy::$enabled = false;
         $versionStatus = \OCP\App::isEnabled('files_versions');
         \OC_App::disable('files_versions');
         $encryptedFiles = array();
         // Encrypt unencrypted files
         foreach ($found['plain'] as $plainFile) {
             //get file info
             $fileInfo = \OC\Files\Filesystem::getFileInfo($plainFile['path']);
             //relative to data/<user>/file
             $relPath = $plainFile['path'];
             //relative to /data
             $rawPath = '/' . $this->userId . '/files/' . $plainFile['path'];
             // keep timestamp
             $timestamp = $fileInfo['mtime'];
             // Open plain file handle for binary reading
             $plainHandle = $this->view->fopen($rawPath, 'rb');
             // Open enc file handle for binary writing, with same filename as original plain file
             $encHandle = fopen('crypt://' . $rawPath . '.part', 'wb');
             if (is_resource($encHandle)) {
                 // Move plain file to a temporary location
                 $size = stream_copy_to_stream($plainHandle, $encHandle);
                 fclose($encHandle);
                 fclose($plainHandle);
                 $fakeRoot = $this->view->getRoot();
                 $this->view->chroot('/' . $this->userId . '/files');
                 $this->view->rename($relPath . '.part', $relPath);
                 // set timestamp
                 $this->view->touch($relPath, $timestamp);
                 $encSize = $this->view->filesize($relPath);
                 $this->view->chroot($fakeRoot);
                 // Add the file to the cache
                 \OC\Files\Filesystem::putFileInfo($relPath, array('encrypted' => true, 'size' => $encSize, 'unencrypted_size' => $size, 'etag' => $fileInfo['etag']));
                 $encryptedFiles[] = $relPath;
             }
         }
         // Encrypt legacy encrypted files
         if (!empty($legacyPassphrase) && !empty($newPassphrase)) {
             foreach ($found['legacy'] as $legacyFile) {
                 // Fetch data from file
                 $legacyData = $this->view->file_get_contents($legacyFile['path']);
                 // decrypt data, generate catfile
                 $decrypted = Crypt::legacyBlockDecrypt($legacyData, $legacyPassphrase);
                 $rawPath = $legacyFile['path'];
                 // enable proxy the ensure encryption is handled
                 \OC_FileProxy::$enabled = true;
                 // Open enc file handle for binary writing, with same filename as original plain file
                 $encHandle = $this->view->fopen($rawPath, 'wb');
                 if (is_resource($encHandle)) {
                     // write data to stream
                     fwrite($encHandle, $decrypted);
                     // close stream
                     fclose($encHandle);
                 }
                 // disable proxy to prevent file being encrypted twice
                 \OC_FileProxy::$enabled = false;
             }
         }
         \OC_FileProxy::$enabled = true;
         if ($versionStatus) {
             \OC_App::enable('files_versions');
         }
         $this->encryptVersions($encryptedFiles);
         // If files were found, return true
         return true;
     } else {
         // If no files were found, return false
         return false;
     }
 }