annotate usrajax.php @ 332:48d53945c6d3

Oops, some debugging code was left in.
author Matti Hamalainen <ccr@tnsp.org>
date Thu, 28 Nov 2013 03:32:49 +0200
parents 899a3583666d
children 2af8458058ab
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
161
50032763bc79 Clean up the code a bit.
Matti Hamalainen <ccr@tnsp.org>
parents: 155
diff changeset
12 //
316
54dfab6ba12c Work on voting.
Matti Hamalainen <ccr@tnsp.org>
parents: 310
diff changeset
13 // Update one vote (prevalidated)
161
50032763bc79 Clean up the code a bit.
Matti Hamalainen <ccr@tnsp.org>
parents: 155
diff changeset
14 //
316
54dfab6ba12c Work on voting.
Matti Hamalainen <ccr@tnsp.org>
parents: 310
diff changeset
15 function stUpdateVote($key_id, $entry_id, $vote)
93
f36ebd03afd6 User AJAX.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
16 {
161
50032763bc79 Clean up the code a bit.
Matti Hamalainen <ccr@tnsp.org>
parents: 155
diff changeset
17 // Check if the vote already exists
316
54dfab6ba12c Work on voting.
Matti Hamalainen <ccr@tnsp.org>
parents: 310
diff changeset
18 $sql = stPrepareSQL("SELECT id FROM votes WHERE key_id=%d AND entry_id=%d",
54dfab6ba12c Work on voting.
Matti Hamalainen <ccr@tnsp.org>
parents: 310
diff changeset
19 $key_id, $entry_id);
93
f36ebd03afd6 User AJAX.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
20
f36ebd03afd6 User AJAX.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
21 if (($res = stFetchSQLColumn($sql)) === false)
f36ebd03afd6 User AJAX.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
22 {
161
50032763bc79 Clean up the code a bit.
Matti Hamalainen <ccr@tnsp.org>
parents: 155
diff changeset
23 // Didn't exist, insert it
93
f36ebd03afd6 User AJAX.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
24 $sql = stPrepareSQL(
316
54dfab6ba12c Work on voting.
Matti Hamalainen <ccr@tnsp.org>
parents: 310
diff changeset
25 "INSERT INTO votes (key_id,entry_id,value) VALUES (%d,%d,%d)",
54dfab6ba12c Work on voting.
Matti Hamalainen <ccr@tnsp.org>
parents: 310
diff changeset
26 $key_id, $entry_id, $vote);
93
f36ebd03afd6 User AJAX.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
27 }
f36ebd03afd6 User AJAX.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
28 else
f36ebd03afd6 User AJAX.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
29 {
161
50032763bc79 Clean up the code a bit.
Matti Hamalainen <ccr@tnsp.org>
parents: 155
diff changeset
30 // Existed, thusly update
93
f36ebd03afd6 User AJAX.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
31 $sql = stPrepareSQL(
316
54dfab6ba12c Work on voting.
Matti Hamalainen <ccr@tnsp.org>
parents: 310
diff changeset
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
f36ebd03afd6 User AJAX.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
34 }
f36ebd03afd6 User AJAX.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
35
225
1bb4f4bcb027 Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 216
diff changeset
36 return stExecSQL($sql);
93
f36ebd03afd6 User AJAX.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
37 }
f36ebd03afd6 User AJAX.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
38
f36ebd03afd6 User AJAX.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
39
165
15182643d672 Cosmetics.
Matti Hamalainen <ccr@tnsp.org>
parents: 162
diff changeset
40 //
15182643d672 Cosmetics.
Matti Hamalainen <ccr@tnsp.org>
parents: 162
diff changeset
41 // Initialize
15182643d672 Cosmetics.
Matti Hamalainen <ccr@tnsp.org>
parents: 162
diff changeset
42 //
93
f36ebd03afd6 User AJAX.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
43 if (!stUserSessionAuth())
f36ebd03afd6 User AJAX.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
44 {
f36ebd03afd6 User AJAX.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
45 stSetupCacheControl();
f36ebd03afd6 User AJAX.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
46
f36ebd03afd6 User AJAX.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
47 stSessionEnd(SESS_USER);
f36ebd03afd6 User AJAX.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
48
123
5837b9333964 Add new "about" page, and setting for default page.
Matti Hamalainen <ccr@tnsp.org>
parents: 101
diff changeset
49 header("Location: ".stGetSetting("defaultPage"));
93
f36ebd03afd6 User AJAX.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
50 exit;
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 stSetupCacheControl();
f36ebd03afd6 User AJAX.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
54
f36ebd03afd6 User AJAX.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
55 if (!stConnectSQLDB())
f36ebd03afd6 User AJAX.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
56 die("Could not connect to SQL database.");
f36ebd03afd6 User AJAX.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
57
f36ebd03afd6 User AJAX.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
58 stReloadSettings();
f36ebd03afd6 User AJAX.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
59
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
60 $voteKeyId = stGetSessionItem("key_id");
245
bb96aef874a9 Work on the voting backend code.
Matti Hamalainen <ccr@tnsp.org>
parents: 225
diff changeset
61 $voteMin = stGetSetting("voteMin");
bb96aef874a9 Work on the voting backend code.
Matti Hamalainen <ccr@tnsp.org>
parents: 225
diff changeset
62 $voteMax = stGetSetting("voteMax");
93
f36ebd03afd6 User AJAX.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
63
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
64
161
50032763bc79 Clean up the code a bit.
Matti Hamalainen <ccr@tnsp.org>
parents: 155
diff changeset
65 //
50032763bc79 Clean up the code a bit.
Matti Hamalainen <ccr@tnsp.org>
parents: 155
diff changeset
66 // Handle the request
50032763bc79 Clean up the code a bit.
Matti Hamalainen <ccr@tnsp.org>
parents: 155
diff changeset
67 //
216
bcc3c4696b3e Some more work.
Matti Hamalainen <ccr@tnsp.org>
parents: 211
diff changeset
68 switch (stGetRequestItem("action"))
93
f36ebd03afd6 User AJAX.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
69 {
f36ebd03afd6 User AJAX.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
70 case "set":
f36ebd03afd6 User AJAX.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
71 //
153
aecf145e7c70 Some work on the voting backend.
Matti Hamalainen <ccr@tnsp.org>
parents: 123
diff changeset
72 // Set vote, if voting is enabled
93
f36ebd03afd6 User AJAX.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
73 //
245
bb96aef874a9 Work on the voting backend code.
Matti Hamalainen <ccr@tnsp.org>
parents: 225
diff changeset
74 if (!stChkSetting("allowVoting"))
294
efba5a51f8fa Fix some 10L's ... durr.
Matti Hamalainen <ccr@tnsp.org>
parents: 245
diff changeset
75 stError("Voting is not enabled.");
245
bb96aef874a9 Work on the voting backend code.
Matti Hamalainen <ccr@tnsp.org>
parents: 225
diff changeset
76 else
bb96aef874a9 Work on the voting backend code.
Matti Hamalainen <ccr@tnsp.org>
parents: 225
diff changeset
77 if (stChkRequestItem("entry_id", $entry_id,
bb96aef874a9 Work on the voting backend code.
Matti Hamalainen <ccr@tnsp.org>
parents: 225
diff changeset
78 array(CHK_TYPE, VT_INT, "Invalid data.")) &&
bb96aef874a9 Work on the voting backend code.
Matti Hamalainen <ccr@tnsp.org>
parents: 225
diff changeset
79 stChkRequestItem("vote", $vote,
bb96aef874a9 Work on the voting backend code.
Matti Hamalainen <ccr@tnsp.org>
parents: 225
diff changeset
80 array(CHK_TYPE, VT_INT, "Invalid data."),
294
efba5a51f8fa Fix some 10L's ... durr.
Matti Hamalainen <ccr@tnsp.org>
parents: 245
diff changeset
81 array(CHK_RANGE, VT_INT, array($voteMin, $voteMax), "Invalid vote value.")))
93
f36ebd03afd6 User AJAX.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
82 {
316
54dfab6ba12c Work on voting.
Matti Hamalainen <ccr@tnsp.org>
parents: 310
diff changeset
83 // Check if the entry_id is actually valid
54dfab6ba12c Work on voting.
Matti Hamalainen <ccr@tnsp.org>
parents: 310
diff changeset
84 $sql = stPrepareSQL("SELECT * FROM entries WHERE id=%d", $entry_id);
54dfab6ba12c Work on voting.
Matti Hamalainen <ccr@tnsp.org>
parents: 310
diff changeset
85 if (($entry = stFetchSQL($sql)) !== false)
54dfab6ba12c Work on voting.
Matti Hamalainen <ccr@tnsp.org>
parents: 310
diff changeset
86 {
54dfab6ba12c Work on voting.
Matti Hamalainen <ccr@tnsp.org>
parents: 310
diff changeset
87 // Check if the compo is valid for the entry
54dfab6ba12c Work on voting.
Matti Hamalainen <ccr@tnsp.org>
parents: 310
diff changeset
88 $sql = stPrepareSQL("SELECT * FROM compos WHERE id=%d", $entry["compo_id"]);
54dfab6ba12c Work on voting.
Matti Hamalainen <ccr@tnsp.org>
parents: 310
diff changeset
89 if (($compo = stFetchSQL($sql)) !== false && $compo["voting"] != 0)
54dfab6ba12c Work on voting.
Matti Hamalainen <ccr@tnsp.org>
parents: 310
diff changeset
90 {
54dfab6ba12c Work on voting.
Matti Hamalainen <ccr@tnsp.org>
parents: 310
diff changeset
91 stUpdateVote($voteKeyId, $entry_id, $vote);
54dfab6ba12c Work on voting.
Matti Hamalainen <ccr@tnsp.org>
parents: 310
diff changeset
92 }
54dfab6ba12c Work on voting.
Matti Hamalainen <ccr@tnsp.org>
parents: 310
diff changeset
93 }
93
f36ebd03afd6 User AJAX.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
94 }
245
bb96aef874a9 Work on the voting backend code.
Matti Hamalainen <ccr@tnsp.org>
parents: 225
diff changeset
95 break;
bb96aef874a9 Work on the voting backend code.
Matti Hamalainen <ccr@tnsp.org>
parents: 225
diff changeset
96
bb96aef874a9 Work on the voting backend code.
Matti Hamalainen <ccr@tnsp.org>
parents: 225
diff changeset
97 case "submit":
bb96aef874a9 Work on the voting backend code.
Matti Hamalainen <ccr@tnsp.org>
parents: 225
diff changeset
98 if (!stChkSetting("allowVoting"))
294
efba5a51f8fa Fix some 10L's ... durr.
Matti Hamalainen <ccr@tnsp.org>
parents: 245
diff changeset
99 stError("Voting is not enabled.");
93
f36ebd03afd6 User AJAX.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
100 else
245
bb96aef874a9 Work on the voting backend code.
Matti Hamalainen <ccr@tnsp.org>
parents: 225
diff changeset
101 {
316
54dfab6ba12c Work on voting.
Matti Hamalainen <ccr@tnsp.org>
parents: 310
diff changeset
102 foreach (stExecSQL("SELECT * FROM compos WHERE visible<>0 AND voting<>0") as $compo)
54dfab6ba12c Work on voting.
Matti Hamalainen <ccr@tnsp.org>
parents: 310
diff changeset
103 {
54dfab6ba12c Work on voting.
Matti Hamalainen <ccr@tnsp.org>
parents: 310
diff changeset
104 $cid = $compo["id"];
54dfab6ba12c Work on voting.
Matti Hamalainen <ccr@tnsp.org>
parents: 310
diff changeset
105 foreach (stExecSQL("SELECT * FROM entries WHERE compo_id=".$cid) as $entry)
54dfab6ba12c Work on voting.
Matti Hamalainen <ccr@tnsp.org>
parents: 310
diff changeset
106 {
329
899a3583666d Yay, noscript voting also works now.
Matti Hamalainen <ccr@tnsp.org>
parents: 325
diff changeset
107 $value = stGetRequestItem("ventry".$entry["id"], 0);
325
aac3bdd73ec1 More work on voting.
Matti Hamalainen <ccr@tnsp.org>
parents: 316
diff changeset
108 if (!stUpdateVote($voteKeyId, $entry["id"], $value))
aac3bdd73ec1 More work on voting.
Matti Hamalainen <ccr@tnsp.org>
parents: 316
diff changeset
109 {
aac3bdd73ec1 More work on voting.
Matti Hamalainen <ccr@tnsp.org>
parents: 316
diff changeset
110 stError("Could not set vote for compo #".$cid.", entry #".$entry["id"]);
aac3bdd73ec1 More work on voting.
Matti Hamalainen <ccr@tnsp.org>
parents: 316
diff changeset
111 break;
aac3bdd73ec1 More work on voting.
Matti Hamalainen <ccr@tnsp.org>
parents: 316
diff changeset
112 }
316
54dfab6ba12c Work on voting.
Matti Hamalainen <ccr@tnsp.org>
parents: 310
diff changeset
113 }
54dfab6ba12c Work on voting.
Matti Hamalainen <ccr@tnsp.org>
parents: 310
diff changeset
114 }
325
aac3bdd73ec1 More work on voting.
Matti Hamalainen <ccr@tnsp.org>
parents: 316
diff changeset
115 if (!$errorSet)
aac3bdd73ec1 More work on voting.
Matti Hamalainen <ccr@tnsp.org>
parents: 316
diff changeset
116 {
aac3bdd73ec1 More work on voting.
Matti Hamalainen <ccr@tnsp.org>
parents: 316
diff changeset
117 stSetSessionItem("mode", "done");
aac3bdd73ec1 More work on voting.
Matti Hamalainen <ccr@tnsp.org>
parents: 316
diff changeset
118 header("Location: ".stGetRequestItem("goto", "vote"));
aac3bdd73ec1 More work on voting.
Matti Hamalainen <ccr@tnsp.org>
parents: 316
diff changeset
119 }
245
bb96aef874a9 Work on the voting backend code.
Matti Hamalainen <ccr@tnsp.org>
parents: 225
diff changeset
120 }
93
f36ebd03afd6 User AJAX.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
121 break;
f36ebd03afd6 User AJAX.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
122
f36ebd03afd6 User AJAX.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
123 default:
f36ebd03afd6 User AJAX.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
124 stSetStatus(404, "Not Found");
f36ebd03afd6 User AJAX.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
125 break;
f36ebd03afd6 User AJAX.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
126 }
f36ebd03afd6 User AJAX.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
127
211
5bae42020034 Factorize some code into msitegen.
Matti Hamalainen <ccr@tnsp.org>
parents: 175
diff changeset
128 stDumpAJAXStatusErrors();
93
f36ebd03afd6 User AJAX.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
129 ?>