]> git.mxchange.org Git - quix0rs-gnu-social.git/blobdiff - lib/util.php
Merge branch '0.9.x' of git@gitorious.org:statusnet/mainline into 0.9.x
[quix0rs-gnu-social.git] / lib / util.php
index d4afafb4c0a277fa47fce240e1328fe78e3142d5..a4281121ebd4d05d936b273630f7ea590dd1933f 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');
@@ -91,8 +91,16 @@ function common_language()
     if (_have_config() && common_logged_in()) {
         $user = common_current_user();
         $user_language = $user->language;
-        if ($user_language)
-          return $user_language;
+
+        if ($user->language) {
+            // Validate -- we don't want to end up with a bogus code
+            // left over from some old junk.
+            foreach (common_config('site', 'languages') as $code => $info) {
+                if ($info['lang'] == $user_language) {
+                    return $user_language;
+                }
+            }
+        }
     }
 
     // Otherwise, find the best match for the languages requested by the
@@ -111,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);
 }
 
@@ -900,6 +913,26 @@ function common_sql_date($datetime)
     return strftime('%Y-%m-%d %H:%M:%S', $datetime);
 }
 
+/**
+ * Return an SQL fragment to calculate an age-based weight from a given
+ * timestamp or datetime column.
+ *
+ * @param string $column name of field we're comparing against current time
+ * @param integer $dropoff divisor for age in seconds before exponentiation
+ * @return string SQL fragment
+ */
+function common_sql_weight($column, $dropoff)
+{
+    if (common_config('db', 'type') == 'pgsql') {
+        // PostgreSQL doesn't support timestampdiff function.
+        // @fixme will this use the right time zone?
+        // @fixme does this handle cross-year subtraction correctly?
+        return "sum(exp(-extract(epoch from (now() - $column)) / $dropoff))";
+    } else {
+        return "sum(exp(timestampdiff(second, utc_timestamp(), $column) / $dropoff))";
+    }
+}
+
 function common_redirect($url, $code=307)
 {
     static $status = array(301 => "Moved Permanently",
@@ -1244,8 +1277,12 @@ function common_copy_args($from)
     return $to;
 }
 
-// Neutralise the evil effects of magic_quotes_gpc in the current request.
-// This is used before handing a request off to OAuthRequest::from_request.
+/**
+ * Neutralise the evil effects of magic_quotes_gpc in the current request.
+ * This is used before handing a request off to OAuthRequest::from_request.
+ * @fixme Doesn't consider vars other than _POST and _GET?
+ * @fixme Can't be undone and could corrupt data if run twice.
+ */
 function common_remove_magic_from_request()
 {
     if(get_magic_quotes_gpc()) {
@@ -1372,41 +1409,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)
@@ -1447,6 +1460,17 @@ function common_database_tablename($tablename)
   return $tablename;
 }
 
+/**
+ * Shorten a URL with the current user's configured shortening service,
+ * or ur1.ca if configured, or not at all if no shortening is set up.
+ * Length is not considered.
+ *
+ * @param string $long_url
+ * @return string may return the original URL if shortening failed
+ *
+ * @fixme provide a way to specify a particular shortener
+ * @fixme provide a way to specify to use a given user's shortening preferences
+ */
 function common_shorten_url($long_url)
 {
     $user = common_current_user();
@@ -1467,6 +1491,16 @@ function common_shorten_url($long_url)
     }
 }
 
+/**
+ * @return mixed array($proxy, $ip) for web requests; proxy may be null
+ *               null if not a web request
+ *
+ * @fixme X-Forwarded-For can be chained by multiple proxies;
+          we should parse the list and provide a cleaner array
+ * @fixme X-Forwarded-For can be forged by clients; only use them if trusted
+ * @fixme X_Forwarded_For headers will override X-Forwarded-For read through $_SERVER;
+ *        use function to get exact request headers from Apache if possible.
+ */
 function common_client_ip()
 {
     if (!isset($_SERVER) || !array_key_exists('REQUEST_METHOD', $_SERVER)) {