MallLogic

  1. <?php
  2. namespace console\logic;
  3. // ......
  4. /**
  5. *
  6. * @author vogin
  7. *
  8. */
  9. class MallLogic
  10. {
  11. // 20 天
  12. const CHECK_DURATION = 1728000;
  13. /**
  14. * 获取https证书信息
  15. *
  16. * @return boolean|array
  17. */
  18. public static function getHttpsCertInfo ($domain)
  19. {
  20. $context = stream_context_create([
  21. 'ssl' => [
  22. 'verify_host' => false,
  23. 'verify_peer' => false,
  24. 'verify_peer_name' => false,
  25. 'capture_peer_cert' => true,
  26. 'capture_peer_cert_chain' => true
  27. ]
  28. ]);
  29. $client = stream_socket_client("ssl://" . $domain . ":443", $errno, $errstr, 3, STREAM_CLIENT_CONNECT, $context);
  30. if(! $client)
  31. {
  32. return false;
  33. }
  34. $params = stream_context_get_params($client);
  35. $cert = $params['options']['ssl']['peer_certificate'];
  36. $cert_info = openssl_x509_parse($cert);
  37. fclose($client);
  38. return $cert_info;
  39. }
  40. /**
  41. * 检测 https证书状态
  42. */
  43. public static function checkHttpsDomain ()
  44. {
  45. // [此处的 $domains 从数据库获取对应的内容]
  46. $domains = [];
  47. $error_message = '';
  48. $nodes = [];
  49. foreach($domains as $data)
  50. {
  51. $label = $data['label'];
  52. $domain = $data['value'];
  53. try
  54. {
  55. // 获取证书信息
  56. $cert_info = static::getHttpsCertInfo($domain);
  57. if(empty($cert_info))
  58. {
  59. throw new UserException('未获取到信息');
  60. }
  61. // 获取成功
  62. $validTo_time_t = $cert_info['validTo_time_t'];
  63. $validTo_time_d = date('Y-m-d H:i:s', $validTo_time_t);
  64. // 将内容转为节点
  65. $node = new HttpsCheckNode();
  66. $node->label = $label;
  67. $node->domain = $domain;
  68. $node->expire = $validTo_time_d;
  69. $node->expireStamp = $validTo_time_t;
  70. $nodes[] = $node;
  71. }
  72. catch(Exception $e)
  73. {
  74. // 获取失败
  75. $error_message .= $label . '[ ' . $domain . ' ]证书检测异常:' . $e->getMessage() . PHP_EOL;
  76. }
  77. }
  78. // 排序
  79. // 自定义排序
  80. usort($nodes, function (HttpsCheckNode $node1, HttpsCheckNode $node2)
  81. {
  82. if($node1->expireStamp == $node2->expireStamp)
  83. {
  84. return 0;
  85. }
  86. // 正序 > 从小到大
  87. return ($node1->expireStamp < $node2->expireStamp) ? - 1 : 1;
  88. });
  89. $msg = '';
  90. // 当前时间
  91. $now = time();
  92. /**
  93. *
  94. * @var HttpsCheckNode $node
  95. */
  96. foreach($nodes as $node)
  97. {
  98. $expire_msg = '';
  99. // 判断是否快到期
  100. if($node->expireStamp - $now < self::CHECK_DURATION)
  101. {
  102. $expire_msg = ',还有' . intval(($node->expireStamp - $now) / 86400) . '天过期';
  103. }
  104. $msg .= $node->label . '[ ' . $node->domain . ' ]的https证书有效期至(' . $node->expire . ')' . $expire_msg . PHP_EOL;
  105. }
  106. // 组装错误信息
  107. $msg .= $error_message;
  108. $res = DingDingLogic::send("[Https证书检测系统]提示:\n" . $msg);
  109. echo '钉钉发送结果:' . $res . PHP_EOL;
  110. }
  111. }

DingDingLogic

  1. <?php
  2. namespace console\logic;
  3. /**
  4. * 钉钉内容自定义发送内容
  5. *
  6. * @author vogin
  7. *
  8. */
  9. class DingDingLogic
  10. {
  11. // 此处改为钉钉的webhook地址
  12. const WEBHOOK = '.....';
  13. /**
  14. * 发送message
  15. *
  16. * @param string $msg
  17. * 信息内容
  18. */
  19. public static function send ($msg)
  20. {
  21. $data = array(
  22. 'msgtype' => 'text',
  23. 'text' => array(
  24. 'content' => $msg
  25. )
  26. );
  27. $data_str = json_encode($data);
  28. return static::post(self::WEBHOOK, $data_str);
  29. }
  30. /**
  31. * 给钉钉发送post内容
  32. *
  33. * @param string $url
  34. * 接口地址
  35. * @param string $post_str
  36. * json的post内容
  37. * @return mixed
  38. */
  39. public static function post ($url, $post_str)
  40. {
  41. $ch = curl_init();
  42. curl_setopt($ch, CURLOPT_URL, $url);
  43. curl_setopt($ch, CURLOPT_POST, 1);
  44. curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 5);
  45. curl_setopt($ch, CURLOPT_HTTPHEADER, array(
  46. 'Content-Type: application/json;charset=utf-8'
  47. ));
  48. curl_setopt($ch, CURLOPT_POSTFIELDS, $post_str);
  49. curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  50. // 线下环境不用开启curl证书验证, 未调通情况可尝试添加该代码
  51. // curl_setopt ($ch, CURLOPT_SSL_VERIFYHOST, 0);
  52. // curl_setopt ($ch, CURLOPT_SSL_VERIFYPEER, 0);
  53. $data = curl_exec($ch);
  54. curl_close($ch);
  55. return $data;
  56. }
  57. }