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