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 | /* unserialize conf and load language */ |
---|
7 | function batch_download_init() |
---|
8 | { |
---|
9 | global $conf; |
---|
10 | |
---|
11 | $conf['batch_download'] = unserialize($conf['batch_download']); |
---|
12 | load_language('plugin.lang', BATCH_DOWNLOAD_PATH); |
---|
13 | } |
---|
14 | |
---|
15 | |
---|
16 | /* define page section from url */ |
---|
17 | function batch_download_section_init() |
---|
18 | { |
---|
19 | global $tokens, $page, $conf; |
---|
20 | |
---|
21 | define('BATCH_DOWNLOAD_PUBLIC', make_index_url(array('section' => 'download')) . '/'); |
---|
22 | |
---|
23 | if ($tokens[0] == 'download') |
---|
24 | { |
---|
25 | if (check_download_access() === false) access_denied(); |
---|
26 | |
---|
27 | $page['section'] = 'download'; |
---|
28 | $page['title'] = '<a href="'.get_absolute_root_url().'">'.l10n('Home').'</a>'.$conf['level_separator'].l10n('Batch Downloader').$conf['level_separator']; |
---|
29 | |
---|
30 | switch (@$tokens[1]) |
---|
31 | { |
---|
32 | case 'init_zip': |
---|
33 | $page['sub_section'] = 'init_zip'; |
---|
34 | $page['title'].= l10n('Generate ZIP'); |
---|
35 | break; |
---|
36 | case 'view': |
---|
37 | $page['sub_section'] = 'view'; |
---|
38 | $page['title'].= l10n('Edit the set'); |
---|
39 | break; |
---|
40 | default: |
---|
41 | redirect('index.php'); |
---|
42 | } |
---|
43 | } |
---|
44 | } |
---|
45 | |
---|
46 | /* download section */ |
---|
47 | function batch_download_page() |
---|
48 | { |
---|
49 | global $page; |
---|
50 | |
---|
51 | if (isset($page['section']) and $page['section'] == 'download') |
---|
52 | { |
---|
53 | include(BATCH_DOWNLOAD_PATH . '/include/download.inc.php'); |
---|
54 | } |
---|
55 | } |
---|
56 | |
---|
57 | |
---|
58 | /* add buttons on thumbnails list */ |
---|
59 | function batch_download_index_button() |
---|
60 | { |
---|
61 | global $page, $template, $user, $conf; |
---|
62 | |
---|
63 | if ( !count($page['items']) or !isset($page['section']) ) return; |
---|
64 | |
---|
65 | if (check_download_access() === false) return; |
---|
66 | |
---|
67 | // download the set |
---|
68 | if ( isset($_GET['action']) and $_GET['action']=='advdown_set' ) |
---|
69 | { |
---|
70 | $set = get_set_info_from_page(); |
---|
71 | |
---|
72 | if ($set !== false) |
---|
73 | { |
---|
74 | $BatchDownloader = new BatchDownloader('new', $page['items'], $set['type'], $set['id']); |
---|
75 | $BatchDownloader->getEstimatedArchiveNumber(); |
---|
76 | |
---|
77 | // if we plan only one zip with less elements than 'max_elements', the download starts immediately |
---|
78 | if ( |
---|
79 | $BatchDownloader->getParam('nb_images') <= $conf['batch_download']['max_elements'] |
---|
80 | and $BatchDownloader->getParam('nb_zip') == 1 |
---|
81 | ) |
---|
82 | { |
---|
83 | $BatchDownloader->createNextArchive(true); // make sure we have only one zip, even if 'max_size' is exceeded |
---|
84 | |
---|
85 | $u_download = get_root_url().BATCH_DOWNLOAD_PATH . 'download.php?set_id='.$BatchDownloader->getParam('id').'&zip=1'; |
---|
86 | |
---|
87 | $null = null; |
---|
88 | $template->block_footer_script(null, 'setTimeout("document.location.href = \''.$u_download.'\';", 1000);', $null, $null); |
---|
89 | |
---|
90 | array_push($page['infos'], sprintf(l10n('The archive is downloading, if the download doesn\'t start automatically please <a href="%s">click here</a>'), $u_download)); |
---|
91 | } |
---|
92 | // oterwise we go to summary page |
---|
93 | else |
---|
94 | { |
---|
95 | redirect(add_url_params(BATCH_DOWNLOAD_PUBLIC . 'init_zip', array('set_id'=>$BatchDownloader->getParam('id')))); |
---|
96 | } |
---|
97 | } |
---|
98 | } |
---|
99 | |
---|
100 | if ($page['section'] == 'collections') |
---|
101 | { |
---|
102 | $url = $_SERVER['REQUEST_URI']; |
---|
103 | } |
---|
104 | else |
---|
105 | { |
---|
106 | $url = duplicate_index_url(array(), array('action')); |
---|
107 | } |
---|
108 | |
---|
109 | $url = add_url_params($url, array('action'=>'advdown_set')); |
---|
110 | |
---|
111 | // toolbar button |
---|
112 | $button = '<script type="text/javascript">var batchdown_count = '.count($page['items']).'; var batchdown_string = "'.l10n('Confirm the download of %d pictures?').'";</script> |
---|
113 | <li><a href="'. $url .'" title="'.l10n('Download all pictures of this selection').'" class="pwg-state-default pwg-button" rel="nofollow" |
---|
114 | onClick="return confirm(batchdown_string.replace(\'%d\', batchdown_count));"> |
---|
115 | <span class="pwg-icon batch-downloader-icon" style="background:url(\'' . get_root_url().BATCH_DOWNLOAD_PATH . 'template/zip.png\') center center no-repeat;"> </span><span class="pwg-button-text">'.l10n('Batch Downloader').'</span> |
---|
116 | </a></li>'; |
---|
117 | $template->concat('PLUGIN_INDEX_ACTIONS', $button); |
---|
118 | $template->concat('COLLECTION_ACTIONS', $button); |
---|
119 | } |
---|
120 | |
---|
121 | |
---|
122 | /* menu block */ |
---|
123 | function batch_download_add_menublock($menu_ref_arr) |
---|
124 | { |
---|
125 | global $user; |
---|
126 | |
---|
127 | $menu = &$menu_ref_arr[0]; |
---|
128 | if ($menu->get_id() != 'menubar') return; |
---|
129 | |
---|
130 | if (check_download_access() === false) return; |
---|
131 | |
---|
132 | $query = ' |
---|
133 | SELECT id |
---|
134 | FROM '.BATCH_DOWNLOAD_TSETS.' |
---|
135 | WHERE |
---|
136 | user_id = '.$user['id'].' |
---|
137 | AND status != "done" |
---|
138 | LIMIT 1 |
---|
139 | ;'; |
---|
140 | $result = pwg_query($query); |
---|
141 | if (!pwg_db_num_rows($result)) return; |
---|
142 | |
---|
143 | $menu->register_block(new RegisteredBlock('mbBatchDownloader', l10n('Batch Downloader'), 'BatchDownloader')); |
---|
144 | } |
---|
145 | |
---|
146 | function batch_download_applymenu($menu_ref_arr) |
---|
147 | { |
---|
148 | global $template, $conf, $user; |
---|
149 | |
---|
150 | $menu = &$menu_ref_arr[0]; |
---|
151 | $block = $menu->get_block('mbBatchDownloader'); |
---|
152 | |
---|
153 | if (!defined('BATCH_DOWNLOAD_PUBLIC')) define('BATCH_DOWNLOAD_PUBLIC', make_index_url(array('section' => 'download')) . '/'); |
---|
154 | |
---|
155 | if ($block != null) |
---|
156 | { |
---|
157 | $query = ' |
---|
158 | SELECT id |
---|
159 | FROM '.BATCH_DOWNLOAD_TSETS.' |
---|
160 | WHERE |
---|
161 | user_id = '.$user['id'].' |
---|
162 | AND status != "done" |
---|
163 | ;'; |
---|
164 | $sets = array_from_query($query, 'id'); |
---|
165 | |
---|
166 | $data = array(); |
---|
167 | foreach ($sets as $set_id) |
---|
168 | { |
---|
169 | $BatchDownloader = new BatchDownloader($set_id); |
---|
170 | $set = $BatchDownloader->getSetInfo(); |
---|
171 | |
---|
172 | array_push($data, array( |
---|
173 | 'URL' => add_url_params(BATCH_DOWNLOAD_PUBLIC . 'init_zip', array('set_id'=>$BatchDownloader->getParam('id'))), |
---|
174 | 'TITLE' => str_replace('"', "'", strip_tags($set['COMMENT'])), |
---|
175 | 'NAME' => $set['sNAME'], |
---|
176 | 'COUNT' => $set['NB_IMAGES'], |
---|
177 | )); |
---|
178 | } |
---|
179 | |
---|
180 | $template->set_template_dir(BATCH_DOWNLOAD_PATH . 'template/'); |
---|
181 | $block->set_title(l10n('Downloads')); |
---|
182 | $block->template = 'menublock_batch_down.tpl'; |
---|
183 | $block->data = $data; |
---|
184 | } |
---|
185 | } |
---|
186 | |
---|
187 | |
---|
188 | /* archives and databse cleanup */ |
---|
189 | function batch_download_clean() |
---|
190 | { |
---|
191 | global $conf; |
---|
192 | |
---|
193 | // we only search for old downloads every hour, nevermind which user is connected |
---|
194 | if ($conf['batch_download']['last_clean'] > time() - 3600) return; |
---|
195 | |
---|
196 | $conf['batch_download']['last_clean'] = time(); |
---|
197 | conf_update_param('batch_download', serialize($conf['batch_download'])); |
---|
198 | |
---|
199 | // set old sets as done and clean images table |
---|
200 | $query = ' |
---|
201 | DELETE i |
---|
202 | FROM '.BATCH_DOWNLOAD_TIMAGES.' AS i |
---|
203 | INNER JOIN '.BATCH_DOWNLOAD_TSETS.' AS s |
---|
204 | ON i.set_id = s.id |
---|
205 | WHERE |
---|
206 | status != "done" AND |
---|
207 | date_creation < DATE_SUB(NOW(), INTERVAL '.$conf['batch_download']['archive_timeout'].' HOUR) |
---|
208 | ;'; |
---|
209 | pwg_query($query); |
---|
210 | |
---|
211 | $query = ' |
---|
212 | UPDATE '.BATCH_DOWNLOAD_TSETS.' |
---|
213 | SET status = "done" |
---|
214 | WHERE |
---|
215 | status != "done" AND |
---|
216 | date_creation < DATE_SUB(NOW(), INTERVAL '.$conf['batch_download']['archive_timeout'].' HOUR) |
---|
217 | ;'; |
---|
218 | pwg_query($query); |
---|
219 | |
---|
220 | // remove old archives |
---|
221 | $zips = glob(BATCH_DOWNLOAD_LOCAL . 'u-*/*.zip'); |
---|
222 | |
---|
223 | if (is_array($zips)) |
---|
224 | { |
---|
225 | foreach ($zips as $zip) |
---|
226 | { |
---|
227 | if (filemtime($zip) < time()-$conf['batch_download']['archive_timeout']*3600) |
---|
228 | { |
---|
229 | unlink($zip); |
---|
230 | } |
---|
231 | } |
---|
232 | } |
---|
233 | } |
---|
234 | |
---|
235 | ?> |
---|