]> git.mxchange.org Git - quix0rs-gnu-social.git/blobdiff - classes/File.php
Merge remote-tracking branch 'upstream/master' into social-master
[quix0rs-gnu-social.git] / classes / File.php
index fadbda1f9bb008ccd90c39373a508ff47173a38b..dbd9a3bb449f71ae79baf79198c7fa8029eea0d2 100644 (file)
@@ -31,10 +31,10 @@ class File extends Managed_DataObject
     public $filehash;                        // varchar(64)     indexed
     public $mimetype;                        // varchar(50)
     public $size;                            // int(4)
-    public $title;                           // varchar(191)   not 255 because utf8mb4 takes more space
+    public $title;                           // text()
     public $date;                            // int(4)
     public $protected;                       // int(4)
-    public $filename;                        // varchar(191)   not 255 because utf8mb4 takes more space
+    public $filename;                        // text()
     public $width;                           // int(4)
     public $height;                          // int(4)
     public $modified;                        // timestamp()   not_null default_CURRENT_TIMESTAMP
@@ -52,10 +52,10 @@ class File extends Managed_DataObject
                 'filehash' => array('type' => 'varchar', 'length' => 64, 'not null' => false, 'description' => 'sha256 of the file contents, only for locally stored files of course'),
                 'mimetype' => array('type' => 'varchar', 'length' => 50, 'description' => 'mime type of resource'),
                 'size' => array('type' => 'int', 'description' => 'size of resource when available'),
-                'title' => array('type' => 'varchar', 'length' => 191, 'description' => 'title of resource when available'),
+                'title' => array('type' => 'text', 'description' => 'title of resource when available'),
                 'date' => array('type' => 'int', 'description' => 'date of resource according to http query'),
                 'protected' => array('type' => 'int', 'description' => 'true when URL is private (needs login)'),
-                'filename' => array('type' => 'varchar', 'length' => 191, 'description' => 'if a local file, name of the file'),
+                'filename' => array('type' => 'text', 'description' => 'if file is stored locally (too) this is the filename'),
                 'width' => array('type' => 'int', 'description' => 'width in pixels, if it can be described as such and data is available'),
                 'height' => array('type' => 'int', 'description' => 'height in pixels, if it can be described as such and data is available'),
 
@@ -71,8 +71,20 @@ class File extends Managed_DataObject
         );
     }
 
