Mercurial > hg > fapweb
annotate msitegen.inc.php @ 172:2359744b4087
Move some functions etc. to msitegen.inc.php
author | Matti Hamalainen <ccr@tnsp.org> |
---|---|
date | Wed, 06 Nov 2013 10:14:37 +0200 |
parents | msite.inc.php@0980e705dea0 |
children | fef53f6170b5 |
rev | line source |
---|---|
0 | 1 <? |
2 // | |
160
0980e705dea0
Add some copyright headers.
Matti Hamalainen <ccr@tnsp.org>
parents:
144
diff
changeset
|
3 // FAPWeb Simple Demoparty System |
33 | 4 // Generic and miscellaneous site support code |
160
0980e705dea0
Add some copyright headers.
Matti Hamalainen <ccr@tnsp.org>
parents:
144
diff
changeset
|
5 // (C) Copyright 2012-2013 Tecnic Software productions (TNSP) |
0 | 6 // |
9
fa9b66f596bb
More work on settings storage.
Matti Hamalainen <ccr@tnsp.org>
parents:
8
diff
changeset
|
7 |
fa9b66f596bb
More work on settings storage.
Matti Hamalainen <ccr@tnsp.org>
parents:
8
diff
changeset
|
8 // Globals and definitions |
0 | 9 $errorSet = FALSE; |
10 $errorMsg = ""; | |
11 | |
8
4c5f651aa107
Migrate certain settings to SQL database, cleanups, etc.
Matti Hamalainen <ccr@tnsp.org>
parents:
7
diff
changeset
|
12 define("VT_STR", 1); |
4c5f651aa107
Migrate certain settings to SQL database, cleanups, etc.
Matti Hamalainen <ccr@tnsp.org>
parents:
7
diff
changeset
|
13 define("VT_INT", 2); |
4c5f651aa107
Migrate certain settings to SQL database, cleanups, etc.
Matti Hamalainen <ccr@tnsp.org>
parents:
7
diff
changeset
|
14 define("VT_BOOL", 3); |
4c5f651aa107
Migrate certain settings to SQL database, cleanups, etc.
Matti Hamalainen <ccr@tnsp.org>
parents:
7
diff
changeset
|
15 define("VT_TEXT", 4); |
4c5f651aa107
Migrate certain settings to SQL database, cleanups, etc.
Matti Hamalainen <ccr@tnsp.org>
parents:
7
diff
changeset
|
16 |
94
6edd7d623eab
Start adding votekey modes code.
Matti Hamalainen <ccr@tnsp.org>
parents:
87
diff
changeset
|
17 |
32 | 18 if (function_exists("ini_set")) |
19 { | |
20 // Use cookies to store the session ID on the client side | |
21 @ini_set("session.use_only_cookies", 1); | |
22 | |
23 // Disable transparent Session ID support | |
24 @ini_set("session.use_trans_sid", 0); | |
25 } | |
26 | |
27 | |
0 | 28 function stError($msg) |
29 { | |
30 global $errorSet, $errorMsg; | |
31 $errorSet = TRUE; | |
32 $errorMsg .= "<li>".$msg."</li>\n"; | |
33 } | |
34 | |
35 | |
36 function stCheckHTTPS() | |
37 { | |
38 return isset($_SERVER["HTTPS"]) && ($_SERVER["HTTPS"] != "" && $_SERVER["HTTPS"] != "off"); | |
39 } | |
40 | |
41 | |
5
76c3b89d7b11
Improve voting, clean up the code, etc.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
42 function stSetupCacheControl() |
76c3b89d7b11
Improve voting, clean up the code, etc.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
43 { |
8
4c5f651aa107
Migrate certain settings to SQL database, cleanups, etc.
Matti Hamalainen <ccr@tnsp.org>
parents:
7
diff
changeset
|
44 header("Cache-Control: must-revalidate, no-store, private"); |
4c5f651aa107
Migrate certain settings to SQL database, cleanups, etc.
Matti Hamalainen <ccr@tnsp.org>
parents:
7
diff
changeset
|
45 header("Expires: Sat, 26 Jul 1997 05:00:00 GMT"); // Date in the past |
5
76c3b89d7b11
Improve voting, clean up the code, etc.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
46 } |
76c3b89d7b11
Improve voting, clean up the code, etc.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
47 |
76c3b89d7b11
Improve voting, clean up the code, etc.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
48 |
8
4c5f651aa107
Migrate certain settings to SQL database, cleanups, etc.
Matti Hamalainen <ccr@tnsp.org>
parents:
7
diff
changeset
|
49 function stReloadSettings() |
4c5f651aa107
Migrate certain settings to SQL database, cleanups, etc.
Matti Hamalainen <ccr@tnsp.org>
parents:
7
diff
changeset
|
50 { |
4c5f651aa107
Migrate certain settings to SQL database, cleanups, etc.
Matti Hamalainen <ccr@tnsp.org>
parents:
7
diff
changeset
|
51 global $siteSettings; |
4c5f651aa107
Migrate certain settings to SQL database, cleanups, etc.
Matti Hamalainen <ccr@tnsp.org>
parents:
7
diff
changeset
|
52 $res = stExecSQL("SELECT * FROM settings"); |
4c5f651aa107
Migrate certain settings to SQL database, cleanups, etc.
Matti Hamalainen <ccr@tnsp.org>
parents:
7
diff
changeset
|
53 if ($res !== FALSE) |
4c5f651aa107
Migrate certain settings to SQL database, cleanups, etc.
Matti Hamalainen <ccr@tnsp.org>
parents:
7
diff
changeset
|
54 { |
4c5f651aa107
Migrate certain settings to SQL database, cleanups, etc.
Matti Hamalainen <ccr@tnsp.org>
parents:
7
diff
changeset
|
55 foreach ($res as $row) |
4c5f651aa107
Migrate certain settings to SQL database, cleanups, etc.
Matti Hamalainen <ccr@tnsp.org>
parents:
7
diff
changeset
|
56 { |
4c5f651aa107
Migrate certain settings to SQL database, cleanups, etc.
Matti Hamalainen <ccr@tnsp.org>
parents:
7
diff
changeset
|
57 switch ($row["vtype"]) |
4c5f651aa107
Migrate certain settings to SQL database, cleanups, etc.
Matti Hamalainen <ccr@tnsp.org>
parents:
7
diff
changeset
|
58 { |
4c5f651aa107
Migrate certain settings to SQL database, cleanups, etc.
Matti Hamalainen <ccr@tnsp.org>
parents:
7
diff
changeset
|
59 case VT_INT: $val = intval($row["vint"]); break; |
4c5f651aa107
Migrate certain settings to SQL database, cleanups, etc.
Matti Hamalainen <ccr@tnsp.org>
parents:
7
diff
changeset
|
60 case VT_BOOL: $val = intval($row["vint"]) ? true : false; break; |
4c5f651aa107
Migrate certain settings to SQL database, cleanups, etc.
Matti Hamalainen <ccr@tnsp.org>
parents:
7
diff
changeset
|
61 case VT_STR: $val = $row["vstr"]; break; |
4c5f651aa107
Migrate certain settings to SQL database, cleanups, etc.
Matti Hamalainen <ccr@tnsp.org>
parents:
7
diff
changeset
|
62 case VT_TEXT: $val = $row["vtext"]; break; |
4c5f651aa107
Migrate certain settings to SQL database, cleanups, etc.
Matti Hamalainen <ccr@tnsp.org>
parents:
7
diff
changeset
|
63 } |
4c5f651aa107
Migrate certain settings to SQL database, cleanups, etc.
Matti Hamalainen <ccr@tnsp.org>
parents:
7
diff
changeset
|
64 $siteSettings[$row["key"]] = $val; |
4c5f651aa107
Migrate certain settings to SQL database, cleanups, etc.
Matti Hamalainen <ccr@tnsp.org>
parents:
7
diff
changeset
|
65 } |
4c5f651aa107
Migrate certain settings to SQL database, cleanups, etc.
Matti Hamalainen <ccr@tnsp.org>
parents:
7
diff
changeset
|
66 } |
4c5f651aa107
Migrate certain settings to SQL database, cleanups, etc.
Matti Hamalainen <ccr@tnsp.org>
parents:
7
diff
changeset
|
67 else |
4c5f651aa107
Migrate certain settings to SQL database, cleanups, etc.
Matti Hamalainen <ccr@tnsp.org>
parents:
7
diff
changeset
|
68 die("Error fetching site settings."); |
4c5f651aa107
Migrate certain settings to SQL database, cleanups, etc.
Matti Hamalainen <ccr@tnsp.org>
parents:
7
diff
changeset
|
69 } |
4c5f651aa107
Migrate certain settings to SQL database, cleanups, etc.
Matti Hamalainen <ccr@tnsp.org>
parents:
7
diff
changeset
|
70 |
4c5f651aa107
Migrate certain settings to SQL database, cleanups, etc.
Matti Hamalainen <ccr@tnsp.org>
parents:
7
diff
changeset
|
71 |
5
76c3b89d7b11
Improve voting, clean up the code, etc.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
72 function stGetSetting($name) |
0 | 73 { |
8
4c5f651aa107
Migrate certain settings to SQL database, cleanups, etc.
Matti Hamalainen <ccr@tnsp.org>
parents:
7
diff
changeset
|
74 global $siteSettings; |
0 | 75 if (isset($siteSettings[$name])) |
76 return $siteSettings[$name]; | |
77 else | |
8
4c5f651aa107
Migrate certain settings to SQL database, cleanups, etc.
Matti Hamalainen <ccr@tnsp.org>
parents:
7
diff
changeset
|
78 die("No config value for '".$name."'.\n"); |
0 | 79 } |
80 | |
81 | |
82 function stChkSetting($name) | |
83 { | |
84 global $siteSettings; | |
85 return isset($siteSettings[$name]) && $siteSettings[$name]; | |
86 } | |
87 | |
88 | |
89 function dhentities($str) | |
90 { | |
91 return str_replace(array("<",">"), array("<", ">"), htmlentities($str, ENT_NOQUOTES, "UTF-8")); | |
92 } | |
93 | |
94 | |
95 function chentities($str) | |
96 { | |
97 return htmlentities($str, ENT_NOQUOTES, "UTF-8"); | |
98 } | |
99 | |
100 | |
12
358534308c61
Clean up some of the html item generation code.
Matti Hamalainen <ccr@tnsp.org>
parents:
9
diff
changeset
|
101 function stGetIDName($name, $id, $prefix = "") |
358534308c61
Clean up some of the html item generation code.
Matti Hamalainen <ccr@tnsp.org>
parents:
9
diff
changeset
|
102 { |
358534308c61
Clean up some of the html item generation code.
Matti Hamalainen <ccr@tnsp.org>
parents:
9
diff
changeset
|
103 return |
358534308c61
Clean up some of the html item generation code.
Matti Hamalainen <ccr@tnsp.org>
parents:
9
diff
changeset
|
104 ($id != "" ? "id=\"".$prefix.$name.$id."\" " : ""). |
358534308c61
Clean up some of the html item generation code.
Matti Hamalainen <ccr@tnsp.org>
parents:
9
diff
changeset
|
105 ($name != "" ? "name=\"".$prefix.$name.$id."\" " : ""); |
358534308c61
Clean up some of the html item generation code.
Matti Hamalainen <ccr@tnsp.org>
parents:
9
diff
changeset
|
106 } |
358534308c61
Clean up some of the html item generation code.
Matti Hamalainen <ccr@tnsp.org>
parents:
9
diff
changeset
|
107 |
358534308c61
Clean up some of the html item generation code.
Matti Hamalainen <ccr@tnsp.org>
parents:
9
diff
changeset
|
108 |
0 | 109 function stGetFormCheckBoxInput($name, $id, $prefix, $checked, $label, $extra = "") |
110 { | |
111 return | |
12
358534308c61
Clean up some of the html item generation code.
Matti Hamalainen <ccr@tnsp.org>
parents:
9
diff
changeset
|
112 "<input ".$extra." type=\"checkbox\" ".stGetIDName($name, $id, $prefix). |
358534308c61
Clean up some of the html item generation code.
Matti Hamalainen <ccr@tnsp.org>
parents:
9
diff
changeset
|
113 ($checked ? "checked=\"checked\" " : "")." />". |
358534308c61
Clean up some of the html item generation code.
Matti Hamalainen <ccr@tnsp.org>
parents:
9
diff
changeset
|
114 ($label != "" ? "<label for=\"".$name."\">".$label."</label>" : ""); |
0 | 115 } |
116 | |
117 | |
26
7be3f8cf1f7a
Lots of cleanups, preparing for adding entry submission support.
Matti Hamalainen <ccr@tnsp.org>
parents:
24
diff
changeset
|
118 function stGetFormRadioButtonInput($name, $id, $prefix, $value, $checked, $label, $extra = "") |
7be3f8cf1f7a
Lots of cleanups, preparing for adding entry submission support.
Matti Hamalainen <ccr@tnsp.org>
parents:
24
diff
changeset
|
119 { |
7be3f8cf1f7a
Lots of cleanups, preparing for adding entry submission support.
Matti Hamalainen <ccr@tnsp.org>
parents:
24
diff
changeset
|
120 return |
7be3f8cf1f7a
Lots of cleanups, preparing for adding entry submission support.
Matti Hamalainen <ccr@tnsp.org>
parents:
24
diff
changeset
|
121 "<input ".$extra." type=\"radio\" ".stGetIDName($name, $id, $prefix). |
7be3f8cf1f7a
Lots of cleanups, preparing for adding entry submission support.
Matti Hamalainen <ccr@tnsp.org>
parents:
24
diff
changeset
|
122 ($checked ? "checked=\"checked\" " : "")." value=\"".$value."\" />". |
7be3f8cf1f7a
Lots of cleanups, preparing for adding entry submission support.
Matti Hamalainen <ccr@tnsp.org>
parents:
24
diff
changeset
|
123 ($label != "" ? "<label for=\"".$name."\">".$label."</label>" : ""); |
7be3f8cf1f7a
Lots of cleanups, preparing for adding entry submission support.
Matti Hamalainen <ccr@tnsp.org>
parents:
24
diff
changeset
|
124 } |
7be3f8cf1f7a
Lots of cleanups, preparing for adding entry submission support.
Matti Hamalainen <ccr@tnsp.org>
parents:
24
diff
changeset
|
125 |
7be3f8cf1f7a
Lots of cleanups, preparing for adding entry submission support.
Matti Hamalainen <ccr@tnsp.org>
parents:
24
diff
changeset
|
126 |
0 | 127 function stGetFormButtonInput($name, $id, $prefix, $label, $onclick = "") |
128 { | |
129 return | |
12
358534308c61
Clean up some of the html item generation code.
Matti Hamalainen <ccr@tnsp.org>
parents:
9
diff
changeset
|
130 "<input type=\"button\" ".stGetIDName($name, $id, $prefix). |
26
7be3f8cf1f7a
Lots of cleanups, preparing for adding entry submission support.
Matti Hamalainen <ccr@tnsp.org>
parents:
24
diff
changeset
|
131 "value=\" ".chentities($label)." \" ". |
0 | 132 ($onclick != "" ? "onClick=\"".$onclick."\"" : "")." />"; |
133 } | |
134 | |
135 | |
136 function stGetFormTextArea($rows, $cols, $name, $id, $prefix, $value, $extra = "") | |
137 { | |
138 return | |
12
358534308c61
Clean up some of the html item generation code.
Matti Hamalainen <ccr@tnsp.org>
parents:
9
diff
changeset
|
139 "<textarea ".$extra." ".stGetIDName($name, $id, $prefix). |
358534308c61
Clean up some of the html item generation code.
Matti Hamalainen <ccr@tnsp.org>
parents:
9
diff
changeset
|
140 "rows=\"".$rows."\" cols=\"".$cols."\">". |
0 | 141 (isset($value) ? chentities($value) : ""). |
142 "</textarea>"; | |
143 } | |
144 | |
145 | |
146 function stGetFormTextInput($size, $len, $name, $id, $prefix, $value, $extra = "") | |
147 { | |
148 return | |
12
358534308c61
Clean up some of the html item generation code.
Matti Hamalainen <ccr@tnsp.org>
parents:
9
diff
changeset
|
149 "<input ".$extra." type=\"text\" ".stGetIDName($name, $id, $prefix). |
358534308c61
Clean up some of the html item generation code.
Matti Hamalainen <ccr@tnsp.org>
parents:
9
diff
changeset
|
150 "size=\"".$size."\" maxlength=\"".$len."\"". |
0 | 151 (isset($value) ? " value=\"".chentities($value)."\"" : ""). |
152 " />"; | |
153 } | |
154 | |
155 | |
156 function stGetFormPasswordInput($name, $id, $prefix) | |
157 { | |
158 return | |
13 | 159 "<input type=\"password\" ".stGetIDName($name, $id, $prefix)." />"; |
0 | 160 } |
161 | |
162 | |
26
7be3f8cf1f7a
Lots of cleanups, preparing for adding entry submission support.
Matti Hamalainen <ccr@tnsp.org>
parents:
24
diff
changeset
|
163 function stGetFormSubmitInput($name, $label, $onclick = "") |
7be3f8cf1f7a
Lots of cleanups, preparing for adding entry submission support.
Matti Hamalainen <ccr@tnsp.org>
parents:
24
diff
changeset
|
164 { |
7be3f8cf1f7a
Lots of cleanups, preparing for adding entry submission support.
Matti Hamalainen <ccr@tnsp.org>
parents:
24
diff
changeset
|
165 return |
7be3f8cf1f7a
Lots of cleanups, preparing for adding entry submission support.
Matti Hamalainen <ccr@tnsp.org>
parents:
24
diff
changeset
|
166 "<input type=\"submit\" name=\"".$name. |
7be3f8cf1f7a
Lots of cleanups, preparing for adding entry submission support.
Matti Hamalainen <ccr@tnsp.org>
parents:
24
diff
changeset
|
167 "\" value=\" ".chentities($label)." \" ". |
7be3f8cf1f7a
Lots of cleanups, preparing for adding entry submission support.
Matti Hamalainen <ccr@tnsp.org>
parents:
24
diff
changeset
|
168 ($onclick != "" ? "onClick=\"".$onclick."\"" : "")." />"; |
7be3f8cf1f7a
Lots of cleanups, preparing for adding entry submission support.
Matti Hamalainen <ccr@tnsp.org>
parents:
24
diff
changeset
|
169 } |
7be3f8cf1f7a
Lots of cleanups, preparing for adding entry submission support.
Matti Hamalainen <ccr@tnsp.org>
parents:
24
diff
changeset
|
170 |
7be3f8cf1f7a
Lots of cleanups, preparing for adding entry submission support.
Matti Hamalainen <ccr@tnsp.org>
parents:
24
diff
changeset
|
171 |
0 | 172 function stGetFormHiddenInput($name, $value) |
173 { | |
12
358534308c61
Clean up some of the html item generation code.
Matti Hamalainen <ccr@tnsp.org>
parents:
9
diff
changeset
|
174 return |
358534308c61
Clean up some of the html item generation code.
Matti Hamalainen <ccr@tnsp.org>
parents:
9
diff
changeset
|
175 "<input type=\"hidden\" name=\"".$name. |
358534308c61
Clean up some of the html item generation code.
Matti Hamalainen <ccr@tnsp.org>
parents:
9
diff
changeset
|
176 "\" value=\"".chentities($value)."\" />"; |
0 | 177 } |
178 | |
179 | |
26
7be3f8cf1f7a
Lots of cleanups, preparing for adding entry submission support.
Matti Hamalainen <ccr@tnsp.org>
parents:
24
diff
changeset
|
180 function stGetFormStart($name, $action = "", $method = "post") |
7be3f8cf1f7a
Lots of cleanups, preparing for adding entry submission support.
Matti Hamalainen <ccr@tnsp.org>
parents:
24
diff
changeset
|
181 { |
7be3f8cf1f7a
Lots of cleanups, preparing for adding entry submission support.
Matti Hamalainen <ccr@tnsp.org>
parents:
24
diff
changeset
|
182 return |
7be3f8cf1f7a
Lots of cleanups, preparing for adding entry submission support.
Matti Hamalainen <ccr@tnsp.org>
parents:
24
diff
changeset
|
183 "<form name=\"".$name."\" action=\"". |
7be3f8cf1f7a
Lots of cleanups, preparing for adding entry submission support.
Matti Hamalainen <ccr@tnsp.org>
parents:
24
diff
changeset
|
184 ($action != "" ? $action : $name). |
7be3f8cf1f7a
Lots of cleanups, preparing for adding entry submission support.
Matti Hamalainen <ccr@tnsp.org>
parents:
24
diff
changeset
|
185 "\" method=\"".$method."\">\n"; |
7be3f8cf1f7a
Lots of cleanups, preparing for adding entry submission support.
Matti Hamalainen <ccr@tnsp.org>
parents:
24
diff
changeset
|
186 } |
7be3f8cf1f7a
Lots of cleanups, preparing for adding entry submission support.
Matti Hamalainen <ccr@tnsp.org>
parents:
24
diff
changeset
|
187 |
7be3f8cf1f7a
Lots of cleanups, preparing for adding entry submission support.
Matti Hamalainen <ccr@tnsp.org>
parents:
24
diff
changeset
|
188 |
0 | 189 function stGetTDEditTextItem($edit, $size, $len, $name, $id, $prefix, $value, $extra = "") |
190 { | |
191 return | |
192 "<td class=\"".$name."\">". | |
193 ($edit ? stGetFormTextInput($size, $len, $name, $id, $prefix, $value, $extra) : chentities($value)). | |
194 "</td>"; | |
195 } | |
196 | |
197 | |
198 function stPrintFormTextInput($text1, $text2, $size, $len, $name, $extra="") | |
199 { | |
200 echo " <tr><th>".chentities($text1)."</th><td>". | |
15
ea0f98a0bed8
Fix some issues and bugs caused by recent changes.
Matti Hamalainen <ccr@tnsp.org>
parents:
13
diff
changeset
|
201 stGetFormTextInput($size, $len, $name, "", "", stGetRequestItem($name), $extra). |
0 | 202 "</td><td>".chentities($text2)."</td></tr>\n"; |
203 } | |
204 | |
205 | |
206 function stPrintFormHiddenInput($name, $value) | |
207 { | |
208 echo " ".stGetFormHiddenInput($name, $value)."\n"; | |
209 } | |
210 | |
211 | |
212 function stChkDataItem($name) | |
213 { | |
214 return !isset($_REQUEST[$name]) || strlen(trim($_REQUEST[$name])) < 1; | |
215 } | |
216 | |
217 | |
218 function stChkRequestItem($name) | |
219 { | |
220 return isset($_REQUEST[$name]); | |
221 } | |
222 | |
223 | |
224 function stGetRequestItem($name, $default = "") | |
225 { | |
226 return isset($_REQUEST[$name]) ? trim($_REQUEST[$name]) : $default; | |
227 } | |
228 | |
229 | |
230 function stGetDRequestItem($name, $default = "") | |
231 { | |
232 return trim(urldecode(stGetRequestItem($name, $default))); | |
233 } | |
234 | |
235 | |
236 function stLogSQLError($sql) | |
237 { | |
238 global $db; | |
239 error_log("SQL error ".implode("; ", $db->errorInfo())." in statement \"".$sql."\""); | |
240 } | |
241 | |
242 | |
243 function stConnectSQLDB() | |
244 { | |
245 global $db; | |
246 try { | |
8
4c5f651aa107
Migrate certain settings to SQL database, cleanups, etc.
Matti Hamalainen <ccr@tnsp.org>
parents:
7
diff
changeset
|
247 $db = new PDO(stGetSetting("sqlDB")); |
0 | 248 } |
249 catch (PDOException $e) { | |
250 error_log("Could not connect to SQL database: ".$e->getMessage()."."); | |
251 return FALSE; | |
252 } | |
253 return TRUE; | |
254 } | |
255 | |
256 | |
257 function stGetSQLParam($type, $value) | |
258 { | |
259 global $db; | |
260 switch ($type) | |
261 { | |
262 case "d": | |
263 return intval($value); | |
264 | |
265 case "s": | |
266 return $db->quote($value); | |
267 | |
72
63a6caf59e5c
Fix creation of boolean settings, and add '%b' formatter into stPrepareSQL().
Matti Hamalainen <ccr@tnsp.org>
parents:
71
diff
changeset
|
268 case "b": |
63a6caf59e5c
Fix creation of boolean settings, and add '%b' formatter into stPrepareSQL().
Matti Hamalainen <ccr@tnsp.org>
parents:
71
diff
changeset
|
269 return intval($value) ? 1 : 0; |
63a6caf59e5c
Fix creation of boolean settings, and add '%b' formatter into stPrepareSQL().
Matti Hamalainen <ccr@tnsp.org>
parents:
71
diff
changeset
|
270 |
0 | 271 case "D": |
272 return intval(stGetRequestItem($value)); | |
273 | |
274 case "S": | |
275 return $db->quote(stGetDRequestItem($value)); | |
276 | |
277 case "Q": | |
278 return $db->quote(stripslashes(stGetDRequestItem($value))); | |
279 | |
280 case "B": | |
281 return intval(stGetRequestItem($value)) ? 1 : 0; | |
282 } | |
283 } | |
284 | |
285 | |
286 function stPrepareSQL() | |
287 { | |
288 $argc = func_num_args(); | |
289 $argv = func_get_args(); | |
290 if ($argc < 1) | |
291 { | |
292 error_log("Invalid stPrepareSQL() call, no arguments!"); | |
293 return FALSE; | |
294 } | |
87 | 295 |
0 | 296 $fmt = $argv[0]; |
297 $len = strlen($fmt); | |
298 $sql = ""; | |
299 $argn = 1; | |
300 $pos = 0; | |
301 while ($pos < $len) | |
302 { | |
303 if ($fmt[$pos] == "%") | |
304 { | |
305 if ($argn < $argc) | |
306 $sql .= stGetSQLParam($fmt[++$pos], $argv[$argn++]); | |
307 else | |
308 { | |
309 error_log("Invalid SQL statement format string '".$fmt. | |
310 "', not enough parameters specified (".$argn." of ".$argc.")"); | |
311 return FALSE; | |
312 } | |
313 } | |
314 else | |
315 $sql .= $fmt[$pos]; | |
316 $pos++; | |
317 } | |
318 | |
319 return $sql; | |
320 } | |
321 | |
322 | |
323 function stPrepareSQLUpdate($table, $cond, $pairs) | |
324 { | |
325 $sql = array(); | |
326 foreach ($pairs as $name => $attr) | |
327 { | |
328 $sql[] = $name."=".stGetSQLParam($attr, $name); | |
329 } | |
330 return | |
331 "UPDATE ".$table." SET ".implode(",", $sql). | |
332 ($cond != "" ? " ".$cond : ""); | |
333 } | |
334 | |
335 | |
336 function stExecSQL($sql) | |
337 { | |
338 global $db; | |
339 if (($res = $db->query($sql)) !== FALSE) | |
340 return $res; | |
341 else | |
342 { | |
343 stLogSQLError($sql); | |
344 stError("Oh noes! SQL error #23!"); | |
345 return FALSE; | |
346 } | |
347 } | |
348 | |
349 | |
350 function stFetchSQL($sql) | |
351 { | |
352 global $db; | |
353 if (($res = $db->query($sql)) !== FALSE) | |
354 { | |
355 return $res->fetch(); | |
356 } | |
357 else | |
358 { | |
359 stLogSQLError($sql); | |
360 stError("Oh noes! SQL error #31!"); | |
361 return FALSE; | |
362 } | |
363 } | |
364 | |
365 | |
366 function stFetchSQLColumn($sql, $column = 0) | |
367 { | |
368 global $db; | |
369 if (($res = $db->query($sql)) !== FALSE) | |
370 { | |
371 return $res->fetchColumn($column); | |
372 } | |
373 else | |
374 { | |
375 stLogSQLError($sql); | |
376 stError("Oh noes! SQL error #81!"); | |
377 return FALSE; | |
378 } | |
379 } | |
380 | |
381 | |
66
230aacc22cb4
Move some functions to site lib.
Matti Hamalainen <ccr@tnsp.org>
parents:
65
diff
changeset
|
382 function stSetStatus($val, $msg) |
230aacc22cb4
Move some functions to site lib.
Matti Hamalainen <ccr@tnsp.org>
parents:
65
diff
changeset
|
383 { |
230aacc22cb4
Move some functions to site lib.
Matti Hamalainen <ccr@tnsp.org>
parents:
65
diff
changeset
|
384 global $statusSet; |
230aacc22cb4
Move some functions to site lib.
Matti Hamalainen <ccr@tnsp.org>
parents:
65
diff
changeset
|
385 if (!$statusSet) |
230aacc22cb4
Move some functions to site lib.
Matti Hamalainen <ccr@tnsp.org>
parents:
65
diff
changeset
|
386 { |
230aacc22cb4
Move some functions to site lib.
Matti Hamalainen <ccr@tnsp.org>
parents:
65
diff
changeset
|
387 header("Status: ".$val." ".$msg); |
230aacc22cb4
Move some functions to site lib.
Matti Hamalainen <ccr@tnsp.org>
parents:
65
diff
changeset
|
388 } |
230aacc22cb4
Move some functions to site lib.
Matti Hamalainen <ccr@tnsp.org>
parents:
65
diff
changeset
|
389 $statusSet = TRUE; |
230aacc22cb4
Move some functions to site lib.
Matti Hamalainen <ccr@tnsp.org>
parents:
65
diff
changeset
|
390 } |
230aacc22cb4
Move some functions to site lib.
Matti Hamalainen <ccr@tnsp.org>
parents:
65
diff
changeset
|
391 |
230aacc22cb4
Move some functions to site lib.
Matti Hamalainen <ccr@tnsp.org>
parents:
65
diff
changeset
|
392 |
230aacc22cb4
Move some functions to site lib.
Matti Hamalainen <ccr@tnsp.org>
parents:
65
diff
changeset
|
393 function stExecSQLCond($sql, $okmsg) |
230aacc22cb4
Move some functions to site lib.
Matti Hamalainen <ccr@tnsp.org>
parents:
65
diff
changeset
|
394 { |
230aacc22cb4
Move some functions to site lib.
Matti Hamalainen <ccr@tnsp.org>
parents:
65
diff
changeset
|
395 if (($res = stExecSQL($sql)) !== FALSE) |
230aacc22cb4
Move some functions to site lib.
Matti Hamalainen <ccr@tnsp.org>
parents:
65
diff
changeset
|
396 { |
230aacc22cb4
Move some functions to site lib.
Matti Hamalainen <ccr@tnsp.org>
parents:
65
diff
changeset
|
397 if ($okmsg != "") |
230aacc22cb4
Move some functions to site lib.
Matti Hamalainen <ccr@tnsp.org>
parents:
65
diff
changeset
|
398 stSetStatus(200, $okmsg); |
230aacc22cb4
Move some functions to site lib.
Matti Hamalainen <ccr@tnsp.org>
parents:
65
diff
changeset
|
399 return $res; |
230aacc22cb4
Move some functions to site lib.
Matti Hamalainen <ccr@tnsp.org>
parents:
65
diff
changeset
|
400 } |
230aacc22cb4
Move some functions to site lib.
Matti Hamalainen <ccr@tnsp.org>
parents:
65
diff
changeset
|
401 else |
230aacc22cb4
Move some functions to site lib.
Matti Hamalainen <ccr@tnsp.org>
parents:
65
diff
changeset
|
402 { |
230aacc22cb4
Move some functions to site lib.
Matti Hamalainen <ccr@tnsp.org>
parents:
65
diff
changeset
|
403 stSetStatus(900, "Error in SQL execution."); |
230aacc22cb4
Move some functions to site lib.
Matti Hamalainen <ccr@tnsp.org>
parents:
65
diff
changeset
|
404 return FALSE; |
230aacc22cb4
Move some functions to site lib.
Matti Hamalainen <ccr@tnsp.org>
parents:
65
diff
changeset
|
405 } |
230aacc22cb4
Move some functions to site lib.
Matti Hamalainen <ccr@tnsp.org>
parents:
65
diff
changeset
|
406 } |
230aacc22cb4
Move some functions to site lib.
Matti Hamalainen <ccr@tnsp.org>
parents:
65
diff
changeset
|
407 |
230aacc22cb4
Move some functions to site lib.
Matti Hamalainen <ccr@tnsp.org>
parents:
65
diff
changeset
|
408 |
135
eecac02579c4
Move stChop() to site module and rename it to stStrChop().
Matti Hamalainen <ccr@tnsp.org>
parents:
121
diff
changeset
|
409 function stStrChop($str, $len) |
eecac02579c4
Move stChop() to site module and rename it to stStrChop().
Matti Hamalainen <ccr@tnsp.org>
parents:
121
diff
changeset
|
410 { |
eecac02579c4
Move stChop() to site module and rename it to stStrChop().
Matti Hamalainen <ccr@tnsp.org>
parents:
121
diff
changeset
|
411 if (strlen($str) > $len) |
eecac02579c4
Move stChop() to site module and rename it to stStrChop().
Matti Hamalainen <ccr@tnsp.org>
parents:
121
diff
changeset
|
412 $s = substr($str, 0, $len - 3)."..."; |
eecac02579c4
Move stChop() to site module and rename it to stStrChop().
Matti Hamalainen <ccr@tnsp.org>
parents:
121
diff
changeset
|
413 else |
eecac02579c4
Move stChop() to site module and rename it to stStrChop().
Matti Hamalainen <ccr@tnsp.org>
parents:
121
diff
changeset
|
414 $s = $str; |
eecac02579c4
Move stChop() to site module and rename it to stStrChop().
Matti Hamalainen <ccr@tnsp.org>
parents:
121
diff
changeset
|
415 return sprintf("%-".$len."s", $s); |
eecac02579c4
Move stChop() to site module and rename it to stStrChop().
Matti Hamalainen <ccr@tnsp.org>
parents:
121
diff
changeset
|
416 } |
eecac02579c4
Move stChop() to site module and rename it to stStrChop().
Matti Hamalainen <ccr@tnsp.org>
parents:
121
diff
changeset
|
417 |
0 | 418 ?> |