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