source: extensions/BatchDownloader/include/events.inc.php @ 25931

Last change on this file since 25931 was 25931, checked in by mistic100, 10 years ago

allow to configure only one downloadable size

File size: 8.8 KB
RevLine 
[16379]1<?php
2defined('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 */
7function 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');
[23362]30        if (isset($conf['GThumb']) && is_array($conf['GThumb']))
31        {
32          $conf['GThumb']['big_thumb'] = false; // big thumb is buggy with removes
33        }
[16379]34        break;
35      default:
36        redirect('index.php');
37    }
38  }
39}
40
[21206]41function batch_download_page_header()
42{
43  global $page;
44  $page['body_id'] = 'theBatchDownloader';
45}
46
[16379]47/* download section */
48function 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 */
60function batch_download_index_button()
61{
[16392]62  global $page, $template, $user, $conf;
[16379]63 
[21422]64  // check accesses
[16379]65  if ( !count($page['items']) or !isset($page['section']) ) return;
66 
67  if (check_download_access() === false) return;
68 
[21422]69  switch ($page['section'])
70  {
71  case 'categories':
[23321]72    if (!isset($page['category']) && !isset($page['chronology_field'])) return; // don't download the full gallery in flat mode !
[21422]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 
[16379]86  // download the set
87  if ( isset($_GET['action']) and $_GET['action']=='advdown_set' )
88  {
89    $set = get_set_info_from_page();
90   
[23771]91    if ($set !== false && count($set['items']))
[16379]92    {
[23771]93      $BatchDownloader = new BatchDownloader('new', $set['items'], $set['type'], $set['id'], $set['size']);
[16392]94     
[23280]95      if ($BatchDownloader->getParam('nb_images') != 0)
[16392]96      {
[23280]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']
[23290]100          and $BatchDownloader->getParam('size') == 'original'
101          and $BatchDownloader->getEstimatedArchiveNumber() == 1
[23280]102        )
103        {
104          $BatchDownloader->createNextArchive(true); // make sure we have only one zip, even if 'max_size' is exceeded
105         
[23760]106          $u_download = get_root_url().BATCH_DOWNLOAD_PATH . 'download.php?set_id='.$BatchDownloader->getParam('id').'&zip=1';
[23280]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        }
[16392]118      }
119      else
120      {
[23280]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']));
[16392]125      }
[16379]126    }
127  }
128 
[16592]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 
[23290]138  $url = add_url_params($url, array('action'=>'advdown_set', 'down_size'=>''));
[17177]139 
[16379]140  // toolbar button
[23290]141  $template->assign(array(
142    'BATCH_DOWNLOAD_PATH' => BATCH_DOWNLOAD_PATH,
143    'BATCH_DWN_COUNT' => count($page['items']),
144    'BATCH_DWN_URL' => $url,
145    ));
[23318]146 
[25931]147  if ($conf['batch_download']['multisize'])
[23290]148  {
[25931]149    foreach (ImageStdParams::get_defined_type_map() as $params)
150    {
151      $template->append(
152        'BATCH_DWN_SIZES',
153        array(
154          'TYPE' => $params->type,
155          'DISPLAY' => l10n($params->type),
156          'SIZE' => $params->sizing->ideal_size[0].' x '.$params->sizing->ideal_size[1],
157          )
158        );
159        if ($params->type == $conf['batch_download']['photo_size']) break;
160    }
161    if ($conf['batch_download']['photo_size'] == 'original')
162    {
163      $template->append(
164        'BATCH_DWN_SIZES',
165        array(
166          'TYPE' => 'original',
167          'DISPLAY' => l10n('Original'),
168          'SIZE' => null,
169          )
170        );
171    }
[23290]172  }
[25931]173  else
[23383]174  {
[25931]175    $template->assign('BATCH_DWN_SIZE', $conf['batch_download']['photo_size']);
[23383]176  }
[23290]177   
[23359]178  $template->set_filename('batchdwn_button', realpath(BATCH_DOWNLOAD_PATH.'template/download_button.tpl'));
[23290]179  $button = $template->parse('batchdwn_button', true);
180  $template->add_index_button($button, 50);
[16592]181  $template->concat('COLLECTION_ACTIONS', $button);
[16379]182}
183
184
185/* menu block */
186function batch_download_add_menublock($menu_ref_arr)
[23280]187{
188  global $user;
[16379]189 
[23280]190  $menu = &$menu_ref_arr[0];
191  if ($menu->get_id() != 'menubar') return;
192 
193  if (check_download_access() === false) return;
194 
195  $query = '
196SELECT id
197  FROM '.BATCH_DOWNLOAD_TSETS.'
198  WHERE
199    user_id = '.$user['id'].'
[16598]200    AND status != "done"
[23280]201  LIMIT 1
[16379]202;';
[23280]203  $result = pwg_query($query);
204  if (!pwg_db_num_rows($result)) return;
[16379]205 
[16598]206  $menu->register_block(new RegisteredBlock('mbBatchDownloader', l10n('Batch Downloader'), 'BatchDownloader'));
[16379]207}
208
209function batch_download_applymenu($menu_ref_arr)
210{
[23280]211  global $template, $conf, $user;
[16379]212 
[23280]213  $menu = &$menu_ref_arr[0];
[16598]214  $block = $menu->get_block('mbBatchDownloader');
[16379]215 
216  if ($block != null)
[23280]217  {
218    $query = '
219SELECT id
220  FROM '.BATCH_DOWNLOAD_TSETS.'
221  WHERE
222    user_id = '.$user['id'].'
223    AND status != "done"
224;';
225    $sets = array_from_query($query, 'id');
[16379]226   
[23280]227    $data = array();
228    foreach ($sets as $set_id)
229    {
230      $BatchDownloader = new BatchDownloader($set_id);
231      $set = $BatchDownloader->getSetInfo();
232     
233      array_push($data, array(
234        'URL' => add_url_params(BATCH_DOWNLOAD_PUBLIC . 'init_zip', array('set_id'=>$BatchDownloader->getParam('id'))),
235        'TITLE' => str_replace('"', "'", strip_tags($set['COMMENT'])),
236        'NAME' => $set['sNAME'],
237        'COUNT' => $set['NB_IMAGES'],
238        ));
239    }
240   
[16379]241    $template->set_template_dir(BATCH_DOWNLOAD_PATH . 'template/');
242    $block->set_title(l10n('Downloads'));
[16598]243    $block->template = 'menublock_batch_down.tpl';
[16379]244    $block->data = $data;
245  }
246}
247
248
249/* archives and databse cleanup */
250function batch_download_clean()
251{
252  global $conf;
253 
[23383]254  $time = time();
255 
[16379]256  // we only search for old downloads every hour, nevermind which user is connected
[23383]257  if ($conf['batch_download']['last_clean'] > $time - 3600) return;
[16379]258 
[23383]259  $conf['batch_download']['last_clean'] = $time;
[16379]260  conf_update_param('batch_download', serialize($conf['batch_download']));
261 
[16592]262  // set old sets as done and clean images table
[16379]263  $query = '
[16592]264DELETE i
265  FROM '.BATCH_DOWNLOAD_TIMAGES.' AS i
266    INNER JOIN '.BATCH_DOWNLOAD_TSETS.' AS s
267    ON i.set_id = s.id
268  WHERE
269    status != "done" AND
270    date_creation < DATE_SUB(NOW(), INTERVAL '.$conf['batch_download']['archive_timeout'].' HOUR)
271;';
272  pwg_query($query);
273 
274  $query = '
275UPDATE '.BATCH_DOWNLOAD_TSETS.'
276  SET status = "done"
[23280]277  WHERE
[16592]278    status != "done" AND
[16379]279    date_creation < DATE_SUB(NOW(), INTERVAL '.$conf['batch_download']['archive_timeout'].' HOUR)
280;';
[16592]281  pwg_query($query);
[16379]282 
283  // remove old archives
284  $zips = glob(BATCH_DOWNLOAD_LOCAL . 'u-*/*.zip');
[17318]285 
286  if (is_array($zips))
[16379]287  {
[17318]288    foreach ($zips as $zip)
[16379]289    {
[23383]290      if (filemtime($zip) < $time-$conf['batch_download']['archive_timeout']*3600)
[17318]291      {
292        unlink($zip);
293      }
[16379]294    }
295  }
296}
297
[23323]298/* ajax request to remove an image */
299function batch_downloader_remove_image()
300{
301  if (!isset($_POST['action']) || $_POST['action']!='bd_remove_image') return;
302 
303  check_status(ACCESS_CLASSIC);
304
305  if (isset($_POST['set_id']) and isset($_POST['toggle_id']))
306  {
307    try
308    {
309      $BatchDownloader = new BatchDownloader($_POST['set_id']);
310      $BatchDownloader->removeImages(array($_POST['toggle_id']));
311      echo 'ok';
312    }
313    catch (Exception $e)
314    {
315      echo 'error';
316    }
317  }
318  else
319  {
320    echo 'error';
321  }
322 
323  exit(0);
324}
325
[16379]326?>
Note: See TracBrowser for help on using the repository browser.