Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100644 211 lines (179 sloc) 7.951 kb
d010d81 db stuff for URLs: redirections, oembed, etc.
Robin Millette authored
1 <?php
2 /*
3 * Laconica - a distributed open-source microblogging tool
aec6456 Update copyright dates in files modified in 2009
Evan Prodromou authored
4 * Copyright (C) 2008, 2009, Control Yourself, Inc.
d010d81 db stuff for URLs: redirections, oembed, etc.
Robin Millette authored
5 *
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.
10 *
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.
15 *
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/>.
18 */
19
20 if (!defined('LACONICA')) { exit(1); }
21
22 require_once INSTALLDIR.'/classes/Memcached_DataObject.php';
3b7ee5a rewrote short url stuff to handle new file/url classes (redirections, oe...
Robin Millette authored
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';
d010d81 db stuff for URLs: redirections, oembed, etc.
Robin Millette authored
28
29 /**
30 * Table Definition for file
31 */
32
d9bebfd Attachment upload server and path now configurable
Zach Copley authored
33 class File extends Memcached_DataObject
d010d81 db stuff for URLs: redirections, oembed, etc.
Robin Millette authored
34 {
35 ###START_AUTOCODE
36 /* the code below is auto generated do not remove the above tag */
37
38 public $__table = 'file'; // table name
05e5122 Database changes for file tables
Evan Prodromou authored
39 public $id; // int(4) primary_key not_null
d010d81 db stuff for URLs: redirections, oembed, etc.
Robin Millette authored
40 public $url; // varchar(255) unique_key
d9bebfd Attachment upload server and path now configurable
Zach Copley authored
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)
05e5122 Database changes for file tables
Evan Prodromou authored
47 public $modified; // timestamp() not_null default_CURRENT_TIMESTAMP
d010d81 db stuff for URLs: redirections, oembed, etc.
Robin Millette authored
48
49 /* Static get */
2e9fc34 change staticGet() from DB_DataObject to Memcached_DataObject for file c...
Evan Prodromou authored
50 function staticGet($k,$v=NULL) { return Memcached_DataObject::staticGet('File',$k,$v); }
d010d81 db stuff for URLs: redirections, oembed, etc.
Robin Millette authored
51
52 /* the code above is auto generated do not remove the tag below */
53 ###END_AUTOCODE
3b7ee5a rewrote short url stuff to handle new file/url classes (redirections, oe...
Robin Millette authored
54
55 function isProtected($url) {
56 return 'http://www.facebook.com/login.php' === $url;
57 }
58
fecb8c7 Attachments and their list now provide "ajax" view. Also added a few sid...
Robin Millette authored
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);
61 $this->query($query);
62 $att = array();
63 while ($this->fetch()) {
64 $att[] = clone($this);
65 }
66 $this->free();
67 return $att;
68 }
69
3b7ee5a rewrote short url stuff to handle new file/url classes (redirections, oe...
Robin Millette authored
70 function saveNew($redir_data, $given_url) {
71 $x = new File;
72 $x->url = $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();
79
80 if (isset($redir_data['type'])
81 && ('text/html' === substr($redir_data['type'], 0, 9))
82 && ($oembed_data = File_oembed::_getOembed($given_url))
83 && isset($oembed_data['json'])) {
84 File_oembed::saveNew($oembed_data['json'], $file_id);
85 }
86 return $x;
87 }
88
89 function processNew($given_url, $notice_id) {
90 if (empty($given_url)) return -1; // error, no url to process
91 $given_url = File_redirection::_canonUrl($given_url);
92 if (empty($given_url)) return -1; // error, no url to process
93 $file = File::staticGet('url', $given_url);
638905c avoid getting duplicate errors on upload
Evan Prodromou authored
94 if (empty($file)) {
3b7ee5a rewrote short url stuff to handle new file/url classes (redirections, oe...
Robin Millette authored
95 $file_redir = File_redirection::staticGet('url', $given_url);
638905c avoid getting duplicate errors on upload
Evan Prodromou authored
96 if (empty($file_redir)) {
97 common_debug("processNew() '$given_url' not a known redirect.\n");
3b7ee5a rewrote short url stuff to handle new file/url classes (redirections, oe...
Robin Millette authored
98 $redir_data = File_redirection::where($given_url);
99 $redir_url = $redir_data['url'];
100 if ($redir_url === $given_url) {
101 $x = File::saveNew($redir_data, $given_url);
102 $file_id = $x->id;
103 } else {
104 $x = File::processNew($redir_url, $notice_id);
105 $file_id = $x->id;
106 File_redirection::saveNew($redir_data, $file_id, $given_url);
107 }
108 } else {
109 $file_id = $file_redir->file_id;
110 }
111 } else {
112 $file_id = $file->id;
113 $x = $file;
114 }
115
116 if (empty($x)) {
117 $x = File::staticGet($file_id);
118 if (empty($x)) die('Impossible!');
119 }
d9bebfd Attachment upload server and path now configurable
Zach Copley authored
120
3b7ee5a rewrote short url stuff to handle new file/url classes (redirections, oe...
Robin Millette authored
121 File_to_post::processNew($file_id, $notice_id);
122 return $x;
123 }
f8dae2b Refactored some attachment code and fixed upload bug in interface.
Robin Millette authored
124
5f72423 @candrews File classes does not use the $FILES array directly, as users of this cl...
candrews authored
125 function isRespectsQuota($user,$fileSize) {
126 if ($fileSize > common_config('attachments', 'file_quota')) {
f8dae2b Refactored some attachment code and fixed upload bug in interface.
Robin Millette authored
127 return sprintf(_('No file may be larger than %d bytes ' .
a21a9f2 append uploads to content rather than showing them double
Evan Prodromou authored
128 'and the file you sent was %d bytes. Try to upload a smaller version.'),
5f72423 @candrews File classes does not use the $FILES array directly, as users of this cl...
candrews authored
129 common_config('attachments', 'file_quota'), $fileSize);
f8dae2b Refactored some attachment code and fixed upload bug in interface.
Robin Millette authored
130 }
131
132 $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'";
133 $this->query($query);
134 $this->fetch();
5f72423 @candrews File classes does not use the $FILES array directly, as users of this cl...
candrews authored
135 $total = $this->total + $fileSize;
f8dae2b Refactored some attachment code and fixed upload bug in interface.
Robin Millette authored
136 if ($total > common_config('attachments', 'user_quota')) {
137 return sprintf(_('A file this large would exceed your user quota of %d bytes.'), common_config('attachments', 'user_quota'));
138 }
139
140 $query .= ' month(modified) = month(now()) and year(modified) = year(now())';
141 $this->query($query);
142 $this->fetch();
5f72423 @candrews File classes does not use the $FILES array directly, as users of this cl...
candrews authored
143 $total = $this->total + $fileSize;
f8dae2b Refactored some attachment code and fixed upload bug in interface.
Robin Millette authored
144 if ($total > common_config('attachments', 'monthly_quota')) {
145 return sprintf(_('A file this large would exceed your monthly quota of %d bytes.'), common_config('attachments', 'monthly_quota'));
146 }
147 return true;
148 }
d9bebfd Attachment upload server and path now configurable
Zach Copley authored
149
150 // where should the file go?
151
a21a9f2 append uploads to content rather than showing them double
Evan Prodromou authored
152 static function filename($profile, $basename, $mimetype)
153 {
154 require_once 'MIME/Type/Extension.php';
155 $mte = new MIME_Type_Extension();
156 $ext = $mte->getExtension($mimetype);
157 $nickname = $profile->nickname;
158 $datestamp = strftime('%Y%m%dT%H%M%S', time());
159 $random = strtolower(common_confirmation_code(32));
160 return "$nickname-$datestamp-$random.$ext";
161 }
d9bebfd Attachment upload server and path now configurable
Zach Copley authored
162
a21a9f2 append uploads to content rather than showing them double
Evan Prodromou authored
163 static function path($filename)
164 {
165 $dir = common_config('attachments', 'dir');
d9bebfd Attachment upload server and path now configurable
Zach Copley authored
166
a21a9f2 append uploads to content rather than showing them double
Evan Prodromou authored
167 if ($dir[strlen($dir)-1] != '/') {
168 $dir .= '/';
169 }
d9bebfd Attachment upload server and path now configurable
Zach Copley authored
170
a21a9f2 append uploads to content rather than showing them double
Evan Prodromou authored
171 return $dir . $filename;
172 }
d9bebfd Attachment upload server and path now configurable
Zach Copley authored
173
a21a9f2 append uploads to content rather than showing them double
Evan Prodromou authored
174 static function url($filename)
175 {
176 $path = common_config('attachments', 'path');
d9bebfd Attachment upload server and path now configurable
Zach Copley authored
177
a21a9f2 append uploads to content rather than showing them double
Evan Prodromou authored
178 if ($path[strlen($path)-1] != '/') {
179 $path .= '/';
180 }
d9bebfd Attachment upload server and path now configurable
Zach Copley authored
181
a21a9f2 append uploads to content rather than showing them double
Evan Prodromou authored
182 if ($path[0] != '/') {
183 $path = '/'.$path;
184 }
d9bebfd Attachment upload server and path now configurable
Zach Copley authored
185
a21a9f2 append uploads to content rather than showing them double
Evan Prodromou authored
186 $server = common_config('attachments', 'server');
d9bebfd Attachment upload server and path now configurable
Zach Copley authored
187
a21a9f2 append uploads to content rather than showing them double
Evan Prodromou authored
188 if (empty($server)) {
189 $server = common_config('site', 'server');
190 }
d9bebfd Attachment upload server and path now configurable
Zach Copley authored
191
a21a9f2 append uploads to content rather than showing them double
Evan Prodromou authored
192 // XXX: protocol
d9bebfd Attachment upload server and path now configurable
Zach Copley authored
193
a21a9f2 append uploads to content rather than showing them double
Evan Prodromou authored
194 return 'http://'.$server.$path.$filename;
195 }
593af9f @candrews Moved the decision logic as to whether an attachment should be an enclos...
candrews authored
196
197 function isEnclosure(){
198 if(isset($this->filename)){
199 return true;
200 }
c97957c @candrews HTML "File"/attachments should probably not be considered enclosures.
candrews authored
201 $notEnclosureMimeTypes = array('text/html','application/xhtml+xml');
593af9f @candrews Moved the decision logic as to whether an attachment should be an enclos...
candrews authored
202 $mimetype = strtolower($this->mimetype);
203 $semicolon = strpos($mimetype,';');
204 if($semicolon){
205 $mimetype = substr($mimetype,0,$semicolon);
206 }
207 return(! in_array($mimetype,$notEnclosureMimeTypes));
208 }
d010d81 db stuff for URLs: redirections, oembed, etc.
Robin Millette authored
209 }
f8dae2b Refactored some attachment code and fixed upload bug in interface.
Robin Millette authored
210
Something went wrong with that request. Please try again.