File: /www/exchange0old/exchange/classes/Storage.php
<?php
if(!class_exists('Storage_ConnectionInterface')){
require_once 'Storage/ConnectionInterface.php';
}
if(!class_exists('Storage_Repository')){
require_once 'Storage/Repository.php';
}
if(!class_exists('Storage_Connection')){
require_once 'Storage/Connection.php';
}
if(!class_exists('Storage_Connection_Local')){
require_once 'Storage/Connection/Local.php';
}
if(!class_exists('Encryption')){
require_once 'Lib/Encryption.php';
}
class Storage
{
private $connection;
/**
* @var string Used for found object name in repository
*/
private $hash;
private $name;
function __construct(Storage_ConnectionInterface $connection)
{
$this->connection = $connection;
$this->hash = uniqid('',true);
}
/**
* Write content to storage.
*
* @return $this
*/
public function set($path, $content, $filename = FALSE)
{
if ( ! is_resource($content)){
if ($filename){
$handle = fopen($content, 'r');
}else{
$handle = tmpfile();
fwrite($handle, $content);
rewind($handle);
}
}else{
$handle = $content;
}
$this->connection->set($path, $handle);
if (is_resource($handle)){
fclose($handle);
}
return $this;
}
/**
* Load file in storage from upload
* @param string $path Path to save
* @param array $field file array
* @param bool|true $deleteAfter delete temporary file
* @return $this
* @throws Exception
*/
public function setFromUpload($path, array $field, $deleteAfter = true)
{
if(!array_key_exists('tmp_name',$field)){
throw new Exception('Error in uploaded file');
}
$this->set($path,$field['tmp_name'],true);
if($deleteAfter){
@unlink($field['tmp_name']);
}
return $this;
}
/**
* @return mixed
*/
public function getName()
{
return $this->name;
}
/**
* @param mixed $name
*/
public function setName($name)
{
$this->name = $name;
}
/**
* Read contents of file.
* @param $path
* @param $handle - Handle or filename
* @return bool
*/
public function get($path, $handle){
$result = $this->read($path,$handle);
if (is_resource($handle)){
fclose($handle);
}
return $result;
}
public function exists($path){
return $this->connection->exists($path);
}
protected function read($path, $handle){
if ( ! is_resource($handle)){
if ( ! $handle = fopen($handle, 'w'))
return FALSE;
}
return $this->connection->get($path, $handle);
}
/**
* Return contents of file.
*/
public function contents($path){
$handle = fopen('php://memory','w');
//Read to tmpfile
$this->read($path,$handle); rewind($handle);
$contents = stream_get_contents($handle);
if (is_resource($handle)){
fclose($handle);
}
return $contents;
}
/**
* Get file url
* @param string $path Path to file
* @param bool|false $inline Display inline instead of download
*/
public function url($path,$inline=true){
if(empty($path)) return;
$params = array();
$params['storage'] = $this->getName();
$params['path'] = $path;
$params['inline'] = $inline;
//TODO: Remove globals
$baseUrl =rtrim(array_key_exists('exchange_url',$_SESSION)?$_SESSION['exchange_url']:'','/');
$encoded = Encryption::encrypt(json_encode($params));
$baseUrl.= '/file.php?'.$encoded;
return $baseUrl;
}
public function delete($path){
$this->connection->delete($path);
}
public function getConnection() {
return $this->connection;
}
}