Plugin index

<?php
if (txpinterface == 'admin' and gps('event') == 'rvm_latin1_to_utf8')
{
  
add_privs('rvm_latin1_to_utf8''1');
  
register_tab('extensions''rvm_latin1_to_utf8''latin1_to_utf8');
  
register_callback('rvm_latin1_to_utf8''rvm_latin1_to_utf8');
}

function 
rvm_latin1_to_utf8()
{
  global 
$txpcfg$DB;

  
$debug FALSE;

  
require_privs('rvm_latin1_to_utf8');

  
pagetop('');

  echo 
'<div style="margin-left: 50px">';

  if (
version_compare(mysqli_get_server_info($DB->link), '4.1.0''<'))
  {
    echo 
graf('The rvm_latin1_to_utf8 plugin has been removed, because your MySQL version is lower than 4.1.');
    
safe_delete('txp_plugin'"name = 'rvm_latin1_to_utf8'");
    
rvm_latin1_to_utf8_end();
  }

  
# standard TXP tables
  
$txptables = array(
    
'textpattern',
    
'txp_category',
    
'txp_css',
    
'txp_discuss',
    
'txp_discuss_ipban',
    
'txp_discuss_nonce',
    
'txp_file',
    
'txp_form',
    
'txp_image',
    
'txp_lang',
    
'txp_link',
    
'txp_log',
    
'txp_page',
    
'txp_plugin',
    
'txp_prefs',
    
'txp_priv',
    
'txp_section',
    
'txp_token',
    
'txp_users'
  
);

  
# find tables that match the TXP table prefix, mark standard and selected ones.
  
$mytables ps('mytables', array()) ? ps('mytables') : array();
  
$unknown  FALSE;

  if (
$rs safe_query("SHOW TABLES LIKE '".addcslashes(doSlash(PFX), '%_')."%'"))
  {
    while (
$row mysqli_fetch_row($rs))
    {
      
$table substr($row[0], strlen(PFX));

      if (
in_array($table$txptables) or in_array($table$mytables))
      {
        
$tables[] = $table;
      }
      else
      {
        
$unknown TRUE;
      }

      
$inputs[] =
        
'<label>'.
          
'<input type="checkbox" name="mytables[]" value="'.htmlspecialchars($table).'" '.
            (
in_array($table$txptables) ? 'disabled="disabled" checked="checked"' '').' />'.
          
htmlspecialchars(PFX.$table).
        
'</label>';
    }
  }
  else
  {
    echo 
graf('Hmmm... strange, I cannot find any tables that match your TXP table prefix');
  }

  
# ask user what to do with tables we're not sure about.
  
if ($unknown and !ps('continue'))
  {
    echo
      
graf('Below, a list of tables is shown that may be part of your Textpattern install.').
      
graf('Some of these tables may have been created by plugins you have installed.<br />
        In that case, check the tables that these plugins have added.<br />
        Be careful not to check tables that are not related to this Textpattern install!'
).
      
graf('The standard Textpattern tables are already checked.').
      
form(
        
join('<br />'$inputs).
        
graf(
          
eInput('rvm_latin1_to_utf8').'<br />'.
          
fInput('submit''continue''Continue''publish')
        )
      );

    
rvm_latin1_to_utf8_end();
  }

  echo 
graf('Updating Textpattern tables...');

  
# loop through tables
  
foreach ($tables as $table)
  {
    
$indexes = array();
    
$drop    = array();
    
$create  = array();
    
$bin     = array();
    
$utf8    = array();
    
$cols    = array();

    
# prepare alter statements for columns
    
$columns getRows('SHOW COLUMNS FROM '.safe_pfx($table));

    if (
$columns) foreach ($columns as $column)
    {
      
extract($column);

      if (
preg_match('/^(char|varchar|tinytext|text|mediumtext|longtext|enum|set)\b/'$Type))
      {
        
$Null = ($Null == 'YES' ' NULL' ' NOT NULL');

        
$begin 'MODIFY `'.doSlash($Field).'` '.$Type.' CHARACTER SET ';
        
$end   $Null." DEFAULT '".doSlash($Default)."'";

        
$bin[]  = $begin.'binary'.$end;
        
$utf8[] = $begin.'utf8 COLLATE utf8_general_ci'.$end;
        
$cols[] = $Field;
      }
    }

    
# analyse index structures
    
$rows getRows('SHOW INDEX FROM '.safe_pfx($table));

    if (
$rows) foreach ($rows as $row)
    {
      
extract($row);

      if (
$Key_name == 'PRIMARY' or !in_array($Column_name$cols))
      {
        continue;
      }

      
$Key_name '`'.$Key_name.'`';

      
$indexes[$Key_name]['unique'] = ($Non_unique '' ' UNIQUE');
      
$indexes[$Key_name]['fulltext'] = ($Index_type == 'FULLTEXT' ' FULLTEXT' '');
      
$indexes[$Key_name]['columns'][$Seq_in_index] = '`'.$Column_name.'`'.($Sub_part ' ('.$Sub_part.')' '');
    }

    
# prepare index drop/create statements
    
foreach ($indexes as $index => $structure)
    {
      
extract($structure);

      
$drop[]   = 'DROP INDEX '.$index;
      
$create[] = 'ADD'.$unique.$fulltext.' INDEX '.$index.' ('.join(', '$columns).')';
    }

    
$success TRUE;

    
# alter the table + indexes
    
if ($drop)
    {
      
$success *= safe_alter($tablejoin(', '$drop), $debug);
    }

    if (
$bin and $success)
    {
      
$success *= safe_alter($tablejoin(', '$bin), $debug);

      if (
$success)
      {
        
$success *= safe_alter($tablejoin(', '$utf8).', DEFAULT CHARACTER SET utf8 COLLATE utf8_general_ci'$debug);
      }
    }

    if (
$create and $success)
    {
      
$success *= safe_alter($tablejoin(', '$create), $debug);
    }
  }

  
# while we're here, might as well optimize the tables
  
safe_query("OPTIMIZE TABLE ".join(', 'array_map('safe_pfx'$tables)), $debug);

  if (
$success)
  {
    echo 
graf('Successfully completed.');

    
$file   txpath.'/config.php';
    
$config file_get_contents($file);
    
$config str_replace('?>'n.'# rvm_latin1_to_utf8 plugin added the following line to ensure a correct dbcharset'.n.'$txpcfg[\'dbcharset\'] = \'utf8\''.n.'?>'$config);

    if (
$txpcfg['dbcharset'] == 'utf8'
      
or is_writable($file)
      and 
$handle fopen($file'w')
      and 
fwrite($handle$config) !== FALSE
      
and fclose($handle))
    {
      echo 
graf('The rvm_latin1_to_utf8 plugin has been automatically de-installed.');
      
safe_delete('txp_plugin'"name = 'rvm_latin1_to_utf8'");
    }
    else
    {
      echo 
graf('<b>WARNING: textpattern/config.php could not be updated automatically. Please update your textpattern/config.php file to contain <code>$txpcfg[\'dbcharset\'] = \'utf8\';</code></b> and deinstall the rvm_latin1_to_utf8 plugin manually');
    }
  }
  else
  {
    echo 
graf('Due to one or more errors the tables could not be updated correctly. You may have to restore a backup.');
  }

  
rvm_latin1_to_utf8_end();
}

function 
rvm_latin1_to_utf8_end()
{
  echo 
'</div>';
  
end_page();
  exit();
}
?>