<?php
	



function elog($string){
    file_put_contents("git_hooks.log", date("Y-m-d H:i:s")." - ".$string.PHP_EOL, FILE_APPEND);
}

$debug_mode = true;

$gitlab_token = $_SERVER["HTTP_X_GITLAB_TOKEN"];
if ($gitlab_token != "tae5yeleLad7bie0Laim2"){
  die("Bad GitLab Token");
}
$gitlab_event = $_SERVER["HTTP_X_GITLAB_EVENT"];
$gitlab_data = json_decode(file_get_contents("php://input"), true);





switch ($gitlab_data["event_name"]){
    case "project_create":
        elog("Creating project: ".print_r($gitlab_data,true));
//        $gitlab_data["name"] = 'dev';
		$execstring =  'sudo /var/www/html/githook/hooks/projectCreate.sh "'.$gitlab_data["path_with_namespace"].'" "'.$gitlab_data["path"].'" 2>&1';
        elog("Running command: $execstring");
        $output = [];
        elog(exec($execstring, $output));
		$gitlab_data['output'] = $output;
    break;

    case "project_destroy":
	    elog("Delete project: ".$gitlab_data["name"]);
		$execstring =  'sudo /var/www/html/githook/hooks/projectDelete.sh "'.$gitlab_data["path"].'" 2>&1';
        elog("Running command: $execstring");
        $output = [];
        elog(exec($execstring, $output));
		$gitlab_data['output'] = $output;
    break;

    case "user_create":
        elog("Creating user: ".$gitlab_data["name"]);
		$execstring =  'sudo /var/www/html/githook/hooks/userCreate.sh "'.$gitlab_data["username"].'" "'.$gitlab_data["name"].'" "'.$gitlab_data["email"].'" 2>&1';
        elog("Running command: $execstring");
        $output = [];
        elog(exec($execstring, $output));
		$gitlab_data['output'] = $output;
    break;


    case "user_destroy":
  		elog("Delete user: ".$gitlab_data["name"]);
		$execstring =  'sudo /var/www/html/githook/hooks/userDelete.sh "'.$gitlab_data["username"].'" 2>&1';
        elog("Running command: $execstring");
        $output = [];
        elog(exec($execstring, $output));
		$gitlab_data['output'] = $output;
    break;

    case "key_create":
        elog("Add key to user: ".$gitlab_data["name"]);
		$execstring =  'sudo /var/www/html/githook/hooks/keyCreate.sh "'.$gitlab_data["username"].'" "'.$gitlab_data["key"].'" 2>&1';
        elog("Running command: $execstring");
        $output = [];
        elog(exec($execstring, $output));
		$gitlab_data['output'] = $output;
    break;

    case "key_destroy":
	    elog("Delete key from user: ".$gitlab_data["name"]);
		$execstring =  'sudo /var/www/html/githook/hooks/keyDelete.sh "'.$gitlab_data["username"].'" "'.$gitlab_data["key"].'" 2>&1';
        elog("Running command: $execstring");
        $output = [];
        elog(exec($execstring, $output));
		$gitlab_data['output'] = $output;
        break;
    case "push":
        elog("Push to repo: ".$gitlab_data["project"]["name"]);
        $execstring =  'sudo /var/www/html/githook/hooks/push.sh "'.$gitlab_data["user_username"].'" "'.$gitlab_data["project"]["name"].'" 2>&1';
        elog("Running command: $execstring");
		$output = [];
        elog(exec($execstring, $output));
		$gitlab_data['output'] = $output;
        break;
    default:
		elog("Unknown event: ".$gitlab_data["event_name"]);
		$debug_mode = true;
    break;
}

if ($debug_mode){
  $filename = 'logs/'.$gitlab_data["event_name"]."_".time();
  elog("Writing debug to ".$filename);
  file_put_contents($filename, json_encode($gitlab_data, JSON_PRETTY_PRINT));
  
  elog("Writing debug to ".$filename);
  file_put_contents("git_hooks_flow.log", json_encode($gitlab_data, JSON_PRETTY_PRINT).PHP_EOL, FILE_APPEND);
}
?>
