Search code examples
phpopenid

Undefined variable with OpenID


I have a website running on a less well known CMS called Ushahidi. There is built in OpenID functionality where folk can login with Facebook or Google.

I don't have enough dev skills to understand whats happening here but, it appears that I've almost got it working, except, I'm receiving the following error when trying to test it out on my own Google login:

An error was detected which prevented the loading of this page. If this problem persists, please contact the website administrator. application/controllers/login.php [503]: Undefined variable: user

I suspect, but am not sure, that defining a variable is easy enough but since I lack the knowledge I hoped to ask someone on here if they could see where I need to define the variable. Line 503 is part of a larger code block of about 100 lines, I know that it's not good practice to post larger chunks of code on here but I'm really unsure of what is and is not relevant. So forgive me. I have highlighted in bold where line 503 is. Can anyone point out what I must do here?

// OpenID Post
            try
            {
                $openid = new OpenID;

                // Retrieve the Name (if available) and Email
                $openid->required = array("namePerson", "contact/email");

                if( ! $openid->mode)
                {
                    if(isset($_POST["openid_identifier"]))
                    {
                        $openid->identity = $_POST["openid_identifier"];
                        header("Location: " . $openid->authUrl());
                    }
                }
                elseif ($openid->mode == "cancel")
                {
                    $openid_error = TRUE;
                    $message_class = 'login_error';
                    $message = "You have canceled authentication!";
                }
                else
                {
                    if ($openid->validate())
                    {
                        // Does User Exist?
                        $openid_user = ORM::factory("openid")
                            ->where("openid", $openid->identity)
                            ->find();

                        if ($openid_user->loaded AND $openid_user->user)
                        {
                            // First log all other sessions out
                            $auth->logout();

                            // Initiate Ushahidi side login + AutoLogin
                            $auth->force_login($openid_user->user->username);

                    // Exists Redirect to Dashboard
        **(THIS IS LINE 503)**         url::redirect($user->dashboard());
                        }
                        else
                        {
                            // Does this openid have the required email??
                            $new_openid = $openid->getAttributes();
                            if ( ! isset($new_openid["contact/email"]) OR
                                empty($new_openid["contact/email"]))
                            {
                                $openid_error = TRUE;
                                $message_class = 'login_error';
                                $message = $openid->identity . " has not been logged in. No Email Address Found.";
                            }
                            else
                            {
                                // Create new User and save OpenID
                                $user = ORM::factory("user");

                                // But first... does this email address already exist
                                // in the system?
                                if ($user->email_exists($new_openid["contact/email"]))
                                {
                                    $openid_error = TRUE;
                                    $message_class = 'login_error';
                                    $message = $new_openid["contact/email"] . " is already registered in our system.";
                                }
                                else
                                {
                                    $username = "user".time(); // Random User Name from TimeStamp - can be changed later
                                    $password = text::random("alnum", 16); // Create Random Strong Password

                                    // Name Available?
                                    $user->name = (isset($new_openid["namePerson"]) AND ! empty($new_openid["namePerson"]))
                                        ? $new_openid["namePerson"]
                                        : $username;
                                    $user->username = $username;
                                    $user->password = $password;
                                    $user->email = $new_openid["contact/email"];

                                    // Add New Roles
                                    $user->add(ORM::factory('role', 'login'));
                                    $user->add(ORM::factory('role', 'member'));

                                    $user->save();

                                    // Save OpenID and Association
                                    $openid_user->user_id = $user->id;
                                    $openid_user->openid = $openid->identity;
                                    $openid_user->openid_email = $new_openid["contact/email"];
                                    $openid_user->openid_server = $openid->server;
                                    $openid_user->openid_date = date("Y-m-d H:i:s");
                                    $openid_user->save();

                                    // Initiate Ushahidi side login + AutoLogin
                                    $auth->login($username, $password, TRUE);

                                    // Redirect to Dashboard
                                    url::redirect($user->dashboard());
                                }
                            }
                        }
                    }
                    else
                    {
                        $openid_error = TRUE;
                        $message_class = 'login_error';
                        $message = $openid->identity . "has not been logged in.";
                    }
                }
            }
            catch (ErrorException $e)
            {
                $openid_error = TRUE;
                $message_class = 'login_error';
                $message = $e->getMessage();
            }

Solution

  • The problem is that the code is using $user several lines before it's actually defined. It might be a typo, though - maybe $openid_user->user->dashboard() at line 503 might work, though it's a WAG.