Skip to content

Instantly share code, notes, and snippets.

@Mte90
Created April 16, 2021 13:36
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Mte90/1b9ae442dd9684e988dacf175be1c79b to your computer and use it in GitHub Desktop.
Save Mte90/1b9ae442dd9684e988dacf175be1c79b to your computer and use it in GitHub Desktop.
Webhook deployer for static websites from GitHub/GitLab
<?php
// This script require to be added in the Webhook section of you repo
// It will download the repository defined in the url in the path defined in the url
// For safety reasons is required a token in the url and will check the repo owner
// Note GitHub/GitLab generate zip files with a folder on it so it isn't enough to decomrpess them
// You need to get that content and a temporary folder is required to mvoe those files
ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);
$TOKEN = 'yourtokenastrangestringtouseingetrequests';
function rrmdir($src) {
$dir = opendir($src);
while(false !== ( $file = readdir($dir)) ) {
if (( $file != '.' ) && ( $file != '..' )) {
$full = $src . '/' . $file;
if ( is_dir($full) ) {
rrmdir($full);
}
else {
unlink($full);
}
}
}
closedir($dir);
rmdir($src);
}
function rcopy($src, $dst) {
if (file_exists ( $dst ))
rrmdir( $dst );
if (is_dir ( $src )) {
mkdir( $dst );
$files = scandir ( $src );
foreach ( $files as $file ) {
if ($file !== "." && $file !== "..") {
rcopy( "$src/$file", "$dst/$file" );
}
}
} else if (file_exists ( $src )) {
copy( $src, $dst );
}
}
function downloadUnzipGetContents( $url, $where ) {
$f = file_put_contents("./temp.zip", fopen($url, 'r'), LOCK_EX);
echo "Downloading in progress\n";
if(FALSE === $f)
die("Couldn't write to file.");
$zip = new ZipArchive;
$res = $zip->open('temp.zip');
if ($res === TRUE) {
$zip->extractTo('./temp');
$zip->close();
}
$files = array_diff(scandir('./temp'), array('.', '..'));
$dir = './temp/' . $files[2];
unlink('temp.zip');
echo "Creating the folder and moving the files\n";
# create folder, cd inside the unique subfolder from the zip and move all the files in the parent
rcopy($dir, $where);
rrmdir('./temp');
}
if ( isset( $_GET['repo'], $_GET['where'], $_GET[ 'token' ] ) && !empty( $_GET['repo'] ) && !empty( $_GET['where'] ) && $_GET[ 'token' ] === $TOKEN ) {
$repo = $_GET['repo'];
// This is just for safety in case someone found the token and want to deploy something but will check the repo owner by the url
if ( substr( $repo, 0, 6 ) === 'your-organization/' ) {
echo "Request blocked";
die();
}
$repo = str_replace( '/', '%2F', $repo );
$where = $_GET['where'];
// For gitlab
$url = 'https://gitlab.com/api/v4/projects/' . $repo . '/repository/archive.zip';
// For github
$url = 'https://github.com/' . $repo . '/archive/refs/heads/master.zip';
downloadUnzipGetContents( $url, $where );
} else {
echo "What are you trying to do?";
die();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment