Mercurial > hg > fapweb
annotate usrajax.php @ 739:17820305bc77
Clean up vote handling.
author | Matti Hamalainen <ccr@tnsp.org> |
---|---|
date | Wed, 19 Nov 2014 04:42:24 +0200 |
parents | bf33cec02dc6 |
children | 539bfbdd43ec |
rev | line source |
---|---|
93 | 1 <? |
2 // | |
571
ce11ea112a65
Change the header blurb a bit.
Matti Hamalainen <ccr@tnsp.org>
parents:
565
diff
changeset
|
3 // FAPWeb - Simple Web-based Demoparty Management System |
155
5b92f130ba87
Add copyright header blurbs.
Matti Hamalainen <ccr@tnsp.org>
parents:
153
diff
changeset
|
4 // User actions page AJAX backend module |
565 | 5 // (C) Copyright 2012-2014 Tecnic Software productions (TNSP) |
93 | 6 // |
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 | 11 |
161 | 12 // |
316 | 13 // Update one vote (prevalidated) |
161 | 14 // |
316 | 15 function stUpdateVote($key_id, $entry_id, $vote) |
93 | 16 { |
161 | 17 // Check if the vote already exists |
316 | 18 $sql = stPrepareSQL("SELECT id FROM votes WHERE key_id=%d AND entry_id=%d", |
19 $key_id, $entry_id); | |
93 | 20 |
21 if (($res = stFetchSQLColumn($sql)) === false) | |
22 { | |
161 | 23 // Didn't exist, insert it |
93 | 24 $sql = stPrepareSQL( |
316 | 25 "INSERT INTO votes (key_id,entry_id,value) VALUES (%d,%d,%d)", |
26 $key_id, $entry_id, $vote); | |
93 | 27 } |
28 else | |
29 { | |
161 | 30 // Existed, thusly update |
93 | 31 $sql = stPrepareSQL( |
316 | 32 "UPDATE votes SET value=%d WHERE key_id=%d AND entry_id=%d", |
329
899a3583666d
Yay, noscript voting also works now.
Matti Hamalainen <ccr@tnsp.org>
parents:
325
diff
changeset
|
33 $vote, $key_id, $entry_id); |
93 | 34 } |
35 | |
225 | 36 return stExecSQL($sql); |
93 | 37 } |
38 | |
39 | |
739 | 40 function stCheckVoteValue($id, &$value) |
41 { | |
42 return | |
43 stChkRequestItem($id, $value, | |
44 array(CHK_TYPE, VT_INT, "Invalid entry vote value data."), | |
45 array(CHK_RANGE, VT_INT, array(stGetSetting("voteMin"), stGetSetting("voteMax")), "Invalid vote value, not in range.")); | |
46 } | |
47 | |
48 | |
165 | 49 // |
50 // Initialize | |
51 // | |
360
2af8458058ab
Implement CSRF token checks.
Matti Hamalainen <ccr@tnsp.org>
parents:
332
diff
changeset
|
52 if (!stUserSessionAuth() || !stCSRFCheck()) |
93 | 53 { |
54 stSetupCacheControl(); | |
55 | |
56 stSessionEnd(SESS_USER); | |
57 | |
123
5837b9333964
Add new "about" page, and setting for default page.
Matti Hamalainen <ccr@tnsp.org>
parents:
101
diff
changeset
|
58 header("Location: ".stGetSetting("defaultPage")); |
93 | 59 exit; |
60 } | |
61 | |
544 | 62 ob_start(); |
63 | |
93 | 64 stSetupCacheControl(); |
65 | |
66 if (!stConnectSQLDB()) | |
67 die("Could not connect to SQL database."); | |
68 | |
69 stReloadSettings(); | |
70 | |
310
8098b5b80f8c
We won't be checking key validity while session is in progress, thus get rid
Matti Hamalainen <ccr@tnsp.org>
parents:
294
diff
changeset
|
71 $voteKeyId = stGetSessionItem("key_id"); |
93 | 72 |
310
8098b5b80f8c
We won't be checking key validity while session is in progress, thus get rid
Matti Hamalainen <ccr@tnsp.org>
parents:
294
diff
changeset
|
73 |
161 | 74 // |
75 // Handle the request | |
76 // | |
216 | 77 switch (stGetRequestItem("action")) |
93 | 78 { |
79 case "set": | |
80 // | |
153
aecf145e7c70
Some work on the voting backend.
Matti Hamalainen <ccr@tnsp.org>
parents:
123
diff
changeset
|
81 // Set vote, if voting is enabled |
93 | 82 // |
245
bb96aef874a9
Work on the voting backend code.
Matti Hamalainen <ccr@tnsp.org>
parents:
225
diff
changeset
|
83 if (!stChkSetting("allowVoting")) |
294 | 84 stError("Voting is not enabled."); |
245
bb96aef874a9
Work on the voting backend code.
Matti Hamalainen <ccr@tnsp.org>
parents:
225
diff
changeset
|
85 else |
bb96aef874a9
Work on the voting backend code.
Matti Hamalainen <ccr@tnsp.org>
parents:
225
diff
changeset
|
86 if (stChkRequestItem("entry_id", $entry_id, |
bb96aef874a9
Work on the voting backend code.
Matti Hamalainen <ccr@tnsp.org>
parents:
225
diff
changeset
|
87 array(CHK_TYPE, VT_INT, "Invalid data.")) && |
739 | 88 stCheckVoteValue("vote", $vote)) |
93 | 89 { |
316 | 90 // Check if the entry_id is actually valid |
580
3929a5a87815
Use the new transaction functions here as well.
Matti Hamalainen <ccr@tnsp.org>
parents:
571
diff
changeset
|
91 stDBBeginTransaction(); |
316 | 92 $sql = stPrepareSQL("SELECT * FROM entries WHERE id=%d", $entry_id); |
93 if (($entry = stFetchSQL($sql)) !== false) | |
94 { | |
95 // Check if the compo is valid for the entry | |
96 $sql = stPrepareSQL("SELECT * FROM compos WHERE id=%d", $entry["compo_id"]); | |
97 if (($compo = stFetchSQL($sql)) !== false && $compo["voting"] != 0) | |
98 stUpdateVote($voteKeyId, $entry_id, $vote); | |
99 } | |
580
3929a5a87815
Use the new transaction functions here as well.
Matti Hamalainen <ccr@tnsp.org>
parents:
571
diff
changeset
|
100 stDBCommitTransaction(); |
93 | 101 } |
245
bb96aef874a9
Work on the voting backend code.
Matti Hamalainen <ccr@tnsp.org>
parents:
225
diff
changeset
|
102 break; |
bb96aef874a9
Work on the voting backend code.
Matti Hamalainen <ccr@tnsp.org>
parents:
225
diff
changeset
|
103 |
bb96aef874a9
Work on the voting backend code.
Matti Hamalainen <ccr@tnsp.org>
parents:
225
diff
changeset
|
104 case "submit": |
bb96aef874a9
Work on the voting backend code.
Matti Hamalainen <ccr@tnsp.org>
parents:
225
diff
changeset
|
105 if (!stChkSetting("allowVoting")) |
294 | 106 stError("Voting is not enabled."); |
93 | 107 else |
245
bb96aef874a9
Work on the voting backend code.
Matti Hamalainen <ccr@tnsp.org>
parents:
225
diff
changeset
|
108 { |
316 | 109 foreach (stExecSQL("SELECT * FROM compos WHERE visible<>0 AND voting<>0") as $compo) |
110 { | |
722 | 111 stDBBeginTransaction(); |
112 foreach (stExecSQL("SELECT * FROM entries WHERE compo_id=".$compo["id"]) as $entry) | |
316 | 113 { |
739 | 114 if (stCheckVoteValue("ventry".$entry["id"], $value)) |
325 | 115 { |
739 | 116 if (!stUpdateVote($voteKeyId, $entry["id"], $value)) |
117 stError("Could not set vote for compo #".$compo["id"].", entry #".$entry["id"]); | |
325 | 118 } |
316 | 119 } |
722 | 120 stDBCommitTransaction(); |
316 | 121 } |
368
cbe2693a3cd1
Error handling improvements.
Matti Hamalainen <ccr@tnsp.org>
parents:
360
diff
changeset
|
122 |
cbe2693a3cd1
Error handling improvements.
Matti Hamalainen <ccr@tnsp.org>
parents:
360
diff
changeset
|
123 if ($errorSet) |
325 | 124 { |
368
cbe2693a3cd1
Error handling improvements.
Matti Hamalainen <ccr@tnsp.org>
parents:
360
diff
changeset
|
125 stSetSessionItem("mode", "error"); |
cbe2693a3cd1
Error handling improvements.
Matti Hamalainen <ccr@tnsp.org>
parents:
360
diff
changeset
|
126 stSetSessionItem("error", $errorMsgs); |
cbe2693a3cd1
Error handling improvements.
Matti Hamalainen <ccr@tnsp.org>
parents:
360
diff
changeset
|
127 } |
cbe2693a3cd1
Error handling improvements.
Matti Hamalainen <ccr@tnsp.org>
parents:
360
diff
changeset
|
128 else |
325 | 129 stSetSessionItem("mode", "done"); |
368
cbe2693a3cd1
Error handling improvements.
Matti Hamalainen <ccr@tnsp.org>
parents:
360
diff
changeset
|
130 |
cbe2693a3cd1
Error handling improvements.
Matti Hamalainen <ccr@tnsp.org>
parents:
360
diff
changeset
|
131 header("Location: ".stGetRequestItem("goto", "vote")); |
245
bb96aef874a9
Work on the voting backend code.
Matti Hamalainen <ccr@tnsp.org>
parents:
225
diff
changeset
|
132 } |
93 | 133 break; |
134 | |
135 default: | |
739 | 136 stSetStatus(902, "Not Found"); |
93 | 137 break; |
138 } | |
139 | |
544 | 140 if ($errorSet) |
141 { | |
142 ob_clean(); | |
143 stDumpAJAXStatusErrors(); | |
144 } | |
145 | |
146 ob_end_flush(); | |
93 | 147 ?> |