]> CyberLeo.Net >> Repos - Github/sugarcrm.git/blob - modules/UpgradeWizard/upload.php
Release 6.5.0
[Github/sugarcrm.git] / modules / UpgradeWizard / upload.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-2012 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
40  * Description:
41  * Portions created by SugarCRM are Copyright (C) SugarCRM, Inc. All Rights
42  * Reserved. Contributor(s): ______________________________________..
43  * *******************************************************************************/
44 logThis('At upload.php');
45
46 //set the upgrade progress status.
47 set_upgrade_progress('upload','in_progress');
48
49
50 $stop = true; // flag to show "next"
51 $run = isset($_REQUEST['run']) ? $_REQUEST['run'] : '';
52 $out = '';
53
54 if(file_exists('ModuleInstall/PackageManager/PackageManagerDisplay.php')) {
55         require_once('ModuleInstall/PackageManager/PackageManagerDisplay.php');
56 }
57
58 ///////////////////////////////////////////////////////////////////////////////
59 ////    UPLOAD FILE PROCESSING
60 switch($run) {
61         case 'upload':
62                 logThis('running upload');
63         $perform = false;
64         $tempFile = '';
65
66                 if(isset($_REQUEST['release_id']) && $_REQUEST['release_id'] != ""){
67             require_once('ModuleInstall/PackageManager/PackageManager.php');
68             $pm = new PackageManager();
69             $tempFile = '';
70             $perform = false;
71             if(!empty($_SESSION['ML_PATCHES'])){
72                 $release_map = $_SESSION['ML_PATCHES'][$_REQUEST['release_id']];
73                 if(!empty($release_map)){
74                         $tempFile = $pm->download($release_map['category_id'], $release_map['package_id'], $_REQUEST['release_id']);
75                         $perform = true;
76                                         if($release_map['type'] != 'patch'){
77                                                 $pm->performSetup($tempFile, $release_map['type'], false);
78                                                 header('Location: index.php?module=Administration&action=UpgradeWizard&view=module');
79                                         }
80                 }
81             }
82
83             $base_filename = urldecode($tempFile);
84         } else {
85             $upload = new UploadFile('upgrade_zip');
86             /* Bug 51722 - Cannot Upload Upgrade File if System Settings Are Not Sufficient, Just Make sure that we can
87             upload no matter what, set the default to 60M */
88             global $sugar_config;
89             $upload_maxsize_backup = $sugar_config['upload_maxsize'];
90             $sugar_config['upload_maxsize'] = 60000000;
91             /* End Bug 51722 */
92             if(!$upload->confirm_upload()) {
93                         logThis('ERROR: no file uploaded!');
94                         echo $mod_strings['ERR_UW_NO_FILE_UPLOADED'];
95                 $error = $upload->get_upload_error();
96                         // add PHP error if isset
97                             if($error) {
98                                     $out = "<b><span class='error'>{$mod_strings['ERR_UW_PHP_FILE_ERRORS'][$error]}</span></b><br />";
99                             }
100             } else {
101                $tempFile = "upload://".$upload->get_stored_file_name();
102                if(!$upload->final_move($tempFile)) {
103                                 logThis('ERROR: could not move temporary file to final destination!');
104                                 unlinkUWTempFiles();
105                                 $out = "<b><span class='error'>{$mod_strings['ERR_UW_NOT_VALID_UPLOAD']}</span></b><br />";
106                } else {
107                                 logThis('File uploaded to '.$tempFile);
108                     $base_filename = urldecode(basename($tempFile));
109                     $perform = true;
110                }
111             }
112             /* Bug 51722 - Restore the upload size in the config */
113             $sugar_config['upload_maxsize'] = $upload_maxsize_backup;
114             /* End Bug 51722 */
115         }
116         if($perform){
117                     $manifest_file = extractManifest($tempFile);
118
119                         if(is_file($manifest_file)) {
120                         require_once( $manifest_file );
121                                 $error = validate_manifest( $manifest );
122                                 if(!empty($error)) {
123                                         $out = "<b><span class='error'>{$error}</span></b><br />";
124                                         break;
125                                 }
126                                 $upgrade_zip_type = $manifest['type'];
127
128                                 // exclude the bad permutations
129                                 if($upgrade_zip_type != "patch") {
130                                         logThis('ERROR: incorrect patch type found: '.$upgrade_zip_type);
131                                         unlinkUWTempFiles();
132                                         $out = "<b><span class='error'>{$mod_strings['ERR_UW_ONLY_PATCHES']}</span></b><br />";
133                                         break;
134                                 }
135
136                                 sugar_mkdir("$base_upgrade_dir/$upgrade_zip_type", 0775, true);
137                                 $target_path = "$base_upgrade_dir/$upgrade_zip_type/$base_filename";
138                                 $target_manifest = remove_file_extension( $target_path ) . "-manifest.php";
139
140                                 if(isset($manifest['icon']) && $manifest['icon'] != "" ) {
141                                         logThis('extracting icons.');
142                                          $icon_location = extractFile( $tempFile ,$manifest['icon'] );
143                                          $path_parts = pathinfo( $icon_location );
144                                          copy( $icon_location, remove_file_extension( $target_path ) . "-icon." . pathinfo($icon_location, PATHINFO_EXTENSION) );
145                                 }
146
147                                 if(rename($tempFile , $target_path)){
148                                         logThis('copying manifest.php to final destination.');
149                                         copy($manifest_file, $target_manifest);
150                                         $out .= "<b>{$base_filename} {$mod_strings['LBL_UW_FILE_UPLOADED']}.</b><br>\n";
151                                 } else {
152                                         logThis('ERROR: cannot copy manifest.php to final destination.');
153                                         $out .= "<b><span class='error'>{$mod_strings['ERR_UW_UPLOAD_ERR']}</span></b><br />";
154                                         break;
155                                 }
156                         } else {
157                                 logThis('ERROR: no manifest.php file found!');
158                                 unlinkUWTempFiles();
159                                 $out = "<b><span class='error'>{$mod_strings['ERR_UW_NO_MANIFEST']}</span></b><br />";
160                                 break;
161                         }
162                         $_SESSION['install_file'] = basename($tempFile);
163                         logThis('zip file moved to ['.$_SESSION['install_file'].']');
164                         //rrs serialize manifest for saving in the db
165                         $serial_manifest = array();
166                         $serial_manifest['manifest'] = (isset($manifest) ? $manifest : '');
167                         $serial_manifest['installdefs'] = (isset($installdefs) ? $installdefs : '');
168                         $serial_manifest['upgrade_manifest'] = (isset($upgrade_manifest) ? $upgrade_manifest : '');
169                         $_SESSION['install_manifest'] = base64_encode(serialize($serial_manifest));
170                 }
171
172                 if(!empty($tempFile)) {
173                         upgradeUWFiles($target_path);
174                         //set the upgrade progress status. actually it should be set when a file is uploaded
175                         set_upgrade_progress('upload','done');
176
177                 }
178
179         break; // end 'upload'
180
181         case 'delete':
182                 logThis('running delete');
183
184         if(!isset($_REQUEST['install_file']) || ($_REQUEST['install_file'] == "")) {
185                 logThis('ERROR: trying to delete non-existent file: ['.$_REQUEST['install_file'].']');
186             $error = $mod_strings['ERR_UW_NO_FILE_UPLOADED'];
187         }
188
189         // delete file in upgrades/patch
190         $delete_me = 'upload://upgrades/patch/'.basename(urldecode( $_REQUEST['install_file'] ));
191         if(@unlink($delete_me)) {
192                 logThis('unlinking: '.$delete_me);
193             $out = basename($delete_me).$mod_strings['LBL_UW_FILE_DELETED'];
194         } else {
195                 logThis('ERROR: could not delete ['.$delete_me.']');
196                         $error = $mod_strings['ERR_UW_FILE_NOT_DELETED'].$delete_me;
197         }
198
199         if(!empty($error)) {
200                         $out = "<b><span class='error'>{$error}</span></b><br />";
201         }
202
203         unlinkUWTempFiles();
204         //set the upgrade progress status. actually it should be set when a file is uploaded
205                 set_upgrade_progress('upload','in_progress');
206
207         break;
208 }
209 ////    END UPLOAD FILE PROCESSING FORM
210 ///////////////////////////////////////////////////////////////////////////////
211
212
213 ///////////////////////////////////////////////////////////////////////////////
214 ////    READY TO INSTALL UPGRADES
215 $validReturn = getValidPatchName();
216 $ready = $validReturn['ready'];
217 $disabled = $validReturn['disabled'];
218 ////    END READY TO INSTALL UPGRADES
219 ///////////////////////////////////////////////////////////////////////////////
220
221 if(isset($_SESSION['install_file']) && !empty($_SESSION['install_file']) && is_file($_SESSION['install_file'])) {
222         $stop = false;
223 } else {
224         $stop = true;
225 }
226 if($stop == false) set_upgrade_progress('upload','done');
227 $frozen = $out;
228
229 if(!$stop){
230     if(!empty($GLOBALS['top_message'])){
231         $GLOBALS['top_message'] .= "<br />";
232     }
233     else{
234         $GLOBALS['top_message'] = '';
235     }
236     $GLOBALS['top_message'] .= "<b>{$mod_strings['LBL_UPLOAD_SUCCESS']}</b>";
237 }
238 else if(!$frozen){
239     $GLOBALS['top_message'] .= "<br />";
240 }
241 else{
242     $GLOBALS['top_message'] = "<b>{$frozen}</b>";
243 }
244
245 ///////////////////////////////////////////////////////////////////////////////
246 ////    UPLOAD FORM
247 $form = '';
248 if(empty($GLOBALS['sugar_config']['disable_uw_upload'])){
249 $form =<<<eoq
250 <form name="the_form" id='the_form' enctype="multipart/form-data" action="index.php" method="post">
251         <input type="hidden" name="module" value="UpgradeWizard">
252         <input type="hidden" name="action" value="index">
253         <input type="hidden" name="step" value="{$_REQUEST['step']}">
254         <input type="hidden" name="run" value="upload">
255 <table width="100%" border="0" cellspacing="0" cellpadding="0" class="edit view">
256 <tr><td>
257         <table width="450" border="0" cellspacing="0" cellpadding="0">
258                 <tr>
259                         <td>
260                                 {$mod_strings['LBL_SELECT_FILE']}
261                                 <input type="file" onchange="uploadCheck();" name="upgrade_zip" id="upgrade_zip" size="40" />
262                         </td>
263                         <td valign="bottom">&nbsp;
264                                 <input id="upload_button" class='button' type=button
265                                                 {$disabled}
266                                                 disabled="disabled"
267                                                 value="{$mod_strings['LBL_UW_TITLE_UPLOAD']}"
268                                                 onClick="uploadCheck();upgradeP('uploadingUpgradePackage', false);document.the_form.upgrade_zip_escaped.value = escape( document.the_form.upgrade_zip.value );document.the_form.submit();" />
269                                 <input type=hidden name="upgrade_zip_escaped" value="" />
270                         </td>
271                 </tr>
272         </table>
273 </td></tr>
274 </table>
275 </form>
276 eoq;
277 }
278 $hidden_fields = "<input type=\"hidden\" name=\"module\" value=\"UpgradeWizard\">";
279 $hidden_fields .= "<input type=\"hidden\" name=\"action\" value=\"index\">";
280 $hidden_fields .= "<input type=\"hidden\" name=\"step\" value=\"{$_REQUEST['step']}\">";
281 $hidden_fields .= "<input type=\"hidden\" name=\"run\" value=\"upload\">";
282 $form2 = '';
283 /*  Removing Install From Sugar tab from Upgradewizard.
284 if(class_exists("PackageManagerDisplay")) {
285         $form2 = PackageManagerDisplay::buildPatchDisplay($form, $hidden_fields, 'index.php', array('patch', 'module'));
286 }
287 */
288 if($form2 == null){
289         $form2 = $form;
290 }
291 $json = getVersionedScript('include/JSON.js');
292 $form3 =<<<eoq2
293 <br>
294
295 <table width="100%" border="0" cellspacing="0" cellpadding="0" class="edit view">
296 <tr><td>
297
298         <table width="100%" border="0" cellspacing="0" cellpadding="0">
299                 <tr>
300                         <td>
301                                 {$mod_strings['LBL_UW_FILES_QUEUED']}<br>
302                                 {$ready}
303                         </td>
304                 </tr>
305         </table>
306 </td></tr>
307 </table>
308 <script>
309  function fileBrowseLoaded(){
310         //alert(document.the_form.upgrade_zip.value.length);
311         if(escape(document.the_form.upgrade_zip.value).length == 0 || escape(document.the_form.upgrade_zip.value) == 'undefined'){
312        document.the_form.upload_button.disabled= 'disabled';
313         }
314         else{
315                 document.the_form.upload_button.disabled= '';
316         }
317         var len = escape(document.the_form.upgrade_zip.value).length;
318  }
319
320  function uploadCheck(){
321    var len = escape(document.the_form.upgrade_zip.value).length;
322    var file_extn = escape(document.the_form.upgrade_zip.value).substr(len-3,len);
323    if(file_extn.toLowerCase() !='zip'){
324                 //document.the_form.upgrade_zip.value = '';
325                 //document.getElementById("upgrade_zip").value = '';
326                 alert('Not a zip file');
327                 document.getElementById("upgrade_zip").value='';
328                 document.getElementById("upload_button").disabled='disabled';
329    } else {
330         //AJAX call for checking the file size and comparing with php.ini settings.
331         var callback = {
332                  success:function(r) {
333                      var file_size = r.responseText;
334                      //alert(file_size.length);
335                      if(file_size.length >0){
336                        var msg = SUGAR.language.get('UpgradeWizard','LBL_UW_FILE_SIZE');
337                        msg1 =SUGAR.language.get('UpgradeWizard','LBL_UW_FILE_BIGGER_MSG');
338                        msg2 = SUGAR.language.get('UpgradeWizard','LBL_BYTES_WEBSERVER_MSG');
339                        if(msg  == 'undefined') msg = 'The file size, ';
340                        if(msg1 == 'undefined') msg1 = ' Bytes, is greater than what is allowed by the upload_max_filesize and/or the post_max_size settings in php.ini. Change the settings so that they are greater than ';
341                        if(msg2 == 'undefined') msg2 = ' Bytes and restart the webserver.';
342                        msg = msg+file_size+msg1;
343                        msg = msg+file_size+msg2;
344                        alert(msg);
345                        document.getElementById("upload_button").disabled='disabled';
346                      }
347                      else{
348                        document.getElementById("upload_button").disabled='';
349                      }
350                  }
351         }
352
353     //var file_name = document.getElementById('upgrade_zip').value;
354         var file_name = document.the_form.upgrade_zip.value;
355         postData = 'file_name=' + YAHOO.lang.JSON.stringify(file_name) + '&module=UpgradeWizard&action=UploadFileCheck&to_pdf=1';
356         YAHOO.util.Connect.asyncRequest('POST', 'index.php', callback, postData);
357    }
358 }
359 </script>
360 eoq2;
361 $form5 =<<<eoq5
362 <br>
363 <div id="upgradeDiv" style="display:none">
364     <table cellspacing="0" cellpadding="0" border="0">
365         <tr><td>
366            <p><!--not_in_theme!--><img src='modules/UpgradeWizard/processing.gif' alt='Processing'></p>
367         </td></tr>
368      </table>
369  </div>
370
371 eoq5;
372 $uwMain = $form2.$form3.$form5;
373 ////    END UPLOAD FORM
374 ///////////////////////////////////////////////////////////////////////////////
375 //set the upgrade progress status. actually it should be set when a file is uploaded
376 //set_upgrade_progress('upload','done');
377
378
379 $showBack               = true;
380 $showCancel             = true;
381 $showRecheck    = false;
382 $showNext               = true;
383
384 $stepBack               = $_REQUEST['step'] - 1;
385 $stepNext               = $_REQUEST['step'] + 1;
386 $stepCancel             = -1;
387 $stepRecheck    = $_REQUEST['step'];
388
389
390 $_SESSION['step'][$steps['files'][$_REQUEST['step']]] = ($stop) ? 'failed' : 'success';
391
392 ?>