diff --git a/lib/api/common.php b/lib/api/common.php index 5cb0896..858008e 100644 --- a/lib/api/common.php +++ b/lib/api/common.php @@ -1,272 +1,271 @@ | +--------------------------------------------------------------------------+ | Author: Aleksander Machniak | +--------------------------------------------------------------------------+ */ class file_api_common { protected $api; protected $rc; protected $args = array(); public function __construct($api, $args = array()) { $this->rc = rcube::get_instance(); $this->api = $api; $this->args = (array) $args; } /** * Request handler */ public function handle() { // GET arguments if (!empty($_GET)) { foreach (array_keys($_GET) as $key) { $this->args[$key] = &$_GET[$key]; } } // POST arguments (JSON) if ($_SERVER['REQUEST_METHOD'] == 'POST') { $post = file_get_contents('php://input'); $this->args += (array) json_decode($post, true); unset($post); } // disable script execution time limit, so we can handle big files @set_time_limit(360); } /** * File uploads handler */ protected function upload() { $files = array(); if (is_array($_FILES['file']['tmp_name'])) { foreach ($_FILES['file']['tmp_name'] as $i => $filepath) { if ($err = $_FILES['file']['error'][$i]) { if ($err == UPLOAD_ERR_INI_SIZE || $err == UPLOAD_ERR_FORM_SIZE) { $maxsize = ini_get('upload_max_filesize'); $maxsize = $this->show_bytes(parse_bytes($maxsize)); throw new Exception("Maximum file size ($maxsize) exceeded", file_api_core::ERROR_CODE); } throw new Exception("File upload failed", file_api_core::ERROR_CODE); } $files[] = array( 'path' => $filepath, 'name' => $_FILES['file']['name'][$i], 'size' => filesize($filepath), 'type' => rcube_mime::file_content_type($filepath, $_FILES['file']['name'][$i], $_FILES['file']['type']), ); } } else if ($_SERVER['REQUEST_METHOD'] == 'POST') { // 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_core::ERROR_CODE); } throw new Exception("File upload failed", file_api_core::ERROR_CODE); } return $files; } /** * Return built-in viewer opbject for specified mimetype * * @return object Viewer object */ protected function find_viewer($mimetype) { $dir = RCUBE_INSTALL_PATH . 'lib/viewers'; $files = array(); // First get viewers and sort by name to get priority if ($handle = opendir($dir)) { while (false !== ($file = readdir($handle))) { if (preg_match('/^([a-z0-9_]+)\.php$/i', $file, $matches)) { $files[$matches[1]] = $dir . '/' . $file; } } closedir($handle); } ksort($files); foreach ($files as $name => $file) { include_once $file; $class = 'file_viewer_' . $name; $viewer = new $class($this->api); if ($viewer->supports($mimetype)) { return $viewer; } } } /** * 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; } /** * Get folder rights */ protected function folder_rights($folder) { list($driver, $path) = $this->api->get_driver($folder); $rights = $driver->folder_rights($path); $result = array(); $map = array( file_storage::ACL_READ => 'read', file_storage::ACL_WRITE => 'write', ); foreach ($map as $key => $value) { if ($rights & $key) { $result[] = $value; } } return $result; } /** * Collect folder list request parameters */ protected function folder_list_params() { $params = array('type' => 0); if (!empty($this->args['unsubscribed']) && rcube_utils::get_boolean((string) $this->args['unsubscribed'])) { $params['type'] |= file_storage::FILTER_UNSUBSCRIBED; } if (!empty($this->args['writable']) && rcube_utils::get_boolean((string) $this->args['writable'])) { $params['type'] |= file_storage::FILTER_WRITABLE; } if (isset($this->args['search']) && strlen($this->args['search'])) { $params['search'] = $this->args['search']; } if (!empty($this->args['permissions']) && rcube_utils::get_boolean((string) $this->args['permissions'])) { $params['extended'] = true; $params['permissions'] = true; } if (!empty($this->args['level']) && ($level = intval($this->args['level']))) { if ($level < 0) { $level *= -1; $params['auto_level'] = true; } $params['level'] = $level; } return $params; } /** * Wrapper for folder_list() method on specified driver */ protected function folder_list($driver, $params, $prefix = null) { $caps = $driver->capabilities(); if ($params['type'] & file_storage::FILTER_UNSUBSCRIBED) { if (empty($caps[file_storage::CAPS_SUBSCRIPTIONS])) { return array(); } } // If the driver has fast way to get the whole folders hierarchy // we'll return all folders, despite the requested level, when requested if (!empty($params['auto_level']) & !empty($caps[file_storage::CAPS_FAST_FOLDER_LIST])) { unset($params['level']); } $folders = $driver->folder_list($params); - $plen = is_string($prefix) ? strlen($prefix) : 0; if (!empty($folders) && is_string($prefix) && strlen($prefix) > 1) { foreach ($folders as $idx => $folder) { if (is_array($folder)) { - $folder[$idx]['folder'] = $prefix . $folder['folder']; + $folders[$idx]['folder'] = $prefix . $folder['folder']; } else { - $folder[$idx] = $prefix . $folder; + $folders[$idx] = $prefix . $folder; } } } return $folders; } /** * Update document session on file/folder move */ protected function session_uri_update($from, $to, $is_folder = false) { // check Manticore/WOPI support. Note: we don't use config->get('fileapi_manticore') // here as it may be not properly set if backend driver wasn't initialized yet $capabilities = $this->api->capabilities(false); if (!empty($capabilities['WOPI'])) { $document = new file_wopi($this->api); } else if (!empty($capabilities['MANTICORE'])) { $document = new file_manticore($this->api); } if (!empty($document)) { $document->session_uri_update($from, $to, $is_folder); } } }