Skip to content

Commit

Permalink
Improve permissions
Browse files Browse the repository at this point in the history
  • Loading branch information
wizzymore committed Jul 14, 2022
1 parent 5584170 commit a9e5ef6
Show file tree
Hide file tree
Showing 5 changed files with 131 additions and 44 deletions.
40 changes: 39 additions & 1 deletion cli/Valet/Hosts.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,11 +48,49 @@ public function link(string $siteName)
return;
}

$contents[] = "127.0.0.1 $url #" . self::comment . "\r\n";
$contentsText = "127.0.0.1 $url";

for ($i = 0; $i < 24 - strlen($url); $i++) {
$contentsText .= ' ';
}
$contents[] = $contentsText . "#" . self::comment . "\r\n";

$this->files->put(self::hosts_path, implode('', $contents));
}

/**
* Clear all linked websites from the hosts file.
*
* @return void
*/
public function clear()
{
$domain = $this->configuration->get('domain');

$contents = file(self::hosts_path);
$contentsCount = count($contents);

for ($i = 0; $i < $contentsCount; $i++) {
if (strpos($contents[$i], '.' . $domain) !== false) {
unset($contents[$i]);
}
}

$this->files->put(self::hosts_path, implode('', $contents));
}

/**
* Add all the websites currently linked to the hosts file.
*
* @return void
*/
public function linkAll()
{
foreach ($this->files->scanDir(VALET_HOME_PATH . '/Sites') as $site) {
$this->link($site);
}
}

/**
* Remove an existing record from the hosts file.
*
Expand Down
23 changes: 21 additions & 2 deletions cli/Valet/Valet.php
Original file line number Diff line number Diff line change
Expand Up @@ -114,13 +114,32 @@ public function getLatestVersion()
* @return void
*/
public function environmentSetup()
{
$this->packageManagerSetup();
$this->serviceManagerSetup();
}

/**
* Require admin rights.
*
* @return void
*/
public function checkAdminRights()
{
if ((!$this->files->writable(VALET_HOSTS_PATH) || !$this->files->readable(VALET_HOSTS_PATH))) {
warning('You must start WSL as administrator.');
exit();
}
$this->packageManagerSetup();
$this->serviceManagerSetup();
}

/**
* Check if the user has admin rights.
*
* @return bool
*/
public function hasAdminRights()
{
return $this->files->writable(VALET_HOSTS_PATH) && $this->files->readable(VALET_HOSTS_PATH);
}

/**
Expand Down
14 changes: 13 additions & 1 deletion cli/includes/helpers.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
*/
function info($output)
{
output('<info>' . $output . '</info>');
output('<fg=yellow>' . $output . '</>');
}

/**
Expand All @@ -38,6 +38,18 @@ function warning($output)
output('<fg=red>' . $output . '</>');
}

/**
* Output the given text to the console.
*
* @param string $output
*
* @return void
*/
function success($output)
{
output('<fg=green>' . $output . '</>');
}

/**
* Output a table to the console.
*
Expand Down
80 changes: 49 additions & 31 deletions cli/valet.php
Original file line number Diff line number Diff line change
Expand Up @@ -67,16 +67,21 @@
return info(Configuration::read()['domain']);
}

Valet::checkAdminRights();

Hosts::clear();

$oldDomain = Configuration::read()['domain'];
$domain = trim($domain, '.');

Configuration::updateKey('domain', $domain);
Hosts::linkAll();
Site::resecureForNewDomain($oldDomain, $domain);
// Mailhog::updateDomain();
PhpFpm::restart();
Nginx::restart();

info('Your Valet domain has been updated to [' . $domain . '].');
success('Your Valet domain has been updated to [' . $domain . '].');
})->descriptions('Get or set the domain used for Valet sites');

/**
Expand Down Expand Up @@ -105,7 +110,7 @@
PhpFpm::restart();

$protocol = $https ? 'HTTPS' : 'HTTP';
info("Your Nginx {$protocol} port has been updated to [{$port}].");
success("Your Nginx {$protocol} port has been updated to [{$port}].");
})->descriptions('Get or set the port number used for Valet sites');

/**
Expand Down Expand Up @@ -134,7 +139,7 @@

Configuration::addPath($path ?: getcwd());

info(($path === null ? 'This' : "The [{$path}]") . " directory has been added to Valet's paths.");
success(($path === null ? 'This' : "The [{$path}]") . " directory has been added to Valet's paths.");
})->descriptions('Register the current working (or specified) directory with Valet');

/**
Expand All @@ -146,7 +151,7 @@

Configuration::removePath($path ?: getcwd());

info(($path === null ? 'This' : "The [{$path}]") . " directory has been removed from Valet's paths.");
success(($path === null ? 'This' : "The [{$path}]") . " directory has been removed from Valet's paths.");
})->descriptions('Remove the current working (or specified) directory from Valet\'s list of paths');

/**
Expand All @@ -161,11 +166,13 @@
* Register a symbolic link with Valet.
*/
$app->command('link [name]', function ($name) {
Valet::checkAdminRights();

$linkPath = Site::link(getcwd(), $name = $name ?: basename(getcwd()));

Hosts::link($name);

info('A [' . $name . '] symbolic link has been created in [' . $linkPath . '].');
success('A [' . $name . '] symbolic link has been created in [' . $linkPath . '].');
})->descriptions('Link the current working directory to Valet');

