Mercurial > hg > fapweb
annotate usrajax.php @ 153:aecf145e7c70
Some work on the voting backend.
author | Matti Hamalainen <ccr@tnsp.org> |
---|---|
date | Fri, 25 Oct 2013 15:07:43 +0300 |
parents | 5837b9333964 |
children | 5b92f130ba87 |
rev | line source |
---|---|
93 | 1 <? |
2 // | |
101 | 3 // User-level AJAX request handler backend module |
93 | 4 // |
5 $sessionType = "user"; | |
6 require "mconfig.inc.php"; | |
7 require "msite.inc.php"; | |
8 require "msession.inc.php"; | |
9 | |
10 | |
11 function stSubmitOneVote($voter_id, $entry_id, $vote) | |
12 { | |
13 $sql = stPrepareSQL("SELECT id FROM votes WHERE voter_id=%d AND entry_id=%d", | |
14 $voter_id, $entry_id); | |
15 | |
16 if (($res = stFetchSQLColumn($sql)) === false) | |
17 { | |
18 $sql = stPrepareSQL( | |
19 "INSERT INTO votes (voter_id,entry_id,value) VALUES (%d,%d,%d)", | |
20 $voter_id, $entry_id, $vote); | |
21 | |
22 if (stExecSQL($sql) === false) | |
23 return FALSE; | |
24 } | |
25 else | |
26 { | |
27 $sql = stPrepareSQL( | |
28 "UPDATE votes SET value=%d WHERE voter_id=%d AND entry_id=%d", | |
29 $vote, $voter_id, $eid); | |
30 | |
31 if (stExecSQL($sql) === false) | |
32 return FALSE; | |
33 } | |
34 | |
35 return TRUE; | |
36 } | |
37 | |
38 | |
39 | |
40 // Check if we are allowed to execute | |
41 if (!stUserSessionAuth()) | |
42 { | |
43 stSetupCacheControl(); | |
44 | |
45 stSessionEnd(SESS_USER); | |
46 | |
123
5837b9333964
Add new "about" page, and setting for default page.
Matti Hamalainen <ccr@tnsp.org>
parents:
101
diff
changeset
|
47 header("Location: ".stGetSetting("defaultPage")); |
93 | 48 exit; |
49 } | |
50 | |
51 stSetupCacheControl(); | |
52 | |
53 // Initiate SQL database connection | |
54 if (!stConnectSQLDB()) | |
55 die("Could not connect to SQL database."); | |
56 | |
57 // Fetch non-"hardcoded" settings from SQL database | |
58 stReloadSettings(); | |
59 | |
60 | |
61 // XMLHttp responses | |
62 $action = "ERROR"; | |
63 if (stChkRequestItem("action") && stChkRequestItem("type")) | |
64 { | |
65 $action = $_REQUEST["action"]; | |
66 $type = $_REQUEST["type"]; | |
67 } | |
68 | |
69 switch ($action) | |
70 { | |
71 case "get": | |
72 // | |
73 // Get specific data | |
74 // | |
75 switch ($type) | |
76 { | |
77 case "votes": | |
78 $sql = "SELECT * FROM votes ORDER BY utime DESC"; | |
79 break; | |
80 } | |
81 | |
82 // | |
83 // Perform query if we need to, output results | |
84 // | |
85 if (isset($sql) && ($res = stExecSQLCond($sql, "")) !== FALSE) | |
86 { | |
87 if ($type == "votes") | |
88 { | |
89 } | |
90 } | |
91 break; | |
92 | |
93 case "set": | |
94 // | |
153
aecf145e7c70
Some work on the voting backend.
Matti Hamalainen <ccr@tnsp.org>
parents:
123
diff
changeset
|
95 // Set vote, if voting is enabled |
93 | 96 // |
153
aecf145e7c70
Some work on the voting backend.
Matti Hamalainen <ccr@tnsp.org>
parents:
123
diff
changeset
|
97 if ($type == "votes" && stChkSetting("allowVoting") && |
aecf145e7c70
Some work on the voting backend.
Matti Hamalainen <ccr@tnsp.org>
parents:
123
diff
changeset
|
98 stChkRequestItem("votekey") && |
aecf145e7c70
Some work on the voting backend.
Matti Hamalainen <ccr@tnsp.org>
parents:
123
diff
changeset
|
99 stChkRequestItem("entry_id") && |
aecf145e7c70
Some work on the voting backend.
Matti Hamalainen <ccr@tnsp.org>
parents:
123
diff
changeset
|
100 stChkRequestItem("vote")) |
93 | 101 { |
153
aecf145e7c70
Some work on the voting backend.
Matti Hamalainen <ccr@tnsp.org>
parents:
123
diff
changeset
|
102 // 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
|
103 $entry_id = stGetRequestItem("entry_id"); |
aecf145e7c70
Some work on the voting backend.
Matti Hamalainen <ccr@tnsp.org>
parents:
123
diff
changeset
|
104 |
aecf145e7c70
Some work on the voting backend.
Matti Hamalainen <ccr@tnsp.org>
parents:
123
diff
changeset
|
105 $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
|
106 if (($votekey = stFetchSQLColumn($sql)) !== false) |
aecf145e7c70
Some work on the voting backend.
Matti Hamalainen <ccr@tnsp.org>
parents:
123
diff
changeset
|
107 { |
aecf145e7c70
Some work on the voting backend.
Matti Hamalainen <ccr@tnsp.org>
parents:
123
diff
changeset
|
108 } |
93 | 109 |
153
aecf145e7c70
Some work on the voting backend.
Matti Hamalainen <ccr@tnsp.org>
parents:
123
diff
changeset
|
110 |
aecf145e7c70
Some work on the voting backend.
Matti Hamalainen <ccr@tnsp.org>
parents:
123
diff
changeset
|
111 $sql = stPrepareSQL("SELECT * FROM entries WHERE id=%d", $entry_id); |
aecf145e7c70
Some work on the voting backend.
Matti Hamalainen <ccr@tnsp.org>
parents:
123
diff
changeset
|
112 if (($entry = stFetchSQL($sql)) !== false) |
aecf145e7c70
Some work on the voting backend.
Matti Hamalainen <ccr@tnsp.org>
parents:
123
diff
changeset
|
113 { |
aecf145e7c70
Some work on the voting backend.
Matti Hamalainen <ccr@tnsp.org>
parents:
123
diff
changeset
|
114 if (($compo = stFetchSQL("SELECT * FROM compos WHERE id=".$entry["compo_id"])) !== false |
aecf145e7c70
Some work on the voting backend.
Matti Hamalainen <ccr@tnsp.org>
parents:
123
diff
changeset
|
115 && $compo["voting"] != 0) |
aecf145e7c70
Some work on the voting backend.
Matti Hamalainen <ccr@tnsp.org>
parents:
123
diff
changeset
|
116 { |
aecf145e7c70
Some work on the voting backend.
Matti Hamalainen <ccr@tnsp.org>
parents:
123
diff
changeset
|
117 stSubmitOneVote(stGetRequestItem("voter_id"), $entry_id, stGetRequestItem("vote")); |
aecf145e7c70
Some work on the voting backend.
Matti Hamalainen <ccr@tnsp.org>
parents:
123
diff
changeset
|
118 } |
aecf145e7c70
Some work on the voting backend.
Matti Hamalainen <ccr@tnsp.org>
parents:
123
diff
changeset
|
119 else |
aecf145e7c70
Some work on the voting backend.
Matti Hamalainen <ccr@tnsp.org>
parents:
123
diff
changeset
|
120 stSetStatus(902, "Voting not enabled on that compo."); |
aecf145e7c70
Some work on the voting backend.
Matti Hamalainen <ccr@tnsp.org>
parents:
123
diff
changeset
|
121 } |
aecf145e7c70
Some work on the voting backend.
Matti Hamalainen <ccr@tnsp.org>
parents:
123
diff
changeset
|
122 else |
aecf145e7c70
Some work on the voting backend.
Matti Hamalainen <ccr@tnsp.org>
parents:
123
diff
changeset
|
123 stSetStatus(902, "No such entry."); |
93 | 124 } |
125 else | |
126 stSetStatus(902, "No data."); | |
127 break; | |
128 | |
129 default: | |
130 stSetStatus(404, "Not Found"); | |
131 break; | |
132 } | |
133 | |
134 ?> |