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())) { |
|
|
|
|
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); |
|
|
|
|
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); |
|
|
|
|
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; |
|
|
|
|
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
|
|
|
$this->sendTelegram($domain[1].' left '.$domain[4]); |
276
|
|
|
} |
277
|
|
|
|
278
|
|
|
system_log($renew_log ?: sprintf("在%s这个时刻,并没有需要续期的域名,写这条日志是为了证明我确实执行了。今次取得的域名信息如是:\n%s", date('Y-m-d H:i:s'), var_export($domains, true))); |
279
|
|
|
if ($this->notRenewed || $this->renewed) { |
280
|
|
|
if (static::$config['telegram']['enable'] == 'true') { |
281
|
|
|
$this->sendTelegram( |
282
|
|
|
[ |
283
|
|
|
$this->renewed ? '续期成功:' . $this->renewed . '<br>' : '', |
284
|
|
|
$this->notRenewed ? '续期出错:' . $this->notRenewed . '<br>' : '', |
285
|
|
|
$this->domainsInfo ?: '啊咧~没看到其它域名呢。' |
286
|
|
|
] |
287
|
|
|
); |
288
|
|
|
} else { |
289
|
|
|
$this->sendEmail( |
290
|
|
|
'主人,我刚刚帮你续期域名啦~', |
291
|
|
|
[ |
292
|
|
|
$this->renewed ? '续期成功:' . $this->renewed . '<br>' : '', |
293
|
|
|
$this->notRenewed ? '续期出错:' . $this->notRenewed . '<br>' : '', |
294
|
|
|
$this->domainsInfo ?: '啊咧~没看到其它域名呢。' |
295
|
|
|
] |
296
|
|
|
); |
297
|
|
|
} |
298
|
|
|
} |
299
|
|
|
|
300
|
|
|
$curl->close(); |
301
|
|
|
|
302
|
|
|
return $curl->response; |
|
|
|
|
303
|
|
|
} |
304
|
|
|
|
305
|
|
|
/** |
306
|
|
|
* 发送邮件 |
307
|
|
|
* @param string $subject 标题 |
308
|
|
|
* @param string|array $content 正文 |
309
|
|
|
* @param string $to 收件人,选传 |
310
|
|
|
* @param string $template 模板,选传 |
311
|
|
|
* @throws \Exception |
312
|
|
|
*/ |
313
|
|
|
public function sendEmail($subject, $content, $to = '', $template = '') |
314
|
|
|
{ |
315
|
|
|
$mail = new PHPMailer(true); |
316
|
|
|
|
317
|
|
|
// 邮件服务配置 |
318
|
|
|
$mail->SMTPDebug = static::$config['mail']['debug']; // debug,正式环境应关闭 0:关闭 1:客户端信息 2:客户端和服务端信息 |
319
|
|
|
$mail->isSMTP(); // 告诉PHPMailer使用SMTP |
320
|
|
|
$mail->Host = 'smtp.gmail.com'; // SMTP服务器 |
321
|
|
|
$mail->SMTPAuth = true; // 启用SMTP身份验证 |
322
|
|
|
$mail->Username = static::$config['mail']['username']; // 账号 |
323
|
|
|
$mail->Password = static::$config['mail']['password']; // 密码 |
324
|
|
|
$mail->SMTPSecure = 'tls'; // 将加密系统设置为使用 - ssl(不建议使用)或tls |
325
|
|
|
$mail->Port = 587; // 设置SMTP端口号 - tsl使用587端口,ssl使用465端口 |
326
|
|
|
|
327
|
|
|
$mail->CharSet = 'UTF-8'; // 防止中文邮件乱码 |
328
|
|
|
$mail->setLanguage('zh_cn', VENDOR_PATH . '/phpmailer/phpmailer/language/'); // 设置语言 |
329
|
|
|
|
330
|
|
|
$mail->setFrom(static::$config['mail']['from'], 'im robot'); // 发件人 |
331
|
|
|
$mail->addAddress($to ?: static::$config['mail']['to'], '罗叔叔'); // 添加收件人,参数2选填 |
332
|
|
|
$mail->addReplyTo(static::$config['mail']['replyTo'], '罗飞飞'); // 备用回复地址,收到的回复的邮件将被发到此地址 |
333
|
|
|
|
334
|
|
|
/** |
335
|
|
|
* 抄送和密送都是添加收件人,抄送方式下,被抄送者知道除被密送者外的所有的收件人,密送方式下, |
336
|
|
|
* 被密送者知道所有的被抄送者,但不知道其它的被密送者。 |
337
|
|
|
* 抄送好比@,密送好比私信。 |
338
|
|
|
*/ |
339
|
|
|
// $mail->addCC('[email protected]'); // 抄送 |
340
|
|
|
// $mail->addBCC('[email protected]'); // 密送 |
341
|
|
|
|
342
|
|
|
// 添加附件,参数2选填 |
343
|
|
|
// $mail->addAttachment('README.md', '说明.txt'); |
344
|
|
|
|
345
|
|
|
// 内容 |
346
|
|
|
$mail->Subject = $subject; // 标题 |
347
|
|
|
/** |
348
|
|
|
* 正文 |
349
|
|
|
* 使用html文件内容作为正文,其中的图片将被base64编码,另确保html样式为内联形式,且某些样式可能需要!important方能正常显示, |
350
|
|
|
* msgHTML方法的第二个参数指定html内容中图片的路径,在转换时会拼接html中图片的相对路径得到完整的路径,最右侧无需“/”,PHPMailer |
351
|
|
|
* 源码里有加。css中的背景图片不会被转换,这是PHPMailer已知问题,建议外链。 |
352
|
|
|
* 此处也可替换为: |
353
|
|
|
* $mail->isHTML(true); // 设为html格式 |
354
|
|
|
* $mail->Body = '正文'; // 支持html |
355
|
|
|
* $mail->AltBody = 'This is an HTML-only message. To view it, activate HTML in your email application.'; // 纯文本消息正文。不支持html预览的邮件客户端将显示此预览消息,其它情况将显示正常的body |
356
|
|
|
*/ |
357
|
|
|
$template = file_get_contents('mail/' . ($template ?: 'default') . '.html'); |
358
|
|
|
if (is_array($content)) { |
359
|
|
|
array_unshift($content, $template); |
360
|
|
|
$message = call_user_func_array('sprintf', $content); |
361
|
|
|
} else if (is_string($content)) { |
|
|
|
|
362
|
|
|
$message = sprintf($template, $content); |
363
|
|
|
} else { |
364
|
|
|
throw new LlfException(6007); |
365
|
|
|
} |
366
|
|
|
$mail->msgHTML($message, __DIR__ . '/mail'); |
367
|
|
|
|
368
|
|
|
if (!$mail->send()) throw new \Exception($mail->ErrorInfo); |
369
|
|
|
} |
370
|
|
|
|
371
|
|
|
public function sendTelegram($text) |
372
|
|
|
{ |
373
|
|
|
$token = static::$config['telegram']['token']; |
374
|
|
|
$chatid = static::$config['telegram']['chatid']; |
375
|
|
|
$curl = new Curl(); |
376
|
|
|
if (!is_string($text)) { |
377
|
|
|
$text=var_export($text,true); |
378
|
|
|
} |
379
|
|
|
$curl->get('https://api.telegram.org/bot'.$token.'/sendMessage?chat_id='.$chatid.'&text='.$text); |
380
|
|
|
$curl->close(); |
381
|
|
|
} |
382
|
|
|
} |
383
|
|
|
|
384
|
|
|
try { |
385
|
|
|
/** |
386
|
|
|
* 先登录 |
387
|
|
|
*/ |
388
|
|
|
FREENOM::instance()->autoLogin(); |
389
|
|
|
|
390
|
|
|
/** |
391
|
|
|
* 再续期 |
392
|
|
|
*/ |
393
|
|
|
FREENOM::instance()->renewDomains(); |
394
|
|
|
} catch (LlfException $e) { |
395
|
|
|
system_log($e->getMessage()); |
396
|
|
|
if (static::$config['telegram']['enable'] == 'true') { |
397
|
|
|
FREENOM::instance()->sendTelegram( |
398
|
|
|
[$e->getMessage(),$e->getFile(),$e->getLine(),$e->getMessage()] |
399
|
|
|
); |
400
|
|
|
} else { |
401
|
|
|
FREENOM::instance()->sendEmail( |
402
|
|
|
'主人,' . $e->getMessage(), |
403
|
|
|
sprintf('具体是在%s文件的%d行,抛出了一个异常。异常的内容是%s,快去看看吧。', $e->getFile(), $e->getLine(), $e->getMessage()), |
404
|
|
|
'', |
405
|
|
|
'llfexception' |
406
|
|
|
); |
407
|
|
|
} |
408
|
|
|
} catch (\Exception $e) { |
409
|
|
|
system_log(sprintf('#%d - %s', $e->getCode(), $e->getMessage())); |
410
|
|
|
} |
411
|
|
|
|