summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorRobin Appelman <icewind@owncloud.com>2013-07-02 17:45:34 +0200
committerRobin Appelman <icewind@owncloud.com>2013-07-25 00:31:51 +0200
commit10d3e63ce5f3923dc629ac3c16f3cd6324bf96ed (patch)
treed4af433d74fdaad9433677db6d9c46c16eccc8d1
parent94560c68ba8974e72098e525ff1d96f11a3ae2e0 (diff)
downloadnextcloud-server-10d3e63ce5f3923dc629ac3c16f3cd6324bf96ed.tar.gz
nextcloud-server-10d3e63ce5f3923dc629ac3c16f3cd6324bf96ed.zip
add quota streamwrapper that limits the amount of data that can be written to a stream
-rw-r--r--lib/base.php35
-rw-r--r--lib/files/stream/quota.php128
-rw-r--r--tests/lib/files/stream/quota.php78
3 files changed, 225 insertions, 16 deletions
diff --git a/lib/base.php b/lib/base.php
index eaee8424651..2613e88d053 100644
--- a/lib/base.php
+++ b/lib/base.php
@@ -91,7 +91,7 @@ class OC {
// ensure we can find OC_Config
set_include_path(
OC::$SERVERROOT . '/lib' . PATH_SEPARATOR .
- get_include_path()
+ get_include_path()
);
OC::$SUBURI = str_replace("\\", "/", substr(realpath($_SERVER["SCRIPT_FILENAME"]), strlen(OC::$SERVERROOT)));
@@ -160,11 +160,11 @@ class OC {
// set the right include path
set_include_path(
OC::$SERVERROOT . '/lib' . PATH_SEPARATOR .
- OC::$SERVERROOT . '/config' . PATH_SEPARATOR .
- OC::$THIRDPARTYROOT . '/3rdparty' . PATH_SEPARATOR .
- implode($paths, PATH_SEPARATOR) . PATH_SEPARATOR .
- get_include_path() . PATH_SEPARATOR .
- OC::$SERVERROOT
+ OC::$SERVERROOT . '/config' . PATH_SEPARATOR .
+ OC::$THIRDPARTYROOT . '/3rdparty' . PATH_SEPARATOR .
+ implode($paths, PATH_SEPARATOR) . PATH_SEPARATOR .
+ get_include_path() . PATH_SEPARATOR .
+ OC::$SERVERROOT
);
}
@@ -278,17 +278,17 @@ class OC {
ini_set('session.cookie_httponly', '1;');
// set the cookie path to the ownCloud directory
- $cookie_path = OC::$WEBROOT ?: '/';
+ $cookie_path = OC::$WEBROOT ? : '/';
ini_set('session.cookie_path', $cookie_path);
//set the session object to a dummy session so code relying on the session existing still works
self::$session = new \OC\Session\Memory('');
-
- try{
+
+ try {
// set the session name to the instance id - which is unique
self::$session = new \OC\Session\Internal(OC_Util::getInstanceId());
// if session cant be started break with http 500 error
- }catch (Exception $e){
+ } catch (Exception $e) {
OC_Log::write('core', 'Session could not be initialized',
OC_Log::ERROR);
@@ -352,7 +352,7 @@ class OC {
public static function init() {
// register autoloader
require_once __DIR__ . '/autoloader.php';
- self::$loader=new \OC\Autoloader();
+ self::$loader = new \OC\Autoloader();
self::$loader->registerPrefix('Doctrine\\Common', 'doctrine/common/lib');
self::$loader->registerPrefix('Doctrine\\DBAL', 'doctrine/dbal/lib');
self::$loader->registerPrefix('Symfony\\Component\\Routing', 'symfony/routing');
@@ -373,7 +373,7 @@ class OC {
ini_set('arg_separator.output', '&amp;');
// try to switch magic quotes off.
- if (get_magic_quotes_gpc()==1) {
+ if (get_magic_quotes_gpc() == 1) {
ini_set('magic_quotes_runtime', 0);
}
@@ -398,7 +398,8 @@ class OC {
//set http auth headers for apache+php-cgi work around
if (isset($_SERVER['HTTP_AUTHORIZATION'])
- && preg_match('/Basic\s+(.*)$/i', $_SERVER['HTTP_AUTHORIZATION'], $matches)) {
+ && preg_match('/Basic\s+(.*)$/i', $_SERVER['HTTP_AUTHORIZATION'], $matches)
+ ) {
list($name, $password) = explode(':', base64_decode($matches[1]), 2);
$_SERVER['PHP_AUTH_USER'] = strip_tags($name);
$_SERVER['PHP_AUTH_PW'] = strip_tags($password);
@@ -406,7 +407,8 @@ class OC {
//set http auth headers for apache+php-cgi work around if variable gets renamed by apache
if (isset($_SERVER['REDIRECT_HTTP_AUTHORIZATION'])
- && preg_match('/Basic\s+(.*)$/i', $_SERVER['REDIRECT_HTTP_AUTHORIZATION'], $matches)) {
+ && preg_match('/Basic\s+(.*)$/i', $_SERVER['REDIRECT_HTTP_AUTHORIZATION'], $matches)
+ ) {
list($name, $password) = explode(':', base64_decode($matches[1]), 2);
$_SERVER['PHP_AUTH_USER'] = strip_tags($name);
$_SERVER['PHP_AUTH_PW'] = strip_tags($password);
@@ -435,10 +437,11 @@ class OC {
stream_wrapper_register('fakedir', 'OC\Files\Stream\Dir');
stream_wrapper_register('static', 'OC\Files\Stream\StaticStream');
stream_wrapper_register('close', 'OC\Files\Stream\Close');
+ stream_wrapper_register('quota', 'OC\Files\Stream\Quota');
stream_wrapper_register('oc', 'OC\Files\Stream\OC');
self::initTemplateEngine();
- if ( !self::$CLI ) {
+ if (!self::$CLI) {
self::initSession();
} else {
self::$session = new \OC\Session\Memory('');
@@ -459,7 +462,7 @@ class OC {
// User and Groups
if (!OC_Config::getValue("installed", false)) {
- self::$session->set('user_id','');
+ self::$session->set('user_id', '');
}
OC_User::useBackend(new OC_User_Database());
diff --git a/lib/files/stream/quota.php b/lib/files/stream/quota.php
new file mode 100644
index 00000000000..53d8a03d30f
--- /dev/null
+++ b/lib/files/stream/quota.php
@@ -0,0 +1,128 @@
+<?php
+/**
+ * Copyright (c) 2013 Robin Appelman <icewind@owncloud.com>
+ * This file is licensed under the Affero General Public License version 3 or
+ * later.
+ * See the COPYING-README file.
+ */
+
+namespace OC\Files\Stream;
+
+/**
+ * stream wrapper limits the amount of data that can be written to a stream
+ *
+ * usage: void \OC\Files\Stream\Quota::register($id, $stream, $limit)
+ * or: resource \OC\Files\Stream\Quota::wrap($stream, $limit)
+ */
+class Quota {
+ private static $streams = array();
+
+ /**
+ * @var resource $source
+ */
+ private $source;
+
+ /**
+ * @var int $limit
+ */
+ private $limit;
+
+ /**
+ * @param string $id
+ * @param resource $stream
+ * @param int $limit
+ */
+ public static function register($id, $stream, $limit) {
+ self::$streams[$id] = array($stream, $limit);
+ }
+
+ /**
+ * remove all registered streams
+ */
+ public static function clear() {
+ self::$streams = array();
+ }
+
+ /**
+ * @param resource $stream
+ * @param int $limit
+ * @return resource
+ */
+ static public function wrap($stream, $limit) {
+ $id = uniqid();
+ self::register($id, $stream, $limit);
+ $meta = stream_get_meta_data($stream);
+ return fopen('quota://' . $id, $meta['mode']);
+ }
+
+ public function stream_open($path, $mode, $options, &$opened_path) {
+ $id = substr($path, strlen('quota://'));
+ if (isset(self::$streams[$id])) {
+ list($this->source, $this->limit) = self::$streams[$id];
+ return true;
+ } else {
+ return false;
+ }
+ }
+
+ public function stream_seek($offset, $whence = SEEK_SET) {
+ if ($whence === SEEK_SET) {
+ $this->limit += $this->stream_tell() - $offset;
+ } else {
+ $this->limit -= $offset;
+ }
+ fseek($this->source, $offset, $whence);
+ }
+
+ public function stream_tell() {
+ return ftell($this->source);
+ }
+
+ public function stream_read($count) {
+ $this->limit -= $count;
+ return fread($this->source, $count);
+ }
+
+ public function stream_write($data) {
+ $size = strlen($data);
+ if ($size > $this->limit) {
+ $data = substr($data, 0, $this->limit);
+ $size = $this->limit;
+ }
+ $this->limit -= $size;
+ return fwrite($this->source, $data);
+ }
+
+ public function stream_set_option($option, $arg1, $arg2) {
+ switch ($option) {
+ case STREAM_OPTION_BLOCKING:
+ stream_set_blocking($this->source, $arg1);
+ break;
+ case STREAM_OPTION_READ_TIMEOUT:
+ stream_set_timeout($this->source, $arg1, $arg2);
+ break;
+ case STREAM_OPTION_WRITE_BUFFER:
+ stream_set_write_buffer($this->source, $arg1, $arg2);
+ }
+ }
+
+ public function stream_stat() {
+ return fstat($this->source);
+ }
+
+ public function stream_lock($mode) {
+ flock($this->source, $mode);
+ }
+
+ public function stream_flush() {
+ return fflush($this->source);
+ }
+
+ public function stream_eof() {
+ return feof($this->source);
+ }
+
+ public function stream_close() {
+ fclose($this->source);
+ }
+}
diff --git a/tests/lib/files/stream/quota.php b/tests/lib/files/stream/quota.php
new file mode 100644
index 00000000000..22d3e93592c
--- /dev/null
+++ b/tests/lib/files/stream/quota.php
@@ -0,0 +1,78 @@
+<?php
+/**
+ * Copyright (c) 2013 Robin Appelman <icewind@owncloud.com>
+ * This file is licensed under the Affero General Public License version 3 or
+ * later.
+ * See the COPYING-README file.
+ */
+
+namespace Test\Files\Stream;
+
+class Quota extends \PHPUnit_Framework_TestCase {
+ public function tearDown() {
+ \OC\Files\Stream\Quota::clear();
+ }
+
+ protected function getStream($mode, $limit) {
+ $source = fopen('php://temp', $mode);
+ return \OC\Files\Stream\Quota::wrap($source, $limit);
+ }
+
+ public function testWriteEnoughSpace() {
+ $stream = $this->getStream('w+', 100);
+ $this->assertEquals(6, fwrite($stream, 'foobar'));
+ rewind($stream);
+ $this->assertEquals('foobar', fread($stream, 100));
+ }
+
+ public function testWriteNotEnoughSpace() {
+ $stream = $this->getStream('w+', 3);
+ $this->assertEquals(3, fwrite($stream, 'foobar'));
+ rewind($stream);
+ $this->assertEquals('foo', fread($stream, 100));
+ }
+
+ public function testWriteNotEnoughSpaceSecondTime() {
+ $stream = $this->getStream('w+', 9);
+ $this->assertEquals(6, fwrite($stream, 'foobar'));
+ $this->assertEquals(3, fwrite($stream, 'qwerty'));
+ rewind($stream);
+ $this->assertEquals('foobarqwe', fread($stream, 100));
+ }
+
+ public function testWriteEnoughSpaceRewind() {
+ $stream = $this->getStream('w+', 6);
+ $this->assertEquals(6, fwrite($stream, 'foobar'));
+ rewind($stream);
+ $this->assertEquals(3, fwrite($stream, 'qwe'));
+ rewind($stream);
+ $this->assertEquals('qwebar', fread($stream, 100));
+ }
+
+ public function testWriteNotEnoughSpaceRead() {
+ $stream = $this->getStream('w+', 6);
+ $this->assertEquals(6, fwrite($stream, 'foobar'));
+ rewind($stream);
+ $this->assertEquals('foobar', fread($stream, 6));
+ $this->assertEquals(0, fwrite($stream, 'qwe'));
+ }
+
+ public function testWriteNotEnoughSpaceExistingStream() {
+ $source = fopen('php://temp', 'w+');
+ fwrite($source, 'foobar');
+ $stream = \OC\Files\Stream\Quota::wrap($source, 3);
+ $this->assertEquals(3, fwrite($stream, 'foobar'));
+ rewind($stream);
+ $this->assertEquals('foobarfoo', fread($stream, 100));
+ }
+
+ public function testWriteNotEnoughSpaceExistingStreamRewind() {
+ $source = fopen('php://temp', 'w+');
+ fwrite($source, 'foobar');
+ $stream = \OC\Files\Stream\Quota::wrap($source, 3);
+ rewind($stream);
+ $this->assertEquals(6, fwrite($stream, 'qwerty'));
+ rewind($stream);
+ $this->assertEquals('qwerty', fread($stream, 100));
+ }
+}