method.php 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304
  1. <?php
  2. error_reporting(E_ALL^E_NOTICE^E_WARNING^E_DEPRECATED);
  3. $myhost = $_SERVER['HTTP_HOST'];
  4. //判断主机,不允许公网访问
  5. if(($myhost != 'localhost') && ($myhost != '127.0.0.1')) {
  6. echo "不允许公网访问!";
  7. exit;
  8. }
  9. require_once 'medoo.php';
  10. include_once( '../config.php' );
  11. $database = new medoo([
  12. 'database_type' => 'sqlite',
  13. 'database_file' => '../data/nssl.db3']);
  14. //设置证书路径
  15. //$ssldir = "../ssl/";
  16. //设置配置文件路径
  17. //$confdir = "D:/wwwroot/nssl/conf/";
  18. //$confdir = "../vhost/";
  19. //获取域名
  20. $domain = $_POST['domain'];
  21. //替换http and https
  22. $domain = str_replace("http://","",$domain);
  23. $domain = str_replace("https://","",$domain);
  24. $crt = $_POST['crt']; //获取证书内容
  25. $key = $_POST['key']; //获取证书私钥
  26. $note = $_POST['note']; //获取证书备注
  27. $id = $_POST['id']; //获取ID
  28. $cerid = $_GET['cerid']; //获取证书ID,更新证书时使用
  29. $sslid = $_POST['sslid']; //获取证书id,更新站点时使用
  30. $https = $_POST['https']; //是否强制https
  31. //获取类型
  32. $type = $_GET['type'];
  33. switch ( $type )
  34. {
  35. //添加证书
  36. case 'add_cert':
  37. check($domain,"domain");
  38. check($crt,"crt");
  39. check($key,"key");
  40. check($note,"note");
  41. //查询域名是否存在
  42. $count = $database->count("s_cert", [
  43. "domain" => $domain
  44. ]);
  45. if($count >= 1) {
  46. echo "添加失败,域名已存在!";
  47. exit;
  48. }
  49. $time = date('Y-m-d',time());
  50. $last_user_id = $database->insert("s_cert", [
  51. "domain" => $domain,
  52. "crt" => $crt,
  53. "key" => $key,
  54. "note" => $note,
  55. "time" => $time
  56. ]);
  57. if($last_user_id) {
  58. $mycrt = fopen($ssldir.$domain.".crt", "w") or die("Unable to open file!");
  59. fwrite($mycrt, $crt);
  60. $mykey = fopen($ssldir.$domain.".key", "w") or die("Unable to open file!");
  61. fwrite($mykey, $key);
  62. fclose($mycrt);
  63. fclose($mykey);
  64. echo '添加成功!';
  65. }
  66. break;
  67. //更新证书
  68. case 'update_ssl':
  69. check($domain,"domain");
  70. check($crt,"crt");
  71. check($key,"key");
  72. check($note,"note");
  73. $time = date('Y-m-d',time());
  74. $last_user_id = $database->update("s_cert", [
  75. "crt" => $crt,
  76. "key" => $key,
  77. "note" => $note,
  78. "time" => $time
  79. ],["id" => $cerid]);
  80. if($last_user_id) {
  81. $mycrt = fopen($ssldir.$domain.".crt", "w") or die("Unable to open file!");
  82. fwrite($mycrt, $crt);
  83. $mykey = fopen($ssldir.$domain.".key", "w") or die("Unable to open file!");
  84. fwrite($mykey, $key);
  85. fclose($mycrt);
  86. fclose($mykey);
  87. echo '更新成功!';
  88. }
  89. break;
  90. //显示证书
  91. case 'list_ssl':
  92. $datas = $database->select("s_cert", [
  93. "id",
  94. "domain",
  95. "time",
  96. "note"
  97. ]);
  98. foreach($datas as $data)
  99. {
  100. $sslid = $data['id'];
  101. echo "<tr id ='ssl$sslid'>";
  102. echo "<td>".$data['domain']."</td>";
  103. echo "<td>".$data['time']."</td>";
  104. echo "<td>".$data['note']."</td>";
  105. 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>";
  106. echo "</tr>";
  107. }
  108. break;
  109. //选择证书
  110. case 'select_ssl':
  111. $datas = $database->select("s_cert", [
  112. "id",
  113. "domain",
  114. "time",
  115. "note"
  116. ]);
  117. echo "<option value=''></option>";
  118. foreach($datas as $data)
  119. {
  120. $sslid = $data['id'];
  121. $ssldomain = $data['domain'];
  122. $sslnote = $data['note'];
  123. echo "<option value='$sslid'>$ssldomain($sslnote)</option>";
  124. }
  125. break;
  126. //删除证书
  127. case 'del_ssl':
  128. $delcert = $database->get("s_cert",["domain"],["id" => $id]);
  129. //查询证书是否部署站点
  130. $selectsite = $database->get("s_site",["id"],["sslid" => $id]);
  131. if($selectsite) {
  132. echo "该证书已部署到站点,无法删除!";
  133. exit;
  134. }
  135. //仅删除数据库
  136. $redel = $database->delete("s_cert",["id" => $id]);
  137. if($redel) {
  138. unlink($ssldir.$delcert['domain'].".crt");
  139. unlink($ssldir.$delcert['domain'].".key");
  140. echo 'ok';
  141. }
  142. break;
  143. //添加站点
  144. case 'add_site':
  145. check($domain,"domain");
  146. check($sslid,"sslid");
  147. //查询域名是否存在
  148. $count = $database->count("s_site", [
  149. "domain" => $domain
  150. ]);
  151. if($count >= 1) {
  152. echo "添加失败,域名已存在!";
  153. exit;
  154. }
  155. $time = date('Y-m-d',time());
  156. $last_user_id = $database->insert("s_site", [
  157. "domain" => $domain,
  158. "sslid" => $sslid,
  159. "https" => $https,
  160. "time" => $time
  161. ]);
  162. if($last_user_id) {
  163. //查询证书
  164. $cert = $database->get("s_cert",["id","domain","note"],["id" => $sslid]);
  165. $certdir = $ssldir.$cert['domain']; //设置证书路径
  166. $content = file_get_contents("../data/temp.conf");
  167. $ip = file_get_contents("https://api-ipv4.ip.sb/ip");
  168. $ip = str_replace("\n","",$ip);
  169. //替换监听地址
  170. $content = str_replace("192.168.1.1",$ip,$content);
  171. //替换域名
  172. $content = str_replace("domain.com",$domain,$content);
  173. //替换证书路径
  174. $content = str_replace("mycer",$certdir,$content);
  175. //如果强制https
  176. if($https == 1) {
  177. $content = str_replace("test301.com",$domain,$content);
  178. }
  179. //生成配置文件
  180. $myconf = fopen($confdir.$domain.".conf", "w") or die("Unable to open file!");
  181. fwrite($myconf, $content);
  182. fclose($myconf);
  183. echo '添加成功!';
  184. }
  185. break;
  186. //显示站点
  187. case 'list_site':
  188. $datas = $database->select("s_site", [
  189. "id",
  190. "domain",
  191. "sslid",
  192. "time",
  193. "https"
  194. ]);
  195. foreach($datas as $data)
  196. {
  197. if($data['https'] == 1){
  198. $https = "<img src = 'static/yes.png'></img> 是";
  199. }
  200. else {
  201. $https = "<img src = 'static/no.png'></img> 否";
  202. }
  203. $id = $data['id'];
  204. $sslid = $data['sslid'];
  205. //查询证书
  206. $cert = $database->get("s_cert",["id","domain","note"],["id" => $sslid]);
  207. $ressl = $cert['domain']."(".$cert['note'].")";
  208. echo "<tr id ='ssl$id'>";
  209. echo "<td>".$data['domain']."</td>";
  210. echo "<td>".$ressl."</td>";
  211. echo "<td>".$https."</td>";
  212. echo "<td>".$data['time']."</td>";
  213. echo "<td><a href = 'javascript:;' class='layui-btn layui-btn-danger layui-btn-xs' onclick = 'delsite(\"$id\")'>"."删除</a></td>";
  214. echo "</tr>";
  215. }
  216. break;
  217. //删除站点
  218. case 'del_site':
  219. //先查询配置文件
  220. $selectconf = $database->get("s_site",["domain"],["id" => $id]);
  221. //仅删除数据库
  222. $redel = $database->delete("s_site",["id" => $id]);
  223. if($redel) {
  224. //删除配置文件
  225. unlink($confdir.$selectconf['domain'].".conf");
  226. //print_r($confdir.$selectconf['domain']);
  227. echo 'ok';
  228. }
  229. break;
  230. default:
  231. ;
  232. break;
  233. }
  234. ?>
  235. <?php
  236. function check($parameter,$category){
  237. switch ( $category )
  238. {
  239. case 'domain':
  240. if($parameter == '') {
  241. echo "域名不能为空!";
  242. exit;
  243. }
  244. //正则匹配
  245. //$rule = '/.*\.[a-zA-Z]*$/';
  246. $rule = '/^[0-9a-zA-Z-\.]+\.[a-zA-Z]+$/';
  247. if(!preg_match($rule,$parameter)) {
  248. echo "域名格式不正确!";
  249. exit;
  250. }
  251. break;
  252. case 'crt':
  253. if($parameter == '') {
  254. echo "请填写证书内容!(.cer and .crt)";
  255. exit;
  256. }
  257. $iscrt = strpos($parameter,"BEGIN CERTIFICATE");
  258. if(!$iscrt) {
  259. echo "证书格式不对!";
  260. exit;
  261. }
  262. break;
  263. case 'key':
  264. if($parameter == '') {
  265. echo "请填写私钥!(.key)";
  266. exit;
  267. }
  268. $iskey = strpos($parameter,"PRIVATE KEY");
  269. if(!$iskey) {
  270. echo "密钥(key)格式不对!";
  271. exit;
  272. }
  273. break;
  274. case 'note':
  275. $strlen = strlen($parameter);
  276. if($strlen > 20) {
  277. echo "备注不能超过20个字符";
  278. exit;
  279. }
  280. break;
  281. case 'sslid':
  282. if($parameter == '') {
  283. echo "请选择证书!";
  284. exit;
  285. }
  286. default:
  287. ;
  288. break;
  289. }
  290. }
  291. ?>