Main Page | Class Hierarchy | Alphabetical List | Class List | Directories | File List | Class Members | File Members | Related Pages

update.php

Go to the documentation of this file.
00001 <?php
00009 /***************************************************************************
00010  This program is free software; you can redistribute it and/or
00011  modify it under the terms of the GNU General Public License
00012  as published by the Free Software Foundation; either version 2
00013  of the License, or (at your option) any later version.
00014 
00015  This program is distributed in the hope that it will be useful,
00016  but WITHOUT ANY WARRANTY; without even the implied warranty of
00017  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00018  GNU General Public License for more details.
00019 
00020  You should have received a copy of the GNU General Public License
00021  along with this program; if not, write to the Free Software
00022  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
00023  ***************************************************************************/
00024 
00025 define( 'PHPMAP_FROM_VERSION', '2.0' );
00026 define( 'PHPMAP_TO_VERSION', '2.0.1' );
00027 
00028 if(isset($_POST['step']))     $intStep = $_POST['step'];
00029 elseif(isset($_GET['step']))  $intStep = $_GET['step'];
00030 else $intStep = 0;
00031 
00032 /* Steps
00033  * 0 - check if template system, config and mysql are available and correct version is used
00034  * 1 - perform the update
00035  * 2 - (for security reasons) try delete update.html and update.php
00036  */
00037 if( $intStep == 0 )
00038 {
00039     // try to create a Template object
00040     if( !file_exists('./lib/template.php') || !file_exists('./templates/default/update.html') )
00041     {
00042         echo 'Template Klasse wurde nicht gefunden<br />'.
00043             'Vergewissere dich, dass alle Dateien korrekt hochgeladen wurden und alle Ordner richtig erstellt wurden.<br />'.
00044             'Falls du Hilfe ben&ouml;tigst, komm in #wdstools im Andaloria Irc :)';
00045         die();
00046     }
00047     require_once( './lib/template.php' );
00048     $oStyle = new Template( './templates/default/' );
00049     $oStyle->addReplacements( array( 'TEMPLATES' => $oStyle->getPath() ) );
00050     $oStyle->setTemplate ( 'update.html' );
00051 
00052     // look for the config.php
00053     if( !file_exists( './conf/config.php' )  )
00054     {
00055         $strMessage = $oStyle->get_area( 'ErrorConfig' );
00056         $oStyle->addReplacements( array( 'ERROR' => $strMessage ) );
00057         echo $oStyle->get_area( 'Error' );
00058         die();
00059     }
00060 
00061     // get the config settings
00062     error_reporting(0);
00063     include( './conf/config.php' );
00064     error_reporting(E_ALL);
00065 
00066     if( $strPhpMapVersion != PHPMAP_FROM_VERSION )
00067     {
00068         $oStyle->addReplacements( array( 'FOUND_VERSION' => $strPhpMapVersion, 'NEW_VERSION' => PHPMAP_FROM_VERSION ) );
00069         $strMessage = $oStyle->get_area( 'ErrorVersion' );
00070         $oStyle->addReplacements( array( 'ERROR' => $strMessage ) );
00071         echo $oStyle->get_area( 'Error' );
00072         die();
00073     }
00074 
00075     // try to connect to DB
00076     $sqlLink = @mysql_connect( $strMysqlHost, $strMysqlUser, $strMysqlPwd );
00077 
00078     if( $sqlLink === false || !@mysql_select_db( $strMysqlDb, $sqlLink ) )
00079     {
00080         $strMessage = $oStyle->get_area( 'ErrorMysql' );
00081         $oStyle->addReplacements( array( 'ERROR' => $strMessage ) );
00082         echo $oStyle->get_area( 'Error' );
00083         die();
00084     }
00085 
00086     unset( $strMysqlHost, $strMysqlUser, $strMysqlPwd, $strMysqlDb ); // aren't used anymore
00087 
00088     // check for double entries in map_data
00089 
00090     $sqlResult = mysql_query( 'SELECT COUNT(x) AS cnt FROM `'.$strMysqlPrefix.'map_data` GROUP BY x, y HAVING cnt>1' );
00091     $intCount = intval( @mysql_result( $sqlResult , 0, 0 ) );
00092     if( $intCount > 0 )
00093     {
00094         $oStyle->addReplacements( array( 'DOUBLES' => $intCount ) );
00095         $strContinue = $oStyle->get_area( 'Step0Warning' );
00096     }
00097     else
00098     {
00099         $strContinue = $oStyle->get_area( 'Step0Continue' );
00100     }
00101     $oStyle->addReplacements( array( 'CONTINUE' => $strContinue ) );
00102 
00103     // templatesystem, config and mysql are available
00104     // show infos and start update in next step
00105     echo $oStyle->get_area( 'Step0' );
00106     die();
00107 }
00108 // 1 - perform the update
00109 elseif( $intStep == 1 )
00110 {
00111     // template class was validated in step 0, if the user tricks us in faking the _GET Parameter, it's his own fault :)
00112     require_once( './lib/template.php' );
00113     $oStyle = new Template( './templates/default/' );
00114     $oStyle->addReplacements( array( 'TEMPLATES' => $oStyle->getPath() ) );
00115     $oStyle->setTemplate ( 'update.html' );
00116 
00117     include( './conf/config.php' );
00118 
00119     $sqlLink = @mysql_connect( $strMysqlHost, $strMysqlUser, $strMysqlPwd );
00120     @mysql_select_db( $strMysqlDb, $sqlLink );
00121 
00122     //-------------------------------------------------------------------------
00123     // fix map_data table
00124     $sqlResult = mysql_query( 'SELECT COUNT(x) AS cnt FROM `'.$strMysqlPrefix.'map_data` GROUP BY x, y HAVING cnt>1' );
00125     $intCount = intval( @mysql_result( $sqlResult , 0, 0 ) );
00126 
00127     if( $intCount > 0 && !empty( $_REQUEST['clear_doubles'] ) )
00128     {
00129         $sqlResult = mysql_query( 'SELECT COUNT(x) AS cnt, m.* FROM `'.$strMysqlPrefix.'map_data` m GROUP BY x, y HAVING cnt>1' );
00130         while( $aRow = mysql_fetch_assoc( $sqlResult ) )
00131         {
00132             if( $aRow['x'] == 0 || $aRow['y'] == 0 || $aRow['last_update'] == 0 ) continue;
00133 
00134             // find most recent Record
00135             $sqlResult2 = mysql_query( "SELECT * FROM `".$strMysqlPrefix."map_data` WHERE x='".$aRow['x']."' AND y='".$aRow['y']."' ORDER BY last_update DESC LIMIT 1" );
00136 
00137             $aRow = mysql_fetch_assoc( $sqlResult2 );
00138 
00139             if( $aRow['building'] == 0 ) $aRow['building'] = 'NULL';
00140             if( $aRow['border'] == 0 ) $aRow['border'] = 'NULL';
00141             if( $aRow['street'] == 0 ) $aRow['street'] = 'NULL';
00142             if( $aRow['unit'] == 0 ) $aRow['unit'] = 'NULL';
00143 
00144             mysql_query( 'DELETE FROM '.$strMysqlPrefix."map_data WHERE x='".$aRow['x']."' AND y='".$aRow['y']."'" );
00145             mysql_query( 'INSERT INTO '.$strMysqlPrefix."map_data SET x='".$aRow['x']."', y='".$aRow['y']."', fow='".$aRow['fow']."', building=".$aRow['building'].", border=".$aRow['border'].", unit=".$aRow['unit'].", street=".$aRow['street'].", last_update='".$aRow['last_update']."', last_update_by='".$aRow['last_update_by']."'" );
00146         }
00147     }
00148 
00149     // select all towns and check if a building exists in map_data
00150     $sqlResult = mysql_query( 'SELECT x, y FROM `'.$strMysqlPrefix.'town` t'  );
00151     while( $aRow = mysql_fetch_assoc( $sqlResult ) )
00152     {
00153         // get the terrain info for that field
00154         $sqlResult2 = mysql_query( 'SELECT x FROM `'.$strMysqlPrefix."map` WHERE x='".$aRow['x']."' AND y='".$aRow['y']."'" );
00155         if( mysql_num_rows( $sqlResult2 ) == 0 )
00156         {
00157             // insert terrain field
00158             mysql_query( 'INSERT INTO `'.$strMysqlPrefix."map` SET x='".$aRow['x']."', y='".$aRow['y']."', terrain='0'" );
00159         }
00160         $sqlResult2 = mysql_query( 'SELECT x, building FROM `'.$strMysqlPrefix."map_data` WHERE x='".$aRow['x']."' AND y='".$aRow['y']."'" );
00161         if( mysql_num_rows( $sqlResult2 ) == 0 )
00162         {
00163             // insert details
00164             mysql_query( 'INSERT INTO `'.$strMysqlPrefix."map_data` SET x='".$aRow['x']."', y='".$aRow['y']."', building='-', last_update='".time()."', last_update_by='1';" );
00165         }
00166         else
00167         {
00168             $aRow2 = mysql_fetch_assoc( $sqlResult2 );
00169             if( !in_array( $aRow2['building'], array( -11, -2, -1, 97, 98, 99, 100 ) ) )
00170             {
00171                 mysql_query( 'UPDATE `'.$strMysqlPrefix."map_data` SET building='-1', last_update='".time()."', last_update_by='1' WHERE x='".$aRow['x']."' AND y='".$aRow['y']."';" );
00172             }
00173         }
00174     }
00175 
00176 
00177     //-------------------------------------------------------------------------
00178     // update queries
00179     $oStyle->addReplacements( array( 'PREFIX' => $strMysqlPrefix ) );
00180     $strError = '';
00181     $aSqlQueries = explode ( ";\n", str_replace( "\r\n", "\n", $oStyle->get_area( 'Sql' ) ) );
00182     foreach( $aSqlQueries AS $strQuery )
00183     {
00184         $strQuery = trim( $strQuery );
00185         if( $strQuery == '' ) continue;
00186         @mysql_query( $strQuery ) OR $strError .= mysql_error().'<br />';
00187     }
00188 
00189     //-------------------------------------------------------------------------
00190     // update config
00191     $oStyle->addReplacements( array(
00192         'CFG_SERVER' => $intServerId,
00193         'CFG_SQL_HOST' => $strMysqlHost,
00194         'CFG_SQL_DB' => $strMysqlDb,
00195         'CFG_SQL_USER' => $strMysqlUser,
00196         'CFG_SQL_PW' => $strMysqlPwd,
00197         'CFG_SQL_PREFIX' => $strMysqlPrefix,
00198         'CFG_COOKIE_PREFIX' => $strCookiePrefix,
00199         'CFG_REFRESH' => $intTmplRefresh,
00200         'CFG_TMPL_PATH' => $strTmplPath,
00201         'CFG_SERVER_STATS' => $strServerStats,
00202         'CFG_SERVER_NAME' => $strServerName,
00203         'CFG_MAP_SIZE' => $intMapSize,
00204         'ERROR_MESSAGE' => $strError
00205     ) );
00206 
00207     for( $i=0; $i<9; $i++ )
00208     {
00209         $aReplace['DIPLO_BG'.$i] = $aDiploBgColor[$i];
00210         $aReplace['DIPLO_FG'.$i] = $aDiploFgColor[$i];
00211     }
00212     $oStyle->addReplacements( $aReplace );
00213 
00214     $strConfig = trim( $oStyle->get_area( 'Config' ) );
00215 
00216     if( ( $fp = @fopen('./conf/config.php', 'w' ) ) === false )
00217     {
00218         // should not happen, we checked if config.php is writeable in step 1
00219         die ( 'Konnte <i>./conf/config.php</i> nicht zum schreiben &ouml;ffnen.' );
00220     }
00221     if( @fwrite ( $fp, $strConfig ) === false )
00222     {
00223         // should not happen, we checked if config.php is writeable in step 1
00224         die ( 'Konnte in <i>./conf/config.php</i> nicht schreiben.' );
00225     }
00226     fclose ( $fp );
00227 
00228     echo $oStyle->get_area( 'Step1' );
00229     die();
00230 }
00231 // 2 - (for security reasons) try delete update.html and update.php
00232 elseif( $intStep == 2 )
00233 {
00234     // template class was validated in step 0, if the user tricks us in faking the _GET Parameter, it's his own fault :)
00235     require_once( './lib/template.php' );
00236     $oStyle = new Template( './templates/default/' );
00237     $oStyle->addReplacements( array( 'TEMPLATES' => $oStyle->getPath() ) );
00238     $oStyle->setTemplate ( 'update.html' );
00239 
00240     $aFiles = array( './templates/default/update.html', './update.php', './endpage.php', './templates/default/template.php' );
00241 
00242     $strMessage = '';
00243     foreach( $aFiles AS $strFile )
00244     {
00245         if( !is_writable( $strFile ) )
00246         {
00247             // try to chmod the file
00248             @chmod ( $strFile, 0666 );
00249         }
00250 
00251         if( !@unlink( $strFile ) )
00252         {
00253             $booError = true;
00254             $strMessage .= $strFile.' konnte <b>nicht</b> gelöscht werden.<br />';
00255         }
00256         else
00257         {
00258             $strMessage .= $strFile.' gelöscht.<br />';
00259         }
00260     }
00261 
00262     $oStyle->addReplacements( array( 'MESSAGE' => $strMessage ) );
00263 
00264     // show finish
00265     if( empty( $booError ) )
00266     {
00267         echo trim( $oStyle->get_area( 'Step2' ) );
00268     }
00269     else
00270     {
00271         echo trim( $oStyle->get_area( 'Step2Error' ) );
00272     }
00273     die();
00274 }

Generated on Sun May 8 19:29:45 2005 for PhpMap by  doxygen 1.4.2