steffen: server/kolab-webadmin/kolab-webadmin/www/admin/user forward.php, NONE, 1.1 index.php, NONE, 1.1 user.php, NONE, 1.1 vacation.php, NONE, 1.1

cvs at intevation.de cvs at intevation.de
Mon May 17 17:24:29 CEST 2004


Author: steffen

Update of /kolabrepository/server/kolab-webadmin/kolab-webadmin/www/admin/user
In directory doto:/tmp/cvs-serv23429/kolab-webadmin/www/admin/user

Added Files:
	forward.php index.php user.php vacation.php 
Log Message:
new webinterface for the kolab server

--- NEW FILE: forward.php ---
<?php
require_once 'admin/include/mysmarty.php';
require_once 'admin/include/headers.php';
require_once 'admin/include/authenticate.php';

// Funny multiline string escaping in Sieve
function dotstuff( $str ) {
  return str_replace( "\n.", "\n..", $str );
}

function undotstuff( $str ) {
  return str_replace( "\n..", "\n.", $str );
}

$errors = array();
if( (@include_once 'admin/include/Sieve.php' ) === false ) {
  $errors[] = 'Net/Sieve.php is missing. Without that, vacation settings are not available';
  $errors[] = 'Suggest your system administrator to run "/kolab/bin/pear install http://pear.php.net/get/Net_Sieve" on the server';
}

/**** Authentication etc. ***/
$sidx = 'user';

require_once('admin/include/menu.php');

/**** Submenu for current page ***/
$menuitems[$sidx]['selected'] = 'selected';

/**** Sieve handling ***/
// this is the name KDE client stores - so we can also change the setting from KDE client
$scriptname = "kmail-forward.siv";
if( !$errors ) {
  $sieve =& new Net_Sieve( $auth->uid(), $auth->password() );

  
  // Update sieve script on server in case we have submit data
  if( $_REQUEST['submit'] ) {
	$address = trim($_REQUEST['address']);
	$keep = isset($_REQUEST['keep']);
	$active = isset($_REQUEST['active']);

	if( empty( $address ) ) {
	  $errors[] = 'Please enter an email address';
	} else {
	  $script = 
		"redirect \"".addslashes($address)."\";".($keep?" keep;":"");
	  if( PEAR::isError( $res = $sieve->installScript( $scriptname, $script, $active ) ) ) {
		$errors[] = $res->getMessage();
		$errors[] = 'Script was:';
		$errors[] = '<pre>'.htmlentities($script).'</pre>';
	  }
	  if( !$active && $sieve->getActive() === $scriptname ) {
		$sieve->setActive( '' );
	  }

	  if( !$errors ) {
		if( $active ) $messages[] = 'Forwarding to '.$address.' successfully activated';
		else $messages[] =  'Forwarding to '.$address.' successfully deactivated';
	  }
	}
  }

  // Fetch script data from server
  $scripts = $sieve->listScripts();
  if( in_array( $scriptname, $scripts ) ) {
	$script = $sieve->getScript( $scriptname );
	if( ereg('redirect "(.*)";', $script, $regs ) ) {
	  $address = $regs[1];
	}
	$keep = ereg('"; keep;', $script, $regs );
  } else {
	$address = '';
	$keep = true;
  }
  $active = ( $sieve->getActive() === $scriptname );  
}

/**** Insert into template and output ***/
$smarty = new MySmarty();
$smarty->assign( 'errors', $errors );
$smarty->assign( 'messages', $messages );
$smarty->assign( 'uid', $auth->uid() );
$smarty->assign( 'group', $auth->group() );
$smarty->assign( 'page_title', $menuitems[$sidx]['title'] );
$smarty->assign( 'menuitems', $menuitems );
$smarty->assign( 'submenuitems', 
				 array_key_exists('submenu', 
								  $menuitems[$sidx])?$menuitems[$sidx]['submenu']:array() );
$smarty->assign( 'active', $active );
$smarty->assign( 'keep', $keep );
$smarty->assign( 'address', $address );
$smarty->assign( 'maincontent', 'forward.tpl' );
$smarty->display('page.tpl');

/*
  Local variables:
  mode: php
  indent-tabs-mode: t
  tab-width: 4
  buffer-file-coding-system: utf-8
  End:
 */
?>

--- NEW FILE: index.php ---
<?php
require_once('admin/include/mysmarty.php');
require_once('admin/include/headers.php');
require_once('admin/include/authenticate.php');

