3 * StatusNet - the distributed open-source microblogging tool
4 * Copyright (C) 2008, 2009, StatusNet, Inc.
6 * This program is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU Affero General Public License as published by
8 * the Free Software Foundation, either version 3 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU Affero General Public License for more details.
16 * You should have received a copy of the GNU Affero General Public License
17 * along with this program. If not, see <http://www.gnu.org/licenses/>.
20 if (!defined('STATUSNET') && !defined('LACONICA')) { exit(1); }
22 require_once INSTALLDIR.'/classes/Memcached_DataObject.php';
23 require_once INSTALLDIR.'/classes/File_redirection.php';
24 require_once INSTALLDIR.'/classes/File_oembed.php';
25 require_once INSTALLDIR.'/classes/File_thumbnail.php';
26 require_once INSTALLDIR.'/classes/File_to_post.php';
27 //require_once INSTALLDIR.'/classes/File_redirection.php';
30 * Table Definition for file
33 class File extends Memcached_DataObject
36 /* the code below is auto generated do not remove the above tag */
38 public $__table = 'file'; // table name
39 public $id; // int(4) primary_key not_null
40 public $url; // varchar(255) unique_key
41 public $mimetype; // varchar(50)
42 public $size; // int(4)
43 public $title; // varchar(255)
44 public $date; // int(4)
45 public $protected; // int(4)
46 public $filename; // varchar(255)
47 public $modified; // timestamp() not_null default_CURRENT_TIMESTAMP
50 function staticGet($k,$v=NULL) { return Memcached_DataObject::staticGet('File',$k,$v); }
52 /* the code above is auto generated do not remove the tag below */
55 function isProtected($url) {
56 return 'http://www.facebook.com/login.php' === $url;
59 function getAttachments($post_id) {
60 $query = "select file.* from file join file_to_post on (file_id = file.id) join notice on (post_id = notice.id) where post_id = " . $this->escape($post_id);
63 while ($this->fetch()) {
64 $att[] = clone($this);
70 function saveNew($redir_data, $given_url) {
73 if (!empty($redir_data['protected'])) $x->protected = $redir_data['protected'];
74 if (!empty($redir_data['title'])) $x->title = $redir_data['title'];
75 if (!empty($redir_data['type'])) $x->mimetype = $redir_data['type'];
76 if (!empty($redir_data['size'])) $x->size = intval($redir_data['size']);
77 if (isset($redir_data['time']) && $redir_data['time'] > 0) $x->date = intval($redir_data['time']);
78 $file_id = $x->insert();
80 if (isset($redir_data['type'])
81 && (('text/html' === substr($redir_data['type'], 0, 9) || 'application/xhtml+xml' === substr($redir_data['type'], 0, 21)))
82 && ($oembed_data = File_oembed::_getOembed($given_url))) {
83 File_oembed::saveNew($oembed_data, $file_id);
88 function processNew($given_url, $notice_id=null) {
89 if (empty($given_url)) return -1; // error, no url to process
90 $given_url = File_redirection::_canonUrl($given_url);
91 if (empty($given_url)) return -1; // error, no url to process
92 $file = File::staticGet('url', $given_url);
94 $file_redir = File_redirection::staticGet('url', $given_url);
95 if (empty($file_redir)) {
96 $redir_data = File_redirection::where($given_url);
97 if (is_array($redir_data)) {
98 $redir_url = $redir_data['url'];
99 } elseif (is_string($redir_data)) {
100 $redir_url = $redir_data;
102 throw new ServerException("Can't process url '$given_url'");
104 // TODO: max field length
105 if ($redir_url === $given_url || strlen($redir_url) > 255) {
106 $x = File::saveNew($redir_data, $given_url);
109 $x = File::processNew($redir_url, $notice_id);
111 File_redirection::saveNew($redir_data, $file_id, $given_url);
114 $file_id = $file_redir->file_id;
117 $file_id = $file->id;
122 $x = File::staticGet($file_id);
124 throw new ServerException("Robin thinks something is impossible.");
128 if (!empty($notice_id)) {
129 File_to_post::processNew($file_id, $notice_id);
134 function isRespectsQuota($user,$fileSize) {
136 if ($fileSize > common_config('attachments', 'file_quota')) {
137 return sprintf(_('No file may be larger than %d bytes ' .
138 'and the file you sent was %d bytes. Try to upload a smaller version.'),
139 common_config('attachments', 'file_quota'), $fileSize);
142 $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 = {$user->id} and file.url like '%/notice/%/file'";
143 $this->query($query);
145 $total = $this->total + $fileSize;
146 if ($total > common_config('attachments', 'user_quota')) {
147 return sprintf(_('A file this large would exceed your user quota of %d bytes.'), common_config('attachments', 'user_quota'));
149 $query .= ' AND EXTRACT(month FROM file.modified) = EXTRACT(month FROM now()) and EXTRACT(year FROM file.modified) = EXTRACT(year FROM now())';
150 $this->query($query);
152 $total = $this->total + $fileSize;
153 if ($total > common_config('attachments', 'monthly_quota')) {
154 return sprintf(_('A file this large would exceed your monthly quota of %d bytes.'), common_config('attachments', 'monthly_quota'));
159 // where should the file go?
161 static function filename($profile, $basename, $mimetype)
163 require_once 'MIME/Type/Extension.php';
164 $mte = new MIME_Type_Extension();
165 $ext = $mte->getExtension($mimetype);
166 $nickname = $profile->nickname;
167 $datestamp = strftime('%Y%m%dT%H%M%S', time());
168 $random = strtolower(common_confirmation_code(32));
169 return "$nickname-$datestamp-$random.$ext";
172 static function path($filename)
174 $dir = common_config('attachments', 'dir');
176 if ($dir[strlen($dir)-1] != '/') {
180 return $dir . $filename;
183 static function url($filename)
185 $path = common_config('attachments', 'path');
187 if ($path[strlen($path)-1] != '/') {
191 if ($path[0] != '/') {
195 $server = common_config('attachments', 'server');
197 if (empty($server)) {
198 $server = common_config('site', 'server');
203 return 'http://'.$server.$path.$filename;
206 function getEnclosure(){
207 $enclosure = (object) array();
208 $enclosure->title=$this->title;
209 $enclosure->url=$this->url;
210 $enclosure->title=$this->title;
211 $enclosure->date=$this->date;
212 $enclosure->modified=$this->modified;
213 $enclosure->size=$this->size;
214 $enclosure->mimetype=$this->mimetype;
216 if(! isset($this->filename)){
217 $notEnclosureMimeTypes = array('text/html','application/xhtml+xml');
218 $mimetype = strtolower($this->mimetype);
219 $semicolon = strpos($mimetype,';');
221 $mimetype = substr($mimetype,0,$semicolon);
223 if(in_array($mimetype,$notEnclosureMimeTypes)){
224 $oembed = File_oembed::staticGet('file_id',$this->id);
226 $mimetype = strtolower($oembed->mimetype);
227 $semicolon = strpos($mimetype,';');
229 $mimetype = substr($mimetype,0,$semicolon);
231 if(in_array($mimetype,$notEnclosureMimeTypes)){
234 if($oembed->mimetype) $enclosure->mimetype=$oembed->mimetype;
235 if($oembed->url) $enclosure->url=$oembed->url;
236 if($oembed->title) $enclosure->title=$oembed->title;
237 if($oembed->modified) $enclosure->modified=$oembed->modified;
238 unset($oembed->size);