/**
Expand All @@ -181,43 +188,51 @@
* Unlink a link from the Valet links directory.
*/
$app->command('unlink [name]', function ($name) {
Valet::checkAdminRights();

Site::unlink($name = $name ?: basename(getcwd()));

Hosts::unlink($name);

info('The [' . $name . '] symbolic link has been removed.');
success('The [' . $name . '] symbolic link has been removed.');
})->descriptions('Remove the specified Valet link');

/**
* Secure the given domain with a trusted TLS certificate.
*/
$app->command('secure [domain]', function ($domain = null) {
Valet::checkAdminRights();

$url = ($domain ?: Site::host(getcwd())) . '.' . Configuration::read()['domain'];

Site::secure($url);
PhpFpm::restart();
Nginx::restart();

info('The [' . $url . '] site has been secured with a fresh TLS certificate.');
success('The [' . $url . '] site has been secured with a fresh TLS certificate.');
})->descriptions('Secure the given domain with a trusted TLS certificate');

/**
* Stop serving the given domain over HTTPS and remove the trusted TLS certificate.
*/
$app->command('unsecure [domain]', function ($domain = null) {
Valet::checkAdminRights();

$url = ($domain ?: Site::host(getcwd())) . '.' . Configuration::read()['domain'];

Site::unsecure($url);
PhpFpm::restart();
Nginx::restart();

info('The [' . $url . '] site will now serve traffic over HTTP.');
success('The [' . $url . '] site will now serve traffic over HTTP.');
})->descriptions('Stop serving the given domain over HTTPS and remove the trusted TLS certificate');

/**
* Register a subdomain link.
*/
$app->command('subdomain:create [name] [--secure]', function ($name, $secure) {
Valet::checkAdminRights();

$name = $name ?: 'www';
Site::link(getcwd(), $name . '.' . basename(getcwd()));

Expand All @@ -226,17 +241,19 @@
}
$domain = Configuration::read()['domain'];

info('Subdomain ' . $name . '.' . basename(getcwd()) . '.' . $domain . ' created');
success('Subdomain ' . $name . '.' . basename(getcwd()) . '.' . $domain . ' created');
})->descriptions('Create a subdomains');

/**
* Unregister a subdomain link.
*/
$app->command('subdomain:remove [name]', function ($name) {
Valet::checkAdminRights();

$name = $name ?: 'www';
Site::unlink($name . '.' . basename(getcwd()));
$domain = Configuration::read()['domain'];
info('Subdomain ' . $name . '.' . basename(getcwd()) . '.' . $domain . ' removed');
success('Subdomain ' . $name . '.' . basename(getcwd()) . '.' . $domain . ' removed');
})->descriptions('Remove a subdomains');

