Overview

Packages

  • ClipIt
    • clipit
      • api
    • urjc
      • backend
  • Elgg
    • Core
      • Access
      • Authentication
      • Cache
      • Caches
      • Core
      • DataMode
        • Site
      • DataModel
        • Annotations
        • Entities
        • Extender
        • File
        • Importable
        • Loggable
        • Notable
        • Object
        • User
      • DataStorage
      • Exception
      • Exceptions
        • Stub
      • FileStore
        • Disk
      • Groups
      • Helpers
      • HMAC
      • Memcache
      • Metadata
      • Navigation
      • ODD
      • Output
      • Plugins
        • Settings
      • Sessions
      • SocialModel
        • Friendable
        • Locatable
      • WebServicesAPI
      • Widgets
      • XML
      • XMLRPC
    • Exceptions
      • Stub
  • None
  • PHP

Classes

  • UBCollection
  • UBEvent
  • UBFile
  • UBItem
  • UBMessage
  • UBSite
  • UBUser
  • Overview
  • Package
  • Class
  • Tree
  1: <?php
  2: /**
  3:  * ClipIt - JuxtaLearn Web Space
  4:  * PHP version:     >= 5.2
  5:  * Creation date:   2013-10-10
  6:  * Last update:     $Date$
  7:  * @author          Pablo LlinĂ¡s Arnaiz <pebs74@gmail.com>, URJC JuxtaLearn Team
  8:  * @version         $Version$
  9:  * @link            http://www.juxtalearn.eu
 10:  * @license         GNU Affero General Public License v3
 11:  * @package         ClipIt
 12:  * @subpackage      urjc_backend
 13:  */
 14: 
 15: /**
 16:  * <Class Description>
 17:  */
 18: class UBFile extends UBItem {
 19:     /**
 20:      * @const string Elgg entity SUBTYPE for this class
 21:      */
 22:     const SUBTYPE = "UBFile";
 23:     /**
 24:      * Class constants
 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:      * Class variables
 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:      * Constructor
 45:      *
 46:      * @param int $id If != null, load instance.
 47:      *
 48:      * @throws APIException
 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:      * Loads object parameters stored in Elgg
 66:      *
 67:      * @param ElggFile $elgg_file
 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:      * Copy $this file parameters into an Elgg File entity.
 96:      *
 97:      * @param ElggFile $elgg_file Elgg object instance to save $this to
 98:      */
 99:     protected function copy_to_elgg($elgg_file) {
100:         if ($this->time_created == 0) { // new file
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)) { // new file or new 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)) { // File was uploaded into local temp dir
120:             $elgg_file->open("write"); // to ensure file is created in disk
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") { // Detect Office 2007+ mimetype
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:      * Saves this instance into the system.
144:      *
145:      * @param bool $double_save defaults to false. This param has no effect
146:      * in the current implementation and is just added for compatibility reasons to UBFile's ancestors.
147:      * @return bool|int Returns id of saved instance, or false if error.
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:             // Only updates are saving time_created, thus first save for creation, second save for updating to
163:             //proper creation time if given.
164:             $elgg_file->save();
165:         }
166:         return $this->id = $elgg_file->guid;
167:     }
168: 
169:     /**
170:      * Clone the specified File, including all of its properties.
171:      *
172:      * @param int $id File id from which to create a clone.
173:      * @param bool $linked Selects whether the clone will be linked to the parent object.
174:      * @param bool $keep_owner Selects whether the clone will keep the parent file's owner (default: no)
175:      *
176:      * @return bool|int Id of the new clone Item, false in case of error.
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:      * Get File MIME Type
195:      *
196:      * @param string $file Filename on Filestore
197:      *
198:      * @return bool|mixed|null|string Mime Type
199:      */
200:     static function get_mime_type($file) {
201:         $mime = false;
202:         // for PHP5 folks.
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:         // default
210:         if (!$mime) {
211:             return null;
212:         }
213:         return $mime;
214:     }
215: 
216:     /**
217:      * Returns an overall file type from the mimetype
218:      *
219:      * @param string $mime_type The MIME type
220:      *
221:      * @return string The overall type
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:      * Create thumbnails for Files with MIME type = image
258:      *
259:      * @param ElggFile $elgg_file
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:         // if image, we need to create thumbnails (this should be moved into a function)
266:         if ($simple_mime_type == "image") {
267:             $thumb = new ElggFile();
268:             // squared small thumbnail
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:             // squared medium thumbnail
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:             // original proportion large thumbnail
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: }
API documentation generated by ApiGen 2.8.0