/** * @medium */ function testFopenFile() { $filename = '/tmp-' . uniqid(); $view = new \OC\Files\View('/' . $this->userId . '/files'); // Save short data as encrypted file using stream wrapper $cryptedFile = $view->file_put_contents($filename, $this->dataShort); // Test that data was successfully written $this->assertTrue(is_int($cryptedFile)); $handle = $view->fopen($filename, 'r'); // Get file decrypted contents $decrypt = fgets($handle); $this->assertEquals($this->dataShort, $decrypt); // tear down $view->unlink($filename); }
function testStreamSetWriteBuffer() { $filename = '/tmp-' . time(); $view = new \OC\Files\View('/' . $this->userId . '/files'); // Save short data as encrypted file using stream wrapper $cryptedFile = $view->file_put_contents($filename, $this->dataShort); // Test that data was successfully written $this->assertTrue(is_int($cryptedFile)); $handle = $view->fopen($filename, 'r'); // set stream options $this->assertEquals(0, stream_set_write_buffer($handle, 1024)); // tear down $view->unlink($filename); }
public function test() { // FIXME: Use as expression in empty once PHP 5.4 support is dropped $host = $this->getHost(); if (empty($host)) { \OC::$server->getLogger()->warning('Hostname has not been specified'); return false; } // FIXME: Use as expression in empty once PHP 5.4 support is dropped $user = $this->getUser(); if (empty($user)) { \OC::$server->getLogger()->warning('Username has not been specified'); return false; } if (!isset($this->privateKey)) { \OC::$server->getLogger()->warning('Private key was missing from the request'); return false; } // Sanity check the host $hostParts = explode(':', $this->getHost()); try { if (count($hostParts) == 1) { $hostname = $hostParts[0]; $this->assertHostAddressValid($hostname); } else { if (count($hostParts) == 2) { $hostname = $hostParts[0]; $this->assertHostAddressValid($hostname); $this->assertPortNumberValid($hostParts[1]); } else { throw new \Exception('Host connection string is invalid.'); } } } catch (\Exception $e) { \OC::$server->getLogger()->warning($e->getMessage()); return false; } // Validate the key $key = $this->getPrivateKey(); if (is_null($key)) { \OC::$server->getLogger()->warning('Secret key could not be loaded'); return false; } try { if ($this->getConnection()->nlist() === false) { return false; } } catch (\Exception $e) { // We should be throwing a more specific error, so we're not just catching // Exception here \OC::$server->getLogger()->warning($e->getMessage()); return false; } // Save the key somewhere it can easily be extracted later if (\OC::$server->getUserSession()->getUser()) { $view = new \OC\Files\View('/' . \OC::$server->getUserSession()->getUser()->getUId() . '/files_external/sftp_keys'); if (!$view->is_dir('')) { if (!$view->mkdir('')) { \OC::$server->getLogger()->warning('Could not create secret key directory.'); return false; } } $key_filename = $this->sanitizeUserName($this->getUser()) . '@' . $this->sanitizeHostName($hostname) . '.pub'; $key_file = $view->fopen($key_filename, "w"); if ($key_file) { fwrite($key_file, $this->publicKey); fclose($key_file); } else { \OC::$server->getLogger()->warning('Could not write secret key file.'); } } return true; }
public function testFopenWithDeleteOnlyPermission() { $this->view->file_put_contents($this->folder . '/existing.txt', 'foo'); $fileinfoFolder = $this->view->getFileInfo($this->folder); $result = \OCP\Share::shareItem('folder', $fileinfoFolder['fileid'], \OCP\Share::SHARE_TYPE_USER, self::TEST_FILES_SHARING_API_USER2, \OCP\Constants::PERMISSION_READ | \OCP\Constants::PERMISSION_DELETE); $this->assertTrue($result); self::loginHelper(self::TEST_FILES_SHARING_API_USER2); $user2View = new \OC\Files\View('/' . self::TEST_FILES_SHARING_API_USER2 . '/files'); // part file should be forbidden $handle = $user2View->fopen($this->folder . '/test.txt.part', 'w'); $this->assertFalse($handle); // regular file forbidden $handle = $user2View->fopen($this->folder . '/test.txt', 'w'); $this->assertFalse($handle); // rename forbidden $this->assertFalse($user2View->rename($this->folder . '/existing.txt', $this->folder . '/existing2.txt')); // delete allowed $this->assertTrue($user2View->unlink($this->folder . '/existing.txt')); //cleanup self::loginHelper(self::TEST_FILES_SHARING_API_USER1); $result = \OCP\Share::unshare('folder', $fileinfoFolder['fileid'], \OCP\Share::SHARE_TYPE_USER, self::TEST_FILES_SHARING_API_USER2); $this->assertTrue($result); }
/** * Generate thumbnail of an Image with ImageMagick * @param string $viewPath Source view path * @param string $srcImagePath Source image path relative to the ownCloud fakeroot * @param string $dstImagePath Destination image path * @return boolean TRUE image generated successfully, FALSE otherwise */ private function generateImageThumbnailIM($viewPath, $srcImagePath, $dstImagePath) { $view = new \OC\Files\View($viewPath); $handle = $view->fopen($srcImagePath, 'r'); // In case we was unable to open the file, forfait if ($handle === FALSE) { return FALSE; } try { $imageHandler = new \Imagick(); $imageHandler->readimagefile($handle); } catch (Exception $exc) { return FALSE; } // Get number of images and choose the best for the resolution of the thumbnail //$imgsNumber = $imageHandler->getnumberimages(); $imageInfo = $imageHandler->getImageProperties(); $orientation = \OCA\OCLife\ImageHandler::getRotation($imageInfo['exif:Orientation']); $imageHandler->setiteratorindex(0); $imageHandler->rotateImage($this->htmlBgColor, $orientation); // Compute aspect ratio $srcImgGeometry = $imageHandler->getImageGeometry(); $srcAspectRatio = $srcImgGeometry['width'] / $srcImgGeometry['height']; $thumb_aspect_ratio = $this->width / $this->height; if ($srcImgGeometry['width'] <= $this->width && $srcImgGeometry['height'] <= $this->height) { $thumbImageWidth = $srcImgGeometry['width']; $thumbImageHeight = $srcImgGeometry['height']; } elseif ($thumb_aspect_ratio > $srcAspectRatio) { $thumbImageWidth = intval($this->height * $srcAspectRatio); $thumbImageHeight = $this->height; } else { $thumbImageWidth = $this->width; $thumbImageHeight = intval($this->width / $srcAspectRatio); } // Create new image structure $verticalOffset = intval(($this->height - $thumbImageHeight) / 2); $horizontalOffset = intval(($this->width - $thumbImageWidth) / 2); // Copy and save generated thumbnail $imageHandler->thumbnailImage($thumbImageWidth, $thumbImageHeight); $imageHandler->borderimage($this->htmlBgColor, $horizontalOffset, $verticalOffset); $imageHandler->setImageFormat('png'); try { $imageHandler->writeimage($dstImagePath); } catch (Exception $ex) { return FALSE; } return TRUE; }