XOOPS 2.5.6  Final
 All Classes Namespaces Files Functions Variables Pages
prefix_manager.php
Go to the documentation of this file.
1 <?php
2 include '../../../include/cp_header.php';
3 include 'admin_header.php';
4 require_once dirname(dirname(__FILE__)).'/class/gtickets.php' ;
6 
7 // COPY TABLES
8 if( ! empty( $_POST['copy'] ) && ! empty( $_POST['old_prefix'] ) ) {
9 
10  if( preg_match( '/[^0-9A-Za-z_-]/' , $_POST['new_prefix'] ) ) die( 'wrong prefix' ) ;
11 
12  // Ticket check
13  if ( ! $xoopsGTicket->check( true , 'protector_admin' ) ) {
14  redirect_header(XOOPS_URL.'/',3,$xoopsGTicket->getErrors());
15  }
16 
17  $new_prefix = empty( $_POST['new_prefix'] ) ? 'x' . substr( md5( time() ) , -5 ) : $_POST['new_prefix'] ;
18  $old_prefix = $_POST['old_prefix'] ;
19 
20  $srs = $db->queryF( 'SHOW TABLE STATUS FROM `'.XOOPS_DB_NAME.'`' ) ;
21 
22  if( ! $db->getRowsNum( $srs ) ) die( "You are not allowed to copy tables" ) ;
23 
24  $count = 0;
25  while( $row_table = $db->fetchArray( $srs ) ) {
26  $count ++ ;
27  $old_table = $row_table['Name'] ;
28  if( substr( $old_table , 0 , strlen( $old_prefix ) + 1 ) !== $old_prefix . '_' ) continue ;
29 
30  $new_table = $new_prefix . substr( $old_table , strlen( $old_prefix ) ) ;
31 
32  $crs = $db->queryF( 'SHOW CREATE TABLE '.$old_table ) ;
33  if( ! $db->getRowsNum( $crs ) ) {
34  echo "error: SHOW CREATE TABLE ($old_table)<br />\n" ;
35  continue ;
36  }
37  $row_create = $db->fetchArray( $crs ) ;
38  $create_sql = preg_replace( "/^CREATE TABLE `$old_table`/" , "CREATE TABLE `$new_table`" , $row_create['Create Table'] , 1 ) ;
39 
40  $crs = $db->queryF( $create_sql ) ;
41  if( ! $crs ) {
42  echo "error: CREATE TABLE ($new_table)<br />\n" ;
43  continue ;
44  }
45 
46  $irs = $db->queryF( "INSERT INTO `$new_table` SELECT * FROM `$old_table`" ) ;
47  if( ! $irs ) {
48  echo "error: INSERT INTO ($new_table)<br />\n" ;
49  continue ;
50  }
51 
52  }
53 
54  $_SESSION['protector_logger'] = $xoopsLogger->dump('queries') ;
55 
56  redirect_header( 'index.php?page=prefix_manager' , 1 , _AM_MSG_DBUPDATED ) ;
57  exit ;
58 
59 // DUMP INTO A LOCAL FILE
60 } else if( ! empty( $_POST['backup'] ) && ! empty( $_POST['prefix'] ) ) {
61 
62  if( preg_match( '/[^0-9A-Za-z_-]/' , $_POST['prefix'] ) ) die( 'wrong prefix' ) ;
63 
64  // Ticket check
65  if ( ! $xoopsGTicket->check( true , 'protector_admin' ) ) {
66  redirect_header(XOOPS_URL.'/',3,$xoopsGTicket->getErrors());
67  }
68 
69  $prefix = $_POST['prefix'] ;
70 
71  // get table list
72  $srs = $db->queryF( 'SHOW TABLE STATUS FROM `'.XOOPS_DB_NAME.'`' ) ;
73  if( ! $db->getRowsNum( $srs ) ) die( "You are not allowed to delete tables" ) ;
74 
75  $export_string = '' ;
76 
77  while( $row_table = $db->fetchArray( $srs ) ) {
78  $table = $row_table['Name'] ;
79  if( substr( $table , 0 , strlen( $prefix ) + 1 ) !== $prefix . '_' ) continue ;
80  $drs = $db->queryF( "SHOW CREATE TABLE `$table`" ) ;
81  $export_string .= "\nDROP TABLE IF EXISTS `$table`;\n".mysql_result($drs,0,1).";\n\n" ;
82  $result = mysql_query( "SELECT * FROM `$table`" ) ;
83  $fields_cnt = mysql_num_fields( $result ) ;
84  $field_flags = array();
85  for ($j = 0; $j < $fields_cnt; $j++) {
86  $field_flags[$j] = mysql_field_flags( $result , $j ) ;
87  }
88  $search = array("\x00", "\x0a", "\x0d", "\x1a");
89  $replace = array('\0', '\n', '\r', '\Z');
90  $current_row = 0;
91  while( $row = mysql_fetch_row($result) ) {
92  $current_row ++ ;
93  for( $j = 0 ; $j < $fields_cnt ; $j ++ ) {
94  $fields_meta = mysql_fetch_field( $result , $j ) ;
95  // NULL
96  if (!isset($row[$j]) || is_null($row[$j])) {
97  $values[] = 'NULL';
98  // a number
99  // timestamp is numeric on some MySQL 4.1
100  } elseif ($fields_meta->numeric && $fields_meta->type != 'timestamp') {
101  $values[] = $row[$j];
102  // a binary field
103  // Note: with mysqli, under MySQL 4.1.3, we get the flag
104  // "binary" for those field types (I don't know why)
105  } else if (stristr($field_flags[$j], 'BINARY')
106  && $fields_meta->type != 'datetime'
107  && $fields_meta->type != 'date'
108  && $fields_meta->type != 'time'
109  && $fields_meta->type != 'timestamp'
110  ) {
111  // empty blobs need to be different, but '0' is also empty :-(
112  if (empty($row[$j]) && $row[$j] != '0') {
113  $values[] = '\'\'';
114  } else {
115  $values[] = '0x' . bin2hex($row[$j]);
116  }
117  // something else -> treat as a string
118  } else {
119  $values[] = '\'' . str_replace($search, $replace, addslashes($row[$j])) . '\'';
120  } // end if
121  } // end for
122 
123  $export_string .= "INSERT INTO `$table` VALUES (" . implode(', ', $values) . ");\n" ;
124  unset($values);
125 
126  } // end while
127  mysql_free_result( $result ) ;
128 
129  }
130 
131  header('Content-Type: Application/octet-stream') ;
132  header('Content-Disposition: attachment; filename="'.$prefix.'_'.date('YmdHis').'.sql"') ;
133  header('Content-Length: '.strlen($export_string)) ;
134  set_time_limit( 0 ) ;
135  echo $export_string ;
136  exit ;
137 
138 // DROP TABLES
139 } else if( ! empty( $_POST['delete'] ) && ! empty( $_POST['prefix'] ) ) {
140 
141  if( preg_match( '/[^0-9A-Za-z_-]/' , $_POST['prefix'] ) ) die( 'wrong prefix' ) ;
142 
143  // Ticket check
144  if ( ! $xoopsGTicket->check( true , 'protector_admin' ) ) {
145  redirect_header(XOOPS_URL.'/',3,$xoopsGTicket->getErrors());
146  }
147 
148  $prefix = $_POST['prefix'] ;
149 
150  // check if prefix is working
151  if( $prefix == XOOPS_DB_PREFIX ) die( "You can't drop working tables" ) ;
152 
153  // check if prefix_xoopscomments exists
154  $check_rs = $db->queryF( "SELECT * FROM {$prefix}_xoopscomments LIMIT 1" ) ;
155  if( ! $check_rs ) die( "This is not a prefix for XOOPS" ) ;
156 
157  // get table list
158  $srs = $db->queryF( 'SHOW TABLE STATUS FROM `'.XOOPS_DB_NAME.'`' ) ;
159  if( ! $db->getRowsNum( $srs ) ) die( "You are not allowed to delete tables" ) ;
160 
161  while( $row_table = $db->fetchArray( $srs ) ) {
162  $table = $row_table['Name'] ;
163  if( substr( $table , 0 , strlen( $prefix ) + 1 ) !== $prefix . '_' ) continue ;
164  $drs = $db->queryF( "DROP TABLE `$table`" ) ;
165  }
166 
167  $_SESSION['protector_logger'] = $xoopsLogger->dump('queries') ;
168 
169  redirect_header( 'index.php?page=prefix_manager' , 1 , _AM_MSG_DBUPDATED ) ;
170  exit ;
171 }
172 
173 
174 // beggining of Output
176 include dirname(__FILE__).'/mymenu.php' ;
177 
178 // query
179 $srs = $db->queryF( "SHOW TABLE STATUS FROM `".XOOPS_DB_NAME.'`' ) ;
180 if( ! $db->getRowsNum( $srs ) ) {
181  die( "You are not allowed to copy tables" ) ;
182  xoops_cp_footer() ;
183  exit ;
184 }
185 
186 // search prefixes
187 $tables = array() ;
188 $prefixes = array() ;
189 while( $row_table = $db->fetchArray( $srs ) ) {
190  if( substr( $row_table["Name"] , -6 ) === '_users' ) {
191  $prefixes[] = array(
192  'name' => substr( $row_table["Name"] , 0 , -6 ) ,
193  'updated' => $row_table["Update_time"]
194  ) ;
195  }
196  $tables[] = $row_table["Name"] ;
197 }
198 
199 
200 // table
201 echo "
202 <h3>"._AM_H3_PREFIXMAN."</h3>
203 <table class='outer' width='95%'>
204  <tr>
205  <th>PREFIX</th>
206  <th>TABLES</th>
207  <th>UPDATED</th>
208  <th>COPY</th>
209  <th>ACTIONS</th>
210  </tr>
211 " ;
212 
213 foreach( $prefixes as $prefix ) {
214 
215  // count the number of tables with the prefix
216  $table_count = 0 ;
217  $has_xoopscomments = false ;
218  foreach( $tables as $table ) {
219  if( $table == $prefix['name'] . '_xoopscomments' ) $has_xoopscomments = true ;
220  if( substr( $table , 0 , strlen( $prefix['name'] ) + 1 ) === $prefix['name'] . '_' ) $table_count ++ ;
221  }
222 
223  // check if prefix_xoopscomments exists
224  if( ! $has_xoopscomments ) continue ;
225 
226  $prefix4disp = htmlspecialchars( $prefix['name'] , ENT_QUOTES ) ;
227  $ticket_input = $xoopsGTicket->getTicketHtml( __LINE__ , 1800 , 'protector_admin' ) ;
228 
229  if( $prefix['name'] == XOOPS_DB_PREFIX ) {
230  $del_button = '' ;
231  $style_append = 'background-color:#FFFFFF' ;
232  } else {
233  $del_button = "<input type='submit' name='delete' value='delete' onclick='return confirm(\""._AM_CONFIRM_DELETE."\")' />" ;
234  $style_append = '' ;
235  }
236 
237  echo "
238  <tr>
239  <td class='odd' style='$style_append;'>$prefix4disp</td>
240  <td class='odd' style='text-align:right;$style_append;'>$table_count</td>
241  <td class='odd' style='text-align:right;$style_append;'>{$prefix['updated']}</td>
242  <td class='odd' style='text-align:center;$style_append;' nowrap='nowrap'>
243  <form action='?page=prefix_manager' method='POST' style='margin:0px;'>
244  $ticket_input
245  <input type='hidden' name='old_prefix' value='$prefix4disp' />
246  <input type='text' name='new_prefix' size='8' maxlength='16' />
247  <input type='submit' name='copy' value='copy' />
248  </form>
249  </td>
250  <td class='odd' style='text-align:center;$style_append;'>
251  <form action='?page=prefix_manager' method='POST' style='margin:0px;'>
252  $ticket_input
253  <input type='hidden' name='prefix' value='$prefix4disp' />
254  $del_button
255  <input type='submit' name='backup' value='backup' onclick='this.form.target=\"_blank\"' />
256  </form>
257  </td>
258  </tr>\n" ;
259 
260 }
261 
262 echo "
263 </table>
264 <p>".sprintf(_AM_TXT_HOWTOCHANGEDB,XOOPS_ROOT_PATH,XOOPS_DB_PREFIX)."</p>
265 
266 " ;
267 
268 // Display Log if exists
269 if( ! empty( $_SESSION['protector_logger'] ) ) {
270  echo $_SESSION['protector_logger'] ;
271  $_SESSION['protector_logger'] = '' ;
272  unset( $_SESSION['protector_logger'] ) ;
273 }
274 
276 ?>