]> CyberLeo.Net >> Repos - Github/sugarcrm.git/blob - modules/Administration/UpgradeWizard_commit.php
Release 6.4.0
[Github/sugarcrm.git] / modules / Administration / UpgradeWizard_commit.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 require_once('modules/Administration/UpgradeWizardCommon.php');
41 require_once('modules/Configurator/Configurator.php');
42 function UWrebuild() {
43         global $log;
44         global $db;
45         $log->info('Deleting Relationship Cache. Relationships will automatically refresh.');
46
47         echo "
48         <div id='rrresult'></div>
49         <script>
50                 var xmlhttp=false;
51                 /*@cc_on @*/
52                 /*@if (@_jscript_version >= 5)
53                 // JScript gives us Conditional compilation, we can cope with old IE versions.
54                 // and security blocked creation of the objects.
55                  try {
56                   xmlhttp = new ActiveXObject(\"Msxml2.XMLHTTP\");
57                  } catch (e) {
58                   try {
59                    xmlhttp = new ActiveXObject(\"Microsoft.XMLHTTP\");
60                   } catch (E) {
61                    xmlhttp = false;
62                   }
63                  }
64                 @end @*/
65                 if (!xmlhttp && typeof XMLHttpRequest!='undefined') {
66                         try {
67                                 xmlhttp = new XMLHttpRequest();
68                         } catch (e) {
69                                 xmlhttp = false;
70                         }
71                 }
72                 if (!xmlhttp && window.createRequest) {
73                         try {
74                                 xmlhttp = window.createRequest();
75                         } catch (e) {
76                                 xmlhttp = false;
77                         }
78                 }
79                 xmlhttp.onreadystatechange = function() {
80                             if(xmlhttp.readyState == 4) {
81                               document.getElementById('rrresult').innerHTML = xmlhttp.responseText;
82                             }
83                           }
84                 xmlhttp.open('GET', 'index.php?module=Administration&action=RebuildRelationship&to_pdf=true', true);
85                 xmlhttp.send(null);
86                 </script>";
87
88         $log->info('Rebuilding everything.');
89         require_once('ModuleInstall/ModuleInstaller.php');
90         $mi = new ModuleInstaller();
91         $mi->rebuild_all();
92         $query = "DELETE FROM versions WHERE name='Rebuild Extensions'";
93         $log->info($query);
94         $db->query($query);
95
96         // insert a new database row to show the rebuild extensions is done
97         $id = create_guid();
98         $gmdate = TimeDate::getInstance()->nowDb();
99         $date_entered = db_convert("'$gmdate'", 'datetime');
100         $query = 'INSERT INTO versions (id, deleted, date_entered, date_modified, modified_user_id, created_by, name, file_version, db_version) '
101                 . "VALUES ('$id', '0', $date_entered, $date_entered, '1', '1', 'Rebuild Extensions', '4.0.0', '4.0.0')";
102         $log->info($query);
103         $db->query($query);
104 }
105
106 unset($_SESSION['rebuild_relationships']);
107 unset($_SESSION['rebuild_extensions']);
108
109 global $log, $db;
110
111 // process commands
112 if( !isset($_REQUEST['mode']) || ($_REQUEST['mode'] == "") ){
113     die($mod_strings['ERR_UW_NO_MODE']);
114 }
115 $mode = $_REQUEST['mode'];
116
117
118 if( !isset($_REQUEST['version']) ){
119     die($mod_strings['ERR_UW_NO_MODE']);
120 }
121 $version = $_REQUEST['version'];
122
123 if( !isset($_REQUEST['copy_count']) || ($_REQUEST['copy_count'] == "") ){
124     die($mod_strings['ERR_UW_NO_FILES']);
125 }
126
127 if( empty($_REQUEST['unzip_dir']) || $_REQUEST['unzip_dir'] == "." || $_REQUEST['unzip_dir'] == ".."){
128     die($mod_strings['ERR_UW_NO_TEMP_DIR']);
129 }
130 $unzip_dir = $base_tmp_upgrade_dir. "/". basename($_REQUEST['unzip_dir']);
131
132 if(empty($_REQUEST['install_file'])){
133     die($mod_strings['ERR_UW_NO_INSTALL_FILE']);
134 }
135
136 $install_file   = hashToFile($_REQUEST['install_file'] );
137 $install_type   = getInstallType( $install_file );
138 $id_name = '';
139 if(isset($_REQUEST['id_name'])){
140  $id_name = $_REQUEST['id_name'];
141 }
142 $s_manifest = '';
143 if(isset($_REQUEST['s_manifest'])){
144  $s_manifest = $_REQUEST['s_manifest'];
145 }
146 $previous_version = '';
147 if(isset($_REQUEST['previous_version'])){
148         $previous_version = $_REQUEST['previous_version'];
149 }
150 $previous_id = '';
151 if(isset($_REQUEST['previous_id'])){
152         $previous_id = $_REQUEST['previous_id'];
153 }
154 if( $install_type != "module" ){
155     if( !isset($_REQUEST['zip_from_dir']) || ($_REQUEST['zip_from_dir'] == "") ){
156         $zip_from_dir     = ".";
157     }
158     else{
159         $zip_from_dir   = $_REQUEST['zip_from_dir'];
160     }
161     if( !isset($_REQUEST['zip_to_dir']) || ($_REQUEST['zip_to_dir'] == "") ){
162         $zip_to_dir     = ".";
163     }
164     else{
165         $zip_to_dir     = $_REQUEST['zip_to_dir'];
166     }
167 }
168 $remove_tables = 'true';
169 if(isset($_REQUEST['remove_tables'])){
170         $remove_tables = $_REQUEST['remove_tables'];
171 }
172 $overwrite_files = true;
173 if(isset($_REQUEST['radio_overwrite'])){
174  $overwrite_files = (($_REQUEST['radio_overwrite'] == 'do_not_overwrite') ? false : true);
175 }
176
177 //rrs
178 $author = '';
179 $is_uninstallable = true;
180 $name = '';
181 $description = '';
182
183 if($install_type == 'module'){
184     $is_uninstallable = $_REQUEST['is_uninstallable'];
185     $name = $_REQUEST['name'];
186     $description = $_REQUEST['description'];
187 }
188
189
190 $file_action    = "";
191 $uh_status      = "";
192
193 $rest_dir = remove_file_extension($install_file)."-restore";
194
195 $files_to_handle  = array();
196
197 if(!empty($GLOBALS['sugar_config']['moduleInstaller']['packageScan']) && $install_type != 'patch'){
198         require_once('ModuleInstall/ModuleScanner.php');
199         $ms = new ModuleScanner();
200         $ms->scanPackage($unzip_dir);
201         if($ms->hasIssues()){
202                 $ms->displayIssues();
203                 sugar_cleanup(true);
204         }
205 }
206
207 //
208 // execute the PRE scripts
209 //
210 if($install_type == 'patch' || $install_type == 'module')
211 {
212         switch($mode)
213         {
214                 case 'Install':
215                         $file = "$unzip_dir/" . constant('SUGARCRM_PRE_INSTALL_FILE');
216                         if(is_file($file))
217                         {
218                                 print("{$mod_strings['LBL_UW_INCLUDING']}: $file <br>\n");
219                                 include($file);
220                                 pre_install();
221                 }
222                         break;
223                 case 'Uninstall':
224                         $file = "$unzip_dir/" . constant('SUGARCRM_PRE_UNINSTALL_FILE');
225                         if(is_file($file))
226                         {
227                                 print("{$mod_strings['LBL_UW_INCLUDING']}: $file <br>\n");
228                                 include($file);
229                                 pre_uninstall();
230                 }
231                         break;
232                 default:
233                         break;
234                 }
235 }
236
237 //
238 // perform the action
239 //
240
241 for( $iii = 0; $iii < $_REQUEST['copy_count']; $iii++ ){
242     if( isset($_REQUEST["copy_" . $iii]) && ($_REQUEST["copy_" . $iii] != "") ){
243         $file_to_copy = $_REQUEST["copy_" . $iii];
244         $src_file   = clean_path( "$unzip_dir/$zip_from_dir/$file_to_copy" );
245
246         $sugar_home_dir = getCwd();
247         $dest_file  = clean_path( "$sugar_home_dir/$zip_to_dir/$file_to_copy" );
248         if($zip_to_dir != '.')
249                 $rest_file  = clean_path("$rest_dir/$zip_to_dir/$file_to_copy");
250         else
251                 $rest_file  = clean_path("$rest_dir/$file_to_copy");
252
253         switch( $mode ){
254             case "Install":
255                 mkdir_recursive( dirname( $dest_file ) );
256
257                 if($install_type=="patch" && is_file($dest_file))
258                 {
259                         if(!is_dir(dirname( $rest_file )))
260                                 mkdir_recursive( dirname( $rest_file ) );
261
262                         copy( $dest_file, $rest_file);
263                         sugar_touch( $rest_file, filemtime($dest_file) );
264                 }
265
266                 if( !copy( $src_file, $dest_file ) ){
267                     die( $mod_strings['ERR_UW_COPY_FAILED'].$src_file.$mod_strings['LBL_TO'].$dest_file);
268                 }
269                 $uh_status = "installed";
270                 break;
271             case "Uninstall":
272                 if($install_type=="patch" && is_file($rest_file))
273                 {
274                         copy( $rest_file, $dest_file);
275                         sugar_touch( $dest_file, filemtime($rest_file) );
276                 }
277                 elseif(file_exists($dest_file) && !unlink($dest_file))
278                 {
279                     die($mod_strings['ERR_UW_REMOVE_FAILED'].$dest_file);
280                 }
281                 $uh_status = "uninstalled";
282                 break;
283             default:
284                 die("{$mod_strings['LBL_UW_OP_MODE']} '$mode' {$mod_strings['ERR_UW_NOT_RECOGNIZED']}." );
285         }
286         $files_to_handle[] = clean_path( "$zip_to_dir/$file_to_copy" );
287     }
288 }
289
290 switch( $install_type ){
291     case "langpack":
292         if( !isset($_REQUEST['new_lang_name']) || ($_REQUEST['new_lang_name'] == "") ){
293             die($mod_strings['ERR_UW_NO_LANG']);
294         }
295         if( !isset($_REQUEST['new_lang_desc']) || ($_REQUEST['new_lang_desc'] == "") ){
296             die($mod_strings['ERR_UW_NO_LANG_DESC']);
297         }
298
299         if( $mode == "Install" || $mode=="Enable" ){
300             $sugar_config['languages'] = $sugar_config['languages'] + array( $_REQUEST['new_lang_name'] => $_REQUEST['new_lang_desc'] );
301         }
302         else if( $mode == "Uninstall" || $mode=="Disable" ){
303             $new_langs = array();
304             $old_langs = $sugar_config['languages'];
305             foreach( $old_langs as $key => $value ){
306                 if( $key != $_REQUEST['new_lang_name'] ){
307                     $new_langs += array( $key => $value );
308                 }
309             }
310                         $sugar_config['languages'] = $new_langs;
311
312                 $default_sugar_instance_lang = 'en_us';
313                 if($current_language == $_REQUEST['new_lang_name']){
314                         $_SESSION['authenticated_user_language'] =$default_sugar_instance_lang;
315                         $lang_changed_string = $mod_strings['LBL_CURRENT_LANGUAGE_CHANGE'].$sugar_config['languages'][$default_sugar_instance_lang].'<br/>';
316                 }
317
318                 if($sugar_config['default_language'] == $_REQUEST['new_lang_name']){
319                         $cfg = new Configurator();
320                 $cfg->config['languages'] = $new_langs;
321                                 $cfg->config['default_language'] = $default_sugar_instance_lang;
322                                 $cfg->handleOverride();
323                         $lang_changed_string .= $mod_strings['LBL_DEFAULT_LANGUAGE_CHANGE'].$sugar_config['languages'][$default_sugar_instance_lang].'<br/>';
324                 }
325         }
326         ksort( $sugar_config );
327         if( !write_array_to_file( "sugar_config", $sugar_config, "config.php" ) ){
328             die($mod_strings['ERR_UW_CONFIG_FAILED']);
329         }
330         break;
331     case "module":
332         require_once( "ModuleInstall/ModuleInstaller.php" );
333         $mi = new ModuleInstaller();
334         switch( $mode ){
335             case "Install":
336             //here we can determine if this is an upgrade or a new version
337                 if(!empty($previous_version)){
338                         $mi->install( "$unzip_dir", true, $previous_version);
339                 }else{
340                         $mi->install( "$unzip_dir" );
341                 }
342
343                                 $file = "$unzip_dir/" . constant('SUGARCRM_POST_INSTALL_FILE');
344                                 if(is_file($file))
345                                 {
346                                         print("{$mod_strings['LBL_UW_INCLUDING']}: $file <br>\n");
347                                         include($file);
348                                         post_install();
349                                 }
350                 break;
351             case "Uninstall":
352                 if($remove_tables == 'false')
353                         $GLOBALS['mi_remove_tables'] = false;
354                 else
355                         $GLOBALS['mi_remove_tables'] = true;
356                 $mi->uninstall( "$unzip_dir" );
357                 break;
358              case "Disable":
359                 if(!$overwrite_files)
360                         $GLOBALS['mi_overwrite_files'] = false;
361                 else
362                         $GLOBALS['mi_overwrite_files'] = true;
363                 $mi->disable( "$unzip_dir" );
364                 break;
365              case "Enable":
366                 if(!$overwrite_files)
367                         $GLOBALS['mi_overwrite_files'] = false;
368                 else
369                         $GLOBALS['mi_overwrite_files'] = true;
370                 $mi->enable( "$unzip_dir" );
371                 break;
372             default:
373                 break;
374         }
375         break;
376     case "full":
377         // purposely flow into "case: patch"
378     case "patch":
379                 switch($mode)
380                 {
381                         case 'Install':
382                                 $file = "$unzip_dir/" . constant('SUGARCRM_POST_INSTALL_FILE');
383                                 if(is_file($file))
384                                 {
385                                         print("{$mod_strings['LBL_UW_INCLUDING']}: $file <br>\n");
386                                         include($file);
387                                         post_install();
388                                 }
389
390                                 UWrebuild();
391                                 break;
392                         case 'Uninstall':
393                                 $file = "$unzip_dir/" . constant('SUGARCRM_POST_UNINSTALL_FILE');
394                                 if(is_file($file)) {
395                                         print("{$mod_strings['LBL_UW_INCLUDING']}: $file <br>\n");
396                                         include($file);
397                                         post_uninstall();
398                                 }
399
400                                 if(is_dir($rest_dir))
401                                 {
402                                         rmdir_recursive($rest_dir);
403                                 }
404
405                                 UWrebuild();
406                                 break;
407                         default:
408                                 break;
409                 }
410
411                 require( "sugar_version.php" );
412                 $sugar_config['sugar_version'] = $sugar_version;
413                 ksort( $sugar_config );
414
415                 if( !write_array_to_file( "sugar_config", $sugar_config, "config.php" ) )
416                 {
417                         die($mod_strings['ERR_UW_UPDATE_CONFIG']);
418                 }
419         break;
420     default:
421         break;
422 }
423
424 switch( $mode ){
425     case "Install":
426         $file_action = "copied";
427         // if error was encountered, script should have died before now
428         $new_upgrade = new UpgradeHistory();
429         //determine if this module has already been installed given the unique_key to
430         //identify the module
431        // $new_upgrade->checkForExisting($unique_key);
432         if(!empty($previous_id)){
433                 $new_upgrade->id = $previous_id;
434                 $uh = new UpgradeHistory();
435                 $uh->retrieve($previous_id);
436                 if(is_file($uh->filename)) {
437                 unlink($uh->filename);
438                 }
439         }
440         $new_upgrade->filename      = $install_file;
441         $new_upgrade->md5sum        = md5_file( $install_file );
442         $new_upgrade->type          = $install_type;
443         $new_upgrade->version       = $version;
444         $new_upgrade->status        = "installed";
445         $new_upgrade->name          = $name;
446         $new_upgrade->description   = $description;
447         $new_upgrade->id_name           = $id_name;
448         $new_upgrade->manifest          = $s_manifest;
449         $new_upgrade->save();
450
451         //Check if we need to show a page for the user to finalize their install with.
452         if (is_file("$unzip_dir/manifest.php"))
453         {
454                 include("$unzip_dir/manifest.php");
455                 if (!empty($manifest['post_install_url']))
456                 {
457                         $url_conf = $manifest['post_install_url'];
458                         if (is_string($url_conf))
459                                 $url_conf = array('url' => $url_conf);
460                         if (isset($url_conf['type']) && $url_conf['type'] == 'popup')
461                         {
462                                 echo '<script type="text/javascript">window.open("' . $url_conf['url']
463                                    . '","' . (empty($url_conf['name']) ? 'sugar_popup' : $url_conf['name']) . '","'
464                                    . 'height=' . (empty($url_conf['height']) ? '500' : $url_conf['height']) . ','
465                                    . 'width=' . (empty($url_conf['width']) ? '800' : $url_conf['width']) . '");</script>';
466                         } else
467                         {
468                                 echo '<iframe src="' . $url_conf['url'] . '" '
469                                    . 'width="' . (empty($url_conf['width']) ? '100%' : $url_conf['width']) . '" '
470                                    . 'height="' . (empty($url_conf['height']) ? '500px' : $url_conf['height']) . '"></iframe>';
471                         }
472                 }
473         }
474     break;
475     case "Uninstall":
476         $file_action = "removed";
477         $uh = new UpgradeHistory();
478         $the_md5 = md5_file( $install_file );
479         $md5_matches = $uh->findByMd5( $the_md5 );
480         if( sizeof( $md5_matches ) == 0 ){
481             die( "{$mod_strings['ERR_UW_NO_UPDATE_RECORD']} $install_file." );
482         }
483         foreach( $md5_matches as $md5_match ){
484             $md5_match->delete();
485         }
486         break;
487     case "Disable":
488         $file_action = "disabled";
489         $uh = new UpgradeHistory();
490         $the_md5 = md5_file( $install_file );
491         $md5_matches = $uh->findByMd5( $the_md5 );
492         if( sizeof( $md5_matches ) == 0 ){
493             die( "{$mod_strings['ERR_UW_NO_UPDATE_RECORD']} $install_file." );
494         }
495         foreach( $md5_matches as $md5_match ){
496              $md5_match->enabled = 0;
497             $md5_match->save();
498         }
499         break;
500     case "Enable":
501         $file_action = "enabled";
502         $uh = new UpgradeHistory();
503         $the_md5 = md5_file( $install_file );
504         $md5_matches = $uh->findByMd5( $the_md5 );
505         if( sizeof( $md5_matches ) == 0 ){
506             die( "{$mod_strings['ERR_UW_NO_UPDATE_RECORD']} $install_file." );
507         }
508         foreach( $md5_matches as $md5_match ){
509             $md5_match->enabled = 1;
510             $md5_match->save();
511         }
512         break;
513 }
514
515 // present list to user
516 ?>
517 <form action="<?php print( $form_action ); ?>" method="post">
518
519
520 <?php
521 echo "<div>";
522 print( getUITextForType($install_type) . " ". getUITextForMode($mode) . " ". $mod_strings['LBL_UW_SUCCESSFULLY']);
523 echo "<br>";
524 echo "<br>";
525 print( "<input type=submit value=\"{$mod_strings['LBL_UW_BTN_BACK_TO_MOD_LOADER']}\" /><br>" );
526 echo "</div>";
527 echo "<br>";
528 if(isset($lang_changed_string))
529         print($lang_changed_string);
530 if ($install_type != "module" && $install_type != "langpack"){
531     if( sizeof( $files_to_handle ) > 0 ){
532         echo '<div style="text-align: left; cursor: hand; cursor: pointer; text-decoration: underline;" onclick=\'this.style.display="none"; toggleDisplay("more");\' id="all_text">' . SugarThemeRegistry::current()->getImage('advanced_search', '', null, null, ".gif", $mod_strings['LBL_ADVANCED_SEARCH']) . ' Show Details</div><div id=\'more\' style=\'display: none\'>
533             <div style="text-align: left; cursor: hand; cursor: pointer; text-decoration: underline;" onclick=\'document.getElementById("all_text").style.display=""; toggleDisplay("more");\'>' . SugarThemeRegistry::current()->getImage('basic_search', '', null, null, ".gif", $mod_strings['LBL_BASIC_SEARCH']) . ' Hide Details</div><br>';
534         print( "{$mod_strings['LBL_UW_FOLLOWING_FILES']} $file_action:<br>\n" );
535         print( "<ul id=\"subMenu\">\n" );
536         foreach( $files_to_handle as $file_to_copy ){
537             print( "<li>$file_to_copy<br>\n" );
538         }
539         print( "</ul>\n" );
540         echo '</div>';
541     }
542     else if( $mode != 'Disable' && $mode !='Enable' ){
543         print( "{$mod_strings['LBL_UW_NO_FILES_SELECTED']} $file_action.<br>\n" );
544     }
545
546         print($mod_strings['LBL_UW_UPGRADE_SUCCESSFUL']);
547         print( "<input class='button' type=submit value=\"{$mod_strings['LBL_UW_BTN_BACK_TO_UW']}\" />\n" );
548 }
549 ?>
550 </form>
551
552 <?php
553     $GLOBALS['log']->info( "Upgrade Wizard patches" );
554 ?>