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

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

typo in language files, fix for ghtumb, fix for question_mark_in_urk=false

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