123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304 |
- <?php
- error_reporting(E_ALL^E_NOTICE^E_WARNING^E_DEPRECATED);
- $myhost = $_SERVER['HTTP_HOST'];
- //判断主机,不允许公网访问
- if(($myhost != 'localhost') && ($myhost != '127.0.0.1')) {
- echo "不允许公网访问!";
- exit;
- }
-
- require_once 'medoo.php';
- include_once( '../config.php' );
- $database = new medoo([
- 'database_type' => 'sqlite',
- 'database_file' => '../data/nssl.db3']);
- //设置证书路径
- //$ssldir = "../ssl/";
- //设置配置文件路径
- //$confdir = "D:/wwwroot/nssl/conf/";
- //$confdir = "../vhost/";
-
- //获取域名
- $domain = $_POST['domain'];
- //替换http and https
- $domain = str_replace("http://","",$domain);
- $domain = str_replace("https://","",$domain);
- $crt = $_POST['crt']; //获取证书内容
- $key = $_POST['key']; //获取证书私钥
- $note = $_POST['note']; //获取证书备注
- $id = $_POST['id']; //获取ID
- $cerid = $_GET['cerid']; //获取证书ID,更新证书时使用
- $sslid = $_POST['sslid']; //获取证书id,更新站点时使用
- $https = $_POST['https']; //是否强制https
- //获取类型
- $type = $_GET['type'];
- switch ( $type )
- {
- //添加证书
- case 'add_cert':
- check($domain,"domain");
- check($crt,"crt");
- check($key,"key");
- check($note,"note");
- //查询域名是否存在
- $count = $database->count("s_cert", [
- "domain" => $domain
- ]);
- if($count >= 1) {
- echo "添加失败,域名已存在!";
- exit;
- }
- $time = date('Y-m-d',time());
- $last_user_id = $database->insert("s_cert", [
- "domain" => $domain,
- "crt" => $crt,
- "key" => $key,
- "note" => $note,
- "time" => $time
- ]);
- if($last_user_id) {
- $mycrt = fopen($ssldir.$domain.".crt", "w") or die("Unable to open file!");
- fwrite($mycrt, $crt);
- $mykey = fopen($ssldir.$domain.".key", "w") or die("Unable to open file!");
- fwrite($mykey, $key);
- fclose($mycrt);
- fclose($mykey);
- echo '添加成功!';
- }
- break;
- //更新证书
- case 'update_ssl':
- check($domain,"domain");
- check($crt,"crt");
- check($key,"key");
- check($note,"note");
-
- $time = date('Y-m-d',time());
- $last_user_id = $database->update("s_cert", [
- "crt" => $crt,
- "key" => $key,
- "note" => $note,
- "time" => $time
- ],["id" => $cerid]);
- if($last_user_id) {
- $mycrt = fopen($ssldir.$domain.".crt", "w") or die("Unable to open file!");
- fwrite($mycrt, $crt);
- $mykey = fopen($ssldir.$domain.".key", "w") or die("Unable to open file!");
- fwrite($mykey, $key);
- fclose($mycrt);
- fclose($mykey);
- echo '更新成功!';
- }
- break;
- //显示证书
- case 'list_ssl':
- $datas = $database->select("s_cert", [
- "id",
- "domain",
- "time",
- "note"
- ]);
- foreach($datas as $data)
- {
- $sslid = $data['id'];
- echo "<tr id ='ssl$sslid'>";
- echo "<td>".$data['domain']."</td>";
- echo "<td>".$data['time']."</td>";
- echo "<td>".$data['note']."</td>";
- echo "<td><a href = 'editssl.php?type=ssl&cerid=$sslid' class='layui-btn layui-btn-normal layui-btn-xs'>编辑</a><a href = 'javascript:;' class='layui-btn layui-btn-danger layui-btn-xs' onclick = 'delssl(\"$sslid\")'>"."删除</a></td>";
- echo "</tr>";
- }
- break;
- //选择证书
- case 'select_ssl':
- $datas = $database->select("s_cert", [
- "id",
- "domain",
- "time",
- "note"
- ]);
-
- echo "<option value=''></option>";
- foreach($datas as $data)
- {
- $sslid = $data['id'];
- $ssldomain = $data['domain'];
- $sslnote = $data['note'];
- echo "<option value='$sslid'>$ssldomain($sslnote)</option>";
- }
- break;
- //删除证书
- case 'del_ssl':
- $delcert = $database->get("s_cert",["domain"],["id" => $id]);
- //查询证书是否部署站点
- $selectsite = $database->get("s_site",["id"],["sslid" => $id]);
- if($selectsite) {
- echo "该证书已部署到站点,无法删除!";
- exit;
- }
- //仅删除数据库
- $redel = $database->delete("s_cert",["id" => $id]);
- if($redel) {
- unlink($ssldir.$delcert['domain'].".crt");
- unlink($ssldir.$delcert['domain'].".key");
- echo 'ok';
- }
- break;
- //添加站点
- case 'add_site':
- check($domain,"domain");
- check($sslid,"sslid");
- //查询域名是否存在
- $count = $database->count("s_site", [
- "domain" => $domain
- ]);
- if($count >= 1) {
- echo "添加失败,域名已存在!";
- exit;
- }
- $time = date('Y-m-d',time());
- $last_user_id = $database->insert("s_site", [
- "domain" => $domain,
- "sslid" => $sslid,
- "https" => $https,
- "time" => $time
- ]);
- if($last_user_id) {
- //查询证书
- $cert = $database->get("s_cert",["id","domain","note"],["id" => $sslid]);
- $certdir = $ssldir.$cert['domain']; //设置证书路径
- $content = file_get_contents("../data/temp.conf");
- $ip = file_get_contents("https://api-ipv4.ip.sb/ip");
- $ip = str_replace("\n","",$ip);
- //替换监听地址
- $content = str_replace("192.168.1.1",$ip,$content);
- //替换域名
- $content = str_replace("domain.com",$domain,$content);
- //替换证书路径
- $content = str_replace("mycer",$certdir,$content);
- //如果强制https
- if($https == 1) {
- $content = str_replace("test301.com",$domain,$content);
-
- }
- //生成配置文件
- $myconf = fopen($confdir.$domain.".conf", "w") or die("Unable to open file!");
- fwrite($myconf, $content);
- fclose($myconf);
- echo '添加成功!';
- }
- break;
- //显示站点
- case 'list_site':
- $datas = $database->select("s_site", [
- "id",
- "domain",
- "sslid",
- "time",
- "https"
- ]);
-
- foreach($datas as $data)
- {
- if($data['https'] == 1){
- $https = "<img src = 'static/yes.png'></img> 是";
- }
- else {
- $https = "<img src = 'static/no.png'></img> 否";
- }
- $id = $data['id'];
- $sslid = $data['sslid'];
-
- //查询证书
- $cert = $database->get("s_cert",["id","domain","note"],["id" => $sslid]);
- $ressl = $cert['domain']."(".$cert['note'].")";
-
- echo "<tr id ='ssl$id'>";
- echo "<td>".$data['domain']."</td>";
- echo "<td>".$ressl."</td>";
- echo "<td>".$https."</td>";
- echo "<td>".$data['time']."</td>";
- echo "<td><a href = 'javascript:;' class='layui-btn layui-btn-danger layui-btn-xs' onclick = 'delsite(\"$id\")'>"."删除</a></td>";
- echo "</tr>";
- }
- break;
- //删除站点
- case 'del_site':
- //先查询配置文件
- $selectconf = $database->get("s_site",["domain"],["id" => $id]);
- //仅删除数据库
- $redel = $database->delete("s_site",["id" => $id]);
- if($redel) {
- //删除配置文件
- unlink($confdir.$selectconf['domain'].".conf");
- //print_r($confdir.$selectconf['domain']);
- echo 'ok';
- }
- break;
- default:
- ;
- break;
- }
- ?>
- <?php
- function check($parameter,$category){
- switch ( $category )
- {
- case 'domain':
- if($parameter == '') {
- echo "域名不能为空!";
- exit;
- }
- //正则匹配
- //$rule = '/.*\.[a-zA-Z]*$/';
- $rule = '/^[0-9a-zA-Z-\.]+\.[a-zA-Z]+$/';
- if(!preg_match($rule,$parameter)) {
- echo "域名格式不正确!";
- exit;
- }
-
- break;
- case 'crt':
- if($parameter == '') {
- echo "请填写证书内容!(.cer and .crt)";
- exit;
- }
- $iscrt = strpos($parameter,"BEGIN CERTIFICATE");
- if(!$iscrt) {
- echo "证书格式不对!";
- exit;
- }
- break;
- case 'key':
- if($parameter == '') {
- echo "请填写私钥!(.key)";
- exit;
- }
- $iskey = strpos($parameter,"PRIVATE KEY");
- if(!$iskey) {
- echo "密钥(key)格式不对!";
- exit;
- }
- break;
- case 'note':
- $strlen = strlen($parameter);
- if($strlen > 20) {
- echo "备注不能超过20个字符";
- exit;
- }
- break;
- case 'sslid':
- if($parameter == '') {
- echo "请选择证书!";
- exit;
- }
- default:
- ;
- break;
- }
- }
- ?>
|