]> git.mxchange.org Git - quix0rs-gnu-social.git/blobdiff - classes/File.php
Merge branch 'merge-requests/29' into social-master
[quix0rs-gnu-social.git] / classes / File.php
index acc90d495f7920cdec132ad17ec8c792df4bb5ce..9bb22e502b6203297980c2c93769b357954820fa 100644 (file)
@@ -245,7 +245,7 @@ class File extends Managed_DataObject
         }
 
         // Normalize and make the original filename more URL friendly.
-        $origname = basename($origname, $ext);
+        $origname = basename($origname, ".$ext");
         if (class_exists('Normalizer')) {
             // http://php.net/manual/en/class.normalizer.php
             // http://www.unicode.org/reports/tr15/
@@ -253,7 +253,7 @@ class File extends Managed_DataObject
         }
         $origname = preg_replace('/[^A-Za-z0-9\.\_]/', '_', $origname);
 
-        $nickname = $profile->nickname;
+        $nickname = $profile->getNickname();
         $datestamp = strftime('%Y%m%d', time());
         do {
             // generate new random strings until we don't run into a filename collision.
@@ -303,7 +303,7 @@ class File extends Managed_DataObject
 
         }
 
-        if (StatusNet::isHTTPS()) {
+        if (StatusNet::useHTTPS()) {
 
             $sslserver = common_config('attachments', 'sslserver');
 
@@ -352,30 +352,21 @@ class File extends Managed_DataObject
 
     function getEnclosure(){
         $enclosure = (object) array();
-        $enclosure->title=$this->title;
-        $enclosure->url=$this->url;
-        $enclosure->title=$this->title;
-        $enclosure->date=$this->date;
-        $enclosure->modified=$this->modified;
-        $enclosure->size=$this->size;
-        $enclosure->mimetype=$this->mimetype;
-
-        if (!isset($this->filename)) {
-            $notEnclosureMimeTypes = array(null,'text/html','application/xhtml+xml');
-            $mimetype = $this->mimetype;
-            if($mimetype != null){
-                $mimetype = strtolower($this->mimetype);
-            }
-            $semicolon = strpos($mimetype,';');
-            if($semicolon){
-                $mimetype = substr($mimetype,0,$semicolon);
-            }
-            if (in_array($mimetype, $notEnclosureMimeTypes)) {
-                Event::handle('FileEnclosureMetadata', array($this, &$enclosure));
-            }
+        foreach (array('title', 'url', 'date', 'modified', 'size', 'mimetype') as $key) {
+            $enclosure->$key = $this->$key;
+        }
+
+        $needMoreMetadataMimetypes = array(null, 'application/xhtml+xml');
+
+        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)) {
-            // This means we don't know what it is, so it can't be an enclosure!
+        if (empty($enclosure->mimetype) || in_array(common_bare_mime($enclosure->mimetype), $needMoreMetadataMimetypes)) {
+            // 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');
         }
         return $enclosure;
@@ -400,17 +391,20 @@ class File extends Managed_DataObject
             throw new UnsupportedMediaException('No image geometry available.');
         }
 
-        if ($width === null) {
+        if ($width === null || $width < 1) {
             $width = common_config('thumbnail', 'width');
             $height = common_config('thumbnail', 'height');
             $crop = common_config('thumbnail', 'crop');
         }
 
-        if ($height === null) {
+        if ($height === null || $height < 1) {
             $height = $width;
             $crop = true;
         }
 
+        // Debug log (convert crop to int to have TRUE being displayed as 1 and FALSE as 0)
+        common_debug('[' . __METHOD__ . ':' . __LINE__ . ']: width=' . $width . ',height=' . $height . ',crop=' . intval($crop));
+
         // Get proper aspect ratio width and height before lookup
         // We have to do it through an ImageFile object because of orientation etc.
         // Only other solution would've been to rotate + rewrite uploaded files.
@@ -442,7 +436,19 @@ class File extends Managed_DataObject
                 || $box['h'] < 1 || $box['y'] >= $this->height) {
             // Fail on bad width parameter. If this occurs, it's due to algorithm in ImageFile->scaleToFit
             common_debug("Boundary box parameters for resize of {$this->filepath} : ".var_export($box,true));
-            throw new ServerException('Bad thumbnail size parameters.');
+            throw new ServerException('Bad thumbnail size parameters. maxsize=' .
+                common_config('thumbnail', 'maxsize') .
+                ',box[width]=' . $box['width'] .
+                ',box[height]=' . $box['height'] .
+                ',box[w]=' . $box['w'] .
+                ',box[h]=' . $box['h'] .
+                ',box[x]=' . $box['x'] .
+                ',box[y]=' . $box['y'] .
+                ',this->width=' . $this->width .
+                ',this->heigh=' . $this->height .
+                ',this->filepath=' . $this->filepath .
+                ',this->filename=' . $this->filename
+            );
         }
 
         // Perform resize and store into file
@@ -462,11 +468,42 @@ class File extends Managed_DataObject
     {
         return self::path($this->filename);
     }
+
     public function getUrl()
     {
+        if (!empty($this->filename)) {
+            // A locally stored file, so let's generate a URL for our instance.
+            $url = self::url($this->filename);
+            if ($url != $this->url) {
+                // 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;
+        }
+
+        // No local filename available, return the URL we have stored
         return $this->url;
     }
 
+    public function updateUrl($url)
+    {
+        $file = File::getKV('url', $url);
+        if ($file instanceof File) {
+            throw new ServerException('URL already exists in DB');
+        }
+        $sql = 'UPDATE %1$s SET url=%2$s WHERE url=%3$s;';
+        $result = $this->query(sprintf($sql, $this->__table,
+                                             $this->_quote((string)$url),
+                                             $this->_quote((string)$this->url)));
+        if ($result === false) {
+            common_log_db_error($this, 'UPDATE', __FILE__);
+            throw new ServerException("Could not UPDATE {$this->__table}.url");
+        }
+
+        return $result;
+    }
+
     /**
      * Blow the cache of notices that link to this URL
      *
@@ -550,4 +587,11 @@ class File extends Managed_DataObject
         // And finally remove the entry from the database
         return parent::delete($useWhere);
     }
+
+    public function getTitle()
+    {
+        $title = $this->title ?: $this->filename;
+
+        return $title ?: null;
+    }
 }