1. 程式人生 > >LDAP批量新增php頁面,包含郵件傳送

LDAP批量新增php頁面,包含郵件傳送

頁面index.html

<html lang="en">
<head>
  <!-- <meta charset="UTF-8"> -->
  <title>ldap新增使用者</title>
</head>

<form action="admin_f.php" method="post"  name="commentform">
  <table  border="0" cellpadding="0" cellspacing="0">
   <tr><td width="500" height="30">請輸入資訊 (帳號,密碼):<br/>
<textarea style="width:400px;height:300px;text-align:left;" type="text" name="txt_info" size="12" placeholder="請在這裡輸入資訊,例如 group,user,
[email protected]
"></textarea><br/> <input type="submit" name="submit" value="登入"> </td> </tr> </table> </form> </body> </html>

處理頁面

admin_f.php

<?php
$user_pd="2018";
$dc='ou=people,dc=ldap,dc=com';
$object="inetOrgPerson";
$hostname="192.168.5.121:389";
$admin='cn=admin,dc=ldap,dc=com';
$admin_pw="admin2018";
$ds=ldap_connect($hostname) or die("Could not connect to LDAP server.");

        if (ldap_set_option($ds, LDAP_OPT_PROTOCOL_VERSION, 3)) {
                   $echo_info.= '\\n'."使用 LDAPv3  protocol version";
        }else{
                   echo '\\n'."Failed to set protocol version to 3".'\\n';
        }
        $r=ldap_bind($ds,$admin,$admin_pw);
        if($r){
                $echo_info.='\\n'."連線Ldap伺服器成功";
        }else{
                echo "<br/>連線Ldap伺服器失敗,請稍候再嘗試,如失敗,請聯絡admin";
        }

if ($_REQUEST["txt_info"]){
	$str=explode("\n",$_REQUEST["txt_info"]);
	foreach ($str as $value){
		$string= trimall($value);
		if( $string == null ){
                continue;
       		 }
		$str_info=explode(",",$string);
		if($str_info[0] != "people"){
			$user_dc='ou='.$str_info[0].",".$dc;
		}else{
			$user_dc=$dc;
		}
		$username=$str_info[1];
	        $Email=$str_info[2];
		$dc_address='cn='.$username.",".$user_dc;
		$info["cn"]=$username;
		$info["sn"]=$username;
		$info["userpassword"]=$user_pd;
		$info["mail"]=$Email;
		$info["objectclass"]=$object;
		
		$result=ldap_add($ds,$dc_address, $info);
                if($result){
                        $echo_info.='\\n'."新增".$username."使用者成功";
			$locale='en_US.UTF-8';
			setlocale(LC_ALL,$locale);
			putenv('LC_ALL='.$locale);
			system( "sh fmail.sh {$Email} {$username} ");
			$echo_info.='\\n'."郵件".$Email."傳送成功".'\\n';
                }else{
                        echo "<br/>error:faile,新增資訊為".var_dump($ds);
                }
	}
ldap_unbind($ds);
ldap_close($ds);
echo "<script>alert('{$echo_info}點選返回上一頁!');location.href='".$_SERVER["HTTP_REFERER"]."';</script>";
}else{
	echo "<script>alert('輸入內容不能為空!');location.href='".$_SERVER["HTTP_REFERER"]."';</script>";
}

function trimall($str){
    $qian=array(" "," ","\t","\\n","\r");
    return str_replace($qian, '', $str);
}

?>
#!/bin/bash
mail_zt="ldap帳號已開通"

mail -s $mail_zt $1 << EOF

ldap帳號已開通,使用者名稱: $2,密碼預設2018

自助修改密碼連線 http://passwd.com.cn/

目前可登陸wiki,svn,git其他功能待續...

wiki地址: http://wiki.com.cn/
svn地址 : http://svn.com.cn/
git地址 :http://git.com.cn/

EOF

mail配置

yum -y install mailx
vim /etc/mail.rc
set [email protected]
set smtp=mail.com.cn set [email protected] set smtp-auth-password=1234567890 set smtp-auth=login