Binary files openid/login-bg.png and openid5/login-bg.png differ
diff -u openid/openid.css openid5/openid.css
--- openid/openid.css	2007-02-06 07:55:39.000000000 -0800
+++ openid5/openid.css	2007-02-06 07:51:42.000000000 -0800
@@ -0,0 +1,7 @@
+#openid-login-create-form #edit-openid-url,
+#user_edit #edit-openid_delegate {
+  background-image: url("login-bg.png");
+  background-position: 0% 50%;
+  background-repeat: no-repeat;
+  padding-left: 20px; 
+}
diff -u openid/openid.info openid5/openid.info
--- openid/openid.info	2007-02-06 07:55:32.000000000 -0800
+++ openid5/openid.info	2007-02-06 07:51:42.000000000 -0800
@@ -0,0 +1,3 @@
+name = "OpenID"
+description = "Allows users to authenticate with OpenID"
+version = "5.x-1.x-dev"
diff -u openid/openid.install openid5/openid.install
--- openid/openid.install	2007-02-06 07:55:35.000000000 -0800
+++ openid5/openid.install	2007-02-06 07:51:42.000000000 -0800
@@ -0,0 +1,30 @@
+<?php
+	function openid_install(){
+		switch($GLOBALS['db_type']){
+		  case 'mysql':
+		  case 'mysqli':
+		  	$query1 = db_query("CREATE TABLE {openid} (
+						uid INT NOT NULL,
+						openid VARCHAR( 64 ) NOT NULL,
+						PRIMARY KEY (uid)
+						)");
+			break;
+		}
+		if($query1){
+			drupal_set_message(t("The OpenID module was installed successfully."));
+		}
+		else{
+			drupal_set_message(t("There was an error installing the OpenID module."), 'error');
+		}
+	}
+
+	function openid_uninstall(){
+		$query1 = db_query('DROP TABLE (users_openid)');
+
+		if($query1){
+			drupal_set_message(t('The OpenID module was removed successfully.'));
+		}
+		else{
+			drupal_set_message(t('There was an error removing the OpenID module.'),'error');
+		}
+	}
diff -u openid/openid.module openid5/openid.module
--- openid/openid.module	2006-08-28 10:26:15.000000000 -0700
+++ openid5/openid.module	2007-02-06 07:51:42.000000000 -0800
@@ -3,7 +3,7 @@
 /**
  * Drupal OpenID Consumer Plugin
  *
- * Copyright 2006 JanRain, Inc.
+ * Copyright 2007 Justin Gallardo
  *
  * This module implements consumer support for OpenID authentication.
  * Using this module, OpenID users can log into a Drupal installation.
@@ -58,30 +58,6 @@
 }
 
 /**
- * Returns a short description of this module.
- */
-function openid_help($section) {
-    switch ($section) {
-    case 'user/help#openid':
-        return t('Allows users to authenticate via OpenID');
-    }
-}
-
-/**
- * Returns this module's visible name.
- */
-function openid_info($field = 0) {
-    $info['name'] = 'OpenID';
-    $info['protocol'] = 'OpenID';
-
-    if ($field) {
-        return $info[$field];
-    }
-
-    return $info;
-}
-
-/**
  * An empty auth hook so this module shows up as an auth module.  The
  * real work is done by openid_login_form_submit and openid_return.
  */
@@ -101,22 +77,142 @@
         $blocks[0]["info"] = t("OpenID Login");
         return $blocks;
     } else if ($op == 'view' && (!$user->uid)) {
-
-        $form = '';
-        $form .= form_textfield('OpenID',
-                                'openid_url',
-                                '', 25, 64);
-
-        $form .= form_submit('Log In');
-
+	drupal_add_css(drupal_get_path('module', 'openid') . '/openid.css');
         $block['subject'] = t("OpenID Login");
-        $block['content'] = form($form, 'post',
-                                 url('openid/login'));
+        $block['content'] = drupal_get_form("openid_login_create_form");// url('openid/login'));
 
         return $block;
     }
 }
 
