Mercurial > hg > fapweb
annotate msite.inc.php @ 65:72b22729ae7e
Add function for generating vote keys into site lib.
author | Matti Hamalainen <ccr@tnsp.org> |
---|---|
date | Sat, 05 Oct 2013 12:51:31 +0300 |
parents | 71256605546b |
children | 230aacc22cb4 |
rev | line source |
---|---|
0 | 1 <? |
2 // | |
9
fa9b66f596bb
More work on settings storage.
Matti Hamalainen <ccr@tnsp.org>
parents:
8
diff
changeset
|
3 // FAPWEB - Demo Party Website System System |
33 | 4 // Generic and miscellaneous site support code |
9
fa9b66f596bb
More work on settings storage.
Matti Hamalainen <ccr@tnsp.org>
parents:
8
diff
changeset
|
5 // (C) Copyright 2012 Matti 'ccr' Hamalainen <ccr@tnsp.org> |
0 | 6 // |
9
fa9b66f596bb
More work on settings storage.
Matti Hamalainen <ccr@tnsp.org>
parents:
8
diff
changeset
|
7 |
fa9b66f596bb
More work on settings storage.
Matti Hamalainen <ccr@tnsp.org>
parents:
8
diff
changeset
|
8 // Globals and definitions |
0 | 9 $errorSet = FALSE; |
10 $errorMsg = ""; | |
11 | |
8
4c5f651aa107
Migrate certain settings to SQL database, cleanups, etc.
Matti Hamalainen <ccr@tnsp.org>
parents:
7
diff
changeset
|
12 define("VT_STR", 1); |
4c5f651aa107
Migrate certain settings to SQL database, cleanups, etc.
Matti Hamalainen <ccr@tnsp.org>
parents:
7
diff
changeset
|
13 define("VT_INT", 2); |
4c5f651aa107
Migrate certain settings to SQL database, cleanups, etc.
Matti Hamalainen <ccr@tnsp.org>
parents:
7
diff
changeset
|
14 define("VT_BOOL", 3); |
4c5f651aa107
Migrate certain settings to SQL database, cleanups, etc.
Matti Hamalainen <ccr@tnsp.org>
parents:
7
diff
changeset
|
15 define("VT_TEXT", 4); |
4c5f651aa107
Migrate certain settings to SQL database, cleanups, etc.
Matti Hamalainen <ccr@tnsp.org>
parents:
7
diff
changeset
|
16 |
51 | 17 define("SESS_USER", "user"); |
18 define("SESS_ADMIN", "admin"); | |
19 | |
8
4c5f651aa107
Migrate certain settings to SQL database, cleanups, etc.
Matti Hamalainen <ccr@tnsp.org>
parents:
7
diff
changeset
|
20 |
32 | 21 if (function_exists("ini_set")) |
22 { | |
23 // Use cookies to store the session ID on the client side | |
24 @ini_set("session.use_only_cookies", 1); | |
25 | |
26 // Disable transparent Session ID support | |
27 @ini_set("session.use_trans_sid", 0); | |
28 } | |
29 | |
30 | |
0 | 31 function stError($msg) |
32 { | |
33 global $errorSet, $errorMsg; | |
34 $errorSet = TRUE; | |
35 $errorMsg .= "<li>".$msg."</li>\n"; | |
36 } | |
37 | |
38 | |
39 function stCheckHTTPS() | |
40 { | |
41 return isset($_SERVER["HTTPS"]) && ($_SERVER["HTTPS"] != "" && $_SERVER["HTTPS"] != "off"); | |
42 } | |
43 | |
44 | |
5
76c3b89d7b11
Improve voting, clean up the code, etc.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
45 function stSetupCacheControl() |
76c3b89d7b11
Improve voting, clean up the code, etc.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
46 { |
8
4c5f651aa107
Migrate certain settings to SQL database, cleanups, etc.
Matti Hamalainen <ccr@tnsp.org>
parents:
7
diff
changeset
|
47 header("Cache-Control: must-revalidate, no-store, private"); |
4c5f651aa107
Migrate certain settings to SQL database, cleanups, etc.
Matti Hamalainen <ccr@tnsp.org>
parents:
7
diff
changeset
|
48 header("Expires: Sat, 26 Jul 1997 05:00:00 GMT"); // Date in the past |
5
76c3b89d7b11
Improve voting, clean up the code, etc.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
49 } |
76c3b89d7b11
Improve voting, clean up the code, etc.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
50 |
76c3b89d7b11
Improve voting, clean up the code, etc.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
51 |
8
4c5f651aa107
Migrate certain settings to SQL database, cleanups, etc.
Matti Hamalainen <ccr@tnsp.org>
parents:
7
diff
changeset
|
52 function stReloadSettings() |
4c5f651aa107
Migrate certain settings to SQL database, cleanups, etc.
Matti Hamalainen <ccr@tnsp.org>
parents:
7
diff
changeset
|
53 { |
4c5f651aa107
Migrate certain settings to SQL database, cleanups, etc.
Matti Hamalainen <ccr@tnsp.org>
parents:
7
diff
changeset
|
54 global $siteSettings; |
4c5f651aa107
Migrate certain settings to SQL database, cleanups, etc.
Matti Hamalainen <ccr@tnsp.org>
parents:
7
diff
changeset
|
55 $res = stExecSQL("SELECT * FROM settings"); |
4c5f651aa107
Migrate certain settings to SQL database, cleanups, etc.
Matti Hamalainen <ccr@tnsp.org>
parents:
7
diff
changeset
|
56 if ($res !== FALSE) |
4c5f651aa107
Migrate certain settings to SQL database, cleanups, etc.
Matti Hamalainen <ccr@tnsp.org>
parents:
7
diff
changeset
|
57 { |
4c5f651aa107
Migrate certain settings to SQL database, cleanups, etc.
Matti Hamalainen <ccr@tnsp.org>
parents:
7
diff
changeset
|
58 foreach ($res as $row) |
4c5f651aa107
Migrate certain settings to SQL database, cleanups, etc.
Matti Hamalainen <ccr@tnsp.org>
parents:
7
diff
changeset
|
59 { |
4c5f651aa107
Migrate certain settings to SQL database, cleanups, etc.
Matti Hamalainen <ccr@tnsp.org>
parents:
7
diff
changeset
|
60 switch ($row["vtype"]) |
4c5f651aa107
Migrate certain settings to SQL database, cleanups, etc.
Matti Hamalainen <ccr@tnsp.org>
parents:
7
diff
changeset
|
61 { |
4c5f651aa107
Migrate certain settings to SQL database, cleanups, etc.
Matti Hamalainen <ccr@tnsp.org>
parents:
7
diff
changeset
|
62 case VT_INT: $val = intval($row["vint"]); break; |
4c5f651aa107
Migrate certain settings to SQL database, cleanups, etc.
Matti Hamalainen <ccr@tnsp.org>
parents:
7
diff
changeset
|
63 case VT_BOOL: $val = intval($row["vint"]) ? true : false; break; |
4c5f651aa107
Migrate certain settings to SQL database, cleanups, etc.
Matti Hamalainen <ccr@tnsp.org>
parents:
7
diff
changeset
|
64 case VT_STR: $val = $row["vstr"]; break; |
4c5f651aa107
Migrate certain settings to SQL database, cleanups, etc.
Matti Hamalainen <ccr@tnsp.org>
parents:
7
diff
changeset
|
65 case VT_TEXT: $val = $row["vtext"]; break; |
4c5f651aa107
Migrate certain settings to SQL database, cleanups, etc.
Matti Hamalainen <ccr@tnsp.org>
parents:
7
diff
changeset
|
66 } |
4c5f651aa107
Migrate certain settings to SQL database, cleanups, etc.
Matti Hamalainen <ccr@tnsp.org>
parents:
7
diff
changeset
|
67 $siteSettings[$row["key"]] = $val; |
4c5f651aa107
Migrate certain settings to SQL database, cleanups, etc.
Matti Hamalainen <ccr@tnsp.org>
parents:
7
diff
changeset
|
68 } |
4c5f651aa107
Migrate certain settings to SQL database, cleanups, etc.
Matti Hamalainen <ccr@tnsp.org>
parents:
7
diff
changeset
|
69 } |
4c5f651aa107
Migrate certain settings to SQL database, cleanups, etc.
Matti Hamalainen <ccr@tnsp.org>
parents:
7
diff
changeset
|
70 else |
4c5f651aa107
Migrate certain settings to SQL database, cleanups, etc.
Matti Hamalainen <ccr@tnsp.org>
parents:
7
diff
changeset
|
71 die("Error fetching site settings."); |
4c5f651aa107
Migrate certain settings to SQL database, cleanups, etc.
Matti Hamalainen <ccr@tnsp.org>
parents:
7
diff
changeset
|
72 } |
4c5f651aa107
Migrate certain settings to SQL database, cleanups, etc.
Matti Hamalainen <ccr@tnsp.org>
parents:
7
diff
changeset
|
73 |
4c5f651aa107
Migrate certain settings to SQL database, cleanups, etc.
Matti Hamalainen <ccr@tnsp.org>
parents:
7
diff
changeset
|
74 |
5
76c3b89d7b11
Improve voting, clean up the code, etc.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
75 function stGetSetting($name) |
0 | 76 { |
8
4c5f651aa107
Migrate certain settings to SQL database, cleanups, etc.
Matti Hamalainen <ccr@tnsp.org>
parents:
7
diff
changeset
|
77 global $siteSettings; |
0 | 78 if (isset($siteSettings[$name])) |
79 return $siteSettings[$name]; | |
80 else | |
8
4c5f651aa107
Migrate certain settings to SQL database, cleanups, etc.
Matti Hamalainen <ccr@tnsp.org>
parents:
7
diff
changeset
|
81 die("No config value for '".$name."'.\n"); |
0 | 82 } |
83 | |
84 | |
85 function stChkSetting($name) | |
86 { | |
87 global $siteSettings; | |
88 return isset($siteSettings[$name]) && $siteSettings[$name]; | |
89 } | |
90 | |
91 | |
92 function dhentities($str) | |
93 { | |
94 return str_replace(array("<",">"), array("<", ">"), htmlentities($str, ENT_NOQUOTES, "UTF-8")); | |
95 } | |
96 | |
97 | |
98 function chentities($str) | |
99 { | |
100 return htmlentities($str, ENT_NOQUOTES, "UTF-8"); | |
101 } | |
102 | |
103 | |
12
358534308c61
Clean up some of the html item generation code.
Matti Hamalainen <ccr@tnsp.org>
parents:
9
diff
changeset
|
104 function stGetIDName($name, $id, $prefix = "") |
358534308c61
Clean up some of the html item generation code.
Matti Hamalainen <ccr@tnsp.org>
parents:
9
diff
changeset
|
105 { |
358534308c61
Clean up some of the html item generation code.
Matti Hamalainen <ccr@tnsp.org>
parents:
9
diff
changeset
|
106 return |
358534308c61
Clean up some of the html item generation code.
Matti Hamalainen <ccr@tnsp.org>
parents:
9
diff
changeset
|
107 ($id != "" ? "id=\"".$prefix.$name.$id."\" " : ""). |
358534308c61
Clean up some of the html item generation code.
Matti Hamalainen <ccr@tnsp.org>
parents:
9
diff
changeset
|
108 ($name != "" ? "name=\"".$prefix.$name.$id."\" " : ""); |
358534308c61
Clean up some of the html item generation code.
Matti Hamalainen <ccr@tnsp.org>
parents:
9
diff
changeset
|
109 } |
358534308c61
Clean up some of the html item generation code.
Matti Hamalainen <ccr@tnsp.org>
parents:
9
diff
changeset
|
110 |
358534308c61
Clean up some of the html item generation code.
Matti Hamalainen <ccr@tnsp.org>
parents:
9
diff
changeset
|
111 |
0 | 112 function stGetFormCheckBoxInput($name, $id, $prefix, $checked, $label, $extra = "") |
113 { | |
114 return | |
12
358534308c61
Clean up some of the html item generation code.
Matti Hamalainen <ccr@tnsp.org>
parents:
9
diff
changeset
|
115 "<input ".$extra." type=\"checkbox\" ".stGetIDName($name, $id, $prefix). |
358534308c61
Clean up some of the html item generation code.
Matti Hamalainen <ccr@tnsp.org>
parents:
9
diff
changeset
|
116 ($checked ? "checked=\"checked\" " : "")." />". |
358534308c61
Clean up some of the html item generation code.
Matti Hamalainen <ccr@tnsp.org>
parents:
9
diff
changeset
|
117 ($label != "" ? "<label for=\"".$name."\">".$label."</label>" : ""); |
0 | 118 } |
119 | |
120 | |
26
7be3f8cf1f7a
Lots of cleanups, preparing for adding entry submission support.
Matti Hamalainen <ccr@tnsp.org>
parents:
24
diff
changeset
|
121 function stGetFormRadioButtonInput($name, $id, $prefix, $value, $checked, $label, $extra = "") |
7be3f8cf1f7a
Lots of cleanups, preparing for adding entry submission support.
Matti Hamalainen <ccr@tnsp.org>
parents:
24
diff
changeset
|
122 { |
7be3f8cf1f7a
Lots of cleanups, preparing for adding entry submission support.
Matti Hamalainen <ccr@tnsp.org>
parents:
24
diff
changeset
|
123 return |
7be3f8cf1f7a
Lots of cleanups, preparing for adding entry submission support.
Matti Hamalainen <ccr@tnsp.org>
parents:
24
diff
changeset
|
124 "<input ".$extra." type=\"radio\" ".stGetIDName($name, $id, $prefix). |
7be3f8cf1f7a
Lots of cleanups, preparing for adding entry submission support.
Matti Hamalainen <ccr@tnsp.org>
parents:
24
diff
changeset
|
125 ($checked ? "checked=\"checked\" " : "")." value=\"".$value."\" />". |
7be3f8cf1f7a
Lots of cleanups, preparing for adding entry submission support.
Matti Hamalainen <ccr@tnsp.org>
parents:
24
diff
changeset
|
126 ($label != "" ? "<label for=\"".$name."\">".$label."</label>" : ""); |
7be3f8cf1f7a
Lots of cleanups, preparing for adding entry submission support.
Matti Hamalainen <ccr@tnsp.org>
parents:
24
diff
changeset
|
127 } |
7be3f8cf1f7a
Lots of cleanups, preparing for adding entry submission support.
Matti Hamalainen <ccr@tnsp.org>
parents:
24
diff
changeset
|
128 |
7be3f8cf1f7a
Lots of cleanups, preparing for adding entry submission support.
Matti Hamalainen <ccr@tnsp.org>
parents:
24
diff
changeset
|
129 |
0 | 130 function stGetFormButtonInput($name, $id, $prefix, $label, $onclick = "") |
131 { | |
132 return | |
12
358534308c61
Clean up some of the html item generation code.
Matti Hamalainen <ccr@tnsp.org>
parents:
9
diff
changeset
|
133 "<input type=\"button\" ".stGetIDName($name, $id, $prefix). |
26
7be3f8cf1f7a
Lots of cleanups, preparing for adding entry submission support.
Matti Hamalainen <ccr@tnsp.org>
parents:
24
diff
changeset
|
134 "value=\" ".chentities($label)." \" ". |
0 | 135 ($onclick != "" ? "onClick=\"".$onclick."\"" : "")." />"; |
136 } | |
137 | |
138 | |
139 function stGetFormTextArea($rows, $cols, $name, $id, $prefix, $value, $extra = "") | |
140 { | |
141 return | |
12
358534308c61
Clean up some of the html item generation code.
Matti Hamalainen <ccr@tnsp.org>
parents:
9
diff
changeset
|
142 "<textarea ".$extra." ".stGetIDName($name, $id, $prefix). |
358534308c61
Clean up some of the html item generation code.
Matti Hamalainen <ccr@tnsp.org>
parents:
9
diff
changeset
|
143 "rows=\"".$rows."\" cols=\"".$cols."\">". |
0 | 144 (isset($value) ? chentities($value) : ""). |
145 "</textarea>"; | |
146 } | |
147 | |
148 | |
149 function stGetFormTextInput($size, $len, $name, $id, $prefix, $value, $extra = "") | |
150 { | |
151 return | |
12
358534308c61
Clean up some of the html item generation code.
Matti Hamalainen <ccr@tnsp.org>
parents:
9
diff
changeset
|
152 "<input ".$extra." type=\"text\" ".stGetIDName($name, $id, $prefix). |
358534308c61
Clean up some of the html item generation code.
Matti Hamalainen <ccr@tnsp.org>
parents:
9
diff
changeset
|
153 "size=\"".$size."\" maxlength=\"".$len."\"". |
0 | 154 (isset($value) ? " value=\"".chentities($value)."\"" : ""). |
155 " />"; | |
156 } | |
157 | |
158 | |
159 function stGetFormPasswordInput($name, $id, $prefix) | |
160 { | |
161 return | |
13 | 162 "<input type=\"password\" ".stGetIDName($name, $id, $prefix)." />"; |
0 | 163 } |
164 | |
165 | |
26
7be3f8cf1f7a
Lots of cleanups, preparing for adding entry submission support.
Matti Hamalainen <ccr@tnsp.org>
parents:
24
diff
changeset
|
166 function stGetFormSubmitInput($name, $label, $onclick = "") |
7be3f8cf1f7a
Lots of cleanups, preparing for adding entry submission support.
Matti Hamalainen <ccr@tnsp.org>
parents:
24
diff
changeset
|
167 { |
7be3f8cf1f7a
Lots of cleanups, preparing for adding entry submission support.
Matti Hamalainen <ccr@tnsp.org>
parents:
24
diff
changeset
|
168 return |
7be3f8cf1f7a
Lots of cleanups, preparing for adding entry submission support.
Matti Hamalainen <ccr@tnsp.org>
parents:
24
diff
changeset
|
169 "<input type=\"submit\" name=\"".$name. |
7be3f8cf1f7a
Lots of cleanups, preparing for adding entry submission support.
Matti Hamalainen <ccr@tnsp.org>
parents:
24
diff
changeset
|
170 "\" value=\" ".chentities($label)." \" ". |
7be3f8cf1f7a
Lots of cleanups, preparing for adding entry submission support.
Matti Hamalainen <ccr@tnsp.org>
parents:
24
diff
changeset
|
171 ($onclick != "" ? "onClick=\"".$onclick."\"" : "")." />"; |
7be3f8cf1f7a
Lots of cleanups, preparing for adding entry submission support.
Matti Hamalainen <ccr@tnsp.org>
parents:
24
diff
changeset
|
172 } |
7be3f8cf1f7a
Lots of cleanups, preparing for adding entry submission support.
Matti Hamalainen <ccr@tnsp.org>
parents:
24
diff
changeset
|
173 |
7be3f8cf1f7a
Lots of cleanups, preparing for adding entry submission support.
Matti Hamalainen <ccr@tnsp.org>
parents:
24
diff
changeset
|
174 |
0 | 175 function stGetFormHiddenInput($name, $value) |
176 { | |
12
358534308c61
Clean up some of the html item generation code.
Matti Hamalainen <ccr@tnsp.org>
parents:
9
diff
changeset
|
177 return |
358534308c61
Clean up some of the html item generation code.
Matti Hamalainen <ccr@tnsp.org>
parents:
9
diff
changeset
|
178 "<input type=\"hidden\" name=\"".$name. |
358534308c61
Clean up some of the html item generation code.
Matti Hamalainen <ccr@tnsp.org>
parents:
9
diff
changeset
|
179 "\" value=\"".chentities($value)."\" />"; |
0 | 180 } |
181 | |
182 | |
26
7be3f8cf1f7a
Lots of cleanups, preparing for adding entry submission support.
Matti Hamalainen <ccr@tnsp.org>
parents:
24
diff
changeset
|
183 function stGetFormStart($name, $action = "", $method = "post") |
7be3f8cf1f7a
Lots of cleanups, preparing for adding entry submission support.
Matti Hamalainen <ccr@tnsp.org>
parents:
24
diff
changeset
|
184 { |
7be3f8cf1f7a
Lots of cleanups, preparing for adding entry submission support.
Matti Hamalainen <ccr@tnsp.org>
parents:
24
diff
changeset
|
185 return |
7be3f8cf1f7a
Lots of cleanups, preparing for adding entry submission support.
Matti Hamalainen <ccr@tnsp.org>
parents:
24
diff
changeset
|
186 "<form name=\"".$name."\" action=\"". |
7be3f8cf1f7a
Lots of cleanups, preparing for adding entry submission support.
Matti Hamalainen <ccr@tnsp.org>
parents:
24
diff
changeset
|
187 ($action != "" ? $action : $name). |
7be3f8cf1f7a
Lots of cleanups, preparing for adding entry submission support.
Matti Hamalainen <ccr@tnsp.org>
parents:
24
diff
changeset
|
188 "\" method=\"".$method."\">\n"; |
7be3f8cf1f7a
Lots of cleanups, preparing for adding entry submission support.
Matti Hamalainen <ccr@tnsp.org>
parents:
24
diff
changeset
|
189 } |
7be3f8cf1f7a
Lots of cleanups, preparing for adding entry submission support.
Matti Hamalainen <ccr@tnsp.org>
parents:
24
diff
changeset
|
190 |
7be3f8cf1f7a
Lots of cleanups, preparing for adding entry submission support.
Matti Hamalainen <ccr@tnsp.org>
parents:
24
diff
changeset
|
191 |
0 | 192 function stGetTDEditTextItem($edit, $size, $len, $name, $id, $prefix, $value, $extra = "") |
193 { | |
194 return | |
195 "<td class=\"".$name."\">". | |
196 ($edit ? stGetFormTextInput($size, $len, $name, $id, $prefix, $value, $extra) : chentities($value)). | |
197 "</td>"; | |
198 } | |
199 | |
200 | |
201 function stPrintFormTextInput($text1, $text2, $size, $len, $name, $extra="") | |
202 { | |
203 echo " <tr><th>".chentities($text1)."</th><td>". | |
15
ea0f98a0bed8
Fix some issues and bugs caused by recent changes.
Matti Hamalainen <ccr@tnsp.org>
parents:
13
diff
changeset
|
204 stGetFormTextInput($size, $len, $name, "", "", stGetRequestItem($name), $extra). |
0 | 205 "</td><td>".chentities($text2)."</td></tr>\n"; |
206 } | |
207 | |
208 | |
209 function stPrintFormHiddenInput($name, $value) | |
210 { | |
211 echo " ".stGetFormHiddenInput($name, $value)."\n"; | |
212 } | |
213 | |
214 | |
215 function stChkDataItem($name) | |
216 { | |
217 return !isset($_REQUEST[$name]) || strlen(trim($_REQUEST[$name])) < 1; | |
218 } | |
219 | |
220 | |
221 function stChkRequestItem($name) | |
222 { | |
223 return isset($_REQUEST[$name]); | |
224 } | |
225 | |
226 | |
227 function stGetRequestItem($name, $default = "") | |
228 { | |
229 return isset($_REQUEST[$name]) ? trim($_REQUEST[$name]) : $default; | |
230 } | |
231 | |
232 | |
233 function stGetDRequestItem($name, $default = "") | |
234 { | |
235 return trim(urldecode(stGetRequestItem($name, $default))); | |
236 } | |
237 | |
238 | |
239 function stLogSQLError($sql) | |
240 { | |
241 global $db; | |
242 error_log("SQL error ".implode("; ", $db->errorInfo())." in statement \"".$sql."\""); | |
243 } | |
244 | |
245 | |
246 function stConnectSQLDB() | |
247 { | |
248 global $db; | |
249 try { | |
8
4c5f651aa107
Migrate certain settings to SQL database, cleanups, etc.
Matti Hamalainen <ccr@tnsp.org>
parents:
7
diff
changeset
|
250 $db = new PDO(stGetSetting("sqlDB")); |
0 | 251 } |
252 catch (PDOException $e) { | |
253 error_log("Could not connect to SQL database: ".$e->getMessage()."."); | |
254 return FALSE; | |
255 } | |
256 return TRUE; | |
257 } | |
258 | |
259 | |
260 function stGetSQLParam($type, $value) | |
261 { | |
262 global $db; | |
263 switch ($type) | |
264 { | |
265 case "d": | |
266 return intval($value); | |
267 | |
268 case "s": | |
269 return $db->quote($value); | |
270 | |
271 case "D": | |
272 return intval(stGetRequestItem($value)); | |
273 | |
274 case "S": | |
275 return $db->quote(stGetDRequestItem($value)); | |
276 | |
277 case "Q": | |
278 return $db->quote(stripslashes(stGetDRequestItem($value))); | |
279 | |
280 case "B": | |
281 return intval(stGetRequestItem($value)) ? 1 : 0; | |
282 } | |
283 } | |
284 | |
285 | |
286 function stPrepareSQL() | |
287 { | |
288 $argc = func_num_args(); | |
289 $argv = func_get_args(); | |
290 if ($argc < 1) | |
291 { | |
292 error_log("Invalid stPrepareSQL() call, no arguments!"); | |
293 return FALSE; | |
294 } | |
295 | |
296 $fmt = $argv[0]; | |
297 $len = strlen($fmt); | |
298 $sql = ""; | |
299 $argn = 1; | |
300 $pos = 0; | |
301 while ($pos < $len) | |
302 { | |
303 if ($fmt[$pos] == "%") | |
304 { | |
305 if ($argn < $argc) | |
306 $sql .= stGetSQLParam($fmt[++$pos], $argv[$argn++]); | |
307 else | |
308 { | |
309 error_log("Invalid SQL statement format string '".$fmt. | |
310 "', not enough parameters specified (".$argn." of ".$argc.")"); | |
311 return FALSE; | |
312 } | |
313 } | |
314 else | |
315 $sql .= $fmt[$pos]; | |
316 $pos++; | |
317 } | |
318 | |
319 return $sql; | |
320 } | |
321 | |
322 | |
323 function stPrepareSQLUpdate($table, $cond, $pairs) | |
324 { | |
325 $sql = array(); | |
326 foreach ($pairs as $name => $attr) | |
327 { | |
328 $sql[] = $name."=".stGetSQLParam($attr, $name); | |
329 } | |
330 return | |
331 "UPDATE ".$table." SET ".implode(",", $sql). | |
332 ($cond != "" ? " ".$cond : ""); | |
333 } | |
334 | |
335 | |
336 function stExecSQL($sql) | |
337 { | |
338 global $db; | |
339 if (($res = $db->query($sql)) !== FALSE) | |
340 return $res; | |
341 else | |
342 { | |
343 stLogSQLError($sql); | |
344 stError("Oh noes! SQL error #23!"); | |
345 return FALSE; | |
346 } | |
347 } | |
348 | |
349 | |
350 function stFetchSQL($sql) | |
351 { | |
352 global $db; | |
353 if (($res = $db->query($sql)) !== FALSE) | |
354 { | |
355 return $res->fetch(); | |
356 } | |
357 else | |
358 { | |
359 stLogSQLError($sql); | |
360 stError("Oh noes! SQL error #31!"); | |
361 return FALSE; | |
362 } | |
363 } | |
364 | |
365 | |
366 function stFetchSQLColumn($sql, $column = 0) | |
367 { | |
368 global $db; | |
369 if (($res = $db->query($sql)) !== FALSE) | |
370 { | |
371 return $res->fetchColumn($column); | |
372 } | |
373 else | |
374 { | |
375 stLogSQLError($sql); | |
376 stError("Oh noes! SQL error #81!"); | |
377 return FALSE; | |
378 } | |
379 } | |
380 | |
381 | |
8
4c5f651aa107
Migrate certain settings to SQL database, cleanups, etc.
Matti Hamalainen <ccr@tnsp.org>
parents:
7
diff
changeset
|
382 // |
37 | 383 // Site-specific common functions .. these should be elsewhere |
384 // | |
385 function stPrintAttendee($item, $row, $edit, $eclass = "") | |
386 { | |
387 $id = $item["id"]; | |
388 $prefix = "at"; | |
389 echo " ". | |
53
71256605546b
More work on admin interface.
Matti Hamalainen <ccr@tnsp.org>
parents:
52
diff
changeset
|
390 " <tr class=\"".($item["active"] ? "vactive " : ""). |
71256605546b
More work on admin interface.
Matti Hamalainen <ccr@tnsp.org>
parents:
52
diff
changeset
|
391 ($row % 2 == 1 ? "rodd" : "reven")."\" id=\"attendee".$id."\">"; |
71256605546b
More work on admin interface.
Matti Hamalainen <ccr@tnsp.org>
parents:
52
diff
changeset
|
392 |
71256605546b
More work on admin interface.
Matti Hamalainen <ccr@tnsp.org>
parents:
52
diff
changeset
|
393 if ($edit) |
71256605546b
More work on admin interface.
Matti Hamalainen <ccr@tnsp.org>
parents:
52
diff
changeset
|
394 { |
71256605546b
More work on admin interface.
Matti Hamalainen <ccr@tnsp.org>
parents:
52
diff
changeset
|
395 echo |
71256605546b
More work on admin interface.
Matti Hamalainen <ccr@tnsp.org>
parents:
52
diff
changeset
|
396 stGetTDEditTextItem(FALSE, 20, 40, "name", $id, $prefix, $item["name"]." / ".$item["groups"]); |
71256605546b
More work on admin interface.
Matti Hamalainen <ccr@tnsp.org>
parents:
52
diff
changeset
|
397 } |
71256605546b
More work on admin interface.
Matti Hamalainen <ccr@tnsp.org>
parents:
52
diff
changeset
|
398 else |
71256605546b
More work on admin interface.
Matti Hamalainen <ccr@tnsp.org>
parents:
52
diff
changeset
|
399 { |
71256605546b
More work on admin interface.
Matti Hamalainen <ccr@tnsp.org>
parents:
52
diff
changeset
|
400 echo |
37 | 401 stGetTDEditTextItem(FALSE, 20, 40, "name", $id, $prefix, $item["name"]). |
402 stGetTDEditTextItem(FALSE, 20, 40, "groups", $id, $prefix, $item["groups"]). | |
53
71256605546b
More work on admin interface.
Matti Hamalainen <ccr@tnsp.org>
parents:
52
diff
changeset
|
403 "<td class=\"regtime\">".date("d M Y / H:i", $item["regtime"])."</td>"; |
71256605546b
More work on admin interface.
Matti Hamalainen <ccr@tnsp.org>
parents:
52
diff
changeset
|
404 } |
71256605546b
More work on admin interface.
Matti Hamalainen <ccr@tnsp.org>
parents:
52
diff
changeset
|
405 |
71256605546b
More work on admin interface.
Matti Hamalainen <ccr@tnsp.org>
parents:
52
diff
changeset
|
406 echo |
37 | 407 stGetTDEditTextItem($edit, 30, 64, "oneliner", $id, $prefix, $item["oneliner"], "autocomplete=\"off\""); |
408 | |
409 if ($edit) | |
410 { | |
411 echo | |
412 stGetTDEditTextItem($edit, 20, 40, "email", $id, $prefix, $item["email"], "autocomplete=\"off\""). | |
52
cba0b944da79
Start combining "users" with "attendees".
Matti Hamalainen <ccr@tnsp.org>
parents:
51
diff
changeset
|
413 |
37 | 414 "<td>". |
415 "<button class=\"button\" id=\"atupd".$id."\" type=\"button\" onclick=\"updateAttendee(".$id.")\">Update</button>". | |
416 "<button class=\"button\" id=\"atdel".$id."\" type=\"button\" onclick=\"deleteAttendee(".$id.")\">Delete</button>". | |
52
cba0b944da79
Start combining "users" with "attendees".
Matti Hamalainen <ccr@tnsp.org>
parents:
51
diff
changeset
|
417 "</td>". |
cba0b944da79
Start combining "users" with "attendees".
Matti Hamalainen <ccr@tnsp.org>
parents:
51
diff
changeset
|
418 |
cba0b944da79
Start combining "users" with "attendees".
Matti Hamalainen <ccr@tnsp.org>
parents:
51
diff
changeset
|
419 " <td class=\"vkey\">".chentities($item["key"])."</td>\n". |
cba0b944da79
Start combining "users" with "attendees".
Matti Hamalainen <ccr@tnsp.org>
parents:
51
diff
changeset
|
420 " <td class=\"vactive\">".stGetFormCheckBoxInput("active", $id, $prefix, $item["active"], "", |
cba0b944da79
Start combining "users" with "attendees".
Matti Hamalainen <ccr@tnsp.org>
parents:
51
diff
changeset
|
421 "onClick=\"updateAttendee2(".$id.")\"")."</td>\n"; |
37 | 422 } |
423 | |
424 echo "</tr>\n"; | |
425 } | |
426 | |
427 | |
428 function stPrintNewsItem($item, $edit = "") | |
429 { | |
430 echo | |
431 "<div class=\"newsitem\" id=\"news".$item["id"]."\">\n". | |
432 " <h2>".chentities($item["title"])."</h2>\n". | |
433 " <div class=\"text\">".dhentities($item["text"])."</div>\n". | |
434 " <div class=\"sig\">-- ".chentities($item["author"])."<br />". | |
435 date("d M Y / H:i", $item["utime"]). | |
436 $edit."</div>\n". | |
437 "</div>\n"; | |
438 } | |
439 | |
440 | |
441 function stGetCompoList($fvisible, $fvoting = FALSE) | |
442 { | |
443 global $compos; | |
444 | |
445 // Get entries and competitions into an array structure | |
446 $sql = "SELECT * FROM compos"; | |
447 if ($fvisible || $fvoting) | |
448 { | |
449 $sql .= " WHERE ".implode(" AND ", array($fvisible ? "visible<>0" : "", $fvoting ? "voting<>0" : "")); | |
450 } | |
451 foreach (stExecSQL($sql) as $compo) | |
452 { | |
453 $id = $compo["id"]; | |
454 | |
455 $compos[$compo["id"]] = array( | |
456 "name" => $compo["name"], | |
457 "entries" => array() | |
458 ); | |
459 | |
460 $sql = stPrepareSQL("SELECT * FROM entries WHERE compo_id=%d", $id); | |
461 foreach (stExecSQL($sql) as $entry) | |
462 { | |
463 $compos[$id]["entries"][$entry["id"]] = $entry; | |
464 } | |
465 } | |
466 } | |
467 | |
468 | |
65
72b22729ae7e
Add function for generating vote keys into site lib.
Matti Hamalainen <ccr@tnsp.org>
parents:
53
diff
changeset
|
469 function stGenerateUserKey($uid) |
72b22729ae7e
Add function for generating vote keys into site lib.
Matti Hamalainen <ccr@tnsp.org>
parents:
53
diff
changeset
|
470 { |
72b22729ae7e
Add function for generating vote keys into site lib.
Matti Hamalainen <ccr@tnsp.org>
parents:
53
diff
changeset
|
471 $keyChars = "abcdefghjkmnpqrstuvwxyzABCDEFGHJKLMNPQRSTUVWXYZ23456789"; |
72b22729ae7e
Add function for generating vote keys into site lib.
Matti Hamalainen <ccr@tnsp.org>
parents:
53
diff
changeset
|
472 |
72b22729ae7e
Add function for generating vote keys into site lib.
Matti Hamalainen <ccr@tnsp.org>
parents:
53
diff
changeset
|
473 while (TRUE) |
72b22729ae7e
Add function for generating vote keys into site lib.
Matti Hamalainen <ccr@tnsp.org>
parents:
53
diff
changeset
|
474 { |
72b22729ae7e
Add function for generating vote keys into site lib.
Matti Hamalainen <ccr@tnsp.org>
parents:
53
diff
changeset
|
475 // Generate one keycode |
72b22729ae7e
Add function for generating vote keys into site lib.
Matti Hamalainen <ccr@tnsp.org>
parents:
53
diff
changeset
|
476 $key = ""; |
72b22729ae7e
Add function for generating vote keys into site lib.
Matti Hamalainen <ccr@tnsp.org>
parents:
53
diff
changeset
|
477 for ($n = 0; $n < stGetSetting("userKeyLength"); $n++) |
72b22729ae7e
Add function for generating vote keys into site lib.
Matti Hamalainen <ccr@tnsp.org>
parents:
53
diff
changeset
|
478 $key .= $keyChars[rand() % strlen($keyChars)]; |
72b22729ae7e
Add function for generating vote keys into site lib.
Matti Hamalainen <ccr@tnsp.org>
parents:
53
diff
changeset
|
479 |
72b22729ae7e
Add function for generating vote keys into site lib.
Matti Hamalainen <ccr@tnsp.org>
parents:
53
diff
changeset
|
480 // Check if it already exists, to avoid duplicates |
72b22729ae7e
Add function for generating vote keys into site lib.
Matti Hamalainen <ccr@tnsp.org>
parents:
53
diff
changeset
|
481 $sql = stPrepareSQL("SELECT * FROM attendees WHERE key=%s", $key); |
72b22729ae7e
Add function for generating vote keys into site lib.
Matti Hamalainen <ccr@tnsp.org>
parents:
53
diff
changeset
|
482 if (($res = @$db->query($sql)) !== FALSE) |
72b22729ae7e
Add function for generating vote keys into site lib.
Matti Hamalainen <ccr@tnsp.org>
parents:
53
diff
changeset
|
483 { |
72b22729ae7e
Add function for generating vote keys into site lib.
Matti Hamalainen <ccr@tnsp.org>
parents:
53
diff
changeset
|
484 if ($res->fetchColumn() === FALSE) |
72b22729ae7e
Add function for generating vote keys into site lib.
Matti Hamalainen <ccr@tnsp.org>
parents:
53
diff
changeset
|
485 { |
72b22729ae7e
Add function for generating vote keys into site lib.
Matti Hamalainen <ccr@tnsp.org>
parents:
53
diff
changeset
|
486 // Nope, add into database |
72b22729ae7e
Add function for generating vote keys into site lib.
Matti Hamalainen <ccr@tnsp.org>
parents:
53
diff
changeset
|
487 $sql = stPrepareSQL( |
72b22729ae7e
Add function for generating vote keys into site lib.
Matti Hamalainen <ccr@tnsp.org>
parents:
53
diff
changeset
|
488 "UPDATE attendees SET key=%s,active=0 WHERE id=%d", |
72b22729ae7e
Add function for generating vote keys into site lib.
Matti Hamalainen <ccr@tnsp.org>
parents:
53
diff
changeset
|
489 $key, $uid); |
72b22729ae7e
Add function for generating vote keys into site lib.
Matti Hamalainen <ccr@tnsp.org>
parents:
53
diff
changeset
|
490 |
72b22729ae7e
Add function for generating vote keys into site lib.
Matti Hamalainen <ccr@tnsp.org>
parents:
53
diff
changeset
|
491 if (($res = $db->query($sql)) === FALSE) |
72b22729ae7e
Add function for generating vote keys into site lib.
Matti Hamalainen <ccr@tnsp.org>
parents:
53
diff
changeset
|
492 { |
72b22729ae7e
Add function for generating vote keys into site lib.
Matti Hamalainen <ccr@tnsp.org>
parents:
53
diff
changeset
|
493 stLogSQLError($sql); |
72b22729ae7e
Add function for generating vote keys into site lib.
Matti Hamalainen <ccr@tnsp.org>
parents:
53
diff
changeset
|
494 return FALSE; |
72b22729ae7e
Add function for generating vote keys into site lib.
Matti Hamalainen <ccr@tnsp.org>
parents:
53
diff
changeset
|
495 } |
72b22729ae7e
Add function for generating vote keys into site lib.
Matti Hamalainen <ccr@tnsp.org>
parents:
53
diff
changeset
|
496 |
72b22729ae7e
Add function for generating vote keys into site lib.
Matti Hamalainen <ccr@tnsp.org>
parents:
53
diff
changeset
|
497 return TRUE; |
72b22729ae7e
Add function for generating vote keys into site lib.
Matti Hamalainen <ccr@tnsp.org>
parents:
53
diff
changeset
|
498 } |
72b22729ae7e
Add function for generating vote keys into site lib.
Matti Hamalainen <ccr@tnsp.org>
parents:
53
diff
changeset
|
499 } |
72b22729ae7e
Add function for generating vote keys into site lib.
Matti Hamalainen <ccr@tnsp.org>
parents:
53
diff
changeset
|
500 else |
72b22729ae7e
Add function for generating vote keys into site lib.
Matti Hamalainen <ccr@tnsp.org>
parents:
53
diff
changeset
|
501 { |
72b22729ae7e
Add function for generating vote keys into site lib.
Matti Hamalainen <ccr@tnsp.org>
parents:
53
diff
changeset
|
502 stLogSQLError($sql); |
72b22729ae7e
Add function for generating vote keys into site lib.
Matti Hamalainen <ccr@tnsp.org>
parents:
53
diff
changeset
|
503 return FALSE; |
72b22729ae7e
Add function for generating vote keys into site lib.
Matti Hamalainen <ccr@tnsp.org>
parents:
53
diff
changeset
|
504 } |
72b22729ae7e
Add function for generating vote keys into site lib.
Matti Hamalainen <ccr@tnsp.org>
parents:
53
diff
changeset
|
505 } |
72b22729ae7e
Add function for generating vote keys into site lib.
Matti Hamalainen <ccr@tnsp.org>
parents:
53
diff
changeset
|
506 } |
0 | 507 ?> |