-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathconvert.php
173 lines (131 loc) · 4.64 KB
/
convert.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
<?php
require "app/Init.php";
require "app/functions.php";
if ($_SERVER['REQUEST_METHOD'] != 'POST') {
header("Location: http://" . $_SERVER['HTTP_HOST'] . "/");
}
emptyXPICache();
try {
$url = isset($_POST['url']) ? trim($_POST['url']) : null;
$uploadFileName = (empty($_FILES) || empty($_FILES['xpi']['name'])) ? '' : $_FILES['xpi']['name'];
if (!$uploadFileName && !$url) {
throw new Exception("No file to process");
}
$dirPart = uniqid("", true);
$tmpDir = "tmp/convert/$dirPart";
mkdir($tmpDir);
$tmpSourceDir = "$tmpDir/source";
$tmpDestDir = "$tmpDir/dest";
mkdir($tmpSourceDir);
mkdir($tmpDestDir);
$tmpFile = "$tmpSourceDir/$uploadFileName";
$maxFileSize = 16 * 1024 * 1024;
$logger = new ConversionLogger;
$logData = array();
if (!empty($_POST['installButton'])) {
$logData['button'] = 'inst';
} elseif (!empty($_POST['detailsButton'])) {
$logData['button'] = 'det';
}
if ($uploadFileName) {
$logData += array(
'url' => $uploadFileName,
'file_length' => $_FILES['xpi']['size'],
);
$logger->log($logData);
if (!@move_uploaded_file($_FILES['xpi']['tmp_name'], $tmpFile)) {
throw new Exception("Error moving file to temporary folder");
}
if (filesize($tmpFile) > $maxFileSize) {
unlink($tmpFile);
$maxMB = round($maxFileSize / 1024 / 1024, 1);
throw new Exception("Input file too large. Maximum $maxMB MB is allowed");
}
} elseif ($url) {
$logData += array(
'url' => $url,
);
$logger->log($logData);
$ag = new AMOGrabber($maxFileSize);
$tmpFile = $ag->fetch($url, $tmpSourceDir);
}
$startTime = microtime(true);
$conv = new AddOnConverter($tmpFile);
// pass options from form to converter object
$conv->maxVersionStr = (string) substr(trim(@$_POST['maxVersion']), 0, 10);
$conv->appendName = (string) substr(trim(@$_POST['appendName']), 0, 500);
$conv->convertChromeUrls = !empty($_POST['convertChromeUrls']);
$conv->convertChromeURLsInExt = array();
if (isset($_POST['convertChromeExtensions'])
&& is_array($_POST['convertChromeExtensions'])
&& count($_POST['convertChromeExtensions'] < 30)
) {
$conv->convertChromeURLsInExt = $_POST['convertChromeExtensions'];
}
$conv->convertManifest = !empty($_POST['convertManifest']);
$conv->convertPageInfoChrome = !empty($_POST['convertPageInfoChrome']);
$conv->xulIds = !empty($_POST['xulIds']);
$conv->jsShortcuts = !empty($_POST['jsShortcuts']);
$conv->replaceEntities = !empty($_POST['replaceEntities']);
$conv->jsKeywords = !empty($_POST['jsKeywords']);
$destFile = $conv->convert($tmpDestDir);
$result = $conv->getLogMessages();
$warnings = $conv->getWarnings();
$duration = number_format(microtime(true) - $startTime, 2, '.', '');
unlink($tmpFile);
$addOnInfo = $conv->getAddOnInfo();
$logger->log(array(
"addon_name" => trim($addOnInfo['name'] . " " . $addOnInfo['version']),
"duration" => $duration,
));
} catch (Exception $ex) {
$error = $ex->getMessage();
include "index.php";
exit;
}
if (!empty($_POST['installButton']) && $addOnInfo['type'] != 8) {
// install directly but not for language packs (type=8)
header("HTTP/1.1 303 See Other");
header("Location: http://$_SERVER[HTTP_HOST]/install.php?file=" . urlencode($destFile));
exit;
}
?>
<? include "templates/header.php" ?>
<h1 class="addon-name">
<?= htmlspecialchars($addOnInfo['name']) ?>
<span class="version"><?= htmlspecialchars($addOnInfo['version']) ?></span>
</h1>
<h2>Conversion Results (click on file names to see changes):</h2>
<? if ($destFile): ?>
<? if ($warnings): ?>
<? foreach ($warnings as $warning): ?>
<div class="warning"><strong>Warning!</strong> <?=$warning ?></div>
<? endforeach;?>
<? endif ?>
<ol>
<? foreach ($result as $file => $messages): ?>
<li><?=makeLinkToDiff($file, $dirPart) ?>
<ul>
<? foreach ($messages as $msg): ?>
<li><?=$msg; ?></li>
<? endforeach ?>
</ul>
</li>
<? endforeach ?>
</ol>
<h2>Your converted add-on is available here for download.</h2>
<p style="font-size: 75%">Left-click to install, or right click -> <em>Save Link Target As</em> to download:</p>
<p>
<a href="<?=htmlspecialchars($destFile) ?>" class="download"><?=htmlspecialchars(basename($destFile)) ?></a>
—
<span class="filesize">
<?=round(filesize($destFile) / 1024) ?> KB
</span>
</p>
<? else: ?>
<p>I didn't find anything to convert in this add-on.</p>
<? endif ?>
<br>
<hr>
<p style="margin-top: 1em"><a href=".">« perform another conversion</a></p>
<? include "templates/footer.php" ?>