]> CyberLeo.Net >> Repos - Github/sugarcrm.git/blob - include/SugarFields/Fields/File/SugarFieldFile.php
Release 6.2.3
[Github/sugarcrm.git] / include / SugarFields / Fields / File / SugarFieldFile.php
1 <?php
2 /*********************************************************************************
3  * SugarCRM Community Edition is a customer relationship management program developed by
4  * SugarCRM, Inc. Copyright (C) 2004-2011 SugarCRM Inc.
5  * 
6  * This program is free software; you can redistribute it and/or modify it under
7  * the terms of the GNU Affero General Public License version 3 as published by the
8  * Free Software Foundation with the addition of the following permission added
9  * to Section 15 as permitted in Section 7(a): FOR ANY PART OF THE COVERED WORK
10  * IN WHICH THE COPYRIGHT IS OWNED BY SUGARCRM, SUGARCRM DISCLAIMS THE WARRANTY
11  * OF NON INFRINGEMENT OF THIRD PARTY RIGHTS.
12  * 
13  * This program is distributed in the hope that it will be useful, but WITHOUT
14  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
15  * FOR A PARTICULAR PURPOSE.  See the GNU Affero General Public License for more
16  * details.
17  * 
18  * You should have received a copy of the GNU Affero General Public License along with
19  * this program; if not, see http://www.gnu.org/licenses or write to the Free
20  * Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
21  * 02110-1301 USA.
22  * 
23  * You can contact SugarCRM, Inc. headquarters at 10050 North Wolfe Road,
24  * SW2-130, Cupertino, CA 95014, USA. or at email address contact@sugarcrm.com.
25  * 
26  * The interactive user interfaces in modified source and object code versions
27  * of this program must display Appropriate Legal Notices, as required under
28  * Section 5 of the GNU Affero General Public License version 3.
29  * 
30  * In accordance with Section 7(b) of the GNU Affero General Public License version 3,
31  * these Appropriate Legal Notices must retain the display of the "Powered by
32  * SugarCRM" logo. If the display of the logo is not reasonably feasible for
33  * technical reasons, the Appropriate Legal Notices must display the words
34  * "Powered by SugarCRM".
35  ********************************************************************************/
36
37 require_once('include/SugarFields/Fields/Base/SugarFieldBase.php');
38
39 class SugarFieldFile extends SugarFieldBase {
40     private function fillInOptions(&$vardef,&$displayParams) {
41         if ( isset($vardef['allowEapm']) && $vardef['allowEapm'] == true ) {
42             if ( empty($vardef['docType']) ) {
43                 $vardef['docType'] = 'doc_type';
44             }
45             if ( empty($vardef['docId']) ) {
46                 $vardef['docId'] = 'doc_id';
47             }
48             if ( empty($vardef['docUrl']) ) {
49                 $vardef['docUrl'] = 'doc_url';
50             }
51             $displayParams['max_fileupload_size'] = $this->getMaxFileUploadSize();
52         } else {
53             $vardef['allowEapm'] = false;
54         }
55
56         // Override the default module
57         if ( isset($vardef['linkModuleOverride']) ) {
58             $vardef['linkModule'] = $vardef['linkModuleOverride'];
59         } else {
60             $vardef['linkModule'] = '{$module}';
61         }
62
63         // This is needed because these aren't always filled out in the edit/detailview defs
64         if ( !isset($vardef['fileId']) ) {
65             if ( isset($displayParams['id']) ) {
66                 $vardef['fileId'] = $displayParams['id'];
67             } else {
68                 $vardef['fileId'] = 'id';
69             }
70         }
71     }
72
73
74         function getDetailViewSmarty($parentFieldArray, $vardef, $displayParams, $tabindex) {
75         $this->fillInOptions($vardef,$displayParams);
76         return parent::getDetailViewSmarty($parentFieldArray, $vardef, $displayParams, $tabindex);
77     }
78     
79         function getEditViewSmarty($parentFieldArray, $vardef, $displayParams, $tabindex) {
80         $this->fillInOptions($vardef,$displayParams);
81         return parent::getEditViewSmarty($parentFieldArray, $vardef, $displayParams, $tabindex);
82     }
83     
84         public function save(&$bean, $params, $field, $vardef, $prefix = ''){
85         $fakeDisplayParams = array();
86         $this->fillInOptions($vardef,$fakeDisplayParams);
87
88                 require_once('include/upload_file.php');
89                 $upload_file = new UploadFile($prefix . $field . '_file');
90
91                 //remove file
92                 if (isset($_REQUEST['remove_file_' . $field]) && $params['remove_file_' . $field] == 1) {
93                         $upload_file->unlink_file($bean->$field);
94                         $bean->$field="";
95                 }
96                 
97                 $move=false;
98                 if (isset($_FILES[$prefix . $field . '_file']) && $upload_file->confirm_upload())
99                 {
100                 $bean->$field = $upload_file->get_stored_file_name();
101                 $bean->file_mime_type = $upload_file->mime_type;
102                         $bean->file_ext = $upload_file->file_ext;
103                         $move=true;
104                 }
105
106         if (!empty($params['isDuplicate']) && $params['isDuplicate'] == 'true' ) {
107             // This way of detecting duplicates is used in Notes
108             $old_id = $params['relate_id'];
109         }
110         if (!empty($params['duplicateSave']) && !empty($params['duplicateId']) ) {
111             // It's a duplicate
112             $old_id = $params['duplicateId'];
113         }
114
115         // Backwards compatibility for fields that still use customCode to handle the file uploads
116         if ( !$move && empty($old_id) && isset($_FILES['uploadfile']) ) {
117             $upload_file = new UploadFile('uploadfile');
118             if ( $upload_file->confirm_upload() ) {
119                 $bean->$field = $upload_file->get_stored_file_name();
120                 $bean->file_mime_type = $upload_file->mime_type;
121                 $bean->file_ext = $upload_file->file_ext;
122                 $move=true;
123                 
124             }
125         } else if ( !$move && !empty($old_id) && isset($_REQUEST['uploadfile']) && !isset($_REQUEST[$prefix . $field . '_file']) ) {
126             // I think we are duplicating a backwards compatibility module.
127             $upload_file = new UploadFile('uploadfile');
128         }
129
130
131         if (empty($bean->id)) { 
132             $bean->id = create_guid();
133             $bean->new_with_id = true;
134         }
135
136                 if ($move) {
137             $upload_file->final_move($bean->id);
138             $upload_file->upload_doc($bean, $bean->id, $params[$prefix . $vardef['docType']], $bean->$field, $upload_file->mime_type);
139         } else if ( ! empty($old_id) ) {
140             // It's a duplicate, I think
141
142             if ( empty($params[$prefix . $vardef['docUrl'] ]) ) {
143                 $upload_file->duplicate_file($old_id, $bean->id, $bean->$field);
144             } else {
145                 $docType = $vardef['docType'];
146                 $bean->$docType = $params[$prefix . $field . '_old_doctype'];
147             }
148                 } else if ( !empty($params[$prefix . $field . '_remoteName']) ) {
149             // We ain't moving, we might need to do some remote linking
150             $displayParams = array();
151             $this->fillInOptions($vardef,$displayParams);
152             
153             if ( isset($params[$prefix . $vardef['docId']])
154                  && ! empty($params[$prefix . $vardef['docId']])
155                  && isset($params[$prefix . $vardef['docType']]) 
156                  && ! empty($params[$prefix . $vardef['docType']])
157                 ) {
158                 $bean->$field = $params[$prefix . $field . '_remoteName'];
159                 
160                 require_once('include/utils/file_utils.php');
161                 $extension = get_file_extension($bean->$field);
162                 if(!empty($extension))
163                 {
164                         $bean->file_ext = $extension;
165                         $bean->file_mime_type = get_mime_content_type_from_filename($bean->$field);
166                 }
167             }
168         }
169         
170         if ( $vardef['allowEapm'] == true && empty($bean->$field) ) {
171             $GLOBALS['log']->info("The $field is empty, clearing out the lot");
172             // Looks like we are emptying this out
173             $clearFields = array('docId', 'docType', 'docUrl', 'docDirectUrl');
174             foreach ( $clearFields as $clearMe ) {
175                 if ( ! isset($vardef[$clearMe]) ) {
176                     continue;
177                 }
178                 $clearField = $vardef[$clearMe];
179                 $bean->$clearField = '';
180             }
181         }
182         }
183         
184         /**
185          * Takes the size from the php.ini and converts it to bytes
186          *
187          * @param string $size
188          * @return int
189          */
190         protected function convertIniSizeToBytes($size)
191         {               
192                 $ret_size = $size;
193                 switch (substr (strtolower($size), -1)){
194                 case 'k': 
195                     $ret_size = (int)$size * 1024;
196                     break;
197                 case 'm': 
198                     $ret_size = (int)$size * 1048576;
199                     break;
200                 case 'g': 
201                     $ret_size = (int)$size * 1073741824;
202                     break;
203                 }
204                 
205                 return $ret_size;  
206         }
207         
208         /**
209          * Converts the size into a more reader friendly format 
210          *
211          * @param int $size
212          * @return string
213          */
214         protected function getReaderFriendlySize($size) 
215     {
216                 if(($size / 1024) < 1024) {
217                         $size = number_format(($size / 1024), 2);
218                         $size .= ' kb';
219                 } 
220                 else if(($size / 1048576) < 1024) {
221                         $size = number_format(($size / 1048576), 2);
222                         $size .= ' mb';
223                 } 
224                 else if(($size / 1073741824) < 1024) {
225                         $size = number_format(($size / 1073741824), 2);
226                         $size .= ' gb';
227                 }
228                 
229                 return $size;
230         } 
231
232         /**
233          * Returns a string that represents the max file size that can be uploaded.
234          *
235          * @return string
236          */
237         protected function getMaxFileUploadSize()
238         {
239                 $max_upload_size = min($this->convertIniSizeToBytes(ini_get('post_max_size')), $this->convertIniSizeToBytes(ini_get('upload_max_filesize')));
240                 $max_upload_size = min($GLOBALS['sugar_config']['upload_maxsize'],$max_upload_size);
241                 
242                 return $this->getReaderFriendlySize($max_upload_size);
243         }
244 }