]> CyberLeo.Net >> Repos - Github/sugarcrm.git/blob - modules/Administration/repairDatabase.php
Release 6.5.0
[Github/sugarcrm.git] / modules / Administration / repairDatabase.php
1 <?php
2 if (!defined('sugarEntry') || !sugarEntry)
3         die('Not A Valid Entry Point');
4 /*********************************************************************************
5  * SugarCRM Community Edition is a customer relationship management program developed by
6  * SugarCRM, Inc. Copyright (C) 2004-2012 SugarCRM Inc.
7  * 
8  * This program is free software; you can redistribute it and/or modify it under
9  * the terms of the GNU Affero General Public License version 3 as published by the
10  * Free Software Foundation with the addition of the following permission added
11  * to Section 15 as permitted in Section 7(a): FOR ANY PART OF THE COVERED WORK
12  * IN WHICH THE COPYRIGHT IS OWNED BY SUGARCRM, SUGARCRM DISCLAIMS THE WARRANTY
13  * OF NON INFRINGEMENT OF THIRD PARTY RIGHTS.
14  * 
15  * This program is distributed in the hope that it will be useful, but WITHOUT
16  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
17  * FOR A PARTICULAR PURPOSE.  See the GNU Affero General Public License for more
18  * details.
19  * 
20  * You should have received a copy of the GNU Affero General Public License along with
21  * this program; if not, see http://www.gnu.org/licenses or write to the Free
22  * Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
23  * 02110-1301 USA.
24  * 
25  * You can contact SugarCRM, Inc. headquarters at 10050 North Wolfe Road,
26  * SW2-130, Cupertino, CA 95014, USA. or at email address contact@sugarcrm.com.
27  * 
28  * The interactive user interfaces in modified source and object code versions
29  * of this program must display Appropriate Legal Notices, as required under
30  * Section 5 of the GNU Affero General Public License version 3.
31  * 
32  * In accordance with Section 7(b) of the GNU Affero General Public License version 3,
33  * these Appropriate Legal Notices must retain the display of the "Powered by
34  * SugarCRM" logo. If the display of the logo is not reasonably feasible for
35  * technical reasons, the Appropriate Legal Notices must display the words
36  * "Powered by SugarCRM".
37  ********************************************************************************/
38
39
40 global $current_user, $beanFiles;
41 set_time_limit(3600);
42
43
44 $db = DBManagerFactory::getInstance();
45
46 if (is_admin($current_user) || isset ($from_sync_client) || is_admin_for_any_module($current_user)) {
47         isset($_REQUEST['execute'])? $execute=$_REQUEST['execute'] : $execute= false;
48         $export = false;
49
50         if (sizeof($_POST) && isset ($_POST['raction'])) {
51                 if (isset ($_POST['raction']) && strtolower($_POST['raction']) == "export") {
52                         //jc - output buffering is being used. if we do not clean the output buffer
53                         //the contents of the buffer up to the length of the repair statement(s)
54                         //will be saved in the file...
55                         ob_clean();
56
57                         header("Content-Disposition: attachment; filename=repairSugarDB.sql");
58                         header("Content-Type: text/sql; charset={$app_strings['LBL_CHARSET']}");
59                         header("Expires: Mon, 26 Jul 1997 05:00:00 GMT");
60                         header("Last-Modified: " . TimeDate::httpTime());
61                         header("Cache-Control: post-check=0, pre-check=0", false);
62                         header("Content-Length: " . strlen($_POST['sql']));
63
64                     //jc:7347 - for whatever reason, html_entity_decode is choking on converting
65                     //the html entity &#039; to a single quote, so we will use str_replace
66                     //instead
67                     $sql = str_replace('&#039;',"'", $_POST['sql']);
68                     //echo html_entity_decode($_POST['sql']);
69                     echo $sql;
70                 }
71                 elseif (isset ($_POST['raction']) && strtolower($_POST['raction']) == "execute") {
72                         $sql = str_replace(
73                                 array(
74                                         "\n",
75                                         '&#039;',
76                                 ),
77                                 array(
78                                         '',
79                                         "'",
80                                 ),
81                                 preg_replace('#(/\*.+?\*/\n*)#', '', $_POST['sql'])
82                         );
83                         foreach (explode(";", $sql) as $stmt) {
84                                 $stmt = trim($stmt);
85
86                                 if (!empty ($stmt)) {
87                                         $db->query($stmt,true,'Executing repair query: ');
88                                 }
89                         }
90
91                         echo "<h3>{$mod_strings['LBL_REPAIR_DATABASE_SYNCED']}</h3>";
92                 }
93         } else {
94
95                 if (!$export && empty ($_REQUEST['repair_silent'])) {
96                         if ( empty($hideModuleMenu) )
97                         echo getClassicModuleTitle($mod_strings['LBL_REPAIR_DATABASE'], array($mod_strings['LBL_REPAIR_DATABASE']), true);
98                         echo "<h1 id=\"rdloading\">{$mod_strings['LBL_REPAIR_DATABASE_PROCESSING']}</h1>";
99                         ob_flush();
100                 }
101
102                 $sql = '';
103
104                 VardefManager::clearVardef();
105                 $repairedTables = array();
106
107                 foreach ($beanFiles as $bean => $file) {
108                         if(file_exists($file)){
109                                 require_once ($file);
110                                 unset($GLOBALS['dictionary'][$bean]);
111                                 $focus = new $bean ();
112                                 if (($focus instanceOf SugarBean) && !isset($repairedTables[$focus->table_name])) {
113                                     $sql .= $db->repairTable($focus, $execute);
114                                     $repairedTables[$focus->table_name] = true;
115                                 }
116                 //Repair Custom Fields
117                 if (($focus instanceOf SugarBean) && $focus->hasCustomFields() && !isset($repairedTables[$focus->table_name . '_cstm'])) {
118                                     $df = new DynamicField($focus->module_dir);
119                     //Need to check if the method exists as during upgrade an old version of Dynamic Fields may be loaded.
120                     if (method_exists($df, "repairCustomFields"))
121                     {
122                         $df->bean = $focus;
123                         $sql .= $df->repairCustomFields($execute);
124                         $repairedTables[$focus->table_name . '_cstm'] = true;
125                     }
126                                 }
127                         }
128                 }
129
130                 $olddictionary = $dictionary;
131
132                 unset ($dictionary);
133                 include ('modules/TableDictionary.php');
134
135                 foreach ($dictionary as $meta) {
136
137                         if ( !isset($meta['table']) || isset($repairedTables[$meta['table']]))
138                 continue;
139             
140             $tablename = $meta['table'];
141                         $fielddefs = $meta['fields'];
142                         $indices = $meta['indices'];
143                         $engine = isset($meta['engine'])?$meta['engine']:null;
144                         $sql .= $db->repairTableParams($tablename, $fielddefs, $indices, $execute, $engine);
145                         $repairedTables[$tablename] = true;
146                 }
147
148                 $dictionary = $olddictionary;
149
150
151
152                 if (empty ($_REQUEST['repair_silent'])) {
153                         echo "<script type=\"text/javascript\">document.getElementById('rdloading').style.display = \"none\";</script>";
154
155                         if (isset ($sql) && !empty ($sql)) {
156
157                                 $qry_str = "";
158                                 foreach (explode("\n", $sql) as $line) {
159                                         if (!empty ($line) && substr($line, -2) != "*/") {
160                                                 $line .= ";";
161                                         }
162
163                                         $qry_str .= $line . "\n";
164                                 }
165
166                     $ss = new Sugar_Smarty();
167                     $ss->assign('MOD', $GLOBALS['mod_strings']);
168                     $ss->assign('qry_str', $qry_str);
169                     echo $ss->fetch('modules/Administration/templates/RepairDatabase.tpl');
170                         } else {
171                                 echo "<h3>{$mod_strings['LBL_REPAIR_DATABASE_SYNCED']}</h3>";
172                         }
173                 }
174         }
175
176 } else {
177         sugar_die($GLOBALS['app_strings']['ERR_NOT_ADMIN']);
178 }