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