Changeset 122

Show
Ignore:
Timestamp:
11/12/07 20:27:28 (1 year ago)
Author:
pdingle
Message:

enabling WP upgrade from original versions

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • branches/RB-0.9/wp-infocard/wp/admin.pw.php

    r110 r122  
    1111include_once( dirname(__FILE__) . '/../pwlib/admin.pwlib.php'); 
    1212include_once( dirname(__FILE__) . '/processing.pw.php'); 
     13include_once( dirname(__FILE__) . '/utils.pw.php'); 
    1314//require_once( ABSPATH . WPINC . '/registration-functions.php'); 
    1415/* 
     
    6263 
    6364                $myTableAuditQuery = 
    64                                 "CREATE TABLE `$this->table_audit` ( 
     65                                "CREATE TABLE IF NOT EXISTS `$this->table_audit` ( 
    6566                                                `cardhash` text default '', 
    6667                                                `accountid` bigint(20) unsigned, 
     
    168169                foreach ( $wpdb->get_col("SHOW TABLES", 0) as $table ) 
    169170                { 
    170                         if ( $table == $this->table_identity
     171                        if ( $table == $this->table_options
    171172                        { 
    172173                                return true; 
     
    184185        function setup() 
    185186        { 
     187                pw_admin::createInfocardAuditTable(); 
     188                return; 
    186189                global $wpdb; 
    187190 
     
    319322        function getOption($attr_name) 
    320323        { 
    321                 return get_settings($attr_name); 
     324                return get_option($attr_name); 
    322325        } //end function getOption 
    323326 
    324         /* 
    325                 Function: createNewAccount 
    326                 Description: creates a new db record 
    327                 Input: account details 
    328                 Output: returns the user Id on success, FALSE on failure 
    329                 Side effect: none 
    330         */ 
    331         function createNewAccount( $datamap ) 
    332         { 
    333                 require_once( ABSPATH . WPINC . '/registration-functions.php'); 
    334  
    335                 $user_login = $datamap['user_login']->new_value; 
    336                 $user_firstname = $datamap['first_name']->new_value; 
    337                 $user_lastname = $datamap['last_name']->new_value; 
    338                 $user_email = $datamap['user_email']->new_value; 
    339                 $display_name = $datamap['display_name']->new_value; 
    340                 $user_nicename = $display_name; 
    341                 $user_pass = MD5($datamap['cardhash']->new_value); 
    342                 $user_url = $datamap['user_url']->new_value; 
    343  
    344                 InfocardUtils::printDebug("<br/>Action: createNewAccount<br>"); 
    345                 InfocardUtils::printDebug("<br/>Nice Name: $user_nicename<br>"); 
    346                 $userdata = compact('user_login', 'user_pass', 'user_email', 'user_nicename', 'user_url', 'display_name', 'user_firstname', 'user_lastname'); 
    347                 $user_ID = wp_insert_user($userdata); 
    348  
    349                 if ($user_ID)  
    350                 { 
    351                         InfocardUtils::printDebug("&nbsp;Inserted record # $user_ID<br>"); 
    352                         return $user_ID; 
    353                 } 
    354                 else 
    355                 { 
    356                         InfocardUtils::printDebug("&nbsp;Error - new user not created<br/>"); 
    357                         return FALSE; 
    358                 } 
    359         } // end function createNewAccount 
    360  
    361327        function createOption( $option_name, $option_value ) 
    362         { 
    363                 // this function needs to be inside the pw_infocard class for WP to call it.  This is just a  
    364                 // placeholder 
    365         } 
     328        {        
     329                // this is duplicated in the case of the wordpress plugin. 
     330                $options = get_option($option_name); 
     331                if ( !$options ) 
     332                        $options = $option_value; 
     333 
     334                update_option($option_name, $options); 
     335        } 
     336 
     337        function removeOption( $option_name ) 
     338        { 
     339                delete_option( $option_name ); 
     340 
     341        } 
     342         
    366343        function pwAdminScreen() 
    367344        { 
     
    369346                $opts = pw_admin::updateAdminOptions(); 
    370347                // write submitted changes if this page reload shows a submit 
     348                if (!pw_utils::databaseUpToDate()) 
     349                { 
     350?> 
     351                <form  action="" method="post"> 
     352<?php 
     353                        echo "<div id='message' class='updated'>" . pw_admin::fixDatabase() . "</div>"; 
     354?> 
     355                        <input type="submit" name="infocard_admin_upgrade" id="infocard_admin_upgrade" value="Continue" /> 
     356                </form> 
     357<?php 
     358                        return; 
     359                } 
    371360 
    372361                pw_admin::printSystemCheck($opts['pw_securesiteurl']); 
     
    491480                return $option_val; 
    492481        } 
    493         function checkDatabaseStructure() 
    494         { 
    495                 // eventually this is where I will put my database upgrade code. 
     482                 
     483        function createInfocardAuditTable()  
     484        { 
     485                global $wpdb; 
     486                $audittable = PW_TABLE_PREFIX . "infocard_audit"; 
     487 
     488                $myTableAuditQuery = 
     489                        "CREATE TABLE IF NOT EXISTS `" . $audittable . "` ( 
     490                                `cardhash` text default '', 
     491                                `accountid` bigint(20) unsigned, 
     492                                `email` varchar(128) NOT NULL default '', 
     493                                `time` datetime NOT NULL default '0000-00-00 00:00:00', 
     494                                `IP` varchar(16) NOT NULL default '', 
     495                                `status` varchar(128) NOT NULL default '' 
     496                        ) ENGINE = MYISAM"; 
     497 
     498                // Check if table is already there 
     499                foreach ($wpdb->get_col("SHOW TABLES", 0) as $table ) 
     500                { 
     501                        if ($table == $audittable) 
     502                        { 
     503                                return false; 
     504                        } 
     505                } 
     506 
     507                // Ok, let's proceed 
     508                if ( $wpdb->query( $myTableAuditQuery ) === false ) 
     509                { 
     510                        return false; 
     511                } 
     512 
     513                // Just to be sure, we check that tables were actually created 
     514                foreach ( $wpdb->get_col("SHOW TABLES", 0) as $table ) 
     515                { 
     516                        if ( $table == $audittable ) 
     517                        { 
     518                                return true; 
     519                        } 
     520                } 
     521                return false; 
     522        } //end function createInfocardAuditTable 
     523 
     524        function createInfocardIdentityTable() 
     525        { 
     526                global $wpdb; 
     527                $identitytable = PW_TABLE_PREFIX . "infocard_identities"; 
     528 
     529                $myTableIdentityQuery = 
     530                        "CREATE TABLE `" . $identitytable . "` ( 
     531                                `cardhash` text default '', 
     532                                `accountid` bigint(20) unsigned, 
     533                                `cardhandle` varchar(128) NOT NULL default '', 
     534                                `startdate` varchar(128) NOT NULL default '', 
     535                                `lastused` varchar(128) NOT NULL default '' 
     536                        ) ENGINE = MYISAM"; 
     537 
     538                // Check if table is already there 
     539                foreach ($wpdb->get_col("SHOW TABLES", 0) as $table ) 
     540                { 
     541                        if ($table == $identitytable) 
     542                        { 
     543                                return false; 
     544                        } 
     545                } 
     546 
     547                // Ok, let's proceed 
     548                if ( $wpdb->query( $myTableIdentityQuery ) === false ) 
     549                { 
     550                        return false; 
     551                } 
     552 
     553                // Just to be sure, we check that tables were actually created 
     554                foreach ( $wpdb->get_col("SHOW TABLES", 0) as $table ) 
     555                { 
     556                        if ( $table == $identitytable ) 
     557                        { 
     558                                return true; 
     559                        } 
     560                } 
     561                return false; 
     562        } // end function createInfocardIdentityTable 
     563 
     564        /* 
     565                Function:       fixDatabase 
     566                Description:    updates/alters database depending on detection 
     567                                of database version 
     568                Input:          none 
     569                Output:         status string 
     570                Side-Effect:    may alter database structure 
     571        */ 
     572        function fixDatabase() 
     573        { 
     574                include_once('db_upgrade.pw.php'); 
     575                $output = "<h4>Upgrading PamelaWare to " . PW_VERSION . "</h4>"; 
     576                if (pw_admin::getOption('pw_db_version'))        
     577                        return $output . "oops sorry, up to date</p>"; 
     578 
     579                if (pw_admin::getOption('pw_infocard_key')) 
     580                { 
     581                        $output .= pw_db_upgrade::upgradeDBv2ToDBv3(); 
     582                        return $output . "</p>";                 
     583                } 
     584 
     585                if (pw_admin::getOption('infocard_key')) 
     586                { 
     587                        $output .= pw_db_upgrade::upgradeDBv1ToDBv3(); 
     588                        return $output . "</p>"; 
     589                } 
     590                return $output . "ERROR:  non-standard database state!"; 
    496591        } 
    497592} // end class pw_admin 
  • branches/RB-0.9/wp-infocard/wp/processing.pw.php

    r108 r122  
    5151        } // end function showProcessingSummary 
    5252 
     53        /* 
     54                Function:       getDBv1CardHash 
     55                Description:    Before we started storing card hash, we stored 
     56                                the PPID and an MD5 hash of the base64-decoded 
     57                                modulus. 
     58                                This means that when we upgraded the database 
     59                                from v1 to v3, we had to make a hash out of 
     60                                the ppid and the md5 hash, not the ppid and the 
     61                                modulus. 
     62                Input:          none 
     63                Output:         SHA256 card hash with ppid  
     64                                        & md5(base64_decode(modulus)) 
     65        */ 
     66        function getDBv1CardHash() 
     67        { 
     68                global $claimslist; 
     69 
     70                $mod = md5(base64_decode($claimslist['signerkeymodulus']->token_value)); 
     71                pw_utils::printDebug('<br/>Checking for DBv1 card hash<br/>'); 
     72                pw_utils::printDebug('Modulus:  ' . $mod . '<br/>'); 
     73                 
     74                $cardhash = pw_processing::calculateCardHash( 
     75                        $claimslist['privatepersonalidentifier']->token_value, 
     76                        $mod 
     77                ); 
     78                pw_utils::printDebug('CardHash:  ' . $cardhash . '<br/>'); 
     79                return $cardhash; 
     80        } //end function getDBv1CardHash 
     81         
    5382} // end class pw_processing 
    5483?> 
  • branches/RB-0.9/wp-infocard/wp/userdata.pw.php

    r110 r122  
    527527                        . $cardhash . "'"; 
    528528                $identity_info = $wpdb->get_results( $query, OBJECT ); 
     529 
     530                if ((count($identity_info) === 0) && (pw_admin::getOption('pw_dbv1_users'))) 
     531                { 
     532                        $legacy_cardhash = pw_processing::getDBv1CardHash(); 
     533                        $query = "SELECT * FROM `" . PW_TABLE_PREFIX 
     534                        . "infocard_identities` WHERE cardhash = '" 
     535                        . $legacy_cardhash . "'"; 
     536                        $identity_info = $wpdb->get_results( $query, OBJECT ); 
     537 
     538                        if (count($identity_info) == 1) 
     539                        {        
     540                                pw_userdata::updateLegacyCardHashFromToken($cardhash, $identity_info[0]->accountid); 
     541 
     542                                $query = "SELECT * FROM `" . PW_TABLE_PREFIX 
     543                                . "infocard_identities` WHERE cardhash = '" 
     544                                . $cardhash . "'"; 
     545                                $identity_info = $wpdb->get_results( $query, OBJECT ); 
     546                                delete_usermeta($identity_info[0]->accountid, 'ppid');   
     547                                delete_usermeta($identity_info[0]->accountid, 'identity');       
     548                        } 
     549                } 
     550 
    529551                switch (count($identity_info)) 
    530552                { 
     
    715737        }//end function userLoginIsUnique($user_login) 
    716738 
     739        function updateLegacyCardHashFromToken($cardhash, $accountid) 
     740        {        
     741                if ((!$cardhash) | (!$accountid)) 
     742                { 
     743                        pw_utils::printDebug('ERROR: missing parameter from updateLegacyCardHashFromToken<br/>'); 
     744                        return; 
     745                } 
     746                global $claimslist, $wpdb; 
     747                $cardhandle = pw_processing::constructCardHandle( 
     748                        $claimslist['privatepersonalidentifier']->token_value, 
     749                        $claimslist['signerkeymodulus']->token_value 
     750                ); 
     751                $query = "UPDATE " . PW_TABLE_PREFIX . "infocard_identities " 
     752                . "SET cardhash = '" . $cardhash . "', cardhandle = '"  
     753                . $cardhandle . "' WHERE accountid = '" . $accountid . "'"; 
     754 
     755                $wpdb->query( $query ); 
     756         
     757                $total_cards = pw_admin::getOption('pw_dbv1_users'); 
     758                $total_cards = $total_cards - 1; 
     759                pw_admin::setOption('pw_dbv1_users', $total_cards); 
     760                pw_utils::printDebug('DBv1 users left: ' . $total_cards . '<br/>'); 
     761        } //end function updateLegacyCardHash 
    717762} // end class pw_userdata 
    718763?> 
  • branches/RB-0.9/wp-infocard/wp/utils.pw.php

    r121 r122  
    319319        } 
    320320 
     321        function databaseUpToDate() 
     322        { 
     323                // Current version:  PW DB v3 
     324                // note that for this version anything older than the 
     325                // current version  will not even have a pw_db_version 
     326                // option 
     327                return (bool)pw_admin::getOption('pw_db_version'); 
     328        } 
     329 
    321330} // end class pw_utils 
    322331?>