steffen: utils/admin generatefb.php,NONE,1.1

cvs at kolab.org cvs at kolab.org
Wed May 9 01:45:32 CEST 2007


Author: steffen

Update of /kolabrepository/utils/admin
In directory doto:/tmp/cvs-serv6609

Added Files:
	generatefb.php 
Log Message:
script to generate freebusy info for all calendar folders

--- NEW FILE: generatefb.php ---
#!/kolab/bin/php -c /kolab/etc/apache/php.ini
<?php
error_reporting(E_ALL);

$kolab_prefix = '/kolab';
$kolab_includepath = $kolab_prefix.'/var/kolab/php:'.$kolab_prefix.'/var/kolab/php/pear';
ini_set('include_path', $kolab_includepath.':'.ini_get('include_path'));

require_once($kolab_prefix.'/etc/resmgr/freebusy.conf');
if ( empty($params['ldap_classname_suffix'])) $params['ldap_classname_suffix'] = '';
require_once('freebusy/freebusyldap'.$params['ldap_classname_suffix'].'.class.php');
require_once('freebusy/freebusycache.class.php');
require_once('freebusy/freebusy.class.php');
require_once('Console/Getopt.php');

$imaphost = 'localhost';
$imapport = 143;
$imapoptions = '/notls';
$imapuser = 'manager';
$imappw = false;

/* Print out usage info */
function usage()
{
    global $imaphost,$imapport,$imapuser,$imappw;
    echo "Script for updating freebusy information on a kolab server.\n";
    echo "Normal usage is:\n";
    echo "\tsudo -u kolab-n ./generatefb.php [--help] [--imaphost=<imap hostname>] [--imapport=<imap port>] [--user=<manager user>] --password=<manager password>\n";
    echo "Defaults:\n";
    echo "\t--imaphost:\t$imaphost\n";
    echo "\t--imapport:\t$imapport\n";
    echo "\t--user:\t$imapuser\n";
    echo "\t--password:\t<no default>\n";
    exit(-1);
}

/* Parse command line options */
list($options,$args) = Console_Getopt::getopt(Console_Getopt::readPHPArgv(),'h',array('help','imaphost=','imapport=','user=','password='));
foreach( $options as $o ) {
    $a = $o[1];
    $o = $o[0];
    switch($o) {
	case 'h':
	case '--help':       usage(); break;
	case '--imaphost': $imaphost = $a; break;
	case '--imapport': $imapport = $a; break;
	case '--user':       $imapuser = $a; break;
	case '--password': $imappw   = $a; break;
    }
}

/* Password must be supplied */
if(!$imappw) usage();


function microtime_float() { return 0; }

function imap_foldername($name)
{
    if( $name[0] == '{' ) {
	if( ( $pos = strpos($name,'}') ) !== false ) {
	    $name = substr($name,$pos+1);
	}
    }
    return $name;
}
function imap_foldertype($imap,$name)
{
    $annot = imap_getannotation($imap,$name,'/vendor/kolab/folder-type','value.shared');
    if( is_array($annot) && array_key_exists('value.shared',$annot) ) return $annot['value.shared'];
    else return false;
}

$ldap =& new FreeBusyLDAP( $params['ldap_uri'], $params['base_dn'] );
if( !$ldap->bind( $params['bind_dn'], $params['bind_pw'] ) ) {
  notFound( "Bind failed: ".$ldap->error() );
  exit;
}

$imap_serverstring = "{".$imaphost.":".$imapport.$imapoptions."}";
$imap = imap_open( $imap_serverstring, $imapuser, $imappw );

$cache =& new FreeBusyCache( $params['cache_dir'], $params['pfb_dbformat'], false );
$xcache =& new FreeBusyCache( $params['cache_dir'], $params['pfb_dbformat'], true );

