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