+/**
+ * Adds an OpenID field to the user profile page.
+ */
+
+function openid_user($op, &$edit, &$user, $category = NULL){
+	switch($op){
+	  case 'form':
+	  	if($category == 'account'){
+			return openid_user_form($edit, $user, $category);
+		}
+		break;
+	  case 'view':
+	  	if($openid = openid_view($user->uid)){
+			$form['openid'] = array(
+			  'value' => $openid,
+			  );
+			  return array(t('OpenID') => $form);
+		}
+		break;
+	  case 'delete':
+	  	return openid_delete($user->uid);
+		break;
+	  case 'update':
+	  	return openid_update($user->uid, $edit);
+		break;
+	}
+}
+
+/**
+ * This runs when a user's profile is viewed. We use this to display our user's OpenID.
+ */
+
+function openid_view($uid){
+	$row = db_fetch_object(db_query("SELECT openid FROM {openid} WHERE uid = '%d'", $uid));
+	if($row->openid){
+		return $row->openid;
+	}
+	else{
+		return '';
+	}
+}
+
+/**
+ * This runs when a user is deleted. We want to be sure their entry is deleted from the openid table.
+ */
+
+function openid_delete($uid){
+	db_query("DELETE FROM {openid} WHERE uid = '%d'", $uid);
+}
+
+/**
+ * This runs when a user updates their profile. This way we can make sure their OpenID is up to date.
+ */
+ 
+function openid_update($uid, &$edit){
+	$openid = openid_normalize_url($edit['openid']);
+	
+	if($openid){
+		if(!valid_url($openid, TRUE)){
+			drupal_set_message("Please enter a valid URL for your OpenID (Including the http:// will probably fix this).", 'error');
+		    	header("Location: " . url('user/' . $uid . '/edit'));
+			exit(0);
+		}
+		if($row = db_fetch_object(db_query("SELECT * FROM {openid} WHERE openid = '%s'", $openid))){
+			if($row->uid != $uid){
+				drupal_set_message("That OpenID is unavailable.", 'error');
+				header("Location: " . url('user/' . $uid . '/edit'));
+				exit(0);
+			}
+		}
+		if(db_result(db_query("SELECT uid FROM {openid} WHERE uid = '%d'", $uid))){
+			db_query("UPDATE {openid} SET openid = '%s' WHERE uid = '%d'", $openid, $uid);
+		}
+		else{
+			db_query("INSERT INTO {openid} VALUES ('%d', '%s')", $uid, $openid);
+		}
+	}
+	else{
+		db_query("DELETE FROM {openid} WHERE uid = '%d'", $uid);
+	}
+	
+	unset($edit['openid']);
+}
+
+/**
+ * Creates the form used for grabbing a user's OpenID.
+ */
+
+function openid_user_form($edit, $user, $category){
+	$row = db_fetch_object(db_query("SELECT openid FROM {openid} WHERE uid = '%d'", $user->uid));
+	$form['openid'] = array(
+	  '#type' => 'fieldset',
+	  '#collapsible' => FALSE,
+	  '#collapsed' => FALSE,
+	  '#weight' => 1,
+	  );
+	$form['openid']['openid'] = array(
+	  '#type' => 'textfield',
+	  '#title' => t('OpenID'),
+	  '#maxlength' => 64,
+	  '#default_value' => $row->openid,
+	  '#description' => 'If you have one, enter your OpenID here.',
+	  );
+	return $form;
+}
+
+/**
+ * Creates the OpenID Login Form
+ */
+function openid_login_create_form(){
+	$form['openid_url'] = array(
+	  '#type' => 'textfield',
+	  '#title' => t("OpenID"),
+	  '#default_value' => '',
+	  '#size' => 20,
+	  '#maxlength' => 64,
+	  '#description' => t('For example: myblog.bloghost.com')
+	  );
+	
+	$form['#action'] = url('openid/login');
+	
+	$form['submit'] = array(
+	  '#type' => 'submit',
+	  '#value' => t('Log In')
+	  );
+	
+	return $form;
+}
 
 /**
  * Define paths that will be accessed through Drupal.
@@ -129,23 +225,31 @@
 function openid_menu($may_cache) {
     $items = array();
 
-    $items[] = array('path' => 'openid/get_email', 'title' => t('openid email form'),
+    $items[] = array('path' => 'openid/get_email', 'title' => t('OpenID User Creation'),
                      'callback' => 'openid_email_form', 'type' => MENU_CALLBACK,
-                     'access' => TRUE);
+                     'access' => user_access('access content'));
     $items[] = array('path' => 'openid/submit_email', 'title' => t('email form submit'),
                      'callback' => 'openid_create_account_submit', 'type' => MENU_CALLBACK,
-                     'access' => TRUE);
+                     'access' => user_access('access content'));
     $items[] = array('path' => 'openid/login', 'title' => t('openid login'),
                      'callback' => 'openid_login_form_submit', 'type' => MENU_CALLBACK,
-                     'access' => TRUE);
+                     'access' => user_access('access content'));
     $items[] = array('path' => 'openid/return', 'title' => t('openid return'),
                      'callback' => 'openid_return', 'type' => MENU_CALLBACK,
-                     'access' => TRUE);
+                     'access' => user_access('access content'));
 
     return $items;
 }
 
 /**
+ * Defines the permissions available for the different menu callbacks.
+ */
+
+function openid_perm(){
+	return array('access content');
+}
+
+/**
  * Callback for the OpenID login form.  This is called when the OpenID
  * login form is submitted.  This begins the OpenID authentication
  * process and redirects the user's browser to the OpenID server.
@@ -157,29 +261,26 @@
     $op = $query['op'];
     $dest = $query['destination'];
 
-    $identity_url = $edit['openid_url'];
+    $identity_url = $query['openid_url'];
 
     $previous_error_level = variable_get('error_level', 0);
     variable_set('error_level', 0);
     $consumer = new Auth_OpenID_Consumer(new OpenID_DrupalStore());
-
     $auth_request = $consumer->begin($identity_url);
     variable_set('error_level', $previous_error_level);
-
     if ($auth_request) {
         $trust_root = url(NULL, NULL, NULL, TRUE);
 
         // Add simple registration arguments, but only if the URL
         // being auth'd doesn't exist in the db.
         if (!user_get_authmaps(Auth_OpenID::normalizeUrl($identity_url))) {
-            $auth_request->addExtensionArg('sreg', 'optional', 'email');
+            $auth_request->addExtensionArg('sreg', 'optional', 'email,nickname');
             $auth_request->addExtensionArg('sreg', 'policy_url', 'http://example.com/policy');
         }
-
-        $app_url = url('openid/return', NULL, NULL, TRUE);
+        
+	$app_url = url('openid/return', NULL, NULL, TRUE);
 
         $redirect_url = $auth_request->redirectURL($trust_root, $app_url);
-
         header( 'Location: ' . $redirect_url );
         exit;
     } else {
@@ -221,10 +322,11 @@
 
         $openid_url = $response->identity_url;
         $destination = $query['destination'];
-
-        if ($result = user_get_authmaps($openid_url)) {
-
-            $user = user_external_load($openid_url);
+        
+	$row = db_fetch_object(db_query("SELECT * FROM {openid} WHERE openid = '%s'", $openid_url));
+	if($row->uid){
+	    $user = user_load(array( 'uid' => $row->uid ));
+            //$user = user_external_load($openid_url);
             watchdog('user', t('External load by %user using module %module.',
                                array('%user' => theme('placeholder', $openid_url),
                                      '%module' => theme('placeholder', 'openid'))));
@@ -234,8 +336,8 @@
 
             if ($sreg) {
                 $_SESSION['sreg_email'] = $sreg['email'];
+		$_SESSION['sreg_nickname'] = $sreg['nickname'];
             }
-
             // Give the user a page that will request an email
             // address before creating the drupal account.
             header("Location: " . url('openid/get_email'));
@@ -260,22 +362,50 @@
         exit;
     }
 
-    $form = '';
-
-    $form .= form_textfield('Email Address', 'email', @$_SESSION['sreg_email'],
-                            25, 64, 'Enter your email address.');
-
-    $form .= form_submit('Submit');
-
     $content = sprintf("<h3>Create account with OpenID</h3><p>Before logging in " .
-                       "with your OpenID (%s), you must enter an email address:</p> %s",
-                       $_SESSION['openid'], form($form, 'post',
-                                                 url('openid/submit_email')));
+                       "with your OpenID (%s), we need a little information for you new account:</p> %s",
+                       $_SESSION['openid'], drupal_get_form('openid_email_create_form'));
 
     print theme('page', $content);
 }
 
 /**
+ * Creates the email address create form.
+ */
+
+function openid_email_create_form(){
+	$form['nickname'] = array(
+	  '#type' => 'textfield',
+	  '#title' => t('Username'),
+	  '#default_value' => @$_SESSION['sreg_nickname'],
+	  '#size' => 25,
+	  '#maxlength' => 64,
+	  '#required' => TRUE,
+	  '#description' => 'Enter your desired username.',
+	  );
+	
+	$form['email'] = array(
+	  '#type' => 'textfield',
+	  '#title' => t('Email Address'),
+	  '#default_value' => @$_SESSION['sreg_email'],
+	  '#size' => 25,
+	  '#maxlength' => 64,
+	  '#required' => TRUE,
+	  '#description' => 'Enter your email address.'
+	  );
+	
+	$form['#action'] = url('openid/submit_email');
+	
+	$form['submit'] = array(
+	  '#type' => 'submit',
+	  '#value' => 'Submit'
+	  );
+	
+	return $form;
+	  
+}
+
+/**
  * Once an OpenID-authenticated user submits an email address, this is
  * called and is used to create the user's account and log them in.
  */
