setOwnerOnly (strtoupper (trim ($field ['owner-only'])) == 'TRUE' ? TRUE : FALSE);
if (array_key_exists ('public', $field))
$this->setPublic (strtoupper (trim ($field ['public'])) == 'TRUE' ? TRUE : FALSE);
if (array_key_exists ('resolution', $field) && is_numeric ($field ['resolution']))
$this->resolution = (int) trim ($field ['resolution']);
if (array_key_exists ('mime-type', $field))
{
$archive = Archive::singleton ();
if (!is_array ($field ['mime-type']))
$field ['mime-type'] = array ($field ['mime-type']);
foreach ($field ['mime-type'] as $trash => $item)
if ($archive->isAcceptable ($item))
$this->mimeTypes [] = $item;
}
}
public function setValue ($value)
{
if (is_null ($value) || (is_numeric ($value) && (int) $value === 0) || (is_string ($value) && $value === ''))
$this->value = NULL;
else
$this->value = $value;
}
public function setPublic ($public)
{
$this->public = (bool) $public;
}
public function isPublic ()
{
return $this->public;
}
public function setOwnerOnly ($ownerOnly)
{
$this->ownerOnly = (bool) $ownerOnly;
}
public function ownerOnly ()
{
return $this->ownerOnly;
}
public function getFilter ()
{
return implode (',', $this->mimeTypes);
}
public function isAcceptable ($mime)
{
if (sizeof ($this->mimeTypes))
return in_array ($mime, $this->mimeTypes);
return Archive::singleton ()->isAcceptable ($mime);
}
public function getInfo ()
{
if (!$this->getValue ())
return NULL;
$sth = Database::singleton ()->prepare ("SELECT * FROM _file WHERE _id = :id");
$sth->bindParam (':id', $this->getValue (), PDO::PARAM_INT);
$sth->execute ();
$obj = $sth->fetch (PDO::FETCH_OBJ);
if (!$obj)
return NULL;
return array ('_NAME_' => $obj->_name,
'_SIZE_' => $obj->_size,
'_MIME_' => $obj->_mimetype,
'_HASH_' => (string) @$obj->_hash);
}
public function isEmpty ()
{
return is_null ($this->getValue ()) || !is_numeric ($this->getValue ()) || $this->getValue () == 0;
}
public function getResolution ()
{
return $this->resolution;
}
public static function getFilePath ($id)
{
return Archive::singleton ()->getDataPath () . 'file_' . str_pad ($id, 19, '0', STR_PAD_LEFT);
}
public static function getLegacyFilePath ($id)
{
return Archive::singleton ()->getDataPath () . 'file_' . str_pad ($id, 7, '0', STR_PAD_LEFT);
}
public static function formatFileSizeForHuman ($bytes, $decimals = 0)
{
$size = array ('B', 'kB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB');
$factor = floor ((strlen ($bytes) - 1) / 3);
return sprintf ("%.{$decimals}f", $bytes / pow (1024, $factor)) .' '. @$size [$factor];
}
public static function resize ($id, $type, $width = 0, $height = 0, $force = FALSE, $bw = FALSE, $crop = FALSE)
{
$source = self::getFilePath ($id);
if (!file_exists ($source))
$source = self::getLegacyFilePath ($id);
$cache = Instance::singleton ()->getCachePath ();
if (!file_exists ($cache . self::ENCODE_FOLDER) && !@mkdir ($cache . self::ENCODE_FOLDER, 0777))
throw new Exception ('Unable create cache directory!');
if (!file_exists ($cache . self::ENCODE_FOLDER . DIRECTORY_SEPARATOR .'.htaccess') && !file_put_contents ($cache . self::ENCODE_FOLDER . DIRECTORY_SEPARATOR .'.htaccess', 'deny from all'))
throw new Exception ('Impossible to enhance security for folder ['. $cache . self::ENCODE_FOLDER .'].');
$destination = $cache . self::ENCODE_FOLDER . DIRECTORY_SEPARATOR .'resized_' . str_pad ($id, 19, '0', STR_PAD_LEFT) .'_'. $width .'x'. $height .'_'. ($force ? '1' : '0') .'_'. ($bw ? '1' : '0') .'_'. ($crop ? '1' : '0');
return EncodeMedia::resizeImage ($source, $type, $destination, $width, $height, $force, $bw, $crop);
}
public static function synopsis ($id, $filter = array (), $dimension = 200)
{
$path = self::getFilePath ($id);
if (!file_exists ($path))
$path = self::getLegacyFilePath ($id);
if (!file_exists ($path))
throw new Exception (__ ('The file does not physically exist on the system.'));
try
{
$db = Database::singleton ();
$sth = $db->prepare ("SELECT c.*, u._name AS user, u._email AS email,
EXTRACT (EPOCH FROM c._create_date) AS taken
FROM _file c
LEFT JOIN _user u ON u._id = c._user
WHERE c._id = :id");
$sth->bindParam (':id', $id, PDO::PARAM_INT);
$sth->execute ();
$obj = $sth->fetch (PDO::FETCH_OBJ);
}
catch (PDOException $e)
{
toLog ('['. $e->getLine () .'] '. $e->getMessage ());
throw new Exception (__ ('There was a severe error when trying to load file! Please, contact your administrator.'));
}
if (!$obj)
throw new Exception (__ ('There is no associated file!'));
$archive = Archive::singleton ();
if (!$archive->isAcceptable ($obj->_mimetype))
throw new Exception (__ ('This type of file is not accepted by the system ([1])!', $obj->_mimetype));
if (is_array ($filter) && (int) sizeof ($filter) && !in_array ($obj->_mimetype, $filter))
{
$types = array ();
foreach ($filter as $trash => $mime)
{
$aux = trim ($archive->getExtensionByMime ($mime));
if (empty ($aux))
continue;
$types [] = strtoupper ($aux);
}
throw new Exception (__ ('This type of file ([1]) is not accept at this field! Files accepts are: [2].', $obj->_mimetype, implode (', ', $types)));
}
$hashQueryString = '';
if (!is_null (@$obj->_public) && !(int) $obj->_public)
{
if (is_null (@$obj->_hash) || strlen (trim ($obj->_hash)) != 32)
throw new Exception (__ ('Has a critical error with this file! Please, contact your administrator.'));
$hashQueryString = '&hash='. $obj->_hash;
}
$alt = $obj->_name ." (". File::formatFileSizeForHuman ($obj->_size) ." • ". $obj->_mimetype .") \n". __ ('By [1] ([2]) on [3].', $obj->user, $obj->email, strftime ('%x %X', $obj->taken));
ob_start ();
switch ($archive->getAssume ($obj->_mimetype))
{
case Archive::IMAGE:
?>
_mimetype))
{
?>