1 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 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290
|
<?php
/**
* Copyright 2009-2014 Horde LLC (http://www.horde.org/)
*
* See the enclosed file COPYING for license information (LGPL-2). If you
* did not receive this file, see http://www.horde.org/licenses/lgpl.
*
* @author Michael J Rubinsky <mrubinsk.horde.org>
* @category Horde
* @license http://www.horde.org/licenses/lgpl LGPL-2
* @package Horde
*/
/**
* Defines the AJAX actions used in the Twitter client.
*
* @author Michael J Rubinsky <mrubinsk.horde.org>
* @category Horde
* @license http://www.horde.org/licenses/lgpl LGPL-2
* @package Horde
*/
class Horde_Ajax_Application_TwitterHandler extends Horde_Core_Ajax_Application_Handler
{
/**
* Update the twitter timeline.
*
* @return array An hash containing the following keys:
* - o: The id of the oldest tweet
* - n: The id of the newest tweet
* - c: The HTML content
*/
public function twitterUpdate()
{
global $conf;
if (empty($conf['twitter']['enabled'])) {
return _("Twitter not enabled.");
}
switch ($this->vars->actionID) {
case 'getPage':
return $this->_doTwitterGetPage();
}
}
/**
* Retweet a tweet. Expects the following in $this->vars:
* - tweetId: The tweet id to retweet.
* - i:
*
* @return string The HTML to render the newly retweeted tweet.
*/
public function retweet()
{
$twitter = $this->_getTwitterObject();
try {
$tweet = json_decode($twitter->statuses->retweet($this->vars->tweetId));
$html = $this->_buildTweet($tweet)->render('twitter_tweet');
return $html;
} catch (Horde_Service_Twitter_Exception $e) {
$this->_twitterError($e);
}
}
/**
* Favorite a tweet. Expects:
* - tweetId:
*
* @return stdClass
*/
public function favorite()
{
$twitter = $this->_getTwitterObject();
try {
return json_decode($twitter->favorites->create($this->vars->tweetId));
} catch (Horde_Service_Twitter_Exception $e) {
$this->_twitterError($e);
}
}
/**
* Unfavorite a tweet. Expects:
* - tweetId:
*/
public function unfavorite()
{
$twitter = $this->_getTwitterObject();
try {
return json_decode($twitter->favorites->destroy($this->vars->tweetId));
} catch (Horde_Service_Twitter_Exception $e) {
$this->_twitterError($e);
}
}
/**
* Update twitter status. Expects:
* - inReplyTo:
* - statusText:
*
* @return string The HTML text of the new tweet.
*/
public function updateStatus()
{
$twitter = $this->_getTwitterObject();
if ($inreplyTo = $this->vars->inReplyTo) {
$params = array('in_reply_to_status_id', $inreplyTo);
} else {
$params = array();
}
try {
$tweet = json_decode($twitter->statuses->update($this->vars->statusText, $params));
return $this->_buildTweet($tweet)->render('twitter_tweet');
} catch (Horde_Service_Twitter_Exception $e) {
$this->_twitterError($e);
}
}
/**
*
* @return Horde_Service_Twitter
*/
protected function _getTwitterObject()
{
$twitter = $GLOBALS['injector']->getInstance('Horde_Service_Twitter');
$token = unserialize($GLOBALS['prefs']->getValue('twitter'));
if (!empty($token['key']) && !empty($token['secret'])) {
$auth_token = new Horde_Oauth_Token($token['key'], $token['secret']);
$twitter->auth->setToken($auth_token);
}
return $twitter;
}
/**
* Helper method to build a view object for a tweet.
*
* @param stdClass $tweet The tweet object.
*
* @return Horde_View The view object, populated with tweet data.
*/
protected function _buildTweet($tweet)
{
global $injector, $registry;
$view = new Horde_View(array('templatePath' => HORDE_TEMPLATES . '/block'));
$view->addHelper('Tag');
$view->ajax_uri = $registry->getServiceLink('ajax', $registry->getApp());
$filter = $injector->getInstance('Horde_Core_Factory_TextFilter');
$instance = $this->vars->i;
// Links and media
$map = $previews = array();
foreach ($tweet->entities->urls as $link) {
$replace = '<a target="_blank" href="' . $link->url . '" title="' . $link->expanded_url . '">' . htmlspecialchars($link->display_url) . '</a>';
$map[$link->indices[0]] = array($link->indices[1], $replace);
}
if (!empty($tweet->entities->media)) {
foreach ($tweet->entities->media as $picture) {
$replace = '<a target="_blank" href="' . $picture->url . '" title="' . $picture->expanded_url . '">' . htmlentities($picture->display_url, ENT_COMPAT, 'UTF-8') . '</a>';
$map[$picture->indices[0]] = array($picture->indices[1], $replace);
$previews[] = ' <a href="#" onclick="return Horde[\'twitter' . $instance . '\'].showPreview(\'' . $picture->media_url . ':small\');"><img src="' . Horde_Themes::img('mime/image.png') . '" /></a>';
}
}
if (!empty($tweet->entities->user_mentions)) {
foreach ($tweet->entities->user_mentions as $user) {
$replace = ' <a target="_blank" title="' . $user->name . '" href="http://twitter.com/' . $user->screen_name . '">@' . htmlentities($user->screen_name, ENT_COMPAT, 'UTF-8') . '</a>';
$map[$user->indices[0]] = array($user->indices[1], $replace);
}
}
if (!empty($tweet->entities->hashtags)) {
foreach ($tweet->entities->hashtags as $hashtag) {
$replace = ' <a target="_blank" href="http://twitter.com/search?q=#' . urlencode($hashtag->text) . '">#' . htmlentities($hashtag->text, ENT_COMPAT, 'UTF-8') . '</a>';
$map[$hashtag->indices[0]] = array($hashtag->indices[1], $replace);
}
}
$body = '';
$pos = 0;
while ($pos <= Horde_String::length($tweet->text) - 1) {
if (!empty($map[$pos])) {
$entity = $map[$pos];
$body .= $entity[1];
$pos = $entity[0];
} else {
$body .= Horde_String::substr($tweet->text, $pos, 1);
++$pos;
}
}
foreach ($previews as $preview) {
$body .= $preview;
}
$view->body = $body;
/* If this is a retweet, use the original author's profile info */
if (!empty($tweet->retweeted_status)) {
$tweetObj = $tweet->retweeted_status;
} else {
$tweetObj = $tweet;
}
/* These are all referencing the *original* tweet */
$view->profileLink = Horde::externalUrl('http://twitter.com/' . htmlspecialchars($tweetObj->user->screen_name), true);
$view->profileImg = $GLOBALS['browser']->usingSSLConnection() ? $tweetObj->user->profile_image_url_https : $tweetObj->user->profile_image_url;
$view->authorName = '@' . htmlspecialchars($tweetObj->user->screen_name);
$view->authorFullname = htmlspecialchars($tweetObj->user->name);
$view->createdAt = $tweetObj->created_at;
$view->clientText = $filter->filter($tweet->source, 'xss');
$view->tweet = $tweet;
$view->instanceid = $instance;
return $view;
}
/**
* Helper method for getting a slice of tweets.
*
* Expects the following in $this->vars:
* - max_id:
* - since_id:
* - i:
* - mentions:
*
* @return [type] [description]
*/
protected function _doTwitterGetPage()
{
$twitter = $this->_getTwitterObject();
try {
$params = array('include_entities' => 1);
if ($max = $this->vars->max_id) {
$params['max_id'] = $max;
} elseif ($since = $this->vars->since_id) {
$params['since_id'] = $since;
}
if ($this->vars->mentions) {
$stream = Horde_Serialize::unserialize($twitter->statuses->mentions($params), Horde_Serialize::JSON);
} else {
$stream = Horde_Serialize::unserialize($twitter->statuses->homeTimeline($params), Horde_Serialize::JSON);
}
} catch (Horde_Service_Twitter_Exception $e) {
$this->_twitterError($e);
return;
}
if (count($stream)) {
$newest = $stream[0]->id_str;
} else {
$newest = $params['since_id'];
$oldest = 0;
}
$view = new Horde_View(array('templatePath' => HORDE_TEMPLATES . '/block'));
$view->addHelper('Tag');
$html = '';
foreach ($stream as $tweet) {
/* Don't return the max_id tweet, since we already have it */
if (!empty($params['max_id']) && $params['max_id'] == $tweet->id_str) {
continue;
}
$view = $this->_buildTweet($tweet);
$oldest = $tweet->id_str;
$html .= $view->render('twitter_tweet');
}
$result = array(
'o' => $oldest,
'n' => $newest,
'c' => $html
);
return $result;
}
protected function _twitterError($e)
{
global $notification;
Horde::log($e, 'INFO');
$body = ($e instanceof Exception) ? $e->getMessage() : $e;
if (($errors = json_decode($body, true)) && isset($errors['errors'])) {
$errors = $errors['errors'];
} else {
$errors = array(array('message' => $body));
}
$notification->push(_("Error connecting to Twitter. Details have been logged for the administrator."), 'horde.error', array('sticky'));
foreach ($errors as $error) {
$notification->push($error['message'], 'horde.error', array('sticky'));
}
}
}
|