Mercurial > hg > mgallery
annotate mgallery.inc.php @ 322:2f4e3e458714
Improve configuration handling, and add "string array" configuration item type.
author | Matti Hamalainen <ccr@tnsp.org> |
---|---|
date | Thu, 09 Apr 2020 11:55:08 +0300 |
parents | 9638512be261 |
children | d598b2320878 |
rev | line source |
---|---|
0 | 1 <?php |
2 // | |
3 // Yet Another Image Gallery | |
69
7777887bc515
Update header/copyright blurbs.
Matti Hamalainen <ccr@tnsp.org>
parents:
59
diff
changeset
|
4 // -- Common functions and data include |
7777887bc515
Update header/copyright blurbs.
Matti Hamalainen <ccr@tnsp.org>
parents:
59
diff
changeset
|
5 // Programmed and designed by Matti 'ccr' Hamalainen <ccr@tnsp.org> |
306 | 6 // (C) Copyright 2015-2020 Tecnic Software productions (TNSP) |
0 | 7 // |
8 | |
318 | 9 $mgProgVersion = "v0.9.12"; |
0 | 10 $mgProgInfo = "Programmed by Matti 'ccr' Hamalainen"; |
11 $mgProgEmail = "<ccr@tnsp.org>"; | |
306 | 12 $mgProgCopyright = "2015-2020 Tecnic Software productions (TNSP)"; |
0 | 13 |
297
77f85b1f35f2
Improve configuration file handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
295
diff
changeset
|
14 $mgProgConfigFile = "mgallery.cfg"; |
77f85b1f35f2
Improve configuration file handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
295
diff
changeset
|
15 |
274
b1d004a6d3f1
Remove obsolete debug setting.
Matti Hamalainen <ccr@tnsp.org>
parents:
272
diff
changeset
|
16 $mgalDebug = FALSE; |
0 | 17 |
275 | 18 |
0 | 19 // |
106
a3b8cd4183e6
Change {album,image}_navigation setting to *_flags.
Matti Hamalainen <ccr@tnsp.org>
parents:
105
diff
changeset
|
20 // Navigation control defines |
a3b8cd4183e6
Change {album,image}_navigation setting to *_flags.
Matti Hamalainen <ccr@tnsp.org>
parents:
105
diff
changeset
|
21 // |
136 | 22 define("MGF_JAVASCRIPT" , 0x01); |
23 define("MGF_BREADCRUMBS" , 0x10); | |
24 define("MGF_CAPTIONS" , 0x20); | |
106
a3b8cd4183e6
Change {album,image}_navigation setting to *_flags.
Matti Hamalainen <ccr@tnsp.org>
parents:
105
diff
changeset
|
25 |
a3b8cd4183e6
Change {album,image}_navigation setting to *_flags.
Matti Hamalainen <ccr@tnsp.org>
parents:
105
diff
changeset
|
26 // |
0 | 27 // Constants for different value types |
28 // | |
136 | 29 define("MG_STR" , 1); |
303
6bb434968623
Add new MG_STR_LC setting type, which is string but automatically lowercased.
Matti Hamalainen <ccr@tnsp.org>
parents:
298
diff
changeset
|
30 define("MG_STR_LC" , 2); |
322
2f4e3e458714
Improve configuration handling, and add "string array" configuration item type.
Matti Hamalainen <ccr@tnsp.org>
parents:
318
diff
changeset
|
31 define("MG_STR_ARRAY" , 3); |
2f4e3e458714
Improve configuration handling, and add "string array" configuration item type.
Matti Hamalainen <ccr@tnsp.org>
parents:
318
diff
changeset
|
32 define("MG_INT" , 4); |
2f4e3e458714
Improve configuration handling, and add "string array" configuration item type.
Matti Hamalainen <ccr@tnsp.org>
parents:
318
diff
changeset
|
33 define("MG_DVA" , 5); |
2f4e3e458714
Improve configuration handling, and add "string array" configuration item type.
Matti Hamalainen <ccr@tnsp.org>
parents:
318
diff
changeset
|
34 define("MG_BOOL" , 6); |
2f4e3e458714
Improve configuration handling, and add "string array" configuration item type.
Matti Hamalainen <ccr@tnsp.org>
parents:
318
diff
changeset
|
35 define("MG_FLAGS" , 7); |
2f4e3e458714
Improve configuration handling, and add "string array" configuration item type.
Matti Hamalainen <ccr@tnsp.org>
parents:
318
diff
changeset
|
36 define("MG_DATE" , 8); |
0 | 37 |
38 | |
136 | 39 define("yes" , 1); |
40 define("no" , 0); | |
0 | 41 |
42 | |
114
c803235bd0f9
Implement flag config option type.
Matti Hamalainen <ccr@tnsp.org>
parents:
113
diff
changeset
|
43 $mgGFlags = [ |
136 | 44 "javascript" => MGF_JAVASCRIPT, |
45 "breadcrumbs" => MGF_BREADCRUMBS, | |
46 "captions" => MGF_CAPTIONS, | |
114
c803235bd0f9
Implement flag config option type.
Matti Hamalainen <ccr@tnsp.org>
parents:
113
diff
changeset
|
47 ]; |
c803235bd0f9
Implement flag config option type.
Matti Hamalainen <ccr@tnsp.org>
parents:
113
diff
changeset
|
48 |
c803235bd0f9
Implement flag config option type.
Matti Hamalainen <ccr@tnsp.org>
parents:
113
diff
changeset
|
49 |
0 | 50 function mgPathName($path) |
51 { | |
52 $tmp = mgCleanPathArray(TRUE, 0, func_num_args(), func_get_args()); | |
53 if (count($tmp) > 0) | |
54 return implode("/", array_splice($tmp, 0, -1))."/"; | |
55 else | |
56 return $path; | |
57 } | |
58 | |
322
2f4e3e458714
Improve configuration handling, and add "string array" configuration item type.
Matti Hamalainen <ccr@tnsp.org>
parents:
318
diff
changeset
|
59 |
0 | 60 // |
61 // Configuration settings and their default values | |
62 // | |
127
3ee66c432081
Use modern PHP array syntax.
Matti Hamalainen <ccr@tnsp.org>
parents:
124
diff
changeset
|
63 $mgDefaults = [ |
130
f825d644cf7a
Implement new config setting "timezone" for specifying the timezone used for date/timestamp interpretation.
Matti Hamalainen <ccr@tnsp.org>
parents:
127
diff
changeset
|
64 "timezone" => [MG_STR, NULL], |
0 | 65 |
141
18061527d1cd
Reorder some settings in configuration data.
Matti Hamalainen <ccr@tnsp.org>
parents:
137
diff
changeset
|
66 "clean_urls" => [MG_BOOL, FALSE], |
127
3ee66c432081
Use modern PHP array syntax.
Matti Hamalainen <ccr@tnsp.org>
parents:
124
diff
changeset
|
67 "base_path" => [MG_STR, mgPathName(mgRealPath($_SERVER["SCRIPT_FILENAME"]))], |
3ee66c432081
Use modern PHP array syntax.
Matti Hamalainen <ccr@tnsp.org>
parents:
124
diff
changeset
|
68 "base_url" => [MG_STR, mgPathName($_SERVER["PHP_SELF"])], |
3ee66c432081
Use modern PHP array syntax.
Matti Hamalainen <ccr@tnsp.org>
parents:
124
diff
changeset
|
69 "image_url" => [MG_STR, mgPathName($_SERVER["PHP_SELF"])], |
3ee66c432081
Use modern PHP array syntax.
Matti Hamalainen <ccr@tnsp.org>
parents:
124
diff
changeset
|
70 "mgallery_php" => [MG_STR, "mgallery.php"], |
250
3af3b3f50cb0
Add settings for med_format and tn_format (currently allowed values are
Matti Hamalainen <ccr@tnsp.org>
parents:
241
diff
changeset
|
71 "format_exts" => [MG_STR, "\.jpg|\.png|\.gif|\.jpeg|\.webp"], |
127
3ee66c432081
Use modern PHP array syntax.
Matti Hamalainen <ccr@tnsp.org>
parents:
124
diff
changeset
|
72 "captions_file" => [MG_STR, "captions.txt"], |
3ee66c432081
Use modern PHP array syntax.
Matti Hamalainen <ccr@tnsp.org>
parents:
124
diff
changeset
|
73 "header_file" => [MG_STR, "header.txt"], |
267
108b354ea2ea
Rename default gallery info file from "gallery.info" to "mgallery.info".
Matti Hamalainen <ccr@tnsp.org>
parents:
266
diff
changeset
|
74 "info_file" => [MG_STR, "mgallery.info"], |
127
3ee66c432081
Use modern PHP array syntax.
Matti Hamalainen <ccr@tnsp.org>
parents:
124
diff
changeset
|
75 "cache_file" => [MG_STR, ".mgallery.cache"], |
3ee66c432081
Use modern PHP array syntax.
Matti Hamalainen <ccr@tnsp.org>
parents:
124
diff
changeset
|
76 |
3ee66c432081
Use modern PHP array syntax.
Matti Hamalainen <ccr@tnsp.org>
parents:
124
diff
changeset
|
77 "cover_images" => [MG_BOOL, TRUE], |
141
18061527d1cd
Reorder some settings in configuration data.
Matti Hamalainen <ccr@tnsp.org>
parents:
137
diff
changeset
|
78 "album_icon" => [MG_STR, "album_sm.png"], |
18061527d1cd
Reorder some settings in configuration data.
Matti Hamalainen <ccr@tnsp.org>
parents:
137
diff
changeset
|
79 |
127
3ee66c432081
Use modern PHP array syntax.
Matti Hamalainen <ccr@tnsp.org>
parents:
124
diff
changeset
|
80 "title_prefix" => [MG_STR, ""], |
3ee66c432081
Use modern PHP array syntax.
Matti Hamalainen <ccr@tnsp.org>
parents:
124
diff
changeset
|
81 "title_sep" => [MG_STR, " - "], |
224 | 82 "page_info" => [MG_STR, "Powered by <b>MGallery ".$mgProgVersion."</b> © Copyright ".$mgProgCopyright], |
151
4e25150aa868
Change js_path configuration setting to js_file.
Matti Hamalainen <ccr@tnsp.org>
parents:
150
diff
changeset
|
83 |
322
2f4e3e458714
Improve configuration handling, and add "string array" configuration item type.
Matti Hamalainen <ccr@tnsp.org>
parents:
318
diff
changeset
|
84 "css" => [MG_STR_ARRAY, NULL], |
2f4e3e458714
Improve configuration handling, and add "string array" configuration item type.
Matti Hamalainen <ccr@tnsp.org>
parents:
318
diff
changeset
|
85 "js_file" => [MG_STR_ARRAY, NULL], |
2f4e3e458714
Improve configuration handling, and add "string array" configuration item type.
Matti Hamalainen <ccr@tnsp.org>
parents:
318
diff
changeset
|
86 "urchin_file" => [MG_STR_ARRAY, FALSE], |
0 | 87 |
127
3ee66c432081
Use modern PHP array syntax.
Matti Hamalainen <ccr@tnsp.org>
parents:
124
diff
changeset
|
88 "global_flags" => [MG_FLAGS, MGF_JAVASCRIPT | MGF_BREADCRUMBS | MGF_CAPTIONS, &$mgGFlags], |
3ee66c432081
Use modern PHP array syntax.
Matti Hamalainen <ccr@tnsp.org>
parents:
124
diff
changeset
|
89 "image_flags" => [MG_FLAGS, MGF_JAVASCRIPT | MGF_BREADCRUMBS, &$mgGFlags], |
3ee66c432081
Use modern PHP array syntax.
Matti Hamalainen <ccr@tnsp.org>
parents:
124
diff
changeset
|
90 "album_flags" => [MG_FLAGS, MGF_JAVASCRIPT | MGF_BREADCRUMBS, &$mgGFlags], |
0 | 91 |
127
3ee66c432081
Use modern PHP array syntax.
Matti Hamalainen <ccr@tnsp.org>
parents:
124
diff
changeset
|
92 "tn_path" => [MG_STR, "tn/"], |
185
cb7019690a73
Separate medium image and thumbnail directories / handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
171
diff
changeset
|
93 "med_path" => [MG_STR, "med/"], |
0 | 94 |
303
6bb434968623
Add new MG_STR_LC setting type, which is string but automatically lowercased.
Matti Hamalainen <ccr@tnsp.org>
parents:
298
diff
changeset
|
95 "tn_format" => [MG_STR_LC, "jpeg"], |
127
3ee66c432081
Use modern PHP array syntax.
Matti Hamalainen <ccr@tnsp.org>
parents:
124
diff
changeset
|
96 "tn_width" => [MG_INT, 140], // In pixels, applies as bounding box for w/h |
3ee66c432081
Use modern PHP array syntax.
Matti Hamalainen <ccr@tnsp.org>
parents:
124
diff
changeset
|
97 "tn_height" => [MG_INT, 100], |
289
13cff35dfbec
Change default thumbnail/medium formats from JPEG to WEBP.
Matti Hamalainen <ccr@tnsp.org>
parents:
285
diff
changeset
|
98 "tn_quality" => [MG_INT, 90], // JPEG/WEBP quality percent |
0 | 99 |
303
6bb434968623
Add new MG_STR_LC setting type, which is string but automatically lowercased.
Matti Hamalainen <ccr@tnsp.org>
parents:
298
diff
changeset
|
100 "med_format" => [MG_STR_LC, "jpeg"], |
127
3ee66c432081
Use modern PHP array syntax.
Matti Hamalainen <ccr@tnsp.org>
parents:
124
diff
changeset
|
101 "med_width" => [MG_INT, 960], |
3ee66c432081
Use modern PHP array syntax.
Matti Hamalainen <ccr@tnsp.org>
parents:
124
diff
changeset
|
102 "med_height" => [MG_INT, 640], |
162
3c2674dbd185
Increase the generated mid- and thumbnail images JPEG quality slightly.
Matti Hamalainen <ccr@tnsp.org>
parents:
161
diff
changeset
|
103 "med_quality" => [MG_INT, 90], |
195 | 104 |
303
6bb434968623
Add new MG_STR_LC setting type, which is string but automatically lowercased.
Matti Hamalainen <ccr@tnsp.org>
parents:
298
diff
changeset
|
105 "backend" => [MG_STR_LC, "php"], |
290
19fbf800b1f7
Work on very early form of virtual gallery support.
Matti Hamalainen <ccr@tnsp.org>
parents:
289
diff
changeset
|
106 "sql_db" => [MG_STR, NULL], |
19fbf800b1f7
Work on very early form of virtual gallery support.
Matti Hamalainen <ccr@tnsp.org>
parents:
289
diff
changeset
|
107 "sql_username" => [MG_STR, ""], |
19fbf800b1f7
Work on very early form of virtual gallery support.
Matti Hamalainen <ccr@tnsp.org>
parents:
289
diff
changeset
|
108 "sql_password" => [MG_STR, ""], |
322
2f4e3e458714
Improve configuration handling, and add "string array" configuration item type.
Matti Hamalainen <ccr@tnsp.org>
parents:
318
diff
changeset
|
109 "sql_options" => [MG_STR_ARRAY, []], |
127
3ee66c432081
Use modern PHP array syntax.
Matti Hamalainen <ccr@tnsp.org>
parents:
124
diff
changeset
|
110 ]; |
0 | 111 |
112 | |
113 function mgDebug($msg) | |
114 { | |
274
b1d004a6d3f1
Remove obsolete debug setting.
Matti Hamalainen <ccr@tnsp.org>
parents:
272
diff
changeset
|
115 global $mgalDebug; |
b1d004a6d3f1
Remove obsolete debug setting.
Matti Hamalainen <ccr@tnsp.org>
parents:
272
diff
changeset
|
116 if ($mgalDebug) |
b1d004a6d3f1
Remove obsolete debug setting.
Matti Hamalainen <ccr@tnsp.org>
parents:
272
diff
changeset
|
117 { |
275 | 118 echo "<p>MGAL[debug]: ".htmlspecialchars($msg)."</p>"; |
274
b1d004a6d3f1
Remove obsolete debug setting.
Matti Hamalainen <ccr@tnsp.org>
parents:
272
diff
changeset
|
119 error_log("MGAL[debug]: ".$msg); |
b1d004a6d3f1
Remove obsolete debug setting.
Matti Hamalainen <ccr@tnsp.org>
parents:
272
diff
changeset
|
120 } |
0 | 121 } |
122 | |
123 | |
124 function mgFatal($msg) | |
125 { | |
126 die("MGAL[fatal]: ".$msg); | |
127 } | |
128 | |
129 | |
130 function mgError($msg) | |
131 { | |
275 | 132 error_log("MGAL[error]: ".$msg); |
0 | 133 return FALSE; |
134 } | |
135 | |
136 | |
137 function mgCArg($index, $clip = FALSE) | |
138 { | |
139 global $argc, $argv; | |
140 if ($index < $argc) | |
141 { | |
142 $str = $argv[$index]; | |
143 return ($clip !== FALSE) ? substr($str, 0, $clip) : $str; | |
144 } | |
145 else | |
146 return FALSE; | |
147 } | |
148 | |
149 | |
150 function mgCArgLC($index, $clip = FALSE) | |
151 { | |
152 global $argc, $argv; | |
153 if ($index < $argc) | |
154 { | |
155 $str = strtolower($argv[$index]); | |
156 return ($clip !== FALSE) ? substr($str, 0, $clip) : $str; | |
157 } | |
158 else | |
159 return FALSE; | |
160 } | |
161 | |
162 | |
163 function mgGetSetting($key, $default = NULL) | |
164 { | |
165 global $mgSettings, $mgDefaults; | |
166 | |
167 if (!array_key_exists($key, $mgDefaults)) | |
168 mgFatal("Setting '".$key."' does not exist.\n"); | |
169 | |
170 if (array_key_exists($key, $mgSettings)) | |
171 $val = $mgSettings[$key]; | |
172 else | |
173 $val = $mgDefaults[$key][1]; | |
174 | |
175 if (!isset($val) || $val === NULL) | |
176 { | |
177 if ($default !== NULL) | |
178 $val = $default; | |
179 else | |
180 mgFatal("Setting '".$key."' is not set, but is required to be configured.\n"); | |
181 } | |
182 | |
114
c803235bd0f9
Implement flag config option type.
Matti Hamalainen <ccr@tnsp.org>
parents:
113
diff
changeset
|
183 switch ($mgDefaults[$key][0]) |
c803235bd0f9
Implement flag config option type.
Matti Hamalainen <ccr@tnsp.org>
parents:
113
diff
changeset
|
184 { |
303
6bb434968623
Add new MG_STR_LC setting type, which is string but automatically lowercased.
Matti Hamalainen <ccr@tnsp.org>
parents:
298
diff
changeset
|
185 case MG_STR_LC: |
6bb434968623
Add new MG_STR_LC setting type, which is string but automatically lowercased.
Matti Hamalainen <ccr@tnsp.org>
parents:
298
diff
changeset
|
186 $val = strtolower($val); |
6bb434968623
Add new MG_STR_LC setting type, which is string but automatically lowercased.
Matti Hamalainen <ccr@tnsp.org>
parents:
298
diff
changeset
|
187 break; |
6bb434968623
Add new MG_STR_LC setting type, which is string but automatically lowercased.
Matti Hamalainen <ccr@tnsp.org>
parents:
298
diff
changeset
|
188 |
114
c803235bd0f9
Implement flag config option type.
Matti Hamalainen <ccr@tnsp.org>
parents:
113
diff
changeset
|
189 case MG_FLAGS: |
c803235bd0f9
Implement flag config option type.
Matti Hamalainen <ccr@tnsp.org>
parents:
113
diff
changeset
|
190 if (is_string($val)) |
c803235bd0f9
Implement flag config option type.
Matti Hamalainen <ccr@tnsp.org>
parents:
113
diff
changeset
|
191 { |
c803235bd0f9
Implement flag config option type.
Matti Hamalainen <ccr@tnsp.org>
parents:
113
diff
changeset
|
192 $flags = $mgDefaults[$key][2]; |
c803235bd0f9
Implement flag config option type.
Matti Hamalainen <ccr@tnsp.org>
parents:
113
diff
changeset
|
193 $cval = preg_split("/\s*[,|]\s*/", strtolower($val), -1, PREG_SPLIT_NO_EMPTY); |
c803235bd0f9
Implement flag config option type.
Matti Hamalainen <ccr@tnsp.org>
parents:
113
diff
changeset
|
194 $nval = 0; |
c803235bd0f9
Implement flag config option type.
Matti Hamalainen <ccr@tnsp.org>
parents:
113
diff
changeset
|
195 foreach ($cval as $qval) |
c803235bd0f9
Implement flag config option type.
Matti Hamalainen <ccr@tnsp.org>
parents:
113
diff
changeset
|
196 { |
c803235bd0f9
Implement flag config option type.
Matti Hamalainen <ccr@tnsp.org>
parents:
113
diff
changeset
|
197 if (array_key_exists($qval, $flags)) |
c803235bd0f9
Implement flag config option type.
Matti Hamalainen <ccr@tnsp.org>
parents:
113
diff
changeset
|
198 $nval |= $flags[$qval]; |
c803235bd0f9
Implement flag config option type.
Matti Hamalainen <ccr@tnsp.org>
parents:
113
diff
changeset
|
199 else |
c803235bd0f9
Implement flag config option type.
Matti Hamalainen <ccr@tnsp.org>
parents:
113
diff
changeset
|
200 mgFatal("Invalid flag value for '".$key."': '".$qval."'."); |
c803235bd0f9
Implement flag config option type.
Matti Hamalainen <ccr@tnsp.org>
parents:
113
diff
changeset
|
201 } |
c803235bd0f9
Implement flag config option type.
Matti Hamalainen <ccr@tnsp.org>
parents:
113
diff
changeset
|
202 } |
c803235bd0f9
Implement flag config option type.
Matti Hamalainen <ccr@tnsp.org>
parents:
113
diff
changeset
|
203 break; |
c803235bd0f9
Implement flag config option type.
Matti Hamalainen <ccr@tnsp.org>
parents:
113
diff
changeset
|
204 } |
c803235bd0f9
Implement flag config option type.
Matti Hamalainen <ccr@tnsp.org>
parents:
113
diff
changeset
|
205 |
0 | 206 return $val; |
207 } | |
208 | |
209 | |
293
486398fb60ea
Allow album-specific {tn,med}_{width,height,format} settings.
Matti Hamalainen <ccr@tnsp.org>
parents:
290
diff
changeset
|
210 function mgGetAlbumSetting(&$data, $key, $default = NULL) |
486398fb60ea
Allow album-specific {tn,med}_{width,height,format} settings.
Matti Hamalainen <ccr@tnsp.org>
parents:
290
diff
changeset
|
211 { |
486398fb60ea
Allow album-specific {tn,med}_{width,height,format} settings.
Matti Hamalainen <ccr@tnsp.org>
parents:
290
diff
changeset
|
212 if (array_key_exists($key, $data)) |
305
b816e80bc002
Add a check for that we are running on PHP 7.1 or later if WebP format images are being used.
Matti Hamalainen <ccr@tnsp.org>
parents:
303
diff
changeset
|
213 $val = $data[$key]; |
293
486398fb60ea
Allow album-specific {tn,med}_{width,height,format} settings.
Matti Hamalainen <ccr@tnsp.org>
parents:
290
diff
changeset
|
214 else |
305
b816e80bc002
Add a check for that we are running on PHP 7.1 or later if WebP format images are being used.
Matti Hamalainen <ccr@tnsp.org>
parents:
303
diff
changeset
|
215 $val = mgGetSetting($key, $default); |
b816e80bc002
Add a check for that we are running on PHP 7.1 or later if WebP format images are being used.
Matti Hamalainen <ccr@tnsp.org>
parents:
303
diff
changeset
|
216 |
b816e80bc002
Add a check for that we are running on PHP 7.1 or later if WebP format images are being used.
Matti Hamalainen <ccr@tnsp.org>
parents:
303
diff
changeset
|
217 // XXX This is a rather silly place for this check, but since any album can |
b816e80bc002
Add a check for that we are running on PHP 7.1 or later if WebP format images are being used.
Matti Hamalainen <ccr@tnsp.org>
parents:
303
diff
changeset
|
218 // set their own formats, we can't do this check globally without scanning |
b816e80bc002
Add a check for that we are running on PHP 7.1 or later if WebP format images are being used.
Matti Hamalainen <ccr@tnsp.org>
parents:
303
diff
changeset
|
219 // all the sub-albums etc .. maybe we'll do that some day. |
b816e80bc002
Add a check for that we are running on PHP 7.1 or later if WebP format images are being used.
Matti Hamalainen <ccr@tnsp.org>
parents:
303
diff
changeset
|
220 if (($key == "tn_format" || $key == "med_format") && |
b816e80bc002
Add a check for that we are running on PHP 7.1 or later if WebP format images are being used.
Matti Hamalainen <ccr@tnsp.org>
parents:
303
diff
changeset
|
221 $val == "webp" && version_compare(PHP_VERSION, "7.1.0") < 0) |
b816e80bc002
Add a check for that we are running on PHP 7.1 or later if WebP format images are being used.
Matti Hamalainen <ccr@tnsp.org>
parents:
303
diff
changeset
|
222 { |
307
a09452ac196e
Improve WebP PHP 7.1 requirement error message.
Matti Hamalainen <ccr@tnsp.org>
parents:
306
diff
changeset
|
223 mgFatal("WebP image format support requires PHP version 7.1 or later.\n"); |
305
b816e80bc002
Add a check for that we are running on PHP 7.1 or later if WebP format images are being used.
Matti Hamalainen <ccr@tnsp.org>
parents:
303
diff
changeset
|
224 } |
b816e80bc002
Add a check for that we are running on PHP 7.1 or later if WebP format images are being used.
Matti Hamalainen <ccr@tnsp.org>
parents:
303
diff
changeset
|
225 |
b816e80bc002
Add a check for that we are running on PHP 7.1 or later if WebP format images are being used.
Matti Hamalainen <ccr@tnsp.org>
parents:
303
diff
changeset
|
226 return $val; |
293
486398fb60ea
Allow album-specific {tn,med}_{width,height,format} settings.
Matti Hamalainen <ccr@tnsp.org>
parents:
290
diff
changeset
|
227 } |
486398fb60ea
Allow album-specific {tn,med}_{width,height,format} settings.
Matti Hamalainen <ccr@tnsp.org>
parents:
290
diff
changeset
|
228 |
486398fb60ea
Allow album-specific {tn,med}_{width,height,format} settings.
Matti Hamalainen <ccr@tnsp.org>
parents:
290
diff
changeset
|
229 |
0 | 230 function mgGetPath($path, $key) |
231 { | |
232 $val = mgGetSetting($key); | |
233 return ($val !== FALSE) ? $path."/".$val : FALSE; | |
234 } | |
235 | |
236 | |
297
77f85b1f35f2
Improve configuration file handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
295
diff
changeset
|
237 function mgReadSettings(&$spaths) |
0 | 238 { |
239 global $mgSettings, $mgDefaults; | |
240 | |
127
3ee66c432081
Use modern PHP array syntax.
Matti Hamalainen <ccr@tnsp.org>
parents:
124
diff
changeset
|
241 $spaths = []; |
0 | 242 $spaths[] = getcwd()."/"; |
243 if (($tmp = getenv("HOME")) !== FALSE && strlen($tmp) > 0) | |
244 { | |
245 $spaths[] = $tmp."/.config/mgallery/"; | |
246 $spaths[] = $tmp."/."; | |
247 } | |
248 else | |
249 { | |
250 $data = posix_getpwuid(posix_getuid()); | |
251 if ($data !== FALSE && isset($data["dir"])) | |
252 { | |
253 $tmp = $data["dir"]; | |
254 $spaths[] = $tmp."/.config/mgallery/"; | |
255 $spaths[] = $tmp."/."; | |
256 } | |
257 } | |
258 $spaths[] = dirname(__FILE__)."/"; | |
259 | |
297
77f85b1f35f2
Improve configuration file handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
295
diff
changeset
|
260 $spaths = array_map(function ($path) |
0 | 261 { |
297
77f85b1f35f2
Improve configuration file handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
295
diff
changeset
|
262 global $mgProgConfigFile; |
77f85b1f35f2
Improve configuration file handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
295
diff
changeset
|
263 return $path.$mgProgConfigFile; |
77f85b1f35f2
Improve configuration file handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
295
diff
changeset
|
264 }, $spaths); |
77f85b1f35f2
Improve configuration file handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
295
diff
changeset
|
265 |
77f85b1f35f2
Improve configuration file handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
295
diff
changeset
|
266 foreach (array_unique($spaths) as $file) |
77f85b1f35f2
Improve configuration file handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
295
diff
changeset
|
267 { |
0 | 268 mgDebug("Checking '".$file."' for configuration ..\n"); |
269 if (file_exists($file) && | |
270 ($mgSettings = parse_ini_file($file, FALSE)) !== FALSE) | |
271 { | |
272 mgDebug("Found '".$file."' config.\n"); | |
278
6770ef8b3575
Disable checking of undefined configuration settings.
Matti Hamalainen <ccr@tnsp.org>
parents:
275
diff
changeset
|
273 return TRUE; |
0 | 274 } |
275 } | |
276 | |
127
3ee66c432081
Use modern PHP array syntax.
Matti Hamalainen <ccr@tnsp.org>
parents:
124
diff
changeset
|
277 $mgSettings = []; |
0 | 278 return FALSE; |
279 } | |
280 | |
281 | |
282 function mgRealPath($path) | |
283 { | |
284 return realpath($path); | |
285 } | |
286 | |
287 | |
285
25324e33fc78
Refactor gallery path handling somewhat.
Matti Hamalainen <ccr@tnsp.org>
parents:
280
diff
changeset
|
288 function mgCleanPathStr($path) |
25324e33fc78
Refactor gallery path handling somewhat.
Matti Hamalainen <ccr@tnsp.org>
parents:
280
diff
changeset
|
289 { |
25324e33fc78
Refactor gallery path handling somewhat.
Matti Hamalainen <ccr@tnsp.org>
parents:
280
diff
changeset
|
290 return str_replace("//", "/", $path); |
25324e33fc78
Refactor gallery path handling somewhat.
Matti Hamalainen <ccr@tnsp.org>
parents:
280
diff
changeset
|
291 } |
25324e33fc78
Refactor gallery path handling somewhat.
Matti Hamalainen <ccr@tnsp.org>
parents:
280
diff
changeset
|
292 |
25324e33fc78
Refactor gallery path handling somewhat.
Matti Hamalainen <ccr@tnsp.org>
parents:
280
diff
changeset
|
293 |
0 | 294 function mgCleanPathArray($refs, $start, $argc, $argv) |
295 { | |
127
3ee66c432081
Use modern PHP array syntax.
Matti Hamalainen <ccr@tnsp.org>
parents:
124
diff
changeset
|
296 $path = []; |
0 | 297 $first = TRUE; |
298 for ($n = $start; $n < $argc; $n++) | |
299 { | |
300 foreach (explode("/", $argv[$n]) as $piece) | |
301 { | |
302 switch ($piece) | |
303 { | |
304 case ".": | |
305 case "": | |
306 if ($first) | |
307 $path[] = $piece; | |
308 break; | |
309 | |
310 case "..": | |
311 if ($refs && count($path) > 0) | |
312 array_pop($path); | |
313 break; | |
314 | |
315 default: | |
316 $path[] = $piece; | |
317 break; | |
318 } | |
319 $first = FALSE; | |
320 } | |
321 } | |
285
25324e33fc78
Refactor gallery path handling somewhat.
Matti Hamalainen <ccr@tnsp.org>
parents:
280
diff
changeset
|
322 return mgCleanPathStr($path); |
0 | 323 } |
324 | |
325 | |
326 function mgCleanPath($refs) | |
327 { | |
328 return implode("/", mgCleanPathArray($refs, 1, func_num_args(), func_get_args())); | |
329 } | |
330 | |
331 | |
332 function mgGetArr($data, $skeys, $sfmt1 = "%1", $sfmt2 = "", $func = NULL) | |
333 { | |
47
1e2b55775a84
Begin implementation of translated caption/title support.
Matti Hamalainen <ccr@tnsp.org>
parents:
36
diff
changeset
|
334 global $pageLang; |
1e2b55775a84
Begin implementation of translated caption/title support.
Matti Hamalainen <ccr@tnsp.org>
parents:
36
diff
changeset
|
335 |
0 | 336 if (!is_array($skeys)) |
337 $skeys = array($skeys); | |
338 | |
339 foreach ($skeys as $skey) | |
340 if (!array_key_exists($skey, $data)) | |
341 return $sfmt2; | |
342 | |
343 $str = $sfmt1; | |
344 for ($i = 1; $i <= sizeof($skeys); $i++) | |
345 { | |
346 $val = $data[$skeys[$i - 1]]; | |
47
1e2b55775a84
Begin implementation of translated caption/title support.
Matti Hamalainen <ccr@tnsp.org>
parents:
36
diff
changeset
|
347 if (is_array($val)) |
1e2b55775a84
Begin implementation of translated caption/title support.
Matti Hamalainen <ccr@tnsp.org>
parents:
36
diff
changeset
|
348 $vtmp = array_key_exists($pageLang, $val) ? $val[$pageLang] : reset($val); |
1e2b55775a84
Begin implementation of translated caption/title support.
Matti Hamalainen <ccr@tnsp.org>
parents:
36
diff
changeset
|
349 else |
1e2b55775a84
Begin implementation of translated caption/title support.
Matti Hamalainen <ccr@tnsp.org>
parents:
36
diff
changeset
|
350 $vtmp = $val; |
0 | 351 |
352 if (is_callable($func)) | |
77
fcfd635d7abf
Oops, fix format string callback functions ..
Matti Hamalainen <ccr@tnsp.org>
parents:
70
diff
changeset
|
353 $vtmp = call_user_func($func, $vtmp); |
0 | 354 |
47
1e2b55775a84
Begin implementation of translated caption/title support.
Matti Hamalainen <ccr@tnsp.org>
parents:
36
diff
changeset
|
355 $str = str_replace("%".$i, $vtmp, $str); |
0 | 356 } |
357 | |
358 return $str; | |
359 } | |
360 | |
361 | |
290
19fbf800b1f7
Work on very early form of virtual gallery support.
Matti Hamalainen <ccr@tnsp.org>
parents:
289
diff
changeset
|
362 function mgLogSQLError($dbh, $sql) |
19fbf800b1f7
Work on very early form of virtual gallery support.
Matti Hamalainen <ccr@tnsp.org>
parents:
289
diff
changeset
|
363 { |
19fbf800b1f7
Work on very early form of virtual gallery support.
Matti Hamalainen <ccr@tnsp.org>
parents:
289
diff
changeset
|
364 return mgError("SQL error '".implode("; ", $dbh->errorInfo())."' in statement: \"".$sql."\""); |
19fbf800b1f7
Work on very early form of virtual gallery support.
Matti Hamalainen <ccr@tnsp.org>
parents:
289
diff
changeset
|
365 } |
19fbf800b1f7
Work on very early form of virtual gallery support.
Matti Hamalainen <ccr@tnsp.org>
parents:
289
diff
changeset
|
366 |
19fbf800b1f7
Work on very early form of virtual gallery support.
Matti Hamalainen <ccr@tnsp.org>
parents:
289
diff
changeset
|
367 |
19fbf800b1f7
Work on very early form of virtual gallery support.
Matti Hamalainen <ccr@tnsp.org>
parents:
289
diff
changeset
|
368 function mgDBGetSQLParam($dbh, $type, $value) |
19fbf800b1f7
Work on very early form of virtual gallery support.
Matti Hamalainen <ccr@tnsp.org>
parents:
289
diff
changeset
|
369 { |
19fbf800b1f7
Work on very early form of virtual gallery support.
Matti Hamalainen <ccr@tnsp.org>
parents:
289
diff
changeset
|
370 switch ($type) |
19fbf800b1f7
Work on very early form of virtual gallery support.
Matti Hamalainen <ccr@tnsp.org>
parents:
289
diff
changeset
|
371 { |
19fbf800b1f7
Work on very early form of virtual gallery support.
Matti Hamalainen <ccr@tnsp.org>
parents:
289
diff
changeset
|
372 case "d": return intval($value); |
19fbf800b1f7
Work on very early form of virtual gallery support.
Matti Hamalainen <ccr@tnsp.org>
parents:
289
diff
changeset
|
373 case "s": return $dbh->quote($value); |
19fbf800b1f7
Work on very early form of virtual gallery support.
Matti Hamalainen <ccr@tnsp.org>
parents:
289
diff
changeset
|
374 case "b": return intval($value) ? 1 : 0; |
19fbf800b1f7
Work on very early form of virtual gallery support.
Matti Hamalainen <ccr@tnsp.org>
parents:
289
diff
changeset
|
375 case "D": |
19fbf800b1f7
Work on very early form of virtual gallery support.
Matti Hamalainen <ccr@tnsp.org>
parents:
289
diff
changeset
|
376 if ($value instanceOf DateTime) |
19fbf800b1f7
Work on very early form of virtual gallery support.
Matti Hamalainen <ccr@tnsp.org>
parents:
289
diff
changeset
|
377 { |
19fbf800b1f7
Work on very early form of virtual gallery support.
Matti Hamalainen <ccr@tnsp.org>
parents:
289
diff
changeset
|
378 switch ($dbh->getAttribute(PDO::ATTR_DRIVER_NAME)) |
19fbf800b1f7
Work on very early form of virtual gallery support.
Matti Hamalainen <ccr@tnsp.org>
parents:
289
diff
changeset
|
379 { |
19fbf800b1f7
Work on very early form of virtual gallery support.
Matti Hamalainen <ccr@tnsp.org>
parents:
289
diff
changeset
|
380 case "pgsql" : $fmt = "Y-m-d H:i:sP"; break; |
19fbf800b1f7
Work on very early form of virtual gallery support.
Matti Hamalainen <ccr@tnsp.org>
parents:
289
diff
changeset
|
381 case "sqlite" : $fmt = DATE_RFC3339; break; |
19fbf800b1f7
Work on very early form of virtual gallery support.
Matti Hamalainen <ccr@tnsp.org>
parents:
289
diff
changeset
|
382 case "mysql" : $fmt = "Y-m-d H:i:s"; break; |
19fbf800b1f7
Work on very early form of virtual gallery support.
Matti Hamalainen <ccr@tnsp.org>
parents:
289
diff
changeset
|
383 default : $fmt = DATE_RFC3339; |
19fbf800b1f7
Work on very early form of virtual gallery support.
Matti Hamalainen <ccr@tnsp.org>
parents:
289
diff
changeset
|
384 } |
19fbf800b1f7
Work on very early form of virtual gallery support.
Matti Hamalainen <ccr@tnsp.org>
parents:
289
diff
changeset
|
385 return $dbh->quote($value->format($fmt)); |
19fbf800b1f7
Work on very early form of virtual gallery support.
Matti Hamalainen <ccr@tnsp.org>
parents:
289
diff
changeset
|
386 } |
19fbf800b1f7
Work on very early form of virtual gallery support.
Matti Hamalainen <ccr@tnsp.org>
parents:
289
diff
changeset
|
387 else |
19fbf800b1f7
Work on very early form of virtual gallery support.
Matti Hamalainen <ccr@tnsp.org>
parents:
289
diff
changeset
|
388 return intval($value); |
19fbf800b1f7
Work on very early form of virtual gallery support.
Matti Hamalainen <ccr@tnsp.org>
parents:
289
diff
changeset
|
389 } |
19fbf800b1f7
Work on very early form of virtual gallery support.
Matti Hamalainen <ccr@tnsp.org>
parents:
289
diff
changeset
|
390 } |
19fbf800b1f7
Work on very early form of virtual gallery support.
Matti Hamalainen <ccr@tnsp.org>
parents:
289
diff
changeset
|
391 |
19fbf800b1f7
Work on very early form of virtual gallery support.
Matti Hamalainen <ccr@tnsp.org>
parents:
289
diff
changeset
|
392 |
19fbf800b1f7
Work on very early form of virtual gallery support.
Matti Hamalainen <ccr@tnsp.org>
parents:
289
diff
changeset
|
393 function mgSQLToDateTime($dbh, $stamp) |
19fbf800b1f7
Work on very early form of virtual gallery support.
Matti Hamalainen <ccr@tnsp.org>
parents:
289
diff
changeset
|
394 { |
19fbf800b1f7
Work on very early form of virtual gallery support.
Matti Hamalainen <ccr@tnsp.org>
parents:
289
diff
changeset
|
395 switch ($dbh->getAttribute(PDO::ATTR_DRIVER_NAME)) |
19fbf800b1f7
Work on very early form of virtual gallery support.
Matti Hamalainen <ccr@tnsp.org>
parents:
289
diff
changeset
|
396 { |
19fbf800b1f7
Work on very early form of virtual gallery support.
Matti Hamalainen <ccr@tnsp.org>
parents:
289
diff
changeset
|
397 case "pgsql": |
19fbf800b1f7
Work on very early form of virtual gallery support.
Matti Hamalainen <ccr@tnsp.org>
parents:
289
diff
changeset
|
398 // PostgreSQL 'timestamptz' format |
19fbf800b1f7
Work on very early form of virtual gallery support.
Matti Hamalainen <ccr@tnsp.org>
parents:
289
diff
changeset
|
399 $tmp = DateTime::createFromFormat("Y-m-d H:i:sP", $stamp); |
19fbf800b1f7
Work on very early form of virtual gallery support.
Matti Hamalainen <ccr@tnsp.org>
parents:
289
diff
changeset
|
400 break; |
19fbf800b1f7
Work on very early form of virtual gallery support.
Matti Hamalainen <ccr@tnsp.org>
parents:
289
diff
changeset
|
401 |
19fbf800b1f7
Work on very early form of virtual gallery support.
Matti Hamalainen <ccr@tnsp.org>
parents:
289
diff
changeset
|
402 case "sqlite": |
19fbf800b1f7
Work on very early form of virtual gallery support.
Matti Hamalainen <ccr@tnsp.org>
parents:
289
diff
changeset
|
403 // SQLite can use RFC3339 format |
19fbf800b1f7
Work on very early form of virtual gallery support.
Matti Hamalainen <ccr@tnsp.org>
parents:
289
diff
changeset
|
404 $tmp = DateTime::createFromFormat(DATE_RFC3339, $stamp); |
19fbf800b1f7
Work on very early form of virtual gallery support.
Matti Hamalainen <ccr@tnsp.org>
parents:
289
diff
changeset
|
405 break; |
19fbf800b1f7
Work on very early form of virtual gallery support.
Matti Hamalainen <ccr@tnsp.org>
parents:
289
diff
changeset
|
406 |
19fbf800b1f7
Work on very early form of virtual gallery support.
Matti Hamalainen <ccr@tnsp.org>
parents:
289
diff
changeset
|
407 case "mysql": |
19fbf800b1f7
Work on very early form of virtual gallery support.
Matti Hamalainen <ccr@tnsp.org>
parents:
289
diff
changeset
|
408 // MySQL uses UTC internally, no way to specify TZ |
19fbf800b1f7
Work on very early form of virtual gallery support.
Matti Hamalainen <ccr@tnsp.org>
parents:
289
diff
changeset
|
409 $tmp = DateTime::createFromFormat("Y-m-d H:i:s", $stamp); |
19fbf800b1f7
Work on very early form of virtual gallery support.
Matti Hamalainen <ccr@tnsp.org>
parents:
289
diff
changeset
|
410 break; |
19fbf800b1f7
Work on very early form of virtual gallery support.
Matti Hamalainen <ccr@tnsp.org>
parents:
289
diff
changeset
|
411 |
19fbf800b1f7
Work on very early form of virtual gallery support.
Matti Hamalainen <ccr@tnsp.org>
parents:
289
diff
changeset
|
412 default: |
19fbf800b1f7
Work on very early form of virtual gallery support.
Matti Hamalainen <ccr@tnsp.org>
parents:
289
diff
changeset
|
413 $tmp = NULL; |
19fbf800b1f7
Work on very early form of virtual gallery support.
Matti Hamalainen <ccr@tnsp.org>
parents:
289
diff
changeset
|
414 } |
19fbf800b1f7
Work on very early form of virtual gallery support.
Matti Hamalainen <ccr@tnsp.org>
parents:
289
diff
changeset
|
415 |
19fbf800b1f7
Work on very early form of virtual gallery support.
Matti Hamalainen <ccr@tnsp.org>
parents:
289
diff
changeset
|
416 // echo "<p>".$stamp." :: ".var_export(($tmp instanceOf DateTime) ? $tmp : NULL, TRUE)."</p>"; |
19fbf800b1f7
Work on very early form of virtual gallery support.
Matti Hamalainen <ccr@tnsp.org>
parents:
289
diff
changeset
|
417 return ($tmp instanceOf DateTime) ? $tmp : NULL; |
19fbf800b1f7
Work on very early form of virtual gallery support.
Matti Hamalainen <ccr@tnsp.org>
parents:
289
diff
changeset
|
418 } |
19fbf800b1f7
Work on very early form of virtual gallery support.
Matti Hamalainen <ccr@tnsp.org>
parents:
289
diff
changeset
|
419 |
19fbf800b1f7
Work on very early form of virtual gallery support.
Matti Hamalainen <ccr@tnsp.org>
parents:
289
diff
changeset
|
420 |
19fbf800b1f7
Work on very early form of virtual gallery support.
Matti Hamalainen <ccr@tnsp.org>
parents:
289
diff
changeset
|
421 function mgConnectSQLDB() |
19fbf800b1f7
Work on very early form of virtual gallery support.
Matti Hamalainen <ccr@tnsp.org>
parents:
289
diff
changeset
|
422 { |
19fbf800b1f7
Work on very early form of virtual gallery support.
Matti Hamalainen <ccr@tnsp.org>
parents:
289
diff
changeset
|
423 global $db; |
19fbf800b1f7
Work on very early form of virtual gallery support.
Matti Hamalainen <ccr@tnsp.org>
parents:
289
diff
changeset
|
424 try { |
19fbf800b1f7
Work on very early form of virtual gallery support.
Matti Hamalainen <ccr@tnsp.org>
parents:
289
diff
changeset
|
425 $db = new PDO(mgGetSetting("sql_db"), |
19fbf800b1f7
Work on very early form of virtual gallery support.
Matti Hamalainen <ccr@tnsp.org>
parents:
289
diff
changeset
|
426 mgGetSetting("sql_username", NULL), |
19fbf800b1f7
Work on very early form of virtual gallery support.
Matti Hamalainen <ccr@tnsp.org>
parents:
289
diff
changeset
|
427 mgGetSetting("sql_password", NULL), |
19fbf800b1f7
Work on very early form of virtual gallery support.
Matti Hamalainen <ccr@tnsp.org>
parents:
289
diff
changeset
|
428 mgGetSetting("sql_options", array())); |
19fbf800b1f7
Work on very early form of virtual gallery support.
Matti Hamalainen <ccr@tnsp.org>
parents:
289
diff
changeset
|
429 } |
19fbf800b1f7
Work on very early form of virtual gallery support.
Matti Hamalainen <ccr@tnsp.org>
parents:
289
diff
changeset
|
430 catch (PDOException $e) { |
19fbf800b1f7
Work on very early form of virtual gallery support.
Matti Hamalainen <ccr@tnsp.org>
parents:
289
diff
changeset
|
431 mgError("Could not connect to SQL database: ".$e->getMessage()."."); |
19fbf800b1f7
Work on very early form of virtual gallery support.
Matti Hamalainen <ccr@tnsp.org>
parents:
289
diff
changeset
|
432 return FALSE; |
19fbf800b1f7
Work on very early form of virtual gallery support.
Matti Hamalainen <ccr@tnsp.org>
parents:
289
diff
changeset
|
433 } |
19fbf800b1f7
Work on very early form of virtual gallery support.
Matti Hamalainen <ccr@tnsp.org>
parents:
289
diff
changeset
|
434 return ($db !== false); |
19fbf800b1f7
Work on very early form of virtual gallery support.
Matti Hamalainen <ccr@tnsp.org>
parents:
289
diff
changeset
|
435 } |
19fbf800b1f7
Work on very early form of virtual gallery support.
Matti Hamalainen <ccr@tnsp.org>
parents:
289
diff
changeset
|
436 |
19fbf800b1f7
Work on very early form of virtual gallery support.
Matti Hamalainen <ccr@tnsp.org>
parents:
289
diff
changeset
|
437 |
19fbf800b1f7
Work on very early form of virtual gallery support.
Matti Hamalainen <ccr@tnsp.org>
parents:
289
diff
changeset
|
438 function mgDBPrepareSQLUpdate($dbh, $table, $cond, $pairs, $values = NULL) |
19fbf800b1f7
Work on very early form of virtual gallery support.
Matti Hamalainen <ccr@tnsp.org>
parents:
289
diff
changeset
|
439 { |
19fbf800b1f7
Work on very early form of virtual gallery support.
Matti Hamalainen <ccr@tnsp.org>
parents:
289
diff
changeset
|
440 $sql = []; |
19fbf800b1f7
Work on very early form of virtual gallery support.
Matti Hamalainen <ccr@tnsp.org>
parents:
289
diff
changeset
|
441 foreach ($pairs as $name => $attr) |
19fbf800b1f7
Work on very early form of virtual gallery support.
Matti Hamalainen <ccr@tnsp.org>
parents:
289
diff
changeset
|
442 { |
19fbf800b1f7
Work on very early form of virtual gallery support.
Matti Hamalainen <ccr@tnsp.org>
parents:
289
diff
changeset
|
443 $sql[] = $name."=".mgDBGetSQLParam($dbh, |
19fbf800b1f7
Work on very early form of virtual gallery support.
Matti Hamalainen <ccr@tnsp.org>
parents:
289
diff
changeset
|
444 $attr, $values !== NULL ? $values[$name] : $name); |
19fbf800b1f7
Work on very early form of virtual gallery support.
Matti Hamalainen <ccr@tnsp.org>
parents:
289
diff
changeset
|
445 } |
19fbf800b1f7
Work on very early form of virtual gallery support.
Matti Hamalainen <ccr@tnsp.org>
parents:
289
diff
changeset
|
446 return |
19fbf800b1f7
Work on very early form of virtual gallery support.
Matti Hamalainen <ccr@tnsp.org>
parents:
289
diff
changeset
|
447 "UPDATE ".$table." SET ".implode(",", $sql). |
19fbf800b1f7
Work on very early form of virtual gallery support.
Matti Hamalainen <ccr@tnsp.org>
parents:
289
diff
changeset
|
448 ($cond != "" ? " ".$cond : ""); |
19fbf800b1f7
Work on very early form of virtual gallery support.
Matti Hamalainen <ccr@tnsp.org>
parents:
289
diff
changeset
|
449 } |
19fbf800b1f7
Work on very early form of virtual gallery support.
Matti Hamalainen <ccr@tnsp.org>
parents:
289
diff
changeset
|
450 |
19fbf800b1f7
Work on very early form of virtual gallery support.
Matti Hamalainen <ccr@tnsp.org>
parents:
289
diff
changeset
|
451 |
19fbf800b1f7
Work on very early form of virtual gallery support.
Matti Hamalainen <ccr@tnsp.org>
parents:
289
diff
changeset
|
452 function mgDBPrepareSQL_V($dbh, $fmt, $argv) |
19fbf800b1f7
Work on very early form of virtual gallery support.
Matti Hamalainen <ccr@tnsp.org>
parents:
289
diff
changeset
|
453 { |
19fbf800b1f7
Work on very early form of virtual gallery support.
Matti Hamalainen <ccr@tnsp.org>
parents:
289
diff
changeset
|
454 $len = strlen($fmt); |
19fbf800b1f7
Work on very early form of virtual gallery support.
Matti Hamalainen <ccr@tnsp.org>
parents:
289
diff
changeset
|
455 $sql = ""; |
19fbf800b1f7
Work on very early form of virtual gallery support.
Matti Hamalainen <ccr@tnsp.org>
parents:
289
diff
changeset
|
456 $argn = 0; |
19fbf800b1f7
Work on very early form of virtual gallery support.
Matti Hamalainen <ccr@tnsp.org>
parents:
289
diff
changeset
|
457 $argc = count($argv); |
19fbf800b1f7
Work on very early form of virtual gallery support.
Matti Hamalainen <ccr@tnsp.org>
parents:
289
diff
changeset
|
458 |
19fbf800b1f7
Work on very early form of virtual gallery support.
Matti Hamalainen <ccr@tnsp.org>
parents:
289
diff
changeset
|
459 for ($pos = 0; $pos < $len; $pos++) |
19fbf800b1f7
Work on very early form of virtual gallery support.
Matti Hamalainen <ccr@tnsp.org>
parents:
289
diff
changeset
|
460 { |
19fbf800b1f7
Work on very early form of virtual gallery support.
Matti Hamalainen <ccr@tnsp.org>
parents:
289
diff
changeset
|
461 if ($fmt[$pos] == "%") |
19fbf800b1f7
Work on very early form of virtual gallery support.
Matti Hamalainen <ccr@tnsp.org>
parents:
289
diff
changeset
|
462 { |
19fbf800b1f7
Work on very early form of virtual gallery support.
Matti Hamalainen <ccr@tnsp.org>
parents:
289
diff
changeset
|
463 if ($argn < $argc) |
19fbf800b1f7
Work on very early form of virtual gallery support.
Matti Hamalainen <ccr@tnsp.org>
parents:
289
diff
changeset
|
464 $sql .= mgDBGetSQLParam($dbh, $fmt[++$pos], $argv[$argn++]); |
19fbf800b1f7
Work on very early form of virtual gallery support.
Matti Hamalainen <ccr@tnsp.org>
parents:
289
diff
changeset
|
465 else |
19fbf800b1f7
Work on very early form of virtual gallery support.
Matti Hamalainen <ccr@tnsp.org>
parents:
289
diff
changeset
|
466 { |
19fbf800b1f7
Work on very early form of virtual gallery support.
Matti Hamalainen <ccr@tnsp.org>
parents:
289
diff
changeset
|
467 mgError("Invalid SQL statement format string '".$fmt. |
19fbf800b1f7
Work on very early form of virtual gallery support.
Matti Hamalainen <ccr@tnsp.org>
parents:
289
diff
changeset
|
468 "', not enough parameters specified (".$argn." of ".$argc.")"); |
19fbf800b1f7
Work on very early form of virtual gallery support.
Matti Hamalainen <ccr@tnsp.org>
parents:
289
diff
changeset
|
469 return FALSE; |
19fbf800b1f7
Work on very early form of virtual gallery support.
Matti Hamalainen <ccr@tnsp.org>
parents:
289
diff
changeset
|
470 } |
19fbf800b1f7
Work on very early form of virtual gallery support.
Matti Hamalainen <ccr@tnsp.org>
parents:
289
diff
changeset
|
471 } |
19fbf800b1f7
Work on very early form of virtual gallery support.
Matti Hamalainen <ccr@tnsp.org>
parents:
289
diff
changeset
|
472 else |
19fbf800b1f7
Work on very early form of virtual gallery support.
Matti Hamalainen <ccr@tnsp.org>
parents:
289
diff
changeset
|
473 $sql .= $fmt[$pos]; |
19fbf800b1f7
Work on very early form of virtual gallery support.
Matti Hamalainen <ccr@tnsp.org>
parents:
289
diff
changeset
|
474 } |
19fbf800b1f7
Work on very early form of virtual gallery support.
Matti Hamalainen <ccr@tnsp.org>
parents:
289
diff
changeset
|
475 |
19fbf800b1f7
Work on very early form of virtual gallery support.
Matti Hamalainen <ccr@tnsp.org>
parents:
289
diff
changeset
|
476 return $sql; |
19fbf800b1f7
Work on very early form of virtual gallery support.
Matti Hamalainen <ccr@tnsp.org>
parents:
289
diff
changeset
|
477 } |
19fbf800b1f7
Work on very early form of virtual gallery support.
Matti Hamalainen <ccr@tnsp.org>
parents:
289
diff
changeset
|
478 |
19fbf800b1f7
Work on very early form of virtual gallery support.
Matti Hamalainen <ccr@tnsp.org>
parents:
289
diff
changeset
|
479 |
19fbf800b1f7
Work on very early form of virtual gallery support.
Matti Hamalainen <ccr@tnsp.org>
parents:
289
diff
changeset
|
480 function mgDBPrepareSQL($dbh) |
19fbf800b1f7
Work on very early form of virtual gallery support.
Matti Hamalainen <ccr@tnsp.org>
parents:
289
diff
changeset
|
481 { |
19fbf800b1f7
Work on very early form of virtual gallery support.
Matti Hamalainen <ccr@tnsp.org>
parents:
289
diff
changeset
|
482 $argv = func_get_args(); |
19fbf800b1f7
Work on very early form of virtual gallery support.
Matti Hamalainen <ccr@tnsp.org>
parents:
289
diff
changeset
|
483 return mgDBPrepareSQL_V($dbh, $argv[1], array_splice($argv, 2)); |
19fbf800b1f7
Work on very early form of virtual gallery support.
Matti Hamalainen <ccr@tnsp.org>
parents:
289
diff
changeset
|
484 } |
19fbf800b1f7
Work on very early form of virtual gallery support.
Matti Hamalainen <ccr@tnsp.org>
parents:
289
diff
changeset
|
485 |
19fbf800b1f7
Work on very early form of virtual gallery support.
Matti Hamalainen <ccr@tnsp.org>
parents:
289
diff
changeset
|
486 |
19fbf800b1f7
Work on very early form of virtual gallery support.
Matti Hamalainen <ccr@tnsp.org>
parents:
289
diff
changeset
|
487 function mgPrepareSQL() |
19fbf800b1f7
Work on very early form of virtual gallery support.
Matti Hamalainen <ccr@tnsp.org>
parents:
289
diff
changeset
|
488 { |
19fbf800b1f7
Work on very early form of virtual gallery support.
Matti Hamalainen <ccr@tnsp.org>
parents:
289
diff
changeset
|
489 global $db; |
19fbf800b1f7
Work on very early form of virtual gallery support.
Matti Hamalainen <ccr@tnsp.org>
parents:
289
diff
changeset
|
490 $argv = func_get_args(); |
19fbf800b1f7
Work on very early form of virtual gallery support.
Matti Hamalainen <ccr@tnsp.org>
parents:
289
diff
changeset
|
491 return mgDBPrepareSQL_V($db, $argv[0], array_splice($argv, 1)); |
19fbf800b1f7
Work on very early form of virtual gallery support.
Matti Hamalainen <ccr@tnsp.org>
parents:
289
diff
changeset
|
492 } |
19fbf800b1f7
Work on very early form of virtual gallery support.
Matti Hamalainen <ccr@tnsp.org>
parents:
289
diff
changeset
|
493 |
19fbf800b1f7
Work on very early form of virtual gallery support.
Matti Hamalainen <ccr@tnsp.org>
parents:
289
diff
changeset
|
494 |
19fbf800b1f7
Work on very early form of virtual gallery support.
Matti Hamalainen <ccr@tnsp.org>
parents:
289
diff
changeset
|
495 function mgDBExecSQLInsert($dbh, $sql) |
19fbf800b1f7
Work on very early form of virtual gallery support.
Matti Hamalainen <ccr@tnsp.org>
parents:
289
diff
changeset
|
496 { |
19fbf800b1f7
Work on very early form of virtual gallery support.
Matti Hamalainen <ccr@tnsp.org>
parents:
289
diff
changeset
|
497 switch ($dbh->getAttribute(PDO::ATTR_DRIVER_NAME)) |
19fbf800b1f7
Work on very early form of virtual gallery support.
Matti Hamalainen <ccr@tnsp.org>
parents:
289
diff
changeset
|
498 { |
19fbf800b1f7
Work on very early form of virtual gallery support.
Matti Hamalainen <ccr@tnsp.org>
parents:
289
diff
changeset
|
499 case "pgsql": |
19fbf800b1f7
Work on very early form of virtual gallery support.
Matti Hamalainen <ccr@tnsp.org>
parents:
289
diff
changeset
|
500 if (($res = mgDBFetchSQLColumn($dbh, $sql." RETURNING id")) !== false) |
19fbf800b1f7
Work on very early form of virtual gallery support.
Matti Hamalainen <ccr@tnsp.org>
parents:
289
diff
changeset
|
501 return $res; |
19fbf800b1f7
Work on very early form of virtual gallery support.
Matti Hamalainen <ccr@tnsp.org>
parents:
289
diff
changeset
|
502 else |
19fbf800b1f7
Work on very early form of virtual gallery support.
Matti Hamalainen <ccr@tnsp.org>
parents:
289
diff
changeset
|
503 return FALSE; |
19fbf800b1f7
Work on very early form of virtual gallery support.
Matti Hamalainen <ccr@tnsp.org>
parents:
289
diff
changeset
|
504 |
19fbf800b1f7
Work on very early form of virtual gallery support.
Matti Hamalainen <ccr@tnsp.org>
parents:
289
diff
changeset
|
505 default: |
19fbf800b1f7
Work on very early form of virtual gallery support.
Matti Hamalainen <ccr@tnsp.org>
parents:
289
diff
changeset
|
506 if (mgDBExecSQL($dbh, $sql) !== false) |
19fbf800b1f7
Work on very early form of virtual gallery support.
Matti Hamalainen <ccr@tnsp.org>
parents:
289
diff
changeset
|
507 return $dbh->lastInsertId(); |
19fbf800b1f7
Work on very early form of virtual gallery support.
Matti Hamalainen <ccr@tnsp.org>
parents:
289
diff
changeset
|
508 else |
19fbf800b1f7
Work on very early form of virtual gallery support.
Matti Hamalainen <ccr@tnsp.org>
parents:
289
diff
changeset
|
509 return FALSE; |
19fbf800b1f7
Work on very early form of virtual gallery support.
Matti Hamalainen <ccr@tnsp.org>
parents:
289
diff
changeset
|
510 } |
19fbf800b1f7
Work on very early form of virtual gallery support.
Matti Hamalainen <ccr@tnsp.org>
parents:
289
diff
changeset
|
511 } |
19fbf800b1f7
Work on very early form of virtual gallery support.
Matti Hamalainen <ccr@tnsp.org>
parents:
289
diff
changeset
|
512 |
19fbf800b1f7
Work on very early form of virtual gallery support.
Matti Hamalainen <ccr@tnsp.org>
parents:
289
diff
changeset
|
513 |
19fbf800b1f7
Work on very early form of virtual gallery support.
Matti Hamalainen <ccr@tnsp.org>
parents:
289
diff
changeset
|
514 function mgDBExecSQL($dbh, $sql) |
19fbf800b1f7
Work on very early form of virtual gallery support.
Matti Hamalainen <ccr@tnsp.org>
parents:
289
diff
changeset
|
515 { |
19fbf800b1f7
Work on very early form of virtual gallery support.
Matti Hamalainen <ccr@tnsp.org>
parents:
289
diff
changeset
|
516 if (($res = $dbh->query($sql)) !== FALSE) |
19fbf800b1f7
Work on very early form of virtual gallery support.
Matti Hamalainen <ccr@tnsp.org>
parents:
289
diff
changeset
|
517 return $res; |
19fbf800b1f7
Work on very early form of virtual gallery support.
Matti Hamalainen <ccr@tnsp.org>
parents:
289
diff
changeset
|
518 else |
19fbf800b1f7
Work on very early form of virtual gallery support.
Matti Hamalainen <ccr@tnsp.org>
parents:
289
diff
changeset
|
519 { |
19fbf800b1f7
Work on very early form of virtual gallery support.
Matti Hamalainen <ccr@tnsp.org>
parents:
289
diff
changeset
|
520 mgLogSQLError($dbh, $sql); |
19fbf800b1f7
Work on very early form of virtual gallery support.
Matti Hamalainen <ccr@tnsp.org>
parents:
289
diff
changeset
|
521 return FALSE; |
19fbf800b1f7
Work on very early form of virtual gallery support.
Matti Hamalainen <ccr@tnsp.org>
parents:
289
diff
changeset
|
522 } |
19fbf800b1f7
Work on very early form of virtual gallery support.
Matti Hamalainen <ccr@tnsp.org>
parents:
289
diff
changeset
|
523 } |
19fbf800b1f7
Work on very early form of virtual gallery support.
Matti Hamalainen <ccr@tnsp.org>
parents:
289
diff
changeset
|
524 |
19fbf800b1f7
Work on very early form of virtual gallery support.
Matti Hamalainen <ccr@tnsp.org>
parents:
289
diff
changeset
|
525 |
19fbf800b1f7
Work on very early form of virtual gallery support.
Matti Hamalainen <ccr@tnsp.org>
parents:
289
diff
changeset
|
526 function mgDBFetchSQL($dbh, $sql) |
19fbf800b1f7
Work on very early form of virtual gallery support.
Matti Hamalainen <ccr@tnsp.org>
parents:
289
diff
changeset
|
527 { |
19fbf800b1f7
Work on very early form of virtual gallery support.
Matti Hamalainen <ccr@tnsp.org>
parents:
289
diff
changeset
|
528 if (($res = $dbh->query($sql)) !== FALSE) |
19fbf800b1f7
Work on very early form of virtual gallery support.
Matti Hamalainen <ccr@tnsp.org>
parents:
289
diff
changeset
|
529 return $res->fetch(); |
19fbf800b1f7
Work on very early form of virtual gallery support.
Matti Hamalainen <ccr@tnsp.org>
parents:
289
diff
changeset
|
530 else |
19fbf800b1f7
Work on very early form of virtual gallery support.
Matti Hamalainen <ccr@tnsp.org>
parents:
289
diff
changeset
|
531 { |
19fbf800b1f7
Work on very early form of virtual gallery support.
Matti Hamalainen <ccr@tnsp.org>
parents:
289
diff
changeset
|
532 mgLogSQLError($dbh, $sql); |
19fbf800b1f7
Work on very early form of virtual gallery support.
Matti Hamalainen <ccr@tnsp.org>
parents:
289
diff
changeset
|
533 return FALSE; |
19fbf800b1f7
Work on very early form of virtual gallery support.
Matti Hamalainen <ccr@tnsp.org>
parents:
289
diff
changeset
|
534 } |
19fbf800b1f7
Work on very early form of virtual gallery support.
Matti Hamalainen <ccr@tnsp.org>
parents:
289
diff
changeset
|
535 } |
19fbf800b1f7
Work on very early form of virtual gallery support.
Matti Hamalainen <ccr@tnsp.org>
parents:
289
diff
changeset
|
536 |
19fbf800b1f7
Work on very early form of virtual gallery support.
Matti Hamalainen <ccr@tnsp.org>
parents:
289
diff
changeset
|
537 |
19fbf800b1f7
Work on very early form of virtual gallery support.
Matti Hamalainen <ccr@tnsp.org>
parents:
289
diff
changeset
|
538 function mgDBFetchSQLColumn($dbh, $sql, $column = 0) |
19fbf800b1f7
Work on very early form of virtual gallery support.
Matti Hamalainen <ccr@tnsp.org>
parents:
289
diff
changeset
|
539 { |
19fbf800b1f7
Work on very early form of virtual gallery support.
Matti Hamalainen <ccr@tnsp.org>
parents:
289
diff
changeset
|
540 if (($res = $dbh->query($sql)) !== FALSE) |
19fbf800b1f7
Work on very early form of virtual gallery support.
Matti Hamalainen <ccr@tnsp.org>
parents:
289
diff
changeset
|
541 return $res->fetchColumn($column); |
19fbf800b1f7
Work on very early form of virtual gallery support.
Matti Hamalainen <ccr@tnsp.org>
parents:
289
diff
changeset
|
542 else |
19fbf800b1f7
Work on very early form of virtual gallery support.
Matti Hamalainen <ccr@tnsp.org>
parents:
289
diff
changeset
|
543 { |
19fbf800b1f7
Work on very early form of virtual gallery support.
Matti Hamalainen <ccr@tnsp.org>
parents:
289
diff
changeset
|
544 mgLogSQLError($dbh, $sql); |
19fbf800b1f7
Work on very early form of virtual gallery support.
Matti Hamalainen <ccr@tnsp.org>
parents:
289
diff
changeset
|
545 return FALSE; |
19fbf800b1f7
Work on very early form of virtual gallery support.
Matti Hamalainen <ccr@tnsp.org>
parents:
289
diff
changeset
|
546 } |
19fbf800b1f7
Work on very early form of virtual gallery support.
Matti Hamalainen <ccr@tnsp.org>
parents:
289
diff
changeset
|
547 } |
19fbf800b1f7
Work on very early form of virtual gallery support.
Matti Hamalainen <ccr@tnsp.org>
parents:
289
diff
changeset
|
548 |
19fbf800b1f7
Work on very early form of virtual gallery support.
Matti Hamalainen <ccr@tnsp.org>
parents:
289
diff
changeset
|
549 |
19fbf800b1f7
Work on very early form of virtual gallery support.
Matti Hamalainen <ccr@tnsp.org>
parents:
289
diff
changeset
|
550 function mgPrepareSQLUpdate($table, $cond, $pairs) |
19fbf800b1f7
Work on very early form of virtual gallery support.
Matti Hamalainen <ccr@tnsp.org>
parents:
289
diff
changeset
|
551 { |
19fbf800b1f7
Work on very early form of virtual gallery support.
Matti Hamalainen <ccr@tnsp.org>
parents:
289
diff
changeset
|
552 global $db; |
19fbf800b1f7
Work on very early form of virtual gallery support.
Matti Hamalainen <ccr@tnsp.org>
parents:
289
diff
changeset
|
553 return mgDBPrepareSQLUpdate($db, $table, $cond, $pairs); |
19fbf800b1f7
Work on very early form of virtual gallery support.
Matti Hamalainen <ccr@tnsp.org>
parents:
289
diff
changeset
|
554 } |
19fbf800b1f7
Work on very early form of virtual gallery support.
Matti Hamalainen <ccr@tnsp.org>
parents:
289
diff
changeset
|
555 |
19fbf800b1f7
Work on very early form of virtual gallery support.
Matti Hamalainen <ccr@tnsp.org>
parents:
289
diff
changeset
|
556 |
19fbf800b1f7
Work on very early form of virtual gallery support.
Matti Hamalainen <ccr@tnsp.org>
parents:
289
diff
changeset
|
557 function mgExecSQLInsert($sql) |
19fbf800b1f7
Work on very early form of virtual gallery support.
Matti Hamalainen <ccr@tnsp.org>
parents:
289
diff
changeset
|
558 { |
19fbf800b1f7
Work on very early form of virtual gallery support.
Matti Hamalainen <ccr@tnsp.org>
parents:
289
diff
changeset
|
559 global $db; |
19fbf800b1f7
Work on very early form of virtual gallery support.
Matti Hamalainen <ccr@tnsp.org>
parents:
289
diff
changeset
|
560 return mgDBExecSQLInsert($db, $sql); |
19fbf800b1f7
Work on very early form of virtual gallery support.
Matti Hamalainen <ccr@tnsp.org>
parents:
289
diff
changeset
|
561 } |
19fbf800b1f7
Work on very early form of virtual gallery support.
Matti Hamalainen <ccr@tnsp.org>
parents:
289
diff
changeset
|
562 |
19fbf800b1f7
Work on very early form of virtual gallery support.
Matti Hamalainen <ccr@tnsp.org>
parents:
289
diff
changeset
|
563 |
19fbf800b1f7
Work on very early form of virtual gallery support.
Matti Hamalainen <ccr@tnsp.org>
parents:
289
diff
changeset
|
564 function mgExecSQL($sql) |
19fbf800b1f7
Work on very early form of virtual gallery support.
Matti Hamalainen <ccr@tnsp.org>
parents:
289
diff
changeset
|
565 { |
19fbf800b1f7
Work on very early form of virtual gallery support.
Matti Hamalainen <ccr@tnsp.org>
parents:
289
diff
changeset
|
566 global $db; |
19fbf800b1f7
Work on very early form of virtual gallery support.
Matti Hamalainen <ccr@tnsp.org>
parents:
289
diff
changeset
|
567 return mgDBExecSQL($db, $sql); |
19fbf800b1f7
Work on very early form of virtual gallery support.
Matti Hamalainen <ccr@tnsp.org>
parents:
289
diff
changeset
|
568 } |
19fbf800b1f7
Work on very early form of virtual gallery support.
Matti Hamalainen <ccr@tnsp.org>
parents:
289
diff
changeset
|
569 |
19fbf800b1f7
Work on very early form of virtual gallery support.
Matti Hamalainen <ccr@tnsp.org>
parents:
289
diff
changeset
|
570 |
19fbf800b1f7
Work on very early form of virtual gallery support.
Matti Hamalainen <ccr@tnsp.org>
parents:
289
diff
changeset
|
571 function mgFetchSQL($sql) |
19fbf800b1f7
Work on very early form of virtual gallery support.
Matti Hamalainen <ccr@tnsp.org>
parents:
289
diff
changeset
|
572 { |
19fbf800b1f7
Work on very early form of virtual gallery support.
Matti Hamalainen <ccr@tnsp.org>
parents:
289
diff
changeset
|
573 global $db; |
19fbf800b1f7
Work on very early form of virtual gallery support.
Matti Hamalainen <ccr@tnsp.org>
parents:
289
diff
changeset
|
574 return mgDBFetchSQL($db, $sql); |
19fbf800b1f7
Work on very early form of virtual gallery support.
Matti Hamalainen <ccr@tnsp.org>
parents:
289
diff
changeset
|
575 } |
19fbf800b1f7
Work on very early form of virtual gallery support.
Matti Hamalainen <ccr@tnsp.org>
parents:
289
diff
changeset
|
576 |
19fbf800b1f7
Work on very early form of virtual gallery support.
Matti Hamalainen <ccr@tnsp.org>
parents:
289
diff
changeset
|
577 |
19fbf800b1f7
Work on very early form of virtual gallery support.
Matti Hamalainen <ccr@tnsp.org>
parents:
289
diff
changeset
|
578 function mgFetchSQLColumn($sql, $column = 0) |
19fbf800b1f7
Work on very early form of virtual gallery support.
Matti Hamalainen <ccr@tnsp.org>
parents:
289
diff
changeset
|
579 { |
19fbf800b1f7
Work on very early form of virtual gallery support.
Matti Hamalainen <ccr@tnsp.org>
parents:
289
diff
changeset
|
580 global $db; |
19fbf800b1f7
Work on very early form of virtual gallery support.
Matti Hamalainen <ccr@tnsp.org>
parents:
289
diff
changeset
|
581 return mgDBFetchSQLColumn($db, $sql, $column); |
19fbf800b1f7
Work on very early form of virtual gallery support.
Matti Hamalainen <ccr@tnsp.org>
parents:
289
diff
changeset
|
582 } |
19fbf800b1f7
Work on very early form of virtual gallery support.
Matti Hamalainen <ccr@tnsp.org>
parents:
289
diff
changeset
|
583 |
19fbf800b1f7
Work on very early form of virtual gallery support.
Matti Hamalainen <ccr@tnsp.org>
parents:
289
diff
changeset
|
584 |
19fbf800b1f7
Work on very early form of virtual gallery support.
Matti Hamalainen <ccr@tnsp.org>
parents:
289
diff
changeset
|
585 function mgDBBeginTransaction($dbh = FALSE) |
19fbf800b1f7
Work on very early form of virtual gallery support.
Matti Hamalainen <ccr@tnsp.org>
parents:
289
diff
changeset
|
586 { |
19fbf800b1f7
Work on very early form of virtual gallery support.
Matti Hamalainen <ccr@tnsp.org>
parents:
289
diff
changeset
|
587 global $db; |
19fbf800b1f7
Work on very early form of virtual gallery support.
Matti Hamalainen <ccr@tnsp.org>
parents:
289
diff
changeset
|
588 return mgDBExecSQL(($dbh !== FALSE) ? $dbh : $db, "BEGIN TRANSACTION"); |
19fbf800b1f7
Work on very early form of virtual gallery support.
Matti Hamalainen <ccr@tnsp.org>
parents:
289
diff
changeset
|
589 } |
19fbf800b1f7
Work on very early form of virtual gallery support.
Matti Hamalainen <ccr@tnsp.org>
parents:
289
diff
changeset
|
590 |
19fbf800b1f7
Work on very early form of virtual gallery support.
Matti Hamalainen <ccr@tnsp.org>
parents:
289
diff
changeset
|
591 |
19fbf800b1f7
Work on very early form of virtual gallery support.
Matti Hamalainen <ccr@tnsp.org>
parents:
289
diff
changeset
|
592 function mgDBCommitTransaction($dbh = FALSE) |
19fbf800b1f7
Work on very early form of virtual gallery support.
Matti Hamalainen <ccr@tnsp.org>
parents:
289
diff
changeset
|
593 { |
19fbf800b1f7
Work on very early form of virtual gallery support.
Matti Hamalainen <ccr@tnsp.org>
parents:
289
diff
changeset
|
594 global $db; |
19fbf800b1f7
Work on very early form of virtual gallery support.
Matti Hamalainen <ccr@tnsp.org>
parents:
289
diff
changeset
|
595 return mgDBExecSQL(($dbh !== FALSE) ? $dbh : $db, "COMMIT"); |
19fbf800b1f7
Work on very early form of virtual gallery support.
Matti Hamalainen <ccr@tnsp.org>
parents:
289
diff
changeset
|
596 } |
19fbf800b1f7
Work on very early form of virtual gallery support.
Matti Hamalainen <ccr@tnsp.org>
parents:
289
diff
changeset
|
597 |
19fbf800b1f7
Work on very early form of virtual gallery support.
Matti Hamalainen <ccr@tnsp.org>
parents:
289
diff
changeset
|
598 |
19fbf800b1f7
Work on very early form of virtual gallery support.
Matti Hamalainen <ccr@tnsp.org>
parents:
289
diff
changeset
|
599 function mgDBGetTableSchema($dbh, $schema) |
19fbf800b1f7
Work on very early form of virtual gallery support.
Matti Hamalainen <ccr@tnsp.org>
parents:
289
diff
changeset
|
600 { |
19fbf800b1f7
Work on very early form of virtual gallery support.
Matti Hamalainen <ccr@tnsp.org>
parents:
289
diff
changeset
|
601 $res = []; |
19fbf800b1f7
Work on very early form of virtual gallery support.
Matti Hamalainen <ccr@tnsp.org>
parents:
289
diff
changeset
|
602 $driver = $dbh->getAttribute(PDO::ATTR_DRIVER_NAME); |
19fbf800b1f7
Work on very early form of virtual gallery support.
Matti Hamalainen <ccr@tnsp.org>
parents:
289
diff
changeset
|
603 |
19fbf800b1f7
Work on very early form of virtual gallery support.
Matti Hamalainen <ccr@tnsp.org>
parents:
289
diff
changeset
|
604 // Go through the table schema, definition by definition |
19fbf800b1f7
Work on very early form of virtual gallery support.
Matti Hamalainen <ccr@tnsp.org>
parents:
289
diff
changeset
|
605 foreach ($schema as $scol) |
19fbf800b1f7
Work on very early form of virtual gallery support.
Matti Hamalainen <ccr@tnsp.org>
parents:
289
diff
changeset
|
606 { |
19fbf800b1f7
Work on very early form of virtual gallery support.
Matti Hamalainen <ccr@tnsp.org>
parents:
289
diff
changeset
|
607 $tmp = []; |
19fbf800b1f7
Work on very early form of virtual gallery support.
Matti Hamalainen <ccr@tnsp.org>
parents:
289
diff
changeset
|
608 |
19fbf800b1f7
Work on very early form of virtual gallery support.
Matti Hamalainen <ccr@tnsp.org>
parents:
289
diff
changeset
|
609 // And each element of the one definition |
19fbf800b1f7
Work on very early form of virtual gallery support.
Matti Hamalainen <ccr@tnsp.org>
parents:
289
diff
changeset
|
610 // (like 'foo INTEGER AUTOINCREMENT') |
19fbf800b1f7
Work on very early form of virtual gallery support.
Matti Hamalainen <ccr@tnsp.org>
parents:
289
diff
changeset
|
611 foreach ($scol as $elem) |
19fbf800b1f7
Work on very early form of virtual gallery support.
Matti Hamalainen <ccr@tnsp.org>
parents:
289
diff
changeset
|
612 switch ($driver) |
19fbf800b1f7
Work on very early form of virtual gallery support.
Matti Hamalainen <ccr@tnsp.org>
parents:
289
diff
changeset
|
613 { |
19fbf800b1f7
Work on very early form of virtual gallery support.
Matti Hamalainen <ccr@tnsp.org>
parents:
289
diff
changeset
|
614 case "pgsql": |
19fbf800b1f7
Work on very early form of virtual gallery support.
Matti Hamalainen <ccr@tnsp.org>
parents:
289
diff
changeset
|
615 switch ($elem) |
19fbf800b1f7
Work on very early form of virtual gallery support.
Matti Hamalainen <ccr@tnsp.org>
parents:
289
diff
changeset
|
616 { |
19fbf800b1f7
Work on very early form of virtual gallery support.
Matti Hamalainen <ccr@tnsp.org>
parents:
289
diff
changeset
|
617 case "AUTOINCREMENT": |
19fbf800b1f7
Work on very early form of virtual gallery support.
Matti Hamalainen <ccr@tnsp.org>
parents:
289
diff
changeset
|
618 // For Postgres, use SERIAL for autoincrement and |
19fbf800b1f7
Work on very early form of virtual gallery support.
Matti Hamalainen <ccr@tnsp.org>
parents:
289
diff
changeset
|
619 // "cleverly" replace the 2nd element with SERIAL |
19fbf800b1f7
Work on very early form of virtual gallery support.
Matti Hamalainen <ccr@tnsp.org>
parents:
289
diff
changeset
|
620 // assuming that it is INTEGER or such. |
19fbf800b1f7
Work on very early form of virtual gallery support.
Matti Hamalainen <ccr@tnsp.org>
parents:
289
diff
changeset
|
621 $tmp[1] = "SERIAL"; |
19fbf800b1f7
Work on very early form of virtual gallery support.
Matti Hamalainen <ccr@tnsp.org>
parents:
289
diff
changeset
|
622 break; |
19fbf800b1f7
Work on very early form of virtual gallery support.
Matti Hamalainen <ccr@tnsp.org>
parents:
289
diff
changeset
|
623 |
19fbf800b1f7
Work on very early form of virtual gallery support.
Matti Hamalainen <ccr@tnsp.org>
parents:
289
diff
changeset
|
624 case "DATETIME": |
19fbf800b1f7
Work on very early form of virtual gallery support.
Matti Hamalainen <ccr@tnsp.org>
parents:
289
diff
changeset
|
625 $tmp[] = "TIMESTAMPTZ"; |
19fbf800b1f7
Work on very early form of virtual gallery support.
Matti Hamalainen <ccr@tnsp.org>
parents:
289
diff
changeset
|
626 break; |
19fbf800b1f7
Work on very early form of virtual gallery support.
Matti Hamalainen <ccr@tnsp.org>
parents:
289
diff
changeset
|
627 |
19fbf800b1f7
Work on very early form of virtual gallery support.
Matti Hamalainen <ccr@tnsp.org>
parents:
289
diff
changeset
|
628 default: |
19fbf800b1f7
Work on very early form of virtual gallery support.
Matti Hamalainen <ccr@tnsp.org>
parents:
289
diff
changeset
|
629 $tmp[] = $elem; |
19fbf800b1f7
Work on very early form of virtual gallery support.
Matti Hamalainen <ccr@tnsp.org>
parents:
289
diff
changeset
|
630 break; |
19fbf800b1f7
Work on very early form of virtual gallery support.
Matti Hamalainen <ccr@tnsp.org>
parents:
289
diff
changeset
|
631 } |
19fbf800b1f7
Work on very early form of virtual gallery support.
Matti Hamalainen <ccr@tnsp.org>
parents:
289
diff
changeset
|
632 break; |
19fbf800b1f7
Work on very early form of virtual gallery support.
Matti Hamalainen <ccr@tnsp.org>
parents:
289
diff
changeset
|
633 |
19fbf800b1f7
Work on very early form of virtual gallery support.
Matti Hamalainen <ccr@tnsp.org>
parents:
289
diff
changeset
|
634 case "mysql": |
19fbf800b1f7
Work on very early form of virtual gallery support.
Matti Hamalainen <ccr@tnsp.org>
parents:
289
diff
changeset
|
635 switch ($elem) |
19fbf800b1f7
Work on very early form of virtual gallery support.
Matti Hamalainen <ccr@tnsp.org>
parents:
289
diff
changeset
|
636 { |
19fbf800b1f7
Work on very early form of virtual gallery support.
Matti Hamalainen <ccr@tnsp.org>
parents:
289
diff
changeset
|
637 case "AUTOINCREMENT": |
19fbf800b1f7
Work on very early form of virtual gallery support.
Matti Hamalainen <ccr@tnsp.org>
parents:
289
diff
changeset
|
638 $tmp[] = "AUTO_INCREMENT"; |
19fbf800b1f7
Work on very early form of virtual gallery support.
Matti Hamalainen <ccr@tnsp.org>
parents:
289
diff
changeset
|
639 break; |
19fbf800b1f7
Work on very early form of virtual gallery support.
Matti Hamalainen <ccr@tnsp.org>
parents:
289
diff
changeset
|
640 |
19fbf800b1f7
Work on very early form of virtual gallery support.
Matti Hamalainen <ccr@tnsp.org>
parents:
289
diff
changeset
|
641 case "DATETIME": |
19fbf800b1f7
Work on very early form of virtual gallery support.
Matti Hamalainen <ccr@tnsp.org>
parents:
289
diff
changeset
|
642 $tmp[] = "TIMESTAMP"; |
19fbf800b1f7
Work on very early form of virtual gallery support.
Matti Hamalainen <ccr@tnsp.org>
parents:
289
diff
changeset
|
643 break; |
19fbf800b1f7
Work on very early form of virtual gallery support.
Matti Hamalainen <ccr@tnsp.org>
parents:
289
diff
changeset
|
644 |
19fbf800b1f7
Work on very early form of virtual gallery support.
Matti Hamalainen <ccr@tnsp.org>
parents:
289
diff
changeset
|
645 default: |
19fbf800b1f7
Work on very early form of virtual gallery support.
Matti Hamalainen <ccr@tnsp.org>
parents:
289
diff
changeset
|
646 $tmp[] = $elem; |
19fbf800b1f7
Work on very early form of virtual gallery support.
Matti Hamalainen <ccr@tnsp.org>
parents:
289
diff
changeset
|
647 break; |
19fbf800b1f7
Work on very early form of virtual gallery support.
Matti Hamalainen <ccr@tnsp.org>
parents:
289
diff
changeset
|
648 } |
19fbf800b1f7
Work on very early form of virtual gallery support.
Matti Hamalainen <ccr@tnsp.org>
parents:
289
diff
changeset
|
649 break; |
19fbf800b1f7
Work on very early form of virtual gallery support.
Matti Hamalainen <ccr@tnsp.org>
parents:
289
diff
changeset
|
650 |
19fbf800b1f7
Work on very early form of virtual gallery support.
Matti Hamalainen <ccr@tnsp.org>
parents:
289
diff
changeset
|
651 case "sqlite": |
19fbf800b1f7
Work on very early form of virtual gallery support.
Matti Hamalainen <ccr@tnsp.org>
parents:
289
diff
changeset
|
652 $tmp[] = $elem; |
19fbf800b1f7
Work on very early form of virtual gallery support.
Matti Hamalainen <ccr@tnsp.org>
parents:
289
diff
changeset
|
653 break; |
19fbf800b1f7
Work on very early form of virtual gallery support.
Matti Hamalainen <ccr@tnsp.org>
parents:
289
diff
changeset
|
654 |
19fbf800b1f7
Work on very early form of virtual gallery support.
Matti Hamalainen <ccr@tnsp.org>
parents:
289
diff
changeset
|
655 default: |
19fbf800b1f7
Work on very early form of virtual gallery support.
Matti Hamalainen <ccr@tnsp.org>
parents:
289
diff
changeset
|
656 die("Don't know how to handle PDO driver '".$driver."' yet.\n"); |
19fbf800b1f7
Work on very early form of virtual gallery support.
Matti Hamalainen <ccr@tnsp.org>
parents:
289
diff
changeset
|
657 } |
19fbf800b1f7
Work on very early form of virtual gallery support.
Matti Hamalainen <ccr@tnsp.org>
parents:
289
diff
changeset
|
658 |
19fbf800b1f7
Work on very early form of virtual gallery support.
Matti Hamalainen <ccr@tnsp.org>
parents:
289
diff
changeset
|
659 $res[] = implode(" ", $tmp); |
19fbf800b1f7
Work on very early form of virtual gallery support.
Matti Hamalainen <ccr@tnsp.org>
parents:
289
diff
changeset
|
660 } |
19fbf800b1f7
Work on very early form of virtual gallery support.
Matti Hamalainen <ccr@tnsp.org>
parents:
289
diff
changeset
|
661 |
19fbf800b1f7
Work on very early form of virtual gallery support.
Matti Hamalainen <ccr@tnsp.org>
parents:
289
diff
changeset
|
662 return implode(", ", $res); |
19fbf800b1f7
Work on very early form of virtual gallery support.
Matti Hamalainen <ccr@tnsp.org>
parents:
289
diff
changeset
|
663 } |
19fbf800b1f7
Work on very early form of virtual gallery support.
Matti Hamalainen <ccr@tnsp.org>
parents:
289
diff
changeset
|
664 |
19fbf800b1f7
Work on very early form of virtual gallery support.
Matti Hamalainen <ccr@tnsp.org>
parents:
289
diff
changeset
|
665 |
19fbf800b1f7
Work on very early form of virtual gallery support.
Matti Hamalainen <ccr@tnsp.org>
parents:
289
diff
changeset
|
666 function mgDBCreateOneTable($dbh, $name, $schema) |
19fbf800b1f7
Work on very early form of virtual gallery support.
Matti Hamalainen <ccr@tnsp.org>
parents:
289
diff
changeset
|
667 { |
19fbf800b1f7
Work on very early form of virtual gallery support.
Matti Hamalainen <ccr@tnsp.org>
parents:
289
diff
changeset
|
668 return (mgDBExecSQL($dbh, "CREATE TABLE IF NOT EXISTS ".$name." (".$schema.")") !== FALSE) ? TRUE : FALSE; |
19fbf800b1f7
Work on very early form of virtual gallery support.
Matti Hamalainen <ccr@tnsp.org>
parents:
289
diff
changeset
|
669 } |
19fbf800b1f7
Work on very early form of virtual gallery support.
Matti Hamalainen <ccr@tnsp.org>
parents:
289
diff
changeset
|
670 |
0 | 671 ?> |