if (preg_match("/$source/", mb_strtolower($status->source))) {
common_debug($this->name() . ' - Skipping import of status ' .
- $status->id . ' with source ' . $source);
+ twitter_id($status) . ' with source ' . $source);
return null;
}
return null;
}
- $statusUri = $this->makeStatusURI($status->user->screen_name, $status->id);
+ $statusId = twitter_id($status);
+ $statusUri = $this->makeStatusURI($status->user->screen_name, $statusId);
// check to see if we've already imported the status
- $n2s = Notice_to_status::staticGet('status_id', $status->id);
+ $n2s = Notice_to_status::getKV('status_id', $statusId);
if (!empty($n2s)) {
common_log(
LOG_INFO,
$this->name() .
- " - Ignoring duplicate import: {$status->id}"
+ " - Ignoring duplicate import: {$statusId}"
);
- return Notice::staticGet('id', $n2s->notice_id);
+ return Notice::getKV('id', $n2s->notice_id);
}
// If it's a retweet, save it as a repeat!
if (!empty($status->retweeted_status)) {
- common_log(LOG_INFO, "Status {$status->id} is a retweet of {$status->retweeted_status->id}.");
+ common_log(LOG_INFO, "Status {$statusId} is a retweet of " . twitter_id($status->retweeted_status) . ".");
$original = $this->saveStatus($status->retweeted_status);
if (empty($original)) {
return null;
'uri' => $statusUri,
'is_local' => Notice::GATEWAY));
common_log(LOG_INFO, "Saved {$repeat->id} as a repeat of {$original->id}");
- Notice_to_status::saveNew($repeat->id, $status->id);
+ Notice_to_status::saveNew($repeat->id, $statusId);
return $repeat;
}
}
$notice->reply_to = null;
- if (!empty($status->in_reply_to_status_id)) {
- common_log(LOG_INFO, "Status {$status->id} is a reply to status {$status->in_reply_to_status_id}");
- $n2s = Notice_to_status::staticGet('status_id', $status->in_reply_to_status_id);
+ $replyTo = twitter_id($status, 'in_reply_to_status_id');
+ if (!empty($replyTo)) {
+ common_log(LOG_INFO, "Status {$statusId} is a reply to status {$replyTo}");
+ $n2s = Notice_to_status::getKV('status_id', $replyTo);
if (empty($n2s)) {
- common_log(LOG_INFO, "Couldn't find local notice for status {$status->in_reply_to_status_id}");
+ common_log(LOG_INFO, "Couldn't find local notice for status {$replyTo}");
} else {
- $reply = Notice::staticGet('id', $n2s->notice_id);
+ $reply = Notice::getKV('id', $n2s->notice_id);
if (empty($reply)) {
- common_log(LOG_INFO, "Couldn't find local notice for status {$status->in_reply_to_status_id}");
+ common_log(LOG_INFO, "Couldn't find local notice for status {$replyTo}");
} else {
- common_log(LOG_INFO, "Found local notice {$reply->id} for status {$status->in_reply_to_status_id}");
+ common_log(LOG_INFO, "Found local notice {$reply->id} for status {$replyTo}");
$notice->reply_to = $reply->id;
$notice->conversation = $reply->conversation;
}
if (empty($notice->conversation)) {
$conv = Conversation::create();
$notice->conversation = $conv->id;
- common_log(LOG_INFO, "No known conversation for status {$status->id} so making a new one {$conv->id}.");
+ common_log(LOG_INFO, "No known conversation for status {$statusId} so making a new one {$conv->id}.");
}
$notice->is_local = Notice::GATEWAY;
- $notice->content = html_entity_decode($status->text, ENT_QUOTES, 'UTF-8');
- $notice->rendered = $this->linkify($status);
+ $notice->content = html_entity_decode($this->linkify($status, FALSE), ENT_QUOTES, 'UTF-8');
+ $notice->rendered = $this->linkify($status, TRUE);
if (Event::handle('StartNoticeSave', array(&$notice))) {
Event::handle('EndNoticeSave', array($notice));
}
- Notice_to_status::saveNew($notice->id, $status->id);
+ Notice_to_status::saveNew($notice->id, $statusId);
$this->saveStatusMentions($notice, $status);
$this->saveStatusAttachments($notice, $status);
*/
function makeStatusURI($username, $id)
{
- return 'http://twitter.com/'
+ return 'http://twitter.com/#!/'
. $username
. '/status/'
. $id;
/**
- * Look up a Profile by profileurl field. Profile::staticGet() was
+ * Look up a Profile by profileurl field. Profile::getKV() was
* not working consistently.
*
* @param string $nickname local nickname of the Twitter user
// check for remote profile
- $remote_pro = Remote_profile::staticGet('uri', $profileurl);
+ $remote_pro = Remote_profile::getKV('uri', $profileurl);
if (empty($remote_pro)) {
$remote_pro = new Remote_profile();
{
global $config;
- $path_parts = pathinfo($twitter_user->profile_image_url);
-
- $newname = 'Twitter_' . $twitter_user->id . '_' .
- $path_parts['basename'];
+ $newname = 'Twitter_' . $twitter_user->id . '_' . basename($twitter_user->profile_image_url);
$oldname = $profile->getAvatar(48)->filename;
$path_parts = pathinfo($twitter_user->profile_image_url);
- $img_root = substr($path_parts['basename'], 0, -11);
- $ext = $path_parts['extension'];
- $mediatype = $this->getMediatype($ext);
+ $ext = (isset($path_parts['extension']) ? '.'.$path_parts['extension'] : ''); // some lack extension
+ $img_root = basename($path_parts['basename'], '_normal'.$ext); // cut off extension
+ $mediatype = $this->getMediatype(substr($ext, 1));
foreach (array('mini', 'normal', 'bigger') as $size) {
$url = $path_parts['dirname'] . '/' .
- $img_root . '_' . $size . ".$ext";
+ $img_root . '_' . $size . $ext;
$filename = 'Twitter_' . $twitter_user->id . '_' .
- $img_root . "_$size.$ext";
+ $img_root . '_' . $size . $ext;
$this->updateAvatar($profile->id, $size, $mediatype, $filename);
$this->fetchAvatar($url, $filename);
$mediatype = null;
switch (strtolower($ext)) {
+ case 'jpeg':
case 'jpg':
- $mediatype = 'image/jpg';
+ $mediatype = 'image/jpeg';
break;
case 'gif':
$mediatype = 'image/gif';
global $config;
$path_parts = pathinfo($user->profile_image_url);
- $ext = $path_parts['extension'];
- $end = strlen('_normal' . $ext);
- $img_root = substr($path_parts['basename'], 0, -($end+1));
- $mediatype = $this->getMediatype($ext);
+ $ext = (isset($path_parts['extension']) ? '.'.$path_parts['extension'] : '');
+ $img_root = basename($path_parts['basename'], '_normal'.$ext);
+ $mediatype = $this->getMediatype(substr($ext, 1));
foreach (array('mini', 'normal', 'bigger') as $size) {
$url = $path_parts['dirname'] . '/' .
- $img_root . '_' . $size . ".$ext";
+ $img_root . '_' . $size . $ext;
$filename = 'Twitter_' . $user->id . '_' .
- $img_root . "_$size.$ext";
+ $img_root . '_' . $size . $ext;
if ($this->fetchAvatar($url, $filename)) {
$this->newAvatar($id, $size, $mediatype, $filename);
common_debug($this->name() . " - Updating avatar: $size");
- $profile = Profile::staticGet($profile_id);
+ $profile = Profile::getKV($profile_id);
if (empty($profile)) {
common_debug($this->name() . " - Couldn't get profile: $profile_id!");
const HASHTAG = 2;
const MENTION = 3;
- function linkify($status)
+ function linkify($status, $html = FALSE)
{
$text = $status->text;
if (empty($status->entities)) {
- common_log(LOG_WARNING, "No entities data for {$status->id}; trying to fake up links ourselves.");
+ $statusId = twitter_id($status);
+ common_log(LOG_WARNING, "No entities data for {$statusId}; trying to fake up links ourselves.");
$text = common_replace_urls_callback($text, 'common_linkify');
$text = preg_replace('/(^|\"\;|\'|\(|\[|\{|\s+)#([\pL\pN_\-\.]{1,64})/e', "'\\1#'.TwitterStatusFetcher::tagLink('\\2')", $text);
$text = preg_replace('/(^|\s+)@([a-z0-9A-Z_]{1,64})/e', "'\\1@'.TwitterStatusFetcher::atLink('\\2')", $text);
}
// Move all the entities into order so we can
- // replace them in reverse order and thus
- // not mess up their indices
+ // replace them and escape surrounding plaintext
+ // in order
$toReplace = array();
}
}
- // sort in reverse order by key
+ // sort in forward order by key
+
+ ksort($toReplace);
- krsort($toReplace);
+ $result = '';
+ $cursor = 0;
foreach ($toReplace as $part) {
list($type, $object) = $part;
+ $start = $object->indices[0];
+ $end = $object->indices[1];
+ if ($cursor < $start) {
+ // Copy in the preceding plaintext
+ $result .= $this->twitEscape(mb_substr($text, $cursor, $start - $cursor));
+ $cursor = $start;
+ }
+ $orig = $this->twitEscape(mb_substr($text, $start, $end - $start));
switch($type) {
case self::URL:
- $linkText = $this->makeUrlLink($object);
+ $linkText = $this->makeUrlLink($object, $orig, $html);
break;
case self::HASHTAG:
- $linkText = $this->makeHashtagLink($object);
+ if ($html) {
+ $linkText = $this->makeHashtagLink($object, $orig);
+ }else{
+ $linkText = $orig;
+ }
break;
case self::MENTION:
- $linkText = $this->makeMentionLink($object);
+ if ($html) {
+ $linkText = $this->makeMentionLink($object, $orig);
+ }else{
+ $linkText = $orig;
+ }
break;
default:
+ $linkText = $orig;
continue;
}
- $text = mb_substr($text, 0, $object->indices[0]) . $linkText . mb_substr($text, $object->indices[1]);
+ $result .= $linkText;
+ $cursor = $end;
}
- return $text;
+ $last = $this->twitEscape(mb_substr($text, $cursor));
+ $result .= $last;
+
+ return $result;
+ }
+
+ function twitEscape($str)
+ {
+ // Twitter seems to preemptive turn < and > into < and >
+ // but doesn't for &, so while you may have some magic protection
+ // against XSS by not bothing to escape manually, you still get
+ // invalid XHTML. Thanks!
+ //
+ // Looks like their web interface pretty much sends anything
+ // through intact, so.... to do equivalent, decode all entities
+ // and then re-encode the special ones.
+ return htmlspecialchars(html_entity_decode($str, ENT_COMPAT, 'UTF-8'));
}
- function makeUrlLink($object)
+ function makeUrlLink($object, $orig, $html)
{
- return "<a href='{$object->url}' class='extlink'>{$object->url}</a>";
+ if ($html) {
+ return '<a href="'.htmlspecialchars($object->expanded_url).'" class="extlink">'.htmlspecialchars($object->display_url).'</a>';
+ }else{
+ return htmlspecialchars($object->expanded_url);
+ }
}
- function makeHashtagLink($object)
+ function makeHashtagLink($object, $orig)
{
- return "#" . self::tagLink($object->text);
+ return "#" . self::tagLink($object->text, substr($orig, 1));
}
- function makeMentionLink($object)
+ function makeMentionLink($object, $orig)
{
- return "@".self::atLink($object->screen_name, $object->name);
+ return "@".self::atLink($object->screen_name, $object->name, substr($orig, 1));
}
- static function tagLink($tag)
+ static function tagLink($tag, $orig)
{
- return "<a href='https://twitter.com/search?q=%23{$tag}' class='hashtag'>{$tag}</a>";
+ return "<a href='https://search.twitter.com/search?q=%23{$tag}' class='hashtag'>{$orig}</a>";
}
- static function atLink($screenName, $fullName=null)
+ static function atLink($screenName, $fullName, $orig)
{
if (!empty($fullName)) {
- return "<a href='http://twitter.com/{$screenName}' title='{$fullName}'>{$screenName}</a>";
+ return "<a href='http://twitter.com/#!/{$screenName}' title='{$fullName}'>{$orig}</a>";
} else {
- return "<a href='http://twitter.com/{$screenName}'>{$screenName}</a>";
+ return "<a href='http://twitter.com/#!/{$screenName}'>{$orig}</a>";
}
}
foreach ($status->entities->user_mentions as $mention) {
$flink = Foreign_link::getByForeignID($mention->id, TWITTER_SERVICE);
if (!empty($flink)) {
- $user = User::staticGet('id', $flink->user_id);
+ $user = User::getKV('id', $flink->user_id);
if (!empty($user)) {
$reply = new Reply();
$reply->notice_id = $notice->id;
$reply->profile_id = $user->id;
+ $reply->modified = $notice->created;
common_log(LOG_INFO, __METHOD__ . ": saving reply: notice {$notice->id} to profile {$user->id}");
$id = $reply->insert();
}
}
}
}
-}
\ No newline at end of file
+}