dolibarr 20.0.0
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 $sql = "ALTER TABLE ".$db->sanitize($table)." MODIFY ".$db->sanitize($row[0])." ".$row[1]." COLLATE utf8_unicode_ci"; // We must not sanitize the $row[1]
59 $db->query($sql);
60
61 break;
62 }
63 }
64 }
65}
66if ($action == 'convertutf8mb4') {
67 $sql = "SHOW FULL COLUMNS IN ".$db->sanitize($table);
68
69 $resql = $db->query($sql);
70 if ($resql) {
71 $num = $db->num_rows($resql);
72 $i = 0;
73 while ($i < $num) {
74 $row = $db->fetch_row($resql);
75 if ($row[0] == $field) {
76 $sql = "ALTER TABLE ".$db->sanitize($table)." MODIFY ".$db->sanitize($row[0])." ".$row[1]." CHARACTER SET utf8mb4"; // We must not sanitize the $row[1]
77 $db->query($sql);
78
79 $sql = "ALTER TABLE ".$db->sanitize($table)." MODIFY ".$db->sanitize($row[0])." ".$row[1]." COLLATE utf8mb4_unicode_ci"; // We must not sanitize the $row[1]
80 $db->query($sql);
81
82 break;
83 }
84 }
85 }
86}
87
88
89/*
90 * View
91 */
92
93llxHeader('', '', '', '', 0, 0, '', '', '', 'mod-admin page-system_dbtable');
94
95
96print load_fiche_titre($langs->trans("Table")." ".$table, '', 'title_setup');
97
98// Define request to get table description
99$base = 0;
100if (preg_match('/mysql/i', $conf->db->type)) {
101 $sql = "SHOW TABLE STATUS LIKE '".$db->escape($db->escapeforlike($table))."'";
102 $base = 1;
103} elseif ($conf->db->type == 'pgsql') {
104 $sql = "SELECT conname,contype FROM pg_constraint";
105 $base = 2;
106}
107
108if (!$base) {
109 print $langs->trans("FeatureNotAvailableWithThisDatabaseDriver");
110} else {
111 $resql = $db->query($sql);
112 if ($resql) {
113 $num = $db->num_rows($resql);
114 $i = 0;
115 while ($i < $num) {
116 $row = $db->fetch_row($resql);
117 $i++;
118 }
119 }
120
121 if ($base == 1) { // mysql
122 $link = array();
123 $cons = explode(";", $row[14]);
124 if (!empty($cons)) {
125 foreach ($cons as $cc) {
126 $cx = preg_replace("/\‍)\sREFER/", "", $cc);
127 $cx = preg_replace("/\‍(`/", "", $cx);
128 $cx = preg_replace("/`\‍)/", "", $cx);
129 $cx = preg_replace("/`\s/", "", $cx);
130
131 $val = explode("`", $cx);
132
133 $link[trim($val[0])][0] = (isset($val[1]) ? $val[1] : '');
134 $link[trim($val[0])][1] = (isset($val[2]) ? $val[2] : '');
135 }
136 }
137
138 print '<table class="noborder">';
139 print '<tr class="liste_titre">';
140 print '<td>'.$langs->trans("Fields").'</td>';
141 print '<td>'.$langs->trans("Type").'</td>';
142 print '<td>'.$langs->trans("Collation").'</td>';
143 print '<td>'.$langs->trans("Null").'</td>';
144 print '<td>'.$langs->trans("Index").'</td>';
145 print '<td>'.$langs->trans("Default").'</td>';
146 print '<td>'.$langs->trans("Extra").'</td>';
147 print '<td>'.$langs->trans("Privileges").'</td>';
148 print '<td>'.$langs->trans("FieldsLinked").'</td>';
149 print '</tr>';
150
151 // $sql = "DESCRIBE ".$table;
152 $sql = "SHOW FULL COLUMNS IN ".$db->sanitize($table);
153
154 $resql = $db->query($sql);
155 if ($resql) {
156 $num = $db->num_rows($resql);
157 $i = 0;
158 while ($i < $num) {
159 $row = $db->fetch_row($resql);
160
161 print '<tr class="oddeven">';
162
163 // field
164 print "<td>".$row[0]."</td>";
165
166 // type
167 print "<td>";
168 $proptype = $row[1];
169 $pictoType = '';
170 $matches = array();
171 if (preg_match('/^varchar/', $proptype, $matches)) {
172 $pictoType = 'varchar';
173 } elseif (strpos($proptype, 'int') === 0 || strpos($proptype, 'tinyint') === 0 || strpos($proptype, 'bigint') === 0) {
174 $pictoType = 'int';
175 } elseif (strpos($proptype, 'timestamp') === 0) {
176 $pictoType = 'datetime';
177 } elseif (strpos($proptype, 'real') === 0) {
178 $pictoType = 'double';
179 }
180 print(!empty($pictoType) ? getPictoForType($pictoType) : getPictoForType($proptype)).'<span title="'.dol_escape_htmltag($proptype).'">'.dol_escape_htmltag($proptype).'</span>';
181 print "</td>";
182
183 // collation
184 print "<td>".(empty($row[2]) ? '&nbsp;' : $row[2]);
185
186 // Link to convert collation
187 if (isset($row[2])) {
188 print '<br><span class="opacitymedium small">'.$langs->trans("ConvertInto");
189 if (!in_array($row[2], array("utf8_unicode_ci"))) {
190 print ' <a class="reposition" href="dbtable.php?action=convertutf8&table='.urlencode($table).'&field='.urlencode($row[0]).'&token='.newToken().'">utf8</a>';
191 }
192 if (!in_array($row[2], array("utf8mb4_unicode_ci"))) {
193 print ' <a class="reposition" href="dbtable.php?action=convertutf8mb4&table='.urlencode($table).'&field='.urlencode($row[0]).'&token='.newToken().'">utf8mb4</a>';
194 }
195 print '</span>';
196 } else {
197 print '<br>&nbsp;';
198 }
199
200 print "</td>";
201
202 // null
203 print "<td>".$row[3]."</td>";
204 // key
205 print "<td>".(empty($row[4]) ? '' : $row[4])."</td>";
206 // default
207 print "<td>".(empty($row[5]) ? '' : $row[5])."</td>";
208 // extra
209 print "<td>".(empty($row[6]) ? '' : $row[6])."</td>";
210 // privileges
211 print "<td>".(empty($row[7]) ? '' : $row[7])."</td>";
212
213 print "<td>".(isset($link[$row[0]][0]) ? $link[$row[0]][0] : '').".";
214 print(isset($link[$row[0]][1]) ? $link[$row[0]][1] : '')."</td>";
215
216 print '<!-- ALTER TABLE '.$table.' MODIFY '.$row[0].' '.$row[1].' COLLATE utf8_unicode_ci; -->';
217 print '<!-- ALTER TABLE '.$table.' MODIFY '.$row[0].' '.$row[1].' CHARACTER SET utf8; -->';
218 print '</tr>';
219 $i++;
220 }
221 }
222 print '</table>';
223 }
224}
225
226// End of page
227llxFooter();
228$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.
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.