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.
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.
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
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
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.
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.
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 ********************************************************************************/
40 global $current_user, $beanFiles;
44 $db = DBManagerFactory::getInstance();
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;
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...
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']));
64 //jc:7347 - for whatever reason, html_entity_decode is choking on converting
65 //the html entity ' to a single quote, so we will use str_replace
67 $sql = str_replace(''',"'", $_POST['sql']);
68 //echo html_entity_decode($_POST['sql']);
71 elseif (isset ($_POST['raction']) && strtolower($_POST['raction']) == "execute") {
81 preg_replace('#(/\*.+?\*/\n*)#', '', $_POST['sql'])
83 foreach (explode(";", $sql) as $stmt) {
87 $db->query($stmt,true,'Executing repair query: ');
91 echo "<h3>{$mod_strings['LBL_REPAIR_DATABASE_SYNCED']}</h3>";
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>";
104 VardefManager::clearVardef();
105 $repairedTables = array();
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;
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"))
123 $sql .= $df->repairCustomFields($execute);
124 $repairedTables[$focus->table_name . '_cstm'] = true;
130 $olddictionary = $dictionary;
133 include ('modules/TableDictionary.php');
135 foreach ($dictionary as $meta) {
137 if ( !isset($meta['table']) || isset($repairedTables[$meta['table']]))
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;
148 $dictionary = $olddictionary;
152 if (empty ($_REQUEST['repair_silent'])) {
153 echo "<script type=\"text/javascript\">document.getElementById('rdloading').style.display = \"none\";</script>";
155 if (isset ($sql) && !empty ($sql)) {
158 foreach (explode("\n", $sql) as $line) {
159 if (!empty ($line) && substr($line, -2) != "*/") {
163 $qry_str .= $line . "\n";
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');
171 echo "<h3>{$mod_strings['LBL_REPAIR_DATABASE_SYNCED']}</h3>";
177 sugar_die($GLOBALS['app_strings']['ERR_NOT_ADMIN']);