/
presenter.php
198 lines (172 loc) · 6.09 KB
/
presenter.php
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
<?php
require_once('common.inc.php');
use mikehaertl\wkhtmlto\Pdf;
define('STEP_COMMIT', 1);
define('STEP_FILES', 2);
define('STEP_DISPLAY', 3);
function isInvisible($path) {
return empty($_REQUEST['ignore_invisible']) || ($_REQUEST['ignore_invisible'] && !preg_match('%^(\..*)|(.*\/\..*)%', $path));
}
function isFiltered($filename) {
return empty($_REQUEST['enable_filter']) || ($_REQUEST['enable_filter'] && preg_match("%{$_REQUEST['regex_filter']}%", $filename));
}
$step = (empty($_REQUEST['step']) ? STEP_COMMIT : $_REQUEST['step']);
$extensions = array(
'tpl' => 'smarty'
);
switch($step) {
case STEP_DISPLAY:
try {
$commit = json_decode(
$github->get($_REQUEST['commit']),
true
);
} catch (Exception $e) {
$smarty->addMessage(
'Problem Retrieving Commit Information',
'There was a problem retrieving the information about your commit. Please make sure that you have given the GitHub user <code>' . $secrets->github->user . '</code> access to your repository.',
NotificationMessage::ERROR
);
$step = STEP_COMMIT;
}
if ($step == STEP_DISPLAY) {
$smarty->assign('title', date('Y-m-d ', strtotime($commit['author']['date'])). strtok($commit['message'], "\n") . " by {$commit['author']['name']}");
$commit['message'] = \Michelf\Markdown::defaultTransform('# ' . $commit['message']);
$smarty->assign('commit', $commit);
$files = array();
foreach ($_REQUEST['files'] as $path => $url) {
$blob = json_decode($github->get($url), true);
$language = preg_replace('/^.*\.(\w+)$/', '$1', $path);
if (array_key_exists($language, $extensions)) {
$language = $extensions[$language];
}
$geshi = new GeSHi(base64_decode($blob['content']), $language);
$geshi->set_header_type(GESHI_HEADER_DIV);
$geshi->enable_line_numbers(GESHI_NORMAL_LINE_NUMBERS);
$geshi->set_line_style('color: lightgray;');
$geshi->set_code_style('color: black;');
$files[$path] = array(
'filename' => basename($path),
'path' => dirname($path),
'content' => '<div class="geshi">' . $geshi->parse_code() . '</div>'
);
}
$smarty->assign('files', $files);
if (empty($_REQUEST['pdf']) || !$_REQUEST['pdf']) {
$smarty->display('display.tpl');
} else {
$pdf = new Pdf([
'print-media-type',
'dpi' => 300, // FIXME may be Mac-specific (cf. https://github.com/wkhtmltopdf/wkhtmltopdf/issues/3241)
'margin-left' => 25.4, // FIXME 25.4mm = 1in
'margin-right' => 25.4,
'margin-top' => 25.4,
'margin-bottom' => 25.4
]);
$pdf->binary = $secrets->wkhtmltopdf;
$pdf->addPage($smarty->fetch('display.tpl'));
$pdf->send($smarty->getTemplateVars('title') . '.pdf');
exit;
}
break;
}
/* flow into STEP_FILES */
case STEP_FILES:
if (empty($_REQUEST['commit_url'])) {
$smarty->addMessage(
'Missing Commit URL',
'You must provide the URL of a specific commit to your repository on GitHub for this to work.',
NotificationMessage::ERROR
);
$step = STEP_COMMIT;
}
if ($step == STEP_FILES) {
/* get the commit information */
try {
$commit = json_decode(
$github->get(
preg_replace(
'%^https://github.com/(.*)/commit/(.*)$%',
'repos/$1/commits/$2',
$_REQUEST['commit_url']
)
),
true
);
} catch (Exception $e) {
$smarty->addMessage(
'Commit URL Problem',
'The URL that your provided for your commit (<a href="' . $_REQUEST['commit_url'] . '">' . $_REQUEST['commit_url'] . '</a>) could not be found on GitHub. Please check to make sure that you are, in fact, providing the URL of a specific commit in your repository (rather than, say, the URL of a particular source file or directory) and that you have given the GitHub user <code>' . $secrets->github->user . '</code> access to your repository.',
NotificationMessage::ERROR
);
$step = STEP_COMMIT;
}
if ($step == STEP_FILES) {
/* get the commit tree */
try {
$commitTree = json_decode(
$github->get(
preg_replace(
'%^https://github.com/(.*)/commit/(.*)$%',
'repos/$1/git/trees/$2',
$_REQUEST['commit_url']
),
array(
'recursive' => true
)
),
true
);
} catch (Exception $e) {
$smarty->addMessage(
'Problem Retrieving File List',
'There was a problem retrieving the list of files changed in <a href="">your commit</a>. Please make sure that you have given the GitHub user <code>' . $secrets->github->user . '</code> access to your repository.',
NotificationMessage::EROR
);
$step = STEP_COMMIT;
}
if ($step == STEP_FILES) {
/* make a list of changed subdirectories of the root (i.e. Eclipse projects) */
$changes = array();
foreach ($commit['files'] as $file) {
if (isInvisible($file['filename'])) {
if(isFiltered(basename($file['filename']))) {
$parts = explode('/', $file['filename']);
if (!in_array($parts[0], $changes)) {
$changes[] = $parts[0];
}
}
}
}
/* make a list of viewable files in the commit tree */
$tree = array();
$currentDir = '@';
foreach ($commitTree['tree'] as $leaf) {
if (isInvisible($leaf['path'])) {
if(isFiltered(basename($leaf['path']))) {
$parts = explode('/', dirname($leaf['path']));
$leaf['changed'] = in_array($parts[0], $changes);
$leaf['filename'] = basename($leaf['path']);
$leaf['path_parts'] = $parts;
$tree[$leaf['path']] = $leaf;
}
}
}
$commit['commit']['message'] = \Michelf\Markdown::defaultTransform('### ' . $commit['commit']['message']);
$smarty->assign('commit', $commit['commit']);
$smarty->assign('tree', $tree);
$smarty->assign('formTemplate', 'files.tpl');
$smarty->assign('formHidden', array('step' => STEP_DISPLAY));
$smarty->display('picker.tpl');
break;
}
}
}
/* flow into STEP_COMMIT */
case STEP_COMMIT:
default;
$smarty->assign('formTemplate', 'commit.tpl');
$smarty->assign('formHidden', array('step' => STEP_FILES));
$smarty->display('picker.tpl');
}
?>