Search moodle.org's
Developer Documentation

See Release Notes
Long Term Support Release

  • Bug fixes for general core bugs in 4.1.x will end 13 November 2023 (12 months).
  • Bug fixes for security issues in 4.1.x will end 10 November 2025 (36 months).
  • PHP version: minimum PHP 7.4.0 Note: minimum PHP version has increased since Moodle 4.0. PHP 8.0.x is supported too.

Differences Between: [Versions 310 and 401] [Versions 39 and 401] [Versions 401 and 402] [Versions 401 and 403]

Copyright (c) 2013, Donovan Schönknecht.  All rights reserved. Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:

File Size: 2394 lines (74 kb)
Included or required:0 times
Referenced: 0 times
Includes or requires: 0 files

Defines 3 classes


Class: S3  - X-Ref

Amazon S3 PHP class

__construct($accessKey = null, $secretKey = null, $useSSL = false, $endpoint = 's3.amazonaws.com')   X-Ref
Constructor - if you're not using the class statically

param: string $accessKey Access key
param: string $secretKey Secret key
param: boolean $useSSL Enable SSL
param: string $endpoint Amazon URI
return: void

setEndpoint($host)   X-Ref
Set the service endpoint

param: string $host Hostname
return: void

setAuth($accessKey, $secretKey)   X-Ref
Set AWS access key and secret key

param: string $accessKey Access key
param: string $secretKey Secret key
return: void

hasAuth()   X-Ref
Check if AWS keys have been set

return: boolean

setSSL($enabled, $validate = true)   X-Ref
Set SSL on or off

param: boolean $enabled SSL enabled
param: boolean $validate SSL certificate validation
return: void

setSSLAuth($sslCert = null, $sslKey = null, $sslCACert = null)   X-Ref
Set SSL client certificates (experimental)

param: string $sslCert SSL client certificate
param: string $sslKey SSL client key
param: string $sslCACert SSL CA cert (only required if you are having problems with your system CA cert)
return: void

setProxy($host, $user = null, $pass = null, $type = CURLPROXY_SOCKS5)   X-Ref
Set proxy information

param: string $host Proxy hostname and port (localhost:1234)
param: string $user Proxy username
param: string $pass Proxy password
param: constant $type CURL proxy type
return: void

setExceptions($enabled = true)   X-Ref
Set the error mode to exceptions

param: boolean $enabled Enable exceptions
return: void

setTimeCorrectionOffset($offset = 0)   X-Ref
Set AWS time correction offset (use carefully)

This can be used when an inaccurate system time is generating
invalid request signatures.  It should only be used as a last
resort when the system time cannot be changed.

param: string $offset Time offset (set to zero to use AWS server time)
return: void

setSigningKey($keyPairId, $signingKey, $isFile = true)   X-Ref
Set signing key

param: string $keyPairId AWS Key Pair ID
param: string $signingKey Private Key
param: boolean $isFile Load private key from file, set to false to load string
return: boolean

freeSigningKey()   X-Ref
Free signing key from memory, MUST be called if you are using setSigningKey()

return: void

__triggerError($message, $file, $line, $code = 0)   X-Ref
Internal error handler

param: string $message Error message
param: string $file Filename
param: integer $line Line number
param: integer $code Error code
return: void

listBuckets($detailed = false)   X-Ref
Get a list of buckets

param: boolean $detailed Returns detailed bucket list when true
return: array | false

getBucket($bucket, $prefix = null, $marker = null, $maxKeys = null, $delimiter = null, $returnCommonPrefixes = false)   X-Ref
Get contents for a bucket

If maxKeys is null this method will loop through truncated result sets

param: string $bucket Bucket name
param: string $prefix Prefix
param: string $marker Marker (last file listed)
param: string $maxKeys Max keys (maximum number of keys to return)
param: string $delimiter Delimiter
param: boolean $returnCommonPrefixes Set to true to return CommonPrefixes
return: array | false

putBucket($bucket, $acl = self::ACL_PRIVATE, $location = false)   X-Ref
Put a bucket

param: string $bucket Bucket name
param: constant $acl ACL flag
param: string $location Set as "EU" to create buckets hosted in Europe
return: boolean

deleteBucket($bucket)   X-Ref
Delete an empty bucket

param: string $bucket Bucket name
return: boolean

inputFile($file, $md5sum = true)   X-Ref
Create input info array for putObject()

param: string $file Input file
param: mixed $md5sum Use MD5 hash (supply a string if you want to use your own)
return: array | false

inputResource(&$resource, $bufferSize = false, $md5sum = '')   X-Ref
Create input array info for putObject() with a resource

param: string $resource Input resource to read from
param: integer $bufferSize Input byte size
param: string $md5sum MD5 hash to send (optional)
return: array | false

