View source
<?php
namespace Drupal\acsf;
use Drupal\acsf\Event\AcsfEvent;
class AcsfSite {
protected static $cache;
protected $variableStorage;
protected $info = [];
public static function load() {
if (!empty(self::$cache)) {
return self::$cache;
}
self::$cache = new static();
return self::$cache;
}
public function __construct($site_id = NULL) {
$this->variableStorage = \Drupal::service('acsf.variable_storage');
if (empty($site_id) && !empty($GLOBALS['gardens_site_settings']['conf']['acsf_site_id'])) {
$site_id = $GLOBALS['gardens_site_settings']['conf']['acsf_site_id'];
}
if (function_exists('is_acquia_host') && is_acquia_host() && empty($site_id)) {
throw new AcsfSiteMissingIdentifierException('Cannot instantiate AcsfSite without a site id from the Site Factory. Ensure that it is passed to the constructor or set in sites.json.');
}
$this
->initStoredSiteInfo();
if (!empty($GLOBALS['gardens_site_settings']['conf'])) {
$this
->mergeSiteInfo($GLOBALS['gardens_site_settings']['conf']);
}
unset($this->gardens_site_id);
unset($this->gardens_db_name);
unset($this->acsf_site_id);
unset($this->acsf_db_name);
$this->site_id = $site_id;
}
public function __set($key, $value) {
$this->info[$key] = $value;
}
public function __get($key) {
if (isset($this->info[$key])) {
return $this->info[$key];
}
}
public function __isset($key) {
return isset($this->info[$key]);
}
public function __unset($key) {
if (isset($this->info[$key])) {
unset($this->info[$key]);
}
}
public function refresh(array $data = []) {
if (function_exists('is_acquia_host') && !is_acquia_host()) {
return FALSE;
}
try {
$site_id = !empty($this->site_id) ? $this->site_id : $GLOBALS['gardens_site_settings']['conf']['acsf_site_id'];
$arguments = [
'site_id' => $site_id,
'site_data' => $data,
];
$message = new AcsfMessageRest('GET', 'site-api/v1/sync/' . $site_id, $arguments);
$message
->send();
$site_info = $message
->getResponseBody();
} catch (AcsfMessageFailedResponseException $e) {
}
if (empty($site_info)) {
\Drupal::logger('acsf_site')
->critical('Could not retrieve site information after installation.');
return FALSE;
}
else {
$context = $site_info;
$event = AcsfEvent::create('acsf_site_data_receive', $context);
$event
->run();
$this
->saveSiteInfo($site_info['sf_site']);
return TRUE;
}
}
public function clean() {
$this->info = [];
return $this
->refresh();
}
public function save() {
$context = $this->info;
$event = AcsfEvent::create('acsf_site_data_save', $context);
$event
->run();
$this->info = $event->context;
$this->variableStorage
->set('acsf_site_info', $this->info, 'site_info');
}
private function initStoredSiteInfo() {
$this->info = [];
$site_info = $this->variableStorage
->getGroup('site_info');
foreach ($site_info as $key => $value) {
if (!is_array($value)) {
$this
->mergeSiteInfo([
$key => $value,
]);
}
else {
$this
->mergeSiteInfo($value);
}
}
}
public function initSamlKeyProperties() {
$sitegroup = $_ENV['AH_SITE_GROUP'];
$env = $_ENV['AH_SITE_ENVIRONMENT'];
$creds_path = "/mnt/files/{$sitegroup}.{$env}/nobackup/sf_shared_creds.ini";
$credentials = file_get_contents($creds_path);
$parsed_ini = parse_ini_string($credentials, TRUE);
$this->info['saml_keys'] = [];
if (isset($parsed_ini['saml']['tangle_key'])) {
$this->info['saml_keys']['sp_private_key'] = $parsed_ini['saml']['tangle_key'];
}
else {
$this->info['saml_keys']['sp_private_key'] = '';
}
if (isset($parsed_ini['saml']['tangle_cert'])) {
$this->info['saml_keys']['sp_x509_certificate'] = $parsed_ini['saml']['tangle_cert'];
}
else {
$this->info['saml_keys']['sp_x509_certificate'] = '';
}
if (isset($parsed_ini['saml']['factory_cert'])) {
$this->info['saml_keys']['idp_x509_certificate'] = $parsed_ini['saml']['factory_cert'];
}
else {
$this->info['saml_keys']['idp_x509_certificate'] = '';
}
}
public function mergeSiteInfo(array $site_info) {
if (!empty($site_info)) {
foreach ($site_info as $key => $value) {
$this->info[$key] = $value;
}
}
}
public function saveSiteInfo(array $site_info) {
$this
->mergeSiteInfo($site_info);
$this->last_sf_refresh = time();
$this
->save();
}
}