summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAleksander Machniak <machniak@kolabsys.com>2014-10-21 07:43:23 (GMT)
committerAleksander Machniak <machniak@kolabsys.com>2014-10-21 07:43:23 (GMT)
commit2564dbfe54d2bb637fd8b6766d7d8f87557bb1eb (patch)
tree456ccf78bde6eafd5d3f15977f3b35f4befdce5b
parent101e2cea2c35a8690df75de12cadb4c587f03031 (diff)
downloadkolab-chwala-2564dbfe54d2bb637fd8b6766d7d8f87557bb1eb.tar.gz
Add helper classes for use of Chwala as a library (for iRony)
-rw-r--r--lib/api/common.php9
-rw-r--r--lib/api/file_create.php6
-rw-r--r--lib/api/file_delete.php4
-rw-r--r--lib/api/file_get.php12
-rw-r--r--lib/api/file_info.php4
-rw-r--r--lib/api/file_list.php4
-rw-r--r--lib/api/file_move.php16
-rw-r--r--lib/api/file_upload.php6
-rw-r--r--lib/api/folder_auth.php6
-rw-r--r--lib/api/folder_create.php6
-rw-r--r--lib/api/folder_delete.php4
-rw-r--r--lib/api/folder_list.php6
-rw-r--r--lib/api/folder_move.php16
-rw-r--r--lib/api/folder_types.php2
-rw-r--r--lib/api/lock_create.php4
-rw-r--r--lib/api/lock_delete.php4
-rw-r--r--lib/api/lock_list.php2
-rw-r--r--lib/api/quota.php2
-rw-r--r--lib/drivers/kolab/kolab_file_storage.php21
-rw-r--r--lib/file_api.php294
-rw-r--r--lib/file_api_core.php316
-rw-r--r--lib/file_api_lib.php178
-rw-r--r--lib/file_locale.php8
23 files changed, 562 insertions, 368 deletions
diff --git a/lib/api/common.php b/lib/api/common.php
index 6cdb6cf..4d5b3f6 100644
--- a/lib/api/common.php
+++ b/lib/api/common.php
@@ -25,6 +25,7 @@
class file_api_common
{
protected $api;
+ protected $rc;
protected $args = array();
@@ -67,10 +68,10 @@ class file_api_common
$maxsize = ini_get('upload_max_filesize');
$maxsize = $this->show_bytes(parse_bytes($maxsize));
- throw new Exception("Maximum file size ($maxsize) exceeded", file_api::ERROR_CODE);
+ throw new Exception("Maximum file size ($maxsize) exceeded", file_api_core::ERROR_CODE);
}
- throw new Exception("File upload failed", file_api::ERROR_CODE);
+ throw new Exception("File upload failed", file_api_core::ERROR_CODE);
}
$files[] = array(
@@ -85,10 +86,10 @@ class file_api_common
// if filesize exceeds post_max_size then $_FILES array is empty,
if ($maxsize = ini_get('post_max_size')) {
$maxsize = $this->show_bytes(parse_bytes($maxsize));
- throw new Exception("Maximum file size ($maxsize) exceeded", file_api::ERROR_CODE);
+ throw new Exception("Maximum file size ($maxsize) exceeded", file_api_core::ERROR_CODE);
}
- throw new Exception("File upload failed", file_api::ERROR_CODE);
+ throw new Exception("File upload failed", file_api_core::ERROR_CODE);
}
return $files;
diff --git a/lib/api/file_create.php b/lib/api/file_create.php
index 210d7ec..fb7de85 100644
--- a/lib/api/file_create.php
+++ b/lib/api/file_create.php
@@ -22,8 +22,6 @@
+--------------------------------------------------------------------------+
*/
-require_once __DIR__ . "/common.php";
-
class file_api_file_create extends file_api_common
{
/**
@@ -34,11 +32,11 @@ class file_api_file_create extends file_api_common
parent::handle();
if (!isset($this->args['file']) || $this->args['file'] === '') {
- throw new Exception("Missing file name", file_api::ERROR_CODE);
+ throw new Exception("Missing file name", file_api_core::ERROR_CODE);
}
if (!isset($this->args['content'])) {
- throw new Exception("Missing file content", file_api::ERROR_CODE);
+ throw new Exception("Missing file content", file_api_core::ERROR_CODE);
}
$request = $this instanceof file_api_file_update ? 'file_update' : 'file_create';
diff --git a/lib/api/file_delete.php b/lib/api/file_delete.php
index 2458f42..06b2ea4 100644
--- a/lib/api/file_delete.php
+++ b/lib/api/file_delete.php
@@ -22,8 +22,6 @@
+--------------------------------------------------------------------------+
*/
-require_once __DIR__ . "/common.php";
-
class file_api_file_delete extends file_api_common
{
/**
@@ -34,7 +32,7 @@ class file_api_file_delete extends file_api_common
parent::handle();
if (empty($this->args['file'])) {
- throw new Exception("Missing file name", file_api::ERROR_CODE);
+ throw new Exception("Missing file name", file_api_core::ERROR_CODE);
}
foreach ((array) $this->args['file'] as $file) {
diff --git a/lib/api/file_get.php b/lib/api/file_get.php
index def9751..b7698a4 100644
--- a/lib/api/file_get.php
+++ b/lib/api/file_get.php
@@ -22,8 +22,6 @@
+--------------------------------------------------------------------------+
*/
-require_once __DIR__ . "/common.php";
-
class file_api_file_get extends file_api_common
{
/**
@@ -33,10 +31,10 @@ class file_api_file_get extends file_api_common
{
parent::handle();
- $this->api->output_type = file_api::OUTPUT_HTML;
+ $this->api->output_type = file_api_core::OUTPUT_HTML;
if (!isset($this->args['file']) || $this->args['file'] === '') {
- header("HTTP/1.0 ".file_api::ERROR_CODE." Missing file name");
+ header("HTTP/1.0 ".file_api_core::ERROR_CODE." Missing file name");
}
$params = array(
@@ -54,7 +52,7 @@ class file_api_file_get extends file_api_common
$this->driver->file_get($path, $params);
}
catch (Exception $e) {
- header("HTTP/1.0 " . file_api::ERROR_CODE . " " . $e->getMessage());
+ header("HTTP/1.0 " . file_api_core::ERROR_CODE . " " . $e->getMessage());
}
exit;
@@ -66,7 +64,7 @@ class file_api_file_get extends file_api_common
protected function file_view($file, $args, $params)
{
$viewer = $args['viewer'];
- $path = RCUBE_INSTALL_PATH . "lib/viewers/$viewer.php";
+ $path = __DIR__ . "/../viewers/$viewer.php";
$class = "file_viewer_$viewer";
if (!file_exists($path)) {
@@ -78,7 +76,7 @@ class file_api_file_get extends file_api_common
$info = $this->driver->file_info($file);
}
catch (Exception $e) {
- header("HTTP/1.0 " . file_api::ERROR_CODE . " " . $e->getMessage());
+ header("HTTP/1.0 " . file_api_core::ERROR_CODE . " " . $e->getMessage());
exit;
}
diff --git a/lib/api/file_info.php b/lib/api/file_info.php
index 41d2639..aa1bcc3 100644
--- a/lib/api/file_info.php
+++ b/lib/api/file_info.php
@@ -22,8 +22,6 @@
+--------------------------------------------------------------------------+
*/
-require_once __DIR__ . "/common.php";
-
class file_api_file_info extends file_api_common
{
/**
@@ -34,7 +32,7 @@ class file_api_file_info extends file_api_common
parent::handle();
if (!isset($this->args['file']) || $this->args['file'] === '') {
- throw new Exception("Missing file name", file_api::ERROR_CODE);
+ throw new Exception("Missing file name", file_api_core::ERROR_CODE);
}
list($driver, $path) = $this->api->get_driver($this->args['file']);
diff --git a/lib/api/file_list.php b/lib/api/file_list.php
index 994804e..c0d09e5 100644
--- a/lib/api/file_list.php
+++ b/lib/api/file_list.php
@@ -22,8 +22,6 @@
+--------------------------------------------------------------------------+
*/
-require_once __DIR__ . "/common.php";
-
class file_api_file_list extends file_api_common
{
/**
@@ -34,7 +32,7 @@ class file_api_file_list extends file_api_common
parent::handle();
if (!isset($this->args['folder']) || $this->args['folder'] === '') {
- throw new Exception("Missing folder name", file_api::ERROR_CODE);
+ throw new Exception("Missing folder name", file_api_core::ERROR_CODE);
}
$params = array(
diff --git a/lib/api/file_move.php b/lib/api/file_move.php
index fea8b07..6b89bc7 100644
--- a/lib/api/file_move.php
+++ b/lib/api/file_move.php
@@ -22,8 +22,6 @@
+--------------------------------------------------------------------------+
*/
-require_once __DIR__ . "/common.php";
-
class file_api_file_move extends file_api_common
{
/**
@@ -34,17 +32,17 @@ class file_api_file_move extends file_api_common
parent::handle();
if (!isset($this->args['file']) || $this->args['file'] === '') {
- throw new Exception("Missing file name", file_api::ERROR_CODE);
+ throw new Exception("Missing file name", file_api_core::ERROR_CODE);
}
if (is_array($this->args['file'])) {
if (empty($this->args['file'])) {
- throw new Exception("Missing file name", file_api::ERROR_CODE);
+ throw new Exception("Missing file name", file_api_core::ERROR_CODE);
}
}
else {
if (!isset($this->args['new']) || $this->args['new'] === '') {
- throw new Exception("Missing new file name", file_api::ERROR_CODE);
+ throw new Exception("Missing new file name", file_api_core::ERROR_CODE);
}
$this->args['file'] = array($this->args['file'] => $this->args['new']);
@@ -56,11 +54,11 @@ class file_api_file_move extends file_api_common
foreach ((array) $this->args['file'] as $file => $new_file) {
if ($new_file === '') {
- throw new Exception("Missing new file name", file_api::ERROR_CODE);
+ throw new Exception("Missing new file name", file_api_core::ERROR_CODE);
}
if ($new_file === $file) {
- throw new Exception("Old and new file name is the same", file_api::ERROR_CODE);
+ throw new Exception("Old and new file name is the same", file_api_core::ERROR_CODE);
}
list($driver, $path) = $this->api->get_driver($file);
@@ -128,14 +126,14 @@ class file_api_file_move extends file_api_common
{
// unable to put file on mount point
if (strpos($new_path, file_storage::SEPARATOR) === false) {
- throw new Exception("Unable to copy/move file into specified location", file_api::ERROR_CODE);
+ throw new Exception("Unable to copy/move file into specified location", file_api_core::ERROR_CODE);
}
// get the file from source location
$fp = fopen('php://temp', 'w+');
if (!$fp) {
- throw new Exception("Internal server error", file_api::ERROR_CODE);
+ throw new Exception("Internal server error", file_api_core::ERROR_CODE);
}
$driver->file_get($path, null, $fp);
diff --git a/lib/api/file_upload.php b/lib/api/file_upload.php
index 42ff127..ce0558e 100644
--- a/lib/api/file_upload.php
+++ b/lib/api/file_upload.php
@@ -22,8 +22,6 @@
+--------------------------------------------------------------------------+
*/
-require_once __DIR__ . "/common.php";
-
class file_api_file_upload extends file_api_common
{
/**
@@ -34,10 +32,10 @@ class file_api_file_upload extends file_api_common
parent::handle();
// for Opera upload frame response cannot be application/json
- $this->api->output_type = file_api::OUTPUT_HTML;
+ $this->api->output_type = file_api_core::OUTPUT_HTML;
if (!isset($this->args['folder']) || $this->args['folder'] === '') {
- throw new Exception("Missing folder name", file_api::ERROR_CODE);
+ throw new Exception("Missing folder name", file_api_core::ERROR_CODE);
}
$uploads = $this->upload();
diff --git a/lib/api/folder_auth.php b/lib/api/folder_auth.php
index f6b564c..feb8113 100644
--- a/lib/api/folder_auth.php
+++ b/lib/api/folder_auth.php
@@ -22,8 +22,6 @@
+--------------------------------------------------------------------------+
*/
-require_once __DIR__ . "/common.php";
-
class file_api_folder_auth extends file_api_common
{
/**
@@ -34,7 +32,7 @@ class file_api_folder_auth extends file_api_common
parent::handle();
if (!isset($this->args['folder']) || $this->args['folder'] === '') {
- throw new Exception("Missing folder name", file_api::ERROR_CODE);
+ throw new Exception("Missing folder name", file_api_core::ERROR_CODE);
}
$drivers = $this->api->get_drivers();
@@ -47,7 +45,7 @@ class file_api_folder_auth extends file_api_common
}
if (empty($driver)) {
- throw new Exception("Unknown folder", file_api::ERROR_CODE);
+ throw new Exception("Unknown folder", file_api_core::ERROR_CODE);
}
// check if authentication works
diff --git a/lib/api/folder_create.php b/lib/api/folder_create.php
index 69767ba..843bbe6 100644
--- a/lib/api/folder_create.php
+++ b/lib/api/folder_create.php
@@ -22,8 +22,6 @@
+--------------------------------------------------------------------------+
*/
-require_once __DIR__ . "/common.php";
-
class file_api_folder_create extends file_api_common
{
/**
@@ -34,7 +32,7 @@ class file_api_folder_create extends file_api_common
parent::handle();
if (!isset($this->args['folder']) || $this->args['folder'] === '') {
- throw new Exception("Missing folder name", file_api::ERROR_CODE);
+ throw new Exception("Missing folder name", file_api_core::ERROR_CODE);
}
// normal folder
@@ -46,7 +44,7 @@ class file_api_folder_create extends file_api_common
// external storage (mount point)
if (strpos($this->args['folder'], file_storage::SEPARATOR) !== false) {
- throw new Exception("Unable to mount external storage into a sub-folder", file_api::ERROR_CODE);
+ throw new Exception("Unable to mount external storage into a sub-folder", file_api_core::ERROR_CODE);
}
// check if driver is enabled
diff --git a/lib/api/folder_delete.php b/lib/api/folder_delete.php
index 694c0d5..4036746 100644
--- a/lib/api/folder_delete.php
+++ b/lib/api/folder_delete.php
@@ -22,8 +22,6 @@
+--------------------------------------------------------------------------+
*/
-require_once __DIR__ . "/common.php";
-
class file_api_folder_delete extends file_api_common
{
/**
@@ -34,7 +32,7 @@ class file_api_folder_delete extends file_api_common
parent::handle();
if (!isset($this->args['folder']) || $this->args['folder'] === '') {
- throw new Exception("Missing folder name", file_api::ERROR_CODE);
+ throw new Exception("Missing folder name", file_api_core::ERROR_CODE);
}
list($driver, $path) = $this->api->get_driver($this->args['folder']);
diff --git a/lib/api/folder_list.php b/lib/api/folder_list.php
index 3a9f101..b1bc895 100644
--- a/lib/api/folder_list.php
+++ b/lib/api/folder_list.php
@@ -22,8 +22,6 @@
+--------------------------------------------------------------------------+
*/
-require_once __DIR__ . "/common.php";
-
class file_api_folder_list extends file_api_common
{
/**
@@ -94,8 +92,8 @@ class file_api_folder_list extends file_api_common
*/
protected function sort_folder_comparator($str1, $str2)
{
- $path1 = explode(file_api::SEPARATOR, $str1);
- $path2 = explode(file_api::SEPARATOR, $str2);
+ $path1 = explode(file_storage::SEPARATOR, $str1);
+ $path2 = explode(file_storage::SEPARATOR, $str2);
foreach ($path1 as $idx => $folder1) {
$folder2 = $path2[$idx];
diff --git a/lib/api/folder_move.php b/lib/api/folder_move.php
index dad3036..22eced0 100644
--- a/lib/api/folder_move.php
+++ b/lib/api/folder_move.php
@@ -22,8 +22,6 @@
+--------------------------------------------------------------------------+
*/
-require_once __DIR__ . "/common.php";
-
class file_api_folder_move extends file_api_common
{
/**
@@ -34,11 +32,11 @@ class file_api_folder_move extends file_api_common
parent::handle();
if (!isset($this->args['folder']) || $this->args['folder'] === '') {
- throw new Exception("Missing folder name", file_api::ERROR_CODE);
+ throw new Exception("Missing folder name", file_api_core::ERROR_CODE);
}
if (!isset($this->args['new']) || $this->args['new'] === '') {
- throw new Exception("Missing destination folder name", file_api::ERROR_CODE);
+ throw new Exception("Missing destination folder name", file_api_core::ERROR_CODE);
}
if ($this->args['new'] === $this->args['folder']) {
@@ -55,14 +53,14 @@ class file_api_folder_move extends file_api_common
// @TODO
}
- throw new Exception("Unsupported operation", file_api::ERROR_CODE);
+ throw new Exception("Unsupported operation", file_api_core::ERROR_CODE);
}
// cross-driver move
if ($src_driver != $dst_driver) {
// destination folder is an existing mount point
if (!strlen($dst_path)) {
- throw new Exception("Destination folder already exists", file_api::ERROR_CODE);
+ throw new Exception("Destination folder already exists", file_api_core::ERROR_CODE);
}
return $this->folder_move_to_other_driver($src_driver, $src_path, $dst_driver, $dst_path);
@@ -81,7 +79,7 @@ class file_api_folder_move extends file_api_common
// first check if destination folder not exists
if (in_array($dst_path, $dst_folders)) {
- throw new Exception("Destination folder already exists", file_api::ERROR_CODE);
+ throw new Exception("Destination folder already exists", file_api_core::ERROR_CODE);
}
// now recursively create/delete folders and copy their content
@@ -121,14 +119,14 @@ class file_api_folder_move extends file_api_common
{
// unable to put file on mount point
if (strpos($dst_path, file_storage::SEPARATOR) === false) {
- throw new Exception("Unable to move file into specified location", file_api::ERROR_CODE);
+ throw new Exception("Unable to move file into specified location", file_api_core::ERROR_CODE);
}
// get the file from source location
$fp = fopen('php://temp', 'w+');
if (!$fp) {
- throw new Exception("Internal server error", file_api::ERROR_CODE);
+ throw new Exception("Internal server error", file_api_core::ERROR_CODE);
}
$src_driver->file_get($src_path, null, $fp);
diff --git a/lib/api/folder_types.php b/lib/api/folder_types.php
index fd10ea7..c57a0ac 100644
--- a/lib/api/folder_types.php
+++ b/lib/api/folder_types.php
@@ -22,8 +22,6 @@
+--------------------------------------------------------------------------+
*/
-require_once __DIR__ . "/common.php";
-
class file_api_folder_types extends file_api_common
{
/**
diff --git a/lib/api/lock_create.php b/lib/api/lock_create.php
index 01c9f79..263a786 100644
--- a/lib/api/lock_create.php
+++ b/lib/api/lock_create.php
@@ -22,8 +22,6 @@
+--------------------------------------------------------------------------+
*/
-require_once __DIR__ . "/common.php";
-
class file_api_lock_create extends file_api_common
{
/**
@@ -36,7 +34,7 @@ class file_api_lock_create extends file_api_common
// arguments: uri, owner, timeout, scope, depth, token
foreach (array('uri', 'token') as $arg) {
if (!isset($this->args[$arg]) || $this->args[$arg] === '') {
- throw new Exception("Missing lock $arg", file_api::ERROR_CODE);
+ throw new Exception("Missing lock $arg", file_api_core::ERROR_CODE);
}
}
diff --git a/lib/api/lock_delete.php b/lib/api/lock_delete.php
index ff90835..452350c 100644
--- a/lib/api/lock_delete.php
+++ b/lib/api/lock_delete.php
@@ -22,8 +22,6 @@
+--------------------------------------------------------------------------+
*/
-require_once __DIR__ . "/common.php";
-
class file_api_lock_delete extends file_api_common
{
/**
@@ -36,7 +34,7 @@ class file_api_lock_delete extends file_api_common
// arguments: uri, owner, timeout, scope, depth, token
foreach (array('uri', 'token') as $arg) {
if (!isset($this->args[$arg]) || $this->args[$arg] === '') {
- throw new Exception("Missing lock $arg", file_api::ERROR_CODE);
+ throw new Exception("Missing lock $arg", file_api_core::ERROR_CODE);
}
}
diff --git a/lib/api/lock_list.php b/lib/api/lock_list.php
index db0cfa5..83f609d 100644
--- a/lib/api/lock_list.php
+++ b/lib/api/lock_list.php
@@ -22,8 +22,6 @@
+--------------------------------------------------------------------------+
*/
-require_once __DIR__ . "/common.php";
-
class file_api_lock_list extends file_api_common
{
/**
diff --git a/lib/api/quota.php b/lib/api/quota.php
index 2bda6f2..ac7199a 100644
--- a/lib/api/quota.php
+++ b/lib/api/quota.php
@@ -22,8 +22,6 @@
+--------------------------------------------------------------------------+
*/
-require_once __DIR__ . "/common.php";
-
class file_api_quota extends file_api_common
{
/**
diff --git a/lib/drivers/kolab/kolab_file_storage.php b/lib/drivers/kolab/kolab_file_storage.php
index a8987e9..8e34a1d 100644
--- a/lib/drivers/kolab/kolab_file_storage.php
+++ b/lib/drivers/kolab/kolab_file_storage.php
@@ -37,7 +37,7 @@ class kolab_file_storage implements file_storage
/**
* @var array
*/
- protected $config;
+ protected $config = array();
/**
* @var string
@@ -108,8 +108,8 @@ class kolab_file_storage implements file_storage
public function auth_info()
{
return array(
- 'username' => $_SESSION['username'],
- 'password' => $this->rc->decrypt($_SESSION['password']),
+ 'username' => $this->config['username'] ?: $_SESSION['username'],
+ 'password' => $this->config['password'] ?: $this->rc->decrypt($_SESSION['password']),
);
}
@@ -267,7 +267,7 @@ class kolab_file_storage implements file_storage
*/
public function configure($config, $title = null)
{
- $this->config = $config;
+ $this->config = array_merge($this->config, $config);
// @TODO: this is currently not possible to have multiple sessions in Roundcube
}
@@ -326,6 +326,8 @@ class kolab_file_storage implements file_storage
if (!$status) {
throw new Exception("Driver create failed", file_storage::ERROR);
}
+
+ $this->driver_list = null;
}
/**
@@ -347,6 +349,7 @@ class kolab_file_storage implements file_storage
throw new Exception("Driver delete failed", file_storage::ERROR);
}
+ $this->driver_list = null;
return;
}
@@ -361,6 +364,12 @@ class kolab_file_storage implements file_storage
*/
public function driver_list()
{
+ // use internal cache, this is specifically for iRony
+ // which may call this code path many times in one request
+ if ($this->driver_list !== null) {
+ return $this->driver_list;
+ }
+
// get current relations state
$config = kolab_storage_config::get_instance();
$default = true;
@@ -375,7 +384,7 @@ class kolab_file_storage implements file_storage
$result[$driver['title']] = $driver;
}
- return $result;
+ return $this->driver_list = $result;
}
/**
@@ -400,6 +409,8 @@ class kolab_file_storage implements file_storage
if (!$status) {
throw new Exception("Driver update failed", file_storage::ERROR);
}
+
+ $this->driver_list = null;
}
/**
diff --git a/lib/file_api.php b/lib/file_api.php
index d245e2d..484093b 100644
--- a/lib/file_api.php
+++ b/lib/file_api.php
@@ -22,26 +22,13 @@
+--------------------------------------------------------------------------+
*/
-class file_api extends file_locale
+class file_api extends file_api_core
{
- const ERROR_CODE = 500;
- const ERROR_INVALID = 501;
-
- const OUTPUT_JSON = 'application/json';
- const OUTPUT_HTML = 'text/html';
-
public $session;
- public $output_type = self::OUTPUT_JSON;
- public $config = array(
- 'date_format' => 'Y-m-d H:i',
- 'language' => 'en_US',
- );
-
- private $app_name = 'Kolab File API';
- private $drivers = array();
+ public $output_type = file_api_core::OUTPUT_JSON;
+
private $conf;
private $browser;
- private $backend;
public function __construct()
@@ -226,7 +213,7 @@ class file_api extends file_locale
header('HTTP/1.1 401 Unauthorized');
exit;
*/
- throw new Exception("Invalid password or username", file_api::ERROR_CODE);
+ throw new Exception("Invalid password or username", file_api_core::ERROR_CODE);
}
return $username;
@@ -277,6 +264,7 @@ class file_api extends file_locale
$request = $aliases[$request] ?: $request;
+ require_once __DIR__ . "/api/common.php";
include_once __DIR__ . "/api/$request.php";
$class_name = "file_api_$request";
@@ -286,194 +274,7 @@ class file_api extends file_locale
}
}
- throw new Exception("Unknown method", self::ERROR_INVALID);
- }
-
- /**
- * Initialise authentication/configuration backend class
- *
- * @return file_storage Main storage driver
- */
- public function get_backend()
- {
- if ($this->backend) {
- return $this->backend;
- }
-
- $driver = $this->conf->get('fileapi_backend', 'kolab');
- $class = $driver . '_file_storage';
-
- $include_path = RCUBE_INSTALL_PATH . "/lib/drivers/$driver" . PATH_SEPARATOR;
- $include_path .= ini_get('include_path');
- set_include_path($include_path);
-
- $this->backend = new $class;
-
- // configure api
- $this->backend->configure($this->config);
-
- return $this->backend;
- }
-
- /**
- * Return supported/enabled external storage instances
- *
- * @param bool $as_objects Return drivers as objects not config data
- *
- * @return array List of storage drivers
- */
- public function get_drivers($as_objects = false)
- {
- $enabled = $this->conf->get('fileapi_drivers');
- $preconf = $this->conf->get('fileapi_sources');
- $result = array();
- $all = array();
-
- if (!empty($enabled)) {
- $backend = $this->get_backend();
- $drivers = $backend->driver_list();
-
- foreach ($drivers as $item) {
- $all[] = $item['title'];
-
- if ($item['enabled'] && in_array($item['driver'], (array) $enabled)) {
- $result[] = $as_objects ? $this->get_driver_object($item) : $item;
- }
- }
- }
-
- if (empty($result) && !empty($preconf)) {
- foreach ((array) $preconf as $title => $item) {
- if (!in_array($title, $all)) {
- $item['title'] = $title;
- $item['admin'] = true;
-
- $result[] = $as_objects ? $this->get_driver_object($item) : $item;
- }
- }
- }
-
- return $result;
- }
-
- /**
- * Return driver for specified file/folder path
- *
- * @param string $path Folder/file path
- *
- * @return array Storage driver object and modified path
- */
- public function get_driver($path)
- {
- $drivers = $this->get_drivers();
-
- foreach ($drivers as $item) {
- $prefix = $item['title'] . file_storage::SEPARATOR;
-
- if ($path == $item['title'] || strpos($path, $prefix) === 0) {
- $selected = $item;
- break;
- }
- }
-
- if (empty($selected)) {
- return array($this->get_backend(), $path);
- }
-
- $path = substr($path, strlen($selected['title']) + 1);
-
- return array($this->get_driver_object($selected), $path);
- }
-
- /**
- * Initialize driver instance
- *
- * @param array $config Driver config
- *
- * @return file_storage Storage driver instance
- */
- public function get_driver_object($config)
- {
- $key = $config['title'];
-
- if (empty($this->drivers[$key])) {
- $this->drivers[$key] = $driver = $this->load_driver_object($config['driver']);
-
- if ($config['username'] == '%u') {
- $backend = $this->get_backend();
- $auth_info = $backend->auth_info();
- $config['username'] = $auth_info['username'];
- $config['password'] = $auth_info['password'];
- }
- else if (!empty($config['password']) && empty($config['admin'])) {
- $config['password'] = $this->decrypt($config['password']);
- }
-
- // configure api
- $driver->configure(array_merge($config, $this->config), $key);
- }
-
- return $this->drivers[$key];
- }
-
- /**
- * Loads a driver
- */
- public function load_driver_object($name)
- {
- $class = $name . '_file_storage';
-
- if (!class_exists($class, false)) {
- $include_path = RCUBE_INSTALL_PATH . "/lib/drivers/$name" . PATH_SEPARATOR;
- $include_path .= ini_get('include_path');
- set_include_path($include_path);
- }
-
- return new $class;
- }
-
- /**
- * Returns storage(s) capabilities
- *
- * @return array Capabilities
- */
- public function capabilities()
- {
- $caps = array();
- $backend = $this->get_backend();
-
- // check support for upload progress
- if (($progress_sec = $this->conf->get('upload_progress'))
- && ini_get('apc.rfc1867') && function_exists('apc_fetch')
- ) {
- $caps[file_storage::CAPS_PROGRESS_NAME] = ini_get('apc.rfc1867_name');
- $caps[file_storage::CAPS_PROGRESS_TIME] = $progress_sec;
- }
-
- // get capabilities of main storage module
- foreach ($backend->capabilities() as $name => $value) {
- // skip disabled capabilities
- if ($value !== false) {
- $caps[$name] = $value;
- }
- }
-
- // get capabilities of other drivers
- $drivers = $this->get_drivers(true);
-
- foreach ($drivers as $driver) {
- if ($driver != $backend) {
- $title = $driver->title();
- foreach ($driver->capabilities() as $name => $value) {
- // skip disabled capabilities
- if ($value !== false) {
- $caps['roots'][$title][$name] = $value;
- }
- }
- }
- }
-
- return $caps;
+ throw new Exception("Unknown method", file_api_core::ERROR_INVALID);
}
/**
@@ -502,33 +303,7 @@ class file_api extends file_locale
return $status; // id, done, total, current, percent, start_time, eta, rate
}
- throw new Exception("Not supported", file_api::ERROR_CODE);
- }
-
- /**
- * Return mimetypes list supported by built-in viewers
- *
- * @return array List of mimetypes
- */
- protected function supported_mimetypes()
- {
- $mimetypes = array();
- $dir = RCUBE_INSTALL_PATH . 'lib/viewers';
-
- if ($handle = opendir($dir)) {
- while (false !== ($file = readdir($handle))) {
- if (preg_match('/^([a-z0-9_]+)\.php$/i', $file, $matches)) {
- include_once $dir . '/' . $file;
- $class = 'file_viewer_' . $matches[1];
- $viewer = new $class($this);
-
- $mimetypes = array_merge($mimetypes, $viewer->supported_mimetypes());
- }
- }
- closedir($handle);
- }
-
- return $mimetypes;
+ throw new Exception("Not supported", file_api_core::ERROR_CODE);
}
/**
@@ -602,7 +377,7 @@ class file_api extends file_locale
}
if (empty($response['code'])) {
- $response['code'] = file_api::ERROR_CODE;
+ $response['code'] = file_api_core::ERROR_CODE;
}
$this->output_send($response);
@@ -655,57 +430,4 @@ class file_api extends file_locale
return $str;
}
-
- /**
- * Encrypts data with current user password
- *
- * @param string $str A string to encrypt
- *
- * @return string Encrypted string (and base64-encoded)
- */
- public function encrypt($str)
- {
- $rcube = rcube::get_instance();
- $key = $this->get_crypto_key();
-
- return $rcube->encrypt($str, $key, true);
- }
-
- /**
- * Decrypts data encrypted with encrypt() method
- *
- * @param string $str Encrypted string (base64-encoded)
- *
- * @return string Decrypted string
- */
- public function decrypt($str)
- {
- $rcube = rcube::get_instance();
- $key = $this->get_crypto_key();
-
- return $rcube->decrypt($str, $key, true);
- }
-
- /**
- * Set encryption password
- */
- protected function get_crypto_key()
- {
- $key = 'chwala_crypto_key';
- $backend = $this->get_backend();
- $user = $backend->auth_info();
- $password = $user['password'] . $user['username'];
-
- // encryption password must be 24 characters, no less, no more
- if (($len = strlen($password)) > 24) {
- $password = substr($password, 0, 24);
- }
- else {
- $password = $password . substr($this->conf->get('des_key'), 0, 24 - $len);
- }
-
- $this->conf->set($key, $password);
-
- return $key;
- }
}
diff --git a/lib/file_api_core.php b/lib/file_api_core.php
new file mode 100644
index 0000000..27ccc94
--- /dev/null
+++ b/lib/file_api_core.php
@@ -0,0 +1,316 @@
+<?php
+/*
+ +--------------------------------------------------------------------------+
+ | This file is part of the Kolab File API |
+ | |
+ | Copyright (C) 2012-2014, Kolab Systems AG |
+ | |
+ | This program is free software: you can redistribute it and/or modify |
+ | it under the terms of the GNU Affero General Public License as published |
+ | by the Free Software Foundation, either version 3 of the License, or |
+ | (at your option) any later version. |
+ | |
+ | This program is distributed in the hope that it will be useful, |
+ | but WITHOUT ANY WARRANTY; without even the implied warranty of |
+ | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
+ | GNU Affero General Public License for more details. |
+ | |
+ | You should have received a copy of the GNU Affero General Public License |
+ | along with this program. If not, see <http://www.gnu.org/licenses/> |
+ +--------------------------------------------------------------------------+
+ | Author: Aleksander Machniak <machniak@kolabsys.com> |
+ +--------------------------------------------------------------------------+
+*/
+
+class file_api_core extends file_locale
+{
+ const API_VERSION = 2;
+
+ const ERROR_CODE = 500;
+ const ERROR_INVALID = 501;
+
+ const OUTPUT_JSON = 'application/json';
+ const OUTPUT_HTML = 'text/html';
+
+ public $config = array(
+ 'date_format' => 'Y-m-d H:i',
+ 'language' => 'en_US',
+ );
+
+ protected $app_name = 'Kolab File API';
+ protected $drivers = array();
+ protected $backend;
+
+ /**
+ * Returns API version
+ */
+ public function client_version()
+ {
+ return self::API_VERSION;
+ }
+
+ /**
+ * Initialise authentication/configuration backend class
+ *
+ * @return file_storage Main storage driver
+ */
+ public function get_backend()
+ {
+ if ($this->backend) {
+ return $this->backend;
+ }
+
+ $rcube = rcube::get_instance();
+ $driver = $rcube->config->get('fileapi_backend', 'kolab');
+
+ $this->backend = $this->load_driver_object($driver);
+
+ // configure api
+ $this->backend->configure($this->config);
+
+ return $this->backend;
+ }
+
+ /**
+ * Return supported/enabled external storage instances
+ *
+ * @param bool $as_objects Return drivers as objects not config data
+ *
+ * @return array List of storage drivers
+ */
+ public function get_drivers($as_objects = false)
+ {
+ $rcube = rcube::get_instance();
+ $enabled = $rcube->config->get('fileapi_drivers');
+ $preconf = $rcube->config->get('fileapi_sources');
+ $result = array();
+ $all = array();
+
+ if (!empty($enabled)) {
+ $backend = $this->get_backend();
+ $drivers = $backend->driver_list();
+
+ foreach ($drivers as $item) {
+ $all[] = $item['title'];
+
+ if ($item['enabled'] && in_array($item['driver'], (array) $enabled)) {
+ $result[] = $as_objects ? $this->get_driver_object($item) : $item;
+ }
+ }
+ }
+
+ if (empty($result) && !empty($preconf)) {
+ foreach ((array) $preconf as $title => $item) {
+ if (!in_array($title, $all)) {
+ $item['title'] = $title;
+ $item['admin'] = true;
+
+ $result[] = $as_objects ? $this->get_driver_object($item) : $item;
+ }
+ }
+ }
+
+ return $result;
+ }
+
+ /**
+ * Return driver for specified file/folder path
+ *
+ * @param string $path Folder/file path
+ *
+ * @return array Storage driver object and modified path
+ */
+ public function get_driver($path)
+ {
+ $drivers = $this->get_drivers();
+
+ foreach ($drivers as $item) {
+ $prefix = $item['title'] . file_storage::SEPARATOR;
+
+ if ($path == $item['title'] || strpos($path, $prefix) === 0) {
+ $selected = $item;
+ break;
+ }
+ }
+
+ if (empty($selected)) {
+ return array($this->get_backend(), $path);
+ }
+
+ $path = substr($path, strlen($selected['title']) + 1);
+
+ return array($this->get_driver_object($selected), $path);
+ }
+
+ /**
+ * Initialize driver instance
+ *
+ * @param array $config Driver config
+ *
+ * @return file_storage Storage driver instance
+ */
+ public function get_driver_object($config)
+ {
+ $key = $config['title'];
+
+ if (empty($this->drivers[$key])) {
+ $this->drivers[$key] = $driver = $this->load_driver_object($config['driver']);
+
+ if ($config['username'] == '%u') {
+ $backend = $this->get_backend();
+ $auth_info = $backend->auth_info();
+ $config['username'] = $auth_info['username'];
+ $config['password'] = $auth_info['password'];
+ }
+ else if (!empty($config['password']) && empty($config['admin']) && !empty($key)) {
+ $config['password'] = $this->decrypt($config['password']);
+ }
+
+ // configure api
+ $driver->configure(array_merge($config, $this->config), $key);
+ }
+
+ return $this->drivers[$key];
+ }
+
+ /**
+ * Loads a driver
+ */
+ public function load_driver_object($name)
+ {
+ $class = $name . '_file_storage';
+
+ if (!class_exists($class, false)) {
+ $include_path = __DIR__ . "/drivers/$name" . PATH_SEPARATOR;
+ $include_path .= ini_get('include_path');
+ set_include_path($include_path);
+ }
+
+ return new $class;
+ }
+
+ /**
+ * Returns storage(s) capabilities
+ *
+ * @return array Capabilities
+ */
+ public function capabilities()
+ {
+ $rcube = rcube::get_instance();
+ $backend = $this->get_backend();
+ $caps = array();
+
+ // check support for upload progress
+ if (($progress_sec = $rcube->config->get('upload_progress'))
+ && ini_get('apc.rfc1867') && function_exists('apc_fetch')
+ ) {
+ $caps[file_storage::CAPS_PROGRESS_NAME] = ini_get('apc.rfc1867_name');
+ $caps[file_storage::CAPS_PROGRESS_TIME] = $progress_sec;
+ }
+
+ // get capabilities of main storage module
+ foreach ($backend->capabilities() as $name => $value) {
+ // skip disabled capabilities
+ if ($value !== false) {
+ $caps[$name] = $value;
+ }
+ }
+
+ // get capabilities of other drivers
+ $drivers = $this->get_drivers(true);
+
+ foreach ($drivers as $driver) {
+ if ($driver != $backend) {
+ $title = $driver->title();
+ foreach ($driver->capabilities() as $name => $value) {
+ // skip disabled capabilities
+ if ($value !== false) {
+ $caps['roots'][$title][$name] = $value;
+ }
+ }
+ }
+ }
+
+ return $caps;
+ }
+
+ /**
+ * Return mimetypes list supported by built-in viewers
+ *
+ * @return array List of mimetypes
+ */
+ protected function supported_mimetypes()
+ {
+ $mimetypes = array();
+ $dir = __DIR__ . '/lib/viewers';
+
+ if ($handle = opendir($dir)) {
+ while (false !== ($file = readdir($handle))) {
+ if (preg_match('/^([a-z0-9_]+)\.php$/i', $file, $matches)) {
+ include_once $dir . '/' . $file;
+ $class = 'file_viewer_' . $matches[1];
+ $viewer = new $class($this);
+
+ $mimetypes = array_merge($mimetypes, $viewer->supported_mimetypes());
+ }
+ }
+ closedir($handle);
+ }
+
+ return $mimetypes;
+ }
+
+ /**
+ * Encrypts data with current user password
+ *
+ * @param string $str A string to encrypt
+ *
+ * @return string Encrypted string (and base64-encoded)
+ */
+ public function encrypt($str)
+ {
+ $rcube = rcube::get_instance();
+ $key = $this->get_crypto_key();
+
+ return $rcube->encrypt($str, $key, true);
+ }
+
+ /**
+ * Decrypts data encrypted with encrypt() method
+ *
+ * @param string $str Encrypted string (base64-encoded)
+ *
+ * @return string Decrypted string
+ */
+ public function decrypt($str)
+ {
+ $rcube = rcube::get_instance();
+ $key = $this->get_crypto_key();
+
+ return $rcube->decrypt($str, $key, true);
+ }
+
+ /**
+ * Set encryption password
+ */
+ protected function get_crypto_key()
+ {
+ $key = 'chwala_crypto_key';
+ $rcube = rcube::get_instance();
+ $backend = $this->get_backend();
+ $user = $backend->auth_info();
+ $password = $user['password'] . $user['username'];
+
+ // encryption password must be 24 characters, no less, no more
+ if (($len = strlen($password)) > 24) {
+ $password = substr($password, 0, 24);
+ }
+ else {
+ $password = $password . substr($rcube->config->get('des_key'), 0, 24 - $len);
+ }
+
+ $rcube->config->set($key, $password);
+
+ return $key;
+ }
+}
diff --git a/lib/file_api_lib.php b/lib/file_api_lib.php
new file mode 100644
index 0000000..18742c9
--- /dev/null
+++ b/lib/file_api_lib.php
@@ -0,0 +1,178 @@
+<?php
+/*
+ +--------------------------------------------------------------------------+
+ | This file is part of the Kolab File API |
+ | |
+ | Copyright (C) 2012-2014, Kolab Systems AG |
+ | |
+ | This program is free software: you can redistribute it and/or modify |
+ | it under the terms of the GNU Affero General Public License as published |
+ | by the Free Software Foundation, either version 3 of the License, or |
+ | (at your option) any later version. |
+ | |
+ | This program is distributed in the hope that it will be useful, |
+ | but WITHOUT ANY WARRANTY; without even the implied warranty of |
+ | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
+ | GNU Affero General Public License for more details. |
+ | |
+ | You should have received a copy of the GNU Affero General Public License |
+ | along with this program. If not, see <http://www.gnu.org/licenses/> |
+ +--------------------------------------------------------------------------+
+ | Author: Aleksander Machniak <machniak@kolabsys.com> |
+ +--------------------------------------------------------------------------+
+*/
+
+/**
+ * This class gives access to Chwala API as a library
+ */
+class file_api_lib extends file_api_core
+{
+ /**
+ * API methods handler
+ */
+ public function __call($name, $arguments)
+ {
+ $this->init();
+
+ switch ($name) {
+ case 'configure':
+ foreach (array_keys($this->config) as $name) {
+ if (isset($arguments[0][$name])) {
+ $this->config[$name] = $arguments[0][$name];
+ }
+ }
+ return $this->config;
+
+ case 'mimetypes':
+ return $this->supported_mimetypes();
+
+ case 'file_list':
+ $args = array(
+ 'folder' => $arguments[0],
+ );
+ break;
+
+ case 'file_create':
+ case 'file_update':
+ case 'file_delete':
+ case 'file_info':
+ $args = array(
+ 'file' => $arguments[0],
+ 'content' => $arguments[1]['content'],
+ );
+ break;
+
+ case 'file_copy':
+ case 'file_move':
+ $args = array(
+ 'file' => array($arguments[0] => $arguments[1]),
+ );
+ break;
+
+ case 'file_get':
+ // override default action, we need only to support
+ // writes to file handle
+ list($driver, $path) = $this->get_driver($arguments[0]);
+ $driver->file_get($path, $arguments[1], $arguments[2]);
+ return;
+
+ case 'folder_list':
+ // no arguments
+ $args = array();
+ break;
+
+ case 'folder_create':
+ case 'folder_delete':
+ $args = array(
+ 'folder' => $arguments[0],
+ );
+ break;
+
+ case 'folder_move':
+ $args = array(
+ 'folder' => $arguments[0],
+ 'new' => $arguments[1],
+ );
+ break;
+
+ case 'lock_create':
+ case 'lock_delete':
+ $args = $arguments[1];
+ $args['uri'] = $arguments[0];
+ break;
+
+ case 'lock_list':
+ $args = array(
+ 'uri' => $arguments[0],
+ 'child_locks' => $arguments[1],
+ );
+ break;
+
+ default:
+ throw new Exception("Invalid method name", \file_storage::ERROR_UNSUPPORTED);
+ }
+
+ require_once __DIR__ . "/api/$name.php";
+
+ $class = "file_api_$name";
+ $handler = new $class($this, $args);
+
+ return $handler->handle();
+ }
+
+ /**
+ * Configure environment (this is to be overriden by implementation class)
+ */
+ protected function init()
+ {
+ }
+}
+
+
+/**
+ * Common handler class, from which action handler classes inherit
+ */
+class file_api_common
+{
+ protected $api;
+ protected $rc;
+ protected $args;
+
+
+ public function __construct($api, $args)
+ {
+ $this->rc = rcube::get_instance();
+ $this->api = $api;
+ $this->args = $args;
+ }
+
+ /**
+ * Request handler
+ */
+ public function handle()
+ {
+ // disable script execution time limit, so we can handle big files
+ @set_time_limit(0);
+ }
+
+ /**
+ * Parse driver metadata information
+ */
+ protected function parse_metadata($metadata, $default = false)
+ {
+ if ($default) {
+ unset($metadata['form']);
+ $metadata['name'] .= ' (' . $this->api->translate('localstorage') . ')';
+ }
+
+ // localize form labels
+ foreach ($metadata['form'] as $key => $val) {
+ $label = $this->api->translate('form.' . $val);
+ if (strpos($label, 'form.') !== 0) {
+ $metadata['form'][$key] = $label;
+ }
+ }
+
+ return $metadata;
+ }
+}
diff --git a/lib/file_locale.php b/lib/file_locale.php
index d924a54..7db48a0 100644
--- a/lib/file_locale.php
+++ b/lib/file_locale.php
@@ -40,10 +40,10 @@ class file_locale
$language = 'en_US';
}
- @include RCUBE_INSTALL_PATH . "/lib/locale/en_US.php";
+ @include __DIR__ . "/locale/en_US.php";
if ($language != 'en_US') {
- @include RCUBE_INSTALL_PATH . "/lib/locale/$language.php";
+ @include __DIR__ . "/locale/$language.php";
}
setlocale(LC_ALL, $language . '.utf8', $language . 'UTF-8', 'en_US.utf8', 'en_US.UTF-8');
@@ -77,12 +77,12 @@ class file_locale
$lang = $lang[0];
$lang = str_replace('-', '_', $lang);
- if (file_exists(RCUBE_INSTALL_PATH . "/lib/locale/$lang.php")) {
+ if (file_exists(__DIR__ . "/locale/$lang.php")) {
return $lang;
}
if (isset($aliases[$lang]) && ($alias = $aliases[$lang])
- && file_exists(RCUBE_INSTALL_PATH . "/lib/locale/$alias.php")
+ && file_exists(__DIR__ . "/locale/$alias.php")
) {
return $alias;
}