/** * @dataProvider provider * */ public function testProduction($content, $expected) { $rendered = common_render_text($content); // hack! $rendered = preg_replace('/id="attachment-\\d+"/', 'id="attachment-XXX"', $rendered); $this->assertEquals($expected, $rendered); }
static function saveNew($from, $to, $content, $source) { $sender = Profile::staticGet('id', $from); if (!$sender->hasRight(Right::NEWMESSAGE)) { // TRANS: Client exception thrown when a user tries to send a direct message while being banned from sending them. throw new ClientException(_('You are banned from sending direct messages.')); } $msg = new Message(); $msg->from_profile = $from; $msg->to_profile = $to; $msg->content = common_shorten_links($content); $msg->rendered = common_render_text($content); $msg->created = common_sql_now(); $msg->source = $source; $result = $msg->insert(); if (!$result) { common_log_db_error($msg, 'INSERT', __FILE__); // TRANS: Message given when a message could not be stored on the server. return _('Could not insert message.'); } $orig = clone $msg; $msg->uri = common_local_url('showmessage', array('message' => $msg->id)); $result = $msg->update($orig); if (!$result) { common_log_db_error($msg, 'UPDATE', __FILE__); // TRANS: Message given when a message could not be updated on the server. return _('Could not update message with new URI.'); } return $msg; }
static function saveNew($from, $to, $content, $source) { $msg = new Message(); $msg->from_profile = $from; $msg->to_profile = $to; $msg->content = common_shorten_links($content); $msg->rendered = common_render_text($content); $msg->created = common_sql_now(); $msg->source = $source; $result = $msg->insert(); if (!$result) { common_log_db_error($msg, 'INSERT', __FILE__); return _('Could not insert message.'); } $orig = clone $msg; $msg->uri = common_local_url('showmessage', array('message' => $msg->id)); $result = $msg->update($orig); if (!$result) { common_log_db_error($msg, 'UPDATE', __FILE__); return _('Could not update message with new URI.'); } return $msg; }
function common_render_content($text, $notice) { $r = common_render_text($text); $id = $notice->profile_id; $r = common_linkify_mentions($r, $notice); $r = preg_replace('/(^|[\\s\\.\\,\\:\\;]+)!([A-Za-z0-9]{1,64})/eu', "'\\1!'.common_group_link({$id}, '\\2')", $r); return $r; }
/** * Partial notice markup rendering step: build links to !group references. * * @param string $text partially rendered HTML * @param Notice $notice in whose context we're working * @return string partially rendered HTML */ function common_render_content($text, $notice) { $r = common_render_text($text); $id = $notice->profile_id; $r = common_linkify_mentions($r, $notice); $r = preg_replace('/(^|[\\s\\.\\,\\:\\;]+)!(' . Nickname::DISPLAY_FMT . ')/e', "'\\1!'.common_group_link({$id}, '\\2')", $r); return $r; }
/** * Partial notice markup rendering step: build links to !group references. * * @param string $text partially rendered HTML * @param Profile $author the Profile that is composing the current notice * @param Notice $parent the Notice this is sent in reply to, if any * @return string partially rendered HTML */ function common_render_content($text, Profile $author, Notice $parent = null) { $text = common_render_text($text); $text = common_linkify_mentions($text, $author, $parent); return $text; }
/** * Process an incoming post activity from this remote feed. * @param Activity $activity * @param string $method 'push' or 'salmon' * @return mixed saved Notice or false * @todo FIXME: Break up this function, it's getting nasty long */ public function processPost($activity, $method) { $notice = null; $oprofile = $this->checkAuthorship($activity); if (empty($oprofile)) { return null; } // It's not always an ActivityObject::NOTE, but... let's just say it is. $note = $activity->objects[0]; // The id URI will be used as a unique identifier for for the notice, // protecting against duplicate saves. It isn't required to be a URL; // tag: URIs for instance are found in Google Buzz feeds. $sourceUri = $note->id; $dupe = Notice::staticGet('uri', $sourceUri); if ($dupe) { common_log(LOG_INFO, "OStatus: ignoring duplicate post: {$sourceUri}"); return $dupe; } // We'll also want to save a web link to the original notice, if provided. $sourceUrl = null; if ($note->link) { $sourceUrl = $note->link; } else { if ($activity->link) { $sourceUrl = $activity->link; } else { if (preg_match('!^https?://!', $note->id)) { $sourceUrl = $note->id; } } } // Use summary as fallback for content if (!empty($note->content)) { $sourceContent = $note->content; } else { if (!empty($note->summary)) { $sourceContent = $note->summary; } else { if (!empty($note->title)) { $sourceContent = $note->title; } else { // @todo FIXME: Fetch from $sourceUrl? // TRANS: Client exception. %s is a source URI. throw new ClientException(sprintf(_m('No content for notice %s.'), $sourceUri)); } } } // Get (safe!) HTML and text versions of the content $rendered = $this->purify($sourceContent); $content = html_entity_decode(strip_tags($rendered), ENT_QUOTES, 'UTF-8'); $shortened = common_shorten_links($content); // If it's too long, try using the summary, and make the // HTML an attachment. $attachment = null; if (Notice::contentTooLong($shortened)) { $attachment = $this->saveHTMLFile($note->title, $rendered); $summary = html_entity_decode(strip_tags($note->summary), ENT_QUOTES, 'UTF-8'); if (empty($summary)) { $summary = $content; } $shortSummary = common_shorten_links($summary); if (Notice::contentTooLong($shortSummary)) { $url = common_shorten_url($sourceUrl); $shortSummary = substr($shortSummary, 0, Notice::maxContent() - (mb_strlen($url) + 2)); $content = $shortSummary . ' ' . $url; // We mark up the attachment link specially for the HTML output // so we can fold-out the full version inline. // @todo FIXME i18n: This tooltip will be saved with the site's default language // TRANS: Shown when a notice is longer than supported and/or when attachments are present. At runtime // TRANS: this will usually be replaced with localised text from StatusNet core messages. $showMoreText = _m('Show more'); $attachUrl = common_local_url('attachment', array('attachment' => $attachment->id)); $rendered = common_render_text($shortSummary) . '<a href="' . htmlspecialchars($attachUrl) . '"' . ' class="attachment more"' . ' title="' . htmlspecialchars($showMoreText) . '">' . '…' . '</a>'; } } $options = array('is_local' => Notice::REMOTE, 'url' => $sourceUrl, 'uri' => $sourceUri, 'rendered' => $rendered, 'replies' => array(), 'groups' => array(), 'peopletags' => array(), 'tags' => array(), 'urls' => array()); // Check for optional attributes... if (!empty($activity->time)) { $options['created'] = common_sql_date($activity->time); } if ($activity->context) { // Any individual or group attn: targets? $replies = $activity->context->attention; $options['groups'] = $this->filterReplies($oprofile, $replies); $options['replies'] = $replies; // Maintain direct reply associations // @todo FIXME: What about conversation ID? if (!empty($activity->context->replyToID)) { $orig = Notice::staticGet('uri', $activity->context->replyToID); if (!empty($orig)) { $options['reply_to'] = $orig->id; } } $location = $activity->context->location; if ($location) { $options['lat'] = $location->lat; $options['lon'] = $location->lon; if ($location->location_id) { $options['location_ns'] = $location->location_ns; $options['location_id'] = $location->location_id; } } } if ($this->isPeopletag()) { $options['peopletags'][] = $this->localPeopletag(); } // Atom categories <-> hashtags foreach ($activity->categories as $cat) { if ($cat->term) { $term = common_canonical_tag($cat->term); if ($term) { $options['tags'][] = $term; } } } // Atom enclosures -> attachment URLs foreach ($activity->enclosures as $href) { // @todo FIXME: Save these locally or....? $options['urls'][] = $href; } try { $saved = Notice::saveNew($oprofile->profile_id, $content, 'ostatus', $options); if ($saved) { Ostatus_source::saveNew($saved, $this, $method); if (!empty($attachment)) { File_to_post::processNew($attachment->id, $saved->id); } } } catch (Exception $e) { common_log(LOG_ERR, "OStatus save of remote message {$sourceUri} failed: " . $e->getMessage()); throw $e; } common_log(LOG_INFO, "OStatus saved remote message {$sourceUri} as notice id {$saved->id}"); return $saved; }
/** * @dataProvider provider * */ public function testProduction($content, $expected) { $rendered = common_render_text($content); $this->assertEquals($expected, $rendered); }
/** * Process an incoming post activity from this remote feed. * @param Activity $activity * @param string $method 'push' or 'salmon' * @return mixed saved Notice or false * @fixme break up this function, it's getting nasty long */ public function processPost($activity, $method) { if ($this->isGroup()) { // A group feed will contain posts from multiple authors. // @fixme validate these profiles in some way! $oprofile = self::ensureActorProfile($activity); if ($oprofile->isGroup()) { // Groups can't post notices in StatusNet. common_log(LOG_WARNING, "OStatus: skipping post with group listed as author: {$oprofile->uri} in feed from {$this->uri}"); return false; } } else { $actor = $activity->actor; if (empty($actor)) { // OK here! assume the default } else { if ($actor->id == $this->uri || $actor->link == $this->uri) { $this->updateFromActivityObject($actor); } else { throw new Exception("Got an actor '{$actor->title}' ({$actor->id}) on single-user feed for {$this->uri}"); } } $oprofile = $this; } // It's not always an ActivityObject::NOTE, but... let's just say it is. $note = $activity->objects[0]; // The id URI will be used as a unique identifier for for the notice, // protecting against duplicate saves. It isn't required to be a URL; // tag: URIs for instance are found in Google Buzz feeds. $sourceUri = $note->id; $dupe = Notice::staticGet('uri', $sourceUri); if ($dupe) { common_log(LOG_INFO, "OStatus: ignoring duplicate post: {$sourceUri}"); return false; } // We'll also want to save a web link to the original notice, if provided. $sourceUrl = null; if ($note->link) { $sourceUrl = $note->link; } else { if ($activity->link) { $sourceUrl = $activity->link; } else { if (preg_match('!^https?://!', $note->id)) { $sourceUrl = $note->id; } } } // Use summary as fallback for content if (!empty($note->content)) { $sourceContent = $note->content; } else { if (!empty($note->summary)) { $sourceContent = $note->summary; } else { if (!empty($note->title)) { $sourceContent = $note->title; } else { // @fixme fetch from $sourceUrl? throw new ClientException("No content for notice {$sourceUri}"); } } } // Get (safe!) HTML and text versions of the content $rendered = $this->purify($sourceContent); $content = html_entity_decode(strip_tags($rendered)); $shortened = common_shorten_links($content); // If it's too long, try using the summary, and make the // HTML an attachment. $attachment = null; if (Notice::contentTooLong($shortened)) { $attachment = $this->saveHTMLFile($note->title, $rendered); $summary = html_entity_decode(strip_tags($note->summary)); if (empty($summary)) { $summary = $content; } $shortSummary = common_shorten_links($summary); if (Notice::contentTooLong($shortSummary)) { $url = common_shorten_url($sourceUrl); $shortSummary = substr($shortSummary, 0, Notice::maxContent() - (mb_strlen($url) + 2)); $content = $shortSummary . ' ' . $url; // We mark up the attachment link specially for the HTML output // so we can fold-out the full version inline. $attachUrl = common_local_url('attachment', array('attachment' => $attachment->id)); $rendered = common_render_text($shortSummary) . '<a href="' . htmlspecialchars($attachUrl) . '"' . ' class="attachment more"' . ' title="' . htmlspecialchars(_m('Show more')) . '">' . '…' . '</a>'; } } $options = array('is_local' => Notice::REMOTE_OMB, 'url' => $sourceUrl, 'uri' => $sourceUri, 'rendered' => $rendered, 'replies' => array(), 'groups' => array(), 'tags' => array(), 'urls' => array()); // Check for optional attributes... if (!empty($activity->time)) { $options['created'] = common_sql_date($activity->time); } if ($activity->context) { // Any individual or group attn: targets? $replies = $activity->context->attention; $options['groups'] = $this->filterReplies($oprofile, $replies); $options['replies'] = $replies; // Maintain direct reply associations // @fixme what about conversation ID? if (!empty($activity->context->replyToID)) { $orig = Notice::staticGet('uri', $activity->context->replyToID); if (!empty($orig)) { $options['reply_to'] = $orig->id; } } $location = $activity->context->location; if ($location) { $options['lat'] = $location->lat; $options['lon'] = $location->lon; if ($location->location_id) { $options['location_ns'] = $location->location_ns; $options['location_id'] = $location->location_id; } } } // Atom categories <-> hashtags foreach ($activity->categories as $cat) { if ($cat->term) { $term = common_canonical_tag($cat->term); if ($term) { $options['tags'][] = $term; } } } // Atom enclosures -> attachment URLs foreach ($activity->enclosures as $href) { // @fixme save these locally or....? $options['urls'][] = $href; } try { $saved = Notice::saveNew($oprofile->profile_id, $content, 'ostatus', $options); if ($saved) { Ostatus_source::saveNew($saved, $this, $method); if (!empty($attachment)) { File_to_post::processNew($attachment->id, $saved->id); } } } catch (Exception $e) { common_log(LOG_ERR, "OStatus save of remote message {$sourceUri} failed: " . $e->getMessage()); throw $e; } common_log(LOG_INFO, "OStatus saved remote message {$sourceUri} as notice id {$saved->id}"); return $saved; }
static function toHTML($profile, $question, $answer) { $notice = $question->getNotice(); $out = new XMLStringer(); $cls = array('qna_answer'); if (!empty($answer->best)) { $cls[] = 'best'; } $out->elementStart('p', array('class' => implode(' ', $cls))); $out->elementStart('span', 'answer-content'); $out->raw(common_render_text($answer->content)); $out->elementEnd('span'); if (!empty($answer->revisions)) { $out->elementstart('span', 'answer-revisions'); $out->text(htmlspecialchars(sprintf(_m('%s revision', '%s revisions', $answer->revisions), $answer->revisions))); $out->elementEnd('span'); } $out->elementEnd('p'); return $out->getString(); }
/** * Partial notice markup rendering step: build links to !group references. * * @param string $text partially rendered HTML * @param Notice $notice in whose context we're working * @return string partially rendered HTML */ function common_render_content($text, Notice $notice) { $text = common_render_text($text); $text = common_linkify_mentions($text, $notice); return $text; }
static function send($user, $group, $text) { if (!$user->hasRight(Right::NEWMESSAGE)) { // XXX: maybe break this out into a separate right // TRANS: Exception thrown when trying to send group private message without having the right to do that. // TRANS: %s is a user nickname. throw new Exception(sprintf(_m('User %s is not allowed to send private messages.'), $user->nickname)); } Group_privacy_settings::ensurePost($user, $group); $text = $user->shortenLinks($text); // We use the same limits as for 'regular' private messages. if (Message::contentTooLong($text)) { // TRANS: Exception thrown when trying to send group private message that is too long. // TRANS: %d is the maximum meggage length. throw new Exception(sprintf(_m('That\'s too long. Maximum message size is %d character.', 'That\'s too long. Maximum message size is %d characters.', Message::maxContent()), Message::maxContent())); } // Valid! Let's do this thing! $gm = new Group_message(); $gm->id = UUID::gen(); $gm->uri = common_local_url('showgroupmessage', array('id' => $gm->id)); $gm->from_profile = $user->id; $gm->to_group = $group->id; $gm->content = $text; // XXX: is this cool?! $gm->rendered = common_render_text($text); $gm->url = $gm->uri; $gm->created = common_sql_now(); // This throws a conniption if there's a problem $gm->insert(); $gm->distribute(); return $gm; }
function common_render_content($text, $notice) { $r = common_render_text($text); $id = $notice->profile_id; $r = preg_replace('/(^|\\s+)@([A-Za-z0-9]{1,64})/e', "'\\1@'.common_at_link({$id}, '\\2')", $r); $r = preg_replace('/^T ([A-Z0-9]{1,64}) /e', "'T '.common_at_link({$id}, '\\1').' '", $r); $r = preg_replace('/(^|\\s+)@#([A-Za-z0-9]{1,64})/e', "'\\1@#'.common_at_hash_link({$id}, '\\2')", $r); $r = preg_replace('/(^|\\s)!([A-Za-z0-9]{1,64})/e', "'\\1!'.common_group_link({$id}, '\\2')", $r); return $r; }
static function toHTML($profile, $question) { $notice = $question->getNotice(); $out = new XMLStringer(); $cls = array('qna_question'); if (!empty($question->closed)) { $cls[] = 'closed'; } $out->elementStart('p', array('class' => implode(' ', $cls))); if (!empty($question->description)) { $out->elementStart('span', 'question-description'); $out->raw(common_render_text($question->description)); $out->elementEnd('span'); } $cnt = $question->countAnswers(); if (!empty($cnt)) { $out->elementStart('span', 'answer-count'); // TRANS: Number of given answers to a question. // TRANS: %s is the number of given answers. $out->text(sprintf(_m('%s answer', '%s answers', $cnt), $cnt)); $out->elementEnd('span'); } if (!empty($question->closed)) { $out->elementStart('span', 'question-closed'); // TRANS: Notification that a question cannot be answered anymore because it is closed. $out->text(_m('This question is closed.')); $out->elementEnd('span'); } $out->elementEnd('p'); return $out->getString(); }