Example #1
0
 function onStartNoticeSave($notice)
 {
     // Only run this on local notices
     if ($notice->isLocal()) {
         $text = $notice->content;
         // From /lib/util.php::common_render_text
         // We don't want to call it directly since we don't want to
         // run common_linkify() on the text
         $text = common_remove_unicode_formatting($text);
         $text = preg_replace('/[\\x{0}-\\x{8}\\x{b}-\\x{c}\\x{e}-\\x{19}]/', '', $text);
         $text = common_replace_urls_callback($text, 'common_linkify');
         // Link #hashtags
         $rendered = preg_replace_callback('/(^|\\&quot\\;|\'|\\(|\\[|\\{|\\s+)#([\\pL\\pN_\\-\\.]{1,64})/u', function ($m) {
             return "{$m[1]}#" . common_tag_link($m[2]);
         }, $text);
         // Link @mentions, !mentions, @#mentions
         $rendered = common_linkify_mentions($rendered, $notice->getProfile(), $notice->hasParent() ? $notice->getParent() : null);
         // Prevent leading #hashtags from becoming headers by adding a backslash
         // before the "#", telling markdown to leave it alone
         // $repl_rendered = preg_replace('/^#[\pL\pN_\-\.]{1,64}/u', 'replaced!', $rendered);
         $repl_rendered = preg_replace('/^#<span class="tag">/u', '\\\\\\0', $rendered);
         // Only use the replaced value from above if it returned a success
         if ($rendered !== null) {
             $rendered = $repl_rendered;
         }
         // handle Markdown link forms in order not to convert doubly.
         $rendered = preg_replace('/\\[([^]]+)\\]\\((<a [^>]+>)([^<]+)<\\/a>\\)/u', '\\2\\1</a>', $rendered);
         // Convert Markdown to HTML
         $notice->rendered = \Michelf\Markdown::defaultTransform($rendered);
     }
     return true;
 }
 function onHandleQueuedNotice($notice)
 {
     if (intval($notice->is_local) === Notice::LOCAL_PUBLIC) {
         // Try to avoid actually mucking with the
         // notice content
         $c = $notice->content;
         $this->notice = $notice;
         if (!$notice->getProfile()->getPref("linkbackplugin", "disable_linkbacks")) {
             // Ignoring results
             common_replace_urls_callback($c, array($this, 'linkbackUrl'));
         }
         if ($notice->isRepeat()) {
             $repeat = Notice::getByID($notice->repeat_of);
             $this->linkbackUrl($repeat->getUrl());
         } else {
             if (!empty($notice->reply_to)) {
                 $parent = $notice->getParent();
                 $this->linkbackUrl($parent->getUrl());
             }
         }
         $replyProfiles = Profile::multiGet('id', $notice->getReplies());
         foreach ($replyProfiles->fetchAll('profileurl') as $profileurl) {
             $this->linkbackUrl($profileurl);
         }
     }
     return true;
 }
Example #3
0
 function render_text($text)
 {
     $text = common_remove_unicode_formatting($text);
     $text = preg_replace('/[\\x{0}-\\x{8}\\x{b}-\\x{c}\\x{e}-\\x{19}]/', '', $text);
     $text = common_replace_urls_callback($text, 'common_linkify');
     // Link #hashtags
     $rendered = preg_replace_callback('/(^|\\&quot\\;|\'|\\(|\\[|\\{|\\s+)#([\\pL\\pN_\\-\\.]{1,64})/u', function ($m) {
         return "{$m[1]}#" . common_tag_link($m[2]);
     }, $text);
     return $rendered;
 }
Example #4
0
 function onEndNoticeSave($notice)
 {
     if ($notice->is_local == 1) {
         // Try to avoid actually mucking with the
         // notice content
         $c = $notice->content;
         $this->notice = $notice;
         // Ignoring results
         common_replace_urls_callback($c, array($this, 'linkbackUrl'));
     }
     return true;
 }
Example #5
0
function common_shorten_links($text)
{
    $maxLength = Notice::maxContent();
    if ($maxLength == 0 || mb_strlen($text) <= $maxLength) {
        return $text;
    }
    return common_replace_urls_callback($text, array('File_redirection', 'makeShort'));
}
Example #6
0
/**
 * Find and shorten links in a given chunk of text if it's longer than the
 * configured notice content limit (or unconditionally).
 *
 * Side effects: may save file and file_redirection records for referenced URLs.
 *
 * Pass the $user option or call $user->shortenLinks($text) to ensure the proper
 * user's options are used; otherwise the current web session user's setitngs
 * will be used or ur1.ca if there is no active web login.
 *
 * @param string $text
 * @param boolean $always (optional)
 * @param User $user (optional)
 *
 * @return string
 */
function common_shorten_links($text, $always = false, User $user = null)
{
    $user = common_current_user();
    $maxLength = User_urlshortener_prefs::maxNoticeLength($user);
    if ($always || mb_strlen($text) > $maxLength) {
        return common_replace_urls_callback($text, array('File_redirection', 'forceShort'), $user);
    } else {
        return common_replace_urls_callback($text, array('File_redirection', 'makeShort'), $user);
    }
}
Example #7
0
/**
 * Find and shorten links in a given chunk of text if it's longer than the
 * configured notice content limit (or unconditionally).
 *
 * Side effects: may save file and file_redirection records for referenced URLs.
 *
 * Pass the $user option or call $user->shortenLinks($text) to ensure the proper
 * user's options are used; otherwise the current web session user's setitngs
 * will be used or ur1.ca if there is no active web login.
 *
 * @param string $text
 * @param boolean $always (optional)
 * @param User $user (optional)
 *
 * @return string
 */
