]> git.mxchange.org Git - quix0rs-gnu-social.git/blobdiff - lib/util.php
Merge branch '0.7.x' of git@gitorious.org:laconica/dev into 0.7.x
[quix0rs-gnu-social.git] / lib / util.php
index 3f4fae3a59e2d193e5001e7a41efad137b210202..46aa7b9df907b2691f0ce782376a7005da573a98 100644 (file)
@@ -394,20 +394,20 @@ function common_render_text($text)
 
 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';
+    $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
@@ -472,16 +472,25 @@ function common_replace_urls_callback($text, $callback) {
 }
 
 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;
+    $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($url)) {
-        $longurl = htmlentities($longurl, ENT_QUOTES, 'UTF-8');
-        $title = "title=\"$longurl\"";
+        $attrs['title'] = $longurl;
     }
-    else $title = '';
 
-    return "<a href=\"$url\" $title rel=\"external\">$display</a>";
+    return XMLStringer::estring('a', $attrs, $display);
 }
 
 function common_longurl($short_url)
@@ -582,7 +591,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)
@@ -600,7 +615,14 @@ 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>';
+        $xs = new XMLStringer(false);
+        $xs->elementStart('span', 'vcard');
+        $xs->elementStart('a', array('href' => $recipient->profileurl,
+                                     'class' => 'url'));
+        $xs->element('span', 'fn nickname', $nickname);
+        $xs->elementEnd('a');
+        $xs->elementEnd('span');
+        return $xs->getString();
     } else {
         return $nickname;
     }
@@ -611,7 +633,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;
     }
@@ -628,7 +657,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;
     }
@@ -672,12 +707,8 @@ function common_relative_profile($sender, $nickname, $dt=null)
 
 function common_local_url($action, $args=null, $fragment=null)
 {
-    common_debug("Action = $action, args = " . (($args) ? '(' . implode($args, ',') . ')' : $args) . ", fragment = $fragment");
     $r = Router::get();
-    $start = microtime();
     $path = $r->build($action, $args, $fragment);
-    $end = microtime();
-    common_debug("Pathbuilding took " . ($end - $start));
     if ($path) {
     }
     if (common_config('site','fancy')) {
@@ -808,7 +839,7 @@ function common_broadcast_notice($notice, $remote=false)
 
 function common_enqueue_notice($notice)
 {
-    foreach (array('jabber', 'omb', 'sms', 'public') as $transport) {
+    foreach (array('jabber', 'omb', 'sms', 'public', 'twitter', 'facebook') as $transport) {
         $qi = new Queue_item();
         $qi->notice_id = $notice->id;
         $qi->transport = $transport;
@@ -861,7 +892,9 @@ function common_real_broadcast($notice, $remote=false)
             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;
 }