]> CyberLeo.Net >> Repos - Github/sugarcrm.git/blob - download.php
Release 6.4.0
[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-2011 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(empty($_REQUEST['id']) || empty($_REQUEST['type']) || !isset($_SESSION['authenticated_user_id'])) {
42         die("Not a Valid Entry Point");
43 }
44 else {
45     $file_type=''; // bug 45896
46     require_once("data/BeanFactory.php");
47     ini_set('zlib.output_compression','Off');//bug 27089, if use gzip here, the Content-Length in hearder may be incorrect.
48     // cn: bug 8753: current_user's preferred export charset not being honored
49     $GLOBALS['current_user']->retrieve($_SESSION['authenticated_user_id']);
50     $GLOBALS['current_language'] = $_SESSION['authenticated_user_language'];
51     $app_strings = return_application_language($GLOBALS['current_language']);
52     $mod_strings = return_module_language($GLOBALS['current_language'], 'ACL');
53         $file_type = strtolower($_REQUEST['type']);
54     if(!isset($_REQUEST['isTempFile'])) {
55             //Custom modules may have capilizations anywhere in thier names. We should check the passed in format first.
56                 require('include/modules.php');
57                 $module = $db->quote($_REQUEST['type']);
58                 if(empty($beanList[$module])) {
59                         //start guessing at a module name
60                         $module = ucfirst($file_type);
61                 if(empty($beanList[$module])) {
62                         die($app_strings['ERROR_TYPE_NOT_VALID']);
63                 }
64                 }
65         $bean_name = $beanList[$module];
66             if(!file_exists('modules/' . $module . '/' . $bean_name . '.php')) {
67                  die($app_strings['ERROR_TYPE_NOT_VALID']);
68             }
69
70             $focus = BeanFactory::newBean($module);
71         if(!$focus->ACLAccess('view')){
72             die($mod_strings['LBL_NO_ACCESS']);
73             } // if
74         $focus->retrieve($_REQUEST['id']);
75         // Pull up the document revision, if it's of type Document
76         if ( isset($focus->object_name) && $focus->object_name == 'Document' ) {
77             // It's a document, get the revision that really stores this file
78             $focusRevision = new DocumentRevision();
79             $focusRevision->retrieve($_REQUEST['id']);
80
81             if ( empty($focusRevision->id) ) {
82                 // This wasn't a document revision id, it's probably actually a document id,
83                 // we need to grab the latest revision and use that
84                 $focusRevision->retrieve($focus->document_revision_id);
85
86                 if ( !empty($focusRevision->id) ) {
87                     $_REQUEST['id'] = $focusRevision->id;
88                 }
89             }
90         }
91
92         // See if it is a remote file, if so, send them that direction
93         if ( isset($focus->doc_url) && !empty($focus->doc_url) ) {
94             header('Location: '.$focus->doc_url);
95             sugar_die();
96         }
97
98         if ( isset($focusRevision) && isset($focusRevision->doc_url) && !empty($focusRevision->doc_url) ) {
99             header('Location: '.$focusRevision->doc_url);
100             sugar_die();
101         }
102
103     } // if
104
105     if(isset($_REQUEST['ieId']) && isset($_REQUEST['isTempFile'])) {
106                 $local_location = sugar_cached("modules/Emails/{$_REQUEST['ieId']}/attachments/{$_REQUEST['id']}");
107     } elseif(isset($_REQUEST['isTempFile']) && $file_type == "import") {
108         $local_location = "upload://import/{$_REQUEST['tempName']}";
109     } else {
110                 $local_location = "upload://{$_REQUEST['id']}";
111     }
112
113         if(isset($_REQUEST['isTempFile']) && ($_REQUEST['type']=="SugarFieldImage")) {
114             $local_location =  "upload://{$_REQUEST['id']}";
115     }
116
117         if(!file_exists( $local_location ) || strpos($local_location, "..")) {
118                 die($app_strings['ERR_INVALID_FILE_REFERENCE']);
119         } else {
120                 $doQuery = true;
121
122                 if($file_type == 'documents') {
123                         // cn: bug 9674 document_revisions table has no 'name' column.
124                         $query = "SELECT filename name FROM document_revisions INNER JOIN documents ON documents.id = document_revisions.document_id ";
125                         $query .= "WHERE document_revisions.id = '".$db->quote($_REQUEST['id'])."' ";
126                 } elseif($file_type == 'kbdocuments') {
127                                 $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 ";
128             $query .= "WHERE document_revisions.id = '" . $db->quote($_REQUEST['id']) ."'";
129                 }  elseif($file_type == 'notes') {
130                         $query = "SELECT filename name FROM notes ";
131                         $query .= "WHERE notes.id = '" . $db->quote($_REQUEST['id']) ."'";
132                 } elseif( !isset($_REQUEST['isTempFile']) && !isset($_REQUEST['tempName'] ) && isset($_REQUEST['type']) && $file_type!='temp' ){ //make sure not email temp file.
133                         $query = "SELECT filename name FROM ". $file_type ." ";
134                         $query .= "WHERE ". $file_type .".id= '".$db->quote($_REQUEST['id'])."'";
135                 }elseif( $file_type == 'temp'){
136                         $doQuery = false;
137                 }
138
139                 if($doQuery && isset($query)) {
140             $rs = $GLOBALS['db']->query($query);
141                         $row = $GLOBALS['db']->fetchByAssoc($rs);
142
143                         if(empty($row)){
144                                 die($app_strings['ERROR_NO_RECORD']);
145                         }
146                         $name = $row['name'];
147                         $download_location = "upload://{$_REQUEST['id']}";
148                 } else if(isset(  $_REQUEST['tempName'] ) && isset($_REQUEST['isTempFile']) ){
149                         // downloading a temp file (email 2.0)
150                         $download_location = $local_location;
151                         $name = isset($_REQUEST['tempName'])?$_REQUEST['tempName']:'';
152                 } else if(isset($_REQUEST['isTempFile']) && ($_REQUEST['type']=="SugarFieldImage")) {
153                         $download_location = $local_location;
154                         $name = isset($_REQUEST['tempName'])?$_REQUEST['tempName']:'';
155                 }
156
157                 if(isset($_SERVER['HTTP_USER_AGENT']) && preg_match("/MSIE/", $_SERVER['HTTP_USER_AGENT']))
158                 {
159                         $name = urlencode($name);
160                         $name = str_replace("+", "_", $name);
161                 }
162
163                 header("Pragma: public");
164                 header("Cache-Control: maxage=1, post-check=0, pre-check=0");
165                 if(isset($_REQUEST['isTempFile']) && ($_REQUEST['type']=="SugarFieldImage")) {
166                     $mime = getimagesize($download_location);
167                     if(!empty($mime)) {
168                             header("Content-Type: {$mime['mime']}");
169                     } else {
170                         header("Content-Type: image/png");
171                     }
172                 } else {
173             header("Content-Type: application/force-download");
174             header("Content-type: application/octet-stream");
175             header("Content-Disposition: attachment; filename=\"".$name."\";");
176                 }
177                 // disable content type sniffing in MSIE
178                 header("X-Content-Type-Options: nosniff");
179                 header("Content-Length: " . filesize($local_location));
180                 header("Expires: 0");
181                 set_time_limit(0);
182
183                 @ob_end_clean();
184                 ob_start();
185
186                 readfile($download_location);
187                 @ob_flush();
188         }
189 }
190 ?>