]> git.mxchange.org Git - quix0rs-gnu-social.git/blobdiff - lib/util.php
Merge branch 'ping' of /var/www/mublog.corrupt into corrupt/ping
[quix0rs-gnu-social.git] / lib / util.php
index 419e21e828ca0700ab398e8e0b0d458b10ef8154..9637dc5063aa541b9f065a66a00f7d4d9822e366 100644 (file)
@@ -81,7 +81,7 @@ function common_language()
 
     // If there is a user logged in and they've set a language preference
     // then return that one...
-    if (common_logged_in()) {
+    if (_have_config() && common_logged_in()) {
         $user = common_current_user();
         $user_language = $user->language;
         if ($user_language)
@@ -315,6 +315,10 @@ function common_current_user()
 {
     global $_cur;
 
+    if (!_have_config()) {
+        return null;
+    }
+
     if ($_cur === false) {
 
         if (isset($_REQUEST[session_name()]) || (isset($_SESSION['userid']) && $_SESSION['userid'])) {
@@ -370,8 +374,6 @@ function common_canonical_email($email)
     return $email;
 }
 
-define('URL_REGEX', '^|[ \t\r\n])((ftp|http|https|gopher|mailto|news|nntp|telnet|wais|file|prospero|aim|webcal):(([A-Za-z0-9$_.+!*(),;/?:@&~=-])|%[A-Fa-f0-9]{2}){2,}(#([a-zA-Z0-9][a-zA-Z0-9$_.+!*(),;/?:@&~=%-]*))?([A-Za-z0-9$_+!*();/?:~-]))');
-
 function common_render_content($text, $notice)
 {
     $r = common_render_text($text);
@@ -388,45 +390,114 @@ function common_render_text($text)
     $r = htmlspecialchars($text);
 
     $r = preg_replace('/[\x{0}-\x{8}\x{b}-\x{c}\x{e}-\x{19}]/', '', $r);
-    $r = preg_replace_callback('@https?://[^\]>\s]+@', 'common_render_uri_thingy', $r);
-    $r = preg_replace('/(^|\s+)#([A-Za-z0-9_\-\.]{1,64})/e', "'\\1#'.common_tag_link('\\2')", $r);
+    $r = common_replace_urls_callback($r, 'common_linkify');
+    $r = preg_replace('/(^|\(|\[|\s+)#([A-Za-z0-9_\-\.]{1,64})/e', "'\\1#'.common_tag_link('\\2')", $r);
     // XXX: machine tags
     return $r;
 }
 
-function common_render_uri_thingy($matches)
-{
-    $uri = $matches[0];
-    $trailer = '';
+function common_replace_urls_callback($text, $callback) {
+    // Start off with a regex
+    $regex = '#'.
+    '(?:'.
+        '(?:'.
+            '(?:https?|ftps?|mms|rtsp|gopher|news|nntp|telnet|wais|file|prospero|webcal|xmpp|irc)://'.
+            '|'.
+            '(?:mailto|aim|tel):'.
+        ')'.
+        '[^.\s]+\.[^\s]+'.
+        '|'.
+        '(?:[^.\s/:]+\.)+'.
+        '(?:museum|travel|[a-z]{2,4})'.
+        '(?:[:/][^\s]*)?'.
+    ')'.
+    '#ix';
+    preg_match_all($regex, $text, $matches);
+
+    // Then clean up what the regex left behind
+    $offset = 0;
+    foreach($matches[0] as $orig_url) {
+        $url = htmlspecialchars_decode($orig_url);
+
+        // Make sure we didn't pick up an email address
+        if (preg_match('#^[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,4}$#i', $url)) continue;
+
+        // Remove surrounding punctuation
+        $url = trim($url, '.?!,;:\'"`([<');
+
+        // Remove surrounding parens and the like
+        preg_match('/[)\]>]+$/', $url, $trailing);
+        if (isset($trailing[0])) {
+            preg_match_all('/[(\[<]/', $url, $opened);
+            preg_match_all('/[)\]>]/', $url, $closed);
+            $unopened = count($closed[0]) - count($opened[0]);
+
+            // Make sure not to take off more closing parens than there are at the end
+            $unopened = ($unopened > mb_strlen($trailing[0])) ? mb_strlen($trailing[0]):$unopened;
+
+            $url = ($unopened > 0) ? mb_substr($url, 0, $unopened * -1):$url;
+        }
 
-    // Some heuristics for extracting URIs from surrounding punctuation
-    // Strip from trailing text...
-    if (preg_match('/^(.*)([,.:"\']+)$/', $uri, $matches)) {
-        $uri = $matches[1];
-        $trailer = $matches[2];
-    }
+        // Remove trailing punctuation again (in case there were some inside parens)
+        $url = rtrim($url, '.?!,;:\'"`');
+
+        // Make sure we didn't capture part of the next sentence
+        preg_match('#((?:[^.\s/]+\.)+)(museum|travel|[a-z]{2,4})#i', $url, $url_parts);
 
-    $pairs = array(
-                   ']' => '[', // technically disallowed in URIs, but used in Java docs
-                   ')' => '(', // far too frequent in Wikipedia and MSDN
-                   );
-    $final = substr($uri, -1, 1);
-    if (isset($pairs[$final])) {
-        $openers = substr_count($uri, $pairs[$final]);
-        $closers = substr_count($uri, $final);
-        if ($closers > $openers) {
-            // Assume the paren was opened outside the URI
-            $uri = substr($uri, 0, -1);
-            $trailer = $final . $trailer;
+        // Were the parts capitalized any?
+        $last_part = (mb_strtolower($url_parts[2]) !== $url_parts[2]) ? true:false;
+        $prev_part = (mb_strtolower($url_parts[1]) !== $url_parts[1]) ? true:false;
+
+        // If the first part wasn't cap'd but the last part was, we captured too much
+        if ((!$prev_part && $last_part)) {
+            $url = mb_substr($url, 0 , mb_strpos($url, '.'.$url_parts['2'], 0));
         }
+
+        // Capture the new TLD
+        preg_match('#((?:[^.\s/]+\.)+)(museum|travel|[a-z]{2,4})#i', $url, $url_parts);
+
+        $tlds = array('ac', 'ad', 'ae', 'aero', 'af', 'ag', 'ai', 'al', 'am', 'an', 'ao', 'aq', 'ar', 'arpa', 'as', 'asia', 'at', 'au', 'aw', 'ax', 'az', 'ba', 'bb', 'bd', 'be', 'bf', 'bg', 'bh', 'bi', 'biz', 'bj', 'bm', 'bn', 'bo', 'br', 'bs', 'bt', 'bv', 'bw', 'by', 'bz', 'ca', 'cat', 'cc', 'cd', 'cf', 'cg', 'ch', 'ci', 'ck', 'cl', 'cm', 'cn', 'co', 'com', 'coop', 'cr', 'cu', 'cv', 'cx', 'cy', 'cz', 'de', 'dj', 'dk', 'dm', 'do', 'dz', 'ec', 'edu', 'ee', 'eg', 'er', 'es', 'et', 'eu', 'fi', 'fj', 'fk', 'fm', 'fo', 'fr', 'ga', 'gb', 'gd', 'ge', 'gf', 'gg', 'gh', 'gi', 'gl', 'gm', 'gn', 'gov', 'gp', 'gq', 'gr', 'gs', 'gt', 'gu', 'gw', 'gy', 'hk', 'hm', 'hn', 'hr', 'ht', 'hu', 'id', 'ie', 'il', 'im', 'in', 'info', 'int', 'io', 'iq', 'ir', 'is', 'it', 'je', 'jm', 'jo', 'jobs', 'jp', 'ke', 'kg', 'kh', 'ki', 'km', 'kn', 'kp', 'kr', 'kw', 'ky', 'kz', 'la', 'lb', 'lc', 'li', 'lk', 'lr', 'ls', 'lt', 'lu', 'lv', 'ly', 'ma', 'mc', 'md', 'me', 'mg', 'mh', 'mil', 'mk', 'ml', 'mm', 'mn', 'mo', 'mobi', 'mp', 'mq', 'mr', 'ms', 'mt', 'mu', 'museum', 'mv', 'mw', 'mx', 'my', 'mz', 'na', 'name', 'nc', 'ne', 'net', 'nf', 'ng', 'ni', 'nl', 'no', 'np', 'nr', 'nu', 'nz', 'om', 'org', 'pa', 'pe', 'pf', 'pg', 'ph', 'pk', 'pl', 'pm', 'pn', 'pr', 'pro', 'ps', 'pt', 'pw', 'py', 'qa', 're', 'ro', 'rs', 'ru', 'rw', 'sa', 'sb', 'sc', 'sd', 'se', 'sg', 'sh', 'si', 'sj', 'sk', 'sl', 'sm', 'sn', 'so', 'sr', 'st', 'su', 'sv', 'sy', 'sz', 'tc', 'td', 'tel', 'tf', 'tg', 'th', 'tj', 'tk', 'tl', 'tm', 'tn', 'to', 'tp', 'tr', 'travel', 'tt', 'tv', 'tw', 'tz', 'ua', 'ug', 'uk', 'us', 'uy', 'uz', 'va', 'vc', 've', 'vg', 'vi', 'vn', 'vu', 'wf', 'ws', 'ye', 'yt', 'yu', 'za', 'zm', 'zw');
+
+        if (!in_array($url_parts[2], $tlds)) continue;
+
+        // Make sure we didn't capture a hash tag
+        if (strpos($url, '#') === 0) continue;
+
+        // Put the url back the way we found it.
+        $url = (mb_strpos($orig_url, htmlspecialchars($url)) === FALSE) ? $url:htmlspecialchars($url);
+
+        // Call user specified func
+        $modified_url = $callback($url);
+
+        // Replace it!
+        $start = mb_strpos($text, $url, $offset);
+        $text = mb_substr($text, 0, $start).$modified_url.mb_substr($text, $start + mb_strlen($url), mb_strlen($text));
+        $offset = $start + mb_strlen($modified_url);
+    }
+
+    return $text;
+}
+
+function common_linkify($url) {
+    // It comes in special'd, so we unspecial it before passing to the stringifying
+    // functions
+    $ext = pathinfo($url, PATHINFO_EXTENSION);
+    $url = htmlspecialchars_decode($url);
+    $video_ext = array('mp4', 'flv', 'avi', 'mpg', 'mp3', 'ogg');
+    $display = $url;
+    $url = (!preg_match('#^([a-z]+://|(mailto|aim|tel):)#i', $url)) ? 'http://'.$url : $url;
+
+    $attrs = array('href' => $url, 'rel' => 'external');
+
+    if (in_array($ext, $video_ext)) {
+        $attrs['class'] = 'media';
     }
-    if ($longurl = common_longurl($uri)) {
-        $longurl = htmlentities($longurl, ENT_QUOTES, 'UTF-8');
-        $title = " title='$longurl'";
+
+    if ($longurl = common_longurl($url)) {
+        $attrs['title'] = $longurl;
     }
-    else $title = '';
 
-    return '<a href="' . $uri . '"' . $title . ' class="extlink">' . $uri . '</a>' . $trailer;
+    return XMLStringer::estring('a', $attrs, $display);
 }
 
 function common_longurl($short_url)
@@ -450,7 +521,7 @@ function common_shorten_links($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] = preg_replace('@https?://[^)\]>\s]+@e', "common_shorten_link('\\0')", $text);
+    return $cache[$text] = common_replace_urls_callback($text, 'common_shorten_link');;
 }
 
 function common_shorten_link($url, $reverse = false)
@@ -527,7 +598,13 @@ function common_tag_link($tag)
 {
     $canonical = common_canonical_tag($tag);
     $url = common_local_url('tag', array('tag' => $canonical));
-    return '<span class="tag"><a href="' . htmlspecialchars($url) . '" rel="tag">' . htmlspecialchars($tag) . '</a></span>';
+    $xs = new XMLStringer();
+    $xs->elementStart('span', 'tag');
+    $xs->element('a', array('href' => $url,
+                            'rel' => 'tag'),
+                 $tag);
+    $xs->elementEnd('span');
+    return $xs->getString();
 }
 
 function common_canonical_tag($tag)
@@ -545,7 +622,20 @@ function common_at_link($sender_id, $nickname)
     $sender = Profile::staticGet($sender_id);
     $recipient = common_relative_profile($sender, common_canonical_nickname($nickname));
     if ($recipient) {
-        return '<span class="vcard"><a href="'.htmlspecialchars($recipient->profileurl).'" class="url"><span class="fn nickname">'.$nickname.'</span></a></span>';
+        $user = User::staticGet('id', $recipient->id);
+        if ($user) {
+            $url = common_local_url('userbyid', array('id' => $user->id));
+        } else {
+            $url = $recipient->profileurl;
+        }
+        $xs = new XMLStringer(false);
+        $xs->elementStart('span', 'vcard');
+        $xs->elementStart('a', array('href' => $url,
+                                     'class' => 'url'));
+        $xs->element('span', 'fn nickname', $nickname);
+        $xs->elementEnd('a');
+        $xs->elementEnd('span');
+        return $xs->getString();
     } else {
         return $nickname;
     }
@@ -556,7 +646,14 @@ function common_group_link($sender_id, $nickname)
     $sender = Profile::staticGet($sender_id);
     $group = User_group::staticGet('nickname', common_canonical_nickname($nickname));
     if ($group && $sender->isMember($group)) {
-        return '<span class="vcard"><a href="'.htmlspecialchars($group->permalink()).'" class="url"><span class="fn nickname">'.$nickname.'</span></a></span>';
+        $xs = new XMLStringer();
+        $xs->elementStart('span', 'vcard');
+        $xs->elementStart('a', array('href' => $group->permalink(),
+                                     'class' => 'url'));
+        $xs->element('span', 'fn nickname', $nickname);
+        $xs->elementEnd('a');
+        $xs->elementEnd('span');
+        return $xs->getString();
     } else {
         return $nickname;
     }
@@ -573,7 +670,13 @@ function common_at_hash_link($sender_id, $tag)
         $url = common_local_url('subscriptions',
                                 array('nickname' => $user->nickname,
                                       'tag' => $tag));
-        return '<span class="tag"><a href="'.htmlspecialchars($url).'" rel="tag">'.$tag.'</a></span>';
+        $xs = new XMLStringer();
+        $xs->elementStart('span', 'tag');
+        $xs->element('a', array('href' => $url,
+                                'rel' => $tag),
+                     $tag);
+        $xs->elementEnd('span');
+        return $xs->getString();
     } else {
         return $tag;
     }
@@ -615,315 +718,20 @@ function common_relative_profile($sender, $nickname, $dt=null)
     return null;
 }
 
-// where should the avatar go for this user?
-
-function common_avatar_filename($id, $extension, $size=null, $extra=null)
+function common_local_url($action, $args=null, $params=null, $fragment=null)
 {
-    global $config;
-
-    if ($size) {
-        return $id . '-' . $size . (($extra) ? ('-' . $extra) : '') . $extension;
-    } else {
-        return $id . '-original' . (($extra) ? ('-' . $extra) : '') . $extension;
+    $r = Router::get();
+    $path = $r->build($action, $args, $params, $fragment);
+    if ($path) {
     }
-}
-
-function common_avatar_path($filename)
-{
-    global $config;
-    return INSTALLDIR . '/avatar/' . $filename;
-}
-
-function common_avatar_url($filename)
-{
-    return common_path('avatar/'.$filename);
-}
-
-function common_avatar_display_url($avatar)
-{
-    $server = common_config('avatar', 'server');
-    if ($server) {
-        return 'http://'.$server.'/'.$avatar->filename;
-    } else {
-        return $avatar->url;
-    }
-}
-
-function common_default_avatar($size)
-{
-    static $sizenames = array(AVATAR_PROFILE_SIZE => 'profile',
-                              AVATAR_STREAM_SIZE => 'stream',
-                              AVATAR_MINI_SIZE => 'mini');
-    return theme_path('default-avatar-'.$sizenames[$size].'.png');
-}
-
-function common_local_url($action, $args=null, $fragment=null)
-{
-    $url = null;
     if (common_config('site','fancy')) {
-        $url = common_fancy_url($action, $args);
+        $url = common_path(mb_substr($path, 1));
     } else {
-        $url = common_simple_url($action, $args);
-    }
-    if (!is_null($fragment)) {
-        $url .= '#'.$fragment;
+        $url = common_path('index.php'.$path);
     }
     return $url;
 }
 
-function common_fancy_url($action, $args=null)
-{
-    switch (strtolower($action)) {
-     case 'public':
-        if ($args && isset($args['page'])) {
-            return common_path('?page=' . $args['page']);
-        } else {
-            return common_path('');
-        }
-     case 'featured':
-        if ($args && isset($args['page'])) {
-            return common_path('featured?page=' . $args['page']);
-        } else {
-            return common_path('featured');
-        }
-     case 'favorited':
-        if ($args && isset($args['page'])) {
-            return common_path('favorited?page=' . $args['page']);
-        } else {
-            return common_path('favorited');
-        }
-     case 'publicrss':
-        return common_path('rss');
-     case 'publicatom':
-        return common_path("api/statuses/public_timeline.atom");
-     case 'publicxrds':
-        return common_path('xrds');
-     case 'featuredrss':
-        return common_path('featuredrss');
-     case 'favoritedrss':
-        return common_path('favoritedrss');
-     case 'opensearch':
-        if ($args && $args['type']) {
-            return common_path('opensearch/'.$args['type']);
-        } else {
-            return common_path('opensearch/people');
-        }
-     case 'doc':
-        return common_path('doc/'.$args['title']);
-     case 'block':
-     case 'login':
-     case 'logout':
-     case 'subscribe':
-     case 'unsubscribe':
-     case 'invite':
-        return common_path('main/'.$action);
-     case 'tagother':
-        return common_path('main/tagother?id='.$args['id']);
-     case 'register':
-        if ($args && $args['code']) {
-            return common_path('main/register/'.$args['code']);
-        } else {
-            return common_path('main/register');
-        }
-     case 'remotesubscribe':
-        if ($args && $args['nickname']) {
-            return common_path('main/remote?nickname=' . $args['nickname']);
-        } else {
-            return common_path('main/remote');
-        }
-     case 'nudge':
-        return common_path($args['nickname'].'/nudge');
-     case 'openidlogin':
-        return common_path('main/openid');
-     case 'profilesettings':
-        return common_path('settings/profile');
-     case 'passwordsettings':
-        return common_path('settings/password');
-     case 'emailsettings':
-        return common_path('settings/email');
-     case 'openidsettings':
-        return common_path('settings/openid');
-     case 'smssettings':
-        return common_path('settings/sms');
-     case 'twittersettings':
-        return common_path('settings/twitter');
-     case 'othersettings':
-        return common_path('settings/other');
-     case 'deleteprofile':
-        return common_path('settings/delete');
-     case 'newnotice':
-        if ($args && $args['replyto']) {
-            return common_path('notice/new?replyto='.$args['replyto']);
-        } else {
-            return common_path('notice/new');
-        }
-     case 'shownotice':
-        return common_path('notice/'.$args['notice']);
-     case 'deletenotice':
-        if ($args && $args['notice']) {
-            return common_path('notice/delete/'.$args['notice']);
-        } else {
-            return common_path('notice/delete');
-        }
-     case 'microsummary':
-     case 'xrds':
-     case 'foaf':
-        return common_path($args['nickname'].'/'.$action);
-     case 'all':
-     case 'replies':
-     case 'inbox':
-     case 'outbox':
-        if ($args && isset($args['page'])) {
-            return common_path($args['nickname'].'/'.$action.'?page=' . $args['page']);
-        } else {
-            return common_path($args['nickname'].'/'.$action);
-        }
-     case 'subscriptions':
-     case 'subscribers':
-        $nickname = $args['nickname'];
-        unset($args['nickname']);
-        if (isset($args['tag'])) {
-            $tag = $args['tag'];
-            unset($args['tag']);
-        }
-        $params = http_build_query($args);
-        if ($params) {
-            return common_path($nickname.'/'.$action . (($tag) ? '/' . $tag : '') . '?' . $params);
-        } else {
-            return common_path($nickname.'/'.$action . (($tag) ? '/' . $tag : ''));
-        }
-     case 'allrss':
-        return common_path($args['nickname'].'/all/rss');
-     case 'repliesrss':
-        return common_path($args['nickname'].'/replies/rss');
-     case 'userrss':
-        if (isset($args['limit']))
-          return common_path($args['nickname'].'/rss?limit=' . $args['limit']);
-        return common_path($args['nickname'].'/rss');
-     case 'showstream':
-        if ($args && isset($args['page'])) {
-            return common_path($args['nickname'].'?page=' . $args['page']);
-        } else {
-            return common_path($args['nickname']);
-        }
-
-     case 'usertimeline':
-        return common_path("api/statuses/user_timeline/".$args['nickname'].".atom");
-     case 'confirmaddress':
-        return common_path('main/confirmaddress/'.$args['code']);
-     case 'userbyid':
-        return common_path('user/'.$args['id']);
-     case 'recoverpassword':
-        $path = 'main/recoverpassword';
-        if ($args['code']) {
-            $path .= '/' . $args['code'];
-        }
-        return common_path($path);
-     case 'imsettings':
-        return common_path('settings/im');
-     case 'avatarsettings':
-        return common_path('settings/avatar');
-     case 'groupsearch':
-        return common_path('search/group' . (($args) ? ('?' . http_build_query($args)) : ''));
-     case 'peoplesearch':
-        return common_path('search/people' . (($args) ? ('?' . http_build_query($args)) : ''));
-     case 'noticesearch':
-        return common_path('search/notice' . (($args) ? ('?' . http_build_query($args)) : ''));
-     case 'noticesearchrss':
-        return common_path('search/notice/rss' . (($args) ? ('?' . http_build_query($args)) : ''));
-     case 'avatarbynickname':
-        return common_path($args['nickname'].'/avatar/'.$args['size']);
-     case 'tag':
-        $path = 'tag/' . $args['tag'];
-        unset($args['tag']);
-        return common_path($path . (($args) ? ('?' . http_build_query($args)) : ''));
-     case 'publictagcloud':
-        return common_path('tags');
-     case 'peopletag':
-        $path = 'peopletag/' . $args['tag'];
-        unset($args['tag']);
-        return common_path($path . (($args) ? ('?' . http_build_query($args)) : ''));
-     case 'tags':
-        return common_path('tags' . (($args) ? ('?' . http_build_query($args)) : ''));
-     case 'favor':
-        return common_path('main/favor');
-     case 'disfavor':
-        return common_path('main/disfavor');
-     case 'showfavorites':
-        if ($args && isset($args['page'])) {
-            return common_path($args['nickname'].'/favorites?page=' . $args['page']);
-        } else {
-            return common_path($args['nickname'].'/favorites');
-        }
-     case 'favoritesrss':
-        return common_path($args['nickname'].'/favorites/rss');
-     case 'showmessage':
-        return common_path('message/' . $args['message']);
-     case 'newmessage':
-        return common_path('message/new' . (($args) ? ('?' . http_build_query($args)) : ''));
-     case 'api':
-        // XXX: do fancy URLs for all the API methods
-        switch (strtolower($args['apiaction'])) {
-         case 'statuses':
-            switch (strtolower($args['method'])) {
-             case 'user_timeline.rss':
-                return common_path('api/statuses/user_timeline/'.$args['argument'].'.rss');
-             case 'user_timeline.atom':
-                return common_path('api/statuses/user_timeline/'.$args['argument'].'.atom');
-             case 'user_timeline.json':
-                return common_path('api/statuses/user_timeline/'.$args['argument'].'.json');
-             case 'user_timeline.xml':
-                return common_path('api/statuses/user_timeline/'.$args['argument'].'.xml');
-             default: return common_simple_url($action, $args);
-            }
-         default: return common_simple_url($action, $args);
-        }
-     case 'sup':
-        if ($args && isset($args['seconds'])) {
-            return common_path('main/sup?seconds='.$args['seconds']);
-        } else {
-            return common_path('main/sup');
-        }
-     case 'newgroup':
-        return common_path('group/new');
-     case 'showgroup':
-        return common_path('group/'.$args['nickname']);
-     case 'editgroup':
-        return common_path('group/'.$args['nickname'].'/edit');
-     case 'joingroup':
-        return common_path('group/'.$args['nickname'].'/join');
-     case 'leavegroup':
-        return common_path('group/'.$args['nickname'].'/leave');
-     case 'groupbyid':
-        return common_path('group/'.$args['id'].'/id');
-     case 'grouprss':
-        return common_path('group/'.$args['nickname'].'/rss');
-     case 'groupmembers':
-        return common_path('group/'.$args['nickname'].'/members');
-     case 'grouplogo':
-        return common_path('group/'.$args['nickname'].'/logo');
-     case 'usergroups':
-        return common_path($args['nickname'].'/groups' . (($args) ? ('?' . http_build_query($args)) : ''));
-     case 'groups':
-        return common_path('group' . (($args) ? ('?' . http_build_query($args)) : ''));
-     default:
-        return common_simple_url($action, $args);
-    }
-}
-
-function common_simple_url($action, $args=null)
-{
-    global $config;
-    /* XXX: pretty URLs */
-    $extra = '';
-    if ($args) {
-        foreach ($args as $key => $value) {
-            $extra .= "&${key}=${value}";
-        }
-    }
-    return common_path("index.php?action=${action}${extra}");
-}
-
 function common_path($relative)
 {
     global $config;
@@ -1032,24 +840,6 @@ function common_redirect($url, $code=307)
 
 function common_broadcast_notice($notice, $remote=false)
 {
-
-    // Check to see if notice should go to Twitter
-    $flink = Foreign_link::getByUserID($notice->profile_id, 1); // 1 == Twitter
-    if (($flink->noticesync & FOREIGN_NOTICE_SEND) == FOREIGN_NOTICE_SEND) {
-
-        // If it's not a Twitter-style reply, or if the user WANTS to send replies...
-
-        if (!preg_match('/^@[a-zA-Z0-9_]{1,15}\b/u', $notice->content) ||
-            (($flink->noticesync & FOREIGN_NOTICE_SEND_REPLY) == FOREIGN_NOTICE_SEND_REPLY)) {
-
-            $result = common_twitter_broadcast($notice, $flink);
-
-            if (!$result) {
-                common_debug('Unable to send notice: ' . $notice->id . ' to Twitter.', __FILE__);
-            }
-        }
-    }
-
     if (common_config('queue', 'enabled')) {
         // Do it later!
         return common_enqueue_notice($notice);
@@ -1058,73 +848,11 @@ function common_broadcast_notice($notice, $remote=false)
     }
 }
 
-function common_twitter_broadcast($notice, $flink)
-{
-    global $config;
-    $success = true;
-    $fuser = $flink->getForeignUser();
-    $twitter_user = $fuser->nickname;
-    $twitter_password = $flink->credentials;
-    $uri = 'http://www.twitter.com/statuses/update.json';
-
-    // XXX: Hack to get around PHP cURL's use of @ being a a meta character
-    $statustxt = preg_replace('/^@/', ' @', $notice->content);
-
-    $options = array(
-                     CURLOPT_USERPWD         => "$twitter_user:$twitter_password",
-                     CURLOPT_POST            => true,
-                     CURLOPT_POSTFIELDS        => array(
-                                                        'status'    => $statustxt,
-                                                        'source'    => $config['integration']['source']
-                                                        ),
-                     CURLOPT_RETURNTRANSFER    => true,
-                     CURLOPT_FAILONERROR        => true,
-                     CURLOPT_HEADER            => false,
-                     CURLOPT_FOLLOWLOCATION    => true,
-                     CURLOPT_USERAGENT        => "Laconica",
-                     CURLOPT_CONNECTTIMEOUT    => 120,  // XXX: Scary!!!! How long should this be?
-                     CURLOPT_TIMEOUT            => 120,
-
-                     # Twitter is strict about accepting invalid "Expect" headers
-                     CURLOPT_HTTPHEADER => array('Expect:')
-                     );
-
-    $ch = curl_init($uri);
-    curl_setopt_array($ch, $options);
-    $data = curl_exec($ch);
-    $errmsg = curl_error($ch);
-
-    if ($errmsg) {
-        common_debug("cURL error: $errmsg - trying to send notice for $twitter_user.",
-                     __FILE__);
-        $success = false;
-    }
-
-    curl_close($ch);
-
-    if (!$data) {
-        common_debug("No data returned by Twitter's API trying to send update for $twitter_user",
-                     __FILE__);
-        $success = false;
-    }
-
-    // Twitter should return a status
-    $status = json_decode($data);
-
-    if (!$status->id) {
-        common_debug("Unexpected data returned by Twitter API trying to send update for $twitter_user",
-                     __FILE__);
-        $success = false;
-    }
-
-    return $success;
-}
-
 // Stick the notice on the queue
 
 function common_enqueue_notice($notice)
 {
-    foreach (array('jabber', 'omb', 'sms', 'public', 'ping') as $transport) {
+    foreach (array('jabber', 'omb', 'sms', 'public', 'twitter', 'facebook', 'ping') as $transport) {
         $qi = new Queue_item();
         $qi->notice_id = $notice->id;
         $qi->transport = $transport;
@@ -1140,23 +868,6 @@ function common_enqueue_notice($notice)
     return $result;
 }
 
-function common_dequeue_notice($notice)
-{
-    $qi = Queue_item::staticGet($notice->id);
-    if ($qi) {
-        $result = $qi->delete();
-        if (!$result) {
-            $last_error = &PEAR::getStaticProperty('DB_DataObject','lastError');
-            common_log(LOG_ERR, 'DB error deleting queue item: ' . $last_error->message);
-            return false;
-        }
-        common_log(LOG_DEBUG, 'complete dequeueing notice ID = ' . $notice->id);
-        return $result;
-    } else {
-        return false;
-    }
-}
-
 function common_real_broadcast($notice, $remote=false)
 {
     $success = true;
@@ -1188,6 +899,15 @@ function common_real_broadcast($notice, $remote=false)
             common_log(LOG_ERR, 'Error in public broadcast for notice ' . $notice->id);
         }
     }
+    if ($success) {
+        $success = broadcast_twitter($notice);
+        if (!$success) {
+            common_log(LOG_ERR, 'Error in Twitter broadcast for notice ' . $notice->id);
+        }
+    }
+
+    // XXX: Do a real-time FB broadcast here?
+
     // XXX: broadcast notices to other IM
     return $success;
 }
@@ -1406,7 +1126,7 @@ function common_negotiate_type($cprefs, $sprefs)
     }
 
     $bestq = 0;
-    $besttype = "text/html";
+    $besttype = 'text/html';
 
     foreach(array_keys($combine) as $type) {
         if($combine[$type] > $bestq) {
@@ -1415,6 +1135,9 @@ function common_negotiate_type($cprefs, $sprefs)
         }
     }
 
+    if ('text/html' === $besttype) {
+        return "text/html; charset=utf-8";
+    }
     return $besttype;
 }
 
@@ -1483,16 +1206,6 @@ function common_markup_to_html($c)
     return Markdown($c);
 }
 
-function common_profile_avatar_url($profile, $size=AVATAR_PROFILE_SIZE)
-{
-    $avatar = $profile->getAvatar($size);
-    if ($avatar) {
-        return common_avatar_display_url($avatar);
-    } else {
-        return common_default_avatar($size);
-    }
-}
-
 function common_profile_uri($profile)
 {
     if (!$profile) {