-    function isProtected($url) {
-        return 'http://www.facebook.com/login.php' === $url;
+    public static function isProtected($url) {
+
+               $protected_urls_exps = array(
+                       'https://www.facebook.com/login.php',
+               common_path('main/login')
+               );
+
+               foreach ($protected_urls_exps as $protected_url_exp) {
+                       if (preg_match('!^'.preg_quote($protected_url_exp).'(.*)$!i', $url) === 1) {
+                               return true;
+                       }
+               }
+
+               return false;
     }
 
     /**
@@ -150,18 +162,6 @@ class File extends Managed_DataObject
         $redir = File_redirection::where($given_url);
         $file = $redir->getFile();
 
-        // If we still don't have a File object, let's create one now!
-        if (empty($file->id)) {
-            if ($redir->url === $given_url || !$followRedirects) {
-                // Save the File object based on our lookup trace
-                $file->saveFile();
-            } else {
-                $file->saveFile();
-                $redir->file_id = $file->id;
-                $redir->insert();
-            }
-        }
-
         if (!$file instanceof File || empty($file->id)) {
             // This should not happen
             throw new ServerException('URL processing failed without new File object');
@@ -264,9 +264,13 @@ class File extends Managed_DataObject
             $ext = common_supported_mime_to_ext($mimetype);
         } catch (Exception $e) {
             // We don't support this mimetype, but let's guess the extension
-            $ext = substr(strrchr($mimetype, '/'), 1);
+            $matches = array();
+            if (!preg_match('/\/([a-z0-9]+)/', mb_strtolower($mimetype), $matches)) {
+                throw new Exception('Malformed mimetype: '.$mimetype);
+            }
+            $ext = $matches[1];
         }
-        return strtolower($ext);
+        return mb_strtolower($ext);
     }
 
     /**
@@ -356,28 +360,48 @@ class File extends Managed_DataObject
         return $protocol.'://'.$server.$path.$filename;
     }
 
+    static $_enclosures = array();
+
     function getEnclosure(){
+        if (isset(self::$_enclosures[$this->getID()])) {
+            return self::$_enclosures[$this->getID()];
+        }
+
         $enclosure = (object) array();
-        foreach (array('title', 'url', 'date', 'modified', 'size', 'mimetype') as $key) {
-            $enclosure->$key = $this->$key;
+        foreach (array('title', 'url', 'date', 'modified', 'size', 'mimetype', 'width', 'height') as $key) {
+            if ($this->$key !== '') {
+                $enclosure->$key = $this->$key;
+            }
         }
 
-        $needMoreMetadataMimetypes = array(null, 'application/xhtml+xml');
+        $needMoreMetadataMimetypes = array(null, 'application/xhtml+xml', 'text/html');
 
         if (!isset($this->filename) && in_array(common_bare_mime($enclosure->mimetype), $needMoreMetadataMimetypes)) {
             // This fetches enclosure metadata for non-local links with unset/HTML mimetypes,
             // which may be enriched through oEmbed or similar (implemented as plugins)
             Event::handle('FileEnclosureMetadata', array($this, &$enclosure));
         }
-        if (empty($enclosure->mimetype) || in_array(common_bare_mime($enclosure->mimetype), $needMoreMetadataMimetypes)) {
+        if (empty($enclosure->mimetype)) {
             // This means we either don't know what it is, so it can't
             // be shown as an enclosure, or it is an HTML link which
             // does not link to a resource with further metadata.
             throw new ServerException('Unknown enclosure mimetype, not enough metadata');
         }
+
+        self::$_enclosures[$this->getID()] = $enclosure;
         return $enclosure;
     }
 
+    public function hasThumbnail()
+    {
+        try {
+            $this->getThumbnail();
+        } catch (Exception $e) {
+            return false;
+        }
+        return true;
+    }
+
     /**
      * Get the attachment's thumbnail record, if any.
      * Make sure you supply proper 'int' typed variables (or null).
@@ -392,7 +416,7 @@ class File extends Managed_DataObject
      * @throws UnsupportedMediaException    if, despite trying, we can't understand how to make a thumbnail for this format
      * @throws ServerException              on various other errors
      */
-    public function getThumbnail($width=null, $height=null, $crop=false, $force_still=true)
+    public function getThumbnail($width=null, $height=null, $crop=false, $force_still=true, $upscale=null)
     {
         // Get some more information about this file through our ImageFile class
         $image = ImageFile::fromFileObject($this);
@@ -404,7 +428,11 @@ class File extends Managed_DataObject
             }
         }
 
-        return $image->getFileThumbnail($width, $height, $crop);
+        // Debug log (convert crop to int to have TRUE being displayed as 1 and FALSE as 0)
+        /* NOISY-DEBUG: */ common_debug('[' . __METHOD__ . ':' . __LINE__ . ']: width=' . $width . ',height=' . $height . ',crop=' . intval($crop) . ',upscale=' . intval($upscale));
+
+        return $image->getFileThumbnail($width, $height, $crop,
+                                        !is_null($upscale) ? $upscale : common_config('thumbnail', 'upscale'));
     }
 
     public function getPath()
@@ -416,17 +444,11 @@ class File extends Managed_DataObject
         return $filepath;
     }
 
-    public function getUrl()
+    public function getUrl($prefer_local=true)
     {
-        if (!empty($this->filename)) {
+        if ($prefer_local && !empty($this->filename)) {
             // A locally stored file, so let's generate a URL for our instance.
-            $url = self::url($this->filename);
-            if (self::hashurl($url) !== $this->urlhash) {
-                // For indexing purposes, in case we do a lookup on the 'url' field.
-                // also we're fixing possible changes from http to https, or paths
-                $this->updateUrl($url);
-            }
-            return $url;
+            return self::url($this->filename);
         }
 
         // No local filename available, return the URL we have stored