genpdf.php
12.5 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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
<?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;
}
?>