* @author Robin Rainton
* @package plugins
* @subpackage ldapfolderinfo
* @version $Id: functions.inc.php,v 1.4 2007/08/23 13:54:56 avel Exp $
*/
include_once(SM_PATH . 'plugins/useracl/imap_acl.php');
include_once(SM_PATH . 'plugins/directory/include/functions.php');
/**
* Ask LDAP about folder information and save results in Session.
*
* @param array $boxes
* @return array
* @todo Use NAMESPACE
*/
function ldapfolderinfo_cache_ldap(&$boxes) {
global $ldap_base_dn, $data_dir, $username, $languages;
$filteritemsuser = array();
$filteritemsbb = array();
for($i=0; $i 1) {
$filterbb = "(&(ou=VirtUsersBB)(|";
for($i=0; $i 1) {
$filteruser = "(&(ou=VirtUsers)(|";
for($i=0; $i' .
_("No Folder Information Found") .
'
' ;
}
$info = ldap_get_entries($ldap, $search_result);
$lang_iso = getPref($data_dir, $username, 'language');
$lang = substr($lang_iso, 0, 2);
for ($i=0; $i<$info['count']; $i++) {
$folderinfo[$info[$i]['foldername'][0]]['mail'] = htmlspecialchars($info[$i]['mail'][0]);
if(isset($info[$i]['description;lang-'.$lang]) ) {
/* 1st shot: Localized description */
if(function_exists("mb_convert_encoding")) {
$folderinfo[$info[$i]['foldername'][0]]['description'] = htmlspecialchars(
mb_convert_encoding($info[$i]['description;lang-'.$lang][0], $languages[$lang_iso]['CHARSET'], "UTF-8") );
} elseif(function_exists("recode")) {
$folderinfo[$info[$i]['foldername'][0]]['description'] = htmlspecialchars(
recode("UTF-8..".$languages[$lang_iso]['CHARSET'] , $info[$i]['description;lang-'.$lang][0]));
} elseif (function_exists("iconv")) {
$folderinfo[$info[$i]['foldername'][0]]['description'] = htmlspecialchars(
iconv("UTF-8" , $languages[$lang_iso]['CHARSET'] , $info[$i]['description;lang-'.$lang][0]));
} else {
$folderinfo[$info[$i]['foldername'][0]]['description'] = htmlspecialchars(
$info[$i]['description'][0]);
}
} elseif(isset( $info[$i]['description'][0])) {
/* 2nd: Default description */
$folderinfo[$info[$i]['foldername'][0]]['description'] = htmlspecialchars($info[$i]['description'][0]);
} else {
/* 3rd: A default description for all shared folders. */
$folderinfo[$info[$i]['foldername'][0]]['description'] = _("Shared Folder");
}
}
sqsession_register($folderinfo, 'ldapfolderinfo');
return $folderinfo;
}
}
/**
* Fill the boxes structure, that is returned by function
* sqimap_mailbox_tree(), with information from ldap.
*
* This structure is also fed to the getBoxStructure()
* function of templates/util_left_main.php, in order to
* be fed to the template.
*
* @param array $boxes
* @return void
* @see sqimap_mailbox_tree()
* @see getBoxStructure()
*/
function ldapfolderinfo_fill_boxes_structure_do(&$boxes) {
if(empty($boxes) || !isset($_SESSION['ldapfolderinfo'])) {
return array();
}
$ldapfolderinfo = $_SESSION['ldapfolderinfo'];
if(!empty($boxes->mailboxname_full) && isset($ldapfolderinfo[$boxes->mailboxname_full])) {
$boxes->postaddress = $ldapfolderinfo[$boxes->mailboxname_full]['mail'];
/* FIXME - move this outta here */
global $imapConnection;
$folderperm = sqimap_myrights($imapConnection, $boxes->mailboxname_full);
if(strstr($folderperm, 'p')) {
$boxes->postenabled = true;
} else {
$boxes->postenabled = false;
}
$boxes->description = $ldapfolderinfo[$boxes->mailboxname_full]['description'];
}
for ($i = 0; $i mbxs); $i++) {
ldapfolderinfo_fill_boxes_structure_do($boxes->mbxs[$i]);
}
}
/**
* This function is called inside getBoxStructure() hook, and makes sure
* that these variables are available to the template author.
*
* @param array $args
* args[0] => $box (what we are filling in)
* args[1] => $boxes (what we are getting information from)
* @return array The modified args[0] (aka $box)
*
* @see sqimap_mailbox_tree()
* @see getBoxStructure()
*/
function ldapfolderinfo_template_getboxstructure_do(&$args) {
$args[0]['Description'] = !empty($args[1]->description) ? $args[1]->description : '';
$args[0]['PostAddress'] = !empty($args[1]->postaddress) ? $args[1]->postaddress : '';
$args[0]['PostEnabled'] = !empty($args[1]->postenabled) ? $args[1]->postenabled : '';
return($args[0]);
}
/**
* This function searches the tree from the node folder to the root folder to
* search for an attribute
*
* This attribute may be one of the : owner, mailDomain, MaxDepth, MaxChild
* This function provides a generic template for searching for any of the above
* attributes for any type of BB folder or VirtUser Folder
*
* @author Christos Soulios (soulbros@noc.uoa.gr)
* @param string $FolderName Name of Folder / Mailbox
* @param string $attribute_name attribute to search for. Examples:
* * description
* * owner (returns: dn)
* * administrator (returns: dn)
* * allowmaildomain (returns: maildomains)
* * imappartition
* * maxquotaroot
* * mailhost
* * allowaplyto
* * maxdepth
* * maxchild
*
* @return mixed String or array.
* @todo enable single-value return
*/
function ldapfolderinfo_get_folder_attribute($FolderName, $attribute_name) {
// Find first set of the sought attribute
ldapfolderinfo_get_folder_parents($FolderName, $parents);
global $ldap, $ldap_base_dn;
for($i=0;$i 1) {
for($j=0; $j<$entry[0][strtolower($attribute_name)]['count']; $j++) {
$ret[$j] = $entry[0][strtolower($attribute_name)][$j];
}
return $ret;
} else {
return '';
}
}
}
}
/**
* This function returns an array with the parents of the folder on the route
* towards the root
*
* @param string $FolderName Name of Folder / Mailbox
* @param array $parents array of results
* @return int
*
* @author Christos Soulios
* @author Alexandros Vellis
*
* @todo use $delimiter
* @todo use functions enabled for IMAP namespace
*/
function ldapfolderinfo_get_folder_parents($FolderName, &$parents) {
global $delimiter;
if(ereg("^user\.",$FolderName)) { // If folder is a VirtUser folder.
$tokens = explode(".", $FolderName);
array_shift($tokens);
$tokens[0] = "user.".$tokens[0];
} else {
$tokens = explode(".", $FolderName);
} // Now each $token cell contains each folder node in the folder tree
//Find first set of the sought attribute
$count = 0;
for($i=count($tokens) -1 ;$i>=0;$i--) {
$parents[$count++] =implode(".", $tokens);
unset($tokens[$i]); // Set search folder path one less than last one.
}
return 1;
}
/**
* Get folder size.
*
* @param object $imap_stream
* @param string $mailbox
* @return int
* @todo Add new way of getting folder size, via Annotations.
* @todo Add compatible way of getting folder sizes
*/
function sqimap_get_used ($imap_stream, $mailbox) {
return sqimap_avelused ($imap_stream, $mailbox);
}
/**
* Issue an AVELUSED command to get folder size.
*
* @param object $imap_stream
* @param string $mailbox
* @return int
*/
function sqimap_avelused ($imap_stream, $mailbox) {
fputs ($imap_stream, "a001 AVELUSED \"$mailbox\"\r\n");
$read_ary = sqimap_read_data ($imap_stream, 'a001', false, $result, $message);
for ($i = 0; $i < count($read_ary); $i++) {
if (ereg("AVELUSED", $read_ary[$i])) {
$size = ereg_replace("^.*[(](.*)[)].*$", "\\1", $read_ary[$i]);
return $size;
} else {
return false;
}
}
}
/**
* Get a more human-readable description of a user's folder
*
* @param $folder string
* @return string
*/
function ldapfolderinfo_userfolder_to_humanreadable($folder) {
global $base_uri, $plugins;
$delimiter = sqimap_get_delimiter();
$parts = explode($delimiter, $folder);
if($parts[0] != 'user') {
return $folder;
}
if(sizeof($parts) < 3) {
return $folder;
}
$user = $parts[1];
$folder_disp = '';
for($i=2;$i$ns) {
}
}
}
/**
* Check if given mailbox is a folder that belongs to a user.
* @param string $mbox
* @param array $return_parts In this array, the mailbox name will be splitted
* to its parts according to the IMAP delimiter.
* @return boolean
* @obsolete
*/
function is_user_folder_old($mbox, &$return_parts) {
global $delimiter;
$parts = explode($delimiter, $mbox, 3);
if($parts[0] == 'user') {
$return_parts['username'] = $parts[1];
$return_parts['foldername'] = $parts[2];
return true;
}
return false;
}
/**
* Check if given mailbox is a folder that does not belong to a user, but is a
* global shared folder.
*
* @param string $mbox
* @return boolean
*/
function is_shared_folder($mbox) {
global $delimiter;
sqgetGlobalVar('sqimap_namespace', $sqimap_namespace, SQ_SESSION);
if(isset($sqimap_namespace) && !empty($sqimap_namespace)) {
/* Namespace - enabled logic */
foreach($sqimap_namespace['shared'] as $no=>$ns) {
if($ns['prefix'] == '') {
/* In this case, we just want to check that we are NOT in
* some other namespace. */
if(!is_user_folder($mbox)) {
return true;
/* TODO: */
if(!is_personal_folder($mbox)) {
return true;
}
}
}
//print "debug: ". substr($mbox, 0, sizeof($ns['prefix'])) . " vs ". $ns['prefix'];
if(substr($mbox, 0, sizeof($ns['prefix'])) == $ns['prefix']) {
return true;
}
}
return false;
} else {
/* Old way to check, deprecated. Only applicable for Cyrus normal
* namespace */
$parts = explode($delimiter, $mbox, 2);
if($parts[0] != 'user' && strtolower($parts[0]) != 'inbox') {
return true;
}
return false;
}
}