Mercurial > hg > fapweb
annotate msite.inc.php @ 7:d76020022881
Various fixes, cleanups and improvements.
author | Matti Hamalainen <ccr@tnsp.org> |
---|---|
date | Wed, 05 Dec 2012 09:35:30 +0200 |
parents | 76c3b89d7b11 |
children | 4c5f651aa107 |
rev | line source |
---|---|
0 | 1 <? |
2 // | |
3 // Nothing to touch after this, mostly | |
4 // | |
5 $errorSet = FALSE; | |
6 $errorMsg = ""; | |
7 | |
8 function stError($msg) | |
9 { | |
10 global $errorSet, $errorMsg; | |
11 $errorSet = TRUE; | |
12 $errorMsg .= "<li>".$msg."</li>\n"; | |
13 } | |
14 | |
15 | |
16 function stCheckHTTPS() | |
17 { | |
18 return isset($_SERVER["HTTPS"]) && ($_SERVER["HTTPS"] != "" && $_SERVER["HTTPS"] != "off"); | |
19 } | |
20 | |
21 | |
5
76c3b89d7b11
Improve voting, clean up the code, etc.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
22 function stSetupCacheControl() |
76c3b89d7b11
Improve voting, clean up the code, etc.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
23 { |
76c3b89d7b11
Improve voting, clean up the code, etc.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
24 header("Cache-Control: private"); |
76c3b89d7b11
Improve voting, clean up the code, etc.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
25 header("Cache-Control: must-revalidate"); |
76c3b89d7b11
Improve voting, clean up the code, etc.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
26 header("Cache-Control: no-store"); |
76c3b89d7b11
Improve voting, clean up the code, etc.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
27 } |
76c3b89d7b11
Improve voting, clean up the code, etc.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
28 |
76c3b89d7b11
Improve voting, clean up the code, etc.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
29 |
76c3b89d7b11
Improve voting, clean up the code, etc.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
30 function stSessionExpire($type) |
0 | 31 { |
5
76c3b89d7b11
Improve voting, clean up the code, etc.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
32 // Check for session expiration |
7
d76020022881
Various fixes, cleanups and improvements.
Matti Hamalainen <ccr@tnsp.org>
parents:
5
diff
changeset
|
33 if (!isset($_SESSION["expires"])) |
d76020022881
Various fixes, cleanups and improvements.
Matti Hamalainen <ccr@tnsp.org>
parents:
5
diff
changeset
|
34 { |
d76020022881
Various fixes, cleanups and improvements.
Matti Hamalainen <ccr@tnsp.org>
parents:
5
diff
changeset
|
35 if (stGetSetting("debug")) error_log("Session $type expires due to expire time not set."); |
5
76c3b89d7b11
Improve voting, clean up the code, etc.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
36 return FALSE; |
7
d76020022881
Various fixes, cleanups and improvements.
Matti Hamalainen <ccr@tnsp.org>
parents:
5
diff
changeset
|
37 } |
d76020022881
Various fixes, cleanups and improvements.
Matti Hamalainen <ccr@tnsp.org>
parents:
5
diff
changeset
|
38 |
d76020022881
Various fixes, cleanups and improvements.
Matti Hamalainen <ccr@tnsp.org>
parents:
5
diff
changeset
|
39 if ($_SESSION["expires"] < time()) |
d76020022881
Various fixes, cleanups and improvements.
Matti Hamalainen <ccr@tnsp.org>
parents:
5
diff
changeset
|
40 { |
d76020022881
Various fixes, cleanups and improvements.
Matti Hamalainen <ccr@tnsp.org>
parents:
5
diff
changeset
|
41 if (stGetSetting("debug")) error_log("Session $type / ".session_id()." expires due to timeout ".$_SESSION["expires"]." < ".time()); |
d76020022881
Various fixes, cleanups and improvements.
Matti Hamalainen <ccr@tnsp.org>
parents:
5
diff
changeset
|
42 return FALSE; |
d76020022881
Various fixes, cleanups and improvements.
Matti Hamalainen <ccr@tnsp.org>
parents:
5
diff
changeset
|
43 } |
5
76c3b89d7b11
Improve voting, clean up the code, etc.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
44 |
76c3b89d7b11
Improve voting, clean up the code, etc.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
45 // Add more time to expiration |
7
d76020022881
Various fixes, cleanups and improvements.
Matti Hamalainen <ccr@tnsp.org>
parents:
5
diff
changeset
|
46 if (stGetSetting("debug")) error_log("Adding more time to $type session ".session_id()." :: ".stGetSetting($type)); |
5
76c3b89d7b11
Improve voting, clean up the code, etc.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
47 $_SESSION["expires"] = time() + stGetSetting($type); |
76c3b89d7b11
Improve voting, clean up the code, etc.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
48 return TRUE; |
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 |
7
d76020022881
Various fixes, cleanups and improvements.
Matti Hamalainen <ccr@tnsp.org>
parents:
5
diff
changeset
|
52 function stSessionEnd($ok) |
d76020022881
Various fixes, cleanups and improvements.
Matti Hamalainen <ccr@tnsp.org>
parents:
5
diff
changeset
|
53 { |
d76020022881
Various fixes, cleanups and improvements.
Matti Hamalainen <ccr@tnsp.org>
parents:
5
diff
changeset
|
54 if (stGetSetting("debug")) error_log("END session ".$_SESSION["type"]." / ".$_SESSION["expires"]." == ".$ok); |
d76020022881
Various fixes, cleanups and improvements.
Matti Hamalainen <ccr@tnsp.org>
parents:
5
diff
changeset
|
55 $_SESSION = array(); |
d76020022881
Various fixes, cleanups and improvements.
Matti Hamalainen <ccr@tnsp.org>
parents:
5
diff
changeset
|
56 if (ini_get("session.use_cookies")) |
d76020022881
Various fixes, cleanups and improvements.
Matti Hamalainen <ccr@tnsp.org>
parents:
5
diff
changeset
|
57 { |
d76020022881
Various fixes, cleanups and improvements.
Matti Hamalainen <ccr@tnsp.org>
parents:
5
diff
changeset
|
58 $params = session_get_cookie_params(); |
d76020022881
Various fixes, cleanups and improvements.
Matti Hamalainen <ccr@tnsp.org>
parents:
5
diff
changeset
|
59 setcookie(session_name(), "", time() - 242000, |
d76020022881
Various fixes, cleanups and improvements.
Matti Hamalainen <ccr@tnsp.org>
parents:
5
diff
changeset
|
60 $params["path"], $params["domain"], |
d76020022881
Various fixes, cleanups and improvements.
Matti Hamalainen <ccr@tnsp.org>
parents:
5
diff
changeset
|
61 $params["secure"], $params["httponly"] |
d76020022881
Various fixes, cleanups and improvements.
Matti Hamalainen <ccr@tnsp.org>
parents:
5
diff
changeset
|
62 ); |
d76020022881
Various fixes, cleanups and improvements.
Matti Hamalainen <ccr@tnsp.org>
parents:
5
diff
changeset
|
63 } |
d76020022881
Various fixes, cleanups and improvements.
Matti Hamalainen <ccr@tnsp.org>
parents:
5
diff
changeset
|
64 @session_destroy(); |
d76020022881
Various fixes, cleanups and improvements.
Matti Hamalainen <ccr@tnsp.org>
parents:
5
diff
changeset
|
65 return $ok; |
d76020022881
Various fixes, cleanups and improvements.
Matti Hamalainen <ccr@tnsp.org>
parents:
5
diff
changeset
|
66 } |
d76020022881
Various fixes, cleanups and improvements.
Matti Hamalainen <ccr@tnsp.org>
parents:
5
diff
changeset
|
67 |
d76020022881
Various fixes, cleanups and improvements.
Matti Hamalainen <ccr@tnsp.org>
parents:
5
diff
changeset
|
68 |
5
76c3b89d7b11
Improve voting, clean up the code, etc.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
69 function stAdmSessionAuth() |
76c3b89d7b11
Improve voting, clean up the code, etc.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
70 { |
76c3b89d7b11
Improve voting, clean up the code, etc.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
71 if (@session_start() === TRUE && isset($_SESSION["admPassword"]) && |
76c3b89d7b11
Improve voting, clean up the code, etc.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
72 $_SESSION["admPassword"] == stGetSetting("admPassword")) |
7
d76020022881
Various fixes, cleanups and improvements.
Matti Hamalainen <ccr@tnsp.org>
parents:
5
diff
changeset
|
73 { |
d76020022881
Various fixes, cleanups and improvements.
Matti Hamalainen <ccr@tnsp.org>
parents:
5
diff
changeset
|
74 if (stGetSetting("debug")) error_log("AUTH admin session."); |
5
76c3b89d7b11
Improve voting, clean up the code, etc.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
75 return stSessionExpire("admTimeout"); |
7
d76020022881
Various fixes, cleanups and improvements.
Matti Hamalainen <ccr@tnsp.org>
parents:
5
diff
changeset
|
76 } |
5
76c3b89d7b11
Improve voting, clean up the code, etc.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
77 else |
76c3b89d7b11
Improve voting, clean up the code, etc.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
78 return FALSE; |
76c3b89d7b11
Improve voting, clean up the code, etc.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
79 } |
76c3b89d7b11
Improve voting, clean up the code, etc.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
80 |
76c3b89d7b11
Improve voting, clean up the code, etc.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
81 |
76c3b89d7b11
Improve voting, clean up the code, etc.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
82 function stAdmSessionStart() |
76c3b89d7b11
Improve voting, clean up the code, etc.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
83 { |
76c3b89d7b11
Improve voting, clean up the code, etc.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
84 if (@session_start() === TRUE) |
0 | 85 { |
7
d76020022881
Various fixes, cleanups and improvements.
Matti Hamalainen <ccr@tnsp.org>
parents:
5
diff
changeset
|
86 if (stGetSetting("debug")) error_log("START admin session OK."); |
d76020022881
Various fixes, cleanups and improvements.
Matti Hamalainen <ccr@tnsp.org>
parents:
5
diff
changeset
|
87 $_SESSION["type"] = "admin"; |
5
76c3b89d7b11
Improve voting, clean up the code, etc.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
88 $_SESSION["admPassword"] = stGetSetting("admPassword"); |
76c3b89d7b11
Improve voting, clean up the code, etc.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
89 $_SESSION["expires"] = time() + stGetSetting("admTimeout"); |
0 | 90 return TRUE; |
91 } | |
92 else | |
7
d76020022881
Various fixes, cleanups and improvements.
Matti Hamalainen <ccr@tnsp.org>
parents:
5
diff
changeset
|
93 { |
d76020022881
Various fixes, cleanups and improvements.
Matti Hamalainen <ccr@tnsp.org>
parents:
5
diff
changeset
|
94 if (stGetSetting("debug")) error_log("START admin session --FAILED--"); |
0 | 95 return FALSE; |
7
d76020022881
Various fixes, cleanups and improvements.
Matti Hamalainen <ccr@tnsp.org>
parents:
5
diff
changeset
|
96 } |
0 | 97 } |
98 | |
99 | |
5
76c3b89d7b11
Improve voting, clean up the code, etc.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
100 function stVoteSessionAuth() |
76c3b89d7b11
Improve voting, clean up the code, etc.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
101 { |
76c3b89d7b11
Improve voting, clean up the code, etc.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
102 if (@session_start() === TRUE && isset($_SESSION["key"]) && isset($_SESSION["votes"])) |
76c3b89d7b11
Improve voting, clean up the code, etc.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
103 return stSessionExpire("voteTimeout"); |
76c3b89d7b11
Improve voting, clean up the code, etc.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
104 else |
76c3b89d7b11
Improve voting, clean up the code, etc.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
105 return FALSE; |
76c3b89d7b11
Improve voting, clean up the code, etc.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
106 } |
76c3b89d7b11
Improve voting, clean up the code, etc.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
107 |
76c3b89d7b11
Improve voting, clean up the code, etc.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
108 |
76c3b89d7b11
Improve voting, clean up the code, etc.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
109 function stVoteSessionStart() |
0 | 110 { |
111 if (@session_start() === TRUE) | |
112 { | |
7
d76020022881
Various fixes, cleanups and improvements.
Matti Hamalainen <ccr@tnsp.org>
parents:
5
diff
changeset
|
113 $_SESSION["type"] = "vote"; |
5
76c3b89d7b11
Improve voting, clean up the code, etc.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
114 $_SESSION["expires"] = time() + stGetSetting("voteTimeout"); |
0 | 115 return TRUE; |
116 } | |
117 else | |
118 return FALSE; | |
119 } | |
120 | |
121 | |
5
76c3b89d7b11
Improve voting, clean up the code, etc.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
122 function stSetVoteStatus($st) |
0 | 123 { |
5
76c3b89d7b11
Improve voting, clean up the code, etc.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
124 $_SESSION["status"] = $st; |
76c3b89d7b11
Improve voting, clean up the code, etc.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
125 } |
76c3b89d7b11
Improve voting, clean up the code, etc.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
126 |
76c3b89d7b11
Improve voting, clean up the code, etc.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
127 |
76c3b89d7b11
Improve voting, clean up the code, etc.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
128 function stGetSetting($name) |
0 | 129 { |
5
76c3b89d7b11
Improve voting, clean up the code, etc.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
130 global $siteSettings, $siteDefaults; |
0 | 131 if (isset($siteSettings[$name])) |
132 return $siteSettings[$name]; | |
133 else | |
5
76c3b89d7b11
Improve voting, clean up the code, etc.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
134 if (isset($siteDefaults[$name])) |
76c3b89d7b11
Improve voting, clean up the code, etc.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
135 return $siteDefaults[$name]; |
76c3b89d7b11
Improve voting, clean up the code, etc.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
136 else |
76c3b89d7b11
Improve voting, clean up the code, etc.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
137 die("No config value or default for '".$name."'.\n"); |
0 | 138 } |
139 | |
140 | |
141 function stChkSetting($name) | |
142 { | |
143 global $siteSettings; | |
144 return isset($siteSettings[$name]) && $siteSettings[$name]; | |
145 } | |
146 | |
147 | |
148 function stSpecURL($id) | |
149 { | |
150 global $specURLs; | |
151 if (isset($specURLs[$id])) | |
152 return "<a href=\"".$specURLs[$id][0]."\">".$specURLs[$id][1]."</a>"; | |
153 else | |
154 return ""; | |
155 } | |
156 | |
157 | |
158 function stPrintSpecURL($id) | |
159 { | |
160 echo stSpecURL($id); | |
161 } | |
162 | |
163 | |
164 function dhentities($str) | |
165 { | |
166 return str_replace(array("<",">"), array("<", ">"), htmlentities($str, ENT_NOQUOTES, "UTF-8")); | |
167 } | |
168 | |
169 | |
170 function chentities($str) | |
171 { | |
172 return htmlentities($str, ENT_NOQUOTES, "UTF-8"); | |
173 } | |
174 | |
175 | |
176 function stGetFormCheckBoxInput($name, $id, $prefix, $checked, $label, $extra = "") | |
177 { | |
178 return | |
179 "<input ".$extra." type=\"checkbox\" id=\"".$prefix.$name.$id."\" ". | |
180 "name=\"".$prefix.$name.$id."\" ".($checked ? "checked=\"checked\" " : "")." />". | |
5
76c3b89d7b11
Improve voting, clean up the code, etc.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
181 ($label != "" ? "<label for=\"".$prefix.$name.$id."\">".$label."</label>" : ""); |
0 | 182 } |
183 | |
184 | |
185 function stGetFormButtonInput($name, $id, $prefix, $label, $onclick = "") | |
186 { | |
187 return | |
188 "<input type=\"button\" id=\"".$prefix.$name.$id."\" ". | |
189 "name=\"".$name."\" value=\"".$label."\" ". | |
190 ($onclick != "" ? "onClick=\"".$onclick."\"" : "")." />"; | |
191 } | |
192 | |
193 | |
194 function stGetFormTextArea($rows, $cols, $name, $id, $prefix, $value, $extra = "") | |
195 { | |
196 return | |
197 "<textarea ".$extra." id=\"".$prefix.$name.$id."\" ". | |
198 "name=\"".$name."\" rows=\"".$rows."\" cols=\"".$cols."\">". | |
199 (isset($value) ? chentities($value) : ""). | |
200 "</textarea>"; | |
201 } | |
202 | |
203 | |
204 function stGetFormTextInput($size, $len, $name, $id, $prefix, $value, $extra = "") | |
205 { | |
206 return | |
207 "<input ".$extra." type=\"text\" id=\"".$prefix.$name.$id."\" ". | |
208 "name=\"".$name."\" size=\"".$size."\" maxlength=\"".$len."\"". | |
209 (isset($value) ? " value=\"".chentities($value)."\"" : ""). | |
210 " />"; | |
211 } | |
212 | |
213 | |
214 function stGetFormPasswordInput($name, $id, $prefix) | |
215 { | |
216 return | |
217 "<input type=\"password\" id=\"".$prefix.$name.$id. | |
218 "\" name=\"".$name."\" />"; | |
219 } | |
220 | |
221 | |
222 function stGetFormHiddenInput($name, $value) | |
223 { | |
224 return "<input type=\"hidden\" name=\"".$name."\" value=\"".chentities($value)."\" />"; | |
225 } | |
226 | |
227 | |
228 function stGetTDEditTextItem($edit, $size, $len, $name, $id, $prefix, $value, $extra = "") | |
229 { | |
230 return | |
231 "<td class=\"".$name."\">". | |
232 ($edit ? stGetFormTextInput($size, $len, $name, $id, $prefix, $value, $extra) : chentities($value)). | |
233 "</td>"; | |
234 } | |
235 | |
236 | |
237 function stPrintFormTextInput($text1, $text2, $size, $len, $name, $extra="") | |
238 { | |
239 echo " <tr><th>".chentities($text1)."</th><td>". | |
240 stGetFormTextInput($size, $len, $name, "", "", $_REQUEST[$name], $extra). | |
241 "</td><td>".chentities($text2)."</td></tr>\n"; | |
242 } | |
243 | |
244 | |
245 function stPrintFormHiddenInput($name, $value) | |
246 { | |
247 echo " ".stGetFormHiddenInput($name, $value)."\n"; | |
248 } | |
249 | |
250 | |
251 function stPrintAttendee($item, $row, $edit, $eclass = "") | |
252 { | |
253 $id = $item["id"]; | |
254 $prefix = "at"; | |
255 echo " ". | |
256 "<tr class=\"".($row % 2 == 1 ? "rodd" : "reven").$eclass."\" id=\"attendee".$id."\">". | |
257 stGetTDEditTextItem(FALSE, 20, 40, "name", $id, $prefix, $item["name"]). | |
258 stGetTDEditTextItem(FALSE, 20, 40, "groups", $id, $prefix, $item["groups"]). | |
259 "<td class=\"regtime\">".date("d M Y / H:i", $item["regtime"])."</td>". | |
260 stGetTDEditTextItem($edit, 30, 64, "oneliner", $id, $prefix, $item["oneliner"], "autocomplete=\"off\""); | |
261 | |
262 if ($edit) | |
263 { | |
264 echo | |
265 stGetTDEditTextItem($edit, 20, 40, "email", $id, $prefix, $item["email"], "autocomplete=\"off\""). | |
266 "<td>". | |
267 "<button class=\"button\" id=\"atupd".$id."\" type=\"button\" onclick=\"updateAttendee(".$id.")\">Update</button>". | |
268 "<button class=\"button\" id=\"atdel".$id."\" type=\"button\" onclick=\"deleteAttendee(".$id.")\">Delete</button>". | |
269 "</td>"; | |
270 } | |
271 | |
272 echo "</tr>\n"; | |
273 } | |
274 | |
275 | |
276 function stPrintNewsItem($item, $edit = "") | |
277 { | |
278 echo | |
279 "<div class=\"newsitem\" id=\"news".$item["id"]."\">\n". | |
280 " <h2>".chentities($item["title"])."</h2>\n". | |
281 " <div class=\"text\">".dhentities($item["text"])."</div>\n". | |
282 " <div class=\"sig\">-- ".chentities($item["author"])."<br />". | |
283 date("d M Y / H:i", $item["utime"]). | |
284 $edit."</div>\n". | |
285 "</div>\n"; | |
286 } | |
287 | |
288 | |
289 function stChkDataItem($name) | |
290 { | |
291 return !isset($_REQUEST[$name]) || strlen(trim($_REQUEST[$name])) < 1; | |
292 } | |
293 | |
294 | |
295 function stChkRequestItem($name) | |
296 { | |
297 return isset($_REQUEST[$name]); | |
298 } | |
299 | |
300 | |
301 function stGetRequestItem($name, $default = "") | |
302 { | |
303 return isset($_REQUEST[$name]) ? trim($_REQUEST[$name]) : $default; | |
304 } | |
305 | |
306 | |
307 function stGetDRequestItem($name, $default = "") | |
308 { | |
309 return trim(urldecode(stGetRequestItem($name, $default))); | |
310 } | |
311 | |
312 | |
313 function stLogSQLError($sql) | |
314 { | |
315 global $db; | |
316 error_log("SQL error ".implode("; ", $db->errorInfo())." in statement \"".$sql."\""); | |
317 } | |
318 | |
319 | |
320 function stConnectSQLDB() | |
321 { | |
322 global $db; | |
323 try { | |
5
76c3b89d7b11
Improve voting, clean up the code, etc.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
324 $db = new PDO("sqlite:".stGetSetting("sqlDBfilename")); |
0 | 325 } |
326 catch (PDOException $e) { | |
327 error_log("Could not connect to SQL database: ".$e->getMessage()."."); | |
328 return FALSE; | |
329 } | |
330 return TRUE; | |
331 } | |
332 | |
333 | |
334 function stGetSQLParam($type, $value) | |
335 { | |
336 global $db; | |
337 switch ($type) | |
338 { | |
339 case "d": | |
340 return intval($value); | |
341 | |
342 case "s": | |
343 return $db->quote($value); | |
344 | |
345 case "D": | |
346 return intval(stGetRequestItem($value)); | |
347 | |
348 case "S": | |
349 return $db->quote(stGetDRequestItem($value)); | |
350 | |
351 case "Q": | |
352 return $db->quote(stripslashes(stGetDRequestItem($value))); | |
353 | |
354 case "B": | |
355 return intval(stGetRequestItem($value)) ? 1 : 0; | |
356 } | |
357 } | |
358 | |
359 | |
360 function stPrepareSQL() | |
361 { | |
362 $argc = func_num_args(); | |
363 $argv = func_get_args(); | |
364 if ($argc < 1) | |
365 { | |
366 error_log("Invalid stPrepareSQL() call, no arguments!"); | |
367 return FALSE; | |
368 } | |
369 | |
370 $fmt = $argv[0]; | |
371 $len = strlen($fmt); | |
372 $sql = ""; | |
373 $argn = 1; | |
374 $pos = 0; | |
375 while ($pos < $len) | |
376 { | |
377 if ($fmt[$pos] == "%") | |
378 { | |
379 if ($argn < $argc) | |
380 $sql .= stGetSQLParam($fmt[++$pos], $argv[$argn++]); | |
381 else | |
382 { | |
383 error_log("Invalid SQL statement format string '".$fmt. | |
384 "', not enough parameters specified (".$argn." of ".$argc.")"); | |
385 return FALSE; | |
386 } | |
387 } | |
388 else | |
389 $sql .= $fmt[$pos]; | |
390 $pos++; | |
391 } | |
392 | |
393 return $sql; | |
394 } | |
395 | |
396 | |
397 function stPrepareSQLUpdate($table, $cond, $pairs) | |
398 { | |
399 $sql = array(); | |
400 foreach ($pairs as $name => $attr) | |
401 { | |
402 $sql[] = $name."=".stGetSQLParam($attr, $name); | |
403 } | |
404 return | |
405 "UPDATE ".$table." SET ".implode(",", $sql). | |
406 ($cond != "" ? " ".$cond : ""); | |
407 } | |
408 | |
409 | |
410 function stExecSQL($sql) | |
411 { | |
412 global $db; | |
413 if (($res = $db->query($sql)) !== FALSE) | |
414 return $res; | |
415 else | |
416 { | |
417 stLogSQLError($sql); | |
418 stError("Oh noes! SQL error #23!"); | |
419 return FALSE; | |
420 } | |
421 } | |
422 | |
423 | |
424 function stFetchSQL($sql) | |
425 { | |
426 global $db; | |
427 if (($res = $db->query($sql)) !== FALSE) | |
428 { | |
429 return $res->fetch(); | |
430 } | |
431 else | |
432 { | |
433 stLogSQLError($sql); | |
434 stError("Oh noes! SQL error #31!"); | |
435 return FALSE; | |
436 } | |
437 } | |
438 | |
439 | |
440 function stFetchSQLColumn($sql, $column = 0) | |
441 { | |
442 global $db; | |
443 if (($res = $db->query($sql)) !== FALSE) | |
444 { | |
445 return $res->fetchColumn($column); | |
446 } | |
447 else | |
448 { | |
449 stLogSQLError($sql); | |
450 stError("Oh noes! SQL error #81!"); | |
451 return FALSE; | |
452 } | |
453 } | |
454 | |
455 | |
456 function stGetCompoList($all) | |
457 { | |
458 global $compos; | |
459 | |
460 // Get entries and competitions into an array structure | |
461 $sql = "SELECT * FROM compos ".($all ? "" :"WHERE enabled<>0 ")."ORDER BY name DESC"; | |
462 foreach (stExecSQL($sql) as $compo) | |
463 { | |
464 $id = $compo["id"]; | |
465 | |
466 $compos[$compo["id"]] = array( | |
467 "name" => $compo["name"], | |
468 "entries" => array() | |
469 ); | |
470 | |
471 $sql = stPrepareSQL("SELECT * FROM entries WHERE compo_id=%d", $id); | |
472 foreach (stExecSQL($sql) as $entry) | |
473 { | |
474 $compos[$id]["entries"][$entry["id"]] = $entry; | |
475 } | |
476 } | |
477 } | |
478 | |
479 | |
480 function stConvSwitchMode(&$str, &$mode, $newMode) | |
481 { | |
482 if ($newMode != $mode) | |
483 { | |
484 if ($mode != "") | |
485 $str .= "\n</".$mode.">\n"; | |
486 | |
487 $mode = $newMode; | |
488 | |
489 if ($mode != "") | |
490 $str .= "<".$mode.">\n"; | |
491 } | |
492 } | |
493 | |
494 | |
495 function stConvertCompoDesc($desc) | |
496 { | |
497 global $stDescConversion; | |
498 $str = ""; | |
499 $mode = ""; | |
500 | |
501 foreach (explode("\n", $desc) as $line) | |
502 { | |
503 if (preg_match("/^\s*\s*\*(.+)$/", $line, $m)) | |
504 { | |
505 stConvSwitchMode($str, $mode, "ol"); | |
506 $str .= "<li>".$m[1]."</li>\n"; | |
507 } | |
508 else | |
509 if (preg_match("/^\s*-\s*(.+)$/", $line, $m)) | |
510 { | |
511 stConvSwitchMode($str, $mode, "ul"); | |
512 $str .= "<li>".$m[1]."</li>\n"; | |
513 } | |
514 else | |
515 { | |
516 stConvSwitchMode($str, $mode, "p"); | |
517 $str .= $line; | |
518 } | |
519 } | |
520 | |
521 stConvSwitchMode($str, $mode, ""); | |
522 | |
523 return $str; | |
524 } | |
525 | |
526 ?> |