genpdf.php 12.5 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/User.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";

/*

$user_redmine

Array
(
    [user] => Array
        (
            [id] => 17
            [login] => AAAnikeyev
            [firstname] => Артем
            [lastname] => Аникеев
            [mail] => AAAnikeyev@mephi.ru
            [created_on] => 2014-03-05T06:25:14Z
            [last_login_on] => 2014-07-17T11:10:01Z
            [api_key] => xxxxxxxxxxxxxxxxxxxxxxxxxxxxx
            [status] => 1
            [custom_fields] => Array
                (
                    [0] => Array
                        (
                            [id] => 19
                            [name] => Отчество
                            [value] => Андреевич
                        )

                )

            [groups] => Array
                (
                    [0] => Array
                        (
                            [id] => 7
                            [name] => отдел юникс-технологий
                        )

                    [1] => Array
                        (
                            [id] => 55
                            [name] => группа HPC
                        )

                )

            [memberships] => Array
                (
                    [0] => Array
                        (
                            [id] => 777
                            [project] => Array
                                (
                                    [id] => 102
                                    [name] => *nix hosting request
                                )

                            [roles] => Array
                                (
                                    [0] => Array
                                        (
                                            [id] => 4
                                            [name] => Разработчик
                                            [inherited] => 1
                                        )

                                )

                        )
                )

        )

)
*/

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;
}

$custom_fields = NULL;

$custom_fields_keys = array();
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   = 102;
		$memo_subject = 'Заявка на предоставление nix-хостинга';
		break;
	case 'request/win-hosting':
		$project_id   = 1;
		$memo_subject = 'Заявка на предоставление win-хостинга';
		break;
	case 'request/voip-softphone':
		$project_id   = 18;
		$memo_subject = 'Заявка на создание учётной записи к softphone';
		break;
	case 'request/voip3':
		$project_id   = 124;
		$memo_subject = 'Заявка на доступ к систему «Биллинг» IP-телефонии';
		break;
	case 'request/cps':
		$project_id   = 109;
//		$project_id   = 1;
		$memo_subject = 'Заявка на доступ к системе учёта рабочего времени';
		break;
	case 'request/hpc':
//		$project_id   = NULL; //38;
		$project_id   = 38;
		$memo_subject = 'Запрос на учётную запись к HPC-кластерам';

		$custom_fields_keys = array(2 => 'hdd-amount', 3 => 'cpu-amount', 4 => 'subdivision', 5 => 'scientific-director', 6 => 'ram-amount', 7 => 'preffered-login', 8 => 'ic-bandwidth', 16 => 'contact-email');
		break;
	case 'request/vacation':
		$project_id   = 118;
		$memo_subject = 'Заявление на отпуск';

		switch($_GET['vacation-type']) {
			case 'main':
				$_GET['vacation-type'] = 'ежегодный основной оплачиваемый отпуск';
				$_GET['of-vacation']   = 'ежегодного основного оплачиваемого отпуска';
				break;
			case 'educational':
				$_GET['vacation-type'] = 'учебный отпуск';
				$_GET['of-vacation']   = 'учебного отпуска';
				break;
			case 'free':
				$_GET['vacation-type'] = 'отпуск без сохранения заработной платы';
				$_GET['of-vacation']   = 'отпуска без сохранения заработной платы';
				break;
			case 'parental':
				$_GET['vacation-type'] = 'отпуск по уходу за ребёнком до достижения им возраста трёх лет';
				$_GET['of-vacation']   = 'отпуска по уходу за ребёнком до достижения им возраста трёх лет';
				break;
		}

		$time_from = strtotime($_GET['days-from']);
		$time_to   = strtotime($_GET['days-to']);

		$time_diff = $time_from - $time_to;

		$days_diff = floor(($time_diff / (3600*24)) + 0.5);

		$_GET['days-count'] = ($days_diff + 1);

		break;
	case 'request/voip-transfer-equipment':
		$project_id   = 1;//18;
		$memo_subject = 'Заявка на передачу телефонных аппаратов в ОСП';
		break;
	default:
		die('unknown memo-type');
}

switch ($_GET['file']) {
	case 'request/voip3':
	case 'request/cps':
		if (empty($_GET['subdiv-code'])) {
			if (!empty($_SERVER['HTTP_REFERER'])) {
				header('Location: '.$_SERVER['HTTP_REFERER'].'?errmsg='.urlencode('Не указан код подразделения'));
				exit(0);
			} else
				die('"subdiv-code" is not set');
		}
		$subdivs      = json_decode(file_get_contents('https://cps.mephi.ru/?cmd=getsubdivinfo&s_code='.$_GET['subdiv-code']));
		$subdiv       = $subdivs->result->s0;
		$_GET['subdiv-name'] = $subdiv->name;
		break;
}

foreach($custom_fields_keys as $id => $key)
	$custom_fields[] = array('id' => $id, 'value' => empty($_GET[$key]) ? '0' : $_GET[$key]);

$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');
file_put_contents("request.get", print_r($_GET, true));

#ob_start();
#print_r($_GET);
#$_GET_print_r = ob_get_clean();
$text = '';

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