$errors = array();

/**** Authentication etc. ***/
$sidx = 'user';

if( $auth->group() != 'maintainer' && $auth->group() != 'admin') {
   array_push($errors, "Error: You don't have Permissions to access this Menu");
}

require_once('admin/include/menu.php');

/**** Submenu for current page ***/
$menuitems[$sidx]['selected'] = 'selected';

/**** Extract data from LDAP ***/

// read selector for register display
if (isset($HTTP_GET_VARS['alphaselect'])) $alphaselect = $HTTP_GET_VARS['alphaselect'];
else $alphaselect = "[A-F]";
if (isset($HTTP_GET_VARS['page'])) $page = $HTTP_GET_VARS['page'];
else $page = "1";

// Get all entries & dynamically split the letters with growing entries
if( !$errors ) {
  if (isset($_SESSION['base_dn'])) $base_dn = $_SESSION['base_dn'];
  else $base_dn = 'k=kolab';

  $privmembers = array_merge( $ldap->groupMembers( $base_dn, 'admin' ),
							  $ldap->groupMembers( $base_dn, 'maintainer' ) );

  $filter = "(&(cn=*)(objectclass=inetOrgPerson)(uid=*)(mail=*)(sn=*))";
  $result = ldap_search($ldap->connection, $base_dn, $filter, array( 'uid', 'mail', 'sn', 'cn', 'deleteflag' ));

  if( $result ) {
	$count = ldap_count_entries($ldap->connection, $result);
	$title = "Manage Email User ($count Users)";
	// if there are more than 2000 entries, split in 26 categories for every letter,
	// or if more than 50, put in groups, or else just show all.
	if (false && $count > 2000) {
	  // ... TODO
	  $template = 'userlistalpha.tpl';
	} else if( false && $count > 50 ) {
	  // ... TODO
	  $template = 'userlistgroup.tpl';
	}  else {
	  $template = 'userlistall.tpl';
	  $starttime = getmicrotime();
	  ldap_sort($ldap->connection,$result,'sn');
	  $endtime = getmicrotime();
	  //print "sorting took ".($endtime-$starttime)."<br/>";
	  $entry = ldap_first_entry($ldap->connection, $result);
	  while( $entry ) {
		$attrs = ldap_get_attributes($ldap->connection, $entry);
		$dn = ldap_get_dn($ldap->connection,$entry);
		$deleted = array_key_exists('deleteflag',$attrs)?$attrs['deleteflag'][0]:"FALSE";
        $userid = $attrs['uid'][0];
        $mail = $attrs['mail'][0];
        $sn = $attrs['sn'][0];
        $cn = $attrs['cn'][0];
        $a = strlen($sn);
        $b = strlen($cn);
        $fn = substr($cn, 0, $b - $a);
		// skip admins and maintainers
		if( !array_key_exists( $dn, $privmembers ) ) {
		  $entries[] = array( 'dn' => $dn,
							  'sn' => $sn,
							  'fn' => $fn,
							  'mail' => $mail,
							  'deleted' => $deleted );
		}
		$entry = ldap_next_entry( $ldap->connection,$entry );
	  }
	}
  }
}

/**** Insert into template and output ***/
$smarty = new MySmarty();
$smarty->assign( 'errors', $errors );
$smarty->assign( 'uid', $auth->uid() );
$smarty->assign( 'group', $auth->group() );
$smarty->assign( 'page_title', $menuitems[$sidx]['title'] );
$smarty->assign( 'entries', $entries );
$smarty->assign( 'menuitems', $menuitems );
$smarty->assign( 'submenuitems', 
				 array_key_exists('submenu', 
								  $menuitems[$sidx])?$menuitems[$sidx]['submenu']:array() );
$smarty->assign( 'maincontent', $template );
$smarty->display('page.tpl');

/*
  Local variables:
  mode: php
  indent-tabs-mode: t
  tab-width: 4
  buffer-file-coding-system: utf-8
  End:
 */
?>

--- NEW FILE: user.php ---
<?php
require_once('admin/include/mysmarty.php');
require_once('admin/include/headers.php');
require_once('admin/include/authenticate.php');
require_once('admin/include/form.class.php');

/**** Functions ***/
function comment( $s ) {
  return $s;
}

