upgrade.php
changeset 0 902822492a68
child 16 64e0d3d4cf14
equal deleted inserted replaced
-1:000000000000 0:902822492a68
       
     1 <?php
       
     2 
       
     3 /*
       
     4  * Enano - an open-source CMS capable of wiki functions, Drupal-like sidebar blocks, and everything in between
       
     5  * Version 1.0 (Banshee)
       
     6  * upgrade.php - upgrade script
       
     7  * Copyright (C) 2006-2007 Dan Fuhry
       
     8  *
       
     9  * This program is Free Software; you can redistribute and/or modify it under the terms of the GNU General Public License
       
    10  * as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version.
       
    11  *
       
    12  * This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied
       
    13  * warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for details.
       
    14  */
       
    15 
       
    16 define('IN_ENANO_INSTALL', 'true');   
       
    17 
       
    18 if(!defined('scriptPath')) {
       
    19   $sp = dirname($_SERVER['REQUEST_URI']);
       
    20   if($sp == '/' || $sp == '\\') $sp = '';
       
    21   define('scriptPath', $sp);
       
    22 }
       
    23 
       
    24 if(!defined('contentPath')) {
       
    25   $sp = dirname($_SERVER['REQUEST_URI']);
       
    26   if($sp == '/' || $sp == '\\') $sp = '';
       
    27   define('contentPath', $sp);
       
    28 }
       
    29 
       
    30 global $_starttime, $this_page, $sideinfo;
       
    31 $_starttime = microtime(true);
       
    32 
       
    33 define('ENANO_ROOT', dirname(__FILE__));
       
    34 require(ENANO_ROOT.'/includes/constants.php');
       
    35 
       
    36 if(defined('ENANO_DEBUG'))
       
    37 {
       
    38   require_once(ENANO_ROOT.'/includes/debugger/debugConsole.php');
       
    39 }
       
    40 else
       
    41 {
       
    42   function dc_here($m)     { return false; }
       
    43   function dc_dump($a, $g) { return false; }
       
    44   function dc_watch($n)    { return false; }
       
    45   function dc_start_timer($u) { return false; }
       
    46   function dc_stop_timer($m) { return false; }
       
    47 }
       
    48 
       
    49 // SCRIPT CONFIGURATION
       
    50 // Everything related to versions goes here!
       
    51 
       
    52 // Valid versions to upgrade from
       
    53 $valid_versions = Array('1.0b1', '1.0b2', '1.0b3', '1.0b4', '1.0RC1', '1.0RC2');
       
    54 
       
    55 // Basically a list of dependencies, which should be resolved automatically
       
    56 // If, for example, if upgrading from 1.0b1 to 1.0RC1 requires one extra query that would not
       
    57 // normally be required (for whatever reason) then you would add a custom version number to the array under key '1.0b1'.
       
    58 $deps_list = Array(
       
    59     '1.0b1' => Array('1.0b2'),
       
    60     '1.0b2' => Array('1.0b3'),
       
    61     '1.0b3' => Array('1.0b4'),
       
    62     '1.0b4' => Array('1.0RC1'),
       
    63     '1.0RC1' => Array('1.0RC2')
       
    64   );
       
    65 $this_version   = '1.0';
       
    66 $func_list = Array(
       
    67     '1.0b4' => Array('u_1_0_RC1_update_user_ids', 'u_1_0_RC1_add_admins_to_group', 'u_1_0_RC1_alter_files_table', 'u_1_0_RC1_destroy_session_cookie', 'u_1_0_RC1_set_contact_email', 'u_1_0_RC1_update_page_text'),
       
    68     '1.0RC2' => Array('u_1_0_populate_userpage_comments')
       
    69   );
       
    70 
       
    71 if(!isset($_GET['mode'])) 
       
    72 {
       
    73   $_GET['mode'] = 'login';
       
    74 }
       
    75 
       
    76 function err($t)
       
    77 {
       
    78   global $template;
       
    79   echo $t;
       
    80   $template->footer(); 
       
    81   exit;
       
    82 }
       
    83 
       
    84 require(ENANO_ROOT.'/includes/template.php');
       
    85 
       
    86 // Initialize the session manager
       
    87 require(ENANO_ROOT.'/includes/functions.php');
       
    88 require(ENANO_ROOT.'/includes/dbal.php');
       
    89 require(ENANO_ROOT.'/includes/paths.php');
       
    90 require(ENANO_ROOT.'/includes/sessions.php');
       
    91 require(ENANO_ROOT.'/includes/plugins.php');
       
    92 require(ENANO_ROOT.'/includes/rijndael.php');
       
    93 require(ENANO_ROOT.'/includes/render.php');
       
    94 $db = new mysql();
       
    95 $db->connect();
       
    96 
       
    97 $plugins = new pluginLoader();
       
    98 
       
    99 if(!defined('ENANO_CONFIG_FETCHED'))
       
   100 {
       
   101   // Select and fetch the site configuration
       
   102   $e = $db->sql_query('SELECT config_name, config_value FROM '.table_prefix.'config;');
       
   103   if ( !$e )
       
   104   {
       
   105     $db->_die('Some critical configuration information could not be selected.');
       
   106   }
       
   107   else
       
   108   {
       
   109     define('ENANO_CONFIG_FETCHED', ''); // Used in die_semicritical to figure out whether to call getConfig() or not
       
   110   }
       
   111   
       
   112   $enano_config = Array();
       
   113   while($r = $db->fetchrow())
       
   114   {
       
   115     $enano_config[$r['config_name']] = $r['config_value'];
       
   116   }
       
   117   $db->free_result();
       
   118 }
       
   119 
       
   120 $v = enano_version();
       
   121 if(in_array($v, Array(false, '', '1.0b3', '1.0b4')))
       
   122 {
       
   123   $ul_admin  = 2;
       
   124   $ul_mod    = 1;
       
   125   $ul_member = 0;
       
   126   $ul_guest  = -1;
       
   127 }
       
   128 else
       
   129 {
       
   130   $ul_admin  = USER_LEVEL_ADMIN;
       
   131   $ul_mod    = USER_LEVEL_MOD;
       
   132   $ul_member = USER_LEVEL_MEMBER;
       
   133   $ul_guest  = USER_LEVEL_GUEST;
       
   134 }
       
   135 
       
   136 $_GET['title'] = 'unset';
       
   137 
       
   138 $session = new sessionManager();
       
   139 $paths = new pathManager();
       
   140 $session->start();
       
   141 
       
   142 $template = new template_nodb();
       
   143 $template->load_theme('oxygen', 'bleu', false);
       
   144 
       
   145 $modestrings = Array(
       
   146               'login'      => 'Administrative login',
       
   147               'welcome'    => 'Welcome',
       
   148               'setversion' => 'Select Enano version',
       
   149               'confirm'    => 'Confirm upgrade',
       
   150               'upgrade'    => 'Database installation',
       
   151               'finish'     => 'Upgrade complete'
       
   152             );
       
   153 
       
   154 $sideinfo = '';
       
   155 $vars = $template->extract_vars('elements.tpl');
       
   156 $p = $template->makeParserText($vars['sidebar_button']);
       
   157 foreach ( $modestrings as $id => $str )
       
   158 {
       
   159   if ( $_GET['mode'] == $id )
       
   160   {
       
   161     $flags = 'style="font-weight: bold; text-decoration: underline;"';
       
   162     $this_page = $str;
       
   163   }
       
   164   else
       
   165   {
       
   166     $flags = '';
       
   167   }
       
   168   $p->assign_vars(Array(
       
   169       'HREF' => '#',
       
   170       'FLAGS' => $flags . ' onclick="return false;"',
       
   171       'TEXT' => $str
       
   172     ));
       
   173   $sideinfo .= $p->run();
       
   174 }
       
   175 
       
   176 $template->init_vars();
       
   177 
       
   178 function upg_assign_vars($schema)
       
   179 {
       
   180   $schema = str_replace('{{SITE_NAME}}',   mysql_real_escape_string(getConfig('site_name')), $schema);
       
   181   $schema = str_replace('{{SITE_DESC}}',   mysql_real_escape_string(getConfig('site_desc')), $schema);
       
   182   $schema = str_replace('{{COPYRIGHT}}',   mysql_real_escape_string(getConfig('copyright_notice')), $schema);
       
   183   $schema = str_replace('{{TABLE_PREFIX}}', table_prefix, $schema);
       
   184   if(getConfig('wiki_mode')=='1') $schema = str_replace('{{WIKI_MODE}}', '1', $schema);
       
   185   else $schema = str_replace('{{WIKI_MODE}}', '0', $schema);
       
   186   return $schema;
       
   187 }
       
   188 
       
   189 /* Version-specific functions */
       
   190 
       
   191 function u_1_0_RC1_update_user_ids()
       
   192 {
       
   193   global $db;
       
   194   // First, make sure this hasn't already been done
       
   195   $q = $db->sql_query('SELECT username FROM '.table_prefix.'users WHERE user_id=1;');
       
   196   if ( !$q )
       
   197     $db->_die();
       
   198   $row = $db->fetchrow();
       
   199   if ( $row['username'] == 'Anonymous' )
       
   200     return true;
       
   201   // Find the first unused user ID
       
   202   $used = Array();
       
   203   $q = $db->sql_query('SELECT user_id FROM '.table_prefix.'users');
       
   204   if ( !$q )
       
   205     $db->_die();
       
   206   $c = false;
       
   207   while ( $row = $db->fetchrow() )
       
   208   {
       
   209     $i = intval($row['user_id']);
       
   210     $used[$i] = true;
       
   211     if ( !isset($used[$i - 1]) && $c )
       
   212     {
       
   213       $id = $i - 1;
       
   214       break;
       
   215     }
       
   216     $c = true;
       
   217   }
       
   218   if ( !isset($id) )
       
   219     $id = $i + 1;
       
   220   $db->free_result();
       
   221   
       
   222   $q = $db->sql_query('UPDATE '.table_prefix.'users SET user_id=' . $id . ' WHERE user_id=1;');
       
   223   if(!$q)
       
   224     $db->_die();
       
   225   $q = $db->sql_query('UPDATE '.table_prefix.'users SET user_id=1 WHERE user_id=-1 AND username=\'Anonymous\';');
       
   226   if(!$q)
       
   227     $db->_die();
       
   228   
       
   229 }
       
   230 
       
   231 function u_1_0_RC1_add_admins_to_group()
       
   232 {
       
   233   global $db;
       
   234   $q = $db->sql_query('SELECT user_id FROM '.table_prefix.'users WHERE user_level=' . USER_LEVEL_ADMIN . ';');
       
   235   if ( !$q )
       
   236     $db->_die();
       
   237   $base = 'INSERT INTO '.table_prefix.'group_members(group_id,user_id) VALUES';
       
   238   $blocks = Array();
       
   239   while ( $row = $db->fetchrow($q) )
       
   240   {
       
   241     $blocks[] = '(2,' . $row['user_id'] . ')';
       
   242   }
       
   243   $blocks = implode(',', $blocks);
       
   244   $sql = $base . $blocks . ';';
       
   245   if(!$db->sql_query($sql))
       
   246     $db->_die();
       
   247 }
       
   248 
       
   249 function u_1_0_RC1_alter_files_table()
       
   250 {
       
   251   global $db;
       
   252   if(!is_dir(ENANO_ROOT.'/files'))
       
   253     @mkdir(ENANO_ROOT . '/files');
       
   254   if(!is_dir(ENANO_ROOT.'/files'))
       
   255     die('ERROR: Couldn\'t create files directory');
       
   256   $q = $db->sql_unbuffered_query('SELECT * FROM '.table_prefix.'files;', $db->_conn);
       
   257   if(!$q) $db->_die();
       
   258   while ( $row = $db->fetchrow() )
       
   259   {
       
   260     $file_data = base64_decode($row['data']);
       
   261     $path = ENANO_ROOT . '/files/' . md5( $row['filename'] . '_' . $file_data ) . '_' . $row['time_id'] . $row['file_extension'];
       
   262     @unlink($path);
       
   263     $handle = @fopen($path, 'w');
       
   264     if(!$handle)
       
   265       die('fopen failed');
       
   266     fwrite($handle, $file_data);
       
   267     fclose($handle);
       
   268     
       
   269   }
       
   270   
       
   271   $q = $db->sql_query('ALTER TABLE '.table_prefix.'files DROP PRIMARY KEY, ADD COLUMN file_id int(12) NOT NULL auto_increment FIRST, ADD PRIMARY KEY (file_id), ADD COLUMN file_key varchar(32) NOT NULL;');
       
   272   if(!$q) $db->_die();
       
   273   
       
   274   $list = Array();
       
   275   $q = $db->sql_unbuffered_query('SELECT * FROM '.table_prefix.'files;', $db->_conn);
       
   276   if(!$q) $db->_die();
       
   277   while ( $row = $db->fetchrow($q) )
       
   278   {
       
   279     $file_data = base64_decode($row['data']);
       
   280     $key = md5( $row['filename'] . '_' . $file_data );
       
   281     $list[] = 'UPDATE '.table_prefix.'files SET file_key=\'' . $key . '\' WHERE file_id=' . $row['file_id'] . ';';
       
   282   }
       
   283   
       
   284   foreach ( $list as $sql )
       
   285   {
       
   286     if(!$db->sql_query($sql)) $db->_die();
       
   287   }
       
   288   
       
   289   if(!$db->sql_query('ALTER TABLE '.table_prefix.'files DROP data')) $db->_die();
       
   290   
       
   291 }
       
   292 
       
   293 function u_1_0_RC1_destroy_session_cookie()
       
   294 {
       
   295   unset($_COOKIE['sid']);
       
   296   setcookie('sid', '', time()-3600*24, scriptPath);
       
   297   setcookie('sid', '', time()-3600*24, scriptPath.'/');
       
   298 }
       
   299 
       
   300 function u_1_0_RC1_set_contact_email()
       
   301 {
       
   302   global $db;
       
   303   $q = $db->sql_query('SELECT email FROM '.table_prefix.'users WHERE user_level='.USER_LEVEL_ADMIN.' ORDER BY user_level ASC LIMIT 1;');
       
   304   if(!$q)
       
   305     $db->_die();
       
   306   $row = $db->fetchrow();
       
   307   setConfig('contact_email', $row['email']);
       
   308 }
       
   309 
       
   310 function u_1_0_RC1_update_page_text()
       
   311 {
       
   312   global $db;
       
   313   $q = $db->sql_unbuffered_query('SELECT page_id,namespace,page_text,char_tag FROM '.table_prefix.'page_text');
       
   314   if (!$q)
       
   315     $db->_die();
       
   316   
       
   317   $qs = array();
       
   318   
       
   319   while ( $row = $db->fetchrow($q) )
       
   320   {
       
   321     $row['page_text'] = str_replace(Array(
       
   322       "{QUOT:{$row['char_tag']}}",
       
   323       "{APOS:{$row['char_tag']}}",
       
   324       "{SLASH:{$row['char_tag']}}"
       
   325       ), Array(
       
   326       '"', "'", '\\'
       
   327       ), $row['page_text']);
       
   328     $qs[] = 'UPDATE '.table_prefix.'page_text SET page_text=\'' . mysql_real_escape_string($row['page_text']) . '\'
       
   329       WHERE page_id=\'' . mysql_real_escape_string($row['page_id']) . '\' AND
       
   330             namespace=\'' . mysql_real_escape_string($row['namespace']) . '\';';
       
   331   }
       
   332   
       
   333   foreach($qs as $query)
       
   334   {
       
   335     if(!$db->sql_query($query))
       
   336       $db->_die();
       
   337   }
       
   338 }
       
   339 
       
   340 function u_1_0_populate_userpage_comments()
       
   341 {
       
   342   global $db;
       
   343   $q = $db->sql_query('SELECT COUNT(c.comment_id) AS num_comments...');
       
   344   if ( !$q )
       
   345     $db->_die();
       
   346   
       
   347   while ( $row = $db->fetchrow() )
       
   348   {
       
   349     
       
   350   }
       
   351 }
       
   352 
       
   353 switch($_GET['mode'])
       
   354 {
       
   355   case "login":
       
   356     if($session->user_logged_in && $session->user_level >= $ul_admin)
       
   357     {
       
   358       if(isset($_POST['login']))
       
   359       {
       
   360         $session->login_without_crypto($_POST['username'], $_POST['password'], false, $ul_admin);
       
   361         if($session->sid_super)
       
   362         {
       
   363           header('Location: upgrade.php?mode=welcome&auth='.$session->sid_super);
       
   364           exit;
       
   365         }
       
   366       }
       
   367       $template->header();
       
   368       ?>
       
   369       <form action="upgrade.php?mode=login" method="post">
       
   370       <table border="0" style="margin-left: auto; margin-right: auto; margin-top: 5px;" cellspacing="1" cellpadding="4">
       
   371         <tr>
       
   372           <th colspan="2">You must re-authenticate to perform this upgrade.</th>
       
   373         </tr>
       
   374         <?php
       
   375         if(isset($_POST['login']))
       
   376         {
       
   377           echo '<tr><td colspan="2"><p style="color: red;">Login failed. Bad password?</p></td></tr>';
       
   378         }
       
   379         ?>
       
   380         <tr>
       
   381           <td>Username:</td><td><input type="text" name="username" size="30" /></td>
       
   382         </tr>
       
   383         <tr>
       
   384           <td>Password:</td><td><input type="password" name="password" size="30" /></td>
       
   385         </tr>
       
   386         <tr>
       
   387           <td colspan="2" style="text-align: center;"><input type="submit" name="login" value="Log in" />
       
   388         </tr>
       
   389       </table>
       
   390       </form>
       
   391       <?php
       
   392     }
       
   393     else
       
   394     {
       
   395       if(isset($_POST['login']))
       
   396       {
       
   397         $result = $session->login_without_crypto($_POST['username'], $_POST['password'], false, $ul_member);
       
   398         if($result == 'success')
       
   399         {
       
   400           header('Location: upgrade.php');
       
   401           exit;
       
   402         }
       
   403       }
       
   404       $template->header();
       
   405       ?>
       
   406       <form action="upgrade.php?mode=login" method="post">
       
   407       <table border="0" style="margin-left: auto; margin-right: auto; margin-top: 5px;" cellspacing="1" cellpadding="4">
       
   408         <tr>
       
   409           <th colspan="2">Please log in to continue with this upgrade.</th>
       
   410         </tr>
       
   411         <?php
       
   412         if(isset($_POST['login']))
       
   413         {
       
   414           echo '<tr><td colspan="2"><p style="color: red;">Login failed. Bad password?</p></td></tr>';
       
   415         }
       
   416         ?>
       
   417         <tr>
       
   418           <td>Username:</td><td><input type="text" name="username" size="30" /></td>
       
   419         </tr>
       
   420         <tr>
       
   421           <td>Password:</td><td><input type="password" name="password" size="30" /></td>
       
   422         </tr>
       
   423         <tr>
       
   424           <td colspan="2" style="text-align: center;"><input type="submit" name="login" value="Log in" />
       
   425         </tr>
       
   426       </table>
       
   427       </form>
       
   428       <?php
       
   429     }
       
   430     break;
       
   431   case "welcome":
       
   432     if(!$session->sid_super) { $template->header(); echo '<p>No admin session found! Please <a href="upgrade.php">restart the upgrade</a>.</p>'; $template->footer(); exit; }
       
   433     
       
   434     // Just show a simple welcome page to display version information
       
   435     $template->header();
       
   436     require('config.php');
       
   437     
       
   438     ?>
       
   439     
       
   440     <div style="text-align: center; margin-top: 10px;">
       
   441       <img alt="[ Enano CMS Project logo ]" src="images/enano-artwork/installer-greeting-blue.png" style="display: block; margin: 0 auto; padding-left: 134px;" />
       
   442       <h2>Welcome to the Enano upgrade wizard</h2>
       
   443       <?php
       
   444       if ( file_exists('./_nightly.php') )
       
   445       {
       
   446         echo '<div class="warning-box" style="text-align: left; margin: 10px auto; display: table; width: 60%;"><b>You are about to upgrade to a NIGHTLY BUILD of Enano.</b><br />Nightly builds CANNOT be re-upgraded to the final release. They may also contain serious flaws, security problems, or extraneous debugging information. Continuing this process on a production site is NOT recommended.</div>';
       
   447       }
       
   448       ?>
       
   449     </div>
       
   450     <div style="display: table; margin: 0 auto;">
       
   451       <p>You are about to upgrade Enano to version <b><?php echo $this_version; ?></b>. Before you continue, please ensure that:</p>
       
   452       <ul>
       
   453         <li>You have completely backed up your database (<b><?php echo "$dbhost:$dbname"; ?></b>)</li>
       
   454         <li>You have backed up the entire Enano directory (<b><?php echo ENANO_ROOT; ?></b>)</li>
       
   455         <li>You have reviewed the release notes for this version, and you<br />are comfortable with any known bugs or issues</li>
       
   456       </ul>
       
   457     </div>
       
   458     <div style="text-align: center; margin-top: 10px;">
       
   459       <form action="upgrade.php?mode=setversion&amp;auth=<?php echo $session->sid_super; ?>" method="post">
       
   460         <input type="submit" value="Continue with upgrade" />
       
   461       </form>
       
   462     </div>
       
   463     
       
   464     <?php
       
   465     
       
   466     break;
       
   467   case "setversion":
       
   468     if(!$session->sid_super) { $template->header(); echo '<p>No admin session found! Please <a href="upgrade.php">restart the upgrade</a>.</p>'; $template->footer(); exit; }
       
   469     $v = ( function_exists('enano_version') ) ? enano_version() : '';
       
   470     if(!in_array($v, $valid_versions) && $v != '')
       
   471     {
       
   472       $template->header();
       
   473       ?>
       
   474       <p>Your version of Enano (<?php echo $v; ?>) can't be upgraded to this version (<?php echo $this_version; ?>).</p>
       
   475       <?php
       
   476       break;
       
   477     } elseif($v == '') {
       
   478       // OK, we don't know which version he's running. So we'll cheat ;-)
       
   479       $template->header();
       
   480       echo "<form action='upgrade.php?mode=confirm&amp;auth={$session->sid_super}' method='post'>";
       
   481       ?>
       
   482       <p>Sorry, we couldn't detect which version of Enano you're running on your server. Please select which version of Enano you have below, and make absolutely sure that you're correct.</p>
       
   483       <p><select name="version"><?php
       
   484         foreach($valid_versions as $c)
       
   485         {
       
   486           echo "<option value='{$c}'>{$c}</option>";
       
   487         }
       
   488       ?></select></p>
       
   489       <p>
       
   490         <input type="submit" value="Continue" />
       
   491       </p>
       
   492       <?php
       
   493       echo `</form>`;
       
   494       break;
       
   495     } else {
       
   496       header('Location: upgrade.php?mode=confirm&auth='.$session->sid_super);
       
   497     }
       
   498     break;
       
   499   case "confirm":
       
   500     $enano_version = ( isset($_POST['version']) ) ? $_POST['version'] : enano_version();
       
   501     
       
   502     $template->header();
       
   503     if(!$session->sid_super) { echo '<p>No admin session found! Please <a href="upgrade.php">restart the upgrade</a>.</p>'; $template->footer(); exit; }
       
   504     ?>
       
   505       <form action="upgrade.php?mode=upgrade&amp;auth=<?php echo $session->sid_super; ?>" method="post">
       
   506         <table border="0" style="margin-left: auto; margin-right: auto; margin-top: 5px;" cellspacing="1" cellpadding="4">
       
   507           <tr>
       
   508             <td colspan="2"><p><b>Are you sure you want to perform this upgrade?</b></p><p>You can still cancel the upgrade process now. If<br />the upgrade fails, you will need to roll back<br />any actions made using manual SQL queries.</p><p><b>Please clear your browser cache or<br />shift-reload after the upgrade.</b><br />If you fail to do so, some page elements may<br />be broken.</td>
       
   509           </tr>
       
   510           <tr>
       
   511             <td colspan="2" style="text-align: center;">
       
   512               <input type="hidden" name="enano_version" value="<?php echo $enano_version; ?>" />
       
   513               <input type="submit" name="doit" value="Upgrade Enano!" />
       
   514             </td>
       
   515           </tr>
       
   516         </table>
       
   517       </form>
       
   518     <?php
       
   519     break;
       
   520   case "upgrade":
       
   521     $template->header();
       
   522     if(!$session->sid_super) { echo '<p>No admin session found! Please <a href="upgrade.php">restart the upgrade</a>.</p>'; $template->footer(); exit; }
       
   523     if(!isset($_POST['enano_version'])) { echo '<p>Can\'t find the version information on the POST query, are you trying to do this upgrade directly? Please <a href="upgrade.php">restart the upgrade</a>.</p>'; break; }
       
   524     $enano_version = $_POST['enano_version'];
       
   525     echo '<p>Preparing for schema execution...';
       
   526       // Build an array of queries
       
   527       $schema = file_get_contents('upgrade.sql');
       
   528       
       
   529       // Strip out and process version blocks
       
   530       preg_match_all('#---BEGIN ([0-9A-z\.\-]*?)---'."\n".'(.*?)'."\n".'---END \\1---#is', $schema, $matches);
       
   531       
       
   532       $from_list  =& $matches[1];
       
   533       $query_list =& $matches[2];
       
   534       
       
   535       foreach($matches[0] as $m)
       
   536       {
       
   537         $schema = str_replace($m, '', $schema);
       
   538       }
       
   539       $schema = explode("\n", $schema);
       
   540       foreach($schema as $k => $q)
       
   541       {
       
   542         if(substr($q, 0, 2) == '--' || $q == '')
       
   543         {
       
   544           unset($schema[$k]);
       
   545           //die('<pre>'.htmlspecialchars(print_r($schema, true)).'</pre>');
       
   546         }
       
   547         else
       
   548         {
       
   549           $schema[$k] = upg_assign_vars($schema[$k]);
       
   550         }
       
   551       }
       
   552       
       
   553       foreach($query_list as $k => $q)
       
   554       {
       
   555         $query_list[$k] = explode("\n", $query_list[$k]);
       
   556         foreach($query_list[$k] as $i => $s)
       
   557         {
       
   558           $tq =& $query_list[$k][$i];
       
   559           if(substr($s, 0, 2) == '--' || $s == '')
       
   560           {
       
   561             unset($query_list[$k][$i]);
       
   562             //die('<pre>'.htmlspecialchars(print_r($schema, true)).'</pre>');
       
   563           }
       
   564           else
       
   565           {
       
   566             $query_list[$k][$i] = upg_assign_vars($query_list[$k][$i]);
       
   567           }
       
   568         }
       
   569         $query_list[$k] = array_values($query_list[$k]);
       
   570       }
       
   571       
       
   572       $assoc_list = Array();
       
   573       
       
   574       foreach($from_list as $i => $v)
       
   575       {
       
   576         $assoc_list[$v] = $query_list[$i];
       
   577       }
       
   578       
       
   579       $schema = array_values($schema);
       
   580       
       
   581       $deps_resolved = false;
       
   582       $installing_versions = Array($enano_version);
       
   583       
       
   584       while(true)
       
   585       {
       
   586         $v = array_keys($deps_list);
       
   587         foreach($v as $i => $ver)
       
   588         {
       
   589           if(in_array($ver, $installing_versions))
       
   590           {
       
   591             // $ver is on the list of versions to be installed. Add its dependencies to the list of versions to install.
       
   592             foreach($deps_list[$ver] as $dep)
       
   593             {
       
   594               if(!in_array($dep, $installing_versions))
       
   595               {
       
   596                 $installing_versions[] = $dep;
       
   597               }
       
   598             }
       
   599           }
       
   600           if($i == count($deps_list) - 1)
       
   601           {
       
   602             break 2;
       
   603           }
       
   604         }
       
   605       }
       
   606       
       
   607       foreach($installing_versions as $this_ver)
       
   608       {
       
   609         $schema = array_merge($schema, $assoc_list[$this_ver]);
       
   610       }
       
   611       
       
   612       // Time for some proper SQL syntax!
       
   613       // Also check queries for so-called injection attempts to make
       
   614       // sure that it doesn't fail during the upgrade process and
       
   615       // leave the user with a half-upgraded database
       
   616       foreach($schema as $s => $q)
       
   617       {
       
   618         if(substr($q, strlen($q)-1, 1) != ';') 
       
   619         {
       
   620           $schema[$s] .= ';';
       
   621         }
       
   622         if ( !$db->check_query($schema[$s]) )
       
   623         {
       
   624           // Uh-oh, the check failed, bail out
       
   625           // The DBAL runs sanity checks on all queries for safety,
       
   626           // so if the check fails in mid-upgrade we are in deep
       
   627           // dodo doo-doo.
       
   628           echo 'Query failed sanity check, this should never happen and is a bug.</p><p>Query was:</p><pre>'.$schema[$s].'</pre>';
       
   629           break 2;
       
   630         }
       
   631       }
       
   632       
       
   633       $schema = array_values($schema);
       
   634       
       
   635       // Used extensively for debugging
       
   636       // echo '<pre>'.htmlspecialchars(print_r($schema, true)).'</pre>';
       
   637       // break;
       
   638       
       
   639       echo 'done!<br />Executing upgrade schema...';
       
   640       
       
   641       // OK, do the loop, baby!!!
       
   642       foreach($schema as $q)
       
   643       {
       
   644         $r = $db->sql_query($q);
       
   645         if(!$r)
       
   646         {
       
   647           echo $db->get_error();
       
   648           break 2;
       
   649         }
       
   650       }
       
   651       
       
   652       // Call any custom functions
       
   653       foreach ( $installing_versions as $ver )
       
   654       {
       
   655         if ( isset($func_list[$ver]) )
       
   656         {
       
   657           foreach($func_list[$ver] as $function)
       
   658           {
       
   659             @call_user_func($function);
       
   660           }
       
   661         }
       
   662       }
       
   663       
       
   664       echo 'done!</p>';
       
   665       echo '<p>You will be redirected shortly. If you aren\'t redirected, <a href="index.php">click here</a>.</p>
       
   666             <script type="text/javascript">setTimeout("window.location=\'index.php\'", 2000)</script>';
       
   667     break;
       
   668 }
       
   669 $template->footer();
       
   670 
       
   671 ?>