µ±Ç°Î»ÖãºÊ×Ò³ - ÉçÇø¾«»ª - ÕýÎÄ
Óë´ó¼Ò·ÖÏíÎÒдµÄÒ»¸öPHP²Ù×÷LDAPÊý¾Ý¿âµÄͨÓÃÀ࣬µ÷Ó÷½±ã! - ÖйúPHPÁªÃË - ÐÅÏ¢×ÊÑ
·¢²¼Ê±¼ä: 2006-10-08 08:15¡¡¡¡×÷Õß: songwl ¡¡¡¡ÐÅÏ¢À´Ô´: http://www.phpx.com

Óë´ó¼Ò·ÖÏíÎÒдµÄÒ»¸öPHP²Ù×÷LDAPÊý¾Ý¿âµÄͨÓÃÀ࣬µ÷Ó÷½±ã!

ǰ¼¸Ì칫˾ÓÐÒ»¸öÏîĿҪÓõ½£Ì£Ä£Á£ÐÊý¾Ý¿â£¬Ã»ÓнӴ¥¹ý£¬½á¹ûËÑË÷һͨҲû¼ûÓÐÈËÌṩһ¸ö·â×°µÄÊý¾Ý²Ù×÷À࣬ËùÒÔ»¨Á˶þÌìÖÕÓÚдÁËÕâÑùÒ»¸öÀ࣮
ÒòˮƽÓÐÏÞ£¬³ÌÐòÖ®ÖÐÓв»×ãÖ®´¦»¹Çë´ó¼ÒÁ½⣡²»Òª¸øÎÒש°å¾ÍºÃ¡£¡£¡£¡£
ÒÔÏÂÖ»ÊÇÎÒµÄÒ»µã¸öÈËÀí½â£¬µ÷ÓÃÓв»Ã÷°×µÄµØ·½ÁªÏµÎÒ£ºQQ405214421

