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:  * Creates the Horde_Push recipients.
  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/components/Horde_Push
 12:  */
 13: 
 14: /**
 15:  * Creates the Horde_Push recipients.
 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/components/Horde_Push
 27:  */
 28: class Horde_Push_Factory_Recipients
 29: {
 30:     /**
 31:      * Create the recipient list.
 32:      *
 33:      * @param array $options Command line options.
 34:      * @param array $conf    The configuration.
 35:      *
 36:      * @return array The list of recipients.
 37:      */
 38:     public function create($options, $conf)
 39:     {
 40:         $result = array();
 41: 
 42:         if (isset($options['recipients'])) {
 43:             $recipients = array_map('trim', explode(',', $options['recipients']));
 44:         } elseif (isset($conf['recipients'])) {
 45:             $recipients = $conf['recipients'];
 46:         } else {
 47:             return $result;
 48:         }
 49: 
 50:         foreach ($recipients as $recipient) {
 51:             if (strpos($recipient, ':') !== false) {
 52:                 list($recipient, $acl) = explode(':', $recipient);
 53:             } else {
 54:                 $acl = null;
 55:             }
 56:             if (isset($conf['recipient'][$recipient])) {
 57:                 $type = $conf['recipient'][$recipient]['type'];
 58:                 if ($acl === null && isset($conf['recipient'][$recipient]['acl'])) {
 59:                     $acl = $conf['recipient'][$recipient]['acl'];
 60:                 }
 61:                 $recipient_conf = array_merge($conf, $conf['recipient'][$recipient]);
 62:             } else {
 63:                 $type = $recipient;
 64:                 $recipient_conf = $conf;
 65:             }
 66:             switch($type) {
 67:             case 'twitter':
 68:                 $r = $this->_createTwitter($recipient_conf);
 69:                 break;
 70:             case 'facebook':
 71:                 $r = $this->_createFacebook($recipient_conf);
 72:                 break;
 73:             case 'blogger':
 74:                 $r = $this->_createBlogger($recipient_conf);
 75:                 break;
 76:             case 'mail':
 77:                 $r = $this->_createMail($recipient_conf);
 78:                 break;
 79:             case 'mock':
 80:                 $r = new Horde_Push_Recipient_Mock();
 81:                 break;
 82:             default:
 83:                 throw new Horde_Push_Exception(
 84:                     sprintf('Unknown recipient type "%s"!', $type)
 85:                 );
 86:             }
 87:             $r->setAcl($acl);
 88:             $result[] = $r;
 89:         }
 90:         return $result;
 91:     }
 92: 
 93:     /**
 94:      * Create the twitter recipient.
 95:      *
 96:      * @param array $conf The configuration.
 97:      * 
 98:     * @return Horde_Push_Recipient_Twitter The twitter recipient.
 99:      */
100:     private function _createTwitter($conf)
101:     {
102:         $params = array(
103:             'key' => $conf['twitter']['key'],
104:             'secret' => $conf['twitter']['secret'],
105:             'requestTokenUrl' => Horde_Service_Twitter::REQUEST_TOKEN_URL,
106:             'authorizeTokenUrl' => Horde_Service_Twitter::USER_AUTHORIZE_URL,
107:             'accessTokenUrl' => Horde_Service_Twitter::ACCESS_TOKEN_URL,
108:             'signatureMethod' => new Horde_Oauth_SignatureMethod_HmacSha1()
109:         );
110: 
111:         /* Create the Consumer */
112:         $auth = new Horde_Service_Twitter_Auth_Oauth(new Horde_Oauth_Consumer($params));
113:         $request = new Horde_Service_Twitter_Request_Oauth($this->_createControllerRequest($conf));
114:         $twitter = new Horde_Service_Twitter($auth, $request);
115: 
116:         $http = $this->_createHttpClient($conf);
117:         $twitter->setHttpClient($http);
118: 
119:         $auth_token = new Horde_Oauth_Token($conf['twitter']['token_key'], $conf['twitter']['token_secret']);
120:         $twitter->auth->setToken($auth_token);
121: 
122:         return new Horde_Push_Recipient_Twitter($twitter, $http);
123:     }
124: 
125:     /**
126:      * Create the facebook recipient.
127:      *
128:      * @param array $conf The configuration.
129:      * 
130:     * @return Horde_Push_Recipient_Facebook The facebook recipient.
131:      */
132:     private function _createFacebook($conf)
133:     {
134:         $facebook = new Horde_Service_Facebook(
135:             $conf['facebook']['key'],
136:             $conf['facebook']['secret'],
137:             array(
138:                 'http_client' => $this->_createHttpClient($conf)
139:             )
140:         );
141:         $facebook->auth->setSession($conf['facebook']['sid']);
142:         return new Horde_Push_Recipient_Facebook($facebook, $conf['facebook']);
143:     }
144: 
145:     /**
146:      * Create the blogger recipient.
147:      *
148:      * @param array $conf The configuration.
149:      *
150:      * @return Horde_Push_Recipient_Blogger The blogger recipient.
151:      */
152:     private function _createBlogger($conf)
153:     {
154:         return new Horde_Push_Recipient_Blogger(
155:             $this->_createHttpClient($conf),
156:             $conf['blogger']
157:         );
158:     }
159: 
160:     /**
161:      * Create the mail recipient(s).
162:      *
163:      * @param array $conf The configuration.
164:      *
165:      * @return Horde_Push_Recipient_Mail The mail recipient(s).
166:      */
167:     private function _createMail($conf)
168:     {
169:         return new Horde_Push_Recipient_Mail(
170:             $this->_createMailTransport($conf),
171:             array('from' => isset($conf['mailer']['from']) ? $conf['mailer']['from'] : null)
172:         );
173:     }
174: 
175:     /**
176:      * Create a HTTP client.
177:      *
178:      * @param array $conf The configuration.
179:      *
180:      * @return Horde_Http_Client The HTTP client.
181:      */
182:     private function _createHttpClient($conf)
183:     {
184:         $client_opts = array();
185:         if (!empty($conf['http']['proxy']['proxy_host'])) {
186:             $client_opts['request.proxyServer'] = $conf['http']['proxy']['proxy_host'];
187:             $client_opts['request.proxyPort'] = $conf['http']['proxy']['proxy_port'];
188:             if (!empty($conf['http']['proxy']['proxy_user'])) {
189:                 $client_opts['request.proxyUsername'] = $conf['http']['proxy']['proxy_user'];
190:                 if (!empty($conf['http']['proxy']['proxy_pass'])) {
191:                     $client_opts['request.proxyPassword'] = $conf['http']['proxy']['proxy_pass'];
192:                 }
193:             }
194:         }
195:         return new Horde_Http_Client($client_opts);
196:     }
197: 
198:     /**
199:      * Create a controller request.
200:      *
201:      * @param array $conf The configuration.
202:      *
203:      * @return Horde_Controller_Request The request representation.
204:      */
205:     private function _createControllerRequest($conf)
206:     {
207:         return new Horde_Controller_Request_Http();
208:     }
209: 
210:     /**
211:      * Create a mail transport.
212:      *
213:      * @param array $conf The configuration.
214:      *
215:      * @return Horde_Mail_Transport The mail transport.
216:      */
217:     private function _createMailTransport($conf)
218:     {
219:         $transport = isset($conf['mailer']['type'])
220:             ? $conf['mailer']['type']
221:             : 'null';
222:         $params = isset($conf['mailer']['params'])
223:             ? $conf['mailer']['params']
224:             : array();
225:         $class = 'Horde_Mail_Transport_' . ucfirst($transport);
226:         if (class_exists($class)) {
227:             return new $class($params);
228:         }
229:         throw new Horde_Push_Exception('Unable to find class for transport ' . $transport);
230:     }
231: }
API documentation generated by ApiGen