method.php 8.6 KB

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