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

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

add two triggers batchdownload_get_set_info and batchdownload_init_zip

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