root / src / admin.php @ d072e29c
Historique | Voir | Annoter | Télécharger (5,92 ko)
1 | d072e29c | Florent Chuffart | <?php
|
---|---|---|---|
2 | d072e29c | Florent Chuffart | session_start (); |
3 | d072e29c | Florent Chuffart | require("headers.php"); |
4 | d072e29c | Florent Chuffart | |
5 | d072e29c | Florent Chuffart | |
6 | d072e29c | Florent Chuffart | |
7 | d072e29c | Florent Chuffart | if (! defined('PHP_EOL')) { |
8 | d072e29c | Florent Chuffart | define('PHP_EOL', strtoupper(substr(PHP_OS, 0, 3) == 'WIN') ? "\r\n" |
9 | d072e29c | Florent Chuffart | : strtoupper(substr(PHP_OS, 0, 3) == 'MAC') ? "\r" : "\n"); |
10 | d072e29c | Florent Chuffart | } |
11 | d072e29c | Florent Chuffart | |
12 | d072e29c | Florent Chuffart | $hn = SERVEUR; |
13 | d072e29c | Florent Chuffart | $un = NOM; |
14 | d072e29c | Florent Chuffart | $pw = PASSE; |
15 | d072e29c | Florent Chuffart | $db = BASE; |
16 | d072e29c | Florent Chuffart | $submit = "Submit"; |
17 | d072e29c | Florent Chuffart | $options = 1; |
18 | d072e29c | Florent Chuffart | $baseFilename = $tb; |
19 | d072e29c | Florent Chuffart | $pageTitle = $tb; |
20 | d072e29c | Florent Chuffart | |
21 | d072e29c | Florent Chuffart | if (@$_REQUEST["action"] == "CHANGE_TB") { |
22 | d072e29c | Florent Chuffart | $_SESSION["tb"] = $_REQUEST["tb"]; |
23 | d072e29c | Florent Chuffart | } else {
|
24 | d072e29c | Florent Chuffart | if (!isset($_SESSION["tb"])) { |
25 | d072e29c | Florent Chuffart | $_SESSION["tb"] = "lab_members"; |
26 | d072e29c | Florent Chuffart | } |
27 | d072e29c | Florent Chuffart | } |
28 | d072e29c | Florent Chuffart | $tb = $_SESSION["tb"]; |
29 | d072e29c | Florent Chuffart | |
30 | d072e29c | Florent Chuffart | $phpExtension = '.php'; |
31 | d072e29c | Florent Chuffart | if (isset($baseFilename) && $baseFilename != '') { |
32 | d072e29c | Florent Chuffart | $phpFile = $baseFilename.$phpExtension; |
33 | d072e29c | Florent Chuffart | //$contentFile = $baseFilename.'Content.inc';
|
34 | d072e29c | Florent Chuffart | $contentFile = $baseFilename.'.php'; |
35 | d072e29c | Florent Chuffart | } elseif (isset($tb)) { |
36 | d072e29c | Florent Chuffart | $phpFile = $tb.$phpExtension; |
37 | d072e29c | Florent Chuffart | //$contentFile = $tb.'Content.inc';
|
38 | d072e29c | Florent Chuffart | $contentFile = $tb.'.php'; |
39 | d072e29c | Florent Chuffart | } else {
|
40 | d072e29c | Florent Chuffart | $phpFile = 'index'.$phpExtension; |
41 | d072e29c | Florent Chuffart | //$contentFile = 'Content.inc';
|
42 | d072e29c | Florent Chuffart | $contentFile = 'phpMyEdit-content.php'; |
43 | d072e29c | Florent Chuffart | } |
44 | d072e29c | Florent Chuffart | |
45 | d072e29c | Florent Chuffart | $buffer = ''; |
46 | d072e29c | Florent Chuffart | |
47 | d072e29c | Florent Chuffart | function echo_buffer($x) |
48 | d072e29c | Florent Chuffart | { |
49 | d072e29c | Florent Chuffart | global $buffer; |
50 | d072e29c | Florent Chuffart | $buffer .= $x.PHP_EOL; |
51 | d072e29c | Florent Chuffart | } |
52 | d072e29c | Florent Chuffart | |
53 | d072e29c | Florent Chuffart | function check_constraints($tb,$fd) |
54 | d072e29c | Florent Chuffart | { |
55 | d072e29c | Florent Chuffart | $query = "show create table $tb"; |
56 | d072e29c | Florent Chuffart | $result = mysql_query($query); |
57 | d072e29c | Florent Chuffart | $tableDef = preg_split('/\n/',mysql_result($result,0,1)); |
58 | d072e29c | Florent Chuffart | |
59 | d072e29c | Florent Chuffart | $constraint_arg=""; |
60 | d072e29c | Florent Chuffart | while (list($key,$val) = each($tableDef)) { |
61 | d072e29c | Florent Chuffart | $words=preg_split("/[\s'`()]+/", $val); |
62 | d072e29c | Florent Chuffart | if ($words[1] == "CONSTRAINT" && $words[6]=="REFERENCES") { |
63 | d072e29c | Florent Chuffart | if ($words[5]==$fd) { |
64 | d072e29c | Florent Chuffart | $constraint_arg=" 'values' => array(\n". |
65 | d072e29c | Florent Chuffart | " 'table' => '$words[7]',\n".
|
66 | d072e29c | Florent Chuffart | " 'column' => '$words[8]'\n".
|
67 | d072e29c | Florent Chuffart | " ),\n";
|
68 | d072e29c | Florent Chuffart | } |
69 | d072e29c | Florent Chuffart | |
70 | d072e29c | Florent Chuffart | } |
71 | d072e29c | Florent Chuffart | } |
72 | d072e29c | Florent Chuffart | return $constraint_arg; |
73 | d072e29c | Florent Chuffart | } |
74 | d072e29c | Florent Chuffart | |
75 | d072e29c | Florent Chuffart | $self = basename($_SERVER['PHP_SELF']); |
76 | d072e29c | Florent Chuffart | $dbl = @mysql_pconnect($hn, $un, $pw); |
77 | d072e29c | Florent Chuffart | |
78 | d072e29c | Florent Chuffart | |
79 | d072e29c | Florent Chuffart | |
80 | d072e29c | Florent Chuffart | |
81 | d072e29c | Florent Chuffart | |
82 | d072e29c | Florent Chuffart | |
83 | d072e29c | Florent Chuffart | |
84 | d072e29c | Florent Chuffart | |
85 | d072e29c | Florent Chuffart | |
86 | d072e29c | Florent Chuffart | |
87 | d072e29c | Florent Chuffart | |
88 | d072e29c | Florent Chuffart | |
89 | d072e29c | Florent Chuffart | |
90 | d072e29c | Florent Chuffart | |
91 | d072e29c | Florent Chuffart | |
92 | d072e29c | Florent Chuffart | |
93 | d072e29c | Florent Chuffart | $select_tb = "<form action='admin.php' method='POST'>"; |
94 | d072e29c | Florent Chuffart | $select_tb .= "<input type='hidden' name='action' value='CHANGE_TB'/>"; |
95 | d072e29c | Florent Chuffart | $select_tb .= "<select name='tb'>"; |
96 | d072e29c | Florent Chuffart | $tbs = @mysql_list_tables($db, $dbl); |
97 | d072e29c | Florent Chuffart | $num_tbs = @mysql_num_rows($tbs); |
98 | d072e29c | Florent Chuffart | for ($j = 0; $j < $num_tbs; $j++) { |
99 | d072e29c | Florent Chuffart | $tb_choice = @mysql_tablename($tbs, $j); |
100 | d072e29c | Florent Chuffart | $tb_choice = htmlspecialchars($tb_choice); |
101 | d072e29c | Florent Chuffart | $checked = $tb_choice == $tb ? ' selected="selected" ' : ''; |
102 | d072e29c | Florent Chuffart | $select_tb .= "<option value='$tb_choice' $checked>$tb_choice</option>"; |
103 | d072e29c | Florent Chuffart | } |
104 | d072e29c | Florent Chuffart | $select_tb .= "</select>"; |
105 | d072e29c | Florent Chuffart | $select_tb .= "<input type='Submit' name='Submit' value='Show this table' />"; |
106 | d072e29c | Florent Chuffart | $select_tb .= "</form>"; |
107 | d072e29c | Florent Chuffart | |
108 | d072e29c | Florent Chuffart | echo $select_tb; |
109 | d072e29c | Florent Chuffart | |
110 | d072e29c | Florent Chuffart | |
111 | d072e29c | Florent Chuffart | |
112 | d072e29c | Florent Chuffart | |
113 | d072e29c | Florent Chuffart | |
114 | d072e29c | Florent Chuffart | |
115 | d072e29c | Florent Chuffart | |
116 | d072e29c | Florent Chuffart | |
117 | d072e29c | Florent Chuffart | |
118 | d072e29c | Florent Chuffart | |
119 | d072e29c | Florent Chuffart | |
120 | d072e29c | Florent Chuffart | |
121 | d072e29c | Florent Chuffart | |
122 | d072e29c | Florent Chuffart | |
123 | d072e29c | Florent Chuffart | |
124 | d072e29c | Florent Chuffart | |
125 | d072e29c | Florent Chuffart | |
126 | d072e29c | Florent Chuffart | |
127 | d072e29c | Florent Chuffart | @mysql_select_db($db); |
128 | d072e29c | Florent Chuffart | $tb_desc = @mysql_query("DESCRIBE $tb"); |
129 | d072e29c | Florent Chuffart | $fds = @mysql_list_fields($db,$tb,$dbl); |
130 | d072e29c | Florent Chuffart | $j = 0; |
131 | d072e29c | Florent Chuffart | $fd = @mysql_field_name($fds, $j); |
132 | d072e29c | Florent Chuffart | $ff = @mysql_field_flags($fds, $j); |
133 | d072e29c | Florent Chuffart | |
134 | d072e29c | Florent Chuffart | if (!stristr($ff, 'primary_key')) { |
135 | d072e29c | Florent Chuffart | echo "ERROR, first field is not a primary_key."; |
136 | d072e29c | Florent Chuffart | exit();
|
137 | d072e29c | Florent Chuffart | } |
138 | d072e29c | Florent Chuffart | |
139 | d072e29c | Florent Chuffart | $id = htmlspecialchars($fd); |
140 | d072e29c | Florent Chuffart | |
141 | d072e29c | Florent Chuffart | { |
142 | d072e29c | Florent Chuffart | echo_buffer("
|
143 | d072e29c | Florent Chuffart | // MySQL host name, user name, password, database, and table
|
144 | d072e29c | Florent Chuffart | \$opts['hn'] = '$hn';
|
145 | d072e29c | Florent Chuffart | \$opts['un'] = '$un';
|
146 | d072e29c | Florent Chuffart | \$opts['pw'] = '$pw';
|
147 | d072e29c | Florent Chuffart | \$opts['db'] = '$db';
|
148 | d072e29c | Florent Chuffart | \$opts['tb'] = '$tb';
|
149 | d072e29c | Florent Chuffart |
|
150 | d072e29c | Florent Chuffart | // Name of field which is the unique key
|
151 | d072e29c | Florent Chuffart | \$opts['key'] = '$id';
|
152 | d072e29c | Florent Chuffart |
|
153 | d072e29c | Florent Chuffart | // Type of key field (int/real/string/date etc.)");
|
154 | d072e29c | Florent Chuffart | |
155 | d072e29c | Florent Chuffart | if ($id == '') { |
156 | d072e29c | Florent Chuffart | echo_buffer("\$opts['key_type'] = '';");
|
157 | d072e29c | Florent Chuffart | } else {
|
158 | d072e29c | Florent Chuffart | $fds = @mysql_list_fields($db,$tb,$dbl); |
159 | d072e29c | Florent Chuffart | for ($j = 0; ($fd = @mysql_field_name($fds, $j)) != ''; $j++) { |
160 | d072e29c | Florent Chuffart | if ($fd == $id) { |
161 | d072e29c | Florent Chuffart | echo_buffer("\$opts['key_type'] = '".@mysql_field_type($fds, $j)."';"); |
162 | d072e29c | Florent Chuffart | break;
|
163 | d072e29c | Florent Chuffart | } |
164 | d072e29c | Florent Chuffart | } |
165 | d072e29c | Florent Chuffart | } |
166 | d072e29c | Florent Chuffart | echo_buffer("
|
167 | d072e29c | Florent Chuffart | // Sorting field(s)
|
168 | d072e29c | Florent Chuffart | \$opts['sort_field'] = array('$id');
|
169 | d072e29c | Florent Chuffart |
|
170 | d072e29c | Florent Chuffart | // Options you wish to give the users
|
171 | d072e29c | Florent Chuffart | // A - add, C - change, P - copy, V - view, D - delete,
|
172 | d072e29c | Florent Chuffart | // F - filter, I - initial sort suppressed
|
173 | d072e29c | Florent Chuffart | \$opts['options'] = \$privopt;
|
174 | d072e29c | Florent Chuffart | ");
|
175 | d072e29c | Florent Chuffart | |
176 | d072e29c | Florent Chuffart | @mysql_select_db($db); |
177 | d072e29c | Florent Chuffart | $tb_desc = @mysql_query("DESCRIBE $tb"); |
178 | d072e29c | Florent Chuffart | $fds = @mysql_list_fields($db, $tb, $dbl); |
179 | d072e29c | Florent Chuffart | $num_fds = @mysql_num_fields($fds); |
180 | d072e29c | Florent Chuffart | $ts_cnt = 0; |
181 | d072e29c | Florent Chuffart | for ($k = 0; $k < $num_fds; $k++) { |
182 | d072e29c | Florent Chuffart | $fd = mysql_field_name($fds,$k); |
183 | d072e29c | Florent Chuffart | $fm = mysql_fetch_field($fds,$k); |
184 | d072e29c | Florent Chuffart | $fn = strtr($fd, '_-.', ' '); |
185 | d072e29c | Florent Chuffart | $fn = preg_replace('/(^| +)id( +|$)/', '\\1ID\\2', $fn); // uppercase IDs |
186 | d072e29c | Florent Chuffart | $fn = ucfirst($fn); |
187 | d072e29c | Florent Chuffart | $row = @mysql_fetch_array($tb_desc); |
188 | d072e29c | Florent Chuffart | echo_buffer('$opts[\'fdd\'][\''.$fd.'\'] = array('); // ) |
189 | d072e29c | Florent Chuffart | echo_buffer(" 'name' => '".str_replace('\'','\\\'',$fn)."',"); |
190 | d072e29c | Florent Chuffart | $auto_increment = strstr($row[5], 'auto_increment') ? 1 : 0; |
191 | d072e29c | Florent Chuffart | if (substr($row[1],0,3) == 'set') { |
192 | d072e29c | Florent Chuffart | echo_buffer(" 'select' => 'M',");
|
193 | d072e29c | Florent Chuffart | } else {
|
194 | d072e29c | Florent Chuffart | echo_buffer(" 'select' => 'T',");
|
195 | d072e29c | Florent Chuffart | } |
196 | d072e29c | Florent Chuffart | if ($auto_increment) { |
197 | d072e29c | Florent Chuffart | echo_buffer(" 'options' => 'AVCPDR', // auto increment");
|
198 | d072e29c | Florent Chuffart | } |
199 | d072e29c | Florent Chuffart | // timestamps are read-only
|
200 | d072e29c | Florent Chuffart | else if (@mysql_field_type($fds, $k) == 'timestamp') { |
201 | d072e29c | Florent Chuffart | if ($ts_cnt > 0) { |
202 | d072e29c | Florent Chuffart | echo_buffer(" 'options' => 'AVCPD',");
|
203 | d072e29c | Florent Chuffart | } else { // first timestamp |
204 | d072e29c | Florent Chuffart | echo_buffer(" 'options' => 'AVCPDR', // updated automatically (MySQL feature)");
|
205 | d072e29c | Florent Chuffart | } |
206 | d072e29c | Florent Chuffart | $ts_cnt++;
|
207 | d072e29c | Florent Chuffart | } |
208 | d072e29c | Florent Chuffart | echo_buffer(" 'maxlen' => ".@mysql_field_len($fds,$k).','); |
209 | d072e29c | Florent Chuffart | // blobs -> textarea
|
210 | d072e29c | Florent Chuffart | if (@mysql_field_type($fds,$k) == 'blob') { |
211 | d072e29c | Florent Chuffart | echo_buffer(" 'textarea' => array(");
|
212 | d072e29c | Florent Chuffart | echo_buffer(" 'rows' => 5,");
|
213 | d072e29c | Florent Chuffart | echo_buffer(" 'cols' => 50),");
|
214 | d072e29c | Florent Chuffart | } |
215 | d072e29c | Florent Chuffart | // SETs and ENUMs get special treatment
|
216 | d072e29c | Florent Chuffart | if ((substr($row[1],0,3) == 'set' || substr($row[1],0,4) == 'enum') |
217 | d072e29c | Florent Chuffart | && ! (($pos = strpos($row[1], '(')) === false)) { |
218 | d072e29c | Florent Chuffart | $indent = str_repeat(' ', 18); |
219 | d072e29c | Florent Chuffart | $outstr = substr($row[1], $pos + 2, -2); |
220 | d072e29c | Florent Chuffart | $outstr = explode("','", $outstr); |
221 | d072e29c | Florent Chuffart | $outstr = str_replace("''", "'", $outstr); |
222 | d072e29c | Florent Chuffart | $outstr = str_replace('"', '\\"', $outstr); |
223 | d072e29c | Florent Chuffart | $outstr = implode('",'.PHP_EOL.$indent.'"', $outstr); |
224 | d072e29c | Florent Chuffart | echo_buffer(" 'values' => array(".PHP_EOL.$indent.'"'.$outstr.'"),'); |
225 | d072e29c | Florent Chuffart | } |
226 | d072e29c | Florent Chuffart | // automatic support for Default values
|
227 | d072e29c | Florent Chuffart | if ($row[4] != '' && $row[4] != 'NULL') { |
228 | d072e29c | Florent Chuffart | echo_buffer(" 'default' => '".$row[4]."',"); |
229 | d072e29c | Florent Chuffart | } else if ($auto_increment) { |
230 | d072e29c | Florent Chuffart | echo_buffer(" 'default' => '0',");
|
231 | d072e29c | Florent Chuffart | } |
232 | d072e29c | Florent Chuffart | // check for table constraints
|
233 | d072e29c | Florent Chuffart | $outstr = check_constraints($tb, $fd); |
234 | d072e29c | Florent Chuffart | if ($outstr != '') { |
235 | d072e29c | Florent Chuffart | echo_buffer($outstr);
|
236 | d072e29c | Florent Chuffart | } |
237 | d072e29c | Florent Chuffart | echo_buffer(" 'sort' => true");
|
238 | d072e29c | Florent Chuffart | //echo_buffer(" 'nowrap' => false,");
|
239 | d072e29c | Florent Chuffart | echo_buffer(');');
|
240 | d072e29c | Florent Chuffart | } |
241 | d072e29c | Florent Chuffart | |
242 | d072e29c | Florent Chuffart | |
243 | d072e29c | Florent Chuffart | eval($buffer); |
244 | d072e29c | Florent Chuffart | |
245 | d072e29c | Florent Chuffart | } |
246 | d072e29c | Florent Chuffart | |
247 | d072e29c | Florent Chuffart | require("footers.php"); |
248 | d072e29c | Florent Chuffart | ?> |