1: 2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14: 15: 16: 17: 18: 19: 20: 21: 22: 23: 24: 25: 26: 27: 28: 29: 30: 31: 32: 33: 34: 35: 36: 37: 38: 39: 40: 41: 42: 43: 44: 45: 46: 47: 48: 49: 50: 51: 52: 53: 54: 55: 56: 57: 58: 59: 60: 61: 62: 63: 64: 65: 66: 67: 68: 69: 70: 71: 72: 73: 74: 75: 76: 77: 78: 79: 80: 81: 82: 83: 84: 85: 86: 87: 88: 89: 90: 91: 92: 93: 94: 95: 96: 97: 98: 99: 100: 101: 102: 103: 104: 105: 106: 107: 108: 109: 110: 111: 112: 113: 114: 115: 116: 117: 118: 119: 120: 121: 122: 123: 124: 125: 126: 127: 128: 129: 130: 131: 132: 133: 134: 135: 136: 137: 138: 139: 140: 141: 142: 143: 144: 145: 146: 147: 148: 149: 150: 151: 152: 153: 154: 155: 156: 157: 158: 159: 160: 161: 162: 163: 164: 165: 166: 167: 168: 169: 170: 171: 172: 173: 174: 175: 176: 177: 178: 179: 180: 181: 182: 183: 184: 185: 186: 187: 188: 189: 190: 191: 192: 193: 194: 195: 196: 197: 198: 199: 200: 201: 202: 203: 204: 205: 206: 207: 208: 209: 210: 211: 212: 213: 214: 215: 216: 217: 218: 219: 220: 221: 222: 223: 224: 225: 226: 227: 228: 229: 230: 231: 232: 233: 234: 235: 236: 237: 238: 239: 240: 241: 242: 243: 244: 245: 246: 247: 248: 249: 250: 251: 252: 253: 254: 255: 256: 257: 258: 259: 260: 261: 262: 263: 264: 265: 266: 267: 268: 269: 270: 271: 272: 273: 274: 275: 276: 277: 278: 279: 280: 281: 282: 283: 284: 285: 286: 287: 288: 289: 290: 291: 292: 293: 294: 295: 296: 297: 298: 299:
<?php
namespace Baguette\Mastodon\Config;
use Baguette\Mastodon;
use Baguette\Mastodon\Service\Authorization;
use Baguette\Mastodon\Service\Scope;
use Respect\Validation\Validator as v;
class DotEnvStorage extends \Dotenv\Loader implements Storage
{
use \Teto\Object\ReadOnly;
private $file_path;
private $key_names = [
'app_name' => 'APP_NAME',
'client_id' => 'CLIENT_ID',
'client_secret' => 'CLIENT_SECRET',
'username' => 'USERNAME',
'password' => 'PASSWORD',
'access_token' => 'ACCESS_TOKEN',
'scope' => 'SCOPE',
'created_at' => 'CREATED_AT',
];
private $read_only = true;
private $save_values = [];
public function __construct($file_path, $options = [])
{
$this->file_path = $file_path;
if (isset($options['key_names'])) {
$this->key_names = $options['key_names'] + $this->key_names;
}
if (isset($options['read_only'])) {
v::boolType()->assert($options['read_only']);
$this->read_only = $options['read_only'];
}
if (false) {
parent::__construct($file_path);
}
}
public function getAppName()
{
$appname_key = $this->key_names['app_name'];
$values = $this->getValues();
return [
'app_name' => isset($values[$appname_key]) ? $values[$appname_key] : null,
];
}
public function setAppName($app_name)
{
v::stringType()->not(v::contains("\n"))->assert($app_name);
$this->save_values['app_name'] = $app_name;
}
public function getAuthorization()
{
$token_key = $this->key_names['access_token'];
$scope_key = $this->key_names['scope'];
$cat_key = $this->key_names['created_at'];
$values = $this->getValues();
return [
'access_token' => isset($values[$token_key]) ? $values[$token_key] : null,
'scope' => isset($values[$scope_key]) ? $values[$scope_key] : null,
'created_at' => isset($values[$cat_key]) ? $values[$cat_key] : null,
];
}
public function setAuthorization($access_token, $scope = null, $created_at = null)
{
v::stringType()->not(v::contains("\n"))->assert($access_token);
v::intType()->min(0)->assert($created_at);
$this->save_values['access_token'] = $access_token;
$this->save_values['scope'] = (string)Mastodon\scope($scope);
$this->save_values['created_at'] = $created_at;
}
public function setAuthorizationFromObject(Authorization $authorization)
{
$this->setAuthorization(
$authorization->access_token,
$authorization->scope,
$authorization->created_at ? $authorization->created_at->getTimestamp() : time()
);
}
public function getClientIdAndSecret()
{
$id_key = $this->key_names['client_id'];
$sec_key = $this->key_names['client_secret'];
$values = $this->getValues();
return [
'client_id' => isset($values[$id_key]) ? $values[$id_key] : null,
'client_secret' => isset($values[$sec_key]) ? $values[$sec_key] : null,
];
}
public function setClientIdAndSecret($client_id, $client_secret)
{
v::stringType()->not(v::contains("\n"))->assert($client_id);
v::stringType()->not(v::contains("\n"))->assert($client_secret);
$this->save_values['client_id'] = $client_id;
$this->save_values['client_secret'] = $client_secret;
}
public function getUsernameAndPassword()
{
$user_key = $this->key_names['username'];
$pass_key = $this->key_names['password'];
$values = $this->getValues();
return [
'username' => isset($values[$user_key]) ? $values[$user_key] : null,
'password' => isset($values[$pass_key]) ? $values[$pass_key] : null,
];
}
public function setUsernameAndPassword($username, $password)
{
v::stringType()->not(v::contains("\n"))->assert($username);
v::stringType()->not(v::contains("\n"))->assert($password);
$this->save_values['username'] = $username;
$this->save_values['password'] = $password;
}
public function getValues()
{
$values = [];
foreach ($this->getLines() as $line) {
if (!$this->isComment($line) && $this->looksLikeSetter($line)) {
list($key, $val) = $this->normaliseEnvironmentVariable($line, '');
$values[$key] = $val;
}
}
return $values;
}
public function getLines($fp = null)
{
$fp = ($fp === null) ? $this->file_path : $fp;
if (is_string($fp)) {
return $this->readLinesFromFile($fp);
}
rewind($fp);
return preg_split("/\r?\n/", stream_get_contents($fp));
}
public function save()
{
if ($this->read_only) {
throw new \RuntimeException('This config store is read only.');
}
$tbl = array_flip($this->key_names);
$opend_file = false;
if (is_string($this->file_path)) {
$fp = fopen($this->file_path, 'rwb');
$opend_file = true;
} else {
$fp = $this->file_path;
rewind($fp);
}
$lines = $this->getLines($fp);
$orig_lines = $lines;
if ($opend_file) {
fclose($fp);
}
foreach ($lines as $i => $line) {
if ($this->isComment($line) || !$this->looksLikeSetter($line)) {
continue;
}
list($key, $val) = $this->normaliseEnvironmentVariable($line, '');
if (isset($tbl[$key]) && isset($this->save_values[$tbl[$key]])) {
$saveval = $this->save_values[$tbl[$key]];
$lines[$i] = sprintf('%s=%s', $key, $this->quoteValue($saveval));
unset($this->save_values[$tbl[$key]]);
}
}
foreach ($this->save_values as $key => $saveval) {
$lines[] = sprintf('%s=%s', $this->key_names[$key], $this->quoteValue($saveval));
}
if ($orig_lines === $lines) {
return;
}
if ($opend_file) {
file_put_contents($this->file_path, implode("\n", $lines) . "\n");
} else {
rewind($fp);
fwrite($fp, implode("\n", $lines) . "\n");
}
}
public static function quoteValue($value)
{
return sprintf('"%s"', strtr($value, [
'"' => '\\"',
]));
}
}