install/install.php

00001 <?php
00002 # streber - a php5 based project management system  (c) 2005-2007  / www.streber-pm.org
00003 # Distributed under the terms and conditions of the GPL as stated in lang/license.html
00004 
00005 
00011 error_reporting (E_ERROR | E_WARNING | E_PARSE | E_NOTICE | E_STRICT
00012                 |E_PARSE|E_CORE_ERROR|E_CORE_WARNING|E_COMPILE_ERROR
00013 );
00014 
00018 function startedIndexPhp() {return true; }                     # define function
00019 
00020 ### bypassing date & timezone-related warnings with php 5.1
00021 if (function_exists('date_default_timezone_set')) {
00022     $tz= @date_default_timezone_get();
00023     date_default_timezone_set($tz);
00024     #echo $tz;
00025 }
00026 
00032 $g_supported_db_types= array();
00033 
00034 if(function_exists('mysql_connect')){
00035     $g_supported_db_types[]='mysql';
00036 }
00037 
00038 if(function_exists('mysqli_connect')){
00039     $g_supported_db_types[]='mysqli';
00040 }
00041 
00042 
00043 require_once(dirname(__FILE__)."/../std/common.inc.php");
00044 #require_once(dirname(__FILE__)."/../std/errorhandler.inc.php");
00045 require_once(dirname(__FILE__)."/../conf/defines.inc.php");
00046 require_once(dirname(__FILE__)."/../conf/conf.inc.php");
00047 
00048 require_once(dirname(__FILE__)."/install_forms.inc.php");
00049 
00050 clearRequestVars();
00051 addRequestVars($_GET);
00052 addRequestVars($_POST);
00053 addRequestVars($_COOKIE);
00054 
00055 
00056 
00057 print_InstallationHTMLOpen();
00058 
00059 if(!get('install_step')) {
00060     step_01_checkEvironment();
00061 }
00062 else {
00063     step_02_form_submit();
00064 }
00065 
00066 print_InstallationHTMLClose();
00067 
00068 
00069 exit();
00070 
00071 
00075 function step_01_checkEvironment() {
00076     global $g_supported_db_types;
00077 
00078     $flag_errors=false;
00079     echo "<h1>Welcome to installing streber ".confGet('STREBER_VERSION')."</h1>";
00080     echo "<h2>Checking environment...</h2>";
00081 
00082     ### check php version ###
00083     {
00084         print_testStart("PHP-Version...");
00085         $php_version=phpversion();
00086         if($php_version > confGet('PHP_VERSION_REQUIRED')) {
00087             print_testResult(RESULT_GOOD,"is $php_version");
00088         }
00089         else {
00090             print_testResult(RESULT_FAILED,"Insufficient php version $php_version. Streber requires php v".confGet('PHP_VERSION_REQUIRED').".
00091              You find additional information on how to get the latest php-version or a service provider
00092             with php5 at the ".getStreberWikiLink('installation','installation guide'));
00093             $flag_errors= true;
00094         }
00095     }
00096 
00097     ### check mysql-installed ###
00098     {
00099         print_testStart("Database installed?");
00100         if(count($g_supported_db_types)) {
00101             #$sql_obj = new sql_class('mysqli');                #@@@pixtur 2005-01-04: would creating the obj be better???
00102             #if($sql_obj -> error == false){
00103             print_testResult(RESULT_GOOD);
00104         }
00105         else{
00106             #print_testResult(RESULT_FAILED, $sql_obj->error);
00107             print_testResult(RESULT_FAILED, "No mysql or mysqli supported");
00108             $flag_errors= true;
00109         }
00110         #unset($sql_obj);
00111     }
00112 
00113     ### check _settings-directory writeable ###
00114     {
00115         print_testStart("check write-permissions for directory '<b>". confGet('DIR_SETTINGS') ."</b>'?");
00116         if(!is_writeable('../'. confGet('DIR_SETTINGS'))) {
00117             if(!is_dir('../'. confGet('DIR_SETTINGS'))){
00118                 @mkdir('../'. confGet('DIR_SETTINGS'));
00119             }
00120             @chmod('../'. confGet('DIR_SETTINGS'), 0777);
00121             if(!is_writeable('../'. confGet('DIR_SETTINGS'))){
00122                 print_testResult(RESULT_FAILED,"Please grand write-permissions for this directory.");
00123               $flag_errors= true;
00124             }else{
00125                 print_testResult(RESULT_GOOD, 'Folder written by Streber, please check permissions rights with your root account.');
00126             }
00127         }
00128         else {
00129             print_testResult(RESULT_GOOD);
00130         }
00131     }
00132 
00133     ### check _tmp-directory writeable ###
00134     {
00135         print_testStart("check write-permissions for directory '<b>". confGet('DIR_TEMP') ."</b>'?");
00136         if(!is_writeable('../'. confGet('DIR_TEMP'))) {
00137             if(!is_dir('../'. confGet('DIR_TEMP'))){
00138                 @mkdir('../'. confGet('DIR_TEMP'));
00139             }
00140             @chmod('../'. confGet('DIR_TEMP'), 0777);
00141             if(!is_writeable('../'. confGet('DIR_TEMP'))){
00142                 print_testResult(RESULT_PROBLEM,"Please grand write-permissions for this directory. Although you can proceed with installation, you will get warnings later.");
00143                 $flag_errors= true;
00144             }else{
00145                 print_testResult(RESULT_GOOD, 'Folder written by Streber, please check permissions rights with your root account.');
00146             }
00147         }
00148         else {
00149             print_testResult(RESULT_GOOD);
00150         }
00151     }
00152 
00153     ### check _tmp-directory writeable ###
00154     {
00155         print_testStart("check write-permissions for directory '<b>". confGet('DIR_FILES') ."</b>'?");
00156         if(!is_writeable('../'. confGet('DIR_FILES'))) {
00157             if(!is_dir('../'. confGet('DIR_FILES'))){
00158                 @mkdir('../'. confGet('DIR_FILES'));
00159             }
00160             @chmod('../'. confGet('DIR_FILES'), 0777);
00161             if(!is_writeable('../'. confGet('DIR_FILES'))){
00162                 print_testResult(RESULT_PROBLEM,"Please grand write-permissions for this directory. Although you can proceed with installation, you will get warnings later.");
00163                 $flag_errors= true;
00164             }else{
00165                 print_testResult(RESULT_GOOD, 'Folder written by Streber, please check permissions rights with your root account.');
00166             }
00167         }
00168         else {
00169             print_testResult(RESULT_GOOD);
00170         }
00171     }
00172 
00173 
00174     ### check db-setting exists ###
00175     {
00176         print_testStart("check previous db-settings in'<b>". confGet('DIR_SETTINGS') ."</b>'...");
00177         $filepath_db_settings= '../'. confGet('DIR_SETTINGS'). confGet('FILE_DB_SETTINGS');
00178 
00179         if(file_exists($filepath_db_settings)) {
00180             print_testResult(RESULT_GOOD,"use settings of previous installation.");
00181             require_once($filepath_db_settings);
00182 
00183         }
00184         ### check if old style .inc setting exists...
00185         else if(file_exists('../'. confGet('DIR_SETTINGS'). "db_settings.inc.php")) {
00186             require_once('../'. confGet('DIR_SETTINGS'). "db_settings.inc.php");
00187             print_testResult(RESULT_PROBLEM,"'db_settings.inc.php' found. This extension has been depreciated. Trying to rename to ".confGet('DIR_SETTINGS'));
00188             if(!rename('../'. confGet('DIR_SETTINGS'). "db_settings.inc.php",
00189                    '../'. confGet('DIR_SETTINGS'). confGet('FILE_DB_SETTINGS'))
00190             ) {
00191                 print_testResult(RESULT_PROBLEM,"Renaming failed. Please remove manually.");
00192             }
00193         }
00194         else {
00195             print_testResult(RESULT_GOOD,"does not exists. Fresh installation");
00196             if($FO = fopen("../" . confGet('DIR_TEMP') . "/errors.log.php", "w")) {
00197                 fputs($FO,'<? header("Location: ../index.php");exit(); ?>');
00198             }
00199             
00200         }
00201         global $g_form_fields;
00202         $g_form_fields['db_username']['value']=     confGet('DB_USERNAME') ? confGet('DB_USERNAME') : NULL;
00203         $g_form_fields['db_password']['value']=     confGet('DB_PASSWORD') ? confGet('DB_PASSWORD') : NULL;
00204         $g_form_fields['db_name']['value']=         confGet('DB_NAME')     ? confGet('DB_NAME') : NULL;
00205         $g_form_fields['db_table_prefix']['value']= confGet('DB_TABLE_PREFIX') ? confGet('DB_TABLE_PREFIX') : NULL;
00206     }
00207 
00208     ### abort on errors... ##
00209     if($flag_errors) {
00210         echo "<h2>Installation failed</h2>";
00211         echo "You may find help at ".getStreberWikiLink('installation','the wiki-installation guide');
00212 
00213         return false;
00214     }
00215     ### ...or render the configuration-form ###
00216     else {
00217         print_setup_form();
00218         return true;
00219     }
00220 }
00221 
00222 
00226 function step_02_form_submit()
00227 {
00228 
00229     ### check params passed ###
00230     global $g_form_fields;
00231 
00232     $errors=false;
00233 
00234     foreach($g_form_fields as $key=>$value) {
00235         $f= &$g_form_fields[$key];
00236         $value=get($f['id']);
00237 
00238         if(isset($value)) {
00239             $f['value']= $value;
00240         }
00241         else if($f['type'] == 'checkbox') {
00242             $f['value']= false;
00243         }
00244         if(isset($f['required']) && $f['required'] && !$value ) {
00245             $errors=true;
00246             $f['error']= true;
00247         }
00248     }
00249 
00250     ### reshow form if errors ###
00251     if($errors) {
00252          echo "<h2>Note: some fields are required</h2>";
00253          print_setup_form();
00254          return;
00255     }
00256 
00257     ### if no error continue ###
00258     if(step_02_proceed()) {
00259         echo "<h2>Installation finished successfully</h2>";
00260         echo "NOTE: If you don't remove the install-directory now, other people can spy out your database-settings!<br>";
00261 
00262         echo "Please proceed by either...";
00263         echo "<ul>";
00264         echo "<li><a href='remove_install_dir.php'>try to delete installation-directory</a> now";
00265         echo "<li>".getStreberWikiLink('first steps','read a fast tutorial about first steps');
00266         echo "<li><a href='../index.php'>login</a>";
00267         echo "</ul>";
00268     }
00269     else {
00270         echo "<h2>Installation failed</h2>";
00271         echo "You may find help at ".getStreberWikiLink('installation','the wiki-installation guide');
00272     }
00273 }
00274 
00278 function step_02_proceed()
00279 {
00280     global $g_form_fields, $sql_obj;
00281 
00282 
00283 
00284     echo "<h2>Proceeding...</h2>";
00285 
00286     $f_db_type =                $g_form_fields['db_type']['value'];
00287     $f_hostname =               $g_form_fields['hostname']['value'];
00288     $f_db_name =                $g_form_fields['db_name']['value'];
00289     $f_db_username =            $g_form_fields['db_username']['value'];
00290     $f_db_password =            $g_form_fields['db_password']['value'];
00291     $f_db_table_prefix =        $g_form_fields['db_table_prefix']['value'];
00292     $f_user_admin_name =        $g_form_fields['user_admin_name']['value'];
00293     $f_user_admin_password =    $g_form_fields['user_admin_password']['value'];
00294     $f_continue_on_sql_errors = $g_form_fields['continue_on_sql_errors']['value'];
00295 
00296 
00297     require_once(dirname(__FILE__)."/../db/db_".$f_db_type."_class.php");
00298 
00299     ### check mysql-connection ###
00300     {
00301 
00302         print_testStart("checking mysql connecting to '$f_hostname'...");
00303 
00304         $sql_obj = new sql_class($f_hostname, $f_db_username, $f_db_password, $f_db_name);
00305         if($sql_obj->error == false){
00306 
00307             # Connection DB
00308             if(!$sql_obj->connect()) {
00309                 $hint= 'This could be a problem with incorrect setup of your sql-server. <a href="http://www.streber-pm.org/1176">Read more...</a>';
00310                 print_testResult(RESULT_FAILED,"mysql-error:<pre>".$sql_obj->error."</pre><br>$hint");
00311                 return false;
00312             }else{
00313                 print_testResult(RESULT_GOOD, $sql_obj->error);
00314             }
00315         }else{
00316             print_testResult(RESULT_FAILED, $sql_obj->error);
00317             return false;
00318         }
00319     }
00320 
00321     ### does database already exists? ###
00322     {
00323         print_testStart("Make sure to not overwrite existing streber-db called '$f_db_name'");
00324 
00325         ### db does NOT exists ###
00326         if(!$sql_obj->selectdb()) {
00327             print_testResult(RESULT_GOOD, $sql_obj->error);
00328 
00329             ### create new database ###
00330             print_testStart("create database");
00331             if(!$sql_obj->execute("CREATE DATABASE $f_db_name")) {
00332                 print_testResult(RESULT_FAILED,"<pre>".$sql_obj->error."</pre>");
00333                 return false;
00334             }
00335             else {
00336                 if(!$sql_obj->selectdb()) {
00337                     print_testResult(RESULT_FAILED, $sql_obj->error);
00338                     return false;
00339                 }
00340                 else {
00341                     print_testResult(RESULT_GOOD, 'Database '.$f_db_name.' created.');
00342                 }
00343             }
00344         }
00345 
00346         ### db exists / upgrade ###
00347         else {
00348             print_testResult(RESULT_PROBLEM,"DB '$f_db_name' already exists");
00349 
00350             ### check version of existing database ###
00351             print_testStart("checking version of existing database");
00352             if($sql_obj->execute("SELECT
00353             *
00354 
00355             FROM
00356             {$f_db_table_prefix}db
00357 
00358             WHERE
00359             updated is NULL")
00360             ) {
00361                 $count=0;
00362                 $db_version=NULL;
00363                 $streber_version_required=NULL;
00364                 while ($row = $sql_obj->fetchArray()) {
00365                     $db_version= $row['version'];
00366                     $streber_version_required= $row['version_streber_required'];
00367                     $count++;
00368                 }
00372                 if( $count!=1 ) {
00373                     print_testResult(RESULT_FAILED,"could not get propper db-version table entry. Please view ".getStreberWikiLink('installation','Installation Guide')." on hints how to proceed.");
00374                     return false;
00375                 }
00376                 else if($db_version < confGet('DB_VERSION_REQUIRED')) {
00377 
00378                     ### update ###
00379                     print_testResult(RESULT_PROBLEM,"version is $db_version. Upgrading...");
00380 
00381                     $result= upgrade(array(
00382                         'db_type'       => $f_db_type,
00383                         'hostname'      => $f_hostname,
00384                         'db_username'   => $f_db_username,
00385                         'db_password'   => $f_db_password,
00386                         'db_table_prefix'=> $f_db_table_prefix,
00387                         'db_name'       => $f_db_name,
00388                         'db_version'    => $db_version,                    # autodetect
00389                         'continue_on_sql_errors'=>$f_continue_on_sql_errors,
00390                     ));
00391                     return $result;
00392 
00393                 }
00394                 else if($streber_version_required > confGet('STREBER_VERSION')) {
00395                     print_testResult(RESULT_PROBLEM,"version is $db_version. It's requires Version " .confGet('DB_VERSION_REQUIRED'). " of Streber. Current Version is ".confGet('STREBER_VERSION').". Please download and install the latest version.");
00396                     return false;
00397                 }
00398                 else {
00399                     print_testResult(RESULT_GOOD, "Current database (version $db_version) looks fine. Installation finished with nothing changed. Please view ".getStreberWikiLink('installation','Installation Guide')." on how to fix unsolved problems.");
00400                     return true;
00401                 }
00402                 print_testResult(RESULT_PROBLEM,"Installation aborted due to unknown reason.");
00403                 return false;
00404             }
00405 
00406             ### no version / fresh installation ###
00407             else {
00408                 print_testResult(RESULT_GOOD,
00409                                 "Could not query streber-db version. Assuming fresh installation");
00410             }
00411         }
00412 
00413 
00414 
00415         ### creating database-structure ###
00416         print_testStart("creating tables...");
00417 
00418         $filename= "./create_structure_v".confGet('DB_CREATE_VERSION').".sql";
00419 
00420         if(!file_exists($filename)) {
00421             $filenames = glob("./create_structure_v*.sql");
00422             if($filenames) {
00423                 rsort($filenames);
00424                 print_testResult(RESULT_PROBLEM,"Required file $filename is missing, trying to use $filenames[0] instead and then upgrade.");
00425                 $filename = $filenames[0];
00426                 ereg("create_structure_v(.*)\.sql", $filename, $matches);
00427                 $upgradeFromVersion = $matches[1];
00428             }
00429             else {
00430                 print_testResult(RESULT_FAILED,"Getting sql-code failed. This is an internal error. Look at ". getStreberWikiLink('installation','Installation Guide') ." for clues. ");
00431                 return false;
00432             }
00433         }
00434         if(!parse_mysql_dump($filename, $f_db_table_prefix)) {
00435             print_testResult(RESULT_FAILED,"SQL-Error:<br><pre>".$sql_obj->error."</pre>");
00436             return false;
00437         }
00438         
00439         if($upgradeFromVersion) {
00440             $result= upgrade(array(
00441                 'db_type'       => $f_db_type,
00442                 'hostname'      => $f_hostname,
00443                 'db_username'   => $f_db_username,
00444                 'db_password'   => $f_db_password,
00445                 'db_table_prefix'=> $f_db_table_prefix,
00446                 'db_name'       => $f_db_name,
00447                 'continue_on_sql_errors'=>$f_continue_on_sql_errors,
00448                 'db_version'    => $upgradeFromVersion,
00449             ));
00450             if(!$result) {
00451                 print_testResult(RESULT_FAILED,"Upgrading failed. This is an internal error. Look at ". getStreberWikiLink('installation','Installation Guide') ." for clues. ");
00452                 return false;
00453             }
00454         }
00455         
00456         print_testResult(RESULT_GOOD);
00457 
00458         ### create db-version entry ###
00459         print_testStart("add db-version entry");
00460         $db_version= confGet('DB_CREATE_VERSION');
00461         $streber_version_required= confGet('DB_CREATE_STREBER_VERSION_REQUIRED');
00462         $str_query= "INSERT into {$f_db_table_prefix}db (id,version,version_streber_required,created) VALUES(1,'$db_version','$streber_version_required',NOW() )";
00463         if(!$sql_obj->execute($str_query)) {
00464             print_testResult(RESULT_FAILED,"SQL-Error:<pre>".$sql_obj->error. "</pre>");
00465             return false;
00466         }
00467         else {
00468             print_testResult(RESULT_GOOD);
00469         }
00470 
00471         ### create admin entry entry ###
00472         print_testStart("add admin-user entry 1/2");
00473         $password_md5=md5($f_user_admin_password);
00474         $str_query= "INSERT into {$f_db_table_prefix}person
00475                           (id,
00476                           name,
00477                           nickname,
00478                           password,
00479                           user_rights,
00480                           can_login,
00481                           profile,
00482                           language,
00483                           ip_address
00484                           )
00485                           VALUES(
00486                           1,
00487                           '$f_user_admin_name',
00488                           '$f_user_admin_name',
00489                           '$password_md5',
00490                           268435455, /* all rights */
00491                           1,
00492                           1,
00493                           '".confGet('DEFAULT_LANGUAGE')."',
00494                           ''
00495                           )";
00496         if(!$sql_obj->execute($str_query)) {
00497             print_testResult(RESULT_FAILED,"SQL-Error:<br><pre>".$sql_obj->error."</pre>");
00498             return false;
00499         }
00500         else {
00501             print_testResult(RESULT_GOOD);
00502         }
00503 
00504         ### create admin entry entry ###
00505         print_testStart("add admin-user entry 2/2");
00506         $str_query= "INSERT into {$f_db_table_prefix}item
00507                           (id,
00508                           type,
00509                           state,
00510                           created_by,
00511                           modified_by
00512                           )
00513                           VALUES(
00514                           1,
00515                           ".ITEM_PERSON.",
00516                           ".ITEM_STATE_OK.",
00517                           1,
00518                           1 )";
00519         if(!$sql_obj->execute($str_query)) {
00520             print_testResult(RESULT_FAILED,"SQL-Error:<br><pre>".$sql_obj->error."</pre>");
00521             return false;
00522         }
00523         else {
00524             print_testResult(RESULT_GOOD);
00525         }
00526 
00527 
00528         ### settings-directory already exists? ###
00529         if(!file_exists('../'. confGet('DIR_SETTINGS'))) {
00530             print_testStart("try to create ".confGet('DIR_SETTINGS')."...");
00531             if(!mkdir('../'. confGet('DIR_SETTINGS'))) {
00532                 print_testResult(RESULT_FAILED,"could not create directory. This could be a file permission problem...");
00533             }
00534             else {
00535                 print_testResult(RESULT_GOOD);
00536             }
00537         }
00538 
00539         ### writing setting-file ###
00540         {
00541             $filename='../'. confGet('DIR_SETTINGS').  confGet('FILE_DB_SETTINGS');
00542             print_testStart("writing configuration file '$filename'...");
00543             $settings= array(
00544                 'DB_TYPE'       => $f_db_type,
00545                 'HOSTNAME'      => $f_hostname,
00546                 'DB_USERNAME'   => $f_db_username,
00547                 'DB_PASSWORD'   => $f_db_password,
00548                 'DB_TABLE_PREFIX'=> $f_db_table_prefix,
00549                 'DB_NAME'       => $f_db_name,
00550                 'DB_VERSION'    => confGet('DB_CREATE_VERSION'),
00551             );
00552 
00553             $write_ok= writeSettingsFile($filename, $settings);
00554 
00555             if(!$write_ok) {
00556                 print_testResult(RESULT_FAILED,"can not write '$filename'. Please create it with this content:<br><pre>&lt;?php".buildSettingsFile($settings)."?&gt;</pre>");
00557                 return false;
00558             }
00559             else {
00560                 print_testResult(RESULT_GOOD);
00561             }
00562         }
00563 
00564         ### tmp-directory already exists? ###
00565         if(!file_exists('../'. confGet('DIR_TEMP'))) {
00566             print_testStart("try to create directory of tempory files ".confGet('DIR_TEMP')."...");
00567             if(!mkdir('../'. confGet('DIR_TEMP'))) {
00568                 print_testResult(RESULT_FAILED,"could not create directory. This could be a file permission problem...");
00569             }
00570             else {
00571                 print_testResult(RESULT_GOOD);
00572             }
00573         }
00574         return true;
00575     }
00576 }
00577 
00578 
00582 function upgrade($args=NULL) {
00583 
00584 
00585     $db_type=          $args['db_type'];
00586     $hostname=          $args['hostname'];
00587     $db_username=       $args['db_username'];
00588     $db_password=       $args['db_password'];
00589     $db_table_prefix=   $args['db_table_prefix'];
00590     $db_name=           $args['db_name'];
00591     $flag_continue_on_sql_errors= $args['continue_on_sql_errors'];
00592     $db_version=        $args['db_version'];            # set to NULL if autodetect
00593 
00594     require_once(dirname(__FILE__)."/../db/db_".$db_type."_class.php");
00595 
00596     echo "<h2>Upgrading...</h2>";
00597 
00598     ### connect db ###
00599     $sql_obj = new sql_class($hostname, $db_username, $db_password, $db_name);
00600 
00601     if($sql_obj -> error != false) {
00602         print_testResult(RESULT_FAILED,"mysql-error:<pre>".$sql_obj -> error."</pre>");
00603         return false;
00604     }
00605 
00606     ### select db? ###
00607     if(!$sql_obj->selectdb()) {
00608         print_testResult(RESULT_FAILED,"Database does not exists mysql-error:<pre>".$sql_obj -> error."</pre>");
00609         return false;
00610     }
00611 
00612     ### get version ###
00613     if(!$db_version)
00614     {
00615         print_testStart("getting original version for upgrading database '$db_name' at '$hostname'...");
00616 
00617         if(!$result=$sql_obj->execute("SELECT *
00618                                    FROM {$db_table_prefix}db
00619                                   WHERE updated is NULL")
00620         ) {
00621             print_testResult(RESULT_FAILED,"Count not get version:<pre>".$sql_obj -> error."</pre>");
00622             return false;
00623         }
00624 
00625         $count= 0;
00626         while ($row = $sql_obj->fetchArray()) {
00627             $db_version= $row['version'];
00628             $streber_version_required= $row['version_streber_required'];
00629             $count++;
00630         }
00631         if( $count!=1 ) {
00632             print_testResult(RESULT_FAILED,"could not get propper db-version table entry. Please view ".getStreberWikiLink('installation','Installation Guide')." on hints how to proceed.");
00633             return false;
00634         }
00635         if($db_version < 0.044) {
00636             print_testResult(RESULT_FAILED,"Sorry upgrading is supoorted since v0.044");
00637             return false;
00638         }
00639         print_testResult(RESULT_GOOD,"v $db_version");
00640 
00641     }
00642     else {
00643         print_testResult(RESULT_GOOD,"assuming v$db_version");
00644     }
00645 
00646 
00647     $update_queries=array();
00648     require(dirname(__FILE__)."/db_updates.inc.php");
00649 
00650 
00651 
00652 
00653 
00654 
00655 
00656 
00657     print_testStart("doing " .count($update_queries). " changes to database...");
00658     foreach($update_queries as $q) {
00659 
00660         ### strict mode for development ###
00661         #if($result= $sql_obj->execute('SET session sql_mode = "STRICT_ALL_TABLES,STRICT_TRANS_TABLES,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION"')){
00662         #  }
00663         #}
00664 
00665         if(!$result=$sql_obj->execute($q)){
00666             if(function_exists('mysql_error') && mysql_error()) {
00667                 $mysql_error= mysql_error();
00668             }
00669             else if(function_exists('mysqli_error') && mysqli_error()) {
00670                 $mysql_error= mysql_error();
00671             }
00672             else {
00673                 $mysql_error = $sql_obj->error;
00674             }
00675             print_testResult(RESULT_FAILED,"Failed:<pre>".$sql_obj -> error."</pre><br>Error:<pre>". $mysql_error . "</pre>");
00676 
00677             if(isset($flag_continue_on_sql_errors) && $flag_continue_on_sql_errors) {
00678                 print_testStart("proceeding upgrade...");
00679             }
00680             else {
00681                 return false;
00682             }
00683         }
00684     }
00685 
00686     ### update the db-version ###
00687     print_testStart("update db-version information");
00688     $str_query= "UPDATE {$db_table_prefix}db
00689                 SET  updated = now();
00690                 ";
00691     if(!$sql_obj->execute($str_query)) {
00692         print_testResult(RESULT_FAILED,"SQL-Error:<br><pre>".$sql_obj -> error."</pre><br><br>Querry was:<br>$str_query");
00693         return false;
00694     }
00695 
00696 
00697     ### create new db-version ###
00698     $db_version_new= confGet('DB_CREATE_VERSION');
00699     $streber_version_required= confGet('DB_CREATE_STREBER_VERSION_REQUIRED');
00700     $str_query= "INSERT into {$db_table_prefix}db (id,version,version_streber_required,created) VALUES(1,'$db_version_new','$streber_version_required',NOW() )";
00701     if(!$sql_obj->execute($str_query)) {
00702         print_testResult(RESULT_FAILED,"SQL-Error:<pre>".$sql_obj -> error."</pre>Query was:<pre>$str_query</pre>");
00703         return false;
00704     }
00705     else {
00706         print_testResult(RESULT_GOOD);
00707     }
00708 
00709     ### rewrite setting-file ###
00710     {
00711         $filename='../'. confGet('DIR_SETTINGS').  confGet('FILE_DB_SETTINGS');
00712         print_testStart("writing configuration file '$filename'...");
00713         $write_ok= writeSettingsFile($filename, array(
00714             'DB_TYPE'       => $db_type,
00715             'HOSTNAME'      => $hostname,
00716             'DB_USERNAME'   => $db_username,
00717             'DB_PASSWORD'   => $db_password,
00718             'DB_TABLE_PREFIX'=> $db_table_prefix,
00719             'DB_NAME'       => $db_name,
00720             'DB_VERSION'    => confGet('DB_CREATE_VERSION'),
00721         ));
00722 
00723         if(!$write_ok) {
00724             print_testResult(RESULT_FAILED,"can not write '$filename'.");
00729             # Please create it with this content:<br><pre>&lt;?php".$buffer."?&gt;</pre>");
00730             return false;
00731         }
00732         else {
00733             print_testResult(RESULT_GOOD);
00734         }
00735     }
00736     return true;
00737 }
00738 
00739 
00745 function buildSettingsFile($args) {
00746 
00747     $buffer='
00748 #--- streber db-configuration file ---
00749 # this file has automatically been created and might be
00750 # overwritten be installation procedures. If you want
00751 # to overwrite any of these settings add lines to
00752 # "customize.inc.php" in streber-root directory
00753 ';
00754     foreach($args as $key=>$value) {
00755         $buffer.='confChange("' . $key . '","' . $value .'");
00756 ';
00757     }
00758     return $buffer;
00759 }
00760 
00761 
00762 
00763 
00769 function writeSettingsFile($filename, $args) {
00770 
00771     $buffer= buildSettingsFile($args);
00772 
00773     $FH= @fopen ($filename,"w");
00774     if(!$FH) {
00775         return false;
00776     }
00777     else if(!fputs ($FH, "<"."?php".$buffer."?".">")) {
00778         return false;
00779     }
00780     fclose ($FH);
00781 
00782     return true;
00783 
00784 }
00785 
00786 
00796 function parse_mysql_dump($url,$table_prefix="")
00797 {
00798     global $sql_obj;
00799     $file_content = file($url);
00800     $query = "";
00801 
00802     foreach($file_content as $sql_line){
00803         if(trim($sql_line) != "" && strpos($sql_line, "--") === false){
00804             $query .= $sql_line;
00805             ### query complete ###
00806             if(preg_match("/;\s*$/", $sql_line)){
00807 
00808                 ### add table-prefixes ###
00809                 $matches= array();
00810                 if(preg_match("/(CREATE\s*TABLE\s[`'](.*)[`'])\s*\(/", $query, $matches)) {
00811                     $create_string_old= $matches[1];
00812                     $table_name_old= $matches[2];
00813                     $create_string_new= str_replace($table_name_old, $table_prefix.$table_name_old, $create_string_old);
00814                     $query= str_replace($create_string_old, $create_string_new, $query);
00815                 }
00816 
00817                 ### send query ###
00818                 if(!$result = $sql_obj->execute($query)) {
00819                     return false;
00820                 }
00821                 $query = "";
00822             }
00823          }
00824     }
00825     return true;
00826 }
00827 
00828 
00829 
00830 
00831 
00832 
00833 

Generated on Sun Mar 4 17:19:28 2007 for streber by  doxygen 1.5.1-p1