1: <?php
2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12:
13: class Folks_Notification_facebook extends Folks_Notification {
14:
15: 16: 17:
18: private $_fb;
19:
20: 21: 22:
23: private $_fbp;
24:
25: 26: 27:
28: public function getName()
29: {
30: return _("Facebook");
31: }
32:
33: 34: 35: 36: 37: 38: 39:
40: public function isAvailable($type)
41: {
42:
43: if (!$GLOBALS['conf']['facebook']['enabled']) {
44: return false;
45: }
46:
47:
48: $fbp = unserialize($GLOBALS['prefs']->getValue('facebook'));
49: if (!$fbp || empty($fbp['uid'])) {
50: return false;
51: }
52:
53: return true;
54: }
55:
56: 57: 58: 59: 60: 61: 62: 63: 64: 65:
66: public function notify($user, $subject, $body, $attachments = array())
67: {
68: if (!$this->_loadFB()) {
69: return $this->_fb;
70: }
71:
72: try {
73: $message = $this->_formatBody($subject, $body);
74: $result = $this->_fb->notifications->send(array($this->_fbp['uid']), $message, 'user_to_user');
75: } catch (Horde_Service_Facebook_Exception $e) {
76: return PEAR::raiseError($e->getMessage(), $e->getCode());
77: }
78:
79: return $result;
80: }
81:
82: 83: 84: 85: 86: 87: 88: 89: 90: 91:
92: public function notifyFriends($user, $subject, $body, $attachments = array())
93: {
94: if (!$this->_loadFB()) {
95: return $this->_fb;
96: }
97:
98: try {
99: $friends = $this->_fb->friends->get(null, $this->_fbp['uid']);
100: } catch (Horde_Service_Facebook_Exception $e) {
101: return PEAR::raiseError($e->getMessage(), $e->getCode());
102: }
103:
104: try {
105: $message = $this->_formatBody($subject, $body);
106: $result = $this->_fb->notifications->send($friends, $message, 'user_to_user');
107: } catch (Horde_Service_Facebook_Exception $e) {
108: return PEAR::raiseError($e->getMessage(), $e->getCode());
109: }
110:
111: return $result;
112: }
113:
114: 115: 116:
117: private function _loadFB()
118: {
119: if ($this->_fb) {
120: return true;
121: }
122:
123:
124: if (!$GLOBALS['conf']['facebook']['enabled']) {
125: $this->_fb = PEAR::raiseError(_("No Facebook integration exists."));
126: return false;
127: }
128:
129:
130: $this->_fbp = unserialize($GLOBALS['prefs']->getValue('facebook'));
131: if (!$this->_fbp || empty($this->_fbp['uid'])) {
132: $this->_fb = PEAR::raiseError(sprintf(_("Could not find authorization for %s to interact with your Facebook account."), $GLOBALS['registry']->get('name', 'horde')));
133: return false;
134: }
135:
136:
137: try {
138: $this->_fb = $GLOBALS['injector']->getInstance('Horde_Service_Facebook');
139: } catch (Horde_Exception $e) {
140: $error = PEAR::raiseError($e->getMessage(), $e->getCode());
141: Horde::logMessage($error, 'ERR');
142:
143: return $error;
144: }
145:
146:
147: $this->_fb->auth->setUser($this->_fbp['uid'], $this->_fbp['sid'], 0);
148:
149: return true;
150: }
151:
152: 153: 154: 155: 156: 157: 158: 159:
160: private function _formatBody($subject, $body)
161: {
162: return '<b>' . $subject . ':</b> '
163: . $GLOBALS['injector']->getInstance('Horde_Core_Factory_TextFilter')->filter($body, 'text2html', array('parselevel' => Horde_Text_Filter_Text2html::MICRO_LINKURL));
164: }
165: }
166: