diff --git a/includes/migrations.php b/includes/migrations.php index 484ffc1..252af0a 100644 --- a/includes/migrations.php +++ b/includes/migrations.php @@ -24,6 +24,7 @@ function run_migrations() { apply_migration( '0.0.2', 'migration_0_0_2' ); apply_migration( '0.0.3', 'migration_0_0_3' ); apply_migration( '0.0.4', 'migration_0_0_4' ); + apply_migration( '0.0.5', 'migration_0_0_5' ); update_option( 'pterotype_previously_migrated_version', PTEROTYPE_VERSION ); } @@ -222,4 +223,20 @@ function migration_0_0_4() { " ); } + +function migration_0_0_5() { + global $wpdb; + $wpdb->query( + ' + CREATE TABLE pterotype_keys( + actor_id INT UNSIGNED PRIMARY KEY, + public_key TEXT NOT NULL, + private_key TEXT NOT NULL, + FOREIGN KEY keys_actor_fk(actor_id) + REFERENCES pterotype_actors(id) + ) + ENGINE=InnoDB DEFAULT CHARSET=utf8; + ' + ); +} ?> diff --git a/includes/pgp.php b/includes/pgp.php new file mode 100644 index 0000000..8d581e9 --- /dev/null +++ b/includes/pgp.php @@ -0,0 +1,28 @@ +createKey( 2048 ); +} + +function persist_key( $actor_id, $public_key, $private_key ) { + global $wpdb; + return $wpdb->replace( + 'pterotype_keys', + array( + 'actor_id' => $actor_id, + 'public_key' => $public_key, + 'private_key' => $private_key + ), + array( '%d', '%s', '%s' ) + ); +} + +function get_public_key( $actor_id ) { + global $wpdb; + return $wpdb->get_var( $wpdb->prepare( + 'SELECT public_key FROM pterotype_keys WHERE actor_id = %d', $actor_id + ) ); +} +?> diff --git a/includes/server/actors.php b/includes/server/actors.php index c0d98ba..d810e90 100644 --- a/includes/server/actors.php +++ b/includes/server/actors.php @@ -1,6 +1,8 @@ get_row( $wpdb->prepare( @@ -109,8 +111,20 @@ function initialize_actors() { ); foreach ( $user_slugs as $user_slug ) { create_actor( $user_slug, 'user' ); + $actor_id = get_actor_id( $user_slug ); + $keys_created = \pgp\get_public_key( $actor_id ); + if ( ! $keys_created ) { + $keys = \pgp\gen_key( $user_slug ); + \pgp\persist_key( $actor_id, $keys['publickey'], $keys['privatekey'] ); + } } create_actor( PTEROTYPE_BLOG_ACTOR_SLUG, 'blog' ); + $blog_actor_id = get_actor_id( PTEROTYPE_BLOG_ACTOR_SLUG ); + $keys_created = \pgp\get_public_key( $blog_actor_id ); + if ( ! $keys_created ) { + $keys = \pgp\gen_key( PTEROTYPE_BLOG_ACTOR_SLUG ); + \pgp\persist_key( $blog_actor_id, $keys['publickey'], $keys['privatekey'] ); + } } function create_actor( $slug, $type ) { diff --git a/pterotype.php b/pterotype.php index acad744..e28d0ca 100644 --- a/pterotype.php +++ b/pterotype.php @@ -5,7 +5,7 @@ Plugin Name: Pterotype require_once plugin_dir_path( __FILE__ ) . 'vendor/autoload.php'; require_once plugin_dir_path( __FILE__ ) . 'includes/init.php'; -define( 'PTEROTYPE_VERSION', '0.0.4' ); +define( 'PTEROTYPE_VERSION', '0.0.5' ); define( 'PTEROTYPE_BLOG_ACTOR_SLUG', '-blog' ); define( 'PTEROTYPE_BLOG_ACTOR_USERNAME', 'blog' );