Helix Installation   Getting Started   Reference Documentation   Global Functions   Quick Start
ModulesClassesFunctions
 

Class Reference

class s3 extends obj

Helix obj s3
Hide Inherited MembersAmazon Web Services
Description
No description.
 
Constants
No constants.
 
Properties
protected_access_key_id[ s3 ]
protected_bucket[ s3 ]
protected_dirty[ obj ]
protected_host[ s3 ]
publicproperty_objects_array[ obj ]
protected_query_string[ s3 ]
protected_secret_access_key[ s3 ]
 
Methods
privatebuild_authentication_header ( method , bucket , object , date , amz_headers , content_type )[ s3 ]
Parameters
requiredmethod
requiredbucket
requiredobject
requireddate
requiredamz_headers
requiredcontent_type
Code
147  private function build_authentication_header($__method, $__bucket, $__object, $__date, $__amz_headers, $__content_type)
148  {
149      $canonicalized_amz_headers = count($__amz_headers)>0 ? join("\n",$__amz_headers) . "\n" : null;
150      if (strlen($__bucket) > 0)
151      {
152          if (strlen($__object) > 0)
153          {
154              $canonicalized_resource = "/" . strtolower($__bucket) . "/" . $__object;
155          }
156          else
157          {
158              $canonicalized_resource = "/" . strtolower($__bucket) . "/";
159          }
160      }
161      else
162      {
163          $canonicalized_resource = "/";
164      }
165      if (preg_match('/\?(acl|location|logging|torrent)/i',$this->query_string))
166      {
167          $canonicalized_resource .= is_null($this->query_string) ? null : $this->query_string;
168      }
169      $content_md5 = "";
170      $string_to_sign = $__method . "\n" . $content_md5 . "\n" . $__content_type . "\n" . $__date . "\n" . $canonicalized_amz_headers . $canonicalized_resource;
171      $auth = "AWS " . $this->access_key_id . ":" . base64_encode(hash_hmac("sha1",utf8_encode($string_to_sign), $this->secret_access_key, true));
172      return $auth;
173  }
privatebuild_headers ( method , bucket , object , file , amz_headers )[ s3 ]
Parameters
requiredmethod
requiredbucket
requiredobject
requiredfile
requiredamz_headers
Code
128  private function build_headers($__method, $__bucket, $__object, $__file, $__amz_headers)
129  {
130      $filesize = is_object($__file) ? $__file->size : filesize($__file);
131      $filemime = is_object($__file) ? $__file->mime : mime($__file);
132      $date = gmdate("r");
133      $content_type = ($filesize > 0 && $__method==="PUT") ? $filemime : null;
134      $header_array = array();
135      $header_array[] = "Date: $date";
136      $header_array[] = "Authorization: " . $this->build_authentication_header($__method, $__bucket, $__object, $date, $__amz_headers, $content_type);
137      if (($filesize > 0 && $__method==="PUT"))
138      {
139          $header_array[] = "Content-Type: $content_type";
140      }
141      $header_string = join("\n",$header_array) . "\n" . join("\n",$__amz_headers);
142      $headers = array($header_string);
143     
144      return $headers;
145  }
public__construct ( access_key_id , secret_access_key , bucket )[ s3 ]
Parameters
requiredaccess_key_id
requiredsecret_access_key
optionalbucket
Code
010  public function __construct($__access_key_id, $__secret_access_key, $__bucket=null)
011  {
012      $this->access_key_id = $__access_key_id;
013      $this->secret_access_key = $__secret_access_key;
014      $this->host = "s3.amazonaws.com";
015      $this->bucket = $__bucket;
016      $this->query_string = null;
017  }
publicdelete ( object , bucket , query_string )[ s3 ]
Parameters
requiredobject
optionalbucket
optionalquery_string
Code
187  public function delete($__object, $__bucket=null, $__query_string=null)
188  {
189      $bucket = is_null($__bucket) ? $this->bucket : $__bucket;
190      return $this->request("DELETE", $bucket, $__object, null, $__query_string);
191  }
public__get ( p )[ obj ]
Parameters
requiredp
Code
007  public function __get($p)
008  {
009      $methods = get_class_methods($this);
010      $properties = get_object_vars($this);
011      if (in_array("get_$p",$methods))
012      {
013          return $this->{"get_$p"}();
014      }
015      else if (array_key_exists("_$p",$properties))
016      {
017          return $this->{"_$p"};
018      }
019      else if (in_array("get_" . substr($p,strpos($p,"_")+1), $methods))
020      {
021          return $this->{"get_" . substr($p,strpos($p,"_")+1)}(substr($p,0,strpos($p,"_")));
022      }
023      else
024      {
025          trigger_error("This object [" . get_class($this) . ":$this->id] has no private property '_$p' for the requested public property '$p'",E_USER_WARNING);
026          return null;
027      }
028  }
publicget ( object , bucket , query_string )[ s3 ]
Parameters
optionalobject
optionalbucket
optionalquery_string
Code
175  public function get($__object=null, $__bucket=null, $__query_string=null)
176  {
177      $bucket = is_null($__bucket) ? $this->bucket : $__bucket;
178      return $this->request("GET", $bucket, $__object, null, $__query_string);
179  }
publicget_array ( )[ obj ]
Parameters
No parameters for this method
Code
096  public function get_array()
097  {
098      return get_object_vars($this);
099  }
publicget_classname ( )[ obj ]
Parameters
No parameters for this method
Code
086  public function get_classname()
087  {
088      return get_class($this);
089  }
publicget_dirty ( )[ obj ]
Parameters
No parameters for this method
Code
063  public function get_dirty()
064  {
065      if ($this->id === 0)
066      {
067          $this->_dirty = true;
068      }
069      else
070      {
071          foreach (get_object_vars($this) as $k=>$v)
072          {
073              if ($this->_dirty)
074              {
075                  break;
076              }
077              if (preg_match('/_dirty$/',$k))
078              {
079                  $this->_dirty = $this->_dirty || $v;
080              }
081          }
082      }
083      return $this->_dirty;
084  }
publicget_json ( )[ obj ]
Parameters
No parameters for this method
Code
101  public function get_json()
102  {
103      $class = new ReflectionClass($this->classname);
104      $methods = $class->getMethods();
105      usort($methods,"sort_reflection_objects");
106      $accessors = array();
107      foreach ($methods as $index=>$method)
108      {
109          $declarer = $method->getDeclaringClass()->name;
110          if (preg_match('/^(get|set|)_(.+)$/',$method->name,$matches))
111          {
112              $accessor = $matches[2];
113              $getset = $matches[1];
114              eval("\$accessors[\$accessor] = \$this->".$accessor.";");
115          }
116      }
117      //return json_encode($this->get_array());
118      return json_encode($accessors);
119  }
publicget_properties ( )[ obj ]
Parameters
No parameters for this method
Code
121  public function get_properties()
122  {
123      return array_keys($this->array);
124  }
protectedget_string ( )[ obj ]
Parameters
No parameters for this method
Code
091  protected function get_string()
092  {
093      return method_exists($this,"__toString") ? $this->__toString() : null;
094  }
publichead ( object , bucket , query_string )[ s3 ]
Parameters
requiredobject
optionalbucket
optionalquery_string
Code
193  public function head($__object, $__bucket=null, $__query_string=null)
194  {
195      $bucket = is_null($__bucket) ? $this->bucket : $__bucket;
196      return $this->request("HEAD", $bucket, $__object, null, $__query_string);
197  }
publicput ( object , bucket , query_string )[ s3 ]
Parameters
requiredobject
optionalbucket
optionalquery_string
Code
181  public function put($__object, $__bucket=null, $__query_string=null)
182  {
183      $bucket = is_null($__bucket) ? $this->bucket : $__bucket;
184      return $this->request("PUT", $bucket, $__object, null, $__query_string);
185  }
privaterequest ( method , bucket , object , acl , query_string )[ s3 ]
Parameters
requiredmethod
requiredbucket
optionalobject
optionalacl
optionalquery_string
Code
019  private function request($__method, $__bucket, $__object=null, $__acl=null, $__query_string=null)
020  {
021      $virtualhost = is_null($__bucket) ? null : "{$__bucket}.";
022      $__acl = is_null($__acl) ? "public-read" : $__acl;
023      $this->query_string = is_null($__query_string) ? null : "?{$__query_string}";
024      $amz_headers = array();
025      if (is_object($__object))
026      {
027          $file = $__object;
028          $object = $file->filename;
029      }
030      else if (is_array($__object))
031      {
032          $file = new resource(null,null,$__object);
033          $object = $file->filename;
034      }
035      else if (is_string($__object))
036      {
037          if (file_exists($__object))
038          {
039              $file = $__object;
040              $object = basename($file);
041          }
042          else
043          {
044              $file = new resource();
045              $object = $__object;
046          }
047      }
048      else
049      {
050          $file = new resource();
051          $object = $__object;
052      }
053     
054      $filesize = is_object($file) ? $file->size : filesize($file);
055      $filepath = is_object($file) ? $file->current_path : $file;
056     
057      $ch = curl_init();
058      if ($__method==="PUT" || $__method=="DELETE")
059      {
060          if ($__method==="PUT")
061          {
062              $amz_headers[] = "x-amz-acl:$__acl";
063              curl_setopt($ch, CURLOPT_PUT, true);
064              curl_setopt($ch, CURLOPT_INFILESIZE, $filesize);               
065              if ($filesize > 0)
066              {
067                  debug("file to s3 is $filepath with size $filesize");
068                  $fo = fopen($filepath,"rb");
069              }
070              else
071              {
072                  $fo = null;
073              }
074              curl_setopt($ch, CURLOPT_INFILE, $fo);
075          }
076      }
077      curl_setopt($ch, CURLOPT_URL, "http://" . $virtualhost . $this->host . "/" . $object . $this->query_string);
078      curl_setopt($ch, CURLOPT_HEADER, true);
079      curl_setopt($ch, CURLOPT_HTTPHEADER, $this->build_headers($__method, $__bucket, $object, $file, $amz_headers));
080      curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
081      curl_setopt($ch, CURLOPT_VERBOSE, true);
082      curl_setopt($ch, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_1);
083      curl_setopt($ch, CURLINFO_HEADER_OUT, true);
084      curl_setopt($ch, CURLINFO_TOTAL_TIME, true);
085      if ($filesize > 0 && $__method==="PUT")
086      {
087          $response = curl_exec($ch);
088          if (!is_null($fo))
089          {
090              fclose($fo);
091          }
092      }
093      else if ($__method==="DELETE")
094      {
095          curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "DELETE");
096          $response = curl_exec($ch);
097      }
098      else if ($__method==="HEAD")
099      {
100          //curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "HEAD");
101          curl_setopt($ch, CURLOPT_NOBODY, true);
102          $response = curl_exec($ch);
103      }
104      else
105      {
106          $response = curl_exec($ch);
107      }
108      $error = curl_error($ch);
109      $errno = curl_errno($ch);
110      $info = curl_getinfo($ch);
111      curl_close($ch);
112     
113      debug("S3 request took " . number_format($info["total_time"],4) . " seconds",__FILE__,__LINE__);
114     
115      $ret = "########################################################" . "\n";
116      $ret .= "## CURL REQUEST HEADERS [{$info["total_time"]} sec] [$errno] $error" . "\n";
117      $ret .= "########################################################" . "\n";
118      $ret .= $info["request_header"];
119     
120      $ret .= "########################################################" . "\n";
121      $ret .= "## RESPONSE FROM AMAZON S3                              " . "\n";
122      $ret .= "########################################################" . "\n";
123      $ret .= $response;
124     
125      return $ret;
126  }
public__set ( p , v )[ obj ]
Parameters
requiredp
requiredv
Code
030  public function __set($p,$v)
031  {
032      $methods = get_class_methods($this);
033      $properties = get_object_vars($this);
034      if (in_array("set_$p",$methods))
035      {
036          $this->{"set_$p"}($v);
037      }
038      else if (array_key_exists("_$p",$properties))
039      {
040          if (!preg_match('/_dirty$/',$p) && property_exists($this,"_{$p}_dirty"))
041          {
042              if (is_object($this->{"_$p"}))
043              {
044                  $this->{"_$p"}->dirty = true;
045              }
046              else
047              {
048                  $this->{"_{$p}_dirty"} = $this->{$p}==$v ? false : true;
049              }
050          }
051          $this->{"_$p"} = $v;
052      }
053      else if (in_array("set_" . substr($p,strpos($p,"_")+1), $methods))
054      {
055          $this->{"set_" . substr($p,strpos($p,"_")+1)}($v,substr($p,0,strpos($p,"_")));
056      }
057      else
058      {
059          trigger_error("This object [" . get_class($this) . ":$this->id] has no private property '_$p' for the requested public property '$p'",E_USER_WARNING);
060      }
061  }
 