function is_unique ($a, $b) {
  global $ldap;
  if (($result = $ldap->search( $_SESSION['base_dn'],"(".$a."=".$ldap->escape($b).")")) &&
      (ldap_count_entries($ldap->connection,$result) <= 0))
    return true;
  return false;
}

function domain_dn()
{
  /*
  global $ldap;
  global $errors;
  if ($dattrs = $ldap->read( 'k=kolab,'.$_SESSION['base_dn'])) {
    $domain = $dattrs['postfix-mydomain'][0];
    $dcs = array_reverse(explode('.', $domain));
    $domain_dn = $_SESSION['base_dn'];
    foreach ($dcs as $dc) $domain_dn = "dc=$dc,".$domain_dn;

  } else {
    array_push($errors, "LDAP Error: could not determin domain");
    $domain_dn = $_SESSION['base_dn'];
  }
  return $domain_dn;
  */
  return $_SESSION['base_dn'];
}

// Check that a uid is unique
function checkuniquemail( $form, $key, $value ) {
  debug("checkuniquemail( $form, $key, $value )");
  if( is_unique( 'uid', $value ) ) {
	return '';
  } else {
	return 'User with this email address already exists';
  }
}

// Check that password match
function checkpw( $form, $key, $value ) {
  global $action;
  if( $action == "firstsave" ) {
    if( $key == 'password_0' ) {
      if( $value == '' ) return 'Password is empty';
    } else if( $key == 'password_1' ) {
      if( $value != $_POST['password_0'] ) {
        return 'Passwords dont match';
      }
    }
  } else {
    if( $value != $_POST['password_0'] ) {
      return 'Passwords dont match';
    }
  }
  return '';
}
function fill_form_for_modify( &$form, $dn, &$ldap_object ) {
  if (is_array($ldap_object['sn'])) $lastname = $ldap_object['sn'][0];
  else $lastname = $ldap_object['sn'];
  if (is_array($ldap_object['cn'])) $cn = $ldap_object['cn'][0];
  else $cn = $ldap_object['cn'];
  if ($lastname) {
    $a = strlen($lastname);
    if ($cn) {
      $b = strlen($cn);
      $firstname = substr($cn, 0, $b - $a);
    }
  }
  if (is_array($ldap_object['mail'])) $mail_0 = $ldap_object['mail'][0];
  else $mail_0 = $ldap_object['mail'];
  $form->entries['firstname']['value'] = $firstname;
  $form->entries['lastname']['value'] = $lastname;
  $form->entries['password_0']['value'] = '';
  $form->entries['password_1']['value'] = '';
  $form->entries['mail_0']['value'] = $mail_0;
  $form->entries['mail_0']['attrs'] = 'readonly';

  $internaldn = 'cn=internal,'.domain_dn();
  debug("<br/>internaldn=$internaldn");
  debug("<br/>ldap_object['dn']=$dn");
  debug("<br/>substr=".substr( $dn, strlen($dn)-strlen( $internaldn ) ));
  if( substr( $dn, strlen($dn)-strlen( $internaldn ) ) === $internaldn ) {
	$form->entries['visible']['value'] = false;
  } else {
	$form->entries['visible']['value'] = true;
  }

  foreach( array( 'title', 'o', 'ou', 'street', 'postOfficeBox',
                  'postalCode', 'l', 'c', 'telephoneNumber',
                  'facsimileTelephoneNumber' ) as $attr ) {
    if (is_array($ldap_object[$attr])) $v = $ldap_object[$attr][0];
    else $v = $ldap_object[$attr];
    $form->entries[$attr.'_0']['value'] = $v;
  }
  if (is_array($ldap_object['alias'])) {
	$arr = $ldap_object['alias'];
	unset( $arr['count'] );
	$v = join("\n", $arr );
  }
  else $v = $ldap_object[$attr];
  $form->entries['alias']['value'] = $v;
  $form->entries['action']['value'] = 'save';
  if( isset( $form->entries['userquota'] ) ) {
    if (is_array($ldap_object['userquota'])) $userquota = $ldap_object['userquota'][0];
    else $userquota = $ldap_object['userquota'];
    if( $userquota > 0 ) {
      $form->entries['userquota']['value'] = $userquota;
    } else {
      $form->entries['userquota']['value'] = '';
    }
  }
}

/**** Authentication etc. ***/
$sidx = 'user';

require_once('admin/include/menu.php');
$menuitems[$sidx]['selected'] = 'selected';

