]> CyberLeo.Net >> Repos - Github/sugarcrm.git/blob - modules/Administration/QuickRepairAndRebuild.php
Release 6.4.0
[Github/sugarcrm.git] / modules / Administration / QuickRepairAndRebuild.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-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->clearExternalAPICache();
112                 $this->rebuildExtensions();
113                 $this->rebuildAuditTables();
114                 $this->repairDatabase();
115                 break;
116         }
117     }
118
119         /////////////OLD
120
121
122         public function repairDatabase()
123         {
124                 global $dictionary, $mod_strings;
125                 if(false == $this->show_output)
126                         $_REQUEST['repair_silent']='1';
127                 $_REQUEST['execute']=$this->execute;
128         $GLOBALS['reload_vardefs'] = true;
129         $hideModuleMenu = true;
130                 include_once('modules/Administration/repairDatabase.php');
131         }
132
133         public function repairDatabaseSelectModules()
134         {
135                 global $current_user, $mod_strings, $dictionary;
136                 set_time_limit(3600);
137
138                 include('include/modules.php'); //bug 15661
139                 $db = DBManagerFactory::getInstance();
140
141                 if (is_admin($current_user) || is_admin_for_any_module($current_user))
142                 {
143                         $export = false;
144                 if($this->show_output) echo getClassicModuleTitle($mod_strings['LBL_REPAIR_DATABASE'], array($mod_strings['LBL_REPAIR_DATABASE']), false);
145             if($this->show_output) {
146                 echo "<h1 id=\"rdloading\">{$mod_strings['LBL_REPAIR_DATABASE_PROCESSING']}</h1>";
147                 ob_flush();
148             }
149                 $sql = '';
150                         if($this->module_list && !in_array($mod_strings['LBL_ALL_MODULES'],$this->module_list))
151                         {
152                                 $repair_related_modules = array_keys($dictionary);
153                                 //repair DB
154                                 $dm = !empty($GLOBALS['sugar_config']['developerMode']);
155                                 $GLOBALS['sugar_config']['developerMode'] = true;
156                                 foreach($this->module_list as $bean_name)
157                                 {
158
159                                         if (isset($beanFiles[$bean_name]) && file_exists($beanFiles[$bean_name]))
160                                         {
161                                                 require_once($beanFiles[$bean_name]);
162                                                 $GLOBALS['reload_vardefs'] = true;
163                                                 $focus = new $bean_name ();
164                                                 #30273
165                                                 if($focus->disable_vardefs == false) {
166                                                         include('modules/' . $focus->module_dir . '/vardefs.php');
167
168
169                                                         if($this->show_output)
170                                                                 print_r("<p>" .$mod_strings['LBL_REPAIR_DB_FOR'].' '. $bean_name . "</p>");
171                                                         $sql .= $db->repairTable($focus, $this->execute);
172                                                 }
173                                         }
174                                 }
175
176                                 $GLOBALS['sugar_config']['developerMode'] = $dm;
177
178                         if ($this->show_output) echo "<script type=\"text/javascript\">document.getElementById('rdloading').style.display = \"none\";</script>";
179                         if (isset ($sql) && !empty ($sql))
180                         {
181                                         $qry_str = "";
182                                         foreach (explode("\n", $sql) as $line) {
183                                                 if (!empty ($line) && substr($line, -2) != "*/") {
184                                                         $line .= ";";
185                                                 }
186
187                                                 $qry_str .= $line . "\n";
188                                         }
189                                         if ($this->show_output){
190                                                 echo "<h3>{$mod_strings['LBL_REPAIR_DATABASE_DIFFERENCES']}</h3>";
191                                                 echo "<p>{$mod_strings['LBL_REPAIR_DATABASE_TEXT']}</p>";
192
193                                                 echo "<form method=\"post\" action=\"index.php?module=Administration&amp;action=repairDatabase\">";
194                                                 echo "<textarea name=\"sql\" rows=\"24\" cols=\"150\" id=\"repairsql\">$qry_str</textarea>";
195                                                 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                                         }
197                                 }
198                                 else
199                                         if ($this->show_output) echo "<h3>{$mod_strings['LBL_REPAIR_DATABASE_SYNCED']}</h3>";
200                         }
201
202                 }
203                 else {
204                         sugar_die($GLOBALS['app_strings']['ERR_NOT_ADMIN']);
205                 }
206         }
207
208         public function rebuildExtensions()
209         {
210                 global $mod_strings;
211                 if($this->show_output) echo $mod_strings['LBL_QR_REBUILDEXT'];
212                 global $current_user;
213                 require_once('ModuleInstall/ModuleInstaller.php');
214                 $mi = new ModuleInstaller();
215                 $mi->rebuild_all(!$this->show_output);
216
217                 // Remove the "Rebuild Extensions" red text message on admin logins
218
219         if($this->show_output) echo $mod_strings['LBL_REBUILD_REL_UPD_WARNING'];
220
221         // clear the database row if it exists (just to be sure)
222         $query = "DELETE FROM versions WHERE name='Rebuild Extensions'";
223         $GLOBALS['log']->info($query);
224         $GLOBALS['db']->query($query);
225
226         // insert a new database row to show the rebuild extensions is done
227         $id = create_guid();
228         $gmdate = gmdate('Y-m-d H:i:s');
229         $date_entered = db_convert("'$gmdate'", 'datetime');
230         $query = 'INSERT INTO versions (id, deleted, date_entered, date_modified, modified_user_id, created_by, name, file_version, db_version) '
231             . "VALUES ('$id', '0', $date_entered, $date_entered, '1', '1', 'Rebuild Extensions', '4.0.0', '4.0.0')";
232         $GLOBALS['log']->info($query);
233         $GLOBALS['db']->query($query);
234
235         // unset the session variable so it is not picked up in DisplayWarnings.php
236         if(isset($_SESSION['rebuild_extensions'])) {
237             unset($_SESSION['rebuild_extensions']);
238         }
239         }
240
241         //Cache Clear Methods
242         public function clearSmarty()
243         {
244                 global $mod_strings;
245                 if($this->show_output) echo "<h3>{$mod_strings['LBL_QR_CLEARSMARTY']}</h3>";
246                 $this->_clearCache(sugar_cached('smarty/templates_c'), '.tpl.php');
247         }
248         public function clearXMLfiles()
249         {
250                 global $mod_strings;
251                 if($this->show_output) echo "<h3>{$mod_strings['LBL_QR_XMLFILES']}</h3>";
252                 $this->_clearCache(sugar_cached("xml"), '.xml');
253
254                 include('modules/Versions/ExpectedVersions.php');
255
256         global $expect_versions;
257
258         if (isset($expect_versions['Chart Data Cache'])) {
259             $version = new Version();
260             $version->retrieve_by_string_fields(array('name'=>'Chart Data Cache'));
261
262             $version->name = $expect_versions['Chart Data Cache']['name'];
263             $version->file_version = $expect_versions['Chart Data Cache']['file_version'];
264             $version->db_version = $expect_versions['Chart Data Cache']['db_version'];
265             $version->save();
266         }
267         }
268         public function clearDashlets()
269         {
270                 global $mod_strings;
271                 if($this->show_output) echo "<h3>{$mod_strings['LBL_QR_CLEARDASHLET']}</h3>";
272                 $this->_clearCache(sugar_cached('dashlets'), '.php');
273         }
274     public function clearThemeCache()
275     {
276                 global $mod_strings;
277                 if($this->show_output) echo "<h3>{$mod_strings['LBL_QR_CLEARTHEMECACHE']}</h3>";
278                 SugarThemeRegistry::clearAllCaches();
279         }
280         public function clearSugarFeedCache()
281         {
282                 global $mod_strings;
283                 if($this->show_output) echo "<h3>{$mod_strings['LBL_QR_CLEARSUGARFEEDCACHE']}</h3>";
284
285         SugarFeed::flushBackendCache();
286         }
287         public function clearTpls()
288         {
289                 global $mod_strings;
290                 if($this->show_output) echo "<h3>{$mod_strings['LBL_QR_CLEARTEMPLATE']}</h3>";
291                 if(!in_array( translate('LBL_ALL_MODULES'),$this->module_list) && !empty($this->module_list))
292                 {
293                         foreach($this->module_list as $module_name_singular )
294                                 $this->_clearCache(sugar_cached('modules/').$this->_getModuleNamePlural($module_name_singular), '.tpl');
295                 }
296                 else
297                         $this->_clearCache(sugar_cached('modules/'), '.tpl');
298         }
299         public function clearVardefs()
300         {
301                 global $mod_strings;
302                 if($this->show_output) echo "<h3>{$mod_strings['LBL_QR_CLEARVADEFS']}</h3>";
303                 if(!empty($this->module_list) && is_array($this->module_list) && !in_array( translate('LBL_ALL_MODULES'),$this->module_list))
304                 {
305                         foreach($this->module_list as $module_name_singular )
306                                 $this->_clearCache(sugar_cached('modules/').$this->_getModuleNamePlural($module_name_singular), 'vardefs.php');
307                 }
308                 else
309                         $this->_clearCache(sugar_cached('modules/'), 'vardefs.php');
310         }
311         public function clearJsFiles()
312         {
313                 global $mod_strings;
314                 if($this->show_output) echo "<h3>{$mod_strings['LBL_QR_CLEARJS']}</h3>";
315
316                 if(!in_array( translate('LBL_ALL_MODULES'),$this->module_list) && !empty($this->module_list))
317                 {
318                         foreach($this->module_list as $module_name_singular )
319                                 $this->_clearCache(sugar_cached('modules/').$this->_getModuleNamePlural($module_name_singular), '.js');
320                 }
321
322                 else
323                         $this->_clearCache(sugar_cached('modules/'), '.js');
324
325         }
326         public function clearJsLangFiles()
327         {
328                 global $mod_strings;
329                 if($this->show_output) echo "<h3>{$mod_strings['LBL_QR_CLEARJSLANG']}</h3>";
330                 if(!in_array(translate('LBL_ALL_MODULES'),$this->module_list ) && !empty($this->module_list))
331                 {
332                         foreach($this->module_list as $module_name_singular )
333                                 $this->_clearCache(sugar_cached('jsLanguage/').$this->_getModuleNamePlural($module_name_singular), '.js');
334                 }
335                 else
336                         $this->_clearCache(sugar_cached('jsLanguage'), '.js');
337         }
338         /**
339          * Remove the language cache files from cache/modules/<module>/language
340          */
341         public function clearLanguageCache()
342         {
343                 global $mod_strings;
344
345                 if($this->show_output) echo "<h3>{$mod_strings['LBL_QR_CLEARLANG']}</h3>";
346                 //clear cache using the list $module_list_from_cache
347                 if ( !empty($this->module_list) && is_array($this->module_list) ) {
348             if( in_array(translate('LBL_ALL_MODULES'), $this->module_list))
349             {
350                 LanguageManager::clearLanguageCache();
351             }
352             else { //use the modules selected thrut the select list.
353                 foreach($this->module_list as $module_name)
354                     LanguageManager::clearLanguageCache($module_name);
355             }
356         }
357         }
358
359         /**
360          * Remove the cached unified_search_modules.php file
361          */
362     public function clearSearchCache() {
363         global $mod_strings, $sugar_config;
364         if($this->show_output) echo "<h3>{$mod_strings['LBL_QR_CLEARSEARCH']}</h3>";
365         $search_dir=sugar_cached('');
366         $src_file = $search_dir . 'modules/unified_search_modules.php';
367         if(file_exists($src_file)) {
368             unlink( "$src_file" );
369         }
370     }
371     public function clearExternalAPICache()
372         {
373         global $mod_strings, $sugar_config;
374         if($this->show_output) echo "<h3>{$mod_strings['LBL_QR_CLEAR_EXT_API']}</h3>";
375         require_once('include/externalAPI/ExternalAPIFactory.php');
376         ExternalAPIFactory::clearCache();
377     }
378
379         //////////////////////////////////////////////////////////////
380         /////REPAIR AUDIT TABLES
381         public function rebuildAuditTables()
382         {
383                 global $mod_strings;
384                 include('include/modules.php'); //bug 15661
385                 if($this->show_output) echo "<h3> {$mod_strings['LBL_QR_REBUILDAUDIT']}</h3>";
386
387                 if(!in_array( translate('LBL_ALL_MODULES'), $this->module_list) && !empty($this->module_list))
388                 {
389                         foreach ($this->module_list as $bean_name){
390                                 if( isset($beanFiles[$bean_name]) && file_exists($beanFiles[$bean_name])) {
391                                         require_once($beanFiles[$bean_name]);
392                                     $this->_rebuildAuditTablesHelper(new $bean_name());
393                                 }
394                         }
395                 } else if(in_array(translate('LBL_ALL_MODULES'), $this->module_list)) {
396                         foreach ($beanFiles as $bean => $file){
397                                 if( file_exists($file)) {
398                                         require_once($file);
399                                     $this->_rebuildAuditTablesHelper(new $bean());
400                                 }
401                         }
402                 }
403                 if($this->show_output) echo $mod_strings['LBL_DONE'];
404         }
405
406         private function _rebuildAuditTablesHelper($focus)
407         {
408                 global $mod_strings;
409
410                 // skip if not a SugarBean object
411                 if ( !($focus instanceOf SugarBean) )
412                     return;
413
414                 if ($focus->is_AuditEnabled()) {
415                         if (!$focus->db->tableExists($focus->get_audit_table_name())) {
416                                 if($this->show_output) echo $mod_strings['LBL_QR_CREATING_TABLE']." ".$focus->get_audit_table_name().' '.$mod_strings['LBL_FOR'].' '. $focus->object_name.'.<br/>';
417                                 $focus->create_audit_table();
418                         } else {
419                                 if($this->show_output){
420                                         $echo=str_replace('%1$',$focus->object_name,$mod_strings['LBL_REBUILD_AUDIT_SKIP']);
421                                         echo $echo;
422                                 }
423                         }
424                 }else
425                         if($this->show_output) echo $focus->object_name.$mod_strings['LBL_QR_NOT_AUDIT_ENABLED'];
426         }
427
428         ///////////////////////////////////////////////////////////////
429         ////END REPAIR AUDIT TABLES
430
431
432         ///////////////////////////////////////////////////////////////
433         //// Recursively unlink all files of the given $extension in the given $thedir.
434         //
435         private function _clearCache($thedir, $extension)
436         {
437         if ($current = @opendir($thedir)) {
438             while (false !== ($children = readdir($current))) {
439                 if ($children != "." && $children != "..") {
440                     if (is_dir($thedir . "/" . $children)) {
441                         $this->_clearCache($thedir . "/" . $children, $extension);
442                     }
443                     elseif (is_file($thedir . "/" . $children) && (substr_count($children, $extension))) {
444                         unlink($thedir . "/" . $children);
445                     }
446                 }
447             }
448         }
449         }
450         /////////////////////////////////////////////////////////////
451         ////////
452         private function _getModuleNamePlural($module_name_singular)
453         {
454                 global $beanList;
455                 while ($curr_module = current($beanList))
456                 {
457                         if ($curr_module == $module_name_singular)
458                                 return key($beanList); //name of the module, plural.
459                         next($beanList);
460                 }
461         }
462 }