+ /**
+ * Begin the OpenID login by redirecting user to the OP to authenticate.
+ *
+ * @param string $openid
+ */
+ public function openid_begin_login($openid)
+ {
+ $this->lang->load('openid');
+ $this->load->library('openid');
+
+ $request_to = site_url('user/check_openid_login');
+
+ $req = array('nickname');
+ $opt = array('fullname', 'email', 'dob', 'country');
+ $policy = site_url('user/openid_policy');
+
+ $ax_attributes[] = Auth_OpenID_AX_AttrInfo::make(
+ 'http://axschema.org/contact/email', 1, TRUE);
+ $ax_attributes[] = Auth_OpenID_AX_AttrInfo::make(
+ 'http://axschema.org/namePerson/first', 1, TRUE);
+ $ax_attributes[] = Auth_OpenID_AX_AttrInfo::make(
+ 'http://axschema.org/namePerson/last', 1, TRUE);
+ $ax_attributes[] = Auth_OpenID_AX_AttrInfo::make(
+ 'http://axschema.org/contact/country', 1, TRUE);
+
+ $this->openid->set_request_to($request_to);
+ $this->openid->set_trust_root(base_url());
+ $this->openid->set_sreg(TRUE, $req, $opt, $policy);
+ $this->openid->set_ax(TRUE, $ax_attributes);
+
+ // Redirection to OP site will follow.
+ $this->openid->authenticate($openid);
+ }
+
+ /**
+ * Finalize the OpenID login. Register user if is here for the first time.
+ *
+ * @return mixed returns a `users` DB row as an associative array if
+ * authentication was successful or Auth_OpenID_CANCEL/_FAILURE if it was
+ * unsuccessful.
+ */
+ public function openid_complete_login()
+ {
+ $this->lang->load('openid');
+ $this->load->library('openid');
+
+ $request_to = site_url('user/check_openid_login');
+ $this->openid->set_request_to($request_to);
+
+ $response = $this->openid->get_response();
+
+ if ($response->status === Auth_OpenID_CANCEL
+ || $response->status === Auth_OpenID_FAILURE)
+ return $response->status;
+
+ // Auth_OpenID_SUCCESS
+ $openid = $response->getDisplayIdentifier();
+ //$esc_openid = htmlspecialchars($openid, ENT_QUOTES);
+
+ // Get user_id to see if it's the first time the user logs in with
+ // OpenID.
+ $query = $this->db->query("SELECT * from `users_openid`
+ WHERE openid_url = '$openid'");
+ $import = FALSE;
+
+ // First time with OpenID => register user
+ if ($query->num_rows() === 0)
+ {
+ $user_id = $this->openid_register($response);
+ $import = TRUE;
+ }
+ // Not first time with OpenID.
+ else
+ $user_id = $query->row()->user_id;
+
+ // Login
+ $query = $this->db->query("SELECT * FROM `users`
+ WHERE id = $user_id");
+ $userdata = $query->row_array();
+ $userdata['import'] = $import;
+
+ if (empty($userdata['username']) || empty($userdata['email'])
+ || empty($userdata['first_name'])
+ || empty($userdata['last_name'])
+ || empty($userdata['country']))
+ $userdata['import'] = TRUE;
+
+ // Update last login time.
+ $this->db->query("UPDATE `users`
+ SET last_login = UTC_TIMESTAMP()
+ WHERE id = $user_id");
+
+ return $userdata;
+ }
+
+ /**
+ * Register an user that logged in with OpenID for the first time.
+ *
+ * @param object $op_response object returned by Janrain
+ * Consumer::complete method.
+ * @return mixed the user_id inserted or FALSE on error
+ */
+ public function openid_register($op_response)
+ {
+ $sreg_resp = Auth_OpenID_SRegResponse::fromSuccessResponse($op_response);
+ $ax_resp = Auth_OpenID_AX_FetchResponse::fromSuccessResponse($op_response);
+
+ if ($ax_resp)
+ {
+ $ax_email = $ax_resp->get('http://axschema.org/contact/email');
+ $ax_first_name = $ax_resp->get(
+ 'http://axschema.org/namePerson/first');
+ $ax_last_name = $ax_resp->get('http://axschema.org/namePerson/last');
+ $ax_country = $ax_resp->get('http://axschema.org/contact/country');
+ }
+ else
+ {
+ $ax_email = '';
+ $ax_first_name = '';
+ $ax_last_name = '';
+ $ax_country = '';
+ }
+
+ if ($sreg_resp)
+ {
+ $sreg_email = $sreg_resp->get('email', '');
+ $sreg_fullname = $sreg_resp->get('fullname', '');
+ $sreg_nickname = $sreg_resp->get('nickname', '');
+ $sreg_country = $sreg_resp->get('country', '');
+ $sreg_dob = $sreg_resp->get('dob', NULL);
+ }
+ else
+ {
+ $sreg_email = $sreg_fullname = $sreg_nickname = $sreg_country = '';
+ $sreg_dob = NULL;
+ }
+
+ // E-mail
+ if (empty($ax_email) || is_a($ax_email, 'Auth_OpenID_AX_Error'))
+ $data['email'] = $sreg_email;
+ else
+ $data['email'] = $ax_email[0];
+ $data['email'] = strtolower($data['email']);
+
+ // First Name
+ if (empty($ax_first_name)
+ || is_a($ax_first_name, 'Auth_OpenID_AX_Error'))
+ $data['first_name'] = '';
+ else
+ $data['first_name'] = $ax_first_name[0];
+
+ // Sur Name
+ if (empty($ax_last_name) || is_a($ax_last_name, 'Auth_OpenID_AX_Error'))
+ $data['last_name'] = '';
+ else
+ $data['last_name'] = $ax_last_name[0];
+
+ // First Name and Last Name
+ if (empty($data['first_name']) || empty($data['last_name']))
+ {
+ if ($sreg_fullname)
+ {
+ if (empty($data['first_name']))
+ $data['first_name'] = substr(
+ $sreg_fullname, 0, strrpos($sreg_fullname, ' '));
+ if (empty($data['last_name']))
+ $data['last_name'] = substr(
+ $sreg_fullname, strrpos($sreg_fullname, ' ') + 1);
+ }
+ }
+
+ // Username
+ $data['username'] = $sreg_nickname;
+ if (!$data['username'])
+ {
+ // Generate username from email
+ if (!empty($data['email']))
+ {
+ $data['username'] = substr($data['email'],
+ 0, strpos($data['email'], '@'));
+ $data['username'] = preg_replace(array('/[^a-z0-9\._]*/'),
+ array(''), $data['username']);
+ }
+ // Generate username from first name and sur name
+ else if(!empty($data['first_name']) || !empty($data['last_name']))
+ {
+ $data['username'] = $data['first_name'] . '_'
+ . $data['last_name'];
+ }
+ // Generate a random username
+ else
+ $data['username'] = $this->gen_username();
+ }
+ // Limit username to 24 characters because a prefix of 8 characters
+ // will be added: 'autogen_'.
+ $data['username'] = substr($data['username'], 0, 24);
+ // Append a random character to the username each time it still exists.
+ if ($this->get_userdata($data['username']))
+ {
+ $chars = 'abcdefghijklmnopqrstuvwxyz0123456789';
+ $len_chars = strlen($chars);
+ $data['username'] .= '_';
+ do
+ {
+ $data['username'] .= $chars[ mt_rand(0, $len_chars - 1) ];
+ } while($this->get_userdata($data['username']));
+ }
+ // Usernames autogenerated have 'autogen_' prefix and can be changed
+ // by the user from the account section. After this process it cannot
+ // be changed anymore.
+ $data['username'] = 'autogen_' . $data['username'];
+
+ // Country
+ if (empty($ax_country) || is_a($ax_country, 'Auth_OpenID_AX_Error'))
+ $data['country'] = $sreg_country;
+ else
+ $data['country'] = $ax_country[0];
+
+ // Birth Date
+ $data['birth_date'] = $sreg_dob;
+
+ // OpenID
+ $data['auth_src'] = 'openid';
+
+ if (!$this->register($data, $op_response->getDisplayIdentifier()))
+ return FALSE;
+
+ $query = $this->db->query("SELECT id from `users`
+ WHERE username = '{$data['username']}'");
+ return $query->row()->id;
+ }
+