/*
if (empty($_GET['date-end']))
	$_GET['date-end'] = (date('Y')+1).'-12-31';
*/
foreach (array('fullname' => 40, 'phonenumber' => 20, 'email' => 30, 'appointment' => 40, 'pgp-id' => 12) as $key => $space) {
	if (empty($_GET['signer-'.$key]))
		$_GET['signer-'.$key] = str_pad('', $space, '_');

	if (empty($_GET['user-'.$key]))
		$_GET['user-'.$key]   = str_pad('', $space, '_');

	if (empty($_GET['contact-'.$key]))
		$_GET['contact-'.$key] = $_GET['user-'.$key];
}

foreach (array('work-date-end' => 'Срок окончания работ') as $key => $descr) {
	$_GET[$key.'-descr'] = $descr.': '. 
		( 
			isset($_GET['no-'.$key]) ?
				'Бессрочно'      :
				(isset ($_GET['work-date-end']) ?
					$_GET['work-date-end']  :
					'__.__.____'
				)
		).'.';
}
foreach (array('ram-amount' => 'GB', 'hdd-amount' => 'GB', 'ic-bandwidth' => 'Gbps') as $key => $descr) {
	$_GET[$key.'-descr'] =  empty($_GET[$key]) ? '' : $_GET[$key].' '.$descr;
}

foreach ($_GET as $key => &$value) {
	if (substr($key, -12) == "-other-value")
		if ($_GET[substr($key, 0, strlen($key)-12)] == "other")
			$_GET[substr($key, 0, strlen($key)-12)] = $value;
	$value = str_replace('_', '\_', $value);
}

if (isset($_GET['user-serialized'])) {
	$user_drupal_serialized = base64_decode($_GET['user-serialized']);
	$hash = $_GET['user-signature'];
	$good_signature = 0;
	foreach ($SIGNKEYS as $key) {
		if (sha1($user_drupal_serialized.$key) === $hash) {
			$good_signature = 1;
			break;
		}
	}
	if ($good_signature == 0)
		die('Bad signature');

	$user_drupal = unserialize($user_drupal_serialized);

	$user_redmine = NULL;
	foreach ($redmine_admin->api('user')->all(array('name' => split('@', $user_drupal->user_id)[0]))['users'] as &$_user) {
		if (strtolower($_user['login']).'@mephi.ru' == strtolower($user_drupal->user_id)) {
			$user_redmine = $redmine_admin->api('user')->show($_user['id']);
			break;
		}
	}

	if (is_null($user_redmine))
		die('Cannot find user "'.$user_drupal->name.'" in Redmine');


	$api_key = $user_redmine['user']['api_key'];

//	file_put_contents("/tmp/user", print_r($user_redmine)); die();
	$redmine = new Redmine\Client('https://redmine.ut.mephi.ru', $api_key);
} else
	$redmine = &$redmine_admin;

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

switch ($_GET['file']) {
	case 'request/vacation':
		$header = '\mmheader{Начальнику отдела кадров НИЯУ МИФИ}{}';
		break;
	case 'request/voip-transfer-equipment':
		$header = '\mmfullheader{'.$_GET['subdiv-name'].'}{Начальнику управления информатизации}{Романову Н.Н.}';
		break;
	default:
		$header = '\mmheader{Начальнику управления информатизации}{Н. Н. Романову}';
}
file_put_contents('header.tex',		$header);
file_put_contents('body.tex',		$body);
file_put_contents('footer.tex',		'\mmfooter{'.$_GET['signer-appointment'].'}{'.$_GET{'signer-fullname'}.'}');
file_put_contents('responsible.tex',	'\mmresponsibleskip');

if (!is_null($project_id)) {
	file_put_contents('urlqr.tex',	'');
	exec('make');
	exec('pdftotext memo.pdf');

	$text = file_get_contents('memo.txt');
	unlink('memo.pdf');

	$issue_props = 
		array(
			'project_id'  => $project_id,
			'subject'     => $memo_subject,
			'description' => $text,
		);

	if (!is_null($custom_fields))
		$issue_props['custom_fields'] = $custom_fields;

	$issue = $redmine->api('issue')->create($issue_props);

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

file_put_contents('urlqr.tex',		'\mmredmineurlqr{'.$issue_id.'}');

if (isset($user_redmine['user']['id']))
	$redmine_admin->api('issue')->addWatcher($issue_id, $user_redmine['user']['id']);

switch ($_GET['file']) {
	case 'request/cps':
	case 'request/voip3':
	case 'request/voip-softphone':
		$redmine_admin->api('issue')->addWatcher($issue_id, USERID_NNROMANOV);
		$redmine_admin->api('issue')->addWatcher($issue_id, USERID_DYOKUNEV);
		break;
	case 'request/voip-transfer-equipment':
	case 'request/vacation':
		break;
	default:
		file_put_contents('responsible.tex', '\mmresponsible{'.$_GET['contact-fullname'].'}{'.$_GET{'contact-phonenumber'}.'}{'.$_GET['contact-email'].'}');
}

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

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

$redmine_admin->api('issue')->attach(
	$issue_id,
	array(
		'token'       => $pdf_upload->upload->token,
		'filename'    => 'sluzhebnaya_zapiska.pdf',
		'description' => 'Служебная записка',
		'content_type'=> 'application/pdf'
	)
);

header('Content-type: application/pdf');
if (preg_match('/Firefox/i', $_SERVER['HTTP_USER_AGENT'])) {
	system('convert -density 200 memo.pdf memo.png');
	system('convert -density 200 memo.png memo_png.pdf');
	print file_get_contents('memo_png.pdf');
} else {
	print $pdf_content;
}

?>