attachment-server/phplib/libattachmentmaker.php

89 lines
2.9 KiB
PHP

<?php
require_once(__DIR__."/libfilelist2zip.php");
function make_attachment($thash) {
global $proj;
$pf=realpath(__DIR__."/../".$proj["path"]."/automake_attachment.php");
if(!is_readable($pf)) {
gen_error_500("challange ".$proj["name"]." has been found, but the entry automake_attachment.php does not exist.");
exit;
}
require($pf);
if(!function_exists("make_attachment_impl")) {
gen_error_500("challange ".$proj["name"]." has been found, but the entry automake_attachment.php doesn't implement the function make_attachment_impl(\$team_hash).");
exit;
}
$font_path=getenv("GDFONTPATH");
if($font_path===false) {
$font_path="";
} else {
$font_path.=";";
}
//init GDFONTPATH to generate images containing text
//$font_path.=realpath(__DIR__).";";
//$font_path.=realpath(__DIR__."/../").";";
$font_path.=realpath(__DIR__."/../assets/");//.";";
//$font_path.=dirname($pf).";";
putenv("GDFONTPATH=".$font_path);
//echo getenv("GDFONTPATH");
$GLOBALS["c_random_int_seed"]=hash("sha512", hash("md5", hash("sha256", $thash, true)), true);
$GLOBALS["c_random_int"]=function($min, $max) {
if(!is_int($min) || !is_int($max)) {
gen_error_500("\$c_random_int(): invalid arguments: \$min or \$max is not integer");
exit;
}
if($min==$max) {
return $min;
}
if($min>$max) {
list($min, $max)=[$max, $min];
}
$delta=$max-$min;
if($delta<0||$delta>PHP_INT_MAX) {
gen_error_500("\$c_random_int(): \$delta is not between 0 and PHP_INT_MAX(".(string)PHP_INT_MAX.")");
exit;
}
$t=$delta;
$res=0;
for($a=0; $t>0; $a++) {
$res<<=8;
$res|=unpack("C", $GLOBALS["c_random_int_seed"][$a&63])[1]; //&63 is equal to %64
$t>>=8;
}
unset($a);
$res&=$delta; //avoid mod(%) operator, as it makes the result not evenly distributed
$GLOBALS["c_random_int_seed"]=hash("sha512", $GLOBALS["c_random_int_seed"], true);
return $min+$res;
};
//the function make_attachment_impl($team_hash) should return an array [(string)filename, (string)file_content] on success, or boolean false if it failed to generate.
$fn=make_attachment_impl($thash);
if($fn===false||!is_array($fn)||count($fn)!=2||gettype($fn[0])!="string"||gettype($fn[1])!="string") {
$out="failed to make attachment for challange ".$proj["name"].": function call to make_attachment_impl(\$team_hash) failed or the returned value is invalid.";
if(gettype($fn)=="string") {
$out.="\nThe generator returned the following error:\n";
$out.=$fn;
}
gen_error_500($out);
exit;
}
if(strlen($fn[0])<=0) {
$fn[0]="attachment.bin";
}
return $fn;
}