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