/**** Logic ***/
$errors = array();
$messages = array();
$valid_actions = array('save','firstsave','modify','create','delete','kill');
$contenttemplate = 'formcontainer.tpl';

// Get request data
if (!empty($_REQUEST['action']) &&
    in_array($_REQUEST['action'],$valid_actions)) $action = trim(urldecode($_REQUEST['action']));
else array_push($errors, "Error: need valid action to proceed");
$dn="";
if (!empty($_REQUEST['dn'])) $dn = trim(urldecode($_REQUEST['dn']));

if( $auth->group() == 'user' ) {
	$dn = $auth->dn();
}

// Check auth
if (!$errors && $auth->group() != "maintainer" && $auth->group() != "admin" &&
    !($auth->group() == "user" && $dn == $auth->dn() )) {
  array_push($errors, "Error: You don't have the required Permissions");
}   

// Fill in data
if ($action == "create") {
  $comment_mail_0 = 'Required, non volatile' ;
  $comment_password = 'Required';
} else {
  $comment_mail_0 = 'non volatile' ;
  $comment_password = 'Leave blank to keep password unchanged';
}

$entries = array( 'firstname' => array( 'name' => 'First Name',
					'validation' => 'notempty',
					'comment' => 'Required' ),
		  'lastname' => array( 'name' => 'Last Name',
				       'validation' => 'notempty',
				       'comment' => 'Required' ),
		  'password_0' => array( 'name' => 'Password',
					 'type' => 'password',
					 'validation' => 'checkpw',
					 'comment' => $comment_password ),
		  'password_1' => array( 'name' => 'Verify Password',
					 'type' => 'password',
					 'validation' => 'checkpw',
					 'comment' => $comment_password ),
		  'mail_0' => array( 'name' => 'Primary Email Address',
				     'validation' => 'notempty',
				     'comment' => $comment_mail_0 ),
		  'title_0' => array( 'name' => 'Title' ) );
$entries['alias'] = array( 'name' => 'Email Aliases', 
						   'type' => 'textarea',
						   'comment' => 'One address per line' );

$entries['o_0'] = array( 'name' => 'Organisation' );
$entries['ou_0'] = array( 'name' => 'Organisational Unit' );
$entries['roomNumber_0'] = array( 'name' => 'Room Number' );
$entries['street_0'] = array( 'name' => 'Street Address' );
$entries['postOfficeBox_0'] = array( 'name' => 'Postbox' );
$entries['postalCode_0'] = array( 'name' => 'Postal Code' );
$entries['l_0'] = array( 'name' => 'City' );
$entries['c_0'] = array( 'name' => 'Country' );
$entries['telephoneNumber_0'] = array( 'name' => 'Telephone Number' );
$entries['facsimileTelephoneNumber_0'] = array( 'name' => 'Fax Number' );
$entries['visible'] = array( 'name' => 'Addressbook',
			     'type' => 'checkbox',
			     'value' => true,
			     'comment' => 'check here to make this users address <br> visible in the address book');
if( $auth->group() == "admin" || $auth->group() == "maintainer" ) {
  $entries['userquota'] = array( 'name' => 'User Quota in KB',
				 'comment' => 'Leave blank for unlimited' );
} else {
  $entries['alias']['attrs'] = 'readonly';
}
$entries['action'] = array( 'name' => 'action',
			    'type' => 'hidden' );

if( $dn ) {
  $ldap_object = $ldap->read( $dn );
  if( !$ldap_object ) {
    array_push($errors, "LDAP Error: No such dn: $dn: ".ldap_error($ldap->connection));
  }
}

