Page Menu
Home
Phorge
Search
Configure Global Search
Log In
Files
F117756769
No One
Temporary
Actions
View File
Edit File
Delete File
View Transforms
Subscribe
Flag For Later
Award Token
Authored By
Unknown
Size
33 KB
Referenced Files
None
Subscribers
None
View Options
diff --git a/plugins/kolab_auth/kolab_auth.php b/plugins/kolab_auth/kolab_auth.php
index 30fab834..d7e07bd3 100644
--- a/plugins/kolab_auth/kolab_auth.php
+++ b/plugins/kolab_auth/kolab_auth.php
@@ -1,403 +1,397 @@
<?php
/**
* Kolab Authentication (based on ldap_authentication plugin)
*
* Authenticates on LDAP server, finds canonized authentication ID for IMAP
* and for new users creates identity based on LDAP information.
*
* Supports impersonate feature (login as another user). To use this feature
* imap_auth_type/smtp_auth_type must be set to DIGEST-MD5 or PLAIN.
*
* @version 0.1
* @author Aleksander Machniak <machniak@kolabsys.com>
*
* Copyright (C) 2011, Kolab Systems AG
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 2
* as published by the Free Software Foundation.
*
* 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 General Public License for more details.
*
* You should have received a copy of the GNU General Public License along
* with this program; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/
class kolab_auth extends rcube_plugin
{
private $ldap;
private $data = array();
public function init()
{
$rcmail = rcmail::get_instance();
$this->add_hook('authenticate', array($this, 'authenticate'));
$this->add_hook('user_create', array($this, 'user_create'));
// Hooks related to "Login As" feature
$this->add_hook('template_object_loginform', array($this, 'login_form'));
$this->add_hook('imap_connect', array($this, 'imap_connect'));
$this->add_hook('managesieve_connect', array($this, 'imap_connect'));
$this->add_hook('smtp_connect', array($this, 'smtp_connect'));
$this->add_hook('write_log', array($this, 'write_log'));
if ($rcmail->config->get('kolab_auth_auditlog', false)) {
$rcmail->config->set('debug_level', 1);
$rcmail->config->set('devel_mode', true);
$rcmail->config->set('smtp_log', true);
$rcmail->config->set('log_logins', true);
$rcmail->config->set('log_session', true);
$rcmail->config->set('sql_debug', true);
$rcmail->config->set('memcache_debug', true);
$rcmail->config->set('imap_debug', true);
$rcmail->config->set('ldap_debug', true);
$rcmail->config->set('smtp_debug', true);
}
}
public function write_log($args) {
$rcmail = rcmail::get_instance();
if (!$rcmail->config->get('kolab_auth_auditlog', false)) {
return $args;
}
$args['abort'] = true;
if ($rcmail->config->get('log_driver') == 'syslog') {
$prio = $args['name'] == 'errors' ? LOG_ERR : LOG_INFO;
syslog($prio, $args['line']);
return $args;
} else {
$line = sprintf("[%s]: %s\n", $args['date'], $args['line']);
// log_driver == 'file' is assumed here
$log_dir = $rcmail->config->get('log_dir', INSTALL_PATH . 'logs');
// Append original username + target username
if (!is_dir($log_dir.'/'.strtolower($_SESSION['kolab_auth_admin']).'/'.strtolower($_SESSION['username']))) {
// Attempt to create the directory
if (@mkdir($log_dir.'/'.strtolower($_SESSION['kolab_auth_admin']).'/'.strtolower($_SESSION['username']), 0750, true)) {
$log_dir = $log_dir.'/'.strtolower($_SESSION['kolab_auth_admin']).'/'.strtolower($_SESSION['username']);
}
} else {
$log_dir = $log_dir.'/'.strtolower($_SESSION['kolab_auth_admin']).'/'.strtolower($_SESSION['username']);
}
// try to open specific log file for writing
$logfile = $log_dir.'/'.$args['name'];
if ($fp = fopen($logfile, 'a')) {
fwrite($fp, $line);
fflush($fp);
fclose($fp);
return $args;
}
else
trigger_error("Error writing to log file $logfile; Please check permissions", E_USER_WARNING);
}
return $args;
}
/**
* Sets defaults for new user.
*/
public function user_create($args)
{
if (!empty($this->data['user_email']))
$args['user_email'] = $this->data['user_email'];
if (!empty($this->data['user_name']))
$args['user_name'] = $this->data['user_name'];
if (!empty($this->data['user_alias']))
$args['user_alias'] = $this->data['user_alias'];
return $args;
}
/**
* Modifies login form adding additional "Login As" field
*/
public function login_form($args)
{
$this->load_config();
$this->add_texts('localization/');
$rcmail = rcmail::get_instance();
$admin_login = $rcmail->config->get('kolab_auth_admin_login');
$group = $rcmail->config->get('kolab_auth_group');
$role_attr = $rcmail->config->get('kolab_auth_role');
// Show "Login As" input
if (empty($admin_login) || (empty($group) && empty($role_attr))) {
return $args;
}
$input = new html_inputfield(array('name' => '_loginas', 'id' => 'rcmloginas',
'type' => 'text', 'autocomplete' => 'off'));
$row = html::tag('tr', null,
html::tag('td', 'title', html::label('rcmloginas', Q($this->gettext('loginas'))))
. html::tag('td', 'input', $input->show(trim(get_input_value('_loginas', RCUBE_INPUT_POST))))
);
$args['content'] = preg_replace('/<\/tbody>/i', $row . '</tbody>', $args['content']);
return $args;
}
/**
* Find user credentials In LDAP.
*/
public function authenticate($args)
{
$this->load_config();
if (!$this->init_ldap()) {
return $args;
}
$rcmail = rcmail::get_instance();
$admin_login = $rcmail->config->get('kolab_auth_admin_login');
+ $admin_pass = $rcmail->config->get('kolab_auth_admin_password');
$login_attr = $rcmail->config->get('kolab_auth_login');
$alias_attr = $rcmail->config->get('kolab_auth_alias');
$name_attr = $rcmail->config->get('kolab_auth_name');
// get username and host
$host = rcube_parse_host($args['host']);
$user = $args['user'];
$pass = $args['pass'];
$loginas = trim(get_input_value('_loginas', RCUBE_INPUT_POST));
if (empty($user) || empty($pass)) {
return $args;
}
// Find user record in LDAP
$record = $this->get_user_record($user, $host);
if (empty($record)) {
return $args;
}
// Login As...
if (!empty($loginas) && $admin_login) {
// Authenticate to LDAP
$dn = $this->ldap->dn_decode($record['ID']);
$result = $this->ldap->bind($dn, $pass);
if (!$result) {
return $args;
}
// check if the original user has/belongs to administrative role/group
$isadmin = false;
$group = $rcmail->config->get('kolab_auth_group');
$role_attr = $rcmail->config->get('kolab_auth_role');
$role_dn = $rcmail->config->get('kolab_auth_role_value');
// check role attribute
if (!empty($role_attr) && !empty($role_dn) && !empty($record[$role_attr])) {
$role_dn = $this->parse_vars($role_dn, $user, $host);
foreach ((array)$record[$role_attr] as $role) {
if ($role == $role_dn) {
$isadmin = true;
break;
}
}
}
// check group
if (!$isadmin && !empty($group)) {
$groups = $this->ldap->get_record_groups($record['ID']);
foreach ($groups as $g) {
if ($group == $this->ldap->dn_decode($g)) {
$isadmin = true;
break;
}
}
}
// Save original user login for log (see below)
if ($login_attr)
$origname = is_array($record[$login_attr]) ? $record[$login_attr][0] : $record[$login_attr];
else
$origname = $user;
$record = null;
// user has the privilage, get "login as" user credentials
if ($isadmin) {
$record = $this->get_user_record($loginas, $host);
}
if (empty($record)) {
$args['valid'] = false;
return $args;
}
$args['user'] = $loginas;
+
// Mark session to use SASL proxy for IMAP authentication
- $_SESSION['kolab_auth_admin'] = true;
+ $_SESSION['kolab_auth_admin'] = strtolower($origname);
+ $_SESSION['kolab_auth_login'] = $rcmail->encrypt($admin_login);
+ $_SESSION['kolab_auth_password'] = $rcmail->encrypt($admin_pass);
}
// Set credentials
if ($record) {
if ($login_attr)
$this->data['user_login'] = is_array($record[$login_attr]) ? $record[$login_attr][0] : $record[$login_attr];
if ($alias_attr)
$this->data['user_alias'] = is_array($record[$alias_attr]) ? $record[$alias_attr][0] : $record[$alias_attr];
if ($name_attr)
$this->data['user_name'] = is_array($record[$name_attr]) ? $record[$name_attr][0] : $record[$name_attr];
if ($this->data['user_login'])
$args['user'] = $this->data['user_login'];
}
// Log "Login As" usage
if (!empty($origname)) {
write_log('userlogins', sprintf('Admin login for %s by %s from %s',
$args['user'], $origname, rcmail_remote_ip()));
-
- // If available, additionally mark the session to come from the
- // original user. Useful for logging sessions of user A pretending
- // to be user B.
- $_SESSION['kolab_auth_admin'] = strtolower($origname);
-
}
return $args;
}
/**
* Sets SASL Proxy login/password for IMAP and Managesieve auth
*/
public function imap_connect($args)
{
if (!empty($_SESSION['kolab_auth_admin'])) {
- $this->load_config();
-
$rcmail = rcmail::get_instance();
- $admin_login = $rcmail->config->get('kolab_auth_admin_login');
- $admin_pass = $rcmail->config->get('kolab_auth_admin_password');
+ $admin_login = $rcmail->decrypt($_SESSION['kolab_auth_login']);
+ $admin_pass = $rcmail->decrypt($_SESSION['kolab_auth_password']);
$args['auth_cid'] = $admin_login;
$args['auth_pw'] = $admin_pass;
}
return $args;
}
/**
* Sets SASL Proxy login/password for SMTP auth
*/
public function smtp_connect($args)
{
if (!empty($_SESSION['kolab_auth_admin'])) {
- $this->load_config();
-
$rcmail = rcmail::get_instance();
- $admin_login = $rcmail->config->get('kolab_auth_admin_login');
- $admin_pass = $rcmail->config->get('kolab_auth_admin_password');
+ $admin_login = $rcmail->decrypt($_SESSION['kolab_auth_login']);
+ $admin_pass = $rcmail->decrypt($_SESSION['kolab_auth_password']);
$args['options']['smtp_auth_cid'] = $admin_login;
$args['options']['smtp_auth_pw'] = $admin_pass;
}
return $args;
}
/**
* Initializes LDAP object and connects to LDAP server
*/
private function init_ldap()
{
if ($this->ldap)
return $this->ldap->ready;
$rcmail = rcmail::get_instance();
$addressbook = $rcmail->config->get('kolab_auth_addressbook');
if (!is_array($addressbook)) {
$ldap_config = (array)$rcmail->config->get('ldap_public');
$addressbook = $ldap_config[$addressbook];
}
if (empty($addressbook)) {
return false;
}
$this->ldap = new kolab_auth_ldap_backend(
$addressbook,
$rcmail->config->get('ldap_debug'),
$rcmail->config->mail_domain($_SESSION['imap_host'])
);
return $this->ldap->ready;
}
/**
* Fetches user data from LDAP addressbook
*/
private function get_user_record($user, $host)
{
$rcmail = rcmail::get_instance();
$filter = $rcmail->config->get('kolab_auth_filter');
$filter = $this->parse_vars($filter, $user, $host);
// reset old result
$this->ldap->reset();
// get record
$this->ldap->set_filter($filter);
$results = $this->ldap->list_records();
if (count($results->records) == 1)
return $results->records[0];
}
/**
* Prepares filter query for LDAP search
*/
private function parse_vars($str, $user, $host)
{
$rcmail = rcmail::get_instance();
$domain = $rcmail->config->get('username_domain');
if (!empty($domain) && strpos($user, '@') === false) {
if (is_array($domain) && isset($domain[$host]))
$user .= '@'.rcube_parse_host($domain[$host], $host);
else if (is_string($domain))
$user .= '@'.rcube_parse_host($domain, $host);
}
// replace variables in filter
list($u, $d) = explode('@', $user);
$dc = 'dc='.strtr($d, array('.' => ',dc=')); // hierarchal domain string
$replaces = array('%dc' => $dc, '%d' => $d, '%fu' => $user, '%u' => $u);
return strtr($str, $replaces);
}
}
/**
* Wrapper class for rcube_ldap addressbook
*/
class kolab_auth_ldap_backend extends rcube_ldap
{
function set_filter($filter)
{
if ($filter)
$this->prop['filter'] = $filter;
}
}
diff --git a/plugins/kolab_core/rcube_kolab.php b/plugins/kolab_core/rcube_kolab.php
index c773a93d..72655b48 100644
--- a/plugins/kolab_core/rcube_kolab.php
+++ b/plugins/kolab_core/rcube_kolab.php
@@ -1,549 +1,561 @@
<?php
/**
* Copyright (C) 2011, Kolab Systems AG
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 2
* as published by the Free Software Foundation.
*
* @author Thomas Bruederli <roundcube@gmail.com>
* @author Aleksander Machniak <machniak@kolabsys.com>
*
*/
ini_set('error_reporting', E_ALL&~(E_DEPRECATED | E_NOTICE));
require_once 'Horde/Kolab/Storage/List.php';
require_once 'Horde/Kolab/Format.php';
require_once 'Horde/Auth.php';
require_once 'Horde/Auth/kolab.php';
require_once 'Horde/Perms.php';
/**
* Glue class to handle access to the Kolab data using the Kolab_* classes
* from the Horde project.
*/
class rcube_kolab
{
public static $last_error;
private static $horde_auth;
private static $config;
private static $ready = false;
private static $list;
private static $cache;
/**
* Setup the environment needed by the Kolab_* classes to access Kolab data
*/
public static function setup()
{
global $conf;
// setup already done
if (self::$horde_auth)
return;
$rcmail = rcmail::get_instance();
// get password of logged user
$pwd = $rcmail->decrypt($_SESSION['password']);
// load ldap credentials from local config
$conf['kolab'] = (array) $rcmail->config->get('kolab');
// Set global Horde config (e.g. Cache settings)
if (!empty($conf['kolab']['global'])) {
$conf = array_merge($conf, $conf['kolab']['global']);
unset($conf['kolab']['global']);
}
// Set Horde configuration (for cache db)
$dsnw = $rcmail->config->get('db_dsnw');
$dsnr = $rcmail->config->get('db_dsnr');
$conf['sql'] = MDB2::parseDSN($dsnw);
$conf['sql']['charset'] = 'utf-8';
if (!empty($dsnr) && $dsnr != $dsnw) {
$conf['sql']['read'] = MDB2::parseDSN($dsnr);
$conf['sql']['read']['charset'] = 'utf-8';
$conf['sql']['splitread'] = true;
}
// Re-set LDAP/IMAP host config
$ldap = array('server' => 'ldap://' . $_SESSION['imap_host'] . ':389');
$imap = array('server' => $_SESSION['imap_host'], 'port' => $_SESSION['imap_port']);
$freebusy = array('server' => 'https://' . $_SESSION['imap_host'] . '/freebusy');
$conf['kolab']['ldap'] = array_merge($ldap, (array)$conf['kolab']['ldap']);
$conf['kolab']['imap'] = array_merge($imap, (array)$conf['kolab']['imap']);
$conf['kolab']['freebusy'] = array_merge($freebusy, (array)$conf['kolab']['freebusy']);
self::$config = &$conf;
// pass the current IMAP authentication credentials to the Horde auth system
self::$horde_auth = Auth::singleton('kolab');
+ $username = $_SESSION['username'];
+ $credentials = array('password' => $pwd);
+
+ // Hack proxy auth for "Login As" feature of kolab_auth plugin
+ if (!empty($_SESSION['kolab_auth_admin'])) {
+ $username = $_SESSION['kolab_auth_admin'];
+ $conf['kolab']['imap']['user'] = $_SESSION['username'];
+ $conf['kolab']['imap']['authuser'] = $rcmail->decrypt($_SESSION['kolab_auth_login']);
+ $conf['kolab']['imap']['password'] = $rcmail->decrypt($_SESSION['kolab_auth_password']);
+ $conf['kolab']['user_mail'] = $_SESSION['username'];
+ }
+
if (self::$horde_auth->isAuthenticated()) {
self::$ready = true;
}
- else if (self::$horde_auth->authenticate($_SESSION['username'], array('password' => $pwd), false)) {
+ else if (self::$horde_auth->authenticate($username, $credentials, false)) {
// we could use Auth::setAuth() here, but it requires the whole bunch
// of includes and global objects, do it as simple as possible
$_SESSION['__auth'] = array(
'authenticated' => true,
'userId' => $_SESSION['username'],
'timestamp' => time(),
'remote_addr' => $_SERVER['REMOTE_ADDR'],
);
Auth::setCredential('password', $pwd);
self::$ready = true;
}
else {
raise_error(array(
'code' => 600, 'type' => 'php',
'file' => __FILE__, 'line' => __LINE__,
'message' => sprintf("Unable to authenticate user %s!", $_SESSION['username'])),
true, true);
}
// Register shutdown function for saving cache/session objects
$rcmail->add_shutdown_function(array('rcube_kolab', 'shutdown'));
NLS::setCharset('UTF-8');
String::setDefaultCharset('UTF-8');
}
/**
* Get instance of Kolab_List object
*
* @return object Kolab_List Folders list object
*/
public static function get_folders_list()
{
self::setup();
if (self::$list)
return self::$list;
if (!self::$ready)
return null;
$rcmail = rcmail::get_instance();
$imap_cache = $rcmail->config->get('imap_cache');
if ($imap_cache) {
self::$cache = $rcmail->get_cache('IMAP', $imap_cache);
self::$list = self::$cache->get('mailboxes.kolab');
// Disable Horde folders caching, we're using our own cache
self::$config['kolab']['imap']['cache_folders'] = false;
}
if (empty(self::$list)) {
self::$list = Kolab_List::singleton();
}
return self::$list;
}
/**
* Get instance of a Kolab (XML) format object
*
* @param string Data type (contact,event,task,note)
*
* @return object Horde_Kolab_Format_XML The format object
*/
public static function get_format($type)
{
self::setup();
return Horde_Kolab_Format::factory('XML', $type);
}
/**
* Get a list of storage folders for the given data type
*
* @param string Data type to list folders for (contact,event,task,note)
*
* @return array List of Kolab_Folder objects (folder names in UTF7-IMAP)
*/
public static function get_folders($type)
{
$kolab = self::get_folders_list();
return self::$ready ? $kolab->getByType($type) : array();
}
/**
* Getter for a specific storage folder
*
* @param string IMAP folder to access (UTF7-IMAP)
* @return object Kolab_Folder The folder object
*/
public static function get_folder($folder)
{
$kolab = self::get_folders_list();
return self::$ready ? $kolab->getFolder($folder) : null;
}
/**
* Checks if the given folder is subscribed.
* Much nicer would be if the Kolab_Folder object could tell this information
*
* @param string Full IMAP folder name
* @return boolean True if in the list of subscribed folders, False if not
*/
public static function is_subscribed($folder)
{
static $subscribed; // local cache
if (!$subscribed) {
$rcmail = rcmail::get_instance();
// try without connection first (list could be served from cache)
$subscribed = $rcmail->imap ? $rcmail->imap->list_mailboxes() : array();
// now really get the list from the IMAP server
if (empty($subscribed) || $subscribed == array('INBOX')) {
$rcmail->imap_connect();
$subscribed = $rcmail->imap->list_mailboxes();
}
}
return in_array($folder, $subscribed);
}
/**
* Get storage object for read/write access to the Kolab backend
*
* @param string IMAP folder to access (UTF7-IMAP)
* @param string Object type to deal with (leave empty for auto-detection using annotations)
*
* @return object Kolab_Data The data storage object
*/
public static function get_storage($folder, $data_type = null)
{
$kolab = self::get_folders_list();
return self::$ready ? $kolab->getFolder($folder)->getData($data_type) : null;
}
/**
* Compose an URL to query the free/busy status for the given user
*/
public static function get_freebusy_url($email)
{
return unslashify(self::$config['kolab']['freebusy']['server']) . '/' . $email . '.ifb';
}
/**
* Do session/cache operations on shutdown
*/
public static function shutdown()
{
if (self::$ready) {
// Horde's shutdown function doesn't work with Roundcube session
// save Horde_Kolab_Session object in session here
require_once 'Horde/SessionObjects.php';
$session = Horde_SessionObjects::singleton();
$kolab = Horde_Kolab_Session::singleton();
$session->overwrite('kolab_session', $kolab, false);
// Write Kolab_List object to cache
if (self::$cache && self::$list) {
self::$cache->set('mailboxes.kolab', self::$list);
}
}
}
/**
* Creates folder ID from folder name
*
* @param string $folder Folder name (UTF7-IMAP)
*
* @return string Folder ID string
*/
public static function folder_id($folder)
{
return asciiwords(strtr($folder, '/.', '--'));
}
/**
* Deletes IMAP folder
*
* @param string $name Folder name (UTF7-IMAP)
*
* @return bool True on success, false on failure
*/
public static function folder_delete($name)
{
$kolab = self::get_folders_list();
$folder = $kolab->getFolder($name);
$result = $folder->delete();
if (is_object($result) && is_a($result, 'PEAR_Error')) {
self::$last_error = $result->getMessage();
return false;
}
return true;
}
/**
* Creates IMAP folder
*
* @param string $name Folder name (UTF7-IMAP)
* @param string $type Folder type
* @param bool $default True if older is default (for specified type)
*
* @return bool True on success, false on failure
*/
public static function folder_create($name, $type=null, $default=false)
{
$kolab = self::get_folders_list();
$folder = new Kolab_Folder();
$folder->setList($kolab);
$folder->setFolder($name);
$result = $folder->save(array(
'type' => $type,
'default' => $default,
));
if (is_object($result) && is_a($result, 'PEAR_Error')) {
self::$last_error = $result->getMessage();
return false;
}
return true;
}
/**
* Renames IMAP folder
*
* @param string $oldname Old folder name (UTF7-IMAP)
* @param string $newname New folder name (UTF7-IMAP)
*
* @return bool True on success, false on failure
*/
public static function folder_rename($oldname, $newname)
{
$kolab = self::get_folders_list();
$folder = $kolab->getFolder($oldname);
$folder->setFolder($newname);
$result = $kolab->rename($folder);
if (is_object($result) && is_a($result, 'PEAR_Error')) {
self::$last_error = $result->getMessage();
return false;
}
// @TODO: Horde doesn't update subfolders cache nor subscriptions
// but we cannot use Roundcube imap object here, because
// when two connections are used in one request and we have
// multi-server configuration, updating the cache after all
// would get wrong information (e.g. annotations)
// Reset the List object and cache
$kolab = null;
if (self::$cache) {
self::$list = null;
self::$cache->remove('mailboxes', true);
}
return true;
}
/**
* Getter for human-readable name of Kolab object (folder)
* See http://wiki.kolab.org/UI-Concepts/Folder-Listing for reference
*
* @param string $folder IMAP folder name (UTF7-IMAP)
* @param string $folder_ns Will be set to namespace name of the folder
*
* @return string Name of the folder-object
*/
public static function object_name($folder, &$folder_ns=null)
{
$rcmail = rcmail::get_instance();
$rcmail->imap_init();
$namespace = $rcmail->imap->get_namespace();
$found = false;
if (!empty($namespace['shared'])) {
foreach ($namespace['shared'] as $ns) {
if (strlen($ns[0]) && strpos($folder, $ns[0]) === 0) {
$prefix = '';
$folder = substr($folder, strlen($ns[0]));
$delim = $ns[1];
$found = true;
$folder_ns = 'shared';
break;
}
}
}
if (!$found && !empty($namespace['other'])) {
foreach ($namespace['other'] as $ns) {
if (strlen($ns[0]) && strpos($folder, $ns[0]) === 0) {
// remove namespace prefix
$folder = substr($folder, strlen($ns[0]));
$delim = $ns[1];
// get username
$pos = strpos($folder, $delim);
if ($pos) {
$prefix = '('.substr($folder, 0, $pos).') ';
$folder = substr($folder, $pos+1);
}
else {
$prefix = '('.$folder.')';
$folder = '';
}
$found = true;
$folder_ns = 'other';
break;
}
}
}
if (!$found && !empty($namespace['personal'])) {
foreach ($namespace['personal'] as $ns) {
if (strlen($ns[0]) && strpos($folder, $ns[0]) === 0) {
// remove namespace prefix
$folder = substr($folder, strlen($ns[0]));
$prefix = '';
$delim = $ns[1];
$found = true;
break;
}
}
}
if (empty($delim))
$delim = $rcmail->imap->get_hierarchy_delimiter();
$folder = rcube_charset_convert($folder, 'UTF7-IMAP');
$folder = str_replace($delim, ' » ', $folder);
if ($prefix)
$folder = $prefix . ' ' . $folder;
if (!$folder_ns)
$folder_ns = 'personal';
return $folder;
}
/**
* Getter for the name of the namespace to which the IMAP folder belongs
*
* @param string $folder IMAP folder name (UTF7-IMAP)
*
* @return string Name of the namespace (personal, other, shared)
*/
public static function folder_namespace($folder)
{
$rcmail = rcmail::get_instance();
$rcmail->imap_init();
$namespace = $rcmail->imap->get_namespace();
if (!empty($namespace)) {
foreach ($namespace as $nsname => $nsvalue) {
if (in_array($nsname, array('personal', 'other', 'shared')) && !empty($nsvalue)) {
foreach ($nsvalue as $ns) {
if (strlen($ns[0]) && strpos($folder, $ns[0]) === 0) {
return $namespace = $nsname;
}
}
}
}
}
return 'personal';
}
/**
* Creates a SELECT field with folders list
*
* @param string $type Folder type
* @param array $attrs SELECT field attributes (e.g. name)
* @param string $current The name of current folder (to skip it)
*
* @return html_select SELECT object
*/
public static function folder_selector($type, $attrs, $current = '')
{
// get all folders of specified type
$folders = self::get_folders($type);
$delim = $_SESSION['imap_delimiter'];
$names = array();
$len = strlen($current);
if ($len && ($rpos = strrpos($current, $delim))) {
$parent = substr($current, 0, $rpos);
$p_len = strlen($parent);
}
// Filter folders list
foreach ($folders as $c_folder) {
$name = $c_folder->name;
// skip current folder and it's subfolders
if ($len && ($name == $current || strpos($name, $current.$delim) === 0)) {
continue;
}
// always show the parent of current folder
if ($p_len && $name == $parent) { }
// skip folders where user have no rights to create subfolders
else if ($c_folder->getOwner() != $_SESSION['username']) {
$rights = $c_folder->getMyRights();
if (PEAR::IsError($rights) || !preg_match('/[ck]/', $rights)) {
continue;
}
}
$names[$name] = rcube_charset_convert($name, 'UTF7-IMAP');
}
// Make sure parent folder is listed (might be skipped e.g. if it's namespace root)
if ($p_len && !isset($names[$parent])) {
$names[$parent] = rcube_charset_convert($parent, 'UTF7-IMAP');
}
// Sort folders list
asort($names, SORT_LOCALE_STRING);
$folders = array_keys($names);
$names = array();
// Build SELECT field of parent folder
$select = new html_select($attrs);
$select->add('---', '');
foreach ($folders as $name) {
$imap_name = $name;
$name = $origname = self::object_name($name);
// find folder prefix to truncate
for ($i = count($names)-1; $i >= 0; $i--) {
if (strpos($name, $names[$i].' » ') === 0) {
$length = strlen($names[$i].' » ');
$prefix = substr($name, 0, $length);
$count = count(explode(' » ', $prefix));
$name = str_repeat(' ', $count-1) . '» ' . substr($name, $length);
break;
}
}
$names[] = $origname;
$select->add($name, $imap_name);
}
return $select;
}
}
File Metadata
Details
Attached
Mime Type
text/x-diff
Expires
Sat, Apr 4, 8:51 AM (2 w, 5 d ago)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
18823340
Default Alt Text
(33 KB)
Attached To
Mode
rRPK roundcubemail-plugins-kolab
Attached
Detach File
Event Timeline