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

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

clean definition of BATCH_DOWNLOAD_PUBLIC
remove warning on admin when a downloaded category was deleted

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