1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497
|
<?php
/**
* Zend Framework
*
* LICENSE
*
* This source file is subject to the new BSD license that is bundled
* with this package in the file LICENSE.txt.
* It is also available through the world-wide-web at this URL:
* http://framework.zend.com/license/new-bsd
* If you did not receive a copy of the license and are unable to
* obtain it through the world-wide-web, please send an email
* to license@zend.com so we can send you a copy immediately.
*
* @category Zend
* @package Zend_Service
* @subpackage Amazon_S3
* @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
* @license http://framework.zend.com/license/new-bsd New BSD License
* @version $Id: Stream.php 20096 2010-01-06 02:05:09Z bkarwin $
*/
/**
* @see Zend_Service_Amazon_S3
*/
require_once 'Zend/Service/Amazon/S3.php';
/**
* Amazon S3 PHP stream wrapper
*
* @category Zend
* @package Zend_Service
* @subpackage Amazon_S3
* @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
* @license http://framework.zend.com/license/new-bsd New BSD License
*/
class Zend_Service_Amazon_S3_Stream
{
/**
* @var boolean Write the buffer on fflush()?
*/
private $_writeBuffer = false;
/**
* @var integer Current read/write position
*/
private $_position = 0;
/**
* @var integer Total size of the object as returned by S3 (Content-length)
*/
private $_objectSize = 0;
/**
* @var string File name to interact with
*/
private $_objectName = null;
/**
* @var string Current read/write buffer
*/
private $_objectBuffer = null;
/**
* @var array Available buckets
*/
private $_bucketList = array();
/**
* @var Zend_Service_Amazon_S3
*/
private $_s3 = null;
/**
* Retrieve client for this stream type
*
* @param string $path
* @return Zend_Service_Amazon_S3
*/
protected function _getS3Client($path)
{
if ($this->_s3 === null) {
$url = explode(':', $path);
if (!$url) {
/**
* @see Zend_Service_Amazon_S3_Exception
*/
require_once 'Zend/Service/Amazon/S3/Exception.php';
throw new Zend_Service_Amazon_S3_Exception("Unable to parse URL $path");
}
$this->_s3 = Zend_Service_Amazon_S3::getWrapperClient($url[0]);
if (!$this->_s3) {
/**
* @see Zend_Service_Amazon_S3_Exception
*/
require_once 'Zend/Service/Amazon/S3/Exception.php';
throw new Zend_Service_Amazon_S3_Exception("Unknown client for wrapper {$url[0]}");
}
}
return $this->_s3;
}
/**
* Extract object name from URL
*
* @param string $path
* @return string
*/
protected function _getNamePart($path)
{
$url = parse_url($path);
if ($url['host']) {
return !empty($url['path']) ? $url['host'].$url['path'] : $url['host'];
}
return '';
}
/**
* Open the stream
*
* @param string $path
* @param string $mode
* @param integer $options
* @param string $opened_path
* @return boolean
*/
public function stream_open($path, $mode, $options, $opened_path)
{
$name = $this->_getNamePart($path);
// If we open the file for writing, just return true. Create the object
// on fflush call
if (strpbrk($mode, 'wax')) {
$this->_objectName = $name;
$this->_objectBuffer = null;
$this->_objectSize = 0;
$this->_position = 0;
$this->_writeBuffer = true;
$this->_getS3Client($path);
return true;
}
else {
// Otherwise, just see if the file exists or not
$info = $this->_getS3Client($path)->getInfo($name);
if ($info) {
$this->_objectName = $name;
$this->_objectBuffer = null;
$this->_objectSize = $info['size'];
$this->_position = 0;
$this->_writeBuffer = false;
$this->_getS3Client($path);
return true;
}
}
return false;
}
/**
* Close the stream
*
* @return void
*/
public function stream_close()
{
$this->_objectName = null;
$this->_objectBuffer = null;
$this->_objectSize = 0;
$this->_position = 0;
$this->_writeBuffer = false;
unset($this->_s3);
}
/**
* Read from the stream
*
* @param integer $count
* @return string
*/
public function stream_read($count)
{
if (!$this->_objectName) {
return false;
}
$range_start = $this->_position;
$range_end = $this->_position+$count;
// Only fetch more data from S3 if we haven't fetched any data yet (postion=0)
// OR, the range end position is greater than the size of the current object
// buffer AND if the range end position is less than or equal to the object's
// size returned by S3
if (($this->_position == 0) || (($range_end > strlen($this->_objectBuffer)) && ($range_end <= $this->_objectSize))) {
$headers = array(
'Range' => "$range_start-$range_end"
);
$response = $this->_s3->_makeRequest('GET', $this->_objectName, null, $headers);
if ($response->getStatus() == 200) {
$this->_objectBuffer .= $response->getBody();
}
}
$data = substr($this->_objectBuffer, $this->_position, $count);
$this->_position += strlen($data);
return $data;
}
/**
* Write to the stream
*
* @param string $data
* @return integer
*/
public function stream_write($data)
{
if (!$this->_objectName) {
return 0;
}
$len = strlen($data);
$this->_objectBuffer .= $data;
$this->_objectSize += $len;
// TODO: handle current position for writing!
return $len;
}
/**
* End of the stream?
*
* @return boolean
*/
public function stream_eof()
{
if (!$this->_objectName) {
return true;
}
return ($this->_position >= $this->_objectSize);
}
/**
* What is the current read/write position of the stream
*
* @return integer
*/
public function stream_tell()
{
return $this->_position;
}
/**
* Update the read/write position of the stream
*
* @param integer $offset
* @param integer $whence
* @return boolean
*/
public function stream_seek($offset, $whence)
{
if (!$this->_objectName) {
return false;
}
switch ($whence) {
case SEEK_CUR:
// Set position to current location plus $offset
$new_pos = $this->_position + $offset;
break;
case SEEK_END:
// Set position to end-of-file plus $offset
$new_pos = $this->_objectSize + $offset;
break;
case SEEK_SET:
default:
// Set position equal to $offset
$new_pos = $offset;
break;
}
$ret = ($new_pos >= 0 && $new_pos <= $this->_objectSize);
if ($ret) {
$this->_position = $new_pos;
}
return $ret;
}
/**
* Flush current cached stream data to storage
*
* @return boolean
*/
public function stream_flush()
{
// If the stream wasn't opened for writing, just return false
if (!$this->_writeBuffer) {
return false;
}
$ret = $this->_s3->putObject($this->_objectName, $this->_objectBuffer);
$this->_objectBuffer = null;
return $ret;
}
/**
* Returns data array of stream variables
*
* @return array
*/
public function stream_stat()
{
if (!$this->_objectName) {
return false;
}
$stat = array();
$stat['dev'] = 0;
$stat['ino'] = 0;
$stat['mode'] = 0777;
$stat['nlink'] = 0;
$stat['uid'] = 0;
$stat['gid'] = 0;
$stat['rdev'] = 0;
$stat['size'] = 0;
$stat['atime'] = 0;
$stat['mtime'] = 0;
$stat['ctime'] = 0;
$stat['blksize'] = 0;
$stat['blocks'] = 0;
if(($slash = strchr($this->_objectName, '/')) === false || $slash == strlen($this->_objectName)-1) {
/* bucket */
$stat['mode'] |= 040000;
} else {
$stat['mode'] |= 0100000;
}
$info = $this->_s3->getInfo($this->_objectName);
if (!empty($info)) {
$stat['size'] = $info['size'];
$stat['atime'] = time();
$stat['mtime'] = $info['mtime'];
}
return $stat;
}
/**
* Attempt to delete the item
*
* @param string $path
* @return boolean
*/
public function unlink($path)
{
return $this->_getS3Client($path)->removeObject($this->_getNamePart($path));
}
/**
* Attempt to rename the item
*
* @param string $path_from
* @param string $path_to
* @return boolean False
*/
public function rename($path_from, $path_to)
{
// TODO: Renaming isn't supported, always return false
return false;
}
/**
* Create a new directory
*
* @param string $path
* @param integer $mode
* @param integer $options
* @return boolean
*/
public function mkdir($path, $mode, $options)
{
return $this->_getS3Client($path)->createBucket(parse_url($path, PHP_URL_HOST));
}
/**
* Remove a directory
*
* @param string $path
* @param integer $options
* @return boolean
*/
public function rmdir($path, $options)
{
return $this->_getS3Client($path)->removeBucket(parse_url($path, PHP_URL_HOST));
}
/**
* Attempt to open a directory
*
* @param string $path
* @param integer $options
* @return boolean
*/
public function dir_opendir($path, $options)
{
if (preg_match('@^([a-z0-9+.]|-)+://$@', $path)) {
$this->_bucketList = $this->_getS3Client($path)->getBuckets();
}
else {
$host = parse_url($path, PHP_URL_HOST);
$this->_bucketList = $this->_getS3Client($path)->getObjectsByBucket($host);
}
return ($this->_bucketList !== false);
}
/**
* Return array of URL variables
*
* @param string $path
* @param integer $flags
* @return array
*/
public function url_stat($path, $flags)
{
$stat = array();
$stat['dev'] = 0;
$stat['ino'] = 0;
$stat['mode'] = 0777;
$stat['nlink'] = 0;
$stat['uid'] = 0;
$stat['gid'] = 0;
$stat['rdev'] = 0;
$stat['size'] = 0;
$stat['atime'] = 0;
$stat['mtime'] = 0;
$stat['ctime'] = 0;
$stat['blksize'] = 0;
$stat['blocks'] = 0;
$name = $this->_getNamePart($path);
if(($slash = strchr($name, '/')) === false || $slash == strlen($name)-1) {
/* bucket */
$stat['mode'] |= 040000;
} else {
$stat['mode'] |= 0100000;
}
$info = $this->_getS3Client($path)->getInfo($name);
if (!empty($info)) {
$stat['size'] = $info['size'];
$stat['atime'] = time();
$stat['mtime'] = $info['mtime'];
}
return $stat;
}
/**
* Return the next filename in the directory
*
* @return string
*/
public function dir_readdir()
{
$object = current($this->_bucketList);
if ($object !== false) {
next($this->_bucketList);
}
return $object;
}
/**
* Reset the directory pointer
*
* @return boolean True
*/
public function dir_rewinddir()
{
reset($this->_bucketList);
return true;
}
/**
* Close a directory
*
* @return boolean True
*/
public function dir_closedir()
{
$this->_bucketList = array();
return true;
}
}
|