File.php 24.1 KB
Newer Older
1 2
<?php
/*
3
 * StatusNet - the distributed open-source microblogging tool
4
 * Copyright (C) 2008, 2009, StatusNet, Inc.
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.     See the
 * GNU Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program.     If not, see <http://www.gnu.org/licenses/>.
 */

20
if (!defined('GNUSOCIAL')) { exit(1); }
21 22 23 24

/**
 * Table Definition for file
 */
25
class File extends Managed_DataObject
26 27
{
    public $__table = 'file';                            // table name
28
    public $id;                              // int(4)  primary_key not_null
29
    public $url;                             // varchar(191)  unique_key   not 255 because utf8mb4 takes more space
30 31
    public $mimetype;                        // varchar(50)
    public $size;                            // int(4)
32
    public $title;                           // varchar(191)   not 255 because utf8mb4 takes more space
33 34
    public $date;                            // int(4)
    public $protected;                       // int(4)
35
    public $filename;                        // varchar(191)   not 255 because utf8mb4 takes more space
36 37
    public $width;                           // int(4)
    public $height;                          // int(4)
38
    public $modified;                        // timestamp()   not_null default_CURRENT_TIMESTAMP
39

40 41 42 43 44
    public static function schemaDef()
    {
        return array(
            'fields' => array(
                'id' => array('type' => 'serial', 'not null' => true),
45
                'url' => array('type' => 'varchar', 'length' => 191, 'description' => 'destination URL after following redirections'),
46 47
                'mimetype' => array('type' => 'varchar', 'length' => 50, 'description' => 'mime type of resource'),
                'size' => array('type' => 'int', 'description' => 'size of resource when available'),
48
                'title' => array('type' => 'varchar', 'length' => 191, 'description' => 'title of resource when available'),
49 50
                'date' => array('type' => 'int', 'description' => 'date of resource according to http query'),
                'protected' => array('type' => 'int', 'description' => 'true when URL is private (needs login)'),
51
                'filename' => array('type' => 'varchar', 'length' => 191, 'description' => 'if a local file, name of the file'),
52 53
                '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'),
54 55 56 57 58 59 60 61 62 63

                'modified' => array('type' => 'timestamp', 'not null' => true, 'description' => 'date this record was modified'),
            ),
            'primary key' => array('id'),
            'unique keys' => array(
                'file_url_key' => array('url'),
            ),
        );
    }

64 65 66 67
    function isProtected($url) {
        return 'http://www.facebook.com/login.php' === $url;
    }

68 69 70 71 72 73 74
    /**
     * Save a new file record.
     *
     * @param array $redir_data lookup data eg from File_redirection::where()
     * @param string $given_url
     * @return File
     */
75
    public static function saveNew(array $redir_data, $given_url) {
76 77 78 79

        // I don't know why we have to keep doing this but I'm adding this last check to avoid
        // uniqueness bugs.

80
        $file = File::getKV('url', $given_url);
81
        
82 83 84 85 86 87 88 89 90
        if (!$file instanceof File) {
            $file = new File;
            $file->url = $given_url;
            if (!empty($redir_data['protected'])) $file->protected = $redir_data['protected'];
            if (!empty($redir_data['title'])) $file->title = $redir_data['title'];
            if (!empty($redir_data['type'])) $file->mimetype = $redir_data['type'];
            if (!empty($redir_data['size'])) $file->size = intval($redir_data['size']);
            if (isset($redir_data['time']) && $redir_data['time'] > 0) $file->date = intval($redir_data['time']);
            $file_id = $file->insert();
91
        }
92

93
        Event::handle('EndFileSaveNew', array($file, $redir_data, $given_url));
94
        assert ($file instanceof File);
95
        return $file;
96 97
    }

98
    /**
99 100 101 102 103 104 105
     * Go look at a URL and possibly save data about it if it's new:
     * - follow redirect chains and store them in file_redirection
     * - if a thumbnail is available, save it in file_thumbnail
     * - save file record with basic info
     * - optionally save a file_to_post record
     * - return the File object with the full reference
     *
106
     * @fixme refactor this mess, it's gotten pretty scary.
107 108 109 110 111 112
     * @param string $given_url the URL we're looking at
     * @param int $notice_id (optional)
     * @param bool $followRedirects defaults to true
     *
     * @return mixed File on success, -1 on some errors
     *
113
     * @throws ServerException on failure
114
     */
