public function update_config_xml($xml, $opt = null)
{
// If we receive a full CFResponse object, only use the body.
if ($xml instanceof CFResponse)
{
$xml = $xml->body;
}
// If we received a string of XML, convert it into a CFSimpleXML object.
if (is_string($xml))
{
$xml = simplexml_load_string($xml, $this->parser_class);
}
// Default, empty XML
$update = simplexml_load_string(sprintf($this->base_xml, (
(isset($opt['Streaming']) && $opt['Streaming'] == (bool) true) ? 'StreamingDistributionConfig' : 'DistributionConfig')
), $this->parser_class);
// These can't change.
if (isset($xml->S3Origin))
{
$origin = $update->addChild('S3Origin');
$origin->addChild('DNSName', $xml->S3Origin->DNSName);
// origin access identity
if (isset($opt['OriginAccessIdentity']))
{
$origin->addChild('OriginAccessIdentity', 'origin-access-identity/cloudfront/' . $opt['OriginAccessIdentity']);
}
elseif (isset($xml->S3Origin->OriginAccessIdentity))
{
$origin->addChild('OriginAccessIdentity', $xml->S3Origin->OriginAccessIdentity);
}
}
elseif (isset($xml->CustomOrigin))
{
$origin = $update->addChild('CustomOrigin');
$origin->addChild('DNSName', $xml->CustomOrigin->DNSName);
// Copy OriginProtocolPolicy for update
if ( isset($xml->CustomOrigin->OriginProtocolPolicy) )
{
$origin->addChild('OriginProtocolPolicy', $xml->CustomOrigin->OriginProtocolPolicy);
}
// origin access identity
if (isset($opt['OriginAccessIdentity']))
{
$origin->addChild('OriginAccessIdentity', 'origin-access-identity/cloudfront/' . $opt['OriginAccessIdentity']);
}
elseif (isset($xml->CustomOrigin->OriginAccessIdentity))
{
$origin->addChild('OriginAccessIdentity', $xml->CustomOrigin->OriginAccessIdentity);
}
}
$update->addChild('CallerReference', $xml->CallerReference);
// Add existing CNAME values
if ($xml->CNAME)
{
$update->addChild('CNAME', $xml->CNAME);
}
// Add new CNAME values
if (isset($opt['CNAME']))
{
if (is_array($opt['CNAME']))
{
foreach ($opt['CNAME'] as $cname)
{
$update->addChild('CNAME', $cname);
}
}
else
{
$update->addChild('CNAME', $opt['CNAME']);
}
}
// Comment
if (isset($opt['Comment']))
{
$update->addChild('Comment', $opt['Comment']);
}
elseif (isset($xml->Comment))
{
$update->addChild('Comment', $xml->Comment);
}
// DefaultRootObject
if (isset($opt['DefaultRootObject']))
{
$update->addChild('DefaultRootObject', $opt['DefaultRootObject']);
}
elseif (isset($xml->DefaultRootObject))
{
$update->addChild('DefaultRootObject', $xml->DefaultRootObject);
}
// Enabled
if (isset($opt['Enabled']))
{
$update->addChild('Enabled', $opt['Enabled'] ? 'true' : 'false');
}
elseif (isset($xml->Enabled))
{
$update->addChild('Enabled', $xml->Enabled);
}
// Logging
if (isset($opt['Logging']))
{
if (is_array($opt['Logging']))
{
$logging = $update->addChild('Logging');
$bucket_name = $opt['Logging']['Bucket'];
// Origin
$logging->addChild('Bucket', $bucket_name . ((stripos($bucket_name, '.s3.amazonaws.com') === false) ? '.s3.amazonaws.com' : ''));
$logging->addChild('Prefix', $opt['Logging']['Prefix']);
}
}
elseif (isset($xml->Logging))
{
$logging = $update->addChild('Logging');
$logging->addChild('Bucket', $xml->Logging->Bucket);
$logging->addChild('Prefix', $xml->Logging->Prefix);
}
// Required Protocols
if (isset($opt['RequiredProtocols']))
{
$required_protocols = $xml->addChild('RequiredProtocols');
$required_protocols->addChild('Protocol', $opt['RequiredProtocols']);
}
elseif (isset($xml->RequiredProtocols))
{
$required_protocols = $update->addChild('RequiredProtocols');
$required_protocols->addChild('Protocol', $xml->RequiredProtocols->Protocol);
}
// Caching Behavior
if (isset($opt['CachingBehavior']) && is_array($opt['CachingBehavior']))
{
$caching_behavior = $xml->addChild('CachingBehavior');
if (isset($opt['CachingBehavior']['MinTTL']))
{
$min_ttl = $opt['CachingBehavior']['MinTTL'];
if (is_string($min_ttl))
{
$min_ttl = strtotime($min_ttl);
}
$caching_behavior->addChild('MinTTL', $min_ttl);
}
}
elseif (isset($xml->CachingBehavior))
{
$caching_behavior = $update->addChild('CachingBehavior');
$caching_behavior->addChild('MinTTL', $xml->CachingBehavior->MinTTL);
}
// Trusted Signers
if (isset($opt['TrustedSigners']))
{
$trusted_signers = $update->addChild('TrustedSigners');
// Not an array? Convert to one.
if (!is_array($opt['TrustedSigners']))
{
$opt['TrustedSigners'] = array($opt['TrustedSigners']);
}
// Handle 'Self' vs. everything else
foreach ($opt['TrustedSigners'] as $signer)
{
if (strtolower($signer) === 'self')
{
$trusted_signers->addChild('Self');
}
else
{
$trusted_signers->addChild('AwsAccountNumber', $signer);
}
}
}
elseif (isset($xml->TrustedSigners) && $xml->TrustedSigners->count())
{
$trusted_signers = $update->addChild('TrustedSigners');
// Handle 'Self' vs. everything else
foreach ($xml->TrustedSigners->children() as $signer_key => $signer_value)
{
if (strtolower((string) $signer_key) === 'self')
{
$trusted_signers->addChild('Self');
}
else
{
$trusted_signers->addChild('AwsAccountNumber', (string) $signer_value);
}
}
}
// Output
return $update->asXML();
}