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

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

update for Piwigo 2.6 + code cleaning + fix unable to cancel set during generation

File size: 8.5 KB
Line 
1<?php
2defined('BATCH_DOWNLOAD_PATH') or die('Hacking attempt!');
3
4/* define page section from url */
5function batch_download_section_init()
6{
7  global $tokens, $page, $conf;
8
9  if ($tokens[0] == 'download')
10  {
11    if (check_download_access() === false) access_denied();
12
13    $page['body_id'] = 'theBatchDownloader';
14    $page['is_external'] = true;
15    $page['is_homepage'] = false;
16    $page['section'] = 'download';
17
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        if (isset($conf['GThumb']) && is_array($conf['GThumb']))
31        {
32          $conf['GThumb']['big_thumb'] = false; // big thumb is buggy with removes
33        }
34        break;
35      default:
36        redirect('index.php');
37    }
38  }
39}
40
41/* download section */
42function batch_download_page()
43{
44  global $page;
45
46  if (isset($page['section']) and $page['section'] == 'download')
47  {
48    include(BATCH_DOWNLOAD_PATH . '/include/download.inc.php');
49  }
50}
51
52
53/* add buttons on thumbnails list */
54function batch_download_index_button()
55{
56  global $page, $template, $user, $conf;
57
58  // check accesses
59  if ( !count($page['items']) or !isset($page['section']) ) return;
60
61  if (check_download_access() === false) return;
62
63  switch ($page['section'])
64  {
65  case 'categories':
66    if (!isset($page['category']) && !isset($page['chronology_field'])) return; // don't download the full gallery in flat mode !
67
68    if (!in_array('categories', $conf['batch_download']['what'])) return;
69    break;
70
71  case 'collections':
72    if (!in_array('collections', $conf['batch_download']['what'])) return;
73    break;
74
75  default:
76    if (!in_array('specials', $conf['batch_download']['what'])) return;
77  }
78
79
80  // download the set
81  if ( isset($_GET['action']) and $_GET['action']=='advdown_set' )
82  {
83    $set = get_set_info_from_page();
84
85    if ($set !== false && count($set['items']))
86    {
87      $BatchDownloader = new BatchDownloader('new', $set['items'], $set['type'], $set['id'], $set['size']);
88
89      if ($BatchDownloader->getParam('nb_images') != 0)
90      {
91        // if we plan only one zip with less elements than 'max_elements', the download starts immediately
92        if (
93          $BatchDownloader->getParam('nb_images') <= $conf['batch_download']['max_elements']
94          and $BatchDownloader->getParam('size') == 'original'
95          and $BatchDownloader->getEstimatedArchiveNumber() == 1
96        )
97        {
98          $BatchDownloader->createNextArchive(true); // make sure we have only one zip, even if 'max_size' is exceeded
99
100          $u_download = get_root_url().BATCH_DOWNLOAD_PATH . 'download.php?set_id='.$BatchDownloader->getParam('id').'&zip=1';
101
102          $null = null;
103          $template->block_footer_script(null, 'setTimeout("document.location.href = \''.$u_download.'\';", 1000);', $null, $null);
104
105          $page['infos'][] = l10n('The archive is downloading, if the download doesn\'t start automatically please <a href="%s">click here</a>', $u_download);
106        }
107        // otherwise we go to summary page
108        else
109        {
110          redirect(add_url_params(BATCH_DOWNLOAD_PUBLIC . 'init_zip', array('set_id'=>$BatchDownloader->getParam('id'))));
111        }
112      }
113      else
114      {
115        $BatchDownloader->delete();
116        unset($BatchDownloader);
117
118        $page['errors'][] = 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']));
119      }
120    }
121  }
122
123  if ($page['section'] == 'collections')
124  {
125    $url = $_SERVER['REQUEST_URI'];
126  }
127  else
128  {
129    $url = duplicate_index_url(array(), array('action'));
130  }
131
132  $url = add_url_params($url, array('action'=>'advdown_set', 'down_size'=>''));
133
134  // toolbar button
135  $template->assign(array(
136    'BATCH_DOWNLOAD_PATH' => BATCH_DOWNLOAD_PATH,
137    'BATCH_DWN_COUNT' => count($page['items']),
138    'BATCH_DWN_URL' => $url,
139    ));
140
141  if ($conf['batch_download']['multisize'])
142  {
143    foreach (ImageStdParams::get_defined_type_map() as $params)
144    {
145      $template->append(
146        'BATCH_DWN_SIZES',
147        array(
148          'TYPE' => $params->type,
149          'DISPLAY' => l10n($params->type),
150          'SIZE' => $params->sizing->ideal_size[0].' x '.$params->sizing->ideal_size[1],
151          )
152        );
153        if ($params->type == $conf['batch_download']['photo_size']) break;
154    }
155    if ($conf['batch_download']['photo_size'] == 'original')
156    {
157      $template->append(
158        'BATCH_DWN_SIZES',
159        array(
160          'TYPE' => 'original',
161          'DISPLAY' => l10n('Original'),
162          'SIZE' => null,
163          )
164        );
165    }
166  }
167  else
168  {
169    $template->assign('BATCH_DWN_SIZE', $conf['batch_download']['photo_size']);
170  }
171
172  $template->set_filename('batchdwn_button', realpath(BATCH_DOWNLOAD_PATH.'template/download_button.tpl'));
173  $button = $template->parse('batchdwn_button', true);
174  $template->add_index_button($button, 50);
175  $template->concat('COLLECTION_ACTIONS', $button);
176}
177
178
179/* menu block */
180function batch_download_add_menublock($menu_ref_arr)
181{
182  global $user;
183
184  $menu = &$menu_ref_arr[0];
185  if ($menu->get_id() != 'menubar') return;
186
187  if (check_download_access() === false) return;
188
189  $query = '
190SELECT id
191  FROM '.BATCH_DOWNLOAD_TSETS.'
192  WHERE
193    user_id = '.$user['id'].'
194    AND status != "done"
195  LIMIT 1
196;';
197  $result = pwg_query($query);
198  if (!pwg_db_num_rows($result)) return;
199
200  $menu->register_block(new RegisteredBlock('mbBatchDownloader', l10n('Batch Downloader'), 'BatchDownloader'));
201}
202
203function batch_download_applymenu($menu_ref_arr)
204{
205  global $template, $conf, $user;
206
207  $menu = &$menu_ref_arr[0];
208  $block = $menu->get_block('mbBatchDownloader');
209
210  if ($block != null)
211  {
212    $query = '
213SELECT id
214  FROM '.BATCH_DOWNLOAD_TSETS.'
215  WHERE
216    user_id = '.$user['id'].'
217    AND status != "done"
218;';
219    $sets = array_from_query($query, 'id');
220
221    $data = array();
222    foreach ($sets as $set_id)
223    {
224      $BatchDownloader = new BatchDownloader($set_id);
225      $set = $BatchDownloader->getSetInfo();
226
227      $data[] = array(
228        'URL' => add_url_params(BATCH_DOWNLOAD_PUBLIC . 'init_zip', array('set_id'=>$BatchDownloader->getParam('id'))),
229        'TITLE' => str_replace('"', "'", strip_tags($set['COMMENT'])),
230        'NAME' => $set['sNAME'],
231        'COUNT' => $set['NB_IMAGES'],
232        );
233    }
234
235    $template->set_template_dir(BATCH_DOWNLOAD_PATH . 'template/');
236    $block->set_title(l10n('Downloads'));
237    $block->template = 'menublock_batch_down.tpl';
238    $block->data = $data;
239  }
240}
241
242
243/* archives and databse cleanup */
244function batch_download_clean()
245{
246  global $conf;
247
248  $time = time();
249
250  // we only search for old downloads every hour, nevermind which user is connected
251  if ($conf['batch_download']['last_clean'] > $time - 3600) return;
252
253  $conf['batch_download']['last_clean'] = $time;
254  conf_update_param('batch_download', serialize($conf['batch_download']));
255
256  // set old sets as done and clean images table
257  $query = '
258DELETE i
259  FROM '.BATCH_DOWNLOAD_TIMAGES.' AS i
260    INNER JOIN '.BATCH_DOWNLOAD_TSETS.' AS s
261    ON i.set_id = s.id
262  WHERE
263    status != "done" AND
264    date_creation < DATE_SUB(NOW(), INTERVAL '.$conf['batch_download']['archive_timeout'].' HOUR)
265;';
266  pwg_query($query);
267
268  $query = '
269UPDATE '.BATCH_DOWNLOAD_TSETS.'
270  SET status = "done"
271  WHERE
272    status != "done" AND
273    date_creation < DATE_SUB(NOW(), INTERVAL '.$conf['batch_download']['archive_timeout'].' HOUR)
274;';
275  pwg_query($query);
276
277  // remove old archives
278  $zips = glob(BATCH_DOWNLOAD_LOCAL . 'u-*/*.zip');
279
280  if (is_array($zips))
281  {
282    foreach ($zips as $zip)
283    {
284      if (filemtime($zip) < $time-$conf['batch_download']['archive_timeout']*3600)
285      {
286        unlink($zip);
287      }
288    }
289  }
290}
291
292/* ajax request to remove an image */
293function batch_downloader_remove_image()
294{
295  if (!isset($_POST['action']) || $_POST['action']!='bd_remove_image') return;
296
297  check_status(ACCESS_CLASSIC);
298
299  if (isset($_POST['set_id']) and isset($_POST['toggle_id']))
300  {
301    try
302    {
303      $BatchDownloader = new BatchDownloader($_POST['set_id']);
304      $BatchDownloader->removeImages(array($_POST['toggle_id']));
305      echo 'ok';
306    }
307    catch (Exception $e)
308    {
309      echo 'error';
310    }
311  }
312  else
313  {
314    echo 'error';
315  }
316
317  exit(0);
318}
Note: See TracBrowser for help on using the repository browser.