]> git.mxchange.org Git - quix0rs-gnu-social.git/blobdiff - lib/util.php
Merge remote-tracking branch 'upstream/master' into social-master
[quix0rs-gnu-social.git] / lib / util.php
index 64af25f059f7ffdd9806305c57aa2238727a3b1d..e07dc47f409b0c06610c180250146a2800b0b98b 100644 (file)
@@ -264,6 +264,11 @@ function common_logged_in()
     return (!is_null(common_current_user()));
 }
 
+function common_local_referer()
+{
+    return parse_url($_SERVER['HTTP_REFERER'], PHP_URL_HOST) === common_config('site', 'server');
+}
+
 function common_have_session()
 {
     return (0 != strcmp(session_id(), ''));
@@ -321,7 +326,7 @@ function common_set_user($user)
         return false;
     }
 
-    if ($user) {
+    if ($user instanceof User) {
         if (Event::handle('StartSetUser', array(&$user))) {
             if (!empty($user)) {
                 if (!$user->hasRight(Right::WEBLOGIN)) {
@@ -538,7 +543,7 @@ function common_user_cache_hash($user=false)
     if ($user === false) {
         $user = common_current_user();
     }
-    if ($user) {
+    if ($user instanceof User) {
         return crc32($user->id . ':' . $user->nickname);
     } else {
         return '0';
@@ -1363,7 +1368,7 @@ function common_local_url($action, $args=null, $params=null, $fragment=null, $ad
 
 function common_path($relative, $ssl=false, $addSession=true)
 {
-    $pathpart = (common_config('site', 'path')) ? common_config('site', 'path')."/" : '';
+    $pathpart = (!empty(common_config('site', 'path'))) ? common_config('site', 'path') . '/' : '';
 
     if ($ssl && GNUsocial::useHTTPS()) {
         $proto = 'https';
@@ -1428,6 +1433,37 @@ function common_fake_local_fancy_url($url)
     return $fancy_url;
 }
 
+// FIXME: Maybe this should also be able to handle non-fancy URLs with index.php?p=...
+function common_fake_local_nonfancy_url($url)
+{
+    /**
+     * This is a hacky fix to make URIs NOT generated with "index.php/" match against
+     * locally stored URIs WITH that. The reverse from the above.
+     *
+     * It will also "repair" index.php URLs with multiple / prepended. Like https://some.example///index.php/user/1
+     */
+    if (!preg_match(
+                // [1] protocol part, we can only rewrite http/https anyway.
+                '/^(https?:\/\/)' .
+                // [2] site name.
+                // FIXME: Dunno how this acts if we're aliasing ourselves with a .onion domain etc.
+                '('.preg_quote(common_config('site', 'server'), '/').')' .
+                // [3] site path, or if that is empty just '/' (to retain the /)
+                '('.preg_quote(common_config('site', 'path') ?: '/', '/').')' .
+                // [4] should be empty (might contain one or more / and then maybe also index.php). Will be overwritten.
+                // [5] will have the extracted actual URL part (besides site path)
+                '((?!index.php\/)\/*(?:index.php\/)?)(.*)$/', $url, $matches)) {
+        // if preg_match failed to match
+        throw new Exception('No known change could be made to the URL.');
+    }
+
+    $matches[4] = 'index.php/'; // inject the index.php/ rewritethingy
+
+    // remove the first element, which is the full matching string
+    array_shift($matches);
+    return implode($matches);
+}
+
 function common_inject_session($url, $serverpart = null)
 {
     if (!common_have_session()) {
@@ -2474,7 +2510,7 @@ function common_perf_counter($key, $val=null)
                 $_perfCounters[$key] = array($val);
             }
             if (common_config('site', 'logperf_detail')) {
-                common_log(LOG_DEBUG, "PERF COUNTER HIT: $key $val");
+                common_debug("PERF COUNTER HIT: $key $val");
             }
         }
     }
@@ -2488,14 +2524,14 @@ function common_log_perf_counters()
         if (isset($_startTime)) {
             $endTime = microtime(true);
             $diff = round(($endTime - $_startTime) * 1000);
-            common_log(LOG_DEBUG, "PERF runtime: ${diff}ms");
+            common_debug("PERF runtime: ${diff}ms");
         }
         $counters = $_perfCounters;
         ksort($counters);
         foreach ($counters as $key => $values) {
             $count = count($values);
             $unique = count(array_unique($values));
-            common_log(LOG_DEBUG, "PERF COUNTER: $key $count ($unique unique)");
+            common_debug("PERF COUNTER: $key $count ($unique unique)");
         }
     }
 }
@@ -2551,6 +2587,38 @@ function html_sprintf()
     return call_user_func_array('sprintf', $args);
 }
 
+function common_location_shared()
+{
+    // Get default setting
+    $cfg = common_config('location', 'share');
+
+    if ($cfg == 'always') {
+        // Always enabled
+        return true;
+    } elseif ($cfg == 'never') {
+        // Never enabled
+        return false;
+    }
+
+    // Is the user logged-in?
+    if (common_logged_in()) {
+        // Get current user
+        $user = common_current_user();
+
+        // Is it there?
+        if ($user instanceof User) {
+            // Yes, get profile
+            $profile = $user->getProfile();
+
+            // Get it from the profile
+            return $profile->shareLocation();
+        }
+    }
+
+    // User is not logged in, get default
+    return common_config('location', 'sharedefault');
+}
+
 function _ve($var)
 {
     return var_export($var, true);