gunnar: server/kolab-webadmin/admin/addressbook addr.php.in, NONE, 1.1 index.php, NONE, 1.1

cvs at kolab.org cvs at kolab.org
Fri Aug 17 10:51:28 CEST 2007


Author: gunnar

Update of /kolabrepository/server/kolab-webadmin/admin/addressbook
In directory doto:/tmp/cvs-serv14729/addressbook

Added Files:
	addr.php.in index.php 
Log Message:
The web admin will be restructured to get a better distinction between the library and the web application parts. This currently a draft but I wanted to add this early into CVS so that the following changes will be visible.

--- NEW FILE: addr.php.in ---
<?php
require_once('Kolab/Webadmin/mysmarty.php');
require_once('Kolab/Webadmin/headers.php');
require_once('Kolab/Webadmin/locale.php');
require_once('Kolab/Webadmin/authenticate.php');
require_once('Kolab/Webadmin/form.class.php');


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

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

require_once('Kolab/Webadmin/menu.php');

function fill_form_for_modify( &$form, &$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 = trim(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['mail']['value'] = $mail_0;

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

$dn="";
if (!empty($_REQUEST['dn'])) $dn = trim($_REQUEST['dn']);

function checkuniquemail( $form, $key, $value ) {
  global $ldap;
  global $dn;
  global $action;
  $value = trim($value);
  if( $value == '' ) return ''; // OK

  $excludedn = false;
  if( $action == 'save' ) $excludedn = trim($dn);

  if( $ldap->countMail( $_SESSION['base_dn'], $value, $excludedn ) > 0 ) {	
	return _('User, vCard or distribution list with this email address already exists');
  } else {
	return '';
  }
}

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

/**** Form/data handling ***/
if (!empty($_REQUEST['action']) && 
    in_array($_REQUEST['action'],$valid_actions)) $action = trim($_REQUEST['action']);
else array_push($errors, _("Error: need valid action to proceed") );

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

$attributes = array( 'title', 'cn', 'sn', 'mail', 'alias', 'o',
                     'ou', 'roomNumber', 'street', 'postOfficeBox',
                     'postalCode', 'l', 'c', 'telephoneNumber',
                     'facsimileTelephoneNumber' );

$entries = array( 'firstname' => array( 'name' => _('First Name'),
										'validation' => 'notempty',
										'comment' => _('Required') ),
				  'lastname' => array( 'name' => _('Last Name'),
									   'validation' => 'notempty',
									   'comment' => _('Required') ),
				  'title' => array( 'name' => _('Title') ),
				  'mail'  => array( 'name' => _('Primary E-Mail Address'),
									'validation' => 'checkuniquemail' ),
				  'alias' => array( 'name' => _('E-Mail Aliases'),
									'type' => 'textarea',
									'validation' => 'checkuniquealias',
									'comment' => _('One address per line')),
				  'o' => array( 'name' => _('Organisation') ),
				  'ou' => array( 'name' => _('Organisational Unit') ),
				  'roomNumber' => array( 'name' => _('Room Number') ),
				  'street' => array( 'name' => _('Street Address') ),
				  'postOfficeBox' => array( 'name' => _('Post Box') ),
				  'postalCode' => array( 'name' => _('Postal Code') ),
				  'l' => array( 'name' => _('City') ),
				  'c' => array( 'name' => _('Country') ),
				  'telephoneNumber' => array( 'name' => _('Telephone Number'),
											  'validation' => 'checkphone' ),
				  'facsimileTelephoneNumber' => array( 'name' => _('Fax Number'),
													   'validation' => 'checkphone' ));
$entries['action'] = array( 'name' => 'action',
							'type' => 'hidden' );

$dn = '';
if( $action == 'modify' || $action == 'delete' || $action == 'save' || $action == 'kill' ) {
  if( $_REQUEST['dn'] ) {
	$dn = $_REQUEST['dn'];
  } else {  
	array_push($errors, sprintf(_("Error: DN required for %s operation"), $action ));
  }
}

$form =& new KolabForm( 'vcard', 'createaddr.tpl', $entries );

if( !$errors ) {
  switch( $action ) {
  case 'create':
	$form->entries['action']['value'] = 'firstsave';
	$heading = _('Add External Address'); 
	$content = $form->outputForm();
	break;
  case 'firstsave':
		$ldap_object = array('objectClass' => array( 'top', 'inetOrgPerson', 'kolabInetOrgPerson' ) );
  case 'save':	
	if( $form->isSubmitted() ) {
	  if( !$form->validate() ) {
		$form->setValues();
		$content = $form->outputForm();
	  } else {
		$addressbook_root = "cn=external,".$_SESSION['base_dn'];   
		$firstname = trim($_POST['firstname']);
		$lastname = trim($_POST['lastname']);
		$ldap_object['sn'] = trim($lastname);
		$ldap_object['cn'] = trim( $firstname.' '.$ldap_object['sn']);
		$ldap_object['givenName'] = trim($firstname);
		foreach ($attributes as $attr) {
		  if ($attr == 'sn' || $attr == 'cn' || $attr == 'alias' ) continue;
		  $count = 0;
		  $key = $attr;
		  $args = array();
		  while (!empty($_POST[$key])) {
			$args[$count] = $_POST[$key];
			$count++;
			$key = $attr."_".$count;
		  }
		  if ($count > 0) $ldap_object[$attr] = $args;
		  elseif (!empty($_POST[$key])) $ldap_object[$attr] = $_POST[$key];  
		  else $ldap_object[$attr] = array();
		}
		$ldap_object['alias'] = array_unique( array_filter( array_map( 'trim', preg_split( '/\n/', $_POST['alias'] ) ), 'strlen') );

		if ($action == "save") {
		  if (!$errors) {
			if (!empty($ldap_object['cn'])) $newdn = "cn=".$ldap_object['cn'].",".$addressbook_root;
			else $newdn = $dn;
			debug("action=save, dn=$dn, newdn=$newdn<br/>\n");
			if (strcmp($dn,$newdn) != 0) {
			  // Check for distribution lists with this entry as member
			  $ldap->search( $_SESSION['base_dn'], 
							 '(&(objectClass=kolabGroupOfNames)(!(kolabDeleteFlag=*))(member='.$ldap->escape($dn).'))',
							 array( 'dn', 'mail' ) );
			  $distlists = $ldap->getEntries();
			  unset( $distlists['count'] );
			  foreach( $distlists as $distlist ) {
				$dlcn = $distlist['mail'][0];
				$errors[] = sprintf(_("Addressbook entry DN could not be modified, distribution list <a href='@webserver_web_prefix@/admin/distributionlist/list.php?action=modify&dn=%s'>'%s'</a> depends on it. To modify this entry, first remove it from the distribution list."), urlencode($distlist['dn']), $dlcn );
			  }
			  
			  if( !$errors ) {
				if (($result=ldap_read($ldap->connection,$dn,"(objectclass=*)")) &&
					($entry=ldap_first_entry($ldap->connection,$result)) &&
					($oldattrs=ldap_get_attributes($ldap->connection,$entry))) {
				  foreach( $ldap_object as $k => $v ) if( $v == array() ) unset( $ldap_object[$k] );

				  // Try to rename the object
				  if (!ldap_rename($ldap->connection, $dn, "cn=" . $ldap_object['cn'], $addressbook_root, true)) {
					array_push($errors, sprintf(_("LDAP Error: could not rename %s to %s: %s"), $dn,
												$newdn, ldap_error($ldap->connection)));
				  }
				  if( !$errors ) {
					// Renaming was ok, now try to modify the object accordingly
					if (!ldap_modify($ldap->connection, $newdn, $ldap_object)) {
					  // While this should not happen, in case it does, we need to revert the
					  // renaming
					  array_push($errors, sprintf(_("LDAP Error: could not modify %s to %s: %s"), $newdn,
												  ldap_error($ldap->connection)));
					  $old_dn = substr($dn, 0, strlen($dn) - strlen($addressbook_root) - 1);
					  ldap_rename($ldap->connection, $newdn, $old_dn, $addressbook_root, true);
					} else {
					  // everything is fine and we can move on
					  $messages[] = sprintf( _("%s successfully updated"), $newdn);
					  $dn = $newdn;
					}
				  }

				} else {
				  array_push($errors,sprintf(_("LDAP Error: could not read %s: %s"), $dn,
											 ldap_error($ldap->connection)));
				}
			  }
			} else {
			  if (!ldap_modify($ldap->connection, $dn, $ldap_object)) {
				array_push($errors, sprintf(_("LDAP Error: could not modify object %s: %s"),$dn,
											ldap_error($ldap->connection)));
			  } else {
				$messages[] = sprintf(_("%s successfully updated"), $dn);
			  }
			}
		  } 
		} else {
		  if (!$errors) {
			$dn = "cn=".$ldap_object['cn'].",".$addressbook_root;
			foreach( $ldap_object as $k => $v ) if( $v == array() ) unset( $ldap_object[$k] );
			if ($dn && !ldap_add($ldap->connection, $dn, $ldap_object)) {
			  array_push($errors, sprintf(_("LDAP Error: could not add object %s: %s"), $dn,
										  ldap_error($ldap->connection)));
			} else {
			  $messages[] = sprintf(_("%s successfully added"), $dn);
			}
		  }
		  if ($errors) {
			//print("<div class=\"maintitle\"> Create New Address Book Entry </div>\n");
			$form->entries['action']['value'] = 'create';
			break;
		  }
		}
		$form->entries['action']['value'] = 'modify';
		$heading = _('Modify External Address');
		$form->setValues();
		$form->entries['dn'] = array( 'name' => 'dn',
									  'type' => 'hidden',
									  'value' => $dn );
		$content = $form->outputForm();		
		
	  }
	}
	break;
  case 'modify':
	$ldap_object = $ldap->read( $dn );
	if( $ldap_object ) {
	  fill_form_for_modify( $form, $ldap_object );
	  $form->entries['action']['value'] = 'save';
	  $form->entries['dn'] = array( 'name' => 'dn',
									'type' => 'hidden',
									'value' => $dn );
	  $heading = _('Modify External Address'); 
	  $content = $form->outputForm();
	} else {
	  array_push($errors, sprintf(_("Error: No entry with DN %s found"), $dn) );
	}
	break;
  case 'delete':
	foreach( $form->entries as $k => $v ) {
	  if( $v['type'] != 'hidden' ) {
		$form->entries[$k]['attrs'] = 'readonly';
	  }
	}
	$ldap_object = $ldap->read( $dn );
	if( $ldap_object ) {
	  fill_form_for_modify( $form, $ldap_object );
	  $form->entries['action']['value'] = 'kill';
	  foreach( array_keys($form->entries) as $key ) {
		$form->entries[$key]['attrs'] = 'readonly';
	  }
	  $form->submittext = _('Delete');
	  $heading = _('Delete External Address');
	  $content = $form->outputForm();
	} else {
	  array_push($errors, sprintf(_("Error: No entry with DN %s found"), $dn) );
	}
	break;
  case 'kill':
	// Check for distribution lists with only this user as member
	$ldap->search( $_SESSION['base_dn'], 
				   '(&(objectClass=kolabGroupOfNames)(member='.$ldap->escape($dn).'))',
				   array( 'dn', 'cn', 'mail', 'member' ) );
	$distlists = $ldap->getEntries();
	unset($distlists['count']);
	foreach( $distlists as $distlist ) {
	  $dlmail = $distlist['mail'][0];
	  if( !$dlmail ) $dlmail = $distlist['cn'][0]; # Compatibility with old stuff
	  if( $distlist['member']['count'] == 1 ) {
		$errors[] = sprintf(_("Account could not be deleted, distribution list '%s' depends on it."), $dlmail);
	  }
	}
	if( !$errors ) foreach( $distlists as $distlist ) {
	  $dlmail = $distlist['mail'][0];
	  if( !$dlmail ) $dlmail = $distlist['cn'][0]; # Compatibility with old stuff
	  if( ldap_mod_del( $ldap->connection, $distlist['dn'], array('member' => $dn ) ) ) {
		$messages[] = sprintf(_("Addressbook entry removed from distribution list '%s'."), $dlmail);
	  } else {
		$errors[] = sprintf(_("Failure to remove addressbook entry from distribution list '%s', entry will not be deleted."),
							$dlmail);
		break;
	  }
	}
	
	if (!$errors) {    
	  if (!($ldap->deleteObject($dn))) {
		array_push($errors, sprintf(_("LDAP Error: could not delete %s: %s"),$dn,
									ldap_error($ldap->connection)));
	  } else {
		$heading = _('Entry Deleted');
		$messages[] = sprintf( _("Address book entry with DN %s was deleted"), $dn);
		$contenttemplate = 'addrdeleted.tpl';
	  }
	}
	break;
  }
}

/**** Insert into template and output ***/
$smarty =& new MySmarty();
$smarty->assign( 'errors', array_merge((array)$errors,(array)$form->errors) );
$smarty->assign( 'messages', $messages );
$smarty->assign( 'heading', $heading );
$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: index.php ---
<?php
/*
 (c) 2004 Klaraedalens Datakonsult AB
 (c) 2003 Tassilo Erlewein <tassilo.erlewein at erfrakon.de>
 (c) 2003 Martin Konold <martin.konold at erfrakon.de>
 This program is Free Software under the GNU General Public License (>=v2).
 Read the file COPYING that comes with this packages for details.
*/

require_once('Kolab/Webadmin/mysmarty.php');
require_once('Kolab/Webadmin/headers.php');
require_once('Kolab/Webadmin/locale.php');
require_once('Kolab/Webadmin/authenticate.php');
require_once('Kolab/Webadmin/form.class.php');

$errors = array();

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

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

require_once('Kolab/Webadmin/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
$entries = array();
if( !$errors ) {
  if (isset($_SESSION['base_dn'])) $base_dn = $_SESSION['base_dn'];
  else $base_dn = 'k=kolab';
  $userfilter = "cn=*";
  $filterattr = KolabForm::getRequestVar('filterattr');
  $filtertype = KolabForm::getRequestVar('filtertype');
  $filtervalue = KolabForm::getRequestVar('filtervalue');
  if( !in_array( $filterattr, array( 'cn', 'mail' ) ) ) $filterattr = 'cn';
  if( isset( $filtervalue ) && !empty( $filtervalue ) ) {
	switch( $filtertype ) {
	case 'contains': // contains
	  $userfilter = "$filterattr=*".$ldap->escape($filtervalue).'*';
	  break;
	case 'is': // is
	  $userfilter = "$filterattr=".$ldap->escape($filtervalue);
	  break;
	case 'begins': // begins with
	  $userfilter = "$filterattr=".$ldap->escape($filtervalue).'*';
	  break;
	case 'ends': // ends with
	  $userfilter = "$filterattr=*".$ldap->escape($filtervalue);
	  break;
	}
  }
  $alphalimit = '';
  if( isset( $_REQUEST['alphalimit']) ) {
	$ala='sn'; // alpha limit attibute
	switch( $_REQUEST['alphalimit'] ) {
	case 'a': $alphalimit = "(|($ala=a*)($ala=b*)($ala=c*)($ala=d*)($ala=e*)($ala=f*))"; break;
	case 'g': $alphalimit = "(|($ala=g*)($ala=h*)($ala=i*)($ala=j*)($ala=k*)($ala=l*))"; break;
	case 'm': $alphalimit = "(|($ala=m*)($ala=n*)($ala=o*)($ala=p*)($ala=q*)($ala=r*))"; break;
	case 's': $alphalimit = "(|($ala=s*)($ala=t*)($ala=u*)($ala=v*)($ala=w*)($ala=x*)($ala=y*)($ala=z*))"; break;
	case 'other': $alphalimit = "(|($ala=æ*)($ala=ø*)($ala=å*)($ala=ä*)($ala=ö*)($ala=ü*)($ala=0*)($ala=1*)($ala=2*)($ala=3*)($ala=4*)($ala=5*)($ala=6*)($ala=7*)($ala=8*)($ala=9*))"; break;
	default: $alphalimit = '';
	}
  }
  $filter = "(&($userfilter)$alphalimit(objectclass=inetOrgPerson)(!(uid=*))(sn=*))";
  $result = ldap_search($ldap->connection, $base_dn, $filter);
  if( $result ) {
	$count = ldap_count_entries($ldap->connection, $result);
	$title = "Manage Address Book ($count Addresses)";
	// 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 = 'addrlistalpha.tpl';
	} else if( false && $count > 50 ) {
	  // ... TODO
	  $template = 'addrlistgroup.tpl';
	}  else {
	  $template = 'addrlistall.tpl';
	  ldap_sort($ldap->connection,$result,'sn');
	  $entry = ldap_first_entry($ldap->connection, $result);
	  while( $entry ) {
		$attrs = ldap_get_attributes($ldap->connection, $entry);
		$dn = ldap_get_dn($ldap->connection,$entry);
        $mail = $attrs['mail'][0];
        $sn = $attrs['sn'][0];
        $cn = $attrs['cn'][0];
        $a = strlen($sn);
        $b = strlen($cn);
        $fn = substr($cn, 0, $b - $a);
		$deleted = array_key_exists('kolabDeleteflag',$attrs)?$attrs['kolabDeleteflag'][0]:"FALSE";
		$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( 'self_url', $_SERVER['PHP_SELF'] );

$smarty->assign( 'filterattrs', array( 'cn'   => _('Name'),
									   'mail' => _('Email') ) );
$smarty->assign( 'filtertypes', array( 'contains'   => _('contains'),
									   'is' => _('is'),
									   'begins'  => _('begins with'),
									   'ends'  => _('ends with') ) );
$smarty->assign( 'filterattr', $filterattr );
$smarty->assign( 'filtertype', $filtertype );
$smarty->assign( 'filtervalue', $filtervalue );

$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:
 */
?>





More information about the commits mailing list