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