<?
class ldap
{
private $hostName; //·þÎñÆ÷µØÖ·
private $userDn; //Óû§Ãû
private $pwd; //ÃÜÂë
private $port; //¶Ë¿ÚºÅ
private $link; //·µ»ØµÄÁ¬½Ó¶ÔÏó
public $state; //·µ»ØµÄÒ»¸ö¹«¹²µÄÁ¬½Ó״̬

//------------ ¶¨ÒåÁª½ÓÀàµÄ¹¹Ô캯Êý ------------------
function __construct($hostName,$userDn,$pwd,$port=80)
{
$this->hostName=$hostName;
$this->userDn=$userDn;
$this->pwd=$pwd;
$this->port=$port;
$this->state=0;
}
//--------------- end conStruct -------------------

//-------------- ¶¨Òålink·½·¨ Á¬½Ó²¢BINDÊý¾Ý¿â ----------------
function open()
{
/////////////////////*********¼ÓÉÏ@×èÖ¹´íÎó********///////////////////////////////
$this->link = @ldap_connect($this->hostName);
@ldap_set_option($this->link, LDAP_OPT_PROTOCOL_VERSION, 3);
if ($this->link && @ldap_bind($this->link,$this->userDn,$this->pwd)) {
$ldapbind = @ldap_bind($this->link,$this->userDn,$this->pwd);
$this->state=1;
return $this->link;
@ldap_unbind($this->link); //¹Ø±ÕÊý¾Ý¿â
@ldap_close($this->link); //¹Ø±ÕÁ¬½Ó
} else {
return $this->link;
//echo "±§Ç¸£¬ÎÞ·¨Á¬ÉÏ LDAP ·þÎñÆ÷¡£";
exit;
}
}
//--------------end open()--------------------------

}
//--------------------¶¨Òå»ùÀàrecordSet------------------------------
class recordSet
{
public $link;
private $result;
function __construct($conn)
{
$this->link=$conn->open();
}
//------ ¶¨Òåexecute ·½·¨--------
public function execute($dn,$who)
{
$tdn=$dn;
$twho=$who;
$sr=ldap_search($this->link,$tdn,$twho);
$this->result = ldap_get_entries($this->link, $sr);
return $this->result;
}
//////////////ºìÉ«Ëù±êΪ¹Ø¼ü²¿·Ö///////////////////////
public function arrsort($arr,$column)
{
for($j=0;$j<=$arr["count"];$j++){
for($i=0;$i<=@count($column);$i++){
if($column[$i]=="dn"){
$array[$j][$i]=$arr[$j][$column[$i]];
} else {
$array[$j][$i]=$arr[$j][$column[$i]][0];
}
}
}
@sort($array);
return $array;
}
}
//-------------------- END »ùÀàrecordSet------------------------------
class selectRs extends recordSet
{
private $result; //·µ»ØµÄ²éѯ½á¹ûÊý×é
//public $dn;
//public $who;
///////////////// Ö´Ðм̳ÐÀàrecordSetµÄexecute£¨$dn,$who£©·½·¨////////////////////
function getArray($dn,$who)
{
$this->result=$this->execute($dn,$who);
return $this->result;
}
//---------*****************END EXECUTE£¨$dn,$who£©****************************-----------
//------ ¶¨Òå delete ·½·¨--------
function delete($dn) // ´«ÈëÒ»¸öÏêϸµÄ×éÖ¯½á¹¹DN ɾ³ý³É¹¦Ôò·µ»ØTRUE£¬·ñÔò·µ»ØFALSE
{
$state=@ldap_delete($this->link,$dn);
if($state){
return true;
} else {
return false;
}
}
//------ end delete ·½·¨--------
//------ ¶¨Òå update ·½·¨--------
function update($dn,$column) // ´«ÈëÒ»¸öÏêϸµÄ×éÖ¯½á¹¹DN¼°³ÉÔ±Êý×é ³É¹¦Ôò·µ»ØTRUE£¬·ñÔò·µ»ØFALSE
{
$state=ldap_modify($this->link,$dn,$column);
if($state){
return true;
} else {
return false;
}
}
//------ end update ·½·¨--------
//------ ¶¨Òå insert ·½·¨--------
function insert($dn,$column) // ´«ÈëÒ»¸öÏêϸµÄ×éÖ¯½á¹¹DN¼°³ÉÔ±Êý×é ²åÈë³É¹¦Ôò·µ»ØTRUE£¬·ñÔò·µ»ØFALSE
{
$state=ldap_add($this->link,$dn,$column);
if($state){
return true;
} else {
return false;
}
}
//------ end insert ·½·¨--------
/////////////////´«»Ø²éѯÊý¾ÝµÄ×ÜÐÐÊýgetRowNum()////////////////////
function getRowNum()
{
return $this->result["count"];
}
////////////////end-------getRowNum()////////////////////

///////////////// ·ÖÒ³½á¹û START////////////////////
function page($page,$arr,$column,$num=5)
{
if($page<1) $page=1;
$rownum=$this->getRowNum();
$pagecount=ceil($rownum/$num);
if($page>$pagecount) $page=$pagecount;
$a=$this->arrsort($arr,$column);
$start=($page-1)*$num+1;
$end=$page*$num;
for($i=$start;$i<=$end;$i++)
{
if($i>$rownum)
{
continue;
}
for($j=0;$j<=count($column)-1;$j++)
{
$array[$i][$j]=$a[$i][$j];
}
}
return $array;
}
/////////////////·ÖÒ³½á¹û END()////////////////////

}
/* ÒÔÏÂÊÇÎÒÏë¶Ô·ÖÒ³µÄÒ»¸ö¼ò»¯£¬¿Éϧʱ¼ä²»¹»£¬Ã»ÓÐÄܹ»Íê³É£¬²»¹ýÉÏÃæµÄ´úÂë¶ÔÒ»°ãµÄÔöɾ¸Ä²é·ÖÒ³¶¼ËÈ×ã¹»ÁË
//-------------------- ¼Ì³Ð »ùÀàselectRs----Ö´ÐзÖÒ³Àà ´ýÍêÉÆ ½ñºó»á¸üÐÂ--------------------------
class pselectRs extends selectRs
{
private $result; //·µ»ØµÄ²éѯ½á¹ûÊý×é
private $currentPage; //µ±Ç°Ò³Âë
public $absoultpage=5; //ÿҳµÄ¼Ç¼Êý
private $isSystemPage; //ÊÇ·ñÓÃϵͳĬÈÏ·ÖÒ³,·ñÔòÐèÒª×Ô¼º¶¨ÖÆ
public $pagecount; //×ÜÒ³Êý
///////////////// ÖØÐ´¼Ì³ÐÀàselectRsµÄgetArray£¨$dn,$who£©·½·¨////////////////////
function getArray($dn,$who,$column,$currentPage=1,$isSystemPage=true)
{
$this->result=$this->execute($dn,$who);
$this->currentPage=$currentPage;
$this->isSystemPage=$isSystemPage;
$this->pagecount=ceil($this->getRowNum/$this->absoultpage);
if($this->currentPage<0) $this->currentPage=1;
if($this->currentPage>$this->pagecount) $this->currentPage=$this->pagecount;
$row=$this->arrsort($this->result,$column);
$start=($this->currentPage-1)*$this->absoultpage+1;
$end=$this->currentPage*$this->absoultpage;
for($i=$start;$i<=$end;$i++)
{
for($j=0;$j<=count($column)-1;$j++)
{
$arr[$i][$j]=$row[$i][$j];
}
}
return $arr;
}
//---------*****************END selectRs£¨$dn,$who£©****************************-----------

} */
?>