$form =& new KolabForm( 'user', 'createuser.tpl', $entries );
/***************** Main action swicth **********************/
switch( $action ) {
 case 'firstsave':
   debug("adding checkuniquemail to validation");
   $form->entries['mail_0']['validation'] = 'checkuniquemail';   
 case 'save':
   if( $form->isSubmitted() ) {
     if( !$form->validate() ) {
       $form->setValues();
       $content = $form->outputForm();
     } else {
       $ldap_object = array();
       $ldap_object['objectClass'] = 'inetOrgPerson';
       $ldap_object['sn'] = trim($_POST['lastname']);
       $ldap_object['cn'] = trim($_POST['firstname']).' '.$ldap_object['sn'];
	   $ldap_object['givenName'] = trim($_POST['firstname']);
       if( !empty( $_POST['password_0'] ) ) {
		 $ldap_object['userPassword'] = '{sha}'.base64_encode( pack('H*', 
																	sha1( $_POST['password_0'])));
       }
       $ldap_object['mail'] = trim( strtolower( $_POST['mail_0'] ) );
       if( $action == 'firstsave' ) $ldap_object['uid'] = $ldap_object['mail'];
       foreach( array( 'title', 'o', 'ou', 'roomNumber', 'street', 'postOfficeBox',
		       'postalCode', 'l', 'c', 'telephoneNumber',
		       'facsimileTelephoneNumber' ) as $attr ) {
		 $count = 0;
		 $key = $attr."_0";
		 $args = array();
		 while (!empty($_POST[$key])) {
		   $args[$count] = trim($_POST[$key]);
		   $count++;
		   $key = $attr."_".$count;
		 }
		 if ($count > 0) $ldap_object[$attr] = $args;
		 elseif (!empty($_POST[$key])) $ldap_object[$attr] = $_POST[$key];  
       }
	   if( !empty( $_POST['alias'] ) ) {
		 $ldap_object['alias'] = array_unique( array_filter( array_map( 'trim', preg_split( '/\n/', $_POST['alias'] ) ), 'strlen') );
	   }

       if ($auth->group() == "maintainer" || $auth->group() == "admin") {
		 if (!empty($_POST['visible']) && $_POST['visible'] == "on") 
		   $visible = true;
		 else $visible = false;
		 if( isset( $_POST['userquota'] ) ) {
		   $ldap_object['userquota'] = $_POST['userquota'];
		   if( empty( $ldap_object['userquota'] ) ) {
			 $ldap_object['userquota'] = array();
		   }
		 }
		 if (!$visible) $dn_add = ",cn=internal";
       }   
       else $dn_add = "";
       $domain_dn = domain_dn();
	   
       if ($action == "save") {
		 if (!$errors) {
		   if (!empty($ldap_object['cn'])) $newdn = "cn=".$ldap_object['cn'].",".$domain_dn;
		   else $newdn = $dn;
		   if (!$visible && !strstr($newdn,$dn_add)) {
			 list($cn,$rest) = split(',', $newdn, 2); 
			 $newdn = $cn.$dn_add.",".$rest;
		   } 
		   if (strcmp($dn,$newdn) != 0) {
			 if (($result=ldap_read($ldap->connection,$dn,"(objectclass=*)")) &&
				 ($entry=ldap_first_entry($ldap->connection,$result)) &&
				 ($oldattrs=ldap_get_attributes($ldap->connection,$entry))) {
			   $ldap_object['uid'] = $oldattrs['uid'][0];
			   $ldap_object['mail'] = $oldattrs['mail'][0];
			   if( !$ldap_object['userPassword'] ) $ldap_object['userPassword'] = $oldattrs['userPassword'][0];
			   if( $ldap_object['userquota'] == array() ) unset( $ldap_object['userquota'] );
			   if ( !$errors && !ldap_add($ldap->connection,$newdn, $ldap_object) ) {
				 array_push($errors, "LDAP Error: Could not rename $dn to $newdn ".ldap_error($ldap->connection));
				 debug("ldap_object: "); var_dump( $ldap_object );
				 debug("<br />old_object: "); var_dump( $oldattrs );
			   }
			   if( !$errors ) {
				 if( !ldap_delete($ldap->connection,$dn)) {
				   array_push($errors, "LDAP Error: Could not remove old entry $dn: ".ldap_error($ldap->connection));
				 }
			   }
			   $dn = $newdn;
			 } else array_push($errors,"LDAP Error: Could not read $dn ".ldap_error($ldap->connection));
		   } else {
			 //$ldap_object = fill_up($ldap_object);
			 if ($auth->group() == "user") {
			   unset($ldap_object['sn']);
			   unset($ldap_object['cn']);
			   unset($ldap_object['mail']);
			 }
			 if (!ldap_modify($ldap->connection, $dn, $ldap_object)) {			   
			   array_push($errors, "LDAP Error: Could not modify object $dn ".ldap_error($ldap->connection));
			   var_dump($ldap_object);
			 }
		   }
		 }
		 $heading = 'Modify User';
		 if( !$errors ) $messages[] = 'User '.$dn.' successfully modified';
		 $form->setValues();
		 $form->entries['mail_0']['attrs'] = 'readonly';
		 $form->entries['action']['value'] = 'save';
		 $form->entries['dn'] = array( 'name' => 'dn',
									   'type' => 'hidden',
									   'value' => $dn );
		 $content = $form->outputForm();
       } else {
		 // firstsave
		 if (!$errors) {
		   $dn = "cn=".$ldap_object['cn'].$dn_add.",".$domain_dn;
		   if( $ldap_object['userquota'] == array() ) unset( $ldap_object['userquota'] );
		   debug("Calling ldap_add with dn=$dn");
		   if ($dn && !ldap_add($ldap->connection, $dn, $ldap_object)) 
			 array_push($errors, "LDAP Error: could not add object $dn ".ldap_error($ldap->connection));
		   if( !$errors ) {
			 $messages[] = 'User '.$ldap_object['dn'].' successfully created';
			 $heading = 'Create New User';
			 $form->entries['action']['value'] = 'firstsave';
			 $content = $form->outputForm();
			 break;
		   }
		 } else {
		   $heading = 'Create New User';
		   $blacklist = array('mail');
		   $form->entries['action']['value'] = 'firstsave';
		   $form->outputForm();
		   break;
		 }
       }
     }
     break;
   }
 case 'create':
   $heading = 'Create New User';
   if( !$dn ) {
     $form->entries['action']['value'] = 'firstsave';
   } else {
     $form->entries['action']['value'] = 'save';
   }
   $content = $form->outputForm();
   break;
 case 'modify':
   $heading = 'Modify User';
   fill_form_for_modify( $form, $dn, $ldap_object );
   $form->entries['action']['value'] = 'save';
   $content = $form->outputForm();
   break;
 case 'delete':
   $heading = 'Delete User';
   foreach( $form->entries as $k => $v ) {
     if( $v['type'] != 'hidden' ) {
       $form->entries[$k]['attrs'] = 'readonly';
     }
   }
   fill_form_for_modify( $form, $dn, $ldap_object );
   $form->entries['action']['value'] = 'kill';
   $form->submittext = 'Delete';
   $content = $form->outputForm();
   break;
 case 'kill':
   if (!$dn) array_push($errors, "Error: need dn for delete operation");
   elseif ($auth->group() != "maintainer" && $auth->group() != "admin") 
     array_push($errors, "Error: you need administrative permissions to delete users");
   
   if (!$errors) {
     $delete_template['deleteflag'] = 'TRUE';
     if (!(ldap_modify($ldap->connection,$dn,$delete_template))) {
       array_push($errors, "LDAP Error: could not mark ".$dn." for deletion ".ldap_error($ldap->connection));
     } else {
       $heading = "User Deleted";
	   $contenttemplate = 'userdeleted.tpl';
     }
   } 
   break;
}