115 116 117 118 119
    public static function processNew($given_url, $notice_id=null, $followRedirects=true) {
        if (empty($given_url)) {
            throw new ServerException('No given URL to process');
        }

120
        $given_url = File_redirection::_canonUrl($given_url);
121 122 123 124
        if (empty($given_url)) {
            throw new ServerException('No canonical URL from given URL to process');
        }

125
        $file = File::getKV('url', $given_url);
126 127
        if (!$file instanceof File) {
            // First check if we have a lookup trace for this URL already
128
            $file_redir = File_redirection::getKV('url', $given_url);
129 130 131 132 133 134 135 136 137 138
            if ($file_redir instanceof File_redirection) {
                $file = File::getKV('id', $file_redir->file_id);
                if (!$file instanceof File) {
                    // File did not exist, let's clean up the File_redirection entry
                    $file_redir->delete();
                }
            }

            // If we still don't have a File object, let's create one now!
            if (!$file instanceof File) {
139 140 141 142
                // @fixme for new URLs this also looks up non-redirect data
                // such as target content type, size, etc, which we need
                // for File::saveNew(); so we call it even if not following
                // new redirects.
143
                $redir_data = File_redirection::where($given_url);
144 145 146 147
                if (is_array($redir_data)) {
                    $redir_url = $redir_data['url'];
                } elseif (is_string($redir_data)) {
                    $redir_url = $redir_data;
148
                    $redir_data = array();
149
                } else {
150
                    // TRANS: Server exception thrown when a URL cannot be processed.
151
                    throw new ServerException(sprintf(_("Cannot process URL '%s'"), $given_url));
152
                }
153

154
                // TODO: max field length
155
                if ($redir_url === $given_url || strlen($redir_url) > 191 || !$followRedirects) {
156 157
                    // Save the File object based on our lookup trace
                    $file = File::saveNew($redir_data, $given_url);
158
                } else {
159 160 161 162 163 164 165 166
                    // This seems kind of messed up... for now skipping this part
                    // if we're already under a redirect, so we don't go into
                    // horrible infinite loops if we've been given an unstable
                    // redirect (where the final destination of the first request
                    // doesn't match what we get when we ask for it again).
                    //
                    // Seen in the wild with clojure.org, which redirects through
                    // wikispaces for auth and appends session data in the URL params.
167 168
                    $file = self::processNew($redir_url, $notice_id, /*followRedirects*/false);
                    File_redirection::saveNew($redir_data, $file->id, $given_url);
169 170 171
                }
            }

172 173 174 175 176
            if (!$file instanceof File) {
                // This should only happen if File::saveNew somehow did not return a File object,
                // though we have an assert for that in case the event there might've gone wrong.
                // If anything else goes wrong, there should've been an exception thrown.
                throw new ServerException('URL processing failed without new File object');
177
            }
178
        }
179

180
        if (!empty($notice_id)) {
181
            File_to_post::processNew($file->id, $notice_id);
182
        }
183
        return $file;
184
    }
185

