日度归档:2010年8月10日

通过php web方式更新服务器svn[新]

主要的功能为二点:1、签出svn,2、更新svn;

需事前注意:apache或是nginx+php 用户要有svn程序执行的权限,如果没有的话,就需要通过visudo进行sudo授权,让apache用户(例www-data)可以执行svn程序

visudo文件最后加上:(意思:sudo无需输入密码可执行/usr/bin/svn命令)
www-data ALL = NOPASSWD:/usr/bin/svn

界面:

1.png

2.png

3.png

4.png

 

注意:如果通过visudo的方式让apache等执行svn命令,则下面的php代码中的svn 需改成sudo svn …..

<?php
//author 冻番茄 www.phpd.cn
session_start();
//echo $_SERVER['REQUEST_URI'];
//print_r($_SERVER);
//location.reload();

function update(){
	echo '<h2>svn update ...</h2>';
	//echo "svn up --username {$_SESSION['username']} --password {$_SESSION['password']} ";
	$hl=popen("svn up --username {$_SESSION['username']} --password {$_SESSION['password']} ","r");
	$read=stream_get_contents($hl);
	echo "<pre>";
	printf($read);
	echo "</pre>";
	pclose($hl);
}

function co($url,$username,$password){
	echo '<h2>svn checkout ...</h2>';
	$command="svn co $url --username {$_SESSION['username']} --password {$_SESSION['password']} ".$_SERVER['DOCUMENT_ROOT'];
	exec($command);
	echo 'checkout Success!';
}
if(isset($_POST['sub']) && $_POST['sub']=="登录"){
	if($_POST['password']=="7279915"){
		$_SESSION['login']=true;
		echo '<script>location.href="'.$_SERVER['PHP_SELF'].'";</script>';
	}else{
		echo '<script>alert("PASSWORD ERROR");location.href="'.$_SERVER['PHP_SELF'].'";</script>';
	}
	exit;
}elseif(isset($_POST['sub']) && $_POST['sub']=='执行'){
	if(empty($_POST['username']) || empty($_POST['password'])){
		echo '<script>alert("SVN服务器的用户名或是密码不能为空");location.href="?a=update";</script>';
	}else{
		$_SESSION['username']=$_POST['username'];
		$_SESSION['password']=$_POST['password'];
		echo '<script>location.href="?a=update";</script>';
	}
	exit;
}

if(isset($_GET['a']) && $_GET['a']=='logout'){
	unset($_SESSION['login']);
	unset($_SESSION['username']);
	unset($_SESSION['password']);
	echo '<script>location.href="?a=co";</script>';
	exit;
}

?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="content-type" content="text/html; charset=utf-8" />
<title>svn更新 | Author:常州亚萌 冻番茄</title>
<style>
*{ margin:0px; padding:0px; font-size:12px;font-family:Arial,Helvetica,sans-serif}
a:link,a:visited,a:active{ color:#5A5A5A; text-decoration:none;font-size:14px;}
a:hover{ color:#33A9D0; text-decoration:none;}
.login{margin:0px auto;margin-top:200px;border:1px solid #666600;width:300px;height:90px;background:#FFFFD0}
input{border:1px solid #666600;background:#fff;width:160px;padding:3px;}
.in1{border:1px solid #666600;background:#fff;width:120px;padding:2px;font-size:9px;}
.run{margin:20px auto;;width:700px;height:90px}
h2{padding-bottom:8px;}
</style>
</head>
<body>
<?php
if(!isset($_SESSION['login'])){
?>
<div class="login">
	<FORM METHOD=POST ACTION="">
	<div style="font-size:14px;margin:10px;border-bottom:1px solid #666600;padding-bottom:10px;text-align:center">svn更新操作 [管理员登录]</div>
	<div style="margin:10px;clear:both;text-align:center"><INPUT TYPE="password" NAME="password">&nbsp;&nbsp; <INPUT TYPE="submit" name="sub" value="登录" style="width:70px;height:25px;"></div>
	</FORM>
</div>
<?php
}else{	
?>
<div class="run">
	<div style="font-size:14px;margin:10px;border-bottom:1px solid #666600;padding-bottom:10px;"> [<a href="?a=update">svn刷新/upload</a>] [<a href="?a=co">svn签出/checkout</a>]  [<a href="/svn.php?a=logout">退出</a>]</div>
	<div style="margin:10px;clear:both;">
		<?php
			if(isset($_GET['a']) && $_GET['a']=='update'){
				if(!isset($_SESSION['username']) || !isset($_SESSION['password'])){
					echo '<FORM METHOD=POST ACTION="">
							<h2>SVN更新操作</h2>
							<div>svn用户名:<INPUT TYPE="text" NAME="username" class="in1">
								svn密码:<INPUT TYPE="password" NAME="password" class="in1">
								<INPUT TYPE="submit" name="sub" value="执行" style="width:70px;background:#eee" class="in1">
							</div>
						</FORM>';
				}else{
					update();
				}
			}elseif(isset($_GET['a']) && $_GET['a']=='co'){
				if(isset($_POST['sub']) && $_POST['sub']=='签出'){
					if(empty($_POST['username']) || empty($_POST['password']) || empty($_POST['svn']) ){
						echo '<script>alert("签出的svn地址、用户名、密码不能为空");location.href="?a=co";</script>';
					}else{
						$_SESSION['username']=$_POST['username'];
						$_SESSION['password']=$_POST['password'];
						co($_POST['svn']);
					}
					exit;
				}
				echo '<FORM METHOD=POST ACTION="">
							<h2>SVN签出操作 <注:已签出的项目,第二次签出无效!></h2>
							<div>svn地址:<INPUT TYPE="text" NAME="svn" class="in1" value="svn://">
								svn用户名:<INPUT TYPE="text" NAME="username" class="in1">
								svn密码:<INPUT TYPE="password" NAME="password" class="in1">
								<INPUT TYPE="submit" name="sub" value="签出" style="width:70px;background:#eee" class="in1">
							</div>
						</FORM>';
			}else{
				echo '欢迎进入svn更新程序!Author:冻番茄<br />';
			}
		?>
		<div style="clear:both;height:10px"></div>
	</div>
	
</div>
<?php
}
?>
</body>
</html>

源代码下载:

svn.php