|
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 ## GALLERY NAMESPACE HANDLER |
|
17 ## |
|
18 |
|
19 $plugins->attachHook('page_not_found', 'gallery_namespace_handler($this);'); |
|
20 |
|
21 function gallery_namespace_handler(&$page) |
|
22 { |
|
23 global $db, $session, $paths, $template, $plugins; // Common objects |
|
24 |
|
25 if ( $page->namespace != 'Gallery' ) |
|
26 return false; |
|
27 |
|
28 if ( $page->page_id == 'Root' ) |
|
29 { |
|
30 page_Special_Gallery(); |
|
31 return true; |
|
32 } |
|
33 |
|
34 if ( preg_match('/^[0-9]+$/', $page->page_id) ) |
|
35 { |
|
36 $img_id = intval($page->page_id); |
|
37 if ( !$img_id ) |
|
38 return false; |
|
39 $q = $db->sql_query('SELECT img_id, img_title, img_desc, print_sizes, img_time_upload, img_time_mod, folder_parent FROM '.table_prefix.'gallery WHERE img_id=' . $img_id . ';'); |
|
40 if ( !$q ) |
|
41 $db->_die(); |
|
42 } |
|
43 else |
|
44 { |
|
45 // Ech... he sent us a string... parse it and see what we get |
|
46 if ( strstr($page->page_id, '/') ) |
|
47 { |
|
48 $folders = explode('/', $page->page_id); |
|
49 } |
|
50 else |
|
51 { |
|
52 $folders = array($page->page_id); |
|
53 } |
|
54 foreach ( $folders as $i => $_crap ) |
|
55 { |
|
56 $folder =& $folders[$i]; |
|
57 $folder = dirtify_page_id($folder); |
|
58 $folder = str_replace('_', ' ', $folder); |
|
59 } |
|
60 unset($folder); |
|
61 |
|
62 $folders = array_reverse($folders); |
|
63 // This is one of the best MySQL tricks on the market. We're going to reverse-travel a folder path using LEFT JOIN and the incredible power of metacoded SQL |
|
64 $sql = 'SELECT g0.img_id, g0.img_title, g0.img_desc, g0.print_sizes, g0.img_time_upload, g0.img_time_mod, g0.folder_parent FROM '.table_prefix.'gallery AS g0'; |
|
65 $where = "\n " . 'WHERE g0.img_title=\'' . $db->escape($folders[0]) . '\''; |
|
66 foreach ( $folders as $i => $folder ) |
|
67 { |
|
68 if ( $i == 0 ) |
|
69 continue; |
|
70 $i_dec = $i - 1; |
|
71 $folder = $db->escape($folder); |
|
72 $sql .= "\n LEFT JOIN ".table_prefix."gallery AS g{$i}\n ON ( g{$i}.img_id=g{$i_dec}.folder_parent AND g{$i}.img_title='$folder' )"; |
|
73 $where .= "\n ".'AND g'.$i.'.img_id IS NOT NULL'; |
|
74 } |
|
75 $where .= "\n AND g{$i}.folder_parent IS NULL"; |
|
76 $sql .= $where . ';'; |
|
77 |
|
78 if ( !$db->sql_query($sql) ) |
|
79 { |
|
80 $db->_die('The image metadata could not be loaded.'); |
|
81 } |
|
82 |
|
83 // Now that the folder data is no longer needed, we can fool around with it a little |
|
84 $folders = $page->page_id; |
|
85 if ( !strstr($folders, '/') ) |
|
86 { |
|
87 $hier = '/'; |
|
88 } |
|
89 else |
|
90 { |
|
91 $hier = preg_replace('/\/([^\/]+)$/', '/', $folders); |
|
92 $hier = sanitize_page_id($hier); |
|
93 } |
|
94 |
|
95 } |
|
96 if ( $db->numrows() < 1 ) |
|
97 { |
|
98 // Image not found - show custom error message |
|
99 $template->header(); |
|
100 echo '<h3>There is no image in the gallery with this ID.</h3>'; |
|
101 echo '<p>You have requested an image that couldn\'t be looked up. Please check the URL and try again, or visit the <a href="' . makeUrlNS('Special', 'Gallery') . '">Gallery index</a>.</p>'; |
|
102 $template->footer(); |
|
103 return false; |
|
104 } |
|
105 $row = $db->fetchrow(); |
|
106 |
|
107 $db->free_result(); |
|
108 |
|
109 $img_id = $row['img_id']; |
|
110 |
|
111 if ( !$row['folder_parent'] ) |
|
112 $row['folder_parent'] = ' IS NULL'; |
|
113 else |
|
114 $row['folder_parent'] = '=' . $row['folder_parent']; |
|
115 |
|
116 // Fetch image parent properties |
|
117 $q = $db->sql_query('SELECT img_id, img_title FROM '.table_prefix.'gallery WHERE folder_parent' . $row['folder_parent'] . ' AND is_folder!=1 ORDER BY img_title ASC;'); |
|
118 if ( !$q ) |
|
119 $db->_die(); |
|
120 |
|
121 $folder_total = $db->numrows(); |
|
122 $folder_this = 0; |
|
123 $prev = false; |
|
124 $next = false; |
|
125 $next_title = ''; |
|
126 $prev_title = ''; |
|
127 |
|
128 $i = 0; |
|
129 |
|
130 while ( $r = $db->fetchrow() ) |
|
131 { |
|
132 $i++; |
|
133 if ( $i == $folder_total && $r['img_id'] == $img_id ) |
|
134 { |
|
135 $folder_this = $i; |
|
136 $next = false; |
|
137 } |
|
138 else if ( $i < $folder_total && $r['img_id'] == $img_id ) |
|
139 { |
|
140 $folder_this = $i; |
|
141 $next = true; |
|
142 } |
|
143 else |
|
144 { |
|
145 if ( $next ) |
|
146 { |
|
147 $next = $r['img_id']; |
|
148 $next_title = $r['img_title']; |
|
149 break; |
|
150 } |
|
151 $prev = $r['img_id']; |
|
152 $prev_title = $r['img_title']; |
|
153 } |
|
154 } |
|
155 |
|
156 if ( $next ) |
|
157 { |
|
158 $next_sanitized = sanitize_page_id($next_title); |
|
159 $next_url = ( isset($hier) ) ? makeUrlNS('Gallery', $hier . $next_sanitized ) : makeUrlNS('Gallery', $next); |
|
160 } |
|
161 if ( $prev ) |
|
162 { |
|
163 $prev_sanitized = sanitize_page_id($prev_title); |
|
164 $prev_url = ( isset($hier) ) ? makeUrlNS('Gallery', $hier . $prev_sanitized ) : makeUrlNS('Gallery', $prev); |
|
165 } |
|
166 |
|
167 $db->free_result(); |
|
168 |
|
169 $template->tpl_strings['PAGE_NAME'] = 'Gallery image: ' . htmlspecialchars($row['img_title']); |
|
170 $title_spacey = strtolower(htmlspecialchars($row['img_title'])); |
|
171 |
|
172 $perms = $session->fetch_page_acl(strval($img_id), 'Gallery'); |
|
173 |
|
174 $template->header(); |
|
175 |
|
176 $img_id = intval($img_id); |
|
177 $bc_folders = gallery_imgid_to_folder($img_id); |
|
178 $bc_folders = array_reverse($bc_folders); |
|
179 $bc_url = ''; |
|
180 $breadcrumbs = array(); |
|
181 $breadcrumbs[] = '<a href="' . makeUrlNS('Special', 'Gallery') . '">Gallery index</a>'; |
|
182 |
|
183 foreach ( $bc_folders as $folder ) |
|
184 { |
|
185 $bc_url .= '/' . dirtify_page_id($folder); |
|
186 $breadcrumbs[] = '<a href="' . makeUrlNS('Special', 'Gallery' . $bc_url, false, true) . '">' . htmlspecialchars($folder) . '</a>'; |
|
187 } |
|
188 |
|
189 $breadcrumbs[] = htmlspecialchars($row['img_title']); |
|
190 |
|
191 // From here, this breadcrumb stuff is a piece of... sourdough French bread :-) *smacks lips* |
|
192 echo '<div class="tblholder" style="padding: 4px; margin-bottom: 7px;">'; |
|
193 // The actual breadcrumbs |
|
194 echo '<b><small>' . implode(' » ', $breadcrumbs) . '</small></b>'; |
|
195 echo '</div>'; |
|
196 |
|
197 echo '<div style="text-align: center; margin: 10px auto; border: 1px solid #DDDDDD; padding: 7px 10px; display: table;">'; |
|
198 $img_url = makeUrlNS('Special', 'GalleryFetcher/preview/' . $img_id); |
|
199 $img_href = makeUrlNS('Special', 'GalleryFetcher/full/' . $img_id); |
|
200 |
|
201 if ( $perms->get_permissions('gal_full_res') ) |
|
202 { |
|
203 echo '<a href="' . $img_href . '" title="Click to view this image at full resolution, right click to save image" onclick="window.open(this.href, \'\', \'toolbar=no,address=no,menus=no,status=no\'); return false;">'; |
|
204 } |
|
205 |
|
206 echo '<img alt="Image preview (640px max width)" src="' . $img_url . '" style="border-width: 0; margin-bottom: 5px; display: block;" />'; |
|
207 |
|
208 if ( $perms->get_permissions('gal_full_res') ) |
|
209 { |
|
210 echo '</a>'; |
|
211 } |
|
212 |
|
213 echo '<table border="0" width="100%"><tr><td style="text-align: left; width: 24px;">'; |
|
214 |
|
215 // Prev button |
|
216 if ( $prev ) |
|
217 echo '<a href="' . $prev_url . '"><img style="border-width: 0px;" alt="< Previous" src="' . scriptPath . '/plugins/gallery/prev.gif" /></a>'; |
|
218 //echo '</td><td style="text-align: left;">'; |
|
219 // if ( $prev ) |
|
220 // echo '<a href="' . $prev_url . '">previous image</a>'; |
|
221 |
|
222 echo '</td><td style="text-align: center; letter-spacing: 5px;">'; |
|
223 |
|
224 // Image title |
|
225 echo $title_spacey; |
|
226 |
|
227 echo '</td><td style="text-align: right; width: 24px;">'; |
|
228 |
|
229 // Next button |
|
230 if ( $next ) |
|
231 // echo '<a href="' . $next_url . '">next image</a>'; |
|
232 //echo '</td><td style="text-align: right;">'; |
|
233 if ( $next ) |
|
234 echo '<a href="' . $next_url . '"><img style="border-width: 0px;" alt="< Previous" src="' . scriptPath . '/plugins/gallery/next.gif" /></a>'; |
|
235 |
|
236 echo '</td></tr>'; |
|
237 echo '<tr><td colspan="3">' . "image $folder_this of $folder_total" . '</td></tr>'; |
|
238 echo '</table>'; |
|
239 echo '</div>'; |
|
240 |
|
241 if ( $session->user_level >= USER_LEVEL_ADMIN ) |
|
242 { |
|
243 echo '<div style="float: right;">[ <a href="' . makeUrlNS('Special', 'GalleryUpload', 'edit_img=' . $img_id, true) . '">edit image</a> ]</div>'; |
|
244 } |
|
245 |
|
246 if ( !empty($row['img_desc']) ) |
|
247 { |
|
248 echo '<h2>Image description</h2>'; |
|
249 |
|
250 $desc = RenderMan::render($row['img_desc']); |
|
251 echo $desc; |
|
252 } |
|
253 |
|
254 echo '<div class="tblholder" style="font-size: smaller; display: table;' . ( empty($row['img_desc']) ? '' : 'margin: 0 auto;' ) . '"> |
|
255 <table border="0" cellspacing="1" cellpadding="3">'; |
|
256 |
|
257 // By the time I got to this point, it was 1:32AM (I was on vacation) and my 5-hour playlist on my iPod had been around about 3 times today. |
|
258 // So I'm glad this is like the last thing on the list tonight. |
|
259 |
|
260 echo '<tr><th colspan="2">Image details</th></tr>'; |
|
261 echo '<tr><td class="row2">Uploaded:</td><td class="row1">' . date('F d, Y h:i a', $row['img_time_upload']) . '</td></tr>'; |
|
262 echo '<tr><td class="row2">Last modified:</td><td class="row1">' . date('F d, Y h:i a', $row['img_time_mod']) . '</td></tr>'; |
|
263 |
|
264 echo '</table></div>'; |
|
265 |
|
266 $template->footer(); |
|
267 |
|
268 } |
|
269 |
|
270 ?> |