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

Last change on this file since 18780 was 17656, checked in by mistic100, 12 years ago

consolidate upgrade process, always use original filenames

File size: 6.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    $page['section'] = 'download';
16    $page['title'] = '<a href="'.get_absolute_root_url().'">'.l10n('Home').'</a>'.$conf['level_separator'].l10n('Batch Downloader').$conf['level_separator'];
17   
18    switch (@$tokens[1])
19    {
20      case 'init_zip':
21        $page['sub_section'] = 'init_zip';
22        $page['title'].= l10n('Generate ZIP');
23        break;
24      case 'view':
25        $page['sub_section'] = 'view';
26        $page['title'].= l10n('Edit the set');
27        break;
28      default:
29        redirect('index.php');
30    }
31  }
32}
33
34/* download section */
35function batch_download_page() 
36{
37  global $page;
38
39  if (isset($page['section']) and $page['section'] == 'download')
40  {
41    include(BATCH_DOWNLOAD_PATH . '/include/download.inc.php');
42  }
43}
44
45
46/* add buttons on thumbnails list */
47function batch_download_index_button()
48{
49  global $page, $template, $user, $conf;
50 
51  if ( !count($page['items']) or !isset($page['section']) ) return;
52 
53  if (check_download_access() === false) return;
54 
55  // download the set
56  if ( isset($_GET['action']) and $_GET['action']=='advdown_set' )
57  {
58    $set = get_set_info_from_page();
59   
60    if ($set !== false)
61    {
62      $BatchDownloader = new BatchDownloader('new', $page['items'], $set['type'], $set['id']);
63      $BatchDownloader->getEstimatedArchiveNumber();
64     
65      // if we plan only one zip with less elements than 'max_elements', the download starts immediately
66      if (
67        $BatchDownloader->getParam('nb_images') <= $conf['batch_download']['max_elements']
68        and $BatchDownloader->getParam('nb_zip') == 1
69      )
70      {
71        $BatchDownloader->createNextArchive(true); // make sure we have only one zip, even if 'max_size' is exceeded
72       
73        $u_download = get_root_url().BATCH_DOWNLOAD_PATH . 'download.php?set_id='.$BatchDownloader->getParam('id').'&amp;zip=1';
74       
75        $null = null;
76        $template->block_footer_script(null, 'setTimeout("document.location.href = \''.$u_download.'\';", 1000);', $null, $null);
77       
78        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));
79      }
80      // oterwise we go to summary page
81      else
82      {
83        redirect(add_url_params(BATCH_DOWNLOAD_PUBLIC . 'init_zip', array('set_id'=>$BatchDownloader->getParam('id'))));
84      }
85    }
86  }
87 
88  if ($page['section'] == 'collections')
89  {
90    $url = $_SERVER['REQUEST_URI'];
91  }
92  else
93  {
94    $url = duplicate_index_url(array(), array('action'));
95  }
96 
97  $url = add_url_params($url, array('action'=>'advdown_set'));
98 
99  // toolbar button
100  $button = '<script type="text/javascript">var batchdown_count = '.count($page['items']).'; var batchdown_string = "'.l10n('Confirm the download of %d pictures?').'";</script>
101    <li><a href="'. $url .'" title="'.l10n('Download all pictures of this selection').'" class="pwg-state-default pwg-button" rel="nofollow"
102    onClick="return confirm(batchdown_string.replace(\'%d\', batchdown_count));">
103                        <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('Batch Downloader').'</span>
104                </a></li>';
105  $template->concat('PLUGIN_INDEX_ACTIONS', $button);
106  $template->concat('COLLECTION_ACTIONS', $button);
107}
108
109
110/* menu block */
111function batch_download_add_menublock($menu_ref_arr)
112{
113  global $user;
114 
115  $menu = &$menu_ref_arr[0];
116  if ($menu->get_id() != 'menubar') return;
117 
118  if (check_download_access() === false) return;
119 
120  $query = '
121SELECT id
122  FROM '.BATCH_DOWNLOAD_TSETS.'
123  WHERE
124    user_id = '.$user['id'].'
125    AND status != "done"
126  LIMIT 1
127;';
128  $result = pwg_query($query);
129  if (!pwg_db_num_rows($result)) return;
130 
131  $menu->register_block(new RegisteredBlock('mbBatchDownloader', l10n('Batch Downloader'), 'BatchDownloader'));
132}
133
134function batch_download_applymenu($menu_ref_arr)
135{
136  global $template, $conf, $user;
137 
138  $menu = &$menu_ref_arr[0];
139  $block = $menu->get_block('mbBatchDownloader');
140 
141  if ($block != null)
142  {
143    $query = '
144SELECT id
145  FROM '.BATCH_DOWNLOAD_TSETS.'
146  WHERE
147    user_id = '.$user['id'].'
148    AND status != "done"
149;';
150    $sets = array_from_query($query, 'id');
151   
152    $data = array();
153    foreach ($sets as $set_id)
154    {
155      $BatchDownloader = new BatchDownloader($set_id);
156      $set = $BatchDownloader->getSetInfo();
157     
158      array_push($data, array(
159        'URL' => add_url_params(BATCH_DOWNLOAD_PUBLIC . 'init_zip', array('set_id'=>$BatchDownloader->getParam('id'))),
160        'TITLE' => str_replace('"', "'", strip_tags($set['COMMENT'])),
161        'NAME' => $set['sNAME'],
162        'COUNT' => $set['NB_IMAGES'],
163        ));
164    }
165   
166    $template->set_template_dir(BATCH_DOWNLOAD_PATH . 'template/');
167    $block->set_title(l10n('Downloads'));
168    $block->template = 'menublock_batch_down.tpl';
169    $block->data = $data;
170  }
171}
172
173
174/* archives and databse cleanup */
175function batch_download_clean()
176{
177  global $conf;
178 
179  // we only search for old downloads every hour, nevermind which user is connected
180  if ($conf['batch_download']['last_clean'] > time() - 3600) return;
181 
182  $conf['batch_download']['last_clean'] = time();
183  conf_update_param('batch_download', serialize($conf['batch_download']));
184 
185  // set old sets as done and clean images table
186  $query = '
187DELETE i
188  FROM '.BATCH_DOWNLOAD_TIMAGES.' AS i
189    INNER JOIN '.BATCH_DOWNLOAD_TSETS.' AS s
190    ON i.set_id = s.id
191  WHERE
192    status != "done" AND
193    date_creation < DATE_SUB(NOW(), INTERVAL '.$conf['batch_download']['archive_timeout'].' HOUR)
194;';
195  pwg_query($query);
196 
197  $query = '
198UPDATE '.BATCH_DOWNLOAD_TSETS.'
199  SET status = "done"
200  WHERE
201    status != "done" AND
202    date_creation < DATE_SUB(NOW(), INTERVAL '.$conf['batch_download']['archive_timeout'].' HOUR)
203;';
204  pwg_query($query);
205 
206  // remove old archives
207  $zips = glob(BATCH_DOWNLOAD_LOCAL . 'u-*/*.zip');
208 
209  if (is_array($zips))
210  {
211    foreach ($zips as $zip)
212    {
213      if (filemtime($zip) < time()-$conf['batch_download']['archive_timeout']*3600)
214      {
215        unlink($zip);
216      }
217    }
218  }
219}
220
221?>
Note: See TracBrowser for help on using the repository browser.