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