$smarty = new MySmarty();
$smarty->assign( 'topdir', $topdir );
$smarty->assign( 'errors', array_merge($errors,$form->errors) );
$smarty->assign( 'uid', $auth->uid() );
$smarty->assign( 'group', $auth->group() );
$smarty->assign( 'page_title', $menuitems[$sidx]['title'] );
$smarty->assign( 'menuitems', $menuitems );
$smarty->assign( 'submenuitems', 
				 array_key_exists('submenu', 
								  $menuitems[$sidx])?$menuitems[$sidx]['submenu']:array() );
$smarty->assign( 'heading', $heading );
$smarty->assign( 'form', $content );
if( isset( $dn ) ) $smarty->assign( 'dn', $dn );
if( count($messages)>0) $smarty->assign( 'messages', $messages );
$smarty->assign( 'maincontent', $contenttemplate );
$smarty->display('page.tpl');

/*
  Local variables:
  mode: php
  indent-tabs-mode: t
  tab-width: 4
  buffer-file-coding-system: utf-8
  End:
 */
?>

--- NEW FILE: vacation.php ---
<?php
require_once 'admin/include/mysmarty.php';
require_once 'admin/include/headers.php';
require_once 'admin/include/authenticate.php';

// Funny multiline string escaping in Sieve
function dotstuff( $str ) {
  return str_replace( "\n.", "\n..", $str );
}

function undotstuff( $str ) {
  return str_replace( "\n..", "\n.", $str );
}

