wpAliases/wpAliases.php

89 lines
2.4 KiB
PHP
Executable File

#!/usr/bin/php
<?php
require 'getwebsites.cfg.php';
// Get password from the "pass" utility.
$password = $password[0];
$headers = login($password);
// Pull the view that shows the list of websites
$resource = 'views/website_list?display_id=services_1';
// Get the host the script is running on & only make dev aliases when the server matches
$host = gethostname();
$websites = get($headers, $resource, NULL);
$websiteList = [];
$yaml = '';
foreach ($websites as $website) {
$key = count($websiteList);
if ($website['env'] === 'Dev' && $host !== $website['server']) {
continue;
}
if ($website['cms'] === 'WordPress') {
if ($website['env'] === 'Live') {
$aliasName = $website['short_name'] ?? $website['client'];
$alias = '@' . $aliasName;
$websiteList[$alias]['ssh'] = $website['server'];
}
else {
$alias = '@' . $aliasName . '-' . strtolower($website['env']);
}
$websiteList[$alias]['path'] = $website['webroot'];
$websiteList[$alias]['url'] = $website['primary_url'];
}
};
ksort($websiteList);
$yaml = yaml_emit_file($aliasesFile, $websiteList);
var_dump($yaml);
function post($curlHeaders, $operation, $postFields = NULL) {
$curlHeaders[] = 'Content-Type: application/json';
$curlHeaders[] = 'Accept: application/json';
$curlOptions = [
CURLOPT_HTTPHEADER => $curlHeaders,
CURLOPT_RETURNTRANSFER => TRUE,
CURLOPT_POST => 1,
CURLOPT_URL => ENDPOINT . $operation,
CURLOPT_POSTFIELDS => $postFields ? json_encode($postFields) : NULL,
];
$curl = curl_init();
curl_setopt_array($curl, $curlOptions);
$result = curl_exec($curl);
curl_close($curl);
return json_decode($result);
}
function get($curlHeaders, $operation, $body = NULL)
{
$curlHeaders[] = 'Accept: application/json';
$curlOptions = [
CURLOPT_HTTPHEADER => $curlHeaders,
CURLOPT_RETURNTRANSFER => TRUE,
CURLOPT_URL => ENDPOINT . $operation,
];
$curl = curl_init();
curl_setopt_array($curl, $curlOptions);
$result = curl_exec($curl);
curl_close($curl);
return json_decode($result, TRUE);
};
/**
* Handle login (including getting a CSRF header)
* @return array $headers The necessary headers to perform further POST/GET actions.
*/
function login($password)
{
$creds = [
'username' => USERNAME,
'password' => $password,
];
$result = post(NULL, 'user/login.json', $creds);
$headers[] = "Cookie: $result->session_name=$result->sessid";
return $headers;
}