Esempio n. 1
0
	/**
	 * Decrypt the encrypted/signed file and return a valid File object
	 *
	 * @return mixed
	 */
	public function decrypt($dest = false) {
		if ($dest) {
			if (is_a($dest, 'File') || $dest instanceof Filestore\File) {
				// Don't need to do anything! The object either is a File
				// Or is an implmentation of the File interface.
			}
			else {
				// Well it should be damnit!....
				$file = $dest;

				// Is the destination a directory or filename?
				// If it's a directory just tack on this current file's basename.
				if (substr($file, -1) == '/') {
					$file .= $this->_file->getBaseFilename();
				}

				// Drop the .asc extension if it's there.
				if ($this->_file->getExtension() == 'asc') $file = substr($file, 0, -4);

				$dest = Filestore\Factory::File($file);
			}

			// And load up the contents!
			$dest->putContents($this->decrypt());

			return $dest;
		}
		else {
			// Extract and return the file contents
			ob_start();
			passthru('gpg --homedir "' . GPG_HOMEDIR . '" --no-permission-warning --decrypt "' . $this->_file->getLocalFilename() . '"');
			$content = ob_get_contents();
			ob_end_clean();

			return $content;
		}
	}
Esempio n. 2
0
/**
 * Get an array of the various resize components from a given dimension set.
 * These include: width, height, mode, key.
 *
 * @param string|int $dimensions
 * @param File $file
 *
 * @return array
 */
function get_resized_key_components($dimensions, $file){
	// The legacy support for simply a number.
	if (is_numeric($dimensions)) {
		$width  = $dimensions;
		$height = $dimensions;
		$mode = '';
	}
	elseif ($dimensions === null) {
		$width  = 300;
		$height = 300;
		$mode = '';
	}
	elseif($dimensions === false){
		$width = false;
		$height = false;
		$mode = '';
	}
	else {
		// Allow some special modifiers.
		if(strpos($dimensions, '^') !== false){
			// Fit the smallest dimension instead of the largest, (useful for overflow tricks)
			$mode = '^';
			$dimensions = str_replace('^', '', $dimensions);
		}
		elseif(strpos($dimensions, '!') !== false){
			// Absolutely resize, regardless of aspect ratio
			$mode = '!';
			$dimensions = str_replace('!', '', $dimensions);
		}
		elseif(strpos($dimensions, '>') !== false){
			// Only increase images.
			$mode = '>';
			$dimensions = str_replace('>', '', $dimensions);
		}
		elseif(strpos($dimensions, '<') !== false){
			// Only decrease images.
			$mode = '<';
			$dimensions = str_replace('<', '', $dimensions);
		}
		else{
			// Default mode
			$mode = '';
		}
		// New method. Split on the "x" and that should give me the width/height.
		$vals   = explode('x', strtolower($dimensions));
		$width  = (int)$vals[0];
		$height = (int)$vals[1];
	}

	$ext = $file->getExtension();
	// Ensure that an extension is used if none present, (may happen with temporary files).
	if(!$ext){
		$ext = mimetype_to_extension($file->getMimetype());
	}

	// The basename is for SEO purposes, that way even resized images still contain the filename.
	// The hash is just to ensure that no two files conflict, ie: /public/a/file1.png and /public/b/file1.png
	//  might conflict without this hash.
	// Finally, the width and height dimensions are there just because as well; it gives more of a human
	//  touch to the file. :p
	// Also, keep the original file extension, this way PNGs remain PNGs, GIFs remain GIFs, JPEGs remain JPEGs.
	// This is critical particularly when it comes to animated GIFs.
	$key = str_replace(' ', '-', $file->getBasename(true)) . '-' . $file->getHash() . '-' . $width . 'x' . $height . $mode . '.' . $ext;

	// The directory can be used with the new File backend to create this file in a correctly nested subdirectory.
	$dir = dirname($file->getFilename(false)) . '/';

	if(substr($dir, 0, 7) == 'public/'){
		// Replace the necessary prefix with a more useful one.
		// Anything within public/ needs to be remapped to public/tmp
		$dir = 'public/tmp/' . substr($dir, 7);
	}
	else{
		// Everything else gets prepended to public/tmp/
		// so if the original file is in themes/blah/imgs/blah.jpg,
		// it will be copied to public/tmp/blah.jpg
		$dir = 'public/tmp/';
	}

	return array(
		'width'  => $width,
		'height' => $height,
		'mode'   => $mode,
		'key'    => $key,
		'ext'    => $ext,
		'dir'    => $dir,
	);
}