putObject($input, $bucket, $uri, $acl = self::ACL_PRIVATE, $metaHeaders = array()   X-Ref
Put an object

param: mixed $input Input data
param: string $bucket Bucket name
param: string $uri Object URI
param: constant $acl ACL constant
param: array $metaHeaders Array of x-amz-meta-* headers
param: array $requestHeaders Array of request headers or content type as a string
param: constant $storageClass Storage class constant
param: constant $serverSideEncryption Server-side encryption
return: boolean

putObjectFile($file, $bucket, $uri, $acl = self::ACL_PRIVATE, $metaHeaders = array()   X-Ref
Put an object from a file (legacy function)

param: string $file Input file path
param: string $bucket Bucket name
param: string $uri Object URI
param: constant $acl ACL constant
param: array $metaHeaders Array of x-amz-meta-* headers
param: string $contentType Content type
return: boolean

putObjectString($string, $bucket, $uri, $acl = self::ACL_PRIVATE, $metaHeaders = array()   X-Ref
Put an object from a string (legacy function)

param: string $string Input data
param: string $bucket Bucket name
param: string $uri Object URI
param: constant $acl ACL constant
param: array $metaHeaders Array of x-amz-meta-* headers
param: string $contentType Content type
return: boolean

getObject($bucket, $uri, $saveTo = false)   X-Ref
Get an object

param: string $bucket Bucket name
param: string $uri Object URI
param: mixed $saveTo Filename or resource to write to
return: mixed

getObjectInfo($bucket, $uri, $returnInfo = true)   X-Ref
Get object information

param: string $bucket Bucket name
param: string $uri Object URI
param: boolean $returnInfo Return response information
return: mixed | false

copyObject($srcBucket, $srcUri, $bucket, $uri, $acl = self::ACL_PRIVATE, $metaHeaders = array()   X-Ref
Copy an object

param: string $srcBucket Source bucket name
param: string $srcUri Source object URI
param: string $bucket Destination bucket name
param: string $uri Destination object URI
param: constant $acl ACL constant
param: array $metaHeaders Optional array of x-amz-meta-* headers
param: array $requestHeaders Optional array of request headers (content type, disposition, etc.)
param: constant $storageClass Storage class constant
return: mixed | false

setBucketRedirect($bucket = NULL, $location = NULL)   X-Ref
Set up a bucket redirection

param: string $bucket Bucket name
param: string $location Target host name
return: boolean

setBucketLogging($bucket, $targetBucket, $targetPrefix = null)   X-Ref
Set logging for a bucket

param: string $bucket Bucket name
param: string $targetBucket Target bucket (where logs are stored)
param: string $targetPrefix Log prefix (e,g; domain.com-)
return: boolean

getBucketLogging($bucket)   X-Ref
Get logging status for a bucket

This will return false if logging is not enabled.
Note: To enable logging, you also need to grant write access to the log group

param: string $bucket Bucket name
return: array | false

disableBucketLogging($bucket)   X-Ref
Disable bucket logging

param: string $bucket Bucket name
return: boolean

getBucketLocation($bucket)   X-Ref
Get a bucket's location

param: string $bucket Bucket name
return: string | false

setAccessControlPolicy($bucket, $uri = '', $acp = array()   X-Ref
Set object or bucket Access Control Policy

param: string $bucket Bucket name
param: string $uri Object URI
param: array $acp Access Control Policy Data (same as the data returned from getAccessControlPolicy)
return: boolean

getAccessControlPolicy($bucket, $uri = '')   X-Ref
Get object or bucket Access Control Policy

param: string $bucket Bucket name
param: string $uri Object URI
return: mixed | false

deleteObject($bucket, $uri)   X-Ref
Delete an object

param: string $bucket Bucket name
param: string $uri Object URI
return: boolean

getAuthenticatedURL($bucket, $uri, $lifetime, $hostBucket = false, $https = false)   X-Ref
Get a query string authenticated URL

param: string $bucket Bucket name
param: string $uri Object URI
param: integer $lifetime Lifetime in seconds
param: boolean $hostBucket Use the bucket name as the hostname
param: boolean $https Use HTTPS ($hostBucket should be false for SSL verification)
return: string

getSignedPolicyURL($policy)   X-Ref
Get a CloudFront signed policy URL

param: array $policy Policy
return: string

getSignedCannedURL($url, $lifetime)   X-Ref
Get a CloudFront canned policy URL

param: string $url URL to sign
param: integer $lifetime URL lifetime
return: string

getHttpUploadPostParams($bucket, $uriPrefix = '', $acl = self::ACL_PRIVATE, $lifetime = 3600,$maxFileSize = 5242880, $successRedirect = "201", $amzHeaders = array()   X-Ref
Get upload POST parameters for form uploads

param: string $bucket Bucket name
param: string $uriPrefix Object URI prefix
param: constant $acl ACL constant
param: integer $lifetime Lifetime in seconds
param: integer $maxFileSize Maximum filesize in bytes (default 5MB)
param: string $successRedirect Redirect URL or 200 / 201 status code
param: array $amzHeaders Array of x-amz-meta-* headers
param: array $headers Array of request headers or content type as a string
param: boolean $flashVars Includes additional "Filename" variable posted by Flash
return: object

createDistribution($bucket, $enabled = true, $cnames = array()   X-Ref
Create a CloudFront distribution

param: string $bucket Bucket name
param: boolean $enabled Enabled (true/false)
param: array $cnames Array containing CNAME aliases
param: string $comment Use the bucket name as the hostname
param: string $defaultRootObject Default root object
param: string $originAccessIdentity Origin access identity
param: array $trustedSigners Array of trusted signers
return: array | false

getDistribution($distributionId)   X-Ref
Get CloudFront distribution info

param: string $distributionId Distribution ID from listDistributions()
return: array | false

updateDistribution($dist)   X-Ref
Update a CloudFront distribution

param: array $dist Distribution array info identical to output of getDistribution()
return: array | false

deleteDistribution($dist)   X-Ref
Delete a CloudFront distribution

param: array $dist Distribution array info identical to output of getDistribution()
return: boolean

listDistributions()   X-Ref
Get a list of CloudFront distributions

return: array

listOriginAccessIdentities()   X-Ref
List CloudFront Origin Access Identities

return: array

invalidateDistribution($distributionId, $paths)   X-Ref
Invalidate objects in a CloudFront distribution

Thanks to Martin Lindkvist for S3::invalidateDistribution()

param: string $distributionId Distribution ID from listDistributions()
param: array $paths Array of object paths to invalidate
return: boolean

__getCloudFrontInvalidationBatchXML($paths, $callerReference = '0')   X-Ref
Get a InvalidationBatch DOMDocument

param: array $paths Paths to objects to invalidateDistribution
param: int $callerReference
return: string

getDistributionInvalidationList($distributionId)   X-Ref
List your invalidation batches for invalidateDistribution() in a CloudFront distribution

http://docs.amazonwebservices.com/AmazonCloudFront/latest/APIReference/ListInvalidation.html
returned array looks like this:
Array
(
[I31TWB0CN9V6XD] => InProgress
[IT3TFE31M0IHZ] => Completed
[I12HK7MPO1UQDA] => Completed
[I1IA7R6JKTC3L2] => Completed
)

param: string $distributionId Distribution ID from listDistributions()
return: array

__getCloudFrontDistributionConfigXML($bucket, $enabled, $comment, $callerReference = '0', $cnames = array()   X-Ref
Get a DistributionConfig DOMDocument

http://docs.amazonwebservices.com/AmazonCloudFront/latest/APIReference/index.html?PutConfig.html

param: string $bucket S3 Origin bucket
param: boolean $enabled Enabled (true/false)
param: string $comment Comment to append
param: string $callerReference Caller reference
param: array $cnames Array of CNAME aliases
param: string $defaultRootObject Default root object
param: string $originAccessIdentity Origin access identity
param: array $trustedSigners Array of trusted signers
return: string

__parseCloudFrontDistributionConfig(&$node)   X-Ref
Parse a CloudFront distribution config

See http://docs.amazonwebservices.com/AmazonCloudFront/latest/APIReference/index.html?GetDistribution.html

param: object &$node DOMNode
return: array

__getCloudFrontResponse(&$rest)   X-Ref
Grab CloudFront response

param: object &$rest S3Request instance
return: object

__getMIMEType(&$file)   X-Ref
Get MIME type for file

To override the putObject() Content-Type, add it to $requestHeaders

To use fileinfo, ensure the MAGIC environment variable is set

param: string &$file File path
return: string

__getTime()   X-Ref
Get the current time

return: integer

__getSignature($string)   X-Ref
Generate the auth string: "AWS AccessKey:Signature"

param: string $string String to sign
return: string

__getHash($string)   X-Ref
Creates a HMAC-SHA1 hash

This uses the hash extension if loaded

param: string $string String to sign
return: string

Class: S3Request  - X-Ref

S3 Request class

__construct($verb, $bucket = '', $uri = '', $endpoint = 's3.amazonaws.com')   X-Ref
Constructor

param: string $verb Verb
param: string $bucket Bucket name
param: string $uri Object URI
param: string $endpoint AWS endpoint URI
return: mixed

setParameter($key, $value)   X-Ref
Set request parameter

param: string $key Key
param: string $value Value
return: void

setHeader($key, $value)   X-Ref
Set request header

param: string $key Key
param: string $value Value
return: void

setAmzHeader($key, $value)   X-Ref
Set x-amz-meta-* header

param: string $key Key
param: string $value Value
return: void

getResponse()   X-Ref
Get the S3 response

return: object | false

__sortMetaHeadersCmp($a, $b)   X-Ref
Sort compare for meta headers

param: string $a String A
param: string $b String B
return: integer

__responseWriteCallback(&$curl, &$data)   X-Ref
CURL write callback

param: resource &$curl CURL resource
param: string &$data Data
return: integer

__dnsBucketName($bucket)   X-Ref
Check DNS conformity

param: string $bucket Bucket name
return: boolean

__responseHeaderCallback($curl, $data)   X-Ref
CURL header callback

param: resource $curl CURL resource
param: string $data Data
return: integer

Class: S3Exception  - X-Ref

S3 exception class

__construct($message, $file, $line, $code = 0)   X-Ref
Class constructor

param: string $message Exception message
param: string $file File in which exception was created
param: string $line Line number on which exception was created
param: int $code Exception code