Overview

Packages

  • Push

Classes

  • Horde_Push
  • Horde_Push_Cli
  • Horde_Push_Exception
  • Horde_Push_Factory_Push
  • Horde_Push_Factory_Recipients
  • Horde_Push_Recipient_Base
  • Horde_Push_Recipient_Blogger
  • Horde_Push_Recipient_Facebook
  • Horde_Push_Recipient_Mail
  • Horde_Push_Recipient_Mock
  • Horde_Push_Recipient_Twitter
  • Horde_Push_Translation

Interfaces

  • Horde_Push_Recipient
  • Overview
  • Package
  • Class
  • Tree
  1: <?php
  2: /**
  3:  * Blogger.com as recipient.
  4:  *
  5:  * PHP version 5
  6:  *
  7:  * @category Horde
  8:  * @package  Push
  9:  * @author   Gunnar Wrobel <wrobel@pardus.de>
 10:  * @license  http://www.horde.org/licenses/lgpl21 LGPL 2.1
 11:  * @link     http://www.horde.org/libraries/Horde_Push
 12:  */
 13: 
 14: /**
 15:  * Blogger.com as recipient.
 16:  *
 17:  * Copyright 2011-2012 Horde LLC (http://www.horde.org/)
 18:  *
 19:  * See the enclosed file COPYING for license information (LGPL). If you did not
 20:  * receive this file, see http://www.horde.org/licenses/lgpl21.
 21:  *
 22:  * @category Horde
 23:  * @package  Push
 24:  * @author   Gunnar Wrobel <wrobel@pardus.de>
 25:  * @license  http://www.horde.org/licenses/lgpl21 LGPL 2.1
 26:  * @link     http://www.horde.org/libraries/Horde_Push
 27:  */
 28: class Horde_Push_Recipient_Blogger
 29: extends Horde_Push_Recipient_Base
 30: {
 31:     /**
 32:      * The HTTP client.
 33:      *
 34:      * @var Horde_Http_Client
 35:      */
 36:     private $_client;
 37: 
 38:     /**
 39:      * The connection details for blogger.com
 40:      *
 41:      * @var array
 42:      */
 43:     private $_params;
 44: 
 45:     /**
 46:      * Constructor.
 47:      *
 48:      * @param Horde_Http_Client $client The HTTP handler for connecting to 
 49:      *                                  blogger.com.
 50:      * @param array $params             The connection details for blogger.com.
 51:      */
 52:     public function __construct($client, $params)
 53:     {
 54:         $this->_client = $client;
 55:         $this->_params = $params;
 56:     }
 57: 
 58:     /**
 59:      * Push content to the recipient.
 60:      *
 61:      * @param Horde_Push $content The content element.
 62:      * @param array      $options Additional options.
 63:      *
 64:      * @return NULL
 65:      */
 66:     public function push(Horde_Push $content, $options = array())
 67:     {
 68:         $entry = new Horde_Feed_Entry_Atom(null, $this->_client);
 69: 
 70:         $types = $content->getMimeTypes();
 71:         if (isset($types['text/html'])) {
 72:             $body = $content->getStringContent($types['text/html'][0]);
 73:         } else if (isset($types['text/plain'])) {
 74:             $body = $content->getStringContent($types['text/plain'][0]);
 75:         } else {
 76:             $body = '';
 77:         }
 78: 
 79:         /* Give the entry its initial values. */
 80:         $entry->{'atom:title'} = $content->getSummary();
 81:         $entry->{'atom:title'}['type'] = 'text';
 82:         $entry->{'atom:content'} = $body;
 83:         $entry->{'atom:content'}['type'] = 'text';
 84:         
 85:         if (!empty($options['pretend'])) {
 86:             return sprintf(
 87:                 "Would push \n\n%s\n\n to %s.",
 88:                 (string) $entry,
 89:                 $this->_params['url']
 90:             );
 91:         }
 92: 
 93:         /* Authenticate. */
 94:         $response = $this->_client->post(
 95:             'https://www.google.com/accounts/ClientLogin',
 96:             'accountType=GOOGLE&service=blogger&source=horde-push&Email=' . $this->_params['username'] . '&Passwd=' . $this->_params['password'],
 97:             array('Content-type', 'application/x-www-form-urlencoded')
 98:         );
 99:         if ($response->code !== 200) {
100:             throw new Horde_Push_Exception('Expected response code 200, got ' . $response->code);
101:         }
102: 
103:         $auth = null;
104:         foreach (explode("\n", $response->getBody()) as $line) {
105:             $param = explode('=', $line);
106:             if ($param[0] == 'Auth') {
107:                 $auth = $param[1];
108:             }
109:         }
110:         if (empty($auth)) {
111:             throw new Horde_Push_Exception(
112:                 'Missing authentication token in the response!'
113:             );
114:         }
115:         
116:         /* Do the initial post. */
117:         try {
118:             $entry->save(
119:                 $this->_params['url'],
120:                 array('Authorization' => 'GoogleLogin auth=' . $auth)
121:             );
122:             $reference = $entry->link('alternate');
123:             if (!empty($reference)) {
124:                 $content->addReference($reference);
125:             }
126:         } catch (Horde_Exception $e) {
127:             throw new Horde_Push_Exception($e);
128:         }
129: 
130:         return sprintf(
131:             'Pushed blog entry to %s.', $this->_params['url']
132:         );
133:     }
134: }
135: 
API documentation generated by ApiGen