function common_shorten_links($text, $always = false, User $user = null)
{
    $maxLength = Notice::maxContent();
    if (!$always && ($maxLength == 0 || mb_strlen($text) <= $maxLength)) {
        return $text;
    }
    return common_replace_urls_callback($text, array('File_redirection', 'makeShort'), $user);
}
 /**
  * Hook notice save to prevent blacklisted urls
  *
  * Throws an exception if there's a blacklisted url in the content.
  *
  * @param Notice &$notice Notice being saved
  *
  * @return boolean hook value
  */
 function onStartNoticeSave(&$notice)
 {
     common_replace_urls_callback($notice->content, array($this, 'checkNoticeUrl'));
     return true;
 }
 function linkify($status)
 {
     $text = $status->text;
     if (empty($status->entities)) {
         $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('/(^|\\&quot\\;|\'|\\(|\\[|\\{|\\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);
         return $text;
     }
     // Move all the entities into order so we can
     // replace them and escape surrounding plaintext
     // in order
     $toReplace = array();
     if (!empty($status->entities->urls)) {
         foreach ($status->entities->urls as $url) {
             $toReplace[$url->indices[0]] = array(self::URL, $url);
         }
     }
     if (!empty($status->entities->hashtags)) {
         foreach ($status->entities->hashtags as $hashtag) {
             $toReplace[$hashtag->indices[0]] = array(self::HASHTAG, $hashtag);
         }
     }
     if (!empty($status->entities->user_mentions)) {
         foreach ($status->entities->user_mentions as $mention) {
             $toReplace[$mention->indices[0]] = array(self::MENTION, $mention);
         }
     }
     // sort in forward order by key
     ksort($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, $orig);
                 break;
             case self::HASHTAG:
                 $linkText = $this->makeHashtagLink($object, $orig);
                 break;
             case self::MENTION:
                 $linkText = $this->makeMentionLink($object, $orig);
                 break;
             default:
                 $linkText = $orig;
                 continue;
         }
         $result .= $linkText;
         $cursor = $end;
     }
     $last = $this->twitEscape(mb_substr($text, $cursor));
     $result .= $last;
     return $result;
 }
Example #10
0
 /** save all urls in the notice to the db
  *
  * follow redirects and save all available file information
  * (mimetype, date, size, oembed, etc.)
  *
  * @return void
  */
 function saveUrls()
 {
     if (common_config('attachments', 'process_links')) {
         common_replace_urls_callback($this->content, array($this, 'saveUrl'), $this);
     }
 }
Example #11
0
 /** save all urls in the notice to the db
  *
  * follow redirects and save all available file information
  * (mimetype, date, size, oembed, etc.)
  *
  * @return void
  */
 function saveUrls()
 {
     common_replace_urls_callback($this->content, array($this, 'saveUrl'), $this->id);
 }
Example #12
0
function common_shorten_links($text)
{
    if (mb_strlen($text) <= 140) {
        return $text;
    }
    static $cache = array();
    if (isset($cache[$text])) {
        return $cache[$text];
    }
    // \s = not a horizontal whitespace character (since PHP 5.2.4)
    return $cache[$text] = common_replace_urls_callback($text, 'common_shorten_link');
}
 function linkify($status)
 {
     $text = $status->text;
     if (empty($status->entities)) {
         common_log(LOG_WARNING, "No entities data for {$status->id}; trying to fake up links ourselves.");
         $text = common_replace_urls_callback($text, 'common_linkify');
         $text = preg_replace('/(^|\\&quot\\;|\'|\\(|\\[|\\{|\\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);
         return $text;
     }
     // Move all the entities into order so we can
     // replace them in reverse order and thus
     // not mess up their indices
     $toReplace = array();
     if (!empty($status->entities->urls)) {
         foreach ($status->entities->urls as $url) {
             $toReplace[$url->indices[0]] = array(self::URL, $url);
         }
     }
     if (!empty($status->entities->hashtags)) {
         foreach ($status->entities->hashtags as $hashtag) {
             $toReplace[$hashtag->indices[0]] = array(self::HASHTAG, $hashtag);
         }
     }
     if (!empty($status->entities->user_mentions)) {
         foreach ($status->entities->user_mentions as $mention) {
             $toReplace[$mention->indices[0]] = array(self::MENTION, $mention);
         }
     }
     // sort in reverse order by key
     krsort($toReplace);
     foreach ($toReplace as $part) {
         list($type, $object) = $part;
         switch ($type) {
             case self::URL:
                 $linkText = $this->makeUrlLink($object);
                 break;
             case self::HASHTAG:
                 $linkText = $this->makeHashtagLink($object);
                 break;
             case self::MENTION:
                 $linkText = $this->makeMentionLink($object);
                 break;
             default:
                 continue;
         }
         $text = mb_substr($text, 0, $object->indices[0]) . $linkText . mb_substr($text, $object->indices[1]);
     }
     return $text;
 }