public function avatar() { if (isset($this->cache['avatar'])) { return $this->cache['avatar']; } // try to find the avatar $root = c::get('root') . DS . 'assets' . DS . 'avatars' . DS . $this->username() . '.{jpg,png}'; if ($avatar = a::first((array) glob($root, GLOB_BRACE))) { return $this->cache['avatar'] = new Media($avatar, url('assets/avatars/' . f::filename($avatar))); } else { return $this->cache['avatar'] = false; } }
public function __construct(User $user) { // store the parent user object $this->user = $user; // this should rather be coming from the user object $this->kirby = kirby::instance(); // try to find the avatar if ($file = f::resolve($this->kirby->roots()->avatars() . DS . $user->username(), ['jpg', 'jpeg', 'gif', 'png'])) { $filename = f::filename($file); } else { $filename = $user->username() . '.jpg'; $file = $this->kirby->roots()->avatars() . DS . $filename; } parent::__construct($file, $this->kirby->urls()->avatars() . '/' . $filename); }
function file($field, $destination, $params = array()) { $allowed = a::get($params, 'allowed', c::get('upload.allowed', array('image/jpeg', 'image/png', 'image/gif'))); $maxsize = a::get($params, 'maxsize', c::get('upload.maxsize', self::max_size())); $overwrite = a::get($params, 'overwrite', c::get('upload.overwrite', true)); $sanitize = a::get($params, 'sanitize', true); $file = a::get($_FILES, $field); if (empty($file)) { return array('status' => 'error', 'msg' => l::get('upload.errors.missing-file', 'The file has not been found')); } $name = a::get($file, 'name'); $type = a::get($file, 'type'); $tmp_name = a::get($file, 'tmp_name'); $error = a::get($file, 'error'); $size = a::get($file, 'size'); $msg = false; $extension = self::mime_to_extension($type, 'jpg'); // convert the filename to a save name $fname = $sanitize ? f::safe_name(f::name($name)) : f::name($name); // setup the destination $destination = str_replace('{name}', $fname, $destination); $destination = str_replace('{extension}', $extension, $destination); if (file_exists($destination) && $overwrite == false) { return array('status' => 'error', 'msg' => l::get('upload.errors.file-exists', 'The file exists and cannot be overwritten')); } if (empty($tmp_name)) { return array('status' => 'error', 'msg' => l::get('upload.errors.missing-file', 'The file has not been found')); } if ($error != 0) { return array('status' => 'error', 'msg' => l::get('upload.errors.invalid-upload', 'The upload failed')); } if ($size > $maxsize) { return array('status' => 'error', 'msg' => l::get('upload.errors.too-big', 'The file is too big')); } if (!in_array($type, $allowed)) { return array('status' => 'error', 'msg' => l::get('upload.errors.invalid-file', 'The file type is not allowed') . ': ' . $type); } // try to change the permissions for the destination @chmod(dirname($destination), 0777); if (!@copy($tmp_name, $destination)) { return array('status' => 'error', 'msg' => l::get('upload.errors.move-error', 'The file could not be moved to the server')); } // try to change the permissions for the final file @chmod($destination, 0777); return array('status' => 'success', 'msg' => l::get('upload.success', 'The file has been uploaded'), 'type' => $type, 'extension' => $extension, 'file' => $destination, 'size' => $size, 'name' => f::filename($destination)); }
public static function download($file, $name = null) { // stop the download if the file does not exist or is not readable if (!is_file($file) or !is_readable($file)) { return false; } header::download(array('name' => $name ? $name : f::filename($file), 'size' => f::size($file), 'mime' => f::mime($file), 'modified' => f::modified($file))); die(f::read($file)); }
public function avatar() { if (isset($this->cache['avatar'])) { return $this->cache['avatar']; } // allowed extensions $extensions = array('jpg', 'jpeg', 'png', 'gif'); // try to find the avatar $root = kirby::instance()->roots()->avatars() . DS . $this->username(); foreach ($extensions as $ext) { $file = $root . '.' . $ext; if (file_exists($file)) { return $this->cache['avatar'] = new Media($file, kirby::instance()->urls()->avatars() . '/' . f::filename($file)); } } return $this->cache['avatar'] = false; }
public function testFilename() { $this->assertEquals('content.php', f::filename($this->contentFile)); }