2.11.3-rc1, 2.11.4-dev
[phpmyadmin/crack.git] / libraries / display_create_table.lib.php
blob62d522b687305095365a3e2ff37a84bf37e9ccbd
1 <?php
2 /* vim: set expandtab sw=4 ts=4 sts=4: */
3 /**
4 * Displays form for creating a table (if user has privileges for that)
6 * @version $Id$
7 */
9 /**
12 require_once './libraries/check_user_privileges.lib.php';
14 // for MySQL >= 4.1.0, we should be able to detect if user has a CREATE
15 // privilege by looking at SHOW GRANTS output;
16 // for < 4.1.0, it could be more difficult because the logic tries to
17 // detect the current host and it might be expressed in many ways; also
18 // on a shared server, the user might be unable to define a controluser
19 // that has the proper rights to the "mysql" db;
20 // so we give up and assume that user has the right to create a table
22 // Note: in this case we could even skip the following "foreach" logic
24 // Addendum, 2006-01-19: ok, I give up. We got some reports about servers
25 // where the hostname field in mysql.user is not the same as the one
26 // in mysql.db for a user. In this case, SHOW GRANTS does not return
27 // the db-specific privileges. And probably, those users are on a shared
28 // server, so can't set up a control user with rights to the "mysql" db.
29 // We cannot reliably detect the db-specific privileges, so no more
30 // warnings about the lack of privileges for CREATE TABLE. Tested
31 // on MySQL 5.0.18.
33 $is_create_table_priv = true;
36 if (PMA_MYSQL_INT_VERSION >= 40100) {
37 $is_create_table_priv = false;
38 } else {
39 $is_create_table_priv = true;
42 foreach ($dbs_where_create_table_allowed as $allowed_db) {
44 // if we find the exact db name, we stop here
45 if ($allowed_db == $db) {
46 $is_create_table_priv = TRUE;
47 break;
50 // '*' indicates a global CREATE priv
51 if ($allowed_db == '*') {
52 $is_create_table_priv = TRUE;
53 break;
56 if (ereg('%|_', $allowed_db)) {
57 // take care of wildcards and escaped wildcards,
58 // transforming them into regexp patterns
59 $max_position = strlen($allowed_db) - 1;
60 $i = 0;
61 $pattern = '';
62 while ($i <= $max_position) {
63 if ($allowed_db[$i] == '\\'){
64 if ($i < $max_position - 1 && $allowed_db[$i+1] == '_'){
65 $chunk = '_';
66 $i++;
67 } elseif ($i < $max_position - 1 && $allowed_db[$i+1] == '%'){
68 $chunk = '%';
69 $i++;
70 } else {
71 $chunk = $allowed_db[$i];
73 } elseif ($allowed_db[$i] == '_'){
74 $chunk = '.';
75 } elseif ($allowed_db[$i] == '%'){
76 $chunk = '(.)*';
77 } else {
78 $chunk = $allowed_db[$i];
80 $pattern .= $chunk;
81 $i++;
82 } // end while
83 unset($i, $max_position, $chunk);
85 $matches = '';
86 if (preg_match('@' .$pattern . '@i', $db, $matches)) {
87 if ($matches[0] == $db) {
88 $is_create_table_priv = TRUE;
89 break;
90 //TODO: maybe receive in $allowed_db also the db names
91 // on which we cannot CREATE, and check them
92 // in this foreach, because if a user is allowed to CREATE
93 // on db foo% but forbidden on db foobar, he should not
94 // see the Create table dialog
98 } // end foreach
99 unset($i, $max_position, $chunk, $pattern);
102 <form method="post" action="tbl_create.php"
103 onsubmit="return (emptyFormElements(this, 'table') &amp;&amp; checkFormElementInRange(this, 'num_fields', '<?php echo str_replace('\'', '\\\'', $GLOBALS['strInvalidFieldCount']); ?>', 1))">
104 <fieldset>
105 <legend>
106 <?php
107 if ($GLOBALS['cfg']['PropertiesIconic']) {
108 echo '<img class="icon" src="' . $pmaThemeImage . 'b_newtbl.png" width="16" height="16" alt="" />';
110 echo sprintf($strCreateNewTable, PMA_getDbLink());
112 </legend>
113 <?php if ($is_create_table_priv) { ?>
114 <?php echo PMA_generate_common_hidden_inputs($db); ?>
115 <div class="formelement">
116 <?php echo $strName; ?>:
117 <input type="text" name="table" maxlength="64" size="30" />
118 </div>
119 <div class="formelement">
120 <?php echo $strNumberOfFields; ?>:
121 <input type="text" name="num_fields" size="2" />
122 </div>
123 <div class="clearfloat"></div>
124 </fieldset>
125 <fieldset class="tblFooters">
126 <input type="submit" value="<?php echo $strGo; ?>" />
127 <?php } else { ?>
128 <div class="error"><?php echo $strNoPrivileges; ?></div>
129 <?php } // end if else ?>
130 </fieldset>
131 </form>