3 /*********************************************************************************
4 * SugarCRM is a customer relationship management program developed by
5 * SugarCRM, Inc. Copyright (C) 2004-2011 SugarCRM Inc.
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.
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
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
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.
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.
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 ********************************************************************************/
39 //////////////////////////////////////////////////////////////////////////////////////////
40 //// This is a stand alone file that can be run from the command prompt for upgrading a
41 //// Sugar Instance. Three parameters are required to be defined in order to execute this file.
42 //// php.exe -f silentUpgrade.php [Path to Upgrade Package zip] [Path to Log file] [Path to Instance]
43 //// See below the Usage for more details.
44 /////////////////////////////////////////////////////////////////////////////////////////
45 ini_set('memory_limit',-1);
46 ///////////////////////////////////////////////////////////////////////////////
47 //// UTILITIES THAT MUST BE LOCAL :(
48 //Bug 24890, 24892. default_permissions not written to config.php. Following function checks and if
49 //no found then adds default_permissions to the config file.
50 function checkConfigForPermissions(){
51 if(file_exists(getcwd().'/config.php')){
52 require(getcwd().'/config.php');
55 if(!isset($sugar_config['default_permissions'])){
56 $sugar_config['default_permissions'] = array (
63 if(is_writable('config.php') && write_array_to_file("sugar_config", $sugar_config,'config.php')) {
69 function checkLoggerSettings(){
70 if(file_exists(getcwd().'/config.php')){
71 require(getcwd().'/config.php');
74 if(!isset($sugar_config['logger'])){
75 $sugar_config['logger'] =array (
88 if(is_writable('config.php') && write_array_to_file("sugar_config", $sugar_config,'config.php')) {
94 function checkResourceSettings(){
95 if(file_exists(getcwd().'/config.php')){
96 require(getcwd().'/config.php');
99 if(!isset($sugar_config['resource_management'])){
100 $sugar_config['resource_management'] =
102 'special_query_limit' => 50000,
103 'special_query_modules' =>
108 3 => 'Administration',
111 'default_limit' => 1000,
113 ksort($sugar_config);
114 if(is_writable('config.php') && write_array_to_file("sugar_config", $sugar_config,'config.php')) {
115 //writing to the file
121 function verifyArguments($argv,$usage_regular){
123 $cwd = getcwd(); // default to current, assumed to be in a valid SugarCRM root dir.
124 if(isset($argv[3])) {
125 if(is_dir($argv[3])) {
129 echo "*******************************************************************************\n";
130 echo "*** ERROR: 3rd parameter must be a valid directory. Tried to cd to [ {$argv[3]} ].\n";
135 //check if this is an instance
136 if(is_file("{$cwd}/include/entryPoint.php")) {
137 //this should be a regular sugar install
138 $upgradeType = constant('SUGARCRM_INSTALL');
139 //check if this is a valid zip file
140 if(!is_file($argv[1])) { // valid zip?
141 echo "*******************************************************************************\n";
142 echo "*** ERROR: First argument must be a full path to the patch file. Got [ {$argv[1]} ].\n";
147 if(count($argv) < 5) {
148 echo "*******************************************************************************\n";
149 echo "*** ERROR: Missing required parameters. Received ".count($argv)." argument(s), require 5.\n";
156 //this should be a regular sugar install
157 echo "*******************************************************************************\n";
158 echo "*** ERROR: Tried to execute in a non-SugarCRM root directory.\n";
162 if(isset($argv[7]) && file_exists($argv[7].'SugarTemplateUtilties.php')){
163 require_once($argv[7].'SugarTemplateUtilties.php');
169 //// END UTILITIES THAT MUST BE LOCAL :(
170 ///////////////////////////////////////////////////////////////////////////////
173 // only run from command line
174 if(isset($_SERVER['HTTP_USER_AGENT'])) {
175 fwrite(STDERR,'This utility may only be run from the command line or command prompt.');
178 //Clean_string cleans out any file passed in as a parameter
179 $_SERVER['PHP_SELF'] = 'silentUpgrade.php';
182 ///////////////////////////////////////////////////////////////////////////////
184 $usage_regular =<<<eoq2
185 Usage: php.exe -f silentUpgrade.php [upgradeZipFile] [logFile] [pathToSugarInstance] [admin-user]
187 On Command Prompt Change directory to where silentUpgrade.php resides. Then type path to
188 php.exe followed by -f silentUpgrade.php and the arguments.
191 [path-to-PHP/]php.exe -f silentUpgrade.php [path-to-upgrade-package/]SugarEnt-Upgrade-5.2.0-to-5.5.0.zip [path-to-log-file/]silentupgrade.log [path-to-sugar-instance/] admin
194 upgradeZipFile : Upgrade package file.
195 logFile : Silent Upgarde log file.
196 pathToSugarInstance : Sugar Instance instance being upgraded.
197 admin-user : admin user performing the upgrade
200 ///////////////////////////////////////////////////////////////////////////////
204 ///////////////////////////////////////////////////////////////////////////////
205 //// STANDARD REQUIRED SUGAR INCLUDES AND PRESETS
206 if(!defined('sugarEntry')) define('sugarEntry', true);
209 $_SESSION['schema_change'] = 'sugar'; // we force-run all SQL
210 $_SESSION['silent_upgrade'] = true;
211 $_SESSION['step'] = 'silent'; // flag to NOT try redirect to 4.5.x upgrade wizard
214 $_REQUEST['addTaskReminder'] = 'remind';
217 define('SUGARCRM_INSTALL', 'SugarCRM_Install');
218 define('DCE_INSTANCE', 'DCE_Instance');
221 $cwd = getcwd(); // default to current, assumed to be in a valid SugarCRM root dir.
223 $upgradeType = verifyArguments($argv,$usage_regular);
225 $path = $argv[2]; // custom log file, if blank will use ./upgradeWizard.log
226 $subdirs = array('full', 'langpack', 'module', 'patch', 'theme', 'temp');
228 require_once('include/entryPoint.php');
229 require_once('modules/UpgradeWizard/uw_utils.php');
230 require_once('include/utils/zip_utils.php');
231 require_once('include/utils/sugar_file_utils.php');
232 require_once('include/SugarObjects/SugarConfig.php');
234 global $sugar_config;
235 $isDCEInstance = false;
238 require('config.php');
239 if(isset($argv[3])) {
240 if(is_dir($argv[3])) {
246 require_once("{$cwd}/sugar_version.php"); // provides $sugar_version & $sugar_flavor
248 global $sugar_config;
249 $configOptions = $sugar_config['dbconfig'];
251 $GLOBALS['log'] = LoggerManager::getLogger('SugarCRM');
252 $patchName = basename($argv[1]);
253 $zip_from_dir = substr($patchName, 0, strlen($patchName) - 4); // patch folder name (minus ".zip")
254 $path = $argv[2]; // custom log file, if blank will use ./upgradeWizard.log
256 if($sugar_version < '5.1.0'){
257 $db = &DBManager :: getInstance();
259 $db = &DBManagerFactory::getInstance();
262 $UWstrings = return_module_language('en_us', 'UpgradeWizard');
263 $adminStrings = return_module_language('en_us', 'Administration');
264 $mod_strings = array_merge($adminStrings, $UWstrings);
265 $subdirs = array('full', 'langpack', 'module', 'patch', 'theme', 'temp');
267 $license_accepted = false;
268 if(isset($argv[5]) && (strtolower($argv[5])=='yes' || strtolower($argv[5])=='y')){
269 $license_accepted = true;
271 //////////////////////////////////////////////////////////////////////////////
272 //Adding admin user to the silent upgrade
274 $current_user = new User();
275 if(isset($argv[4])) {
276 //if being used for internal upgrades avoid admin user verification
277 $user_name = $argv[4];
278 $q = "select id from users where user_name = '" . $user_name . "' and is_admin=1";
279 $result = $GLOBALS['db']->query($q, false);
280 $logged_user = $GLOBALS['db']->fetchByAssoc($result);
281 if(isset($logged_user['id']) && $logged_user['id'] != null){
283 $current_user->retrieve($logged_user['id']);
286 echo "Not an admin user in users table. Please provide an admin user\n";
291 echo "*******************************************************************************\n";
292 echo "*** ERROR: 4th parameter must be a valid admin user.\n";
298 /////retrieve admin user
303 $unzip_dir = clean_path("{$cwd}/{$sugar_config['upload_dir']}upgrades/temp");
304 $install_file = clean_path("{$cwd}/{$sugar_config['upload_dir']}upgrades/patch/".basename($argv[1]));
306 $_SESSION['unzip_dir'] = $unzip_dir;
307 $_SESSION['install_file'] = $install_file;
308 $_SESSION['zip_from_dir'] = $zip_from_dir;
310 mkdir_recursive($unzip_dir);
311 if(!is_dir($unzip_dir)) {
312 fwrite(STDERR,"\n{$unzip_dir} is not an available directory\nFAILURE\n");
315 unzip($argv[1], $unzip_dir);
316 // mimic standard UW by copy patch zip to appropriate dir
317 copy($argv[1], $install_file);
318 //// END UPGRADE PREP
319 ///////////////////////////////////////////////////////////////////////////////
322 if(function_exists('set_upgrade_vars')){
327 if($configOptions['db_type'] == 'mysql'){
328 //Change the db wait_timeout for this session
329 $que ="select @@wait_timeout";
330 $result = $db->query($que);
331 $tb = $db->fetchByAssoc($result);
332 logThis('Wait Timeout before change ***** '.$tb['@@wait_timeout'] , $path);
333 $query ="set wait_timeout=28800";
335 $result = $db->query($que);
336 $ta = $db->fetchByAssoc($result);
337 logThis('Wait Timeout after change ***** '.$ta['@@wait_timeout'] , $path);
341 ///////////////////////////////////////////////////////////////////////////////
342 //// RUN SILENT UPGRADE
346 /// RELOAD NEW DEFINITIONS
347 global $ACLActions, $beanList, $beanFiles;
348 include('modules/ACLActions/actiondefs.php');
349 include('include/modules.php');
351 // clear out the theme cache
352 if(is_dir($GLOBALS['sugar_config']['cache_dir'].'themes')){
353 $allModFiles = array();
354 $allModFiles = findAllFiles($GLOBALS['sugar_config']['cache_dir'].'themes',$allModFiles);
355 foreach($allModFiles as $file){
356 //$file_md5_ref = str_replace(clean_path(getcwd()),'',$file);
357 if(file_exists($file)){
363 // re-minify the JS source files
364 $_REQUEST['root_directory'] = getcwd();
365 $_REQUEST['js_rebuild_concat'] = 'rebuild';
366 require_once('jssource/minify.php');
369 //First repair the databse to ensure it is up to date with the new vardefs/tabledefs
370 logThis('About to repair the database.', $path);
371 //Use Repair and rebuild to update the database.
373 require_once("modules/Administration/QuickRepairAndRebuild.php");
374 $rac = new RepairAndClear();
375 $rac->clearVardefs();
376 $rac->rebuildExtensions();
378 $repairedTables = array();
379 foreach ($beanFiles as $bean => $file) {
380 if(file_exists($file)){
381 unset($GLOBALS['dictionary'][$bean]);
383 $focus = new $bean ();
384 if(empty($focus->table_name) || isset($repairedTables[$focus->table_name])) {
388 if (($focus instanceOf SugarBean)) {
389 $sql = $db->repairTable($focus, true);
391 logThis($sql, $path);
392 $repairedTables[$focus->table_name] = true;
399 include ("{$argv[3]}/modules/TableDictionary.php");
400 foreach ($dictionary as $meta) {
401 $tablename = $meta['table'];
403 if(isset($repairedTables[$tablename])) {
407 $fielddefs = $meta['fields'];
408 $indices = $meta['indices'];
409 $sql = $GLOBALS['db']->repairTableParams($tablename, $fielddefs, $indices, true);
411 logThis($sql, $path);
412 $repairedTables[$tablename] = true;
417 logThis('database repaired', $path);
420 include("{$cwd}/{$sugar_config['upload_dir']}upgrades/temp/manifest.php");
421 $ce_to_pro_ent = isset($manifest['name']) && ($manifest['name'] == 'SugarCE to SugarPro' || $manifest['name'] == 'SugarCE to SugarEnt');
422 $origVersion = getSilentUpgradeVar('origVersion');
424 global $silent_upgrade_vars_loaded;
425 logThis("Error retrieving silent upgrade var for origVersion: cache dir is {$GLOBALS['sugar_config']['cache_dir']} -- full cache for \$silent_upgrade_vars_loaded is ".var_export($silent_upgrade_vars_loaded, true), $path);
429 if($origVersion < '550' || $ce_to_pro_ent) {
430 //add the global team if it does not exist
431 $globalteam = new Team();
432 $globalteam->retrieve('1');
433 require_once('modules/Administration/language/en_us.lang.php');
434 if(isset($globalteam->name)){
435 echo 'Global '.$mod_strings['LBL_UPGRADE_TEAM_EXISTS'].'<br>';
436 logThis(" Finish Building Global Team", $path);
438 $globalteam->create_team("Global", $mod_strings['LBL_GLOBAL_TEAM_DESC'], $globalteam->global_team);
441 logThis(" Start Building private teams", $path);
443 upgradeModulesForTeam();
444 logThis(" Finish Building private teams", $path);
446 logThis(" Start Building the team_set and team_sets_teams", $path);
447 upgradeModulesForTeamsets();
448 logThis(" Finish Building the team_set and team_sets_teams", $path);
450 logThis(" Start modules/Administration/upgradeTeams.php", $path);
451 include('modules/Administration/upgradeTeams.php');
452 logThis(" Finish modules/Administration/upgradeTeams.php", $path);
454 if($sugar_config['dbconfig']['db_type'] == 'mssql') {
456 $GLOBALS['db']->wakeupFTS();
462 if(function_exists('upgrade_connectors'))
464 upgrade_connectors($path);
467 //Unlink files that have been removed
468 if(function_exists('unlinkUpgradeFiles'))
470 unlinkUpgradeFiles($origVersion);
473 //also add the cache cleaning here.
474 if(function_exists('deleteCache')){
478 ///////////////////////////////////////////////////////////////////////////////
481 set_upgrade_progress('end','in_progress','unlinkingfiles','in_progress');
482 logThis('Taking out the trash, unlinking temp files.', $path);
483 unlinkTempFiles(true);
484 removeSilentUpgradeVarsCache();
485 logThis('Taking out the trash, done.', $path);
488 ///////////////////////////////////////////////////////////////////////////////
491 $phpErrors = ob_get_contents();
493 logThis("**** Potential PHP generated error messages: {$phpErrors}", $path);
495 if(count($errors) > 0) {
496 foreach($errors as $error) {
497 logThis("****** SilentUpgrade ERROR: {$error}", $path);
501 logThis("***** SilentUpgrade completed successfully.", $path);
502 echo "********************************************************************\n";
503 echo "*************************** SUCCESS*********************************\n";
504 echo "********************************************************************\n";
505 echo "******** If your pre-upgrade Leads data is not showing ************\n";
506 echo "******** Or you see errors in detailview subpanels ****************\n";
507 echo "************* In order to resolve them ****************************\n";
508 echo "******** Log into application as Administrator ********************\n";
509 echo "******** Go to Admin panel ****************************************\n";
510 echo "******** Run Repair -> Rebuild Relationships **********************\n";
511 echo "********************************************************************\n";