]> git.mxchange.org Git - quix0rs-gnu-social.git/blobdiff - lib/util.php
Merge branch 'testing' of git@gitorious.org:statusnet/mainline into testing
[quix0rs-gnu-social.git] / lib / util.php
index fdcc87677b08011829c0b77c62e2cf44e8f6244d..ae812e8cf4d0aacdd6a7166461c761fed0eeb7e1 100644 (file)
@@ -62,7 +62,7 @@ function common_init_language()
     // gettext will still select the right language.
     $language = common_language();
     $locale_set = common_init_locale($language);
-    
+
     setlocale(LC_CTYPE, 'C');
     // So we do not have to make people install the gettext locales
     $path = common_config('site','locale_path');
@@ -119,6 +119,11 @@ function common_language()
 
 function common_munge_password($password, $id)
 {
+    if (is_object($id) || is_object($password)) {
+        $e = new Exception();
+        common_log(LOG_ERR, __METHOD__ . ' object in param to common_munge_password ' .
+                   str_replace("\n", " ", $e->getTraceAsString()));
+    }
     return md5($password . $id);
 }
 
@@ -166,10 +171,18 @@ function common_ensure_session()
         if (common_config('sessions', 'handle')) {
             Session::setSaveHandler();
         }
+       if (array_key_exists(session_name(), $_GET)) {
+           $id = $_GET[session_name()];
+       } else if (array_key_exists(session_name(), $_COOKIE)) {
+           $id = $_COOKIE[session_name()];
+       }
+       if (isset($id)) {
+           session_id($id);
+       }
         @session_start();
         if (!isset($_SESSION['started'])) {
             $_SESSION['started'] = time();
-            if (!empty($c)) {
+            if (!empty($id)) {
                 common_log(LOG_WARNING, 'Session cookie "' . $_COOKIE[session_name()] . '" ' .
                            ' is set but started value is null');
             }
@@ -241,7 +254,6 @@ function common_rememberme($user=null)
     if (!$user) {
         $user = common_current_user();
         if (!$user) {
-            common_debug('No current user to remember', __FILE__);
             return false;
         }
     }
@@ -259,14 +271,11 @@ function common_rememberme($user=null)
 
     if (!$result) {
         common_log_db_error($rm, 'INSERT', __FILE__);
-        common_debug('Error adding rememberme record for ' . $user->nickname, __FILE__);
         return false;
     }
 
     $rm->query('COMMIT');
 
-    common_debug('Inserted rememberme record (' . $rm->code . ', ' . $rm->user_id . '); result = ' . $result . '.', __FILE__);
-
     $cookieval = $rm->user_id . ':' . $rm->code;
 
     common_log(LOG_INFO, 'adding rememberme cookie "' . $cookieval . '" for ' . $user->nickname);
@@ -358,7 +367,8 @@ function common_current_user()
 
     if ($_cur === false) {
 
-        if (isset($_REQUEST[session_name()]) || (isset($_SESSION['userid']) && $_SESSION['userid'])) {
+        if (isset($_COOKIE[session_name()]) || isset($_GET[session_name()])
+            || (isset($_SESSION['userid']) && $_SESSION['userid'])) {
             common_ensure_session();
             $id = isset($_SESSION['userid']) ? $_SESSION['userid'] : false;
             if ($id) {
@@ -374,8 +384,6 @@ function common_current_user()
         $_cur = common_remembered_user();
 
         if ($_cur) {
-            common_debug("Got User " . $_cur->nickname);
-            common_debug("Faking session on remembered user");
             // XXX: Is this necessary?
             $_SESSION['userid'] = $_cur->id;
         }
@@ -658,6 +666,9 @@ function common_valid_profile_tag($str)
 function common_at_link($sender_id, $nickname)
 {
     $sender = Profile::staticGet($sender_id);
+    if (!$sender) {
+        return $nickname;
+    }
     $recipient = common_relative_profile($sender, common_canonical_nickname($nickname));
     if ($recipient) {
         $user = User::staticGet('id', $recipient->id);
@@ -687,7 +698,7 @@ function common_group_link($sender_id, $nickname)
 {
     $sender = Profile::staticGet($sender_id);
     $group = User_group::getForNickname($nickname);
-    if ($group && $sender->isMember($group)) {
+    if ($sender && $group && $sender->isMember($group)) {
         $attrs = array('href' => $group->permalink(),
                        'class' => 'url');
         if (!empty($group->fullname)) {
@@ -809,20 +820,50 @@ function common_path($relative, $ssl=false)
         } else if (common_config('site', 'server')) {
             $serverpart = common_config('site', 'server');
         } else {
-            common_log(LOG_ERR, 'Site Sever not configured, unable to determine site name.');
+            common_log(LOG_ERR, 'Site server not configured, unable to determine site name.');
         }
     } else {
         $proto = 'http';
         if (common_config('site', 'server')) {
             $serverpart = common_config('site', 'server');
         } else {
-            common_log(LOG_ERR, 'Site Sever not configured, unable to determine site name.');
+            common_log(LOG_ERR, 'Site server not configured, unable to determine site name.');
         }
     }
 
+    $relative = common_inject_session($relative, $serverpart);
+
     return $proto.'://'.$serverpart.'/'.$pathpart.$relative;
 }
 
+function common_inject_session($url, $serverpart = null)
+{
+    if (common_have_session()) {
+
+       if (empty($serverpart)) {
+           $serverpart = parse_url($url, PHP_URL_HOST);
+       }
+
+        $currentServer = $_SERVER['HTTP_HOST'];
+
+        // Are we pointing to another server (like an SSL server?)
+
+        if (!empty($currentServer) &&
+            0 != strcasecmp($currentServer, $serverpart)) {
+            // Pass the session ID as a GET parameter
+            $sesspart = session_name() . '=' . session_id();
+            $i = strpos($url, '?');
+            if ($i === false) { // no GET params, just append
+                $url .= '?' . $sesspart;
+            } else {
+                $url = substr($url, 0, $i + 1).$sesspart.'&'.substr($url, $i + 1);
+            }
+        }
+    }
+
+    return $url;
+}
+
 function common_date_string($dt)
 {
     // XXX: do some sexy date formatting
@@ -949,7 +990,7 @@ function common_redirect($url, $code=307)
 
 function common_broadcast_notice($notice, $remote=false)
 {
-    return common_enqueue_notice($notice);
+    // DO NOTHING!
 }
 
 // Stick the notice on the queue
@@ -959,9 +1000,13 @@ function common_enqueue_notice($notice)
     static $localTransports = array('omb',
                                     'ping');
 
-    static $allTransports = array('sms', 'plugin');
-
-    $transports = $allTransports;
+    $transports = array();
+    if (common_config('sms', 'enabled')) {
+        $transports[] = 'sms';
+    }
+    if (Event::hasHandler('HandleQueuedNotice')) {
+        $transports[] = 'plugin';
+    }
 
     $xmpp = common_config('xmpp', 'enabled');
 
@@ -969,6 +1014,7 @@ function common_enqueue_notice($notice)
         $transports[] = 'jabber';
     }
 
+    // @fixme move these checks into QueueManager and/or individual handlers
     if ($notice->is_local == Notice::LOCAL_PUBLIC ||
         $notice->is_local == Notice::LOCAL_NONPUBLIC) {
         $transports = array_merge($transports, $localTransports);
@@ -1010,7 +1056,12 @@ function common_profile_url($nickname)
 
 function common_root_url($ssl=false)
 {
-    return common_path('', $ssl);
+    $url = common_path('', $ssl);
+    $i = strpos($url, '?');
+    if ($i !== false) {
+        $url = substr($url, 0, $i);
+    }
+    return $url;
 }
 
 // returns $bytes bytes of random data as a hexadecimal string
@@ -1085,8 +1136,10 @@ function common_log_line($priority, $msg)
 function common_request_id()
 {
     $pid = getmypid();
+    $server = common_config('site', 'server');
     if (php_sapi_name() == 'cli') {
-        return $pid;
+        $script = basename($_SERVER['PHP_SELF']);
+        return "$server:$script:$pid";
     } else {
         static $req_id = null;
         if (!isset($req_id)) {
@@ -1096,7 +1149,7 @@ function common_request_id()
             $url = $_SERVER['REQUEST_URI'];
         }
         $method = $_SERVER['REQUEST_METHOD'];
-        return "$pid.$req_id $method $url";
+        return "$server:$pid.$req_id $method $url";
     }
 }
 
@@ -1404,41 +1457,17 @@ function common_session_token()
 
 function common_cache_key($extra)
 {
-    $base_key = common_config('memcached', 'base');
-
-    if (empty($base_key)) {
-        $base_key = common_keyize(common_config('site', 'name'));
-    }
-
-    return 'statusnet:' . $base_key . ':' . $extra;
+    return Cache::key($extra);
 }
 
 function common_keyize($str)
 {
-    $str = strtolower($str);
-    $str = preg_replace('/\s/', '_', $str);
-    return $str;
+    return Cache::keyize($str);
 }
 
 function common_memcache()
 {
-    static $cache = null;
-    if (!common_config('memcached', 'enabled')) {
-        return null;
-    } else {
-        if (!$cache) {
-            $cache = new Memcache();
-            $servers = common_config('memcached', 'server');
-            if (is_array($servers)) {
-                foreach($servers as $server) {
-                    $cache->addServer($server);
-                }
-            } else {
-                $cache->addServer($servers);
-            }
-        }
-        return $cache;
-    }
+    return Cache::instance();
 }
 
 function common_license_terms($uri)
@@ -1544,3 +1573,56 @@ function common_client_ip()
 
     return array($proxy, $ip);
 }
+
+function common_url_to_nickname($url)
+{
+    static $bad = array('query', 'user', 'password', 'port', 'fragment');
+
+    $parts = parse_url($url);
+
+    # If any of these parts exist, this won't work
+
+    foreach ($bad as $badpart) {
+        if (array_key_exists($badpart, $parts)) {
+            return null;
+        }
+    }
+
+    # We just have host and/or path
+
+    # If it's just a host...
+    if (array_key_exists('host', $parts) &&
+        (!array_key_exists('path', $parts) || strcmp($parts['path'], '/') == 0))
+    {
+        $hostparts = explode('.', $parts['host']);
+
+        # Try to catch common idiom of nickname.service.tld
+
+        if ((count($hostparts) > 2) &&
+            (strlen($hostparts[count($hostparts) - 2]) > 3) && # try to skip .co.uk, .com.au
+            (strcmp($hostparts[0], 'www') != 0))
+        {
+            return common_nicknamize($hostparts[0]);
+        } else {
+            # Do the whole hostname
+            return common_nicknamize($parts['host']);
+        }
+    } else {
+        if (array_key_exists('path', $parts)) {
+            # Strip starting, ending slashes
+            $path = preg_replace('@/$@', '', $parts['path']);
+            $path = preg_replace('@^/@', '', $path);
+            if (strpos($path, '/') === false) {
+                return common_nicknamize($path);
+            }
+        }
+    }
+
+    return null;
+}
+
+function common_nicknamize($str)
+{
+    $str = preg_replace('/\W/', '', $str);
+    return strtolower($str);
+}