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