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_Factory_Recipients
29: {
30: 31: 32: 33: 34: 35: 36: 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: 95: 96: 97: 98: 99:
100: private function ($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:
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: 127: 128: 129: 130: 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: 147: 148: 149: 150: 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: 162: 163: 164: 165: 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: 177: 178: 179: 180: 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: 200: 201: 202: 203: 204:
205: private function _createControllerRequest($conf)
206: {
207: return new Horde_Controller_Request_Http();
208: }
209:
210: 211: 212: 213: 214: 215: 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: }