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