]> CyberLeo.Net >> Repos - Github/sugarcrm.git/blob - modules/Administration/QuickRepairAndRebuild.php
Release 6.1.4
[Github/sugarcrm.git] / modules / Administration / QuickRepairAndRebuild.php
1 <?php
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.
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 class RepairAndClear
40 {
41     public $module_list;
42     public $show_output;
43     protected $actions;
44     public $execute;
45     protected $module_list_from_cache;
46
47     public function repairAndClearAll($selected_actions, $modules, $autoexecute=false, $show_output=true)
48     {
49         global $mod_strings;
50         $this->module_list= $modules;
51         $this->show_output = $show_output;
52         $this->actions = $selected_actions;
53         $this->actions[] = 'repairDatabase';
54         $this->execute=$autoexecute;
55
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)
62         {
63             case 'repairDatabase':
64                 if(in_array($mod_strings['LBL_ALL_MODULES'], $this->module_list))
65                     $this->repairDatabase();
66                 else
67                     $this->repairDatabaseSelectModules();
68                 break;
69             case 'rebuildExtensions':
70                 $this->rebuildExtensions();
71                 break;
72             case 'clearTpls':
73                 $this->clearTpls();
74                 break;
75             case 'clearJsFiles':
76                 $this->clearJsFiles();
77                 break;
78             case 'clearDashlets':
79                 $this->clearDashlets();
80                 break;
81             case 'clearSugarFeedCache':
82                 $this->clearSugarFeedCache();
83                 break;
84             case 'clearThemeCache':
85                 $this->clearThemeCache();
86                 break;
87             case 'clearVardefs':
88                 $this->clearVardefs();
89                 break;
90             case 'clearJsLangFiles':
91                 $this->clearJsLangFiles();
92                 break;
93             case 'rebuildAuditTables':
94                 $this->rebuildAuditTables();
95                 break;
96             case 'clearSearchCache':
97                 $this->clearSearchCache();
98                 break;
99             case 'clearAll':
100                 $this->clearTpls();
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();
114                 break;
115         }
116     }
117
118         /////////////OLD
119
120
121         public function repairDatabase()
122         {
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');
130         }
131         
132         public function repairDatabaseSelectModules()
133         {
134                 global $current_user, $mod_strings, $dictionary;
135                 set_time_limit(3600);
136                 
137                 include('include/modules.php'); //bug 15661
138                 $db = DBManagerFactory::getInstance();
139
140                 if (is_admin($current_user) || is_admin_for_any_module($current_user))
141                 {
142                         $export = false;
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>";
145             ob_flush();
146                 $sql = '';
147                         if($this->module_list && !in_array($mod_strings['LBL_ALL_MODULES'],$this->module_list))
148                         {
149                                 $repair_related_modules = array_keys($dictionary);
150                                 //repair DB
151                                 $dm = !empty($GLOBALS['sugar_config']['developerMode']);
152                                 $GLOBALS['sugar_config']['developerMode'] = true;
153                                 foreach($this->module_list as $bean_name)
154                                 {
155
156                                         if (isset($beanFiles[$bean_name]) && file_exists($beanFiles[$bean_name]))
157                                         {
158                                                 require_once($beanFiles[$bean_name]);
159                                                 $GLOBALS['reload_vardefs'] = true;
160                                                 $focus = new $bean_name ();
161                                                 #30273  
162                                                 if($focus->disable_vardefs == false) {
163                                                         include('modules/' . $focus->module_dir . '/vardefs.php');
164         
165         
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);
169                                                 }
170                                         }
171                                 }
172                                 
173                                 $GLOBALS['sugar_config']['developerMode'] = $dm;
174                                 
175                         if ($this->show_output) echo "<script type=\"text/javascript\">document.getElementById('rdloading').style.display = \"none\";</script>";
176                         if (isset ($sql) && !empty ($sql))
177                         {
178                                         $qry_str = "";
179                                         foreach (explode("\n", $sql) as $line) {
180                                                 if (!empty ($line) && substr($line, -2) != "*/") {
181                                                         $line .= ";";
182                                                 }
183
184                                                 $qry_str .= $line . "\n";
185                                         }
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>";
189
190                                                 echo "<form method=\"post\" action=\"index.php?module=Administration&amp;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']."\" />";
193                                         }
194                                 }
195                                 else
196                                         if ($this->show_output) echo "<h3>{$mod_strings['LBL_REPAIR_DATABASE_SYNCED']}</h3>";
197                         }
198
199                 }
200                 else {
201                         sugar_die($GLOBALS['app_strings']['ERR_NOT_ADMIN']);
202                 } 
203         }
204         
205         public function rebuildExtensions()
206         {
207                 global $mod_strings;
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);
213                 
214                 // Remove the "Rebuild Extensions" red text message on admin logins
215         
216         if($this->show_output) echo $mod_strings['LBL_REBUILD_REL_UPD_WARNING'];
217                        
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);
222         
223         // insert a new database row to show the rebuild extensions is done
224         $id = create_guid();
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);
231         
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']);
235         }
236         }
237         
238         //Cache Clear Methods
239         public function clearSmarty()
240         {
241                 global $mod_strings;
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');
244         }
245         public function clearXMLfiles()
246         {
247                 global $mod_strings;
248                 if($this->show_output) echo "<h3>{$mod_strings['LBL_QR_XMLFILES']}</h3>";
249                 $this->_clearCache($GLOBALS['sugar_config']['tmp_dir'], '.xml');
250                 
251                 include('modules/Versions/ExpectedVersions.php');
252                 
253         global $expect_versions;
254         
255         if (isset($expect_versions['Chart Data Cache'])) {
256             $version = new Version();
257             $version->retrieve_by_string_fields(array('name'=>'Chart Data Cache'));
258         
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'];
262             $version->save();
263         }
264         }
265         public function clearDashlets()
266         {
267                 global $mod_strings;
268                 if($this->show_output) echo "<h3>{$mod_strings['LBL_QR_CLEARDASHLET']}</h3>";
269                 $this->_clearCache($GLOBALS['sugar_config']['cache_dir'].'dashlets', '.php');
270         }
271     public function clearThemeCache()
272     {
273                 global $mod_strings;
274                 if($this->show_output) echo "<h3>{$mod_strings['LBL_QR_CLEARTHEMECACHE']}</h3>";
275                 SugarThemeRegistry::clearAllCaches();
276         }
277         public function clearSugarFeedCache()
278         {
279                 global $mod_strings;
280                 if($this->show_output) echo "<h3>{$mod_strings['LBL_QR_CLEARSUGARFEEDCACHE']}</h3>";
281         
282         SugarFeed::flushBackendCache();
283         }
284         public function clearTpls() 
285         {
286                 global $mod_strings;
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))
289                 {
290                         foreach($this->module_list as $module_name_singular )
291                                 $this->_clearCache($GLOBALS['sugar_config']['cache_dir'].'modules/'.$this->_getModuleNamePlural($module_name_singular), '.tpl');
292                 }
293                 else
294                         $this->_clearCache($GLOBALS['sugar_config']['cache_dir'].'modules', '.tpl');
295         }
296         public function clearVardefs() 
297         {
298                 global $mod_strings;
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))
301                 {
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');
304                 }
305                 else
306                         $this->_clearCache($GLOBALS['sugar_config']['cache_dir'].'modules', 'vardefs.php');
307         }
308         public function clearJsFiles() 
309         {
310                 global $mod_strings;
311                 if($this->show_output) echo "<h3>{$mod_strings['LBL_QR_CLEARJS']}</h3>";
312
313                 if(!in_array( translate('LBL_ALL_MODULES'),$this->module_list) && !empty($this->module_list))
314                 {
315                         foreach($this->module_list as $module_name_singular )
316                                 $this->_clearCache($GLOBALS['sugar_config']['cache_dir'].'modules/'.$this->_getModuleNamePlural($module_name_singular), '.js');
317                 }
318
319                 else
320                         $this->_clearCache($GLOBALS['sugar_config']['cache_dir'].'modules', '.js');
321
322         }
323         public function clearJsLangFiles() 
324         {
325                 global $mod_strings;
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))
328                 {
329                         foreach($this->module_list as $module_name_singular )
330                                 $this->_clearCache($GLOBALS['sugar_config']['cache_dir'].'jsLanguage/'.$this->_getModuleNamePlural($module_name_singular), '.js');
331                 }
332                 else
333                         $this->_clearCache($GLOBALS['sugar_config']['cache_dir'].'jsLanguage', '.js');
334         }
335         /**
336          * Remove the language cache files from cache/modules/<module>/language
337          */
338         public function clearLanguageCache() 
339         {
340                 global $mod_strings;
341                 
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))
346             {
347                 LanguageManager::clearLanguageCache();
348             }
349             else { //use the modules selected thrut the select list.
350                 foreach($this->module_list as $module_name)
351                     LanguageManager::clearLanguageCache($module_name);
352             }
353         }
354         }
355
356         /**
357          * Remove the cache/modules/unified_search_modules.php
358          */
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'];
365         }
366         $src_file = $search_dir . 'modules/unified_search_modules.php';
367         if(file_exists($src_file)) {
368             unlink( "$src_file" );
369         }
370         
371     }
372     
373         //////////////////////////////////////////////////////////////
374         /////REPAIR AUDIT TABLES
375         public function rebuildAuditTables()
376         {
377                 global $mod_strings;
378                 include('include/modules.php'); //bug 15661
379                 if($this->show_output) echo "<h3> {$mod_strings['LBL_QR_REBUILDAUDIT']}</h3>";
380
381                 if(!in_array( translate('LBL_ALL_MODULES'), $this->module_list) && !empty($this->module_list))
382                 {
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());
387                                 }
388                         }
389                 } else if(in_array(translate('LBL_ALL_MODULES'), $this->module_list)) {
390                         foreach ($beanFiles as $bean => $file){
391                                 if( file_exists($file)) {
392                                         require_once($file);
393                                     $this->_rebuildAuditTablesHelper(new $bean());
394                                 }
395                         }
396                 }
397                 if($this->show_output) echo $mod_strings['LBL_DONE'];
398         }
399
400         private function _rebuildAuditTablesHelper($focus)
401         {
402                 global $mod_strings;
403                 
404                 // skip if not a SugarBean object
405                 if ( !($focus instanceOf SugarBean) )
406                     return;
407                 
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();
412                         } else {
413                                 if($this->show_output){
414                                         $echo=str_replace('%1$',$focus->object_name,$mod_strings['LBL_REBUILD_AUDIT_SKIP']);
415                                         echo $echo;
416                                 }
417                         }
418                 }else
419                         if($this->show_output) echo $focus->object_name.$mod_strings['LBL_QR_NOT_AUDIT_ENABLED'];
420         }
421
422         ///////////////////////////////////////////////////////////////
423         ////END REPAIR AUDIT TABLES
424
425
426         ///////////////////////////////////////////////////////////////
427         //// Recursively unlink all files of the given $extension in the given $thedir.
428         //
429         private function _clearCache($thedir, $extension) 
430         {
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);
436                     }
437                     elseif (is_file($thedir . "/" . $children) && (substr_count($children, $extension))) {
438                         unlink($thedir . "/" . $children);
439                     }
440                 }
441             }
442         }
443         }
444         /////////////////////////////////////////////////////////////
445         ////////
446         private function _getModuleNamePlural($module_name_singular)
447         {
448                 global $beanList;
449                 while ($curr_module = current($beanList))
450                 {
451                         if ($curr_module == $module_name_singular)
452                                 return key($beanList); //name of the module, plural.
453                         next($beanList);
454                 }
455         }
456 }