genpdf.php 4.06 KB
<?php

require_once "lib/3rdparty/php-redmine-api/lib/Redmine/Api/AbstractApi.php";
require_once "lib/3rdparty/php-redmine-api/lib/Redmine/Api/SimpleXMLElement.php";
require_once "lib/3rdparty/php-redmine-api/lib/Redmine/Api/Issue.php";
require_once "lib/3rdparty/php-redmine-api/lib/Redmine/Api/Attachment.php";
require_once "lib/3rdparty/php-redmine-api/lib/Redmine/Client.php";

require_once "config.php";

function _tempdir_cleanup($dir) {
#	system('rm -rf "'.$dir.'"');
}

function tempdir($dir=FALSE, $prefix='php') {
	$tempfile = tempnam(sys_get_temp_dir(),'');

	if (file_exists($tempfile)) 
		unlink($tempfile);

	mkdir($tempfile);
	if (is_dir($tempfile)) {
		register_shutdown_function("_tempdir_cleanup", $tempfile);
		return $tempfile;
	}

	return FALSE;
}

function body_parse($body) {
	$_GET_withbraces = array();

	foreach ($_GET as $key => $value)
		$_GET_withbraces['[{'.$key.'}]'] = $value;

	switch ($_GET['file']) {
		case 'request/vpn':
			if (isset($_GET['vlan']))
				$_GET_withbraces['[{additional-access}]'] = 'Также прошу предоставить доступ к VLAN '. $_GET['vlan']. ' через данную учётную запись.';
			if (!isset($_GET_withbraces['[{is-additional-login}]']));
				$_GET_withbraces['[{is-additional-login}]'] = '';
			break;
	}

	$body = str_replace(array_keys($_GET_withbraces), array_values($_GET_withbraces), $body);

	return $body;
}

switch ($_GET['file']) {
	case 'request/vpn':
		$project_id   = 1;
		$memo_subject = 'Запрос на учётную запись к vpn.mephi.ru';
		break;
	case 'request/vpn-private-net':
		$project_id   = 1;
		$memo_subject = 'Запрос на доступ к VLAN через vpn.mephi.ru';
		break;
	case 'request/nix-hosting':
		$project_id   = 1;
		$memo_subject = 'Заявка на предоставление nix-хостинга';
		break;
	case 'request/win-hosting':
		$project_id   = 1;
		$memo_subject = 'Заявка на предоставление win-хостинга';
		break;
	case 'request/hpc':
		$project_id   = NULL; //38;
		$memo_subject = 'Запрос на учётную запись к vpn.mephi.ru';
		break;
	default:
		die('unknown memo-type');
}

$tempdir = tempdir();

if ($tempdir === FALSE)
	die('Cannot create temporary directory');


exec('cp -a /var/www/ut.mephi.ru/ut-tex/mephimemo "'.$tempdir.'/"');
chdir($tempdir.'/mephimemo');

ob_start();
print_r($_GET);
$_GET_print_r = ob_get_clean();

$redmine = new Redmine\Client('https://redmine.ut.mephi.ru', REDMINE_LOGIN, REDMINE_PASSWORD);

if (!is_null($project_id)) {
	$issue = $redmine->api('issue')->create(
		array(
			'project_id'  => $project_id,
			'subject'     => $memo_subject,
			'description' => $_GET_print_r,
		)
	);

	$issue_id = $issue->id;
} else {
	$issue_id = $_GET['issue-id'];
}

if (!isset($_GET['date-end']))
	$_GET['date-end'] = (date('Y')+1).'-12-31';

foreach (array('fullname', 'phonenumber', 'email') as $key)
	if (!isset($_GET['contact-'.$key]))
		$_GET['contact-'.$key] = $_GET['user-'.$key];

foreach ($_GET as $key => &$value)
	$value = str_replace('_', '\_', $value);

$body_template = file_get_contents('template/'.$_GET['file'].'.tex');
$body          = body_parse($body_template);

file_put_contents('header.tex',		'\mmheader{Начальнику управления информатизации}{Н. Н. Романову}');
file_put_contents('body.tex',		$body);
file_put_contents('footer.tex',		'\mmfooter{'.$_GET['signer-appointment'].'}{'.$_GET{'signer-fullname'}.'}');
file_put_contents('responsible.tex',	'\mmresponsible{'.$_GET['contact-fullname'].'}{'.$_GET{'contact-phonenumber'}.'}{'.$_GET['contact-email'].'}');
file_put_contents('urlqr.tex',		'\mmredmineurlqr{'.$issue_id.'}');

exec('make');
$pdf_content = file_get_contents('memo.pdf');

$pdf_upload = json_decode( $redmine->api('attachment')->upload($pdf_content) );

$redmine->api('issue')->attach(
	$issue_id,
	array(
		'token'       => $pdf_upload->upload->token,
		'filename'    => 'memorandum.pdf',
		'description' => 'Memorandum',
		'content_type'=> 'application/pdf'
	)
);

header('Content-type: application/pdf');
print $pdf_content;

?>