php-censor/src/Helper/SshKey.php
Dmitry Khomutov 069026bc2d
Added ability to merge in-database project config over in-repository
config instead of only overwrite. This commit solve issues: #14, #70,
#106, #121.
2018-04-15 15:58:23 +07:00

59 lines
1.3 KiB
PHP

<?php
namespace PHPCensor\Helper;
use PHPCensor\Config;
/**
* Helper class for dealing with SSH keys.
*/
class SshKey
{
/**
* Uses ssh-keygen to generate a public/private key pair.
*
* @return array
*/
public function generate()
{
$tempPath = sys_get_temp_dir() . '/';
$keyFile = $tempPath . md5(microtime(true));
if (!is_dir($tempPath)) {
mkdir($tempPath);
}
$return = [
'ssh_private_key' => '',
'ssh_public_key' => ''
];
$sshStrength = Config::getInstance()->get('php-censor.ssh.strength', 2048);
$sshComment = Config::getInstance()->get('php-censor.ssh.comment', 'admin@php-censor');
$output = @shell_exec(
sprintf(
'ssh-keygen -t rsa -b %s -f %s -N "" -C "%s"',
$sshStrength,
$keyFile,
$sshComment
)
);
if (!empty($output)) {
$pub = file_get_contents($keyFile . '.pub');
$prv = file_get_contents($keyFile);
if (!empty($pub)) {
$return['ssh_public_key'] = $pub;
}
if (!empty($prv)) {
$return['ssh_private_key'] = $prv;
}
}
return $return;
}
}