Zend Framework - Oauth - Get LinkedInContacts

Get a request token

            
            /* Set up the Oauth consumer */
            $oauth = new Zend_Oauth_Consumer(array(
                'consumerKey' => $this->configs->connections->consumerKey,
                'consumerSecret' => $this->configs->connections->consumerSecret));
            $oauth->setRequestTokenUrl($this->configs->token_uri);

            /* Get the request token */
            $token = $oauth->getRequestToken();

            /* This is the URI to allow the user to authorize access */
            $auth_uri = $token->xoauth_request_auth_url.'?oauth_token='.urlencode($token->oauth_token);

            /* Save the request token */
            $session=new Zend_Session_Namespace('linkedin_connections');
            $session->token = serialize($token);

Get the OAuth token and token secret for the user

              
                /* Set up the OAuth consumer */
                $oauth = new Zend_Oauth_Consumer(array(
                    'consumerKey' => $this->configs->connections->consumerKey,
                    'consumerSecret' => $this->configs->connections->consumerSecret));
                $oauth->setAccessTokenUrl($this->configs->access_uri);

                /* Get the request token */
                $session=new Zend_Session_Namespace('linkedin_connections');
                $token = unserialize($session->token);

                /* Get the access token and token secret */
                $token = $oauth->getAccessToken(
                    array('oauth_token' => $token->oauth_token,
                        'oauth_token_secret' => $token->oauth_token_secret,
                        'oauth_verifier' => $data['auth_code']),
                    $token,Zend_Oauth_Consumer::POST);

                $response = $token->getResponse();
                $this->linkedinconnections_data->oauth_token = $token->oauth_token;
                $this->linkedinconnections_data->oauth_token_secret = $token->oauth_token_secret;
                $this->linkedinconnections_data->oauth_expires_in = $token->oauth_expires_in;
                $this->linkedinconnections_data->oauth_authorization_expires_in = $token->oauth_authorization_expires_in;

Get the connections

            
            /* Set up the Oauth client */
            $oauth = new Zend_Oauth_Client(array(
                'consumerKey' => $this->configs->connections->consumerKey,
                'consumerSecret' => $this->configs->connections->consumerSecret),$this->configs->connections_uri);

            /* Set up the tokens for the user */
            $token = new Zend_Oauth_Token_Access();
            $token->setToken($this->linkedinconnections_data->oauth_token);
            $token->setTokenSecret($this->linkedinconnections_data->oauth_token_secret);

            /* Set the tokens for the client */
            $oauth->setToken($token);
            $oauth->setMethod(Zend_Oauth_Client::GET);

            /* Make the request */
            $response = $oauth->request();

Zend Framework Version 1.11

Zend Framework - Navigation View Helper - Active Style Rules

The objective of these rules are to ensure only the active link is highlighted.

The key is the ‘element>element’ selector, which indicates that only a tags which have a parent of li.active will be affected by the rule.

#mainNav a
{
color:#888;
}
#mainNav li.active>a
{
color:#000;
}
#mainNav a:hover
{
color:#444;
}

Token Handling - Zend Framework - OAuth 2.0 - Google

Sample code to get a request token from Google through OAuth 2.0. These are snippets of code to show the request and response interaction.

This is the link to allow a user to authorize application access.

        $auth_uri='https://accounts.google.com/o/oauth2/auth?'.
                'client_id='.$this->configs->contacts->client->id.'&'.
                'redirect_uri='.$this->configs->contacts->redirect_uri.'&'.
                'scope='.$this->configs->contacts->scope.'&'.
                'response_type=code';

