/** * Returns the memory usage in a readable format * * @return string */ function memory() { return f::niceSize(memory_get_usage()); }
/** * Returns the human readable version of the file size * * @return string */ public function niceSize() { return f::niceSize($this->size()); }
protected function messages() { return array(static::ERROR_MISSING_FILE => 'The file is missing', static::ERROR_MISSING_TMP_DIR => 'The /tmp directory is missing on your server', static::ERROR_FAILED_UPLOAD => 'The upload failed', static::ERROR_PARTIAL_UPLOAD => 'The file has been only been partially uploaded', static::ERROR_UNALLOWED_OVERWRITE => 'The file exists and cannot be overwritten', static::ERROR_MAX_SIZE => 'The file is too big. The maximum size is ' . f::niceSize($this->maxSize()), static::ERROR_MOVE_FAILED => 'The file could not be moved', static::ERROR_UNACCEPTED => 'The file is not accepted by the server'); }
protected function checkUpload($file, $blueprint) { if (strtolower($file->extension()) == kirby()->option('content.file.extension', 'txt')) { throw new Exception('Content files cannot be uploaded'); } else { if (strtolower($file->extension()) == 'php' or str::contains($file->extension(), 'php') or in_array($file->mime(), f::$mimes['php'])) { throw new Exception('PHP files cannot be uploaded'); } else { if (strtolower($file->extension()) == 'html' or $file->mime() == 'text/html') { throw new Exception('HTML files cannot be uploaded'); } else { if (strtolower($file->extension()) == 'exe' or $file->mime() == 'application/x-msdownload') { throw new Exception('EXE files cannot be uploaded'); } else { if (strtolower($file->filename()) == '.htaccess') { throw new Exception('htaccess files cannot be uploaded'); } else { if (str::startsWith($file->filename(), '.')) { throw new Exception('Invisible files cannot be uploaded'); // Files blueprint option 'type' } else { if (count($blueprint->files()->type()) > 0 and !in_array($file->type(), $blueprint->files()->type())) { throw new Exception('Page only allows: ' . implode(', ', $blueprint->files()->type())); // Files blueprint option 'size' } else { if ($blueprint->files()->size() and f::size($file->root()) > $blueprint->files()->size()) { throw new Exception('Page only allows file size of ' . f::niceSize($blueprint->files()->size())); // Files blueprint option 'width' } else { if ($file->type() == 'image' and $blueprint->files()->width() and $file->width() > $blueprint->files()->width()) { throw new Exception('Page only allows image width of ' . $blueprint->files()->width() . 'px'); // Files blueprint option 'height' } else { if ($file->type() == 'image' and $blueprint->files()->height() and $file->height() > $blueprint->files()->height()) { throw new Exception('Page only allows image height of ' . $blueprint->files()->height() . 'px'); } } } } } } } } } } }
/** * Returns a nicely formatted size of all the contents of the folder * * @param string $dir The path of the directory * @param boolean $recursive * @return mixed */ public static function niceSize($dir) { return f::niceSize(static::size($dir)); }
public function checkUpload($file) { $filesettings = $this->blueprint->files(); $forbiddenExtensions = array('php', 'html', 'htm', 'exe', kirby()->option('content.file.extension', 'txt')); $forbiddenMimes = array_merge(f::$mimes['php'], array('text/html', 'application/x-msdownload')); $extension = strtolower($file->extension()); // files without extension are not allowed if (empty($extension)) { throw new Exception(l('files.add.error.extension.missing')); } // block forbidden extensions if (in_array($extension, $forbiddenExtensions)) { throw new Exception(l('files.add.error.extension.forbidden')); } // especially block any connection that contains php if (str::contains($extension, 'php')) { throw new Exception(l('files.add.error.extension.forbidden')); } // block forbidden mimes if (in_array(strtolower($file->mime()), $forbiddenMimes)) { throw new Exception(l('files.add.error.mime.forbidden')); } // Block htaccess files if (strtolower($file->filename()) == '.htaccess') { throw new Exception(l('files.add.error.htaccess')); } // Block invisible files if (str::startsWith($file->filename(), '.')) { throw new Exception(l('files.add.error.invisible')); } // Files blueprint option 'type' if (count($filesettings->type()) > 0 and !in_array($file->type(), $filesettings->type())) { throw new Exception(l('files.add.blueprint.type.error') . implode(', ', $filesettings->type())); } // Files blueprint option 'size' if ($filesettings->size() and f::size($file->root()) > $filesettings->size()) { throw new Exception(l('files.add.blueprint.size.error') . f::niceSize($filesettings->size())); } // Files blueprint option 'width' if ($file->type() == 'image' and $filesettings->width() and $file->width() > $filesettings->width()) { throw new Exception('Page only allows image width of ' . $filesettings->width() . 'px'); } // Files blueprint option 'height' if ($file->type() == 'image' and $filesettings->height() and $file->height() > $filesettings->height()) { throw new Exception('Page only allows image height of ' . $filesettings->height() . 'px'); } }
public function testNiceSize() { $this->assertEquals('37 b', f::niceSize($this->contentFile)); $this->assertEquals('37 b', f::niceSize(37)); }