steffen: server/kolab-webadmin/kolab-webadmin/www/admin/administrator admin.php, NONE, 1.1 index.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/administrator
In directory doto:/tmp/cvs-serv23429/kolab-webadmin/www/admin/administrator

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

--- NEW FILE: admin.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()
{
  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 'Administrator with this UID 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, &$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['uid'])) $uid = $ldap_object['uid'][0];
  else $uid = $ldap_object['uid'];
  $form->entries['firstname']['value'] = $firstname;
  $form->entries['lastname']['value'] = $lastname;
  $form->entries['password_0']['value'] = '';
  $form->entries['password_1']['value'] = '';
  $form->entries['uid']['value'] = $uid;
  $form->entries['uid']['attrs'] = 'readonly';

  /*
  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 = 'administrator';

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']));

// Check auth
if (!$errors && $auth->group() != "admin"  ) {
  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 ),
		  'uid' => array( 'name' => 'Unique User ID',
				     'validation' => 'notempty',
				     'comment' => $comment_mail_0 ));

$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( 'admin', 'createadmin.tpl', $entries );
/***************** Main action swicth **********************/
switch( $action ) {
 case 'firstsave':
   debug("adding checkuniquemail to validation");
   $form->entries['uid']['validation'] = 'checkuniquemail';   
 case 'save':
   if( $form->isSubmitted() ) {
     if( !$form->validate() ) {
       $form->setValues();
       $content = $form->outputForm();
     } else {
       debug("Process...");
       $ldap_object = array();
       $ldap_object['objectClass'] = 'inetOrgPerson';
       $ldap_object['sn'] = trim($_POST['lastname']);
       $ldap_object['cn'] = trim($_POST['firstname']).' '.$ldap_object['sn'];
       if( !empty( $_POST['password_0'] ) ) {
		 $ldap_object['userPassword'] = '{sha}'.base64_encode( pack('H*', 
																	sha1( $_POST['password_0'])));
       }
       if( $action == 'firstsave' ) $ldap_object['uid'] = trim( strtolower( $_POST['uid'] ) );

	   var_dump( $ldap_object );

       $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];
			   if (!ldap_add($ldap->connection,$newdn, $ldap_object) )
				 array_push($errors, "LDAP Error: could not rename $dn to $newdn ".ldap_error($ldap->connection));
			   if( !$errors ) {
				 if( !ldap_delete($ldap->connection,$dn)) {
				   array_push($errors, "LDAP Error: could not remove old entry $dn: ".ldap_error($ldap->connection));
				 }
			   }
			   if( !$errors ) {
				 // Update admin group
				 $groupdn = 'cn=admin,'.$domain_dn;
				 if( !ldap_mod_delete($ldap->connection,$groupdn,
									  array( 'member' => $dn ) ) ) {
				   $errors[] = "LDAP Error: Could not remove old group entry $dn: "
					 .ldap_error($ldap->connection());
				 }
				 if( !$errors && ldap_mod_add( $ldap->connection,
											   $groupdn,
											   array( 'member' => $newdn ) ) ) {
				   $errors[] = "LDAP Error: Could not add new group entry $newdn: "
					 .ldap_error($ldap->connection());
				 }
			   }			   
			   $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));
			 }
		   }
		 }
		 $heading = 'Modify Administrator';
		 $messages[] = 'Administrator '.$ldap_object['dn'].' successfully modified';
		 $form->setValues();
		 $form->entries['action']['value'] = 'save';
		 $content = $form->outputForm();
		 break;
       } else {
		 // firstsave
		 if (!$errors) {
		   $dn = "cn=".$ldap_object['cn'].",".$domain_dn;
		   debug("Calling ldap_add with dn=$dn");
		   // Add object to db
		   if ($dn && !ldap_add($ldap->connection, $dn, $ldap_object)) 
			 array_push($errors, "LDAP Error: could not add object $dn ".ldap_error($ldap->connection));

		   // Add object to admin group
		   if( $dn && !ldap_mod_add($ldap->connection, 'cn=admin,'.$domain_dn, 
									array( 'member' => $dn ) ) ) {
			 array_push($errors, "LDAP Error: could not add object $dn to maintainer group".ldap_error($ldap->connection));			 
		   }
		   if( !$errors ) {
			 $messages[] = 'Administrator '.$ldap_object['dn'].' successfully created';
			 $heading = 'Create New Administrator';
			 $form->entries['action']['value'] = 'firstsave';
			 $content = $form->outputForm();
			 break;
		   }
		 } else {
		   $heading = 'Create New Administrator';
		   $blacklist = array('mail');
		   $form->entries['action']['value'] = 'firstsave';
		   $form->outputForm();
		   break;
		 }
       }
     }
     break;
   }
 case 'create':
   $heading = 'Create New Administrator';
   if( !$dn ) {
     $form->entries['action']['value'] = 'firstsave';
   } else {
     $form->entries['action']['value'] = 'save';
   }
   $content = $form->outputForm();
   break;
 case 'modify':
   $heading = 'Modify Administrator';
   fill_form_for_modify( $form, $ldap_object );
   $form->entries['action']['value'] = 'save';
   $content = $form->outputForm();
   break;
 case 'delete':
   $heading = 'Delete Administrator';
   foreach( $form->entries as $k => $v ) {
     if( $v['type'] != 'hidden' ) {
       $form->entries[$k]['attrs'] = 'readonly';
     }
   }
   fill_form_for_modify( $form, $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() != "admin") 
     array_push($errors, "Error: you need administrative permissions to delete administrators");
   
   if (!$errors) {
	 if(!ldap_mod_del($ldap->connection, 'cn=admin,'.domain_dn(), array('member' => $dn ) )) {
	   $errors[] = "LDAP Error: Could not remove $dn from admin group: "
		 .ldap_error($ldap->connection);
	 }
	 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));
	   }
	 }
   }
   if( !$errors ) {
	 $heading = "Administrator Deleted";
	 $contenttemplate = 'admindeleted.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: 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 = 'administrator';

if( $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';

  $maintainers = $ldap->groupMembers( $base_dn, 'admin' );

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

  if( $result ) {
	$title = 'Manage Administrators ('.count($maintainers).' Administrators)';
	// 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 = 'adminlistalpha.tpl';
	} else if( false && $count > 50 ) {
	  // ... TODO
	  $template = 'adminlistgroup.tpl';
	}  else {
	  $template = 'adminlistall.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];
        $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, $maintainers ) ) {
		  $entries[] = array( 'dn' => $dn,
							  'sn' => $sn,
							  'fn' => $fn,
							  'uid' => $userid,
							  '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:
 */
?>





More information about the commits mailing list