$errors = array();
if( (@include_once 'admin/include/Sieve.php' ) === false ) {
  $errors[] = 'Net/Sieve.php is missing. Without that, vacation settings are not available';
  $errors[] = 'Suggest your system administrator to run "/kolab/bin/pear install http://pear.php.net/get/Net_Sieve" on the server';
}

/**** Authentication etc. ***/
$sidx = 'user';

require_once('admin/include/menu.php');

/**** Submenu for current page ***/
$menuitems[$sidx]['selected'] = 'selected';

/**** Sieve handling ***/
$scriptname = 'kmail-vacation.siv';
if( !$errors ) {
  $sieve =& new Net_Sieve( $auth->uid(), $auth->password() );
  
  // Update sieve script on server in case we have submit data
  if( $_REQUEST['submit'] ) {
	$addresses = array_unique( array_filter( array_map( 'trim', preg_split( '/\n/', $_POST['addresses'] ) ), 'strlen') );

	$script = 
	  "require \"vacation\";\r\n\r\n".
	  "vacation :addresses [ \"".join('", "', $addresses )."\" ] :days ".
	  $_REQUEST['days']." text:\r\n".
	  dotstuff($_REQUEST['text'])."\r\n.\r\n;";
	$active = isset($_REQUEST['active']);

	if( PEAR::isError( $res = $sieve->installScript( $scriptname, $script, $active ) ) ) {
	  $errors[] = $res->getMessage();
	  $errors[] = 'Script was:';
	  $errors[] = '<pre>'.htmlentities($script).'</pre>';
	}
	if( !$active && $sieve->getActive() === $scriptname ) {
	  $sieve->setActive( '' );
	}

	if( !$errors ) {
	  if( $active ) $messages[] = 'Vacation message successfully activated';
	  else $messages[] = 'Vacation message successfully deactivated';
	}
  }

  // Fetch script data from server
  $scripts = $sieve->listScripts();
  if( in_array( $scriptname, $scripts ) ) {
	$script = $sieve->getScript( $scriptname );
	//debug("<pre>".htmlentities($script)."</pre>");
	if( ereg(':addresses \[(.*)\]', $script, $regs ) ) {
	  $tmp = split(',', $regs[1] );
	  $addresses = array();
	  foreach( $tmp as $a ) {
		if( ereg('^ *"(.*)" *$', $a, $regs ) ) $addresses[] = $regs[1];
		else $addresses[] = $a;
	  }
	}
	if( ereg(':days ([0-9]+)', $script, $regs ) ) {
	  $days = $regs[1];
	}
	if( ereg("text:\r\n(.*)\r\n\\.\r\n", $script, $regs ) ) {
	  $text = $regs[1];
	  $text = str_replace( '\n', "\r\n", $text );
	  $text = undotstuff($text);
	}
  }
  if( !$addresses ) $addresses = array( $auth->uid() );
  if( !$days ) $days = 7;
  if( !$text ) {
	$date = strftime(_('%x'));
	$text = "I am out of office till $date.\r\n".
	  "In urgent cases, please contact Mrs. <vacation replacement>\r\n\r\n".
	  "email: <email address of vacation replacement>\r\n".
	  "phone: +49 711 1111 11\r\n".
	  "fax.:  +49 711 1111 12\r\n\r\n".
	  "Yours sincerely,\r\n".
	  "-- \r\n".
	  "<enter your name and email address here>";
  }
  $active = ( $sieve->getActive() === $scriptname );  
}

/**** Insert into template and output ***/
$smarty = new MySmarty();
$smarty->assign( 'errors', $errors );
$smarty->assign( 'messages', $messages );
$smarty->assign( 'uid', $auth->uid() );
$smarty->assign( 'group', $auth->group() );
$smarty->assign( 'page_title', $menuitems[$sidx]['title'] );
$smarty->assign( 'menuitems', $menuitems );
$smarty->assign( 'submenuitems', 
				 array_key_exists('submenu', 
								  $menuitems[$sidx])?$menuitems[$sidx]['submenu']:array() );
$smarty->assign( 'active', $active );
$smarty->assign( 'text', $text );
$smarty->assign( 'addresses', $addresses );
$smarty->assign( 'days', $days );
$smarty->assign( 'maincontent', 'vacation.tpl' );
$smarty->display('page.tpl');

/*
  Local variables:
  mode: php
  indent-tabs-mode: t
  tab-width: 4
  buffer-file-coding-system: utf-8
  End:
 */
?>





More information about the commits mailing list