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

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

move remove_image.php code to a trigger on init

File size: 8.3 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']) && !isset($page['chronology_field'])) 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'], $set['size']);
90     
91      if ($BatchDownloader->getParam('nb_images') != 0)
92      {
93        // if we plan only one zip with less elements than 'max_elements', the download starts immediately
94        if (
95          $BatchDownloader->getParam('nb_images') <= $conf['batch_download']['max_elements']
96          and $BatchDownloader->getParam('size') == 'original'
97          and $BatchDownloader->getEstimatedArchiveNumber() == 1
98        )
99        {
100          $BatchDownloader->createNextArchive(true); // make sure we have only one zip, even if 'max_size' is exceeded
101         
102          $u_download = get_root_url().BATCH_DOWNLOAD_PATH . 'download.php?set_id='.$BatchDownloader->getParam('id').'&amp;zip=1';
103         
104          $null = null;
105          $template->block_footer_script(null, 'setTimeout("document.location.href = \''.$u_download.'\';", 1000);', $null, $null);
106         
107          $page['infos'][] = sprintf(l10n('The archive is downloading, if the download doesn\'t start automatically please <a href="%s">click here</a>'), $u_download);
108        }
109        // otherwise we go to summary page
110        else
111        {
112          redirect(add_url_params(BATCH_DOWNLOAD_PUBLIC . 'init_zip', array('set_id'=>$BatchDownloader->getParam('id'))));
113        }
114      }
115      else
116      {
117        $BatchDownloader->delete();
118        unset($BatchDownloader);
119       
120        $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']));
121      }
122    }
123  }
124 
125  if ($page['section'] == 'collections')
126  {
127    $url = $_SERVER['REQUEST_URI'];
128  }
129  else
130  {
131    $url = duplicate_index_url(array(), array('action'));
132  }
133 
134  $url = add_url_params($url, array('action'=>'advdown_set', 'down_size'=>''));
135 
136  // toolbar button
137  $template->set_filename('batchdwn_button', realpath(BATCH_DOWNLOAD_PATH.'template/download_button.tpl'));
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  $button = $template->parse('batchdwn_button', true);
165  $template->add_index_button($button, 50);
166  $template->concat('COLLECTION_ACTIONS', $button);
167}
168
169
170/* menu block */
171function batch_download_add_menublock($menu_ref_arr)
172{
173  global $user;
174 
175  $menu = &$menu_ref_arr[0];
176  if ($menu->get_id() != 'menubar') return;
177 
178  if (check_download_access() === false) return;
179 
180  $query = '
181SELECT id
182  FROM '.BATCH_DOWNLOAD_TSETS.'
183  WHERE
184    user_id = '.$user['id'].'
185    AND status != "done"
186  LIMIT 1
187;';
188  $result = pwg_query($query);
189  if (!pwg_db_num_rows($result)) return;
190 
191  $menu->register_block(new RegisteredBlock('mbBatchDownloader', l10n('Batch Downloader'), 'BatchDownloader'));
192}
193
194function batch_download_applymenu($menu_ref_arr)
195{
196  global $template, $conf, $user;
197 
198  $menu = &$menu_ref_arr[0];
199  $block = $menu->get_block('mbBatchDownloader');
200 
201  if ($block != null)
202  {
203    $query = '
204SELECT id
205  FROM '.BATCH_DOWNLOAD_TSETS.'
206  WHERE
207    user_id = '.$user['id'].'
208    AND status != "done"
209;';
210    $sets = array_from_query($query, 'id');
211   
212    $data = array();
213    foreach ($sets as $set_id)
214    {
215      $BatchDownloader = new BatchDownloader($set_id);
216      $set = $BatchDownloader->getSetInfo();
217     
218      array_push($data, array(
219        'URL' => add_url_params(BATCH_DOWNLOAD_PUBLIC . 'init_zip', array('set_id'=>$BatchDownloader->getParam('id'))),
220        'TITLE' => str_replace('"', "'", strip_tags($set['COMMENT'])),
221        'NAME' => $set['sNAME'],
222        'COUNT' => $set['NB_IMAGES'],
223        ));
224    }
225   
226    $template->set_template_dir(BATCH_DOWNLOAD_PATH . 'template/');
227    $block->set_title(l10n('Downloads'));
228    $block->template = 'menublock_batch_down.tpl';
229    $block->data = $data;
230  }
231}
232
233
234/* archives and databse cleanup */
235function batch_download_clean()
236{
237  global $conf;
238 
239  // we only search for old downloads every hour, nevermind which user is connected
240  if ($conf['batch_download']['last_clean'] > time() - 3600) return;
241 
242  $conf['batch_download']['last_clean'] = time();
243  conf_update_param('batch_download', serialize($conf['batch_download']));
244 
245  // set old sets as done and clean images table
246  $query = '
247DELETE i
248  FROM '.BATCH_DOWNLOAD_TIMAGES.' AS i
249    INNER JOIN '.BATCH_DOWNLOAD_TSETS.' AS s
250    ON i.set_id = s.id
251  WHERE
252    status != "done" AND
253    date_creation < DATE_SUB(NOW(), INTERVAL '.$conf['batch_download']['archive_timeout'].' HOUR)
254;';
255  pwg_query($query);
256 
257  $query = '
258UPDATE '.BATCH_DOWNLOAD_TSETS.'
259  SET status = "done"
260  WHERE
261    status != "done" AND
262    date_creation < DATE_SUB(NOW(), INTERVAL '.$conf['batch_download']['archive_timeout'].' HOUR)
263;';
264  pwg_query($query);
265 
266  // remove old archives
267  $zips = glob(BATCH_DOWNLOAD_LOCAL . 'u-*/*.zip');
268 
269  if (is_array($zips))
270  {
271    foreach ($zips as $zip)
272    {
273      if (filemtime($zip) < time()-$conf['batch_download']['archive_timeout']*3600)
274      {
275        unlink($zip);
276      }
277    }
278  }
279}
280
281/* ajax request to remove an image */
282function batch_downloader_remove_image()
283{
284  if (!isset($_POST['action']) || $_POST['action']!='bd_remove_image') return;
285 
286  check_status(ACCESS_CLASSIC);
287
288  if (isset($_POST['set_id']) and isset($_POST['toggle_id']))
289  {
290    try
291    {
292      $BatchDownloader = new BatchDownloader($_POST['set_id']);
293      $BatchDownloader->removeImages(array($_POST['toggle_id']));
294      echo 'ok';
295    }
296    catch (Exception $e)
297    {
298      echo 'error';
299    }
300  }
301  else
302  {
303    echo 'error';
304  }
305 
306  exit(0);
307}
308
309?>
Note: See TracBrowser for help on using the repository browser.