]> CyberLeo.Net >> Repos - Github/sugarcrm.git/blob - download.php
Release 6.5.16
[Github/sugarcrm.git] / download.php
1 <?php
2 if(!defined('sugarEntry') || !sugarEntry) die('Not A Valid Entry Point');
3 /*********************************************************************************
4  * SugarCRM Community Edition is a customer relationship management program developed by
5  * SugarCRM, Inc. Copyright (C) 2004-2013 SugarCRM Inc.
6  * 
7  * This program is free software; you can redistribute it and/or modify it under
8  * the terms of the GNU Affero General Public License version 3 as published by the
9  * Free Software Foundation with the addition of the following permission added
10  * to Section 15 as permitted in Section 7(a): FOR ANY PART OF THE COVERED WORK
11  * IN WHICH THE COPYRIGHT IS OWNED BY SUGARCRM, SUGARCRM DISCLAIMS THE WARRANTY
12  * OF NON INFRINGEMENT OF THIRD PARTY RIGHTS.
13  * 
14  * This program is distributed in the hope that it will be useful, but WITHOUT
15  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
16  * FOR A PARTICULAR PURPOSE.  See the GNU Affero General Public License for more
17  * details.
18  * 
19  * You should have received a copy of the GNU Affero General Public License along with
20  * this program; if not, see http://www.gnu.org/licenses or write to the Free
21  * Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
22  * 02110-1301 USA.
23  * 
24  * You can contact SugarCRM, Inc. headquarters at 10050 North Wolfe Road,
25  * SW2-130, Cupertino, CA 95014, USA. or at email address contact@sugarcrm.com.
26  * 
27  * The interactive user interfaces in modified source and object code versions
28  * of this program must display Appropriate Legal Notices, as required under
29  * Section 5 of the GNU Affero General Public License version 3.
30  * 
31  * In accordance with Section 7(b) of the GNU Affero General Public License version 3,
32  * these Appropriate Legal Notices must retain the display of the "Powered by
33  * SugarCRM" logo. If the display of the logo is not reasonably feasible for
34  * technical reasons, the Appropriate Legal Notices must display the words
35  * "Powered by SugarCRM".
36  ********************************************************************************/
37
38
39 global $db;
40
41 if((!isset($_REQUEST['isProfile']) && empty($_REQUEST['id'])) || empty($_REQUEST['type']) || !isset($_SESSION['authenticated_user_id'])) {
42         die("Not a Valid Entry Point");
43 }
44 else {
45     require_once("data/BeanFactory.php");
46     $file_type=''; // bug 45896
47     require_once("data/BeanFactory.php");
48     ini_set('zlib.output_compression','Off');//bug 27089, if use gzip here, the Content-Length in header may be incorrect.
49     // cn: bug 8753: current_user's preferred export charset not being honored
50     $GLOBALS['current_user']->retrieve($_SESSION['authenticated_user_id']);
51     $GLOBALS['current_language'] = $_SESSION['authenticated_user_language'];
52     $app_strings = return_application_language($GLOBALS['current_language']);
53     $mod_strings = return_module_language($GLOBALS['current_language'], 'ACL');
54         $file_type = strtolower($_REQUEST['type']);
55     if(!isset($_REQUEST['isTempFile'])) {
56             //Custom modules may have capitalizations anywhere in their names. We should check the passed in format first.
57                 require('include/modules.php');
58                 $module = $db->quote($_REQUEST['type']);
59                 if(empty($beanList[$module])) {
60                         //start guessing at a module name
61                         $module = ucfirst($file_type);
62                 if(empty($beanList[$module])) {
63                         die($app_strings['ERROR_TYPE_NOT_VALID']);
64                 }
65                 }
66         $bean_name = $beanList[$module];
67             if(!file_exists('modules/' . $module . '/' . $bean_name . '.php')) {
68                  die($app_strings['ERROR_TYPE_NOT_VALID']);
69             }
70
71             $focus = BeanFactory::newBean($module);
72         if(!$focus->ACLAccess('view')){
73             die($mod_strings['LBL_NO_ACCESS']);
74             } // if
75         $focus->retrieve($_REQUEST['id']);
76         // Pull up the document revision, if it's of type Document
77         if ( isset($focus->object_name) && $focus->object_name == 'Document' ) {
78             // It's a document, get the revision that really stores this file
79             $focusRevision = new DocumentRevision();
80             $focusRevision->retrieve($_REQUEST['id']);
81
82             if ( empty($focusRevision->id) ) {
83                 // This wasn't a document revision id, it's probably actually a document id,
84                 // we need to grab the latest revision and use that
85                 $focusRevision->retrieve($focus->document_revision_id);
86
87                 if ( !empty($focusRevision->id) ) {
88                     $_REQUEST['id'] = $focusRevision->id;
89                 }
90             }
91         }
92
93         // See if it is a remote file, if so, send them that direction
94         if ( isset($focus->doc_url) && !empty($focus->doc_url) ) {
95             header('Location: '.$focus->doc_url);
96             sugar_die("Remote file detected, location header sent.");
97         }
98
99         if ( isset($focusRevision) && isset($focusRevision->doc_url) && !empty($focusRevision->doc_url) ) {
100             header('Location: '.$focusRevision->doc_url);
101             sugar_die("Remote file detected, location header sent.");
102         }
103
104     } // if
105
106     if(isset($_REQUEST['ieId']) && isset($_REQUEST['isTempFile'])) {
107                 $local_location = sugar_cached("modules/Emails/{$_REQUEST['ieId']}/attachments/{$_REQUEST['id']}");
108     } elseif(isset($_REQUEST['isTempFile']) && $file_type == "import") {
109         $local_location = "upload://import/{$_REQUEST['tempName']}";
110     } else {
111                 $local_location = "upload://{$_REQUEST['id']}";
112     }
113
114         if(isset($_REQUEST['isTempFile']) && ($_REQUEST['type']=="SugarFieldImage")) {
115             $local_location =  "upload://{$_REQUEST['id']}";
116     }
117     
118     if(isset($_REQUEST['isTempFile']) && ($_REQUEST['type']=="SugarFieldImage") && (isset($_REQUEST['isProfile'])) && empty($_REQUEST['id'])) {
119         $local_location = "include/images/default-profile.png";
120     }
121     
122         if(!file_exists( $local_location ) || strpos($local_location, "..")) {
123                 die($app_strings['ERR_INVALID_FILE_REFERENCE']);
124         } else {
125                 $doQuery = true;
126
127                 if($file_type == 'documents') {
128                         // cn: bug 9674 document_revisions table has no 'name' column.
129                         $query = "SELECT filename name FROM document_revisions INNER JOIN documents ON documents.id = document_revisions.document_id ";
130                         $query .= "WHERE document_revisions.id = '".$db->quote($_REQUEST['id'])."' ";
131                 } elseif($file_type == 'kbdocuments') {
132                                 $query="SELECT document_revisions.filename name FROM document_revisions INNER JOIN kbdocument_revisions ON document_revisions.id = kbdocument_revisions.document_revision_id INNER JOIN kbdocuments ON kbdocument_revisions.kbdocument_id = kbdocuments.id ";
133             $query .= "WHERE document_revisions.id = '" . $db->quote($_REQUEST['id']) ."'";
134                 }  elseif($file_type == 'notes') {
135             $query = "SELECT filename name, file_mime_type FROM notes ";
136                         $query .= "WHERE notes.id = '" . $db->quote($_REQUEST['id']) ."'";
137                 } elseif( !isset($_REQUEST['isTempFile']) && !isset($_REQUEST['tempName'] ) && isset($_REQUEST['type']) && $file_type!='temp' ){ //make sure not email temp file.
138                         $query = "SELECT filename name FROM ". $file_type ." ";
139                         $query .= "WHERE ". $file_type .".id= '".$db->quote($_REQUEST['id'])."'";
140                 }elseif( $file_type == 'temp'){
141                         $doQuery = false;
142                 }
143
144         $mime_type = 'application/octet-stream';
145                 if($doQuery && isset($query)) {
146             $rs = $GLOBALS['db']->query($query);
147                         $row = $GLOBALS['db']->fetchByAssoc($rs);
148
149                         if(empty($row)){
150                                 die($app_strings['ERROR_NO_RECORD']);
151                         }
152                         $name = $row['name'];
153
154             // expose original mime type only for images, otherwise the content of arbitrary type
155             // may be interpreted/executed by browser
156             if (isset($row['file_mime_type']) && strpos($row['file_mime_type'], 'image/') === 0) {
157                 $mime_type = $row['file_mime_type'];
158             }
159                         $download_location = "upload://{$_REQUEST['id']}";
160                 } else if(isset(  $_REQUEST['tempName'] ) && isset($_REQUEST['isTempFile']) ){
161                         // downloading a temp file (email 2.0)
162                         $download_location = $local_location;
163                         $name = isset($_REQUEST['tempName'])?$_REQUEST['tempName']:'';
164                 } else if(isset($_REQUEST['isTempFile']) && ($_REQUEST['type']=="SugarFieldImage")) {
165                         $download_location = $local_location;
166                         $name = isset($_REQUEST['tempName'])?$_REQUEST['tempName']:'';
167                 }
168
169                 if(isset($_SERVER['HTTP_USER_AGENT']) && preg_match("/MSIE/", $_SERVER['HTTP_USER_AGENT']))
170                 {
171                         $name = urlencode($name);
172                         $name = str_replace("+", "_", $name);
173                 }
174
175                 header("Pragma: public");
176                 header("Cache-Control: maxage=1, post-check=0, pre-check=0");
177                 if(isset($_REQUEST['isTempFile']) && ($_REQUEST['type']=="SugarFieldImage")) {
178                         $mime = getimagesize($download_location);
179                         if(!empty($mime)) {
180                             header("Content-Type: {$mime['mime']}");
181                     } else {
182                         header("Content-Type: image/png");
183                     }
184                 } else {
185             header('Content-type: ' . $mime_type);
186                header("Content-Disposition: attachment; filename=\"".$name."\";");
187             
188                 }
189                 // disable content type sniffing in MSIE
190                 header("X-Content-Type-Options: nosniff");
191                 header("Content-Length: " . filesize($local_location));
192                 header('Expires: ' . gmdate('D, d M Y H:i:s \G\M\T', time() + 2592000));
193                 set_time_limit(0);
194
195         // When output_buffering = On, ob_get_level() may return 1 even if ob_end_clean() returns false 
196         // This happens on some QA stacks. See Bug#64860
197         while (ob_get_level() && @ob_end_clean());
198
199                 readfile($download_location);
200         }
201 }