Accessors
publicarray[ obj ]
Code
096  public function get_array()
097  {
098      return get_object_vars($this);
099  }
publicclassname[ obj ]
Code
086  public function get_classname()
087  {
088      return get_class($this);
089  }
publicdirty[ obj ]
Code
063  public function get_dirty()
064  {
065      if ($this->id === 0)
066      {
067          $this->_dirty = true;
068      }
069      else
070      {
071          foreach (get_object_vars($this) as $k=>$v)
072          {
073              if ($this->_dirty)
074              {
075                  break;
076              }
077              if (preg_match('/_dirty$/',$k))
078              {
079                  $this->_dirty = $this->_dirty || $v;
080              }
081          }
082      }
083      return $this->_dirty;
084  }
publicjson[ obj ]
Code
101  public function get_json()
102  {
103      $class = new ReflectionClass($this->classname);
104      $methods = $class->getMethods();
105      usort($methods,"sort_reflection_objects");
106      $accessors = array();
107      foreach ($methods as $index=>$method)
108      {
109          $declarer = $method->getDeclaringClass()->name;
110          if (preg_match('/^(get|set|)_(.+)$/',$method->name,$matches))
111          {
112              $accessor = $matches[2];
113              $getset = $matches[1];
114              eval("\$accessors[\$accessor] = \$this->".$accessor.";");
115          }
116      }
117      //return json_encode($this->get_array());
118      return json_encode($accessors);
119  }
publicproperties[ obj ]
Code
121  public function get_properties()
122  {
123      return array_keys($this->array);
124  }
publicstring[ obj ]
Code
091  protected function get_string()
092  {
093      return method_exists($this,"__toString") ? $this->__toString() : null;
094  }
Get Helix PHP Framework at SourceForge.net. Fast, secure and Free Open Source software downloads