186
    public static function respectsQuota(Profile $scoped, $fileSize) {
187
        if ($fileSize > common_config('attachments', 'file_quota')) {
188 189 190 191 192 193
            // TRANS: Message used to be inserted as %2$s in  the text "No file may
            // TRANS: be larger than %1$d byte and the file you sent was %2$s.".
            // TRANS: %1$d is the number of bytes of an uploaded file.
            $fileSizeText = sprintf(_m('%1$d byte','%1$d bytes',$fileSize),$fileSize);

            $fileQuota = common_config('attachments', 'file_quota');
194
            // TRANS: Message given if an upload is larger than the configured maximum.
195 196 197
            // TRANS: %1$d (used for plural) is the byte limit for uploads,
            // TRANS: %2$s is the proper form of "n bytes". This is the only ways to have
            // TRANS: gettext support multiple plurals in the same message, unfortunately...
198 199
            throw new ClientException(
                    sprintf(_m('No file may be larger than %1$d byte and the file you sent was %2$s. Try to upload a smaller version.',
200 201
                              'No file may be larger than %1$d bytes and the file you sent was %2$s. Try to upload a smaller version.',
                              $fileQuota),
202
                    $fileQuota, $fileSizeText));
203 204
        }

205 206 207 208 209 210
        $file = new File;

        $query = "select sum(size) as total from file join file_to_post on file_to_post.file_id = file.id join notice on file_to_post.post_id = notice.id where profile_id = {$scoped->id} and file.url like '%/notice/%/file'";
        $file->query($query);
        $file->fetch();
        $total = $file->total + $fileSize;
211
        if ($total > common_config('attachments', 'user_quota')) {
212
            // TRANS: Message given if an upload would exceed user quota.
Siebrand Mazeland's avatar
Siebrand Mazeland committed
213
            // TRANS: %d (number) is the user quota in bytes and is used for plural.
214 215
            throw new ClientException(
                    sprintf(_m('A file this large would exceed your user quota of %d byte.',
Siebrand Mazeland's avatar
Siebrand Mazeland committed
216 217
                              'A file this large would exceed your user quota of %d bytes.',
                              common_config('attachments', 'user_quota')),
218
                    common_config('attachments', 'user_quota')));
219
        }
220
        $query .= ' AND EXTRACT(month FROM file.modified) = EXTRACT(month FROM now()) and EXTRACT(year FROM file.modified) = EXTRACT(year FROM now())';
221 222 223
        $file->query($query);
        $file->fetch();
        $total = $file->total + $fileSize;
224
        if ($total > common_config('attachments', 'monthly_quota')) {
225
            // TRANS: Message given id an upload would exceed a user's monthly quota.
Siebrand Mazeland's avatar
Siebrand Mazeland committed
226
            // TRANS: $d (number) is the monthly user quota in bytes and is used for plural.
227 228
            throw new ClientException(
                    sprintf(_m('A file this large would exceed your monthly quota of %d byte.',
Siebrand Mazeland's avatar
Siebrand Mazeland committed
229 230
                              'A file this large would exceed your monthly quota of %d bytes.',
                              common_config('attachments', 'monthly_quota')),
231
                    common_config('attachments', 'monthly_quota')));
232 233 234
        }
        return true;
    }
235 236 237

    // where should the file go?

238
    static function filename(Profile $profile, $origname, $mimetype)
239
    {
mmn's avatar
mmn committed
240 241 242 243 244
        try {
            $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);
245
        }
246

247
        // Normalize and make the original filename more URL friendly.
mmn's avatar
mmn committed
248
        $origname = basename($origname, ".$ext");
249 250 251 252 253 254 255
        if (class_exists('Normalizer')) {
            // http://php.net/manual/en/class.normalizer.php
            // http://www.unicode.org/reports/tr15/
            $origname = Normalizer::normalize($origname, Normalizer::FORM_KC);
        }
        $origname = preg_replace('/[^A-Za-z0-9\.\_]/', '_', $origname);

mmn's avatar
mmn committed
256
        $nickname = $profile->getNickname();
257 258 259 260 261 262 263
        $datestamp = strftime('%Y%m%d', time());
        do {
            // generate new random strings until we don't run into a filename collision.
            $random = strtolower(common_confirmation_code(16));
            $filename = "$nickname-$datestamp-$origname-$random.$ext";
        } while (file_exists(self::path($filename)));
        return $filename;
264
    }
265

266 267 268 269 270
    /**
     * Validation for as-saved base filenames
     */
    static function validFilename($filename)
    {
271
        return preg_match('/^[A-Za-z0-9._-]+$/', $filename);
272 273 274 275 276
    }

    /**
     * @throws ClientException on invalid filename
     */
277 278
    static function path($filename)
    {
279
        if (!self::validFilename($filename)) {
280 281
            // TRANS: Client exception thrown if a file upload does not have a valid name.
            throw new ClientException(_("Invalid filename."));
282
        }
283
        $dir = common_config('attachments', 'dir');
284

285 286 287
        if ($dir[strlen($dir)-1] != '/') {
            $dir .= '/';
        }
288

289 290
        return $dir . $filename;
    }
291

