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

Last change on this file since 21422 was 21422, checked in by mistic100, 11 years ago

add option too allow/disallow download of categories/collections/specials

File size: 7.1 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');
[16379]30        break;
31      default:
32        redirect('index.php');
33    }
34  }
35}
36
[21206]37function batch_download_page_header()
38{
39  global $page;
40  $page['body_id'] = 'theBatchDownloader';
41}
42
[16379]43/* download section */
44function 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 */
56function 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':
68    if (!isset($page['category'])) return; // don't download the full gallery in flat mode !
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    {
89      $BatchDownloader = new BatchDownloader('new', $page['items'], $set['type'], $set['id']);
[16392]90      $BatchDownloader->getEstimatedArchiveNumber();
91     
[16400]92      // if we plan only one zip with less elements than 'max_elements', the download starts immediately
[16392]93      if (
94        $BatchDownloader->getParam('nb_images') <= $conf['batch_download']['max_elements']
95        and $BatchDownloader->getParam('nb_zip') == 1
96      )
97      {
[16400]98        $BatchDownloader->createNextArchive(true); // make sure we have only one zip, even if 'max_size' is exceeded
[16392]99       
[17177]100        $u_download = get_root_url().BATCH_DOWNLOAD_PATH . 'download.php?set_id='.$BatchDownloader->getParam('id').'&amp;zip=1';
[16392]101       
102        $null = null;
103        $template->block_footer_script(null, 'setTimeout("document.location.href = \''.$u_download.'\';", 1000);', $null, $null);
104       
105        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));
106      }
[16400]107      // oterwise we go to summary page
[16392]108      else
109      {
[17177]110        redirect(add_url_params(BATCH_DOWNLOAD_PUBLIC . 'init_zip', array('set_id'=>$BatchDownloader->getParam('id'))));
[16392]111      }
[16379]112    }
113  }
114 
[16592]115  if ($page['section'] == 'collections')
116  {
117    $url = $_SERVER['REQUEST_URI'];
118  }
119  else
120  {
121    $url = duplicate_index_url(array(), array('action'));
122  }
123 
[17177]124  $url = add_url_params($url, array('action'=>'advdown_set'));
125 
[16379]126  // toolbar button
[16697]127  $button = '<script type="text/javascript">var batchdown_count = '.count($page['items']).'; var batchdown_string = "'.l10n('Confirm the download of %d pictures?').'";</script>
[17177]128    <li><a href="'. $url .'" title="'.l10n('Download all pictures of this selection').'" class="pwg-state-default pwg-button" rel="nofollow"
[16697]129    onClick="return confirm(batchdown_string.replace(\'%d\', batchdown_count));">
[20138]130                        <span class="pwg-icon batch-downloader-icon" style="background:url(\'' . get_root_url().BATCH_DOWNLOAD_PATH . 'template/zip.png\') center center no-repeat;">&nbsp;</span><span class="pwg-button-text">'.l10n('Download').'</span>
[16379]131                </a></li>';
132  $template->concat('PLUGIN_INDEX_ACTIONS', $button);
[16592]133  $template->concat('COLLECTION_ACTIONS', $button);
[16379]134}
135
136
137/* menu block */
138function batch_download_add_menublock($menu_ref_arr)
139{
140  global $user;
141 
142  $menu = &$menu_ref_arr[0];
143  if ($menu->get_id() != 'menubar') return;
144 
145  if (check_download_access() === false) return;
146 
147  $query = '
148SELECT id
149  FROM '.BATCH_DOWNLOAD_TSETS.'
150  WHERE
151    user_id = '.$user['id'].'
[16598]152    AND status != "done"
153  LIMIT 1
[16379]154;';
155  $result = pwg_query($query);
156  if (!pwg_db_num_rows($result)) return;
157 
[16598]158  $menu->register_block(new RegisteredBlock('mbBatchDownloader', l10n('Batch Downloader'), 'BatchDownloader'));
[16379]159}
160
161function batch_download_applymenu($menu_ref_arr)
162{
163  global $template, $conf, $user;
164 
165  $menu = &$menu_ref_arr[0];
[16598]166  $block = $menu->get_block('mbBatchDownloader');
[16379]167 
168  if ($block != null)
169  {
170    $query = '
171SELECT id
172  FROM '.BATCH_DOWNLOAD_TSETS.'
173  WHERE
174    user_id = '.$user['id'].'
175    AND status != "done"
176;';
177    $sets = array_from_query($query, 'id');
178   
179    $data = array();
180    foreach ($sets as $set_id)
181    {
182      $BatchDownloader = new BatchDownloader($set_id);
183      $set = $BatchDownloader->getSetInfo();
184     
185      array_push($data, array(
[17177]186        'URL' => add_url_params(BATCH_DOWNLOAD_PUBLIC . 'init_zip', array('set_id'=>$BatchDownloader->getParam('id'))),
[16609]187        'TITLE' => str_replace('"', "'", strip_tags($set['COMMENT'])),
[16379]188        'NAME' => $set['sNAME'],
189        'COUNT' => $set['NB_IMAGES'],
190        ));
191    }
192   
193    $template->set_template_dir(BATCH_DOWNLOAD_PATH . 'template/');
194    $block->set_title(l10n('Downloads'));
[16598]195    $block->template = 'menublock_batch_down.tpl';
[16379]196    $block->data = $data;
197  }
198}
199
200
201/* archives and databse cleanup */
202function batch_download_clean()
203{
204  global $conf;
205 
206  // we only search for old downloads every hour, nevermind which user is connected
207  if ($conf['batch_download']['last_clean'] > time() - 3600) return;
208 
209  $conf['batch_download']['last_clean'] = time();
210  conf_update_param('batch_download', serialize($conf['batch_download']));
211 
[16592]212  // set old sets as done and clean images table
[16379]213  $query = '
[16592]214DELETE i
215  FROM '.BATCH_DOWNLOAD_TIMAGES.' AS i
216    INNER JOIN '.BATCH_DOWNLOAD_TSETS.' AS s
217    ON i.set_id = s.id
218  WHERE
219    status != "done" AND
220    date_creation < DATE_SUB(NOW(), INTERVAL '.$conf['batch_download']['archive_timeout'].' HOUR)
221;';
222  pwg_query($query);
223 
224  $query = '
225UPDATE '.BATCH_DOWNLOAD_TSETS.'
226  SET status = "done"
[16379]227  WHERE
[16592]228    status != "done" AND
[16379]229    date_creation < DATE_SUB(NOW(), INTERVAL '.$conf['batch_download']['archive_timeout'].' HOUR)
230;';
[16592]231  pwg_query($query);
[16379]232 
233  // remove old archives
234  $zips = glob(BATCH_DOWNLOAD_LOCAL . 'u-*/*.zip');
[17318]235 
236  if (is_array($zips))
[16379]237  {
[17318]238    foreach ($zips as $zip)
[16379]239    {
[17318]240      if (filemtime($zip) < time()-$conf['batch_download']['archive_timeout']*3600)
241      {
242        unlink($zip);
243      }
[16379]244    }
245  }
246}
247
248?>
Note: See TracBrowser for help on using the repository browser.