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