292 293
    static function url($filename)
    {
294
        if (!self::validFilename($filename)) {
295 296
            // TRANS: Client exception thrown if a file upload does not have a valid name.
            throw new ClientException(_("Invalid filename."));
297
        }
298 299

        if (common_config('site','private')) {
300

301 302
            return common_local_url('getfile',
                                array('filename' => $filename));
303

304
        }
305

306
        if (StatusNet::useHTTPS()) {
307 308

            $sslserver = common_config('attachments', 'sslserver');
309

310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325
            if (empty($sslserver)) {
                // XXX: this assumes that background dir == site dir + /file/
                // not true if there's another server
                if (is_string(common_config('site', 'sslserver')) &&
                    mb_strlen(common_config('site', 'sslserver')) > 0) {
                    $server = common_config('site', 'sslserver');
                } else if (common_config('site', 'server')) {
                    $server = common_config('site', 'server');
                }
                $path = common_config('site', 'path') . '/file/';
            } else {
                $server = $sslserver;
                $path   = common_config('attachments', 'sslpath');
                if (empty($path)) {
                    $path = common_config('attachments', 'path');
                }
326 327
            }

328 329 330
            $protocol = 'https';
        } else {
            $path = common_config('attachments', 'path');
331
            $server = common_config('attachments', 'server');
332

333 334 335
            if (empty($server)) {
                $server = common_config('site', 'server');
            }
336

Evan Prodromou's avatar
Evan Prodromou committed
337
            $ssl = common_config('attachments', 'ssl');
338

Evan Prodromou's avatar
Evan Prodromou committed
339
            $protocol = ($ssl) ? 'https' : 'http';
340
        }
Evan Prodromou's avatar
Evan Prodromou committed
341

342 343
        if ($path[strlen($path)-1] != '/') {
            $path .= '/';
344
        }
345 346 347 348 349 350

        if ($path[0] != '/') {
            $path = '/'.$path;
        }

        return $protocol.'://'.$server.$path.$filename;
351
    }
352

353 354
    function getEnclosure(){
        $enclosure = (object) array();
355 356 357 358
        foreach (array('title', 'url', 'date', 'modified', 'size', 'mimetype') as $key) {
            $enclosure->$key = $this->$key;
        }

359
        $needMoreMetadataMimetypes = array(null, 'application/xhtml+xml');
360 361 362 363 364

        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));
365
        }
366 367 368 369
        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.
370 371
            throw new ServerException('Unknown enclosure mimetype, not enough metadata');
        }
372
        return $enclosure;
373
    }
374

375 376
    /**
     * Get the attachment's thumbnail record, if any.
377
     * Make sure you supply proper 'int' typed variables (or null).
378
     *
379 380 381
     * @param $width  int   Max width of thumbnail in pixels. (if null, use common_config values)
     * @param $height int   Max height of thumbnail in pixels. (if null, square-crop to $width)
     * @param $crop   bool  Crop to the max-values' aspect ratio
382
     *
383 384
     * @return File_thumbnail
     */
385
    public function getThumbnail($width=null, $height=null, $crop=false, $force_still=true)
386
    {
387 388
        // Get some more information about this file through our ImageFile class
        $image = ImageFile::fromFileObject($this);
389 390 391 392 393 394
        if ($image->animated && !common_config('thumbnail', 'animated')) {
            // null  means "always use file as thumbnail"
            // false means you get choice between frozen frame or original when calling getThumbnail
            if (is_null(common_config('thumbnail', 'animated')) || !$force_still) {
                throw new UseFileAsThumbnailException($this->id);
            }
395 396
        }

397
        if ($width === null) {
398 399 400 401 402 403 404 405 406
            $width = common_config('thumbnail', 'width');
            $height = common_config('thumbnail', 'height');
            $crop = common_config('thumbnail', 'crop');
        }

        if ($height === null) {
            $height = $width;
            $crop = true;
        }
407

408
        // Get proper aspect ratio width and height before lookup
409 410
        // We have to do it through an ImageFile object because of orientation etc.
        // Only other solution would've been to rotate + rewrite uploaded files.
411
        list($width, $height, $x, $y, $w, $h) =
412
                                $image->scaleToFit($width, $height, $crop);
413

414 415
        $params = array('file_id'=> $this->id,
                        'width'  => $width,
416
                        'height' => $height);
417
        $thumb = File_thumbnail::pkeyGet($params);
418 419
        if ($thumb instanceof File_thumbnail) {
            return $thumb;
420
        }
421

422
        // throws exception on failure to generate thumbnail
423
        $outname = "thumb-{$width}x{$height}-" . $image->filename;
424 425
        $outpath = self::path($outname);

426 427 428 429 430 431 432 433
        // The boundary box for our resizing
        $box = array('width'=>$width, 'height'=>$height,
                     'x'=>$x,         'y'=>$y,
                     'w'=>$w,         'h'=>$h);

        // Doublecheck that parameters are sane and integers.
        if ($box['width'] < 1 || $box['width'] > common_config('thumbnail', 'maxsize')
                || $box['height'] < 1 || $box['height'] > common_config('thumbnail', 'maxsize')
434 435
                || $box['w'] < 1 || $box['x'] >= $image->width
                || $box['h'] < 1 || $box['y'] >= $image->height) {
436
            // Fail on bad width parameter. If this occurs, it's due to algorithm in ImageFile->scaleToFit
437
            common_debug("Boundary box parameters for resize of {$image->filepath} : ".var_export($box,true));
438 439 440
            throw new ServerException('Bad thumbnail size parameters.');
        }

441
        common_debug(sprintf('Generating a thumbnail of File id==%u of size %ux%u', $this->id, $width, $height));
442 443
        // Perform resize and store into file
        $image->resizeTo($outpath, $box);
444 445

        // Avoid deleting the original
446
        if ($image->getPath() != self::path($image->filename)) {
447 448 449 450
            $image->unlink();
        }
        return File_thumbnail::saveThumbnail($this->id,
                                      self::url($outname),
451 452
                                      $width, $height,
                                      $outname);
453 454
    }

