2007-07-19 12:40:28 +02:00
|
|
|
<?php
|
|
|
|
/**
|
2010-10-15 05:12:39 +02:00
|
|
|
* This class handles the representation of a file on the filesystem within the framework.
|
|
|
|
* Most of the methods also handle the {@link Folder} subclass.
|
2014-08-15 08:53:05 +02:00
|
|
|
*
|
2012-03-24 04:38:57 +01:00
|
|
|
* Note: The files are stored in the assets/ directory, but SilverStripe
|
2007-07-19 12:40:28 +02:00
|
|
|
* looks at the db object to gather information about a file such as URL
|
2010-10-13 01:02:10 +02:00
|
|
|
* It then uses this for all processing functions (like image manipulation).
|
2014-08-15 08:53:05 +02:00
|
|
|
*
|
2010-10-15 05:12:39 +02:00
|
|
|
* <b>Security</b>
|
2014-08-15 08:53:05 +02:00
|
|
|
*
|
2010-10-15 05:04:27 +02:00
|
|
|
* Caution: It is recommended to disable any script execution in the "assets/"
|
|
|
|
* directory in the webserver configuration, to reduce the risk of exploits.
|
|
|
|
* See http://doc.silverstripe.org/secure-development#filesystem
|
2014-08-15 08:53:05 +02:00
|
|
|
*
|
2010-10-15 05:12:39 +02:00
|
|
|
* <b>Properties</b>
|
2014-08-15 08:53:05 +02:00
|
|
|
*
|
2010-10-15 05:12:39 +02:00
|
|
|
* - "Name": File name (including extension) or folder name.
|
2014-08-15 08:53:05 +02:00
|
|
|
* Should be the same as the actual filesystem.
|
2010-10-15 05:12:39 +02:00
|
|
|
* - "Title": Optional title of the file (for display purposes only).
|
2011-03-23 01:23:43 +01:00
|
|
|
* Defaults to "Name". Note that the Title field of Folder (subclass of File)
|
|
|
|
* is linked to Name, so Name and Title will always be the same.
|
2010-10-15 05:12:39 +02:00
|
|
|
* - "Filename": Path of the file or folder, relative to the webroot.
|
|
|
|
* Usually starts with the "assets/" directory, and has no trailing slash.
|
|
|
|
* Defaults to the "assets/" directory plus "Name" property if not set.
|
|
|
|
* Setting the "Filename" property will override the "Name" property.
|
|
|
|
* The value should be in sync with "ParentID".
|
|
|
|
* - "Content": Typically unused, but handy for a textual representation of
|
|
|
|
* files, e.g. for fulltext indexing of PDF documents.
|
|
|
|
* - "ParentID": Points to a {@link Folder} record. Should be in sync with
|
|
|
|
* "Filename". A ParentID=0 value points to the "assets/" folder, not the webroot.
|
2014-08-15 08:53:05 +02:00
|
|
|
*
|
2010-10-15 05:12:39 +02:00
|
|
|
* <b>Synchronization</b>
|
2014-08-15 08:53:05 +02:00
|
|
|
*
|
|
|
|
* Changes to a File database record can change the filesystem entry,
|
2010-10-15 05:12:39 +02:00
|
|
|
* but not the other way around. If the filesystem path is renamed outside
|
|
|
|
* of SilverStripe, there's no way for the database to recover this linkage.
|
|
|
|
* New physical files on the filesystem can be "discovered" via {@link Filesystem::sync()},
|
2014-08-15 08:53:05 +02:00
|
|
|
* the equivalent {@link File} and {@link Folder} records are automatically
|
2010-10-15 05:12:39 +02:00
|
|
|
* created by this method.
|
2014-08-15 08:53:05 +02:00
|
|
|
*
|
2010-10-15 05:12:39 +02:00
|
|
|
* Certain property changes within the File API that can cause a "delayed" filesystem change:
|
|
|
|
* The change is enforced in {@link onBeforeWrite()} later on.
|
|
|
|
* - setParentID()
|
|
|
|
* - setFilename()
|
|
|
|
* - setName()
|
|
|
|
* It is recommended that you use {@link write()} directly after setting any of these properties,
|
|
|
|
* otherwise getters like {@link getFullPath()} and {@link getRelativePath()}
|
|
|
|
* will result paths that are inconsistent with the filesystem.
|
2014-08-15 08:53:05 +02:00
|
|
|
*
|
2010-10-15 05:12:39 +02:00
|
|
|
* Caution: Calling {@link delete()} will also delete from the filesystem.
|
|
|
|
* Call {@link deleteDatabaseOnly()} if you want to avoid this.
|
2014-08-15 08:53:05 +02:00
|
|
|
*
|
2010-10-15 05:12:39 +02:00
|
|
|
* <b>Creating Files and Folders</b>
|
2014-08-15 08:53:05 +02:00
|
|
|
*
|
2010-10-15 05:12:39 +02:00
|
|
|
* Typically both files and folders should be created first on the filesystem,
|
|
|
|
* and then reflected in as database records. Folders can be created recursively
|
2012-03-24 04:38:57 +01:00
|
|
|
* from SilverStripe both in the database and filesystem through {@link Folder::findOrMake()}.
|
2010-10-15 05:12:39 +02:00
|
|
|
* Ensure that you always set a "Filename" property when writing to the database,
|
|
|
|
* leaving it out can lead to unexpected results.
|
2014-08-15 08:53:05 +02:00
|
|
|
*
|
2012-04-12 08:02:46 +02:00
|
|
|
* @package framework
|
2008-02-25 03:10:37 +01:00
|
|
|
* @subpackage filesystem
|
2014-01-26 04:17:17 +01:00
|
|
|
*
|
|
|
|
* @property string Name Basename of the file
|
|
|
|
* @property string Title Title of the file
|
|
|
|
* @property string Filename Filename including path
|
|
|
|
* @property string Content
|
|
|
|
* @property string ShowInSearch Boolean that indicates if file is shown in search. Doesn't apply to Folder
|
|
|
|
*
|
|
|
|
* @property int ParentID ID of parent File/Folder
|
|
|
|
* @property int OwnerID ID of Member who owns the file
|
|
|
|
*
|
|
|
|
* @method File Parent() Returns parent File
|
|
|
|
* @method Member Owner() Returns Member object of file owner.
|
2007-07-19 12:40:28 +02:00
|
|
|
*/
|
|
|
|
class File extends DataObject {
|
2007-09-14 21:19:32 +02:00
|
|
|
|
2013-03-21 19:48:54 +01:00
|
|
|
private static $default_sort = "\"Name\"";
|
2007-09-14 21:19:32 +02:00
|
|
|
|
2013-03-21 19:48:54 +01:00
|
|
|
private static $singular_name = "File";
|
2007-09-14 21:19:32 +02:00
|
|
|
|
2013-03-21 19:48:54 +01:00
|
|
|
private static $plural_name = "Files";
|
2007-09-14 21:19:32 +02:00
|
|
|
|
2013-03-21 19:48:54 +01:00
|
|
|
private static $db = array(
|
2007-07-19 12:40:28 +02:00
|
|
|
"Name" => "Varchar(255)",
|
|
|
|
"Title" => "Varchar(255)",
|
2010-10-19 06:05:29 +02:00
|
|
|
"Filename" => "Text",
|
2007-07-19 12:40:28 +02:00
|
|
|
"Content" => "Text",
|
2011-09-15 16:00:54 +02:00
|
|
|
// Only applies to files, doesn't inherit for folder
|
|
|
|
'ShowInSearch' => 'Boolean(1)',
|
2007-07-19 12:40:28 +02:00
|
|
|
);
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2013-03-21 19:48:54 +01:00
|
|
|
private static $has_one = array(
|
2007-07-19 12:40:28 +02:00
|
|
|
"Parent" => "File",
|
|
|
|
"Owner" => "Member"
|
|
|
|
);
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2013-03-21 19:48:54 +01:00
|
|
|
private static $has_many = array();
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2013-03-21 19:48:54 +01:00
|
|
|
private static $many_many = array();
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2013-03-21 19:48:54 +01:00
|
|
|
private static $defaults = array(
|
2011-09-15 16:00:54 +02:00
|
|
|
"ShowInSearch" => 1,
|
|
|
|
);
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2013-03-21 19:48:54 +01:00
|
|
|
private static $extensions = array(
|
2009-02-02 00:49:53 +01:00
|
|
|
"Hierarchy",
|
|
|
|
);
|
2014-03-27 04:24:11 +01:00
|
|
|
|
|
|
|
private static $casting = array (
|
|
|
|
'TreeTitle' => 'HTMLText'
|
|
|
|
);
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2010-04-12 04:35:12 +02:00
|
|
|
/**
|
2013-03-21 19:48:54 +01:00
|
|
|
* @config
|
2010-10-15 05:04:27 +02:00
|
|
|
* @var array List of allowed file extensions, enforced through {@link validate()}.
|
2014-08-15 08:53:05 +02:00
|
|
|
*
|
2010-12-05 01:05:05 +01:00
|
|
|
* Note: if you modify this, you should also change a configuration file in the assets directory.
|
|
|
|
* Otherwise, the files will be able to be uploaded but they won't be able to be served by the
|
|
|
|
* webserver.
|
2014-08-15 08:53:05 +02:00
|
|
|
*
|
2014-12-04 21:57:31 +01:00
|
|
|
* - If you are running Apache you will need to change assets/.htaccess
|
2014-08-15 08:53:05 +02:00
|
|
|
* - If you are running IIS you will need to change assets/web.config
|
2010-12-05 01:05:05 +01:00
|
|
|
*
|
|
|
|
* Instructions for the change you need to make are included in a comment in the config file.
|
2010-04-12 04:35:12 +02:00
|
|
|
*/
|
2013-03-21 19:48:54 +01:00
|
|
|
private static $allowed_extensions = array(
|
2014-12-04 21:57:31 +01:00
|
|
|
'','ace','arc','arj','asf','au','avi','bmp','bz2','cab','cda','css','csv','dmg','doc','docx','dotx','dotm',
|
2014-04-09 07:37:25 +02:00
|
|
|
'flv','gif','gpx','gz','hqx','ico','jar','jpeg','jpg','js','kml', 'm4a','m4v',
|
2013-05-29 17:36:11 +02:00
|
|
|
'mid','midi','mkv','mov','mp3','mp4','mpa','mpeg','mpg','ogg','ogv','pages','pcx','pdf','pkg',
|
2015-10-08 08:32:35 +02:00
|
|
|
'png','pps','ppt','pptx','potx','potm','ra','ram','rm','rtf','sit','sitx', 'svg', 'tar','tgz','tif','tiff',
|
2015-01-15 06:52:46 +01:00
|
|
|
'txt','wav','webm','wma','wmv','xls','xlsx','xltx','xltm','zip','zipx',
|
2010-04-12 04:42:51 +02:00
|
|
|
);
|
2012-02-23 22:43:37 +01:00
|
|
|
|
2012-03-02 17:30:48 +01:00
|
|
|
/**
|
2013-03-21 19:48:54 +01:00
|
|
|
* @config
|
2012-03-02 17:30:48 +01:00
|
|
|
* @var array Category identifiers mapped to commonly used extensions.
|
|
|
|
*/
|
2013-03-21 19:48:54 +01:00
|
|
|
private static $app_categories = array(
|
2012-03-02 17:30:48 +01:00
|
|
|
'audio' => array(
|
2012-09-26 23:34:00 +02:00
|
|
|
"aif" ,"au" ,"mid" ,"midi" ,"mp3" ,"ra" ,"ram" ,"rm","mp3" ,"wav" ,"m4a" ,"snd" ,"aifc" ,"aiff" ,"wma",
|
2015-07-08 15:58:38 +02:00
|
|
|
"apl", "avr" ,"cda" ,"ogg"
|
2012-03-02 17:30:48 +01:00
|
|
|
),
|
|
|
|
'mov' => array(
|
2015-07-08 15:58:38 +02:00
|
|
|
"mpeg" ,"mpg" ,"mp4" ,"m1v" ,"mp2" ,"mpa" ,"mpe" ,"ifo" ,"vob","avi" ,"wmv" ,"asf" ,"m2v" ,"qt", "ogv", "webm"
|
2012-03-02 17:30:48 +01:00
|
|
|
),
|
|
|
|
'zip' => array(
|
|
|
|
"arc" ,"rar" ,"tar" ,"gz" ,"tgz" ,"bz2" ,"dmg" ,"jar","ace" ,"arj" ,"bz" ,"cab"
|
|
|
|
),
|
|
|
|
'image' => array(
|
|
|
|
"bmp" ,"gif" ,"jpg" ,"jpeg" ,"pcx" ,"tif" ,"png" ,"alpha","als" ,"cel" ,"icon" ,"ico" ,"ps"
|
|
|
|
),
|
|
|
|
'flash' => array(
|
|
|
|
'swf', 'fla'
|
2012-09-04 01:08:38 +02:00
|
|
|
),
|
|
|
|
'doc' => array(
|
|
|
|
'doc','docx','txt','rtf','xls','xlsx','pages', 'ppt','pptx','pps','csv', 'html','htm','xhtml', 'xml','pdf'
|
2012-03-02 17:30:48 +01:00
|
|
|
)
|
|
|
|
);
|
|
|
|
|
2010-04-12 04:35:12 +02:00
|
|
|
/**
|
2013-03-21 19:48:54 +01:00
|
|
|
* @config
|
2010-10-15 05:04:27 +02:00
|
|
|
* @var If this is true, then restrictions set in {@link $allowed_max_file_size} and
|
|
|
|
* {@link $allowed_extensions} will be applied to users with admin privileges as
|
2010-04-12 04:35:12 +02:00
|
|
|
* well.
|
|
|
|
*/
|
2013-03-21 19:48:54 +01:00
|
|
|
private static $apply_restrictions_to_admin = true;
|
2009-02-02 00:49:53 +01:00
|
|
|
|
2013-03-21 19:48:54 +01:00
|
|
|
/**
|
|
|
|
* @config
|
|
|
|
* @var boolean
|
|
|
|
*/
|
|
|
|
private static $update_filesystem = true;
|
2012-03-13 23:01:02 +01:00
|
|
|
|
2008-03-07 04:19:49 +01:00
|
|
|
/**
|
|
|
|
* Cached result of a "SHOW FIELDS" call
|
|
|
|
* in instance_get() for performance reasons.
|
|
|
|
*
|
|
|
|
* @var array
|
|
|
|
*/
|
|
|
|
protected static $cache_file_fields = null;
|
2012-03-01 09:13:42 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Replace "[file_link id=n]" shortcode with an anchor tag or link to the file.
|
|
|
|
* @param $arguments array Arguments to the shortcode
|
|
|
|
* @param $content string Content of the returned link (optional)
|
|
|
|
* @param $parser object Specify a parser to parse the content (see {@link ShortCodeParser})
|
|
|
|
* @return string anchor HTML tag if content argument given, otherwise file path link
|
|
|
|
*/
|
|
|
|
public static function link_shortcode_handler($arguments, $content = null, $parser = null) {
|
|
|
|
if(!isset($arguments['id']) || !is_numeric($arguments['id'])) return;
|
|
|
|
|
2012-06-19 12:45:44 +02:00
|
|
|
$record = DataObject::get_by_id('File', $arguments['id']);
|
|
|
|
|
|
|
|
if (!$record) {
|
|
|
|
if(class_exists('ErrorPage')) {
|
2013-06-21 00:32:08 +02:00
|
|
|
$record = ErrorPage::get()->filter("ErrorCode", 404)->first();
|
2012-06-19 12:45:44 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!$record) return; // There were no suitable matches at all.
|
2012-03-01 09:13:42 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// build the HTML tag
|
|
|
|
if($content) {
|
|
|
|
// build some useful meta-data (file type and size) as data attributes
|
|
|
|
$attrs = ' ';
|
|
|
|
if($record instanceof File) {
|
|
|
|
foreach(array(
|
|
|
|
'class' => 'file',
|
|
|
|
'data-type' => $record->getExtension(),
|
|
|
|
'data-size' => $record->getSize()
|
|
|
|
) as $name => $value) {
|
|
|
|
$attrs .= sprintf('%s="%s" ', $name, $value);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return sprintf('<a href="%s"%s>%s</a>', $record->Link(), rtrim($attrs), $parser->parse($content));
|
|
|
|
} else {
|
|
|
|
return $record->Link();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-04-02 12:27:33 +02:00
|
|
|
/**
|
|
|
|
* A file only exists if the file_exists() and is in the DB as a record
|
|
|
|
*
|
|
|
|
* @return bool
|
|
|
|
*/
|
|
|
|
public function exists() {
|
|
|
|
return parent::exists() && file_exists($this->getFullPath());
|
|
|
|
}
|
|
|
|
|
2008-12-04 23:38:32 +01:00
|
|
|
/**
|
|
|
|
* Find a File object by the given filename.
|
2014-08-15 08:53:05 +02:00
|
|
|
*
|
2010-10-15 05:12:39 +02:00
|
|
|
* @param String $filename Matched against the "Name" property.
|
2008-12-04 23:38:32 +01:00
|
|
|
* @return mixed null if not found, File object of found file
|
|
|
|
*/
|
2012-09-19 12:07:39 +02:00
|
|
|
public static function find($filename) {
|
2008-12-04 23:38:32 +01:00
|
|
|
// Get the base file if $filename points to a resampled file
|
2015-08-17 18:31:12 +02:00
|
|
|
$filename = Image::strip_resampled_prefix($filename);
|
2008-12-04 23:38:32 +01:00
|
|
|
|
2010-10-15 05:12:39 +02:00
|
|
|
// Split to folders and the actual filename, and traverse the structure.
|
2008-12-04 23:38:32 +01:00
|
|
|
$parts = explode("/", $filename);
|
|
|
|
$parentID = 0;
|
|
|
|
$item = null;
|
|
|
|
foreach($parts as $part) {
|
2010-10-15 04:37:41 +02:00
|
|
|
if($part == ASSETS_DIR && !$parentID) continue;
|
2013-06-21 00:32:08 +02:00
|
|
|
$item = File::get()->filter(array(
|
2014-08-15 08:53:05 +02:00
|
|
|
'Name' => $part,
|
2013-06-21 00:32:08 +02:00
|
|
|
'ParentID' => $parentID
|
|
|
|
))->first();
|
2008-12-04 23:38:32 +01:00
|
|
|
if(!$item) break;
|
|
|
|
$parentID = $item->ID;
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2008-12-04 23:38:32 +01:00
|
|
|
return $item;
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2014-08-20 12:14:46 +02:00
|
|
|
/**
|
|
|
|
* Just an alias function to keep a consistent API with SiteTree
|
|
|
|
*
|
|
|
|
* @return string The link to the file
|
|
|
|
*/
|
2012-09-19 12:07:39 +02:00
|
|
|
public function Link() {
|
2014-08-20 12:14:46 +02:00
|
|
|
return $this->getURL();
|
2008-04-06 10:20:13 +02:00
|
|
|
}
|
2007-07-19 12:40:28 +02:00
|
|
|
|
2014-08-20 12:14:46 +02:00
|
|
|
/**
|
|
|
|
* Just an alias function to keep a consistent API with SiteTree
|
|
|
|
*
|
|
|
|
* @return string The relative link to the file
|
|
|
|
*/
|
2012-09-19 12:07:39 +02:00
|
|
|
public function RelativeLink() {
|
2014-08-20 12:14:46 +02:00
|
|
|
return $this->getFilename();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Just an alias function to keep a consistent API with SiteTree
|
|
|
|
*
|
|
|
|
* @return string The absolute link to the file
|
|
|
|
*/
|
|
|
|
public function AbsoluteLink() {
|
|
|
|
return $this->getAbsoluteURL();
|
2008-04-06 10:20:13 +02:00
|
|
|
}
|
2012-03-01 09:13:42 +01:00
|
|
|
|
2010-12-16 06:16:06 +01:00
|
|
|
/**
|
|
|
|
* @return string
|
|
|
|
*/
|
2012-09-19 12:07:39 +02:00
|
|
|
public function getTreeTitle() {
|
2012-05-03 17:41:02 +02:00
|
|
|
return Convert::raw2xml($this->Title);
|
2008-04-06 10:20:13 +02:00
|
|
|
}
|
2007-09-14 21:19:32 +02:00
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
/**
|
2008-04-06 10:20:13 +02:00
|
|
|
* Event handler called before deleting from the database.
|
|
|
|
* You can overload this to clean up or otherwise process data before delete this
|
2010-10-15 05:12:39 +02:00
|
|
|
* record. Don't forget to call {@link parent::onBeforeDelete()}, though!
|
2007-07-19 12:40:28 +02:00
|
|
|
*/
|
2008-04-06 10:20:13 +02:00
|
|
|
protected function onBeforeDelete() {
|
|
|
|
parent::onBeforeDelete();
|
2007-09-14 21:19:32 +02:00
|
|
|
|
2010-10-15 05:20:47 +02:00
|
|
|
// ensure that the record is synced with the filesystem before deleting
|
|
|
|
$this->updateFilesystem();
|
|
|
|
|
2015-04-02 12:27:33 +02:00
|
|
|
if($this->exists() && !is_dir($this->getFullPath())) {
|
2008-04-06 10:20:13 +02:00
|
|
|
unlink($this->getFullPath());
|
|
|
|
}
|
2010-10-04 06:52:46 +02:00
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2008-11-07 13:17:42 +01:00
|
|
|
/**
|
|
|
|
* @todo Enforce on filesystem URL level via mod_rewrite
|
2014-08-15 08:53:05 +02:00
|
|
|
*
|
2008-11-07 13:17:42 +01:00
|
|
|
* @return boolean
|
|
|
|
*/
|
2012-09-19 12:07:39 +02:00
|
|
|
public function canView($member = null) {
|
2008-11-07 13:17:42 +01:00
|
|
|
if(!$member) $member = Member::currentUser();
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2008-11-07 13:17:42 +01:00
|
|
|
$results = $this->extend('canView', $member);
|
|
|
|
if($results && is_array($results)) if(!min($results)) return false;
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2008-11-07 13:17:42 +01:00
|
|
|
return true;
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2008-11-07 13:17:42 +01:00
|
|
|
/**
|
|
|
|
* Returns true if the following conditions are met:
|
|
|
|
* - CMS_ACCESS_AssetAdmin
|
2014-08-15 08:53:05 +02:00
|
|
|
*
|
2008-11-07 13:17:42 +01:00
|
|
|
* @todo Decouple from CMS view access
|
2014-08-15 08:53:05 +02:00
|
|
|
*
|
2008-11-07 13:17:42 +01:00
|
|
|
* @return boolean
|
|
|
|
*/
|
2012-09-19 12:07:39 +02:00
|
|
|
public function canEdit($member = null) {
|
2008-11-07 13:17:42 +01:00
|
|
|
if(!$member) $member = Member::currentUser();
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2011-01-11 05:40:04 +01:00
|
|
|
$result = $this->extendedCan('canEdit', $member);
|
|
|
|
if($result !== null) return $result;
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2015-04-20 08:02:08 +02:00
|
|
|
return Permission::checkMember($member, array('CMS_ACCESS_AssetAdmin', 'CMS_ACCESS_LeftAndMain'));
|
2008-11-07 13:17:42 +01:00
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2008-11-07 13:17:42 +01:00
|
|
|
/**
|
|
|
|
* @return boolean
|
|
|
|
*/
|
2012-09-19 12:07:39 +02:00
|
|
|
public function canCreate($member = null) {
|
2008-11-07 13:17:42 +01:00
|
|
|
if(!$member) $member = Member::currentUser();
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2011-01-11 05:40:04 +01:00
|
|
|
$result = $this->extendedCan('canCreate', $member);
|
|
|
|
if($result !== null) return $result;
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2008-11-07 13:17:42 +01:00
|
|
|
return $this->canEdit($member);
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2008-11-07 13:17:42 +01:00
|
|
|
/**
|
|
|
|
* @return boolean
|
|
|
|
*/
|
2012-09-19 12:07:39 +02:00
|
|
|
public function canDelete($member = null) {
|
2008-11-07 13:17:42 +01:00
|
|
|
if(!$member) $member = Member::currentUser();
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2008-11-07 13:17:42 +01:00
|
|
|
$results = $this->extend('canDelete', $member);
|
|
|
|
if($results && is_array($results)) if(!min($results)) return false;
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2008-11-07 13:17:42 +01:00
|
|
|
return $this->canEdit($member);
|
|
|
|
}
|
2012-01-09 17:14:40 +01:00
|
|
|
|
2012-02-28 18:07:55 +01:00
|
|
|
/**
|
2012-03-30 16:55:35 +02:00
|
|
|
* Returns the fields to power the edit screen of files in the CMS.
|
|
|
|
* You can modify this FieldList by subclassing folder, or by creating a {@link DataExtension}
|
|
|
|
* and implemeting updateCMSFields(FieldList $fields) on that extension.
|
2014-08-15 08:53:05 +02:00
|
|
|
*
|
2012-02-28 18:07:55 +01:00
|
|
|
* @return FieldList
|
2012-02-23 22:43:37 +01:00
|
|
|
*/
|
2012-09-19 12:07:39 +02:00
|
|
|
public function getCMSFields() {
|
2012-02-28 18:07:55 +01:00
|
|
|
// Preview
|
|
|
|
if($this instanceof Image) {
|
2013-03-21 19:48:54 +01:00
|
|
|
$formattedImage = $this->getFormattedImage(
|
2015-06-13 01:42:51 +02:00
|
|
|
'ScaleWidth',
|
2013-03-21 19:48:54 +01:00
|
|
|
Config::inst()->get('Image', 'asset_preview_width')
|
|
|
|
);
|
2012-02-28 18:07:55 +01:00
|
|
|
$thumbnail = $formattedImage ? $formattedImage->URL : '';
|
|
|
|
$previewField = new LiteralField("ImageFull",
|
2014-08-15 08:53:05 +02:00
|
|
|
"<img id='thumbnailImage' class='thumbnail-preview' src='{$thumbnail}?r="
|
2012-09-26 23:34:00 +02:00
|
|
|
. rand(1,100000) . "' alt='{$this->Name}' />\n"
|
2012-02-28 18:07:55 +01:00
|
|
|
);
|
|
|
|
} else {
|
|
|
|
$previewField = new LiteralField("ImageFull", $this->CMSThumbnail());
|
|
|
|
}
|
|
|
|
|
|
|
|
// Upload
|
2013-05-27 05:22:59 +02:00
|
|
|
$uploadField = UploadField::create('UploadField','Upload Field')
|
|
|
|
->setPreviewMaxWidth(40)
|
|
|
|
->setPreviewMaxHeight(30)
|
|
|
|
->setAllowedMaxFileNumber(1);
|
2012-02-23 22:43:37 +01:00
|
|
|
//$uploadField->setTemplate('FileEditUploadField');
|
|
|
|
if ($this->ParentID) {
|
|
|
|
$parent = $this->Parent();
|
|
|
|
if ($parent) { //set the parent that the Upload field should use for uploads
|
|
|
|
$uploadField->setFolderName($parent->getFilename());
|
|
|
|
$uploadField->setRecord($parent);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
//create the file attributes in a FieldGroup
|
2012-04-04 16:59:30 +02:00
|
|
|
$filePreview = CompositeField::create(
|
|
|
|
CompositeField::create(
|
2012-02-28 18:07:55 +01:00
|
|
|
$previewField
|
|
|
|
)->setName("FilePreviewImage")->addExtraClass('cms-file-info-preview'),
|
2012-04-04 16:59:30 +02:00
|
|
|
CompositeField::create(
|
|
|
|
CompositeField::create(
|
2012-02-28 18:07:55 +01:00
|
|
|
new ReadonlyField("FileType", _t('AssetTableField.TYPE','File type') . ':'),
|
|
|
|
new ReadonlyField("Size", _t('AssetTableField.SIZE','File size') . ':', $this->getSize()),
|
|
|
|
$urlField = new ReadonlyField('ClickableURL', _t('AssetTableField.URL','URL'),
|
|
|
|
sprintf('<a href="%s" target="_blank">%s</a>', $this->Link(), $this->RelativeLink())
|
|
|
|
),
|
|
|
|
new DateField_Disabled("Created", _t('AssetTableField.CREATED','First uploaded') . ':'),
|
2012-06-19 12:45:44 +02:00
|
|
|
new DateField_Disabled("LastEdited", _t('AssetTableField.LASTEDIT','Last changed') . ':')
|
2012-02-28 18:07:55 +01:00
|
|
|
)
|
|
|
|
)->setName("FilePreviewData")->addExtraClass('cms-file-info-data')
|
|
|
|
)->setName("FilePreview")->addExtraClass('cms-file-info');
|
|
|
|
$urlField->dontEscape = true;
|
2012-02-23 22:43:37 +01:00
|
|
|
|
2012-03-07 04:40:24 +01:00
|
|
|
//get a tree listing with only folder, no files
|
|
|
|
$folderTree = new TreeDropdownField("ParentID", _t('AssetTableField.FOLDER','Folder'), 'Folder');
|
|
|
|
|
|
|
|
$fields = new FieldList(
|
2012-01-09 17:14:40 +01:00
|
|
|
new TabSet('Root',
|
2012-02-23 22:43:37 +01:00
|
|
|
new Tab('Main',
|
|
|
|
$filePreview,
|
2012-02-28 18:07:55 +01:00
|
|
|
//TODO: make the uploadField replace the existing file
|
|
|
|
// $uploadField,
|
2012-01-09 17:14:40 +01:00
|
|
|
new TextField("Title", _t('AssetTableField.TITLE','Title')),
|
|
|
|
new TextField("Name", _t('AssetTableField.FILENAME','Filename')),
|
2015-01-15 06:52:46 +01:00
|
|
|
$ownerField
|
|
|
|
= new DropdownField("OwnerID", _t('AssetTableField.OWNER','Owner'), Member::mapInCMSGroups()),
|
2012-03-07 04:40:24 +01:00
|
|
|
$folderTree
|
2012-01-09 17:14:40 +01:00
|
|
|
)
|
|
|
|
)
|
|
|
|
);
|
2015-01-15 06:52:46 +01:00
|
|
|
$ownerField->setHasEmptyDefault(true);
|
2012-03-07 04:40:24 +01:00
|
|
|
|
2012-03-30 16:55:35 +02:00
|
|
|
// Folder has its own updateCMSFields hook
|
|
|
|
if(!($this instanceof Folder)) $this->extend('updateCMSFields', $fields);
|
|
|
|
|
2012-03-07 04:40:24 +01:00
|
|
|
return $fields;
|
2012-01-09 17:14:40 +01:00
|
|
|
}
|
2012-03-07 04:40:24 +01:00
|
|
|
|
2010-10-15 05:12:39 +02:00
|
|
|
/**
|
|
|
|
* Returns a category based on the file extension.
|
|
|
|
* This can be useful when grouping files by type,
|
|
|
|
* showing icons on filelinks, etc.
|
|
|
|
* Possible group values are: "audio", "mov", "zip", "image".
|
2014-08-15 08:53:05 +02:00
|
|
|
*
|
2010-10-15 05:12:39 +02:00
|
|
|
* @return String
|
|
|
|
*/
|
2012-06-25 17:13:18 +02:00
|
|
|
public static function get_app_category($ext) {
|
|
|
|
$ext = strtolower($ext);
|
2013-03-21 19:48:54 +01:00
|
|
|
foreach(Config::inst()->get('File', 'app_categories') as $category => $exts) {
|
2012-03-02 17:30:48 +01:00
|
|
|
if(in_array($ext, $exts)) return $category;
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
2012-03-02 17:30:48 +01:00
|
|
|
return false;
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2012-06-25 17:13:18 +02:00
|
|
|
/**
|
|
|
|
* Returns a category based on the file extension.
|
2014-08-15 08:53:05 +02:00
|
|
|
*
|
2012-06-25 17:13:18 +02:00
|
|
|
* @return String
|
|
|
|
*/
|
|
|
|
public function appCategory() {
|
2013-03-07 17:09:48 +01:00
|
|
|
return self::get_app_category($this->getExtension());
|
2012-06-25 17:13:18 +02:00
|
|
|
}
|
2007-09-14 21:19:32 +02:00
|
|
|
|
2016-03-15 19:12:38 +01:00
|
|
|
/**
|
|
|
|
* Return image markup for use as a thumbnail in a strip
|
|
|
|
*
|
|
|
|
* @return HTMLVarchar
|
|
|
|
*/
|
|
|
|
public function StripThumbnail() {
|
|
|
|
return DBField::create_field('HTMLVarchar', '<img src="'.$this->Icon().'"/>');
|
|
|
|
}
|
|
|
|
|
2012-09-19 12:07:39 +02:00
|
|
|
public function CMSThumbnail() {
|
2012-01-09 11:36:39 +01:00
|
|
|
return '<img src="' . $this->Icon() . '" />';
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
2007-09-14 21:19:32 +02:00
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
/**
|
2010-10-15 05:12:39 +02:00
|
|
|
* Return the relative URL of an icon for the file type,
|
|
|
|
* based on the {@link appCategory()} value.
|
2012-03-24 04:38:57 +01:00
|
|
|
* Images are searched for in "framework/images/app_icons/".
|
2014-08-15 08:53:05 +02:00
|
|
|
*
|
|
|
|
* @return String
|
2007-07-19 12:40:28 +02:00
|
|
|
*/
|
2012-09-19 12:07:39 +02:00
|
|
|
public function Icon() {
|
2013-03-07 17:09:48 +01:00
|
|
|
$ext = strtolower($this->getExtension());
|
2016-03-15 19:12:38 +01:00
|
|
|
if(!Director::fileExists(FRAMEWORK_DIR . "/images/app_icons/{$ext}_32.png")) {
|
2007-07-19 12:40:28 +02:00
|
|
|
$ext = $this->appCategory();
|
|
|
|
}
|
|
|
|
|
2016-03-15 19:12:38 +01:00
|
|
|
if(!Director::fileExists(FRAMEWORK_DIR . "/images/app_icons/{$ext}_32.png")) {
|
2007-07-19 12:40:28 +02:00
|
|
|
$ext = "generic";
|
|
|
|
}
|
2007-09-14 21:19:32 +02:00
|
|
|
|
2016-03-15 19:12:38 +01:00
|
|
|
return FRAMEWORK_DIR . "/images/app_icons/{$ext}_32.png";
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2009-04-29 01:52:15 +02:00
|
|
|
/**
|
2014-08-15 08:53:05 +02:00
|
|
|
* Should be called after the file was uploaded
|
|
|
|
*/
|
2012-09-19 12:07:39 +02:00
|
|
|
public function onAfterUpload() {
|
2009-04-29 01:52:15 +02:00
|
|
|
$this->extend('onAfterUpload');
|
|
|
|
}
|
2007-09-14 21:19:32 +02:00
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
/**
|
|
|
|
* Delete the database record (recursively for folders) without touching the filesystem
|
|
|
|
*/
|
2008-05-24 03:21:04 +02:00
|
|
|
public function deleteDatabaseOnly() {
|
2013-06-21 00:32:08 +02:00
|
|
|
if(is_numeric($this->ID)) {
|
|
|
|
DB::prepared_query('DELETE FROM "File" WHERE "ID" = ?', array($this->ID));
|
|
|
|
}
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2010-10-19 06:00:57 +02:00
|
|
|
* Make sure the file has a name
|
2007-07-19 12:40:28 +02:00
|
|
|
*/
|
|
|
|
protected function onBeforeWrite() {
|
|
|
|
parent::onBeforeWrite();
|
|
|
|
|
2012-02-07 18:32:04 +01:00
|
|
|
// Set default owner
|
2013-06-14 18:30:09 +02:00
|
|
|
if(!$this->ID && !$this->OwnerID) {
|
|
|
|
$this->OwnerID = (Member::currentUser() ? Member::currentUser()->ID : 0);
|
|
|
|
}
|
2012-02-07 18:32:04 +01:00
|
|
|
|
2010-10-15 05:12:39 +02:00
|
|
|
// Set default name
|
2010-10-15 05:20:47 +02:00
|
|
|
if(!$this->getField('Name')) $this->Name = "new-" . strtolower($this->class);
|
2010-10-19 06:00:57 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Set name on filesystem. If the current object is a "Folder", will also update references
|
|
|
|
* to subfolders and contained file records (both in database and filesystem)
|
|
|
|
*/
|
|
|
|
protected function onAfterWrite() {
|
|
|
|
parent::onAfterWrite();
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2010-10-15 05:20:47 +02:00
|
|
|
$this->updateFilesystem();
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2010-10-15 05:20:47 +02:00
|
|
|
/**
|
|
|
|
* Moving the file if appropriate according to updated database content.
|
|
|
|
* Throws an Exception if the new file already exists.
|
2014-08-15 08:53:05 +02:00
|
|
|
*
|
2010-10-15 05:20:47 +02:00
|
|
|
* Caution: This method should just be called during a {@link write()} invocation,
|
|
|
|
* as it relies on {@link DataObject->isChanged()}, which is reset after a {@link write()} call.
|
|
|
|
* Might be called as {@link File->updateFilesystem()} from within {@link Folder->updateFilesystem()},
|
|
|
|
* so it has to handle both files and folders.
|
2014-08-15 08:53:05 +02:00
|
|
|
*
|
2010-10-15 05:20:47 +02:00
|
|
|
* Assumes that the "Filename" property was previously updated, either directly or indirectly.
|
|
|
|
* (it might have been influenced by {@link setName()} or {@link setParentID()} before).
|
|
|
|
*/
|
|
|
|
public function updateFilesystem() {
|
2013-03-21 19:48:54 +01:00
|
|
|
if(!$this->config()->update_filesystem) return false;
|
2012-03-13 23:01:02 +01:00
|
|
|
|
2010-10-15 05:20:47 +02:00
|
|
|
// Regenerate "Filename", just to be sure
|
|
|
|
$this->setField('Filename', $this->getRelativePath());
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2010-10-15 05:20:47 +02:00
|
|
|
// If certain elements are changed, update the filesystem reference
|
|
|
|
if(!$this->isChanged('Filename')) return false;
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2010-10-15 05:20:47 +02:00
|
|
|
$changedFields = $this->getChangedFields();
|
|
|
|
$pathBefore = $changedFields['Filename']['before'];
|
|
|
|
$pathAfter = $changedFields['Filename']['after'];
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2010-10-15 05:20:47 +02:00
|
|
|
// If the file or folder didn't exist before, don't rename - its created
|
|
|
|
if(!$pathBefore) return;
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2010-10-15 05:20:47 +02:00
|
|
|
$pathBeforeAbs = Director::getAbsFile($pathBefore);
|
|
|
|
$pathAfterAbs = Director::getAbsFile($pathAfter);
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2010-10-15 05:20:47 +02:00
|
|
|
// TODO Fix Filetest->testCreateWithFilenameWithSubfolder() to enable this
|
|
|
|
// // Create parent folders recursively in database and filesystem
|
|
|
|
// if(!is_a($this, 'Folder')) {
|
|
|
|
// $folder = Folder::findOrMake(dirname($pathAfterAbs));
|
|
|
|
// if($folder) $this->ParentID = $folder->ID;
|
|
|
|
// }
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2010-10-15 05:20:47 +02:00
|
|
|
// Check that original file or folder exists, and rename on filesystem if required.
|
|
|
|
// The folder of the path might've already been renamed by Folder->updateFilesystem()
|
|
|
|
// before any filesystem update on contained file or subfolder records is triggered.
|
|
|
|
if(!file_exists($pathAfterAbs)) {
|
|
|
|
if(!is_a($this, 'Folder')) {
|
|
|
|
// Only throw a fatal error if *both* before and after paths don't exist.
|
2012-09-26 23:34:00 +02:00
|
|
|
if(!file_exists($pathBeforeAbs)) {
|
2013-05-28 19:08:59 +02:00
|
|
|
throw new Exception("Cannot move $pathBeforeAbs to $pathAfterAbs - $pathBeforeAbs doesn't exist");
|
2012-09-26 23:34:00 +02:00
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2010-10-15 05:20:47 +02:00
|
|
|
// Check that target directory (not the file itself) exists.
|
|
|
|
// Only check if we're dealing with a file, otherwise the folder will need to be created
|
2012-09-26 23:34:00 +02:00
|
|
|
if(!file_exists(dirname($pathAfterAbs))) {
|
2013-05-28 19:08:59 +02:00
|
|
|
throw new Exception("Cannot move $pathBeforeAbs to $pathAfterAbs - Directory " . dirname($pathAfter)
|
2012-09-26 23:34:00 +02:00
|
|
|
. " doesn't exist");
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
}
|
2012-03-13 23:01:02 +01:00
|
|
|
|
2010-10-15 05:20:47 +02:00
|
|
|
// Rename file or folder
|
|
|
|
$success = rename($pathBeforeAbs, $pathAfterAbs);
|
|
|
|
if(!$success) throw new Exception("Cannot move $pathBeforeAbs to $pathAfterAbs");
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
|
|
|
|
2010-10-15 05:20:47 +02:00
|
|
|
// Update any database references
|
|
|
|
$this->updateLinks($pathBefore, $pathAfter);
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
2007-09-14 21:19:32 +02:00
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
/**
|
2007-09-14 21:19:32 +02:00
|
|
|
* Collate selected descendants of this page.
|
2007-07-19 12:40:28 +02:00
|
|
|
* $condition will be evaluated on each descendant, and if it is succeeds, that item will be added
|
|
|
|
* to the $collator array.
|
|
|
|
* @param condition The PHP condition to be evaluated. The page will be called $item
|
|
|
|
* @param collator An array, passed by reference, to collect all of the matching descendants.
|
|
|
|
*/
|
|
|
|
public function collateDescendants($condition, &$collator) {
|
|
|
|
if($children = $this->Children()) {
|
|
|
|
foreach($children as $item) {
|
|
|
|
if(!$condition || eval("return $condition;")) $collator[] = $item;
|
|
|
|
$item->collateDescendants($condition, $collator);
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
2007-09-14 21:19:32 +02:00
|
|
|
}
|
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
/**
|
2010-10-15 05:12:39 +02:00
|
|
|
* Setter function for Name. Automatically sets a default title,
|
2010-10-15 05:20:47 +02:00
|
|
|
* and removes characters that might be invalid on the filesystem.
|
2010-10-15 05:12:39 +02:00
|
|
|
* Also adds a suffix to the name if the filename already exists
|
|
|
|
* on the filesystem, and is associated to a different {@link File} database record
|
|
|
|
* in the same folder. This means "myfile.jpg" might become "myfile-1.jpg".
|
2014-08-15 08:53:05 +02:00
|
|
|
*
|
2010-10-15 05:20:47 +02:00
|
|
|
* Does not change the filesystem itself, please use {@link write()} for this.
|
2014-08-15 08:53:05 +02:00
|
|
|
*
|
2010-10-15 05:12:39 +02:00
|
|
|
* @param String $name
|
2007-07-19 12:40:28 +02:00
|
|
|
*/
|
2012-09-19 12:07:39 +02:00
|
|
|
public function setName($name) {
|
2007-07-19 12:40:28 +02:00
|
|
|
$oldName = $this->Name;
|
2007-09-14 21:19:32 +02:00
|
|
|
|
2010-10-15 05:12:39 +02:00
|
|
|
// It can't be blank, default to Title
|
2007-07-19 12:40:28 +02:00
|
|
|
if(!$name) $name = $this->Title;
|
|
|
|
|
|
|
|
// Fix illegal characters
|
2012-04-04 16:59:30 +02:00
|
|
|
$filter = FileNameFilter::create();
|
2011-08-26 17:57:05 +02:00
|
|
|
$name = $filter->filter($name);
|
2010-01-21 23:59:19 +01:00
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
// We might have just turned it blank, so check again.
|
|
|
|
if(!$name) $name = 'new-folder';
|
|
|
|
|
|
|
|
// If it's changed, check for duplicates
|
|
|
|
if($oldName && $oldName != $name) {
|
2015-05-19 07:43:00 +02:00
|
|
|
$base = pathinfo($name, PATHINFO_FILENAME);
|
2010-10-15 05:15:38 +02:00
|
|
|
$ext = self::get_file_extension($name);
|
2007-07-19 12:40:28 +02:00
|
|
|
$suffix = 1;
|
2012-09-26 23:34:00 +02:00
|
|
|
|
2014-01-12 08:54:55 +01:00
|
|
|
while(File::get()->filter(array(
|
2014-08-15 08:53:05 +02:00
|
|
|
'Name' => $name,
|
2014-01-12 08:54:55 +01:00
|
|
|
'ParentID' => (int) $this->ParentID
|
|
|
|
))->exclude(array(
|
|
|
|
'ID' => $this->ID
|
|
|
|
))->first()
|
|
|
|
) {
|
2007-07-19 12:40:28 +02:00
|
|
|
$suffix++;
|
2015-05-19 07:43:00 +02:00
|
|
|
$name = "$base-$suffix.$ext";
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
|
|
|
}
|
2007-09-14 21:19:32 +02:00
|
|
|
|
2010-10-15 05:12:39 +02:00
|
|
|
// Update actual field value
|
2007-07-19 12:40:28 +02:00
|
|
|
$this->setField('Name', $name);
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2010-10-15 05:20:47 +02:00
|
|
|
// Ensure that the filename is updated as well (only in-memory)
|
|
|
|
// Important: Circumvent the getter to avoid infinite loops
|
|
|
|
$this->setField('Filename', $this->getRelativePath());
|
2014-05-02 04:41:10 +02:00
|
|
|
|
|
|
|
// Update title
|
|
|
|
if(!$this->Title) {
|
|
|
|
$this->Title = str_replace(array('-','_'),' ', preg_replace('/\.[^.]+$/', '', $name));
|
|
|
|
}
|
|
|
|
|
|
|
|
return $name;
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
2007-09-14 21:19:32 +02:00
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
/**
|
2010-10-15 05:12:39 +02:00
|
|
|
* @param String $old File path relative to the webroot
|
|
|
|
* @param String $new File path relative to the webroot
|
2007-07-19 12:40:28 +02:00
|
|
|
*/
|
|
|
|
protected function updateLinks($old, $new) {
|
2011-03-22 22:40:29 +01:00
|
|
|
$this->extend('updateLinks', $old, $new);
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
2007-09-14 21:19:32 +02:00
|
|
|
|
2010-10-15 05:20:47 +02:00
|
|
|
/**
|
|
|
|
* Does not change the filesystem itself, please use {@link write()} for this.
|
|
|
|
*/
|
2012-09-19 12:07:39 +02:00
|
|
|
public function setParentID($parentID) {
|
2009-11-21 03:33:30 +01:00
|
|
|
$this->setField('ParentID', (int)$parentID);
|
2007-07-19 12:40:28 +02:00
|
|
|
|
2010-10-15 05:20:47 +02:00
|
|
|
// Don't change on the filesystem, we'll handle that in onBeforeWrite()
|
2014-08-15 08:53:05 +02:00
|
|
|
$this->setField('Filename', $this->getRelativePath());
|
2007-09-14 21:19:32 +02:00
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
return $this->getField('ParentID');
|
|
|
|
}
|
2007-09-14 21:19:32 +02:00
|
|
|
|
2008-03-18 00:50:18 +01:00
|
|
|
/**
|
|
|
|
* Gets the absolute URL accessible through the web.
|
2014-08-15 08:53:05 +02:00
|
|
|
*
|
2008-03-18 00:50:18 +01:00
|
|
|
* @uses Director::absoluteBaseURL()
|
|
|
|
* @return string
|
|
|
|
*/
|
2012-09-19 12:07:39 +02:00
|
|
|
public function getAbsoluteURL() {
|
2014-08-20 12:14:46 +02:00
|
|
|
return Director::absoluteURL($this->getURL());
|
2008-03-18 00:50:18 +01:00
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2008-03-18 00:50:18 +01:00
|
|
|
/**
|
2010-04-12 05:29:56 +02:00
|
|
|
* Gets the relative URL accessible through the web.
|
2014-08-15 08:53:05 +02:00
|
|
|
*
|
2010-04-12 05:29:56 +02:00
|
|
|
* @uses Director::baseURL()
|
2008-03-18 00:50:18 +01:00
|
|
|
* @return string
|
|
|
|
*/
|
2012-09-19 12:07:39 +02:00
|
|
|
public function getURL() {
|
2017-06-05 05:17:37 +02:00
|
|
|
$url = Controller::join_links(Director::baseURL(), $this->getFilename());
|
|
|
|
return $this->extend('updateURL', $url);
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
2007-09-14 21:19:32 +02:00
|
|
|
|
2010-10-15 05:12:39 +02:00
|
|
|
/**
|
|
|
|
* Returns an absolute filesystem path to the file.
|
|
|
|
* Use {@link getRelativePath()} to get the same path relative to the webroot.
|
2014-08-15 08:53:05 +02:00
|
|
|
*
|
|
|
|
* @return String
|
2010-10-15 05:12:39 +02:00
|
|
|
*/
|
2012-09-19 12:07:39 +02:00
|
|
|
public function getFullPath() {
|
2008-08-13 00:26:52 +02:00
|
|
|
$baseFolder = Director::baseFolder();
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2008-08-13 00:26:52 +02:00
|
|
|
if(strpos($this->getFilename(), $baseFolder) === 0) {
|
|
|
|
// if path is absolute already, just return
|
|
|
|
return $this->getFilename();
|
|
|
|
} else {
|
|
|
|
// otherwise assume silverstripe-basefolder
|
|
|
|
return Director::baseFolder() . '/' . $this->getFilename();
|
|
|
|
}
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
2007-09-14 21:19:32 +02:00
|
|
|
|
2010-10-13 01:02:10 +02:00
|
|
|
/**
|
2010-10-15 05:12:39 +02:00
|
|
|
* Returns path relative to webroot.
|
2010-10-15 05:20:47 +02:00
|
|
|
* Serves as a "fallback" method to create the "Filename" property if it isn't set.
|
2010-10-15 05:12:39 +02:00
|
|
|
* If no {@link Folder} is set ("ParentID" property),
|
|
|
|
* defaults to a filename relative to the ASSETS_DIR (usually "assets/").
|
2014-08-15 08:53:05 +02:00
|
|
|
*
|
2010-10-13 01:02:10 +02:00
|
|
|
* @return String
|
|
|
|
*/
|
2012-09-19 12:07:39 +02:00
|
|
|
public function getRelativePath() {
|
2007-07-19 12:40:28 +02:00
|
|
|
if($this->ParentID) {
|
2012-09-26 23:34:00 +02:00
|
|
|
// Don't use the cache, the parent has just been changed
|
2014-08-15 08:53:05 +02:00
|
|
|
$p = DataObject::get_by_id('Folder', $this->ParentID, false);
|
2016-03-21 05:22:38 +01:00
|
|
|
if($p && $p->isInDB()) {
|
|
|
|
return $p->getRelativePath() . $this->getField("Name");
|
|
|
|
}
|
ENHANCEMENT Introduced constants for system paths like /sapphire in preparation for a more flexible directory reorganisation. Instead of hardcoding your path, please use the following constants: BASE_PATH, BASE_URL, SAPPHIRE_DIR, SAPPHIRE_PATH, CMS_DIR, CMS_PATH, THIRDPARTY_DIR, THIRDPARTY_PATH, ASSETS_DIR, ASSETS_PATH, THEMES_DIR, THEMES_PATH
git-svn-id: svn://svn.silverstripe.com/silverstripe/open/modules/sapphire/trunk@63154 467b73ca-7a2a-4603-9d3b-597d59a354a9
2008-09-27 18:02:38 +02:00
|
|
|
else return ASSETS_DIR . "/" . $this->getField("Name");
|
2007-07-19 12:40:28 +02:00
|
|
|
} else if($this->getField("Name")) {
|
ENHANCEMENT Introduced constants for system paths like /sapphire in preparation for a more flexible directory reorganisation. Instead of hardcoding your path, please use the following constants: BASE_PATH, BASE_URL, SAPPHIRE_DIR, SAPPHIRE_PATH, CMS_DIR, CMS_PATH, THIRDPARTY_DIR, THIRDPARTY_PATH, ASSETS_DIR, ASSETS_PATH, THEMES_DIR, THEMES_PATH
git-svn-id: svn://svn.silverstripe.com/silverstripe/open/modules/sapphire/trunk@63154 467b73ca-7a2a-4603-9d3b-597d59a354a9
2008-09-27 18:02:38 +02:00
|
|
|
return ASSETS_DIR . "/" . $this->getField("Name");
|
2007-07-19 12:40:28 +02:00
|
|
|
} else {
|
ENHANCEMENT Introduced constants for system paths like /sapphire in preparation for a more flexible directory reorganisation. Instead of hardcoding your path, please use the following constants: BASE_PATH, BASE_URL, SAPPHIRE_DIR, SAPPHIRE_PATH, CMS_DIR, CMS_PATH, THIRDPARTY_DIR, THIRDPARTY_PATH, ASSETS_DIR, ASSETS_PATH, THEMES_DIR, THEMES_PATH
git-svn-id: svn://svn.silverstripe.com/silverstripe/open/modules/sapphire/trunk@63154 467b73ca-7a2a-4603-9d3b-597d59a354a9
2008-09-27 18:02:38 +02:00
|
|
|
return ASSETS_DIR;
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
|
|
|
}
|
2007-09-14 21:19:32 +02:00
|
|
|
|
2010-10-15 05:12:39 +02:00
|
|
|
/**
|
|
|
|
* @todo Coupling with cms module, remove this method.
|
|
|
|
*/
|
2012-09-19 12:07:39 +02:00
|
|
|
public function DeleteLink() {
|
2007-07-19 12:40:28 +02:00
|
|
|
return Director::absoluteBaseURL()."admin/assets/removefile/".$this->ID;
|
|
|
|
}
|
2007-09-14 21:19:32 +02:00
|
|
|
|
2012-09-19 12:07:39 +02:00
|
|
|
public function getFilename() {
|
2010-10-15 05:20:47 +02:00
|
|
|
// Default behaviour: Return field if its set
|
2008-06-25 07:52:58 +02:00
|
|
|
if($this->getField('Filename')) {
|
|
|
|
return $this->getField('Filename');
|
|
|
|
} else {
|
ENHANCEMENT Introduced constants for system paths like /sapphire in preparation for a more flexible directory reorganisation. Instead of hardcoding your path, please use the following constants: BASE_PATH, BASE_URL, SAPPHIRE_DIR, SAPPHIRE_PATH, CMS_DIR, CMS_PATH, THIRDPARTY_DIR, THIRDPARTY_PATH, ASSETS_DIR, ASSETS_PATH, THEMES_DIR, THEMES_PATH
git-svn-id: svn://svn.silverstripe.com/silverstripe/open/modules/sapphire/trunk@63154 467b73ca-7a2a-4603-9d3b-597d59a354a9
2008-09-27 18:02:38 +02:00
|
|
|
return ASSETS_DIR . '/';
|
2008-06-25 07:52:58 +02:00
|
|
|
}
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
2007-09-14 21:19:32 +02:00
|
|
|
|
2010-10-15 05:20:47 +02:00
|
|
|
/**
|
2014-02-26 02:09:09 +01:00
|
|
|
* Caution: this does not change the location of the file on the filesystem.
|
2010-10-15 05:20:47 +02:00
|
|
|
*/
|
2012-09-19 12:07:39 +02:00
|
|
|
public function setFilename($val) {
|
2007-07-19 12:40:28 +02:00
|
|
|
$this->setField('Filename', $val);
|
2014-08-15 08:53:05 +02:00
|
|
|
|
|
|
|
// "Filename" is the "master record" (existing on the filesystem),
|
2010-10-15 05:12:39 +02:00
|
|
|
// meaning we have to adjust the "Name" property in the database as well.
|
2007-07-19 12:40:28 +02:00
|
|
|
$this->setField('Name', basename($val));
|
|
|
|
}
|
2007-09-14 21:19:32 +02:00
|
|
|
|
2010-10-15 05:12:39 +02:00
|
|
|
/**
|
|
|
|
* Returns the file extension
|
2014-08-15 08:53:05 +02:00
|
|
|
*
|
2010-10-15 05:12:39 +02:00
|
|
|
* @todo This overrides getExtension() in DataObject, but it does something completely different.
|
2007-07-19 12:40:28 +02:00
|
|
|
* This should be renamed to getFileExtension(), but has not been yet as it may break
|
|
|
|
* legacy code.
|
2014-08-15 08:53:05 +02:00
|
|
|
*
|
2010-10-15 05:12:39 +02:00
|
|
|
* @return String
|
2007-07-19 12:40:28 +02:00
|
|
|
*/
|
2012-09-19 12:07:39 +02:00
|
|
|
public function getExtension() {
|
2008-04-09 13:08:58 +02:00
|
|
|
return self::get_file_extension($this->getField('Filename'));
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2008-04-09 13:08:58 +02:00
|
|
|
/**
|
|
|
|
* Gets the extension of a filepath or filename,
|
|
|
|
* by stripping away everything before the last "dot".
|
2010-10-15 05:14:59 +02:00
|
|
|
* Caution: Only returns the last extension in "double-barrelled"
|
|
|
|
* extensions (e.g. "gz" for "tar.gz").
|
2014-08-15 08:53:05 +02:00
|
|
|
*
|
2010-10-15 05:14:59 +02:00
|
|
|
* Examples:
|
|
|
|
* - "myfile" returns ""
|
|
|
|
* - "myfile.txt" returns "txt"
|
|
|
|
* - "myfile.tar.gz" returns "gz"
|
2008-04-09 13:08:58 +02:00
|
|
|
*
|
|
|
|
* @param string $filename
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
public static function get_file_extension($filename) {
|
2010-10-15 05:14:59 +02:00
|
|
|
return pathinfo($filename, PATHINFO_EXTENSION);
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2008-10-16 05:55:33 +02:00
|
|
|
/**
|
|
|
|
* Return the type of file for the given extension
|
|
|
|
* on the current file name.
|
|
|
|
*
|
|
|
|
* @return string
|
|
|
|
*/
|
2012-09-19 12:07:39 +02:00
|
|
|
public function getFileType() {
|
2007-07-19 12:40:28 +02:00
|
|
|
$types = array(
|
2012-08-27 00:02:52 +02:00
|
|
|
'gif' => _t('File.GifType', 'GIF image - good for diagrams'),
|
|
|
|
'jpg' => _t('File.JpgType', 'JPEG image - good for photos'),
|
|
|
|
'jpeg' => _t('File.JpgType', 'JPEG image - good for photos'),
|
|
|
|
'png' => _t('File.PngType', 'PNG image - good general-purpose format'),
|
|
|
|
'ico' => _t('File.IcoType', 'Icon image'),
|
|
|
|
'tiff' => _t('File.TiffType', 'Tagged image format'),
|
|
|
|
'doc' => _t('File.DocType', 'Word document'),
|
|
|
|
'xls' => _t('File.XlsType', 'Excel spreadsheet'),
|
|
|
|
'zip' => _t('File.ZipType', 'ZIP compressed file'),
|
|
|
|
'gz' => _t('File.GzType', 'GZIP compressed file'),
|
|
|
|
'dmg' => _t('File.DmgType', 'Apple disk image'),
|
|
|
|
'pdf' => _t('File.PdfType', 'Adobe Acrobat PDF file'),
|
|
|
|
'mp3' => _t('File.Mp3Type', 'MP3 audio file'),
|
|
|
|
'wav' => _t('File.WavType', 'WAV audo file'),
|
|
|
|
'avi' => _t('File.AviType', 'AVI video file'),
|
|
|
|
'mpg' => _t('File.MpgType', 'MPEG video file'),
|
|
|
|
'mpeg' => _t('File.MpgType', 'MPEG video file'),
|
|
|
|
'js' => _t('File.JsType', 'Javascript file'),
|
|
|
|
'css' => _t('File.CssType', 'CSS file'),
|
|
|
|
'html' => _t('File.HtmlType', 'HTML file'),
|
2015-03-10 11:24:03 +01:00
|
|
|
'htm' => _t('File.HtmlType', 'HTML file')
|
2007-07-19 12:40:28 +02:00
|
|
|
);
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2015-05-04 16:10:16 +02:00
|
|
|
$ext = strtolower($this->getExtension());
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
return isset($types[$ext]) ? $types[$ext] : 'unknown';
|
|
|
|
}
|
2007-09-14 21:19:32 +02:00
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
/**
|
|
|
|
* Returns the size of the file type in an appropriate format.
|
|
|
|
*/
|
2012-09-19 12:07:39 +02:00
|
|
|
public function getSize() {
|
2007-07-19 12:40:28 +02:00
|
|
|
$size = $this->getAbsoluteSize();
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2008-04-06 10:20:13 +02:00
|
|
|
return ($size) ? self::format_size($size) : false;
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2012-02-03 00:59:40 +01:00
|
|
|
/**
|
|
|
|
* Formats a file size (eg: (int)42 becomes string '42 bytes')
|
|
|
|
* @param int $size
|
|
|
|
* @return string
|
|
|
|
*/
|
2008-04-06 10:20:13 +02:00
|
|
|
public static function format_size($size) {
|
|
|
|
if($size < 1024) return $size . ' bytes';
|
|
|
|
if($size < 1024*10) return (round($size/1024*10)/10). ' KB';
|
|
|
|
if($size < 1024*1024) return round($size/1024) . ' KB';
|
|
|
|
if($size < 1024*1024*10) return (round(($size/1024)/1024*10)/10) . ' MB';
|
|
|
|
if($size < 1024*1024*1024) return round(($size/1024)/1024) . ' MB';
|
2008-08-25 03:42:27 +02:00
|
|
|
return round($size/(1024*1024*1024)*10)/10 . ' GB';
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2012-02-03 00:59:40 +01:00
|
|
|
/**
|
|
|
|
* Convert a php.ini value (eg: 512M) to bytes
|
2014-08-15 08:53:05 +02:00
|
|
|
*
|
2017-04-04 11:02:21 +02:00
|
|
|
* @param string $iniValue
|
2012-02-03 00:59:40 +01:00
|
|
|
* @return int
|
|
|
|
*/
|
2017-04-04 11:02:21 +02:00
|
|
|
public static function ini2bytes($iniValue) {
|
|
|
|
$iniValues = str_split(trim($iniValue));
|
|
|
|
$unit = strtolower(array_pop($iniValues));
|
|
|
|
$quantity = (int) implode($iniValues);
|
|
|
|
switch ($unit) {
|
2012-02-03 00:59:40 +01:00
|
|
|
case 'g':
|
2017-04-04 11:02:21 +02:00
|
|
|
$quantity *= 1024;
|
|
|
|
// deliberate no break
|
2012-02-03 00:59:40 +01:00
|
|
|
case 'm':
|
2017-04-04 11:02:21 +02:00
|
|
|
$quantity *= 1024;
|
|
|
|
// deliberate no break
|
2012-02-03 00:59:40 +01:00
|
|
|
case 'k':
|
2017-04-04 11:02:21 +02:00
|
|
|
$quantity *= 1024;
|
|
|
|
// deliberate no break
|
|
|
|
default:
|
|
|
|
// no-op: pre-existing behaviour
|
|
|
|
break;
|
2012-02-03 00:59:40 +01:00
|
|
|
}
|
2017-04-04 11:02:21 +02:00
|
|
|
return $quantity;
|
2012-02-03 00:59:40 +01:00
|
|
|
}
|
2007-09-14 21:19:32 +02:00
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
/**
|
2008-12-04 23:38:32 +01:00
|
|
|
* Return file size in bytes.
|
|
|
|
* @return int
|
2007-07-19 12:40:28 +02:00
|
|
|
*/
|
2012-09-19 12:07:39 +02:00
|
|
|
public function getAbsoluteSize(){
|
2008-12-04 23:38:32 +01:00
|
|
|
if(file_exists($this->getFullPath())) {
|
2007-07-19 12:40:28 +02:00
|
|
|
$size = filesize($this->getFullPath());
|
|
|
|
return $size;
|
2008-12-04 23:38:32 +01:00
|
|
|
} else {
|
2007-07-19 12:40:28 +02:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2012-03-27 06:04:11 +02:00
|
|
|
public function flushCache($persistant = true) {
|
|
|
|
parent::flushCache($persistant);
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2008-03-09 06:08:08 +01:00
|
|
|
self::$cache_file_fields = null;
|
2008-03-07 04:19:49 +01:00
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2009-04-29 02:07:39 +02:00
|
|
|
/**
|
|
|
|
*
|
|
|
|
* @param boolean $includerelations a boolean value to indicate if the labels returned include relation fields
|
2014-08-15 08:53:05 +02:00
|
|
|
*
|
2009-04-29 02:07:39 +02:00
|
|
|
*/
|
2012-09-19 12:07:39 +02:00
|
|
|
public function fieldLabels($includerelations = true) {
|
2009-04-29 02:07:39 +02:00
|
|
|
$labels = parent::fieldLabels($includerelations);
|
2008-11-02 21:04:10 +01:00
|
|
|
$labels['Name'] = _t('File.Name', 'Name');
|
|
|
|
$labels['Title'] = _t('File.Title', 'Title');
|
|
|
|
$labels['Filename'] = _t('File.Filename', 'Filename');
|
|
|
|
$labels['Filename'] = _t('File.Filename', 'Filename');
|
|
|
|
$labels['Content'] = _t('File.Content', 'Content');
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2008-11-02 21:04:10 +01:00
|
|
|
return $labels;
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2015-06-16 01:39:32 +02:00
|
|
|
protected function validate() {
|
2013-03-21 19:48:54 +01:00
|
|
|
if($this->config()->apply_restrictions_to_admin || !Permission::check('ADMIN')) {
|
2010-10-15 05:14:59 +02:00
|
|
|
// Extension validation
|
|
|
|
// TODO Merge this with Upload_Validator
|
|
|
|
$extension = $this->getExtension();
|
2013-03-21 19:48:54 +01:00
|
|
|
$allowed = array_map('strtolower', $this->config()->allowed_extensions);
|
2013-04-17 10:21:49 +02:00
|
|
|
if(!in_array(strtolower($extension), $allowed)) {
|
2010-10-15 05:53:54 +02:00
|
|
|
$exts = $allowed;
|
2010-04-12 04:42:51 +02:00
|
|
|
sort($exts);
|
2013-06-13 23:08:44 +02:00
|
|
|
$message = _t(
|
|
|
|
'File.INVALIDEXTENSION',
|
|
|
|
'Extension is not allowed (valid: {extensions})',
|
|
|
|
'Argument 1: Comma-separated list of valid extensions',
|
|
|
|
array('extensions' => wordwrap(implode(', ',$exts)))
|
2010-04-12 04:34:38 +02:00
|
|
|
);
|
|
|
|
return new ValidationResult(false, $message);
|
|
|
|
}
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2010-10-15 05:20:47 +02:00
|
|
|
// We aren't validating for an existing "Filename" on the filesystem.
|
|
|
|
// A record should still be saveable even if the underlying record has been removed.
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2013-12-16 17:07:33 +01:00
|
|
|
$result = new ValidationResult(true);
|
|
|
|
$this->extend('validate', $result);
|
|
|
|
return $result;
|
2010-04-12 04:34:38 +02:00
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2011-08-22 13:04:15 +02:00
|
|
|
/**
|
2013-03-21 19:48:54 +01:00
|
|
|
* @config
|
2011-08-22 13:04:15 +02:00
|
|
|
* @var Array Only use lowercase extensions in here.
|
|
|
|
*/
|
2013-03-21 19:48:54 +01:00
|
|
|
private static $class_for_file_extension = array(
|
2011-08-22 13:04:15 +02:00
|
|
|
'*' => 'File',
|
|
|
|
'jpg' => 'Image',
|
|
|
|
'jpeg' => 'Image',
|
|
|
|
'png' => 'Image',
|
|
|
|
'gif' => 'Image',
|
|
|
|
);
|
2007-07-19 12:40:28 +02:00
|
|
|
|
2011-08-22 13:04:15 +02:00
|
|
|
/**
|
|
|
|
* Maps a {@link File} subclass to a specific extension.
|
|
|
|
* By default, files with common image extensions will be created
|
2014-08-15 08:53:05 +02:00
|
|
|
* as {@link Image} instead of {@link File} when using
|
2011-08-22 13:04:15 +02:00
|
|
|
* {@link Folder::constructChild}, {@link Folder::addUploadToFolder}),
|
|
|
|
* and the {@link Upload} class (either directly or through {@link FileField}).
|
|
|
|
* For manually instanciated files please use this mapping getter.
|
2014-08-15 08:53:05 +02:00
|
|
|
*
|
2011-08-22 13:04:15 +02:00
|
|
|
* Caution: Changes to mapping doesn't apply to existing file records in the database.
|
|
|
|
* Also doesn't hook into {@link Object::getCustomClass()}.
|
2014-08-15 08:53:05 +02:00
|
|
|
*
|
2011-08-22 13:04:15 +02:00
|
|
|
* @param String File extension, without dot prefix. Use an asterisk ('*')
|
|
|
|
* to specify a generic fallback if no mapping is found for an extension.
|
|
|
|
* @return String Classname for a subclass of {@link File}
|
|
|
|
*/
|
2012-09-19 12:07:39 +02:00
|
|
|
public static function get_class_for_file_extension($ext) {
|
2013-03-21 19:48:54 +01:00
|
|
|
$map = array_change_key_case(self::config()->class_for_file_extension, CASE_LOWER);
|
2011-08-22 13:04:15 +02:00
|
|
|
return (array_key_exists(strtolower($ext), $map)) ? $map[strtolower($ext)] : $map['*'];
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2011-08-22 13:04:15 +02:00
|
|
|
/**
|
|
|
|
* See {@link get_class_for_file_extension()}.
|
2014-08-15 08:53:05 +02:00
|
|
|
*
|
2011-08-22 13:04:15 +02:00
|
|
|
* @param String|array
|
|
|
|
* @param String
|
|
|
|
*/
|
2012-09-19 12:07:39 +02:00
|
|
|
public static function set_class_for_file_extension($exts, $class) {
|
2011-08-22 13:04:15 +02:00
|
|
|
if(!is_array($exts)) $exts = array($exts);
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2011-08-22 13:04:15 +02:00
|
|
|
foreach($exts as $ext) {
|
2013-05-07 20:27:34 +02:00
|
|
|
if(!is_subclass_of($class, 'File')) {
|
2011-08-22 13:04:15 +02:00
|
|
|
throw new InvalidArgumentException(
|
|
|
|
sprintf('Class "%s" (for extension "%s") is not a valid subclass of File', $class, $ext)
|
|
|
|
);
|
|
|
|
}
|
2013-03-21 19:48:54 +01:00
|
|
|
self::config()->class_for_file_extension = array($ext => $class);
|
2011-08-22 13:04:15 +02:00
|
|
|
}
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2011-08-22 13:04:15 +02:00
|
|
|
}
|