public function stream_open($path, $mode, $options, &$opened_path) { $this->setup(); $path = substr($path, strlen('oc://')); $this->path = $path; $this->fileSource = self::$rootView->fopen($path, $mode); if (is_resource($this->fileSource)) { $this->meta = stream_get_meta_data($this->fileSource); } return is_resource($this->fileSource); }
/** * create the certificate bundle of all trusted certificated */ protected function createCertificateBundle() { $path = $this->getPathToCertificates(); $certs = $this->listCertificates(); $fh_certs = $this->view->fopen($path . '/rootcerts.crt', 'w'); foreach ($certs as $cert) { $file = $path . '/uploads/' . $cert->getName(); $data = $this->view->file_get_contents($file); if (strpos($data, 'BEGIN CERTIFICATE')) { fwrite($fh_certs, $data); fwrite($fh_certs, "\r\n"); } } fclose($fh_certs); }
/** * create the certificate bundle of all trusted certificated */ public function createCertificateBundle() { $path = $this->getPathToCertificates(); $certs = $this->listCertificates(); if (!$this->view->file_exists($path)) { $this->view->mkdir($path); } $fhCerts = $this->view->fopen($path . '/rootcerts.crt', 'w'); // Write user certificates foreach ($certs as $cert) { $file = $path . '/uploads/' . $cert->getName(); $data = $this->view->file_get_contents($file); if (strpos($data, 'BEGIN CERTIFICATE')) { fwrite($fhCerts, $data); fwrite($fhCerts, "\r\n"); } } // Append the default certificates $defaultCertificates = file_get_contents(\OC::$SERVERROOT . '/resources/config/ca-bundle.crt'); fwrite($fhCerts, $defaultCertificates); // Append the system certificate bundle $systemBundle = $this->getCertificateBundle(null); if ($this->view->file_exists($systemBundle)) { $systemCertificates = $this->view->file_get_contents($systemBundle); fwrite($fhCerts, $systemCertificates); } fclose($fhCerts); }
/** * @param string $path * @param int $maxX * @param int $maxY * @param boolean $scalingup * @param \OC\Files\View $fileview * @return bool|\OC_Image */ public function getThumbnail($path, $maxX, $maxY, $scalingup, $fileview) { $content = $fileview->fopen($path, 'r'); $content = stream_get_contents($content); //don't create previews of empty text files if (trim($content) === '') { return false; } $lines = preg_split("/\r\n|\n|\r/", $content); $fontSize = 5; //5px $lineSize = ceil($fontSize * 1.25); $image = imagecreate($maxX, $maxY); imagecolorallocate($image, 255, 255, 255); $textColor = imagecolorallocate($image, 0, 0, 0); foreach ($lines as $index => $line) { $index = $index + 1; $x = (int) 1; $y = (int) ($index * $lineSize) - $fontSize; imagestring($image, 1, $x, $y, $line, $textColor); if ($index * $lineSize >= $maxY) { break; } } $image = new \OC_Image($image); return $image->valid() ? $image : false; }
/** * Opens a file for reading * @throws \RuntimeException */ private function getFileHandle() { $fileHandle = $this->view->fopen($this->path, "r"); if ($fileHandle === false) { $this->logError('Can not open for reading.', $this->id, $this->path); throw new \RuntimeException(); } else { $this->logDebug('Scan started'); $this->fileHandle = $fileHandle; } }
/** * Encrypt all files in a directory * @param string $dirPath the directory whose files will be encrypted * @return bool * @note Encryption is recursive */ public function encryptAll($dirPath) { $result = true; $found = $this->findEncFiles($dirPath); // 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) && is_resource($plainHandle)) { // 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; } else { \OCP\Util::writeLog('files_encryption', 'initial encryption: could not encrypt ' . $rawPath, \OCP\Util::FATAL); $result = false; } } \OC_FileProxy::$enabled = true; if ($versionStatus) { \OC_App::enable('files_versions'); } $result = $result && $this->encryptVersions($encryptedFiles); return $result; }
/** * @param $path raw path relative to data/ * @param $mode * @param $options * @param $opened_path * @return bool */ public function stream_open($path, $mode, $options, &$opened_path) { // assume that the file already exist before we decide it finally in getKey() $this->newFile = false; if (!isset($this->rootView)) { $this->rootView = new \OC_FilesystemView('/'); } $this->session = new \OCA\Encryption\Session($this->rootView); $this->privateKey = $this->session->getPrivateKey(); // rawPath is relative to the data directory $this->rawPath = \OC\Files\Filesystem::normalizePath(str_replace('crypt://', '', $path)); $this->userId = Helper::getUser($this->rawPath); $util = new Util($this->rootView, $this->userId); // get the key ID which we want to use, can be the users key or the // public share key $this->keyId = $util->getKeyId(); // Strip identifier text from path, this gives us the path relative to data/<user>/files $this->relPath = Helper::stripUserFilesPath($this->rawPath); // if raw path doesn't point to a real file, check if it is a version or a file in the trash bin if ($this->relPath === false) { $this->relPath = Helper::getPathToRealFile($this->rawPath); } if ($this->relPath === false) { \OCP\Util::writeLog('Encryption library', 'failed to open file "' . $this->rawPath . '" expecting a path to "files", "files_versions" or "cache"', \OCP\Util::ERROR); return false; } // Disable fileproxies so we can get the file size and open the source file without recursive encryption $proxyStatus = \OC_FileProxy::$enabled; \OC_FileProxy::$enabled = false; if ($mode === 'w' or $mode === 'w+' or $mode === 'wb' or $mode === 'wb+') { // We're writing a new file so start write counter with 0 bytes $this->size = 0; $this->unencryptedSize = 0; } else { if ($this->privateKey === false) { // if private key is not valid redirect user to a error page \OCA\Encryption\Helper::redirectToErrorPage($this->session); } $this->size = $this->rootView->filesize($this->rawPath, $mode); } $this->handle = $this->rootView->fopen($this->rawPath, $mode); \OC_FileProxy::$enabled = $proxyStatus; if (!is_resource($this->handle)) { \OCP\Util::writeLog('Encryption library', 'failed to open file "' . $this->rawPath . '"', \OCP\Util::ERROR); } else { $this->meta = stream_get_meta_data($this->handle); } return is_resource($this->handle); }
/** * write one-time encryption passwords to a csv file * * @param array $passwords */ protected function writePasswordsToFile(array $passwords) { $fp = $this->rootView->fopen('oneTimeEncryptionPasswords.csv', 'w'); foreach ($passwords as $pwd) { fputcsv($fp, $pwd); } fclose($fp); $this->output->writeln("\n"); $this->output->writeln('A list of all newly created passwords was written to data/oneTimeEncryptionPasswords.csv'); $this->output->writeln(''); $this->output->writeln('Each of these users need to login to the web interface, go to the'); $this->output->writeln('personal settings section "ownCloud basic encryption module" and'); $this->output->writeln('update the private key password to match the login password again by'); $this->output->writeln('entering the one-time password into the "old log-in password" field'); $this->output->writeln('and their current login password'); }
private function readHeader() { if ($this->isLocalTmpFile) { $handle = fopen($this->localTmpFile, 'r'); } else { $handle = $this->rootView->fopen($this->rawPath, 'r'); } if (is_resource($handle)) { $data = fread($handle, Crypt::BLOCKSIZE); $header = Crypt::parseHeader($data); $this->cipher = Crypt::getCipher($header); // remeber that we found a header if (!empty($header)) { $this->containHeader = true; } fclose($handle); } }
public function show() { if ($this->useOriginal) { $fp = @$this->view->fopen($this->path, 'rb'); $mtime = $this->view->filemtime($this->path); $size = $this->view->filesize($this->path); $mime = $this->view->getMimetype($this->path); } else { $fp = @fopen($this->path, 'rb'); $mtime = filemtime($this->path); $size = filesize($this->path); $mime = \OC_Helper::getMimetype($this->path); } if ($fp) { \OCP\Response::enableCaching(); \OCP\Response::setLastModifiedHeader($mtime); header('Content-Length: ' . $size); header('Content-Type: ' . $mime); fpassthru($fp); } else { \OC_Response::setStatus(\OC_Response::STATUS_NOT_FOUND); } }
/** * @param string $path * @param int $maxX * @param int $maxY * @param boolean $scalingup * @param \OC\Files\View $fileview * @return bool|\OC_Image */ public function getThumbnail($path, $maxX, $maxY, $scalingup, $fileview) { $content = $fileview->fopen($path, 'r'); $content = stream_get_contents($content); //don't create previews of empty text files if (trim($content) === '') { return false; } $lines = preg_split("/\r\n|\n|\r/", $content); $fontSize = 5; //5px $lineSize = ceil($fontSize * 1.25); $image = imagecreate($maxX, $maxY); imagecolorallocate($image, 255, 255, 255); $textColor = imagecolorallocate($image, 0, 0, 0); $fontFile = __DIR__; $fontFile .= '/../../../core'; $fontFile .= '/fonts/OpenSans-Regular.ttf'; $canUseTTF = function_exists('imagettftext'); foreach ($lines as $index => $line) { $index = $index + 1; $x = (int) 1; $y = (int) ($index * $lineSize); if ($canUseTTF === true) { imagettftext($image, $fontSize, 0, $x, $y, $textColor, $fontFile, $line); } else { $y -= $fontSize; imagestring($image, 1, $x, $y, $line, $textColor); } if ($index * $lineSize >= $maxY) { break; } } $image = new \OC_Image($image); return $image->valid() ? $image : false; }
/** * @param $path * @param $mode * @param $options * @param $opened_path * @return bool */ public function stream_open($path, $mode, $options, &$opened_path) { if (!isset($this->rootView)) { $this->rootView = new \OC_FilesystemView('/'); } $this->session = new \OCA\Encryption\Session($this->rootView); $this->privateKey = $this->session->getPrivateKey($this->userId); $util = new Util($this->rootView, \OCP\USER::getUser()); $this->userId = $util->getUserId(); // Strip identifier text from path, this gives us the path relative to data/<user>/files $this->relPath = \OC\Files\Filesystem::normalizePath(str_replace('crypt://', '', $path)); // rawPath is relative to the data directory $this->rawPath = $util->getUserFilesDir() . $this->relPath; // Disable fileproxies so we can get the file size and open the source file without recursive encryption $proxyStatus = \OC_FileProxy::$enabled; \OC_FileProxy::$enabled = false; if ($mode === 'w' or $mode === 'w+' or $mode === 'wb' or $mode === 'wb+') { // We're writing a new file so start write counter with 0 bytes $this->size = 0; $this->unencryptedSize = 0; } else { if ($this->privateKey === false) { // if private key is not valid redirect user to a error page \OCA\Encryption\Helper::redirectToErrorPage(); } $this->size = $this->rootView->filesize($this->rawPath, $mode); } $this->handle = $this->rootView->fopen($this->rawPath, $mode); \OC_FileProxy::$enabled = $proxyStatus; if (!is_resource($this->handle)) { \OCP\Util::writeLog('Encryption library', 'failed to open file "' . $this->rawPath . '"', \OCP\Util::ERROR); } else { $this->meta = stream_get_meta_data($this->handle); } return is_resource($this->handle); }
public static function fopen($path, $mode) { return self::$defaultInstance->fopen($path, $mode); }
/** * Gets information about trashed notes * * @NoAdminRequired * @NoCSRFRequired * @CORS * * @return string */ public function getTrashedNotes() { $dir = $this->request->getParam("dir", ""); $customFileExtensions = $this->request->getParam("extensions", array()); if (!is_array($customFileExtensions)) { $customFileExtensions = array(); } $noteFileExtensions = array_merge(array("md", "txt"), $customFileExtensions); // remove leading "/" if (substr($dir, 0, 1) === "/") { $dir = substr($dir, 1); } // remove trailing "/" if (substr($dir, -1) === "/") { $dir = substr($dir, 0, -1); } $sortAttribute = $this->request->getParam("sort", "mtime"); $sortDirectionParam = $this->request->getParam("sortdirection", ""); $sortDirection = $sortDirectionParam !== "" ? $sortDirectionParam === 'desc' : true; $filesInfo = array(); // generate the file list try { $files = Helper::getTrashFiles("/", $this->user, $sortAttribute, $sortDirection); $filesInfo = Helper::formatFileInfos($files); } catch (Exception $e) { } // only return notes (with extension ".txt", ".md" and the custom extensions) in the $dir directory $resultFilesInfo = array(); foreach ($filesInfo as $fileInfo) { $pathParts = pathinfo($fileInfo["name"]); // if $fileInfo["extraData"] is not set we will have to show the note files from all folders in QOwnNotes $isInDir = isset($fileInfo["extraData"]) ? strpos($fileInfo["extraData"], $dir . "/" . $fileInfo["name"]) === 0 : true; $isNoteFile = in_array($pathParts["extension"], $noteFileExtensions); if ($isInDir && $isNoteFile) { $timestamp = (int) ($fileInfo["mtime"] / 1000); $fileName = '/files_trashbin/files/' . $fileInfo["name"] . ".d{$timestamp}"; $view = new \OC\Files\View('/' . $this->user); $data = ""; // load the file data $handle = $view->fopen($fileName, 'rb'); if ($handle) { $chunkSize = 8192; // 8 kB chunks while (!feof($handle)) { $data .= fread($handle, $chunkSize); } } $resultFilesInfo[] = ["noteName" => $pathParts["filename"], "fileName" => $fileInfo["name"], "timestamp" => $timestamp, "dateString" => $fileInfo["date"], "data" => $data]; } } $data = array(); $data['directory'] = $dir; $data['notes'] = $resultFilesInfo; return $data; }