· last year · Feb 13, 2025, 06:30 AM
1<?php
2/**
3 * Template Name: Like Sending Tool
4 */
5?>
6
7<?php
8session_start();
9
10// Database connection details (replace with your credentials)
11$host = 'localhost';
12$dbname = 'u495250137_Like_Test';
13$username = 'u495250137_Like_Test';
14$password = 'Xhn606029@';
15
16// Initialize error message
17$error = null;
18$result = null;
19$uid = '';
20
21// Attempt to connect to the database
22try {
23 $pdo = new PDO( "mysql:host=$host;dbname=$dbname", $username, $password );
24 $pdo->setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION );
25} catch ( PDOException $e ) {
26 die( 'Database connection failed: ' . $e->getMessage() );
27}
28
29// API URLs
30$apiUrls = array(
31 'https://smartclowngameshop.vercel.app/like?uid={uid}&server_name=ind15&key=3519567351',
32 'https://smartclowngameshop.vercel.app/like?uid={uid}&server_name=ind16&key=3519567351',
33 'https://smartclowngameshop.vercel.app/like?uid={uid}&server_name=ind17&key=3519567351',
34);
35
36// Max requests per API per day
37$maxRequestsPerApi = 30;
38
39// Define the reset time (4:00 AM)
40$resetHour = 4;
41
42// Handle Form Submission
43if ( $_SERVER['REQUEST_METHOD'] === 'POST' ) {
44 $uid = htmlspecialchars( $_POST['uid'] ?? '' );
45
46 if ( empty( $uid ) ) {
47 $error = 'UID is required.';
48 } else {
49 // Get the current time and calculate the reset window
50 $currentTime = new DateTime();
51 $startOfDay = ( new DateTime() )->setTime( $resetHour, 0, 0 );
52 if ( $currentTime < $startOfDay ) {
53 $startOfDay->modify( '-1 day' );
54 }
55 $endOfDay = ( clone $startOfDay )->modify( '+1 day' );
56
57 // Iterate over the APIs and check their usage
58 foreach ( $apiUrls as $index => $apiUrl ) {
59 // Check the usage count for the current API within the reset window
60 $stmt = $pdo->prepare(
61 'SELECT COUNT(*) as usage_count FROM api_usage WHERE api_url = :api_url AND request_time >= :start_of_day AND request_time < :end_of_day'
62 );
63 $stmt->execute(
64 array(
65 'api_url' => $apiUrl,
66 'start_of_day' => $startOfDay->format( 'Y-m-d H:i:s' ),
67 'end_of_day' => $endOfDay->format( 'Y-m-d H:i:s' ),
68 )
69 );
70 $usage = $stmt->fetch( PDO::FETCH_ASSOC );
71
72 if ( $usage['usage_count'] < $maxRequestsPerApi ) {
73 // Use this API
74 $apiEndpoint = str_replace( '{uid}', $uid, $apiUrl );
75
76 // Send API request
77 $response = file_get_contents( $apiEndpoint );
78 if ( $response === false ) {
79 $error = 'Failed to connect to the API.';
80 } else {
81 // Store the API request details in the database
82 try {
83 $stmt = $pdo->prepare( 'INSERT INTO `api_usage` (api_url, request_time) VALUES (:api_url, NOW())' );
84 $stmt->execute( array( 'api_url' => $apiUrl ) );
85 } catch ( PDOException $e ) {
86 $error = 'Failed to record the API usage in the database: ' . $e->getMessage();
87 }
88
89 // Decode API response
90 $result = json_decode( $response, true );
91
92 // Redirect to avoid form resubmission and preserve result
93 header( 'Location: ?success=1' );
94 $_SESSION['api_result'] = $result;
95 exit;
96 }
97 }
98 }
99
100 if ( ! $result && ! $error ) {
101 $error = 'All APIs have reached their daily limit.';
102 }
103 }
104}
105
106// Fetch the API usage details from the database for the current reset window
107try {
108 $currentTime = new DateTime();
109 $startOfDay = ( new DateTime() )->setTime( $resetHour, 0, 0 );
110 if ( $currentTime < $startOfDay ) {
111 $startOfDay->modify( '-1 day' );
112 }
113 $endOfDay = ( clone $startOfDay )->modify( '+1 day' );
114
115 $stmt = $pdo->prepare(
116 'SELECT api_url, COUNT(*) AS usage_count FROM api_usage WHERE request_time >= :start_of_day AND request_time < :end_of_day GROUP BY api_url'
117 );
118 $stmt->execute(
119 array(
120 'start_of_day' => $startOfDay->format( 'Y-m-d H:i:s' ),
121 'end_of_day' => $endOfDay->format( 'Y-m-d H:i:s' ),
122 )
123 );
124 $apiUsage = $stmt->fetchAll( PDO::FETCH_ASSOC );
125} catch ( PDOException $e ) {
126 $error = 'Error fetching API usage: ' . $e->getMessage();
127}
128
129if ( isset( $_SESSION['api_result'] ) ) {
130 $result = $_SESSION['api_result'];
131 unset( $_SESSION['api_result'] );
132}
133?>
134
135<?php
136get_header();
137the_post();
138$single = new \JNews\Single\SinglePage();
139?>
140
141 <div class="jeg_main <?php $single->main_class(); ?>">
142 <div class="jeg_container">
143
144 <div class="jeg_content jeg_singlepage">
145 <div class="container">
146
147 <div class="row">
148 <div class="jeg_main_content col-md-8">
149 <div class="entry-header">
150
151
152 </div>
153 <div class="entry-content">
154
155 <div class="content-inner">
156 <h1>Like Sending Tool</h1>
157 <form method="POST">
158 <label for="uid">Enter UID:</label>
159 <input type="text" name="uid" id="uid" value="<?php echo htmlspecialchars( $uid ); ?>" required>
160 <button type="submit">Send Likes</button>
161 </form>
162 <?php if ( isset( $_GET['success'] ) && $_GET['success'] == 1 ) : ?>
163 <p>Status of Your Request:</p>
164 <?php if ( $result ) : ?>
165 <div style="background-color: #f9f9f9; padding: 10px; border-radius: 4px; font-family: monospace;">
166 <?php
167 echo "╭ Player's Nickname: " . htmlspecialchars( $result['PlayerNickname'] ) . '<br>';
168 echo "├ Player's Uid: " . htmlspecialchars( $result['UID'] ) . '<br>';
169 echo '├ Likes before Sent: ' . htmlspecialchars( $result['LikesbeforeCommand'] ) . '<br>';
170 echo '├ Likes after Sent: ' . htmlspecialchars( $result['LikesafterCommand'] ) . '<br>';
171 echo '╰ Likes Given: ' . htmlspecialchars( $result['LikesGivenByAPI'] ) . '<br>';
172 ?>
173 </div>
174 <?php endif; ?>
175 <?php elseif ( $error ) : ?>
176 <p style="color: red;"><?php echo $error; ?></p>
177 <?php endif; ?>
178 <h2>API Usage:</h2>
179 <ul>
180 <?php
181 $apiDisplay = array(
182 'https://smartclowngameshop.vercel.app/like?uid={uid}&server_name=ind15&key=3519567351' => 'API 1',
183 'https://smartclowngameshop.vercel.app/like?uid={uid}&server_name=ind16&key=3519567351' => 'API 2',
184 'https://smartclowngameshop.vercel.app/like?uid={uid}&server_name=ind17&key=3519567351' => 'API 3',
185 );
186 foreach ( $apiDisplay as $apiUrl => $apiName ) {
187 $count = 0;
188 foreach ( $apiUsage as $usage ) {
189 if ( $usage['api_url'] === $apiUrl ) {
190 $count = $usage['usage_count'];
191 }
192 }
193 echo "<li>{$apiName}: {$count} requests</li>";
194 }
195 ?>
196 </ul>
197
198 </div>
199 </div>
200
201
202 <?php
203 if ( comments_open() || '0' != jnews_get_comments_number() ) {
204 comments_template();
205 }
206
207 get_template_part( 'fragment/post/author-box' );
208 ?>
209 </div>
210 <?php
211 set_query_var(
212 'sidebar',
213 array(
214 'content-sidebar' => 'default-sidebar',
215 'is_sticky' => 'jeg_sticky_sidebar',
216 'sticky-sidebar' => 'jeg_sticky_sidebar',
217 'width-sidebar' => 4,
218 'position-sidebar' => 'left',
219 )
220 );
221 get_template_part( 'fragment/archive-sidebar' );
222 ?>
223 </div>
224
225 </div>
226 </div>
227 <?php do_action( 'jnews_after_main' ); ?>
228
229 </div>
230 </div>
231
232<?php
233get_footer();
234