|
1 <?php |
|
2 |
|
3 /* |
|
4 * Snapr |
|
5 * Version 0.1 beta 1 |
|
6 * Copyright (C) 2007 Dan Fuhry |
|
7 * |
|
8 * This program is Free Software; you can redistribute and/or modify it under the terms of the GNU General Public License |
|
9 * as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version. |
|
10 * |
|
11 * This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied |
|
12 * warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for details. |
|
13 */ |
|
14 |
|
15 /** |
|
16 * Generates a random filename for Snapr images. |
|
17 * @param int $length Optional - length of filename |
|
18 * @return string |
|
19 */ |
|
20 |
|
21 function gallery_make_filename($length = 24) |
|
22 { |
|
23 $valid_chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789'; |
|
24 $valid_chars = enano_str_split($valid_chars); |
|
25 $ret = ''; |
|
26 for ( $i = 0; $i < $length; $i++ ) |
|
27 { |
|
28 $ret .= $valid_chars[mt_rand(0, count($valid_chars)-1)]; |
|
29 } |
|
30 return $ret; |
|
31 } |
|
32 |
|
33 /** |
|
34 * Returns the extension of a file. |
|
35 * @param string file |
|
36 * @return string |
|
37 */ |
|
38 |
|
39 function get_file_extension($file) |
|
40 { |
|
41 return substr($file, ( strrpos($file, '.') + 1 )); |
|
42 } |
|
43 |
|
44 /** |
|
45 * For a given image ID, return the folder hierarchy. |
|
46 * @param int The image ID |
|
47 * @return array |
|
48 */ |
|
49 |
|
50 function gallery_imgid_to_folder($img_id) |
|
51 { |
|
52 global $db, $session, $paths, $template, $plugins; // Common objects |
|
53 |
|
54 if ( !is_int($img_id) ) |
|
55 return array(); |
|
56 |
|
57 $img_id = strval($img_id); |
|
58 $ret = array(); |
|
59 |
|
60 $sanity = 0; |
|
61 $sanity_stack = array(); |
|
62 |
|
63 while(true) |
|
64 { |
|
65 $sanity++; |
|
66 $q = $db->sql_query('SELECT img_title, img_id, folder_parent FROM '.table_prefix.'gallery WHERE img_id=' . $img_id . ';'); |
|
67 if ( !$q ) |
|
68 $db->_die(); |
|
69 $row = $db->fetchrow(); |
|
70 if ( !$row ) |
|
71 { |
|
72 break; |
|
73 } |
|
74 if ( $sanity > 1 ) |
|
75 { |
|
76 $ret[] = $row['img_title']; |
|
77 } |
|
78 if ( !$row['folder_parent'] ) |
|
79 { |
|
80 break; |
|
81 } |
|
82 if ( in_array($row['img_id'], $sanity_stack) ) |
|
83 return array('Infinite loop'); |
|
84 $sanity_stack[] = $row['img_id']; |
|
85 $img_id = $row['folder_parent']; |
|
86 } |
|
87 return $ret; |
|
88 } |
|
89 |
|
90 /** |
|
91 * Generates a hierarchy of Gallery folders. |
|
92 * @return array |
|
93 */ |
|
94 |
|
95 function gallery_folder_hierarchy() |
|
96 { |
|
97 global $db, $session, $paths, $template, $plugins; // Common objects |
|
98 |
|
99 $q = $db->sql_query('SELECT img_id, img_title, folder_parent FROM '.table_prefix.'gallery WHERE is_folder=1'); |
|
100 if ( !$q ) |
|
101 $db->_die(); |
|
102 |
|
103 if ( $db->numrows() < 1 ) |
|
104 { |
|
105 return array(); |
|
106 } |
|
107 |
|
108 $lookup_table = array(); |
|
109 $hier = array('_id' => 'NULL'); |
|
110 $orphans = array(); |
|
111 $persist_orphans = array(); |
|
112 |
|
113 while ( $row = $db->fetchrow() ) |
|
114 { |
|
115 if ( !$row['folder_parent'] ) |
|
116 { |
|
117 // root-level folder |
|
118 $hier[ $row['img_title'] ] = array('_id' => $row['img_id']); |
|
119 $lookup_table[$row['img_id']] =& $hier[ $row['img_title'] ]; |
|
120 } |
|
121 else if ( $row['folder_parent'] && isset($lookup_table[$row['folder_parent']]) ) |
|
122 { |
|
123 // child folder, parent is resolved |
|
124 $lookup_table[ $row['folder_parent'] ][ $row['img_title'] ] = array('_id' => $row['img_id']); |
|
125 $lookup_table[ $row['img_id'] ] =& $lookup_table[ $row['folder_parent'] ][ $row['img_title'] ]; |
|
126 } |
|
127 else if ( $row['folder_parent'] && !isset($lookup_table[$row['folder_parent']]) ) |
|
128 { |
|
129 // child folder, orphan as of yet |
|
130 $orphans[] = $row; |
|
131 } |
|
132 } |
|
133 |
|
134 // Resolve orphans |
|
135 do |
|
136 { |
|
137 $persist_orphans = array(); |
|
138 while ( count($orphans) > 0 ) |
|
139 { |
|
140 $orphan =& $orphans[ ( count($orphans) - 1 ) ]; |
|
141 if ( isset($lookup_table[$orphan['folder_parent']]) ) |
|
142 { |
|
143 $lookup_table[ $orphan['folder_parent'] ][ $orphan['img_title'] ] = array('_id' => $orphan['img_id']); |
|
144 $lookup_table[ $orphan['img_id'] ] =& $lookup_table[ $orphan['folder_parent'] ][ $orphan['img_title'] ]; |
|
145 } |
|
146 else |
|
147 { |
|
148 $persist_orphans[] = $orphans[ ( count($orphans) - 1 ) ]; |
|
149 //echo 'BUG: ' . htmlspecialchars($orphan['img_title']) . ' (' . $orphan['img_id'] . ') is an orphan folder (parent is ' . $orphan['folder_parent'] . '); placing in root<br />'; |
|
150 // $hier[ $orphan['img_title'] ] = array(); |
|
151 // $lookup_table[$orphan['img_id']] =& $hier[ $orphan['img_title'] ]; |
|
152 } |
|
153 unset($orphan, $orphans[ ( count($orphans) - 1 ) ]); |
|
154 } |
|
155 $orphans = $persist_orphans; |
|
156 //die('insanity:<pre>'.print_r($hier,true).print_r($lookup_table,true).print_r($persist_orphans,true).'</pre>'); |
|
157 } |
|
158 while ( count($persist_orphans) > 0 ); |
|
159 |
|
160 return $hier; |
|
161 |
|
162 } |
|
163 |
|
164 /** |
|
165 * Generates HTML for a folder selector. |
|
166 * @param string The form field name, defaults to folder_id. |
|
167 * @param bool Whether to auto-select the root or not. Defaults to true. |
|
168 * @return string |
|
169 */ |
|
170 |
|
171 function gallery_hier_formfield($field_name = 'folder_id', $autosel = true) |
|
172 { |
|
173 $hier = gallery_folder_hierarchy(); |
|
174 $img_join = scriptPath . '/images/icons/joinbottom.gif'; |
|
175 $img_join_term = scriptPath . '/images/icons/join.gif'; |
|
176 $img_line = scriptPath . '/images/icons/line.gif'; |
|
177 $img_empty = scriptPath . '/images/icons/empty.gif'; |
|
178 |
|
179 $html = _gallery_hier_form_inner($hier, '<Root>', $field_name, -1, array(), $img_join, $img_join_term, $img_line, $img_empty, $autosel); |
|
180 |
|
181 return $html; |
|
182 } |
|
183 |
|
184 // |
|
185 |
|
186 /** |
|
187 * Inner loop for form field generator (needs to call itself recursively) |
|
188 * @access private |
|
189 */ |
|
190 |
|
191 function _gallery_hier_form_inner($el, $name, $fname, $depth, $depth_img, $img_join, $img_join_term, $img_line, $img_empty, $sel = false) |
|
192 { |
|
193 $html = ''; |
|
194 foreach ( $depth_img as $sw ) |
|
195 $html .= '<img alt=" " src="' . $sw . '" />'; |
|
196 |
|
197 $html .= '<label><input ' . ( $sel ? 'checked="checked"' : '' ) . ' type="radio" name="' . $fname . '" value="' . $el['_id'] . '" /> ' . htmlspecialchars($name) . '</label><br />'; |
|
198 |
|
199 if ( count($el) > 1 ) |
|
200 { |
|
201 // Writing this image logic sucked. |
|
202 $count = 0; |
|
203 foreach ( $el as $key => $el_lower ) |
|
204 { |
|
205 $count++; |
|
206 if ( $key == '_id' ) |
|
207 continue; |
|
208 $depth_mod = $depth_img; |
|
209 $last = ( $count == count($el) ); |
|
210 |
|
211 for ( $i = 0; $i < count($depth_mod); $i++ ) |
|
212 { |
|
213 if ( $depth_mod[$i] == $img_join_term || $depth_mod[$i] == $img_empty ) |
|
214 $depth_mod[$i] = $img_empty; |
|
215 else |
|
216 $depth_mod[$i] = $img_line; |
|
217 } |
|
218 |
|
219 if ( $last ) |
|
220 $depth_mod[] = $img_join_term; |
|
221 else |
|
222 $depth_mod[] = $img_join; |
|
223 |
|
224 $html .= _gallery_hier_form_inner($el_lower, $key, $fname, ( $depth + 1 ), $depth_mod, $img_join, $img_join_term, $img_line, $img_empty); |
|
225 } |
|
226 } |
|
227 return $html; |
|
228 } |
|
229 |
|
230 /** |
|
231 * Returns an array containing the IDs of all of the given folder ID's children. Recursive function. |
|
232 * @param int ID of folder |
|
233 */ |
|
234 |
|
235 function gal_fetch_all_children($id) |
|
236 { |
|
237 global $db, $session, $paths, $template, $plugins; // Common objects |
|
238 |
|
239 if ( !is_int($id) ) |
|
240 { |
|
241 die('not int'); |
|
242 return false; |
|
243 } |
|
244 |
|
245 $children = array(); |
|
246 |
|
247 $q = $db->sql_query('SELECT img_id,is_folder FROM '.table_prefix.'gallery WHERE folder_parent=' . $id . ';'); |
|
248 if ( !$q ) |
|
249 $db->_die(); |
|
250 if ( $db->numrows() < 1 ) |
|
251 { |
|
252 return $children; |
|
253 } |
|
254 $folders = array(); |
|
255 while ( $row = $db->fetchrow() ) |
|
256 { |
|
257 $children[] = intval($row['img_id']); |
|
258 if ( $row['is_folder'] == 1 ) |
|
259 $folders[] = intval($row['img_id']); |
|
260 } |
|
261 foreach ( $folders as $folder ) |
|
262 { |
|
263 $grandchildren = gal_fetch_all_children($folder); |
|
264 if ( $grandchildren === false ) |
|
265 { |
|
266 return false; |
|
267 } |
|
268 $children = array_merge($children, $grandchildren); |
|
269 } |
|
270 |
|
271 return $children; |
|
272 |
|
273 } |
|
274 |
|
275 /** |
|
276 * Lists all normal files within a given directory. Recursive function. Can also return the list of directories in the second parameter by reference. |
|
277 * @param string Directory to search |
|
278 * @param array Variable in which to store |
|
279 * @return array Not multi-depth |
|
280 */ |
|
281 |
|
282 function gal_dir_recurse($dir, &$dirlist) |
|
283 { |
|
284 $dir_handle = opendir($dir); |
|
285 if ( !$dir_handle ) |
|
286 return false; |
|
287 $entries = array(); |
|
288 $dirlist = array(); |
|
289 while ( true ) |
|
290 { |
|
291 $file = readdir($dir_handle); |
|
292 if ( !$file ) |
|
293 break; |
|
294 if ( $file == '.' || $file == '..' ) |
|
295 continue; |
|
296 $file = $dir . '/' . $file; |
|
297 if ( is_dir($file) ) |
|
298 { |
|
299 $children = gal_dir_recurse($file, $dirtemp); |
|
300 $dirlist[] = $file; |
|
301 $dirlist = array_merge($dirlist, $dirtemp); |
|
302 $entries = array_merge($entries, $children); |
|
303 } |
|
304 else if ( is_file($file) ) |
|
305 { |
|
306 $entries[] = $file; |
|
307 } |
|
308 else |
|
309 { |
|
310 die($file . ' is not a file or directory'); |
|
311 } |
|
312 } |
|
313 closedir($dir_handle); |
|
314 return $entries; |
|
315 } |
|
316 |
|
317 ?> |