1: <?php
2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13:
14:
15: 16: 17:
18: class UBFile extends UBItem {
19: 20: 21:
22: const SUBTYPE = "UBFile";
23: 24: 25:
26: const TIMESTAMP_DELIMITER = "#";
27: const DEFAULT_FILENAME = "unnamed_file";
28: const THUMB_SMALL = 64;
29: const THUMB_MEDIUM = 128;
30: const THUMB_LARGE = 256;
31: 32: 33:
34: public $data = null;
35: public $size = 0;
36: public $file_path = "";
37: public $temp_path = "";
38: public $thumb_small = array();
39: public $thumb_medium = array();
40: public $thumb_large = array();
41: public $mime_type = array();
42:
43: 44: 45: 46: 47: 48: 49:
50: function __construct($id = null) {
51: if (!empty($id)) {
52: if (!($elgg_file = new ElggFile((int)$id))) {
53: throw new APIException("ERROR: Id '" . $id . "' does not correspond to a " . get_called_class() . " object.");
54: }
55: $elgg_type = $elgg_file->type;
56: $elgg_subtype = $elgg_file->getSubtype();
57: if (($elgg_type != static::TYPE) || ($elgg_subtype != static::SUBTYPE)) {
58: throw new APIException("ERROR: ID '" . $id . "' does not correspond to a " . get_called_class() . " object.");
59: }
60: $this->copy_from_elgg($elgg_file);
61: }
62: }
63:
64: 65: 66: 67: 68:
69: protected function copy_from_elgg($elgg_file) {
70: $this->id = (int)$elgg_file->get("guid");
71: $this->description = (string)$elgg_file->get("description");
72: $this->owner_id = (int)$elgg_file->getOwnerGUID();
73: $this->time_created = (int)$elgg_file->getTimeCreated();
74: $this->name = (string)$elgg_file->get("name");
75: $this->size = (int)$elgg_file->size();
76: $this->file_path = (string)$elgg_file->getFilenameOnFilestore();
77: $this->url = (string)elgg_get_site_url() . "file/download/" . $this->id;
78: if (!empty($elgg_file->thumb_small)) {
79: $this->thumb_small["path"] = (string)$elgg_file->get("thumb_small");
80: $this->thumb_small["url"] = (string)elgg_get_site_url() . "file/thumbnail/small/" . $this->id;
81: $this->thumb_medium["path"] = (string)$elgg_file->get("thumb_medium");
82: $this->thumb_medium["url"] = (string)elgg_get_site_url() . "file/thumbnail/medium/" . $this->id;
83: $this->thumb_large["path"] = (string)$elgg_file->get("thumb_large");
84: $this->thumb_large["url"] = (string)elgg_get_site_url() . "file/thumbnail/large/" . $this->id;
85: }
86: if (!empty($elgg_file->mime_type)) {
87: $this->mime_type["full"] = $elgg_file->mime_type[0];
88: $this->mime_type["short"] = $elgg_file->mime_type[1];
89: }
90: $this->cloned_from = (int)static::get_cloned_from($this->id);
91: $this->clone_array = (array)static::get_clones($this->id);
92: }
93:
94: 95: 96: 97: 98:
99: protected function copy_to_elgg($elgg_file) {
100: if ($this->time_created == 0) {
101: $elgg_file->set("filename", (string)rand());
102: }
103: $elgg_file->set("name", (string)$this->name);
104: $elgg_file->description = (string)$this->description;
105: if(!empty($this->owner_id)) {
106: $elgg_file->set("owner_guid", (int)$this->owner_id);
107: }
108: $elgg_file->access_id = ACCESS_PUBLIC;
109: if (!empty($this->data)) {
110: $elgg_file->open("write");
111: $decoded_data = base64_decode($this->data, true);
112: if ($decoded_data !== false) {
113: $elgg_file->write($decoded_data);
114: } else {
115: $elgg_file->write($this->data);
116: }
117: $elgg_file->close();
118: static::create_thumbnails($elgg_file);
119: } elseif (!empty($this->temp_path)) {
120: $elgg_file->open("write");
121: $elgg_file->close();
122: move_uploaded_file($this->temp_path, $elgg_file->getFilenameOnFilestore());
123: static::create_thumbnails($elgg_file);
124: } elseif (!empty($this->thumb_small)) {
125: $elgg_file->set("thumb_small", (string)$this->thumb_small["path"]);
126: $elgg_file->set("thumb_medium", (string)$this->thumb_medium["path"]);
127: $elgg_file->set("thumb_large", (string)$this->thumb_large["path"]);
128: }
129: $filestore_name = $elgg_file->getFilenameOnFilestore();
130: $mime_type["full"] = (string)static::get_mime_type($filestore_name);
131: if ($mime_type["full"] == "application/zip") {
132: $new_mime = getMicrosoftOfficeMimeInfo($filestore_name);
133: if ($new_mime !== false) {
134: $mime_type["full"] = (string)$new_mime["mime"];
135: }
136: }
137: $mime_type["short"] = (string)static::get_simple_mime_type($mime_type["full"]);
138: $elgg_file->set("mime_type", (array)$mime_type);
139: }
140:
141:
142: 143: 144: 145: 146: 147: 148:
149: protected function save($double_save = false) {
150: if (!empty($this->id)) {
151: if (!$elgg_file = new ElggFile($this->id)) {
152: return false;
153: }
154: } else {
155: $elgg_file = new ElggFile();
156: $elgg_file->type = static::TYPE;
157: $elgg_file->subtype = static::SUBTYPE;
158: }
159: $this->copy_to_elgg($elgg_file);
160: $elgg_file->save();
161: if ($double_save) {
162:
163:
164: $elgg_file->save();
165: }
166: return $this->id = $elgg_file->guid;
167: }
168:
169: 170: 171: 172: 173: 174: 175: 176: 177:
178: static function create_clone($id, $linked = true, $keep_owner = false) {
179: $elgg_file = new ElggFile($id);
180: $elgg_file_clone = clone $elgg_file;
181: $elgg_file_clone->save();
182: $prop_value_array = static::get_properties($id);
183: if($keep_owner === false){
184: $prop_value_array["owner_id"] = elgg_get_logged_in_user_guid();
185: }
186: $clone_id = static::set_properties($elgg_file_clone->getGUID(), $prop_value_array);
187: if($linked){
188: static::link_parent_clone($id, $clone_id);
189: }
190: return $clone_id;
191: }
192:
193: 194: 195: 196: 197: 198: 199:
200: static function get_mime_type($file) {
201: $mime = false;
202:
203: if (function_exists('finfo_file') && defined('FILEINFO_MIME_TYPE')) {
204: $resource = finfo_open(FILEINFO_MIME_TYPE);
205: if ($resource) {
206: $mime = finfo_file($resource, $file);
207: }
208: }
209:
210: if (!$mime) {
211: return null;
212: }
213: return $mime;
214: }
215:
216: 217: 218: 219: 220: 221: 222:
223: static function get_simple_mime_type($mime_type) {
224: switch ($mime_type) {
225: case "application/msword":
226: case "application/vnd.openxmlformats-officedocument.wordprocessingml.document":
227: case 'application/vnd.openxmlformats-officedocument.presentationml.presentation':
228: case 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet':
229: return "document";
230: case "application/pdf":
231: return "document";
232: case "application/ogg":
233: return "audio";
234: case "application/x-rar":
235: case "application/zip":
236: return "compressed";
237: }
238: if (substr_count($mime_type, 'text/')) {
239: return "document";
240: }
241: if (substr_count($mime_type, 'audio/')) {
242: return "audio";
243: }
244: if (substr_count($mime_type, 'image/')) {
245: return "image";
246: }
247: if (substr_count($mime_type, 'video/')) {
248: return "video";
249: }
250: if (substr_count($mime_type, 'opendocument')) {
251: return "document";
252: }
253: return "general";
254: }
255:
256: 257: 258: 259: 260:
261: private static function create_thumbnails($elgg_file) {
262: $file_name = $elgg_file->getFilename();
263: $filestore_name = $elgg_file->getFilenameOnFilestore();
264: $simple_mime_type = static::get_simple_mime_type(static::get_mime_type($filestore_name));
265:
266: if ($simple_mime_type == "image") {
267: $thumb = new ElggFile();
268:
269: $thumbnail_small = get_resized_image_from_existing_file($filestore_name,
270: static::THUMB_SMALL,
271: static::THUMB_SMALL,
272: true);
273: if ($thumbnail_small) {
274: $thumb->setFilename("thumb_small-" . $file_name);
275: $thumb->open("write");
276: $thumb->write($thumbnail_small);
277: $thumb->close();
278: $elgg_file->set("thumb_small", (string)$thumb->getFilenameOnFilestore());
279: }
280:
281: $thumbnail_medium = get_resized_image_from_existing_file($filestore_name,
282: static::THUMB_MEDIUM,
283: static::THUMB_MEDIUM,
284: true);
285: if ($thumbnail_medium) {
286: $thumb->setFilename("thumb_medium-" . $file_name);
287: $thumb->open("write");
288: $thumb->write($thumbnail_medium);
289: $thumb->close();
290: $elgg_file->set("thumb_medium", (string)$thumb->getFilenameOnFilestore());
291: }
292:
293: $thumbnail_large = get_resized_image_from_existing_file($filestore_name,
294: static::THUMB_LARGE,
295: static::THUMB_LARGE,
296: false);
297: if ($thumbnail_large) {
298: $thumb->setFilename("thumb_large-" . $file_name);
299: $thumb->open("write");
300: $thumb->write($thumbnail_large);
301: $thumb->close();
302: $elgg_file->set("thumb_large", (string)$thumb->getFilenameOnFilestore());
303: }
304: }
305: }
306: }