dolibarr 20.0.2
dbtable.php
Go to the documentation of this file.
1<?php
2/* Copyright (C) 2003 Rodolphe Quiedeville <rodolphe@quiedeville.org>
3 * Copyright (C) 2004-2005 Laurent Destailleur <eldy@users.sourceforge.net>
4 * Copyright (C) 2004 Sebastien Di Cintio <sdicintio@ressource-toi.org>
5 * Copyright (C) 2004 Benoit Mortier <benoit.mortier@opensides.be>
6 * Copyright (C) 2005-2012 Regis Houssin <regis.houssin@inodbox.com>
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 3 of the License, or
11 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program. If not, see <https://www.gnu.org/licenses/>.
20 */
21
27// Load Dolibarr environment
28require '../../main.inc.php';
29
30$langs->load("admin");
31
32if (!$user->admin) {
34}
35
36$table = GETPOST('table', 'aZ09');
37$field = GETPOST('field', 'aZ09');
38$action = GETPOST('action', 'aZ09');
39
40
41/*
42 * Actions
43 */
44
45if ($action == 'convertutf8') {
46 $sql = "SHOW FULL COLUMNS IN ".$db->sanitize($table);
47
48 $resql = $db->query($sql);
49 if ($resql) {
50 $num = $db->num_rows($resql);
51 $i = 0;
52 while ($i < $num) {
53 $row = $db->fetch_row($resql);
54 if ($row[0] == $field) {
55 $sql = "ALTER TABLE ".$db->sanitize($table)." MODIFY ".$db->sanitize($row[0])." ".$row[1]." CHARACTER SET utf8"; // We must not sanitize the $row[1]
56 $db->query($sql);
57
58 $collation = 'utf8_unicode_ci';
59 $defaultcollation = $db->getDefaultCollationDatabase();
60 if (preg_match('/general/', $defaultcollation)) {
61 $collation = 'utf8_general_ci';
62 }
63
64 $sql = "ALTER TABLE ".$db->sanitize($table)." MODIFY ".$db->sanitize($row[0])." ".$row[1]." COLLATE ".$db->sanitize($collation); // We must not sanitize the $row[1]
65 $resql2 = $db->query($sql);
66 if (!$resql2) {
67 setEventMessages($db->lasterror(), null, 'warnings');
68 }
69
70 break;
71 }
72 }
73 }
74}
75if ($action == 'convertutf8mb4') {
76 $sql = "SHOW FULL COLUMNS IN ".$db->sanitize($table);
77
78 $resql = $db->query($sql);
79 if ($resql) {
80 $num = $db->num_rows($resql);
81 $i = 0;
82 while ($i < $num) {
83 $row = $db->fetch_row($resql);
84 if ($row[0] == $field) {
85 $sql = "ALTER TABLE ".$db->sanitize($table)." MODIFY ".$db->sanitize($row[0])." ".$row[1]." CHARACTER SET utf8mb4"; // We must not sanitize the $row[1]
86 $db->query($sql);
87
88 $collation = 'utf8mb4_unicode_ci';
89 $defaultcollation = $db->getDefaultCollationDatabase();
90 if (preg_match('/general/', $defaultcollation)) {
91 $collation = 'utf8mb4_general_ci';
92 }
93
94 $sql = "ALTER TABLE ".$db->sanitize($table)." MODIFY ".$db->sanitize($row[0])." ".$row[1]." COLLATE ".$db->sanitize($collation); // We must not sanitize the $row[1]
95 $resql2 = $db->query($sql);
96 if (!$resql2) {
97 setEventMessages($db->lasterror(), null, 'warnings');
98 }
99
100 break;
101 }
102 }
103 }
104}
105
106
107/*
108 * View
109 */
110
111llxHeader('', '', '', '', 0, 0, '', '', '', 'mod-admin page-system_dbtable');
112
113
114print load_fiche_titre($langs->trans("Table")." ".$table, '', 'title_setup');
115
116// Define request to get table description
117$base = 0;
118if (preg_match('/mysql/i', $conf->db->type)) {
119 $sql = "SHOW TABLE STATUS LIKE '".$db->escape($db->escapeforlike($table))."'";
120 $base = 1;
121} elseif ($conf->db->type == 'pgsql') {
122 $sql = "SELECT conname,contype FROM pg_constraint";
123 $base = 2;
124}
125
126if (!$base) {
127 print $langs->trans("FeatureNotAvailableWithThisDatabaseDriver");
128} else {
129 $resql = $db->query($sql);
130 if ($resql) {
131 $num = $db->num_rows($resql);
132 $i = 0;
133 while ($i < $num) {
134 $row = $db->fetch_row($resql);
135 $i++;
136 }
137 }
138
139 if ($base == 1) { // mysql
140 $link = array();
141 $cons = explode(";", $row[14]);
142 if (!empty($cons)) {
143 foreach ($cons as $cc) {
144 $cx = preg_replace("/\‍)\sREFER/", "", $cc);
145 $cx = preg_replace("/\‍(`/", "", $cx);
146 $cx = preg_replace("/`\‍)/", "", $cx);
147 $cx = preg_replace("/`\s/", "", $cx);
148
149 $val = explode("`", $cx);
150
151 $link[trim($val[0])][0] = (isset($val[1]) ? $val[1] : '');
152 $link[trim($val[0])][1] = (isset($val[2]) ? $val[2] : '');
153 }
154 }
155
156 print '<div class="div-table-responsive-no-min">';
157 print '<table class="noborder">';
158 print '<tr class="liste_titre">';
159 print '<td>'.$langs->trans("Fields").'</td>';
160 print '<td>'.$langs->trans("Type").'</td>';
161 print '<td>'.$langs->trans("Collation").'</td>';
162 print '<td>'.$langs->trans("Null").'</td>';
163 print '<td>'.$langs->trans("Index").'</td>';
164 print '<td>'.$langs->trans("Default").'</td>';
165 print '<td>'.$langs->trans("Extra").'</td>';
166 print '<td>'.$langs->trans("Privileges").'</td>';
167 print '<td>'.$langs->trans("FieldsLinked").'</td>';
168 print '</tr>';
169
170 // $sql = "DESCRIBE ".$table;
171 $sql = "SHOW FULL COLUMNS IN ".$db->sanitize($table);
172
173 $resql = $db->query($sql);
174 if ($resql) {
175 $num = $db->num_rows($resql);
176 $i = 0;
177 while ($i < $num) {
178 $row = $db->fetch_row($resql);
179
180 print '<tr class="oddeven">';
181
182 // field
183 print "<td>".$row[0]."</td>";
184
185 // type
186 print "<td>";
187 $proptype = $row[1];
188 $pictoType = '';
189 $matches = array();
190 if (preg_match('/^varchar/', $proptype, $matches)) {
191 $pictoType = 'varchar';
192 } elseif (strpos($proptype, 'int') === 0 || strpos($proptype, 'tinyint') === 0 || strpos($proptype, 'bigint') === 0) {
193 $pictoType = 'int';
194 } elseif (strpos($proptype, 'timestamp') === 0) {
195 $pictoType = 'datetime';
196 } elseif (strpos($proptype, 'real') === 0) {
197 $pictoType = 'double';
198 }
199 print(!empty($pictoType) ? getPictoForType($pictoType) : getPictoForType($proptype)).'<span title="'.dol_escape_htmltag($proptype).'">'.dol_escape_htmltag($proptype).'</span>';
200 print "</td>";
201
202 // collation
203 print "<td>".(empty($row[2]) ? '&nbsp;' : $row[2]);
204
205 // Link to convert collation
206 if (isset($row[2])) {
207 print '<br><span class="opacitymedium small">'.$langs->trans("ConvertInto");
208 if (!in_array($row[2], array("utf8_unicode_ci"))) {
209 print ' <a class="reposition" href="dbtable.php?action=convertutf8&table='.urlencode($table).'&field='.urlencode($row[0]).'&token='.newToken().'">utf8</a>';
210 }
211 if (!in_array($row[2], array("utf8mb4_unicode_ci"))) {
212 print ' <a class="reposition" href="dbtable.php?action=convertutf8mb4&table='.urlencode($table).'&field='.urlencode($row[0]).'&token='.newToken().'">utf8mb4</a>';
213 }
214 print '</span>';
215 } else {
216 print '<br>&nbsp;';
217 }
218
219 print "</td>";
220
221 // null
222 print "<td>".$row[3]."</td>";
223 // key
224 print "<td>".(empty($row[4]) ? '' : $row[4])."</td>";
225 // default
226 print "<td>".(empty($row[5]) ? '' : $row[5])."</td>";
227 // extra
228 print "<td>".(empty($row[6]) ? '' : $row[6])."</td>";
229 // privileges
230 print "<td>".(empty($row[7]) ? '' : $row[7])."</td>";
231
232 print "<td>".(isset($link[$row[0]][0]) ? $link[$row[0]][0] : '').".";
233 print(isset($link[$row[0]][1]) ? $link[$row[0]][1] : '')."</td>";
234
235 print '<!-- ALTER TABLE '.$table.' MODIFY '.$row[0].' '.$row[1].' COLLATE utf8mb4_unicode_ci; -->';
236 print '<!-- ALTER TABLE '.$table.' MODIFY '.$row[0].' '.$row[1].' CHARACTER SET utf8mb4; -->';
237 print '</tr>';
238 $i++;
239 }
240 }
241 print '</table>';
242 print '</div>';
243 }
244}
245
246// End of page
247llxFooter();
248$db->close();
if(!defined('NOREQUIRESOC')) if(!defined( 'NOREQUIRETRAN')) if(!defined('NOTOKENRENEWAL')) if(!defined( 'NOREQUIREMENU')) if(!defined('NOREQUIREHTML')) if(!defined( 'NOREQUIREAJAX')) llxHeader()
Empty header.
Definition wrapper.php:55
llxFooter()
Empty footer.
Definition wrapper.php:69
load_fiche_titre($title, $morehtmlright='', $picto='generic', $pictoisfullpath=0, $id='', $morecssontable='', $morehtmlcenter='')
Load a title with picto.
getPictoForType($key, $morecss='')
Return the picto for a data type.
newToken()
Return the value of token currently saved into session with name 'newtoken'.
GETPOST($paramname, $check='alphanohtml', $method=0, $filter=null, $options=null, $noreplace=0)
Return value of a param into GET or POST supervariable.
setEventMessages($mesg, $mesgs, $style='mesgs', $messagekey='', $noduplicate=0)
Set event messages in dol_events session object.
dol_escape_htmltag($stringtoescape, $keepb=0, $keepn=0, $noescapetags='', $escapeonlyhtmltags=0, $cleanalsojavascript=0)
Returns text escaped for inclusion in HTML alt or title or value tags, or into values of HTML input f...
accessforbidden($message='', $printheader=1, $printfooter=1, $showonlymessage=0, $params=null)
Show a message to say access is forbidden and stop program.