'sqlite', 'database_file' => '../data/nssl.db3']); //获取域名 $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 $siteid = $_POST['siteid']; //获取站点ID $cerid = $_GET['cerid']; //获取证书ID,更新证书时使用 $sslid = $_POST['sslid']; //获取证书id,更新站点时使用 $https = $_POST['https']; //是否强制https //当前时间 $time = date('Y-m-d',time()); //获取类型 $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; } $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"); $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 ""; echo "".$data['domain'].""; echo "".$data['time'].""; echo "".$data['note'].""; echo "编辑"."删除"; echo ""; } break; //选择证书 case 'select_ssl': $datas = $database->select("s_cert", [ "id", "domain", "time", "note" ]); echo ""; foreach($datas as $data) { $sslid = $data['id']; $ssldomain = $data['domain']; $sslnote = $data['note']; echo ""; } 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; } $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 '添加成功!'; //reload(); } break; //显示站点 case 'list_site': $datas = $database->select("s_site", [ "id", "domain", "sslid", "time", "https" ]); foreach($datas as $data) { if($data['https'] == 1){ $https = " 是"; } else { $https = " 否"; } $id = $data['id']; $sslid = $data['sslid']; //查询证书 $cert = $database->get("s_cert",["id","domain","note"],["id" => $sslid]); $ressl = $cert['domain']."(".$cert['note'].")"; echo ""; echo "".$data['domain'].""; echo "".$ressl.""; echo "".$https.""; echo "".$data['time'].""; echo "设置 "."删除"; echo ""; } 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; //更新站点 case 'update_site': $upsite = $database->update("s_site",["https" => $https,"time" => $time],["id" => $siteid]); //如果更新成功 if($upsite) { $sitecontent = file_get_contents("$confdir".$domain.".conf"); if($https == 1) { $sitecontent = str_replace("test301.com",$domain,$sitecontent); } if($https == 0) { $sitecontent = str_replace("server_name ".$domain,"server_name test301.com",$sitecontent); $sitecontent = str_replace("https://".$domain,"https://test301.com",$sitecontent); } $mysite = fopen($confdir.$domain.".conf", "w") or die("Unable to open file!"); fwrite($mysite, $sitecontent); fclose($mysite); echo '更新成功!'; //reload(); } else { echo '未知错误!'; } break; default: ; break; } ?> 20) { echo "备注不能超过20个字符"; exit; } break; case 'sslid': if($parameter == '') { echo "请选择证书!"; exit; } default: ; break; } } ?>