]> CyberLeo.Net >> Repos - Github/sugarcrm.git/blob - modules/Administration/expandDatabase.php
Release 6.4.0
[Github/sugarcrm.git] / modules / Administration / expandDatabase.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 $db = DBManagerFactory::getInstance();
39 if(!$db->supports('fix:expandDatabase')) {
40         echo "<BR>";
41         echo "<p>".$mod_strings['ERR_NOT_IMPLEMENTED']."</p>";
42         echo "<BR>";
43         sugar_die('');
44 }
45 global $current_user,$beanFiles;
46
47 set_time_limit(3600);
48 if(is_admin($current_user) || isset($from_sync_client)){
49
50         $execute = false;
51         $export = false;
52
53
54         if(isset($_REQUEST['do_action'])){
55                 switch($_REQUEST['do_action']){
56                         case 'display':
57                                 break;
58                         case 'execute':
59                                 $execute = true;
60                                 break;
61                         case 'export':
62                                 header('Location: index.php?module=Administration&action=expandDatabase&do_action=do_export&to_pdf=true');
63                                 die();
64                         case 'do_export':
65                                 $export = true;
66                                 break;
67                 }
68
69                 if(!$export && empty($_REQUEST['repair_silent'])){
70                         echo getClassicModuleTitle($mod_strings['LBL_EXPAND_DATABASE_COLUMNS'], array($mod_strings['LBL_EXPAND_DATABASE_COLUMNS'],$_REQUEST['do_action']), true);
71                 }
72
73         $alter_queries = array();
74         $restore_quries = array();
75         $sql = "SELECT SO.name AS table_name, SC.name AS column_name, CONVERT(int, SC.length) AS length, SC.isnullable, type_name(SC.xusertype) AS type
76                 FROM sys.sysobjects AS SO INNER JOIN sys.syscolumns AS SC ON SC.id = SO.id
77                 WHERE (SO.type = 'U')
78                 AND (type_name(SC.xusertype) IN ('varchar', 'char', ' text '))
79                 AND (SC.name NOT LIKE '%_id') AND (SC.name NOT LIKE 'id_%') AND (SC.name <> 'id')
80                 ORDER BY SO.name, column_name";
81         $result = $db->query($sql);
82
83
84         $theAlterQueries = '';
85         $theRestoreQueries = '';
86         $alter_queries = array();
87         while ($row = $db->fetchByAssoc($result)) {
88               $length = (int)$row['length'];
89               if($length < 255) {
90                  $newLength = ($length * 3 < 255) ? $length * 3 : 255;
91                  $sql = 'ALTER TABLE ' . $row['table_name'] . ' ALTER COLUMN ' . $row['column_name'] . ' ' . $row['type'] . ' (' . $newLength . ')';
92              $theAlterQueries .= $sql . "\n";
93              $alter_queries[] = $sql;
94
95              $sql2 = 'ALTER TABLE ' . $row['table_name'] . ' ALTER COLUMN ' . $row['column_name'] . ' ' . $row['type'] . ' (' . $length . ')';
96              $theRestoreQueries .= $sql2 . "\n";
97           }
98         } //while
99
100         //If there are no ALTER queries to run, echo message
101         if(count($alter_queries) == 0) {
102            echo $mod_strings['ERR_NO_COLUMNS_TO_EXPAND'];
103         } else {
104
105                 // Create a backup file to restore columns to original length
106                 if($execute) {
107                    $fh = sugar_fopen('restoreExpand.sql', 'w');
108                    if(-1 == fwrite($fh, $theRestoreQueries)) {
109                           $GLOBALS['log']->error($mod_strings['ERR_CANNOT_CREATE_RESTORE_FILE']);
110                           echo($mod_strings['ERR_CANNOT_CREATE_RESTORE_FILE']);
111                    } else {
112                           $GLOBALS['log']->info($mod_strings['LBL_CREATE_RESOTRE_FILE']);
113                           echo($mod_strings['LBL_CREATE_RESOTRE_FILE']);
114                    }
115
116                    foreach($alter_queries as $key=>$value) {
117                                $db->query($value);
118                    }
119                 }
120
121                         if($export) {
122                                 header("Content-Disposition: attachment; filename=expandSugarDB.sql");
123                                 header("Content-Type: text/sql; charset={$app_strings['LBL_CHARSET']}");
124                                 header( "Expires: Mon, 26 Jul 1997 05:00:00 GMT" );
125                                 header( "Last-Modified: " . TimeDate::httpTime() );
126                                 header( "Cache-Control: post-check=0, pre-check=0", false );
127                                 header("Content-Length: ".strlen($theAlterQueries));
128                                 echo $theAlterQueries;
129                                 die();
130                         } else {
131                                 if(empty($_REQUEST['repair_silent'])) {
132                                         echo nl2br($theAlterQueries);
133                                 }
134                         }
135
136         } //if-else
137         } // end do_action
138
139         if(empty($_REQUEST['repair_silent']) && empty($_REQUEST['do_action'])) {
140                 if(!file_exists('restoreExpand.sql')) {
141                         echo "  <b>{$mod_strings['LBL_REPAIR_ACTION']}</b><br>
142                                 <form name='repairdb'>
143                                         <input type='hidden' name='action' value='expandDatabase'>
144                                         <input type='hidden' name='module' value='Administration'>
145
146                                         <select name='do_action'>
147                                                         <option value='display'>".$mod_strings['LBL_REPAIR_DISPLAYSQL']."
148                                                         <option value='export'>".$mod_strings['LBL_REPAIR_EXPORTSQL']."
149                                                         <option value='execute'>".$mod_strings['LBL_REPAIR_EXECUTESQL']."
150                                         </select><input type='submit' class='button' value='".$mod_strings['LBL_GO']."'>
151                                 </form><br><br>
152                                 ".$mod_strings['LBL_EXPAND_DATABASE_TEXT'];
153                 } else {
154                             echo "<b>{$mod_strings['LBL_EXPAND_DATABASE_FINISHED_ERROR']}</b><br>";
155                 } //if-else
156         } //if
157 }else{
158         sugar_die($GLOBALS['app_strings']['ERR_NOT_ADMIN']);
159 }
160
161
162 ?>