genpdf.php
4.06 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
<?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;
?>