1 | <?php |
---|
2 | defined('BATCH_DOWNLOAD_PATH') or die('Hacking attempt!'); |
---|
3 | |
---|
4 | # this file contains all functions directly called by the triggers # |
---|
5 | |
---|
6 | /* define page section from url */ |
---|
7 | function batch_download_section_init() |
---|
8 | { |
---|
9 | global $tokens, $page, $conf; |
---|
10 | |
---|
11 | if ($tokens[0] == 'download') |
---|
12 | { |
---|
13 | if (check_download_access() === false) access_denied(); |
---|
14 | |
---|
15 | add_event_handler('loc_begin_page_header', 'batch_download_page_header'); |
---|
16 | |
---|
17 | $page['section'] = 'download'; |
---|
18 | $page['section_title'] = '<a href="'.get_absolute_root_url().'">'.l10n('Home').'</a>'.$conf['level_separator'].l10n('Batch Downloader').$conf['level_separator']; |
---|
19 | $page['title'] = l10n('Batch Downloader'); |
---|
20 | |
---|
21 | switch (@$tokens[1]) |
---|
22 | { |
---|
23 | case 'init_zip': |
---|
24 | $page['sub_section'] = 'init_zip'; |
---|
25 | $page['section_title'].= l10n('Generate ZIP'); |
---|
26 | break; |
---|
27 | case 'view': |
---|
28 | $page['sub_section'] = 'view'; |
---|
29 | $page['section_title'].= l10n('Edit the set'); |
---|
30 | if (isset($conf['GThumb']) && is_array($conf['GThumb'])) |
---|
31 | { |
---|
32 | $conf['GThumb']['big_thumb'] = false; // big thumb is buggy with removes |
---|
33 | } |
---|
34 | break; |
---|
35 | default: |
---|
36 | redirect('index.php'); |
---|
37 | } |
---|
38 | } |
---|
39 | } |
---|
40 | |
---|
41 | function batch_download_page_header() |
---|
42 | { |
---|
43 | global $page; |
---|
44 | $page['body_id'] = 'theBatchDownloader'; |
---|
45 | } |
---|
46 | |
---|
47 | /* download section */ |
---|
48 | function batch_download_page() |
---|
49 | { |
---|
50 | global $page; |
---|
51 | |
---|
52 | if (isset($page['section']) and $page['section'] == 'download') |
---|
53 | { |
---|
54 | include(BATCH_DOWNLOAD_PATH . '/include/download.inc.php'); |
---|
55 | } |
---|
56 | } |
---|
57 | |
---|
58 | |
---|
59 | /* add buttons on thumbnails list */ |
---|
60 | function batch_download_index_button() |
---|
61 | { |
---|
62 | global $page, $template, $user, $conf; |
---|
63 | |
---|
64 | // check accesses |
---|
65 | if ( !count($page['items']) or !isset($page['section']) ) return; |
---|
66 | |
---|
67 | if (check_download_access() === false) return; |
---|
68 | |
---|
69 | switch ($page['section']) |
---|
70 | { |
---|
71 | case 'categories': |
---|
72 | if (!isset($page['category']) && !isset($page['chronology_field'])) return; // don't download the full gallery in flat mode ! |
---|
73 | |
---|
74 | if (!in_array('categories', $conf['batch_download']['what'])) return; |
---|
75 | break; |
---|
76 | |
---|
77 | case 'collections': |
---|
78 | if (!in_array('collections', $conf['batch_download']['what'])) return; |
---|
79 | break; |
---|
80 | |
---|
81 | default: |
---|
82 | if (!in_array('specials', $conf['batch_download']['what'])) return; |
---|
83 | } |
---|
84 | |
---|
85 | |
---|
86 | // download the set |
---|
87 | if ( isset($_GET['action']) and $_GET['action']=='advdown_set' ) |
---|
88 | { |
---|
89 | $set = get_set_info_from_page(); |
---|
90 | |
---|
91 | if ($set !== false) |
---|
92 | { |
---|
93 | $BatchDownloader = new BatchDownloader('new', $page['items'], $set['type'], $set['id'], $set['size']); |
---|
94 | |
---|
95 | if ($BatchDownloader->getParam('nb_images') != 0) |
---|
96 | { |
---|
97 | // if we plan only one zip with less elements than 'max_elements', the download starts immediately |
---|
98 | if ( |
---|
99 | $BatchDownloader->getParam('nb_images') <= $conf['batch_download']['max_elements'] |
---|
100 | and $BatchDownloader->getParam('size') == 'original' |
---|
101 | and $BatchDownloader->getEstimatedArchiveNumber() == 1 |
---|
102 | ) |
---|
103 | { |
---|
104 | $BatchDownloader->createNextArchive(true); // make sure we have only one zip, even if 'max_size' is exceeded |
---|
105 | |
---|
106 | $u_download = get_root_url().BATCH_DOWNLOAD_PATH . 'download.php?set_id='.$BatchDownloader->getParam('id').'&zip=1'; |
---|
107 | |
---|
108 | $null = null; |
---|
109 | $template->block_footer_script(null, 'setTimeout("document.location.href = \''.$u_download.'\';", 1000);', $null, $null); |
---|
110 | |
---|
111 | $page['infos'][] = sprintf(l10n('The archive is downloading, if the download doesn\'t start automatically please <a href="%s">click here</a>'), $u_download); |
---|
112 | } |
---|
113 | // otherwise we go to summary page |
---|
114 | else |
---|
115 | { |
---|
116 | redirect(add_url_params(BATCH_DOWNLOAD_PUBLIC . 'init_zip', array('set_id'=>$BatchDownloader->getParam('id')))); |
---|
117 | } |
---|
118 | } |
---|
119 | else |
---|
120 | { |
---|
121 | $BatchDownloader->delete(); |
---|
122 | unset($BatchDownloader); |
---|
123 | |
---|
124 | $page['errors'][] = sprintf(l10n('Sorry, there is nothing to download. Some files may have been excluded because of <i title="Authorized types are : %s">filetype restrictions</i>.'), implode(', ', $conf['batch_download']['allowed_ext'])); |
---|
125 | } |
---|
126 | } |
---|
127 | } |
---|
128 | |
---|
129 | if ($page['section'] == 'collections') |
---|
130 | { |
---|
131 | $url = $_SERVER['REQUEST_URI']; |
---|
132 | } |
---|
133 | else |
---|
134 | { |
---|
135 | $url = duplicate_index_url(array(), array('action')); |
---|
136 | } |
---|
137 | |
---|
138 | $url = add_url_params($url, array('action'=>'advdown_set', 'down_size'=>'')); |
---|
139 | |
---|
140 | // toolbar button |
---|
141 | $template->assign(array( |
---|
142 | 'BATCH_DOWNLOAD_PATH' => BATCH_DOWNLOAD_PATH, |
---|
143 | 'BATCH_DWN_COUNT' => count($page['items']), |
---|
144 | 'BATCH_DWN_URL' => $url, |
---|
145 | )); |
---|
146 | |
---|
147 | foreach (ImageStdParams::get_defined_type_map() as $params) |
---|
148 | { |
---|
149 | $template->append( |
---|
150 | 'BATCH_DOWNLOAD_SIZES', |
---|
151 | array( |
---|
152 | 'TYPE' => $params->type, |
---|
153 | 'DISPLAY' => l10n($params->type), |
---|
154 | 'SIZE' => $params->sizing->ideal_size[0].' x '.$params->sizing->ideal_size[1], |
---|
155 | ) |
---|
156 | ); |
---|
157 | } |
---|
158 | $template->append( |
---|
159 | 'BATCH_DOWNLOAD_SIZES', |
---|
160 | array( |
---|
161 | 'TYPE' => 'original', |
---|
162 | 'DISPLAY' => l10n('Original'), |
---|
163 | 'SIZE' => null, |
---|
164 | ) |
---|
165 | ); |
---|
166 | |
---|
167 | $template->set_filename('batchdwn_button', realpath(BATCH_DOWNLOAD_PATH.'template/download_button.tpl')); |
---|
168 | $button = $template->parse('batchdwn_button', true); |
---|
169 | $template->add_index_button($button, 50); |
---|
170 | $template->concat('COLLECTION_ACTIONS', $button); |
---|
171 | } |
---|
172 | |
---|
173 | |
---|
174 | /* menu block */ |
---|
175 | function batch_download_add_menublock($menu_ref_arr) |
---|
176 | { |
---|
177 | global $user; |
---|
178 | |
---|
179 | $menu = &$menu_ref_arr[0]; |
---|
180 | if ($menu->get_id() != 'menubar') return; |
---|
181 | |
---|
182 | if (check_download_access() === false) return; |
---|
183 | |
---|
184 | $query = ' |
---|
185 | SELECT id |
---|
186 | FROM '.BATCH_DOWNLOAD_TSETS.' |
---|
187 | WHERE |
---|
188 | user_id = '.$user['id'].' |
---|
189 | AND status != "done" |
---|
190 | LIMIT 1 |
---|
191 | ;'; |
---|
192 | $result = pwg_query($query); |
---|
193 | if (!pwg_db_num_rows($result)) return; |
---|
194 | |
---|
195 | $menu->register_block(new RegisteredBlock('mbBatchDownloader', l10n('Batch Downloader'), 'BatchDownloader')); |
---|
196 | } |
---|
197 | |
---|
198 | function batch_download_applymenu($menu_ref_arr) |
---|
199 | { |
---|
200 | global $template, $conf, $user; |
---|
201 | |
---|
202 | $menu = &$menu_ref_arr[0]; |
---|
203 | $block = $menu->get_block('mbBatchDownloader'); |
---|
204 | |
---|
205 | if ($block != null) |
---|
206 | { |
---|
207 | $query = ' |
---|
208 | SELECT id |
---|
209 | FROM '.BATCH_DOWNLOAD_TSETS.' |
---|
210 | WHERE |
---|
211 | user_id = '.$user['id'].' |
---|
212 | AND status != "done" |
---|
213 | ;'; |
---|
214 | $sets = array_from_query($query, 'id'); |
---|
215 | |
---|
216 | $data = array(); |
---|
217 | foreach ($sets as $set_id) |
---|
218 | { |
---|
219 | $BatchDownloader = new BatchDownloader($set_id); |
---|
220 | $set = $BatchDownloader->getSetInfo(); |
---|
221 | |
---|
222 | array_push($data, array( |
---|
223 | 'URL' => add_url_params(BATCH_DOWNLOAD_PUBLIC . 'init_zip', array('set_id'=>$BatchDownloader->getParam('id'))), |
---|
224 | 'TITLE' => str_replace('"', "'", strip_tags($set['COMMENT'])), |
---|
225 | 'NAME' => $set['sNAME'], |
---|
226 | 'COUNT' => $set['NB_IMAGES'], |
---|
227 | )); |
---|
228 | } |
---|
229 | |
---|
230 | $template->set_template_dir(BATCH_DOWNLOAD_PATH . 'template/'); |
---|
231 | $block->set_title(l10n('Downloads')); |
---|
232 | $block->template = 'menublock_batch_down.tpl'; |
---|
233 | $block->data = $data; |
---|
234 | } |
---|
235 | } |
---|
236 | |
---|
237 | |
---|
238 | /* archives and databse cleanup */ |
---|
239 | function batch_download_clean() |
---|
240 | { |
---|
241 | global $conf; |
---|
242 | |
---|
243 | // we only search for old downloads every hour, nevermind which user is connected |
---|
244 | if ($conf['batch_download']['last_clean'] > time() - 3600) return; |
---|
245 | |
---|
246 | $conf['batch_download']['last_clean'] = time(); |
---|
247 | conf_update_param('batch_download', serialize($conf['batch_download'])); |
---|
248 | |
---|
249 | // set old sets as done and clean images table |
---|
250 | $query = ' |
---|
251 | DELETE i |
---|
252 | FROM '.BATCH_DOWNLOAD_TIMAGES.' AS i |
---|
253 | INNER JOIN '.BATCH_DOWNLOAD_TSETS.' AS s |
---|
254 | ON i.set_id = s.id |
---|
255 | WHERE |
---|
256 | status != "done" AND |
---|
257 | date_creation < DATE_SUB(NOW(), INTERVAL '.$conf['batch_download']['archive_timeout'].' HOUR) |
---|
258 | ;'; |
---|
259 | pwg_query($query); |
---|
260 | |
---|
261 | $query = ' |
---|
262 | UPDATE '.BATCH_DOWNLOAD_TSETS.' |
---|
263 | SET status = "done" |
---|
264 | WHERE |
---|
265 | status != "done" AND |
---|
266 | date_creation < DATE_SUB(NOW(), INTERVAL '.$conf['batch_download']['archive_timeout'].' HOUR) |
---|
267 | ;'; |
---|
268 | pwg_query($query); |
---|
269 | |
---|
270 | // remove old archives |
---|
271 | $zips = glob(BATCH_DOWNLOAD_LOCAL . 'u-*/*.zip'); |
---|
272 | |
---|
273 | if (is_array($zips)) |
---|
274 | { |
---|
275 | foreach ($zips as $zip) |
---|
276 | { |
---|
277 | if (filemtime($zip) < time()-$conf['batch_download']['archive_timeout']*3600) |
---|
278 | { |
---|
279 | unlink($zip); |
---|
280 | } |
---|
281 | } |
---|
282 | } |
---|
283 | } |
---|
284 | |
---|
285 | /* ajax request to remove an image */ |
---|
286 | function batch_downloader_remove_image() |
---|
287 | { |
---|
288 | if (!isset($_POST['action']) || $_POST['action']!='bd_remove_image') return; |
---|
289 | |
---|
290 | check_status(ACCESS_CLASSIC); |
---|
291 | |
---|
292 | if (isset($_POST['set_id']) and isset($_POST['toggle_id'])) |
---|
293 | { |
---|
294 | try |
---|
295 | { |
---|
296 | $BatchDownloader = new BatchDownloader($_POST['set_id']); |
---|
297 | $BatchDownloader->removeImages(array($_POST['toggle_id'])); |
---|
298 | echo 'ok'; |
---|
299 | } |
---|
300 | catch (Exception $e) |
---|
301 | { |
---|
302 | echo 'error'; |
---|
303 | } |
---|
304 | } |
---|
305 | else |
---|
306 | { |
---|
307 | echo 'error'; |
---|
308 | } |
---|
309 | |
---|
310 | exit(0); |
---|
311 | } |
---|
312 | |
---|
313 | ?> |
---|