Skip to content

Instantly share code, notes, and snippets.

@Exieros
Last active June 15, 2022 03:54
Show Gist options
  • Select an option

  • Save Exieros/44a4ccf62f82d93494c1b2735c3566e4 to your computer and use it in GitHub Desktop.

Select an option

Save Exieros/44a4ccf62f82d93494c1b2735c3566e4 to your computer and use it in GitHub Desktop.
<?php
$hookSecret = 'your secret';
set_error_handler(function($severity, $message, $file, $line) {
throw new \ErrorException($message, 0, $severity, $file, $line);
});
set_exception_handler(function($e) {
header('HTTP/1.1 500 Internal Server Error');
echo "Ошибка в строке: {$e->getLine()}: " . htmlSpecialChars($e->getMessage());
die();
});
$rawPost = null;
//Проверка соответствия секретной фразы
if ($hookSecret !== null) {
if (!isset($_SERVER['HTTP_X_HUB_SIGNATURE'])) {
throw new \Exception("Отсутствует 'X-Hub-Signature'.");
} elseif (!extension_loaded('hash')) {
throw new \Exception("Для работы скрипта нужно включеное расширение 'hash'.");
}
list($algo, $hash) = explode('=', $_SERVER['HTTP_X_HUB_SIGNATURE'], 2) + array('', '');
if (!in_array($algo, hash_algos(), true)) {
throw new \Exception("Алгоритм хеширования '$algo' не поддерживается.");
}
$rawPost = file_get_contents('php://input');
if (!hash_equals($hash, hash_hmac($algo, $rawPost, $hookSecret))) {
throw new \Exception('Отказано в доступе.');
}
};
//Проверяем наличие необходимых заголовков
if (!isset($_SERVER['CONTENT_TYPE'])) {
throw new \Exception("Отсутствует заголовок 'Content-Type'.");
} elseif (!isset($_SERVER['HTTP_X_GITHUB_EVENT'])) {
throw new \Exception("Отсутствует заголовок 'X-Github-Event'.");
}
//Получаем тело запроса
switch ($_SERVER['CONTENT_TYPE']) {
case 'application/json':
$json = $rawPost ?: file_get_contents('php://input');
break;
case 'application/x-www-form-urlencoded':
$json = $_POST['payload'];
break;
default:
throw new \Exception("Не поддерживается тип контента: $_SERVER[CONTENT_TYPE]");
}
$payload = json_decode($json);
switch (strtolower($_SERVER['HTTP_X_GITHUB_EVENT'])) {
case 'ping':
echo 'pong';
break;
case 'push':
pushHandler($payload->repository->git_url);
echo 'success';
break;
default:
header('HTTP/1.0 404 Not Found');
//echo "Event:$_SERVER[HTTP_X_GITHUB_EVENT] Payload:\n";
//print_r($payload);
die();
}
//Сброк кеша плагина Surge
function resetCache(){
shell_exec("/opt/php/8.0/bin/php ~/wp-cli.phar plugin deactivate surge");
shell_exec("/opt/php/8.0/bin/php ~/wp-cli.phar plugin activate surge");
}
//Обработчик 'push' - событий
function pushHandler($repo){
switch ($repo) {
case 'git://github.com/Exieros/handmade.git':
shell_exec("cd ~/www/maglib.ru/wp-content/themes/handmade/ && git pull git@github.com:Exieros/handmade.git");
resetCache();
break;
case 'git://github.com/Exieros/wp-irbis.git':
shell_exec("cd ~/www/maglib.ru/wp-content/plugins/wp-irbis/ && git pull git@github.com:Exieros/wp-irbis.git");
resetCache();
break;
default:
echo 'unknown repo', PHP_EOL;
die();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment