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

Revision 21422, 7.1 KB checked in by mistic100, 6 years ago (diff)

add option too allow/disallow download of categories/collections/specials

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      $BatchDownloader->getEstimatedArchiveNumber();
91     
92      // if we plan only one zip with less elements than 'max_elements', the download starts immediately
93      if (
94        $BatchDownloader->getParam('nb_images') <= $conf['batch_download']['max_elements']
95        and $BatchDownloader->getParam('nb_zip') == 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').'&amp;zip=1';
101       
102        $null = null;
103        $template->block_footer_script(null, 'setTimeout("document.location.href = \''.$u_download.'\';", 1000);', $null, $null);
104       
105        array_push($page['infos'], sprintf(l10n('The archive is downloading, if the download doesn\'t start automatically please <a href="%s">click here</a>'), $u_download));
106      }
107      // oterwise 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  }
114 
115  if ($page['section'] == 'collections')
116  {
117    $url = $_SERVER['REQUEST_URI'];
118  }
119  else
120  {
121    $url = duplicate_index_url(array(), array('action'));
122  }
123 
124  $url = add_url_params($url, array('action'=>'advdown_set'));
125 
126  // toolbar button
127  $button = '<script type="text/javascript">var batchdown_count = '.count($page['items']).'; var batchdown_string = "'.l10n('Confirm the download of %d pictures?').'";</script>
128    <li><a href="'. $url .'" title="'.l10n('Download all pictures of this selection').'" class="pwg-state-default pwg-button" rel="nofollow"
129    onClick="return confirm(batchdown_string.replace(\'%d\', batchdown_count));">
130                        <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>
131                </a></li>';
132  $template->concat('PLUGIN_INDEX_ACTIONS', $button);
133  $template->concat('COLLECTION_ACTIONS', $button);
134}
135
136
137/* menu block */
138function batch_download_add_menublock($menu_ref_arr)
139{
140  global $user;
141 
142  $menu = &$menu_ref_arr[0];
143  if ($menu->get_id() != 'menubar') return;
144 
145  if (check_download_access() === false) return;
146 
147  $query = '
148SELECT id
149  FROM '.BATCH_DOWNLOAD_TSETS.'
150  WHERE
151    user_id = '.$user['id'].'
152    AND status != "done"
153  LIMIT 1
154;';
155  $result = pwg_query($query);
156  if (!pwg_db_num_rows($result)) return;
157 
158  $menu->register_block(new RegisteredBlock('mbBatchDownloader', l10n('Batch Downloader'), 'BatchDownloader'));
159}
160
161function batch_download_applymenu($menu_ref_arr)
162{
163  global $template, $conf, $user;
164 
165  $menu = &$menu_ref_arr[0];
166  $block = $menu->get_block('mbBatchDownloader');
167 
168  if ($block != null)
169  {
170    $query = '
171SELECT id
172  FROM '.BATCH_DOWNLOAD_TSETS.'
173  WHERE
174    user_id = '.$user['id'].'
175    AND status != "done"
176;';
177    $sets = array_from_query($query, 'id');
178   
179    $data = array();
180    foreach ($sets as $set_id)
181    {
182      $BatchDownloader = new BatchDownloader($set_id);
183      $set = $BatchDownloader->getSetInfo();
184     
185      array_push($data, array(
186        'URL' => add_url_params(BATCH_DOWNLOAD_PUBLIC . 'init_zip', array('set_id'=>$BatchDownloader->getParam('id'))),
187        'TITLE' => str_replace('"', "'", strip_tags($set['COMMENT'])),
188        'NAME' => $set['sNAME'],
189        'COUNT' => $set['NB_IMAGES'],
190        ));
191    }
192   
193    $template->set_template_dir(BATCH_DOWNLOAD_PATH . 'template/');
194    $block->set_title(l10n('Downloads'));
195    $block->template = 'menublock_batch_down.tpl';
196    $block->data = $data;
197  }
198}
199
200
201/* archives and databse cleanup */
202function batch_download_clean()
203{
204  global $conf;
205 
206  // we only search for old downloads every hour, nevermind which user is connected
207  if ($conf['batch_download']['last_clean'] > time() - 3600) return;
208 
209  $conf['batch_download']['last_clean'] = time();
210  conf_update_param('batch_download', serialize($conf['batch_download']));
211 
212  // set old sets as done and clean images table
213  $query = '
214DELETE i
215  FROM '.BATCH_DOWNLOAD_TIMAGES.' AS i
216    INNER JOIN '.BATCH_DOWNLOAD_TSETS.' AS s
217    ON i.set_id = s.id
218  WHERE
219    status != "done" AND
220    date_creation < DATE_SUB(NOW(), INTERVAL '.$conf['batch_download']['archive_timeout'].' HOUR)
221;';
222  pwg_query($query);
223 
224  $query = '
225UPDATE '.BATCH_DOWNLOAD_TSETS.'
226  SET status = "done"
227  WHERE
228    status != "done" AND
229    date_creation < DATE_SUB(NOW(), INTERVAL '.$conf['batch_download']['archive_timeout'].' HOUR)
230;';
231  pwg_query($query);
232 
233  // remove old archives
234  $zips = glob(BATCH_DOWNLOAD_LOCAL . 'u-*/*.zip');
235 
236  if (is_array($zips))
237  {
238    foreach ($zips as $zip)
239    {
240      if (filemtime($zip) < time()-$conf['batch_download']['archive_timeout']*3600)
241      {
242        unlink($zip);
243      }
244    }
245  }
246}
247
248?>
Note: See TracBrowser for help on using the repository browser.