]> git.mxchange.org Git - quix0rs-gnu-social.git/blobdiff - lib/util.php
Merge branch '0.8.x' of git://gitorious.org/laconica/dev into 0.8.x
[quix0rs-gnu-social.git] / lib / util.php
index a6f6a935186ef4ca754837a43f9be86850cc5cc4..91b15ccf554afcb3f7080493b73956b016335122 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'])) {
@@ -456,6 +460,9 @@ function common_replace_urls_callback($text, $callback) {
 
         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);
 
@@ -472,13 +479,21 @@ 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');
         $attrs['title'] = $longurl;
     }
 
@@ -588,7 +603,7 @@ function common_tag_link($tag)
     $xs->element('a', array('href' => $url,
                             'rel' => 'tag'),
                  $tag);
-    $xs->elementEnd();
+    $xs->elementEnd('span');
     return $xs->getString();
 }
 
@@ -607,9 +622,15 @@ function common_at_link($sender_id, $nickname)
     $sender = Profile::staticGet($sender_id);
     $recipient = common_relative_profile($sender, common_canonical_nickname($nickname));
     if ($recipient) {
+        $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' => $recipient->profileurl,
+        $xs->elementStart('a', array('href' => $url,
                                      'class' => 'url'));
         $xs->element('span', 'fn nickname', $nickname);
         $xs->elementEnd('a');
@@ -697,14 +718,10 @@ function common_relative_profile($sender, $nickname, $dt=null)
     return null;
 }
 
-function common_local_url($action, $args=null, $fragment=null)
+function common_local_url($action, $args=null, $params=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));
+    $path = $r->build($action, $args, $params, $fragment);
     if ($path) {
     }
     if (common_config('site','fancy')) {
@@ -823,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);
@@ -849,84 +848,62 @@ 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') as $transport) {
-        $qi = new Queue_item();
-        $qi->notice_id = $notice->id;
-        $qi->transport = $transport;
-        $qi->created = $notice->created;
-        $result = $qi->insert();
-        if (!$result) {
-            $last_error = &PEAR::getStaticProperty('DB_DataObject','lastError');
-            common_log(LOG_ERR, 'DB error inserting queue item: ' . $last_error->message);
-            return false;
+    if (common_config('queue','subsystem') == 'stomp') {
+       // use an external message queue system via STOMP
+       require_once("Stomp.php");
+       $con = new Stomp(common_config('queue','stomp_server'));
+       if (!$con->connect()) {
+               common_log(LOG_ERR, 'Failed to connect to queue server');
+               return false;
+       }
+       $queue_basename = common_config('queue','queue_basename');
+       foreach (array('jabber', 'omb', 'sms', 'public', 'twitter', 'facebook', 'ping') as $transport) {
+               if (!$con->send(
+                       '/queue/'.$queue_basename.'-'.$transport, // QUEUE
+                       $notice->id,            // BODY of the message
+                       array (                 // HEADERS of the msg
+                       'created' => $notice->created
+                       ))) {
+                       common_log(LOG_ERR, 'Error sending to '.$transport.' queue');
+                       return false;
+               }
+        common_log(LOG_DEBUG, 'complete remote queueing notice ID = ' . $notice->id . ' for ' . $transport);
+       }
+       $con->send('/topic/laconica.'.$notice->profile_id,
+                       $notice->content,
+                       array(
+                               'profile_id' => $notice->profile_id,
+                               'created' => $notice->created
+                               )
+                       );
+       $con->send('/topic/laconica.allusers',
+                       $notice->content,
+                       array(
+                               'profile_id' => $notice->profile_id,
+                               'created' => $notice->created
+                               )
+                       );
+       $result = true;
+    }
+    else {
+       // in any other case, 'internal'
+       foreach (array('jabber', 'omb', 'sms', 'public', 'twitter', 'facebook', 'ping') as $transport) {
+               $qi = new Queue_item();
+               $qi->notice_id = $notice->id;
+               $qi->transport = $transport;
+               $qi->created = $notice->created;
+               $result = $qi->insert();
+               if (!$result) {
+                   $last_error = &PEAR::getStaticProperty('DB_DataObject','lastError');
+                   common_log(LOG_ERR, 'DB error inserting queue item: ' . $last_error->message);
+                   return false;
+               }
+               common_log(LOG_DEBUG, 'complete queueing notice ID = ' . $notice->id . ' for ' . $transport);
         }
-        common_log(LOG_DEBUG, 'complete queueing notice ID = ' . $notice->id . ' for ' . $transport);
     }
     return $result;
 }
@@ -962,6 +939,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;
 }