|
| 1 | +<?php |
| 2 | + |
| 3 | +$TICKTICK_AUTH_URL = "https://ticktick.com/oauth/token"; |
| 4 | + |
| 5 | +$corsHeaders = [ |
| 6 | + 'Access-Control-Allow-Origin' => '*', |
| 7 | + 'Access-Control-Allow-Headers' => 'authorization, x-client-info, apikey, content-type', |
| 8 | +]; |
| 9 | + |
| 10 | +if ($_SERVER['REQUEST_METHOD'] === 'GET') { |
| 11 | + header("Location: https://github.com/mxschll/logseq-ticktick-plugin"); |
| 12 | + exit(); |
| 13 | +} |
| 14 | + |
| 15 | +if ($_SERVER['REQUEST_METHOD'] === 'OPTIONS') { |
| 16 | + http_response_code(200); |
| 17 | + foreach ($corsHeaders as $name => $value) { |
| 18 | + header("$name: $value"); |
| 19 | + } |
| 20 | + echo 'ok'; |
| 21 | + exit(); |
| 22 | +} |
| 23 | + |
| 24 | +$ip = $_SERVER['REMOTE_ADDR']; |
| 25 | +$now = time(); |
| 26 | +$limitFile = 'ratelimit_' . md5($ip) . '.txt'; |
| 27 | + |
| 28 | +// Clean up old files |
| 29 | +cleanupOldFiles(); |
| 30 | + |
| 31 | +if (file_exists($limitFile)) { |
| 32 | + $lastAccessTime = (int)file_get_contents($limitFile); |
| 33 | + $cooldownTime = 60; // 60 seconds cooldown time |
| 34 | + |
| 35 | + if ($now - $lastAccessTime < $cooldownTime) { |
| 36 | + http_response_code(429); |
| 37 | + foreach (array_merge(["Content-Type" => "application/json"], $corsHeaders) as $name => $value) { |
| 38 | + header("$name: $value"); |
| 39 | + } |
| 40 | + echo json_encode(['error' => 'rate_limit_exceeded', 'error_description' => 'Rate limit exceeded']); |
| 41 | + exit(); |
| 42 | + } |
| 43 | +} |
| 44 | + |
| 45 | +file_put_contents($limitFile, $now); |
| 46 | + |
| 47 | +$data = json_decode(file_get_contents('php://input'), true); |
| 48 | +$client_id = $data['client_id'] ?? null; |
| 49 | +$client_secret = $data['client_secret'] ?? null; |
| 50 | +$code = $data['code'] ?? null; |
| 51 | +$redirect_uri = $data['redirect_uri'] ?? null; |
| 52 | + |
| 53 | +if (!$client_id || !$client_secret || !$code || !$redirect_uri) { |
| 54 | + http_response_code(400); |
| 55 | + foreach (array_merge(["Content-Type" => "application/json"], $corsHeaders) as $name => $value) { |
| 56 | + header("$name: $value"); |
| 57 | + } |
| 58 | + echo json_encode(['error' => 'missing_parameter', 'error_description' => 'Missing parameter: client_id, client_secret, code, redirect_uri']); |
| 59 | + exit(); |
| 60 | +} |
| 61 | + |
| 62 | +$ch = curl_init($TICKTICK_AUTH_URL); |
| 63 | +curl_setopt($ch, CURLOPT_POST, true); |
| 64 | +curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query([ |
| 65 | + 'client_id' => $client_id, |
| 66 | + 'client_secret' => $client_secret, |
| 67 | + 'code' => $code, |
| 68 | + 'redirect_uri' => $redirect_uri, |
| 69 | + 'grant_type' => 'authorization_code', |
| 70 | +])); |
| 71 | +curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); |
| 72 | +$response = curl_exec($ch); |
| 73 | +$status = curl_getinfo($ch, CURLINFO_HTTP_CODE); |
| 74 | +curl_close($ch); |
| 75 | + |
| 76 | +if ($status !== 200) { |
| 77 | + http_response_code(400); |
| 78 | + foreach (array_merge(["Content-Type" => "application/json"], $corsHeaders) as $name => $value) { |
| 79 | + header("$name: $value"); |
| 80 | + } |
| 81 | + echo json_encode(['error' => 'unauthorized', 'error_description' => 'Unauthorized']); |
| 82 | + exit(); |
| 83 | +} |
| 84 | + |
| 85 | +http_response_code(200); |
| 86 | +foreach (array_merge(["Content-Type" => "application/json"], $corsHeaders) as $name => $value) { |
| 87 | + header("$name: $value"); |
| 88 | +} |
| 89 | +echo json_encode(json_decode($response, true)); |
| 90 | + |
| 91 | +function cleanupOldFiles() { |
| 92 | + $expirationTime = 3600; // 1 hour |
| 93 | + $now = time(); |
| 94 | + |
| 95 | + foreach (glob("ratelimit_*.txt") as $file) { |
| 96 | + if (is_file($file)) { |
| 97 | + if ($now - filemtime($file) >= $expirationTime) { |
| 98 | + unlink($file); |
| 99 | + } |
| 100 | + } |
| 101 | + } |
| 102 | +} |
0 commit comments