Ticket #591 (closed Patch: fixed)

Opened 5 months ago

Last modified 3 months ago

missed MIME type, new helpers: file::size() & security::generate_token()

Reported by: brettalton Owned by: Shadowhand
Priority: minor Milestone: 2.2
Component: Core Version: SVN HEAD
Keywords: file size, filesize, token, mime Cc:

Description

Just some helpers I thought would be useful.

I'm sure you guys can come up with better token generators, but its a start. Maybe security::verify_token() is in order too?

Attachments

diff.patch (2.9 kB) - added by brettalton 5 months ago.
diff.2.patch (2.8 kB) - added by brettalton 5 months ago.
Second attempt at a patch file

Change History

Changed 5 months ago by brettalton

Changed 5 months ago by brettalton

Second attempt at a patch file

Changed 5 months ago by brettalton

I must have done something wrong with the patch file, so here's the code:

/system/config/mimes.php

	'po'    => array('text/x-gettext-translation'),

/system/helpers/file.php

	/**
	 * Return file size using metric prefixes (SI), allow rounding
	 *
	 * @param   string   path to file
	 * @param   integer  integer to round to
	 * @return  string   file size with metric prefix
	 */
	public static function size($filename, $round = FALSE)
	{
		// Check that file exists
		if ( ! file_exists($filename) OR ! is_file($filename) OR ! is_readable($filename))
			return FALSE;

		// Find filesize in bytes
		$file_size = sprintf("%u", filesize($filename)); // filesize() has troubles between 2GB and 4GB

		// Make sure $file_size was calculated
		if (empty($file_size) || $file_size === FALSE)
			return FALSE;
	
		$sizes = array('B', 'kB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB');
		for ($i=0; $file_size > 1024 && $i < count($sizes) - 1; $i++)
			$file_size /= 1024;
		return round($file_size,$round).' '.$sizes[$i]; // ex: 10 kB
	}

/system/helpers/security.php

	/**
	 * Generate a pseudo-random token between 1-40 characters
	 *
	 * @param   integar  length of token (min 1, max 40)
	 * @param   string   string to hash
	 * @return  string   return psuedo-random token
	 */
	public static function generate_token($length = FALSE, $key = uniqid(rand(), TRUE))
	{
		// sha1 is a 256-bit hex hash and is 40 characters long, thus it can not be longer than 40
		// generate a 40 character long hash if length is greater than 40 or less than 0 (fail safe)
		if ($length == FALSE || $length >= 40 || $length <= 0) 
		{
			return sha1($key);
		}
		else
		{
			$start = rand(0,40-$length); // random number between 0 and maximum acceptable number before $start + $length > 40
			return substr(sha1($key),$start,$length);
		}
	}

Changed 5 months ago by Shadowhand

"po" added to mimes.php in r2698.

Changed 3 months ago by Shadowhand

  • status changed from new to assigned

file::size is already implemented by text::bytes. security:: generate_token is already handled by text::random.

Changed 3 months ago by Shadowhand

  • status changed from assigned to closed
  • resolution set to fixed
Note: See TracTickets for help on using tickets.