1: <?php
2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12:
13:
14: 15: 16: 17: 18: 19: 20: 21: 22: 23: 24: 25: 26: 27:
28: class Horde_Push_Recipient_Blogger
29: extends Horde_Push_Recipient_Base
30: {
31: 32: 33: 34: 35:
36: private $_client;
37:
38: 39: 40: 41: 42:
43: private $_params;
44:
45: 46: 47: 48: 49: 50: 51:
52: public function __construct($client, $params)
53: {
54: $this->_client = $client;
55: $this->_params = $params;
56: }
57:
58: 59: 60: 61: 62: 63: 64: 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:
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:
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:
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: