voting.php

main 76 lines · 2.3 KB Raw
Alex Alex Commit Initial commit 01/10/2026 09:20
1<?php
2require_once 'engine/init.php';
3theme_open();
4
5$otservers_eu_voting = $config['otservers_eu_voting'];
6$votingMessage = '';
7
8if ($otservers_eu_voting['enabled']) {
9 if (user_logged_in()) {
10 $isRewardRequest = isset($_GET['action']) && $_GET['action'] === 'reward';
11 if (!$isRewardRequest) {
12 $result = vote($user_data['id'], $otservers_eu_voting);
13 if ($result === false) {
14 $votingMessage = t('voting.request_failed');
15 } else {
16 header('Location: ' . $result['voteLink']);
17 die;
18 }
19 } else {
20 $result = checkHasVoted($user_data['id'], $otservers_eu_voting);
21 if ($result !== false) {
22 if ($result['voted'] === true) {
23 $points = $otservers_eu_voting['points'];
24 $pointsText = $points === '1' ? t('voting.point_singular') : t('voting.point_plural');
25 db()->execute("UPDATE `znote_accounts` SET `points` = `points` + ? WHERE `account_id` = ?", [(int)$points, (int)$user_data['id']]);
26 $votingMessage = t('voting.rewarded', ['points' => $points, 'unit' => $pointsText]);
27 } else {
28 $votingMessage = t('voting.not_voted');
29 }
30 } else {
31 $votingMessage = t('voting.cannot_verify');
32 }
33 }
34 } else {
35 header('Location: ' . $otservers_eu_voting['simpleVoteUrl']);
36 die;
37 }
38} else {
39 $votingMessage = t('voting.disabled');
40}
41
42view('voting');
43theme_close();
44
45function vote($otUserId, $otservers_eu_voting) {
46 $context = stream_context_create([
47 'http' => [
48 'header' => "Content-type: application/json",
49 'method' => 'POST',
50 'content' => json_encode([
51 'otUserId' => $otUserId,
52 'secretToken' => $otservers_eu_voting['secretToken'],
53 'landingPage' => $otservers_eu_voting['landingPage']
54 ])
55 ]
56 ]);
57 $result = file_get_contents($otservers_eu_voting['voteUrl'], false, $context);
58 return $result !== false ? json_decode($result, true) : false;
59}
60
61function checkHasVoted($otUserId, $otservers_eu_voting) {
62 $context = stream_context_create([
63 'http' => [
64 'header' => "Content-type: application/json",
65 'method' => 'POST',
66 'content' => json_encode([
67 'otUserId' => $otUserId,
68 'secretToken' => $otservers_eu_voting['secretToken'],
69 'consume' => true
70 ])
71 ]
72 ]);
73 $result = file_get_contents($otservers_eu_voting['voteCheckUrl'], false, $context);
74 return $result !== false ? json_decode($result, true) : false;
75}
76
Top