luolongfei /
freenom
| 1 | <?php |
||||
| 2 | /** |
||||
| 3 | * Freenom域名自动续期 |
||||
| 4 | * @author mybsdc <[email protected]> |
||||
| 5 | * @date 2018/8/9 |
||||
| 6 | * @time 10:05 |
||||
| 7 | */ |
||||
| 8 | |||||
| 9 | error_reporting(E_ERROR); |
||||
| 10 | ini_set('display_errors', 1); |
||||
| 11 | set_time_limit(0); |
||||
| 12 | |||||
| 13 | define('IS_CLI', PHP_SAPI === 'cli' ? true : false); |
||||
| 14 | define('DS', DIRECTORY_SEPARATOR); |
||||
| 15 | define('VENDOR_PATH', realpath('vendor') . DS); |
||||
| 16 | |||||
| 17 | date_default_timezone_set('Asia/Shanghai'); |
||||
| 18 | |||||
| 19 | /** |
||||
| 20 | * 自定义错误处理 |
||||
| 21 | */ |
||||
| 22 | register_shutdown_function('customize_error_handler'); |
||||
| 23 | function customize_error_handler() |
||||
| 24 | { |
||||
| 25 | if (!is_null($error = error_get_last())) { |
||||
|
0 ignored issues
–
show
introduced
by
Loading history...
|
|||||
| 26 | system_log($error); |
||||
| 27 | |||||
| 28 | $response = [ |
||||
| 29 | 'STATUS' => 9, |
||||
| 30 | 'MESSAGE_ARRAY' => array( |
||||
| 31 | array( |
||||
| 32 | 'MESSAGE' => '程序执行出错,请稍后再试。' |
||||
| 33 | ) |
||||
| 34 | ), |
||||
| 35 | 'SYSTEM_DATE' => date('Y-m-d H:i:s') |
||||
| 36 | ]; |
||||
| 37 | |||||
| 38 | header('Content-Type: application/json'); |
||||
| 39 | |||||
| 40 | echo json_encode($response); |
||||
| 41 | } |
||||
| 42 | } |
||||
| 43 | |||||
| 44 | /** |
||||
| 45 | * 自定义异常处理 |
||||
| 46 | * 设置默认的异常处理程序,用于没有用 try/catch 块来捕获的异常。 在 exception_handler 调用后异常会中止。由于要实现在 try/catch 块 |
||||
| 47 | * 每次抛出异常后,在catch块自动发送一封通知邮件的功能,而使用PHPMailer发送邮件的流程本身也可能抛出异常,要在catch代码块中捕获异常就意味着 |
||||
| 48 | * 要嵌套try/catch,而我并不想嵌套try/catch代码块,因此自定义此异常处理函数。 |
||||
| 49 | */ |
||||
| 50 | set_exception_handler('exception_handler'); |
||||
| 51 | function exception_handler($e) |
||||
| 52 | { |
||||
| 53 | system_log(sprintf('#%d - %s', $e->getCode(), $e->getMessage())); |
||||
| 54 | } |
||||
| 55 | |||||
| 56 | /** |
||||
| 57 | * 记录程序日志 |
||||
| 58 | * @param array|string $logContent 日志内容 |
||||
| 59 | * @param string $mark LOG | ERROR | WARNING 日志标志 |
||||
| 60 | */ |
||||
| 61 | function system_log($logContent, $mark = 'LOG') |
||||
| 62 | { |
||||
| 63 | try { |
||||
| 64 | $logPath = __DIR__ . '/logs/' . date('Y') . '/' . date('m') . '/'; |
||||
| 65 | $logFile = $logPath . date('d') . '.php'; |
||||
| 66 | |||||
| 67 | if (!is_dir($logPath)) { |
||||
| 68 | mkdir($logPath, 0777, true); |
||||
| 69 | chmod($logPath, 0777); |
||||
| 70 | } |
||||
| 71 | |||||
| 72 | $handle = fopen($logFile, 'a'); // 文件不存在则自动创建 |
||||
| 73 | |||||
| 74 | if (!filesize($logFile)) { |
||||
| 75 | fwrite($handle, "<?php defined('VENDOR_PATH') or die('No direct script access allowed.'); ?>" . PHP_EOL . PHP_EOL); |
||||
|
0 ignored issues
–
show
It seems like
$handle can also be of type false; however, parameter $handle of fwrite() does only seem to accept resource, maybe add an additional type check?
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
Loading history...
|
|||||
| 76 | chmod($logFile, 0666); |
||||
| 77 | } |
||||
| 78 | |||||
| 79 | fwrite($handle, $mark . ' - ' . date('Y-m-d H:i:s') . ' --> ' . (IS_CLI ? 'CLI' : 'URI: ' . $_SERVER['REQUEST_URI'] . PHP_EOL . 'REMOTE_ADDR: ' . $_SERVER['REMOTE_ADDR'] . PHP_EOL . 'SERVER_ADDR: ' . $_SERVER['SERVER_ADDR']) . PHP_EOL . (is_string($logContent) ? $logContent : var_export($logContent, true)) . PHP_EOL); // CLI模式下,$_SERVER中几乎无可用值 |
||||
| 80 | |||||
| 81 | fclose($handle); |
||||
|
0 ignored issues
–
show
It seems like
$handle can also be of type false; however, parameter $handle of fclose() does only seem to accept resource, maybe add an additional type check?
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
Loading history...
|
|||||
| 82 | } catch (\Exception $e) { |
||||
| 83 | // do nothing |
||||
| 84 | } |
||||
| 85 | } |
||||
| 86 | |||||
| 87 | require VENDOR_PATH . 'autoload.php'; |
||||
| 88 | require 'llfexception.php'; |
||||
| 89 | |||||
| 90 | use Curl\Curl; |
||||
| 91 | use PHPMailer\PHPMailer\PHPMailer; |
||||
| 92 | use PHPMailer\PHPMailer\Exception; |
||||
|
0 ignored issues
–
show
This use statement conflicts with another class in this namespace,
Exception. Consider defining an alias.
Let?s assume that you have a directory layout like this: .
|-- OtherDir
| |-- Bar.php
| `-- Foo.php
`-- SomeDir
`-- Foo.php
and let?s assume the following content of // Bar.php
namespace OtherDir;
use SomeDir\Foo; // This now conflicts the class OtherDir\Foo
If both files PHP Fatal error: Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.php
However, as // Bar.php
namespace OtherDir;
use SomeDir\Foo as SomeDirFoo; // There is no conflict anymore.
Loading history...
|
|||||
| 93 | |||||
| 94 | class FREENOM |
||||
| 95 | { |
||||
| 96 | // FREENOM登录地址 |
||||
| 97 | const LOGIN_URL = 'https://my.freenom.com/dologin.php'; |
||||
| 98 | |||||
| 99 | // 域名状态地址 |
||||
| 100 | const DOMAIN_STATUS_URL = 'https://my.freenom.com/domains.php'; |
||||
| 101 | |||||
| 102 | // 域名续期地址 |
||||
| 103 | const RENEW_DOMAIN_URL = 'https://my.freenom.com/domains.php?submitrenewals=true'; |
||||
| 104 | |||||
| 105 | /** |
||||
| 106 | * @var FREENOM |
||||
| 107 | */ |
||||
| 108 | protected static $instance; |
||||
| 109 | |||||
| 110 | /** |
||||
| 111 | * @var array 配置文件 |
||||
| 112 | */ |
||||
| 113 | protected static $config; |
||||
| 114 | |||||
| 115 | /** |
||||
| 116 | * @var int curl超时秒数 |
||||
| 117 | */ |
||||
| 118 | protected static $timeOut = 20; |
||||
| 119 | |||||
| 120 | /** |
||||
| 121 | * @var string 储存用户登录状态的cookie |
||||
| 122 | */ |
||||
| 123 | protected static $authCookie; |
||||
| 124 | |||||
| 125 | /** |
||||
| 126 | * @var string 匹配token的正则 |
||||
| 127 | */ |
||||
| 128 | private static $tokenRegex = '/name="token" value="([^"]+)"/i'; |
||||
| 129 | |||||
| 130 | /** |
||||
| 131 | * @var string 匹配域名信息的正则 |
||||
| 132 | */ |
||||
| 133 | private static $domainInfoRegex = '/<tr><td>([^<]+)<\/td><td>([^<]+)<\/td><td>[^<]+<span class="([^"]+)">([^<]+)<\/span>[^&]+&domain=(\d+)"/i'; |
||||
| 134 | |||||
| 135 | /** |
||||
| 136 | * @var string 记录续期出错的域名,用于邮件通知内容 |
||||
| 137 | */ |
||||
| 138 | public $notRenewed = ''; |
||||
| 139 | |||||
| 140 | /** |
||||
| 141 | * @var string 续期成功的域名 |
||||
| 142 | */ |
||||
| 143 | public $renewed = ''; |
||||
| 144 | |||||
| 145 | /** |
||||
| 146 | * @var string 域名状态信息,用于邮件通知内容 |
||||
| 147 | */ |
||||
| 148 | public $domainsInfo = ''; |
||||
| 149 | |||||
| 150 | public function __construct() |
||||
| 151 | { |
||||
| 152 | static::$config = require __DIR__ . DS . 'config.php'; |
||||
| 153 | } |
||||
| 154 | |||||
| 155 | public static function instance() |
||||
| 156 | { |
||||
| 157 | if (static::$instance === null) { |
||||
| 158 | static::$instance = new static(); |
||||
| 159 | } |
||||
| 160 | |||||
| 161 | return static::$instance; |
||||
| 162 | } |
||||
| 163 | |||||
| 164 | /** |
||||
| 165 | * 自动登录 |
||||
| 166 | * @return mixed |
||||
| 167 | * @throws ErrorException |
||||
| 168 | * @throws \Exception |
||||
| 169 | */ |
||||
| 170 | public function autoLogin() |
||||
| 171 | { |
||||
| 172 | $curl = new Curl(); |
||||
| 173 | $curl->setUserAgent(static::$config['userInfo']['userAgent']); |
||||
| 174 | $curl->setReferrer('https://my.freenom.com/clientarea.php'); |
||||
| 175 | $curl->setHeaders([ |
||||
| 176 | 'Accept' => 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8', |
||||
| 177 | 'Content-Type' => 'application/x-www-form-urlencoded', |
||||
| 178 | ]); |
||||
| 179 | $curl->setTimeout(static::$timeOut); |
||||
| 180 | $curl->post(static::LOGIN_URL, [ |
||||
| 181 | 'username' => static::$config['userInfo']['username'], |
||||
| 182 | 'password' => static::$config['userInfo']['password'] |
||||
| 183 | ]); |
||||
| 184 | |||||
| 185 | if ($curl->error) { |
||||
| 186 | throw new LlfException(6001, [$curl->errorCode, $curl->errorMessage]); |
||||
| 187 | } |
||||
| 188 | |||||
| 189 | $curl->close(); |
||||
| 190 | |||||
| 191 | if (!isset($curl->responseCookies['WHMCSZH5eHTGhfvzP'])) { // freenom有几率出现未成功登录也写此cookie的情况,所以此处不能完全断定是否登录成功,这是freenom的锅。若未成功登录,会在后面匹配域名信息的时候抛出异常。 |
||||
| 192 | throw new LlfException(6002); |
||||
| 193 | } |
||||
| 194 | |||||
| 195 | static::$authCookie = $curl->responseCookies['WHMCSZH5eHTGhfvzP']; |
||||
| 196 | |||||
| 197 | return $curl->responseCookies; |
||||
| 198 | } |
||||
| 199 | |||||
| 200 | /** |
||||
| 201 | * 域名续期 |
||||
| 202 | * @return array |
||||
| 203 | * @throws ErrorException |
||||
| 204 | * @throws \Exception |
||||
| 205 | */ |
||||
| 206 | public function renewDomains() |
||||
| 207 | { |
||||
| 208 | $curl = new Curl(); |
||||
| 209 | $curl->setUserAgent(static::$config['userInfo']['userAgent']); |
||||
| 210 | $curl->setTimeout(static::$timeOut); |
||||
| 211 | $curl->setCookies([ // 验证登录状态 |
||||
| 212 | 'WHMCSZH5eHTGhfvzP' => static::$authCookie |
||||
| 213 | ]); |
||||
| 214 | |||||
| 215 | /** |
||||
| 216 | * 取得需要续期的域名id以及页面token |
||||
| 217 | */ |
||||
| 218 | $curl->setHeader('Accept', 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8'); |
||||
| 219 | $curl->setReferrer('https://my.freenom.com/clientarea.php'); |
||||
| 220 | $curl->setOpts([ |
||||
| 221 | CURLOPT_FOLLOWLOCATION => true, |
||||
| 222 | CURLOPT_AUTOREFERER => true |
||||
| 223 | ]); |
||||
| 224 | $curl->get(self::DOMAIN_STATUS_URL, [ |
||||
| 225 | 'a' => 'renewals' |
||||
| 226 | ]); |
||||
| 227 | |||||
| 228 | if ($curl->error) { |
||||
| 229 | throw new LlfException(6003, [$curl->errorCode, $curl->errorMessage]); |
||||
| 230 | } |
||||
| 231 | |||||
| 232 | // 取得token |
||||
| 233 | if (!preg_match(self::$tokenRegex, $curl->response, $token)) { |
||||
| 234 | throw new LlfException(6004); |
||||
| 235 | } |
||||
| 236 | $token = $token[1]; |
||||
| 237 | |||||
| 238 | // 取得域名数据 |
||||
| 239 | if (!preg_match_all(self::$domainInfoRegex, $curl->response, $domains, PREG_SET_ORDER)) { // PREG_SET_ORDER结果排序为$matches[0]包含第一次匹配得到的所有匹配(包含子组), $matches[1]是包含第二次匹配到的所有匹配(包含子组)的数组,以此类推。 |
||||
| 240 | throw new LlfException(6005); |
||||
| 241 | } |
||||
| 242 | |||||
| 243 | /** |
||||
| 244 | * 域名续期 |
||||
| 245 | */ |
||||
| 246 | $renew_log = ''; |
||||
| 247 | foreach ($domains as $domain) { |
||||
| 248 | if (intval($domain[4]) <= 14) { // 免费域名只允许在到期前14天内续期 |
||||
| 249 | $curl->setReferrer('https://my.freenom.com/domains.php?a=renewdomain&domain=' . $domain[5]); |
||||
| 250 | $curl->setHeader('Content-Type', 'application/x-www-form-urlencoded'); |
||||
| 251 | $curl->post(static::RENEW_DOMAIN_URL, [ |
||||
| 252 | 'token' => $token, |
||||
| 253 | 'renewalid' => $domain[5], // 域名id |
||||
| 254 | 'renewalperiod[' . $domain[5] . ']' => '12M', // 续期一年 |
||||
| 255 | 'paymentmethod' => 'credit', // 支付方式 - 信用卡 |
||||
| 256 | ]); |
||||
| 257 | |||||
| 258 | if ($curl->error) { |
||||
| 259 | throw new LlfException(6006, [$domain[1], $curl->errorCode, $curl->errorMessage]); |
||||
| 260 | } |
||||
| 261 | |||||
| 262 | sleep(1); // 防止操作过于频繁 |
||||
| 263 | |||||
| 264 | if (stripos($curl->rawResponse, 'Order Confirmation') === false) { // 续期失败 |
||||
| 265 | $renew_log .= $domain[1] . '续期失败' . "\n"; |
||||
| 266 | $this->notRenewed .= sprintf('<a href="http://%s/" rel="noopener" target="_blank">%s</a>', $domain[1], $domain[1]); |
||||
| 267 | } else { |
||||
| 268 | $renew_log .= $domain[1] . '续期成功' . "\n"; |
||||
| 269 | $this->renewed .= sprintf('<a href="http://%s/" rel="noopener" target="_blank">%s</a>', $domain[1], $domain[1]); |
||||
| 270 | continue; |
||||
| 271 | } |
||||
| 272 | } |
||||
| 273 | |||||
| 274 | $this->domainsInfo .= sprintf('<a href="http://%s/" rel="noopener" target="_blank">%s</a>' . '还有<span style="font-weight: bold; font-size: 16px;">%d</span>天到期,', $domain[1], $domain[1], intval($domain[4])); |
||||
| 275 | } |
||||
| 276 | |||||
| 277 | system_log($renew_log ?: sprintf("在%s这个时刻,并没有需要续期的域名,写这条日志是为了证明我确实执行了。今次取得的域名信息如是:\n%s", date('Y-m-d H:i:s'), var_export($domains, true))); |
||||
| 278 | if ($this->notRenewed || $this->renewed) { |
||||
| 279 | $this->sendEmail( |
||||
| 280 | '主人,我刚刚帮你续期域名啦~', |
||||
| 281 | [ |
||||
| 282 | $this->renewed ? '续期成功:' . $this->renewed . '<br>' : '', |
||||
| 283 | $this->notRenewed ? '续期出错:' . $this->notRenewed . '<br>' : '', |
||||
| 284 | $this->domainsInfo ?: '啊咧~没看到其它域名呢。' |
||||
| 285 | ] |
||||
| 286 | ); |
||||
| 287 | } |
||||
| 288 | |||||
| 289 | $curl->close(); |
||||
| 290 | |||||
| 291 | return $curl->response; |
||||
| 292 | } |
||||
| 293 | |||||
| 294 | /** |
||||
| 295 | * 发送邮件 |
||||
| 296 | * @param string $subject 标题 |
||||
| 297 | * @param string|array $content 正文 |
||||
| 298 | * @param string $to 收件人,选传 |
||||
| 299 | * @param string $template 模板,选传 |
||||
| 300 | * @throws \Exception |
||||
| 301 | */ |
||||
| 302 | public function sendEmail($subject, $content, $to = '', $template = '') |
||||
| 303 | { |
||||
| 304 | $mail = new PHPMailer(true); |
||||
| 305 | |||||
| 306 | // 邮件服务配置 |
||||
| 307 | $mail->SMTPDebug = static::$config['mail']['debug']; // debug,正式环境应关闭 0:关闭 1:客户端信息 2:客户端和服务端信息 |
||||
| 308 | $mail->isSMTP(); // 告诉PHPMailer使用SMTP |
||||
| 309 | $mail->Host = 'smtp.gmail.com'; // SMTP服务器 |
||||
| 310 | $mail->SMTPAuth = true; // 启用SMTP身份验证 |
||||
| 311 | $mail->Username = static::$config['mail']['username']; // 账号 |
||||
| 312 | $mail->Password = static::$config['mail']['password']; // 密码 |
||||
| 313 | $mail->SMTPSecure = 'tls'; // 将加密系统设置为使用 - ssl(不建议使用)或tls |
||||
| 314 | $mail->Port = 587; // 设置SMTP端口号 - tsl使用587端口,ssl使用465端口 |
||||
| 315 | |||||
| 316 | $mail->CharSet = 'UTF-8'; // 防止中文邮件乱码 |
||||
| 317 | $mail->setLanguage('zh_cn', VENDOR_PATH . '/phpmailer/phpmailer/language/'); // 设置语言 |
||||
| 318 | |||||
| 319 | $mail->setFrom(static::$config['mail']['from'], 'im robot'); // 发件人 |
||||
| 320 | $mail->addAddress($to ?: static::$config['mail']['to'], '罗叔叔'); // 添加收件人,参数2选填 |
||||
| 321 | $mail->addReplyTo(static::$config['mail']['replyTo'], '罗飞飞'); // 备用回复地址,收到的回复的邮件将被发到此地址 |
||||
| 322 | |||||
| 323 | /** |
||||
| 324 | * 抄送和密送都是添加收件人,抄送方式下,被抄送者知道除被密送者外的所有的收件人,密送方式下, |
||||
| 325 | * 被密送者知道所有的被抄送者,但不知道其它的被密送者。 |
||||
| 326 | * 抄送好比@,密送好比私信。 |
||||
| 327 | */ |
||||
| 328 | // $mail->addCC('[email protected]'); // 抄送 |
||||
| 329 | // $mail->addBCC('[email protected]'); // 密送 |
||||
| 330 | |||||
| 331 | // 添加附件,参数2选填 |
||||
| 332 | // $mail->addAttachment('README.md', '说明.txt'); |
||||
| 333 | |||||
| 334 | // 内容 |
||||
| 335 | $mail->Subject = $subject; // 标题 |
||||
| 336 | /** |
||||
| 337 | * 正文 |
||||
| 338 | * 使用html文件内容作为正文,其中的图片将被base64编码,另确保html样式为内联形式,且某些样式可能需要!important方能正常显示, |
||||
| 339 | * msgHTML方法的第二个参数指定html内容中图片的路径,在转换时会拼接html中图片的相对路径得到完整的路径,最右侧无需“/”,PHPMailer |
||||
| 340 | * 源码里有加。css中的背景图片不会被转换,这是PHPMailer已知问题,建议外链。 |
||||
| 341 | * 此处也可替换为: |
||||
| 342 | * $mail->isHTML(true); // 设为html格式 |
||||
| 343 | * $mail->Body = '正文'; // 支持html |
||||
| 344 | * $mail->AltBody = 'This is an HTML-only message. To view it, activate HTML in your email application.'; // 纯文本消息正文。不支持html预览的邮件客户端将显示此预览消息,其它情况将显示正常的body |
||||
| 345 | */ |
||||
| 346 | $template = file_get_contents('mail/' . ($template ?: 'default') . '.html'); |
||||
| 347 | if (is_array($content)) { |
||||
| 348 | array_unshift($content, $template); |
||||
| 349 | $message = call_user_func_array('sprintf', $content); |
||||
| 350 | } else if (is_string($content)) { |
||||
|
0 ignored issues
–
show
|
|||||
| 351 | $message = sprintf($template, $content); |
||||
| 352 | } else { |
||||
| 353 | throw new LlfException(6007); |
||||
| 354 | } |
||||
| 355 | $mail->msgHTML($message, __DIR__ . '/mail'); |
||||
| 356 | |||||
| 357 | if (!$mail->send()) throw new \Exception($mail->ErrorInfo); |
||||
| 358 | } |
||||
| 359 | } |
||||
| 360 | |||||
| 361 | try { |
||||
| 362 | /** |
||||
| 363 | * 先登录 |
||||
| 364 | */ |
||||
| 365 | FREENOM::instance()->autoLogin(); |
||||
| 366 | |||||
| 367 | /** |
||||
| 368 | * 再续期 |
||||
| 369 | */ |
||||
| 370 | FREENOM::instance()->renewDomains(); |
||||
| 371 | } catch (LlfException $e) { |
||||
| 372 | system_log($e->getMessage()); |
||||
| 373 | FREENOM::instance()->sendEmail( |
||||
| 374 | '主人,' . $e->getMessage(), |
||||
| 375 | sprintf('具体是在%s文件的%d行,抛出了一个异常。异常的内容是%s,快去看看吧。', $e->getFile(), $e->getLine(), $e->getMessage()), |
||||
| 376 | '', |
||||
| 377 | 'llfexception' |
||||
| 378 | ); |
||||
| 379 | } catch (\Exception $e) { |
||||
| 380 | system_log(sprintf('#%d - %s', $e->getCode(), $e->getMessage())); |
||||
| 381 | } |