/**
Expand All @@ -256,9 +273,9 @@
$driver = ValetDriver::assign(getcwd(), basename(getcwd()), '/');

if ($driver) {
info('This site is served by [' . get_class($driver) . '].');
success('This site is served by [' . get_class($driver) . '].');
} else {
warning('Valet could not determine which driver to use for this site.');
info('Valet could not determine which driver to use for this site.');
}
})->descriptions('Determine which Valet driver serves the current working directory');

Expand All @@ -268,13 +285,13 @@
$app->command('paths', function () {
warning('Currently this command is disabled as we can\'t achieve dynamic domains without a DNS Proxy.');
exit();
$paths = Configuration::read()['paths'];
// $paths = Configuration::read()['paths'];

if (count($paths) > 0) {
info(json_encode($paths, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES));
} else {
warning('No paths have been registered.');
}
// if (count($paths) > 0) {
// info(json_encode($paths, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES));
// } else {
// warning('No paths have been registered.');
// }
})->descriptions('Get all of the paths registered with Valet');

/**
Expand Down Expand Up @@ -317,7 +334,7 @@
// Mailhog::restart();
Mysql::restart();
ValetRedis::restart();
info('Valet services have been started.');
success('Valet services have been started.');

return;
}
Expand Down Expand Up @@ -345,7 +362,7 @@
}
}

info('Specified Valet services have been started.');
success('Specified Valet services have been started.');
})->descriptions('Start the Valet services');

/**
Expand All @@ -358,7 +375,7 @@
// Mailhog::restart();
Mysql::restart();
ValetRedis::restart();
info('Valet services have been restarted.');
success('Valet services have been restarted.');

return;
}
Expand Down Expand Up @@ -400,7 +417,7 @@
}
}

info('Specified Valet services have been restarted.');
success('Specified Valet services have been restarted.');
})->descriptions('Restart the Valet services');

/**
Expand All @@ -416,7 +433,7 @@
// Elasticsearch::stop();
// RabbitMq::stop();
// Varnish::stop();
info('Valet services have been stopped.');
success('Valet services have been stopped.');

return;
}
Expand Down Expand Up @@ -458,20 +475,21 @@
}
}

info('Specified Valet services have been stopped.');
success('Specified Valet services have been stopped.');
})->descriptions('Stop the Valet services');

/**
* Uninstall Valet entirely.
*/
$app->command('uninstall', function () {
Hosts::clear();
Nginx::uninstall();
PhpFpm::uninstall();
// Mailhog::uninstall();
Configuration::uninstall();
Valet::uninstall();

info('Valet has been uninstalled.');
success('Valet has been uninstalled.');
})->descriptions('Uninstall the Valet services');

/**
Expand All @@ -481,11 +499,11 @@
$script = dirname(__FILE__) . '/scripts/update.sh';

if (Valet::onLatestVersion($version)) {
info('You have the latest version of Valet WSL');
success('You have the latest version of Valet WSL');
passthru($script);
} else {
warning('There is a new release of Valet WSL');
warning('Updating now...');
info('There is a new release of Valet WSL');
info('Updating now...');
$latestVersion = Valet::getLatestVersion();
if ($latestVersion) {
passthru($script . " update $latestVersion");
Expand All @@ -501,7 +519,7 @@
$app->command('use [preferedversion] [--update-cli]', function ($preferedversion = null, $updateCli = null) {
info('Changing php-fpm version...');
PhpFpm::changeVersion($preferedversion, $updateCli);
info('php-fpm version successfully changed! 🎉');
success('php-fpm version successfully changed! 🎉');
})->descriptions('Set the PHP-fpm version to use, enter "default" or leave empty to use version: ' . PhpFpm::getVersion(true), [
'--update-cli' => 'Updates CLI version as well',
]);
Expand Down Expand Up @@ -582,7 +600,7 @@
return;
}

info("Database [{$database_name}] reset successfully");
success("Database [{$database_name}] reset successfully");
})->descriptions('Clear all tables for given database in MySQL');

/**
Expand Down Expand Up @@ -622,7 +640,7 @@
info('Exporting database...');
$defaults = $input->getOptions();
$data = Mysql::exportDatabase($database_name, $defaults['sql']);
info("Database [{$data['database']}] exported into file {$data['filename']}");
success("Database [{$data['database']}] exported into file {$data['filename']}");
})->descriptions('Export selected MySQL database');

/**
Expand Down
Loading

0 comments on commit a9e5ef6

Please sign in to comment.