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