]> CyberLeo.Net >> Repos - Github/sugarcrm.git/blob - modules/Administration/UpgradeWizard_prepare.php
Release 6.5.15
[Github/sugarcrm.git] / modules / Administration / UpgradeWizard_prepare.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
40
41
42 require_once('modules/Administration/UpgradeWizardCommon.php');
43
44
45 unset($_SESSION['rebuild_relationships']);
46 unset($_SESSION['rebuild_extensions']);
47 // process commands
48 if(empty($_REQUEST['install_file'])){
49     die( "File to install not specified." );
50 }
51 if( !isset($_REQUEST['mode']) || ($_REQUEST['mode'] == "") ){
52     die( "No mode specified." );
53 }
54
55 if(!file_exists($base_tmp_upgrade_dir)) {
56     mkdir($base_tmp_upgrade_dir, 0755, true);
57 }
58
59 $unzip_dir      = mk_temp_dir( $base_tmp_upgrade_dir );
60 $install_file   = hashToFile($_REQUEST['install_file']);
61 $hidden_fields = "";
62 $new_lang_name = "";
63 $new_lang_desc = "";
64
65 $mode           = $_REQUEST['mode'];
66 $hidden_fields .= "<input type=hidden name=\"mode\" value=\"$mode\"/>";
67
68
69 $install_type   = getInstallType( $install_file );
70
71 $version        = "";
72 $previous_version = "";
73 $show_files     = true;
74
75 $zip_from_dir   = ".";
76 $zip_to_dir     = ".";
77 $zip_force_copy = array();
78 $license_file = $unzip_dir.'/LICENSE.txt';
79 $readme_file  = $unzip_dir.'/README.txt';
80 $require_license = false;
81 $found_readme = false;
82 $author = '';
83 $name = '';
84 $description = '';
85 $is_uninstallable = true;
86 $id_name = '';
87 $dependencies = array();
88 $remove_tables = 'true';
89
90 unzip( $install_file, $unzip_dir );
91 if($install_type == 'module' && $mode != 'Uninstall'){
92    if(file_exists($license_file)){
93         $require_license = true;
94    }
95 }
96
97 //Scan the unzip dir for unsafe files
98 if (((defined('MODULE_INSTALLER_PACKAGE_SCAN') && MODULE_INSTALLER_PACKAGE_SCAN)
99     || !empty($GLOBALS['sugar_config']['moduleInstaller']['packageScan'])) && $install_type != 'patch') {
100         require_once('ModuleInstall/ModuleScanner.php');
101         $ms = new ModuleScanner();
102         $ms->scanPackage($unzip_dir);
103         if($ms->hasIssues()){
104             rmdir_recursive($unzip_dir);
105                 $ms->displayIssues();
106                 sugar_cleanup(true);
107         }
108 }
109
110 // assumption -- already validated manifest.php at time of upload
111 require( "$unzip_dir/manifest.php" );
112
113
114
115 if( isset( $manifest['copy_files']['from_dir'] ) && $manifest['copy_files']['from_dir'] != "" ){
116     $zip_from_dir   = $manifest['copy_files']['from_dir'];
117 }
118 if( isset( $manifest['copy_files']['to_dir'] ) && $manifest['copy_files']['to_dir'] != "" ){
119     $zip_to_dir     = $manifest['copy_files']['to_dir'];
120 }
121 if( isset( $manifest['copy_files']['force_copy'] ) && $manifest['copy_files']['force_copy'] != "" ){
122     $zip_force_copy     = $manifest['copy_files']['force_copy'];
123 }
124 if( isset( $manifest['version'] ) ){
125     $version    = $manifest['version'];
126 }
127 if( isset( $manifest['author'] ) ){
128     $author    = $manifest['author'];
129 }
130 if( isset( $manifest['name'] ) ){
131     $name    = $manifest['name'];
132 }
133 if( isset( $manifest['description'] ) ){
134     $description    = $manifest['description'];
135 }
136 if( isset( $manifest['is_uninstallable'] ) ){
137     $is_uninstallable    = $manifest['is_uninstallable'];
138 }
139 if(isset($installdefs) && isset( $installdefs['id'] ) ){
140     $id_name    = $installdefs['id'];
141 }
142 if( isset( $manifest['dependencies']) ){
143     $dependencies    = $manifest['dependencies'];
144 }
145 if( isset( $manifest['remove_tables']) ){
146     $remove_tables = $manifest['remove_tables'];
147 }
148
149 if($remove_tables != 'prompt'){
150         $hidden_fields .= "<input type=hidden name=\"remove_tables\" value='".$remove_tables."'>";
151 }
152 if(file_exists($readme_file) || !empty($manifest['readme'])){
153         $found_readme = true;
154    }
155 $uh = new UpgradeHistory();
156 //check dependencies first
157 if(!empty($dependencies)){
158         $not_found = $uh->checkDependencies($dependencies);
159         if(!empty($not_found) && count($not_found) > 0){
160                         die( $mod_strings['ERR_UW_NO_DEPENDENCY']."[".implode(',', $not_found)."]");
161         }//fi
162 }
163 switch( $install_type ){
164         case "full":
165         case "patch":
166                 if( !is_writable( "config.php" ) ){
167                         die( $mod_strings['ERR_UW_CONFIG'] );
168                 }
169                 break;
170         case "theme":
171                 break;
172         case "langpack":
173                 // find name of language pack: find single file in include/language/xx_xx.lang.php
174                 $d = dir( "$unzip_dir/$zip_from_dir/include/language" );
175                 while( $f = $d->read() ){
176                         if( $f == "." || $f == ".." ){
177                                 continue;
178                         }
179                         else if( preg_match("/(.*)\.lang\.php\$/", $f, $match) ){
180                                 $new_lang_name = $match[1];
181                         }
182                 }
183                 if( $new_lang_name == "" ){
184                         die( $mod_strings['ERR_UW_NO_LANGPACK'].$install_file );
185                 }
186                 $hidden_fields .= "<input type=hidden name=\"new_lang_name\" value=\"$new_lang_name\"/>";
187
188                 $new_lang_desc = getLanguagePackName( "$unzip_dir/$zip_from_dir/include/language/$new_lang_name.lang.php" );
189                 if( $new_lang_desc == "" ){
190                         die( $mod_strings['ERR_UW_NO_LANG_DESC_1']."include/language/$new_lang_name.lang.php".$mod_strings['ERR_UW_NO_LANG_DESC_2']."$install_file." );
191                 }
192                 $hidden_fields .= "<input type=hidden name=\"new_lang_desc\" value=\"$new_lang_desc\"/>";
193
194                 if( !is_writable( "config.php" ) ){
195                         die( $mod_strings['ERR_UW_CONFIG'] );
196                 }
197                 break;
198         case "module":
199                 $previous_install = array();
200                 if(!empty($id_name) & !empty($version))
201                 $previous_install = $uh->determineIfUpgrade($id_name, $version);
202                 $previous_version = (empty($previous_install['version'])) ? '' : $previous_install['version'];
203                 $previous_id = (empty($previous_install['id'])) ? '' : $previous_install['id'];
204                 $show_files = false;
205                 //rrs pull out unique_key
206                 $hidden_fields .= "<input type=hidden name=\"author\" value=\"$author\"/>";
207                 $hidden_fields .= "<input type=hidden name=\"name\" value=\"$name\"/>";
208                 $hidden_fields .= "<input type=hidden name=\"description\" value=\"$description\"/>";
209                 $hidden_fields .= "<input type=hidden name=\"is_uninstallable\" value=\"$is_uninstallable\"/>";
210                 $hidden_fields .= "<input type=hidden name=\"id_name\" value=\"$id_name\"/>";
211                 $hidden_fields .= "<input type=hidden name=\"previous_version\" value=\"$previous_version\"/>";
212                 $hidden_fields .= "<input type=hidden name=\"previous_id\" value=\"$previous_id\"/>";
213                 break;
214         default:
215                 die( $mod_strings['ERR_UW_WRONG_TYPE'].$install_type );
216 }
217
218
219 $new_files      = findAllFilesRelative( "$unzip_dir/$zip_from_dir", array() );
220 $hidden_fields .= "<input type=hidden name=\"version\" value=\"$version\"/>";
221 $serial_manifest = array();
222 $serial_manifest['manifest'] = (isset($manifest) ? $manifest : '');
223 $serial_manifest['installdefs'] = (isset($installdefs) ? $installdefs : '');
224 $serial_manifest['upgrade_manifest'] = (isset($upgrade_manifest) ? $upgrade_manifest : '');
225 $hidden_fields .= "<input type=hidden name=\"s_manifest\" value='".base64_encode(serialize($serial_manifest))."'>";
226 // present list to user
227 ?>
228 <form action="<?php print( $form_action . "_commit" ); ?>" name="files" method="post"  onSubmit="return validateForm(<?php print($require_license); ?>);">
229 <?php
230 if(empty($new_studio_mod_files)) {
231         if(!empty($mode) && $mode == 'Uninstall')
232         echo $mod_strings['LBL_UW_UNINSTALL_READY'];
233         else if($mode == 'Disable')
234         echo $mod_strings['LBL_UW_DISABLE_READY'];
235         else if($mode == 'Enable')
236         echo $mod_strings['LBL_UW_ENABLE_READY'];
237         else
238         echo $mod_strings['LBL_UW_PATCH_READY'];
239 } else {
240         echo $mod_strings['LBL_UW_PATCH_READY2'];
241         echo '<input type="checkbox" onclick="toggle_these(0, ' . count($new_studio_mod_files) . ', this)"> '.$mod_strings['LBL_UW_CHECK_ALL'];
242         foreach($new_studio_mod_files as $the_file) {
243                 $new_file   = clean_path( "$zip_to_dir/$the_file" );
244                 print( "<li><input id=\"copy_$count\" name=\"copy_$count\" type=\"checkbox\" value=\"" . $the_file . "\"> " . $new_file . "</li>");
245                 $count++;
246         }
247 }
248 echo '<br>';
249 if($require_license){
250     $contents = sugar_file_get_contents($license_file);
251         $readme_contents = '';
252         if($found_readme){
253                 if(file_exists($readme_file) && filesize($readme_file) > 0){
254                         $readme_contents = file_get_contents($readme_file);
255                 }elseif(!empty($manifest['readme'])){
256                         $readme_contents = $manifest['readme'];
257                 }
258         }
259         $license_final =<<<eoq2
260         <table width='100%'>
261         <tr>
262         <td colspan="3"><ul class="tablist">
263         <li id="license_li" class="active"><a id="license_link"  class="current" href="javascript:selectTabCSS('license');">{$mod_strings['LBL_LICENSE']}</a></li>
264         <li class="active" id="readme_li"><a id="readme_link" href="javascript:selectTabCSS('readme');">{$mod_strings['LBL_README']}</a></li>
265         </ul></td>
266         </tr>
267         </table>
268         <div id='license_div'>
269         <table>
270         <tr>
271         <td colspan="3">&nbsp;</td>
272         </tr>
273         <tr>
274         <td align="left" valign="top" colspan=2>
275         <b>{$mod_strings['LBL_MODULE_LICENSE']}</b>
276         </td>
277         </tr>
278         <tr>
279         <td align="left" valign="top" colspan=2>
280         <textarea cols="100" rows="8" readonly>{$contents}</textarea>
281         </td>
282
283         </tr>
284         <tr>
285         <td align="left" valign="top" colspan=2>
286         <input type='radio' id='radio_license_agreement_accept' name='radio_license_agreement' value='accept'>{$mod_strings['LBL_ACCEPT']}&nbsp;
287         <input type='radio' id='radio_license_agreement_reject' name='radio_license_agreement' value='reject' checked>{$mod_strings['LBL_DENY']}
288         </td>
289
290         </tr></table>
291         </div>
292         <div id='readme_div' style='display: none;'>
293         <table>
294         <tr>
295         <td colspan="3">&nbsp;</td>
296         </tr>
297         <tr>
298         <td align="left" valign="top" colspan=2>
299         <b>{$mod_strings['LBL_README']}</b>
300         </td>
301         </tr>
302         <tr>
303         <td align="left" valign="top" colspan=2>
304         <textarea cols="100" rows="8" readonly>{$readme_contents}</textarea>
305         </td>
306
307     </tr>
308 </table>
309 </div>
310
311 eoq2;
312         echo $license_final;
313         echo "<br>";
314 }
315
316 switch( $mode ){
317         case "Install":
318                 if( $install_type == "langpack") {
319                         print( $mod_strings['LBL_UW_LANGPACK_READY'] );
320                         echo '<br><br>';
321                 }
322                 break;
323         case "Uninstall":
324                 if( $install_type == "langpack" ){
325                         print( $mod_strings['LBL_UW_LANGPACK_READY_UNISTALL'] );
326                         echo '<br><br>';
327                 }
328                 else if($install_type != "module"){
329                         print( $mod_strings['LBL_UW_FILES_REMOVED'] );
330                 }
331                 break;
332         case "Disable":
333                 if( $install_type == "langpack" ){
334                         print( $mod_strings['LBL_UW_LANGPACK_READY_DISABLE'] );
335                         echo '<br><br>';
336                 }
337                 break;
338         case "Enable":
339                 if( $install_type == "langpack" ){
340                         print( $mod_strings['LBL_UW_LANGPACK_READY_ENABLE'] );
341                         echo '<br><br>';
342                 }
343                 break;
344 }
345
346
347 ?>
348 <input type=submit value="<?php echo $mod_strings['LBL_ML_COMMIT'];?>" class="button" id="submit_button" />
349 <input type=button value="<?php echo $mod_strings['LBL_ML_CANCEL'];?>" class="button" onClick="location.href='index.php?module=Administration&action=UpgradeWizard&view=module';"/>
350
351 <?php
352
353 if($remove_tables == 'prompt' && $mode == 'Uninstall'){
354     print ("<br/><br/>");
355         print ("<input type='radio' id='remove_tables_true' name='remove_tables' value='true' checked>".$mod_strings['ML_LBL_REMOVE_TABLES']."&nbsp;");
356     print ("<input type='radio' id='remove_tables_false' name='remove_tables' value='false'>".$mod_strings['ML_LBL_DO_NOT_REMOVE_TABLES']."<br>");
357 }
358 $count = 0;
359
360 if( $show_files == true ){
361         $count = 0;
362
363         $new_studio_mod_files = array();
364         $new_sugar_mod_files = array();
365
366         $cache_html_files = findAllFilesRelative( sugar_cached("layout"), array());
367
368         foreach($new_files as $the_file) {
369                 if(substr(strtolower($the_file), -5, 5) == '.html' && in_array($the_file, $cache_html_files))
370                 array_push($new_studio_mod_files, $the_file);
371                 else
372                 array_push($new_sugar_mod_files, $the_file);
373         }
374
375         echo '<script>
376             function toggle_these(start, end, ca) {
377               while(start < end) {
378                 elem = eval("document.forms.files.copy_" + start);
379                 if(!ca.checked) elem.checked = false;
380                 else elem.checked = true;
381                 start++;
382               }
383             }
384                         </script>';
385
386
387
388         global $theme;
389
390         echo '<br/><br/>';
391
392     echo '<div style="text-align: left; cursor: hand; cursor: pointer; text-decoration: underline;'.(($mode == 'Enable' || $mode == 'Disable')?'display:none;':'').'" onclick=\'this.style.display="none"; toggleDisplay("more");\'id="all_text">
393         '.SugarThemeRegistry::current()->getImage('advanced_search', '', null, null, ".gif", $mod_strings['LBL_ADVANCED_SEARCH']).$mod_strings['LBL_UW_SHOW_DETAILS'].'</div><div id=\'more\' style=\'display: none\'>
394               <div style="text-align: left; cursor: hand; cursor: pointer; text-decoration: underline;" onclick=\'document.getElementById("all_text").style.display=""; toggleDisplay("more");\'>'
395          .SugarThemeRegistry::current()->getImage('basic_search', '', null, null, ".gif", $mod_strings['LBL_BASIC_SEARCH']).$mod_strings['LBL_UW_HIDE_DETAILS'].'</div><br>';
396     echo '<input type="checkbox" checked onclick="toggle_these(' . count($new_studio_mod_files) . ',' . count($new_files) . ', this)"> '.$mod_strings['LBL_UW_CHECK_ALL'];
397         echo '<ul>';
398         foreach( $new_sugar_mod_files as $the_file ){
399                 $highlight_start    = "";
400                 $highlight_end      = "";
401                 $checked            = "";
402                 $disabled           = "";
403                 $unzip_file = "$unzip_dir/$zip_from_dir/$the_file";
404                 $new_file   = clean_path( "$zip_to_dir/$the_file" );
405                 $forced_copy    = false;
406
407                 if( $mode == "Install" ){
408                         $checked = "checked";
409                         foreach( $zip_force_copy as $pattern ){
410                                 if( preg_match("#" . $pattern . "#", $unzip_file) ){
411                                         $disabled = "disabled=\"true\"";
412                                         $forced_copy = true;
413                                 }
414                         }
415                         if( !$forced_copy && is_file( $new_file ) && (md5_file( $unzip_file ) == md5_file( $new_file )) ){
416                                 $disabled = "disabled=\"true\"";
417                                 //$checked = "";
418                         }
419                         if( $checked != "" && $disabled != "" ){    // need to put a hidden field
420                                 print( "<input name=\"copy_$count\" type=\"hidden\" value=\"" . $the_file . "\">\n" );
421                         }
422                         print( "<li><input id=\"copy_$count\" name=\"copy_$count\" type=\"checkbox\" value=\"" . $the_file . "\" $checked $disabled > " . $highlight_start . $new_file . $highlight_end );
423                         if( $checked == "" && $disabled != "" ){    // need to explain this file hasn't changed
424                                 print( " (no changes)" );
425                         }
426                         print( "<br>\n" );
427                 }
428                 else if( $mode == "Uninstall" && file_exists( $new_file ) ){
429                         if( md5_file( $unzip_file ) == md5_file( $new_file ) ){
430                                 $checked = "checked=\"true\"";
431                         }
432                         else{
433                                 $highlight_start    = "<font color=red>";
434                                 $highlight_end      = "</font>";
435                         }
436                         print( "<li><input name=\"copy_$count\" type=\"checkbox\" value=\"" . $the_file . "\" $checked $disabled > " . $highlight_start . $new_file . $highlight_end . "<br>\n" );
437                 }
438                 $count++;
439         }
440         print( "</ul>\n" );
441 }
442 //    echo '</div>';
443 if($mode == "Disable" || $mode == "Enable"){
444         //check to see if any files have been modified
445         $modified_files = getDiffFiles($unzip_dir, $install_file, ($mode == 'Enable'), $previous_version);
446         if(count($modified_files) > 0){
447                 //we need to tell the user that some files have been modified since they last did an install
448                 echo '<script>' .
449                 'function handleFileChange(){';
450                 if(count($modified_files) > 0){
451                         echo 'if(document.getElementById("radio_overwrite_files") != null && document.getElementById("radio_do_not_overwrite_files") != null){
452                                         var overwrite = false;
453                                         if(document.getElementById("radio_overwrite_files").checked){
454                                          overwrite = true
455                                         }
456                                 }
457                                         return true;';
458                 }else{
459                         echo 'return true;';
460                 }
461                 echo '}</script>';
462                 print('<b>'.$mod_strings['ML_LBL_OVERWRITE_FILES'].'</b>');
463                 print('<table><td align="left" valign="top" colspan=2>');
464                 print("<input type='radio' id='radio_overwrite_files' name='radio_overwrite' value='overwrite'>{$mod_strings['LBL_OVERWRITE_FILES']}&nbsp;");
465                 print("<input type='radio' id='radio_do_not_overwrite_files' name='radio_overwrite' value='do_not_overwrite' checked>{$mod_strings['LBL_DO_OVERWRITE_FILES']}");
466                 print("</td></tr></table>");
467                 print('<ul>');
468                 foreach($modified_files as $modified_file){
469                         print('<li>'.$modified_file.'</li>');
470                 }
471                 print('</ul>');
472         }else{
473                 echo '<script>' .
474                 'function handleFileChange(){';
475                 echo 'return true;';
476                 echo '}</script>';
477         }
478 }else{
479         echo '<script>' .
480         'function handleFileChange(){';
481         echo 'return true;';
482         echo '}</script>';
483 }
484 echo '<script>' .
485                         'function validateForm(process){'.
486                                 'return (handleCommit(process) && handleFileChange());'.
487                         '}'.
488                 'function handleCommit(process){
489         if(process == 1) {
490             if(document.getElementById("radio_license_agreement_reject") != null && document.getElementById("radio_license_agreement_accept") != null){
491                 var accept = false;
492                 if(document.getElementById("radio_license_agreement_accept").checked){
493                     accept = true
494                 }
495                 if(!accept){
496                     //do not allow the form to submit
497                     alert("'.$mod_strings['ERR_UW_ACCEPT_LICENSE'].'");
498                     return false;
499                 }
500             }
501         }
502         document.getElementById("submit_button").disabled = true;
503         return true;
504     }
505     var keys = [ "license","readme"];
506     function selectTabCSS(key){
507                 for( var i=0; i<keys.length;i++)
508                 {
509                         var liclass = "";
510                         var linkclass = "";
511
512                         if ( key == keys[i])
513                         {
514                         var liclass = "active";
515                         var linkclass = "current";
516                         document.getElementById(keys[i]+"_div").style.display = "block";
517                         }else{
518                         document.getElementById(keys[i]+"_div").style.display = "none";
519                         }
520                         document.getElementById(keys[i]+"_li").className = liclass;
521                         document.getElementById(keys[i]+"_link").className = linkclass;
522                 }
523                 tabPreviousKey = key;
524             }
525       </script>';
526
527     $fileHash = fileToHash($install_file );
528 ?>
529     <?php print( $hidden_fields ); ?>
530     <input type="hidden" name="copy_count" value="<?php print( $count );?>"/>
531     <input type="hidden" name="run" value="commit" />
532     <input type="hidden" name="install_file"  value="<?php echo $fileHash; ?>" />
533     <input type="hidden" name="unzip_dir"     value="<?php echo basename($unzip_dir); ?>" />
534     <input type="hidden" name="zip_from_dir"  value="<?php echo $zip_from_dir; ?>" />
535     <input type="hidden" name="zip_to_dir"    value="<?php echo $zip_to_dir; ?>" />
536 </form>
537
538 <?php
539     $GLOBALS['log']->info( "Upgrade Wizard patches" );
540 ?>