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);
71 * Save a new file record.
73 * @param array $redir_data lookup data eg from File_redirection::where()
74 * @param string $given_url
77 function saveNew(array $redir_data, $given_url) {
80 if (!empty($redir_data['protected'])) $x->protected = $redir_data['protected'];
81 if (!empty($redir_data['title'])) $x->title = $redir_data['title'];
82 if (!empty($redir_data['type'])) $x->mimetype = $redir_data['type'];
83 if (!empty($redir_data['size'])) $x->size = intval($redir_data['size']);
84 if (isset($redir_data['time']) && $redir_data['time'] > 0) $x->date = intval($redir_data['time']);
85 $file_id = $x->insert();
87 $x->saveOembed($redir_data, $given_url);
92 * Save embedding information for this file, if applicable.
94 * Normally this won't need to be called manually, as File::saveNew()
97 * @param array $redir_data lookup data eg from File_redirection::where()
98 * @param string $given_url
99 * @return boolean success
101 public function saveOembed($redir_data, $given_url)
103 if (isset($redir_data['type'])
104 && (('text/html' === substr($redir_data['type'], 0, 9) || 'application/xhtml+xml' === substr($redir_data['type'], 0, 21)))
105 && ($oembed_data = File_oembed::_getOembed($given_url))) {
107 $fo = File_oembed::staticGet('file_id', $this->id);
110 File_oembed::saveNew($oembed_data, $this->id);
113 common_log(LOG_WARNING, "Strangely, a File_oembed object exists for new file $file_id", __FILE__);
119 function processNew($given_url, $notice_id=null) {
120 if (empty($given_url)) return -1; // error, no url to process
121 $given_url = File_redirection::_canonUrl($given_url);
122 if (empty($given_url)) return -1; // error, no url to process
123 $file = File::staticGet('url', $given_url);
125 $file_redir = File_redirection::staticGet('url', $given_url);
126 if (empty($file_redir)) {
127 $redir_data = File_redirection::where($given_url);
128 if (is_array($redir_data)) {
129 $redir_url = $redir_data['url'];
130 } elseif (is_string($redir_data)) {
131 $redir_url = $redir_data;
132 $redir_data = array();
134 throw new ServerException("Can't process url '$given_url'");
136 // TODO: max field length
137 if ($redir_url === $given_url || strlen($redir_url) > 255) {
138 $x = File::saveNew($redir_data, $given_url);
141 $x = File::processNew($redir_url, $notice_id);
143 File_redirection::saveNew($redir_data, $file_id, $given_url);
146 $file_id = $file_redir->file_id;
149 $file_id = $file->id;
154 $x = File::staticGet($file_id);
156 throw new ServerException("Robin thinks something is impossible.");
160 if (!empty($notice_id)) {
161 File_to_post::processNew($file_id, $notice_id);
166 function isRespectsQuota($user,$fileSize) {
168 if ($fileSize > common_config('attachments', 'file_quota')) {
169 return sprintf(_('No file may be larger than %d bytes ' .
170 'and the file you sent was %d bytes. Try to upload a smaller version.'),
171 common_config('attachments', 'file_quota'), $fileSize);
174 $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'";
175 $this->query($query);
177 $total = $this->total + $fileSize;
178 if ($total > common_config('attachments', 'user_quota')) {
179 return sprintf(_('A file this large would exceed your user quota of %d bytes.'), common_config('attachments', 'user_quota'));
181 $query .= ' AND EXTRACT(month FROM file.modified) = EXTRACT(month FROM now()) and EXTRACT(year FROM file.modified) = EXTRACT(year FROM now())';
182 $this->query($query);
184 $total = $this->total + $fileSize;
185 if ($total > common_config('attachments', 'monthly_quota')) {
186 return sprintf(_('A file this large would exceed your monthly quota of %d bytes.'), common_config('attachments', 'monthly_quota'));
191 // where should the file go?
193 static function filename($profile, $basename, $mimetype)
195 require_once 'MIME/Type/Extension.php';
196 $mte = new MIME_Type_Extension();
198 $ext = $mte->getExtension($mimetype);
199 } catch ( Exception $e) {
200 $ext = strtolower(preg_replace('/\W/', '', $mimetype));
202 $nickname = $profile->nickname;
203 $datestamp = strftime('%Y%m%dT%H%M%S', time());
204 $random = strtolower(common_confirmation_code(32));
205 return "$nickname-$datestamp-$random.$ext";
209 * Validation for as-saved base filenames
211 static function validFilename($filename)
213 return preg_match('/^[A-Za-z0-9._-]+$/', $filename);
217 * @throws ClientException on invalid filename
219 static function path($filename)
221 if (!self::validFilename($filename)) {
222 throw new ClientException("Invalid filename");
224 $dir = common_config('attachments', 'dir');
226 if ($dir[strlen($dir)-1] != '/') {
230 return $dir . $filename;
233 static function url($filename)
235 if (!self::validFilename($filename)) {
236 throw new ClientException("Invalid filename");
238 if(common_config('site','private')) {
240 return common_local_url('getfile',
241 array('filename' => $filename));
244 $path = common_config('attachments', 'path');
246 if ($path[strlen($path)-1] != '/') {
250 if ($path[0] != '/') {
254 $server = common_config('attachments', 'server');
256 if (empty($server)) {
257 $server = common_config('site', 'server');
260 $ssl = common_config('attachments', 'ssl');
262 if (is_null($ssl)) { // null -> guess
263 if (common_config('site', 'ssl') == 'always' &&
264 !common_config('attachments', 'server')) {
271 $protocol = ($ssl) ? 'https' : 'http';
273 return $protocol.'://'.$server.$path.$filename;
277 function getEnclosure(){
278 $enclosure = (object) array();
279 $enclosure->title=$this->title;
280 $enclosure->url=$this->url;
281 $enclosure->title=$this->title;
282 $enclosure->date=$this->date;
283 $enclosure->modified=$this->modified;
284 $enclosure->size=$this->size;
285 $enclosure->mimetype=$this->mimetype;
287 if(! isset($this->filename)){
288 $notEnclosureMimeTypes = array('text/html','application/xhtml+xml');
289 $mimetype = strtolower($this->mimetype);
290 $semicolon = strpos($mimetype,';');
292 $mimetype = substr($mimetype,0,$semicolon);
294 if(in_array($mimetype,$notEnclosureMimeTypes)){
295 $oembed = File_oembed::staticGet('file_id',$this->id);
297 $mimetype = strtolower($oembed->mimetype);
298 $semicolon = strpos($mimetype,';');
300 $mimetype = substr($mimetype,0,$semicolon);
302 if(in_array($mimetype,$notEnclosureMimeTypes)){
305 if($oembed->mimetype) $enclosure->mimetype=$oembed->mimetype;
306 if($oembed->url) $enclosure->url=$oembed->url;
307 if($oembed->title) $enclosure->title=$oembed->title;
308 if($oembed->modified) $enclosure->modified=$oembed->modified;
309 unset($oembed->size);
319 // quick back-compat hack, since there's still code using this
320 function isEnclosure()
322 $enclosure = $this->getEnclosure();
323 return !empty($enclosure);