2 if(!defined('sugarEntry') || !sugarEntry) die('Not A Valid Entry Point');
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 ********************************************************************************/
45 protected $module_list_from_cache;
47 public function repairAndClearAll($selected_actions, $modules, $autoexecute=false, $show_output=true)
50 $this->module_list= $modules;
51 $this->show_output = $show_output;
52 $this->actions = $selected_actions;
53 $this->actions[] = 'repairDatabase';
54 $this->execute=$autoexecute;
56 //clear vardefs always..
57 $this->clearVardefs();
58 //first clear the language cache.
59 $this->clearLanguageCache();
60 foreach ($this->actions as $current_action)
61 switch($current_action)
63 case 'repairDatabase':
64 if(in_array($mod_strings['LBL_ALL_MODULES'], $this->module_list))
65 $this->repairDatabase();
67 $this->repairDatabaseSelectModules();
69 case 'rebuildExtensions':
70 $this->rebuildExtensions();
76 $this->clearJsFiles();
79 $this->clearDashlets();
81 case 'clearSugarFeedCache':
82 $this->clearSugarFeedCache();
84 case 'clearThemeCache':
85 $this->clearThemeCache();
88 $this->clearVardefs();
90 case 'clearJsLangFiles':
91 $this->clearJsLangFiles();
93 case 'rebuildAuditTables':
94 $this->rebuildAuditTables();
96 case 'clearSearchCache':
97 $this->clearSearchCache();
101 $this->clearJsFiles();
102 $this->clearVardefs();
103 $this->clearJsLangFiles();
104 $this->clearLanguageCache();
105 $this->clearDashlets();
106 $this->clearSugarFeedCache();
107 $this->clearSmarty();
108 $this->clearThemeCache();
109 $this->clearXMLfiles();
110 $this->clearSearchCache();
111 $this->rebuildExtensions();
112 $this->rebuildAuditTables();
113 $this->repairDatabase();
121 public function repairDatabase()
123 global $dictionary, $mod_strings;
124 if(false == $this->show_output)
125 $_REQUEST['repair_silent']='1';
126 $_REQUEST['execute']=$this->execute;
127 $GLOBALS['reload_vardefs'] = true;
128 $hideModuleMenu = true;
129 include_once('modules/Administration/repairDatabase.php');
132 public function repairDatabaseSelectModules()
134 global $current_user, $mod_strings, $dictionary;
135 set_time_limit(3600);
137 include('include/modules.php'); //bug 15661
138 $db = DBManagerFactory::getInstance();
140 if (is_admin($current_user) || is_admin_for_any_module($current_user))
143 if($this->show_output) echo get_module_title($mod_strings['LBL_REPAIR_DATABASE'], $mod_strings['LBL_REPAIR_DATABASE'], true);
144 if($this->show_output) echo "<h1 id=\"rdloading\">{$mod_strings['LBL_REPAIR_DATABASE_PROCESSING']}</h1>";
147 if($this->module_list && !in_array($mod_strings['LBL_ALL_MODULES'],$this->module_list))
149 $repair_related_modules = array_keys($dictionary);
151 $dm = !empty($GLOBALS['sugar_config']['developerMode']);
152 $GLOBALS['sugar_config']['developerMode'] = true;
153 foreach($this->module_list as $bean_name)
156 if (isset($beanFiles[$bean_name]) && file_exists($beanFiles[$bean_name]))
158 require_once($beanFiles[$bean_name]);
159 $GLOBALS['reload_vardefs'] = true;
160 $focus = new $bean_name ();
162 if($focus->disable_vardefs == false) {
163 include('modules/' . $focus->module_dir . '/vardefs.php');
166 if($this->show_output)
167 print_r("<p>" .$mod_strings['LBL_REPAIR_DB_FOR'].' '. $bean_name . "</p>");
168 $sql .= $db->repairTable($focus, $this->execute);
173 $GLOBALS['sugar_config']['developerMode'] = $dm;
175 if ($this->show_output) echo "<script type=\"text/javascript\">document.getElementById('rdloading').style.display = \"none\";</script>";
176 if (isset ($sql) && !empty ($sql))
179 foreach (explode("\n", $sql) as $line) {
180 if (!empty ($line) && substr($line, -2) != "*/") {
184 $qry_str .= $line . "\n";
186 if ($this->show_output){
187 echo "<h3>{$mod_strings['LBL_REPAIR_DATABASE_DIFFERENCES']}</h3>";
188 echo "<p>{$mod_strings['LBL_REPAIR_DATABASE_TEXT']}</p>";
190 echo "<form method=\"post\" action=\"index.php?module=Administration&action=repairDatabase\">";
191 echo "<textarea name=\"sql\" rows=\"24\" cols=\"150\" id=\"repairsql\">$qry_str</textarea>";
192 echo "<br /><input type=\"submit\" value=\"".$mod_strings['LBL_REPAIR_DATABASE_EXECUTE']."\" name=\"raction\" /> <input type=\"submit\" name=\"raction\" value=\"".$mod_strings['LBL_REPAIR_DATABASE_EXPORT']."\" />";
196 if ($this->show_output) echo "<h3>{$mod_strings['LBL_REPAIR_DATABASE_SYNCED']}</h3>";
201 sugar_die($GLOBALS['app_strings']['ERR_NOT_ADMIN']);
205 public function rebuildExtensions()
208 if($this->show_output) echo $mod_strings['LBL_QR_REBUILDEXT'];
209 global $current_user;
210 require_once('ModuleInstall/ModuleInstaller.php');
211 $mi = new ModuleInstaller();
212 $mi->rebuild_all(!$this->show_output);
214 // Remove the "Rebuild Extensions" red text message on admin logins
216 if($this->show_output) echo $mod_strings['LBL_REBUILD_REL_UPD_WARNING'];
218 // clear the database row if it exists (just to be sure)
219 $query = "DELETE FROM versions WHERE name='Rebuild Extensions'";
220 $GLOBALS['log']->info($query);
221 $GLOBALS['db']->query($query);
223 // insert a new database row to show the rebuild extensions is done
225 $gmdate = gmdate($GLOBALS['timedate']->get_db_date_time_format());
226 $date_entered = db_convert("'$gmdate'", 'datetime');
227 $query = 'INSERT INTO versions (id, deleted, date_entered, date_modified, modified_user_id, created_by, name, file_version, db_version) '
228 . "VALUES ('$id', '0', $date_entered, $date_entered, '1', '1', 'Rebuild Extensions', '4.0.0', '4.0.0')";
229 $GLOBALS['log']->info($query);
230 $GLOBALS['db']->query($query);
232 // unset the session variable so it is not picked up in DisplayWarnings.php
233 if(isset($_SESSION['rebuild_extensions'])) {
234 unset($_SESSION['rebuild_extensions']);
238 //Cache Clear Methods
239 public function clearSmarty()
242 if($this->show_output) echo "<h3>{$mod_strings['LBL_QR_CLEARSMARTY']}</h3>";
243 $this->_clearCache($GLOBALS['sugar_config']['cache_dir'].'smarty/templates_c', '.tpl.php');
245 public function clearXMLfiles()
248 if($this->show_output) echo "<h3>{$mod_strings['LBL_QR_XMLFILES']}</h3>";
249 $this->_clearCache($GLOBALS['sugar_config']['tmp_dir'], '.xml');
251 include('modules/Versions/ExpectedVersions.php');
253 global $expect_versions;
255 if (isset($expect_versions['Chart Data Cache'])) {
256 $version = new Version();
257 $version->retrieve_by_string_fields(array('name'=>'Chart Data Cache'));
259 $version->name = $expect_versions['Chart Data Cache']['name'];
260 $version->file_version = $expect_versions['Chart Data Cache']['file_version'];
261 $version->db_version = $expect_versions['Chart Data Cache']['db_version'];
265 public function clearDashlets()
268 if($this->show_output) echo "<h3>{$mod_strings['LBL_QR_CLEARDASHLET']}</h3>";
269 $this->_clearCache($GLOBALS['sugar_config']['cache_dir'].'dashlets', '.php');
271 public function clearThemeCache()
274 if($this->show_output) echo "<h3>{$mod_strings['LBL_QR_CLEARTHEMECACHE']}</h3>";
275 SugarThemeRegistry::clearAllCaches();
277 public function clearSugarFeedCache()
280 if($this->show_output) echo "<h3>{$mod_strings['LBL_QR_CLEARSUGARFEEDCACHE']}</h3>";
282 SugarFeed::flushBackendCache();
284 public function clearTpls()
287 if($this->show_output) echo "<h3>{$mod_strings['LBL_QR_CLEARTEMPLATE']}</h3>";
288 if(!in_array( translate('LBL_ALL_MODULES'),$this->module_list) && !empty($this->module_list))
290 foreach($this->module_list as $module_name_singular )
291 $this->_clearCache($GLOBALS['sugar_config']['cache_dir'].'modules/'.$this->_getModuleNamePlural($module_name_singular), '.tpl');
294 $this->_clearCache($GLOBALS['sugar_config']['cache_dir'].'modules', '.tpl');
296 public function clearVardefs()
299 if($this->show_output) echo "<h3>{$mod_strings['LBL_QR_CLEARVADEFS']}</h3>";
300 if(!empty($this->module_list) && is_array($this->module_list) && !in_array( translate('LBL_ALL_MODULES'),$this->module_list))
302 foreach($this->module_list as $module_name_singular )
303 $this->_clearCache($GLOBALS['sugar_config']['cache_dir'].'modules/'.$this->_getModuleNamePlural($module_name_singular), 'vardefs.php');
306 $this->_clearCache($GLOBALS['sugar_config']['cache_dir'].'modules', 'vardefs.php');
308 public function clearJsFiles()
311 if($this->show_output) echo "<h3>{$mod_strings['LBL_QR_CLEARJS']}</h3>";
313 if(!in_array( translate('LBL_ALL_MODULES'),$this->module_list) && !empty($this->module_list))
315 foreach($this->module_list as $module_name_singular )
316 $this->_clearCache($GLOBALS['sugar_config']['cache_dir'].'modules/'.$this->_getModuleNamePlural($module_name_singular), '.js');
320 $this->_clearCache($GLOBALS['sugar_config']['cache_dir'].'modules', '.js');
323 public function clearJsLangFiles()
326 if($this->show_output) echo "<h3>{$mod_strings['LBL_QR_CLEARJSLANG']}</h3>";
327 if(!in_array(translate('LBL_ALL_MODULES'),$this->module_list ) && !empty($this->module_list))
329 foreach($this->module_list as $module_name_singular )
330 $this->_clearCache($GLOBALS['sugar_config']['cache_dir'].'jsLanguage/'.$this->_getModuleNamePlural($module_name_singular), '.js');
333 $this->_clearCache($GLOBALS['sugar_config']['cache_dir'].'jsLanguage', '.js');
336 * Remove the language cache files from cache/modules/<module>/language
338 public function clearLanguageCache()
342 if($this->show_output) echo "<h3>{$mod_strings['LBL_QR_CLEARLANG']}</h3>";
343 //clear cache using the list $module_list_from_cache
344 if ( !empty($this->module_list) && is_array($this->module_list) ) {
345 if( in_array(translate('LBL_ALL_MODULES'), $this->module_list))
347 LanguageManager::clearLanguageCache();
349 else { //use the modules selected thrut the select list.
350 foreach($this->module_list as $module_name)
351 LanguageManager::clearLanguageCache($module_name);
357 * Remove the cache/modules/unified_search_modules.php
359 public function clearSearchCache() {
360 global $mod_strings, $sugar_config;
361 if($this->show_output) echo "<h3>{$mod_strings['LBL_QR_CLEARSEARCH']}</h3>";
362 $search_dir='cache/';
363 if (!empty($sugar_config['cache_dir'])) {
364 $search_dir=$sugar_config['cache_dir'];
366 $src_file = $search_dir . 'modules/unified_search_modules.php';
367 if(file_exists($src_file)) {
368 unlink( "$src_file" );
373 //////////////////////////////////////////////////////////////
374 /////REPAIR AUDIT TABLES
375 public function rebuildAuditTables()
378 include('include/modules.php'); //bug 15661
379 if($this->show_output) echo "<h3> {$mod_strings['LBL_QR_REBUILDAUDIT']}</h3>";
381 if(!in_array( translate('LBL_ALL_MODULES'), $this->module_list) && !empty($this->module_list))
383 foreach ($this->module_list as $bean_name){
384 if( isset($beanFiles[$bean_name]) && file_exists($beanFiles[$bean_name])) {
385 require_once($beanFiles[$bean_name]);
386 $this->_rebuildAuditTablesHelper(new $bean_name());
389 } else if(in_array(translate('LBL_ALL_MODULES'), $this->module_list)) {
390 foreach ($beanFiles as $bean => $file){
391 if( file_exists($file)) {
393 $this->_rebuildAuditTablesHelper(new $bean());
397 if($this->show_output) echo $mod_strings['LBL_DONE'];
400 private function _rebuildAuditTablesHelper($focus)
404 // skip if not a SugarBean object
405 if ( !($focus instanceOf SugarBean) )
408 if ($focus->is_AuditEnabled()) {
409 if (!$focus->db->tableExists($focus->get_audit_table_name())) {
410 if($this->show_output) echo $mod_strings['LBL_QR_CREATING_TABLE']." ".$focus->get_audit_table_name().' '.$mod_strings['LBL_FOR'].' '. $focus->object_name.'.<br/>';
411 $focus->create_audit_table();
413 if($this->show_output){
414 $echo=str_replace('%1$',$focus->object_name,$mod_strings['LBL_REBUILD_AUDIT_SKIP']);
419 if($this->show_output) echo $focus->object_name.$mod_strings['LBL_QR_NOT_AUDIT_ENABLED'];
422 ///////////////////////////////////////////////////////////////
423 ////END REPAIR AUDIT TABLES
426 ///////////////////////////////////////////////////////////////
427 //// Recursively unlink all files of the given $extension in the given $thedir.
429 private function _clearCache($thedir, $extension)
431 if ($current = @opendir($thedir)) {
432 while (false !== ($children = readdir($current))) {
433 if ($children != "." && $children != "..") {
434 if (is_dir($thedir . "/" . $children)) {
435 $this->_clearCache($thedir . "/" . $children, $extension);
437 elseif (is_file($thedir . "/" . $children) && (substr_count($children, $extension))) {
438 unlink($thedir . "/" . $children);
444 /////////////////////////////////////////////////////////////
446 private function _getModuleNamePlural($module_name_singular)
449 while ($curr_module = current($beanList))
451 if ($curr_module == $module_name_singular)
452 return key($beanList); //name of the module, plural.