annotate usrajax.php @ 211:5bae42020034

Factorize some code into msitegen.
author Matti Hamalainen <ccr@tnsp.org>
date Sun, 17 Nov 2013 20:51:03 +0200
parents 8df523e6326a
children bcc3c4696b3e
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
93
f36ebd03afd6 User AJAX.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1 <?
f36ebd03afd6 User AJAX.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2 //
155
5b92f130ba87 Add copyright header blurbs.
Matti Hamalainen <ccr@tnsp.org>
parents: 153
diff changeset
3 // FAPWeb Simple Demoparty System
5b92f130ba87 Add copyright header blurbs.
Matti Hamalainen <ccr@tnsp.org>
parents: 153
diff changeset
4 // User actions page AJAX backend module
5b92f130ba87 Add copyright header blurbs.
Matti Hamalainen <ccr@tnsp.org>
parents: 153
diff changeset
5 // (C) Copyright 2012-2013 Tecnic Software productions (TNSP)
93
f36ebd03afd6 User AJAX.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
6 //
f36ebd03afd6 User AJAX.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
7 $sessionType = "user";
175
8df523e6326a User require_once instead of require.
Matti Hamalainen <ccr@tnsp.org>
parents: 165
diff changeset
8 require_once "mconfig.inc.php";
8df523e6326a User require_once instead of require.
Matti Hamalainen <ccr@tnsp.org>
parents: 165
diff changeset
9 require_once "msite.inc.php";
8df523e6326a User require_once instead of require.
Matti Hamalainen <ccr@tnsp.org>
parents: 165
diff changeset
10 require_once "msession.inc.php";
93
f36ebd03afd6 User AJAX.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
11
f36ebd03afd6 User AJAX.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
12
161
50032763bc79 Clean up the code a bit.
Matti Hamalainen <ccr@tnsp.org>
parents: 155
diff changeset
13 //
50032763bc79 Clean up the code a bit.
Matti Hamalainen <ccr@tnsp.org>
parents: 155
diff changeset
14 // "Submit" one vote into the database
50032763bc79 Clean up the code a bit.
Matti Hamalainen <ccr@tnsp.org>
parents: 155
diff changeset
15 //
93
f36ebd03afd6 User AJAX.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
16 function stSubmitOneVote($voter_id, $entry_id, $vote)
f36ebd03afd6 User AJAX.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
17 {
161
50032763bc79 Clean up the code a bit.
Matti Hamalainen <ccr@tnsp.org>
parents: 155
diff changeset
18 // Check if the entry_id is actually valid
50032763bc79 Clean up the code a bit.
Matti Hamalainen <ccr@tnsp.org>
parents: 155
diff changeset
19 $sql = stPrepareSQL("SELECT * FROM entries WHERE id=%d", $entry_id);
50032763bc79 Clean up the code a bit.
Matti Hamalainen <ccr@tnsp.org>
parents: 155
diff changeset
20 if (($entry = stFetchSQL($sql)) === false)
50032763bc79 Clean up the code a bit.
Matti Hamalainen <ccr@tnsp.org>
parents: 155
diff changeset
21 return FALSE;
50032763bc79 Clean up the code a bit.
Matti Hamalainen <ccr@tnsp.org>
parents: 155
diff changeset
22
50032763bc79 Clean up the code a bit.
Matti Hamalainen <ccr@tnsp.org>
parents: 155
diff changeset
23 // Check if the compo is valid for the entry
50032763bc79 Clean up the code a bit.
Matti Hamalainen <ccr@tnsp.org>
parents: 155
diff changeset
24 $sql = stPrepareSQL("SELECT * FROM compos WHERE id=%d", $entry["compo_id"]);
50032763bc79 Clean up the code a bit.
Matti Hamalainen <ccr@tnsp.org>
parents: 155
diff changeset
25 if (($compo = stFetchSQL($sql)) === false || $compo["voting"] == 0)
50032763bc79 Clean up the code a bit.
Matti Hamalainen <ccr@tnsp.org>
parents: 155
diff changeset
26 return FALSE;
50032763bc79 Clean up the code a bit.
Matti Hamalainen <ccr@tnsp.org>
parents: 155
diff changeset
27
50032763bc79 Clean up the code a bit.
Matti Hamalainen <ccr@tnsp.org>
parents: 155
diff changeset
28 // Check if the vote already exists
93
f36ebd03afd6 User AJAX.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
29 $sql = stPrepareSQL("SELECT id FROM votes WHERE voter_id=%d AND entry_id=%d",
f36ebd03afd6 User AJAX.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
30 $voter_id, $entry_id);
f36ebd03afd6 User AJAX.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
31
f36ebd03afd6 User AJAX.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
32 if (($res = stFetchSQLColumn($sql)) === false)
f36ebd03afd6 User AJAX.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
33 {
161
50032763bc79 Clean up the code a bit.
Matti Hamalainen <ccr@tnsp.org>
parents: 155
diff changeset
34 // Didn't exist, insert it
93
f36ebd03afd6 User AJAX.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
35 $sql = stPrepareSQL(
f36ebd03afd6 User AJAX.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
36 "INSERT INTO votes (voter_id,entry_id,value) VALUES (%d,%d,%d)",
f36ebd03afd6 User AJAX.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
37 $voter_id, $entry_id, $vote);
f36ebd03afd6 User AJAX.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
38 }
f36ebd03afd6 User AJAX.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
39 else
f36ebd03afd6 User AJAX.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
40 {
161
50032763bc79 Clean up the code a bit.
Matti Hamalainen <ccr@tnsp.org>
parents: 155
diff changeset
41 // Existed, thusly update
93
f36ebd03afd6 User AJAX.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
42 $sql = stPrepareSQL(
f36ebd03afd6 User AJAX.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
43 "UPDATE votes SET value=%d WHERE voter_id=%d AND entry_id=%d",
f36ebd03afd6 User AJAX.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
44 $vote, $voter_id, $eid);
f36ebd03afd6 User AJAX.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
45 }
f36ebd03afd6 User AJAX.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
46
161
50032763bc79 Clean up the code a bit.
Matti Hamalainen <ccr@tnsp.org>
parents: 155
diff changeset
47 if (stExecSQL($sql) === false)
50032763bc79 Clean up the code a bit.
Matti Hamalainen <ccr@tnsp.org>
parents: 155
diff changeset
48 return FALSE;
50032763bc79 Clean up the code a bit.
Matti Hamalainen <ccr@tnsp.org>
parents: 155
diff changeset
49 else
50032763bc79 Clean up the code a bit.
Matti Hamalainen <ccr@tnsp.org>
parents: 155
diff changeset
50 return TRUE;
93
f36ebd03afd6 User AJAX.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
51 }
f36ebd03afd6 User AJAX.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
52
f36ebd03afd6 User AJAX.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
53
165
15182643d672 Cosmetics.
Matti Hamalainen <ccr@tnsp.org>
parents: 162
diff changeset
54 //
15182643d672 Cosmetics.
Matti Hamalainen <ccr@tnsp.org>
parents: 162
diff changeset
55 // Initialize
15182643d672 Cosmetics.
Matti Hamalainen <ccr@tnsp.org>
parents: 162
diff changeset
56 //
93
f36ebd03afd6 User AJAX.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
57 if (!stUserSessionAuth())
f36ebd03afd6 User AJAX.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
58 {
f36ebd03afd6 User AJAX.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
59 stSetupCacheControl();
f36ebd03afd6 User AJAX.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
60
f36ebd03afd6 User AJAX.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
61 stSessionEnd(SESS_USER);
f36ebd03afd6 User AJAX.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
62
123
5837b9333964 Add new "about" page, and setting for default page.
Matti Hamalainen <ccr@tnsp.org>
parents: 101
diff changeset
63 header("Location: ".stGetSetting("defaultPage"));
93
f36ebd03afd6 User AJAX.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
64 exit;
f36ebd03afd6 User AJAX.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
65 }
f36ebd03afd6 User AJAX.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
66
f36ebd03afd6 User AJAX.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
67 stSetupCacheControl();
f36ebd03afd6 User AJAX.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
68
f36ebd03afd6 User AJAX.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
69 if (!stConnectSQLDB())
f36ebd03afd6 User AJAX.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
70 die("Could not connect to SQL database.");
f36ebd03afd6 User AJAX.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
71
f36ebd03afd6 User AJAX.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
72 stReloadSettings();
f36ebd03afd6 User AJAX.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
73
f36ebd03afd6 User AJAX.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
74
161
50032763bc79 Clean up the code a bit.
Matti Hamalainen <ccr@tnsp.org>
parents: 155
diff changeset
75 //
50032763bc79 Clean up the code a bit.
Matti Hamalainen <ccr@tnsp.org>
parents: 155
diff changeset
76 // Handle the request
50032763bc79 Clean up the code a bit.
Matti Hamalainen <ccr@tnsp.org>
parents: 155
diff changeset
77 //
93
f36ebd03afd6 User AJAX.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
78 $action = "ERROR";
f36ebd03afd6 User AJAX.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
79 if (stChkRequestItem("action") && stChkRequestItem("type"))
f36ebd03afd6 User AJAX.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
80 {
f36ebd03afd6 User AJAX.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
81 $action = $_REQUEST["action"];
f36ebd03afd6 User AJAX.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
82 $type = $_REQUEST["type"];
f36ebd03afd6 User AJAX.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
83 }
f36ebd03afd6 User AJAX.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
84
f36ebd03afd6 User AJAX.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
85 switch ($action)
f36ebd03afd6 User AJAX.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
86 {
f36ebd03afd6 User AJAX.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
87 case "get":
f36ebd03afd6 User AJAX.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
88 //
f36ebd03afd6 User AJAX.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
89 // Get specific data
f36ebd03afd6 User AJAX.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
90 //
f36ebd03afd6 User AJAX.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
91 switch ($type)
f36ebd03afd6 User AJAX.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
92 {
f36ebd03afd6 User AJAX.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
93 case "votes":
f36ebd03afd6 User AJAX.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
94 $sql = "SELECT * FROM votes ORDER BY utime DESC";
f36ebd03afd6 User AJAX.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
95 break;
f36ebd03afd6 User AJAX.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
96 }
f36ebd03afd6 User AJAX.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
97
f36ebd03afd6 User AJAX.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
98 //
f36ebd03afd6 User AJAX.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
99 // Perform query if we need to, output results
f36ebd03afd6 User AJAX.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
100 //
f36ebd03afd6 User AJAX.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
101 if (isset($sql) && ($res = stExecSQLCond($sql, "")) !== FALSE)
f36ebd03afd6 User AJAX.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
102 {
f36ebd03afd6 User AJAX.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
103 if ($type == "votes")
f36ebd03afd6 User AJAX.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
104 {
f36ebd03afd6 User AJAX.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
105 }
f36ebd03afd6 User AJAX.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
106 }
f36ebd03afd6 User AJAX.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
107 break;
f36ebd03afd6 User AJAX.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
108
f36ebd03afd6 User AJAX.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
109 case "set":
f36ebd03afd6 User AJAX.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
110 //
153
aecf145e7c70 Some work on the voting backend.
Matti Hamalainen <ccr@tnsp.org>
parents: 123
diff changeset
111 // Set vote, if voting is enabled
93
f36ebd03afd6 User AJAX.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
112 //
153
aecf145e7c70 Some work on the voting backend.
Matti Hamalainen <ccr@tnsp.org>
parents: 123
diff changeset
113 if ($type == "votes" && stChkSetting("allowVoting") &&
aecf145e7c70 Some work on the voting backend.
Matti Hamalainen <ccr@tnsp.org>
parents: 123
diff changeset
114 stChkRequestItem("votekey") &&
aecf145e7c70 Some work on the voting backend.
Matti Hamalainen <ccr@tnsp.org>
parents: 123
diff changeset
115 stChkRequestItem("entry_id") &&
aecf145e7c70 Some work on the voting backend.
Matti Hamalainen <ccr@tnsp.org>
parents: 123
diff changeset
116 stChkRequestItem("vote"))
93
f36ebd03afd6 User AJAX.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
117 {
153
aecf145e7c70 Some work on the voting backend.
Matti Hamalainen <ccr@tnsp.org>
parents: 123
diff changeset
118 // Check if voting is enabled on the compo and voter is valid
aecf145e7c70 Some work on the voting backend.
Matti Hamalainen <ccr@tnsp.org>
parents: 123
diff changeset
119 $entry_id = stGetRequestItem("entry_id");
aecf145e7c70 Some work on the voting backend.
Matti Hamalainen <ccr@tnsp.org>
parents: 123
diff changeset
120
aecf145e7c70 Some work on the voting backend.
Matti Hamalainen <ccr@tnsp.org>
parents: 123
diff changeset
121 $sql = stPrepareSQL("SELECT * FROM votekeys WHERE key=%s", stGetRequestItem("votekey"));
aecf145e7c70 Some work on the voting backend.
Matti Hamalainen <ccr@tnsp.org>
parents: 123
diff changeset
122 if (($votekey = stFetchSQLColumn($sql)) !== false)
aecf145e7c70 Some work on the voting backend.
Matti Hamalainen <ccr@tnsp.org>
parents: 123
diff changeset
123 {
aecf145e7c70 Some work on the voting backend.
Matti Hamalainen <ccr@tnsp.org>
parents: 123
diff changeset
124 }
93
f36ebd03afd6 User AJAX.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
125 }
f36ebd03afd6 User AJAX.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
126 else
f36ebd03afd6 User AJAX.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
127 stSetStatus(902, "No data.");
f36ebd03afd6 User AJAX.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
128 break;
f36ebd03afd6 User AJAX.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
129
f36ebd03afd6 User AJAX.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
130 default:
f36ebd03afd6 User AJAX.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
131 stSetStatus(404, "Not Found");
f36ebd03afd6 User AJAX.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
132 break;
f36ebd03afd6 User AJAX.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
133 }
f36ebd03afd6 User AJAX.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
134
211
5bae42020034 Factorize some code into msitegen.
Matti Hamalainen <ccr@tnsp.org>
parents: 175
diff changeset
135 stDumpAJAXStatusErrors();
93
f36ebd03afd6 User AJAX.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
136 ?>