@@ -284,23 +414,44 @@
     global $user;
 
     $query = $_POST;
-
-    if (!valid_email_address($query['edit']['email'])) {
+    
+    if (user_validate_name($query['nickname']) != NULL){
+	drupal_set_message('Username invalid.', 'error');
+	header("Location: " . url('openid/get_email'));
+	exit(0);
+    }
+    if (!valid_email_address($query['email'])) {
         drupal_set_message('Email address invalid.', 'error');
         header("Location: " . url('openid/get_email'));
         exit(0);
     }
 
+    if (user_load(array('name' => $query['nickname']))){
+    	drupal_set_message('Username exists.', 'error');
+	header("Location: " . url('openid/get_email'));
+	exit(0);
+    }
+
+    if (user_load(array('mail' => $query['email']))){
+    	drupal_set_message('Email exists.', 'error');
+	header("Location: " . url('openid/get_email'));
+	exit(0);
+    }
     // Process a form submission for an openid-authenticated user and
     // create an account for them.
 
-    $user = user_save('', array('name' => $_SESSION['openid'],
+    $user = user_save('', array('name' => $query['nickname'],
                                 'pass' => user_password(),
-                                'mail' => $query['edit']['email'],
-                                'init' => $_SESSION['openid'],
+                                'mail' => $query['email'],
+                                'init' => $query['email'],
                                 'status' => 1,
-                                "authname_openid" => $_SESSION['openid'],
+                                //"authname_openid" => $_SESSION['openid'],
                                 'roles' => array(DRUPAL_AUTHENTICATED_RID)));
+    
+    if($user){
+    	$query = db_query("INSERT INTO {openid} VALUES ('%d','%s')", $user->uid, openid_normalize_url($_SESSION['openid']));
+    }
+    
     watchdog('user',
              t('New external user: %user using module %module.',
                array('%user' => theme('placeholder', $_SESSION['openid']),
@@ -313,6 +464,23 @@
     drupal_goto();
 }
 
+
+/**
+ * Normalizes URLs so we can be sure they are all formed the same way.
+ */
+
+function openid_normalize_url($url){
+	$normalized_url = $url;
+	if(stristr($url, '://') === FALSE){
+		$normalized_url = 'http://' . $url;
+	}
+	if(!preg_match('/\/$/', $normalized_url)){
+		$normalized_url .= '/';
+	}
+	
+	return $normalized_url;
+}
+
 /**
  * Emulates a PEAR database connection which is what the Auth_OpenID
  * library expects to use for its database storage.  This just wraps
diff -u openid/README openid5/README
--- openid/README	2007-02-06 07:55:42.000000000 -0800
+++ openid5/README	2007-02-06 07:51:42.000000000 -0800
@@ -0,0 +1,14 @@
+Drupal 5.0 OpenID Module
+--------------------------
+This module is based on the JanRain Drupal module, but newly updated for the newest release of Drupal, version 5.
+It does require the PHP OpenID library from JanRain. 
+Info on how to install this can be found at http://www.openidenabled.com/openid/libraries/php. 
+There isn't really much configuration needed to get the module working. 
+Installing it, and setting up the block should be all that it takes. 
+
+This module allows users to authenticate to the Drupal site with their OpenID.
+Currently, it will pull a username and email address from the OpenID persona if the OpenID doesn't exist in the db. 
+For existing users, they can now set their OpenID under their profile, 
+allowing them to login with either their normal drupal login, or their newly configured OpenID. 
+
+Thanks to JanRain for taking care of all the heavy lifting with OpenID!