If the user authorizes access, Google gives them a token, which is referred to as an auth_code in the following code. They paste the token in the auth_code input and click a button to initiate this action.

            if ($form->getElement('auth_code')->isValid($data['auth_code']))
            {
                $client = new Zend_Http_Client($this->configs->oauth_uri,
                    array( 'maxredirects' => 0, 'timeout'      => 30));
                $client->setMethod(Zend_Http_Client::POST);
                $client->setHeaders('Content-Type: application/x-www-form-urlencoded');
                $client->setParameterPost(array(
                    'code' => $data['auth_code'],
                    'client_id' => $this->configs->contacts->client->id,
                    'client_secret' => $this->configs->contacts->client->secret,
                    'redirect_uri' => $this->configs->contacts->redirect_uri,
                    'grant_type' => 'authorization_code'));
                $response = $client->request();
                $this->googlecontacts_data->last_status = $response->getStatus();
                if ($response->isSuccessful())
                {
                    $response_data = Zend_Json::decode($response->getBody());
                    $this->googlecontacts_data->last_auth = new Doctrine_Expression('NOW()');
                    $this->googlecontacts_data->access_token = $response_data['access_token'];
                    $this->googlecontacts_data->expires_in = $response_data['expires_in'];
                    $this->googlecontacts_data->token_type = $response_data['token_type'];
                    $this->googlecontacts_data->refresh_token = $response_data['refresh_token'];
                    $this->googlecontacts_data->status = 'authorized';
                }
                else
                {
                    $this->googlecontacts_data->access_token =
                    $this->googlecontacts_data->expires_in =
                    $this->googlecontacts_data->token_type =  
                    $this->googlecontacts_data->refresh_token = null;
                    $this->googlecontacts_data->status = 'not_authorized';
                }
                $this->save_googlecontacts_data(); 

This code uses the access or refresh token to retrieve the contacts.

        if ($this->googlecontacts_data->status == 'authorized')
        {
            $client = new Zend_Http_Client($this->configs->contacts->userinfo,
                array( 'maxredirects' => 0, 'timeout' => 30));
            $client->setMethod(Zend_Http_Client::GET);
            $client->setHeaders('Authorization: Bearer '.$this->googlecontacts_data->access_token);
            $response = $client->request();
            if (!$response->isSuccessful())
            {
                $client = new Zend_Http_Client($this->configs->oauth_uri,
                    array( 'maxredirects' => 0, 'timeout'      => 30));
                $client->setMethod(Zend_Http_Client::POST);
                $client->setHeaders('Content-Type: application/x-www-form-urlencoded');
                $client->setParameterPost(array(
                    'client_id' => $this->configs->contacts->client->id,
                    'client_secret' => $this->configs->contacts->client->secret,
                    'refresh_token' => $this->googlecontacts_data->refresh_token,
                    'grant_type' => 'refresh_token'));
                $response = $client->request();
            }
            $this->googlecontacts_data->last_status = $response->getStatus();
            if ($response->isSuccessful())
            {
                $response_data = Zend_Json::decode($response->getBody());
                $this->googlecontacts_data->last_auth = new Doctrine_Expression('NOW()');
                $this->googlecontacts_data->access_token = $response_data['access_token'];
                $this->googlecontacts_data->expires_in = $response_data['expires_in'];
                $this->googlecontacts_data->token_type = $response_data['token_type'];
                $this->googlecontacts_data->auto = null;
                $this->googlecontacts_data->deleted_at = null;
                $this->googlecontacts_data->status = 'authorized';
            }
            else
            {
                $this->googlecontacts_data->access_token =
                $this->googlecontacts_data->expires_in =
                $this->googlecontacts_data->token_type =
                $this->googlecontacts_data->refresh_token =
                $this->googlecontacts_data->auto = null;
                $this->googlecontacts_data->status = 'not_authorized';
            }
            $return = $this->save_googlecontacts_data();
            if (!isset($return['error']))
            {
                $this->view->results = $this->get_contacts();
                $this->return['success'] = true;
            }
        }
        else
            $this->return['error'] = $this->status();

Some of the config values (other omitted for security):

oauth_uri = “https://accounts.google.com/o/oauth2/token”
contacts.uri = “https://www.google.com/m8/feeds/contacts/default/full”
contacts.scope = “https://www.google.com/m8/feeds/”
contacts.userinfo = “https://www.googleapis.com/oauth2/v1/userinfo”

.ini file settings for auth_code input. This application forces the user to cut and paste the token into the browser.

[production]
action="/contacts/google”
method="post”

disableTranslator = 0
; code element
elements.auth_code.type = “ValidationTextBox”
elements.auth_code.options.label = “Authorization Code”
elements.auth_code.options.required = true
elements.auth_code.options.trim = “true”
elements.auth_code.options.class = “long”
elements.auth_code.options.validators.strlen.validator = “StringLength”
elements.auth_code.options.validators.strlen.options.min = “8″
elements.auth_code.options.validators.strlen.options.max = “100″
elements.auth_code.options.validators.regex.validator = “regex”
elements.auth_code.options.validators.regex.options.pattern = “/^[\w\/\-]{8,100}$/”
elements.auth_code.options.validators.regex.options.messages.regexInvalid = “Invalid code”
elements.auth_code.options.filters[] = “StringTrim”
elements.auth_code.options.filters[] = “StripTags”
elements.auth_code.options.filters[] = “StripNewlines”

displayGroups.gcode.options.order = 10
displayGroups.gcode.options.class = “auth_code”
displayGroups.gcode.elements[] = “auth_code”

Zend Framework 1.11 - Rename Form Elements

Workaround

In this case, the method accepts the name of the element and applies a prefix.

The code was derived from Zend_Form::removeElement.

private function prefixElement($name)
        {
            $name = (string) $name;
            if (isset($this->_elements[$name])) {
                $this->_elements[$name]->setName($this->_prefix.$name);
                $this->_elements[$this->_prefix.$name]=$this->_elements[$name];
                unset($this->_elements[$name]);
                if (array_key_exists($name, $this->_order)) {
                    $this->_order[$this->_prefix.$name]=$this->_order[$name];
                    unset($this->_order[$name]);
                    $this->_orderUpdated = true;
                } else {
                    foreach ($this->_displayGroups as $group) {
                        if (null !== $group->getElement($name)) {
                            $group->addElement($this->getElement($this->_prefix.$name));
                            $group->removeElement($name);
                        }
                    }
                }
                return true;
            }

            return false;
        }

dojo 1.6.1 Form.reset()

Code to clear a form without firing the onChange events on the inputs.

 /* frmDijit.reset() */
                        dojo.query('#frmForm input[class^=dijit]').forEach(
                        function(item)
                        {
                                if ((d=dijit.byId(item.id))!=null)
                                        switch(d.type)
                                        {
                                        case 'checkbox':
                                        case 'radio':
                                                d.set('checked',false);
                                                break;
                                        case 'hidden':
                                        case 'text': 
                                                d.set('value','',false);
                                                break;
                                        }
                        });