455 456 457 458
    public function getPath()
    {
        return self::path($this->filename);
    }
459

460 461
    public function getUrl()
    {
462 463 464 465 466 467 468 469 470 471 472 473
        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
474 475
        return $this->url;
    }
476

477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494
    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;
    }

495 496 497 498 499 500 501 502
    /**
     * Blow the cache of notices that link to this URL
     *
     * @param boolean $last Whether to blow the "last" cache too
     *
     * @return void
     */

503 504 505 506 507 508
    function blowCache($last=false)
    {
        self::blow('file:notice-ids:%s', $this->url);
        if ($last) {
            self::blow('file:notice-ids:%s;last', $this->url);
        }
509
        self::blow('file:notice-count:%d', $this->id);
510 511 512 513 514 515 516 517 518 519 520 521 522 523 524
    }

    /**
     * Stream of notices linking to this URL
     *
     * @param integer $offset   Offset to show; default is 0
     * @param integer $limit    Limit of notices to show
     * @param integer $since_id Since this notice
     * @param integer $max_id   Before this notice
     *
     * @return array ids of notices that link to this file
     */

    function stream($offset=0, $limit=NOTICES_PER_PAGE, $since_id=0, $max_id=0)
    {
525
        $stream = new FileNoticeStream($this);
526
        return $stream->getNotices($offset, $limit, $since_id, $max_id);
527 528
    }

529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547
    function noticeCount()
    {
        $cacheKey = sprintf('file:notice-count:%d', $this->id);
        
        $count = self::cacheGet($cacheKey);

        if ($count === false) {

            $f2p = new File_to_post();

            $f2p->file_id = $this->id;

            $count = $f2p->count();

            self::cacheSet($cacheKey, $count);
        } 

        return $count;
    }
548 549 550 551 552

    public function isLocal()
    {
        return !empty($this->filename);
    }
553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577

    public function delete($useWhere=false)
    {
        // Delete the file, if it exists locally
        if (!empty($this->filename) && file_exists(self::path($this->filename))) {
            $deleted = @unlink(self::path($this->filename));
            if (!$deleted) {
                common_log(LOG_ERR, sprintf('Could not unlink existing file: "%s"', self::path($this->filename)));
            }
        }

        // Clear out related things in the database and filesystem, such as thumbnails
        if (Event::handle('FileDeleteRelated', array($this))) {
            $thumbs = new File_thumbnail();
            $thumbs->file_id = $this->id;
            if ($thumbs->find()) {
                while ($thumbs->fetch()) {
                    $thumbs->delete();
                }
            }
        }

        // And finally remove the entry from the database
        return parent::delete($useWhere);
    }
578 579 580 581 582 583 584

    public function getTitle()
    {
        $title = $this->title ?: $this->filename;

        return $title ?: null;
    }
585
}