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