Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- /**
- * Class GhWebHook
- */
- class GhWebHook
- {
- /**
- * Hook key
- * @var string
- */
- protected $key = '';
- /**
- * @param $key
- */
- public function __construct($key)
- {
- $this->key = (string)$key;
- }
- /**
- * Run commands
- * @param $commands
- */
- public function run($commands)
- {
- if (!$this->validateUserAgent()) {
- static::response('error', 'User agent validation fail');
- }
- if (!$this->validateSig()) {
- static::response('error', 'Sig validation fail');
- }
- system(implode('&&', $commands));
- static::response('success', 'Deploy success');
- }
- /**
- * Response
- * @param $status
- * @param $message
- */
- public static function response($status, $message)
- {
- header('Content-Type: application/json');
- die(json_encode([$status => $message]));
- }
- public static function init($configs)
- {
- if (!file_exists($configs)) {
- static::response('error', 'Can not find configuration file');
- }
- $config = require($configs);
- if (!isset($config['deploykey'])) {
- static::response('error', 'Deploy key not exists');
- }
- return $config['deploykey'];
- }
- /**
- * User Agent must be a GitHub-Hookshot/****
- * @return bool
- */
- protected function validateUserAgent()
- {
- return isset($_SERVER['HTTP_USER_AGENT']) &&
- preg_match('/GitHub\-Hookshot\/[0-9]+/is', $_SERVER['HTTP_USER_AGENT']);
- }
- /**
- * Signature must be sha1 of raw content and key
- * @return bool
- */
- protected function validateSig()
- {
- $input = file_get_contents('php://input');
- return isset($_SERVER['HTTP_X_HUB_SIGNATURE']) &&
- ('sha1=' . hash_hmac('sha1', $input, $this->key)) === $_SERVER['HTTP_X_HUB_SIGNATURE'];
- }
- }
- $key = GhWebHook::init(__DIR__ . '/../app/config/app.php');
- (new GhWebHook($key))
- ->run([
- 'cd ../',
- 'git checkout -f',
- 'composer update',
- ]);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement