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