]> git.mxchange.org Git - quix0rs-gnu-social.git/blobdiff - classes/File_redirection.php
Fix reversed poll & poll response object types in ActivityStreams output
[quix0rs-gnu-social.git] / classes / File_redirection.php
index 1976e3439cf0525bca9ab4aaa284d97c682cb8ca..1096f500bfcc701366eb3eb6cb8a34ea40a07824 100644 (file)
@@ -139,6 +139,7 @@ class File_redirection extends Memcached_DataObject
      * reached.
      *
      * @param string $in_url
+     * @param boolean $discover true to attempt dereferencing the redirect if we don't know it already
      * @return mixed one of:
      *         string - target URL, if this is a direct link or a known redirect
      *         array - redirect info if this is an *unknown* redirect:
@@ -150,7 +151,7 @@ class File_redirection extends Memcached_DataObject
      *                size (optional): byte size from Content-Length header
      *                time (optional): timestamp from Last-Modified header
      */
-    public function where($in_url) {
+    public function where($in_url, $discover=true) {
         // let's see if we know this...
         $a = File::staticGet('url', $in_url);
 
@@ -166,8 +167,42 @@ class File_redirection extends Memcached_DataObject
             }
         }
 
-        $ret = File_redirection::lookupWhere($in_url);
-        return $ret;
+        if ($discover) {
+            $ret = File_redirection::lookupWhere($in_url);
+            return $ret;
+        } else {
+            // No manual dereferencing; leave the unknown URL as is.
+            return $in_url;
+        }
+    }
+
+    /**
+     * Shorten a URL with the current user's configured shortening
+     * options, if applicable.
+     *
+     * If it cannot be shortened or the "short" URL is longer than the
+     * original, the original is returned.
+     *
+     * If the referenced item has not been seen before, embedding data
+     * may be saved.
+     *
+     * @param string $long_url
+     * @param User $user whose shortening options to use; defaults to the current web session user
+     * @return string
+     */
+    function makeShort($long_url, $user=null)
+    {
+        $canon = File_redirection::_canonUrl($long_url);
+
+        $short_url = File_redirection::_userMakeShort($canon, $user);
+
+        // Did we get one? Is it shorter?
+
+        if (!empty($short_url)) {
+            return $short_url;
+        } else {
+            return $long_url;
+        }
     }
 
     /**
@@ -183,22 +218,23 @@ class File_redirection extends Memcached_DataObject
      * @param string $long_url
      * @return string
      */
-    function makeShort($long_url) {
 
+    function forceShort($long_url, $user)
+    {
         $canon = File_redirection::_canonUrl($long_url);
 
-        $short_url = File_redirection::_userMakeShort($canon);
+        $short_url = File_redirection::_userMakeShort($canon, $user, true);
 
         // Did we get one? Is it shorter?
-        if (!empty($short_url) && mb_strlen($short_url) < mb_strlen($long_url)) {
+        if (!empty($short_url)) {
             return $short_url;
         } else {
             return $long_url;
         }
     }
 
-    function _userMakeShort($long_url) {
-        $short_url = common_shorten_url($long_url);
+    function _userMakeShort($long_url, User $user=null, $force = false) {
+        $short_url = common_shorten_url($long_url, $user, $force);
         if (!empty($short_url) && $short_url != $long_url) {
             $short_url = (string)$short_url;
             // store it