diff --git a/PHPCI/Application.php b/PHPCI/Application.php index 77bd7170..3f20c3ed 100644 --- a/PHPCI/Application.php +++ b/PHPCI/Application.php @@ -10,6 +10,7 @@ namespace PHPCI; use b8; +use b8\Http\Response; use b8\Http\Response\RedirectResponse; use b8\View; @@ -19,52 +20,38 @@ use b8\View; */ class Application extends b8\Application { + public function init() + { + $request =& $this->request; + $route = '/:controller/:action'; + $opts = ['controller' => 'Home', 'action' => 'index']; + + $this->router->clearRoutes(); + $this->router->register($route, $opts, function (&$route, Response &$response) use (&$request) + { + $skipValidation = in_array($route['controller'], array('session', 'webhook', 'build-status')); + + if (!$skipValidation && !$this->validateSession()) { + if ($request->isAjax()) { + $response->setResponseCode(401); + $response->setContent(''); + } else { + $response = new RedirectResponse($response); + $response->setHeader('Location', PHPCI_URL.'session/login'); + } + + return false; + } + + return true; + }); + } /** * Handle an incoming web request. */ public function handleRequest() { - try { - $this->initRequest(); - - // Validate the user's session unless it is a login/logout action or a web hook: - $sessionAction = ($this->controllerName == 'Session' && in_array($this->action, array('login', 'logout'))); - $externalAction = in_array($this->controllerName, array('Bitbucket', 'Github', 'Gitlab', 'BuildStatus', 'Git')); - $skipValidation = ($externalAction || $sessionAction); - - if ($skipValidation || $this->validateSession()) { - parent::handleRequest(); - } - } catch (\Exception $ex) { - $content = '

There was a problem with this request

-

Please paste the details below into a - new bug report - so that we can investigate and fix it.

'; - - ob_start(); - var_dump(array( - 'message' => $ex->getMessage(), - 'file' => $ex->getFile(), - 'line' => $ex->getLine(), - 'trace' => $ex->getTraceAsString() - )); - var_dump(array( - 'PATH_INFO' => $_SERVER['PATH_INFO'], - 'REDIRECT_PATH_INFO' => $_SERVER['REDIRECT_PATH_INFO'], - 'REQUEST_URI' => $_SERVER['REQUEST_URI'], - 'PHP_SELF' => $_SERVER['PHP_SELF'], - 'SCRIPT_NAME' => $_SERVER['SCRIPT_NAME'], - 'DOCUMENT_ROOT' => $_SERVER['DOCUMENT_ROOT'], - 'SCRIPT_FILENAME' => $_SERVER['SCRIPT_FILENAME'], - 'SERVER_SOFTWARE' => $_SERVER['SERVER_SOFTWARE'], - )); - $content .= ob_get_contents(); - ob_end_clean(); - - $this->response->setContent($content); - $this->response->disableLayout(); - } - + $this->response = parent::handleRequest(); if (View::exists('layout') && $this->response->hasLayout()) { $view = new View('layout'); @@ -91,14 +78,6 @@ class Application extends b8\Application unset($_SESSION['user_id']); } - if ($this->request->isAjax()) { - $this->response->setResponseCode(401); - $this->response->setContent(''); - } else { - $this->response = new RedirectResponse($this->response); - $this->response->setHeader('Location', PHPCI_URL.'session/login'); - } - return false; } } diff --git a/PHPCI/Controller.php b/PHPCI/Controller.php index d95c24e0..442d9248 100644 --- a/PHPCI/Controller.php +++ b/PHPCI/Controller.php @@ -38,7 +38,7 @@ class Controller extends \b8\Controller if (View::exists($this->className)) { $this->controllerView = new View($this->className); } else { - $this->controllerView = new View\UserView('{@content}'); + $this->controllerView = new View\Template('{@content}'); } } diff --git a/PHPCI/Controller/BitbucketController.php b/PHPCI/Controller/BitbucketController.php deleted file mode 100644 index 783421a3..00000000 --- a/PHPCI/Controller/BitbucketController.php +++ /dev/null @@ -1,67 +0,0 @@ - -* @package PHPCI -* @subpackage Web -*/ -class BitbucketController extends \PHPCI\Controller -{ - /** - * @var \PHPCI\Store\BuildStore - */ - protected $buildStore; - - public function init() - { - $this->buildStore = Store\Factory::getStore('Build'); - } - - /** - * Called by Bitbucket POST service. - */ - public function webhook($project) - { - $payload = json_decode($this->getParam('payload'), true); - $branches = array(); - $commits = array(); - - foreach ($payload['commits'] as $commit) { - if (!in_array($commit['branch'], $branches)) { - $branches[] = $commit['branch']; - $commits[$commit['branch']] = $commit['raw_node']; - } - } - - foreach ($branches as $branch) { - try { - - $build = new Build(); - $build->setProjectId($project); - $build->setCommitId($commits[$branch]); - $build->setStatus(Build::STATUS_NEW); - $build->setLog(''); - $build->setCreated(new \DateTime()); - $build->setBranch($branch); - $this->buildStore->save($build); - } catch (\Exception $ex) { - } - } - - die('OK'); - } -} diff --git a/PHPCI/Controller/GitController.php b/PHPCI/Controller/GitController.php deleted file mode 100644 index b5a6cf87..00000000 --- a/PHPCI/Controller/GitController.php +++ /dev/null @@ -1,69 +0,0 @@ - - */ -class GitController extends \PHPCI\Controller -{ - public function init() - { - $this->_buildStore = Store\Factory::getStore('Build'); - } - - /** - * Called by POSTing to /git/webhook/?branch=&commit= - * - * @param string $project - */ - public function webhook($project) - { - $branch = $this->getParam('branch'); - $commit = $this->getParam('commit'); - - try { - $build = new Build(); - $build->setProjectId($project); - - if ($branch !== null && trim($branch) !== '') { - $build->setBranch($branch); - } else { - $build->setBranch('master'); - } - - if ($commit !== null && trim($commit) !== '') { - $build->setCommitId($commit); - } - - $build->setStatus(Build::STATUS_NEW); - $build->setLog(''); - $build->setCreated(new \DateTime()); - } catch (\Exception $ex) { - header('HTTP/1.1 400 Bad Request'); - header('Ex: ' . $ex->getMessage()); - die('FAIL'); - } - - try { - $this->_buildStore->save($build); - } catch (\Exception $ex) { - header('HTTP/1.1 500 Internal Server Error'); - header('Ex: ' . $ex->getMessage()); - die('FAIL'); - } - - die('OK'); - } -} diff --git a/PHPCI/Controller/GithubController.php b/PHPCI/Controller/GithubController.php deleted file mode 100644 index 4fe99756..00000000 --- a/PHPCI/Controller/GithubController.php +++ /dev/null @@ -1,77 +0,0 @@ - -* @package PHPCI -* @subpackage Web -*/ -class GithubController extends \PHPCI\Controller -{ - /** - * @var \PHPCI\Store\BuildStore - */ - protected $buildStore; - - public function init() - { - $this->buildStore = Store\Factory::getStore('Build'); - } - - /** - * Called by Github Webhooks: - */ - public function webhook($project) - { - $payload = json_decode($this->getParam('payload'), true); - - // Github sends a payload when you close a pull request with a - // non-existant commit. We don't want this. - if ($payload['after'] === '0000000000000000000000000000000000000000') { - die('OK'); - } - - try { - $build = new Build(); - $build->setProjectId($project); - $build->setCommitId($payload['after']); - $build->setStatus(Build::STATUS_NEW); - $build->setLog(''); - $build->setCreated(new \DateTime()); - $build->setBranch(str_replace('refs/heads/', '', $payload['ref'])); - - if (!empty($payload['pusher']['email'])) { - $build->setCommitterEmail($payload['pusher']['email']); - } - - } catch (\Exception $ex) { - header('HTTP/1.1 400 Bad Request'); - header('Ex: ' . $ex->getMessage()); - die('FAIL'); - } - - try { - $build = $this->buildStore->save($build); - $build->sendStatusPostback(); - } catch (\Exception $ex) { - header('HTTP/1.1 500 Internal Server Error'); - header('Ex: ' . $ex->getMessage()); - die('FAIL'); - } - - die('OK'); - } -} diff --git a/PHPCI/Controller/GitlabController.php b/PHPCI/Controller/GitlabController.php deleted file mode 100644 index a466f4dd..00000000 --- a/PHPCI/Controller/GitlabController.php +++ /dev/null @@ -1,66 +0,0 @@ -, Dan Cryer -* @package PHPCI -* @subpackage Web -*/ -class GitlabController extends \PHPCI\Controller -{ - /** - * @var \PHPCI\Store\BuildStore - */ - protected $buildStore; - - public function init() - { - $this->buildStore = Store\Factory::getStore('Build'); - } - - /** - * Called by Gitlab Webhooks: - */ - public function webhook($project) - { - $payload = json_decode(file_get_contents("php://input"), true); - - try { - $build = new Build(); - $build->setProjectId($project); - $build->setCommitId($payload['after']); - $build->setStatus(Build::STATUS_NEW); - $build->setLog(''); - $build->setCreated(new \DateTime()); - $build->setBranch(str_replace('refs/heads/', '', $payload['ref'])); - } catch (\Exception $ex) { - header('HTTP/1.1 400 Bad Request'); - header('Ex: ' . $ex->getMessage()); - die('FAIL'); - } - - try { - $build = $this->buildStore->save($build); - $build->sendStatusPostback(); - } catch (\Exception $ex) { - header('HTTP/1.1 500 Internal Server Error'); - header('Ex: ' . $ex->getMessage()); - die('FAIL'); - } - - die('OK'); - } -} diff --git a/PHPCI/Controller/WebhookController.php b/PHPCI/Controller/WebhookController.php new file mode 100644 index 00000000..2e12fc58 --- /dev/null +++ b/PHPCI/Controller/WebhookController.php @@ -0,0 +1,190 @@ + + * @author Sami Tikka + * @author Alex Russell + * @package PHPCI + * @subpackage Web + */ +class WebhookController extends \PHPCI\Controller +{ + /** + * @var \PHPCI\Store\BuildStore + */ + protected $buildStore; + + public function init() + { + $this->buildStore = Store\Factory::getStore('Build'); + } + + /** + * Called by Bitbucket POST service. + */ + public function bitbucket($project) + { + $payload = json_decode($this->getParam('payload'), true); + $branches = array(); + $commits = array(); + + foreach ($payload['commits'] as $commit) { + if (!in_array($commit['branch'], $branches)) { + $branches[] = $commit['branch']; + $commits[$commit['branch']] = $commit['raw_node']; + } + } + + foreach ($branches as $branch) { + try { + + $build = new Build(); + $build->setProjectId($project); + $build->setCommitId($commits[$branch]); + $build->setStatus(Build::STATUS_NEW); + $build->setLog(''); + $build->setCreated(new \DateTime()); + $build->setBranch($branch); + $this->buildStore->save($build); + } catch (\Exception $ex) { + } + } + + die('OK'); + } + + /** + * Called by POSTing to /git/webhook/?branch=&commit= + * + * @param string $project + */ + public function git($project) + { + $branch = $this->getParam('branch'); + $commit = $this->getParam('commit'); + + try { + $build = new Build(); + $build->setProjectId($project); + + if ($branch !== null && trim($branch) !== '') { + $build->setBranch($branch); + } else { + $build->setBranch('master'); + } + + if ($commit !== null && trim($commit) !== '') { + $build->setCommitId($commit); + } + + $build->setStatus(Build::STATUS_NEW); + $build->setLog(''); + $build->setCreated(new \DateTime()); + } catch (\Exception $ex) { + header('HTTP/1.1 400 Bad Request'); + header('Ex: ' . $ex->getMessage()); + die('FAIL'); + } + + try { + $this->_buildStore->save($build); + } catch (\Exception $ex) { + header('HTTP/1.1 500 Internal Server Error'); + header('Ex: ' . $ex->getMessage()); + die('FAIL'); + } + + die('OK'); + } + + /** + * Called by Github Webhooks: + */ + public function github($project) + { + $payload = json_decode($this->getParam('payload'), true); + + // Github sends a payload when you close a pull request with a + // non-existant commit. We don't want this. + if ($payload['after'] === '0000000000000000000000000000000000000000') { + die('OK'); + } + + try { + $build = new Build(); + $build->setProjectId($project); + $build->setCommitId($payload['after']); + $build->setStatus(Build::STATUS_NEW); + $build->setLog(''); + $build->setCreated(new \DateTime()); + $build->setBranch(str_replace('refs/heads/', '', $payload['ref'])); + + if (!empty($payload['pusher']['email'])) { + $build->setCommitterEmail($payload['pusher']['email']); + } + + } catch (\Exception $ex) { + header('HTTP/1.1 400 Bad Request'); + header('Ex: ' . $ex->getMessage()); + die('FAIL'); + } + + try { + $build = $this->buildStore->save($build); + $build->sendStatusPostback(); + } catch (\Exception $ex) { + header('HTTP/1.1 500 Internal Server Error'); + header('Ex: ' . $ex->getMessage()); + die('FAIL'); + } + + die('OK'); + } + + /** + * Called by Gitlab Webhooks: + */ + public function gitlab($project) + { + $payload = json_decode(file_get_contents("php://input"), true); + + try { + $build = new Build(); + $build->setProjectId($project); + $build->setCommitId($payload['after']); + $build->setStatus(Build::STATUS_NEW); + $build->setLog(''); + $build->setCreated(new \DateTime()); + $build->setBranch(str_replace('refs/heads/', '', $payload['ref'])); + } catch (\Exception $ex) { + header('HTTP/1.1 400 Bad Request'); + header('Ex: ' . $ex->getMessage()); + die('FAIL'); + } + + try { + $build = $this->buildStore->save($build); + $build->sendStatusPostback(); + } catch (\Exception $ex) { + header('HTTP/1.1 500 Internal Server Error'); + header('Ex: ' . $ex->getMessage()); + die('FAIL'); + } + + die('OK'); + } +} diff --git a/PHPCI/View/Project/view.phtml b/PHPCI/View/Project/view.phtml index b5957260..1512071e 100644 --- a/PHPCI/View/Project/view.phtml +++ b/PHPCI/View/Project/view.phtml @@ -27,17 +27,17 @@ switch($project->getType()) { case 'github': - $url = (empty($_SERVER['HTTPS']) ? 'http' : 'https') . '://' . $_SERVER['HTTP_HOST'] . '/github/webhook/' . $project->getId(); + $url = PHPCI_URL . 'webhook/github/' . $project->getId(); print ' as a "WebHook URL" in the Service Hooks section of your Github repository.

' . $url . ''; break; case 'gitlab': - $url = (empty($_SERVER['HTTPS']) ? 'http' : 'https') . '://' . $_SERVER['HTTP_HOST'] . '/gitlab/webhook/' . $project->getId(); + $url = PHPCI_URL. 'webhook/gitlab/' . $project->getId(); print ' as a "WebHook URL" in the Web Hooks section of your Gitlab repository.

' . $url . ''; break; case 'bitbucket': - $url = (empty($_SERVER['HTTPS']) ? 'http' : 'https') . '://' . $_SERVER['HTTP_HOST'] . '/bitbucket/webhook/' . $project->getId(); + $url = PHPCI_URL . 'webhook/bitbucket/' . $project->getId(); print ' as a "POST" service in the Services section of your Bitbucket repository.

' . $url . ''; break; } diff --git a/composer.lock b/composer.lock index ce6ac387..f2c730ae 100644 --- a/composer.lock +++ b/composer.lock @@ -7,16 +7,16 @@ "packages": [ { "name": "block8/b8framework", - "version": "1.0.1", + "version": "1.1.0", "source": { "type": "git", "url": "https://github.com/Block8/b8framework.git", - "reference": "0497ae34ba7ef828db23b35a1f75d4debfa7eed5" + "reference": "f643e0d3497599016cb62611ceb9288710423121" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/Block8/b8framework/zipball/0497ae34ba7ef828db23b35a1f75d4debfa7eed5", - "reference": "0497ae34ba7ef828db23b35a1f75d4debfa7eed5", + "url": "https://api.github.com/repos/Block8/b8framework/zipball/f643e0d3497599016cb62611ceb9288710423121", + "reference": "f643e0d3497599016cb62611ceb9288710423121", "shasum": "" }, "require": { @@ -50,7 +50,7 @@ "mvc", "php" ], - "time": "2013-10-09 14:06:12" + "time": "2014-02-24 11:25:23" }, { "name": "ircmaxell/password-compat", @@ -155,16 +155,16 @@ }, { "name": "pimple/pimple", - "version": "v1.1.0", + "version": "v1.1.1", "source": { "type": "git", "url": "https://github.com/fabpot/Pimple.git", - "reference": "471c7d7c52ad6594e17b8ec33efdd1be592b5d83" + "reference": "2019c145fe393923f3441b23f29bbdfaa5c58c4d" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/fabpot/Pimple/zipball/471c7d7c52ad6594e17b8ec33efdd1be592b5d83", - "reference": "471c7d7c52ad6594e17b8ec33efdd1be592b5d83", + "url": "https://api.github.com/repos/fabpot/Pimple/zipball/2019c145fe393923f3441b23f29bbdfaa5c58c4d", + "reference": "2019c145fe393923f3441b23f29bbdfaa5c58c4d", "shasum": "" }, "require": { @@ -188,7 +188,9 @@ "authors": [ { "name": "Fabien Potencier", - "email": "fabien@symfony.com" + "email": "fabien@symfony.com", + "homepage": "http://fabien.potencier.org", + "role": "Lead Developer" } ], "description": "Pimple is a simple Dependency Injection Container for PHP 5.3", @@ -197,7 +199,7 @@ "container", "dependency injection" ], - "time": "2013-09-19 04:53:08" + "time": "2013-11-22 08:30:29" }, { "name": "psr/log", @@ -288,17 +290,17 @@ }, { "name": "symfony/console", - "version": "v2.4.0", + "version": "v2.4.2", "target-dir": "Symfony/Component/Console", "source": { "type": "git", "url": "https://github.com/symfony/Console.git", - "reference": "3c1496ae96d24ccc6c340fcc25f71d7a1ab4c12c" + "reference": "940f217cbc3c8a33e5403e7c595495c4884400fe" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/symfony/Console/zipball/3c1496ae96d24ccc6c340fcc25f71d7a1ab4c12c", - "reference": "3c1496ae96d24ccc6c340fcc25f71d7a1ab4c12c", + "url": "https://api.github.com/repos/symfony/Console/zipball/940f217cbc3c8a33e5403e7c595495c4884400fe", + "reference": "940f217cbc3c8a33e5403e7c595495c4884400fe", "shasum": "" }, "require": { @@ -328,7 +330,9 @@ "authors": [ { "name": "Fabien Potencier", - "email": "fabien@symfony.com" + "email": "fabien@symfony.com", + "homepage": "http://fabien.potencier.org", + "role": "Lead Developer" }, { "name": "Symfony Community", @@ -337,21 +341,21 @@ ], "description": "Symfony Console Component", "homepage": "http://symfony.com", - "time": "2013-11-27 09:10:40" + "time": "2014-02-11 13:52:09" }, { "name": "symfony/yaml", - "version": "v2.4.0", + "version": "v2.4.2", "target-dir": "Symfony/Component/Yaml", "source": { "type": "git", "url": "https://github.com/symfony/Yaml.git", - "reference": "1ae235a1b9d3ad3d9f3860ff20acc072df95b7f5" + "reference": "bb6ddaf8956139d1b8c360b4b713ed0138e876b3" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/symfony/Yaml/zipball/1ae235a1b9d3ad3d9f3860ff20acc072df95b7f5", - "reference": "1ae235a1b9d3ad3d9f3860ff20acc072df95b7f5", + "url": "https://api.github.com/repos/symfony/Yaml/zipball/bb6ddaf8956139d1b8c360b4b713ed0138e876b3", + "reference": "bb6ddaf8956139d1b8c360b4b713ed0138e876b3", "shasum": "" }, "require": { @@ -375,7 +379,9 @@ "authors": [ { "name": "Fabien Potencier", - "email": "fabien@symfony.com" + "email": "fabien@symfony.com", + "homepage": "http://fabien.potencier.org", + "role": "Lead Developer" }, { "name": "Symfony Community", @@ -384,22 +390,22 @@ ], "description": "Symfony Yaml Component", "homepage": "http://symfony.com", - "time": "2013-11-26 16:40:27" + "time": "2014-01-07 13:28:54" } ], "packages-dev": [ { "name": "phpspec/prophecy", - "version": "v1.0.4", + "version": "1.1.2", "source": { "type": "git", "url": "https://github.com/phpspec/prophecy.git", - "reference": "79d9c8bd94801bffbf9b56964f6438762da6d8cd" + "reference": "976a65af02a2a0e17ce6c949f7b43437205628bb" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/phpspec/prophecy/zipball/79d9c8bd94801bffbf9b56964f6438762da6d8cd", - "reference": "79d9c8bd94801bffbf9b56964f6438762da6d8cd", + "url": "https://api.github.com/repos/phpspec/prophecy/zipball/976a65af02a2a0e17ce6c949f7b43437205628bb", + "reference": "976a65af02a2a0e17ce6c949f7b43437205628bb", "shasum": "" }, "require-dev": { @@ -408,7 +414,7 @@ "type": "library", "extra": { "branch-alias": { - "dev-master": "1.0.x-dev" + "dev-master": "1.1.x-dev" } }, "autoload": { @@ -441,7 +447,7 @@ "spy", "stub" ], - "time": "2013-08-10 11:11:45" + "time": "2014-01-24 11:03:43" }, { "name": "phpspec/prophecy-phpunit", diff --git a/console b/console old mode 100644 new mode 100755