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

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

new advanced param $confbatch_download_additional_ext, default is $confpicture_ext

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