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