/**
  * load text file
  *
  * @NoAdminRequired
  *
  * @param string $dir
  * @param string $filename
  * @return DataResponse
  */
 public function load($dir, $filename)
 {
     try {
         if (!empty($filename)) {
             $path = $dir . '/' . $filename;
             $filecontents = $this->view->file_get_contents($path);
             if ($filecontents !== false) {
                 $writable = $this->view->isUpdatable($path);
                 $mime = $this->view->getMimeType($path);
                 $mtime = $this->view->filemtime($path);
                 $encoding = mb_detect_encoding($filecontents . "a", "UTF-8, WINDOWS-1252, ISO-8859-15, ISO-8859-1, ASCII", true);
                 if ($encoding == "") {
                     // set default encoding if it couldn't be detected
                     $encoding = 'ISO-8859-15';
                 }
                 $filecontents = iconv($encoding, "UTF-8", $filecontents);
                 return new DataResponse(['filecontents' => $filecontents, 'writeable' => $writable, 'mime' => $mime, 'mtime' => $mtime], Http::STATUS_OK);
             } else {
                 return new DataResponse(['message' => (string) $this->l->t('Can not read the file.')], Http::STATUS_BAD_REQUEST);
             }
         } else {
             return new DataResponse(['message' => (string) $this->l->t('Invalid file path supplied.')], Http::STATUS_BAD_REQUEST);
         }
     } catch (\Exception $e) {
         $hint = method_exists($e, 'getHint') ? $e->getHint() : $e->getMessage();
         return new DataResponse(['message' => (string) $hint], Http::STATUS_BAD_REQUEST);
     }
 }
 /**
  * load text file
  *
  * @NoAdminRequired
  *
  * @param string $dir
  * @param string $filename
  * @return DataResponse
  */
 public function load($dir, $filename)
 {
     try {
         if (!empty($filename)) {
             $path = $dir . '/' . $filename;
             // default of 4MB
             $maxSize = 4194304;
             if ($this->view->filesize($path) > $maxSize) {
                 return new DataResponse(['message' => (string) $this->l->t('This file is too big to be opened. Please download the file instead.')], Http::STATUS_BAD_REQUEST);
             }
             $fileContents = $this->view->file_get_contents($path);
             if ($fileContents !== false) {
                 $writable = $this->view->isUpdatable($path);
                 $mime = $this->view->getMimeType($path);
                 $mTime = $this->view->filemtime($path);
                 $encoding = mb_detect_encoding($fileContents . "a", "UTF-8, WINDOWS-1252, ISO-8859-15, ISO-8859-1, ASCII", true);
                 if ($encoding == "") {
                     // set default encoding if it couldn't be detected
                     $encoding = 'ISO-8859-15';
                 }
                 $fileContents = iconv($encoding, "UTF-8", $fileContents);
                 return new DataResponse(['filecontents' => $fileContents, 'writeable' => $writable, 'mime' => $mime, 'mtime' => $mTime], Http::STATUS_OK);
             } else {
                 return new DataResponse(['message' => (string) $this->l->t('Cannot read the file.')], Http::STATUS_BAD_REQUEST);
             }
         } else {
             return new DataResponse(['message' => (string) $this->l->t('Invalid file path supplied.')], Http::STATUS_BAD_REQUEST);
         }
     } catch (LockedException $e) {
         $message = (string) $this->l->t('The file is locked.');
         return new DataResponse(['message' => $message], Http::STATUS_BAD_REQUEST);
     } catch (ForbiddenException $e) {
         return new DataResponse(['message' => $e->getMessage()], Http::STATUS_BAD_REQUEST);
     } catch (HintException $e) {
         $message = (string) $e->getHint();
         return new DataResponse(['message' => $message], Http::STATUS_BAD_REQUEST);
     } catch (\Exception $e) {
         $message = (string) $this->l->t('An internal server error occurred.');
         return new DataResponse(['message' => $message], Http::STATUS_BAD_REQUEST);
     }
 }
Example #3
0
 public static function getMimeType($path)
 {
     return self::$defaultInstance->getMimeType($path);
 }
Example #4
0
 /**
  * @return bool
  */
 public function stream_close()
 {
     $this->flush();
     // if there is no valid private key return false
     if ($this->privateKey === false) {
         // cleanup
         if ($this->meta['mode'] !== 'r' && $this->meta['mode'] !== 'rb' && !$this->isLocalTmpFile) {
             // Disable encryption proxy to prevent recursive calls
             $proxyStatus = \OC_FileProxy::$enabled;
             \OC_FileProxy::$enabled = false;
             if ($this->rootView->file_exists($this->rawPath) && $this->size === 0) {
                 fclose($this->handle);
                 $this->rootView->unlink($this->rawPath);
             }
             // Re-enable proxy - our work is done
             \OC_FileProxy::$enabled = $proxyStatus;
         }
         // if private key is not valid redirect user to a error page
         Helper::redirectToErrorPage($this->session);
     }
     if ($this->meta['mode'] !== 'r' && $this->meta['mode'] !== 'rb' && $this->isLocalTmpFile === false && $this->size > 0 && $this->unencryptedSize > 0) {
         // only write keyfiles if it was a new file
         if ($this->newFile === true) {
             // Disable encryption proxy to prevent recursive calls
             $proxyStatus = \OC_FileProxy::$enabled;
             \OC_FileProxy::$enabled = false;
             // Fetch user's public key
             $this->publicKey = Keymanager::getPublicKey($this->rootView, $this->keyId);
             // Check if OC sharing api is enabled
             $sharingEnabled = \OCP\Share::isEnabled();
             // Get all users sharing the file includes current user
             $uniqueUserIds = $this->util->getSharingUsersArray($sharingEnabled, $this->relPath);
             $checkedUserIds = $this->util->filterShareReadyUsers($uniqueUserIds);
             // Fetch public keys for all sharing users
             $publicKeys = Keymanager::getPublicKeys($this->rootView, $checkedUserIds['ready']);
             // Encrypt enc key for all sharing users
             $this->encKeyfiles = Crypt::multiKeyEncrypt($this->plainKey, $publicKeys);
             // Save the new encrypted file key
             Keymanager::setFileKey($this->rootView, $this->util, $this->relPath, $this->encKeyfiles['data']);
             // Save the sharekeys
             Keymanager::setShareKeys($this->rootView, $this->util, $this->relPath, $this->encKeyfiles['keys']);
             // Re-enable proxy - our work is done
             \OC_FileProxy::$enabled = $proxyStatus;
         }
         // we need to update the file info for the real file, not for the
         // part file.
         $path = Helper::stripPartialFileExtension($this->rawPath);
         $fileInfo = array('mimetype' => $this->rootView->getMimeType($this->rawPath), 'encrypted' => true, 'unencrypted_size' => $this->unencryptedSize);
         // if we write a part file we also store the unencrypted size for
         // the part file so that it can be re-used later
         $this->rootView->putFileInfo($this->rawPath, $fileInfo);
         if ($path !== $this->rawPath) {
             $this->rootView->putFileInfo($path, $fileInfo);
         }
     }
     $result = fclose($this->handle);
     if ($result === false) {
         \OCP\Util::writeLog('Encryption library', 'Could not close stream, file could be corrupted', \OCP\Util::FATAL);
     }
     return $result;
 }
Example #5
0
 /**
  * Get the mimetype of the requested file 
  * @return string
  */
 protected function getMimeType()
 {
     return $this->view->getMimeType($this->filepath);
 }