steffen: server/kolab-webadmin/kolab-webadmin/www/admin/addressbook addr.php, NONE, 1.1 index.php, NONE, 1.1

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


Author: steffen

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

Added Files:
	addr.php index.php 
Log Message:
new webinterface for the kolab server

--- NEW FILE: addr.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');


/**** 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('admin/include/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', '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]['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';
}


/**** 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");

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

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' ),
				  'alias' => array( 'name' => 'E-Mail Aliases',
									'type' => 'textarea',
									'comment' => 'One address per line'),
				  'o' => array( 'name' => 'Organisation' ),
				  'ou' => array( 'name' => 'Organisational Unit' ),
				  'room' => 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' ),
				  'facsimileTelephoneNumber' => array( 'name' => 'Fax Number' ));
$entries['action'] = array( 'name' => 'action',
							'type' => 'hidden' );

if( $action == 'modify' || $action == 'delete' ) {
  if( $_POST['dn'] ) {
	$dn = $POST['dn'];	
  } else if( $_REQUEST['dn'] ) {
	$dn = $_REQUEST['dn'];
  } else {  
	array_push($errors, "Error: DN required for $action operation");	
  }
}

$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':
  case 'save':	
	if( $form->isSubmitted() ) {
	  if( !$form->validate() ) {
		$form->setValues();
		$content = $form->outputForm();
	  } else {
		$addressbook_root = "cn=external,".$_SESSION['base_dn'];   
		$ldap_object = array('objectClass' => 'inetOrgPerson');
		$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] = trim($_POST[$key]);
			$count++;
			$key = $attr."_".$count;
		  }
		  if ($count > 0) $ldap_object[$attr] = $args;
		  else if (!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 ($action == "save") {		  
		  if (!$errors) {
			if (!empty($ldap_object['cn'])) $newdn = "cn=".$ldap_object['cn'].",".$addressbook_root;
			else $newdn = $dn;
			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))) {
				if (!ldap_add($ldap->connection,$newdn, $ldap_object) || !ldap_delete($ldap->connection,$dn)) {
				  array_push($errors, "LDAP Error: could not rename ".$dn.
							 " to ".$newdn." ".ldap_error($ldap->connection));
				} else {
				  $messages[] = "$newdn successfully updated";
				}
				$dn = $newdn;
			  } else {
				array_push($errors,"LDAP Error: could not read ".$dn." ".ldap_error($ldap->connection));
			  }
			} else {
			  if (!ldap_modify($ldap->connection, $dn, $ldap_object)) {
				array_push($errors, "LDAP Error: could not modify object ".$dn." ".ldap_error($ldap->connection)); 
			  } else {
				$messages[] = "$dn successfully updated";
			  }
			}
		  } 
		} else {
		  if (!$errors) {
			$dn = "cn=".$ldap_object['cn'].",".$addressbook_root;
			if ($dn && !ldap_add($ldap->connection, $dn, $ldap_object)) {
			  array_push($errors, "LDAP Error: could not add object ".$dn." ".ldap_error($ldap->connection));
			} else {
				  $messages[] = "$dn successfully added";
			}
		  }
		  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':
	$result = $ldap->search( $dn, '(objectClass=inetOrgPerson)' );
	if( $result ) {
	  $ldap_object = ldap_get_entries( $ldap->connection, $result );
	  if( $ldap_object['count'] == 1 ) {
		fill_form_for_modify( $form, $ldap_object[0] );
		$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, "Error: Multiple results returned for DN $dn");		
	  }
	}
	break;
  case 'delete':
	$result = $ldap->search( $dn, '(objectClass=*)' );
	if( $result ) {
	  $ldap_object = ldap_get_entries( $ldap->connection, $result );
	  if( $ldap_object['count'] == 1 ) {
		fill_form_for_modify( &$form, $ldap_object[0] );
		$form->entries['action']['value'] = 'kill';
		foreach( $form->entries as $key ) {
		  $form->entries[$key]['attrs'] = 'readonly';
		}
		$form->submittext = 'Delete';
		$heading = 'Delete External Address'; 
		$content = $form->outputForm();
	  } else {
		array_push($errors, "Error: Multiple results returned for DN $dn");		
	  }
	}
	break;
  case 'kill':
	if (!$errors) {    
	  if (!(ldap_delete($ldap->connection,$dn))) {
		array_push($errors, "LDAP Error: could not delete ".$dn." ".ldap_error($link));
	  } else {
		$heading = 'Entry Deleted';
		$messages[] = 'Address book entry with DN '.$dn.' was deleted';
		$contenttemplate = 'addrdeleted.tpl';
	  }
	}
	break;
  }
}

/**** Insert into template and output ***/
$smarty =& new MySmarty();
$smarty->assign( 'errors', $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 Klarlvdalens 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('admin/include/mysmarty.php');
require_once('admin/include/headers.php');
require_once('admin/include/authenticate.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('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';
  $filter = "(&(cn=*)(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 ($count > 2000) {
	  // ... TODO
	  $template = 'addrlistalpha.tpl';
	} else if( $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);
		$entries[] = array( 'dn' => $dn,
							'sn' => $sn,
							'fn' => $fn,
							'mail' => $mail );		
		$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:
 */
?>





More information about the commits mailing list