×ªÔØÇë×¢Ã÷£ºÔ­´´QQ405214421£º
×¢ÖØËûÈËÀͶ¯³É¹û£¡ ¡¾ÂÛ̳ä¯ÀÀ¡¿ 
 Ïà¹ØÆÀÂÛ
"; } //} if($SG_USERNAME."@".$SG_DOMAIN!=$username){ $img="";} $ListOut .= "\n". "\t". "\n". "\n". "\n". "\n". "\n". "\t\n"; } } // Éú³ÉpageÁ¬½ÓË÷Òý function PageIndex($page,$pagecount,$rownum,$mailuser){ $list.="µ±Ç°Êǵڠ$page/$pagecount Ò³    Ê×Ò³    ÉÏÒ»Ò³    "; $list.="ÏÂÒ»Ò³    Î²Ò³    ¹² ".$rownum." Ìõ 

"; return $list; } // load Template $OUT['CHARSET'] = $CFG_CHARSET[$Get_Lang]; // Ò³Ãæ×Ö·û±àÂëÉèÖà $OUT['HREF_NAME'] = "?sortby=name&page=$get_page&direct=$get_direct"; $OUT['HREF_EMAIL'] = "?sortby=email&page=$get_page&direct=$get_direct"; $OUT['LIST'] = $ListOut; $OUT['PAGEINDEX'] = PageIndex($page,$pagecount,$rownum,$mailuser); $OUT['LANG'] = $Get_Lang; // µÇ¼ºóÏÔʾÓïÑÔ $OUT['NICKNAME'] = $SG_NICKNAME; // Óû§ÐÕÃû $OUT['EMAIL'] = "$SG_USERNAME@$SG_DOMAIN"; // Óû§emailµØÖ· include "template/$CFG_TEMPLATE/_adv.php"; include "template/$CFG_TEMPLATE/_mailuserset.php"; £Ì£Ä£Á£ÐÊÇÒ»ÖÖרΪ¶Á¶øÉè¼ÆµÄ¿â£¬²éѯËÙ¶ÈÊÇĿǰ¿ìµÄ£¬·Ç³£ÊʺÏÓÚÔÚÓʼþϵͳÖжԴóÁ¿µÄÓʼþÕ˺ŵĴ洢
×÷Õß: hlmhlm ·¢²¼ÈÕÆÚ: 2006-9-25
µÚÒ»²½£ºconn.php <? include("ldap.cs.php"); //ÀàÎļþ ?> <? $conn=new ldap("192.168.1.214","cn=root,dc=test,dc=com","secret");²ÎÊý·Ö±ðΪ£º·þÎñÆ÷£É£Ð£¬×éÖ¯½á¹¹£Ä£Î£¬Êý¾Ý¿âÃÜÂ룬¶Ë¿Ú[Ò»°ãĬÈϳö] Ò»¸ö¼òµ¥µÄʾÀý $select=new selectRs($conn); $arr=$select->getArray("o=mail,dc=test,dc=com","cn=*"); echo "¹²´«»Ø×ÊÁÏ".$arr["count"]."Ìõ
"; //for($j=0;$j<=$arr["count"];$j++){ //echo $a[$j]=$arr[$j]["mail"][0]."
"; //} $column[0]="mail"; $column[1]="cn"; $column[2]="dn"; $column[3]="userpassword"; //echo "µ±Ç°µÄCNΪ".$arr[1]["dn"]; $a=$select->arrsort($arr,$column); for($i=0;$i<=count($a);$i++){ echo $a[$i][0]."
"; echo $a[$i][1]."
"; echo $a[$i][2]."
"; echo $a[$i][3]."
"; } echo $select->getRowNum()."
"; ?> ¡¡
×÷Õß: hlmhlm ·¢²¼ÈÕÆÚ: 2006-9-25
ÐèÓÃ˵Ã÷һϣ¬ÏÂÃæÕâ¶Î´úÂëÊǶԴ洢ÓʼþÕËºÅµÄ¿â½øÐеÄÔöɾ¸Ä²é·ÖÒ³µÄ²Ù×÷£¬Ã»ÓÃʹÓ÷ÖÒ³µÄ·½·¨£¬$this->pagepage($page¡¡//³õÊ·Ò³,$arr //²éѯºóµÄ½á¹ûÊý×é,$column //ÐèÒª·µ»ØµÄÊôÐÔ£¬²ÎÊýÀàÐÍΪÊý×é,$num=5¡¡//ÿҳµÄ¼Ç¼Êý);ÕâÀï¾Í²»ÐèÒªÎÒ½éÉÜÁ衃 include "include/login_inc.php"; include "config/config_inc.php"; include "include/fun_inc.php"; include "language/$CFG_LANGUAGE"."_inc.php"; // get //$get_page = $_GET['page']; $get_keyword = $_POST['keyword']; $Cmd=$_REQUEST["Cmd"]; $mail=$_REQUEST["email"]; $active=chop($_REQUEST["active"]); $mailuser=chop($_REQUEST["mailuser"]); $username=$_REQUEST["mail"]; $cn=$_REQUEST["cn"]; $passwd=trim($_REQUEST["passwd"]); $mquota=$_REQUEST["mquota"]; $fquota=$_REQUEST["fquota"]; if($mquota=="") $mquota=0; if($fquota=="") $fquota=0; $quota=($mquota*1024*1024)."S,".$mquota."C"; $description=$_REQUEST["description"]; $accountstatus=$_REQUEST["accountstatus"]; switch($Cmd){ case "del": $ldaprdn="cn=".$CFG_LDAP_USER.",dc=test,dc=com"; $ldappass=$CFG_LDAP_PASS; include("conn.php"); $select=new selectRs($conn); $domain=str_replace('@',"",strrchr($mail,"@")); $username=str_replace(strrchr($mail,"@"),"",$mail); exec("/usr/bin/sudo /etc/scripts/sysctrl del mailuser $domain $username",$res,$rc); if($rc!=0) { echo "<script>alert('·þÎñ²Ù×÷ʧ°Ü£¡ÇëÖØÊÔÒ»´Î£¡);location.href='mailuserset.php';</script>"; exit; } $select->delete("mail=$mail,domain=$domain,o=mail,dc=test,dc=com"); echo "<script>alert('ɾ³ý³É¹¦£¡');location.href='mailuserset.php?page=$page';</script>"; //header("location: mailuserset.php?page=$page"); break; case "active": $ldaprdn="cn=".$CFG_LDAP_USER.",dc=test,dc=com"; $ldappass=$CFG_LDAP_PASS; include("conn.php"); $select=new selectRs($conn); $domain=str_replace('@',"",strrchr($mail,"@")); $column["accountstatus"]="active"; $select->update("mail=$mail,domain=$domain,o=mail,dc=test,dc=com",$column); header("location: mailuserset.php?page=$page"); break; case "deactive": $ldaprdn="cn=".$CFG_LDAP_USER.",dc=test,dc=com"; $ldappass=$CFG_LDAP_PASS; include("conn.php"); $select=new selectRs($conn); $domain=str_replace('@',"",strrchr($mail,"@")); $column["accountstatus"]="no"; $select->update("mail=$mail,domain=$domain,o=mail,dc=test,dc=com",$column); header("location: mailuserset.php?page=$page"); break; case "edit": $ldaprdn="cn=".$CFG_LDAP_USER.",dc=test,dc=com"; $ldappass=$CFG_LDAP_PASS; include("conn.php"); $select=new selectRs($conn); //$domain=str_replace('@',"",strrchr($mail,"@")); $column["mail"]=$username."@".$SG_DOMAIN; $column["cn"]=$cn; if($passwd!=""){ $column["userpassword"]='{MD5}'.base64_encode(pack( 'H*',md5("$passwd"))); } $column["quota"]=$quota; $column["description"]=$description; $column["accountstatus"]=$accountstatus; $select->update("mail=".$username."@".$SG_DOMAIN.",domain=$SG_DOMAIN,o=mail,dc=test,dc=com",$column); echo "<script>alert('Ð޸ijɹ¦£¡');location.href='mailuserset.php?page=$page';</script>"; //header("location: mailuserset.php?page=$page"); break; case "insert": $ldaprdn="cn=".$CFG_LDAP_USER.",dc=test,dc=com"; $ldappass=$CFG_LDAP_PASS; include("conn.php"); $sel=new selectRs($conn); $arrs=$sel->getArray("domain=$domain,o=mail,dc=test,dc=com","mail=".$username."@".$SG_DOMAIN.""); if($sel->getRowNum()>0){ echo "<script>alert('¶Ô²»Æð£¡Õâ¸öÕ˺ÅÒÑÓÐÈËʹÓã¬ÇëÖØÐÂÌîдһ¸ö');history.back(-1);</script>"; exit; } $select=new selectRs($conn); $column["mail"]=$username."@".$SG_DOMAIN; if($cn=="") $cn=$username; if($description=="") $description="_"; $column["cn"]=$cn; $column["userPassword"]='{MD5}'.base64_encode(pack( 'H*',md5("$passwd"))); $column["quota"]=$quota; $column["homeDirectory"]="/var/mail/"; $column["objectClass"]="CourierMailAccount"; $column["mailbox"]="$SG_DOMAIN/$username/Maildir/"; $column["description"]=$description; $column["accountStatus"]=$accountstatus; exec("/usr/bin/sudo /etc/scripts/sysctrl addmailuser $SG_DOMAIN $username $quota",$res,$rc); if($rc!=0) { echo "<script>alert('·þÎñ²Ù×÷ʧ°Ü£¡ÇëÖØÊÔÒ»´Î£¡);location.href='mailuserset.php';</script>"; exit; } $select->insert("mail=".$username."@".$SG_DOMAIN.",domain=$SG_DOMAIN,o=mail,dc=test,dc=com",$column); echo "<script>alert('²Ù×÷³É¹¦£¡');location.href='mailuserset.php';</script>"; break; } $ldaprdn="cn=".$CFG_LDAP_USER.",dc=test,dc=com"; $ldappass=$CFG_LDAP_PASS; include("conn.php"); $select=new selectRs($conn); if($mailuser!=""){ $if="mail=*".$mailuser."*@".$SG_DOMAIN; } else { $if="mail=*@".$SG_DOMAIN;} if($active=="no"){ $if="accountStatus=no"; //$arr=$select->getArray("domain=$SG_DOMAIN,o=mail,dc=test,dc=com","accountStatus=no"); } $arr=$select->getArray("domain=$SG_DOMAIN,o=mail,dc=test,dc=com",$if); //} $column[0]="mail"; $column[1]="cn"; $column[2]="quota"; $column[3]="accountstatus"; $column[4]="description"; $page=$_REQUEST["page"]; if($page<1) $page=1; $num=8; $rownum=$select->getRowNum(); $pagecount=ceil($rownum/$num); if($page>$pagecount) $page=$pagecount; $a=$select->arrsort($arr,$column); $start=($page-1)*$num+1; $end=$page*$num; // ´¦ÀíÅÅÐò¹Ø¼ü×Ö if($rownum>0){ for($i=$start;$i<=$end;$i++){ if($i>$rownum){ continue; } $username=$a[$i][0]; $realname=$a[$i][1]; $quota=$a[$i][2]; $accountstatus=$a[$i][3]; $description=$a[$i][4]; $aa=explode(',',$quota); $mquota=str_replace('S','',$aa[0]); $mquota=ceil($mquota/1024/1024); $fquota=str_replace('C','',$aa[1]); $user=str_replace(strrchr($username,"@"),"",$username); unset($aa); //if($SG_USERNAME==$admin){ if($accountstatus=="active"){ $type=""; $status=true;$n=0;} else { $type=""; $status=false;$n=1;} //if($accountstatus=="active"){ $type="";} else { $type="
".$i."
".$username."
".$realname."
".$mquota."/".$fquota."
".$type."
".$img."

²é¿´ÆÀÂÛ>>¡¡·¢±íÆÀÂÛ
ÆÀ·Ö:
ÄÚÈÝ:
ÑéÖ¤:

¹²ÓÐÆÀÂÛÊý 2/ÿҳÏÔʾÊý 10