$list = imap_list($imap, $imap_serverstring, "*");
if (is_array($list)) {
    foreach ($list as $val) {
	/* Read foldername and type and skip
	 * anything that is not a calendar folder
	 */
	$foldername = imap_foldername($val);
	$folder = explode('/',$foldername);
	$type = imap_foldertype($imap,$foldername);
	if( $type != 'event' ) continue;

	/* We have to temporarily allow the manager
	 * to get the folder.
	 */
	$oldacl = false;
	$acl = imap_getacl($imap,$foldername);
	if( array_key_exists($imapuser,$acl) ) $oldacl = $acl[$imapuser];
	unset($acl);
	$rc = imap_setacl($imap,$foldername,$imapuser,'lrs');
	if(!$rc) {
	    echo "imap_setacl failed: " . imap_last_error() . "\n";
	}

	/* User handling stuff copied from pfb.php
	 * not sure if we need it, it shouldn't
	 * be used when the user is manager anyway
	 */
	$userinfo = $ldap->userInfo( $imapuser );
	$uinfo = $ldap->userInfo($folder[0]);
	$owner = $uinfo['MAIL'];
	if( empty($owner) || false===strpos($owner,'@')) {
	    // try guessing the domain
	    $idx = strpos( $imapuser, '@' );
	    if( $idx !== false ) {
		$domain = substr( $imapuser, $idx+1 );
		myLog("Trying to append $domain to ".$folder[0], RM_LOG_DEBUG);
		$uinfo = $ldap->userInfo($folder[0].'@'.$domain);
		$owner = $uinfo['MAIL'];
	    }
	}

	/* Deduce domain from foldername
	 * (the domain is encoded in the foldername
	 * when the user is domainless
	 */
	if( ($idx = strpos($foldername,'@')) !== false ) {
	    $domain = substr( $foldername, $idx+1 );
	}
	unset($folder[0]);
	$folder = join('/', $folder);
	$fbpast = $ldap->freeBusyPast();
	echo "Processing folder ".$folder."\n";
	$fb =& new FreeBusy( $params['cache_dir'], $owner, $imapuser, $imappw, 'localhost', $params['imap_options']
			     , $uinfo['FBFUTURE'], $fbpast );

	$fb->default_domain = $params['email_domain'];
	$rc = $fb->imapConnect();
	if( $rc === false ) {
	    echo "Unathorized\n";
	    unauthorized(imap_last_error());
	    return false;
	}
	$rc = $fb->imapOpenMailbox($foldername);
	if( $rc === false ) {
	    notfound( "Folder: ".$fb->foldername.', '.imap_last_error());
	    return false;
	}
	$relevance = $fb->getRelevance();
	list($vfb,$xvfb) = $fb->generateFreeBusy();
	$ts = mktime();
	if( PEAR::isError( $vfb ) ) {
	    unauthorized($vfb->toString());
	    return false;
	}

	if(ereg('(.*)@(.*)',$owner,$regs)) {
	    $owner = $regs[2].'/'.$regs[1];
	} else if( ($idx = strpos($folder,'@')) !== false ) {
	    $owner = $domain.$owner;
	    $folder = substr($folder,0,$idx);
	}

	$acl = $fb->getACL();
	if( !$cache->store( $owner.'/'.$folder, $vfb, $acl, $relevance ) ) {
	    trigger_error('Could not store pfb in cache file '.$owner.'/'.$folder
			  .'.pfb: '.$cache->error, E_USER_WARNING);
	}
	if( !$xcache->store( $owner.'/'.$folder, $xvfb, $acl, $relevance ) ) {
	    trigger_error('Could not store xpfb in cache file '.$owner.'/'.$folder
			  .'.xpfb: '.$cache->error, E_USER_WARNING);
	}

	/* Restore old ACL */
	$rc = imap_setacl($imap,$foldername,$imapuser,$oldacl);
	if(!$rc) {
	    echo "imap_setacl failed: " . imap_last_error() . "\n";
	}
    }
} else {
    echo "imap_getmailboxes failed: " . imap_last_error() . "\n";
}

imap_close($imap);
?>





More information about the commits mailing list