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