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

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

rewrite all urls to be compatible with "question_mark_in_urls"

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