Statistiques
| Branche: | Révision :

root / src / admin.php @ a9b72d88

Historique | Voir | Annoter | Télécharger (6,33 ko)

1 d072e29c Florent Chuffart
<?php
2 0ce29891 Florent Chuffart
session_start();
3 d072e29c Florent Chuffart
require("headers.php");
4 d072e29c Florent Chuffart
5 d072e29c Florent Chuffart
if (! defined('PHP_EOL')) {
6 d072e29c Florent Chuffart
        define('PHP_EOL', strtoupper(substr(PHP_OS, 0, 3) == 'WIN') ? "\r\n"
7 d072e29c Florent Chuffart
                        : strtoupper(substr(PHP_OS, 0, 3) == 'MAC') ? "\r" : "\n");
8 d072e29c Florent Chuffart
}
9 d072e29c Florent Chuffart
10 5bcbc405 Florent Chuffart
11 5bcbc405 Florent Chuffart
12 5bcbc405 Florent Chuffart
13 5bcbc405 Florent Chuffart
14 5bcbc405 Florent Chuffart
15 5bcbc405 Florent Chuffart
16 5bcbc405 Florent Chuffart
17 5bcbc405 Florent Chuffart
  
18 5bcbc405 Florent Chuffart
  
19 5bcbc405 Florent Chuffart
20 d072e29c Florent Chuffart
$hn = SERVEUR;
21 d072e29c Florent Chuffart
$un = NOM;
22 d072e29c Florent Chuffart
$pw = PASSE;
23 d072e29c Florent Chuffart
$db = BASE;
24 d072e29c Florent Chuffart
$submit        = "Submit";
25 d072e29c Florent Chuffart
$options       = 1;
26 d072e29c Florent Chuffart
$baseFilename  = $tb;
27 d072e29c Florent Chuffart
$pageTitle     = $tb;
28 d072e29c Florent Chuffart
29 5bcbc405 Florent Chuffart
30 5bcbc405 Florent Chuffart
function mysql_tables($database='')
31 5bcbc405 Florent Chuffart
{
32 5bcbc405 Florent Chuffart
    $tables = array();
33 5bcbc405 Florent Chuffart
    $list_tables_sql = "SHOW TABLES FROM {$database};";
34 5bcbc405 Florent Chuffart
    $result = mysql_query($list_tables_sql);
35 5bcbc405 Florent Chuffart
    if($result)
36 5bcbc405 Florent Chuffart
    while($table = mysql_fetch_row($result))
37 5bcbc405 Florent Chuffart
    {
38 5bcbc405 Florent Chuffart
        $tables[] = $table[0];
39 5bcbc405 Florent Chuffart
    }
40 5bcbc405 Florent Chuffart
    return $tables;
41 5bcbc405 Florent Chuffart
}
42 5bcbc405 Florent Chuffart
$tbs = mysql_tables($db);
43 5bcbc405 Florent Chuffart
44 5bcbc405 Florent Chuffart
45 5bcbc405 Florent Chuffart
if (!in_array($_SESSION["tb"], $tbs)) {
46 5bcbc405 Florent Chuffart
  unset($_SESSION["tb"]);
47 5bcbc405 Florent Chuffart
}
48 d072e29c Florent Chuffart
if (@$_REQUEST["action"] == "CHANGE_TB") {
49 d072e29c Florent Chuffart
  $_SESSION["tb"] = $_REQUEST["tb"];
50 d072e29c Florent Chuffart
} else {
51 0ce29891 Florent Chuffart
  if (!isset($_SESSION["tb"]) || $_SESSION["tb"] == "admin") {
52 d072e29c Florent Chuffart
    $_SESSION["tb"] = "lab_members";
53 d072e29c Florent Chuffart
  }
54 d072e29c Florent Chuffart
}
55 d072e29c Florent Chuffart
$tb = $_SESSION["tb"];
56 d072e29c Florent Chuffart
57 d072e29c Florent Chuffart
$phpExtension = '.php';
58 d072e29c Florent Chuffart
if (isset($baseFilename) && $baseFilename != '') {
59 d072e29c Florent Chuffart
        $phpFile = $baseFilename.$phpExtension;
60 d072e29c Florent Chuffart
        //$contentFile = $baseFilename.'Content.inc';
61 d072e29c Florent Chuffart
        $contentFile = $baseFilename.'.php';
62 d072e29c Florent Chuffart
} elseif (isset($tb)) {
63 d072e29c Florent Chuffart
        $phpFile = $tb.$phpExtension;
64 d072e29c Florent Chuffart
        //$contentFile = $tb.'Content.inc';
65 d072e29c Florent Chuffart
        $contentFile = $tb.'.php';
66 d072e29c Florent Chuffart
} else {
67 d072e29c Florent Chuffart
        $phpFile = 'index'.$phpExtension;
68 d072e29c Florent Chuffart
        //$contentFile = 'Content.inc';
69 d072e29c Florent Chuffart
        $contentFile = 'phpMyEdit-content.php';
70 d072e29c Florent Chuffart
}
71 d072e29c Florent Chuffart
72 d072e29c Florent Chuffart
$buffer = '';
73 d072e29c Florent Chuffart
74 d072e29c Florent Chuffart
function echo_buffer($x)
75 d072e29c Florent Chuffart
{
76 d072e29c Florent Chuffart
        global $buffer;
77 d072e29c Florent Chuffart
        $buffer .= $x.PHP_EOL;
78 d072e29c Florent Chuffart
}
79 d072e29c Florent Chuffart
80 d072e29c Florent Chuffart
function check_constraints($tb,$fd)
81 d072e29c Florent Chuffart
{
82 d072e29c Florent Chuffart
  $query    = "show create table $tb";
83 d072e29c Florent Chuffart
  $result   = mysql_query($query);
84 d072e29c Florent Chuffart
  $tableDef = preg_split('/\n/',mysql_result($result,0,1));
85 d072e29c Florent Chuffart
86 d072e29c Florent Chuffart
  $constraint_arg="";
87 d072e29c Florent Chuffart
  while (list($key,$val) = each($tableDef)) {
88 d072e29c Florent Chuffart
    $words=preg_split("/[\s'`()]+/", $val);
89 d072e29c Florent Chuffart
    if ($words[1] == "CONSTRAINT" && $words[6]=="REFERENCES") {
90 d072e29c Florent Chuffart
      if ($words[5]==$fd) {
91 d072e29c Florent Chuffart
        $constraint_arg="  'values' => array(\n".
92 d072e29c Florent Chuffart
                        "    'table'  => '$words[7]',\n".
93 d072e29c Florent Chuffart
                        "    'column' => '$words[8]'\n".
94 d072e29c Florent Chuffart
                        "  ),\n";
95 d072e29c Florent Chuffart
      }
96 d072e29c Florent Chuffart
97 d072e29c Florent Chuffart
    }
98 d072e29c Florent Chuffart
  }
99 d072e29c Florent Chuffart
  return $constraint_arg;
100 d072e29c Florent Chuffart
}
101 d072e29c Florent Chuffart
102 d072e29c Florent Chuffart
$self = basename($_SERVER['PHP_SELF']);
103 d072e29c Florent Chuffart
$dbl  = @mysql_pconnect($hn, $un, $pw);
104 d072e29c Florent Chuffart
105 d072e29c Florent Chuffart
106 d072e29c Florent Chuffart
107 d072e29c Florent Chuffart
108 d072e29c Florent Chuffart
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
$select_tb = "<form action='admin.php' method='POST'>";
121 d072e29c Florent Chuffart
$select_tb .= "<input type='hidden' name='action' value='CHANGE_TB'/>";
122 d072e29c Florent Chuffart
$select_tb .= "<select name='tb'>";
123 d072e29c Florent Chuffart
$tbs     = @mysql_list_tables($db, $dbl);
124 d072e29c Florent Chuffart
$num_tbs = @mysql_num_rows($tbs);
125 d072e29c Florent Chuffart
for ($j = 0; $j < $num_tbs; $j++) {
126 d072e29c Florent Chuffart
        $tb_choice = @mysql_tablename($tbs, $j);
127 d072e29c Florent Chuffart
  $tb_choice = htmlspecialchars($tb_choice);
128 d072e29c Florent Chuffart
  $checked = $tb_choice == $tb ? ' selected="selected" ' : '';
129 d072e29c Florent Chuffart
        $select_tb .= "<option value='$tb_choice' $checked>$tb_choice</option>";
130 d072e29c Florent Chuffart
}
131 d072e29c Florent Chuffart
$select_tb .= "</select>";
132 d072e29c Florent Chuffart
$select_tb .= "<input type='Submit' name='Submit' value='Show this table' />";
133 d072e29c Florent Chuffart
$select_tb .= "</form>";
134 d072e29c Florent Chuffart
135 d072e29c Florent Chuffart
echo $select_tb;
136 d072e29c Florent Chuffart
137 d072e29c Florent Chuffart
138 d072e29c Florent Chuffart
139 d072e29c Florent Chuffart
140 d072e29c Florent Chuffart
141 d072e29c Florent Chuffart
142 d072e29c Florent Chuffart
143 d072e29c Florent Chuffart
144 d072e29c Florent Chuffart
145 d072e29c Florent Chuffart
146 d072e29c Florent Chuffart
147 d072e29c Florent Chuffart
148 d072e29c Florent Chuffart
149 d072e29c Florent Chuffart
150 d072e29c Florent Chuffart
151 d072e29c Florent Chuffart
152 d072e29c Florent Chuffart
153 d072e29c Florent Chuffart
154 d072e29c Florent Chuffart
@mysql_select_db($db);
155 d072e29c Florent Chuffart
$tb_desc = @mysql_query("DESCRIBE $tb");
156 d072e29c Florent Chuffart
$fds     = @mysql_list_fields($db,$tb,$dbl);
157 d072e29c Florent Chuffart
$j = 0; 
158 d072e29c Florent Chuffart
$fd = @mysql_field_name($fds, $j);
159 d072e29c Florent Chuffart
$ff = @mysql_field_flags($fds, $j);
160 d072e29c Florent Chuffart
161 d072e29c Florent Chuffart
if (!stristr($ff, 'primary_key')) {
162 d072e29c Florent Chuffart
  echo "ERROR, first field is not a primary_key.";
163 d072e29c Florent Chuffart
  exit();
164 d072e29c Florent Chuffart
}
165 d072e29c Florent Chuffart
166 d072e29c Florent Chuffart
$id = htmlspecialchars($fd);
167 d072e29c Florent Chuffart
168 d072e29c Florent Chuffart
{
169 d072e29c Florent Chuffart
        echo_buffer("
170 d072e29c Florent Chuffart
// MySQL host name, user name, password, database, and table
171 d072e29c Florent Chuffart
\$opts['hn'] = '$hn';
172 d072e29c Florent Chuffart
\$opts['un'] = '$un';
173 d072e29c Florent Chuffart
\$opts['pw'] = '$pw';
174 d072e29c Florent Chuffart
\$opts['db'] = '$db';
175 d072e29c Florent Chuffart
\$opts['tb'] = '$tb';
176 d072e29c Florent Chuffart

177 d072e29c Florent Chuffart
// Name of field which is the unique key
178 d072e29c Florent Chuffart
\$opts['key'] = '$id';
179 d072e29c Florent Chuffart

180 d072e29c Florent Chuffart
// Type of key field (int/real/string/date etc.)");
181 d072e29c Florent Chuffart
182 d072e29c Florent Chuffart
        if ($id == '') {
183 d072e29c Florent Chuffart
                echo_buffer("\$opts['key_type'] = '';");
184 d072e29c Florent Chuffart
        } else {
185 d072e29c Florent Chuffart
                $fds = @mysql_list_fields($db,$tb,$dbl);
186 d072e29c Florent Chuffart
                for ($j = 0; ($fd = @mysql_field_name($fds, $j)) != ''; $j++) {
187 d072e29c Florent Chuffart
                        if ($fd == $id) {
188 d072e29c Florent Chuffart
                                echo_buffer("\$opts['key_type'] = '".@mysql_field_type($fds, $j)."';");
189 d072e29c Florent Chuffart
                                break;
190 d072e29c Florent Chuffart
                        }
191 d072e29c Florent Chuffart
                }
192 d072e29c Florent Chuffart
        }
193 d072e29c Florent Chuffart
        echo_buffer("
194 d072e29c Florent Chuffart
// Sorting field(s)
195 d072e29c Florent Chuffart
\$opts['sort_field'] = array('$id');
196 d072e29c Florent Chuffart

197 d072e29c Florent Chuffart
// Options you wish to give the users
198 d072e29c Florent Chuffart
// A - add,  C - change, P - copy, V - view, D - delete,
199 d072e29c Florent Chuffart
// F - filter, I - initial sort suppressed
200 d072e29c Florent Chuffart
\$opts['options'] = \$privopt;
201 d072e29c Florent Chuffart
");
202 d072e29c Florent Chuffart
203 d072e29c Florent Chuffart
        @mysql_select_db($db);
204 d072e29c Florent Chuffart
        $tb_desc = @mysql_query("DESCRIBE $tb");
205 d072e29c Florent Chuffart
        $fds     = @mysql_list_fields($db, $tb, $dbl);
206 d072e29c Florent Chuffart
        $num_fds = @mysql_num_fields($fds);
207 d072e29c Florent Chuffart
        $ts_cnt  = 0;
208 d072e29c Florent Chuffart
        for ($k = 0; $k < $num_fds; $k++) {
209 d072e29c Florent Chuffart
                $fd = mysql_field_name($fds,$k);
210 d072e29c Florent Chuffart
                $fm = mysql_fetch_field($fds,$k);
211 d072e29c Florent Chuffart
                $fn = strtr($fd, '_-.', '   ');
212 d072e29c Florent Chuffart
                $fn = preg_replace('/(^| +)id( +|$)/', '\\1ID\\2', $fn); // uppercase IDs
213 d072e29c Florent Chuffart
                $fn = ucfirst($fn);
214 d072e29c Florent Chuffart
                $row = @mysql_fetch_array($tb_desc);
215 d072e29c Florent Chuffart
                echo_buffer('$opts[\'fdd\'][\''.$fd.'\'] = array('); // )
216 d072e29c Florent Chuffart
                echo_buffer("  'name'     => '".str_replace('\'','\\\'',$fn)."',");
217 d072e29c Florent Chuffart
                $auto_increment = strstr($row[5], 'auto_increment') ? 1 : 0;
218 d072e29c Florent Chuffart
                if (substr($row[1],0,3) == 'set') {
219 d072e29c Florent Chuffart
                        echo_buffer("  'select'   => 'M',");
220 d072e29c Florent Chuffart
                } else {
221 d072e29c Florent Chuffart
                        echo_buffer("  'select'   => 'T',");
222 d072e29c Florent Chuffart
                }
223 d072e29c Florent Chuffart
                if ($auto_increment) {
224 d072e29c Florent Chuffart
                        echo_buffer("  'options'  => 'AVCPDR', // auto increment");
225 d072e29c Florent Chuffart
                }
226 d072e29c Florent Chuffart
                // timestamps are read-only
227 d072e29c Florent Chuffart
                else if (@mysql_field_type($fds, $k) == 'timestamp') {
228 d072e29c Florent Chuffart
                        if ($ts_cnt > 0) {
229 d072e29c Florent Chuffart
                                echo_buffer("  'options'  => 'AVCPD',");
230 d072e29c Florent Chuffart
                        } else { // first timestamp
231 d072e29c Florent Chuffart
                                echo_buffer("  'options'  => 'AVCPDR', // updated automatically (MySQL feature)");
232 d072e29c Florent Chuffart
                        }
233 d072e29c Florent Chuffart
                        $ts_cnt++;
234 d072e29c Florent Chuffart
                }
235 d072e29c Florent Chuffart
                echo_buffer("  'maxlen'   => ".@mysql_field_len($fds,$k).',');
236 d072e29c Florent Chuffart
                // blobs -> textarea
237 d072e29c Florent Chuffart
                if (@mysql_field_type($fds,$k) == 'blob') {
238 d072e29c Florent Chuffart
                        echo_buffer("  'textarea' => array(");
239 d072e29c Florent Chuffart
                        echo_buffer("    'rows' => 5,");
240 d072e29c Florent Chuffart
                        echo_buffer("    'cols' => 50),");
241 d072e29c Florent Chuffart
                }
242 d072e29c Florent Chuffart
                // SETs and ENUMs get special treatment
243 d072e29c Florent Chuffart
                if ((substr($row[1],0,3) == 'set' || substr($row[1],0,4) == 'enum')
244 d072e29c Florent Chuffart
                                && ! (($pos = strpos($row[1], '(')) === false)) {
245 d072e29c Florent Chuffart
                        $indent = str_repeat(' ', 18);
246 d072e29c Florent Chuffart
                        $outstr = substr($row[1], $pos + 2, -2);
247 d072e29c Florent Chuffart
                        $outstr = explode("','", $outstr);
248 d072e29c Florent Chuffart
                        $outstr = str_replace("''", "'",  $outstr);
249 d072e29c Florent Chuffart
                        $outstr = str_replace('"', '\\"', $outstr);
250 d072e29c Florent Chuffart
                        $outstr = implode('",'.PHP_EOL.$indent.'"', $outstr);
251 d072e29c Florent Chuffart
                        echo_buffer("  'values'   => array(".PHP_EOL.$indent.'"'.$outstr.'"),');
252 d072e29c Florent Chuffart
                }
253 d072e29c Florent Chuffart
                // automatic support for Default values
254 d072e29c Florent Chuffart
                if ($row[4] != '' && $row[4] != 'NULL') {
255 d072e29c Florent Chuffart
                        echo_buffer("  'default'  => '".$row[4]."',");
256 d072e29c Florent Chuffart
                } else if ($auto_increment) {
257 d072e29c Florent Chuffart
                        echo_buffer("  'default'  => '0',");
258 d072e29c Florent Chuffart
                }
259 d072e29c Florent Chuffart
                // check for table constraints
260 d072e29c Florent Chuffart
                $outstr = check_constraints($tb, $fd);
261 d072e29c Florent Chuffart
                if ($outstr != '') {
262 d072e29c Florent Chuffart
                        echo_buffer($outstr);
263 d072e29c Florent Chuffart
                }
264 d072e29c Florent Chuffart
                echo_buffer("  'sort'     => true");
265 d072e29c Florent Chuffart
                //echo_buffer("  'nowrap'   => false,");
266 d072e29c Florent Chuffart
                echo_buffer(');');
267 d072e29c Florent Chuffart
        }
268 d072e29c Florent Chuffart
269 d072e29c Florent Chuffart
   
270 d072e29c Florent Chuffart
  eval($buffer);
271 d072e29c Florent Chuffart
272 d072e29c Florent Chuffart
}
273 d072e29c Florent Chuffart
274 d072e29c Florent Chuffart
require("footers.php");
275 d072e29c Florent Chuffart
?>