From 1e44a1531b9e3ae19c1a805499732555cf6dce68 Mon Sep 17 00:00:00 2001 From: Dan Cryer Date: Fri, 2 May 2014 17:32:18 +0100 Subject: [PATCH] Simplifying ProjectController a little, see #384 --- PHPCI/Controller/ProjectController.php | 84 ++++---------------------- PHPCI/Helper/Github.php | 57 +++++++++++++++++ PHPCI/Helper/SshKey.php | 46 ++++++++++++++ 3 files changed, 115 insertions(+), 72 deletions(-) create mode 100644 PHPCI/Helper/Github.php create mode 100644 PHPCI/Helper/SshKey.php diff --git a/PHPCI/Controller/ProjectController.php b/PHPCI/Controller/ProjectController.php index c4fc5a58..ebf3df1a 100644 --- a/PHPCI/Controller/ProjectController.php +++ b/PHPCI/Controller/ProjectController.php @@ -10,6 +10,8 @@ namespace PHPCI\Controller; use PHPCI\BuildFactory; +use PHPCI\Helper\Github; +use PHPCI\Helper\SshKey; use PHPCI\Model\Build; use PHPCI\Model\Project; use b8; @@ -145,37 +147,19 @@ class ProjectController extends \PHPCI\Controller $method = $this->request->getMethod(); + $pub = null; + $values = array(); + if ($method == 'POST') { $values = $this->getParams(); $pub = null; } else { - $tempPath = sys_get_temp_dir() . '/'; + $sshKey = new SshKey(); + $key = $sshKey->generate(); - // FastCGI fix for Windows machines, where temp path is not available to - // PHP, and defaults to the unwritable system directory. If the temp - // path is pointing to the system directory, shift to the 'TEMP' - // sub-folder, which should also exist, but actually be writable. - if ($tempPath == getenv("SystemRoot") . '/') { - $tempPath = getenv("SystemRoot") . '/TEMP/'; - } - - $keyFile = $tempPath . md5(microtime(true)); - - if (!is_dir($tempPath)) { - mkdir($tempPath); - } - - if ($this->canGenerateKeys()) { - shell_exec('ssh-keygen -q -t rsa -b 2048 -f '.$keyFile.' -N "" -C "deploy@phpci"'); - - $pub = file_get_contents($keyFile . '.pub'); - $prv = file_get_contents($keyFile); - - $values = array('key' => $prv, 'pubkey' => $pub); - } else { - $pub = null; - $values = array(); - } + $values['key'] = $key['private_key']; + $values['pubkey'] = $key['public_key']; + $pub = $key['public_key']; } $form = $this->projectForm($values); @@ -381,46 +365,8 @@ class ProjectController extends \PHPCI\Controller */ protected function githubRepositories() { - $token = Config::getInstance()->get('phpci.github.token'); - - if (!$token) { - die(json_encode(null)); - } - - $cache = \b8\Cache::getCache(\b8\Cache::TYPE_APC); - $rtn = $cache->get('phpci_github_repos'); - - if (!$rtn) { - $orgs = $this->doGithubApiRequest('/user/orgs', array('access_token' => $token)); - - $params = array('type' => 'all', 'access_token' => $token); - $repos = array(); - $repos['user'] = $this->doGithubApiRequest('/user/repos', $params); - - - foreach ($orgs as $org) { - $repos[$org['login']] = $this->doGithubApiRequest('/orgs/'.$org['login'].'/repos', $params); - } - - $rtn = array(); - foreach ($repos as $repoGroup) { - foreach ($repoGroup as $repo) { - $rtn['repos'][] = $repo['full_name']; - } - } - - $cache->set('phpci_github_repos', $rtn); - } - - die(json_encode($rtn)); - } - - protected function doGithubApiRequest($url, $params) - { - $http = new \b8\HttpClient('https://api.github.com'); - $res = $http->get($url, $params); - - return $res['body']; + $github = new Github(); + die(json_encode($github->getRepositories())); } protected function getReferenceValidator($values) @@ -460,10 +406,4 @@ class ProjectController extends \PHPCI\Controller return true; }; } - - protected function canGenerateKeys() - { - $result = @shell_exec('ssh-keygen'); - return !empty($result); - } } diff --git a/PHPCI/Helper/Github.php b/PHPCI/Helper/Github.php new file mode 100644 index 00000000..4278dd8e --- /dev/null +++ b/PHPCI/Helper/Github.php @@ -0,0 +1,57 @@ +get($url, $params); + + return $res['body']; + } + + /** + * Get an array of repositories from Github's API. + */ + public function getRepositories() + { + $token = Config::getInstance()->get('phpci.github.token'); + + if (!$token) { + die(json_encode(null)); + } + + $cache = Cache::getCache(Cache::TYPE_APC); + $rtn = $cache->get('phpci_github_repos'); + + if (!$rtn) { + $orgs = $this->makeRequest('/user/orgs', array('access_token' => $token)); + + $params = array('type' => 'all', 'access_token' => $token); + $repos = array(); + $repos['user'] = $this->makeRequest('/user/repos', $params); + + + foreach ($orgs as $org) { + $repos[$org['login']] = $this->makeRequest('/orgs/'.$org['login'].'/repos', $params); + } + + $rtn = array(); + foreach ($repos as $repoGroup) { + foreach ($repoGroup as $repo) { + $rtn['repos'][] = $repo['full_name']; + } + } + + $cache->set('phpci_github_repos', $rtn); + } + + return $rtn; + } +} \ No newline at end of file diff --git a/PHPCI/Helper/SshKey.php b/PHPCI/Helper/SshKey.php new file mode 100644 index 00000000..49723188 --- /dev/null +++ b/PHPCI/Helper/SshKey.php @@ -0,0 +1,46 @@ +canGenerateKeys()) { + shell_exec('ssh-keygen -q -t rsa -b 2048 -f '.$keyFile.' -N "" -C "deploy@phpci"'); + + $pub = file_get_contents($keyFile . '.pub'); + $prv = file_get_contents($keyFile); + + $return = array('private_key' => $prv, 'public_key' => $pub); + } + + return $return; + } + + public function canGenerateKeys() + { + $keygen = @shell_exec('ssh-keygen'); + $canGenerateKeys = !empty($keygen); + + return $canGenerateKeys; + } +} \ No newline at end of file