Web   ·   Wiki   ·   Activities   ·   Blog   ·   Lists   ·   Chat   ·   Meeting   ·   Bugs   ·   Git   ·   Translate   ·   Archive   ·   People   ·   Donate
summaryrefslogtreecommitdiffstats
path: root/site/app/controllers/downloads_controller.php
blob: fc182db54132068d8c5a56e1bead8cbf64458d6a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
<?php
/* ***** BEGIN LICENSE BLOCK *****
 * Version: MPL 1.1/GPL 2.0/LGPL 2.1
 *
 * The contents of this file are subject to the Mozilla Public License Version
 * 1.1 (the "License"); you may not use this file except in compliance with
 * the License. You may obtain a copy of the License at
 * http://www.mozilla.org/MPL/
 *
 * Software distributed under the License is distributed on an "AS IS" basis,
 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
 * for the specific language governing rights and limitations under the
 * License.
 *
 * The Original Code is addons.mozilla.org site.
 *
 * The Initial Developer of the Original Code is
 * The Mozilla Foundation.
 * Portions created by the Initial Developer are Copyright (C) 2006
 * the Initial Developer. All Rights Reserved.
 *
 * Contributor(s):
 *   Andrei Hajdukewycz <sancus@off.net> (Original Author)
 *   Justin Scott <fligtar@gmail.com>
 *   Frederic Wenzel <fwenzel@mozilla.com>
 *
 * Alternatively, the contents of this file may be used under the terms of
 * either the GNU General Public License Version 2 or later (the "GPL"), or
 * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
 * in which case the provisions of the GPL or the LGPL are applicable instead
 * of those above. If you wish to allow use of your version of this file only
 * under the terms of either the GPL or the LGPL, and not to allow others to
 * use your version of this file under the terms of the MPL, indicate your
 * decision by deleting the provisions above and replace them with the notice
 * and other provisions required by the GPL or the LGPL. If you do not delete
 * the provisions above, a recipient may use your version of this file under
 * the terms of any one of the MPL, the GPL or the LGPL.
 *
 * ***** END LICENSE BLOCK ***** */

class DownloadsController extends AppController
{
    var $name = 'Downloads';
    var $beforeFilter = array('getNamedArgs', '_checkSandbox');
    var $uses = array('Addon', 'Download', 'File', 'Translation');
    var $components = array('Amo', 'Session');
    var $namedArgs = true;

    var $securityLevel = 'low';

    function beforeFilter() {
        // Disable ACLs because this controller is entirely public.
        $this->SimpleAuth->enabled = false;
        $this->SimpleAcl->enabled = false;
    }
    
    function file($id = null, $type = 'xpi') {
        
        if (empty($this->namedArgs['update']))
            $this->namedArgs['update'] = null;
        
        $this->Amo->clean($id);
        $this->layout=null;
        if (!$id || !is_numeric($id)) {
            $this->flash(sprintf(_('error_missing_argument'), 'file_id'), '/', 3);
            return;
        }
        
        $file_data = $this->File->FindbyId($id);
        
        if (!empty($file_data)) {
            
            $this->Addon->unbindFully();
            $addon_data = $this->Addon->FindbyId($file_data['Version']['addon_id']);
            
            $file_loc = REPO_PATH . '/' . $file_data['Version']['addon_id'] . '/' . $file_data['File']['filename'];
                   
            // If add-on is in sandbox, make sure sandbox is enabled. If disabled, make sure admin or author.
            if (($addon_data['Addon']['status'] == STATUS_SANDBOX ||
                 $addon_data['Addon']['status'] == STATUS_NOMINATED ||
                 $file_data['File']['status'] == STATUS_PENDING)
                 && !$this->Session->check('User')) {
                
                $target_url = str_replace(LANG . '/' . APP_SHORTNAME . '/','',$this->params['url']['url']);
                $this->redirect('/users/login?to=' . urlencode($target_url) . "&m=1");
                return;
            } elseif ($addon_data['Addon']['status'] == STATUS_DISABLED &&
                !$this->Amo->checkOwnership($addon_data['Addon']['id'])) {
                
                $this->flash(_('downloads_disable_warning'), '/', 3);
                return;
            }
        } else {
            $this->flash(_('error_addon_notfound'), '/', 3);
            return;
        }
        
        if (file_exists($file_loc))
            $this->set('fileLoc', $file_loc);    
        else {
            $this->flash(_('error_addon_notfound'), '/', 3);
            return;
        }
        
        // force local delivery for non-browser apps
        global $browser_apps;
        if (!in_array(APP_ID, $browser_apps) || $type == 'attachment') {
            $forceLocal = true;
            $this->set('attachment', true);
        } else {
            $forceLocal = false;
            $this->set('attachment', false);
        }
        
        $this->set('fileName', $file_data['File']['filename']);
        
        if (!DEV && !$forceLocal && $addon_data['Addon']['status'] == STATUS_PUBLIC
            && $file_data['File']['status'] == STATUS_PUBLIC
            && (strtotime($file_data['File']['datestatuschanged']) <= strtotime('-'.MIRROR_DELAY.' minutes'))) {
            // serve file from releases mirror only if we are not in dev mode
            // and if the file is public and sufficient time has passed to allow
            // its propagation to the mirror system
            $this->forceCache();
            $this->redirect(FILES_HOST . '/' . $addon_data['Addon']['id'] . '/' . $file_data['File']['filename']);
            return;    
        } else {
            // serve file locally
            $this->disableCache();
            $this->render('file');
        }
    }
    
    /**
     * Retrieves public file for latest version of an add-on, regardless of compatibility
     * Example URL: /downloads/latest/1865/type:attachment/platform:5/
     * @param int $addon_id the add-on id
     */
    function latest($addon_id) {
        $platform_id = (!empty($this->namedArgs['platform']) && is_numeric($this->namedArgs['platform'])) ? $this->namedArgs['platform'] : null;
        
        $type = !empty($this->namedArgs['type']) ? $this->namedArgs['type'] : 'xpi';
        
        // Get the id of the latest public file
        $file_id = $this->File->getLatestFileByAddonId($addon_id, $platform_id);
        $file_data = $this->File->findById($file_id);
        
        if (!empty($file_id)) {
            // Use normal download method if file is found
            $this->redirect("/downloads/file/{$file_id}/{$type}/{$file_data['File']['filename']}");
        }
        else {
            // File wasn't found
            $this->flash(_('error_addon_notfound'), '/', 3);
        }
    }
}   

?>